SensitiveConfigSetCommandTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Console\Command\App;
  7. use Magento\Deploy\Console\Command\App\SensitiveConfigSet\CollectorFactory;
  8. use Magento\Deploy\Console\Command\App\SensitiveConfigSet\InteractiveCollector;
  9. use Magento\Deploy\Console\Command\App\SensitiveConfigSet\SensitiveConfigSetFacade;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. use Magento\Framework\App\DeploymentConfig\FileReader;
  12. use Magento\Framework\App\DeploymentConfig\Writer;
  13. use Magento\Framework\App\Filesystem\DirectoryList;
  14. use Magento\Framework\Config\File\ConfigFilePool;
  15. use Magento\Framework\Filesystem;
  16. use Magento\Framework\ObjectManagerInterface;
  17. use Magento\TestFramework\Helper\Bootstrap;
  18. use Symfony\Component\Console\Helper\QuestionHelper;
  19. use Symfony\Component\Console\Input\InputInterface;
  20. use Symfony\Component\Console\Output\OutputInterface;
  21. /**
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class SensitiveConfigSetCommandTest extends \PHPUnit\Framework\TestCase
  25. {
  26. /**
  27. * @var ObjectManagerInterface
  28. */
  29. private $objectManager;
  30. /**
  31. * @var FileReader
  32. */
  33. private $reader;
  34. /**
  35. * @var Writer
  36. */
  37. private $writer;
  38. /**
  39. * @var ConfigFilePool
  40. */
  41. private $configFilePool;
  42. /**
  43. * @var array
  44. */
  45. private $envConfig;
  46. /**
  47. * @var array
  48. */
  49. private $config;
  50. /**
  51. * @var Filesystem
  52. */
  53. private $filesystem;
  54. /**
  55. * @inheritdoc
  56. */
  57. public function setUp()
  58. {
  59. $this->objectManager = Bootstrap::getObjectManager();
  60. $this->reader = $this->objectManager->get(FileReader::class);
  61. $this->writer = $this->objectManager->get(Writer::class);
  62. $this->configFilePool = $this->objectManager->get(ConfigFilePool::class);
  63. $this->filesystem = $this->objectManager->get(Filesystem::class);
  64. $this->envConfig = $this->loadEnvConfig();
  65. $this->config = $this->loadConfig();
  66. $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
  67. $this->configFilePool->getPath(ConfigFilePool::APP_CONFIG),
  68. file_get_contents(__DIR__ . '/../../../_files/config.php')
  69. );
  70. }
  71. /**
  72. * @param $scope
  73. * @param $scopeCode
  74. * @param callable $assertCallback
  75. * @magentoDataFixture Magento/Store/_files/website.php
  76. * @magentoDbIsolation enabled
  77. * @dataProvider executeDataProvider
  78. * @return void
  79. */
  80. public function testExecute($scope, $scopeCode, callable $assertCallback)
  81. {
  82. $outputMock = $this->createMock(OutputInterface::class);
  83. $outputMock->expects($this->at(0))
  84. ->method('writeln')
  85. ->with('<info>Configuration value saved in app/etc/env.php</info>');
  86. $inputMocks = [];
  87. $inputMocks[] = $this->createInputMock(
  88. 'some/config/path_two',
  89. 'sensitiveValue',
  90. $scope,
  91. $scopeCode
  92. );
  93. $inputMocks[] = $this->createInputMock(
  94. 'some/config/path_three',
  95. 'sensitiveValue',
  96. $scope,
  97. $scopeCode
  98. );
  99. // attempt to overwrite existing value for path with null (should not be allowed)
  100. $inputMocks[] = $this->createInputMock(
  101. 'some/config/path_three',
  102. null,
  103. $scope,
  104. $scopeCode
  105. );
  106. foreach ($inputMocks as $inputMock) {
  107. /** @var SensitiveConfigSetCommand command */
  108. $command = $this->objectManager->create(SensitiveConfigSetCommand::class);
  109. $command->run($inputMock, $outputMock);
  110. }
  111. $config = $this->loadEnvConfig();
  112. $assertCallback($config);
  113. }
  114. public function executeDataProvider()
  115. {
  116. return [
  117. [
  118. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  119. null,
  120. function (array $config) {
  121. $this->assertTrue(isset($config['system']['default']['some']['config']['path_two']));
  122. $this->assertTrue(isset($config['system']['default']['some']['config']['path_three']));
  123. $this->assertEquals(
  124. 'sensitiveValue',
  125. $config['system']['default']['some']['config']['path_two']
  126. );
  127. $this->assertEquals(
  128. 'sensitiveValue',
  129. $config['system']['default']['some']['config']['path_three']
  130. );
  131. }
  132. ],
  133. [
  134. 'website',
  135. 'test',
  136. function (array $config) {
  137. $this->assertTrue(isset($config['system']['website']['test']['some']['config']['path_two']));
  138. $this->assertEquals(
  139. 'sensitiveValue',
  140. $config['system']['website']['test']['some']['config']['path_two']
  141. );
  142. $this->assertEquals(
  143. 'sensitiveValue',
  144. $config['system']['website']['test']['some']['config']['path_three']
  145. );
  146. }
  147. ]
  148. ];
  149. }
  150. /**
  151. * @param $scope
  152. * @param $scopeCode
  153. * @param callable $assertCallback
  154. * @magentoDataFixture Magento/Store/_files/website.php
  155. * @magentoDbIsolation enabled
  156. * @dataProvider executeInteractiveDataProvider
  157. * @return void
  158. */
  159. public function testExecuteInteractive($scope, $scopeCode, callable $assertCallback)
  160. {
  161. $inputMock = $this->createInputMock(null, null, $scope, $scopeCode);
  162. $outputMock = $this->createMock(OutputInterface::class);
  163. $outputMock->expects($this->at(0))
  164. ->method('writeln')
  165. ->with('<info>Please set configuration values or skip them by pressing [Enter]:</info>');
  166. $outputMock->expects($this->at(1))
  167. ->method('writeln')
  168. ->with('<info>Configuration values saved in app/etc/env.php</info>');
  169. $command = $this->createInteractiveCommand('sensitiveValue');
  170. $command->run($inputMock, $outputMock);
  171. // attempt to overwrite existing value for path with null (should not be allowed)
  172. $inputMock = $this->createInputMock(null, null, $scope, $scopeCode);
  173. $command = $this->createInteractiveCommand(null);
  174. $command->run($inputMock, $outputMock);
  175. $config = $this->loadEnvConfig();
  176. $assertCallback($config);
  177. }
  178. public function executeInteractiveDataProvider()
  179. {
  180. return [
  181. [
  182. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  183. null,
  184. function (array $config) {
  185. $this->assertTrue(isset($config['system']['default']['some']['config']['path_one']));
  186. $this->assertTrue(isset($config['system']['default']['some']['config']['path_two']));
  187. $this->assertTrue(isset($config['system']['default']['some']['config']['path_three']));
  188. $this->assertEquals(
  189. 'sensitiveValue',
  190. $config['system']['default']['some']['config']['path_one']
  191. );
  192. $this->assertEquals(
  193. 'sensitiveValue',
  194. $config['system']['default']['some']['config']['path_two']
  195. );
  196. $this->assertEquals(
  197. 'sensitiveValue',
  198. $config['system']['default']['some']['config']['path_three']
  199. );
  200. }
  201. ],
  202. [
  203. 'website',
  204. 'test',
  205. function (array $config) {
  206. $this->assertTrue(isset($config['system']['website']['test']['some']['config']['path_one']));
  207. $this->assertTrue(isset($config['system']['website']['test']['some']['config']['path_two']));
  208. $this->assertTrue(isset($config['system']['website']['test']['some']['config']['path_three']));
  209. $this->assertEquals(
  210. 'sensitiveValue',
  211. $config['system']['website']['test']['some']['config']['path_one']
  212. );
  213. $this->assertEquals(
  214. 'sensitiveValue',
  215. $config['system']['website']['test']['some']['config']['path_two']
  216. );
  217. $this->assertEquals(
  218. 'sensitiveValue',
  219. $config['system']['website']['test']['some']['config']['path_three']
  220. );
  221. }
  222. ]
  223. ];
  224. }
  225. /**
  226. * @inheritdoc
  227. */
  228. public function tearDown()
  229. {
  230. $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
  231. $this->configFilePool->getPath(ConfigFilePool::APP_CONFIG),
  232. "<?php\n return array();\n"
  233. );
  234. $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
  235. $this->configFilePool->getPath(ConfigFilePool::APP_ENV),
  236. "<?php\n return array();\n"
  237. );
  238. /** @var Writer $writer */
  239. $writer = $this->objectManager->get(Writer::class);
  240. $writer->saveConfig([ConfigFilePool::APP_ENV => $this->envConfig]);
  241. /** @var Writer $writer */
  242. $writer = $this->objectManager->get(Writer::class);
  243. $writer->saveConfig([ConfigFilePool::APP_CONFIG => $this->config]);
  244. }
  245. /**
  246. * @return array
  247. */
  248. private function loadEnvConfig()
  249. {
  250. return $this->reader->load(ConfigFilePool::APP_ENV);
  251. }
  252. /**
  253. * @return array
  254. */
  255. private function loadConfig()
  256. {
  257. return $this->reader->load(ConfigFilePool::APP_CONFIG);
  258. }
  259. /**
  260. * @param string|null $key
  261. * @param string|null $val
  262. * @param string $scope
  263. * @param string|null $scopeCode
  264. * @return InputInterface|\PHPUnit_Framework_MockObject_MockObject
  265. */
  266. private function createInputMock($key, $val, $scope, $scopeCode)
  267. {
  268. $inputMock = $this->createMock(InputInterface::class);
  269. $isInteractive = $key === null;
  270. if (!$isInteractive) {
  271. $inputMock->expects($this->exactly(2))
  272. ->method('getArgument')
  273. ->withConsecutive(
  274. [SensitiveConfigSetCommand::INPUT_ARGUMENT_PATH],
  275. [SensitiveConfigSetCommand::INPUT_ARGUMENT_VALUE]
  276. )
  277. ->willReturnOnConsecutiveCalls(
  278. $key,
  279. $val
  280. );
  281. }
  282. $inputMock->expects($this->exactly(3))
  283. ->method('getOption')
  284. ->withConsecutive(
  285. [SensitiveConfigSetCommand::INPUT_OPTION_SCOPE],
  286. [SensitiveConfigSetCommand::INPUT_OPTION_SCOPE_CODE],
  287. [SensitiveConfigSetCommand::INPUT_OPTION_INTERACTIVE]
  288. )
  289. ->willReturnOnConsecutiveCalls(
  290. $scope,
  291. $scopeCode,
  292. $isInteractive
  293. );
  294. return $inputMock;
  295. }
  296. /**
  297. * @param string|null $inputValue
  298. * @return SensitiveConfigSetCommand
  299. */
  300. private function createInteractiveCommand($inputValue)
  301. {
  302. $questionHelperMock = $this->createMock(QuestionHelper::class);
  303. $questionHelperMock->expects($this->exactly(3))
  304. ->method('ask')
  305. ->willReturn($inputValue);
  306. $interactiveCollectorMock = $this->objectManager->create(
  307. InteractiveCollector::class,
  308. [
  309. 'questionHelper' => $questionHelperMock
  310. ]
  311. );
  312. $collectorFactoryMock = $this->getMockBuilder(CollectorFactory::class)
  313. ->disableOriginalConstructor()
  314. ->getMock();
  315. $collectorFactoryMock->expects($this->once())
  316. ->method('create')
  317. ->with(CollectorFactory::TYPE_INTERACTIVE)
  318. ->willReturn($interactiveCollectorMock);
  319. /** @var SensitiveConfigSetCommand command */
  320. $command = $this->objectManager->create(
  321. SensitiveConfigSetCommand::class,
  322. [
  323. 'facade' => $this->objectManager->create(
  324. SensitiveConfigSetFacade::class,
  325. [
  326. 'collectorFactory' => $collectorFactoryMock
  327. ]
  328. )
  329. ]
  330. );
  331. return $command;
  332. }
  333. }