UrlTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Test\Unit;
  7. use Magento\Framework\Url\HostChecker;
  8. /**
  9. * Test class for Magento\Framework\Url
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class UrlTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\Url\RouteParamsResolver|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $routeParamsResolverMock;
  18. /**
  19. * @var \Magento\Framework\Url\RouteParamsPreprocessorInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $routeParamsPreprocessorMock;
  22. /**
  23. * @var \Magento\Framework\Url\ScopeResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $scopeResolverMock;
  26. /**
  27. * @var \Magento\Framework\Url\ScopeInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $scopeMock;
  30. /**
  31. * @var \Magento\Framework\Url\QueryParamsResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $queryParamsResolverMock;
  34. /**
  35. * @var \Magento\Framework\Session\SidResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $sidResolverMock;
  38. /**
  39. * @var \Magento\Framework\Session\Generic|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $sessionMock;
  42. /**
  43. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $scopeConfig;
  46. /**
  47. * @var \Magento\Framework\Url\ModifierInterface|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $urlModifier;
  50. /**
  51. * @var HostChecker|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $hostChecker;
  54. protected function setUp()
  55. {
  56. $this->routeParamsResolverMock = $this->createPartialMock(
  57. \Magento\Framework\Url\RouteParamsResolver::class,
  58. ['getType', 'hasData', 'getData', 'getRouteParams', 'unsetData']
  59. );
  60. $escaperMock = $this->createMock(\Magento\Framework\ZendEscaper::class);
  61. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  62. $objectManager->setBackwardCompatibleProperty($this->routeParamsResolverMock, 'escaper', $escaperMock);
  63. $this->routeParamsPreprocessorMock = $this->getMockForAbstractClass(
  64. \Magento\Framework\Url\RouteParamsPreprocessorInterface::class,
  65. ['unsetData'],
  66. '',
  67. false,
  68. true,
  69. true,
  70. []
  71. );
  72. $this->scopeResolverMock = $this->createMock(\Magento\Framework\Url\ScopeResolverInterface::class);
  73. $this->scopeMock = $this->createMock(\Magento\Framework\Url\ScopeInterface::class);
  74. $this->queryParamsResolverMock = $this->createMock(\Magento\Framework\Url\QueryParamsResolverInterface::class);
  75. $this->sidResolverMock = $this->createMock(\Magento\Framework\Session\SidResolverInterface::class);
  76. $this->sessionMock = $this->createMock(\Magento\Framework\Session\Generic::class);
  77. $this->scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  78. $this->urlModifier = $this->createMock(\Magento\Framework\Url\ModifierInterface::class);
  79. $this->urlModifier->expects($this->any())
  80. ->method('execute')
  81. ->willReturnArgument(0);
  82. }
  83. /**
  84. * @param bool $resolve
  85. * @return \Magento\Framework\Url\RouteParamsResolverFactory|\PHPUnit_Framework_MockObject_MockObject
  86. */
  87. protected function getRouteParamsResolverFactory($resolve = true)
  88. {
  89. $routeParamsResolverFactoryMock = $this->createMock(\Magento\Framework\Url\RouteParamsResolverFactory::class);
  90. if ($resolve) {
  91. $routeParamsResolverFactoryMock->expects($this->once())->method('create')
  92. ->will($this->returnValue($this->routeParamsResolverMock));
  93. }
  94. return $routeParamsResolverFactoryMock;
  95. }
  96. /**
  97. * @param array $mockMethods
  98. * @return \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  99. */
  100. protected function getRequestMock()
  101. {
  102. return $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  103. ->disableOriginalConstructor()->getMock();
  104. }
  105. /**
  106. * @param array $arguments
  107. * @return \Magento\Framework\Url
  108. */
  109. protected function getUrlModel($arguments = [])
  110. {
  111. $arguments = array_merge($arguments, ['scopeType' => \Magento\Store\Model\ScopeInterface::SCOPE_STORE]);
  112. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  113. $model = $objectManager->getObject(\Magento\Framework\Url::class, $arguments);
  114. $modelProperty = (new \ReflectionClass(get_class($model)))
  115. ->getProperty('urlModifier');
  116. $modelProperty->setAccessible(true);
  117. $modelProperty->setValue($model, $this->urlModifier);
  118. $zendEscaper = new \Magento\Framework\ZendEscaper();
  119. $escaper = new \Magento\Framework\Escaper();
  120. $objectManager->setBackwardCompatibleProperty($escaper, 'escaper', $zendEscaper);
  121. $objectManager->setBackwardCompatibleProperty($model, 'escaper', $escaper);
  122. return $model;
  123. }
  124. /**
  125. * @param string $httpHost
  126. * @param string $url
  127. * @dataProvider getCurrentUrlProvider
  128. */
  129. public function testGetCurrentUrl($httpHost, $url)
  130. {
  131. $requestMock = $this->getRequestMock();
  132. $requestMock->expects($this->once())->method('getRequestUri')->willReturn('/fancy_uri');
  133. $requestMock->expects($this->once())->method('getScheme')->will($this->returnValue('http'));
  134. $requestMock->expects($this->once())->method('getHttpHost')->will($this->returnValue($httpHost));
  135. $model = $this->getUrlModel(['request' => $requestMock]);
  136. $this->assertEquals($url, $model->getCurrentUrl());
  137. }
  138. /**
  139. * @return array
  140. */
  141. public function getCurrentUrlProvider()
  142. {
  143. return [
  144. 'without_port' => ['example.com', 'http://example.com/fancy_uri'],
  145. 'default_port' => ['example.com:80', 'http://example.com/fancy_uri'],
  146. 'custom_port' => ['example.com:8080', 'http://example.com:8080/fancy_uri']
  147. ];
  148. }
  149. public function testGetUseSession()
  150. {
  151. $model = $this->getUrlModel();
  152. $model->setUseSession(false);
  153. $this->assertFalse((bool)$model->getUseSession());
  154. $model->setUseSession(true);
  155. $this->assertTrue($model->getUseSession());
  156. }
  157. public function testGetBaseUrlNotLinkType()
  158. {
  159. $model = $this->getUrlModel(
  160. [
  161. 'scopeResolver' => $this->scopeResolverMock,
  162. 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory()
  163. ]
  164. );
  165. $baseUrl = 'base-url';
  166. $urlType = 'not-link';
  167. $this->routeParamsResolverMock->expects($this->any())->method('getType')->will($this->returnValue($urlType));
  168. $this->scopeMock->expects($this->once())->method('getBaseUrl')->will($this->returnValue($baseUrl));
  169. $this->scopeResolverMock->expects($this->any())
  170. ->method('getScope')
  171. ->will($this->returnValue($this->scopeMock));
  172. $baseUrlParams = ['_scope' => $this->scopeMock, '_type' => $urlType, '_secure' => true];
  173. $this->assertEquals($baseUrl, $model->getBaseUrl($baseUrlParams));
  174. }
  175. public function testGetUrlValidateFilter()
  176. {
  177. $model = $this->getUrlModel();
  178. $this->assertEquals('http://test.com', $model->getUrl('http://test.com'));
  179. }
  180. /**
  181. * @param string|array|bool $query
  182. * @param string $queryResult
  183. * @param string $returnUri
  184. * @dataProvider getUrlDataProvider
  185. */
  186. public function testGetUrl($query, $queryResult, $returnUri)
  187. {
  188. $requestMock = $this->getRequestMock();
  189. $routeConfigMock = $this->createMock(\Magento\Framework\App\Route\ConfigInterface::class);
  190. $model = $this->getUrlModel(
  191. [
  192. 'scopeResolver' => $this->scopeResolverMock,
  193. 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
  194. 'queryParamsResolver' => $this->queryParamsResolverMock,
  195. 'request' => $requestMock,
  196. 'routeConfig' => $routeConfigMock,
  197. 'routeParamsPreprocessor' => $this->routeParamsPreprocessorMock
  198. ]
  199. );
  200. $baseUrl = 'http://localhost/index.php/';
  201. $urlType = \Magento\Framework\UrlInterface::URL_TYPE_LINK;
  202. $this->scopeMock->expects($this->once())->method('getBaseUrl')->will($this->returnValue($baseUrl));
  203. $this->scopeResolverMock->expects($this->any())
  204. ->method('getScope')
  205. ->will($this->returnValue($this->scopeMock));
  206. $this->routeParamsResolverMock->expects($this->any())->method('getType')->will($this->returnValue($urlType));
  207. $this->routeParamsResolverMock->expects($this->any())->method('getRouteParams')
  208. ->will($this->returnValue(['id' => 100]));
  209. $this->routeParamsPreprocessorMock->expects($this->once())
  210. ->method('execute')
  211. ->willReturnArgument(2);
  212. $requestMock->expects($this->once())->method('isDirectAccessFrontendName')->will($this->returnValue(true));
  213. $routeConfigMock->expects($this->once())->method('getRouteFrontName')->will($this->returnValue('catalog'));
  214. $this->queryParamsResolverMock->expects($this->once())->method('getQuery')
  215. ->will($this->returnValue($queryResult));
  216. $url = $model->getUrl('catalog/product/view', [
  217. '_scope' => $this->getMockForAbstractClass(\Magento\Store\Api\Data\StoreInterface::class),
  218. '_fragment' => 'anchor',
  219. '_escape' => 1,
  220. '_query' => $query,
  221. '_nosid' => 0,
  222. 'id' => 100
  223. ]);
  224. $this->assertEquals($returnUri, $url);
  225. }
  226. public function testGetUrlIdempotentSetRoutePath()
  227. {
  228. $model = $this->getUrlModel([
  229. 'scopeResolver' => $this->scopeResolverMock,
  230. 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
  231. ]);
  232. $model->setData('route_path', 'catalog/product/view');
  233. $this->scopeResolverMock->expects($this->any())
  234. ->method('getScope')
  235. ->will($this->returnValue($this->scopeMock));
  236. $this->urlModifier->expects($this->exactly(1))->method('execute');
  237. $this->assertEquals('catalog/product/view', $model->getUrl('catalog/product/view'));
  238. }
  239. public function testGetUrlIdempotentSetRouteName()
  240. {
  241. $model = $this->getUrlModel([
  242. 'scopeResolver' => $this->scopeResolverMock,
  243. 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
  244. 'request' => $this->getRequestMock()
  245. ]);
  246. $model->setData('route_name', 'catalog');
  247. $this->scopeResolverMock->expects($this->any())
  248. ->method('getScope')
  249. ->will($this->returnValue($this->scopeMock));
  250. $this->assertEquals('/product/view/', $model->getUrl('catalog/product/view'));
  251. }
  252. public function testGetUrlRouteHasParams()
  253. {
  254. $this->routeParamsResolverMock->expects($this->any())->method('getRouteParams')
  255. ->will($this->returnValue(['foo' => 'bar', 'true' => false]));
  256. $model = $this->getUrlModel([
  257. 'scopeResolver' => $this->scopeResolverMock,
  258. 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
  259. 'request' => $this->getRequestMock()
  260. ]);
  261. $this->scopeResolverMock->expects($this->any())
  262. ->method('getScope')
  263. ->will($this->returnValue($this->scopeMock));
  264. $this->assertEquals('/index/index/foo/bar/', $model->getUrl('catalog'));
  265. }
  266. public function testGetUrlRouteUseRewrite()
  267. {
  268. $this->routeParamsResolverMock->expects($this->any())->method('getRouteParams')
  269. ->will($this->returnValue(['foo' => 'bar']));
  270. $this->routeParamsPreprocessorMock->expects($this->once())
  271. ->method('execute')
  272. ->willReturnArgument(2);
  273. $request = $this->getRequestMock();
  274. $request->expects($this->once())->method('getAlias')->will($this->returnValue('/catalog/product/view/'));
  275. $model = $this->getUrlModel([
  276. 'scopeResolver' => $this->scopeResolverMock,
  277. 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
  278. 'request' => $request,
  279. 'routeParamsPreprocessor' => $this->routeParamsPreprocessorMock
  280. ]);
  281. $this->scopeResolverMock->expects($this->any())
  282. ->method('getScope')
  283. ->will($this->returnValue($this->scopeMock));
  284. $this->assertEquals('/catalog/product/view/', $model->getUrl('catalog', ['_use_rewrite' => 1]));
  285. }
  286. /**
  287. * @return array
  288. */
  289. public function getUrlDataProvider()
  290. {
  291. return [
  292. 'string query' => [
  293. 'foo=bar',
  294. 'foo=bar',
  295. 'http://localhost/index.php/catalog/product/view/id/100/?foo=bar#anchor',
  296. ],
  297. 'array query' => [
  298. ['foo' => 'bar'],
  299. 'foo=bar',
  300. 'http://localhost/index.php/catalog/product/view/id/100/?foo=bar#anchor',
  301. ],
  302. 'without query' => [
  303. false,
  304. '',
  305. 'http://localhost/index.php/catalog/product/view/id/100/#anchor'
  306. ],
  307. ];
  308. }
  309. public function testGetUrlWithAsterisksPath()
  310. {
  311. $requestMock = $this->getRequestMock();
  312. $routeConfigMock = $this->createMock(\Magento\Framework\App\Route\ConfigInterface::class);
  313. $model = $this->getUrlModel(
  314. [
  315. 'scopeResolver' => $this->scopeResolverMock,
  316. 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
  317. 'queryParamsResolver' => $this->queryParamsResolverMock,
  318. 'request' => $requestMock, 'routeConfig' => $routeConfigMock,
  319. ]
  320. );
  321. $baseUrl = 'http://localhost/index.php/';
  322. $urlType = \Magento\Framework\UrlInterface::URL_TYPE_LINK;
  323. $this->scopeMock->expects($this->once())->method('getBaseUrl')->will($this->returnValue($baseUrl));
  324. $this->scopeResolverMock->expects($this->any())
  325. ->method('getScope')
  326. ->will($this->returnValue($this->scopeMock));
  327. $this->routeParamsResolverMock->expects($this->any())->method('getType')->will($this->returnValue($urlType));
  328. $this->routeParamsResolverMock->expects($this->any())->method('getRouteParams')
  329. ->will($this->returnValue(['key' => 'value']));
  330. $requestMock->expects($this->once())->method('isDirectAccessFrontendName')->will($this->returnValue(true));
  331. $requestMock->expects($this->once())->method('getRouteName')->will($this->returnValue('catalog'));
  332. $requestMock->expects($this->once())
  333. ->method('getControllerName')
  334. ->will($this->returnValue('product'));
  335. $requestMock->expects($this->once())->method('getActionName')->will($this->returnValue('view'));
  336. $routeConfigMock->expects($this->once())->method('getRouteFrontName')->will($this->returnValue('catalog'));
  337. $url = $model->getUrl('*/*/*/key/value');
  338. $this->assertEquals('http://localhost/index.php/catalog/product/view/key/value/', $url);
  339. }
  340. public function testGetDirectUrl()
  341. {
  342. $requestMock = $this->getRequestMock();
  343. $routeConfigMock = $this->createMock(\Magento\Framework\App\Route\ConfigInterface::class);
  344. $model = $this->getUrlModel(
  345. [
  346. 'scopeResolver' => $this->scopeResolverMock,
  347. 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
  348. 'queryParamsResolver' => $this->queryParamsResolverMock,
  349. 'request' => $requestMock,
  350. 'routeConfig' => $routeConfigMock,
  351. 'routeParamsPreprocessor' => $this->routeParamsPreprocessorMock
  352. ]
  353. );
  354. $baseUrl = 'http://localhost/index.php/';
  355. $urlType = \Magento\Framework\UrlInterface::URL_TYPE_LINK;
  356. $this->scopeMock->expects($this->once())->method('getBaseUrl')->will($this->returnValue($baseUrl));
  357. $this->scopeResolverMock->expects($this->any())
  358. ->method('getScope')
  359. ->will($this->returnValue($this->scopeMock));
  360. $this->routeParamsResolverMock->expects($this->any())->method('getType')->will($this->returnValue($urlType));
  361. $this->routeParamsPreprocessorMock->expects($this->once())
  362. ->method('execute')
  363. ->willReturnArgument(2);
  364. $requestMock->expects($this->once())->method('isDirectAccessFrontendName')->will($this->returnValue(true));
  365. $url = $model->getDirectUrl('direct-url');
  366. $this->assertEquals('http://localhost/index.php/direct-url', $url);
  367. }
  368. /**
  369. * @param string $inputUrl
  370. * @dataProvider getRebuiltUrlDataProvider
  371. */
  372. public function testGetRebuiltUrl($inputUrl, $outputUrl)
  373. {
  374. $requestMock = $this->getRequestMock();
  375. $model = $this->getUrlModel([
  376. 'session' => $this->sessionMock,
  377. 'request' => $requestMock,
  378. 'sidResolver' => $this->sidResolverMock,
  379. 'scopeResolver' => $this->scopeResolverMock,
  380. 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(false),
  381. 'queryParamsResolver' => $this->queryParamsResolverMock,
  382. ]);
  383. $this->queryParamsResolverMock->expects($this->once())->method('getQuery')
  384. ->will($this->returnValue('query=123'));
  385. $this->assertEquals($outputUrl, $model->getRebuiltUrl($inputUrl));
  386. }
  387. public function testGetRedirectUrl()
  388. {
  389. $model = $this->getUrlModel(
  390. [
  391. 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
  392. 'session' => $this->sessionMock,
  393. 'sidResolver' => $this->sidResolverMock,
  394. 'queryParamsResolver' => $this->queryParamsResolverMock,
  395. ]
  396. );
  397. $this->sidResolverMock->expects($this->once())->method('getUseSessionInUrl')->will($this->returnValue(true));
  398. $this->sessionMock->expects($this->once())->method('getSessionIdForHost')->will($this->returnValue(false));
  399. $this->sidResolverMock->expects($this->once())->method('getUseSessionVar')->will($this->returnValue(true));
  400. $this->routeParamsResolverMock->expects($this->once())->method('hasData')->with('secure_is_forced')
  401. ->will($this->returnValue(true));
  402. $this->sidResolverMock->expects($this->never())->method('getSessionIdQueryParam');
  403. $this->queryParamsResolverMock->expects($this->once())
  404. ->method('getQuery')
  405. ->will($this->returnValue('foo=bar'));
  406. $this->assertEquals('http://example.com/?foo=bar', $model->getRedirectUrl('http://example.com/'));
  407. }
  408. public function testGetRedirectUrlWithSessionId()
  409. {
  410. $model = $this->getUrlModel(
  411. [
  412. 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(false),
  413. 'session' => $this->sessionMock,
  414. 'sidResolver' => $this->sidResolverMock,
  415. 'queryParamsResolver' => $this->queryParamsResolverMock,
  416. ]
  417. );
  418. $this->sidResolverMock->expects($this->once())->method('getUseSessionInUrl')->will($this->returnValue(true));
  419. $this->sessionMock->expects($this->once())->method('getSessionIdForHost')
  420. ->will($this->returnValue('session-id'));
  421. $this->sidResolverMock->expects($this->once())->method('getUseSessionVar')->will($this->returnValue(false));
  422. $this->sidResolverMock->expects($this->once())->method('getSessionIdQueryParam');
  423. $this->queryParamsResolverMock->expects($this->once())
  424. ->method('getQuery')
  425. ->will($this->returnValue('foo=bar'));
  426. $this->assertEquals('http://example.com/?foo=bar', $model->getRedirectUrl('http://example.com/'));
  427. }
  428. /**
  429. * @return array
  430. */
  431. public function getRebuiltUrlDataProvider()
  432. {
  433. return [
  434. 'with port' => [
  435. 'https://example.com:88/index.php/catalog/index/view?query=123#hash',
  436. 'https://example.com:88/index.php/catalog/index/view?query=123#hash'
  437. ],
  438. 'without port' => [
  439. 'https://example.com/index.php/catalog/index/view?query=123#hash',
  440. 'https://example.com/index.php/catalog/index/view?query=123#hash'
  441. ],
  442. 'http' => [
  443. 'http://example.com/index.php/catalog/index/view?query=123#hash',
  444. 'http://example.com/index.php/catalog/index/view?query=123#hash'
  445. ]
  446. ];
  447. }
  448. public function testGetRouteUrlWithValidUrl()
  449. {
  450. $model = $this->getUrlModel(['routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(false)]);
  451. $this->routeParamsResolverMock->expects($this->never())->method('unsetData');
  452. $this->assertEquals('http://example.com', $model->getRouteUrl('http://example.com'));
  453. }
  454. public function testAddSessionParam()
  455. {
  456. $model = $this->getUrlModel([
  457. 'session' => $this->sessionMock,
  458. 'sidResolver' => $this->sidResolverMock,
  459. 'queryParamsResolver' => $this->queryParamsResolverMock,
  460. ]);
  461. $this->sidResolverMock->expects($this->once())->method('getSessionIdQueryParam')->with($this->sessionMock)
  462. ->will($this->returnValue('sid'));
  463. $this->sessionMock->expects($this->once())->method('getSessionId')->will($this->returnValue('session-id'));
  464. $this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('sid', 'session-id');
  465. $model->addSessionParam();
  466. }
  467. /**
  468. * @param bool $result
  469. * @param string $referrer
  470. * @dataProvider isOwnOriginUrlDataProvider
  471. */
  472. public function testIsOwnOriginUrl($result, $referrer)
  473. {
  474. $requestMock = $this->getRequestMock();
  475. $this->hostChecker = $this->getMockBuilder(HostChecker::class)
  476. ->disableOriginalConstructor()->getMock();
  477. $this->hostChecker->expects($this->once())->method('isOwnOrigin')->with($referrer)->willReturn($result);
  478. $model = $this->getUrlModel(['hostChecker' => $this->hostChecker, 'request' => $requestMock]);
  479. $requestMock->expects($this->once())->method('getServer')->with('HTTP_REFERER')
  480. ->will($this->returnValue($referrer));
  481. $this->assertEquals($result, $model->isOwnOriginUrl());
  482. }
  483. /**
  484. * @return array
  485. */
  486. public function isOwnOriginUrlDataProvider()
  487. {
  488. return [
  489. 'is origin url' => [true, 'http://localhost/'],
  490. 'is not origin url' => [false, 'http://example.com/'],
  491. ];
  492. }
  493. /**
  494. * @param string $urlType
  495. * @param string $configPath
  496. * @param bool $isSecure
  497. * @param int $isSecureCallCount
  498. * @param string $key
  499. * @dataProvider getConfigDataDataProvider
  500. */
  501. public function testGetConfigData($urlType, $configPath, $isSecure, $isSecureCallCount, $key)
  502. {
  503. $urlSecurityInfoMock = $this->createMock(\Magento\Framework\Url\SecurityInfoInterface::class);
  504. $model = $this->getUrlModel([
  505. 'urlSecurityInfo' => $urlSecurityInfoMock,
  506. 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
  507. 'scopeResolver' => $this->scopeResolverMock,
  508. 'scopeConfig' => $this->scopeConfig,
  509. ]);
  510. $this->scopeConfig->expects($this->any())
  511. ->method('getValue')
  512. ->with($this->equalTo($configPath), \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->scopeMock)
  513. ->will($this->returnValue('http://localhost/'));
  514. $this->routeParamsResolverMock->expects($this->at(0))->method('hasData')->with('secure_is_forced')
  515. ->will($this->returnValue(false));
  516. $this->scopeResolverMock->expects($this->any())
  517. ->method('getScope')
  518. ->will($this->returnValue($this->scopeMock));
  519. $this->scopeMock->expects($this->once())->method('isUrlSecure')->will($this->returnValue(true));
  520. $this->routeParamsResolverMock->expects($this->at(1))->method('hasData')->with('secure')
  521. ->will($this->returnValue(false));
  522. $this->routeParamsResolverMock->expects($this->any())->method('getType')
  523. ->will($this->returnValue($urlType));
  524. $this->routeParamsResolverMock->expects($this->once())
  525. ->method('getData')
  526. ->will($this->returnValue($isSecure));
  527. $urlSecurityInfoMock->expects($this->exactly($isSecureCallCount))->method('isSecure')
  528. ->will($this->returnValue(false));
  529. $this->assertEquals('http://localhost/', $model->getConfigData($key));
  530. }
  531. /**
  532. * @return array
  533. */
  534. public function getConfigDataDataProvider()
  535. {
  536. return [
  537. 'secure url' => ['some-type', 'web/secure/base_url_secure', true, 0, 'base_url_secure'],
  538. 'unsecure url' => [
  539. \Magento\Framework\UrlInterface::URL_TYPE_LINK,
  540. 'web/unsecure/base_url_unsecure',
  541. false,
  542. 1,
  543. 'base_url_unsecure',
  544. ],
  545. ];
  546. }
  547. public function testGetConfigDataWithSecureIsForcedParam()
  548. {
  549. $model = $this->getUrlModel([
  550. 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
  551. 'scopeResolver' => $this->scopeResolverMock,
  552. 'scopeConfig' => $this->scopeConfig,
  553. ]);
  554. $this->scopeConfig->expects($this->any())
  555. ->method('getValue')
  556. ->with(
  557. 'web/secure/base_url_secure_forced',
  558. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  559. $this->scopeMock
  560. )
  561. ->will($this->returnValue('http://localhost/'));
  562. $this->routeParamsResolverMock->expects($this->once())->method('hasData')->with('secure_is_forced')
  563. ->will($this->returnValue(true));
  564. $this->routeParamsResolverMock->expects($this->once())->method('getData')->with('secure')
  565. ->will($this->returnValue(true));
  566. $this->scopeResolverMock->expects($this->any())
  567. ->method('getScope')
  568. ->will($this->returnValue($this->scopeMock));
  569. $this->assertEquals('http://localhost/', $model->getConfigData('base_url_secure_forced'));
  570. }
  571. /**
  572. * @param string $html
  573. * @param string $result
  574. * @dataProvider sessionUrlVarWithMatchedHostsAndBaseUrlDataProvider
  575. */
  576. public function testSessionUrlVarWithMatchedHostsAndBaseUrl($html, $result)
  577. {
  578. $requestMock = $this->getRequestMock();
  579. $model = $this->getUrlModel(
  580. [
  581. 'session' => $this->sessionMock,
  582. 'request' => $requestMock,
  583. 'sidResolver' => $this->sidResolverMock,
  584. 'scopeResolver' => $this->scopeResolverMock,
  585. 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
  586. ]
  587. );
  588. $requestMock->expects($this->once())
  589. ->method('getHttpHost')
  590. ->will($this->returnValue('localhost'));
  591. $this->scopeMock->expects($this->once())
  592. ->method('getBaseUrl')
  593. ->will($this->returnValue('http://localhost'));
  594. $this->scopeResolverMock->expects($this->any())
  595. ->method('getScope')
  596. ->will($this->returnValue($this->scopeMock));
  597. $this->sidResolverMock->expects($this->never())
  598. ->method('getSessionIdQueryParam');
  599. $this->assertEquals($result, $model->sessionUrlVar($html));
  600. }
  601. public function testSessionUrlVarWithoutMatchedHostsAndBaseUrl()
  602. {
  603. $requestMock = $this->getRequestMock();
  604. $model = $this->getUrlModel(
  605. [
  606. 'session' => $this->sessionMock,
  607. 'request' => $requestMock,
  608. 'sidResolver' => $this->sidResolverMock,
  609. 'scopeResolver' => $this->scopeResolverMock,
  610. 'routeParamsResolverFactory' => $this->getRouteParamsResolverFactory(),
  611. ]
  612. );
  613. $requestMock->expects($this->once())->method('getHttpHost')->will($this->returnValue('localhost'));
  614. $this->scopeMock->expects($this->once())
  615. ->method('getBaseUrl')
  616. ->will($this->returnValue('http://example.com'));
  617. $this->scopeResolverMock->expects($this->any())
  618. ->method('getScope')
  619. ->will($this->returnValue($this->scopeMock));
  620. $this->sidResolverMock->expects($this->once())->method('getSessionIdQueryParam')
  621. ->will($this->returnValue('SID'));
  622. $this->sessionMock->expects($this->once())->method('getSessionId')
  623. ->will($this->returnValue('session-id'));
  624. $this->assertEquals(
  625. '<a href="http://example.com/?SID=session-id">www.example.com</a>',
  626. $model->sessionUrlVar('<a href="http://example.com/?___SID=U">www.example.com</a>')
  627. );
  628. }
  629. /**
  630. * @return array
  631. */
  632. public function sessionUrlVarWithMatchedHostsAndBaseUrlDataProvider()
  633. {
  634. return [
  635. [
  636. '<a href="http://example.com/?___SID=U?SID=session-id">www.example.com</a>',
  637. '<a href="http://example.com/?SID=session-id">www.example.com</a>',
  638. ],
  639. [
  640. '<a href="http://example.com/?___SID=U&SID=session-id">www.example.com</a>',
  641. '<a href="http://example.com/?SID=session-id">www.example.com</a>',
  642. ],
  643. [
  644. '<a href="http://example.com/?foo=bar&___SID=U?SID=session-id">www.example.com</a>',
  645. '<a href="http://example.com/?foo=bar?SID=session-id">www.example.com</a>',
  646. ],
  647. [
  648. '<a href="http://example.com/?foo=bar&___SID=U&SID=session-id">www.example.com</a>',
  649. '<a href="http://example.com/?foo=bar&SID=session-id">www.example.com</a>',
  650. ],
  651. ];
  652. }
  653. public function testSetRequest()
  654. {
  655. $initRequestMock = $this->getRequestMock();
  656. $requestMock = $this->getRequestMock();
  657. $initRequestMock->expects($this->any())->method('getScheme')->will($this->returnValue('fake'));
  658. $initRequestMock->expects($this->any())->method('getHttpHost')->will($this->returnValue('fake-host'));
  659. $requestMock->expects($this->any())->method('getScheme')->will($this->returnValue('http'));
  660. $requestMock->expects($this->any())->method('getHttpHost')->will($this->returnValue('example.com'));
  661. $model = $this->getUrlModel(['request' => $initRequestMock]);
  662. $model->setRequest($requestMock);
  663. $this->assertEquals('http://example.com', $model->getCurrentUrl());
  664. }
  665. }