Skip to content

Commit 2fc966c

Browse files
committed
MAGETWO-93701: Make cron:run CLI command react on cron disable configuration
1 parent 9909d00 commit 2fc966c

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

app/code/Magento/Cron/Console/Command/CronCommand.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Store\Model\Store;
1515
use Magento\Store\Model\StoreManager;
1616
use Magento\Cron\Observer\ProcessCronQueueObserver;
17+
use Magento\Framework\App\DeploymentConfig;
1718
use Magento\Framework\Console\Cli;
1819
use Magento\Framework\Shell\ComplexParameter;
1920

@@ -35,13 +36,22 @@ class CronCommand extends Command
3536
private $objectManagerFactory;
3637

3738
/**
38-
* Constructor
39+
* Application deployment configuration
3940
*
41+
* @var DeploymentConfig
42+
*/
43+
private $deploymentConfig;
44+
45+
/**
4046
* @param ObjectManagerFactory $objectManagerFactory
47+
* @param DeploymentConfig $deploymentConfig Application deployment configuration
4148
*/
42-
public function __construct(ObjectManagerFactory $objectManagerFactory)
43-
{
49+
public function __construct(
50+
ObjectManagerFactory $objectManagerFactory,
51+
DeploymentConfig $deploymentConfig = null
52+
){
4453
$this->objectManagerFactory = $objectManagerFactory;
54+
$this->deploymentConfig = $deploymentConfig;
4555
parent::__construct();
4656
}
4757

@@ -71,10 +81,16 @@ protected function configure()
7181
}
7282

7383
/**
84+
* Runs cron jobs if cron is not disabled in Magento configurations
85+
*
7486
* {@inheritdoc}
7587
*/
7688
protected function execute(InputInterface $input, OutputInterface $output)
7789
{
90+
if (!$this->deploymentConfig->get('cron/enabled', 1)) {
91+
$output->writeln('<info>' . 'Cron is disabled. Jobs were not run.' . '</info>');
92+
return;
93+
}
7894
$omParams = $_SERVER;
7995
$omParams[StoreManager::PARAM_RUN_CODE] = 'admin';
8096
$omParams[Store::CUSTOM_ENTRY_POINT_PARAM] = true;

app/code/Magento/Cron/Test/Unit/Console/Command/CronCommandTest.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,52 @@
1010

1111
class CronCommandTest extends \PHPUnit\Framework\TestCase
1212
{
13+
/**
14+
* Test command with disables cron
15+
*
16+
* @return void
17+
*/
18+
public function testExecuteWithDisabledCrons()
19+
{
20+
$objectManagerFactory = $this->createMock(\Magento\Framework\App\ObjectManagerFactory::class);
21+
$deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
22+
23+
$objectManagerFactory->expects($this->never())
24+
->method('create');
25+
$deploymentConfigMock->expects($this->once())
26+
->method('get')
27+
->with('cron/enabled', 1)
28+
->willReturn(0);
29+
$commandTester = new CommandTester(new CronCommand($objectManagerFactory, $deploymentConfigMock));
30+
$commandTester->execute([]);
31+
$expectedMsg = 'Cron is disabled. Jobs were not run.' . PHP_EOL;
32+
$this->assertEquals($expectedMsg, $commandTester->getDisplay());
33+
}
34+
35+
/**
36+
* Test command with enabled cron
37+
*
38+
* @return void
39+
*/
1340
public function testExecute()
1441
{
1542
$objectManagerFactory = $this->createMock(\Magento\Framework\App\ObjectManagerFactory::class);
43+
$deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
1644
$objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
1745
$cron = $this->createMock(\Magento\Framework\App\Cron::class);
18-
$objectManager->expects($this->once())->method('create')->willReturn($cron);
19-
$cron->expects($this->once())->method('launch');
20-
$objectManagerFactory->expects($this->once())->method('create')->willReturn($objectManager);
21-
$commandTester = new CommandTester(new CronCommand($objectManagerFactory));
46+
$objectManager->expects($this->once())
47+
->method('create')
48+
->willReturn($cron);
49+
$cron->expects($this->once())
50+
->method('launch');
51+
$objectManagerFactory->expects($this->once())
52+
->method('create')
53+
->willReturn($objectManager);
54+
$deploymentConfigMock->expects($this->once())
55+
->method('get')
56+
->with('cron/enabled', 1)
57+
->willReturn(1);
58+
$commandTester = new CommandTester(new CronCommand($objectManagerFactory, $deploymentConfigMock));
2259
$commandTester->execute([]);
2360
$expectedMsg = 'Ran jobs by schedule.' . PHP_EOL;
2461
$this->assertEquals($expectedMsg, $commandTester->getDisplay());

0 commit comments

Comments
 (0)