Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds module:config:status command which checks if the module config i… #22733

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions setup/src/Magento/Setup/Console/Command/ModuleConfigStatusCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Setup\Console\Command;

use Magento\Framework\App\DeploymentConfig\Reader as ConfigReader;
use Magento\Framework\Config\ConfigOptionsListConstants;
use Magento\Framework\Config\File\ConfigFilePool;
use Magento\Framework\Console\Cli;
use Magento\Framework\Setup\ConsoleLogger;
use Magento\Setup\Model\InstallerFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command to check if the modules config in app/etc/config.php matches with how Magento interprets it
*/
class ModuleConfigStatusCommand extends Command
{
/**
* Deployment config reader
*
* @var ConfigReader
*/
private $configReader;

/**
* Installer service factory.
*
* @var InstallerFactory
*/
private $installerFactory;

/**
* @param ConfigReader $configReader
* @param InstallerFactory $installerFactory
*/
public function __construct(
ConfigReader $configReader,
InstallerFactory $installerFactory
) {
$this->configReader = $configReader;
$this->installerFactory = $installerFactory;

parent::__construct();
}

/**
* @inheritdoc
*/
protected function configure()
{
$this
->setName('module:config:status')
->setDescription(
'Checks the modules configuration in the \'app/etc/config.php\' file '
. 'and reports if they are up to date or not'
);

parent::configure();
}

/**
* @inheritdoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
// the config as currently in app/etc/config.php
$currentConfig = $this->configReader->load(ConfigFilePool::APP_CONFIG);
if (!array_key_exists(ConfigOptionsListConstants::KEY_MODULES, $currentConfig)) {
// phpcs:ignore Magento2.Exceptions.DirectThrow
throw new \Exception('Can\'t find the modules configuration in the \'app/etc/config.php\' file.');
}

$currentModuleConfig = $currentConfig[ConfigOptionsListConstants::KEY_MODULES];

$installer = $this->installerFactory->create(new ConsoleLogger($output));

// the module config as Magento calculated it
$correctModuleConfig = $installer->getModulesConfig();

if ($currentModuleConfig !== $correctModuleConfig) {
// phpcs:ignore Magento2.Exceptions.DirectThrow
throw new \Exception(
'The modules configuration in the \'app/etc/config.php\' file is outdated. '
. 'Run \'setup:upgrade\' to fix it.'
);
}

$output->writeln(
'<info>The modules configuration is up to date.</info>'
);
// phpcs:disable Magento2.Exceptions.ThrowCatch
} catch (\Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');

return Cli::RETURN_FAILURE;
}

return Cli::RETURN_SUCCESS;
}
}
2 changes: 2 additions & 0 deletions setup/src/Magento/Setup/Console/CommandList.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ protected function getCommandsClasses()
\Magento\Setup\Console\Command\ModuleDisableCommand::class,
\Magento\Setup\Console\Command\ModuleStatusCommand::class,
\Magento\Setup\Console\Command\ModuleUninstallCommand::class,
\Magento\Setup\Console\Command\ModuleConfigStatusCommand::class,
\Magento\Setup\Console\Command\MaintenanceAllowIpsCommand::class,
\Magento\Setup\Console\Command\MaintenanceDisableCommand::class,
\Magento\Setup\Console\Command\MaintenanceEnableCommand::class,
Expand All @@ -91,6 +92,7 @@ public function getCommands()
if (class_exists($class)) {
$commands[] = $this->serviceManager->get($class);
} else {
// phpcs:ignore Magento2.Exceptions.DirectThrow
throw new \Exception('Class ' . $class . ' does not exist');
}
}
Expand Down
11 changes: 11 additions & 0 deletions setup/src/Magento/Setup/Model/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,17 @@ public function updateModulesSequence($keepGeneratedFiles = false)
$this->createModulesConfig([]);
}

/**
* Get the modules config as Magento sees it
*
* @return array
* @throws \LogicException
*/
public function getModulesConfig()
dmytro-ch marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->createModulesConfig([], true);
}

/**
* Uninstall Magento application
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Setup\Test\Unit\Console\Command;

use Magento\Framework\Config\ConfigOptionsListConstants;
use Magento\Setup\Console\Command\ModuleConfigStatusCommand;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;

/**
* Tests for module config status command.
*/
class ModuleConfigStatusCommandTest extends TestCase
{
/**
* @param array $currentConfig
* @param array $correctConfig
* @param string $expectedOutput
* @dataProvider executeDataProvider
*/
public function testExecute(array $currentConfig, array $correctConfig, string $expectedOutput)
{
$configReader = $this->createMock(\Magento\Framework\App\DeploymentConfig\Reader::class);
$configReader->expects($this->once())
->method('load')
->willReturn([ConfigOptionsListConstants::KEY_MODULES => $currentConfig]);

$installer = $this->createMock(\Magento\Setup\Model\Installer::class);
$installer->expects($this->once())
->method('getModulesConfig')
->willReturn($correctConfig);

$installerFactory = $this->createMock(\Magento\Setup\Model\InstallerFactory::class);
$installerFactory->expects($this->once())
->method('create')
->willReturn($installer);

$command = new ModuleConfigStatusCommand($configReader, $installerFactory);

$tester = new CommandTester($command);
$tester->execute([]);

$this->assertEquals($expectedOutput, $tester->getDisplay());
}

public function executeDataProvider()
{
$successMessage = 'The modules configuration is up to date.' . PHP_EOL;
$failureMessage = 'The modules configuration in the \'app/etc/config.php\' '
. 'file is outdated. Run \'setup:upgrade\' to fix it.' . PHP_EOL;

return [
[
['Magento_ModuleA' => 1, 'Magento_ModuleB' => 1],
['Magento_ModuleA' => 1, 'Magento_ModuleB' => 1],
$successMessage,
],
[
['Magento_ModuleA' => 0, 'Magento_ModuleB' => 1],
['Magento_ModuleA' => 0, 'Magento_ModuleB' => 1],
$successMessage,
],
[
['Magento_ModuleA' => 1, 'Magento_ModuleB' => 1],
['Magento_ModuleB' => 1, 'Magento_ModuleA' => 1],
$failureMessage,
],
[
['Magento_ModuleA' => 0, 'Magento_ModuleB' => 1],
['Magento_ModuleB' => 1, 'Magento_ModuleA' => 0],
$failureMessage,
],
[
['Magento_ModuleA' => 1],
['Magento_ModuleB' => 1, 'Magento_ModuleA' => 1],
$failureMessage,
],
[
['Magento_ModuleA' => 1, 'Magento_ModuleB' => 1],
['Magento_ModuleB' => 1],
$failureMessage,
],
];
}
}