Skip to content

Commit 867867a

Browse files
author
Joan He
authored
Merge pull request #2910 from magento-thunder/MAGETWO-93758
[thunder] MAGETWO-93758: [Backport 2.2.x] Make cron:run CLI command react on cron disable configuration
2 parents c8c2791 + 02fb27c commit 867867a

File tree

2 files changed

+82
-8
lines changed

2 files changed

+82
-8
lines changed

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
use Symfony\Component\Console\Input\InputInterface;
1111
use Symfony\Component\Console\Output\OutputInterface;
1212
use Symfony\Component\Console\Input\InputOption;
13+
use Magento\Framework\App\ObjectManager;
1314
use Magento\Framework\App\ObjectManagerFactory;
1415
use Magento\Store\Model\Store;
1516
use Magento\Store\Model\StoreManager;
1617
use Magento\Cron\Observer\ProcessCronQueueObserver;
18+
use Magento\Framework\App\DeploymentConfig;
1719
use Magento\Framework\Console\Cli;
1820
use Magento\Framework\Shell\ComplexParameter;
1921

@@ -35,13 +37,24 @@ class CronCommand extends Command
3537
private $objectManagerFactory;
3638

3739
/**
38-
* Constructor
40+
* Application deployment configuration
3941
*
42+
* @var DeploymentConfig
43+
*/
44+
private $deploymentConfig;
45+
46+
/**
4047
* @param ObjectManagerFactory $objectManagerFactory
48+
* @param DeploymentConfig $deploymentConfig Application deployment configuration
4149
*/
42-
public function __construct(ObjectManagerFactory $objectManagerFactory)
43-
{
50+
public function __construct(
51+
ObjectManagerFactory $objectManagerFactory,
52+
DeploymentConfig $deploymentConfig = null
53+
) {
4454
$this->objectManagerFactory = $objectManagerFactory;
55+
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(
56+
DeploymentConfig::class
57+
);
4558
parent::__construct();
4659
}
4760

@@ -71,10 +84,16 @@ protected function configure()
7184
}
7285

7386
/**
87+
* Runs cron jobs if cron is not disabled in Magento configurations
88+
*
7489
* {@inheritdoc}
7590
*/
7691
protected function execute(InputInterface $input, OutputInterface $output)
7792
{
93+
if (!$this->deploymentConfig->get('cron/enabled', 1)) {
94+
$output->writeln('<info>' . 'Cron is disabled. Jobs were not run.' . '</info>');
95+
return;
96+
}
7897
$omParams = $_SERVER;
7998
$omParams[StoreManager::PARAM_RUN_CODE] = 'admin';
8099
$omParams[Store::CUSTOM_ENTRY_POINT_PARAM] = true;

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

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,74 @@
66
namespace Magento\Cron\Test\Unit\Console\Command;
77

88
use Magento\Cron\Console\Command\CronCommand;
9+
use Magento\Framework\App\DeploymentConfig;
10+
use Magento\Framework\App\ObjectManagerFactory;
11+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
912
use Symfony\Component\Console\Tester\CommandTester;
1013

1114
class CronCommandTest extends \PHPUnit\Framework\TestCase
1215
{
16+
/**
17+
* @var ObjectManagerFactory|MockObject
18+
*/
19+
private $objectManagerFactory;
20+
21+
/**
22+
* @var DeploymentConfig|MockObject
23+
*/
24+
private $deploymentConfigMock;
25+
26+
protected function setUp()
27+
{
28+
$this->objectManagerFactory = $this->createMock(ObjectManagerFactory::class);
29+
$this->deploymentConfigMock = $this->createMock(DeploymentConfig::class);
30+
}
31+
32+
/**
33+
* Test command with disables cron
34+
*
35+
* @return void
36+
*/
37+
public function testExecuteWithDisabledCrons()
38+
{
39+
$this->objectManagerFactory->expects($this->never())
40+
->method('create');
41+
$this->deploymentConfigMock->expects($this->once())
42+
->method('get')
43+
->with('cron/enabled', 1)
44+
->willReturn(0);
45+
$commandTester = new CommandTester(
46+
new CronCommand($this->objectManagerFactory, $this->deploymentConfigMock)
47+
);
48+
$commandTester->execute([]);
49+
$expectedMsg = 'Cron is disabled. Jobs were not run.' . PHP_EOL;
50+
$this->assertEquals($expectedMsg, $commandTester->getDisplay());
51+
}
52+
53+
/**
54+
* Test command with enabled cron
55+
*
56+
* @return void
57+
*/
1358
public function testExecute()
1459
{
15-
$objectManagerFactory = $this->createMock(\Magento\Framework\App\ObjectManagerFactory::class);
1660
$objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
1761
$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));
62+
$objectManager->expects($this->once())
63+
->method('create')
64+
->willReturn($cron);
65+
$cron->expects($this->once())
66+
->method('launch');
67+
$this->objectManagerFactory->expects($this->once())
68+
->method('create')
69+
->willReturn($objectManager);
70+
$this->deploymentConfigMock->expects($this->once())
71+
->method('get')
72+
->with('cron/enabled', 1)
73+
->willReturn(1);
74+
$commandTester = new CommandTester(
75+
new CronCommand($this->objectManagerFactory, $this->deploymentConfigMock)
76+
);
2277
$commandTester->execute([]);
2378
$expectedMsg = 'Ran jobs by schedule.' . PHP_EOL;
2479
$this->assertEquals($expectedMsg, $commandTester->getDisplay());

0 commit comments

Comments
 (0)