15 class UrlTest extends \PHPUnit\Framework\TestCase
69 $this->routeParamsResolverMock = $this->createPartialMock(
70 \
Magento\Framework\
Url\RouteParamsResolver::class,
71 [
'getType',
'hasData',
'getData',
'getRouteParams',
'unsetData']
74 $escaperMock = $this->createMock(\
Magento\Framework\ZendEscaper::class);
76 $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
78 $objectManager->setBackwardCompatibleProperty($this->routeParamsResolverMock,
'escaper', $escaperMock);
80 $this->routeParamsPreprocessorMock = $this->getMockForAbstractClass(
81 \
Magento\Framework\
Url\RouteParamsPreprocessorInterface::class,
90 $this->scopeResolverMock = $this->createMock(\
Magento\Framework\
Url\ScopeResolverInterface::class);
91 $this->scopeMock = $this->createMock(\
Magento\Framework\
Url\ScopeInterface::class);
92 $this->queryParamsResolverMock = $this->createMock(\
Magento\Framework\
Url\QueryParamsResolverInterface::class);
93 $this->sidResolverMock = $this->createMock(\
Magento\Framework\Session\SidResolverInterface::class);
94 $this->sessionMock = $this->createMock(\
Magento\Framework\Session\Generic::class);
95 $this->scopeConfig = $this->createMock(\
Magento\Framework\
App\
Config\ScopeConfigInterface::class);
97 $this->urlModifier = $this->createMock(\
Magento\Framework\
Url\ModifierInterface::class);
98 $this->urlModifier->expects($this->any())
100 ->willReturnArgument(0);
109 $routeParamsResolverFactoryMock = $this->createMock(\
Magento\Framework\
Url\RouteParamsResolverFactory::class);
111 $routeParamsResolverFactoryMock->expects($this->once())->method(
'create')
112 ->will($this->returnValue($this->routeParamsResolverMock));
114 return $routeParamsResolverFactoryMock;
123 return $this->getMockBuilder(\
Magento\Framework\
App\Request\Http::class)
124 ->disableOriginalConstructor()->getMock();
134 $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
137 $modelProperty = (new \ReflectionClass(get_class(
$model)))
138 ->getProperty(
'urlModifier');
140 $modelProperty->setAccessible(
true);
141 $modelProperty->setValue(
$model, $this->urlModifier);
143 $zendEscaper = new \Magento\Framework\ZendEscaper();
144 $escaper = new \Magento\Framework\Escaper();
145 $objectManager->setBackwardCompatibleProperty($escaper,
'escaper', $zendEscaper);
159 $requestMock->expects($this->once())->method(
'getRequestUri')->willReturn(
'/fancy_uri');
160 $requestMock->expects($this->once())->method(
'getScheme')->will($this->returnValue(
'http'));
161 $requestMock->expects($this->once())->method(
'getHttpHost')->will($this->returnValue($httpHost));
163 $this->assertEquals(
$url,
$model->getCurrentUrl());
172 'without_port' => [
'example.com',
'http://example.com/fancy_uri'],
173 'default_port' => [
'example.com:80',
'http://example.com/fancy_uri'],
174 'custom_port' => [
'example.com:8080',
'http://example.com:8080/fancy_uri']
182 $model->setUseSession(
false);
183 $this->assertFalse((
bool)
$model->getUseSession());
185 $model->setUseSession(
true);
186 $this->assertTrue(
$model->getUseSession());
193 'scopeResolver' => $this->scopeResolverMock,
198 $baseUrl =
'base-url';
199 $urlType =
'not-link';
200 $this->routeParamsResolverMock->expects($this->any())->method(
'getType')->will($this->returnValue($urlType));
201 $this->scopeMock->expects($this->once())->method(
'getBaseUrl')->will($this->returnValue($baseUrl));
202 $this->scopeResolverMock->expects($this->any())
204 ->will($this->returnValue($this->scopeMock));
206 $baseUrlParams = [
'_scope' =>
$this->scopeMock,
'_type' => $urlType,
'_secure' =>
true];
207 $this->assertEquals($baseUrl,
$model->getBaseUrl($baseUrlParams));
213 $this->assertEquals(
'http://test.com',
$model->getUrl(
'http://test.com'));
225 $routeConfigMock = $this->createMock(\
Magento\Framework\
App\Route\ConfigInterface::class);
228 'scopeResolver' => $this->scopeResolverMock,
230 'queryParamsResolver' => $this->queryParamsResolverMock,
231 'request' => $requestMock,
232 'routeConfig' => $routeConfigMock,
233 'routeParamsPreprocessor' => $this->routeParamsPreprocessorMock
237 $baseUrl =
'http://localhost/index.php/';
240 $this->scopeMock->expects($this->once())->method(
'getBaseUrl')->will($this->returnValue($baseUrl));
241 $this->scopeResolverMock->expects($this->any())
243 ->will($this->returnValue($this->scopeMock));
244 $this->routeParamsResolverMock->expects($this->any())->method(
'getType')->will($this->returnValue($urlType));
245 $this->routeParamsResolverMock->expects($this->any())->method(
'getRouteParams')
246 ->will($this->returnValue([
'id' => 100]));
248 $this->routeParamsPreprocessorMock->expects($this->once())
250 ->willReturnArgument(2);
252 $requestMock->expects($this->once())->method(
'isDirectAccessFrontendName')->will($this->returnValue(
true));
253 $routeConfigMock->expects($this->once())->method(
'getRouteFrontName')->will($this->returnValue(
'catalog'));
254 $this->queryParamsResolverMock->expects($this->once())->method(
'getQuery')
255 ->will($this->returnValue($queryResult));
257 $url =
$model->getUrl(
'catalog/product/view', [
258 '_scope' => $this->getMockForAbstractClass(\
Magento\Store\Api\
Data\StoreInterface::class),
259 '_fragment' =>
'anchor',
265 $this->assertEquals($returnUri,
$url);
271 'scopeResolver' => $this->scopeResolverMock,
274 $model->setData(
'route_path',
'catalog/product/view');
276 $this->scopeResolverMock->expects($this->any())
278 ->will($this->returnValue($this->scopeMock));
280 $this->urlModifier->expects($this->exactly(1))->method(
'execute');
282 $this->assertEquals(
'catalog/product/view',
$model->getUrl(
'catalog/product/view'));
288 'scopeResolver' => $this->scopeResolverMock,
292 $model->setData(
'route_name',
'catalog');
294 $this->scopeResolverMock->expects($this->any())
296 ->will($this->returnValue($this->scopeMock));
298 $this->assertEquals(
'/product/view/',
$model->getUrl(
'catalog/product/view'));
303 $this->routeParamsResolverMock->expects($this->any())->method(
'getRouteParams')
304 ->will($this->returnValue([
'foo' =>
'bar',
'true' =>
false]));
306 'scopeResolver' => $this->scopeResolverMock,
311 $this->scopeResolverMock->expects($this->any())
313 ->will($this->returnValue($this->scopeMock));
315 $this->assertEquals(
'/index/index/foo/bar/',
$model->getUrl(
'catalog'));
320 $this->routeParamsResolverMock->expects($this->any())->method(
'getRouteParams')
321 ->will($this->returnValue([
'foo' =>
'bar']));
323 $this->routeParamsPreprocessorMock->expects($this->once())
325 ->willReturnArgument(2);
328 $request->expects($this->once())->method(
'getAlias')->will($this->returnValue(
'/catalog/product/view/'));
330 'scopeResolver' => $this->scopeResolverMock,
333 'routeParamsPreprocessor' => $this->routeParamsPreprocessorMock
336 $this->scopeResolverMock->expects($this->any())
338 ->will($this->returnValue($this->scopeMock));
340 $this->assertEquals(
'/catalog/product/view/',
$model->getUrl(
'catalog', [
'_use_rewrite' => 1]));
352 'http://localhost/index.php/catalog/product/view/id/100/?foo=bar#anchor',
357 'http://localhost/index.php/catalog/product/view/id/100/?foo=bar#anchor',
362 'http://localhost/index.php/catalog/product/view/id/100/#anchor' 370 $routeConfigMock = $this->createMock(\
Magento\Framework\
App\Route\ConfigInterface::class);
373 'scopeResolver' => $this->scopeResolverMock,
375 'queryParamsResolver' => $this->queryParamsResolverMock,
376 'request' => $requestMock,
'routeConfig' => $routeConfigMock,
380 $baseUrl =
'http://localhost/index.php/';
383 $this->scopeMock->expects($this->once())->method(
'getBaseUrl')->will($this->returnValue($baseUrl));
384 $this->scopeResolverMock->expects($this->any())
386 ->will($this->returnValue($this->scopeMock));
387 $this->routeParamsResolverMock->expects($this->any())->method(
'getType')->will($this->returnValue($urlType));
388 $this->routeParamsResolverMock->expects($this->any())->method(
'getRouteParams')
389 ->will($this->returnValue([
'key' =>
'value']));
390 $requestMock->expects($this->once())->method(
'isDirectAccessFrontendName')->will($this->returnValue(
true));
392 $requestMock->expects($this->once())->method(
'getRouteName')->will($this->returnValue(
'catalog'));
393 $requestMock->expects($this->once())
394 ->method(
'getControllerName')
395 ->will($this->returnValue(
'product'));
396 $requestMock->expects($this->once())->method(
'getActionName')->will($this->returnValue(
'view'));
397 $routeConfigMock->expects($this->once())->method(
'getRouteFrontName')->will($this->returnValue(
'catalog'));
400 $this->assertEquals(
'http://localhost/index.php/catalog/product/view/key/value/',
$url);
406 $routeConfigMock = $this->createMock(\
Magento\Framework\
App\Route\ConfigInterface::class);
409 'scopeResolver' => $this->scopeResolverMock,
411 'queryParamsResolver' => $this->queryParamsResolverMock,
412 'request' => $requestMock,
413 'routeConfig' => $routeConfigMock,
414 'routeParamsPreprocessor' => $this->routeParamsPreprocessorMock
418 $baseUrl =
'http://localhost/index.php/';
421 $this->scopeMock->expects($this->once())->method(
'getBaseUrl')->will($this->returnValue($baseUrl));
422 $this->scopeResolverMock->expects($this->any())
424 ->will($this->returnValue($this->scopeMock));
425 $this->routeParamsResolverMock->expects($this->any())->method(
'getType')->will($this->returnValue($urlType));
427 $this->routeParamsPreprocessorMock->expects($this->once())
429 ->willReturnArgument(2);
431 $requestMock->expects($this->once())->method(
'isDirectAccessFrontendName')->will($this->returnValue(
true));
434 $this->assertEquals(
'http://localhost/index.php/direct-url',
$url);
445 'session' => $this->sessionMock,
446 'request' => $requestMock,
447 'sidResolver' => $this->sidResolverMock,
448 'scopeResolver' => $this->scopeResolverMock,
450 'queryParamsResolver' => $this->queryParamsResolverMock,
453 $this->queryParamsResolverMock->expects($this->once())->method(
'getQuery')
454 ->will($this->returnValue(
'query=123'));
456 $this->assertEquals($outputUrl,
$model->getRebuiltUrl($inputUrl));
464 'session' => $this->sessionMock,
465 'sidResolver' => $this->sidResolverMock,
466 'queryParamsResolver' => $this->queryParamsResolverMock,
470 $this->sidResolverMock->expects($this->once())->method(
'getUseSessionInUrl')->will($this->returnValue(
true));
471 $this->sessionMock->expects($this->once())->method(
'getSessionIdForHost')->will($this->returnValue(
false));
472 $this->sidResolverMock->expects($this->once())->method(
'getUseSessionVar')->will($this->returnValue(
true));
473 $this->routeParamsResolverMock->expects($this->once())->method(
'hasData')->with(
'secure_is_forced')
474 ->will($this->returnValue(
true));
475 $this->sidResolverMock->expects($this->never())->method(
'getSessionIdQueryParam');
476 $this->queryParamsResolverMock->expects($this->once())
478 ->will($this->returnValue(
'foo=bar'));
480 $this->assertEquals(
'http://example.com/?foo=bar',
$model->getRedirectUrl(
'http://example.com/'));
488 'session' => $this->sessionMock,
489 'sidResolver' => $this->sidResolverMock,
490 'queryParamsResolver' => $this->queryParamsResolverMock,
494 $this->sidResolverMock->expects($this->once())->method(
'getUseSessionInUrl')->will($this->returnValue(
true));
495 $this->sessionMock->expects($this->once())->method(
'getSessionIdForHost')
496 ->will($this->returnValue(
'session-id'));
497 $this->sidResolverMock->expects($this->once())->method(
'getUseSessionVar')->will($this->returnValue(
false));
498 $this->sidResolverMock->expects($this->once())->method(
'getSessionIdQueryParam');
499 $this->queryParamsResolverMock->expects($this->once())
501 ->will($this->returnValue(
'foo=bar'));
503 $this->assertEquals(
'http://example.com/?foo=bar',
$model->getRedirectUrl(
'http://example.com/'));
513 'https://example.com:88/index.php/catalog/index/view?query=123#hash',
514 'https://example.com:88/index.php/catalog/index/view?query=123#hash' 517 'https://example.com/index.php/catalog/index/view?query=123#hash',
518 'https://example.com/index.php/catalog/index/view?query=123#hash' 521 'http://example.com/index.php/catalog/index/view?query=123#hash',
522 'http://example.com/index.php/catalog/index/view?query=123#hash' 531 $this->routeParamsResolverMock->expects($this->never())->method(
'unsetData');
532 $this->assertEquals(
'http://example.com',
$model->getRouteUrl(
'http://example.com'));
538 'session' => $this->sessionMock,
539 'sidResolver' => $this->sidResolverMock,
540 'queryParamsResolver' => $this->queryParamsResolverMock,
543 $this->sidResolverMock->expects($this->once())->method(
'getSessionIdQueryParam')->with($this->sessionMock)
544 ->will($this->returnValue(
'sid'));
545 $this->sessionMock->expects($this->once())->method(
'getSessionId')->will($this->returnValue(
'session-id'));
546 $this->queryParamsResolverMock->expects($this->once())->method(
'setQueryParam')->with(
'sid',
'session-id');
548 $model->addSessionParam();
559 $this->hostChecker = $this->getMockBuilder(HostChecker::class)
560 ->disableOriginalConstructor()->getMock();
561 $this->hostChecker->expects($this->once())->method(
'isOwnOrigin')->with($referrer)->willReturn(
$result);
562 $model = $this->
getUrlModel([
'hostChecker' => $this->hostChecker,
'request' => $requestMock]);
564 $requestMock->expects($this->once())->method(
'getServer')->with(
'HTTP_REFERER')
565 ->will($this->returnValue($referrer));
576 'is origin url' => [
true,
'http://localhost/'],
577 'is not origin url' => [
false,
'http://example.com/'],
591 $urlSecurityInfoMock = $this->createMock(\
Magento\Framework\
Url\SecurityInfoInterface::class);
593 'urlSecurityInfo' => $urlSecurityInfoMock,
595 'scopeResolver' => $this->scopeResolverMock,
596 'scopeConfig' => $this->scopeConfig,
599 $this->scopeConfig->expects($this->any())
601 ->with($this->equalTo($configPath), \
Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->scopeMock)
602 ->will($this->returnValue(
'http://localhost/'));
603 $this->routeParamsResolverMock->expects($this->at(0))->method(
'hasData')->with(
'secure_is_forced')
604 ->will($this->returnValue(
false));
605 $this->scopeResolverMock->expects($this->any())
607 ->will($this->returnValue($this->scopeMock));
608 $this->scopeMock->expects($this->once())->method(
'isUrlSecure')->will($this->returnValue(
true));
609 $this->routeParamsResolverMock->expects($this->at(1))->method(
'hasData')->with(
'secure')
610 ->will($this->returnValue(
false));
611 $this->routeParamsResolverMock->expects($this->any())->method(
'getType')
612 ->will($this->returnValue($urlType));
613 $this->routeParamsResolverMock->expects($this->once())
615 ->will($this->returnValue($isSecure));
616 $urlSecurityInfoMock->expects($this->exactly($isSecureCallCount))->method(
'isSecure')
617 ->will($this->returnValue(
false));
619 $this->assertEquals(
'http://localhost/',
$model->getConfigData($key));
628 'secure url' => [
'some-type',
'web/secure/base_url_secure',
true, 0,
'base_url_secure'],
631 'web/unsecure/base_url_unsecure',
643 'scopeResolver' => $this->scopeResolverMock,
644 'scopeConfig' => $this->scopeConfig,
647 $this->scopeConfig->expects($this->any())
650 'web/secure/base_url_secure_forced',
651 \
Magento\Store\Model\ScopeInterface::SCOPE_STORE,
654 ->will($this->returnValue(
'http://localhost/'));
655 $this->routeParamsResolverMock->expects($this->once())->method(
'hasData')->with(
'secure_is_forced')
656 ->will($this->returnValue(
true));
657 $this->routeParamsResolverMock->expects($this->once())->method(
'getData')->with(
'secure')
658 ->will($this->returnValue(
true));
660 $this->scopeResolverMock->expects($this->any())
662 ->will($this->returnValue($this->scopeMock));
663 $this->assertEquals(
'http://localhost/',
$model->getConfigData(
'base_url_secure_forced'));
676 'session' => $this->sessionMock,
677 'request' => $requestMock,
678 'sidResolver' => $this->sidResolverMock,
679 'scopeResolver' => $this->scopeResolverMock,
684 $requestMock->expects($this->once())
685 ->method(
'getHttpHost')
686 ->will($this->returnValue(
'localhost'));
687 $this->scopeMock->expects($this->once())
688 ->method(
'getBaseUrl')
689 ->will($this->returnValue(
'http://localhost'));
690 $this->scopeResolverMock->expects($this->any())
692 ->will($this->returnValue($this->scopeMock));
693 $this->sidResolverMock->expects($this->never())
694 ->method(
'getSessionIdQueryParam');
704 'session' => $this->sessionMock,
705 'request' => $requestMock,
706 'sidResolver' => $this->sidResolverMock,
707 'scopeResolver' => $this->scopeResolverMock,
712 $requestMock->expects($this->once())->method(
'getHttpHost')->will($this->returnValue(
'localhost'));
713 $this->scopeMock->expects($this->once())
714 ->method(
'getBaseUrl')
715 ->will($this->returnValue(
'http://example.com'));
716 $this->scopeResolverMock->expects($this->any())
718 ->will($this->returnValue($this->scopeMock));
719 $this->sidResolverMock->expects($this->once())->method(
'getSessionIdQueryParam')
720 ->will($this->returnValue(
'SID'));
721 $this->sessionMock->expects($this->once())->method(
'getSessionId')
722 ->will($this->returnValue(
'session-id'));
725 '<a href="http://example.com/?SID=session-id">www.example.com</a>',
726 $model->sessionUrlVar(
'<a href="http://example.com/?___SID=U">www.example.com</a>')
737 '<a href="http://example.com/?___SID=U?SID=session-id">www.example.com</a>',
738 '<a href="http://example.com/?SID=session-id">www.example.com</a>',
741 '<a href="http://example.com/?___SID=U&SID=session-id">www.example.com</a>',
742 '<a href="http://example.com/?SID=session-id">www.example.com</a>',
745 '<a href="http://example.com/?foo=bar&___SID=U?SID=session-id">www.example.com</a>',
746 '<a href="http://example.com/?foo=bar?SID=session-id">www.example.com</a>',
749 '<a href="http://example.com/?foo=bar&___SID=U&SID=session-id">www.example.com</a>',
750 '<a href="http://example.com/?foo=bar&SID=session-id">www.example.com</a>',
759 $initRequestMock->expects($this->any())->method(
'getScheme')->will($this->returnValue(
'fake'));
760 $initRequestMock->expects($this->any())->method(
'getHttpHost')->will($this->returnValue(
'fake-host'));
761 $requestMock->expects($this->any())->method(
'getScheme')->will($this->returnValue(
'http'));
762 $requestMock->expects($this->any())->method(
'getHttpHost')->will($this->returnValue(
'example.com'));
765 $model->setRequest($requestMock);
766 $this->assertEquals(
'http://example.com',
$model->getCurrentUrl());
getRouteParamsResolverFactory($resolve=true)
testSessionUrlVarWithoutMatchedHostsAndBaseUrl()
testGetRouteUrlWithValidUrl()
testGetConfigData($urlType, $configPath, $isSecure, $isSecureCallCount, $key)
getRebuiltUrlDataProvider()
getUrlModel($arguments=[])
testGetUrlValidateFilter()
getConfigDataDataProvider()
testSessionUrlVarWithMatchedHostsAndBaseUrl($html, $result)
testGetUrlRouteUseRewrite()
testGetUrlIdempotentSetRoutePath()
testIsOwnOriginUrl($result, $referrer)
isOwnOriginUrlDataProvider()
testGetUrlIdempotentSetRouteName()
$routeParamsPreprocessorMock
testGetRebuiltUrl($inputUrl, $outputUrl)
testGetConfigDataWithSecureIsForcedParam()
testGetUrlWithAsterisksPath()
testGetUrl($query, $queryResult, $returnUri)
testGetRedirectUrlWithSessionId()
testGetCurrentUrl($httpHost, $url)
testGetBaseUrlNotLinkType()
testGetUrlRouteHasParams()
sessionUrlVarWithMatchedHostsAndBaseUrlDataProvider()