ApplicationDumpCommandTest.php 11.7 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
<?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
     */
    public function setUp()
    {
        $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->createMock(OutputInterface::class);
        $outputMock->expects($this->at(0))
            ->method('writeln')
            ->with(['system' => $comment]);
        $outputMock->expects($this->at(1))
            ->method('writeln')
            ->with($this->matchesRegularExpression('/<info>Done. Config types dumped: [a-z0-9,\s]+<\/info>/'));

        /** @var ApplicationDumpCommand command */
        $command = $this->objectManager->create(ApplicationDumpCommand::class);
        $command->run($this->createMock(InputInterface::class), $outputMock);

        $config = $this->loadConfig();

        $this->validateSystemSection($config);
        $this->validateThemesSection($config);

        $configEnv = $this->loadEnvConfig();
        $this->validateSystemEnvSection($configEnv);

        $this->assertNotEmpty($this->hash->get());
        $this->assertContains('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
     */
    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 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();
    }
}