UrlTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework;
  7. use Zend\Stdlib\Parameters;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. class UrlTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Framework\UrlInterface
  13. */
  14. protected $model;
  15. protected function setUp()
  16. {
  17. $this->model = Bootstrap::getObjectManager()->create(\Magento\Framework\Url::class);
  18. }
  19. public function testSetGetUseSession()
  20. {
  21. $this->assertTrue((bool)$this->model->getUseSession());
  22. $this->model->setUseSession(false);
  23. $this->assertFalse($this->model->getUseSession());
  24. }
  25. public function testSetRouteFrontName()
  26. {
  27. $value = 'route';
  28. $this->model->setRouteFrontName($value);
  29. $this->assertEquals($value, $this->model->getData('route_front_name'));
  30. }
  31. public function testGetConfigData()
  32. {
  33. $this->assertEquals('http://localhost/', $this->model->getConfigData('base_url'));
  34. }
  35. /**
  36. * Note: isolation should be raised to flush the URL memory cache maintained by the store model
  37. * @magentoAppIsolation enabled
  38. */
  39. public function testGetBaseUrlDefaults()
  40. {
  41. $this->assertEquals('http://localhost/index.php/', $this->model->getBaseUrl());
  42. }
  43. /**
  44. * Note: isolation flushes the URL memory cache
  45. * @magentoAppIsolation enabled
  46. * @magentoConfigFixture current_store web/seo/use_rewrites 1
  47. */
  48. public function testGetBaseUrlSeoRewrites()
  49. {
  50. $this->assertEquals('http://localhost/', $this->model->getBaseUrl());
  51. }
  52. /**
  53. * Note: isolation flushes the URL memory cache
  54. * @magentoAppIsolation enabled
  55. *
  56. * @dataProvider getBaseUrlConfiguredDataProvider
  57. *
  58. * @magentoConfigFixture current_store web/secure/base_url http://sample.com/base_path/
  59. * @magentoConfigFixture current_store web/unsecure/base_link_url http://sample.com/base_link_path/
  60. * @magentoConfigFixture current_store web/secure/base_link_url https://sample.com/base_link_path/
  61. * @magentoConfigFixture current_store web/secure/use_in_frontend 1
  62. *
  63. * @param array $params
  64. * @param string $expectedUrl
  65. */
  66. public function testGetBaseUrlConfigured($params, $expectedUrl)
  67. {
  68. $actualUrl = $this->model->getBaseUrl($params);
  69. $this->assertEquals($expectedUrl, $actualUrl);
  70. }
  71. /**
  72. * Note: isolation flushes the URL memory cache
  73. * @magentoAppIsolation enabled
  74. *
  75. * @magentoConfigFixture current_store web/secure/base_url http://sample.com/
  76. * @magentoConfigFixture current_store web/unsecure/base_link_url http://sample.com/
  77. * @magentoConfigFixture current_store web/secure/base_link_url https://sample.com/
  78. * @magentoConfigFixture current_store web/secure/use_in_frontend 1
  79. *
  80. * @magentoAppArea frontend
  81. */
  82. public function testGetUnsecureUrlInSecureArea()
  83. {
  84. /** @var \Magento\Framework\App\Request\Http $request */
  85. $request = Bootstrap::getObjectManager()->create(\Magento\Framework\App\Request\Http::class);
  86. //Emulate HTTPS request
  87. $request->getServer()->set('HTTPS', 'on');
  88. $request->getServer()->set('SERVER_PORT', 443);
  89. $model = Bootstrap::getObjectManager()->create(\Magento\Framework\Url::class, ['request' => $request]);
  90. $secureUrl = $model->getUrl('some/index/controller', ['_nosid' => 1]);
  91. $this->assertEquals(
  92. 'https://sample.com/index.php/some/index/controller/',
  93. $secureUrl,
  94. 'Default URL in secure area is incorrect'
  95. );
  96. $secureUrl = $model->getUrl('some/index/controller', ['_secure' => true, '_nosid' => 1]);
  97. $this->assertEquals(
  98. 'https://sample.com/index.php/some/index/controller/',
  99. $secureUrl,
  100. 'Secure URL in secure area is incorrect'
  101. );
  102. $unsecureUrl = $model->getUrl('some/index/controller', ['_secure' => false, '_nosid' => 1]);
  103. $this->assertEquals(
  104. 'http://sample.com/index.php/some/index/controller/',
  105. $unsecureUrl,
  106. 'Unsecure URL in secure area is incorrect'
  107. );
  108. }
  109. /**
  110. * Note: isolation flushes the URL memory cache
  111. * @magentoAppIsolation enabled
  112. *
  113. * @magentoConfigFixture current_store web/secure/base_url http://sample.com/
  114. * @magentoConfigFixture current_store web/unsecure/base_link_url http://sample.com/
  115. * @magentoConfigFixture current_store web/secure/base_link_url https://sample.com/
  116. * @magentoConfigFixture current_store web/secure/use_in_frontend 1
  117. *
  118. * @magentoAppArea frontend
  119. */
  120. public function testGetSecureUrlInUnsecureArea()
  121. {
  122. /** @var \Magento\Framework\App\Request\Http $request */
  123. $request = Bootstrap::getObjectManager()->create(\Magento\Framework\App\Request\Http::class);
  124. //Emulate HTTPS request
  125. $request->getServer()->set('HTTPS', 'off');
  126. $request->getServer()->set('SERVER_PORT', 80);
  127. $model = Bootstrap::getObjectManager()->create(\Magento\Framework\Url::class, ['request' => $request]);
  128. $secureUrl = $model->getUrl('some/index/controller', ['_nosid' => 1]);
  129. $this->assertEquals(
  130. 'http://sample.com/index.php/some/index/controller/',
  131. $secureUrl,
  132. 'Default URL in unsecure area is incorrect'
  133. );
  134. $secureUrl = $model->getUrl('some/index/controller', ['_secure' => true, '_nosid' => 1]);
  135. $this->assertEquals(
  136. 'https://sample.com/index.php/some/index/controller/',
  137. $secureUrl,
  138. 'Secure URL in unsecure area is incorrect'
  139. );
  140. $unsecureUrl = $model->getUrl('some/index/controller', ['_secure' => false, '_nosid' => 1]);
  141. $this->assertEquals(
  142. 'http://sample.com/index.php/some/index/controller/',
  143. $unsecureUrl,
  144. 'Unsecure URL in unsecure area is incorrect'
  145. );
  146. }
  147. /**
  148. * Check that url type is restored to default after call getBaseUrl with type specified in params
  149. */
  150. public function testGetBaseUrlWithTypeRestoring()
  151. {
  152. /**
  153. * Get base URL with default type
  154. */
  155. $this->assertEquals('http://localhost/index.php/', $this->model->getBaseUrl(), 'Incorrect link url');
  156. /**
  157. * Set specified type
  158. */
  159. $webUrl = $this->model->getBaseUrl(['_type' => \Magento\Framework\UrlInterface::URL_TYPE_WEB]);
  160. $this->assertEquals('http://localhost/', $webUrl, 'Incorrect web url');
  161. $this->assertEquals('http://localhost/index.php/', $this->model->getBaseUrl(), 'Incorrect link url');
  162. /**
  163. * Get url with type specified in params
  164. */
  165. $mediaUrl = $this->model->getBaseUrl(['_type' => \Magento\Framework\UrlInterface::URL_TYPE_MEDIA]);
  166. $this->assertEquals('http://localhost/pub/media/', $mediaUrl, 'Incorrect media url');
  167. $this->assertEquals('http://localhost/index.php/', $this->model->getBaseUrl(), 'Incorrect link url');
  168. }
  169. public function getBaseUrlConfiguredDataProvider()
  170. {
  171. return [
  172. [['_type' => \Magento\Framework\UrlInterface::URL_TYPE_WEB], 'http://sample.com/base_path/'],
  173. [
  174. ['_type' => \Magento\Framework\UrlInterface::URL_TYPE_LINK],
  175. 'http://sample.com/base_link_path/index.php/'
  176. ],
  177. [
  178. ['_type' => \Magento\Framework\UrlInterface::URL_TYPE_LINK, '_secure' => 1],
  179. 'https://sample.com/base_link_path/index.php/'
  180. ]
  181. ];
  182. }
  183. public function testSetGetRouteName()
  184. {
  185. $this->model->setRouteName('catalog');
  186. $this->assertEquals('catalog', $this->model->getRouteName());
  187. $this->markTestIncomplete('setRouteName() logic is unclear.');
  188. }
  189. public function testSetGetControllerName()
  190. {
  191. $this->model->setControllerName('product');
  192. $this->assertEquals('product', $this->model->getControllerName());
  193. $this->markTestIncomplete('setControllerName() logic is unclear.');
  194. }
  195. public function testSetGetActionName()
  196. {
  197. $this->model->setActionName('view');
  198. $this->assertEquals('view', $this->model->getActionName());
  199. $this->markTestIncomplete('setActionName() logic is unclear.');
  200. }
  201. /**
  202. * Note: isolation flushes the URL memory cache
  203. * @magentoAppIsolation enabled
  204. */
  205. public function testGetRouteUrl()
  206. {
  207. $this->assertEquals('http://localhost/index.php/', $this->model->getRouteUrl());
  208. $this->assertEquals(
  209. 'http://localhost/index.php/catalog/product/view/id/50/',
  210. $this->model->getRouteUrl('catalog/product/view', ['id' => 50])
  211. );
  212. $this->assertEquals(
  213. 'http://localhost/index.php/fancy_uri',
  214. $this->model->getRouteUrl('core/index/index', ['_direct' => 'fancy_uri'])
  215. );
  216. }
  217. public function testSetGetFragment()
  218. {
  219. $this->model->setFragment('value');
  220. $this->assertEquals('value', $this->model->getFragment());
  221. }
  222. /**
  223. * Note: isolation flushes the URL memory cache
  224. * @magentoAppIsolation enabled
  225. */
  226. public function testGetUrl()
  227. {
  228. $result = $this->model->getUrl(
  229. 'catalog/product/view',
  230. ['_fragment' => 'anchor', '_escape' => 1, '_query' => 'foo=bar', '_nosid' => 1, 'id' => 100]
  231. );
  232. $this->assertEquals('http://localhost/index.php/catalog/product/view/id/100/?foo=bar#anchor', $result);
  233. }
  234. /**
  235. * Note: isolation flushes the URL memory cache
  236. * @magentoAppIsolation enabled
  237. */
  238. public function testGetUrlDoesntAddQueryParamsOnConsequentCalls()
  239. {
  240. $result = $this->model->getUrl('catalog/product/view', ['_query' => 'foo=bar', '_nosid' => 1]);
  241. $this->assertEquals('http://localhost/index.php/catalog/product/view/?foo=bar', $result);
  242. $result = $this->model->getUrl('catalog/product/view', ['_nosid' => 1]);
  243. $this->assertEquals('http://localhost/index.php/catalog/product/view/', $result);
  244. }
  245. /**
  246. * Note: isolation flushes the URL memory cache
  247. * @magentoAppIsolation enabled
  248. * @covers \Magento\Framework\Url::getUrl
  249. */
  250. public function testGetUrlDoesntAddFragmentOnConsequentCalls()
  251. {
  252. $result = $this->model->getUrl('catalog/product/view', ['_nosid' => 1, '_fragment' => 'section']);
  253. $this->assertEquals('http://localhost/index.php/catalog/product/view/#section', $result);
  254. $result = $this->model->getUrl('catalog/product/view', ['_nosid' => 1]);
  255. $this->assertEquals('http://localhost/index.php/catalog/product/view/', $result);
  256. }
  257. /**
  258. * Note: isolation flushes the URL memory cache
  259. * @magentoAppIsolation enabled
  260. *
  261. * @dataProvider consequentCallsDataProvider
  262. *
  263. * @param string $firstCallUrl
  264. * @param string $secondCallUrl
  265. * @param array $firstRouteParams
  266. * @param array $secondRouteParams
  267. * @param string $firstExpectedUrl
  268. * @param string $secondExpectedUrl
  269. * @covers \Magento\Framework\Url::getUrl
  270. */
  271. public function testGetUrlOnConsequentCalls(
  272. $firstCallUrl,
  273. $secondCallUrl,
  274. $firstRouteParams,
  275. $secondRouteParams,
  276. $firstExpectedUrl,
  277. $secondExpectedUrl
  278. ) {
  279. $result = $this->model->getUrl($firstCallUrl, $firstRouteParams);
  280. $this->assertEquals($firstExpectedUrl, $result);
  281. $result = $this->model->getUrl($secondCallUrl, $secondRouteParams);
  282. $this->assertEquals($secondExpectedUrl, $result);
  283. }
  284. /**
  285. * Data provider for testGetUrlOnConsequentCalls()
  286. *
  287. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  288. * @return array
  289. */
  290. public function consequentCallsDataProvider()
  291. {
  292. return [
  293. [
  294. 'r_1/c_1/a_1/p_1/v_1',
  295. 'r_1/c_1/a_1/p_1/v_1',
  296. null,
  297. null,
  298. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/',
  299. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/',
  300. ],
  301. [
  302. 'r_1/c_1/a_1/p_1/v_1',
  303. 'r_1/c_1/a_1/p_1/v_2',
  304. null,
  305. null,
  306. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/',
  307. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_2/'
  308. ],
  309. [
  310. 'r_1/c_1/a_1/p_1/v_1',
  311. 'r_1/c_1/a_1/p_1',
  312. null,
  313. null,
  314. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/',
  315. 'http://localhost/index.php/r_1/c_1/a_1/'
  316. ],
  317. [
  318. 'r_1/c_1/a_1/p_1/v_1',
  319. 'r_1/c_1/a_1/p_2/v_2',
  320. null,
  321. null,
  322. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/',
  323. 'http://localhost/index.php/r_1/c_1/a_1/p_2/v_2/'
  324. ],
  325. [
  326. 'r_1/c_1/a_1/p_1/v_1',
  327. 'r_1/c_1/a_1',
  328. null,
  329. null,
  330. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/',
  331. 'http://localhost/index.php/r_1/c_1/a_1/'
  332. ],
  333. [
  334. 'r_1/c_1/a_1/p_1/v_1',
  335. 'r_1/c_1/a_2',
  336. null,
  337. null,
  338. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/',
  339. 'http://localhost/index.php/r_1/c_1/a_2/'
  340. ],
  341. [
  342. 'r_1/c_1/a_1/p_1/v_1',
  343. 'r_1/c_1',
  344. null,
  345. null,
  346. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/',
  347. 'http://localhost/index.php/r_1/c_1/'
  348. ],
  349. [
  350. 'r_1/c_1/a_1/p_1/v_1',
  351. 'r_1/c_2',
  352. null,
  353. null,
  354. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/',
  355. 'http://localhost/index.php/r_1/c_2/'
  356. ],
  357. [
  358. 'r_1/c_1/a_1/p_1/v_1',
  359. 'r_1',
  360. null,
  361. null,
  362. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/',
  363. 'http://localhost/index.php/r_1/'
  364. ],
  365. [
  366. 'r_1/c_1/a_1/p_1/v_1',
  367. 'r_2',
  368. null,
  369. null,
  370. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/',
  371. 'http://localhost/index.php/r_2/'
  372. ],
  373. [
  374. 'r_1/c_1/a_1/p_1/v_1',
  375. null,
  376. null,
  377. null,
  378. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/',
  379. 'http://localhost/index.php/'
  380. ],
  381. [
  382. 'r_1/c_1/a_1',
  383. 'r_1/c_1/a_1/p_1/v_1',
  384. null,
  385. null,
  386. 'http://localhost/index.php/r_1/c_1/a_1/',
  387. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/'
  388. ],
  389. [
  390. null,
  391. 'r_1/c_1/a_1',
  392. null,
  393. null,
  394. 'http://localhost/index.php/',
  395. 'http://localhost/index.php/r_1/c_1/a_1/'
  396. ],
  397. [
  398. 'r_1/c_1/a_1/p_1/v_1',
  399. 'r_1/c_1/a_1/p_1/v_1',
  400. ['p_2' => 'v_2'],
  401. ['p_2' => 'v_2'],
  402. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/p_2/v_2/',
  403. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/p_2/v_2/'
  404. ],
  405. [
  406. 'r_1/c_1/a_1/p_1/v_1',
  407. 'r_1/c_1/a_1',
  408. ['p_2' => 'v_2'],
  409. ['p_2' => 'v_2'],
  410. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/p_2/v_2/',
  411. 'http://localhost/index.php/r_1/c_1/a_1/p_2/v_2/'
  412. ],
  413. [
  414. 'r_1/c_1/a_1/p_1/v_1',
  415. null,
  416. ['p_2' => 'v_2'],
  417. ['p_1' => 'v_1', 'p_2' => 'v_2'],
  418. 'http://localhost/index.php/r_1/c_1/a_1/p_1/v_1/p_2/v_2/',
  419. 'http://localhost/index.php/p_1/v_1/p_2/v_2/'
  420. ]
  421. ];
  422. }
  423. public function testEscape()
  424. {
  425. $this->assertEquals('%22%27%3E%3C', $this->model->escape('"\'><'));
  426. }
  427. /**
  428. * Note: isolation flushes the URL memory cache
  429. * @magentoAppIsolation enabled
  430. */
  431. public function testGetDirectUrl()
  432. {
  433. $directUrl = $this->model->getDirectUrl('fancy_uri', ['_query' => ['foo' => 'bar']]);
  434. $this->assertEquals('http://localhost/index.php/fancy_uri?foo=bar', $directUrl);
  435. }
  436. /**
  437. * Note: isolation flushes the URL memory cache
  438. * @magentoAppIsolation enabled
  439. *
  440. * Note: to enforce SID in URLs, base URL must be different from the current $_SERVER['HTTP_HOST']
  441. * @magentoConfigFixture current_store web/unsecure/base_link_url http://domain.com/
  442. */
  443. public function testSessionUrlVar()
  444. {
  445. $sessionId = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  446. \Magento\Framework\Session\Generic::class
  447. )->getSessionId();
  448. $sessionUrl = $this->model->sessionUrlVar('<a href="http://example.com/?___SID=U">www.example.com</a>');
  449. $this->assertEquals('<a href="http://example.com/?SID=' . $sessionId . '">www.example.com</a>', $sessionUrl);
  450. }
  451. public function testUseSessionIdForUrl()
  452. {
  453. $_SERVER['HTTP_HOST'] = 'localhost';
  454. $this->assertFalse($this->model->useSessionIdForUrl(true));
  455. $this->assertFalse($this->model->useSessionIdForUrl(false));
  456. }
  457. /**
  458. * Note: isolation flushes the URL memory cache
  459. * @magentoAppIsolation enabled
  460. */
  461. public function testIsOwnOriginUrl()
  462. {
  463. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  464. /** @var $request \Magento\TestFramework\Request */
  465. $request = $objectManager->get(\Magento\Framework\App\RequestInterface::class);
  466. $request->setServer(new Parameters(['HTTP_REFERER' => 'http://localhost/']));
  467. $this->assertTrue($this->model->isOwnOriginUrl());
  468. $request->setServer(new Parameters(['HTTP_REFERER' => 'http://example.com/']));
  469. $this->assertFalse($this->model->isOwnOriginUrl());
  470. }
  471. }