ApplicationDumpCommandTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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\Config\Model\Config\Export\ExcludeList;
  8. use Magento\Config\Model\Config\TypePool;
  9. use Magento\Deploy\Model\DeploymentConfig\Hash;
  10. use Magento\Framework\App\DeploymentConfig;
  11. use Magento\Framework\App\Filesystem\DirectoryList;
  12. use Magento\Framework\Config\File\ConfigFilePool;
  13. use Magento\Framework\Filesystem;
  14. use Magento\Framework\ObjectManagerInterface;
  15. use Magento\TestFramework\Helper\Bootstrap;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. /**
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class ApplicationDumpCommandTest extends \PHPUnit\Framework\TestCase
  22. {
  23. /**
  24. * @var ObjectManagerInterface
  25. */
  26. private $objectManager;
  27. /**
  28. * @var DeploymentConfig\FileReader
  29. */
  30. private $reader;
  31. /**
  32. * @var ConfigFilePool
  33. */
  34. private $configFilePool;
  35. /**
  36. * @var Filesystem
  37. */
  38. private $filesystem;
  39. /**
  40. * @var DeploymentConfig\Writer
  41. */
  42. private $writer;
  43. /**
  44. * @var array
  45. */
  46. private $envConfig;
  47. /**
  48. * @var array
  49. */
  50. private $config;
  51. /**
  52. * @var Hash
  53. */
  54. private $hash;
  55. /**
  56. * @inheritdoc
  57. */
  58. public function setUp()
  59. {
  60. $this->objectManager = Bootstrap::getObjectManager();
  61. $this->reader = $this->objectManager->get(DeploymentConfig\FileReader::class);
  62. $this->filesystem = $this->objectManager->get(Filesystem::class);
  63. $this->configFilePool = $this->objectManager->get(ConfigFilePool::class);
  64. $this->reader = $this->objectManager->get(DeploymentConfig\Reader::class);
  65. $this->writer = $this->objectManager->get(DeploymentConfig\Writer::class);
  66. $this->configFilePool = $this->objectManager->get(ConfigFilePool::class);
  67. $this->hash = $this->objectManager->get(Hash::class);
  68. // Snapshot of configuration.
  69. $this->config = $this->loadConfig();
  70. $this->envConfig = $this->loadEnvConfig();
  71. $this->writer->saveConfig(
  72. [
  73. ConfigFilePool::APP_CONFIG => [
  74. 'system' => [
  75. 'default' => [
  76. 'web' => [
  77. 'test' => [
  78. 'test_value_3' => 'value from the file'
  79. ]
  80. ]
  81. ]
  82. ]
  83. ]
  84. ],
  85. true
  86. );
  87. }
  88. /**
  89. * @return array
  90. */
  91. private function loadConfig()
  92. {
  93. return $this->reader->load(ConfigFilePool::APP_CONFIG);
  94. }
  95. /**
  96. * @return array
  97. */
  98. private function loadEnvConfig()
  99. {
  100. return $this->reader->load(ConfigFilePool::APP_ENV);
  101. }
  102. /**
  103. * @return string
  104. */
  105. private function loadRawConfig()
  106. {
  107. return $this->filesystem->getDirectoryRead(DirectoryList::CONFIG)
  108. ->readFile($this->configFilePool->getPath(ConfigFilePool::APP_CONFIG));
  109. }
  110. /**
  111. * @magentoDbIsolation enabled
  112. * @magentoDataFixture Magento/Deploy/_files/config_data.php
  113. */
  114. public function testExecute()
  115. {
  116. $this->objectManager->configure([
  117. ExcludeList::class => [
  118. 'arguments' => [
  119. 'configs' => [
  120. 'web/test/test_value_1' => '',
  121. 'web/test/test_value_2' => '0',
  122. 'web/test/test_sensitive' => '1',
  123. ],
  124. ],
  125. ],
  126. TypePool::class => [
  127. 'arguments' => [
  128. 'sensitive' => [
  129. 'web/test/test_sensitive1' => '',
  130. 'web/test/test_sensitive2' => '0',
  131. 'web/test/test_sensitive3' => '1',
  132. 'web/test/test_sensitive_environment4' => '1',
  133. 'web/test/test_sensitive_environment5' => '1',
  134. 'web/test/test_sensitive_environment6' => '0',
  135. ],
  136. 'environment' => [
  137. 'web/test/test_sensitive_environment4' => '1',
  138. 'web/test/test_sensitive_environment5' => '0',
  139. 'web/test/test_sensitive_environment6' => '1',
  140. 'web/test/test_environment7' => '',
  141. 'web/test/test_environment8' => '0',
  142. 'web/test/test_environment9' => '1',
  143. ],
  144. ]
  145. ]
  146. ]);
  147. $comment = implode(PHP_EOL, [
  148. 'Shared configuration was written to config.php and system-specific configuration to env.php.',
  149. 'Shared configuration file (config.php) doesn\'t contain sensitive data for security reasons.',
  150. 'Sensitive data can be stored in the following environment variables:',
  151. 'CONFIG__DEFAULT__WEB__TEST__TEST_SENSITIVE for web/test/test_sensitive',
  152. 'CONFIG__DEFAULT__WEB__TEST__TEST_SENSITIVE3 for web/test/test_sensitive3',
  153. 'CONFIG__DEFAULT__WEB__TEST__TEST_SENSITIVE_ENVIRONMENT4 for web/test/test_sensitive_environment4',
  154. 'CONFIG__DEFAULT__WEB__TEST__TEST_SENSITIVE_ENVIRONMENT5 for web/test/test_sensitive_environment5'
  155. ]);
  156. $outputMock = $this->createMock(OutputInterface::class);
  157. $outputMock->expects($this->at(0))
  158. ->method('writeln')
  159. ->with(['system' => $comment]);
  160. $outputMock->expects($this->at(1))
  161. ->method('writeln')
  162. ->with($this->matchesRegularExpression('/<info>Done. Config types dumped: [a-z0-9,\s]+<\/info>/'));
  163. /** @var ApplicationDumpCommand command */
  164. $command = $this->objectManager->create(ApplicationDumpCommand::class);
  165. $command->run($this->createMock(InputInterface::class), $outputMock);
  166. $config = $this->loadConfig();
  167. $this->validateSystemSection($config);
  168. $this->validateThemesSection($config);
  169. $configEnv = $this->loadEnvConfig();
  170. $this->validateSystemEnvSection($configEnv);
  171. $this->assertNotEmpty($this->hash->get());
  172. $this->assertContains('For the section: system', $this->loadRawConfig());
  173. }
  174. /**
  175. * Validates 'system' section in configuration data.
  176. *
  177. * @param array $config The configuration array
  178. * @return void
  179. */
  180. private function validateSystemSection(array $config)
  181. {
  182. $this->assertArrayHasKey('test_value_1', $config['system']['default']['web']['test']);
  183. $this->assertArrayHasKey('test_value_2', $config['system']['default']['web']['test']);
  184. $this->assertArrayHasKey('test_sensitive1', $config['system']['default']['web']['test']);
  185. $this->assertArrayHasKey('test_sensitive2', $config['system']['default']['web']['test']);
  186. $this->assertArrayHasKey('test_environment7', $config['system']['default']['web']['test']);
  187. $this->assertArrayHasKey('test_environment8', $config['system']['default']['web']['test']);
  188. $this->assertArrayNotHasKey('test_sensitive', $config['system']['default']['web']['test']);
  189. $this->assertArrayNotHasKey('test_sensitive3', $config['system']['default']['web']['test']);
  190. $this->assertArrayNotHasKey('test_sensitive_environment4', $config['system']['default']['web']['test']);
  191. $this->assertArrayNotHasKey('test_sensitive_environment5', $config['system']['default']['web']['test']);
  192. $this->assertArrayNotHasKey('test_sensitive_environment6', $config['system']['default']['web']['test']);
  193. $this->assertArrayNotHasKey('test_environment9', $config['system']['default']['web']['test']);
  194. /** @see Magento/Deploy/_files/config_data.php */
  195. $this->assertEquals(
  196. 'frontend/Magento/blank',
  197. $config['system']['default']['design']['theme']['theme_id']
  198. );
  199. $this->assertEquals(
  200. 'frontend/Magento/luma',
  201. $config['system']['stores']['default']['design']['theme']['theme_id']
  202. );
  203. $this->assertEquals(
  204. 'frontend/Magento/luma',
  205. $config['system']['websites']['base']['design']['theme']['theme_id']
  206. );
  207. $this->assertEquals('value from the file', $config['system']['default']['web']['test']['test_value_3']);
  208. $this->assertEquals('GB', $config['system']['default']['general']['country']['default']);
  209. $this->assertEquals(
  210. 'HK,IE,MO,PA,GB',
  211. $config['system']['default']['general']['country']['optional_zip_countries']
  212. );
  213. }
  214. /**
  215. * Validates 'system' section in environment configuration data.
  216. *
  217. * @param array $config The configuration array
  218. * @return void
  219. */
  220. private function validateSystemEnvSection(array $config)
  221. {
  222. $envTestKeys = [
  223. 'test_sensitive',
  224. 'test_sensitive3',
  225. 'test_sensitive_environment4',
  226. 'test_sensitive_environment5',
  227. 'test_sensitive_environment6',
  228. 'test_environment9'
  229. ];
  230. $this->assertEmpty(
  231. array_diff($envTestKeys, array_keys($config['system']['default']['web']['test']))
  232. );
  233. }
  234. /**
  235. * Validates 'themes' section in configuration data.
  236. *
  237. * @param array $config The configuration array
  238. * @return void
  239. */
  240. private function validateThemesSection(array $config)
  241. {
  242. $this->assertEquals(
  243. [
  244. 'parent_id' => null,
  245. 'theme_path' => 'Magento/backend',
  246. 'theme_title' => 'Magento 2 backend',
  247. 'is_featured' => '0',
  248. 'area' => 'adminhtml',
  249. 'type' => '0',
  250. 'code' => 'Magento/backend',
  251. ],
  252. $config['themes']['adminhtml/Magento/backend']
  253. );
  254. $this->assertEquals(
  255. [
  256. 'parent_id' => null,
  257. 'theme_path' => 'Magento/blank',
  258. 'theme_title' => 'Magento Blank',
  259. 'is_featured' => '0',
  260. 'area' => 'frontend',
  261. 'type' => '0',
  262. 'code' => 'Magento/blank',
  263. ],
  264. $config['themes']['frontend/Magento/blank']
  265. );
  266. $this->assertEquals(
  267. [
  268. 'parent_id' => 'Magento/blank',
  269. 'theme_path' => 'Magento/luma',
  270. 'theme_title' => 'Magento Luma',
  271. 'is_featured' => '0',
  272. 'area' => 'frontend',
  273. 'type' => '0',
  274. 'code' => 'Magento/luma',
  275. ],
  276. $config['themes']['frontend/Magento/luma']
  277. );
  278. }
  279. /**
  280. * @inheritdoc
  281. */
  282. public function tearDown()
  283. {
  284. $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
  285. $this->configFilePool->getPath(ConfigFilePool::APP_CONFIG),
  286. "<?php\n return array();\n"
  287. );
  288. $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
  289. $this->configFilePool->getPath(ConfigFilePool::APP_ENV),
  290. "<?php\n return array();\n"
  291. );
  292. /** @var DeploymentConfig\Writer $writer */
  293. $writer = $this->objectManager->get(DeploymentConfig\Writer::class);
  294. $writer->saveConfig([ConfigFilePool::APP_CONFIG => $this->config]);
  295. /** @var DeploymentConfig\Writer $writer */
  296. $writer = $this->objectManager->get(DeploymentConfig\Writer::class);
  297. $writer->saveConfig([ConfigFilePool::APP_ENV => $this->envConfig]);
  298. /** @var DeploymentConfig $deploymentConfig */
  299. $deploymentConfig = $this->objectManager->get(DeploymentConfig::class);
  300. $deploymentConfig->resetData();
  301. }
  302. }