diff --git a/lib/Cron/Workers/StatsNotifications/Worker.php b/lib/Cron/Workers/StatsNotifications/Worker.php index aab0c65534..d3a1725f90 100644 --- a/lib/Cron/Workers/StatsNotifications/Worker.php +++ b/lib/Cron/Workers/StatsNotifications/Worker.php @@ -90,9 +90,11 @@ class Worker { private function getNewsletter(ScheduledTask $task) { $statsNotificationModel = $task->statsNotification()->findOne(); - return $statsNotificationModel - ->newsletter() - ->findOne() + $newsletter = $statsNotificationModel->newsletter()->findOne(); + if(!$newsletter) { + throw new \Exception('Newsletter not found'); + } + return $newsletter ->withSendingQueue() ->withTotalSent() ->withStatistics(); diff --git a/lib/Subscribers/NewSubscriberNotificationMailer.php b/lib/Subscribers/NewSubscriberNotificationMailer.php index 71034eb9d0..f96870c5da 100644 --- a/lib/Subscribers/NewSubscriberNotificationMailer.php +++ b/lib/Subscribers/NewSubscriberNotificationMailer.php @@ -6,6 +6,7 @@ use MailPoet\Config\Renderer; use MailPoet\Models\Segment; use MailPoet\Models\Setting; use MailPoet\Models\Subscriber; +use MailPoet\WP\Functions; class NewSubscriberNotificationMailer { @@ -18,11 +19,15 @@ class NewSubscriberNotificationMailer { /** @var \MailPoet\Mailer\Mailer */ private $mailer; + /** @var Functions */ + private $wordpress_functions; + /** * @param \MailPoet\Mailer\Mailer|null $mailer * @param Renderer|null $renderer + * @param Functions|null $wordpress_functions */ - function __construct($mailer = null, $renderer = null) { + function __construct($mailer = null, $renderer = null, $wordpress_functions = null) { if($renderer) { $this->renderer = $renderer; } else { @@ -30,6 +35,11 @@ class NewSubscriberNotificationMailer { $debugging = WP_DEBUG; $this->renderer = new Renderer($caching, $debugging); } + if($wordpress_functions) { + $this->wordpress_functions = $wordpress_functions; + } else { + $this->wordpress_functions = new Functions(); + } if($mailer) { $this->mailer = $mailer; } else { @@ -75,7 +85,7 @@ class NewSubscriberNotificationMailer { } private function constructSenderEmail() { - $url_parts = parse_url(home_url()); + $url_parts = parse_url($this->wordpress_functions->homeUrl()); $site_name = strtolower($url_parts['host']); if(substr($site_name, 0, 4) === 'www.') { $site_name = substr($site_name, 4); diff --git a/lib/WP/Functions.php b/lib/WP/Functions.php index a98c359f44..9a39ff27e1 100644 --- a/lib/WP/Functions.php +++ b/lib/WP/Functions.php @@ -28,6 +28,10 @@ class Functions { return call_user_func_array('current_time', func_get_args()); } + function homeUrl() { + return call_user_func_array('home_url', func_get_args()); + } + function getImageInfo($id) { /* * In some cases wp_get_attachment_image_src ignore the second parameter diff --git a/tests/integration/Cron/CronHelperTest.php b/tests/integration/Cron/CronHelperTest.php index e3283b16ea..5b834b68de 100644 --- a/tests/integration/Cron/CronHelperTest.php +++ b/tests/integration/Cron/CronHelperTest.php @@ -17,6 +17,10 @@ class CronHelperTest extends \MailPoetTest { Setting::setValue('cron_trigger', array( 'method' => 'none' )); + Setting::setValue('sender', array( + 'name' => 'John Doe', + 'address' => 'john.doe@example.com' + )); } function testItDefinesConstants() { diff --git a/tests/integration/Cron/DaemonHttpRunnerTest.php b/tests/integration/Cron/DaemonHttpRunnerTest.php index 2efb6cb842..5a80a7546c 100644 --- a/tests/integration/Cron/DaemonHttpRunnerTest.php +++ b/tests/integration/Cron/DaemonHttpRunnerTest.php @@ -22,27 +22,25 @@ class DaemonHttpRunnerTest extends \MailPoetTest { } function testItDoesNotRunWithoutRequestData() { - $daemon = Stub::construct( - new DaemonHttpRunner(new Daemon(new WorkersFactory(new SendingErrorHandler()))), - array(), - array( + $daemon = Stub::make( + DaemonHttpRunner::class, + [ 'abortWithError' => function($message) { return $message; } - ) + ] ); expect($daemon->run(false))->equals('Invalid or missing request data.'); } function testItDoesNotRunWhenThereIsInvalidOrMissingToken() { - $daemon = Stub::construct( - new DaemonHttpRunner(new Daemon(new WorkersFactory(new SendingErrorHandler()))), - array(), - array( + $daemon = Stub::make( + DaemonHttpRunner::class, + [ 'abortWithError' => function($message) { return $message; } - ) + ] ); $daemon->settings_daemon_data = array( 'token' => 123 @@ -54,14 +52,19 @@ class DaemonHttpRunnerTest extends \MailPoetTest { $data = array( 'token' => 123 ); - $daemon = Stub::construct(Daemon::class, [new WorkersFactory(new SendingErrorHandler())], array( - 'executeScheduleWorker' => function() { - throw new \Exception('Message'); - }, - 'executeQueueWorker' => function() { - throw new \Exception(); - }, - ), $this); + $daemon = Stub::make( + Daemon::class, + [ + 'executeScheduleWorker' => function() { + throw new \Exception('Message'); + }, + 'executeQueueWorker' => function() { + throw new \Exception(); + }, + 'executeMigrationWorker' => null, + 'executeStatsNotificationsWorker' => null, + ] + ); $daemon_http_runner = Stub::make(new DaemonHttpRunner($daemon), array( 'pauseExecution' => null, 'callSelf' => null @@ -74,16 +77,14 @@ class DaemonHttpRunnerTest extends \MailPoetTest { } function testItCanPauseExecution() { - $daemon = Stub::construct(Daemon::class, [new WorkersFactory(new SendingErrorHandler())], array( - 'executeScheduleWorker' => null, - 'executeQueueWorker' => null, - ), $this); - $daemon_http_runner = Stub::make(new DaemonHttpRunner($daemon), array( + $daemon = Stub::makeEmpty(Daemon::class); + $daemon_http_runner = Stub::make(DaemonHttpRunner::class, array( 'pauseExecution' => Expected::exactly(1, function($pause_delay) { expect($pause_delay)->lessThan(CronHelper::DAEMON_EXECUTION_LIMIT); expect($pause_delay)->greaterThan(CronHelper::DAEMON_EXECUTION_LIMIT - 1); }), - 'callSelf' => null + 'callSelf' => null, + 'terminateRequest' => null, ), $this); $data = array( 'token' => 123 @@ -107,7 +108,7 @@ class DaemonHttpRunnerTest extends \MailPoetTest { 'token' => 123 ); Setting::setValue(CronHelper::DAEMON_SETTING, $data); - $daemon->__construct(new Daemon(new WorkersFactory(new SendingErrorHandler()))); + $daemon->__construct(Stub::makeEmpty(Daemon::class)); $daemon->run($data); } @@ -127,7 +128,7 @@ class DaemonHttpRunnerTest extends \MailPoetTest { 'token' => 123 ); Setting::setValue(CronHelper::DAEMON_SETTING, $data); - $daemon->__construct(new Daemon(new WorkersFactory(new SendingErrorHandler()))); + $daemon->__construct(Stub::makeEmpty(Daemon::class)); $daemon->run($data); $data_after_run = Setting::getValue(CronHelper::DAEMON_SETTING); expect($data_after_run['token'], 567); @@ -145,7 +146,7 @@ class DaemonHttpRunnerTest extends \MailPoetTest { 'status' => CronHelper::DAEMON_STATUS_INACTIVE, ]; Setting::setValue(CronHelper::DAEMON_SETTING, $data); - $daemon->__construct(new Daemon(new WorkersFactory(new SendingErrorHandler()))); + $daemon->__construct(Stub::makeEmpty(Daemon::class)); $daemon->run($data); } @@ -154,25 +155,31 @@ class DaemonHttpRunnerTest extends \MailPoetTest { 'executeScheduleWorker' => null, 'executeQueueWorker' => null, 'pauseExecution' => null, - 'callSelf' => null + 'callSelf' => null, + 'terminateRequest' => null, ), $this); $data = array( 'token' => 123 ); Setting::setValue(CronHelper::DAEMON_SETTING, $data); - $daemon_http_runner->__construct(new Daemon(new WorkersFactory(new SendingErrorHandler()))); + $daemon_http_runner->__construct(Stub::makeEmptyExcept(Daemon::class, 'run')); $daemon_http_runner->run($data); $updated_daemon = Setting::getValue(CronHelper::DAEMON_SETTING); expect($updated_daemon['token'])->equals($daemon_http_runner->token); } function testItUpdatesTimestampsDuringExecution() { - $daemon = Stub::construct(Daemon::class, [new WorkersFactory(new SendingErrorHandler())], array( - 'executeScheduleWorker' => function() { - sleep(2); - }, - 'executeQueueWorker' => null, - ), $this); + $daemon = Stub::make(Daemon::class, [ + 'executeScheduleWorker' => function() { + sleep(2); + }, + 'executeQueueWorker' => function() { + throw new \Exception(); + }, + 'executeMigrationWorker' => null, + 'executeStatsNotificationsWorker' => null, + ] + ); $daemon_http_runner = Stub::make(new DaemonHttpRunner($daemon), array( 'pauseExecution' => null, 'callSelf' => null @@ -201,12 +208,13 @@ class DaemonHttpRunnerTest extends \MailPoetTest { 'executeQueueWorker' => Expected::exactly(1), // daemon should call itself 'callSelf' => Expected::exactly(1), + 'terminateRequest' => null, ), $this); $data = array( 'token' => 123 ); Setting::setValue(CronHelper::DAEMON_SETTING, $data); - $daemon->__construct(new Daemon(new WorkersFactory(new SendingErrorHandler()))); + $daemon->__construct(Stub::makeEmptyExcept(Daemon::class, 'run')); $daemon->run($data); expect(ignore_user_abort())->equals(1); } diff --git a/tests/integration/Cron/DaemonTest.php b/tests/integration/Cron/DaemonTest.php index 1dfc4433f0..0e0cee380c 100644 --- a/tests/integration/Cron/DaemonTest.php +++ b/tests/integration/Cron/DaemonTest.php @@ -13,11 +13,14 @@ use MailPoet\Models\Setting; class DaemonTest extends \MailPoetTest { function testItCanExecuteWorkers() { - $daemon = Stub::construct(Daemon::class, [new WorkersFactory(new SendingErrorHandler())], array( + $daemon = Stub::make(Daemon::class, array( 'executeScheduleWorker' => Expected::exactly(1), 'executeQueueWorker' => Expected::exactly(1), - 'pauseExecution' => null, - 'callSelf' => null + 'executeMigrationWorker' => null, + 'executeStatsNotificationsWorker' => null, + 'executeSendingServiceKeyCheckWorker' => null, + 'executePremiumKeyCheckWorker' => null, + 'executeBounceWorker' => null, ), $this); $data = array( 'token' => 123 @@ -27,13 +30,15 @@ class DaemonTest extends \MailPoetTest { } function testItCanRun() { - $daemon = Stub::construct(Daemon::class, [new WorkersFactory(new SendingErrorHandler())], array( - 'pauseExecution' => null, + $daemon = Stub::make(Daemon::class, array( // workers should be executed 'executeScheduleWorker' => Expected::exactly(1), 'executeQueueWorker' => Expected::exactly(1), - // daemon should call itself - 'callSelf' => Expected::exactly(1), + 'executeMigrationWorker' => Expected::exactly(1), + 'executeStatsNotificationsWorker' => Expected::exactly(1), + 'executeSendingServiceKeyCheckWorker' => Expected::exactly(1), + 'executePremiumKeyCheckWorker' => Expected::exactly(1), + 'executeBounceWorker' => Expected::exactly(1) ), $this); $data = array( 'token' => 123 diff --git a/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php b/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php index ff92b07467..ac249ea5cc 100644 --- a/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php +++ b/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php @@ -17,7 +17,7 @@ use PHPUnit\Framework\MockObject\MockObject; class WorkerTest extends \MailPoetTest { - /** @var Scheduler */ + /** @var Worker */ private $stats_notifications; /** @var MockObject */ @@ -27,6 +27,9 @@ class WorkerTest extends \MailPoetTest { private $renderer; function _before() { + \ORM::raw_execute('TRUNCATE ' . Newsletter::$_table); + \ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table); + \ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table); $this->mailer = $this->createMock(Mailer::class); $this->renderer = $this->createMock(Renderer::class); $this->stats_notifications = new Worker($this->mailer, $this->renderer); diff --git a/tests/integration/Subscribers/NewSubscriberNotificationMailerTest.php b/tests/integration/Subscribers/NewSubscriberNotificationMailerTest.php index d3da5fa3b3..9d2cb369b7 100644 --- a/tests/integration/Subscribers/NewSubscriberNotificationMailerTest.php +++ b/tests/integration/Subscribers/NewSubscriberNotificationMailerTest.php @@ -8,6 +8,7 @@ use MailPoet\Mailer\Mailer; use MailPoet\Models\Segment; use MailPoet\Models\Setting; use MailPoet\Models\Subscriber; +use MailPoet\WP\Functions; class NewSubscriberNotificationMailerTest extends \MailPoetTest { @@ -85,7 +86,6 @@ class NewSubscriberNotificationMailerTest extends \MailPoetTest { function testItRemovesWwwFromSenderAddress() { Setting::setValue(NewSubscriberNotificationMailer::SETTINGS_KEY, ['enabled' => true,'address' => 'a@b.c']); - update_option( 'home', 'http://www.example.com/xyz' ); $mailer = Stub::makeEmpty(Mailer::class, [ 'getSenderNameAndAddress' => @@ -96,7 +96,14 @@ class NewSubscriberNotificationMailerTest extends \MailPoetTest { }), ], $this); - $service = new NewSubscriberNotificationMailer($mailer); + $functions = Stub::makeEmpty(Functions::class, [ + 'homeUrl' => + Expected::once(function() { + return 'http://www.example.com/xyz'; + }), + ], $this); + + $service = new NewSubscriberNotificationMailer($mailer, null, $functions); $service->send($this->subscriber, $this->segments); } }