Completed
Push — 7.5 ( 144757...c45e35 )
by Łukasz
22:33
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()
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
        $mockedService = $this->getPartlyMockedURLWildcardService();
48
49
        $this->permissionResolver->expects(
50
            $this->once()
51
        )->method(
52
            'hasAccess'
53
        )->with(
54
            $this->equalTo('content'),
55
            $this->equalTo('urltranslator')
56
        )->will(
57
            $this->returnValue(false)
58
        );
59
60
        $this->expectException(UnauthorizedException::class);
61
62
        $mockedService->create('lorem/ipsum', 'opossum', true);
63
    }
64
65
    /**
66
     * Test for the create() method.
67
     *
68
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::create
69
     */
70
    public function testCreateThrowsInvalidArgumentException()
71
    {
72
        $mockedService = $this->getPartlyMockedURLWildcardService();
73
74
        $this->permissionResolver->expects(
75
            $this->once()
76
        )->method(
77
            'hasAccess'
78
        )->with(
79
            $this->equalTo('content'),
80
            $this->equalTo('urltranslator')
81
        )->will(
82
            $this->returnValue(true)
83
        );
84
85
        $this->urlWildcardHandler->expects(
86
            $this->once()
87
        )->method(
88
            'exactSourceUrlExists'
89
        )->willReturn(true);
90
91
        $this->expectException(InvalidArgumentException::class);
92
93
        $mockedService->create('/lorem/ipsum', 'opossum', true);
94
    }
95
96
    public function providerForTestCreateThrowsContentValidationException()
97
    {
98
        return [
99
            ['fruit', 'food/{1}', true],
100
            ['fruit/*', 'food/{2}', false],
101
            ['fruit/*/*', 'food/{3}', true],
102
        ];
103
    }
104
105
    /**
106
     * Test for the create() method.
107
     *
108
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::create
109
     * @dataProvider providerForTestCreateThrowsContentValidationException
110
     */
111
    public function testCreateThrowsContentValidationException($sourceUrl, $destinationUrl, $forward)
112
    {
113
        $mockedService = $this->getPartlyMockedURLWildcardService();
114
115
        $this->permissionResolver->expects(
116
            $this->once()
117
        )->method(
118
            'hasAccess'
119
        )->with(
120
            $this->equalTo('content'),
121
            $this->equalTo('urltranslator')
122
        )->will(
123
            $this->returnValue(true)
124
        );
125
126
        $this->urlWildcardHandler->expects(
127
            $this->once()
128
        )->method(
129
            'exactSourceUrlExists'
130
        )->willReturn(false);
131
132
        $this->expectException(ContentValidationException::class);
133
134
        $mockedService->create($sourceUrl, $destinationUrl, $forward);
135
    }
136
137
    public function providerForTestCreate()
138
    {
139
        return [
140
            ['fruit', 'food', true],
141
            [' /fruit/ ', ' /food/ ', true],
142
            ['/fruit/*', '/food', false],
143
            ['/fruit/*', '/food/{1}', true],
144
            ['/fruit/*/*', '/food/{1}', true],
145
            ['/fruit/*/*', '/food/{2}', true],
146
            ['/fruit/*/*', '/food/{1}/{2}', true],
147
        ];
148
    }
149
150
    /**
151
     * Test for the create() method.
152
     *
153
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::create
154
     * @dataProvider providerForTestCreate
155
     */
156
    public function testCreate($sourceUrl, $destinationUrl, $forward)
