123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- <?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
- */
- public function setUp()
- {
- $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->createMock(OutputInterface::class);
- $outputMock->expects($this->at(0))
- ->method('writeln')
- ->with('<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->createMock(OutputInterface::class);
- $outputMock->expects($this->at(0))
- ->method('writeln')
- ->with('<info>Please set configuration values or skip them by pressing [Enter]:</info>');
- $outputMock->expects($this->at(1))
- ->method('writeln')
- ->with('<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
- */
- public function tearDown()
- {
- $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->createMock(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;
- }
- }
|