Skip to content

Commit

Permalink
Merge pull request #1585 from magento-plankton/MAGETWO-80205
Browse files Browse the repository at this point in the history
[Plankton] MAGETWO-80205
  • Loading branch information
Alexander Akimov authored Oct 12, 2017
2 parents c06da29 + f69807f commit 08ec8ce
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
15 changes: 14 additions & 1 deletion app/code/Magento/Cron/Model/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
namespace Magento\Cron\Model;

use Magento\Framework\Exception\CronException;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;

/**
* Crontab schedule model
Expand Down Expand Up @@ -43,21 +45,29 @@ class Schedule extends \Magento\Framework\Model\AbstractModel

const STATUS_ERROR = 'error';

/**
* @var TimezoneInterface
*/
private $timezoneConverter;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @param TimezoneInterface $timezoneConverter
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
array $data = [],
TimezoneInterface $timezoneConverter = null
) {
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
$this->timezoneConverter = $timezoneConverter ?: ObjectManager::getInstance()->get(TimezoneInterface::class);
}

/**
Expand Down Expand Up @@ -100,6 +110,9 @@ public function trySchedule()
return false;
}
if (!is_numeric($time)) {
//convert time from UTC to admin store timezone
//we assume that all schedules in configuration (crontab.xml and DB tables) are in admin store timezone
$time = $this->timezoneConverter->date($time)->format('Y-m-d H:i');
$time = strtotime($time);
}
$match = $this->matchCronExpression($e[0], strftime('%M', $time))
Expand Down
33 changes: 32 additions & 1 deletion app/code/Magento/Cron/Test/Unit/Model/ScheduleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*/
class ScheduleTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
*/
protected $helper;

protected $resourceJobMock;
Expand Down Expand Up @@ -174,6 +177,35 @@ public function testTrySchedule($scheduledAt, $cronExprArr, $expected)
$this->assertEquals($expected, $result);
}

public function testTryScheduleWithConversionToAdminStoreTime()
{
$scheduledAt = '2011-12-13 14:15:16';
$cronExprArr = ['*', '*', '*', '*', '*'];

// 1. Create mocks
$timezoneConverter = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
$timezoneConverter->expects($this->once())
->method('date')
->with($scheduledAt)
->willReturn(new \DateTime($scheduledAt));

/** @var \Magento\Cron\Model\Schedule $model */
$model = $this->helper->getObject(
\Magento\Cron\Model\Schedule::class,
['timezoneConverter' => $timezoneConverter]
);

// 2. Set fixtures
$model->setScheduledAt($scheduledAt);
$model->setCronExprArr($cronExprArr);

// 3. Run tested method
$result = $model->trySchedule();

// 4. Compare actual result with expected result
$this->assertTrue($result);
}

/**
* @return array
*/
Expand All @@ -187,7 +219,6 @@ public function tryScheduleDataProvider()
[$date, [], false],
[$date, null, false],
[$date, false, false],
[$date, ['*', '*', '*', '*', '*'], true],
[strtotime($date), ['*', '*', '*', '*', '*'], true],
[strtotime($date), ['15', '*', '*', '*', '*'], true],
[strtotime($date), ['*', '14', '*', '*', '*'], true],
Expand Down

0 comments on commit 08ec8ce

Please sign in to comment.