From bb7ab59ed5d0f7d7df62b037a7c96b82417c6bfb Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Tue, 13 Aug 2024 14:36:05 +0200 Subject: [PATCH] Improve readability of scheduler code [MAILPOET-6142] --- .../lib/Newsletter/Scheduler/Scheduler.php | 13 ++++++------ mailpoet/lib/WP/Functions.php | 4 ++++ .../Scheduler/PostNotificationTest.php | 14 ++++++------- .../Newsletter/Scheduler/SchedulerTest.php | 20 +++++++++++-------- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/mailpoet/lib/Newsletter/Scheduler/Scheduler.php b/mailpoet/lib/Newsletter/Scheduler/Scheduler.php index ba839c96e7..c16a01e3e1 100644 --- a/mailpoet/lib/Newsletter/Scheduler/Scheduler.php +++ b/mailpoet/lib/Newsletter/Scheduler/Scheduler.php @@ -34,13 +34,13 @@ class Scheduler { public function getPreviousRunDate($schedule) { // User enters time in WordPress site timezone, but we need to calculate it in UTC before we save it to DB - // 1) As the initial time we use time in site timezone (gmt is false) + // 1) As the initial time we use time in site timezone via current_datetime // 2) We use CronExpression to calculate previous run (still in site's timezone) // 3) We convert the calculated time to UTC - $fromTimestamp = $this->wp->currentTime('timestamp', false); + $from = $this->wp->currentDatetime(); try { $schedule = \Cron\CronExpression::factory($schedule); - $previousRunDate = $schedule->getPreviousRunDate(Carbon::createFromTimestamp($fromTimestamp, $this->wp->wpTimezone())); + $previousRunDate = $schedule->getPreviousRunDate(Carbon::instance($from)); $previousRunDate->setTimezone(new \DateTimeZone('UTC')); $previousRunDate = $previousRunDate->format('Y-m-d H:i:s'); } catch (\Exception $e) { @@ -88,13 +88,14 @@ class Scheduler { */ public function getNextRunDateTime($schedule) { // User enters time in WordPress site timezone, but we need to calculate it in UTC before we save it to DB - // 1) As the initial time we use time in site timezone (gmt is false) + // 1) As the initial time we use time in site timezone via current_datetime // 2) We use CronExpression to calculate next run (still in site's timezone) // 3) We convert the calculated time to UTC - $fromTimestamp = $this->wp->currentTime('timestamp', false); + //$fromTimestamp = $this->wp->currentTime('timestamp', false); + $from = $this->wp->currentDatetime(); try { $schedule = \Cron\CronExpression::factory($schedule); - $nextRunDate = $schedule->getNextRunDate(Carbon::createFromTimestamp($fromTimestamp, $this->wp->wpTimezone())); + $nextRunDate = $schedule->getNextRunDate(Carbon::instance($from)); $nextRunDate->setTimezone(new \DateTimeZone('UTC')); } catch (\Exception $e) { $nextRunDate = false; diff --git a/mailpoet/lib/WP/Functions.php b/mailpoet/lib/WP/Functions.php index a9ee7d6c7e..10dc63ce73 100644 --- a/mailpoet/lib/WP/Functions.php +++ b/mailpoet/lib/WP/Functions.php @@ -140,6 +140,10 @@ class Functions { return current_time($type, $gmt); } + public function currentDatetime() { + return current_datetime(); + } + public function wpTimezone() { return wp_timezone(); } diff --git a/mailpoet/tests/integration/Newsletter/Scheduler/PostNotificationTest.php b/mailpoet/tests/integration/Newsletter/Scheduler/PostNotificationTest.php index aec7d3fa67..06a614658a 100644 --- a/mailpoet/tests/integration/Newsletter/Scheduler/PostNotificationTest.php +++ b/mailpoet/tests/integration/Newsletter/Scheduler/PostNotificationTest.php @@ -188,7 +188,7 @@ class PostNotificationTest extends \MailPoetTest { $scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE); $this->assertInstanceOf(NewsletterOptionEntity::class, $scheduleOption); - $scheduler = $this->getSchedulerWithMockedTime(1483275600); // Sunday, 1 January 2017 @ 1:00pm (UTC) + $scheduler = $this->getSchedulerWithMockedTime('2017-01-01 13:00+00:00'); // Sunday, 1 January 2017 @ 1:00pm (UTC) verify($scheduler->getNextRunDate($scheduleOption->getValue())) ->equals('2017-01-01 14:00:00'); @@ -228,7 +228,7 @@ class PostNotificationTest extends \MailPoetTest { $scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE); $this->assertInstanceOf(NewsletterOptionEntity::class, $scheduleOption); - $scheduler = $this->getSchedulerWithMockedTime(1483275600); // Sunday, 1 January 2017 @ 1:00pm (UTC) + $scheduler = $this->getSchedulerWithMockedTime('2017-01-01 13:00+00:00'); // Sunday, 1 January 2017 @ 1:00pm (UTC) verify($scheduler->getNextRunDate($scheduleOption->getValue())) ->equals('2017-01-03 14:00:00'); @@ -268,7 +268,7 @@ class PostNotificationTest extends \MailPoetTest { $this->postNotificationScheduler->processPostNotificationSchedule($newsletter); $scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE); $this->assertInstanceOf(NewsletterOptionEntity::class, $scheduleOption); - $scheduler = $this->getSchedulerWithMockedTime(1483275600); // Sunday, 1 January 2017 @ 1:00pm (UTC) + $scheduler = $this->getSchedulerWithMockedTime('2017-01-01 13:00+00:00'); // Sunday, 1 January 2017 @ 1:00pm (UTC) verify($scheduler->getNextRunDate($scheduleOption->getValue())) ->equals('2017-01-19 14:00:00'); @@ -307,7 +307,7 @@ class PostNotificationTest extends \MailPoetTest { $this->postNotificationScheduler->processPostNotificationSchedule($newsletter); $scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE); $this->assertInstanceOf(NewsletterOptionEntity::class, $scheduleOption); - $scheduler = $this->getSchedulerWithMockedTime(1485694800);// Sunday, 29 January 2017 @ 1:00pm (UTC) + $scheduler = $this->getSchedulerWithMockedTime('2017-01-29 13:00+00:00');// Sunday, 29 January 2017 @ 1:00pm (UTC) verify($scheduler->getNextRunDate($scheduleOption->getValue())) ->equals('2017-02-25 14:00:00'); @@ -346,7 +346,7 @@ class PostNotificationTest extends \MailPoetTest { $this->postNotificationScheduler->processPostNotificationSchedule($newsletter); $scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE); $this->assertInstanceOf(NewsletterOptionEntity::class, $scheduleOption); - $scheduler = $this->getSchedulerWithMockedTime(1483275600); + $scheduler = $this->getSchedulerWithMockedTime('2017-01-01 13:00+00:00'); // Sunday, 1 January 2017 @ 1:00pm (UTC) verify($scheduler->getNextRunDate($scheduleOption->getValue())) ->equals('2017-01-01 13:01:00'); } @@ -445,9 +445,9 @@ class PostNotificationTest extends \MailPoetTest { return $newsletterPost; } - private function getSchedulerWithMockedTime(int $timestamp): Scheduler { + private function getSchedulerWithMockedTime(string $datetimeString): Scheduler { $wpMock = $this->createMock(WPFunctions::class); - $wpMock->method('currentTime')->willReturn($timestamp); + $wpMock->method('currentDatetime')->willReturn(new \DateTimeImmutable($datetimeString)); return $this->getServiceWithOverrides(Scheduler::class, [ 'wp' => $wpMock, ]); diff --git a/mailpoet/tests/integration/Newsletter/Scheduler/SchedulerTest.php b/mailpoet/tests/integration/Newsletter/Scheduler/SchedulerTest.php index c4a2247050..187c370aef 100644 --- a/mailpoet/tests/integration/Newsletter/Scheduler/SchedulerTest.php +++ b/mailpoet/tests/integration/Newsletter/Scheduler/SchedulerTest.php @@ -52,8 +52,9 @@ class SchedulerTest extends \MailPoetTest { public function testItCanGetCorrectNextRunDateInGMTTimezone() { $currentTime = '2024-08-09 09:00:00'; $wpMock = $this->createMock(WPFunctions::class); - $wpMock->method('currentTime')->willReturn(strtotime($currentTime)); - $wpMock->method('wpTimezone')->willReturn(new \DateTimeZone('UTC')); + $wpMock->method('currentDatetime')->willReturn( + new \DateTimeImmutable($currentTime, new \DateTimeZone('UTC')), + ); $scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]); verify($scheduler->getNextRunDate('0 10 * * *')) ->equals('2024-08-09 10:00:00'); @@ -62,8 +63,9 @@ class SchedulerTest extends \MailPoetTest { public function testItCanGetCorrectNextRunDateInNewYorkTimezone() { $currentTime = '2024-08-09 09:00:00'; $wpMock = $this->createMock(WPFunctions::class); - $wpMock->method('currentTime')->willReturn(strtotime($currentTime)); - $wpMock->method('wpTimezone')->willReturn(new \DateTimeZone('America/New_York')); + $wpMock->method('currentDatetime')->willReturn( + new \DateTimeImmutable($currentTime, new \DateTimeZone('America/New_York')) + ); $scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]); // Cron was set by user in NewYork timezone (0 10 * * *) in UTC it would be (0 14 * * *) verify($scheduler->getNextRunDate('0 10 * * *')) @@ -83,8 +85,9 @@ class SchedulerTest extends \MailPoetTest { public function testItCanGetCorrectPreviousRunDateInGMTTimezone() { $currentTime = '2024-08-09 09:00:00'; $wpMock = $this->createMock(WPFunctions::class); - $wpMock->method('currentTime')->willReturn(strtotime($currentTime)); - $wpMock->method('wpTimezone')->willReturn(new \DateTimeZone('UTC')); + $wpMock->method('currentDatetime')->willReturn( + new \DateTimeImmutable($currentTime, new \DateTimeZone('UTC')) + ); $scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]); verify($scheduler->getPreviousRunDate('0 10 * * *')) ->equals('2024-08-08 10:00:00'); @@ -93,8 +96,9 @@ class SchedulerTest extends \MailPoetTest { public function testItCanGetCorrectPreviousRunDateInNewYorkTimezone() { $currentTime = '2024-08-09 09:00:00'; $wpMock = $this->createMock(WPFunctions::class); - $wpMock->method('currentTime')->willReturn(strtotime($currentTime)); - $wpMock->method('wpTimezone')->willReturn(new \DateTimeZone('America/New_York')); + $wpMock->method('currentDatetime')->willReturn( + new \DateTimeImmutable($currentTime, new \DateTimeZone('America/New_York')) + ); $scheduler = $this->getServiceWithOverrides(Scheduler::class, ['wp' => $wpMock]); // Cron was set by user in NewYork timezone (0 10 * * *) in UTC it would be (0 14 * * *) verify($scheduler->getPreviousRunDate('0 10 * * *'))