Completed
Push — ezp_30827 ( c6ab75...809fcf )
by
unknown
14:52
created

testListLocationAliasesWithShowAllTranslations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54

Duplication

Lines 54
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 54
loc 54
rs 9.0036
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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