Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RouterTest.php
Go to the documentation of this file.
1 <?php
8 
13 
17 class RouterTest extends \PHPUnit\Framework\TestCase
18 {
20  protected $router;
21 
23  protected $actionFactory;
24 
26  protected $url;
27 
29  protected $storeManager;
30 
32  protected $store;
33 
35  protected $response;
36 
38  protected $request;
39 
41  protected $urlFinder;
42 
46  protected function setUp()
47  {
48  $this->actionFactory = $this->createMock(\Magento\Framework\App\ActionFactory::class);
49  $this->url = $this->createMock(\Magento\Framework\UrlInterface::class);
50  $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
51  $this->response = $this->createPartialMock(
52  \Magento\Framework\App\ResponseInterface::class,
53  ['setRedirect', 'sendResponse']
54  );
55  $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
56  ->disableOriginalConstructor()->getMock();
57  $this->urlFinder = $this->createMock(\Magento\UrlRewrite\Model\UrlFinderInterface::class);
58  $this->store = $this->getMockBuilder(
59  \Magento\Store\Model\Store::class
60  )->disableOriginalConstructor()->getMock();
61 
62  $this->router = (new ObjectManager($this))->getObject(
63  \Magento\UrlRewrite\Controller\Router::class,
64  [
65  'actionFactory' => $this->actionFactory,
66  'url' => $this->url,
67  'storeManager' => $this->storeManager,
68  'response' => $this->response,
69  'urlFinder' => $this->urlFinder
70  ]
71  );
72  }
73 
77  public function testNoRewriteExist()
78  {
79  $this->urlFinder->expects($this->any())->method('findOneByData')->will($this->returnValue(null));
80  $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->store));
81  $this->store->expects($this->any())->method('getId')->will($this->returnValue('current-store-id'));
82 
83  $this->assertNull($this->router->match($this->request));
84  }
85 
90  {
91  $initialRequestPath = 'request-path';
92  $newRequestPath = 'new-request-path';
93  $oldStoreAlias = 'old-store';
94  $oldStoreId = 'old-store-id';
95  $currentStoreId = 'current-store-id';
96  $rewriteEntityType = 'entity-type';
97  $rewriteEntityId = 42;
98  $this->request
99  ->expects($this->any())
100  ->method('getParam')
101  ->with('___from_store')
102  ->willReturn($oldStoreAlias);
103  $this->request
104  ->expects($this->any())
105  ->method('getPathInfo')
106  ->willReturn($initialRequestPath);
107  $oldStore = $this->getMockBuilder(Store::class)
108  ->disableOriginalConstructor()
109  ->getMock();
110  $oldStore->expects($this->any())
111  ->method('getId')
112  ->willReturn($oldStoreId);
113  $this->store
114  ->expects($this->any())
115  ->method('getId')
116  ->willReturn($currentStoreId);
117  $this->storeManager
118  ->expects($this->any())
119  ->method('getStore')
120  ->willReturnMap([[$oldStoreAlias, $oldStore], [null, $this->store]]);
121  $oldUrlRewrite = $this->getMockBuilder(UrlRewrite::class)
122  ->disableOriginalConstructor()
123  ->getMock();
124  $oldUrlRewrite->expects($this->any())
125  ->method('getEntityType')
126  ->willReturn($rewriteEntityType);
127  $oldUrlRewrite->expects($this->any())
128  ->method('getEntityId')
129  ->willReturn($rewriteEntityId);
130  $oldUrlRewrite->expects($this->any())
131  ->method('getRedirectType')
132  ->willReturn(0);
133  $urlRewrite = $this->getMockBuilder(UrlRewrite::class)
134  ->disableOriginalConstructor()
135  ->getMock();
136  $urlRewrite->expects($this->any())
137  ->method('getRequestPath')
138  ->willReturn($newRequestPath);
139  $this->urlFinder
140  ->expects($this->any())
141  ->method('findOneByData')
142  ->willReturnMap([
143  [
144  [
145  UrlRewrite::REQUEST_PATH => $initialRequestPath,
147  ],
148  $urlRewrite,
149  ]
150  ]);
151  $this->actionFactory
152  ->expects($this->once())
153  ->method('create')
154  ->with(Forward::class);
155  $this->router->match($this->request);
156  }
157 
162  {
163  $this->request->expects($this->any())->method('getPathInfo')->will($this->returnValue('request-path'));
164  $this->request->expects($this->any())->method('getParam')->with('___from_store')
165  ->will($this->returnValue('old-store'));
166  $oldStore = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
167  $this->storeManager->expects($this->any())->method('getStore')
168  ->will($this->returnValueMap([['old-store', $oldStore], [null, $this->store]]));
169  $oldStore->expects($this->any())->method('getId')->will($this->returnValue('old-store-id'));
170  $this->store->expects($this->any())->method('getId')->will($this->returnValue('current-store-id'));
171  $oldUrlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
172  ->disableOriginalConstructor()->getMock();
173  $oldUrlRewrite->expects($this->any())->method('getEntityType')->will($this->returnValue('entity-type'));
174  $oldUrlRewrite->expects($this->any())->method('getEntityId')->will($this->returnValue('entity-id'));
175  $oldUrlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue('request-path'));
176  $urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
177  ->disableOriginalConstructor()->getMock();
178  $urlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue('request-path'));
179 
180  $this->assertNull($this->router->match($this->request));
181  }
182 
187  {
188  $this->request->expects($this->any())->method('getPathInfo')->will($this->returnValue('request-path'));
189  $this->request->expects($this->any())->method('getParam')->with('___from_store')
190  ->will($this->returnValue('old-store'));
191  $oldStore = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
192  $this->storeManager->expects($this->any())->method('getStore')
193  ->will($this->returnValueMap([['old-store', $oldStore], [null, $this->store]]));
194  $oldStore->expects($this->any())->method('getId')->will($this->returnValue('old-store-id'));
195  $this->store->expects($this->any())->method('getId')->will($this->returnValue('current-store-id'));
196  $oldUrlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
197  ->disableOriginalConstructor()->getMock();
198  $oldUrlRewrite->expects($this->any())->method('getEntityType')->will($this->returnValue('entity-type'));
199  $oldUrlRewrite->expects($this->any())->method('getEntityId')->will($this->returnValue('entity-id'));
200  $oldUrlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue('old-request-path'));
201  $urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
202  ->disableOriginalConstructor()->getMock();
203  $urlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue('old-request-path'));
204 
205  $this->urlFinder->expects($this->any())->method('findOneByData')->will(
206  $this->returnValueMap([
207  [
208  [UrlRewrite::REQUEST_PATH => 'request-path', UrlRewrite::STORE_ID => 'old-store-id'],
209  $oldUrlRewrite,
210  ],
211  [
212  [
213  UrlRewrite::ENTITY_TYPE => 'entity-type',
214  UrlRewrite::ENTITY_ID => 'entity-id',
215  UrlRewrite::STORE_ID => 'current-store-id',
217  ],
218  $urlRewrite
219  ],
220  ])
221  );
222 
223  $this->assertNull($this->router->match($this->request));
224  }
225 
229  public function testMatchWithRedirect()
230  {
231  $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->store));
232  $urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
233  ->disableOriginalConstructor()->getMock();
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')
238  ->with('new-target-path', 'redirect-code');
239  $this->url->expects($this->once())->method('getUrl')->with('', ['_direct' => 'target-path'])
240  ->will($this->returnValue('new-target-path'));
241  $this->request->expects($this->once())->method('setDispatched')->with(true);
242  $this->actionFactory->expects($this->once())->method('create')
243  ->with(\Magento\Framework\App\Action\Redirect::class);
244 
245  $this->router->match($this->request);
246  }
247 
252  {
253  $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->store));
254  $urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
255  ->disableOriginalConstructor()->getMock();
256  $urlRewrite->expects($this->any())->method('getEntityType')->will($this->returnValue('custom'));
257  $urlRewrite->expects($this->any())->method('getRedirectType')->will($this->returnValue('redirect-code'));
258  $urlRewrite->expects($this->any())->method('getTargetPath')->will($this->returnValue('target-path'));
259  $this->urlFinder->expects($this->any())->method('findOneByData')->will($this->returnValue($urlRewrite));
260  $this->response->expects($this->once())->method('setRedirect')->with('a', 'redirect-code');
261  $this->url->expects($this->once())->method('getUrl')->with('', ['_direct' => 'target-path'])->willReturn('a');
262  $this->request->expects($this->once())->method('setDispatched')->with(true);
263  $this->actionFactory->expects($this->once())->method('create')
264  ->with(\Magento\Framework\App\Action\Redirect::class);
265 
266  $this->router->match($this->request);
267  }
268 
273  public function testMatchWithCustomExternalRedirect($targetPath)
274  {
275  $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->store));
276  $urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
277  ->disableOriginalConstructor()->getMock();
278  $urlRewrite->expects($this->any())->method('getEntityType')->will($this->returnValue('custom'));
279  $urlRewrite->expects($this->any())->method('getRedirectType')->will($this->returnValue('redirect-code'));
280  $urlRewrite->expects($this->any())->method('getTargetPath')->will($this->returnValue($targetPath));
281  $this->urlFinder->expects($this->any())->method('findOneByData')->will($this->returnValue($urlRewrite));
282  $this->response->expects($this->once())->method('setRedirect')->with($targetPath, 'redirect-code');
283  $this->url->expects($this->never())->method('getUrl');
284  $this->request->expects($this->once())->method('setDispatched')->with(true);
285  $this->actionFactory->expects($this->once())->method('create')
286  ->with(\Magento\Framework\App\Action\Redirect::class);
287 
288  $this->router->match($this->request);
289  }
290 
295  {
296  return [
297  ['http://example.com'],
298  ['https://example.com'],
299  ];
300  }
301 
305  public function testMatch()
306  {
307  $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->store));
308  $urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
309  ->disableOriginalConstructor()->getMock();
310  $urlRewrite->expects($this->any())->method('getRedirectType')->will($this->returnValue(0));
311  $urlRewrite->expects($this->any())->method('getTargetPath')->will($this->returnValue('target-path'));
312  $urlRewrite->expects($this->any())->method('getRequestPath')->will($this->returnValue('request-path'));
313  $this->urlFinder->expects($this->any())->method('findOneByData')->will($this->returnValue($urlRewrite));
314  $this->request->expects($this->once())->method('setPathInfo')->with('/target-path');
315  $this->request->expects($this->once())->method('setAlias')
316  ->with(\Magento\Framework\UrlInterface::REWRITE_REQUEST_PATH_ALIAS, 'request-path');
317  $this->actionFactory->expects($this->once())->method('create')
318  ->with(\Magento\Framework\App\Action\Forward::class);
319 
320  $this->router->match($this->request);
321  }
322 }