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

namespace Magento\Store\Test\Unit\Model;

use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\Store;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class PathConfigTest extends \PHPUnit\Framework\TestCase
{
    /** @var \Magento\Framework\App\Config\ScopeConfigInterface | \PHPUnit_Framework_MockObject_MockObject*/
    private $scopeConfigMock;
    /** @var \Magento\Framework\Url\SecurityInfoInterface | \PHPUnit_Framework_MockObject_MockObject*/
    private $urlSecurityInfoMock;
    /** @var StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject*/
    private $storeManagerMock;
    /** @var Store | \PHPUnit_Framework_MockObject_MockObject*/
    private $storeMock;
    /** @var \Magento\Store\Model\RouteConfig */
    protected $model;

    protected function setUp()
    {
        $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->urlSecurityInfoMock = $this->getMockBuilder(\Magento\Framework\Url\SecurityInfoInterface::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
            ->disableOriginalConstructor()
            ->getMock();
        $mockArgs = [
            'scopeConfig' => $this->scopeConfigMock,
            'urlSecurityInfo' => $this->urlSecurityInfoMock,
            'storeManager' => $this->storeManagerMock,
        ];
        $this->model = (new ObjectManager($this))->getObject(\Magento\Store\Model\PathConfig::class, $mockArgs);
    }

    public function testGetCurrentSecureUrlNoAlias()
    {
        $baseUrl = 'base-store.url/';
        $pathInfo = 'path/to/action';

        $this->storeMock->expects($this->once())->method('getBaseUrl')->with('link', true)->willReturn($baseUrl);
        $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);

        $request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
            ->disableOriginalConstructor()
            ->getMock();

        $request->expects($this->once())->method('getAlias')->willReturn(null);
        $request->expects($this->once())->method('getPathInfo')->willReturn($pathInfo);
        $this->assertSame($baseUrl . $pathInfo, $this->model->getCurrentSecureUrl($request));
    }

    public function testGetCurrentSecureUrlWithAlias()
    {
        $baseUrl = 'base-store.url/';
        $alias = 'action-alias';

        $this->storeMock->expects($this->once())->method('getBaseUrl')->with('link', true)->willReturn($baseUrl);
        $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);

        $request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
            ->disableOriginalConstructor()
            ->getMock();

        $request->expects($this->once())->method('getAlias')->willReturn($alias);
        $request->expects($this->never())->method('getPathInfo');
        $this->assertSame($baseUrl . $alias, $this->model->getCurrentSecureUrl($request));
    }

    /**
     * @dataProvider urlSchemeProvider
     * @param string $base Base Url
     * @param bool $secure Expected return value
     */
    public function testShouldBeSecureUnsecureBaseUrl($base, $secure)
    {
        $this->scopeConfigMock->expects($this->once())
            ->method('getValue')
            ->with(Store::XML_PATH_UNSECURE_BASE_URL, ScopeInterface::SCOPE_STORE)
            ->willReturn($base);
        $this->assertSame($secure, $this->model->shouldBeSecure('path/to/action'));
    }

    /**
     * @dataProvider urlSchemeProvider
     * @param string $base Base Url
     * @param bool $secure Expected return value
     */
    public function testShouldBeSecureSecureBaseUrl($base, $secure)
    {
        $path = 'path/to/action';

        $this->scopeConfigMock->expects($this->once())->method('isSetFlag')
            ->with(Store::XML_PATH_SECURE_IN_FRONTEND, ScopeInterface::SCOPE_STORE)
            ->willReturn($secure);

        $getValueReturnMap = [
            [Store::XML_PATH_SECURE_BASE_URL, ScopeInterface::SCOPE_STORE, null, $base],
            [Store::XML_PATH_UNSECURE_BASE_URL, ScopeInterface::SCOPE_STORE, null, 'http://unsecure.url'],
        ];

        $this->scopeConfigMock->expects($this->any())
            ->method('getValue')
            ->will($this->returnValueMap($getValueReturnMap));

        if ($secure) {
            $this->urlSecurityInfoMock->expects($this->once())->method('isSecure')->with($path)->willReturn($secure);
        }

        $this->assertSame($secure, $this->model->shouldBeSecure($path));
    }

    /**
     * @return array
     */
    public function urlSchemeProvider()
    {
        return [
            ['https://base.url', true],
            ['http://base.url', false]
        ];
    }
}