Skip to content

Commit 526619c

Browse files
ENGCOM-5080: Adds module:config:status command which checks if the module config i… #22733
- Merge Pull Request #22733 from hostep/magento2:adds-module-config-status-command - Merged commits: 1. 69a1b98 2. d510642
2 parents f17ca76 + d510642 commit 526619c

File tree

4 files changed

+213
-0
lines changed

4 files changed

+213
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Setup\Console\Command;
10+
11+
use Magento\Framework\App\DeploymentConfig\Reader as ConfigReader;
12+
use Magento\Framework\Config\ConfigOptionsListConstants;
13+
use Magento\Framework\Config\File\ConfigFilePool;
14+
use Magento\Framework\Console\Cli;
15+
use Magento\Framework\Setup\ConsoleLogger;
16+
use Magento\Setup\Model\InstallerFactory;
17+
use Symfony\Component\Console\Command\Command;
18+
use Symfony\Component\Console\Input\InputInterface;
19+
use Symfony\Component\Console\Output\OutputInterface;
20+
21+
/**
22+
* Command to check if the modules config in app/etc/config.php matches with how Magento interprets it
23+
*/
24+
class ModuleConfigStatusCommand extends Command
25+
{
26+
/**
27+
* Deployment config reader
28+
*
29+
* @var ConfigReader
30+
*/
31+
private $configReader;
32+
33+
/**
34+
* Installer service factory.
35+
*
36+
* @var InstallerFactory
37+
*/
38+
private $installerFactory;
39+
40+
/**
41+
* @param ConfigReader $configReader
42+
* @param InstallerFactory $installerFactory
43+
*/
44+
public function __construct(
45+
ConfigReader $configReader,
46+
InstallerFactory $installerFactory
47+
) {
48+
$this->configReader = $configReader;
49+
$this->installerFactory = $installerFactory;
50+
51+
parent::__construct();
52+
}
53+
54+
/**
55+
* @inheritdoc
56+
*/
57+
protected function configure()
58+
{
59+
$this
60+
->setName('module:config:status')
61+
->setDescription(
62+
'Checks the modules configuration in the \'app/etc/config.php\' file '
63+
. 'and reports if they are up to date or not'
64+
);
65+
66+
parent::configure();
67+
}
68+
69+
/**
70+
* @inheritdoc
71+
*/
72+
protected function execute(InputInterface $input, OutputInterface $output)
73+
{
74+
try {
75+
// the config as currently in app/etc/config.php
76+
$currentConfig = $this->configReader->load(ConfigFilePool::APP_CONFIG);
77+
if (!array_key_exists(ConfigOptionsListConstants::KEY_MODULES, $currentConfig)) {
78+
// phpcs:ignore Magento2.Exceptions.DirectThrow
79+
throw new \Exception('Can\'t find the modules configuration in the \'app/etc/config.php\' file.');
80+
}
81+
82+
$currentModuleConfig = $currentConfig[ConfigOptionsListConstants::KEY_MODULES];
83+
84+
$installer = $this->installerFactory->create(new ConsoleLogger($output));
85+
86+
// the module config as Magento calculated it
87+
$correctModuleConfig = $installer->getModulesConfig();
88+
89+
if ($currentModuleConfig !== $correctModuleConfig) {
90+
// phpcs:ignore Magento2.Exceptions.DirectThrow
91+
throw new \Exception(
92+
'The modules configuration in the \'app/etc/config.php\' file is outdated. '
93+
. 'Run \'setup:upgrade\' to fix it.'
94+
);
95+
}
96+
97+
$output->writeln(
98+
'<info>The modules configuration is up to date.</info>'
99+
);
100+
// phpcs:disable Magento2.Exceptions.ThrowCatch
101+
} catch (\Exception $e) {
102+
$output->writeln('<error>' . $e->getMessage() . '</error>');
103+
104+
return Cli::RETURN_FAILURE;
105+
}
106+
107+
return Cli::RETURN_SUCCESS;
108+
}
109+
}

setup/src/Magento/Setup/Console/CommandList.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ protected function getCommandsClasses()
6666
\Magento\Setup\Console\Command\ModuleDisableCommand::class,
6767
\Magento\Setup\Console\Command\ModuleStatusCommand::class,
6868
\Magento\Setup\Console\Command\ModuleUninstallCommand::class,
69+
\Magento\Setup\Console\Command\ModuleConfigStatusCommand::class,
6970
\Magento\Setup\Console\Command\MaintenanceAllowIpsCommand::class,
7071
\Magento\Setup\Console\Command\MaintenanceDisableCommand::class,
7172
\Magento\Setup\Console\Command\MaintenanceEnableCommand::class,
@@ -91,6 +92,7 @@ public function getCommands()
9192
if (class_exists($class)) {
9293
$commands[] = $this->serviceManager->get($class);
9394
} else {
95+
// phpcs:ignore Magento2.Exceptions.DirectThrow
9496
throw new \Exception('Class ' . $class . ' does not exist');
9597
}
9698
}

