Skip to content

Commit 08ec8ce

Browse files
author
Alexander Akimov
authored
Merge pull request magento#1585 from magento-plankton/MAGETWO-80205
[Plankton] MAGETWO-80205
2 parents c06da29 + f69807f commit 08ec8ce

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

app/code/Magento/Cron/Model/Schedule.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
namespace Magento\Cron\Model;
88

99
use Magento\Framework\Exception\CronException;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1012

1113
/**
1214
* Crontab schedule model
@@ -43,21 +45,29 @@ class Schedule extends \Magento\Framework\Model\AbstractModel
4345

4446
const STATUS_ERROR = 'error';
4547

48+
/**
49+
* @var TimezoneInterface
50+
*/
51+
private $timezoneConverter;
52+
4653
/**
4754
* @param \Magento\Framework\Model\Context $context
4855
* @param \Magento\Framework\Registry $registry
4956
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
5057
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
5158
* @param array $data
59+
* @param TimezoneInterface $timezoneConverter
5260
*/
5361
public function __construct(
5462
\Magento\Framework\Model\Context $context,
5563
\Magento\Framework\Registry $registry,
5664
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
5765
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
58-
array $data = []
66+
array $data = [],
67+
TimezoneInterface $timezoneConverter = null
5968
) {
6069
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
70+
$this->timezoneConverter = $timezoneConverter ?: ObjectManager::getInstance()->get(TimezoneInterface::class);
6171
}
6272

6373
/**
@@ -100,6 +110,9 @@ public function trySchedule()
100110
return false;
101111
}
102112
if (!is_numeric($time)) {
113+
//convert time from UTC to admin store timezone
114+
//we assume that all schedules in configuration (crontab.xml and DB tables) are in admin store timezone
115+
$time = $this->timezoneConverter->date($time)->format('Y-m-d H:i');
103116
$time = strtotime($time);
104117
}
105118
$match = $this->matchCronExpression($e[0], strftime('%M', $time))

app/code/Magento/Cron/Test/Unit/Model/ScheduleTest.php

+32-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
*/
1414
class ScheduleTest extends \PHPUnit\Framework\TestCase
1515
{
16+
/**
17+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
18+
*/
1619
protected $helper;
1720

1821
protected $resourceJobMock;
@@ -174,6 +177,35 @@ public function testTrySchedule($scheduledAt, $cronExprArr, $expected)
174177
$this->assertEquals($expected, $result);
175178
}
176179

180+
public function testTryScheduleWithConversionToAdminStoreTime()
181+
{
182+
$scheduledAt = '2011-12-13 14:15:16';
183+
$cronExprArr = ['*', '*', '*', '*', '*'];
184+
185+
// 1. Create mocks
186+
$timezoneConverter = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
187+
$timezoneConverter->expects($this->once())
188+
->method('date')
189+
->with($scheduledAt)
190+
->willReturn(new \DateTime($scheduledAt));
191+
192+
/** @var \Magento\Cron\Model\Schedule $model */
193+
$model = $this->helper->getObject(
194+
\Magento\Cron\Model\Schedule::class,
195+
['timezoneConverter' => $timezoneConverter]
196+
);
197+
198+
// 2. Set fixtures
199+
$model->setScheduledAt($scheduledAt);
200+
$model->setCronExprArr($cronExprArr);
201+
202+
// 3. Run tested method
203+
$result = $model->trySchedule();
204+
205+
// 4. Compare actual result with expected result
206+
$this->assertTrue($result);
207+
}
208+
177209
/**
178210
* @return array
179211
*/
@@ -187,7 +219,6 @@ public function tryScheduleDataProvider()
187219
[$date, [], false],
188220
[$date, null, false],
189221
[$date, false, false],
190-
[$date, ['*', '*', '*', '*', '*'], true],
191222
[strtotime($date), ['*', '*', '*', '*', '*'], true],
192223
[strtotime($date), ['15', '*', '*', '*', '*'], true],
193224
[strtotime($date), ['*', '14', '*', '*', '*'], true],

0 commit comments

Comments
 (0)