Skip to content

Commit 24f2f07

Browse files
author
Oleksii Korshenko
authored
Merge pull request #1435 from magento-engcom/develop-prs
Public Pull Requests #10432
2 parents 6c5ba1f + 7cec700 commit 24f2f07

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
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
@@ -42,21 +44,29 @@ class Schedule extends \Magento\Framework\Model\AbstractModel
4244

4345
const STATUS_ERROR = 'error';
4446

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

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

app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Magento\Framework\App\State;
1313
use Magento\Framework\Console\Cli;
1414
use Magento\Framework\Event\ObserverInterface;
15-
use \Magento\Cron\Model\Schedule;
15+
use Magento\Cron\Model\Schedule;
1616

1717
/**
1818
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)

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)