Completed
Push — ezp-30882-thumbnail ( 274ed9...d4335b )
by
unknown
14:43
created

UrlWildcardTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Repository\Tests\Service\Mock\UrlWildcardTest 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 Exception;
12
use eZ\Publish\API\Repository\Exceptions\ContentValidationException;
13
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
14
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
15
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
16
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest;
17
use eZ\Publish\API\Repository\Values\Content\URLWildcard;
18
use eZ\Publish\Core\Repository\URLWildcardService;
19
use eZ\Publish\SPI\Persistence\Content\UrlWildcard as SPIURLWildcard;
20
use eZ\Publish\API\Repository\Values\Content\URLWildcardTranslationResult;
21
22
/**
23
 * Mock Test case for UrlWildcard Service.
24
 */
25
class UrlWildcardTest extends BaseServiceMockTest
26
{
27
    /** @var \eZ\Publish\API\Repository\PermissionResolver|\PHPUnit\Framework\MockObject\MockObject */
28
    private $permissionResolver;
29
30
    /** @var \eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler|\PHPUnit\Framework\MockObject\MockObject */
31
    private $urlWildcardHandler;
32
33
    protected function setUp(): void
34
    {
35
        parent::setUp();
36
        $this->urlWildcardHandler = $this->getPersistenceMockHandler('Content\\UrlWildcard\\Handler');
37
        $this->permissionResolver = $this->getPermissionResolverMock();
38
    }
39
40
    /**
41
     * Test for the create() method.
42
     *
43
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::create
44
     */
45
    public function testCreateThrowsUnauthorizedException()
46
    {
47
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class);
48
49
        $mockedService = $this->getPartlyMockedURLWildcardService();
50
51
        $this->permissionResolver->expects(
52
            $this->once()
53
        )->method(
54
            'hasAccess'
55
        )->with(
56
            $this->equalTo('content'),
57
            $this->equalTo('urltranslator')
58
        )->will(
59
            $this->returnValue(false)
60
        );
61
62
        $this->expectException(UnauthorizedException::class);
63
64
        $mockedService->create('lorem/ipsum', 'opossum', true);
65
    }
66
67
    /**
68
     * Test for the create() method.
69
     *
70
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::create
71
     */
72
    public function testCreateThrowsInvalidArgumentException()
73
    {
74
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
75
76
        $mockedService = $this->getPartlyMockedURLWildcardService();
77
78
        $this->permissionResolver->expects(
79
            $this->once()
80
        )->method(
81
            'hasAccess'
82
        )->with(
83
            $this->equalTo('content'),
84
            $this->equalTo('urltranslator')
85
        )->will(
86
            $this->returnValue(true)
87
        );
88
89
        $this->urlWildcardHandler->expects(
90
            $this->once()
91
        )->method(
92
            'exactSourceUrlExists'
93
        )->willReturn(true);
94
95
        $this->expectException(InvalidArgumentException::class);
96
97
        $mockedService->create('/lorem/ipsum', 'opossum', true);
98
    }
99
100
    public function providerForTestCreateThrowsContentValidationException()
101
    {
102
        return [
103
            ['fruit', 'food/{1}', true],
104
            ['fruit/*', 'food/{2}', false],
105
            ['fruit/*/*', 'food/{3}', true],
106
        ];
107
    }
108
109
    /**
110
     * Test for the create() method.
111
     *
112
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::create
113
     * @dataProvider providerForTestCreateThrowsContentValidationException
114
     */
115
    public function testCreateThrowsContentValidationException($sourceUrl, $destinationUrl, $forward)
116
    {
117
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\ContentValidationException::class);
118
119
        $mockedService = $this->getPartlyMockedURLWildcardService();
120
121
        $this->permissionResolver->expects(
122
            $this->once()
123
        )->method(
124
            'hasAccess'
125
        )->with(
126
            $this->equalTo('content'),
127
            $this->equalTo('urltranslator')
128
        )->will(
129
            $this->returnValue(true)
130
        );
131
132
        $this->urlWildcardHandler->expects(
133
            $this->once()
134
        )->method(
135
            'exactSourceUrlExists'
136
        )->willReturn(false);
137
138
        $this->expectException(ContentValidationException::class);
139
140
        $mockedService->create($sourceUrl, $destinationUrl, $forward);
141
    }
142
143
    public function providerForTestCreate()
