This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,332 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Deploy\Console\Command\App;
use Magento\Config\Model\Config\Export\ExcludeList;
use Magento\Config\Model\Config\TypePool;
use Magento\Deploy\Model\DeploymentConfig\Hash;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Config\File\ConfigFilePool;
use Magento\Framework\Filesystem;
use Magento\Framework\ObjectManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ApplicationDumpCommandTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var DeploymentConfig\FileReader
*/
private $reader;
/**
* @var ConfigFilePool
*/
private $configFilePool;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var DeploymentConfig\Writer
*/
private $writer;
/**
* @var array
*/
private $envConfig;
/**
* @var array
*/
private $config;
/**
* @var Hash
*/
private $hash;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->reader = $this->objectManager->get(DeploymentConfig\FileReader::class);
$this->filesystem = $this->objectManager->get(Filesystem::class);
$this->configFilePool = $this->objectManager->get(ConfigFilePool::class);
$this->reader = $this->objectManager->get(DeploymentConfig\Reader::class);
$this->writer = $this->objectManager->get(DeploymentConfig\Writer::class);
$this->configFilePool = $this->objectManager->get(ConfigFilePool::class);
$this->hash = $this->objectManager->get(Hash::class);
// Snapshot of configuration.
$this->config = $this->loadConfig();
$this->envConfig = $this->loadEnvConfig();
$this->writer->saveConfig(
[
ConfigFilePool::APP_CONFIG => [
'system' => [
'default' => [
'web' => [
'test' => [
'test_value_3' => 'value from the file'
]
]
]
]
]
],
true
);
}
/**
* @return array
*/
private function loadConfig()
{
return $this->reader->load(ConfigFilePool::APP_CONFIG);
}
/**
* @return array
*/
private function loadEnvConfig()
{
return $this->reader->load(ConfigFilePool::APP_ENV);
}
/**
* @return string
*/
private function loadRawConfig()
{
return $this->filesystem->getDirectoryRead(DirectoryList::CONFIG)
->readFile($this->configFilePool->getPath(ConfigFilePool::APP_CONFIG));
}
/**
* @magentoDbIsolation enabled
* @magentoDataFixture Magento/Deploy/_files/config_data.php
*/
public function testExecute()
{
$this->objectManager->configure([
ExcludeList::class => [
'arguments' => [
'configs' => [
'web/test/test_value_1' => '',
'web/test/test_value_2' => '0',
'web/test/test_sensitive' => '1',
],
],
],
TypePool::class => [
'arguments' => [
'sensitive' => [
'web/test/test_sensitive1' => '',
'web/test/test_sensitive2' => '0',
'web/test/test_sensitive3' => '1',
'web/test/test_sensitive_environment4' => '1',
'web/test/test_sensitive_environment5' => '1',
'web/test/test_sensitive_environment6' => '0',
],
'environment' => [
'web/test/test_sensitive_environment4' => '1',
'web/test/test_sensitive_environment5' => '0',
'web/test/test_sensitive_environment6' => '1',
'web/test/test_environment7' => '',
'web/test/test_environment8' => '0',
'web/test/test_environment9' => '1',
],
]
]
]);
$comment = implode(PHP_EOL, [
'Shared configuration was written to config.php and system-specific configuration to env.php.',
'Shared configuration file (config.php) doesn\'t contain sensitive data for security reasons.',
'Sensitive data can be stored in the following environment variables:',
'CONFIG__DEFAULT__WEB__TEST__TEST_SENSITIVE for web/test/test_sensitive',
'CONFIG__DEFAULT__WEB__TEST__TEST_SENSITIVE3 for web/test/test_sensitive3',
'CONFIG__DEFAULT__WEB__TEST__TEST_SENSITIVE_ENVIRONMENT4 for web/test/test_sensitive_environment4',
'CONFIG__DEFAULT__WEB__TEST__TEST_SENSITIVE_ENVIRONMENT5 for web/test/test_sensitive_environment5'
]);
$outputMock = $this->getMockForAbstractClass(OutputInterface::class);
$outputMock
->method('writeln')
->withConsecutive(
[['system' => $comment]],
[$this->matchesRegularExpression('/<info>Done. Config types dumped: [a-z0-9,\s]+<\/info>/')]
);
/** @var ApplicationDumpCommand command */
$command = $this->objectManager->create(ApplicationDumpCommand::class);
$command->run($this->getMockForAbstractClass(InputInterface::class), $outputMock);
$config = $this->loadConfig();
$this->validateSystemSection($config);
$this->validateThemesSection($config);
$configEnv = $this->loadEnvConfig();
$this->validateSystemEnvSection($configEnv);
$this->assertNotEmpty($this->hash->get());
$this->assertStringContainsString('For the section: system', $this->loadRawConfig());
}
/**
* Validates 'system' section in configuration data.
*
* @param array $config The configuration array
* @return void
*/
private function validateSystemSection(array $config)
{
$this->assertArrayHasKey('test_value_1', $config['system']['default']['web']['test']);
$this->assertArrayHasKey('test_value_2', $config['system']['default']['web']['test']);
$this->assertArrayHasKey('test_sensitive1', $config['system']['default']['web']['test']);
$this->assertArrayHasKey('test_sensitive2', $config['system']['default']['web']['test']);
$this->assertArrayHasKey('test_environment7', $config['system']['default']['web']['test']);
$this->assertArrayHasKey('test_environment8', $config['system']['default']['web']['test']);
$this->assertArrayNotHasKey('test_sensitive', $config['system']['default']['web']['test']);
$this->assertArrayNotHasKey('test_sensitive3', $config['system']['default']['web']['test']);
$this->assertArrayNotHasKey('test_sensitive_environment4', $config['system']['default']['web']['test']);
$this->assertArrayNotHasKey('test_sensitive_environment5', $config['system']['default']['web']['test']);
$this->assertArrayNotHasKey('test_sensitive_environment6', $config['system']['default']['web']['test']);
$this->assertArrayNotHasKey('test_environment9', $config['system']['default']['web']['test']);
/** @see Magento/Deploy/_files/config_data.php */
$this->assertEquals(
'frontend/Magento/blank',
$config['system']['default']['design']['theme']['theme_id']
);
$this->assertEquals(
'frontend/Magento/luma',
$config['system']['stores']['default']['design']['theme']['theme_id']
);
$this->assertEquals(
'frontend/Magento/luma',
$config['system']['websites']['base']['design']['theme']['theme_id']
);
$this->assertEquals('value from the file', $config['system']['default']['web']['test']['test_value_3']);
$this->assertEquals('GB', $config['system']['default']['general']['country']['default']);
$this->assertEquals(
'HK,IE,MO,PA,GB',
$config['system']['default']['general']['country']['optional_zip_countries']
);
}
/**
* Validates 'system' section in environment configuration data.
*
* @param array $config The configuration array
* @return void
*/
private function validateSystemEnvSection(array $config)
{
$envTestKeys = [
'test_sensitive',
'test_sensitive3',
'test_sensitive_environment4',
'test_sensitive_environment5',
'test_sensitive_environment6',
'test_environment9'
];
$this->assertEmpty(
array_diff($envTestKeys, array_keys($config['system']['default']['web']['test']))
);
}
/**
* Validates 'themes' section in configuration data.
*
* @param array $config The configuration array
* @return void
*/
private function validateThemesSection(array $config)
{
$this->assertEquals(
[
'parent_id' => null,
'theme_path' => 'Magento/backend',
'theme_title' => 'Magento 2 backend',
'is_featured' => '0',
'area' => 'adminhtml',
'type' => '0',
'code' => 'Magento/backend',
],
$config['themes']['adminhtml/Magento/backend']
);
$this->assertEquals(
[
'parent_id' => null,
'theme_path' => 'Magento/blank',
'theme_title' => 'Magento Blank',
'is_featured' => '0',
'area' => 'frontend',
'type' => '0',
'code' => 'Magento/blank',
],
$config['themes']['frontend/Magento/blank']
);
$this->assertEquals(
[
'parent_id' => 'Magento/blank',
'theme_path' => 'Magento/luma',
'theme_title' => 'Magento Luma',
'is_featured' => '0',
'area' => 'frontend',
'type' => '0',
'code' => 'Magento/luma',
],
$config['themes']['frontend/Magento/luma']
);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
$this->configFilePool->getPath(ConfigFilePool::APP_CONFIG),
"<?php\n return array();\n"
);
$this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
$this->configFilePool->getPath(ConfigFilePool::APP_ENV),
"<?php\n return array();\n"
);
/** @var DeploymentConfig\Writer $writer */
$writer = $this->objectManager->get(DeploymentConfig\Writer::class);
$writer->saveConfig([ConfigFilePool::APP_CONFIG => $this->config]);
/** @var DeploymentConfig\Writer $writer */
$writer = $this->objectManager->get(DeploymentConfig\Writer::class);
$writer->saveConfig([ConfigFilePool::APP_ENV => $this->envConfig]);
/** @var DeploymentConfig $deploymentConfig */
$deploymentConfig = $this->objectManager->get(DeploymentConfig::class);
$deploymentConfig->resetData();
}
}
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Deploy\Console\Command\App\ConfigImportCommand;
use Magento\Framework\App\DeploymentConfig\ImporterInterface;
class IntegrationTestImporter implements ImporterInterface
{
/**
* @param array $data
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function import(array $data)
{
$messages[] = '<info>Integration test data is imported!</info>';
return $messages;
}
public function getWarningMessages(array $data)
{
return [];
}
}
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Deploy\Console\Command\App\ConfigImportCommand;
use Magento\Framework\App\DeploymentConfig\ImporterInterface;
class IntegrationTestSecondImporter implements ImporterInterface
{
/**
* @param array $data
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function import(array $data)
{
$messages[] = '<info>Integration second test data is imported!</info>';
return $messages;
}
public function getWarningMessages(array $data)
{
return [];
}
}
@@ -0,0 +1,468 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Deploy\Console\Command\App;
use Magento\Config\Model\Config\Backend\Currency\Cron;
use Magento\Deploy\Model\DeploymentConfig\Hash;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Config\File\ConfigFilePool;
use Magento\Framework\Console\Cli;
use Magento\Framework\Filesystem;
use Magento\Framework\Flag;
use Magento\Framework\FlagFactory;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\GroupFactory;
use Magento\Store\Model\StoreFactory;
use Magento\Store\Model\WebsiteFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Symfony\Component\Console\Tester\CommandTester;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* phpcs:disable
*/
class ConfigImportCommandTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var DeploymentConfig\Reader
*/
private $reader;
/**
* @var DeploymentConfig\Writer
*/
private $writer;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var ConfigFilePool
*/
private $configFilePool;
/**
* @var Hash
*/
private $hash;
/**
* @var array
*/
private $config;
/**
* @var array
*/
private $envConfig;
/**
* @var FlagFactory
*/
private $flagFactory;
/**
* @var CacheInterface
*/
private $cacheManager;
/**
* @var ReinitableConfigInterface
*/
private $appConfig;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->reader = $this->objectManager->get(DeploymentConfig\Reader::class);
$this->writer = $this->objectManager->get(DeploymentConfig\Writer::class);
$this->filesystem = $this->objectManager->get(Filesystem::class);
$this->configFilePool = $this->objectManager->get(ConfigFilePool::class);
$this->hash = $this->objectManager->get(Hash::class);
$this->flagFactory = $this->objectManager->get(FlagFactory::class);
$this->cacheManager = $this->objectManager->get(CacheInterface::class);
$this->appConfig = $this->objectManager->get(ReinitableConfigInterface::class);
// Snapshot of configuration.
$this->config = $this->loadConfig();
$this->envConfig = $this->loadEnvConfig();
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
$this->configFilePool->getPath(ConfigFilePool::APP_CONFIG),
"<?php\n return array();\n"
);
$this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
$this->configFilePool->getPath(ConfigFilePool::APP_ENV),
"<?php\n return array();\n"
);
$this->writer->saveConfig([ConfigFilePool::APP_CONFIG => $this->config]);
$this->writer->saveConfig([ConfigFilePool::APP_ENV => $this->envConfig]);
$flag = $this->getFlag();
$flag->getResource()->delete($flag);
$this->appConfig->reinit();
}
/**
* @magentoDbIsolation enabled
*/
public function testExecuteNothingImport()
{
$this->assertEmpty($this->hash->get());
$command = $this->objectManager->create(ConfigImportCommand::class);
$commandTester = new CommandTester($command);
$commandTester->execute([], ['interactive' => false]);
$this->assertSame(Cli::RETURN_SUCCESS, $commandTester->getStatusCode());
$this->assertStringContainsString('Nothing to import.', $commandTester->getDisplay());
$this->assertEmpty($this->hash->get());
}
/**
* @magentoDbIsolation disabled
*/
public function testImportStores()
{
$this->assertEmpty($this->hash->get());
$dumpCommand = $this->objectManager->create(ApplicationDumpCommand::class);
$dumpCommandTester = new CommandTester($dumpCommand);
$dumpCommandTester->execute([]);
$dumpedData = $this->reader->load(ConfigFilePool::APP_CONFIG);
$this->writeConfig(
$dumpedData,
require __DIR__ . '/../../../_files/scopes/config_with_stores.php'
);
$command = $this->objectManager->create(ConfigImportCommand::class);
$commandTester = new CommandTester($command);
$this->runConfigImportCommand($commandTester, 'Stores were processed');
/** @var StoreFactory $storeFactory */
$storeFactory = $this->objectManager->get(StoreFactory::class);
/** @var WebsiteFactory $websiteFactory */
$websiteFactory = $this->objectManager->get(WebsiteFactory::class);
/** @var GroupFactory $groupFactory */
$groupFactory = $this->objectManager->get(GroupFactory::class);
$website = $websiteFactory->create()->load('test_website', 'code');
$this->assertSame($website->getName(), 'Test Website');
$group = $groupFactory->create();
$group->getResource()->load($group, 'test_website_store', 'code');
$this->assertSame($group->getName(), 'Test Website Store');
$this->assertSame($group->getWebsiteId(), $website->getId());
$store = $storeFactory->create();
$store->getResource()->load($store, 'test', 'code');
$this->assertSame($store->getSortOrder(), '23');
$this->assertSame($store->getName(), 'Test Store view');
$this->assertSame($store->getGroupId(), $group->getId());
$this->assertSame($store->getWebsiteId(), $website->getId());
$this->writeConfig(
$dumpedData,
require __DIR__ . '/../../../_files/scopes/config_with_changed_stores.php'
);
$this->runConfigImportCommand($commandTester, 'Stores were processed');
$store = $storeFactory->create();
$store->getResource()->load($store, 'test', 'code');
$this->assertSame($store->getSortOrder(), '23');
$this->assertSame($store->getName(), 'Changed Test Store view');
$this->assertSame($store->getGroupId(), $group->getId());
$this->assertSame($store->getWebsiteId(), $website->getId());
$website = $websiteFactory->create();
$website->getResource()->load($website, 'test_website', 'code');
$this->assertSame($website->getName(), 'Changed Test Website');
$group = $groupFactory->create();
$group->getResource()->load($group, 'test_website_store', 'code');
$this->assertSame($group->getName(), 'Changed Test Website Store');
$this->assertSame($website->getId(), $group->getWebsiteId());
$this->writeConfig(
$dumpedData,
require __DIR__ . '/../../../_files/scopes/config_with_removed_stores.php'
);
$this->runConfigImportCommand($commandTester, 'Stores were processed');
$group = $groupFactory->create();
$group->getResource()->load($group, 'test_website_store', 'code');
$store = $storeFactory->create();
$store->getResource()->load($store, 'test', 'code');
$website = $websiteFactory->create();
$website->getResource()->load($website, 'test_website', 'code');
$this->assertNull($store->getId());
$this->assertNull($website->getId());
$this->assertNull($group->getId());
}
/**
* @magentoDbIsolation disabled
*/
public function testImportStoresWithWrongConfiguration()
{
$this->assertEmpty($this->hash->get());
$dumpCommand = $this->objectManager->create(ApplicationDumpCommand::class);
$dumpCommandTester = new CommandTester($dumpCommand);
$dumpCommandTester->execute([]);
$dumpedData = $this->reader->load(ConfigFilePool::APP_CONFIG);
unset($dumpedData['scopes']['websites']['base']);
$this->writeConfig($dumpedData, []);
$importCommand = $this->objectManager->create(ConfigImportCommand::class);
$importCommandTester = new CommandTester($importCommand);
$importCommandTester->execute([]);
$this->assertStringContainsString(
'Scopes data should have at least one not admin website, group and store.',
$importCommandTester->getDisplay()
);
$this->assertSame(Cli::RETURN_FAILURE, $importCommandTester->getStatusCode());
}
/**
* @magentoDbIsolation enabled
*/
public function testImportConfig()
{
$correctData = [
'system' => [
'default' => [
'web' => [
'secure' => [
'base_url' => 'http://magento2.local/',
],
],
'currency' => [
'options' => [
'base' => 'USD',
'default' => 'EUR',
],
],
],
],
];
$wrongData = [
'system' => [
'default' => [
'web' => [
'secure' => [
'base_url' => 'wrong_url',
],
],
],
],
];
$wrongCurrency = [
'system' => [
'default' => [
'currency' => [
'options' => [
'default' => 'GBP',
],
],
],
],
];
$this->writeConfig($this->config, $correctData);
$command = $this->objectManager->create(ConfigImportCommand::class);
$commandTester = new CommandTester($command);
$commandTester->execute([], ['interactive' => false]);
$this->assertStringContainsString('System config was processed', $commandTester->getDisplay());
$this->assertSame(Cli::RETURN_SUCCESS, $commandTester->getStatusCode());
$this->writeConfig(
$this->config,
$wrongData
);
$commandTester->execute([]);
$this->assertStringContainsString(
'Invalid Secure Base URL. Value must be a URL or one of placeholders: {{base_url}},{{unsecure_base_url}}',
$commandTester->getDisplay()
);
$this->assertSame(Cli::RETURN_FAILURE, $commandTester->getStatusCode());
$this->writeConfig($this->config, $wrongCurrency);
$commandTester->execute([]);
$this->assertStringContainsString(
'Import failed: Sorry, the default display currency you selected is not available in allowed currencies.',
$commandTester->getDisplay()
);
$this->assertSame(Cli::RETURN_FAILURE, $commandTester->getStatusCode());
$this->writeConfig(
$this->config,
$correctData
);
$commandTester->execute([]);
$this->assertStringContainsString('Nothing to import', $commandTester->getDisplay());
$this->assertSame(Cli::RETURN_SUCCESS, $commandTester->getStatusCode());
}
/**
* @magentoDbIsolation disabled
*/
public function testImportCurrencyImportSchedule()
{
$this->assertEmpty($this->hash->get());
$dumpCommand = $this->objectManager->create(ApplicationDumpCommand::class);
$dumpCommandTester = new CommandTester($dumpCommand);
$dumpCommandTester->execute([]);
$dumpedData = $this->reader->load(ConfigFilePool::APP_CONFIG);
$command = $this->objectManager->create(ConfigImportCommand::class);
$commandTester = new CommandTester($command);
/** @var Cron $currencyImportSettings */
$currencyImportSettings = $this->objectManager->get(Cron::class);
/** @var $valueFactory \Magento\Framework\App\Config\ValueFactory */
$reflectionProperty = new \ReflectionProperty(
$currencyImportSettings,
'_configValueFactory'
);
$reflectionProperty->setAccessible(true);
$valueFactory = $reflectionProperty->getValue($currencyImportSettings);
/** @var $configValue \Magento\Framework\App\Config\ValueInterface */
$configValue = $valueFactory->create();
$hour = 6;
$min = 30;
$data = [
'system' => [
'default' => [
'currency' => [
'import' => [
'enabled' => '1',
'service' => 'fixerio',
'time' => sprintf("%02d", $hour) . ',' . sprintf("%02d", $min) . ',00',
'frequency' => 'D',
'error_email_identity' => 'general',
'error_email_template' => 'currency_import_error_email_template',
],
],
],
]
];
$this->writeConfig(
$dumpedData,
$data
);
$this->runConfigImportCommand($commandTester, 'System config was processed');
$configValue->load(Cron::CRON_STRING_PATH, 'path');
$configValue->getFieldsetDataValue('path');
$time = $configValue->getValue();
$this->assertSame($time, "$min $hour * * *");
}
/**
* Saves new data.
*
* @param array $originalData
* @param array $newData
*/
public function writeConfig(array $originalData, array $newData)
{
$this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
$this->configFilePool->getPath(ConfigFilePool::APP_CONFIG),
"<?php\n return array();\n"
);
$newData = array_replace_recursive(
$originalData,
$newData
);
$this->writer->saveConfig([ConfigFilePool::APP_CONFIG => $newData], true);
}
/**
* @return Flag
*/
private function getFlag()
{
$flag = $this->flagFactory->create();
$flag->getResource()->load($flag, Hash::CONFIG_KEY, 'flag_code');
return $flag;
}
/**
* @return array
*/
private function loadConfig()
{
return $this->reader->load(ConfigFilePool::APP_CONFIG);
}
/**
* @return array
*/
private function loadEnvConfig()
{
return $this->reader->load(ConfigFilePool::APP_ENV);
}
/**
* Runs ConfigImportCommand and asserts that command run successfully
*
* @param $commandTester
*/
private function runConfigImportCommand($commandTester, $assertPartialString)
{
$this->appConfig->reinit();
$commandTester->execute([], ['interactive' => false]);
$this->assertStringContainsString(
'Processing configurations data from configuration file...',
$commandTester->getDisplay()
);
$this->assertStringContainsString($assertPartialString, $commandTester->getDisplay());
$this->assertSame(Cli::RETURN_SUCCESS, $commandTester->getStatusCode());
}
}
@@ -0,0 +1,373 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Deploy\Console\Command\App;
use Magento\Deploy\Console\Command\App\SensitiveConfigSet\CollectorFactory;
use Magento\Deploy\Console\Command\App\SensitiveConfigSet\InteractiveCollector;
use Magento\Deploy\Console\Command\App\SensitiveConfigSet\SensitiveConfigSetFacade;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\DeploymentConfig\FileReader;
use Magento\Framework\App\DeploymentConfig\Writer;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Config\File\ConfigFilePool;
use Magento\Framework\Filesystem;
use Magento\Framework\ObjectManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class SensitiveConfigSetCommandTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var FileReader
*/
private $reader;
/**
* @var Writer
*/
private $writer;
/**
* @var ConfigFilePool
*/
private $configFilePool;
/**
* @var array
*/
private $envConfig;
/**
* @var array
*/
private $config;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->reader = $this->objectManager->get(FileReader::class);
$this->writer = $this->objectManager->get(Writer::class);
$this->configFilePool = $this->objectManager->get(ConfigFilePool::class);
$this->filesystem = $this->objectManager->get(Filesystem::class);
$this->envConfig = $this->loadEnvConfig();
$this->config = $this->loadConfig();
$this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
$this->configFilePool->getPath(ConfigFilePool::APP_CONFIG),
file_get_contents(__DIR__ . '/../../../_files/config.php')
);
}
/**
* @param $scope
* @param $scopeCode
* @param callable $assertCallback
* @magentoDataFixture Magento/Store/_files/website.php
* @magentoDbIsolation enabled
* @dataProvider executeDataProvider
* @return void
*/
public function testExecute($scope, $scopeCode, callable $assertCallback)
{
$outputMock = $this->getMockForAbstractClass(OutputInterface::class);
$outputMock
->method('writeln')
->withConsecutive(['<info>Configuration value saved in app/etc/env.php</info>']);
$inputMocks = [];
$inputMocks[] = $this->createInputMock(
'some/config/path_two',
'sensitiveValue',
$scope,
$scopeCode
);
$inputMocks[] = $this->createInputMock(
'some/config/path_three',
'sensitiveValue',
$scope,
$scopeCode
);
// attempt to overwrite existing value for path with null (should not be allowed)
$inputMocks[] = $this->createInputMock(
'some/config/path_three',
null,
$scope,
$scopeCode
);
foreach ($inputMocks as $inputMock) {
/** @var SensitiveConfigSetCommand command */
$command = $this->objectManager->create(SensitiveConfigSetCommand::class);
$command->run($inputMock, $outputMock);
}
$config = $this->loadEnvConfig();
$assertCallback($config);
}
public function executeDataProvider()
{
return [
[
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
null,
function (array $config) {
$this->assertTrue(isset($config['system']['default']['some']['config']['path_two']));
$this->assertTrue(isset($config['system']['default']['some']['config']['path_three']));
$this->assertEquals(
'sensitiveValue',
$config['system']['default']['some']['config']['path_two']
);
$this->assertEquals(
'sensitiveValue',
$config['system']['default']['some']['config']['path_three']
);
}
],
[
'website',
'test',
function (array $config) {
$this->assertTrue(isset($config['system']['website']['test']['some']['config']['path_two']));
$this->assertEquals(
'sensitiveValue',
$config['system']['website']['test']['some']['config']['path_two']
);
$this->assertEquals(
'sensitiveValue',
$config['system']['website']['test']['some']['config']['path_three']
);
}
]
];
}
/**
* @param $scope
* @param $scopeCode
* @param callable $assertCallback
* @magentoDataFixture Magento/Store/_files/website.php
* @magentoDbIsolation enabled
* @dataProvider executeInteractiveDataProvider
* @return void
*/
public function testExecuteInteractive($scope, $scopeCode, callable $assertCallback)
{
$inputMock = $this->createInputMock(null, null, $scope, $scopeCode);
$outputMock = $this->getMockForAbstractClass(OutputInterface::class);
$outputMock
->method('writeln')
->withConsecutive(
['<info>Please set configuration values or skip them by pressing [Enter]:</info>'],
['<info>Configuration values saved in app/etc/env.php</info>']
);
$command = $this->createInteractiveCommand('sensitiveValue');
$command->run($inputMock, $outputMock);
// attempt to overwrite existing value for path with null (should not be allowed)
$inputMock = $this->createInputMock(null, null, $scope, $scopeCode);
$command = $this->createInteractiveCommand(null);
$command->run($inputMock, $outputMock);
$config = $this->loadEnvConfig();
$assertCallback($config);
}
public function executeInteractiveDataProvider()
{
return [
[
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
null,
function (array $config) {
$this->assertTrue(isset($config['system']['default']['some']['config']['path_one']));
$this->assertTrue(isset($config['system']['default']['some']['config']['path_two']));
$this->assertTrue(isset($config['system']['default']['some']['config']['path_three']));
$this->assertEquals(
'sensitiveValue',
$config['system']['default']['some']['config']['path_one']
);
$this->assertEquals(
'sensitiveValue',
$config['system']['default']['some']['config']['path_two']
);
$this->assertEquals(
'sensitiveValue',
$config['system']['default']['some']['config']['path_three']
);
}
],
[
'website',
'test',
function (array $config) {
$this->assertTrue(isset($config['system']['website']['test']['some']['config']['path_one']));
$this->assertTrue(isset($config['system']['website']['test']['some']['config']['path_two']));
$this->assertTrue(isset($config['system']['website']['test']['some']['config']['path_three']));
$this->assertEquals(
'sensitiveValue',
$config['system']['website']['test']['some']['config']['path_one']
);
$this->assertEquals(
'sensitiveValue',
$config['system']['website']['test']['some']['config']['path_two']
);
$this->assertEquals(
'sensitiveValue',
$config['system']['website']['test']['some']['config']['path_three']
);
}
]
];
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
$this->configFilePool->getPath(ConfigFilePool::APP_CONFIG),
"<?php\n return array();\n"
);
$this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
$this->configFilePool->getPath(ConfigFilePool::APP_ENV),
"<?php\n return array();\n"
);
/** @var Writer $writer */
$writer = $this->objectManager->get(Writer::class);
$writer->saveConfig([ConfigFilePool::APP_ENV => $this->envConfig]);
/** @var Writer $writer */
$writer = $this->objectManager->get(Writer::class);
$writer->saveConfig([ConfigFilePool::APP_CONFIG => $this->config]);
}
/**
* @return array
*/
private function loadEnvConfig()
{
return $this->reader->load(ConfigFilePool::APP_ENV);
}
/**
* @return array
*/
private function loadConfig()
{
return $this->reader->load(ConfigFilePool::APP_CONFIG);
}
/**
* @param string|null $key
* @param string|null $val
* @param string $scope
* @param string|null $scopeCode
* @return InputInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private function createInputMock($key, $val, $scope, $scopeCode)
{
$inputMock = $this->getMockForAbstractClass(InputInterface::class);
$isInteractive = $key === null;
if (!$isInteractive) {
$inputMock->expects($this->exactly(2))
->method('getArgument')
->withConsecutive(
[SensitiveConfigSetCommand::INPUT_ARGUMENT_PATH],
[SensitiveConfigSetCommand::INPUT_ARGUMENT_VALUE]
)
->willReturnOnConsecutiveCalls(
$key,
$val
);
}
$inputMock->expects($this->exactly(3))
->method('getOption')
->withConsecutive(
[SensitiveConfigSetCommand::INPUT_OPTION_SCOPE],
[SensitiveConfigSetCommand::INPUT_OPTION_SCOPE_CODE],
[SensitiveConfigSetCommand::INPUT_OPTION_INTERACTIVE]
)
->willReturnOnConsecutiveCalls(
$scope,
$scopeCode,
$isInteractive
);
return $inputMock;
}
/**
* @param string|null $inputValue
* @return SensitiveConfigSetCommand
*/
private function createInteractiveCommand($inputValue)
{
$questionHelperMock = $this->createMock(QuestionHelper::class);
$questionHelperMock->expects($this->exactly(3))
->method('ask')
->willReturn($inputValue);
$interactiveCollectorMock = $this->objectManager->create(
InteractiveCollector::class,
[
'questionHelper' => $questionHelperMock
]
);
$collectorFactoryMock = $this->getMockBuilder(CollectorFactory::class)
->disableOriginalConstructor()
->getMock();
$collectorFactoryMock->expects($this->once())
->method('create')
->with(CollectorFactory::TYPE_INTERACTIVE)
->willReturn($interactiveCollectorMock);
/** @var SensitiveConfigSetCommand command */
$command = $this->objectManager->create(
SensitiveConfigSetCommand::class,
[
'facade' => $this->objectManager->create(
SensitiveConfigSetFacade::class,
[
'collectorFactory' => $collectorFactoryMock
]
)
]
);
return $command;
}
}