123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Test\Unit;
- use Magento\Framework\Url\HostChecker;
- /**
- * Test class for Magento\Framework\Url
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class UrlTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\Url\RouteParamsResolver|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $routeParamsResolverMock;
- /**
- * @var \Magento\Framework\Url\RouteParamsPreprocessorInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $routeParamsPreprocessorMock;
- /**
- * @var \Magento\Framework\Url\ScopeResolverInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $scopeResolverMock;
- /**
- * @var \Magento\Framework\Url\ScopeInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $scopeMock;
- /**
- * @var \Magento\Framework\Url\QueryParamsResolverInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $queryParamsResolverMock;
- /**
- * @var \Magento\Framework\Session\SidResolverInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $sidResolverMock;
- /**
- * @var \Magento\Framework\Session\Generic|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $sessionMock;
- /**
- * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $scopeConfig;
- /**
- * @var \Magento\Framework\Url\ModifierInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $urlModifier;
- /**
- * @var HostChecker|\PHPUnit_Framework_MockObject_MockObject
- */
- private $hostChecker;
- protected function setUp()
- {
- $this->routeParamsResolverMock = $this->createPartialMock(
- \Magento\Framework\Url\RouteParamsResolver::class,
- ['getType', 'hasData', 'getData', 'getRouteParams', 'unsetData']
- );
- $escaperMock = $this->createMock(\Magento\Framework\ZendEscaper::class);
- $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $objectManager->setBackwardCompatibleProperty($this->routeParamsResolverMock, 'escaper', $escaperMock);
- $this->routeParamsPreprocessorMock = $this->getMockForAbstractClass(
- \Magento\Framework\Url\RouteParamsPreprocessorInterface::class,
- ['unsetData'],
- '',
- false,
- true,
- true,
- []
- );
- $this->scopeResolverMock = $this->createMock(\Magento\Framework\Url\ScopeResolverInterface::class);
- $this->scopeMock = $this->createMock(\Magento\Framework\Url\ScopeInterface::class);
- $this->queryParamsResolverMock = $this->createMock(\Magento\Framework\Url\QueryParamsResolverInterface::class);
- $this->sidResolverMock = $this->createMock(\Magento\Framework\Session\SidResolverInterface::class);
- $this->sessionMock = $this->createMock(\Magento\Framework\Session\Generic::class);
- $this->scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
- $this->urlModifier = $this->createMock(\Magento\Framework\Url\ModifierInterface::class);
- $this->urlModifier->expects($this->any())
- ->method('execute')
- ->willReturnArgument(0);
- }
- /**
- * @param bool $resolve
- * @return \Magento\Framework\Url\RouteParamsResolverFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected function getRouteParamsResolverFactory($resolve = true)
- {
- $routeParamsResolverFactoryMock = $this->createMock(\Magento\Framework\Url\RouteParamsResolverFactory::class);
- if ($resolve) {
- $routeParamsResolverFactoryMock->expects($this->once())->method('create')
- ->will($this->returnValue($this->routeParamsResolverMock));
- }
- return $routeParamsResolverFactoryMock;
- }
- /**
- * @param array $mockMethods
- * @return \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
- */
- protected function getRequestMock()
- {
- return $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
- ->disableOriginalConstructor()->getMock();
- }
- /**
- * @param array $arguments
- * @return \Magento\Framework\Url
- */
- protected function getUrlModel($arguments = [])
- {
- $arguments = array_merge($arguments, ['scopeType' => \Magento\Store\Model\ScopeInterface::SCOPE_STORE]);
- $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $model = $objectManager->getObject(\Magento\Framework\Url::class, $arguments);
- $modelProperty = (new \ReflectionClass(get_class($model)))
- ->getProperty('urlModifier');
- $modelProperty->setAccessible(true);
- $modelProperty->setValue($model, $this->urlModifier);
- $zendEscaper = new \Magento\Framework\ZendEscaper();
- $escaper = new \Magento\Framework\Escaper();
- $objectManager->setBackwardCompatibleProperty($escaper, 'escaper', $zendEscaper);
- $objectManager->setBackwardCompatibleProperty($model, 'escaper', $escaper);
- return $model;
- }
- /**
- * @param string $httpHost
- * @param string $url
- * @dataProvider getCurrentUrlProvider
- */
- public function testGetCurrentUrl($httpHost, $url)
- {
- $requestMock = $this->getRequestMock();
- $requestMock->expects($this->once())->method('getRequestUri')->willReturn('/fancy_uri');
- $requestMock->expects($this->once())->method('getScheme')->will($this->returnValue('http'));
- $requestMock->expects($this->once())->method('getHttpHost')->will($this->returnValue($httpHost));
- $model = $this->getUrlModel(['request' => $requestMock]);
- $this->assertEquals($url, $model->getCurrentUrl());
- }
- /**
- * @return array
- */
- public function getCurrentUrlProvider()
- {
- return [
- 'without_port' => ['example.com', 'http://example.com/fancy_uri'],
- 'default_port' => ['example.com:80', 'http://example.com/fancy_uri'],
- 'custom_port' => ['example.com:8080', 'http://example.com:8080/fancy_uri']
- ];
- }
- public function testGetUseSession()
- {
- $model = $this->getUrlModel();
- $model->setUseSession(false);
- $this->assertFalse((bool)$model->getUseSession());
- $model->setUseSession(true);
- $this->assertTrue($model->getUseSession());
- }
- public function testGetBaseUrlNotLinkType()
- {
- $model = $this->getUrlModel(
- [
- 'scopeResolver' => $this->scopeResolverMock,
- 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory()
- ]
- );
- $baseUrl = 'base-url';
- $urlType = 'not-link';
- $this->routeParamsResolverMock->expects($this->any())->method('getType')->will($this->returnValue($urlType));
- $this->scopeMock->expects($this->once())->method('getBaseUrl')->will($this->returnValue($baseUrl));
- $this->scopeResolverMock->expects($this->any())
- ->method('getScope')
- ->will($this->returnValue($this->scopeMock));
- $baseUrlParams = ['_scope' => $this->scopeMock, '_type' => $urlType, '_secure' => true];
- $this->assertEquals($baseUrl, $model->getBaseUrl($baseUrlParams));
- }
- public function testGetUrlValidateFilter()
- {
- $model = $this->getUrlModel();
- $this->assertEquals('http://test.com', $model->getUrl('http://test.com'));
- }
- /**
- * @param string|array|bool $query
- * @param string $queryResult
- * @param string $returnUri
- * @dataProvider getUrlDataProvider
- */
- public function testGetUrl($query, $queryResult, $returnUri)
- {
- $requestMock = $this->getRequestMock();
- $routeConfigMock = $this->createMock(\Magento\Framework\App\Route\ConfigInterface::class);
- $model = $this->getUrlModel(
- [
- 'scopeResolver' => $this->scopeResolverMock,
- 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
- 'queryParamsResolver' => $this->queryParamsResolverMock,
- 'request' => $requestMock,
- 'routeConfig' => $routeConfigMock,
- 'routeParamsPreprocessor' => $this->routeParamsPreprocessorMock
- ]
- );
- $baseUrl = 'http://localhost/index.php/';
- $urlType = \Magento\Framework\UrlInterface::URL_TYPE_LINK;
- $this->scopeMock->expects($this->once())->method('getBaseUrl')->will($this->returnValue($baseUrl));
- $this->scopeResolverMock->expects($this->any())
- ->method('getScope')
- ->will($this->returnValue($this->scopeMock));
- $this->routeParamsResolverMock->expects($this->any())->method('getType')->will($this->returnValue($urlType));
- $this->routeParamsResolverMock->expects($this->any())->method('getRouteParams')
- ->will($this->returnValue(['id' => 100]));
- $this->routeParamsPreprocessorMock->expects($this->once())
- ->method('execute')
- ->willReturnArgument(2);
- $requestMock->expects($this->once())->method('isDirectAccessFrontendName')->will($this->returnValue(true));
- $routeConfigMock->expects($this->once())->method('getRouteFrontName')->will($this->returnValue('catalog'));
- $this->queryParamsResolverMock->expects($this->once())->method('getQuery')
- ->will($this->returnValue($queryResult));
- $url = $model->getUrl('catalog/product/view', [
- '_scope' => $this->getMockForAbstractClass(\Magento\Store\Api\Data\StoreInterface::class),
- '_fragment' => 'anchor',
- '_escape' => 1,
- '_query' => $query,
- '_nosid' => 0,
- 'id' => 100
- ]);
- $this->assertEquals($returnUri, $url);
- }
- public function testGetUrlIdempotentSetRoutePath()
- {
- $model = $this->getUrlModel([
- 'scopeResolver' => $this->scopeResolverMock,
- 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
- ]);
- $model->setData('route_path', 'catalog/product/view');
- $this->scopeResolverMock->expects($this->any())
- ->method('getScope')
- ->will($this->returnValue($this->scopeMock));
- $this->urlModifier->expects($this->exactly(1))->method('execute');
- $this->assertEquals('catalog/product/view', $model->getUrl('catalog/product/view'));
- }
- public function testGetUrlIdempotentSetRouteName()
- {
- $model = $this->getUrlModel([
- 'scopeResolver' => $this->scopeResolverMock,
- 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
- 'request' => $this->getRequestMock()
- ]);
- $model->setData('route_name', 'catalog');
- $this->scopeResolverMock->expects($this->any())
- ->method('getScope')
- ->will($this->returnValue($this->scopeMock));
- $this->assertEquals('/product/view/', $model->getUrl('catalog/product/view'));
- }
- public function testGetUrlRouteHasParams()
- {
- $this->routeParamsResolverMock->expects($this->any())->method('getRouteParams')
- ->will($this->returnValue(['foo' => 'bar', 'true' => false]));
- $model = $this->getUrlModel([
- 'scopeResolver' => $this->scopeResolverMock,
- 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
- 'request' => $this->getRequestMock()
- ]);
- $this->scopeResolverMock->expects($this->any())
- ->method('getScope')
- ->will($this->returnValue($this->scopeMock));
- $this->assertEquals('/index/index/foo/bar/', $model->getUrl('catalog'));
- }
- public function testGetUrlRouteUseRewrite()
- {
- $this->routeParamsResolverMock->expects($this->any())->method('getRouteParams')
- ->will($this->returnValue(['foo' => 'bar']));
- $this->routeParamsPreprocessorMock->expects($this->once())
- ->method('execute')
- ->willReturnArgument(2);
- $request = $this->getRequestMock();
- $request->expects($this->once())->method('getAlias')->will($this->returnValue('/catalog/product/view/'));
- $model = $this->getUrlModel([
- 'scopeResolver' => $this->scopeResolverMock,
- 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
- 'request' => $request,
- 'routeParamsPreprocessor' => $this->routeParamsPreprocessorMock
- ]);
- $this->scopeResolverMock->expects($this->any())
- ->method('getScope')
- ->will($this->returnValue($this->scopeMock));
- $this->assertEquals('/catalog/product/view/', $model->getUrl('catalog', ['_use_rewrite' => 1]));
- }
- /**
- * @return array
- */
- public function getUrlDataProvider()
- {
- return [
- 'string query' => [
- 'foo=bar',
- 'foo=bar',
- 'http://localhost/index.php/catalog/product/view/id/100/?foo=bar#anchor',
- ],
- 'array query' => [
- ['foo' => 'bar'],
- 'foo=bar',
- 'http://localhost/index.php/catalog/product/view/id/100/?foo=bar#anchor',
- ],
- 'without query' => [
- false,
- '',
- 'http://localhost/index.php/catalog/product/view/id/100/#anchor'
- ],
- ];
- }
- public function testGetUrlWithAsterisksPath()
- {
- $requestMock = $this->getRequestMock();
- $routeConfigMock = $this->createMock(\Magento\Framework\App\Route\ConfigInterface::class);
- $model = $this->getUrlModel(
- [
- 'scopeResolver' => $this->scopeResolverMock,
- 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
- 'queryParamsResolver' => $this->queryParamsResolverMock,
- 'request' => $requestMock, 'routeConfig' => $routeConfigMock,
- ]
- );
- $baseUrl = 'http://localhost/index.php/';
- $urlType = \Magento\Framework\UrlInterface::URL_TYPE_LINK;
- $this->scopeMock->expects($this->once())->method('getBaseUrl')->will($this->returnValue($baseUrl));
- $this->scopeResolverMock->expects($this->any())
- ->method('getScope')
- ->will($this->returnValue($this->scopeMock));
- $this->routeParamsResolverMock->expects($this->any())->method('getType')->will($this->returnValue($urlType));
- $this->routeParamsResolverMock->expects($this->any())->method('getRouteParams')
- ->will($this->returnValue(['key' => 'value']));
- $requestMock->expects($this->once())->method('isDirectAccessFrontendName')->will($this->returnValue(true));
- $requestMock->expects($this->once())->method('getRouteName')->will($this->returnValue('catalog'));
- $requestMock->expects($this->once())
- ->method('getControllerName')
- ->will($this->returnValue('product'));
- $requestMock->expects($this->once())->method('getActionName')->will($this->returnValue('view'));
- $routeConfigMock->expects($this->once())->method('getRouteFrontName')->will($this->returnValue('catalog'));
- $url = $model->getUrl('*/*/*/key/value');
- $this->assertEquals('http://localhost/index.php/catalog/product/view/key/value/', $url);
- }
- public function testGetDirectUrl()
- {
- $requestMock = $this->getRequestMock();
- $routeConfigMock = $this->createMock(\Magento\Framework\App\Route\ConfigInterface::class);
- $model = $this->getUrlModel(
- [
- 'scopeResolver' => $this->scopeResolverMock,
- 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
- 'queryParamsResolver' => $this->queryParamsResolverMock,
- 'request' => $requestMock,
- 'routeConfig' => $routeConfigMock,
- 'routeParamsPreprocessor' => $this->routeParamsPreprocessorMock
- ]
- );
- $baseUrl = 'http://localhost/index.php/';
- $urlType = \Magento\Framework\UrlInterface::URL_TYPE_LINK;
- $this->scopeMock->expects($this->once())->method('getBaseUrl')->will($this->returnValue($baseUrl));
- $this->scopeResolverMock->expects($this->any())
- ->method('getScope')
- ->will($this->returnValue($this->scopeMock));
- $this->routeParamsResolverMock->expects($this->any())->method('getType')->will($this->returnValue($urlType));
- $this->routeParamsPreprocessorMock->expects($this->once())
- ->method('execute')
- ->willReturnArgument(2);
- $requestMock->expects($this->once())->method('isDirectAccessFrontendName')->will($this->returnValue(true));
- $url = $model->getDirectUrl('direct-url');
- $this->assertEquals('http://localhost/index.php/direct-url', $url);
- }
- /**
- * @param string $inputUrl
- * @dataProvider getRebuiltUrlDataProvider
- */
- public function testGetRebuiltUrl($inputUrl, $outputUrl)
- {
- $requestMock = $this->getRequestMock();
- $model = $this->getUrlModel([
- 'session' => $this->sessionMock,
- 'request' => $requestMock,
- 'sidResolver' => $this->sidResolverMock,
- 'scopeResolver' => $this->scopeResolverMock,
- 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(false),
- 'queryParamsResolver' => $this->queryParamsResolverMock,
- ]);
- $this->queryParamsResolverMock->expects($this->once())->method('getQuery')
- ->will($this->returnValue('query=123'));
- $this->assertEquals($outputUrl, $model->getRebuiltUrl($inputUrl));
- }
- public function testGetRedirectUrl()
- {
- $model = $this->getUrlModel(
- [
- 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
- 'session' => $this->sessionMock,
- 'sidResolver' => $this->sidResolverMock,
- 'queryParamsResolver' => $this->queryParamsResolverMock,
- ]
- );
- $this->sidResolverMock->expects($this->once())->method('getUseSessionInUrl')->will($this->returnValue(true));
- $this->sessionMock->expects($this->once())->method('getSessionIdForHost')->will($this->returnValue(false));
- $this->sidResolverMock->expects($this->once())->method('getUseSessionVar')->will($this->returnValue(true));
- $this->routeParamsResolverMock->expects($this->once())->method('hasData')->with('secure_is_forced')
- ->will($this->returnValue(true));
- $this->sidResolverMock->expects($this->never())->method('getSessionIdQueryParam');
- $this->queryParamsResolverMock->expects($this->once())
- ->method('getQuery')
- ->will($this->returnValue('foo=bar'));
- $this->assertEquals('http://example.com/?foo=bar', $model->getRedirectUrl('http://example.com/'));
- }
- public function testGetRedirectUrlWithSessionId()
- {
- $model = $this->getUrlModel(
- [
- 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(false),
- 'session' => $this->sessionMock,
- 'sidResolver' => $this->sidResolverMock,
- 'queryParamsResolver' => $this->queryParamsResolverMock,
- ]
- );
- $this->sidResolverMock->expects($this->once())->method('getUseSessionInUrl')->will($this->returnValue(true));
- $this->sessionMock->expects($this->once())->method('getSessionIdForHost')
- ->will($this->returnValue('session-id'));
- $this->sidResolverMock->expects($this->once())->method('getUseSessionVar')->will($this->returnValue(false));
- $this->sidResolverMock->expects($this->once())->method('getSessionIdQueryParam');
- $this->queryParamsResolverMock->expects($this->once())
- ->method('getQuery')
- ->will($this->returnValue('foo=bar'));
- $this->assertEquals('http://example.com/?foo=bar', $model->getRedirectUrl('http://example.com/'));
- }
- /**
- * @return array
- */
- public function getRebuiltUrlDataProvider()
- {
- return [
- 'with port' => [
- 'https://example.com:88/index.php/catalog/index/view?query=123#hash',
- 'https://example.com:88/index.php/catalog/index/view?query=123#hash'
- ],
- 'without port' => [
- 'https://example.com/index.php/catalog/index/view?query=123#hash',
- 'https://example.com/index.php/catalog/index/view?query=123#hash'
- ],
- 'http' => [
- 'http://example.com/index.php/catalog/index/view?query=123#hash',
- 'http://example.com/index.php/catalog/index/view?query=123#hash'
- ]
- ];
- }
- public function testGetRouteUrlWithValidUrl()
- {
- $model = $this->getUrlModel(['routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(false)]);
- $this->routeParamsResolverMock->expects($this->never())->method('unsetData');
- $this->assertEquals('http://example.com', $model->getRouteUrl('http://example.com'));
- }
- public function testAddSessionParam()
- {
- $model = $this->getUrlModel([
- 'session' => $this->sessionMock,
- 'sidResolver' => $this->sidResolverMock,
- 'queryParamsResolver' => $this->queryParamsResolverMock,
- ]);
- $this->sidResolverMock->expects($this->once())->method('getSessionIdQueryParam')->with($this->sessionMock)
- ->will($this->returnValue('sid'));
- $this->sessionMock->expects($this->once())->method('getSessionId')->will($this->returnValue('session-id'));
- $this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('sid', 'session-id');
- $model->addSessionParam();
- }
- /**
- * @param bool $result
- * @param string $referrer
- * @dataProvider isOwnOriginUrlDataProvider
- */
- public function testIsOwnOriginUrl($result, $referrer)
- {
- $requestMock = $this->getRequestMock();
- $this->hostChecker = $this->getMockBuilder(HostChecker::class)
- ->disableOriginalConstructor()->getMock();
- $this->hostChecker->expects($this->once())->method('isOwnOrigin')->with($referrer)->willReturn($result);
- $model = $this->getUrlModel(['hostChecker' => $this->hostChecker, 'request' => $requestMock]);
- $requestMock->expects($this->once())->method('getServer')->with('HTTP_REFERER')
- ->will($this->returnValue($referrer));
- $this->assertEquals($result, $model->isOwnOriginUrl());
- }
- /**
- * @return array
- */
- public function isOwnOriginUrlDataProvider()
- {
- return [
- 'is origin url' => [true, 'http://localhost/'],
- 'is not origin url' => [false, 'http://example.com/'],
- ];
- }
- /**
- * @param string $urlType
- * @param string $configPath
- * @param bool $isSecure
- * @param int $isSecureCallCount
- * @param string $key
- * @dataProvider getConfigDataDataProvider
- */
- public function testGetConfigData($urlType, $configPath, $isSecure, $isSecureCallCount, $key)
- {
- $urlSecurityInfoMock = $this->createMock(\Magento\Framework\Url\SecurityInfoInterface::class);
- $model = $this->getUrlModel([
- 'urlSecurityInfo' => $urlSecurityInfoMock,
- 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
- 'scopeResolver' => $this->scopeResolverMock,
- 'scopeConfig' => $this->scopeConfig,
- ]);
- $this->scopeConfig->expects($this->any())
- ->method('getValue')
- ->with($this->equalTo($configPath), \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->scopeMock)
- ->will($this->returnValue('http://localhost/'));
- $this->routeParamsResolverMock->expects($this->at(0))->method('hasData')->with('secure_is_forced')
- ->will($this->returnValue(false));
- $this->scopeResolverMock->expects($this->any())
- ->method('getScope')
- ->will($this->returnValue($this->scopeMock));
- $this->scopeMock->expects($this->once())->method('isUrlSecure')->will($this->returnValue(true));
- $this->routeParamsResolverMock->expects($this->at(1))->method('hasData')->with('secure')
- ->will($this->returnValue(false));
- $this->routeParamsResolverMock->expects($this->any())->method('getType')
- ->will($this->returnValue($urlType));
- $this->routeParamsResolverMock->expects($this->once())
- ->method('getData')
- ->will($this->returnValue($isSecure));
- $urlSecurityInfoMock->expects($this->exactly($isSecureCallCount))->method('isSecure')
- ->will($this->returnValue(false));
- $this->assertEquals('http://localhost/', $model->getConfigData($key));
- }
- /**
- * @return array
- */
- public function getConfigDataDataProvider()
- {
- return [
- 'secure url' => ['some-type', 'web/secure/base_url_secure', true, 0, 'base_url_secure'],
- 'unsecure url' => [
- \Magento\Framework\UrlInterface::URL_TYPE_LINK,
- 'web/unsecure/base_url_unsecure',
- false,
- 1,
- 'base_url_unsecure',
- ],
- ];
- }
- public function testGetConfigDataWithSecureIsForcedParam()
- {
- $model = $this->getUrlModel([
- 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
- 'scopeResolver' => $this->scopeResolverMock,
- 'scopeConfig' => $this->scopeConfig,
- ]);
- $this->scopeConfig->expects($this->any())
- ->method('getValue')
- ->with(
- 'web/secure/base_url_secure_forced',
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
- $this->scopeMock
- )
- ->will($this->returnValue('http://localhost/'));
- $this->routeParamsResolverMock->expects($this->once())->method('hasData')->with('secure_is_forced')
- ->will($this->returnValue(true));
- $this->routeParamsResolverMock->expects($this->once())->method('getData')->with('secure')
- ->will($this->returnValue(true));
- $this->scopeResolverMock->expects($this->any())
- ->method('getScope')
- ->will($this->returnValue($this->scopeMock));
- $this->assertEquals('http://localhost/', $model->getConfigData('base_url_secure_forced'));
- }
- /**
- * @param string $html
- * @param string $result
- * @dataProvider sessionUrlVarWithMatchedHostsAndBaseUrlDataProvider
- */
- public function testSessionUrlVarWithMatchedHostsAndBaseUrl($html, $result)
- {
- $requestMock = $this->getRequestMock();
- $model = $this->getUrlModel(
- [
- 'session' => $this->sessionMock,
- 'request' => $requestMock,
- 'sidResolver' => $this->sidResolverMock,
- 'scopeResolver' => $this->scopeResolverMock,
- 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
- ]
- );
- $requestMock->expects($this->once())
- ->method('getHttpHost')
- ->will($this->returnValue('localhost'));
- $this->scopeMock->expects($this->once())
- ->method('getBaseUrl')
- ->will($this->returnValue('http://localhost'));
- $this->scopeResolverMock->expects($this->any())
- ->method('getScope')
- ->will($this->returnValue($this->scopeMock));
- $this->sidResolverMock->expects($this->never())
- ->method('getSessionIdQueryParam');
- $this->assertEquals($result, $model->sessionUrlVar($html));
- }
- public function testSessionUrlVarWithoutMatchedHostsAndBaseUrl()
- {
- $requestMock = $this->getRequestMock();
- $model = $this->getUrlModel(
- [
- 'session' => $this->sessionMock,
- 'request' => $requestMock,
- 'sidResolver' => $this->sidResolverMock,
- 'scopeResolver' => $this->scopeResolverMock,
- 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
- ]
- );
- $requestMock->expects($this->once())->method('getHttpHost')->will($this->returnValue('localhost'));
- $this->scopeMock->expects($this->once())
- ->method('getBaseUrl')
- ->will($this->returnValue('http://example.com'));
- $this->scopeResolverMock->expects($this->any())
- ->method('getScope')
- ->will($this->returnValue($this->scopeMock));
- $this->sidResolverMock->expects($this->once())->method('getSessionIdQueryParam')
- ->will($this->returnValue('SID'));
- $this->sessionMock->expects($this->once())->method('getSessionId')
- ->will($this->returnValue('session-id'));
- $this->assertEquals(
- '<a href="http://example.com/?SID=session-id">www.example.com</a>',
- $model->sessionUrlVar('<a href="http://example.com/?___SID=U">www.example.com</a>')
- );
- }
- /**
- * @return array
- */
- public function sessionUrlVarWithMatchedHostsAndBaseUrlDataProvider()
- {
- return [
- [
- '<a href="http://example.com/?___SID=U?SID=session-id">www.example.com</a>',
- '<a href="http://example.com/?SID=session-id">www.example.com</a>',
- ],
- [
- '<a href="http://example.com/?___SID=U&SID=session-id">www.example.com</a>',
- '<a href="http://example.com/?SID=session-id">www.example.com</a>',
- ],
- [
- '<a href="http://example.com/?foo=bar&___SID=U?SID=session-id">www.example.com</a>',
- '<a href="http://example.com/?foo=bar?SID=session-id">www.example.com</a>',
- ],
- [
- '<a href="http://example.com/?foo=bar&___SID=U&SID=session-id">www.example.com</a>',
- '<a href="http://example.com/?foo=bar&SID=session-id">www.example.com</a>',
- ],
- ];
- }
- public function testSetRequest()
- {
- $initRequestMock = $this->getRequestMock();
- $requestMock = $this->getRequestMock();
- $initRequestMock->expects($this->any())->method('getScheme')->will($this->returnValue('fake'));
- $initRequestMock->expects($this->any())->method('getHttpHost')->will($this->returnValue('fake-host'));
- $requestMock->expects($this->any())->method('getScheme')->will($this->returnValue('http'));
- $requestMock->expects($this->any())->method('getHttpHost')->will($this->returnValue('example.com'));
- $model = $this->getUrlModel(['request' => $initRequestMock]);
- $model->setRequest($requestMock);
- $this->assertEquals('http://example.com', $model->getCurrentUrl());
- }
- }
|