diff --git a/lib/Cron/Workers/SendingQueue/Migration.php b/lib/Cron/Workers/SendingQueue/Migration.php index 702834df34..61330aeb12 100644 --- a/lib/Cron/Workers/SendingQueue/Migration.php +++ b/lib/Cron/Workers/SendingQueue/Migration.php @@ -8,6 +8,7 @@ use MailPoet\Mailer\MailerLog; use MailPoet\Models\ScheduledTask; use MailPoet\Models\ScheduledTaskSubscriber; use MailPoet\Models\SendingQueue; +use MailPoet\WP\Functions as WPFunctions; if(!defined('ABSPATH')) exit; @@ -242,6 +243,6 @@ class Migration extends SimpleWorker { static function getNextRunDate() { // run migration immediately - return Carbon::createFromTimestamp(current_time('timestamp')); + return Carbon::createFromTimestamp(WPFunctions::currentTime('timestamp')); } } diff --git a/lib/WP/Functions.php b/lib/WP/Functions.php index 87b5f51b34..0841186beb 100644 --- a/lib/WP/Functions.php +++ b/lib/WP/Functions.php @@ -22,6 +22,10 @@ class Functions { return self::callWithFallback('wp_remote_retrieve_response_message', func_get_args()); } + static function currentTime() { + return self::callWithFallback('current_time', func_get_args()); + } + private static function callWithFallback($func, $args) { $local_func = __NAMESPACE__ . '\\' . $func; if(function_exists($local_func)) { diff --git a/tests/_support/Helper/WordPress.php b/tests/_support/Helper/WordPress.php index 2be5f006ec..e7c1c0b885 100644 --- a/tests/_support/Helper/WordPress.php +++ b/tests/_support/Helper/WordPress.php @@ -70,4 +70,8 @@ function wp_remote_post() { function wp_remote_get() { return override(__FUNCTION__, func_get_args()); +} + +function current_time() { + return override(__FUNCTION__, func_get_args()); } \ No newline at end of file diff --git a/tests/unit/Cron/Workers/SendingQueue/MigrationTest.php b/tests/unit/Cron/Workers/SendingQueue/MigrationTest.php index 7b306a127c..c0fe88ef40 100644 --- a/tests/unit/Cron/Workers/SendingQueue/MigrationTest.php +++ b/tests/unit/Cron/Workers/SendingQueue/MigrationTest.php @@ -2,6 +2,7 @@ namespace MailPoet\Test\Cron\Workers; use Carbon\Carbon; +use Helper\WordPress as WordPressHelper; use MailPoet\Cron\Workers\SendingQueue\Migration; use MailPoet\Mailer\MailerLog; use MailPoet\Models\ScheduledTask; @@ -107,6 +108,20 @@ class MigrationTest extends \MailPoetTest { expect(MailerLog::isSendingPaused())->false(); } + function testItUsesWPTimeToReturnNextRunDate() { + $timestamp = 1514801410; // 01/01/2018 @ 10:10am (UTC) + + WordPressHelper::interceptFunction('current_time', function($time) use($timestamp) { + // "timestamp" string is passed as an argument + expect($time)->equals('timestamp'); + + return $timestamp; + }); + + $next_run_date = Migration::getNextRunDate(); + expect($next_run_date->timestamp)->equals($timestamp); + } + private function createScheduledTask() { $task = ScheduledTask::create(); $task->type = Migration::TASK_TYPE; @@ -185,5 +200,7 @@ class MigrationTest extends \MailPoetTest { $this->restoreTable(); $this->altered = false; } + + WordPressHelper::releaseAllFunctions(); } }