144
    {
145
        return [
146
            ['fruit', 'food', true],
147
            [' /fruit/ ', ' /food/ ', true],
148
            ['/fruit/*', '/food', false],
149
            ['/fruit/*', '/food/{1}', true],
150
            ['/fruit/*/*', '/food/{1}', true],
151
            ['/fruit/*/*', '/food/{2}', true],
152
            ['/fruit/*/*', '/food/{1}/{2}', true],
153
        ];
154
    }
155
156
    /**
157
     * Test for the create() method.
158
     *
159
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::create
160
     * @dataProvider providerForTestCreate
161
     */
162
    public function testCreate($sourceUrl, $destinationUrl, $forward)
163
    {
164
        $mockedService = $this->getPartlyMockedURLWildcardService();
165
166
        $sourceUrl = '/' . trim($sourceUrl, '/ ');
167
        $destinationUrl = '/' . trim($destinationUrl, '/ ');
168
169
        $this->permissionResolver->expects(
170
            $this->once()
171
        )->method(
172
            'hasAccess'
173
        )->with(
174
            $this->equalTo('content'),
175
            $this->equalTo('urltranslator')
176
        )->will(
177
            $this->returnValue(true)
178
        );
179
180
        $repositoryMock = $this->getRepositoryMock();
181
        $repositoryMock->expects($this->once())->method('beginTransaction');
182
        $repositoryMock->expects($this->once())->method('commit');
183
184
        $this->urlWildcardHandler->expects(
185
            $this->once()
186
        )->method(
187
            'exactSourceUrlExists'
188
        )->willReturn(false);
189
190
        $this->urlWildcardHandler->expects(
191
            $this->once()
192
        )->method(
193
            'create'
194
        )->with(
195
            $this->equalTo($sourceUrl),
196
            $this->equalTo($destinationUrl),
197
            $this->equalTo($forward)
198
        )->will(
199
            $this->returnValue(
200
                new SPIURLWildcard(
201
                    [
202
                        'id' => 123456,
203
                        'sourceUrl' => $sourceUrl,
204
                        'destinationUrl' => $destinationUrl,
205
                        'forward' => $forward,
206
                    ]
207
                )
208
            )
209
        );
210
211
        $urlWildCard = $mockedService->create($sourceUrl, $destinationUrl, $forward);
212
213
        $this->assertEquals(
214
            new URLWildcard(
215
                [
216
                    'id' => 123456,
217
                    'sourceUrl' => $sourceUrl,
218
                    'destinationUrl' => $destinationUrl,
219
                    'forward' => $forward,
220
                ]
221
            ),
222
            $urlWildCard
223
        );
224
    }
225
226
    /**
227
     * Test for the create() method.
228
     *
229
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::create
230
     */
231
    public function testCreateWithRollback()
232
    {
233
        $this->expectException(\Exception::class);
234
235
        $mockedService = $this->getPartlyMockedURLWildcardService();
236
237
        $this->permissionResolver->expects(
238
            $this->once()
239
        )->method(
240
            'hasAccess'
241
        )->with(
242
            $this->equalTo('content'),
243
            $this->equalTo('urltranslator')
244
        )->will(
245
            $this->returnValue(true)
246
        );
247
248
        $repositoryMock = $this->getRepositoryMock();
249
        $repositoryMock->expects($this->once())->method('beginTransaction');
250
        $repositoryMock->expects($this->once())->method('rollback');
251
252
        $sourceUrl = '/lorem';
253
        $destinationUrl = '/ipsum';
254
        $forward = true;
255
256
        $this->urlWildcardHandler->expects(
257
            $this->once()
258
        )->method(
259
            'exactSourceUrlExists'
260
        )->willReturn(false);
261
262
        $this->urlWildcardHandler->expects(
263
            $this->once()
264
        )->method(
265
            'create'
266
        )->with(
267
            $this->equalTo($sourceUrl),
268
            $this->equalTo($destinationUrl),
269
            $this->equalTo($forward)
270
        )->will(
271
            $this->throwException(new Exception())
272
        );
273
274
        $this->expectException(Exception::class);
275
276
        $mockedService->create($sourceUrl, $destinationUrl, $forward);
277
    }
278
279
    /**
280
     * Test for the remove() method.
281
     *
282
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::remove
283
     */
284
    public function testRemoveThrowsUnauthorizedException()
285
    {
286
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class);
287
288
        $wildcard = new URLWildcard(['id' => 'McBoom']);
289
290
        $mockedService = $this->getPartlyMockedURLWildcardService();
