Skip to content

Commit

Permalink
Merge pull request #660 from magento-engcom/develop-prs
Browse files Browse the repository at this point in the history
Public Pull Requests:
#10432
  • Loading branch information
Oleksii Korshenko authored Aug 23, 2017
2 parents 6c5ba1f + 7cec700 commit 03213a7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 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 @@ -42,21 +44,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 @@ -99,6 +109,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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Magento\Framework\App\State;
use Magento\Framework\Console\Cli;
use Magento\Framework\Event\ObserverInterface;
use \Magento\Cron\Model\Schedule;
use Magento\Cron\Model\Schedule;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
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 03213a7

Please sign in to comment.