3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
-
7
6
namespace Magento \UrlRewrite \Test \Unit \Controller ;
8
7
9
8
use Magento \Framework \App \Action \Forward ;
10
9
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
10
+ use Magento \Framework \UrlInterface ;
11
11
use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
12
12
use Magento \Store \Model \Store ;
13
+ use PHPUnit \Framework \MockObject \MockObject ;
14
+ use Zend \Stdlib \ParametersInterface ;
13
15
14
16
/**
17
+ * Test class for UrlRewrite Controller Router
18
+ *
15
19
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
16
20
*/
17
21
class RouterTest extends \PHPUnit \Framework \TestCase
18
22
{
19
- /** @var \Magento\UrlRewrite\Controller\Router */
20
- protected $ router ;
23
+ /**
24
+ * @var \Magento\UrlRewrite\Controller\Router
25
+ */
26
+ private $ router ;
27
+
28
+ /**
29
+ * @var \Magento\Framework\App\ActionFactory|MockObject
30
+ */
31
+ private $ actionFactory ;
21
32
22
- /** @var \Magento\Framework\App\ActionFactory|\PHPUnit_Framework_MockObject_MockObject */
23
- protected $ actionFactory ;
33
+ /**
34
+ * @var UrlInterface|MockObject
35
+ */
36
+ private $ url ;
24
37
25
- /** @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
26
- protected $ url ;
38
+ /**
39
+ * @var \Magento\Store\Model\StoreManagerInterface|MockObject
40
+ */
41
+ private $ storeManager ;
27
42
28
- /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
29
- protected $ storeManager ;
43
+ /**
44
+ * @var Store|MockObject
45
+ */
46
+ private $ store ;
30
47
31
- /** @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject */
32
- protected $ store ;
48
+ /**
49
+ * @var \Magento\Framework\App\ResponseInterface|MockObject
50
+ */
51
+ private $ response ;
33
52
34
- /** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
35
- protected $ response ;
53
+ /**
54
+ * @var \Magento\Framework\App\RequestInterface|MockObject
55
+ */
56
+ private $ request ;
36
57
37
- /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
38
- protected $ request ;
58
+ /**
59
+ * @var ParametersInterface|MockObject
60
+ */
61
+ private $ requestQuery ;
39
62
40
- /** @var \Magento\UrlRewrite\Model\UrlFinderInterface|\PHPUnit_Framework_MockObject_MockObject */
41
- protected $ urlFinder ;
63
+ /**
64
+ * @var \Magento\UrlRewrite\Model\UrlFinderInterface|MockObject
65
+ */
66
+ private $ urlFinder ;
42
67
43
68
/**
44
- * @return void
69
+ * @inheritDoc
45
70
*/
46
71
protected function setUp ()
47
72
{
48
73
$ objectManager = new ObjectManager ($ this );
49
74
$ this ->actionFactory = $ this ->createMock (\Magento \Framework \App \ActionFactory::class);
50
- $ this ->url = $ this ->createMock (\ Magento \ Framework \ UrlInterface::class);
75
+ $ this ->url = $ this ->createMock (UrlInterface::class);
51
76
$ this ->storeManager = $ this ->createMock (\Magento \Store \Model \StoreManagerInterface::class);
52
77
$ this ->response = $ this ->createPartialMock (
53
78
\Magento \Framework \App \ResponseInterface::class,
54
79
['setRedirect ' , 'sendResponse ' ]
55
80
);
81
+ $ this ->requestQuery = $ this ->createMock (ParametersInterface::class);
56
82
$ this ->request = $ this ->getMockBuilder (\Magento \Framework \App \Request \Http::class)
57
83
->disableOriginalConstructor ()->getMock ();
84
+ $ this ->request ->method ('getQuery ' )->willReturn ($ this ->requestQuery );
58
85
$ this ->urlFinder = $ this ->createMock (\Magento \UrlRewrite \Model \UrlFinderInterface::class);
59
86
$ this ->store = $ this ->getMockBuilder (
60
- \ Magento \ Store \ Model \ Store::class
87
+ Store::class
61
88
)->disableOriginalConstructor ()->getMock ();
62
89
63
90
$ this ->router = $ objectManager ->getObject (
@@ -166,17 +193,17 @@ public function testNoRewriteAfterStoreSwitcherWhenNoOldRewrite()
166
193
$ this ->request ->expects ($ this ->any ())->method ('getPathInfo ' )->will ($ this ->returnValue ('request-path ' ));
167
194
$ this ->request ->expects ($ this ->any ())->method ('getParam ' )->with ('___from_store ' )
168
195
->will ($ this ->returnValue ('old-store ' ));
169
- $ oldStore = $ this ->getMockBuilder (\ Magento \ Store \ Model \ Store::class)->disableOriginalConstructor ()->getMock ();
196
+ $ oldStore = $ this ->getMockBuilder (Store::class)->disableOriginalConstructor ()->getMock ();
170
197
$ this ->storeManager ->expects ($ this ->any ())->method ('getStore ' )
171
198
->will ($ this ->returnValueMap ([['old-store ' , $ oldStore ], [null , $ this ->store ]]));
172
199
$ oldStore ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue ('old-store-id ' ));
173
200
$ this ->store ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue ('current-store-id ' ));
174
- $ oldUrlRewrite = $ this ->getMockBuilder (\ Magento \ UrlRewrite \ Service \ V1 \ Data \ UrlRewrite::class)
201
+ $ oldUrlRewrite = $ this ->getMockBuilder (UrlRewrite::class)
175
202
->disableOriginalConstructor ()->getMock ();
176
203
$ oldUrlRewrite ->expects ($ this ->any ())->method ('getEntityType ' )->will ($ this ->returnValue ('entity-type ' ));
177
204
$ oldUrlRewrite ->expects ($ this ->any ())->method ('getEntityId ' )->will ($ this ->returnValue ('entity-id ' ));
178
205
$ oldUrlRewrite ->expects ($ this ->any ())->method ('getRequestPath ' )->will ($ this ->returnValue ('request-path ' ));
179
- $ urlRewrite = $ this ->getMockBuilder (\ Magento \ UrlRewrite \ Service \ V1 \ Data \ UrlRewrite::class)
206
+ $ urlRewrite = $ this ->getMockBuilder (UrlRewrite::class)
180
207
->disableOriginalConstructor ()->getMock ();
181
208
$ urlRewrite ->expects ($ this ->any ())->method ('getRequestPath ' )->will ($ this ->returnValue ('request-path ' ));
182
209
@@ -191,17 +218,17 @@ public function testNoRewriteAfterStoreSwitcherWhenOldRewriteEqualsToNewOne()
191
218
$ this ->request ->expects ($ this ->any ())->method ('getPathInfo ' )->will ($ this ->returnValue ('request-path ' ));
192
219
$ this ->request ->expects ($ this ->any ())->method ('getParam ' )->with ('___from_store ' )
193
220
->will ($ this ->returnValue ('old-store ' ));
194
- $ oldStore = $ this ->getMockBuilder (\ Magento \ Store \ Model \ Store::class)->disableOriginalConstructor ()->getMock ();
221
+ $ oldStore = $ this ->getMockBuilder (Store::class)->disableOriginalConstructor ()->getMock ();
195
222
$ this ->storeManager ->expects ($ this ->any ())->method ('getStore ' )
196
223
->will ($ this ->returnValueMap ([['old-store ' , $ oldStore ], [null , $ this ->store ]]));
197
224
$ oldStore ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue ('old-store-id ' ));
198
225
$ this ->store ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue ('current-store-id ' ));
199
- $ oldUrlRewrite = $ this ->getMockBuilder (\ Magento \ UrlRewrite \ Service \ V1 \ Data \ UrlRewrite::class)
226
+ $ oldUrlRewrite = $ this ->getMockBuilder (UrlRewrite::class)
200
227
->disableOriginalConstructor ()->getMock ();
201
228
$ oldUrlRewrite ->expects ($ this ->any ())->method ('getEntityType ' )->will ($ this ->returnValue ('entity-type ' ));
202
229
$ oldUrlRewrite ->expects ($ this ->any ())->method ('getEntityId ' )->will ($ this ->returnValue ('entity-id ' ));
203
230
$ oldUrlRewrite ->expects ($ this ->any ())->method ('getRequestPath ' )->will ($ this ->returnValue ('old-request-path ' ));
204
- $ urlRewrite = $ this ->getMockBuilder (\ Magento \ UrlRewrite \ Service \ V1 \ Data \ UrlRewrite::class)
231
+ $ urlRewrite = $ this ->getMockBuilder (UrlRewrite::class)
205
232
->disableOriginalConstructor ()->getMock ();
206
233
$ urlRewrite ->expects ($ this ->any ())->method ('getRequestPath ' )->will ($ this ->returnValue ('old-request-path ' ));
207
234
@@ -234,7 +261,7 @@ public function testNoRewriteAfterStoreSwitcherWhenOldRewriteEqualsToNewOne()
234
261
public function testMatchWithRedirect ()
235
262
{
236
263
$ this ->storeManager ->expects ($ this ->any ())->method ('getStore ' )->will ($ this ->returnValue ($ this ->store ));
237
- $ urlRewrite = $ this ->getMockBuilder (\ Magento \ UrlRewrite \ Service \ V1 \ Data \ UrlRewrite::class)
264
+ $ urlRewrite = $ this ->getMockBuilder (UrlRewrite::class)
238
265
->disableOriginalConstructor ()->getMock ();
239
266
$ urlRewrite ->expects ($ this ->any ())->method ('getRedirectType ' )->will ($ this ->returnValue ('redirect-code ' ));
240
267
$ urlRewrite ->expects ($ this ->any ())->method ('getTargetPath ' )->will ($ this ->returnValue ('target-path ' ));
@@ -256,7 +283,7 @@ public function testMatchWithRedirect()
256
283
public function testMatchWithCustomInternalRedirect ()
257
284
{
258
285
$ this ->storeManager ->expects ($ this ->any ())->method ('getStore ' )->will ($ this ->returnValue ($ this ->store ));
259
- $ urlRewrite = $ this ->getMockBuilder (\ Magento \ UrlRewrite \ Service \ V1 \ Data \ UrlRewrite::class)
286
+ $ urlRewrite = $ this ->getMockBuilder (UrlRewrite::class)
260
287
->disableOriginalConstructor ()->getMock ();
261
288
$ urlRewrite ->expects ($ this ->any ())->method ('getEntityType ' )->will ($ this ->returnValue ('custom ' ));
262
289
$ urlRewrite ->expects ($ this ->any ())->method ('getRedirectType ' )->will ($ this ->returnValue ('redirect-code ' ));
@@ -278,7 +305,7 @@ public function testMatchWithCustomInternalRedirect()
278
305
public function testMatchWithCustomExternalRedirect ($ targetPath )
279
306
{
280
307
$ this ->storeManager ->expects ($ this ->any ())->method ('getStore ' )->will ($ this ->returnValue ($ this ->store ));
281
- $ urlRewrite = $ this ->getMockBuilder (\ Magento \ UrlRewrite \ Service \ V1 \ Data \ UrlRewrite::class)
308
+ $ urlRewrite = $ this ->getMockBuilder (UrlRewrite::class)
282
309
->disableOriginalConstructor ()->getMock ();
283
310
$ urlRewrite ->expects ($ this ->any ())->method ('getEntityType ' )->will ($ this ->returnValue ('custom ' ));
284
311
$ urlRewrite ->expects ($ this ->any ())->method ('getRedirectType ' )->will ($ this ->returnValue ('redirect-code ' ));
@@ -310,18 +337,68 @@ public function externalRedirectTargetPathDataProvider()
310
337
public function testMatch ()
311
338
{
312
339
$ this ->storeManager ->expects ($ this ->any ())->method ('getStore ' )->will ($ this ->returnValue ($ this ->store ));
313
- $ urlRewrite = $ this ->getMockBuilder (\ Magento \ UrlRewrite \ Service \ V1 \ Data \ UrlRewrite::class)
340
+ $ urlRewrite = $ this ->getMockBuilder (UrlRewrite::class)
314
341
->disableOriginalConstructor ()->getMock ();
315
342
$ urlRewrite ->expects ($ this ->any ())->method ('getRedirectType ' )->will ($ this ->returnValue (0 ));
316
343
$ urlRewrite ->expects ($ this ->any ())->method ('getTargetPath ' )->will ($ this ->returnValue ('target-path ' ));
317
344
$ urlRewrite ->expects ($ this ->any ())->method ('getRequestPath ' )->will ($ this ->returnValue ('request-path ' ));
318
345
$ this ->urlFinder ->expects ($ this ->any ())->method ('findOneByData ' )->will ($ this ->returnValue ($ urlRewrite ));
319
346
$ this ->request ->expects ($ this ->once ())->method ('setPathInfo ' )->with ('/target-path ' );
320
347
$ this ->request ->expects ($ this ->once ())->method ('setAlias ' )
321
- ->with (\ Magento \ Framework \ UrlInterface::REWRITE_REQUEST_PATH_ALIAS , 'request-path ' );
348
+ ->with (UrlInterface::REWRITE_REQUEST_PATH_ALIAS , 'request-path ' );
322
349
$ this ->actionFactory ->expects ($ this ->once ())->method ('create ' )
323
350
->with (\Magento \Framework \App \Action \Forward::class);
324
351
325
352
$ this ->router ->match ($ this ->request );
326
353
}
354
+
355
+ /**
356
+ * Test to match corresponding URL Rewrite on request with query params
357
+ *
358
+ * @param string $originalRequestPath
359
+ * @param string $requestPath
360
+ * @param int $countOfQueryParams
361
+ * @dataProvider matchWithQueryParamsDataProvider
362
+ */
363
+ public function testMatchWithQueryParams (string $ originalRequestPath , string $ requestPath , int $ countOfQueryParams )
364
+ {
365
+ $ targetPath = 'target-path ' ;
366
+
367
+ $ this ->storeManager ->method ('getStore ' )->willReturn ($ this ->store );
368
+ $ urlRewrite = $ this ->createMock (UrlRewrite::class);
369
+ $ urlRewrite ->method ('getRedirectType ' )->willReturn (0 );
370
+ $ urlRewrite ->method ('getTargetPath ' )->willReturn ($ targetPath );
371
+ $ urlRewrite ->method ('getRequestPath ' )->willReturn ($ requestPath );
372
+ $ this ->urlFinder ->method ('findOneByData ' )
373
+ ->with ([UrlRewrite::REQUEST_PATH => $ requestPath , UrlRewrite::STORE_ID => $ this ->store ->getId ()])
374
+ ->willReturn ($ urlRewrite );
375
+
376
+ $ this ->requestQuery ->method ('count ' )->willReturn ($ countOfQueryParams );
377
+ $ this ->request ->method ('getPathInfo ' )
378
+ ->willReturn ($ originalRequestPath );
379
+ $ this ->request ->expects ($ this ->once ())
380
+ ->method ('setPathInfo ' )
381
+ ->with ('/ ' . $ targetPath );
382
+ $ this ->request ->expects ($ this ->once ())
383
+ ->method ('setAlias ' )
384
+ ->with (UrlInterface::REWRITE_REQUEST_PATH_ALIAS , $ requestPath );
385
+ $ this ->actionFactory ->expects ($ this ->once ())
386
+ ->method ('create ' )
387
+ ->with (Forward::class);
388
+
389
+ $ this ->router ->match ($ this ->request );
390
+ }
391
+
392
+ /**
393
+ * Data provider for Test to match corresponding URL Rewrite on request with query params
394
+ *
395
+ * @return array
396
+ */
397
+ public function matchWithQueryParamsDataProvider (): array
398
+ {
399
+ return [
400
+ ['/category.html/ ' , 'category.html/ ' , 0 ],
401
+ ['/category.html/ ' , 'category.html ' , 1 ],
402
+ ];
403
+ }
327
404
}
0 commit comments