PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/integration/testsuite/Magento/Framework/UrlTest.php

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