291
292
        $this->permissionResolver->expects(
293
            $this->once()
294
        )->method(
295
            'canUser'
296
        )->with(
297
            $this->equalTo('content'),
298
            $this->equalTo('urltranslator'),
299
            $this->equalTo($wildcard)
300
        )->will(
301
            $this->returnValue(false)
302
        );
303
304
        $repositoryMock = $this->getRepositoryMock();
305
        $repositoryMock->expects($this->never())->method('beginTransaction');
306
307
        $this->expectException(UnauthorizedException::class);
308
309
        $mockedService->remove($wildcard);
310
    }
311
312
    /**
313
     * Test for the remove() method.
314
     *
315
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::remove
316
     */
317
    public function testRemove()
318
    {
319
        $wildcard = new URLWildcard(['id' => 'McBomb']);
320
321
        $mockedService = $this->getPartlyMockedURLWildcardService();
322
323
        $this->permissionResolver->expects(
324
            $this->once()
325
        )->method(
326
            'canUser'
327
        )->with(
328
            $this->equalTo('content'),
329
            $this->equalTo('urltranslator'),
330
            $this->equalTo($wildcard)
331
        )->will(
332
            $this->returnValue(true)
333
        );
334
335
        $repositoryMock = $this->getRepositoryMock();
336
        $repositoryMock->expects($this->once())->method('beginTransaction');
337
        $repositoryMock->expects($this->once())->method('commit');
338
339
        $this->urlWildcardHandler->expects(
340
            $this->once()
341
        )->method(
342
            'remove'
343
        )->with(
344
            $this->equalTo('McBomb')
345
        );
346
347
        $mockedService->remove($wildcard);
348
    }
349
350
    /**
351
     * Test for the remove() method.
352
     *
353
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::remove
354
     */
355
    public function testRemoveWithRollback()
356
    {
357
        $this->expectException(\Exception::class);
358
359
        $wildcard = new URLWildcard(['id' => 'McBoo']);
360
361
        $mockedService = $this->getPartlyMockedURLWildcardService();
362
363
        $this->permissionResolver->expects(
364
            $this->once()
365
        )->method(
366
            'canUser'
367
        )->with(
368
            $this->equalTo('content'),
369
            $this->equalTo('urltranslator'),
370
            $this->equalTo($wildcard)
371
        )->will(
372
            $this->returnValue(true)
373
        );
374
375
        $repositoryMock = $this->getRepositoryMock();
376
        $repositoryMock->expects($this->once())->method('beginTransaction');
377
        $repositoryMock->expects($this->once())->method('rollback');
378
379
        $this->urlWildcardHandler->expects(
380
            $this->once()
381
        )->method(
382
            'remove'
383
        )->with(
384
            $this->equalTo('McBoo')
385
        )->will(
386
            $this->throwException(new Exception())
387
        );
388
389
        $this->expectException(Exception::class);
390
391
        $mockedService->remove($wildcard);
392
    }
393
394
    /**
395
     * Test for the load() method.
396
     *
397
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::remove
398
     */
399
    public function testLoadThrowsException()
400
    {
401
        $this->expectException(\Exception::class);
402
403
        $mockedService = $this->getPartlyMockedURLWildcardService();
404
405
        $this->urlWildcardHandler->expects(
406
            $this->once()
407
        )->method(
408
            'load'
409
        )->with(
410
            $this->equalTo('Luigi')
411
        )->will(
412
            $this->throwException(new Exception())
413
        );
414
415
        $this->expectException(Exception::class);
416
417
        $mockedService->load('Luigi');
418
    }
419
420
    /**
421
     * Test for the load() method.
422
     *
423
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::remove
424
     */
425
    public function testLoad()
426
    {
427
        $mockedService = $this->getPartlyMockedURLWildcardService();
428
429
        $this->urlWildcardHandler->expects(
430
            $this->once()
431
        )->method(
432
            'load'
433
        )->with(
434
            $this->equalTo('Luigi')
435
        )->will(
436
            $this->returnValue(
437
                new SPIURLWildcard(
438
                    [
439
                        'id' => 'Luigi',
440
                        'sourceUrl' => 'this',
441
                        'destinationUrl' => 'that',
442
                        'forward' => true,
443
                    ]
444
                )
445
            )
446
        );
447
448
        $urlWildcard = $mockedService->load('Luigi');
449
450
        $this->assertEquals(
451
            new URLWildcard(
452
                [
453
                    'id' => 'Luigi',
454
                    'sourceUrl' => 'this',
455
                    'destinationUrl' => 'that',
456
                    'forward' => true,
457
                ]
458
            ),
459
            $urlWildcard
460
        );
461
    }