setup/src/Magento/Setup/Model/Installer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,17 @@ public function updateModulesSequence($keepGeneratedFiles = false)
12001200
$this->createModulesConfig([]);
12011201
}
12021202

1203+
/**
1204+
* Get the modules config as Magento sees it
1205+
*
1206+
* @return array
1207+
* @throws \LogicException
1208+
*/
1209+
public function getModulesConfig()
1210+
{
1211+
return $this->createModulesConfig([], true);
1212+
}
1213+
12031214
/**
12041215
* Uninstall Magento application
12051216
*
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Setup\Test\Unit\Console\Command;
10+
11+
use Magento\Framework\Config\ConfigOptionsListConstants;
12+
use Magento\Setup\Console\Command\ModuleConfigStatusCommand;
13+
use PHPUnit\Framework\TestCase;
14+
use Symfony\Component\Console\Tester\CommandTester;
15+
16+
/**
17+
* Tests for module config status command.
18+
*/
19+
class ModuleConfigStatusCommandTest extends TestCase
20+
{
21+
/**
22+
* @param array $currentConfig
23+
* @param array $correctConfig
24+
* @param string $expectedOutput
25+
* @dataProvider executeDataProvider
26+
*/
27+
public function testExecute(array $currentConfig, array $correctConfig, string $expectedOutput)
28+
{
29+
$configReader = $this->createMock(\Magento\Framework\App\DeploymentConfig\Reader::class);
30+
$configReader->expects($this->once())
31+
->method('load')
32+
->willReturn([ConfigOptionsListConstants::KEY_MODULES => $currentConfig]);
33+
34+
$installer = $this->createMock(\Magento\Setup\Model\Installer::class);
35+
$installer->expects($this->once())
36+
->method('getModulesConfig')
37+
->willReturn($correctConfig);
38+
39+
$installerFactory = $this->createMock(\Magento\Setup\Model\InstallerFactory::class);
40+
$installerFactory->expects($this->once())
41+
->method('create')
42+
->willReturn($installer);
43+
44+
$command = new ModuleConfigStatusCommand($configReader, $installerFactory);
45+
46+
$tester = new CommandTester($command);
47+
$tester->execute([]);
48+
49+
$this->assertEquals($expectedOutput, $tester->getDisplay());
50+
}
51+
52+
public function executeDataProvider()
53+
{
54+
$successMessage = 'The modules configuration is up to date.' . PHP_EOL;
55+
$failureMessage = 'The modules configuration in the \'app/etc/config.php\' '
56+
. 'file is outdated. Run \'setup:upgrade\' to fix it.' . PHP_EOL;
57+
58+
return [
59+
[
60+
['Magento_ModuleA' => 1, 'Magento_ModuleB' => 1],
61+
['Magento_ModuleA' => 1, 'Magento_ModuleB' => 1],
62+
$successMessage,
63+
],
64+
[
65+
['Magento_ModuleA' => 0, 'Magento_ModuleB' => 1],
66+
['Magento_ModuleA' => 0, 'Magento_ModuleB' => 1],
67+
$successMessage,
68+
],
69+
[
70+
['Magento_ModuleA' => 1, 'Magento_ModuleB' => 1],
71+
['Magento_ModuleB' => 1, 'Magento_ModuleA' => 1],
72+
$failureMessage,
73+
],
74+
[
75+
['Magento_ModuleA' => 0, 'Magento_ModuleB' => 1],
76+
['Magento_ModuleB' => 1, 'Magento_ModuleA' => 0],
77+
$failureMessage,
78+
],
79+
[
80+
['Magento_ModuleA' => 1],
81+
['Magento_ModuleB' => 1, 'Magento_ModuleA' => 1],
82+
$failureMessage,
83+
],
84+
[
85+
['Magento_ModuleA' => 1, 'Magento_ModuleB' => 1],
86+
['Magento_ModuleB' => 1],
87+
$failureMessage,
88+
],
89+
];
90+
}
91+
}

0 commit comments

Comments
 (0)