Completed
Push — EZP-29891 ( 916cf6...0402ff )
by
unknown
16:53
created

testMatchRequestLocationWrongCaseUriPrefixExcluded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42

Duplication

Lines 42
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 42
loc 42
rs 9.248
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the UrlAliasRouterTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\MVC\Symfony\Routing\Tests;
10
11
use eZ\Publish\API\Repository\ContentService;
12
use eZ\Publish\API\Repository\LocationService;
13
use eZ\Publish\API\Repository\URLAliasService;
14
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
15
use eZ\Publish\API\Repository\Values\Content\URLAlias;
16
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
17
use eZ\Publish\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator;
18
use eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter;
19
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
20
use eZ\Publish\Core\MVC\Symfony\View\Manager as ViewManager;
21
use eZ\Publish\Core\Repository\Values\Content\Location;
22
use PHPUnit\Framework\TestCase;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
25
use Symfony\Component\Routing\RequestContext;
26
27
class UrlAliasRouterTest extends TestCase
28
{
29
    /**
30
     * @var \PHPUnit_Framework_MockObject_MockObject
31
     */
32
    protected $repository;
33
34
    /**
35
     * @var \PHPUnit_Framework_MockObject_MockObject
36
     */
37
    protected $urlAliasService;
38
39
    /**
40
     * @var \PHPUnit_Framework_MockObject_MockObject
41
     */
42
    protected $locationService;
43
44
    /**
45
     * @var \PHPUnit_Framework_MockObject_MockObject
46
     */
47
    protected $contentService;
48
49
    /**
50
     * @var \PHPUnit_Framework_MockObject_MockObject
51
     */
52
    protected $urlALiasGenerator;
53
54
    protected $requestContext;
55
56
    /**
57
     * @var UrlAliasRouter
58
     */
59
    protected $router;
60
61
    protected function setUp()
62
    {
63
        parent::setUp();
64
        $repositoryClass = 'eZ\\Publish\\Core\\Repository\\Repository';
65
        $this->repository = $repository = $this
66
            ->getMockBuilder($repositoryClass)
67
            ->disableOriginalConstructor()
68
            ->setMethods(
69
                array_diff(
70
                    get_class_methods($repositoryClass),
71
                    array('sudo')
72
                )
73
            )
74
            ->getMock();
75
        $this->urlAliasService = $this->getMock('eZ\\Publish\\API\\Repository\\URLAliasService');
76
        $this->locationService = $this->getMock('eZ\\Publish\\API\\Repository\\LocationService');
77
        $this->contentService = $this->getMock('eZ\\Publish\\API\\Repository\\ContentService');
78
        $this->urlALiasGenerator = $this
79
            ->getMockBuilder('eZ\\Publish\\Core\\MVC\\Symfony\\Routing\\Generator\\UrlAliasGenerator')
80
            ->setConstructorArgs(
81
                array(
82
                    $repository,
83
                    $this->getMock('Symfony\\Component\\Routing\\RouterInterface'),
84
                    $this->getMock('eZ\Publish\Core\MVC\ConfigResolverInterface'),
85
                )
86
            )
87
            ->getMock();
88
        $this->requestContext = new RequestContext();
89
90
        $this->router = $this->getRouter($this->locationService, $this->urlAliasService, $this->contentService, $this->urlALiasGenerator, $this->requestContext);
91
    }
92
93
    /**
94
     * @param \eZ\Publish\API\Repository\LocationService $locationService
95
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
96
     * @param \eZ\Publish\API\Repository\ContentService $contentService
97
     * @param UrlAliasGenerator $urlAliasGenerator
98
     * @param RequestContext $requestContext
99
     *
100
     * @return UrlAliasRouter
101
     */
102
    protected function getRouter(LocationService $locationService, URLAliasService $urlAliasService, ContentService $contentService, UrlAliasGenerator $urlAliasGenerator, RequestContext $requestContext)
103
    {
104
        return new UrlAliasRouter($locationService, $urlAliasService, $contentService, $urlAliasGenerator, $requestContext);
105
    }
106
107
    public function testRequestContext()
108
    {
109
        $this->assertSame($this->requestContext, $this->router->getContext());
110
        $newContext = new RequestContext();
111
        $this->urlALiasGenerator
112
            ->expects($this->once())
113
            ->method('setRequestContext')
114
            ->with($newContext);
115
        $this->router->setContext($newContext);
116
        $this->assertSame($newContext, $this->router->getContext());
117
    }
118
119
    /**
120
     * @expectedException \RuntimeException
121
     */
122
    public function testMatch()
123
    {
124
        $this->router->match('/foo');
125
    }
126
127
    /**
128
     * @dataProvider providerTestSupports
129
     */
130
    public function testSupports($routeReference, $isSupported)
131
    {
132
        $this->assertSame($isSupported, $this->router->supports($routeReference));
133
    }
134
135
    public function providerTestSupports()
136
    {
137
        return array(
138
            array(new Location(), true),
139
            array(new \stdClass(), false),
140
            array(UrlAliasRouter::URL_ALIAS_ROUTE_NAME, true),
141
            array('some_route_name', false),
142
        );
143
    }
144
145
    public function testGetRouteCollection()
146
    {
147
        $this->assertInstanceOf('Symfony\\Component\\Routing\\RouteCollection', $this->router->getRouteCollection());
148
    }
149
150
    /**
151
     * @param $pathInfo
152
     *
153
     * @return Request
154
     */
155
    protected function getRequestByPathInfo($pathInfo)
156
    {
157
        $request = Request::create($pathInfo);
158
        $request->attributes->set('semanticPathinfo', $pathInfo);
159
        $request->attributes->set(
160
            'siteaccess',
161
            new SiteAccess(
162
                'test',
163
                'fake',
164
                $this->getMock('eZ\\Publish\\Core\\MVC\\Symfony\\SiteAccess\\Matcher')
165
            )
166
        );
167
168
        return $request;
169
    }
170
171
    public function testMatchRequestLocation()
172
    {
173
        $pathInfo = '/foo/bar';
174
        $destinationId = 123;
175
        $urlAlias = new URLAlias(
176
            array(
177
                'path' => $pathInfo,
178
                'type' => UrlAlias::LOCATION,
179
                'destination' => $destinationId,
180
                'isHistory' => false,
181
            )
182
        );
183
        $request = $this->getRequestByPathInfo($pathInfo);
184
        $this->urlAliasService
185
            ->expects($this->once())
186
            ->method('lookup')
187
            ->with($pathInfo)
188
            ->will($this->returnValue($urlAlias));
189
        $this->urlALiasGenerator
190
            ->expects($this->once())
191
            ->method('loadLocation')
192
            ->will($this->returnValue(new Location(array('contentInfo' => new ContentInfo(array('id' => 456))))));
193
194
        $expected = array(
195
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
196
            '_controller' => UrlAliasRouter::VIEW_ACTION,
197
            'locationId' => $destinationId,
198
            'contentId' => 456,
199
            'viewType' => ViewManager::VIEW_TYPE_FULL,
200
            'layout' => true,
201
        );
202
        $this->assertEquals($expected, $this->router->matchRequest($request));
203
        $this->assertSame($destinationId, $request->attributes->get('locationId'));
204
    }
205
206 View Code Duplication
    public function testMatchRequestLocationWithCaseRedirect()
207
    {
208
        $pathInfo = '/Foo/bAR';
209
        $urlAliasPath = '/foo/bar';
210
        $destinationId = 123;
211
        $urlAlias = new URLAlias(
212
            array(
213
                'path' => $urlAliasPath,
214
                'type' => UrlAlias::LOCATION,
215
                'destination' => $destinationId,
216
                'isHistory' => false,
217
            )
218
        );
219
        $request = $this->getRequestByPathInfo($pathInfo);
220
        $this->urlALiasGenerator
221
            ->expects($this->once())
222
            ->method('isUriPrefixExcluded')
223
            ->with($pathInfo)
224
            ->will($this->returnValue(false));
225
        $this->urlAliasService
226
            ->expects($this->once())
227
            ->method('lookup')
228
            ->with($pathInfo)
229
            ->will($this->returnValue($urlAlias));
230
        $this->urlALiasGenerator
231
            ->expects($this->once())
232
            ->method('loadLocation')
233
            ->will($this->returnValue(new Location(array('contentInfo' => new ContentInfo(array('id' => 456))))));
234
235
        $expected = array(
236
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
237
            '_controller' => UrlAliasRouter::VIEW_ACTION,
238
            'locationId' => $destinationId,
239
            'contentId' => 456,
240
            'viewType' => ViewManager::VIEW_TYPE_FULL,
241
            'layout' => true,
242
        );
243
        $this->assertEquals($expected, $this->router->matchRequest($request));
244
        $this->assertTrue($request->attributes->get('needsRedirect'));
245
        $this->assertSame($urlAliasPath, $request->attributes->get('semanticPathinfo'));
246
        $this->assertSame($destinationId, $request->attributes->get('locationId'));
247
    }
248
249 View Code Duplication
    public function testMatchRequestLocationWrongCaseUriPrefixExcluded()
250
    {
251
        $pathInfo = '/Foo/bAR';
252
        $urlAliasPath = '/foo/bar';
253
        $destinationId = 123;
254
        $urlAlias = new URLAlias(
255
            array(
256
                'path' => $urlAliasPath,
257
                'type' => UrlAlias::LOCATION,
258
                'destination' => $destinationId,
259
                'isHistory' => false,
260
            )
261
        );
262
        $request = $this->getRequestByPathInfo($pathInfo);
263
        $this->urlALiasGenerator
264
            ->expects($this->once())
265
            ->method('isUriPrefixExcluded')
266
            ->with($pathInfo)
267
            ->will($this->returnValue(true));
268
        $this->urlAliasService
269
            ->expects($this->once())
270
            ->method('lookup')
271
            ->with($pathInfo)
272
            ->will($this->returnValue($urlAlias));
273
        $this->urlALiasGenerator
274
            ->expects($this->once())
275
            ->method('loadLocation')
276
            ->will($this->returnValue(new Location(array('contentInfo' => new ContentInfo(array('id' => 456))))));
277
278
        $expected = array(
279
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
280
            '_controller' => UrlAliasRouter::VIEW_ACTION,
281
            'contentId' => 456,
282
            'locationId' => $destinationId,
283
            'viewType' => ViewManager::VIEW_TYPE_FULL,
284
            'layout' => true,
285
        );
286
        $this->assertEquals($expected, $this->router->matchRequest($request));
287
        $this->assertTrue($request->attributes->has('needsRedirect'));
288
        $this->assertSame($urlAliasPath, $request->attributes->get('semanticPathinfo'));
289
        $this->assertSame($destinationId, $request->attributes->get('locationId'));
290
    }
291
292 View Code Duplication
    public function testMatchRequestLocationCorrectCaseUriPrefixExcluded()
293
    {
294
        $pathInfo = $urlAliasPath = '/foo/bar';
295
        $destinationId = 123;
296
        $urlAlias = new URLAlias(
297
            array(
298
                'path' => $urlAliasPath,
299
                'type' => UrlAlias::LOCATION,
300
                'destination' => $destinationId,
301
                'isHistory' => false,
302
            )
303
        );
304
        $request = $this->getRequestByPathInfo($pathInfo);
305
        $this->urlALiasGenerator
306
            ->expects($this->once())
307
            ->method('isUriPrefixExcluded')
308
            ->with($pathInfo)
309
            ->will($this->returnValue(true));
310
        $this->urlAliasService
311
            ->expects($this->once())
312
            ->method('lookup')
313
            ->with($pathInfo)
314
            ->will($this->returnValue($urlAlias));
315
        $this->urlALiasGenerator
316
            ->expects($this->once())
317
            ->method('loadLocation')
318
            ->will($this->returnValue(new Location(array('contentInfo' => new ContentInfo(array('id' => 456))))));
319
320
        $expected = array(
321
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
322
            '_controller' => UrlAliasRouter::VIEW_ACTION,
323
            'locationId' => $destinationId,
324
            'contentId' => 456,
325
            'viewType' => ViewManager::VIEW_TYPE_FULL,
326
            'layout' => true,
327
        );
328
        $this->assertEquals($expected, $this->router->matchRequest($request));
329
        $this->assertFalse($request->attributes->has('needsRedirect'));
330
        $this->assertSame($pathInfo, $request->attributes->get('semanticPathinfo'));
331
        $this->assertSame($destinationId, $request->attributes->get('locationId'));
332
    }
333
334
    public function testMatchRequestLocationHistory()
335
    {
336
        $pathInfo = '/foo/bar';
337
        $newPathInfo = '/foo/bar-new';
338
        $destinationId = 123;
339
        $destinationLocation = new Location(array('id' => $destinationId, 'contentInfo' => new ContentInfo(array('id' => 456))));
340
        $urlAlias = new URLAlias(
341
            array(
342
                'path' => $pathInfo,
343
                'type' => UrlAlias::LOCATION,
344
                'destination' => $destinationId,
345
                'isHistory' => true,
346
            )
347
        );
348
        $request = $this->getRequestByPathInfo($pathInfo);
349
        $this->urlAliasService
350
            ->expects($this->once())
351
            ->method('lookup')
352
            ->with($pathInfo)
353
            ->will($this->returnValue($urlAlias));
354
        $this->urlALiasGenerator
355
            ->expects($this->once())
356
            ->method('generate')
357
            ->with($destinationLocation)
358
            ->will($this->returnValue($newPathInfo));
359
        $this->urlALiasGenerator
360
            ->expects($this->once())
361
            ->method('loadLocation')
362
            ->will($this->returnValue($destinationLocation));
363
364
        $expected = array(
365
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
366
            '_controller' => UrlAliasRouter::VIEW_ACTION,
367
            'locationId' => $destinationId,
368
            'contentId' => 456,
369
            'viewType' => ViewManager::VIEW_TYPE_FULL,
370
            'layout' => true,
371
        );
372
        $this->assertEquals($expected, $this->router->matchRequest($request));
373
        $this->assertSame($newPathInfo, $request->attributes->get('semanticPathinfo'));
374
        $this->assertTrue($request->attributes->get('needsRedirect'));
375
        $this->assertFalse($request->attributes->get('prependSiteaccessOnRedirect'));
376
    }
377
378
    public function testMatchRequestLocationCustom()
379
    {
380
        $pathInfo = '/foo/bar';
381
        $destinationId = 123;
382
        $urlAlias = new URLAlias(
383
            array(
384
                'path' => $pathInfo,
385
                'type' => UrlAlias::LOCATION,
386
                'destination' => $destinationId,
387
                'isHistory' => false,
388
                'isCustom' => true,
389
                'forward' => false,
390
            )
391
        );
392
        $request = $this->getRequestByPathInfo($pathInfo);
393
        $this->urlAliasService
394
            ->expects($this->once())
395
            ->method('lookup')
396
            ->with($pathInfo)
397
            ->will($this->returnValue($urlAlias));
398
        $this->urlALiasGenerator
399
            ->expects($this->once())
400
            ->method('loadLocation')
401
            ->will($this->returnValue(new Location(array('contentInfo' => new ContentInfo(array('id' => 456))))));
402
403
        $expected = array(
404
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
405
            '_controller' => UrlAliasRouter::VIEW_ACTION,
406
            'locationId' => $destinationId,
407
            'contentId' => 456,
408
            'viewType' => ViewManager::VIEW_TYPE_FULL,
409
            'layout' => true,
410
        );
411
        $this->assertEquals($expected, $this->router->matchRequest($request));
412
        $this->assertSame($destinationId, $request->attributes->get('locationId'));
413
    }
414
415
    public function testMatchRequestLocationCustomForward()
416
    {
417
        $pathInfo = '/foo/bar';
418
        $newPathInfo = '/foo/bar-new';
419
        $destinationId = 123;
420
        $destinationLocation = new Location(array('id' => $destinationId, 'contentInfo' => new ContentInfo(array('id' => 456))));
421
        $urlAlias = new URLAlias(
422
            array(
423
                'path' => $pathInfo,
424
                'type' => UrlAlias::LOCATION,
425
                'destination' => $destinationId,
426
                'isHistory' => false,
427
                'isCustom' => true,
428
                'forward' => true,
429
            )
430
        );
431
        $request = $this->getRequestByPathInfo($pathInfo);
432
        $this->urlAliasService
433
            ->expects($this->once())
434
            ->method('lookup')
435
            ->with($pathInfo)
436
            ->will($this->returnValue($urlAlias));
437
        $this->urlALiasGenerator
438
            ->expects($this->once())
439
            ->method('loadLocation')
440
            ->with($destinationId)
441
            ->will(
442
                $this->returnValue($destinationLocation)
443
            );
444
        $this->urlALiasGenerator
445
            ->expects($this->once())
446
            ->method('generate')
447
            ->with($destinationLocation)
448
            ->will($this->returnValue($newPathInfo));
449
        $this->urlALiasGenerator
450
            ->expects($this->once())
451
            ->method('loadLocation')
452
            ->will($this->returnValue($destinationLocation));
453
454
        $expected = array(
455
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
456
            '_controller' => UrlAliasRouter::VIEW_ACTION,
457
            'locationId' => $destinationId,
458
            'contentId' => 456,
459
            'viewType' => ViewManager::VIEW_TYPE_FULL,
460
            'layout' => true,
461
        );
462
        $this->assertEquals($expected, $this->router->matchRequest($request));
463
        $this->assertSame($newPathInfo, $request->attributes->get('semanticPathinfo'));
464
        $this->assertTrue($request->attributes->get('needsRedirect'));
465
        $this->assertFalse($request->attributes->get('prependSiteaccessOnRedirect'));
466
    }
467
468
    /**
469
     * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
470
     */
471
    public function testMatchRequestFail()
472
    {
473
        $pathInfo = '/foo/bar';
474
        $request = $this->getRequestByPathInfo($pathInfo);
475
        $this->urlAliasService
476
            ->expects($this->once())
477
            ->method('lookup')
478
            ->with($pathInfo)
479
            ->will($this->throwException(new NotFoundException('URLAlias', $pathInfo)));
480
        $this->router->matchRequest($request);
481
    }
482
483
    public function testMatchRequestResource()
484
    {
485
        $pathInfo = '/hello_content/hello_search';
486
        $destination = '/content/search';
487
        $urlAlias = new URLAlias(
488
            array(
489
                'destination' => $destination,
490
                'path' => $pathInfo,
491
                'type' => UrlAlias::RESOURCE,
492
            )
493
        );
494
        $request = $this->getRequestByPathInfo($pathInfo);
495
        $this->urlAliasService
496
            ->expects($this->once())
497
            ->method('lookup')
498
            ->with($pathInfo)
499
            ->will($this->returnValue($urlAlias));
500
501
        $expected = array(
502
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
503
        );
504
        $this->assertEquals($expected, $this->router->matchRequest($request));
505
        $this->assertSame($destination, $request->attributes->get('semanticPathinfo'));
506
    }
507
508 View Code Duplication
    public function testMatchRequestResourceWithRedirect()
509
    {
510
        $pathInfo = '/hello_content/hello_search';
511
        $destination = '/content/search';
512
        $urlAlias = new URLAlias(
513
            array(
514
                'destination' => $destination,
515
                'path' => $pathInfo,
516
                'type' => UrlAlias::RESOURCE,
517
                'forward' => true,
518
            )
519
        );
520
        $request = $this->getRequestByPathInfo($pathInfo);
521
        $this->urlAliasService
522
            ->expects($this->once())
523
            ->method('lookup')
524
            ->with($pathInfo)
525
            ->will($this->returnValue($urlAlias));
526
527
        $expected = array(
528
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
529
        );
530
        $this->assertEquals($expected, $this->router->matchRequest($request));
531
        $this->assertTrue($request->attributes->get('needsRedirect'));
532
        $this->assertSame($destination, $request->attributes->get('semanticPathinfo'));
533
    }
534
535
    public function testMatchRequestResourceWithCaseRedirect()
536
    {
537
        $pathInfo = '/heLLo_contEnt/hEllo_SEarch';
538
        $urlAliasPath = '/hello_content/hello_search';
539
        $destination = '/content/search';
540
        $urlAlias = new URLAlias(
541
            array(
542
                'destination' => $destination,
543
                'path' => $urlAliasPath,
544
                'type' => UrlAlias::RESOURCE,
545
                'forward' => false,
546
            )
547
        );
548
        $request = $this->getRequestByPathInfo($pathInfo);
549
        $this->urlALiasGenerator
550
            ->expects($this->once())
551
            ->method('isUriPrefixExcluded')
552
            ->with($pathInfo)
553
            ->will($this->returnValue(false));
554
        $this->urlAliasService
555
            ->expects($this->once())
556
            ->method('lookup')
557
            ->with($pathInfo)
558
            ->will($this->returnValue($urlAlias));
559
560
        $expected = array(
561
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
562
        );
563
        $this->assertEquals($expected, $this->router->matchRequest($request));
564
        $this->assertTrue($request->attributes->get('needsRedirect'));
565
        $this->assertSame($urlAliasPath, $request->attributes->get('semanticPathinfo'));
566
    }
567
568
    /**
569
     * Tests that forwarding custom alias will redirect to the resource destination rather than
570
     * to the case-corrected alias.
571
     */
572 View Code Duplication
    public function testMatchRequestResourceCaseIncorrectWithForwardRedirect()
573
    {
574
        $pathInfo = '/heLLo_contEnt/hEllo_SEarch';
575
        $urlAliasPath = '/hello_content/hello_search';
576
        $destination = '/content/search';
577
        $urlAlias = new URLAlias(
578
            array(
579
                'destination' => $destination,
580
                'path' => $urlAliasPath,
581
                'type' => UrlAlias::RESOURCE,
582
                'forward' => true,
583
            )
584
        );
585
        $request = $this->getRequestByPathInfo($pathInfo);
586
        $this->urlAliasService
587
            ->expects($this->once())
588
            ->method('lookup')
589
            ->with($pathInfo)
590
            ->will($this->returnValue($urlAlias));
591
592
        $expected = array(
593
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
594
        );
595
        $this->assertEquals($expected, $this->router->matchRequest($request));
596
        $this->assertTrue($request->attributes->get('needsRedirect'));
597
        $this->assertSame($destination, $request->attributes->get('semanticPathinfo'));
598
    }
599
600
    public function testMatchRequestVirtual()
601
    {
602
        $pathInfo = '/foo/bar';
603
        $urlAlias = new URLAlias(
604
            array(
605
                'path' => $pathInfo,
606
                'type' => UrlAlias::VIRTUAL,
607
            )
608
        );
609
        $request = $this->getRequestByPathInfo($pathInfo);
610
        $this->urlAliasService
611
            ->expects($this->once())
612
            ->method('lookup')
613
            ->with($pathInfo)
614
            ->will($this->returnValue($urlAlias));
615
616
        $expected = array(
617
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
618
        );
619
        $this->assertEquals($expected, $this->router->matchRequest($request));
620
        $this->assertTrue($request->attributes->get('needsForward'));
621
        $this->assertSame('/', $request->attributes->get('semanticPathinfo'));
622
    }
623
624
    public function testMatchRequestVirtualWithCaseRedirect()
625
    {
626
        $pathInfo = '/Foo/bAR';
627
        $urlAliasPath = '/foo/bar';
628
        $urlAlias = new URLAlias(
629
            array(
630
                'path' => $urlAliasPath,
631
                'type' => UrlAlias::VIRTUAL,
632
            )
633
        );
634
        $request = $this->getRequestByPathInfo($pathInfo);
635
        $this->urlALiasGenerator
636
            ->expects($this->once())
637
            ->method('isUriPrefixExcluded')
638
            ->with($pathInfo)
639
            ->will($this->returnValue(false));
640
        $this->urlAliasService
641
            ->expects($this->once())
642
            ->method('lookup')
643
            ->with($pathInfo)
644
            ->will($this->returnValue($urlAlias));
645
646
        $expected = array(
647
            '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
648
        );
649
        $this->assertEquals($expected, $this->router->matchRequest($request));
650
        $this->assertTrue($request->attributes->get('needsRedirect'));
651
        $this->assertSame($urlAliasPath, $request->attributes->get('semanticPathinfo'));
652
    }
653
654
    /**
655
     * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
656
     */
657
    public function testGenerateFail()
658
    {
659
        $this->router->generate('invalidRoute');
660
    }
661
662
    public function testGenerateWithLocation()
663
    {
664
        $location = new Location();
665
        $parameters = array('some' => 'thing');
666
        $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH;
667
        $generatedLink = '/foo/bar';
668
        $this->urlALiasGenerator
669
            ->expects($this->once())
670
            ->method('generate')
671
            ->with($location, $parameters, $referenceType)
672
            ->will($this->returnValue($generatedLink));
673
        $this->assertSame($generatedLink, $this->router->generate($location, $parameters, $referenceType));
674
    }
675
676
    /**
677
     * @expectedException \InvalidArgumentException
678
     */
679
    public function testGenerateNoLocation()
680
    {
681
        $this->router->generate(UrlAliasRouter::URL_ALIAS_ROUTE_NAME, array('foo' => 'bar'));
682
    }
683
684
    /**
685
     * @expectedException \LogicException
686
     */
687
    public function testGenerateInvalidLocation()
688
    {
689
        $this->router->generate(UrlAliasRouter::URL_ALIAS_ROUTE_NAME, array('location' => new \stdClass()));
690
    }
691
692
    public function testGenerateWithLocationId()
693
    {
694
        $locationId = 123;
695
        $location = new Location(array('id' => $locationId));
696
        $parameters = array('some' => 'thing');
697
        $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH;
698
        $generatedLink = '/foo/bar';
699
        $this->locationService
700
            ->expects($this->once())
701
            ->method('loadLocation')
702
            ->with($locationId)
703
            ->will($this->returnValue($location));
704
        $this->urlALiasGenerator
705
            ->expects($this->once())
706
            ->method('generate')
707
            ->with($location, $parameters, $referenceType)
708
            ->will($this->returnValue($generatedLink));
709
        $this->assertSame(
710
            $generatedLink,
711
            $this->router->generate(
712
                UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
713
                $parameters + array('locationId' => $locationId),
714
                $referenceType
715
            )
716
        );
717
    }
718
719
    public function testGenerateWithLocationAsParameter()
720
    {
721
        $locationId = 123;
722
        $location = new Location(array('id' => $locationId));
723
        $parameters = array('some' => 'thing');
724
        $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH;
725
        $generatedLink = '/foo/bar';
726
        $this->urlALiasGenerator
727
            ->expects($this->once())
728
            ->method('generate')
729
            ->with($location, $parameters, $referenceType)
730
            ->will($this->returnValue($generatedLink));
731
        $this->assertSame(
732
            $generatedLink,
733
            $this->router->generate(
734
                UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
735
                $parameters + array('location' => $location),
736
                $referenceType
737
            )
738
        );
739
    }
740
741
    public function testGenerateWithContentId()
742
    {
743
        $locationId = 123;
744
        $contentId = 456;
745
        $location = new Location(array('id' => $locationId));
746
        $contentInfo = new ContentInfo(array('id' => $contentId, 'mainLocationId' => $locationId));
747
        $parameters = array('some' => 'thing');
748
        $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH;
749
        $generatedLink = '/foo/bar';
750
        $this->contentService
751
            ->expects($this->once())
752
            ->method('loadContentInfo')
753
            ->with($contentId)
754
            ->will($this->returnValue($contentInfo));
755
        $this->locationService
756
            ->expects($this->once())
757
            ->method('loadLocation')
758
            ->with($contentInfo->mainLocationId)
759
            ->will($this->returnValue($location));
760
        $this->urlALiasGenerator
761
            ->expects($this->once())
762
            ->method('generate')
763
            ->with($location, $parameters, $referenceType)
764
            ->will($this->returnValue($generatedLink));
765
        $this->assertSame(
766
            $generatedLink,
767
            $this->router->generate(
768
                UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
769
                $parameters + array('contentId' => $contentId),
770
                $referenceType
771
            )
772
        );
773
    }
774
775
    /**
776
     * @expectedException \LogicException
777
     */
778
    public function testGenerateWithContentIdWithMissingMainLocation()
779
    {
780
        $contentId = 456;
781
        $contentInfo = new ContentInfo(array('id' => $contentId, 'mainLocationId' => null));
782
        $parameters = array('some' => 'thing');
783
        $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH;
784
        $this->contentService
785
            ->expects($this->once())
786
            ->method('loadContentInfo')
787
            ->with($contentId)
788
            ->will($this->returnValue($contentInfo));
789
790
        $this->router->generate(
791
            UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
792
            $parameters + array('contentId' => $contentId),
793
            $referenceType
794
        );
795
    }
796
}
797