462
463
    /**
464
     * Test for the loadAll() method.
465
     *
466
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::loadAll
467
     */
468
    public function testLoadAll()
469
    {
470
        $mockedService = $this->getPartlyMockedURLWildcardService();
471
472
        $this->urlWildcardHandler->expects(
473
            $this->once()
474
        )->method(
475
            'loadAll'
476
        )->with(
477
            $this->equalTo(0),
478
            $this->equalTo(-1)
479
        )->will(
480
            $this->returnValue([])
481
        );
482
483
        $mockedService->loadAll();
484
    }
485
486
    /**
487
     * Test for the loadAll() method.
488
     *
489
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::loadAll
490
     */
491
    public function testLoadAllWithLimitAndOffset()
492
    {
493
        $mockedService = $this->getPartlyMockedURLWildcardService();
494
495
        $this->urlWildcardHandler->expects(
496
            $this->once()
497
        )->method(
498
            'loadAll'
499
        )->with(
500
            $this->equalTo(12),
501
            $this->equalTo(34)
502
        )->will(
503
            $this->returnValue(
504
                [
505
                    new SPIURLWildcard(
506
                        [
507
                            'id' => 'Luigi',
508
                            'sourceUrl' => 'this',
509
                            'destinationUrl' => 'that',
510
                            'forward' => true,
511
                        ]
512
                    ),
513
                ]
514
            )
515
        );
516
517
        $urlWildcards = $mockedService->loadAll(12, 34);
518
519
        $this->assertEquals(
520
            [
521
                new URLWildcard(
522
                    [
523
                        'id' => 'Luigi',
524
                        'sourceUrl' => 'this',
525
                        'destinationUrl' => 'that',
526
                        'forward' => true,
527
                    ]
528
                ),
529
            ],
530
            $urlWildcards
531
        );
532
    }
533
534
    /**
535
     * @return array
536
     */
537
    public function providerForTestTranslateThrowsNotFoundException()
538
    {
539
        return [
540
            [
541
                [
542
                    'sourceUrl' => '/fruit',
543
                    'destinationUrl' => '/food',
544
                    'forward' => true,
545
                ],
546
                '/vegetable',
547
            ],
548
            [
549
                [
550
                    'sourceUrl' => '/fruit/apricot',
551
                    'destinationUrl' => '/food/apricot',
552
                    'forward' => true,
553
                ],
554
                '/fruit/lemon',
555
            ],
556
            [
557
                [
558
                    'sourceUrl' => '/fruit/*',
559
                    'destinationUrl' => '/food/{1}',
560
                    'forward' => true,
561
                ],
562
                '/fruit',
563
            ],
564
            [
565
                [
566
                    'sourceUrl' => '/fruit/*/*',
567
                    'destinationUrl' => '/food/{1}/{2}',
568
                    'forward' => true,
569
                ],
570
                '/fruit/citrus',
571
            ],
572
        ];
573
    }
574
575
    /**
576
     * Test for the translate() method.
577
     *
578
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::translate
579
     * @dataProvider providerForTestTranslateThrowsNotFoundException
580
     */
581
    public function testTranslateThrowsNotFoundException($createArray, $url)
582
    {
583
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
584
585
        $mockedService = $this->getPartlyMockedURLWildcardService();
586
587
        $trimmedUrl = trim($url, '/ ');
588
589
        $this->urlWildcardHandler
590
            ->expects($this->once())
591
            ->method('translate')
592
            ->with($trimmedUrl)
593
            ->willThrowException(new \eZ\Publish\Core\Base\Exceptions\NotFoundException('UrlWildcard', $trimmedUrl));
594
595
        $this->expectException(NotFoundException::class);
596
597
        $mockedService->translate($url);
598
    }
599
600
    /**
601
     * @return array
602
     */
603
    public function providerForTestTranslate()
