From d40747e0acb2a4310e939031d6ffa7597f6cecfd Mon Sep 17 00:00:00 2001 From: Amine Ben hammou Date: Fri, 13 Sep 2019 02:32:59 +0100 Subject: [PATCH] Add meta when sending stats notifications [MAILPOET-2333] --- .../Workers/StatsNotifications/AutomatedEmails.php | 11 ++++++++++- lib/Cron/Workers/StatsNotifications/Worker.php | 11 ++++++++++- lib/Cron/Workers/WorkersFactory.php | 12 +++++++++--- lib/Mailer/MetaInfo.php | 4 ++++ .../StatsNotifications/AutomatedEmailsTest.php | 2 ++ .../Cron/Workers/StatsNotifications/WorkerTest.php | 3 ++- tests/unit/Mailer/MetaInfoTest.php | 9 +++++++++ 7 files changed, 46 insertions(+), 6 deletions(-) diff --git a/lib/Cron/Workers/StatsNotifications/AutomatedEmails.php b/lib/Cron/Workers/StatsNotifications/AutomatedEmails.php index beb46dcdee..ee723e5348 100644 --- a/lib/Cron/Workers/StatsNotifications/AutomatedEmails.php +++ b/lib/Cron/Workers/StatsNotifications/AutomatedEmails.php @@ -7,6 +7,7 @@ use MailPoet\Config\Renderer; use MailPoet\Cron\Workers\SimpleWorker; use MailPoet\Features\FeaturesController; use MailPoet\Mailer\Mailer; +use MailPoet\Mailer\MetaInfo; use MailPoet\Models\Newsletter; use MailPoet\Models\ScheduledTask; use MailPoet\Settings\SettingsController; @@ -28,6 +29,9 @@ class AutomatedEmails extends SimpleWorker { /** @var WCHelper */ private $woocommerce_helper; + /** @var MetaInfo */ + private $mailerMetaInfo; + /** @var float */ public $timer; @@ -36,6 +40,7 @@ class AutomatedEmails extends SimpleWorker { Renderer $renderer, SettingsController $settings, WCHelper $woocommerce_helper, + MetaInfo $mailerMetaInfo, $timer = false ) { parent::__construct($timer); @@ -43,6 +48,7 @@ class AutomatedEmails extends SimpleWorker { $this->settings = $settings; $this->renderer = $renderer; $this->woocommerce_helper = $woocommerce_helper; + $this->mailerMetaInfo = $mailerMetaInfo; $this->timer = $timer ?: microtime(true); } @@ -71,7 +77,10 @@ class AutomatedEmails extends SimpleWorker { $settings = $this->settings->get(Worker::SETTINGS_KEY); $newsletters = $this->getNewsletters(); if ($newsletters) { - $this->mailer->send($this->constructNewsletter($newsletters), $settings['address']); + $extra_params = [ + 'meta' => $this->mailerMetaInfo->getStatsNotificationMetaInfo(), + ]; + $this->mailer->send($this->constructNewsletter($newsletters), $settings['address'], $extra_params); } } catch (\Exception $e) { if (WP_DEBUG) { diff --git a/lib/Cron/Workers/StatsNotifications/Worker.php b/lib/Cron/Workers/StatsNotifications/Worker.php index cb605c63fd..4ad41f453f 100644 --- a/lib/Cron/Workers/StatsNotifications/Worker.php +++ b/lib/Cron/Workers/StatsNotifications/Worker.php @@ -6,6 +6,7 @@ use Carbon\Carbon; use MailPoet\Config\Renderer; use MailPoet\Cron\CronHelper; use MailPoet\Mailer\Mailer; +use MailPoet\Mailer\MetaInfo; use MailPoet\Models\Newsletter; use MailPoet\Models\NewsletterLink; use MailPoet\Models\ScheduledTask; @@ -35,11 +36,15 @@ class Worker { /** @var WCHelper */ private $woocommerce_helper; + /** @var MetaInfo */ + private $mailerMetaInfo; + function __construct( Mailer $mailer, Renderer $renderer, SettingsController $settings, WCHelper $woocommerce_helper, + MetaInfo $mailerMetaInfo, $timer = false ) { $this->timer = $timer ?: microtime(true); @@ -47,6 +52,7 @@ class Worker { $this->mailer = $mailer; $this->settings = $settings; $this->woocommerce_helper = $woocommerce_helper; + $this->mailerMetaInfo = $mailerMetaInfo; } /** @throws \Exception */ @@ -54,7 +60,10 @@ class Worker { $settings = $this->settings->get(self::SETTINGS_KEY); foreach (self::getDueTasks() as $task) { try { - $this->mailer->send($this->constructNewsletter($task), $settings['address']); + $extra_params = [ + 'meta' => $this->mailerMetaInfo->getStatsNotificationMetaInfo(), + ]; + $this->mailer->send($this->constructNewsletter($task), $settings['address'], $extra_params); } catch (\Exception $e) { if (WP_DEBUG) { throw $e; diff --git a/lib/Cron/Workers/WorkersFactory.php b/lib/Cron/Workers/WorkersFactory.php index b7dcd7468e..0d6f15bd74 100644 --- a/lib/Cron/Workers/WorkersFactory.php +++ b/lib/Cron/Workers/WorkersFactory.php @@ -20,6 +20,7 @@ use MailPoet\Services\AuthorizedEmailsController; use MailPoet\Statistics\Track\WooCommercePurchases; use MailPoet\WooCommerce\Helper as WooCommerceHelper; use MailPoet\Mailer\Mailer; +use MailPoet\Mailer\MetaInfo; use MailPoet\Settings\SettingsController; use MailPoet\Subscribers\InactiveSubscribersController; use MailPoet\WP\Functions as WPFunctions; @@ -61,6 +62,9 @@ class WorkersFactory { /** @var SubscribersFinder */ private $subscribers_finder; + /** @var MetaInfo */ + private $mailerMetaInfo; + public function __construct( SendingErrorHandler $sending_error_handler, StatsNotificationScheduler $statsNotificationsScheduler, @@ -72,7 +76,8 @@ class WorkersFactory { WooCommerceHelper $woocommerce_helper, WooCommercePurchases $woocommerce_purchases, AuthorizedEmailsController $authorized_emails_controller, - SubscribersFinder $subscribers_finder + SubscribersFinder $subscribers_finder, + MetaInfo $mailerMetaInfo ) { $this->sending_error_handler = $sending_error_handler; $this->statsNotificationsScheduler = $statsNotificationsScheduler; @@ -85,6 +90,7 @@ class WorkersFactory { $this->woocommerce_purchases = $woocommerce_purchases; $this->authorized_emails_controller = $authorized_emails_controller; $this->subscribers_finder = $subscribers_finder; + $this->mailerMetaInfo = $mailerMetaInfo; } /** @return SchedulerWorker */ @@ -99,12 +105,12 @@ class WorkersFactory { /** @return StatsNotificationsWorker */ function createStatsNotificationsWorker($timer) { - return new StatsNotificationsWorker($this->mailer, $this->renderer, $this->settings, $this->woocommerce_helper, $timer); + return new StatsNotificationsWorker($this->mailer, $this->renderer, $this->settings, $this->woocommerce_helper, $this->mailerMetaInfo, $timer); } /** @return StatsNotificationsWorkerForAutomatedEmails */ function createStatsNotificationsWorkerForAutomatedEmails($timer) { - return new StatsNotificationsWorkerForAutomatedEmails($this->mailer, $this->renderer, $this->settings, $this->woocommerce_helper, $timer); + return new StatsNotificationsWorkerForAutomatedEmails($this->mailer, $this->renderer, $this->settings, $this->woocommerce_helper, $this->mailerMetaInfo, $timer); } /** @return SendingServiceKeyCheckWorker */ diff --git a/lib/Mailer/MetaInfo.php b/lib/Mailer/MetaInfo.php index 3f05ecda06..a8a14394ac 100644 --- a/lib/Mailer/MetaInfo.php +++ b/lib/Mailer/MetaInfo.php @@ -10,6 +10,10 @@ class MetaInfo { return $this->makeMetaInfo('preview', 'unknown', 'administrator'); } + function getStatsNotificationMetaInfo() { + return $this->makeMetaInfo('email_stats_notification', 'unknown', 'administrator'); + } + private function makeMetaInfo($email_type, $subscriber_status, $subscriber_source) { return [ 'email_type' => $email_type, diff --git a/tests/integration/Cron/Workers/StatsNotifications/AutomatedEmailsTest.php b/tests/integration/Cron/Workers/StatsNotifications/AutomatedEmailsTest.php index 3e483ac3d7..a0d1ff1f29 100644 --- a/tests/integration/Cron/Workers/StatsNotifications/AutomatedEmailsTest.php +++ b/tests/integration/Cron/Workers/StatsNotifications/AutomatedEmailsTest.php @@ -4,6 +4,7 @@ namespace MailPoet\Cron\Workers\StatsNotifications; use MailPoet\Config\Renderer; use MailPoet\Mailer\Mailer; +use MailPoet\Mailer\MetaInfo; use MailPoet\Models\Newsletter; use MailPoet\Models\ScheduledTask; use MailPoet\Settings\SettingsController; @@ -43,6 +44,7 @@ class AutomatedEmailsTest extends \MailPoetTest { $this->renderer, $this->settings, $this->makeEmpty(WCHelper::class), + new MetaInfo, ]) ->setMethods(['getNewsletters']) ->getMock(); diff --git a/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php b/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php index 83e66912af..92a850e546 100644 --- a/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php +++ b/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php @@ -4,6 +4,7 @@ namespace MailPoet\Cron\Workers\StatsNotifications; use MailPoet\Config\Renderer; use MailPoet\Mailer\Mailer; +use MailPoet\Mailer\MetaInfo; use MailPoet\Models\Newsletter; use MailPoet\Models\NewsletterLink; use MailPoet\Models\ScheduledTask; @@ -45,7 +46,7 @@ class WorkerTest extends \MailPoetTest { $this->mailer = $this->createMock(Mailer::class); $this->renderer = $this->createMock(Renderer::class); $this->settings = new SettingsController(); - $this->stats_notifications = new Worker($this->mailer, $this->renderer, $this->settings, $this->makeEmpty(WCHelper::class)); + $this->stats_notifications = new Worker($this->mailer, $this->renderer, $this->settings, $this->makeEmpty(WCHelper::class), new MetaInfo); $this->settings->set(Worker::SETTINGS_KEY, [ 'enabled' => true, 'address' => 'email@example.com', diff --git a/tests/unit/Mailer/MetaInfoTest.php b/tests/unit/Mailer/MetaInfoTest.php index 8499760bb8..f6c9b04c9f 100644 --- a/tests/unit/Mailer/MetaInfoTest.php +++ b/tests/unit/Mailer/MetaInfoTest.php @@ -27,4 +27,13 @@ class MetaInfoTest extends \MailPoetUnitTest { 'subscriber_source' => 'administrator', ]); } + + function testItGetsMetaInfoForStatsNotifications() { + expect($this->meta->getStatsNotificationMetaInfo())->equals([ + 'email_type' => 'email_stats_notification', + 'subscriber_status' => 'unknown', + 'subscriber_source' => 'administrator', + ]); + } + }