Skip to content

Commit ba55dd7

Browse files
author
Oleksii Korshenko
committed
MAGETWO-71359: Test cron expressions against localized time #10432
- fixed timezone conversion logic - fixed unit tests
1 parent 9008b7e commit ba55dd7

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

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

Lines changed: 11 additions & 8 deletions
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
@@ -45,28 +47,28 @@ class Schedule extends \Magento\Framework\Model\AbstractModel
4547
const STATUS_ERROR = 'error';
4648

4749
/**
48-
* @var \Magento\Framework\Stdlib\DateTime\DateTime
50+
* @var TimezoneInterface
4951
*/
50-
private $dateTime;
52+
private $timezoneConverter;
5153

5254
/**
5355
* @param \Magento\Framework\Model\Context $context
5456
* @param \Magento\Framework\Registry $registry
5557
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
5658
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
5759
* @param array $data
58-
* @param \Magento\Framework\Stdlib\DateTime\DateTime|null $dateTime
60+
* @param TimezoneInterface $timezoneConverter
5961
*/
6062
public function __construct(
6163
\Magento\Framework\Model\Context $context,
6264
\Magento\Framework\Registry $registry,
6365
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
6466
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
6567
array $data = [],
66-
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime = null
68+
TimezoneInterface $timezoneConverter = null
6769
) {
6870
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
69-
$this->dateTime = $dateTime;
71+
$this->timezoneConverter = $timezoneConverter ?: ObjectManager::getInstance()->get(TimezoneInterface::class);
7072
}
7173

7274
/**
@@ -102,16 +104,17 @@ public function setCronExpr($expr)
102104
*/
103105
public function trySchedule()
104106
{
105-
$this->dateTime = $this->dateTime ?: \Magento\Framework\App\ObjectManager::getInstance()
106-
->get(\Magento\Framework\Stdlib\DateTime\DateTime::class);
107107
$time = $this->getScheduledAt();
108108
$e = $this->getCronExprArr();
109109

110110
if (!$e || !$time) {
111111
return false;
112112
}
113113
if (!is_numeric($time)) {
114-
$time = strtotime($time) + $this->dateTime->getGmtOffset();
114+
//convert time from UTC to admin store timezone
115+
//we assume that all schedules in configuration (crontab.xml and DB tables) are in admin store timezone
116+
$time = $this->timezoneConverter->date($time)->format('Y-m-d H:i');
117+
$time = strtotime($time);
115118
}
116119
$match = $this->matchCronExpression($e[0], strftime('%M', $time))
117120
&& $this->matchCronExpression($e[1], strftime('%H', $time))

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

Lines changed: 33 additions & 1 deletion
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,36 @@ public function testTrySchedule($scheduledAt, $cronExprArr, $expected)
174177
$this->assertEquals($expected, $result);
175178
}
176179

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

app/code/Magento/Cron/Test/Unit/Observer/ProcessCronQueueObserverTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ public function testDispatchRemoveConfigMismatch()
675675
]);
676676

677677
$jobs = [];
678-
for ($i = 0; $i<20; $i++) {
678+
for ($i = 0; $i < 20; $i++) {
679679
$time = date('Y-m-d H:i:00', strtotime("+$i minutes"));
680680
$jobs[] = [
681681
'age' => $time,
@@ -704,9 +704,20 @@ public function testDispatchRemoveConfigMismatch()
704704
$this->_collection->addItem($schedule);
705705

706706
$scheduleMock = $this->getMockBuilder(Schedule::class)->disableOriginalConstructor()
707-
->setMethods(['save', 'getCollection', 'getResource'])->getMock();
707+
->setMethods(['save', 'getCollection', 'getResource', 'trySchedule'])->getMock();
708+
708709
$scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->_collection));
709710
$scheduleMock->expects($this->any())->method('getResource')->will($this->returnValue($this->scheduleResource));
711+
712+
$callIndex = 0;
713+
$scheduleMock->expects($this->any())->method('trySchedule')->willReturnCallback(
714+
function () use (&$callIndex, $jobs) {
715+
$output = $jobs[$callIndex]['delete'];
716+
$callIndex++;
717+
return !$output;
718+
}
719+
);
720+
710721
$this->_scheduleFactory->expects($this->any())->method('create')->will($this->returnValue($scheduleMock));
711722

712723
$query = [

0 commit comments

Comments
 (0)