604
    {
605
        return [
606
            [
607
                [
608
                    'sourceUrl' => '/fruit/apricot',
609
                    'destinationUrl' => '/food/apricot',
610
                    'forward' => true,
611
                ],
612
                '/fruit/apricot',
613
                '/food/apricot',
614
            ],
615
            [
616
                [
617
                    'sourceUrl' => '/fruit/*',
618
                    'destinationUrl' => '/food/{1}',
619
                    'forward' => true,
620
                ],
621
                '/fruit/citrus',
622
                '/food/citrus',
623
            ],
624
            [
625
                [
626
                    'sourceUrl' => '/fruit/*',
627
                    'destinationUrl' => '/food/{1}',
628
                    'forward' => true,
629
                ],
630
                '/fruit/citrus/orange',
631
                '/food/citrus/orange',
632
            ],
633
            [
634
                [
635
                    'sourceUrl' => '/fruit/*/*',
636
                    'destinationUrl' => '/food/{2}',
637
                    'forward' => true,
638
                ],
639
                '/fruit/citrus/orange',
640
                '/food/orange',
641
            ],
642
            [
643
                [
644
                    'sourceUrl' => '/fruit/*/*',
645
                    'destinationUrl' => '/food/{1}/{2}',
646
                    'forward' => true,
647
                ],
648
                '/fruit/citrus/orange',
649
                '/food/citrus/orange',
650
            ],
651
            [
652
                [
653
                    'sourceUrl' => '/fruit/*/pamplemousse',
654
                    'destinationUrl' => '/food/weird',
655
                    'forward' => true,
656
                ],
657
                '/fruit/citrus/pamplemousse',
658
                '/food/weird',
659
            ],
660
            [
661
                [
662
                    'sourceUrl' => '/fruit/*/pamplemousse',
663
                    'destinationUrl' => '/food/weird/{1}',
664
                    'forward' => true,
665
                ],
666
                '/fruit/citrus/pamplemousse',
667
                '/food/weird/citrus',
668
            ],
669
            [
670
                [
671
                    'sourceUrl' => '/fruit/*/pamplemousse',
672
                    'destinationUrl' => '/food/weird/{1}',
673
                    'forward' => true,
674
                ],
675
                '/fruit/citrus/yellow/pamplemousse',
676
                '/food/weird/citrus/yellow',
677
            ],
678
        ];
679
    }
680
681
    /**
682
     * Test for the translate() method.
683
     *
684
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::translate
685
     * @dataProvider providerForTestTranslate
686
     */
687
    public function testTranslate($createArray, $url, $uri)
688
    {
689
        $mockedService = $this->getPartlyMockedURLWildcardService();
690
691
        $trimmedUrl = trim($url, '/ ');
692
693
        $this->urlWildcardHandler
694
            ->expects($this->once())
695
            ->method('translate')
696
            ->with($trimmedUrl)
697
698
            ->willReturn(new SPIURLWildcard([
699
                'sourceUrl' => $createArray['sourceUrl'],
700
                'destinationUrl' => $uri,
701
                'forward' => $createArray['forward'],
702
            ]));
703
704
        $translationResult = $mockedService->translate($url);
705
706
        $this->assertEquals(
707
            new URLWildcardTranslationResult(
708
                [
709
                    'uri' => $uri,
710
                    'forward' => $createArray['forward'],
711
                ]
712
            ),
713
            $translationResult
714
        );
715
    }
716
717
    /**
718
     * Test for the translate() method.
719
     *
720
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::translate
721
     */
722
    public function testTranslateUsesLongestMatchingWildcard()
723
    {
724
        $mockedService = $this->getPartlyMockedURLWildcardService();
725
726
        $url = '/something/something/thing';
727
        $trimmedUrl = trim($url, '/ ');
728
729
        $this->urlWildcardHandler
730
            ->expects($this->once())
731
            ->method('translate')
732
            ->with($trimmedUrl)
733
            ->willReturn(new SPIURLWildcard([
734
                'destinationUrl' => '/long',
735
                'forward' => false,
736
            ]));
737
738
        $translationResult = $mockedService->translate($url);
739
740
        $this->assertEquals(
741
            new URLWildcardTranslationResult(
742
                [
743
                    'uri' => '/long',
744
                    'forward' => false,
745
                ]
746
            ),
747
            $translationResult
748
        );
749
    }
750
751
    /**
752
     * Returns the content service to test with $methods mocked.
753
     *
754
     * Injected Repository comes from {@see getRepositoryMock()} and persistence handler from {@see getPersistenceMock()}
755
     *
756
     * @param string[] $methods
757
     *
758
     * @return \eZ\Publish\Core\Repository\URLWildcardService|\PHPUnit\Framework\MockObject\MockObject
759
     */
760
    protected function getPartlyMockedURLWildcardService(array $methods = null)
761
    {
762
        return $this->getMockBuilder(URLWildcardService::class)
763
            ->setMethods($methods)
764
            ->setConstructorArgs(
765
                [
766
                    $this->getRepositoryMock(),
767
                    $this->urlWildcardHandler,
768
                    $this->permissionResolver,
769
                ]
770
            )
771
            ->getMock();
772
    }
773
}
774