157
    {
158
        $mockedService = $this->getPartlyMockedURLWildcardService();
159
160
        $sourceUrl = '/' . trim($sourceUrl, '/ ');
161
        $destinationUrl = '/' . trim($destinationUrl, '/ ');
162
163
        $this->permissionResolver->expects(
164
            $this->once()
165
        )->method(
166
            'hasAccess'
167
        )->with(
168
            $this->equalTo('content'),
169
            $this->equalTo('urltranslator')
170
        )->will(
171
            $this->returnValue(true)
172
        );
173
174
        $repositoryMock = $this->getRepositoryMock();
175
        $repositoryMock->expects($this->once())->method('beginTransaction');
176
        $repositoryMock->expects($this->once())->method('commit');
177
178
        $this->urlWildcardHandler->expects(
179
            $this->once()
180
        )->method(
181
            'exactSourceUrlExists'
182
        )->willReturn(false);
183
184
        $this->urlWildcardHandler->expects(
185
            $this->once()
186
        )->method(
187
            'create'
188
        )->with(
189
            $this->equalTo($sourceUrl),
190
            $this->equalTo($destinationUrl),
191
            $this->equalTo($forward)
192
        )->will(
193
            $this->returnValue(
194
                new SPIURLWildcard(
195
                    [
196
                        'id' => 123456,
197
                        'sourceUrl' => $sourceUrl,
198
                        'destinationUrl' => $destinationUrl,
199
                        'forward' => $forward,
200
                    ]
201
                )
202
            )
203
        );
204
205
        $urlWildCard = $mockedService->create($sourceUrl, $destinationUrl, $forward);
206
207
        $this->assertEquals(
208
            new URLWildcard(
209
                [
210
                    'id' => 123456,
211
                    'sourceUrl' => $sourceUrl,
212
                    'destinationUrl' => $destinationUrl,
213
                    'forward' => $forward,
214
                ]
215
            ),
216
            $urlWildCard
217
        );
218
    }
219
220
    /**
221
     * Test for the create() method.
222
     *
223
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::create
224
     */
225
    public function testCreateWithRollback()
226
    {
227
        $mockedService = $this->getPartlyMockedURLWildcardService();
228
229
        $this->permissionResolver->expects(
230
            $this->once()
231
        )->method(
232
            'hasAccess'
233
        )->with(
234
            $this->equalTo('content'),
235
            $this->equalTo('urltranslator')
236
        )->will(
237
            $this->returnValue(true)
238
        );
239
240
        $repositoryMock = $this->getRepositoryMock();
241
        $repositoryMock->expects($this->once())->method('beginTransaction');
242
        $repositoryMock->expects($this->once())->method('rollback');
243
244
        $sourceUrl = '/lorem';
245
        $destinationUrl = '/ipsum';
246
        $forward = true;
247
248
        $this->urlWildcardHandler->expects(
249
            $this->once()
250
        )->method(
251
            'exactSourceUrlExists'
252
        )->willReturn(false);
253
254
        $this->urlWildcardHandler->expects(
255
            $this->once()
256
        )->method(
257
            'create'
258
        )->with(
259
            $this->equalTo($sourceUrl),
260
            $this->equalTo($destinationUrl),
261
            $this->equalTo($forward)
262
        )->will(
263
            $this->throwException(new Exception())
264
        );
265
266
        $this->expectException(Exception::class);
267
268
        $mockedService->create($sourceUrl, $destinationUrl, $forward);
269
    }
270
271
    /**
272
     * Test for the remove() method.
273
     *
274
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::remove
275
     */
276
    public function testRemoveThrowsUnauthorizedException()
277
    {
278
        $wildcard = new URLWildcard(['id' => 'McBoom']);
279
280
        $mockedService = $this->getPartlyMockedURLWildcardService();
281
282
        $this->permissionResolver->expects(
283
            $this->once()
284
        )->method(
285
            'canUser'
286
        )->with(
287
            $this->equalTo('content'),
288
            $this->equalTo('urltranslator'),
289
            $this->equalTo($wildcard)
290
        )->will(
291
            $this->returnValue(false)
292
        );
293
294
        $repositoryMock = $this->getRepositoryMock();
295
        $repositoryMock->expects($this->never())->method('beginTransaction');
296
297
        $this->expectException(UnauthorizedException::class);
298
299
        $mockedService->remove($wildcard);
300
    }
301
302
    /**
303
     * Test for the remove() method.
304
     *
305
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::remove
306
     */
307
    public function testRemove()
