RouterTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\UrlRewrite\Test\Unit\Controller;
  7. use Magento\Framework\App\Action\Forward;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
  10. use Magento\Store\Model\Store;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class RouterTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /** @var \Magento\UrlRewrite\Controller\Router */
  17. protected $router;
  18. /** @var \Magento\Framework\App\ActionFactory|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $actionFactory;
  20. /** @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $url;
  22. /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $storeManager;
  24. /** @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $store;
  26. /** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $response;
  28. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $request;
  30. /** @var \Magento\UrlRewrite\Model\UrlFinderInterface|\PHPUnit_Framework_MockObject_MockObject */
  31. protected $urlFinder;
  32. /**
  33. * @return void
  34. */
  35. protected function setUp()
  36. {
  37. $this->actionFactory = $this->createMock(\Magento\Framework\App\ActionFactory::class);
  38. $this->url = $this->createMock(\Magento\Framework\UrlInterface::class);
  39. $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  40. $this->response = $this->createPartialMock(
  41. \Magento\Framework\App\ResponseInterface::class,
  42. ['setRedirect', 'sendResponse']
  43. );
  44. $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  45. ->disableOriginalConstructor()->getMock();
  46. $this->urlFinder = $this->createMock(\Magento\UrlRewrite\Model\UrlFinderInterface::class);
  47. $this->store = $this->getMockBuilder(
  48. \Magento\Store\Model\Store::class
  49. )->disableOriginalConstructor()->getMock();
  50. $this->router = (new ObjectManager($this))->getObject(
  51. \Magento\UrlRewrite\Controller\Router::class,
  52. [
  53. 'actionFactory' => $this->actionFactory,
  54. 'url' => $this->url,
  55. 'storeManager' => $this->storeManager,
  56. 'response' => $this->response,
  57. 'urlFinder' => $this->urlFinder
  58. ]
  59. );
  60. }
  61. /**
  62. * @return void
  63. */
  64. public function testNoRewriteExist()
  65. {
  66. $this->urlFinder->expects($this->any())->method('findOneByData')->will($this->returnValue(null));
  67. $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->store));
  68. $this->store->expects($this->any())->method('getId')->will($this->returnValue('current-store-id'));
  69. $this->assertNull($this->router->match($this->request));
  70. }
  71. /**
  72. * @return void
  73. */
  74. public function testRewriteAfterStoreSwitcher()
  75. {
  76. $initialRequestPath = 'request-path';
  77. $newRequestPath = 'new-request-path';
  78. $oldStoreAlias = 'old-store';
  79. $oldStoreId = 'old-store-id';
  80. $currentStoreId = 'current-store-id';
  81. $rewriteEntityType = 'entity-type';
  82. $rewriteEntityId = 42;
  83. $this->request
  84. ->expects($this->any())
  85. ->method('getParam')
  86. ->with('___from_store')
  87. ->willReturn($oldStoreAlias);
  88. $this->request
  89. ->expects($this->any())
  90. ->method('getPathInfo')
  91. ->willReturn($initialRequestPath);
  92. $oldStore = $this->getMockBuilder(Store::class)
  93. ->disableOriginalConstructor()
  94. ->getMock();
  95. $oldStore->expects($this->any())
  96. ->method('getId')
  97. ->willReturn($oldStoreId);
  98. $this->store
  99. ->expects($this->any())
  100. ->method('getId')
  101. ->willReturn($currentStoreId);
  102. $this->storeManager
  103. ->expects($this->any())
  104. ->method('getStore')
  105. ->willReturnMap([[$oldStoreAlias, $oldStore], [null, $this->store]]);
  106. $oldUrlRewrite = $this->getMockBuilder(UrlRewrite::class)
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. $oldUrlRewrite->expects($this->any())
  110. ->method('getEntityType')
  111. ->willReturn($rewriteEntityType);
  112. $oldUrlRewrite->expects($this->any())
  113. ->method('getEntityId')
  114. ->willReturn($rewriteEntityId);
  115. $oldUrlRewrite->expects($this->any())
  116. ->method('getRedirectType')
  117. ->willReturn(0);
  118. $urlRewrite = $this->getMockBuilder(UrlRewrite::class)
  119. ->disableOriginalConstructor()
  120. ->getMock();
  121. $urlRewrite->expects($this->any())
  122. ->method('getRequestPath')
  123. ->willReturn($newRequestPath);
  124. $this->urlFinder
  125. ->expects($this->any())
  126. ->method('findOneByData')
  127. ->willReturnMap([
  128. [
  129. [
  130. UrlRewrite::REQUEST_PATH => $initialRequestPath,
  131. UrlRewrite::STORE_ID => $currentStoreId,
  132. ],
  133. $urlRewrite,
  134. ]
  135. ]);
  136. $this->actionFactory
  137. ->expects($this->once())
  138. ->method('create')
  139. ->with(Forward::class);
  140. $this->router->match($this->request);
  141. }
  142. /**
  143. * @return void
  144. */
  145. public function testNoRewriteAfterStoreSwitcherWhenNoOldRewrite()
  146. {
  147. $this->request->expects($this->any())->method('getPathInfo')->will($this->returnValue('request-path'));
  148. $this->request->expects($this->any())->method('getParam')->with('___from_store')
  149. ->will($this->returnValue('old-store'));
  150. $oldStore = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
  151. $this->storeManager->expects($this->any())->method('getStore')
  152. ->will($this->returnValueMap([['old-store', $oldStore], [null, $this->store]]));
  153. $oldStore->expects($this->any())->method('getId')->will($this->returnValue('old-store-id'));
  154. $this->store->expects($this->any())->method('getId')->will($this->returnValue('current-store-id'));
  155. $oldUrlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
  156. ->disableOriginalConstructor()->getMock();
  157. $oldUrlRewrite->expects($this->any())->method('getEntityType')->will($this->returnValue('entity-type'));
  158. $oldUrlRewrite->expects($this->any())->method('getEntityId')->will($this->returnValue('entity-id'));
  159. $oldUrlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue('request-path'));
  160. $urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
  161. ->disableOriginalConstructor()->getMock();
  162. $urlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue('request-path'));
  163. $this->assertNull($this->router->match($this->request));
  164. }
  165. /**
  166. * @return void
  167. */
  168. public function testNoRewriteAfterStoreSwitcherWhenOldRewriteEqualsToNewOne()
  169. {
  170. $this->request->expects($this->any())->method('getPathInfo')->will($this->returnValue('request-path'));
  171. $this->request->expects($this->any())->method('getParam')->with('___from_store')
  172. ->will($this->returnValue('old-store'));
  173. $oldStore = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
  174. $this->storeManager->expects($this->any())->method('getStore')
  175. ->will($this->returnValueMap([['old-store', $oldStore], [null, $this->store]]));
  176. $oldStore->expects($this->any())->method('getId')->will($this->returnValue('old-store-id'));
  177. $this->store->expects($this->any())->method('getId')->will($this->returnValue('current-store-id'));
  178. $oldUrlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
  179. ->disableOriginalConstructor()->getMock();
  180. $oldUrlRewrite->expects($this->any())->method('getEntityType')->will($this->returnValue('entity-type'));
  181. $oldUrlRewrite->expects($this->any())->method('getEntityId')->will($this->returnValue('entity-id'));
  182. $oldUrlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue('old-request-path'));
  183. $urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
  184. ->disableOriginalConstructor()->getMock();
  185. $urlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue('old-request-path'));
  186. $this->urlFinder->expects($this->any())->method('findOneByData')->will(
  187. $this->returnValueMap([
  188. [
  189. [UrlRewrite::REQUEST_PATH => 'request-path', UrlRewrite::STORE_ID => 'old-store-id'],
  190. $oldUrlRewrite,
  191. ],
  192. [
  193. [
  194. UrlRewrite::ENTITY_TYPE => 'entity-type',
  195. UrlRewrite::ENTITY_ID => 'entity-id',
  196. UrlRewrite::STORE_ID => 'current-store-id',
  197. UrlRewrite::IS_AUTOGENERATED => 1,
  198. ],
  199. $urlRewrite
  200. ],
  201. ])
  202. );
  203. $this->assertNull($this->router->match($this->request));
  204. }
  205. /**
  206. * @return void
  207. */
  208. public function testMatchWithRedirect()
  209. {
  210. $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->store));
  211. $urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
  212. ->disableOriginalConstructor()->getMock();
  213. $urlRewrite->expects($this->any())->method('getRedirectType')->will($this->returnValue('redirect-code'));
  214. $urlRewrite->expects($this->any())->method('getTargetPath')->will($this->returnValue('target-path'));
  215. $this->urlFinder->expects($this->any())->method('findOneByData')->will($this->returnValue($urlRewrite));
  216. $this->response->expects($this->once())->method('setRedirect')
  217. ->with('new-target-path', 'redirect-code');
  218. $this->url->expects($this->once())->method('getUrl')->with('', ['_direct' => 'target-path'])
  219. ->will($this->returnValue('new-target-path'));
  220. $this->request->expects($this->once())->method('setDispatched')->with(true);
  221. $this->actionFactory->expects($this->once())->method('create')
  222. ->with(\Magento\Framework\App\Action\Redirect::class);
  223. $this->router->match($this->request);
  224. }
  225. /**
  226. * @return void
  227. */
  228. public function testMatchWithCustomInternalRedirect()
  229. {
  230. $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->store));
  231. $urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
  232. ->disableOriginalConstructor()->getMock();
  233. $urlRewrite->expects($this->any())->method('getEntityType')->will($this->returnValue('custom'));
  234. $urlRewrite->expects($this->any())->method('getRedirectType')->will($this->returnValue('redirect-code'));
  235. $urlRewrite->expects($this->any())->method('getTargetPath')->will($this->returnValue('target-path'));
  236. $this->urlFinder->expects($this->any())->method('findOneByData')->will($this->returnValue($urlRewrite));
  237. $this->response->expects($this->once())->method('setRedirect')->with('a', 'redirect-code');
  238. $this->url->expects($this->once())->method('getUrl')->with('', ['_direct' => 'target-path'])->willReturn('a');
  239. $this->request->expects($this->once())->method('setDispatched')->with(true);
  240. $this->actionFactory->expects($this->once())->method('create')
  241. ->with(\Magento\Framework\App\Action\Redirect::class);
  242. $this->router->match($this->request);
  243. }
  244. /**
  245. * @param string $targetPath
  246. * @dataProvider externalRedirectTargetPathDataProvider
  247. */
  248. public function testMatchWithCustomExternalRedirect($targetPath)
  249. {
  250. $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->store));
  251. $urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
  252. ->disableOriginalConstructor()->getMock();
  253. $urlRewrite->expects($this->any())->method('getEntityType')->will($this->returnValue('custom'));
  254. $urlRewrite->expects($this->any())->method('getRedirectType')->will($this->returnValue('redirect-code'));
  255. $urlRewrite->expects($this->any())->method('getTargetPath')->will($this->returnValue($targetPath));
  256. $this->urlFinder->expects($this->any())->method('findOneByData')->will($this->returnValue($urlRewrite));
  257. $this->response->expects($this->once())->method('setRedirect')->with($targetPath, 'redirect-code');
  258. $this->url->expects($this->never())->method('getUrl');
  259. $this->request->expects($this->once())->method('setDispatched')->with(true);
  260. $this->actionFactory->expects($this->once())->method('create')
  261. ->with(\Magento\Framework\App\Action\Redirect::class);
  262. $this->router->match($this->request);
  263. }
  264. /**
  265. * @return array
  266. */
  267. public function externalRedirectTargetPathDataProvider()
  268. {
  269. return [
  270. ['http://example.com'],
  271. ['https://example.com'],
  272. ];
  273. }
  274. /**
  275. * @return void
  276. */
  277. public function testMatch()
  278. {
  279. $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->store));
  280. $urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
  281. ->disableOriginalConstructor()->getMock();
  282. $urlRewrite->expects($this->any())->method('getRedirectType')->will($this->returnValue(0));
  283. $urlRewrite->expects($this->any())->method('getTargetPath')->will($this->returnValue('target-path'));
  284. $urlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue('request-path'));
  285. $this->urlFinder->expects($this->any())->method('findOneByData')->will($this->returnValue($urlRewrite));
  286. $this->request->expects($this->once())->method('setPathInfo')->with('/target-path');
  287. $this->request->expects($this->once())->method('setAlias')
  288. ->with(\Magento\Framework\UrlInterface::REWRITE_REQUEST_PATH_ALIAS, 'request-path');
  289. $this->actionFactory->expects($this->once())->method('create')
  290. ->with(\Magento\Framework\App\Action\Forward::class);
  291. $this->router->match($this->request);
  292. }
  293. }