Completed
Push — EZP-31644 ( 2e0a1e...93bb44 )
by
unknown
19:12
created

UrlAliasTest::testLoadThrowsNotFoundException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Repository\Tests\Service\Mock\UrlAliasTest 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\Repository\Tests\Service\Mock;
10
11
use eZ\Publish\Core\Repository\URLAliasService;
12
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest;
13
use eZ\Publish\SPI\Persistence\Content\UrlAlias as SPIUrlAlias;
14
use eZ\Publish\API\Repository\Values\Content\UrlAlias;
15
use eZ\Publish\Core\Repository\Values\Content\Location;
16
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
17
use eZ\Publish\Core\Base\Exceptions\ForbiddenException;
18
use Exception;
19
20
/**
21
 * Mock test case for UrlAlias Service.
22
 */
23
class UrlAliasTest extends BaseServiceMockTest
24
{
25
    /**
26
     * Test for the __construct() method.
27
     */
28
    public function testConstructor()
29
    {
30
        $repositoryMock = $this->getRepositoryMock();
31
        $languageServiceMock = $this->createMock('eZ\\Publish\\Core\\Repository\\LanguageService');
32
        /** @var \eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler $urlAliasHandler */
33
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
34
        $settings = ['settings'];
35
36
        $languageServiceMock
37
            ->expects($this->once())
38
            ->method('getPrioritizedLanguageCodeList')
39
            ->will($this->returnValue(['prioritizedLanguageList']));
40
41
        $repositoryMock
42
            ->expects($this->once())
43
            ->method('getContentLanguageService')
44
            ->will($this->returnValue($languageServiceMock));
45
46
        $service = new UrlALiasService(
47
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 30 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...sService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
48
            $urlAliasHandler,
49
            $this->getNameSchemaServiceMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getNameSchemaServiceMock() targeting eZ\Publish\Core\Reposito...NameSchemaServiceMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...sService::__construct() does only seem to accept object<eZ\Publish\Core\R...lper\NameSchemaService>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
50
            $settings
51
        );
52
53
        $this->assertAttributeSame(
54
            $repositoryMock,
55
            'repository',
56
            $service
57
        );
58
59
        $this->assertAttributeSame(
60
            $urlAliasHandler,
61
            'urlAliasHandler',
62
            $service
63
        );
64
65
        $this->assertAttributeSame(
66
            [
67
                'settings',
68
                'showAllTranslations' => false,
69
                'prioritizedLanguageList' => ['prioritizedLanguageList'],
70
            ],
71
            'settings',
72
            $service
73
        );
74
    }
75
76
    /**
77
     * Test for the load() method.
78
     */
79
    public function testLoad()
80
    {
81
        $mockedService = $this->getPartlyMockedURLAliasServiceService(['extractPath']);
82
        /** @var \PHPUnit_Framework_MockObject_MockObject $urlAliasHandlerMock */
83
        $urlAliasHandlerMock = $this->getPersistenceMock()->urlAliasHandler();
84
85
        $urlAliasHandlerMock
86
            ->expects($this->once())
87
            ->method('loadUrlAlias')
88
            ->with(42)
89
            ->will($this->returnValue(new SPIUrlAlias()));
90
91
        $mockedService
92
            ->expects($this->once())
93
            ->method('extractPath')
94
            ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias'), null)
95
            ->will($this->returnValue('path'));
96
97
        $urlAlias = $mockedService->load(42);
98
99
        self::assertInstanceOf(
100
            'eZ\\Publish\\API\\Repository\\Values\\Content\\URLAlias',
101
            $urlAlias
102
        );
103
    }
104
105
    /**
106
     * Test for the load() method.
107
     *
108
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
109
     */
110
    public function testLoadThrowsNotFoundException()
111
    {
112
        $mockedService = $this->getPartlyMockedURLAliasServiceService(['extractPath']);
113
        /** @var \PHPUnit_Framework_MockObject_MockObject $urlAliasHandlerMock */
114
        $urlAliasHandlerMock = $this->getPersistenceMock()->urlAliasHandler();
115
116
        $urlAliasHandlerMock
117
            ->expects($this->once())
118
            ->method('loadUrlAlias')
119
            ->with(42)
120
            ->will($this->throwException(new NotFoundException('UrlAlias', 42)));
121
122
        $mockedService->load(42);
123
    }
124
125
    protected function getSpiUrlAlias()
126
    {
127
        $pathElement1 = [
128
            'always-available' => true,
129
            'translations' => [
130
                'cro-HR' => 'jedan',
131
            ],
132
        ];
133
        $pathElement2 = [
134
            'always-available' => false,
135
            'translations' => [
136
                'cro-HR' => 'dva',
137
                'eng-GB' => 'two',
138
            ],
139
        ];
140
        $pathElement3 = [
141
            'always-available' => false,
142
            'translations' => [
143
                'cro-HR' => 'tri',
144
                'eng-GB' => 'three',
145
                'ger-DE' => 'drei',
146
            ],
147
        ];
148
149
        return new SPIUrlAlias(
150
            [
151
                'id' => '3',
152
                'pathData' => [$pathElement1, $pathElement2, $pathElement3],
153
                'languageCodes' => ['ger-DE'],
154
                'alwaysAvailable' => false,
155
            ]
156
        );
157
    }
158
159
    /**
160
     * Test for the load() method.
161
     *
162
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
163
     */
164
    public function testLoadThrowsNotFoundExceptionPath()
165
    {
166
        $spiUrlAlias = $this->getSpiUrlAlias();
167
        $urlAliasService = $this->getRepository()->getURLAliasService();
168
        $configuration = [
169
            'prioritizedLanguageList' => ['fre-FR'],
170
            'showAllTranslations' => false,
171
        ];
172
        $this->setConfiguration($urlAliasService, $configuration);
173
174
        $urlAliasHandlerMock = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
175
176
        $urlAliasHandlerMock
177
            ->expects($this->once())
178
            ->method('loadUrlAlias')
179
            ->with(42)
180
            ->will($this->returnValue($spiUrlAlias));
181
182
        $urlAliasService->load(42);
183
    }
184
185
    /**
186
     * Test for the removeAliases() method.
187
     *
188
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
189
     */
190
    public function testRemoveAliasesThrowsInvalidArgumentException()
191
    {
192
        $aliasList = [new UrlAlias(['isCustom' => false])];
193
        $mockedService = $this->getPartlyMockedURLAliasServiceService();
194
        $mockedService->removeAliases($aliasList);
195
    }
196
197
    /**
198
     * Test for the removeAliases() method.
199
     */
200 View Code Duplication
    public function testRemoveAliases()
201
    {
202
        $aliasList = [new UrlAlias(['isCustom' => true])];
203
        $spiAliasList = [new SPIUrlAlias(['isCustom' => true])];
204
        $repositoryMock = $this->getRepositoryMock();
205
        $mockedService = $this->getPartlyMockedURLAliasServiceService();
206
        /** @var \PHPUnit_Framework_MockObject_MockObject $urlAliasHandlerMock */
207
        $urlAliasHandlerMock = $this->getPersistenceMock()->urlAliasHandler();
208
209
        $repositoryMock
210
            ->expects($this->once())
211
            ->method('beginTransaction');
212
        $repositoryMock
213
            ->expects($this->once())
214
            ->method('commit');
215
216
        $urlAliasHandlerMock
217
            ->expects($this->once())
218
            ->method('removeURLAliases')
219
            ->with($spiAliasList);
220
221
        $mockedService->removeAliases($aliasList);
222
    }
223
224
    /**
225
     * Test for the removeAliases() method.
226
     *
227
     * @expectedException \Exception
228
     * @expectedExceptionMessage Handler threw an exception
229
     */
230 View Code Duplication
    public function testRemoveAliasesWithRollback()
231
    {
232
        $aliasList = [new UrlAlias(['isCustom' => true])];
233
        $spiAliasList = [new SPIUrlAlias(['isCustom' => true])];
234
        $repositoryMock = $this->getRepositoryMock();
235
        $mockedService = $this->getPartlyMockedURLAliasServiceService();
236
        /** @var \PHPUnit_Framework_MockObject_MockObject $urlAliasHandlerMock */
237
        $urlAliasHandlerMock = $this->getPersistenceMock()->urlAliasHandler();
238
239
        $repositoryMock
240
            ->expects($this->once())
241
            ->method('beginTransaction');
242
        $repositoryMock
243
            ->expects($this->once())
244
            ->method('rollback');
245
246
        $urlAliasHandlerMock
247
            ->expects($this->once())
248
            ->method('removeURLAliases')
249
            ->with($spiAliasList)
250
            ->will($this->throwException(new Exception('Handler threw an exception')));
251
252
        $mockedService->removeAliases($aliasList);
253
    }
254
255
    public function providerForTestListAutogeneratedLocationAliasesPath()
256
    {
257
        $pathElement1 = [
258
            'always-available' => true,
259
            'translations' => [
260
                'cro-HR' => 'jedan',
261
            ],
262
        ];
263
        $pathElement2 = [
264
            'always-available' => false,
265
            'translations' => [
266
                'cro-HR' => 'dva',
267
                'eng-GB' => 'two',
268
            ],
269
        ];
270
        $pathElement3 = [
271
            'always-available' => false,
272
            'translations' => [
273
                'cro-HR' => 'tri',
274
                'eng-GB' => 'three',
275
                'ger-DE' => 'drei',
276
            ],
277
        ];
278
        $pathData1 = [$pathElement1];
279
        $pathData2 = [$pathElement1, $pathElement2];
280
        $pathData3 = [$pathElement1, $pathElement2, $pathElement3];
281
        $spiUrlAliases1 = [
282
            new SPIUrlAlias(
283
                [
284
                    'id' => '1',
285
                    'pathData' => $pathData1,
286
                    'languageCodes' => ['cro-HR'],
287
                    'alwaysAvailable' => true,
288
                ]
289
            ),
290
        ];
291
        $spiUrlAliases2 = [
292
            new SPIUrlAlias(
293
                [
294
                    'id' => '1',
295
                    'pathData' => $pathData2,
296
                    'languageCodes' => ['cro-HR'],
297
                    'alwaysAvailable' => false,
298
                ]
299
            ),
300
            new SPIUrlAlias(
301
                [
302
                    'id' => '2',
303
                    'pathData' => $pathData2,
304
                    'languageCodes' => ['eng-GB'],
305
                    'alwaysAvailable' => false,
306
                ]
307
            ),
308
        ];
309
        $spiUrlAliases3 = [
310
            new SPIUrlAlias(
311
                [
312
                    'id' => '1',
313
                    'pathData' => $pathData3,
314
                    'languageCodes' => ['cro-HR'],
315
                    'alwaysAvailable' => false,
316
                ]
317
            ),
318
            new SPIUrlAlias(
319
                [
320
                    'id' => '2',
321
                    'pathData' => $pathData3,
322
                    'languageCodes' => ['eng-GB'],
323
                    'alwaysAvailable' => false,
324
                ]
325
            ),
326
            new SPIUrlAlias(
327
                [
328
                    'id' => '3',
329
                    'pathData' => $pathData3,
330
                    'languageCodes' => ['ger-DE'],
331
                    'alwaysAvailable' => false,
332
                ]
333
            ),
334
        ];
335
336
        return [
337
            [
338
                $spiUrlAliases1,
339
                ['cro-HR'],
340
                [
341
                    'cro-HR' => '/jedan',
342
                ],
343
                'cro-HR',
344
            ],
345
            [
346
                $spiUrlAliases1,
347
                ['eng-GB'],
348
                [
349
                    'cro-HR' => '/jedan',
350
                ],
351
                'cro-HR',
352
            ],
353
            [
354
                $spiUrlAliases1,
355
                ['ger-DE'],
356
                [
357
                    'cro-HR' => '/jedan',
358
                ],
359
                'cro-HR',
360
            ],
361
            [
362
                $spiUrlAliases1,
363
                ['cro-HR', 'eng-GB', 'ger-DE'],
364
                [
365
                    'cro-HR' => '/jedan',
366
                ],
367
                'cro-HR',
368
            ],
369
            [
370
                $spiUrlAliases2,
371
                ['cro-HR'],
372
                [
373
                    'cro-HR' => '/jedan/dva',
374
                ],
375
                'cro-HR',
376
            ],
377
            [
378
                $spiUrlAliases2,
379
                ['eng-GB'],
380
                [
381
                    'eng-GB' => '/jedan/two',
382
                ],
383
                'eng-GB',
384
            ],
385
            [
386
                $spiUrlAliases2,
387
                ['cro-HR', 'eng-GB'],
388
                [
389
                    'cro-HR' => '/jedan/dva',
390
                    'eng-GB' => '/jedan/two',
391
                ],
392
                'cro-HR',
393
            ],
394
            [
395
                $spiUrlAliases2,
396
                ['cro-HR', 'ger-DE'],
397
                [
398
                    'cro-HR' => '/jedan/dva',
399
                ],
400
                'cro-HR',
401
            ],
402
            [
403
                $spiUrlAliases2,
404
                ['eng-GB', 'cro-HR'],
405
                [
406
                    'eng-GB' => '/jedan/two',
407
                    'cro-HR' => '/jedan/dva',
408
                ],
409
                'eng-GB',
410
            ],
411
            [
412
                $spiUrlAliases2,
413
                ['eng-GB', 'ger-DE'],
414
                [
415
                    'eng-GB' => '/jedan/two',
416
                ],
417
                'eng-GB',
418
            ],
419
            [
420
                $spiUrlAliases2,
421
                ['ger-DE', 'cro-HR'],
422
                [
423
                    'cro-HR' => '/jedan/dva',
424
                ],
425
                'cro-HR',
426
            ],
427
            [
428
                $spiUrlAliases2,
429
                ['ger-DE', 'eng-GB'],
430
                [
431
                    'eng-GB' => '/jedan/two',
432
                ],
433
                'eng-GB',
434
            ],
435
            [
436
                $spiUrlAliases2,
437
                ['cro-HR', 'eng-GB', 'ger-DE'],
438
                [
439
                    'cro-HR' => '/jedan/dva',
440
                    'eng-GB' => '/jedan/two',
441
                ],
442
                'cro-HR',
443
            ],
444
            [
445
                $spiUrlAliases2,
446
                ['cro-HR', 'ger-DE', 'eng-GB'],
447
                [
448
                    'cro-HR' => '/jedan/dva',
449
                    'eng-GB' => '/jedan/two',
450
                ],
451
                'cro-HR',
452
            ],
453
            [
454
                $spiUrlAliases2,
455
                ['eng-GB', 'cro-HR', 'ger-DE'],
456
                [
457
                    'eng-GB' => '/jedan/two',
458
                    'cro-HR' => '/jedan/dva',
459
                ],
460
                'eng-GB',
461
            ],
462
            [
463
                $spiUrlAliases2,
464
                ['eng-GB', 'ger-DE', 'cro-HR'],
465
                [
466
                    'eng-GB' => '/jedan/two',
467
                    'cro-HR' => '/jedan/dva',
468
                ],
469
                'eng-GB',
470
            ],
471
            [
472
                $spiUrlAliases2,
473
                ['ger-DE', 'cro-HR', 'eng-GB'],
474
                [
475
                    'cro-HR' => '/jedan/dva',
476
                    'eng-GB' => '/jedan/two',
477
                ],
478
                'cro-HR',
479
            ],
480
            [
481
                $spiUrlAliases2,
482
                ['ger-DE', 'eng-GB', 'cro-HR'],
483
                [
484
                    'eng-GB' => '/jedan/two',
485
                    'cro-HR' => '/jedan/dva',
486
                ],
487
                'eng-GB',
488
            ],
489
            [
490
                $spiUrlAliases3,
491
                ['cro-HR'],
492
                [
493
                    'cro-HR' => '/jedan/dva/tri',
494
                ],
495
                'cro-HR',
496
            ],
497
            [
498
                $spiUrlAliases3,
499
                ['eng-GB'],
500
                [
501
                    'eng-GB' => '/jedan/two/three',
502
                ],
503
                'eng-GB',
504
            ],
505
            [
506
                $spiUrlAliases3,
507
                ['cro-HR', 'eng-GB'],
508
                [
509
                    'cro-HR' => '/jedan/dva/tri',
510
                    'eng-GB' => '/jedan/dva/three',
511
                ],
512
                'cro-HR',
513
            ],
514
            [
515
                $spiUrlAliases3,
516
                ['cro-HR', 'ger-DE'],
517
                [
518
                    'cro-HR' => '/jedan/dva/tri',
519
                    'ger-DE' => '/jedan/dva/drei',
520
                ],
521
                'cro-HR',
522
            ],
523
            [
524
                $spiUrlAliases3,
525
                ['eng-GB', 'cro-HR'],
526
                [
527
                    'eng-GB' => '/jedan/two/three',
528
                    'cro-HR' => '/jedan/two/tri',
529
                ],
530
                'eng-GB',
531
            ],
532
            [
533
                $spiUrlAliases3,
534
                ['eng-GB', 'ger-DE'],
535
                [
536
                    'eng-GB' => '/jedan/two/three',
537
                    'ger-DE' => '/jedan/two/drei',
538
                ],
539
                'eng-GB',
540
            ],
541
            [
542
                $spiUrlAliases3,
543
                ['ger-DE', 'eng-GB'],
544
                [
545
                    'ger-DE' => '/jedan/two/drei',
546
                    'eng-GB' => '/jedan/two/three',
547
                ],
548
                'ger-DE',
549
            ],
550
            [
551
                $spiUrlAliases3,
552
                ['ger-DE', 'cro-HR'],
553
                [
554
                    'ger-DE' => '/jedan/dva/drei',
555
                    'cro-HR' => '/jedan/dva/tri',
556
                ],
557
                'ger-DE',
558
            ],
559
            [
560
                $spiUrlAliases3,
561
                ['cro-HR', 'eng-GB', 'ger-DE'],
562
                [
563
                    'cro-HR' => '/jedan/dva/tri',
564
                    'eng-GB' => '/jedan/dva/three',
565
                    'ger-DE' => '/jedan/dva/drei',
566
                ],
567
                'cro-HR',
568
            ],
569
            [
570
                $spiUrlAliases3,
571
                ['cro-HR', 'ger-DE', 'eng-GB'],
572
                [
573
                    'cro-HR' => '/jedan/dva/tri',
574
                    'ger-DE' => '/jedan/dva/drei',
575
                    'eng-GB' => '/jedan/dva/three',
576
                ],
577
                'cro-HR',
578
            ],
579
            [
580
                $spiUrlAliases3,
581
                ['eng-GB', 'cro-HR', 'ger-DE'],
582
                [
583
                    'eng-GB' => '/jedan/two/three',
584
                    'cro-HR' => '/jedan/two/tri',
585
                    'ger-DE' => '/jedan/two/drei',
586
                ],
587
                'eng-GB',
588
            ],
589
            [
590
                $spiUrlAliases3,
591
                ['eng-GB', 'ger-DE', 'cro-HR'],
592
                [
593
                    'eng-GB' => '/jedan/two/three',
594
                    'ger-DE' => '/jedan/two/drei',
595
                    'cro-HR' => '/jedan/two/tri',
596
                ],
597
                'eng-GB',
598
            ],
599
            [
600
                $spiUrlAliases3,
601
                ['ger-DE', 'cro-HR', 'eng-GB'],
602
                [
603
                    'ger-DE' => '/jedan/dva/drei',
604
                    'cro-HR' => '/jedan/dva/tri',
605
                    'eng-GB' => '/jedan/dva/three',
606
                ],
607
                'ger-DE',
608
            ],
609
            [
610
                $spiUrlAliases3,
611
                ['ger-DE', 'eng-GB', 'cro-HR'],
612
                [
613
                    'ger-DE' => '/jedan/two/drei',
614
                    'eng-GB' => '/jedan/two/three',
615
                    'cro-HR' => '/jedan/two/tri',
616
                ],
617
                'ger-DE',
618
            ],
619
        ];
620
    }
621
622
    /**
623
     * Test for the listLocationAliases() method.
624
     *
625
     * @dataProvider providerForTestListAutogeneratedLocationAliasesPath
626
     */
627 View Code Duplication
    public function testListAutogeneratedLocationAliasesPath($spiUrlAliases, $prioritizedLanguageCodes, $paths)
628
    {
629
        $urlAliasService = $this->getRepository()->getURLAliasService();
630
        $configuration = [
631
            'prioritizedLanguageList' => $prioritizedLanguageCodes,
632
            'showAllTranslations' => false,
633
        ];
634
        $this->setConfiguration($urlAliasService, $configuration);
635
636
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
637
        $urlAliasHandler->expects(
638
            $this->once()
639
        )->method(
640
            'listURLAliasesForLocation'
641
        )->with(
642
            $this->equalTo(42),
643
            $this->equalTo(false)
644
        )->will(
645
            $this->returnValue($spiUrlAliases)
646
        );
647
648
        $location = $this->getLocationStub();
649
        $urlAliases = $urlAliasService->listLocationAliases($location, false, null);
650
651
        self::assertEquals(
652
            count($paths),
653
            count($urlAliases)
654
        );
655
656
        foreach ($urlAliases as $index => $urlAlias) {
657
            $pathKeys = array_keys($paths);
658
            self::assertEquals(
659
                $paths[$pathKeys[$index]],
660
                $urlAlias->path
661
            );
662
            self::assertEquals(
663
                [$pathKeys[$index]],
664
                $urlAlias->languageCodes
665
            );
666
        }
667
    }
668
669
    /**
670
     * Test for the listLocationAliases() method.
671
     *
672
     * @dataProvider providerForTestListAutogeneratedLocationAliasesPath
673
     */
674 View Code Duplication
    public function testListAutogeneratedLocationAliasesPathCustomConfiguration(
675
        $spiUrlAliases,
676
        $prioritizedLanguageCodes,
677
        $paths
678
    ) {
679
        $urlAliasService = $this->getRepository()->getURLAliasService();
680
        $configuration = [
681
            'prioritizedLanguageList' => [],
682
            'showAllTranslations' => false,
683
        ];
684
        $this->setConfiguration($urlAliasService, $configuration);
685
686
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
687
        $urlAliasHandler->expects(
688
            $this->once()
689
        )->method(
690
            'listURLAliasesForLocation'
691
        )->with(
692
            $this->equalTo(42),
693
            $this->equalTo(false)
694
        )->will(
695
            $this->returnValue($spiUrlAliases)
696
        );
697
698
        $location = $this->getLocationStub();
699
        $urlAliases = $urlAliasService->listLocationAliases(
700
            $location,
701
            false,
702
            null,
703
            false,
704
            $prioritizedLanguageCodes
705
        );
706
707
        self::assertEquals(
708
            count($paths),
709
            count($urlAliases)
710
        );
711
712
        foreach ($urlAliases as $index => $urlAlias) {
713
            $pathKeys = array_keys($paths);
714
            self::assertEquals(
715
                $paths[$pathKeys[$index]],
716
                $urlAlias->path
717
            );
718
            self::assertEquals(
719
                [$pathKeys[$index]],
720
                $urlAlias->languageCodes
721
            );
722
        }
723
    }
724
725
    /**
726
     * Test for the load() method.
727
     */
728 View Code Duplication
    public function testListLocationAliasesWithShowAllTranslations()
729
    {
730
        $pathElement1 = [
731
            'always-available' => true,
732
            'translations' => [
733
                'cro-HR' => 'jedan',
734
            ],
735
        ];
736
        $pathElement2 = [
737
            'always-available' => false,
738
            'translations' => [
739
                'cro-HR' => 'dva',
740
                'eng-GB' => 'two',
741
            ],
742
        ];
743
        $pathElement3 = [
744
            'always-available' => false,
745
            'translations' => [
746
                'cro-HR' => 'tri',
747
                'eng-GB' => 'three',
748
                'ger-DE' => 'drei',
749
            ],
750
        ];
751
        $spiUrlAlias = new SPIUrlAlias(
752
            [
753
                'id' => '3',
754
                'pathData' => [$pathElement1, $pathElement2, $pathElement3],
755
                'languageCodes' => ['ger-DE'],
756
                'alwaysAvailable' => false,
757
            ]
758
        );
759
        $urlAliasService = $this->getRepository()->getURLAliasService();
760
        $configuration = [
761
            'prioritizedLanguageList' => ['fre-FR'],
762
            'showAllTranslations' => true,
763
        ];
764
        $this->setConfiguration($urlAliasService, $configuration);
765
766
        $urlAliasHandlerMock = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
767
768
        $urlAliasHandlerMock->expects(
769
            $this->once()
770
        )->method(
771
            'listURLAliasesForLocation'
772
        )->with(
773
            $this->equalTo(42),
774
            $this->equalTo(false)
775
        )->will(
776
            $this->returnValue([$spiUrlAlias])
777
        );
778
779
        $location = $this->getLocationStub();
780
        $urlAliases = $urlAliasService->listLocationAliases($location, false, null);
781
782
        self::assertCount(1, $urlAliases);
783
        self::assertInstanceOf('eZ\\Publish\\API\\Repository\\Values\\Content\\URLAlias', $urlAliases[0]);
784
        self::assertEquals('/jedan/dva/tri', $urlAliases[0]->path);
785
    }
786
787
    /**
788
     * Test for the load() method.
789
     */
790 View Code Duplication
    public function testListLocationAliasesWithShowAllTranslationsCustomConfiguration()
791
    {
792
        $pathElement1 = [
793
            'always-available' => true,
794
            'translations' => [
795
                'cro-HR' => 'jedan',
796
            ],
797
        ];
798
        $pathElement2 = [
799
            'always-available' => false,
800
            'translations' => [
801
                'cro-HR' => 'dva',
802
                'eng-GB' => 'two',
803
            ],
804
        ];
805
        $pathElement3 = [
806
            'always-available' => false,
807
            'translations' => [
808
                'cro-HR' => 'tri',
809
                'eng-GB' => 'three',
810
                'ger-DE' => 'drei',
811
            ],
812
        ];
813
        $spiUrlAlias = new SPIUrlAlias(
814
            [
815
                'id' => '3',
816
                'pathData' => [$pathElement1, $pathElement2, $pathElement3],
817
                'languageCodes' => ['ger-DE'],
818
                'alwaysAvailable' => false,
819
            ]
820
        );
821
        $urlAliasService = $this->getRepository()->getURLAliasService();
822
        $configuration = [
823
            'prioritizedLanguageList' => [],
824
            'showAllTranslations' => false,
825
        ];
826
        $this->setConfiguration($urlAliasService, $configuration);
827
828
        $urlAliasHandlerMock = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
829
830
        $urlAliasHandlerMock->expects(
831
            $this->once()
832
        )->method(
833
            'listURLAliasesForLocation'
834
        )->with(
835
            $this->equalTo(42),
836
            $this->equalTo(false)
837
        )->will(
838
            $this->returnValue([$spiUrlAlias])
839
        );
840
841
        $location = $this->getLocationStub();
842
        $urlAliases = $urlAliasService->listLocationAliases(
843
            $location,
844
            false,
845
            null,
846
            true,
847
            ['fre-FR']
848
        );
849
850
        self::assertCount(1, $urlAliases);
851
        self::assertInstanceOf('eZ\\Publish\\API\\Repository\\Values\\Content\\URLAlias', $urlAliases[0]);
852
        self::assertEquals('/jedan/dva/tri', $urlAliases[0]->path);
853
    }
854
855
    public function providerForTestListAutogeneratedLocationAliasesEmpty()
856
    {
857
        $pathElement1 = [
858
            'always-available' => true,
859
            'translations' => [
860
                'cro-HR' => '/jedan',
861
            ],
862
        ];
863
        $pathElement2 = [
864
            'always-available' => false,
865
            'translations' => [
866
                'cro-HR' => 'dva',
867
                'eng-GB' => 'two',
868
            ],
869
        ];
870
        $pathElement3 = [
871
            'always-available' => false,
872
            'translations' => [
873
                'cro-HR' => 'tri',
874
                'eng-GB' => 'three',
875
                'ger-DE' => 'drei',
876
            ],
877
        ];
878
        $pathData2 = [$pathElement1, $pathElement2];
879
        $pathData3 = [$pathElement1, $pathElement2, $pathElement3];
880
        $spiUrlAliases2 = [
881
            new SPIUrlAlias(
882
                [
883
                    'pathData' => $pathData2,
884
                    'languageCodes' => ['cro-HR'],
885
                    'alwaysAvailable' => false,
886
                ]
887
            ),
888
            new SPIUrlAlias(
889
                [
890
                    'pathData' => $pathData2,
891
                    'languageCodes' => ['eng-GB'],
892
                    'alwaysAvailable' => false,
893
                ]
894
            ),
895
        ];
896
        $spiUrlAliases3 = [
897
            new SPIUrlAlias(
898
                [
899
                    'pathData' => $pathData3,
900
                    'languageCodes' => ['cro-HR'],
901
                    'alwaysAvailable' => false,
902
                ]
903
            ),
904
            new SPIUrlAlias(
905
                [
906
                    'pathData' => $pathData3,
907
                    'languageCodes' => ['eng-GB'],
908
                    'alwaysAvailable' => false,
909
                ]
910
            ),
911
            new SPIUrlAlias(
912
                [
913
                    'pathData' => $pathData3,
914
                    'languageCodes' => ['ger-DE'],
915
                    'alwaysAvailable' => false,
916
                ]
917
            ),
918
        ];
919
920
        return [
921
            [
922
                $spiUrlAliases2,
923
                ['ger-DE'],
924
            ],
925
            [
926
                $spiUrlAliases3,
927
                ['ger-DE'],
928
            ],
929
        ];
930
    }
931
932
    /**
933
     * Test for the listLocationAliases() method.
934
     *
935
     * @dataProvider providerForTestListAutogeneratedLocationAliasesEmpty
936
     */
937 View Code Duplication
    public function testListAutogeneratedLocationAliasesEmpty($spiUrlAliases, $prioritizedLanguageCodes)
938
    {
939
        $urlAliasService = $this->getRepository()->getURLAliasService();
940
        $configuration = [
941
            'prioritizedLanguageList' => $prioritizedLanguageCodes,
942
            'showAllTranslations' => false,
943
        ];
944
        $this->setConfiguration($urlAliasService, $configuration);
945
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
946
        $urlAliasHandler->expects(
947
            $this->once()
948
        )->method(
949
            'listURLAliasesForLocation'
950
        )->with(
951
            $this->equalTo(42),
952
            $this->equalTo(false)
953
        )->will(
954
            $this->returnValue($spiUrlAliases)
955
        );
956
957
        $location = $this->getLocationStub();
958
        $urlAliases = $urlAliasService->listLocationAliases($location, false, null);
959
960
        self::assertEmpty($urlAliases);
961
    }
962
963
    /**
964
     * Test for the listLocationAliases() method.
965
     *
966
     * @dataProvider providerForTestListAutogeneratedLocationAliasesEmpty
967
     */
968 View Code Duplication
    public function testListAutogeneratedLocationAliasesEmptyCustomConfiguration(
969
        $spiUrlAliases,
970
        $prioritizedLanguageCodes
971
    ) {
972
        $urlAliasService = $this->getRepository()->getURLAliasService();
973
        $configuration = [
974
            'prioritizedLanguageList' => [],
975
            'showAllTranslations' => false,
976
        ];
977
        $this->setConfiguration($urlAliasService, $configuration);
978
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
979
        $urlAliasHandler->expects(
980
            $this->once()
981
        )->method(
982
            'listURLAliasesForLocation'
983
        )->with(
984
            $this->equalTo(42),
985
            $this->equalTo(false)
986
        )->will(
987
            $this->returnValue($spiUrlAliases)
988
        );
989
990
        $location = $this->getLocationStub();
991
        $urlAliases = $urlAliasService->listLocationAliases(
992
            $location,
993
            false,
994
            null,
995
            false,
996
            $prioritizedLanguageCodes
997
        );
998
999
        self::assertEmpty($urlAliases);
1000
    }
1001
1002
    public function providerForTestListAutogeneratedLocationAliasesWithLanguageCodePath()
1003
    {
1004
        $pathElement1 = [
1005
            'always-available' => true,
1006
            'translations' => [
1007
                'cro-HR' => 'jedan',
1008
            ],
1009
        ];
1010
        $pathElement2 = [
1011
            'always-available' => false,
1012
            'translations' => [
1013
                'cro-HR' => 'dva',
1014
                'eng-GB' => 'two',
1015
            ],
1016
        ];
1017
        $pathElement3 = [
1018
            'always-available' => false,
1019
            'translations' => [
1020
                'cro-HR' => 'tri',
1021
                'eng-GB' => 'three',
1022
                'ger-DE' => 'drei',
1023
            ],
1024
        ];
1025
        $pathData1 = [$pathElement1];
1026
        $pathData2 = [$pathElement1, $pathElement2];
1027
        $pathData3 = [$pathElement1, $pathElement2, $pathElement3];
1028
        $spiUrlAliases1 = [
1029
            new SPIUrlAlias(
1030
                [
1031
                    'pathData' => $pathData1,
1032
                    'languageCodes' => ['cro-HR'],
1033
                    'alwaysAvailable' => true,
1034
                ]
1035
            ),
1036
        ];
1037
        $spiUrlAliases2 = [
1038
            new SPIUrlAlias(
1039
                [
1040
                    'pathData' => $pathData2,
1041
                    'languageCodes' => ['cro-HR'],
1042
                    'alwaysAvailable' => false,
1043
                ]
1044
            ),
1045
            new SPIUrlAlias(
1046
                [
1047
                    'pathData' => $pathData2,
1048
                    'languageCodes' => ['eng-GB'],
1049
                    'alwaysAvailable' => false,
1050
                ]
1051
            ),
1052
        ];
1053
        $spiUrlAliases3 = [
1054
            new SPIUrlAlias(
1055
                [
1056
                    'pathData' => $pathData3,
1057
                    'languageCodes' => ['cro-HR'],
1058
                    'alwaysAvailable' => false,
1059
                ]
1060
            ),
1061
            new SPIUrlAlias(
1062
                [
1063
                    'pathData' => $pathData3,
1064
                    'languageCodes' => ['eng-GB'],
1065
                    'alwaysAvailable' => false,
1066
                ]
1067
            ),
1068
            new SPIUrlAlias(
1069
                [
1070
                    'pathData' => $pathData3,
1071
                    'languageCodes' => ['ger-DE'],
1072
                    'alwaysAvailable' => false,
1073
                ]
1074
            ),
1075
        ];
1076
1077
        return [
1078
            [
1079
                $spiUrlAliases1,
1080
                'cro-HR',
1081
                ['cro-HR'],
1082
                [
1083
                    '/jedan',
1084
                ],
1085
            ],
1086
            [
1087
                $spiUrlAliases1,
1088
                'cro-HR',
1089
                ['eng-GB'],
1090
                [
1091
                    '/jedan',
1092
                ],
1093
            ],
1094
            [
1095
                $spiUrlAliases2,
1096
                'cro-HR',
1097
                ['cro-HR'],
1098
                [
1099
                    '/jedan/dva',
1100
                ],
1101
            ],
1102
            [
1103
                $spiUrlAliases2,
1104
                'eng-GB',
1105
                ['eng-GB'],
1106
                [
1107
                    '/jedan/two',
1108
                ],
1109
            ],
1110
            [
1111
                $spiUrlAliases2,
1112
                'eng-GB',
1113
                ['cro-HR', 'eng-GB'],
1114
                [
1115
                    '/jedan/two',
1116
                ],
1117
            ],
1118
            [
1119
                $spiUrlAliases2,
1120
                'cro-HR',
1121
                ['cro-HR', 'ger-DE'],
1122
                [
1123
                    '/jedan/dva',
1124
                ],
1125
            ],
1126
            [
1127
                $spiUrlAliases2,
1128
                'cro-HR',
1129
                ['eng-GB', 'cro-HR'],
1130
                [
1131
                    '/jedan/dva',
1132
                ],
1133
            ],
1134
            [
1135
                $spiUrlAliases2,
1136
                'eng-GB',
1137
                ['eng-GB', 'ger-DE'],
1138
                [
1139
                    '/jedan/two',
1140
                ],
1141
            ],
1142
            [
1143
                $spiUrlAliases2,
1144
                'cro-HR',
1145
                ['ger-DE', 'cro-HR'],
1146
                [
1147
                    '/jedan/dva',
1148
                ],
1149
            ],
1150
            [
1151
                $spiUrlAliases2,
1152
                'eng-GB',
1153
                ['ger-DE', 'eng-GB'],
1154
                [
1155
                    '/jedan/two',
1156
                ],
1157
            ],
1158
            [
1159
                $spiUrlAliases2,
1160
                'cro-HR',
1161
                ['cro-HR', 'eng-GB', 'ger-DE'],
1162
                [
1163
                    '/jedan/dva',
1164
                ],
1165
            ],
1166
            [
1167
                $spiUrlAliases2,
1168
                'eng-GB',
1169
                ['cro-HR', 'ger-DE', 'eng-GB'],
1170
                [
1171
                    '/jedan/two',
1172
                ],
1173
            ],
1174
            [
1175
                $spiUrlAliases2,
1176
                'cro-HR',
1177
                ['eng-GB', 'cro-HR', 'ger-DE'],
1178
                [
1179
                    '/jedan/dva',
1180
                ],
1181
            ],
1182
            [
1183
                $spiUrlAliases2,
1184
                'cro-HR',
1185
                ['eng-GB', 'ger-DE', 'cro-HR'],
1186
                [
1187
                    '/jedan/dva',
1188
                ],
1189
            ],
1190
            [
1191
                $spiUrlAliases2,
1192
                'cro-HR',
1193
                ['ger-DE', 'cro-HR', 'eng-GB'],
1194
                [
1195
                    '/jedan/dva',
1196
                ],
1197
            ],
1198
            [
1199
                $spiUrlAliases2,
1200
                'cro-HR',
1201
                ['ger-DE', 'eng-GB', 'cro-HR'],
1202
                [
1203
                    '/jedan/dva',
1204
                ],
1205
            ],
1206
            [
1207
                $spiUrlAliases3,
1208
                'cro-HR',
1209
                ['cro-HR'],
1210
                [
1211
                    '/jedan/dva/tri',
1212
                ],
1213
            ],
1214
            [
1215
                $spiUrlAliases3,
1216
                'eng-GB',
1217
                ['eng-GB'],
1218
                [
1219
                    '/jedan/two/three',
1220
                ],
1221
            ],
1222
            [
1223
                $spiUrlAliases3,
1224
                'eng-GB',
1225
                ['cro-HR', 'eng-GB'],
1226
                [
1227
                    '/jedan/dva/three',
1228
                ],
1229
            ],
1230
            [
1231
                $spiUrlAliases3,
1232
                'ger-DE',
1233
                ['cro-HR', 'ger-DE'],
1234
                [
1235
                    '/jedan/dva/drei',
1236
                ],
1237
            ],
1238
            [
1239
                $spiUrlAliases3,
1240
                'cro-HR',
1241
                ['eng-GB', 'cro-HR'],
1242
                [
1243
                    '/jedan/two/tri',
1244
                ],
1245
            ],
1246
            [
1247
                $spiUrlAliases3,
1248
                'ger-DE',
1249
                ['eng-GB', 'ger-DE'],
1250
                [
1251
                    '/jedan/two/drei',
1252
                ],
1253
            ],
1254
            [
1255
                $spiUrlAliases3,
1256
                'eng-GB',
1257
                ['ger-DE', 'eng-GB'],
1258
                [
1259
                    '/jedan/two/three',
1260
                ],
1261
            ],
1262
            [
1263
                $spiUrlAliases3,
1264
                'ger-DE',
1265
                ['ger-DE', 'cro-HR'],
1266
                [
1267
                    '/jedan/dva/drei',
1268
                ],
1269
            ],
1270
            [
1271
                $spiUrlAliases3,
1272
                'ger-DE',
1273
                ['cro-HR', 'eng-GB', 'ger-DE'],
1274
                [
1275
                    '/jedan/dva/drei',
1276
                ],
1277
            ],
1278
            [
1279
                $spiUrlAliases3,
1280
                'ger-DE',
1281
                ['cro-HR', 'ger-DE', 'eng-GB'],
1282
                [
1283
                    '/jedan/dva/drei',
1284
                ],
1285
            ],
1286
            [
1287
                $spiUrlAliases3,
1288
                'ger-DE',
1289
                ['eng-GB', 'cro-HR', 'ger-DE'],
1290
                [
1291
                    '/jedan/two/drei',
1292
                ],
1293
            ],
1294
            [
1295
                $spiUrlAliases3,
1296
                'ger-DE',
1297
                ['eng-GB', 'ger-DE', 'cro-HR'],
1298
                [
1299
                    '/jedan/two/drei',
1300
                ],
1301
            ],
1302
            [
1303
                $spiUrlAliases3,
1304
                'eng-GB',
1305
                ['ger-DE', 'cro-HR', 'eng-GB'],
1306
                [
1307
                    '/jedan/dva/three',
1308
                ],
1309
            ],
1310
            [
1311
                $spiUrlAliases3,
1312
                'cro-HR',
1313
                ['ger-DE', 'eng-GB', 'cro-HR'],
1314
                [
1315
                    '/jedan/two/tri',
1316
                ],
1317
            ],
1318
        ];
1319
    }
1320
1321
    /**
1322
     * Test for the listLocationAliases() method.
1323
     *
1324
     * @dataProvider providerForTestListAutogeneratedLocationAliasesWithLanguageCodePath
1325
     */
1326
    public function testListAutogeneratedLocationAliasesWithLanguageCodePath(
1327
        $spiUrlAliases,
1328
        $languageCode,
1329
        $prioritizedLanguageCodes,
1330
        $paths
1331
    ) {
1332
        $urlAliasService = $this->getRepository()->getURLAliasService();
1333
        $configuration = [
1334
            'prioritizedLanguageList' => $prioritizedLanguageCodes,
1335
            'showAllTranslations' => false,
1336
        ];
1337
        $this->setConfiguration($urlAliasService, $configuration);
1338
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
1339
        $urlAliasHandler->expects(
1340
            $this->once()
1341
        )->method(
1342
            'listURLAliasesForLocation'
1343
        )->with(
1344
            $this->equalTo(42),
1345
            $this->equalTo(false)
1346
        )->will(
1347
            $this->returnValue($spiUrlAliases)
1348
        );
1349
1350
        $location = $this->getLocationStub();
1351
        $urlAliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
1352
1353
        self::assertEquals(
1354
            count($paths),
1355
            count($urlAliases)
1356
        );
1357
1358
        foreach ($urlAliases as $index => $urlAlias) {
1359
            self::assertEquals(
1360
                $paths[$index],
1361
                $urlAlias->path
1362
            );
1363
        }
1364
    }
1365
1366
    /**
1367
     * Test for the listLocationAliases() method.
1368
     *
1369
     * @dataProvider providerForTestListAutogeneratedLocationAliasesWithLanguageCodePath
1370
     */
1371 View Code Duplication
    public function testListAutogeneratedLocationAliasesWithLanguageCodePathCustomConfiguration(
1372
        $spiUrlAliases,
1373
        $languageCode,
1374
        $prioritizedLanguageCodes,
1375
        $paths
1376
    ) {
1377
        $urlAliasService = $this->getRepository()->getURLAliasService();
1378
        $configuration = [
1379
            'prioritizedLanguageList' => [],
1380
            'showAllTranslations' => false,
1381
        ];
1382
        $this->setConfiguration($urlAliasService, $configuration);
1383
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
1384
        $urlAliasHandler->expects(
1385
            $this->once()
1386
        )->method(
1387
            'listURLAliasesForLocation'
1388
        )->with(
1389
            $this->equalTo(42),
1390
            $this->equalTo(false)
1391
        )->will(
1392
            $this->returnValue($spiUrlAliases)
1393
        );
1394
1395
        $location = $this->getLocationStub();
1396
        $urlAliases = $urlAliasService->listLocationAliases(
1397
            $location,
1398
            false,
1399
            $languageCode,
1400
            false,
1401
            $prioritizedLanguageCodes
1402
        );
1403
1404
        self::assertEquals(
1405
            count($paths),
1406
            count($urlAliases)
1407
        );
1408
1409
        foreach ($urlAliases as $index => $urlAlias) {
1410
            self::assertEquals(
1411
                $paths[$index],
1412
                $urlAlias->path
1413
            );
1414
        }
1415
    }
1416
1417
    public function providerForTestListAutogeneratedLocationAliasesWithLanguageCodeEmpty()
1418
    {
1419
        $pathElement1 = [
1420
            'always-available' => true,
1421
            'translations' => [
1422
                'cro-HR' => '/jedan',
1423
            ],
1424
        ];
1425
        $pathElement2 = [
1426
            'always-available' => false,
1427
            'translations' => [
1428
                'cro-HR' => 'dva',
1429
                'eng-GB' => 'two',
1430
            ],
1431
        ];
1432
        $pathElement3 = [
1433
            'always-available' => false,
1434
            'translations' => [
1435
                'cro-HR' => 'tri',
1436
                'eng-GB' => 'three',
1437
                'ger-DE' => 'drei',
1438
            ],
1439
        ];
1440
        $pathData1 = [$pathElement1];
1441
        $pathData2 = [$pathElement1, $pathElement2];
1442
        $pathData3 = [$pathElement1, $pathElement2, $pathElement3];
1443
        $spiUrlAliases1 = [
1444
            new SPIUrlAlias(
1445
                [
1446
                    'pathData' => $pathData1,
1447
                    'languageCodes' => ['cro-HR'],
1448
                    'alwaysAvailable' => true,
1449
                ]
1450
            ),
1451
        ];
1452
        $spiUrlAliases2 = [
1453
            new SPIUrlAlias(
1454
                [
1455
                    'pathData' => $pathData2,
1456
                    'languageCodes' => ['cro-HR'],
1457
                    'alwaysAvailable' => false,
1458
                ]
1459
            ),
1460
            new SPIUrlAlias(
1461
                [
1462
                    'pathData' => $pathData2,
1463
                    'languageCodes' => ['eng-GB'],
1464
                    'alwaysAvailable' => false,
1465
                ]
1466
            ),
1467
        ];
1468
        $spiUrlAliases3 = [
1469
            new SPIUrlAlias(
1470
                [
1471
                    'pathData' => $pathData3,
1472
                    'languageCodes' => ['cro-HR'],
1473
                    'alwaysAvailable' => false,
1474
                ]
1475
            ),
1476
            new SPIUrlAlias(
1477
                [
1478
                    'pathData' => $pathData3,
1479
                    'languageCodes' => ['eng-GB'],
1480
                    'alwaysAvailable' => false,
1481
                ]
1482
            ),
1483
            new SPIUrlAlias(
1484
                [
1485
                    'pathData' => $pathData3,
1486
                    'languageCodes' => ['ger-DE'],
1487
                    'alwaysAvailable' => false,
1488
                ]
1489
            ),
1490
        ];
1491
1492
        return [
1493
            [
1494
                $spiUrlAliases1,
1495
                'eng-GB',
1496
                ['ger-DE'],
1497
            ],
1498
            [
1499
                $spiUrlAliases1,
1500
                'ger-DE',
1501
                ['cro-HR', 'eng-GB', 'ger-DE'],
1502
            ],
1503
            [
1504
                $spiUrlAliases2,
1505
                'eng-GB',
1506
                ['cro-HR'],
1507
            ],
1508
            [
1509
                $spiUrlAliases2,
1510
                'ger-DE',
1511
                ['cro-HR', 'eng-GB'],
1512
            ],
1513
            [
1514
                $spiUrlAliases2,
1515
                'ger-DE',
1516
                ['cro-HR', 'ger-DE'],
1517
            ],
1518
            [
1519
                $spiUrlAliases2,
1520
                'ger-DE',
1521
                ['eng-GB', 'ger-DE'],
1522
            ],
1523
            [
1524
                $spiUrlAliases2,
1525
                'ger-DE',
1526
                ['ger-DE', 'cro-HR'],
1527
            ],
1528
            [
1529
                $spiUrlAliases2,
1530
                'ger-DE',
1531
                ['ger-DE', 'eng-GB'],
1532
            ],
1533
            [
1534
                $spiUrlAliases2,
1535
                'ger-DE',
1536
                ['cro-HR', 'eng-GB', 'ger-DE'],
1537
            ],
1538
            [
1539
                $spiUrlAliases2,
1540
                'ger-DE',
1541
                ['cro-HR', 'ger-DE', 'eng-GB'],
1542
            ],
1543
            [
1544
                $spiUrlAliases2,
1545
                'ger-DE',
1546
                ['eng-GB', 'cro-HR', 'ger-DE'],
1547
            ],
1548
            [
1549
                $spiUrlAliases2,
1550
                'ger-DE',
1551
                ['eng-GB', 'ger-DE', 'cro-HR'],
1552
            ],
1553
            [
1554
                $spiUrlAliases2,
1555
                'ger-DE',
1556
                ['ger-DE', 'cro-HR', 'eng-GB'],
1557
            ],
1558
            [
1559
                $spiUrlAliases2,
1560
                'ger-DE',
1561
                ['ger-DE', 'eng-GB', 'cro-HR'],
1562
            ],
1563
            [
1564
                $spiUrlAliases3,
1565
                'ger-DE',
1566
                ['cro-HR'],
1567
            ],
1568
            [
1569
                $spiUrlAliases3,
1570
                'cro-HR',
1571
                ['eng-GB'],
1572
            ],
1573
            [
1574
                $spiUrlAliases3,
1575
                'ger-DE',
1576
                ['cro-HR', 'eng-GB'],
1577
            ],
1578
            [
1579
                $spiUrlAliases3,
1580
                'eng-GB',
1581
                ['cro-HR', 'ger-DE'],
1582
            ],
1583
            [
1584
                $spiUrlAliases3,
1585
                'ger-DE',
1586
                ['eng-GB', 'cro-HR'],
1587
            ],
1588
            [
1589
                $spiUrlAliases3,
1590
                'cro-HR',
1591
                ['eng-GB', 'ger-DE'],
1592
            ],
1593
            [
1594
                $spiUrlAliases3,
1595
                'cro-HR',
1596
                ['ger-DE', 'eng-GB'],
1597
            ],
1598
            [
1599
                $spiUrlAliases3,
1600
                'eng-GB',
1601
                ['ger-DE', 'cro-HR'],
1602
            ],
1603
        ];
1604
    }
1605
1606
    /**
1607
     * Test for the listLocationAliases() method.
1608
     *
1609
     * @dataProvider providerForTestListAutogeneratedLocationAliasesWithLanguageCodeEmpty
1610
     */
1611 View Code Duplication
    public function testListAutogeneratedLocationAliasesWithLanguageCodeEmpty(
1612
        $spiUrlAliases,
1613
        $languageCode,
1614
        $prioritizedLanguageCodes
1615
    ) {
1616
        $urlAliasService = $this->getRepository()->getURLAliasService();
1617
        $configuration = [
1618
            'prioritizedLanguageList' => $prioritizedLanguageCodes,
1619
            'showAllTranslations' => false,
1620
        ];
1621
        $this->setConfiguration($urlAliasService, $configuration);
1622
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
1623
        $urlAliasHandler->expects(
1624
            $this->once()
1625
        )->method(
1626
            'listURLAliasesForLocation'
1627
        )->with(
1628
            $this->equalTo(42),
1629
            $this->equalTo(false)
1630
        )->will(
1631
            $this->returnValue($spiUrlAliases)
1632
        );
1633
1634
        $location = $this->getLocationStub();
1635
        $urlAliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
1636
1637
        self::assertEmpty($urlAliases);
1638
    }
1639
1640
    /**
1641
     * Test for the listLocationAliases() method.
1642
     *
1643
     * @dataProvider providerForTestListAutogeneratedLocationAliasesWithLanguageCodeEmpty
1644
     */
1645 View Code Duplication
    public function testListAutogeneratedLocationAliasesWithLanguageCodeEmptyCustomConfiguration(
1646
        $spiUrlAliases,
1647
        $languageCode,
1648
        $prioritizedLanguageCodes
1649
    ) {
1650
        $urlAliasService = $this->getRepository()->getURLAliasService();
1651
        $configuration = [
1652
            'prioritizedLanguageList' => [],
1653
            'showAllTranslations' => false,
1654
        ];
1655
        $this->setConfiguration($urlAliasService, $configuration);
1656
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
1657
        $urlAliasHandler->expects(
1658
            $this->once()
1659
        )->method(
1660
            'listURLAliasesForLocation'
1661
        )->with(
1662
            $this->equalTo(42),
1663
            $this->equalTo(false)
1664
        )->will(
1665
            $this->returnValue($spiUrlAliases)
1666
        );
1667
1668
        $location = $this->getLocationStub();
1669
        $urlAliases = $urlAliasService->listLocationAliases(
1670
            $location,
1671
            false,
1672
            $languageCode,
1673
            false,
1674
            $prioritizedLanguageCodes
1675
        );
1676
1677
        self::assertEmpty($urlAliases);
1678
    }
1679
1680
    public function providerForTestListAutogeneratedLocationAliasesMultipleLanguagesPath()
1681
    {
1682
        $spiUrlAliases = [
1683
            new SPIUrlAlias(
1684
                [
1685
                    'pathData' => [
1686
                        [
1687
                            'always-available' => false,
1688
                            'translations' => [
1689
                                'cro-HR' => 'jedan',
1690
                                'eng-GB' => 'jedan',
1691
                            ],
1692
                        ],
1693
                        [
1694
                            'always-available' => false,
1695
                            'translations' => [
1696
                                'eng-GB' => 'dva',
1697
                                'ger-DE' => 'dva',
1698
                            ],
1699
                        ],
1700
                    ],
1701
                    'languageCodes' => ['eng-GB', 'ger-DE'],
1702
                    'alwaysAvailable' => false,
1703
                ]
1704
            ),
1705
        ];
1706
1707
        return [
1708
            [
1709
                $spiUrlAliases,
1710
                ['cro-HR', 'ger-DE'],
1711
                [
1712
                    '/jedan/dva',
1713
                ],
1714
            ],
1715
            [
1716
                $spiUrlAliases,
1717
                ['ger-DE', 'cro-HR'],
1718
                [
1719
                    '/jedan/dva',
1720
                ],
1721
            ],
1722
            [
1723
                $spiUrlAliases,
1724
                ['eng-GB'],
1725
                [
1726
                    '/jedan/dva',
1727
                ],
1728
            ],
1729
            [
1730
                $spiUrlAliases,
1731
                ['eng-GB', 'ger-DE', 'cro-HR'],
1732
                [
1733
                    '/jedan/dva',
1734
                ],
1735
            ],
1736
        ];
1737
    }
1738
1739
    /**
1740
     * Test for the listLocationAliases() method.
1741
     *
1742
     * @dataProvider providerForTestListAutogeneratedLocationAliasesMultipleLanguagesPath
1743
     */
1744 View Code Duplication
    public function testListAutogeneratedLocationAliasesMultipleLanguagesPath($spiUrlAliases, $prioritizedLanguageCodes, $paths)
1745
    {
1746
        $urlAliasService = $this->getRepository()->getURLAliasService();
1747
        $configuration = [
1748
            'prioritizedLanguageList' => $prioritizedLanguageCodes,
1749
            'showAllTranslations' => false,
1750
        ];
1751
        $this->setConfiguration($urlAliasService, $configuration);
1752
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
1753
        $urlAliasHandler->expects(
1754
            $this->once()
1755
        )->method(
1756
            'listURLAliasesForLocation'
1757
        )->with(
1758
            $this->equalTo(42),
1759
            $this->equalTo(false)
1760
        )->will(
1761
            $this->returnValue($spiUrlAliases)
1762
        );
1763
1764
        $location = $this->getLocationStub();
1765
        $urlAliases = $urlAliasService->listLocationAliases($location, false, null);
1766
1767
        self::assertEquals(
1768
            count($paths),
1769
            count($urlAliases)
1770
        );
1771
1772
        foreach ($urlAliases as $index => $urlAlias) {
1773
            self::assertEquals(
1774
                $paths[$index],
1775
                $urlAlias->path
1776
            );
1777
        }
1778
    }
1779
1780
    /**
1781
     * Test for the listLocationAliases() method.
1782
     *
1783
     * @dataProvider providerForTestListAutogeneratedLocationAliasesMultipleLanguagesPath
1784
     */
1785 View Code Duplication
    public function testListAutogeneratedLocationAliasesMultipleLanguagesPathCustomConfiguration(
1786
        $spiUrlAliases,
1787
        $prioritizedLanguageCodes,
1788
        $paths
1789
    ) {
1790
        $urlAliasService = $this->getRepository()->getURLAliasService();
1791
        $configuration = [
1792
            'prioritizedLanguageList' => [],
1793
            'showAllTranslations' => false,
1794
        ];
1795
        $this->setConfiguration($urlAliasService, $configuration);
1796
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
1797
        $urlAliasHandler->expects(
1798
            $this->once()
1799
        )->method(
1800
            'listURLAliasesForLocation'
1801
        )->with(
1802
            $this->equalTo(42),
1803
            $this->equalTo(false)
1804
        )->will(
1805
            $this->returnValue($spiUrlAliases)
1806
        );
1807
1808
        $location = $this->getLocationStub();
1809
        $urlAliases = $urlAliasService->listLocationAliases(
1810
            $location,
1811
            false,
1812
            null,
1813
            false,
1814
            $prioritizedLanguageCodes
1815
        );
1816
1817
        self::assertEquals(
1818
            count($paths),
1819
            count($urlAliases)
1820
        );
1821
1822
        foreach ($urlAliases as $index => $urlAlias) {
1823
            self::assertEquals(
1824
                $paths[$index],
1825
                $urlAlias->path
1826
            );
1827
        }
1828
    }
1829
1830
    public function providerForTestListAutogeneratedLocationAliasesMultipleLanguagesEmpty()
1831
    {
1832
        $spiUrlAliases = [
1833
            new SPIUrlAlias(
1834
                [
1835
                    'pathData' => [
1836
                        [
1837
                            'always-available' => false,
1838
                            'translations' => [
1839
                                'cro-HR' => '/jedan',
1840
                                'eng-GB' => '/jedan',
1841
                            ],
1842
                        ],
1843
                        [
1844
                            'always-available' => false,
1845
                            'translations' => [
1846
                                'eng-GB' => 'dva',
1847
                                'ger-DE' => 'dva',
1848
                            ],
1849
                        ],
1850
                    ],
1851
                    'languageCodes' => ['eng-GB', 'ger-DE'],
1852
                    'alwaysAvailable' => false,
1853
                ]
1854
            ),
1855
        ];
1856
1857
        return [
1858
            [
1859
                $spiUrlAliases,
1860
                ['cro-HR'],
1861
            ],
1862
            [
1863
                $spiUrlAliases,
1864
                ['ger-DE'],
1865
            ],
1866
        ];
1867
    }
1868
1869
    /**
1870
     * Test for the listLocationAliases() method.
1871
     *
1872
     * @dataProvider providerForTestListAutogeneratedLocationAliasesMultipleLanguagesEmpty
1873
     */
1874 View Code Duplication
    public function testListAutogeneratedLocationAliasesMultipleLanguagesEmpty($spiUrlAliases, $prioritizedLanguageCodes)
1875
    {
1876
        $urlAliasService = $this->getRepository()->getURLAliasService();
1877
        $configuration = [
1878
            'prioritizedLanguageList' => $prioritizedLanguageCodes,
1879
            'showAllTranslations' => false,
1880
        ];
1881
        $this->setConfiguration($urlAliasService, $configuration);
1882
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
1883
        $urlAliasHandler->expects(
1884
            $this->once()
1885
        )->method(
1886
            'listURLAliasesForLocation'
1887
        )->with(
1888
            $this->equalTo(42),
1889
            $this->equalTo(false)
1890
        )->will(
1891
            $this->returnValue($spiUrlAliases)
1892
        );
1893
1894
        $location = $this->getLocationStub();
1895
        $urlAliases = $urlAliasService->listLocationAliases($location, false, null);
1896
1897
        self::assertEmpty($urlAliases);
1898
    }
1899
1900
    /**
1901
     * Test for the listLocationAliases() method.
1902
     *
1903
     * @dataProvider providerForTestListAutogeneratedLocationAliasesMultipleLanguagesEmpty
1904
     */
1905 View Code Duplication
    public function testListAutogeneratedLocationAliasesMultipleLanguagesEmptyCustomConfiguration(
1906
        $spiUrlAliases,
1907
        $prioritizedLanguageCodes
1908
    ) {
1909
        $urlAliasService = $this->getRepository()->getURLAliasService();
1910
        $configuration = [
1911
            'prioritizedLanguageList' => [],
1912
            'showAllTranslations' => false,
1913
        ];
1914
        $this->setConfiguration($urlAliasService, $configuration);
1915
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
1916
        $urlAliasHandler->expects(
1917
            $this->once()
1918
        )->method(
1919
            'listURLAliasesForLocation'
1920
        )->with(
1921
            $this->equalTo(42),
1922
            $this->equalTo(false)
1923
        )->will(
1924
            $this->returnValue($spiUrlAliases)
1925
        );
1926
1927
        $location = $this->getLocationStub();
1928
        $urlAliases = $urlAliasService->listLocationAliases(
1929
            $location,
1930
            false,
1931
            null,
1932
            false,
1933
            $prioritizedLanguageCodes
1934
        );
1935
1936
        self::assertEmpty($urlAliases);
1937
    }
1938
1939
    public function providerForTestListAutogeneratedLocationAliasesWithLanguageCodeMultipleLanguagesPath()
1940
    {
1941
        $spiUrlAliases = [
1942
            new SPIUrlAlias(
1943
                [
1944
                    'pathData' => [
1945
                        [
1946
                            'always-available' => false,
1947
                            'translations' => [
1948
                                'cro-HR' => 'jedan',
1949
                                'eng-GB' => 'jedan',
1950
                            ],
1951
                        ],
1952
                        [
1953
                            'always-available' => false,
1954
                            'translations' => [
1955
                                'eng-GB' => 'dva',
1956
                                'ger-DE' => 'dva',
1957
                            ],
1958
                        ],
1959
                    ],
1960
                    'languageCodes' => ['eng-GB', 'ger-DE'],
1961
                    'alwaysAvailable' => false,
1962
                ]
1963
            ),
1964
        ];
1965
1966
        return [
1967
            [
1968
                $spiUrlAliases,
1969
                'ger-DE',
1970
                ['cro-HR', 'ger-DE'],
1971
                [
1972
                    '/jedan/dva',
1973
                ],
1974
            ],
1975
            [
1976
                $spiUrlAliases,
1977
                'ger-DE',
1978
                ['ger-DE', 'cro-HR'],
1979
                [
1980
                    '/jedan/dva',
1981
                ],
1982
            ],
1983
            [
1984
                $spiUrlAliases,
1985
                'eng-GB',
1986
                ['eng-GB'],
1987
                [
1988
                    '/jedan/dva',
1989
                ],
1990
            ],
1991
            [
1992
                $spiUrlAliases,
1993
                'eng-GB',
1994
                ['eng-GB', 'ger-DE', 'cro-HR'],
1995
                [
1996
                    '/jedan/dva',
1997
                ],
1998
            ],
1999
        ];
2000
    }
2001
2002
    /**
2003
     * Test for the listLocationAliases() method.
2004
     *
2005
     * @dataProvider providerForTestListAutogeneratedLocationAliasesWithLanguageCodeMultipleLanguagesPath
2006
     */
2007
    public function testListAutogeneratedLocationAliasesWithLanguageCodeMultipleLanguagesPath(
2008
        $spiUrlAliases,
2009
        $languageCode,
2010
        $prioritizedLanguageCodes,
2011
        $paths
2012
    ) {
2013
        $urlAliasService = $this->getRepository()->getURLAliasService();
2014
        $configuration = [
2015
            'prioritizedLanguageList' => $prioritizedLanguageCodes,
2016
            'showAllTranslations' => false,
2017
        ];
2018
        $this->setConfiguration($urlAliasService, $configuration);
2019
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2020
        $urlAliasHandler->expects(
2021
            $this->once()
2022
        )->method(
2023
            'listURLAliasesForLocation'
2024
        )->with(
2025
            $this->equalTo(42),
2026
            $this->equalTo(false)
2027
        )->will(
2028
            $this->returnValue($spiUrlAliases)
2029
        );
2030
2031
        $location = $this->getLocationStub();
2032
        $urlAliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
2033
2034
        self::assertEquals(
2035
            count($paths),
2036
            count($urlAliases)
2037
        );
2038
2039
        foreach ($urlAliases as $index => $urlAlias) {
2040
            self::assertEquals(
2041
                $paths[$index],
2042
                $urlAlias->path
2043
            );
2044
        }
2045
    }
2046
2047
    /**
2048
     * Test for the listLocationAliases() method.
2049
     *
2050
     * @dataProvider providerForTestListAutogeneratedLocationAliasesWithLanguageCodeMultipleLanguagesPath
2051
     */
2052 View Code Duplication
    public function testListAutogeneratedLocationAliasesWithLanguageCodeMultipleLanguagesPathCustomConfiguration(
2053
        $spiUrlAliases,
2054
        $languageCode,
2055
        $prioritizedLanguageCodes,
2056
        $paths
2057
    ) {
2058
        $urlAliasService = $this->getRepository()->getURLAliasService();
2059
        $configuration = [
2060
            'prioritizedLanguageList' => [],
2061
            'showAllTranslations' => false,
2062
        ];
2063
        $this->setConfiguration($urlAliasService, $configuration);
2064
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2065
        $urlAliasHandler->expects(
2066
            $this->once()
2067
        )->method(
2068
            'listURLAliasesForLocation'
2069
        )->with(
2070
            $this->equalTo(42),
2071
            $this->equalTo(false)
2072
        )->will(
2073
            $this->returnValue($spiUrlAliases)
2074
        );
2075
2076
        $location = $this->getLocationStub();
2077
        $urlAliases = $urlAliasService->listLocationAliases(
2078
            $location,
2079
            false,
2080
            $languageCode,
2081
            false,
2082
            $prioritizedLanguageCodes
2083
        );
2084
2085
        self::assertEquals(
2086
            count($paths),
2087
            count($urlAliases)
2088
        );
2089
2090
        foreach ($urlAliases as $index => $urlAlias) {
2091
            self::assertEquals(
2092
                $paths[$index],
2093
                $urlAlias->path
2094
            );
2095
        }
2096
    }
2097
2098
    public function providerForTestListAutogeneratedLocationAliasesWithLanguageCodeMultipleLanguagesEmpty()
2099
    {
2100
        $spiUrlAliases = [
2101
            new SPIUrlAlias(
2102
                [
2103
                    'pathData' => [
2104
                        [
2105
                            'always-available' => false,
2106
                            'translations' => [
2107
                                'cro-HR' => '/jedan',
2108
                                'eng-GB' => '/jedan',
2109
                            ],
2110
                        ],
2111
                        [
2112
                            'always-available' => false,
2113
                            'translations' => [
2114
                                'eng-GB' => 'dva',
2115
                                'ger-DE' => 'dva',
2116
                            ],
2117
                        ],
2118
                    ],
2119
                    'languageCodes' => ['eng-GB', 'ger-DE'],
2120
                    'alwaysAvailable' => false,
2121
                ]
2122
            ),
2123
        ];
2124
2125
        return [
2126
            [
2127
                $spiUrlAliases,
2128
                'cro-HR',
2129
                ['cro-HR'],
2130
            ],
2131
            [
2132
                $spiUrlAliases,
2133
                'cro-HR',
2134
                ['cro-HR', 'eng-GB'],
2135
            ],
2136
            [
2137
                $spiUrlAliases,
2138
                'cro-HR',
2139
                ['ger-DE'],
2140
            ],
2141
            [
2142
                $spiUrlAliases,
2143
                'cro-HR',
2144
                ['cro-HR', 'eng-GB', 'ger-DE'],
2145
            ],
2146
        ];
2147
    }
2148
2149
    /**
2150
     * Test for the listLocationAliases() method.
2151
     *
2152
     * @dataProvider providerForTestListAutogeneratedLocationAliasesWithLanguageCodeMultipleLanguagesEmpty
2153
     */
2154 View Code Duplication
    public function testListAutogeneratedLocationAliasesWithLanguageCodeMultipleLanguagesEmpty(
2155
        $spiUrlAliases,
2156
        $languageCode,
2157
        $prioritizedLanguageCodes
2158
    ) {
2159
        $urlAliasService = $this->getRepository()->getURLAliasService();
2160
        $configuration = [
2161
            'prioritizedLanguageList' => $prioritizedLanguageCodes,
2162
            'showAllTranslations' => false,
2163
        ];
2164
        $this->setConfiguration($urlAliasService, $configuration);
2165
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2166
        $urlAliasHandler->expects(
2167
            $this->once()
2168
        )->method(
2169
            'listURLAliasesForLocation'
2170
        )->with(
2171
            $this->equalTo(42),
2172
            $this->equalTo(false)
2173
        )->will(
2174
            $this->returnValue($spiUrlAliases)
2175
        );
2176
2177
        $location = $this->getLocationStub();
2178
        $urlAliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
2179
2180
        self::assertEmpty($urlAliases);
2181
    }
2182
2183
    /**
2184
     * Test for the listLocationAliases() method.
2185
     *
2186
     * @dataProvider providerForTestListAutogeneratedLocationAliasesWithLanguageCodeMultipleLanguagesEmpty
2187
     */
2188 View Code Duplication
    public function testListAutogeneratedLocationAliasesWithLanguageCodeMultipleLanguagesEmptyCustomConfiguration(
2189
        $spiUrlAliases,
2190
        $languageCode,
2191
        $prioritizedLanguageCodes
2192
    ) {
2193
        $urlAliasService = $this->getRepository()->getURLAliasService();
2194
        $configuration = [
2195
            'prioritizedLanguageList' => [],
2196
            'showAllTranslations' => false,
2197
        ];
2198
        $this->setConfiguration($urlAliasService, $configuration);
2199
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2200
        $urlAliasHandler->expects(
2201
            $this->once()
2202
        )->method(
2203
            'listURLAliasesForLocation'
2204
        )->with(
2205
            $this->equalTo(42),
2206
            $this->equalTo(false)
2207
        )->will(
2208
            $this->returnValue($spiUrlAliases)
2209
        );
2210
2211
        $location = $this->getLocationStub();
2212
        $urlAliases = $urlAliasService->listLocationAliases(
2213
            $location,
2214
            false,
2215
            $languageCode,
2216
            false,
2217
            $prioritizedLanguageCodes
2218
        );
2219
2220
        self::assertEmpty($urlAliases);
2221
    }
2222
2223
    public function providerForTestListAutogeneratedLocationAliasesAlwaysAvailablePath()
2224
    {
2225
        $spiUrlAliases = [
2226
            new SPIUrlAlias(
2227
                [
2228
                    'pathData' => [
2229
                        [
2230
                            'always-available' => false,
2231
                            'translations' => [
2232
                                'cro-HR' => 'jedan',
2233
                                'eng-GB' => 'one',
2234
                            ],
2235
                        ],
2236
                        [
2237
                            'always-available' => true,
2238
                            'translations' => [
2239
                                'ger-DE' => 'zwei',
2240
                            ],
2241
                        ],
2242
                    ],
2243
                    'languageCodes' => ['ger-DE'],
2244
                    'alwaysAvailable' => true,
2245
                ]
2246
            ),
2247
        ];
2248
2249
        return [
2250
            [
2251
                $spiUrlAliases,
2252
                ['cro-HR', 'ger-DE'],
2253
                [
2254
                    '/jedan/zwei',
2255
                ],
2256
            ],
2257
            [
2258
                $spiUrlAliases,
2259
                ['ger-DE', 'cro-HR'],
2260
                [
2261
                    '/jedan/zwei',
2262
                ],
2263
            ],
2264
            [
2265
                $spiUrlAliases,
2266
                ['eng-GB'],
2267
                [
2268
                    '/one/zwei',
2269
                ],
2270
            ],
2271
            [
2272
                $spiUrlAliases,
2273
                ['cro-HR', 'eng-GB', 'ger-DE'],
2274
                [
2275
                    '/jedan/zwei',
2276
                ],
2277
            ],
2278
            [
2279
                $spiUrlAliases,
2280
                ['eng-GB', 'ger-DE', 'cro-HR'],
2281
                [
2282
                    '/one/zwei',
2283
                ],
2284
            ],
2285
        ];
2286
    }
2287
2288
    /**
2289
     * Test for the listLocationAliases() method.
2290
     *
2291
     * @dataProvider providerForTestListAutogeneratedLocationAliasesAlwaysAvailablePath
2292
     */
2293 View Code Duplication
    public function testListAutogeneratedLocationAliasesAlwaysAvailablePath(
2294
        $spiUrlAliases,
2295
        $prioritizedLanguageCodes,
2296
        $paths
2297
    ) {
2298
        $urlAliasService = $this->getRepository()->getURLAliasService();
2299
        $configuration = [
2300
            'prioritizedLanguageList' => $prioritizedLanguageCodes,
2301
            'showAllTranslations' => false,
2302
        ];
2303
        $this->setConfiguration($urlAliasService, $configuration);
2304
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2305
        $urlAliasHandler->expects(
2306
            $this->once()
2307
        )->method(
2308
            'listURLAliasesForLocation'
2309
        )->with(
2310
            $this->equalTo(42),
2311
            $this->equalTo(false)
2312
        )->will(
2313
            $this->returnValue($spiUrlAliases)
2314
        );
2315
2316
        $location = $this->getLocationStub();
2317
        $urlAliases = $urlAliasService->listLocationAliases($location, false, null);
2318
2319
        self::assertEquals(
2320
            count($paths),
2321
            count($urlAliases)
2322
        );
2323
2324
        foreach ($urlAliases as $index => $urlAlias) {
2325
            self::assertEquals(
2326
                $paths[$index],
2327
                $urlAlias->path
2328
            );
2329
        }
2330
    }
2331
2332
    /**
2333
     * Test for the listLocationAliases() method.
2334
     *
2335
     * @dataProvider providerForTestListAutogeneratedLocationAliasesAlwaysAvailablePath
2336
     */
2337 View Code Duplication
    public function testListAutogeneratedLocationAliasesAlwaysAvailablePathCustomConfiguration(
2338
        $spiUrlAliases,
2339
        $prioritizedLanguageCodes,
2340
        $paths
2341
    ) {
2342
        $urlAliasService = $this->getRepository()->getURLAliasService();
2343
        $configuration = [
2344
            'prioritizedLanguageList' => [],
2345
            'showAllTranslations' => false,
2346
        ];
2347
        $this->setConfiguration($urlAliasService, $configuration);
2348
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2349
        $urlAliasHandler->expects(
2350
            $this->once()
2351
        )->method(
2352
            'listURLAliasesForLocation'
2353
        )->with(
2354
            $this->equalTo(42),
2355
            $this->equalTo(false)
2356
        )->will(
2357
            $this->returnValue($spiUrlAliases)
2358
        );
2359
2360
        $location = $this->getLocationStub();
2361
        $urlAliases = $urlAliasService->listLocationAliases(
2362
            $location,
2363
            false,
2364
            null,
2365
            false,
2366
            $prioritizedLanguageCodes
2367
        );
2368
2369
        self::assertEquals(
2370
            count($paths),
2371
            count($urlAliases)
2372
        );
2373
2374
        foreach ($urlAliases as $index => $urlAlias) {
2375
            self::assertEquals(
2376
                $paths[$index],
2377
                $urlAlias->path
2378
            );
2379
        }
2380
    }
2381
2382
    public function providerForTestListAutogeneratedLocationAliasesWithLanguageCodeAlwaysAvailablePath()
2383
    {
2384
        $spiUrlAliases = [
2385
            new SPIUrlAlias(
2386
                [
2387
                    'pathData' => [
2388
                        [
2389
                            'always-available' => false,
2390
                            'translations' => [
2391
                                'cro-HR' => 'jedan',
2392
                                'eng-GB' => 'one',
2393
                            ],
2394
                        ],
2395
                        [
2396
                            'always-available' => true,
2397
                            'translations' => [
2398
                                'ger-DE' => 'zwei',
2399
                            ],
2400
                        ],
2401
                    ],
2402
                    'languageCodes' => ['ger-DE'],
2403
                    'alwaysAvailable' => true,
2404
                ]
2405
            ),
2406
        ];
2407
2408
        return [
2409
            [
2410
                $spiUrlAliases,
2411
                'ger-DE',
2412
                ['cro-HR', 'ger-DE'],
2413
                [
2414
                    '/jedan/zwei',
2415
                ],
2416
            ],
2417
            [
2418
                $spiUrlAliases,
2419
                'ger-DE',
2420
                ['ger-DE', 'cro-HR'],
2421
                [
2422
                    '/jedan/zwei',
2423
                ],
2424
            ],
2425
        ];
2426
    }
2427
2428
    /**
2429
     * Test for the listLocationAliases() method.
2430
     *
2431
     * @dataProvider providerForTestListAutogeneratedLocationAliasesWithLanguageCodeAlwaysAvailablePath
2432
     */
2433
    public function testListAutogeneratedLocationAliasesWithLanguageCodeAlwaysAvailablePath(
2434
        $spiUrlAliases,
2435
        $languageCode,
2436
        $prioritizedLanguageCodes,
2437
        $paths
2438
    ) {
2439
        $urlAliasService = $this->getRepository()->getURLAliasService();
2440
        $configuration = [
2441
            'prioritizedLanguageList' => $prioritizedLanguageCodes,
2442
            'showAllTranslations' => false,
2443
        ];
2444
        $this->setConfiguration($urlAliasService, $configuration);
2445
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2446
        $urlAliasHandler->expects(
2447
            $this->once()
2448
        )->method(
2449
            'listURLAliasesForLocation'
2450
        )->with(
2451
            $this->equalTo(42),
2452
            $this->equalTo(false)
2453
        )->will(
2454
            $this->returnValue($spiUrlAliases)
2455
        );
2456
2457
        $location = $this->getLocationStub();
2458
        $urlAliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
2459
2460
        self::assertEquals(
2461
            count($paths),
2462
            count($urlAliases)
2463
        );
2464
2465
        foreach ($urlAliases as $index => $urlAlias) {
2466
            self::assertEquals(
2467
                $paths[$index],
2468
                $urlAlias->path
2469
            );
2470
        }
2471
    }
2472
2473
    /**
2474
     * Test for the listLocationAliases() method.
2475
     *
2476
     * @dataProvider providerForTestListAutogeneratedLocationAliasesWithLanguageCodeAlwaysAvailablePath
2477
     */
2478 View Code Duplication
    public function testListAutogeneratedLocationAliasesWithLanguageCodeAlwaysAvailablePathCustomConfiguration(
2479
        $spiUrlAliases,
2480
        $languageCode,
2481
        $prioritizedLanguageCodes,
2482
        $paths
2483
    ) {
2484
        $urlAliasService = $this->getRepository()->getURLAliasService();
2485
        $configuration = [
2486
            'prioritizedLanguageList' => [],
2487
            'showAllTranslations' => false,
2488
        ];
2489
        $this->setConfiguration($urlAliasService, $configuration);
2490
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2491
        $urlAliasHandler->expects(
2492
            $this->once()
2493
        )->method(
2494
            'listURLAliasesForLocation'
2495
        )->with(
2496
            $this->equalTo(42),
2497
            $this->equalTo(false)
2498
        )->will(
2499
            $this->returnValue($spiUrlAliases)
2500
        );
2501
2502
        $location = $this->getLocationStub();
2503
        $urlAliases = $urlAliasService->listLocationAliases(
2504
            $location,
2505
            false,
2506
            $languageCode,
2507
            false,
2508
            $prioritizedLanguageCodes
2509
        );
2510
2511
        self::assertEquals(
2512
            count($paths),
2513
            count($urlAliases)
2514
        );
2515
2516
        foreach ($urlAliases as $index => $urlAlias) {
2517
            self::assertEquals(
2518
                $paths[$index],
2519
                $urlAlias->path
2520
            );
2521
        }
2522
    }
2523
2524
    public function providerForTestListAutogeneratedLocationAliasesWithLanguageCodeAlwaysAvailableEmpty()
2525
    {
2526
        $spiUrlAliases = [
2527
            new SPIUrlAlias(
2528
                [
2529
                    'pathData' => [
2530
                        [
2531
                            'always-available' => false,
2532
                            'translations' => [
2533
                                'cro-HR' => 'jedan',
2534
                                'eng-GB' => 'one',
2535
                            ],
2536
                        ],
2537
                        [
2538
                            'always-available' => true,
2539
                            'translations' => [
2540
                                'ger-DE' => 'zwei',
2541
                            ],
2542
                        ],
2543
                    ],
2544
                    'languageCodes' => ['ger-DE'],
2545
                    'alwaysAvailable' => true,
2546
                ]
2547
            ),
2548
        ];
2549
2550
        return [
2551
            [
2552
                $spiUrlAliases,
2553
                'eng-GB',
2554
                ['eng-GB'],
2555
            ],
2556
            [
2557
                $spiUrlAliases,
2558
                'eng-GB',
2559
                ['cro-HR', 'eng-GB', 'ger-DE'],
2560
            ],
2561
            [
2562
                $spiUrlAliases,
2563
                'eng-GB',
2564
                ['eng-GB', 'ger-DE', 'cro-HR'],
2565
            ],
2566
        ];
2567
    }
2568
2569
    /**
2570
     * Test for the listLocationAliases() method.
2571
     *
2572
     * @dataProvider providerForTestListAutogeneratedLocationAliasesWithLanguageCodeAlwaysAvailableEmpty
2573
     */
2574 View Code Duplication
    public function testListAutogeneratedLocationAliasesWithLanguageCodeAlwaysAvailableEmpty(
2575
        $spiUrlAliases,
2576
        $languageCode,
2577
        $prioritizedLanguageCodes
2578
    ) {
2579
        $urlAliasService = $this->getRepository()->getURLAliasService();
2580
        $configuration = [
2581
            'prioritizedLanguageList' => $prioritizedLanguageCodes,
2582
            'showAllTranslations' => false,
2583
        ];
2584
        $this->setConfiguration($urlAliasService, $configuration);
2585
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2586
        $urlAliasHandler->expects(
2587
            $this->once()
2588
        )->method(
2589
            'listURLAliasesForLocation'
2590
        )->with(
2591
            $this->equalTo(42),
2592
            $this->equalTo(false)
2593
        )->will(
2594
            $this->returnValue($spiUrlAliases)
2595
        );
2596
2597
        $location = $this->getLocationStub();
2598
        $urlAliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
2599
2600
        self::assertEmpty($urlAliases);
2601
    }
2602
2603
    /**
2604
     * Test for the listLocationAliases() method.
2605
     *
2606
     * @dataProvider providerForTestListAutogeneratedLocationAliasesWithLanguageCodeAlwaysAvailableEmpty
2607
     */
2608 View Code Duplication
    public function testListAutogeneratedLocationAliasesWithLanguageCodeAlwaysAvailableEmptyCustomConfiguration(
2609
        $spiUrlAliases,
2610
        $languageCode,
2611
        $prioritizedLanguageCodes
2612
    ) {
2613
        $urlAliasService = $this->getRepository()->getURLAliasService();
2614
        $configuration = [
2615
            'prioritizedLanguageList' => [],
2616
            'showAllTranslations' => false,
2617
        ];
2618
        $this->setConfiguration($urlAliasService, $configuration);
2619
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2620
        $urlAliasHandler->expects(
2621
            $this->once()
2622
        )->method(
2623
            'listURLAliasesForLocation'
2624
        )->with(
2625
            $this->equalTo(42),
2626
            $this->equalTo(false)
2627
        )->will(
2628
            $this->returnValue($spiUrlAliases)
2629
        );
2630
2631
        $location = $this->getLocationStub();
2632
        $urlAliases = $urlAliasService->listLocationAliases(
2633
            $location,
2634
            false,
2635
            $languageCode,
2636
            false,
2637
            $prioritizedLanguageCodes
2638
        );
2639
2640
        self::assertEmpty($urlAliases);
2641
    }
2642
2643
    /**
2644
     * Test for the listGlobalAliases() method.
2645
     */
2646 View Code Duplication
    public function testListGlobalAliases()
2647
    {
2648
        $urlAliasService = $this->getRepository()->getURLAliasService();
2649
        $configuration = [
2650
            'prioritizedLanguageList' => ['ger-DE'],
2651
            'showAllTranslations' => true,
2652
        ];
2653
        $this->setConfiguration($urlAliasService, $configuration);
2654
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2655
2656
        $urlAliasHandler->expects(
2657
            $this->once()
2658
        )->method(
2659
            'listGlobalURLAliases'
2660
        )->with(
2661
            $this->equalTo(null),
2662
            $this->equalTo(0),
2663
            $this->equalTo(-1)
2664
        )->will(
2665
            $this->returnValue(
2666
                [
2667
                    new SPIUrlAlias(
2668
                        [
2669
                            'pathData' => [
2670
                                [
2671
                                    'always-available' => true,
2672
                                    'translations' => [
2673
                                        'ger-DE' => 'squirrel',
2674
                                    ],
2675
                                ],
2676
                            ],
2677
                            'languageCodes' => ['ger-DE'],
2678
                            'alwaysAvailable' => true,
2679
                        ]
2680
                    ),
2681
                ]
2682
            )
2683
        );
2684
2685
        $urlAliases = $urlAliasService->listGlobalAliases();
2686
2687
        self::assertCount(1, $urlAliases);
2688
        self::assertInstanceOf(
2689
            'eZ\\Publish\\API\\Repository\\Values\\Content\\URLAlias',
2690
            $urlAliases[0]
2691
        );
2692
    }
2693
2694
    /**
2695
     * Test for the listGlobalAliases() method.
2696
     */
2697 View Code Duplication
    public function testListGlobalAliasesEmpty()
2698
    {
2699
        $urlAliasService = $this->getRepository()->getURLAliasService();
2700
        $configuration = [
2701
            'prioritizedLanguageList' => ['eng-GB'],
2702
            'showAllTranslations' => false,
2703
        ];
2704
        $this->setConfiguration($urlAliasService, $configuration);
2705
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2706
2707
        $urlAliasHandler->expects(
2708
            $this->once()
2709
        )->method(
2710
            'listGlobalURLAliases'
2711
        )->with(
2712
            $this->equalTo(null),
2713
            $this->equalTo(0),
2714
            $this->equalTo(-1)
2715
        )->will(
2716
            $this->returnValue(
2717
                [
2718
                    new SPIUrlAlias(
2719
                        [
2720
                            'pathData' => [
2721
                                [
2722
                                    'always-available' => false,
2723
                                    'translations' => [
2724
                                        'ger-DE' => 'squirrel',
2725
                                    ],
2726
                                ],
2727
                            ],
2728
                            'languageCodes' => ['ger-DE'],
2729
                            'alwaysAvailable' => false,
2730
                        ]
2731
                    ),
2732
                ]
2733
            )
2734
        );
2735
2736
        $urlAliases = $urlAliasService->listGlobalAliases();
2737
2738
        self::assertCount(0, $urlAliases);
2739
    }
2740
2741
    /**
2742
     * Test for the listGlobalAliases() method.
2743
     */
2744
    public function testListGlobalAliasesWithParameters()
2745
    {
2746
        $urlAliasService = $this->getRepository()->getURLAliasService();
2747
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2748
2749
        $urlAliasHandler->expects(
2750
            $this->once()
2751
        )->method(
2752
            'listGlobalURLAliases'
2753
        )->with(
2754
            $this->equalTo('languageCode'),
2755
            $this->equalTo('offset'),
2756
            $this->equalTo('limit')
2757
        )->will(
2758
            $this->returnValue([])
2759
        );
2760
2761
        $urlAliases = $urlAliasService->listGlobalAliases('languageCode', 'offset', 'limit');
2762
2763
        self::assertEmpty($urlAliases);
2764
    }
2765
2766
    /**
2767
     * Test for the lookup() method.
2768
     *
2769
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2770
     */
2771
    public function testLookupThrowsNotFoundException()
2772
    {
2773
        $urlAliasService = $this->getRepository()->getURLAliasService();
2774
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2775
2776
        $urlAliasHandler->expects(
2777
            $this->once()
2778
        )->method(
2779
            'lookup'
2780
        )->with(
2781
            $this->equalTo('url')
2782
        )->will(
2783
            $this->throwException(new NotFoundException('UrlAlias', 'url'))
2784
        );
2785
2786
        $urlAliasService->lookup('url');
2787
    }
2788
2789
    public function providerForTestLookupThrowsNotFoundExceptionPath()
2790
    {
2791
        return [
2792
            // alias does not exist in requested language
2793
            ['ein/dva', ['cro-HR', 'ger-DE'], 'ger-DE'],
2794
            // alias exists in requested language but the language is not in prioritized languages list
2795
            ['ein/dva', ['ger-DE'], 'eng-GB'],
2796
            // alias path is not matched
2797
            ['jedan/dva', ['cro-HR', 'ger-DE'], 'cro-HR'],
2798
            // path is not loadable for prioritized languages list
2799
            ['ein/dva', ['cro-HR'], 'cro-HR'],
2800
        ];
2801
    }
2802
2803
    /**
2804
     * Test for the lookup() method.
2805
     *
2806
     * @dataProvider providerForTestLookupThrowsNotFoundExceptionPath
2807
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2808
     */
2809
    public function testLookupThrowsNotFoundExceptionPathNotMatchedOrNotLoadable($url, $prioritizedLanguageList, $languageCode)
2810
    {
2811
        $urlAliasService = $this->getRepository()->getURLAliasService();
2812
        $configuration = [
2813
            'prioritizedLanguageList' => $prioritizedLanguageList,
2814
            'showAllTranslations' => false,
2815
        ];
2816
        $this->setConfiguration($urlAliasService, $configuration);
2817
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2818
2819
        $urlAliasHandler->expects(
2820
            $this->once()
2821
        )->method(
2822
            'lookup'
2823
        )->with(
2824
            $this->equalTo($url)
2825
        )->will(
2826
            $this->returnValue(
2827
                new SPIUrlAlias(
2828
                    [
2829
                        'pathData' => [
2830
                            [
2831
                                'always-available' => false,
2832
                                'translations' => ['ger-DE' => 'ein'],
2833
                            ],
2834
                            [
2835
                                'always-available' => false,
2836
                                'translations' => [
2837
                                    'cro-HR' => 'dva',
2838
                                    'eng-GB' => 'two',
2839
                                ],
2840
                            ],
2841
                        ],
2842
                        'languageCodes' => ['eng-GB', 'cro-HR'],
2843
                        'alwaysAvailable' => false,
2844
                    ]
2845
                )
2846
            )
2847
        );
2848
2849
        $urlAliasService->lookup($url, $languageCode);
2850
    }
2851
2852
    public function providerForTestLookup()
2853
    {
2854
        return [
2855
            // showAllTranslations setting is true
2856
            [['ger-DE'], true, false, null],
2857
            // alias is always available
2858
            [['ger-DE'], false, true, null],
2859
            // works with available language code
2860
            [['cro-HR'], false, false, 'eng-GB'],
2861
        ];
2862
    }
2863
2864
    /**
2865
     * Test for the lookup() method.
2866
     *
2867
     * @dataProvider providerForTestLookup
2868
     */
2869 View Code Duplication
    public function testLookup($prioritizedLanguageList, $showAllTranslations, $alwaysAvailable, $languageCode)
2870
    {
2871
        $urlAliasService = $this->getRepository()->getURLAliasService();
2872
        $configuration = [
2873
            'prioritizedLanguageList' => $prioritizedLanguageList,
2874
            'showAllTranslations' => $showAllTranslations,
2875
        ];
2876
        $this->setConfiguration($urlAliasService, $configuration);
2877
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2878
2879
        $urlAliasHandler->expects(
2880
            $this->once()
2881
        )->method(
2882
            'lookup'
2883
        )->with(
2884
            $this->equalTo('jedan/dva')
2885
        )->will(
2886
            $this->returnValue(
2887
                new SPIUrlAlias(
2888
                    [
2889
                        'pathData' => [
2890
                            [
2891
                                'always-available' => $alwaysAvailable,
2892
                                'translations' => ['cro-HR' => 'jedan'],
2893
                            ],
2894
                            [
2895
                                'always-available' => $alwaysAvailable,
2896
                                'translations' => [
2897
                                    'cro-HR' => 'dva',
2898
                                    'eng-GB' => 'two',
2899
                                ],
2900
                            ],
2901
                        ],
2902
                        'languageCodes' => ['eng-GB', 'cro-HR'],
2903
                        'alwaysAvailable' => $alwaysAvailable,
2904
                    ]
2905
                )
2906
            )
2907
        );
2908
2909
        $urlAlias = $urlAliasService->lookup('jedan/dva', $languageCode);
2910
2911
        self::assertInstanceOf(
2912
            'eZ\\Publish\\API\\Repository\\Values\\Content\\URLAlias',
2913
            $urlAlias
2914
        );
2915
    }
2916
2917
    public function providerForTestLookupWithSharedTranslation()
2918
    {
2919
        return [
2920
            // showAllTranslations setting is true
2921
            [['ger-DE'], true, false, null],
2922
            // alias is always available
2923
            [['ger-DE'], false, true, null],
2924
            // works with available language codes
2925
            [['cro-HR'], false, false, 'eng-GB'],
2926
            [['eng-GB'], false, false, 'cro-HR'],
2927
            // works with cro-HR only
2928
            [['cro-HR'], false, false, null],
2929
            // works with eng-GB only
2930
            [['eng-GB'], false, false, null],
2931
            // works with cro-HR first
2932
            [['cro-HR', 'eng-GB'], false, false, null],
2933
            // works with eng-GB first
2934
            [['eng-GB', 'cro-HR'], false, false, null],
2935
        ];
2936
    }
2937
2938
    /**
2939
     * Test for the lookup() method.
2940
     *
2941
     * @dataProvider providerForTestLookupWithSharedTranslation
2942
     */
2943 View Code Duplication
    public function testLookupWithSharedTranslation(
2944
        $prioritizedLanguageList,
2945
        $showAllTranslations,
2946
        $alwaysAvailable,
2947
        $languageCode
2948
    ) {
2949
        $urlAliasService = $this->getRepository()->getURLAliasService();
2950
        $configuration = [
2951
            'prioritizedLanguageList' => $prioritizedLanguageList,
2952
            'showAllTranslations' => $showAllTranslations,
2953
        ];
2954
        $this->setConfiguration($urlAliasService, $configuration);
2955
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
2956
2957
        $urlAliasHandler->expects(
2958
            $this->once()
2959
        )->method(
2960
            'lookup'
2961
        )->with(
2962
            $this->equalTo('jedan/two')
2963
        )->will(
2964
            $this->returnValue(
2965
                new SPIUrlAlias(
2966
                    [
2967
                        'pathData' => [
2968
                            [
2969
                                'always-available' => $alwaysAvailable,
2970
                                'translations' => [
2971
                                    'cro-HR' => 'jedan',
2972
                                    'eng-GB' => 'jedan',
2973
                                ],
2974
                            ],
2975
                            [
2976
                                'always-available' => $alwaysAvailable,
2977
                                'translations' => [
2978
                                    'cro-HR' => 'two',
2979
                                    'eng-GB' => 'two',
2980
                                ],
2981
                            ],
2982
                        ],
2983
                        'languageCodes' => ['eng-GB', 'cro-HR'],
2984
                        'alwaysAvailable' => $alwaysAvailable,
2985
                    ]
2986
                )
2987
            )
2988
        );
2989
2990
        $urlAlias = $urlAliasService->lookup('jedan/two', $languageCode);
2991
2992
        self::assertInstanceOf(
2993
            'eZ\\Publish\\API\\Repository\\Values\\Content\\URLAlias',
2994
            $urlAlias
2995
        );
2996
    }
2997
2998
    /**
2999
     * Test for the reverseLookup() method.
3000
     *
3001
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
3002
     */
3003
    public function testReverseLookupCustomConfiguration()
3004
    {
3005
        $mockedService = $this->getPartlyMockedURLAliasServiceService(['listLocationAliases']);
3006
        $location = $this->getLocationStub();
3007
        $mockedService->expects(
3008
            $this->once()
3009
        )->method(
3010
            'listLocationAliases'
3011
        )->with(
3012
            $this->equalTo($location),
3013
            $this->equalTo(false),
3014
            $this->equalTo(null),
3015
            $this->equalTo($showAllTranslations = 'HELLO!'),
3016
            $this->equalTo($prioritizedLanguageList = ['LANGUAGES!'])
3017
        )->will(
3018
            $this->returnValue([])
3019
        );
3020
3021
        $mockedService->reverseLookup($location, null, $showAllTranslations, $prioritizedLanguageList);
3022
    }
3023
3024
    /**
3025
     * Test for the reverseLookup() method.
3026
     *
3027
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
3028
     */
3029
    public function testReverseLookupThrowsNotFoundException()
3030
    {
3031
        $mockedService = $this->getPartlyMockedURLAliasServiceService(['listLocationAliases']);
3032
        $configuration = [
3033
            'prioritizedLanguageList' => ['ger-DE'],
3034
            'showAllTranslations' => false,
3035
        ];
3036
        $this->setConfiguration($mockedService, $configuration);
3037
3038
        $languageCode = 'eng-GB';
3039
        $location = $this->getLocationStub();
3040
3041
        $mockedService->expects(
3042
            $this->once()
3043
        )->method(
3044
            'listLocationAliases'
3045
        )->with(
3046
            $this->equalTo($location),
3047
            $this->equalTo(false),
3048
            $this->equalTo($languageCode)
3049
        )->will(
3050
            $this->returnValue(
3051
                [
3052
                    new UrlAlias(
3053
                        [
3054
                            'languageCodes' => ['eng-GB'],
3055
                            'alwaysAvailable' => false,
3056
                        ]
3057
                    ),
3058
                ]
3059
            )
3060
        );
3061
3062
        $mockedService->reverseLookup($location, $languageCode);
3063
    }
3064
3065
    public function providerForTestReverseLookup()
3066
    {
3067
        return $this->providerForTestListAutogeneratedLocationAliasesPath();
3068
    }
3069
3070
    /**
3071
     * Test for the reverseLookup() method.
3072
     *
3073
     * @dataProvider providerForTestReverseLookup
3074
     */
3075
    public function testReverseLookupPath($spiUrlAliases, $prioritizedLanguageCodes, $paths, $reverseLookupLanguageCode)
3076
    {
3077
        $urlAliasService = $this->getRepository()->getURLAliasService();
3078
        $configuration = [
3079
            'prioritizedLanguageList' => $prioritizedLanguageCodes,
3080
            'showAllTranslations' => false,
3081
        ];
3082
        $this->setConfiguration($urlAliasService, $configuration);
3083
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
3084
        $urlAliasHandler->expects(
3085
            $this->once()
3086
        )->method(
3087
            'listURLAliasesForLocation'
3088
        )->with(
3089
            $this->equalTo(42),
3090
            $this->equalTo(false)
3091
        )->will(
3092
            $this->returnValue($spiUrlAliases)
3093
        );
3094
3095
        $location = $this->getLocationStub();
3096
        $urlAlias = $urlAliasService->reverseLookup($location);
3097
3098
        self::assertEquals(
3099
            [$reverseLookupLanguageCode],
3100
            $urlAlias->languageCodes
3101
        );
3102
        self::assertEquals(
3103
            $paths[$reverseLookupLanguageCode],
3104
            $urlAlias->path
3105
        );
3106
    }
3107
3108
    public function providerForTestReverseLookupAlwaysAvailablePath()
3109
    {
3110
        return $this->providerForTestListAutogeneratedLocationAliasesAlwaysAvailablePath();
3111
    }
3112
3113
    /**
3114
     * Test for the reverseLookup() method.
3115
     *
3116
     * @dataProvider providerForTestReverseLookupAlwaysAvailablePath
3117
     */
3118 View Code Duplication
    public function testReverseLookupAlwaysAvailablePath(
3119
        $spiUrlAliases,
3120
        $prioritizedLanguageCodes,
3121
        $paths
3122
    ) {
3123
        $urlAliasService = $this->getRepository()->getURLAliasService();
3124
        $configuration = [
3125
            'prioritizedLanguageList' => $prioritizedLanguageCodes,
3126
            'showAllTranslations' => false,
3127
        ];
3128
        $this->setConfiguration($urlAliasService, $configuration);
3129
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
3130
        $urlAliasHandler->expects(
3131
            $this->once()
3132
        )->method(
3133
            'listURLAliasesForLocation'
3134
        )->with(
3135
            $this->equalTo(42),
3136
            $this->equalTo(false)
3137
        )->will(
3138
            $this->returnValue($spiUrlAliases)
3139
        );
3140
3141
        $location = $this->getLocationStub();
3142
        $urlAlias = $urlAliasService->reverseLookup($location);
3143
3144
        self::assertEquals(
3145
            reset($paths),
3146
            $urlAlias->path
3147
        );
3148
    }
3149
3150
    /**
3151
     * Test for the reverseLookup() method.
3152
     */
3153
    public function testReverseLookupWithShowAllTranslations()
3154
    {
3155
        $spiUrlAlias = $this->getSpiUrlAlias();
3156
        $urlAliasService = $this->getRepository()->getURLAliasService();
3157
        $configuration = [
3158
            'prioritizedLanguageList' => ['fre-FR'],
3159
            'showAllTranslations' => true,
3160
        ];
3161
        $this->setConfiguration($urlAliasService, $configuration);
3162
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
3163
        $urlAliasHandler->expects(
3164
            $this->once()
3165
        )->method(
3166
            'listURLAliasesForLocation'
3167
        )->with(
3168
            $this->equalTo(42),
3169
            $this->equalTo(false)
3170
        )->will(
3171
            $this->returnValue([$spiUrlAlias])
3172
        );
3173
3174
        $location = $this->getLocationStub();
3175
        $urlAlias = $urlAliasService->reverseLookup($location);
3176
3177
        self::assertEquals('/jedan/dva/tri', $urlAlias->path);
3178
    }
3179
3180
    /**
3181
     * Test for the createUrlAlias() method.
3182
     */
3183 View Code Duplication
    public function testCreateUrlAlias()
3184
    {
3185
        $repositoryMock = $this->getRepositoryMock();
3186
        $mockedService = $this->getPartlyMockedURLAliasServiceService();
3187
        /** @var \PHPUnit_Framework_MockObject_MockObject $urlAliasHandlerMock */
3188
        $urlAliasHandlerMock = $this->getPersistenceMock()->urlAliasHandler();
3189
        $location = $this->getLocationStub();
3190
3191
        $repositoryMock
3192
            ->expects($this->once())
3193
            ->method('beginTransaction');
3194
        $repositoryMock
3195
            ->expects($this->once())
3196
            ->method('commit');
3197
3198
        $urlAliasHandlerMock->expects(
3199
            $this->once()
3200
        )->method(
3201
            'createCustomUrlAlias'
3202
        )->with(
3203
            $this->equalTo($location->id),
3204
            $this->equalTo('path'),
3205
            $this->equalTo('forwarding'),
3206
            $this->equalTo('languageCode'),
3207
            $this->equalTo('alwaysAvailable')
3208
        )->will(
3209
            $this->returnValue(new SPIUrlAlias())
3210
        );
3211
3212
        $urlAlias = $mockedService->createUrlAlias(
3213
            $location,
3214
            'path',
3215
            'languageCode',
3216
            'forwarding',
3217
            'alwaysAvailable'
3218
        );
3219
3220
        self::assertInstanceOf(
3221
            'eZ\\Publish\\API\\Repository\\Values\\Content\\URLAlias',
3222
            $urlAlias
3223
        );
3224
    }
3225
3226
    /**
3227
     * Test for the createUrlAlias() method.
3228
     *
3229
     * @expectedException \Exception
3230
     * @expectedExceptionMessage Handler threw an exception
3231
     */
3232
    public function testCreateUrlAliasWithRollback()
3233
    {
3234
        $repositoryMock = $this->getRepositoryMock();
3235
        $mockedService = $this->getPartlyMockedURLAliasServiceService();
3236
        /** @var \PHPUnit_Framework_MockObject_MockObject $urlAliasHandlerMock */
3237
        $urlAliasHandlerMock = $this->getPersistenceMock()->urlAliasHandler();
3238
        $location = $this->getLocationStub();
3239
3240
        $repositoryMock
3241
            ->expects($this->once())
3242
            ->method('beginTransaction');
3243
        $repositoryMock
3244
            ->expects($this->once())
3245
            ->method('rollback');
3246
3247
        $urlAliasHandlerMock->expects(
3248
            $this->once()
3249
        )->method(
3250
            'createCustomUrlAlias'
3251
        )->with(
3252
            $this->equalTo($location->id),
3253
            $this->equalTo('path'),
3254
            $this->equalTo('forwarding'),
3255
            $this->equalTo('languageCode'),
3256
            $this->equalTo('alwaysAvailable')
3257
        )->will(
3258
            $this->throwException(new Exception('Handler threw an exception'))
3259
        );
3260
3261
        $mockedService->createUrlAlias(
3262
            $location,
3263
            'path',
3264
            'languageCode',
3265
            'forwarding',
3266
            'alwaysAvailable'
3267
        );
3268
    }
3269
3270
    /**
3271
     * Test for the createUrlAlias() method.
3272
     *
3273
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
3274
     */
3275 View Code Duplication
    public function testCreateUrlAliasThrowsInvalidArgumentException()
3276
    {
3277
        $location = $this->getLocationStub();
3278
        $urlAliasService = $this->getRepository()->getURLAliasService();
3279
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
3280
3281
        $urlAliasHandler->expects(
3282
            $this->once()
3283
        )->method(
3284
            'createCustomUrlAlias'
3285
        )->with(
3286
            $this->equalTo($location->id),
3287
            $this->equalTo('path'),
3288
            $this->equalTo('forwarding'),
3289
            $this->equalTo('languageCode'),
3290
            $this->equalTo('alwaysAvailable')
3291
        )->will(
3292
            $this->throwException(new ForbiddenException('Forbidden!'))
3293
        );
3294
3295
        $urlAliasService->createUrlAlias(
3296
            $location,
3297
            'path',
3298
            'languageCode',
3299
            'forwarding',
3300
            'alwaysAvailable'
3301
        );
3302
    }
3303
3304
    /**
3305
     * Test for the createGlobalUrlAlias() method.
3306
     */
3307 View Code Duplication
    public function testCreateGlobalUrlAlias()
3308
    {
3309
        $resource = 'module:content/search';
3310
        $repositoryMock = $this->getRepositoryMock();
3311
        $mockedService = $this->getPartlyMockedURLAliasServiceService();
3312
        /** @var \PHPUnit_Framework_MockObject_MockObject $urlAliasHandlerMock */
3313
        $urlAliasHandlerMock = $this->getPersistenceMock()->urlAliasHandler();
3314
3315
        $repositoryMock
3316
            ->expects($this->once())
3317
            ->method('beginTransaction');
3318
        $repositoryMock
3319
            ->expects($this->once())
3320
            ->method('commit');
3321
3322
        $urlAliasHandlerMock->expects(
3323
            $this->once()
3324
        )->method(
3325
            'createGlobalUrlAlias'
3326
        )->with(
3327
            $this->equalTo($resource),
3328
            $this->equalTo('path'),
3329
            $this->equalTo('forwarding'),
3330
            $this->equalTo('languageCode'),
3331
            $this->equalTo('alwaysAvailable')
3332
        )->will(
3333
            $this->returnValue(new SPIUrlAlias())
3334
        );
3335
3336
        $urlAlias = $mockedService->createGlobalUrlAlias(
3337
            $resource,
3338
            'path',
3339
            'languageCode',
3340
            'forwarding',
3341
            'alwaysAvailable'
3342
        );
3343
3344
        self::assertInstanceOf(
3345
            'eZ\\Publish\\API\\Repository\\Values\\Content\\URLAlias',
3346
            $urlAlias
3347
        );
3348
    }
3349
3350
    /**
3351
     * Test for the createGlobalUrlAlias() method.
3352
     *
3353
     * @expectedException \Exception
3354
     * @expectedExceptionMessage Handler threw an exception
3355
     */
3356
    public function testCreateGlobalUrlAliasWithRollback()
3357
    {
3358
        $resource = 'module:content/search';
3359
        $repositoryMock = $this->getRepositoryMock();
3360
        $mockedService = $this->getPartlyMockedURLAliasServiceService();
3361
        /** @var \PHPUnit_Framework_MockObject_MockObject $urlAliasHandlerMock */
3362
        $urlAliasHandlerMock = $this->getPersistenceMock()->urlAliasHandler();
3363
3364
        $repositoryMock
3365
            ->expects($this->once())
3366
            ->method('beginTransaction');
3367
        $repositoryMock
3368
            ->expects($this->once())
3369
            ->method('rollback');
3370
3371
        $urlAliasHandlerMock->expects(
3372
            $this->once()
3373
        )->method(
3374
            'createGlobalUrlAlias'
3375
        )->with(
3376
            $this->equalTo($resource),
3377
            $this->equalTo('path'),
3378
            $this->equalTo('forwarding'),
3379
            $this->equalTo('languageCode'),
3380
            $this->equalTo('alwaysAvailable')
3381
        )->will(
3382
            $this->throwException(new Exception('Handler threw an exception'))
3383
        );
3384
3385
        $mockedService->createGlobalUrlAlias(
3386
            $resource,
3387
            'path',
3388
            'languageCode',
3389
            'forwarding',
3390
            'alwaysAvailable'
3391
        );
3392
    }
3393
3394
    /**
3395
     * Test for the createGlobalUrlAlias() method.
3396
     *
3397
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
3398
     */
3399
    public function testCreateGlobalUrlAliasThrowsInvalidArgumentExceptionResource()
3400
    {
3401
        $urlAliasService = $this->getRepository()->getURLAliasService();
3402
        $urlAliasService->createGlobalUrlAlias(
3403
            'invalid/resource',
3404
            'path',
3405
            'languageCode',
3406
            'forwarding',
3407
            'alwaysAvailable'
3408
        );
3409
    }
3410
3411
    /**
3412
     * Test for the createGlobalUrlAlias() method.
3413
     *
3414
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
3415
     */
3416 View Code Duplication
    public function testCreateGlobalUrlAliasThrowsInvalidArgumentExceptionPath()
3417
    {
3418
        $resource = 'module:content/search';
3419
        $urlAliasService = $this->getRepository()->getURLAliasService();
3420
        $urlAliasHandler = $this->getPersistenceMockHandler('Content\\UrlAlias\\Handler');
3421
3422
        $urlAliasHandler->expects(
3423
            $this->once()
3424
        )->method(
3425
            'createGlobalUrlAlias'
3426
        )->with(
3427
            $this->equalTo($resource),
3428
            $this->equalTo('path'),
3429
            $this->equalTo('forwarding'),
3430
            $this->equalTo('languageCode'),
3431
            $this->equalTo('alwaysAvailable')
3432
        )->will(
3433
            $this->throwException(new ForbiddenException('Forbidden!'))
3434
        );
3435
3436
        $urlAliasService->createGlobalUrlAlias(
3437
            $resource,
3438
            'path',
3439
            'languageCode',
3440
            'forwarding',
3441
            'alwaysAvailable'
3442
        );
3443
    }
3444
3445
    /**
3446
     * Test for the createGlobalUrlAlias() method.
3447
     *
3448
     * @depends eZ\Publish\Core\Repository\Tests\Service\Mock\UrlAliasTest::testCreateUrlAlias
3449
     * @depends eZ\Publish\Core\Repository\Tests\Service\Mock\UrlAliasTest::testCreateUrlAliasWithRollback
3450
     * @depends eZ\Publish\Core\Repository\Tests\Service\Mock\UrlAliasTest::testCreateUrlAliasThrowsInvalidArgumentException
3451
     */
3452
    public function testCreateGlobalUrlAliasForLocation()
3453
    {
3454
        $repositoryMock = $this->getRepositoryMock();
3455
        $mockedService = $this->getPartlyMockedURLAliasServiceService(['createUrlAlias']);
3456
        $location = $this->getLocationStub();
3457
        $locationServiceMock = $this->createMock('eZ\\Publish\\Core\\Repository\\LocationService');
3458
3459
        $locationServiceMock->expects(
3460
            $this->exactly(2)
3461
        )->method(
3462
            'loadLocation'
3463
        )->with(
3464
            $this->equalTo(42)
3465
        )->will(
3466
            $this->returnValue($location)
3467
        );
3468
3469
        $repositoryMock->expects(
3470
            $this->exactly(2)
3471
        )->method(
3472
            'getLocationService'
3473
        )->will(
3474
            $this->returnValue($locationServiceMock)
3475
        );
3476
3477
        $mockedService->expects(
3478
            $this->exactly(2)
3479
        )->method(
3480
            'createUrlAlias'
3481
        )->with(
3482
            $this->equalTo($location),
3483
            $this->equalTo('path'),
3484
            $this->equalTo('languageCode'),
3485
            $this->equalTo('forwarding'),
3486
            $this->equalTo('alwaysAvailable')
3487
        );
3488
3489
        $mockedService->createGlobalUrlAlias(
3490
            'eznode:42',
3491
            'path',
3492
            'languageCode',
3493
            'forwarding',
3494
            'alwaysAvailable'
3495
        );
3496
        $mockedService->createGlobalUrlAlias(
3497
            'module:content/view/full/42',
3498
            'path',
3499
            'languageCode',
3500
            'forwarding',
3501
            'alwaysAvailable'
3502
        );
3503
    }
3504
3505
    /**
3506
     * @param int $id
3507
     *
3508
     * @return \eZ\Publish\Core\Repository\Values\Content\Location
3509
     */
3510
    protected function getLocationStub($id = 42)
3511
    {
3512
        return new Location(['id' => $id]);
3513
    }
3514
3515
    /**
3516
     * @param object $urlAliasService
3517
     * @param array $configuration
3518
     */
3519
    protected function setConfiguration($urlAliasService, array $configuration)
3520
    {
3521
        $refObject = new \ReflectionObject($urlAliasService);
3522
        $refProperty = $refObject->getProperty('settings');
3523
        $refProperty->setAccessible(true);
3524
        $refProperty->setValue(
3525
            $urlAliasService,
3526
            $configuration
3527
        );
3528
    }
3529
3530
    /**
3531
     * Returns the content service to test with $methods mocked.
3532
     *
3533
     * Injected Repository comes from {@see getRepositoryMock()} and persistence handler from {@see getPersistenceMock()}
3534
     *
3535
     * @param string[] $methods
3536
     *
3537
     * @return \eZ\Publish\Core\Repository\URLAliasService|\PHPUnit_Framework_MockObject_MockObject
3538
     */
3539
    protected function getPartlyMockedURLAliasServiceService(array $methods = null)
3540
    {
3541
        $languageServiceMock = $this->createMock('eZ\\Publish\\Core\\Repository\\LanguageService');
3542
3543
        $languageServiceMock->expects(
3544
            $this->once()
3545
        )->method(
3546
            'getPrioritizedLanguageCodeList'
3547
        )->will(
3548
            $this->returnValue(['eng-GB'])
3549
        );
3550
3551
        $this->getRepositoryMock()->expects(
3552
            $this->once()
3553
        )->method(
3554
            'getContentLanguageService'
3555
        )->will(
3556
            $this->returnValue($languageServiceMock)
3557
        );
3558
3559
        return $this->getMockBuilder('eZ\\Publish\\Core\\Repository\\URLAliasService')
3560
            ->setMethods($methods)
3561
            ->setConstructorArgs(
3562
                [
3563
                    $this->getRepositoryMock(),
3564
                    $this->getPersistenceMock()->urlAliasHandler(),
3565
                    $this->getNameSchemaServiceMock(),
3566
                ]
3567
            )
3568
            ->getMock();
3569
    }
3570
3571
    /**
3572
     * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\Repository\Helper\NameSchemaService
3573
     */
3574
    protected function getNameSchemaServiceMock()
3575
    {
3576
        return $this->createMock('eZ\\Publish\\Core\\Repository\\Helper\\NameSchemaService');
3577
    }
3578
}
3579