Skip to content

Commit 69a1b98

Browse files
committed
Adds module:config:status command which checks if the module config in the app/etc/config.php file is correct or not.
1 parent c20732c commit 69a1b98

File tree

4 files changed

+200
-0
lines changed

4 files changed

+200
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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->setName('module:config:status')
60+
->setDescription('Checks the modules configuration in the \'app/etc/config.php\' file and reports if they are up to date or not');
61+
62+
parent::configure();
63+
}
64+
65+
/**
66+
* @inheritdoc
67+
*/
68+
protected function execute(InputInterface $input, OutputInterface $output)
69+
{
70+
try {
71+
// the config as currently in app/etc/config.php
72+
$currentConfig = $this->configReader->load(ConfigFilePool::APP_CONFIG);
73+
if (!array_key_exists(ConfigOptionsListConstants::KEY_MODULES, $currentConfig)) {
74+
throw new \Exception('Can\'t find the modules configuration in the \'app/etc/config.php\' file.');
75+
}
76+
77+
$currentModuleConfig = $currentConfig[ConfigOptionsListConstants::KEY_MODULES];
78+
79+
$installer = $this->installerFactory->create(new ConsoleLogger($output));
80+
81+
// the module config as Magento calculated it
82+
$correctModuleConfig = $installer->getModulesConfig();
83+
84+
if ($currentModuleConfig !== $correctModuleConfig) {
85+
throw new \Exception(
86+
'The modules configuration in the \'app/etc/config.php\' file is outdated. Run \'setup:upgrade\' to fix it.'
87+
);
88+
}
89+
90+
$output->writeln(
91+
'<info>The modules configuration is up to date.</info>'
92+
);
93+
} catch (\Exception $e) {
94+
$output->writeln('<error>' . $e->getMessage() . '</error>');
95+
96+
return Cli::RETURN_FAILURE;
97+
}
98+
99+
return Cli::RETURN_SUCCESS;
100+
}
101+
}

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

Lines changed: 1 addition & 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,

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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
class ModuleConfigStatusCommandTest extends TestCase
17+
{
18+
/**
19+
* @param array $currentConfig
20+
* @param array $correctConfig
21+
* @param string $expectedOutput
22+
* @dataProvider executeDataProvider
23+
*/
24+
public function testExecute(array $currentConfig, array $correctConfig, string $expectedOutput)
25+
{
26+
$configReader = $this->createMock(\Magento\Framework\App\DeploymentConfig\Reader::class);
27+
$configReader->expects($this->once())
28+
->method('load')
29+
->willReturn([ConfigOptionsListConstants::KEY_MODULES => $currentConfig]);
30+
31+
$installer = $this->createMock(\Magento\Setup\Model\Installer::class);
32+
$installer->expects($this->once())
33+
->method('getModulesConfig')
34+
->willReturn($correctConfig);
35+
36+
$installerFactory = $this->createMock(\Magento\Setup\Model\InstallerFactory::class);
37+
$installerFactory->expects($this->once())
38+
->method('create')
39+
->willReturn($installer);
40+
41+
$command = new ModuleConfigStatusCommand($configReader, $installerFactory);
42+
43+
$tester = new CommandTester($command);
44+
$tester->execute([]);
45+
46+
$this->assertEquals($expectedOutput, $tester->getDisplay());
47+
}
48+
49+
public function executeDataProvider()
50+
{
51+
$successMessage = 'The modules configuration is up to date.' . PHP_EOL;
52+
$failureMessage = 'The modules configuration in the \'app/etc/config.php\' file is outdated. Run \'setup:upgrade\' to fix it.' . PHP_EOL;
53+
54+
return [
55+
[
56+
['Magento_ModuleA' => 1, 'Magento_ModuleB' => 1],
57+
['Magento_ModuleA' => 1, 'Magento_ModuleB' => 1],
58+
$successMessage,
59+
],
60+
[
61+
['Magento_ModuleA' => 0, 'Magento_ModuleB' => 1],
62+
['Magento_ModuleA' => 0, 'Magento_ModuleB' => 1],
63+
$successMessage,
64+
],
65+
[
66+
['Magento_ModuleA' => 1, 'Magento_ModuleB' => 1],
67+
['Magento_ModuleB' => 1, 'Magento_ModuleA' => 1],
68+
$failureMessage,
69+
],
70+
[
71+
['Magento_ModuleA' => 0, 'Magento_ModuleB' => 1],
72+
['Magento_ModuleB' => 1, 'Magento_ModuleA' => 0],
73+
$failureMessage,
74+
],
75+
[
76+
['Magento_ModuleA' => 1],
77+
['Magento_ModuleB' => 1, 'Magento_ModuleA' => 1],
78+
$failureMessage,
79+
],
80+
[
81+
['Magento_ModuleA' => 1, 'Magento_ModuleB' => 1],
82+
['Magento_ModuleB' => 1],
83+
$failureMessage,
84+
],
85+
];
86+
}
87+
}

0 commit comments

Comments
 (0)