ConfigTest.php 2.85 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Config\Controller\Adminhtml\System;

use Magento\TestFramework\Helper\Bootstrap;
use Magento\Framework\App\Request\Http as HttpRequest;

/**
 * @magentoAppArea adminhtml
 */
class ConfigTest extends \Magento\TestFramework\TestCase\AbstractBackendController
{
    /**
     * Test Configuration page existing.
     */
    public function testEditAction()
    {
        $this->dispatch('backend/admin/system_config/edit');
        $this->assertContains('<div id="system_config_tabs"', $this->getResponse()->getBody());
    }

    /**
     * Test redirect after changing base URL.
     *
     * @magentoAppIsolation enabled
     * @magentoDbIsolation enabled
     */
    public function testChangeBaseUrl()
    {
        $defaultHost = Bootstrap::getObjectManager()->create(\Magento\Framework\Url::class)->getBaseUrl();
        $newHost = 'm2test123.loc';
        $request = $this->getRequest();
        $request->setPostValue(
            [
                'groups' =>
                    ['unsecure' =>
                        ['fields' =>
                            ['base_url' =>
                                ['value' => 'http://' . $newHost . '/']
                            ]
                        ]
                    ],
                    'config_state' => ['web_unsecure' => 1]
            ]
        )->setParam(
            'section',
            'web'
        )->setMethod(
            HttpRequest::METHOD_POST
        );
        $this->dispatch('backend/admin/system_config/save');

        $this->assertTrue($this->getResponse()->isRedirect(), 'Redirect was expected, but none was performed.');

        /** @var array|bool $url */
        $url = parse_url($this->getResponse()->getHeader('Location')->getFieldValue());
        $this->assertArrayNotHasKey(
            'query',
            $url,
            'No GET params, including "SID", were expected, but somewhat exists'
        );
        $this->assertEquals($newHost, $url['host'], 'A new host in the url expected, but there is old one');
        $this->resetBaseUrl($defaultHost);
    }

    /**
     * Reset test framework default base url.
     *
     * @param string $defaultHost
     */
    protected function resetBaseUrl($defaultHost)
    {
        $baseUrlData = [
            'section' => 'web',
            'website' => null,
            'store' => null,
            'groups' => [
                'unsecure' => [
                    'fields' => [
                        'base_url' => [
                            'value' => $defaultHost
                        ]
                    ]
                ]
            ]
        ];
        Bootstrap::getObjectManager()->create(\Magento\Config\Model\Config\Factory::class)
            ->create()
            ->addData($baseUrlData)
            ->save();
    }
}