308
    {
309
        $wildcard = new URLWildcard(['id' => 'McBomb']);
310
311
        $mockedService = $this->getPartlyMockedURLWildcardService();
312
313
        $this->permissionResolver->expects(
314
            $this->once()
315
        )->method(
316
            'canUser'
317
        )->with(
318
            $this->equalTo('content'),
319
            $this->equalTo('urltranslator'),
320
            $this->equalTo($wildcard)
321
        )->will(
322
            $this->returnValue(true)
323
        );
324
325
        $repositoryMock = $this->getRepositoryMock();
326
        $repositoryMock->expects($this->once())->method('beginTransaction');
327
        $repositoryMock->expects($this->once())->method('commit');
328
329
        $this->urlWildcardHandler->expects(
330
            $this->once()
331
        )->method(
332
            'remove'
333
        )->with(
334
            $this->equalTo('McBomb')
335
        );
336
337
        $mockedService->remove($wildcard);
338
    }
339
340
    /**
341
     * Test for the remove() method.
342
     *
343
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::remove
344
     */
345
    public function testRemoveWithRollback()
346
    {
347
        $wildcard = new URLWildcard(['id' => 'McBoo']);
348
349
        $mockedService = $this->getPartlyMockedURLWildcardService();
350
351
        $this->permissionResolver->expects(
352
            $this->once()
353
        )->method(
354
            'canUser'
355
        )->with(
356
            $this->equalTo('content'),
357
            $this->equalTo('urltranslator'),
358
            $this->equalTo($wildcard)
359
        )->will(
360
            $this->returnValue(true)
361
        );
362
363
        $repositoryMock = $this->getRepositoryMock();
364
        $repositoryMock->expects($this->once())->method('beginTransaction');
365
        $repositoryMock->expects($this->once())->method('rollback');
366
367
        $this->urlWildcardHandler->expects(
368
            $this->once()
369
        )->method(
370
            'remove'
371
        )->with(
372
            $this->equalTo('McBoo')
373
        )->will(
374
            $this->throwException(new Exception())
375
        );
376
377
        $this->expectException(Exception::class);
378
379
        $mockedService->remove($wildcard);
380
    }
381
382
    /**
383
     * Test for the load() method.
384
     *
385
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::remove
386
     */
387
    public function testLoadThrowsException()
388
    {
389
        $mockedService = $this->getPartlyMockedURLWildcardService();
390
391
        $this->urlWildcardHandler->expects(
392
            $this->once()
393
        )->method(
394
            'load'
395
        )->with(
396
            $this->equalTo('Luigi')
397
        )->will(
398
            $this->throwException(new Exception())
399
        );
400
401
        $this->expectException(Exception::class);
402
403
        $mockedService->load('Luigi');
404
    }
405
406
    /**
407
     * Test for the load() method.
408
     *
409
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::remove
410
     */
411
    public function testLoad()
412
    {
413
        $mockedService = $this->getPartlyMockedURLWildcardService();
414
415
        $this->urlWildcardHandler->expects(
416
            $this->once()
417
        )->method(
418
            'load'
419
        )->with(
420
            $this->equalTo('Luigi')
421
        )->will(
422
            $this->returnValue(
423
                new SPIURLWildcard(
424
                    [
425
                        'id' => 'Luigi',
426
                        'sourceUrl' => 'this',
427
                        'destinationUrl' => 'that',
428
                        'forward' => true,
429
                    ]
430
                )
431
            )
432
        );
433
434
        $urlWildcard = $mockedService->load('Luigi');
435
436
        $this->assertEquals(
437
            new URLWildcard(
438
                [
439
                    'id' => 'Luigi',
440
                    'sourceUrl' => 'this',
441
                    'destinationUrl' => 'that',
442
                    'forward' => true,
443
                ]
444
            ),
445
            $urlWildcard
446
        );
447
    }
448
449
    /**
450
     * Test for the loadAll() method.
451
     *
452
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::loadAll
453
     */
454
    public function testLoadAll()
455
    {
456
        $mockedService = $this->getPartlyMockedURLWildcardService();
457
458
        $this->urlWildcardHandler->expects(
459
            $this->once()
460
        )->method(
461
            'loadAll'
462
        )->with(
463
            $this->equalTo(0),
464
            $this->equalTo(-1)
465
        )->will(
466
            $this->returnValue([])
467
        );
468
469
        $mockedService->loadAll();
470
    }
471
472
    /**
473
     * Test for the loadAll() method.
474
     *
475
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::loadAll
476
     */
477
    public function testLoadAllWithLimitAndOffset()
