init
This commit is contained in:
+98
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Cron\Model\Config\Backend\Product;
|
||||
|
||||
use Magento\Config\Model\Config as ConfigModel;
|
||||
use Magento\Config\Model\PreparedValueFactory;
|
||||
use Magento\Cron\Model\Config\Source\Frequency;
|
||||
use Magento\Framework\App\Config\ValueFactory;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class AlertTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider frequencyDataProvider
|
||||
* @param string $frequency
|
||||
* @param string $expectedCronExpr
|
||||
*/
|
||||
public function testDirectSave(string $frequency, string $expectedCronExpr): void
|
||||
{
|
||||
$preparedValueFactory = $this->objectManager->get(PreparedValueFactory::class);
|
||||
/** @var Alert $sitemapValue */
|
||||
$alertValue = $preparedValueFactory->create('catalog/productalert_cron/frequency', $frequency, 'default', 0);
|
||||
$alertValue->save();
|
||||
|
||||
self::assertEquals($expectedCronExpr, $this->getCronExpression());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider frequencyDataProvider
|
||||
* @param string $frequency
|
||||
* @param string $expectedCronExpr
|
||||
*/
|
||||
public function testSaveFromAdmin(string $frequency, string $expectedCronExpr): void
|
||||
{
|
||||
$config = $this->objectManager->create(ConfigModel::class);
|
||||
$config->setSection('catalog');
|
||||
$config->setGroups(
|
||||
[
|
||||
'productalert_cron' => [
|
||||
'fields' => [
|
||||
'time' => ['value' => ['00', '00', '00']],
|
||||
'frequency' => ['value' => $frequency],
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
$config->save();
|
||||
|
||||
self::assertEquals($expectedCronExpr, $this->getCronExpression());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function frequencyDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'daily' => [Frequency::CRON_DAILY, '0 0 * * *'],
|
||||
'weekly' => [Frequency::CRON_WEEKLY, '0 0 * * 1'],
|
||||
'monthly' => [Frequency::CRON_MONTHLY, '0 0 1 * *'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function getCronExpression(): string
|
||||
{
|
||||
$valueFactory = $this->objectManager->get(ValueFactory::class);
|
||||
$cronExprValue = $valueFactory->create()
|
||||
->load(Alert::CRON_STRING_PATH, 'path');
|
||||
|
||||
return $cronExprValue->getValue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Cron\Model\Config\Backend;
|
||||
|
||||
use Magento\Config\Model\Config as ConfigModel;
|
||||
use Magento\Config\Model\PreparedValueFactory;
|
||||
use Magento\Cron\Model\Config\Source\Frequency;
|
||||
use Magento\Framework\App\Config\ValueFactory;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class SitemapTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider frequencyDataProvider
|
||||
* @param string $frequency
|
||||
* @param string $expectedCronExpr
|
||||
*/
|
||||
public function testDirectSave(string $frequency, string $expectedCronExpr): void
|
||||
{
|
||||
$preparedValueFactory = $this->objectManager->get(PreparedValueFactory::class);
|
||||
/** @var Sitemap $sitemapValue */
|
||||
$sitemapValue = $preparedValueFactory->create('sitemap/generate/frequency', $frequency, 'default', 0);
|
||||
$sitemapValue->save();
|
||||
|
||||
self::assertEquals($expectedCronExpr, $this->getCronExpression());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider frequencyDataProvider
|
||||
* @param string $frequency
|
||||
* @param string $expectedCronExpr
|
||||
*/
|
||||
public function testSaveFromAdmin(string $frequency, string $expectedCronExpr): void
|
||||
{
|
||||
$config = $this->objectManager->create(ConfigModel::class);
|
||||
$config->setSection('sitemap');
|
||||
$config->setGroups(
|
||||
[
|
||||
'generate' => [
|
||||
'fields' => [
|
||||
'time' => ['value' => ['00', '00', '00']],
|
||||
'frequency' => ['value' => $frequency],
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
$config->save();
|
||||
|
||||
self::assertEquals($expectedCronExpr, $this->getCronExpression());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function frequencyDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'daily' => [Frequency::CRON_DAILY, '0 0 * * *'],
|
||||
'weekly' => [Frequency::CRON_WEEKLY, '0 0 * * 1'],
|
||||
'monthly' => [Frequency::CRON_MONTHLY, '0 0 1 * *'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function getCronExpression(): string
|
||||
{
|
||||
$valueFactory = $this->objectManager->get(ValueFactory::class);
|
||||
$cronExprValue = $valueFactory->create()
|
||||
->load(Sitemap::CRON_STRING_PATH, 'path');
|
||||
|
||||
return $cronExprValue->getValue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Cron\Model;
|
||||
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
use \Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
/**
|
||||
* Test \Magento\Cron\Model\Schedule
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
class ScheduleTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var ScheduleFactory
|
||||
*/
|
||||
private $scheduleFactory;
|
||||
|
||||
/**
|
||||
* @var DateTime
|
||||
*/
|
||||
protected $dateTime;
|
||||
|
||||
/**
|
||||
* @ingeritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->dateTime = Bootstrap::getObjectManager()->create(DateTime::class);
|
||||
$this->scheduleFactory = Bootstrap::getObjectManager()->create(ScheduleFactory::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* If there are no currently locked jobs, locking one of them should succeed
|
||||
*/
|
||||
public function testTryLockJobNoLockedJobsSucceeds()
|
||||
{
|
||||
for ($i = 1; $i < 6; $i++) {
|
||||
$this->createSchedule("test_job", Schedule::STATUS_PENDING, 60 * $i);
|
||||
}
|
||||
$schedule = $this->createSchedule("test_job", Schedule::STATUS_PENDING);
|
||||
|
||||
$this->assertTrue($schedule->tryLockJob());
|
||||
}
|
||||
|
||||
/**
|
||||
* If the job is already locked, attempting to lock it again should fail
|
||||
*/
|
||||
public function testTryLockJobAlreadyLockedFails()
|
||||
{
|
||||
$schedule = $this->createSchedule("test_job", Schedule::STATUS_RUNNING);
|
||||
|
||||
$this->assertFalse($schedule->tryLockJob());
|
||||
}
|
||||
|
||||
/**
|
||||
* If the job is already locked but lock time less than 1 day ago, attempting to lock it again should fail
|
||||
*/
|
||||
public function testTryLockJobAlreadyLockedSucceeds()
|
||||
{
|
||||
$offsetInThePast = 2*24*60*60;
|
||||
|
||||
$oldSchedule = $this->scheduleFactory->create()
|
||||
->setCronExpr("* * * * *")
|
||||
->setJobCode("test_job")
|
||||
->setStatus(Schedule::STATUS_RUNNING)
|
||||
->setCreatedAt(date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp() - $offsetInThePast))
|
||||
->setScheduledAt(date('Y-m-d H:i', $this->dateTime->gmtTimestamp() - $offsetInThePast + 60))
|
||||
->setExecutedAt(date('Y-m-d H:i', $this->dateTime->gmtTimestamp() - $offsetInThePast + 61));
|
||||
$oldSchedule->save();
|
||||
|
||||
$schedule = $this->createSchedule("test_job", Schedule::STATUS_PENDING);
|
||||
|
||||
$this->assertTrue($schedule->tryLockJob());
|
||||
}
|
||||
|
||||
/**
|
||||
* If there's a job already has running status, should be able to set this status for another job
|
||||
*/
|
||||
public function testTryLockJobOtherLockedFails()
|
||||
{
|
||||
$this->createSchedule("test_job", Schedule::STATUS_RUNNING);
|
||||
$schedule = $this->createSchedule("test_job", Schedule::STATUS_PENDING, 60);
|
||||
|
||||
$this->assertTrue($schedule->tryLockJob());
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be able to lock a job if a job with a different code is locked
|
||||
*/
|
||||
public function testTryLockJobDifferentJobLocked()
|
||||
{
|
||||
$this->createSchedule("test_job_other", Schedule::STATUS_RUNNING);
|
||||
$schedule = $this->createSchedule("test_job", Schedule::STATUS_PENDING);
|
||||
|
||||
$this->assertTrue($schedule->tryLockJob());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a schedule with the given job code, status, and schedule time offset
|
||||
*
|
||||
* @param string $jobCode
|
||||
* @param string $status
|
||||
* @param int $timeOffset
|
||||
* @return Schedule
|
||||
*/
|
||||
private function createSchedule($jobCode, $status, $timeOffset = 0)
|
||||
{
|
||||
$schedule = $this->scheduleFactory->create()
|
||||
->setCronExpr("* * * * *")
|
||||
->setJobCode($jobCode)
|
||||
->setStatus($status)
|
||||
->setCreatedAt(date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp()))
|
||||
->setScheduledAt(date('Y-m-d H:i', $this->dateTime->gmtTimestamp() + $timeOffset));
|
||||
$schedule->save();
|
||||
|
||||
return $schedule;
|
||||
}
|
||||
}
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Cron\Observer;
|
||||
|
||||
use Magento\Cron\Observer\ProcessCronQueueObserver;
|
||||
use \Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
class ProcessCronQueueObserverTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Cron\Observer\ProcessCronQueueObserver
|
||||
*/
|
||||
private $_model = null;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\App\AreaList::class)
|
||||
->getArea('crontab')
|
||||
->load(\Magento\Framework\App\Area::PART_CONFIG);
|
||||
$request = Bootstrap::getObjectManager()->create(\Magento\Framework\App\Console\Request::class);
|
||||
$request->setParams(['group' => 'default', 'standaloneProcessStarted' => '0']);
|
||||
$this->_model = Bootstrap::getObjectManager()
|
||||
->create(\Magento\Cron\Observer\ProcessCronQueueObserver::class, ['request' => $request]);
|
||||
$this->_model->execute(new \Magento\Framework\Event\Observer());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store crontab/default/jobs/catalog_product_alert/schedule/cron_expr * * * * *
|
||||
*/
|
||||
public function testDispatchScheduled()
|
||||
{
|
||||
$collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Cron\Model\ResourceModel\Schedule\Collection::class
|
||||
);
|
||||
$collection->addFieldToFilter('status', \Magento\Cron\Model\Schedule::STATUS_PENDING);
|
||||
$collection->addFieldToFilter('job_code', 'catalog_product_alert');
|
||||
$this->assertGreaterThan(0, $collection->count(), 'Cron has failed to schedule tasks for itself for future.');
|
||||
}
|
||||
|
||||
public function testDispatchNoFailed()
|
||||
{
|
||||
$collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Cron\Model\ResourceModel\Schedule\Collection::class
|
||||
);
|
||||
$collection->addFieldToFilter('status', \Magento\Cron\Model\Schedule::STATUS_ERROR);
|
||||
foreach ($collection as $item) {
|
||||
$this->fail($item->getMessages());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $expectedGroupsToRun
|
||||
* @param null $group
|
||||
* @param null $excludeGroup
|
||||
* @dataProvider groupFiltersDataProvider
|
||||
*/
|
||||
public function testGroupFilters(array $expectedGroupsToRun, $group = null, $excludeGroup = null)
|
||||
{
|
||||
$config = $this->createMock(\Magento\Cron\Model\ConfigInterface::class);
|
||||
$config->expects($this->any())
|
||||
->method('getJobs')
|
||||
->willReturn($this->getFilterTestCronGroups());
|
||||
|
||||
$request = Bootstrap::getObjectManager()->get(\Magento\Framework\App\Console\Request::class);
|
||||
$lockManager = $this->createMock(\Magento\Framework\Lock\LockManagerInterface::class);
|
||||
|
||||
// The jobs are locked when they are run, assert on them to see which groups would run
|
||||
$expectedLockData = [];
|
||||
foreach ($expectedGroupsToRun as $expectedGroupToRun) {
|
||||
$expectedLockData[] = [
|
||||
ProcessCronQueueObserver::LOCK_PREFIX . $expectedGroupToRun,
|
||||
ProcessCronQueueObserver::LOCK_TIMEOUT
|
||||
];
|
||||
}
|
||||
|
||||
// No expected lock data, means we should never call it
|
||||
if (empty($expectedLockData)) {
|
||||
$lockManager->expects($this->never())
|
||||
->method('lock');
|
||||
}
|
||||
|
||||
$lockManager->expects($this->exactly(count($expectedLockData)))
|
||||
->method('lock')
|
||||
->withConsecutive(...$expectedLockData);
|
||||
|
||||
$request->setParams(
|
||||
[
|
||||
'group' => $group,
|
||||
'exclude-group' => $excludeGroup,
|
||||
'standaloneProcessStarted' => '1'
|
||||
]
|
||||
);
|
||||
$this->_model = Bootstrap::getObjectManager()
|
||||
->create(\Magento\Cron\Observer\ProcessCronQueueObserver::class, [
|
||||
'request' => $request,
|
||||
'lockManager' => $lockManager,
|
||||
'config' => $config
|
||||
]);
|
||||
$this->_model->execute(new \Magento\Framework\Event\Observer());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|array[]
|
||||
*/
|
||||
public function groupFiltersDataProvider(): array
|
||||
{
|
||||
|
||||
return [
|
||||
'no flags runs all groups' => [
|
||||
['index', 'consumers', 'default'] // groups to run
|
||||
],
|
||||
'--group=default should run' => [
|
||||
['default'], // groups to run
|
||||
'default', // --group default
|
||||
],
|
||||
'--group=default with --exclude-group=default, nothing should run' => [
|
||||
[], // groups to run
|
||||
'default', // --group default
|
||||
['default'], // --exclude-group default
|
||||
],
|
||||
'--group=default with --exclude-group=index, default should run' => [
|
||||
['default'], // groups to run
|
||||
'default', // --group default
|
||||
['index'], // --exclude-group index
|
||||
],
|
||||
'--group=index with --exclude-group=default, index should run' => [
|
||||
['index'], // groups to run
|
||||
'index', // --group index
|
||||
['default'], // --exclude-group default
|
||||
],
|
||||
'--exclude-group=index, all other groups should run' => [
|
||||
['consumers', 'default'], // groups to run, all but index
|
||||
null, //
|
||||
['index'] // --exclude-group index
|
||||
],
|
||||
'--exclude-group for every group runs nothing' => [
|
||||
[], // groups to run, none
|
||||
null, //
|
||||
['default', 'consumers', 'index'] // groups to exclude, all of them
|
||||
],
|
||||
'exclude all groups but consumers, consumers runs' => [
|
||||
['consumers'],
|
||||
null,
|
||||
['index', 'default']
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Only run the filter group tests with a limited set of cron groups, keeps tests consistent between EE and CE
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getFilterTestCronGroups()
|
||||
{
|
||||
$listOfGroups = [];
|
||||
$config = Bootstrap::getObjectManager()->get(\Magento\Cron\Model\ConfigInterface::class);
|
||||
foreach ($config->getJobs() as $groupId => $data) {
|
||||
if (in_array($groupId, ['default', 'consumers', 'index'])) {
|
||||
$listOfGroups[$groupId] = $data;
|
||||
}
|
||||
}
|
||||
return $listOfGroups;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user