478
    {
479
        $mockedService = $this->getPartlyMockedURLWildcardService();
480
481
        $this->urlWildcardHandler->expects(
482
            $this->once()
483
        )->method(
484
            'loadAll'
485
        )->with(
486
            $this->equalTo(12),
487
            $this->equalTo(34)
488
        )->will(
489
            $this->returnValue(
490
                [
491
                    new SPIURLWildcard(
492
                        [
493
                            'id' => 'Luigi',
494
                            'sourceUrl' => 'this',
495
                            'destinationUrl' => 'that',
496
                            'forward' => true,
497
                        ]
498
                    ),
499
                ]
500
            )
501
        );
502
503
        $urlWildcards = $mockedService->loadAll(12, 34);
504
505
        $this->assertEquals(
506
            [
507
                new URLWildcard(
508
                    [
509
                        'id' => 'Luigi',
510
                        'sourceUrl' => 'this',
511
                        'destinationUrl' => 'that',
512
                        'forward' => true,
513
                    ]
514
                ),
515
            ],
516
            $urlWildcards
517
        );
518
    }
519
520
    /**
521
     * @return array
522
     */
523
    public function providerForTestTranslateThrowsNotFoundException()
524
    {
525
        return [
526
            [
527
                [
528
                    'sourceUrl' => '/fruit',
529
                    'destinationUrl' => '/food',
530
                    'forward' => true,
531
                ],
532
                '/vegetable',
533
            ],
534
            [
535
                [
536
                    'sourceUrl' => '/fruit/apricot',
537
                    'destinationUrl' => '/food/apricot',
538
                    'forward' => true,
539
                ],
540
                '/fruit/lemon',
541
            ],
542
            [
543
                [
544
                    'sourceUrl' => '/fruit/*',
545
                    'destinationUrl' => '/food/{1}',
546
                    'forward' => true,
547
                ],
548
                '/fruit',
549
            ],
550
            [
551
                [
552
                    'sourceUrl' => '/fruit/*/*',
553
                    'destinationUrl' => '/food/{1}/{2}',
554
                    'forward' => true,
555
                ],
556
                '/fruit/citrus',
557
            ],
558
        ];
559
    }
560
561
    /**
562
     * Test for the translate() method.
563
     *
564
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::translate
565
     * @dataProvider providerForTestTranslateThrowsNotFoundException
566
     */
567
    public function testTranslateThrowsNotFoundException($createArray, $url)
0 ignored issues
show
Unused Code introduced by
The parameter $createArray is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
568
    {
569
        $mockedService = $this->getPartlyMockedURLWildcardService();
570
571
        $trimmedUrl = trim($url, '/ ');
572
573
        $this->urlWildcardHandler
574
            ->expects($this->once())
575
            ->method('translate')
576
            ->with($trimmedUrl)
577
            ->willThrowException(new \eZ\Publish\Core\Base\Exceptions\NotFoundException('UrlWildcard', $trimmedUrl));
578
579
        $this->expectException(NotFoundException::class);
580
581
        $mockedService->translate($url);
582
    }
583
584
    /**
585
     * @return array
586
     */
587
    public function providerForTestTranslate()
588
    {
589
        return [
590
            [
591
                [
592
                    'sourceUrl' => '/fruit/apricot',
593
                    'destinationUrl' => '/food/apricot',
594
                    'forward' => true,
595
                ],
596
                '/fruit/apricot',
597
                '/food/apricot',
598
            ],
599
            [
600
                [
601
                    'sourceUrl' => '/fruit/*',
602
                    'destinationUrl' => '/food/{1}',
603
                    'forward' => true,
604
                ],
605
                '/fruit/citrus',
606
                '/food/citrus',
607
            ],
608
            [
609
                [
610
                    'sourceUrl' => '/fruit/*',
611
                    'destinationUrl' => '/food/{1}',
612
                    'forward' => true,
613
                ],
614
                '/fruit/citrus/orange',
615
                '/food/citrus/orange',
616
            ],
617
            [
618
                [
619
                    'sourceUrl' => '/fruit/*/*',
620
                    'destinationUrl' => '/food/{2}',
621
                    'forward' => true,
622
                ],
623
                '/fruit/citrus/orange',
624
                '/food/orange',
625
            ],
626
            [
627
                [
628
                    'sourceUrl' => '/fruit/*/*',
629
                    'destinationUrl' => '/food/{1}/{2}',
630
                    'forward' => true,
631
                ],
632
                '/fruit/citrus/orange',
633
                '/food/citrus/orange',
634
            ],
635
            [
636
                [
637
                    'sourceUrl' => '/fruit/*/pamplemousse',
638
                    'destinationUrl' => '/food/weird',
639
                    'forward' => true,
640
                ],
641
                '/fruit/citrus/pamplemousse',
642
                '/food/weird',
643
            ],
644
            [
645
                [
646
                    'sourceUrl' => '/fruit/*/pamplemousse',
647
                    'destinationUrl' => '/food/weird/{1}',
648
                    'forward' => true,
649
                ],
650
                '/fruit/citrus/pamplemousse',
651
                '/food/weird/citrus',
652
            ],
653
            [
654
                [
655
                    'sourceUrl' => '/fruit/*/pamplemousse',
656
                    'destinationUrl' => '/food/weird/{1}',
657
                    'forward' => true,
658
                ],
659
                '/fruit/citrus/yellow/pamplemousse',
660
                '/food/weird/citrus/yellow',
661
            ],
662
        ];
663
    }
664
665
    /**
666
     * Test for the translate() method.
667
     *
668
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::translate
669
     * @dataProvider providerForTestTranslate
670
     */
671
    public function testTranslate($createArray, $url, $uri)
672
    {
673
        $mockedService = $this->getPartlyMockedURLWildcardService();
674
675
        $trimmedUrl = trim($url, '/ ');
676
677
        $this->urlWildcardHandler
678
            ->expects($this->once())
679
            ->method('translate')
680
            ->with($trimmedUrl)
681
682
            ->willReturn(new SPIURLWildcard([
683
                'sourceUrl' => $createArray['sourceUrl'],
684
                'destinationUrl' => $uri,
685
                'forward' => $createArray['forward'],
686
            ]));
687
688
        $translationResult = $mockedService->translate($url);
689
690
        $this->assertEquals(
691
            new URLWildcardTranslationResult(
692
                [
693
                    'uri' => $uri,
694
                    'forward' => $createArray['forward'],
695
                ]
696
            ),
697
            $translationResult
698
        );
699
    }
700
701
    /**
702
     * Test for the translate() method.
703
     *
704
     * @covers \eZ\Publish\Core\Repository\URLWildcardService::translate
705
     */
706
    public function testTranslateUsesLongestMatchingWildcard()
707
    {
708
        $mockedService = $this->getPartlyMockedURLWildcardService();
709
710
        $url = '/something/something/thing';
711
        $trimmedUrl = trim($url, '/ ');
712
713
        $this->urlWildcardHandler
714
            ->expects($this->once())
715
            ->method('translate')
716
            ->with($trimmedUrl)
717
            ->willReturn(new SPIURLWildcard([
718
                'destinationUrl' => '/long',
719
                'forward' => false,
720
            ]));
721
722
        $translationResult = $mockedService->translate($url);
723
724
        $this->assertEquals(
725
            new URLWildcardTranslationResult(
726
                [
727
                    'uri' => '/long',
728
                    'forward' => false,
729
                ]
730
            ),
731
            $translationResult
732
        );
733
    }
734
735
    /**
736
     * Returns the content service to test with $methods mocked.
737
     *
738
     * Injected Repository comes from {@see getRepositoryMock()} and persistence handler from {@see getPersistenceMock()}
739
     *
740
     * @param string[] $methods
741
     *
742
     * @return \eZ\Publish\Core\Repository\URLWildcardService|\PHPUnit\Framework\MockObject\MockObject
743
     */
744
    protected function getPartlyMockedURLWildcardService(array $methods = null)
745
    {
746
        return $this->getMockBuilder(URLWildcardService::class)
747
            ->setMethods($methods)
748
            ->setConstructorArgs(
749
                [
750
                    $this->getRepositoryMock(),
751
                    $this->urlWildcardHandler,
752
                    $this->permissionResolver,
753
                ]
754
            )
755
            ->getMock();
756
    }
757
}
758