Completed
Push — master ( 172dbf...184bd5 )
by
unknown
19:10 queued 03:39
created

testFindUrlsUsingVisibleOnlyCriterionReturnsUniqueItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\API\Repository\Tests;
8
9
use eZ\Publish\API\Repository\Values\URL\Query\Criterion;
10
use eZ\Publish\API\Repository\Values\URL\Query\SortClause;
11
use eZ\Publish\API\Repository\Values\URL\URL;
12
use eZ\Publish\API\Repository\Values\URL\URLQuery;
13
use eZ\Publish\API\Repository\Values\URL\URLUpdateStruct;
14
use DateTime;
15
use eZ\Publish\API\Repository\Values\URL\UsageSearchResult;
16
17
/**
18
 * Test case for operations in the UserService using in memory storage.
19
 *
20
 * @see eZ\Publish\API\Repository\URLService
21
 * @group integration
22
 * @group url
23
 */
24
class URLServiceTest extends BaseURLServiceTest
25
{
26
    protected function setUp(): void
27
    {
28
        parent::setUp();
29
30
        $urls = [
31
            [
32
                'name' => 'Twitter',
33
                'url' => 'http://twitter.com/',
34
                'published' => true,
35
                'sectionId' => 1,
36
            ],
37
            [
38
                'name' => 'Facebook',
39
                'url' => 'http://www.facebook.com/',
40
                'published' => true,
41
                'sectionId' => 1,
42
            ],
43
            [
44
                'name' => 'Google',
45
                'url' => 'http://www.google.com/',
46
                'published' => true,
47
                'sectionId' => 1,
48
            ],
49
            [
50
                'name' => 'Vimeo',
51
                'url' => 'http://vimeo.com/',
52
                'published' => true,
53
                'sectionId' => 1,
54
            ],
55
            [
56
                'name' => 'Facebook Sharer',
57
                'url' => 'http://www.facebook.com/sharer.php',
58
                'published' => true,
59
                'sectionId' => 1,
60
            ],
61
            [
62
                'name' => 'Youtube',
63
                'url' => 'http://www.youtube.com/',
64
                'published' => true,
65
                'sectionId' => 1,
66
            ],
67
            [
68
                'name' => 'Googel support',
69
                'url' => 'http://support.google.com/chrome/answer/95647?hl=es',
70
                'published' => true,
71
                'sectionId' => 1,
72
            ],
73
            [
74
                'name' => 'Instagram',
75
                'url' => 'http://instagram.com/',
76
                'published' => true,
77
                'sectionId' => 1,
78
            ],
79
            [
80
                'name' => 'Discuz',
81
                'url' => 'http://www.discuz.net/forum.php',
82
                'published' => true,
83
                'sectionId' => 1,
84
            ],
85
            [
86
                'name' => 'Google calendar',
87
                'url' => 'http://calendar.google.com/calendar/render',
88
                'published' => true,
89
                'sectionId' => 1,
90
            ],
91
            [
92
                'name' => 'Wikipedia',
93
                'url' => 'http://www.wikipedia.org/',
94
                'published' => true,
95
                'sectionId' => 1,
96
            ],
97
            [
98
                'name' => 'Google Analytics',
99
                'url' => 'http://www.google.com/analytics/',
100
                'published' => true,
101
                'sectionId' => 1,
102
            ],
103
            [
104
                'name' => 'nazwa.pl',
105
                'url' => 'http://www.nazwa.pl/',
106
                'published' => true,
107
                'sectionId' => 1,
108
            ],
109
            [
110
                'name' => 'Apache',
111
                'url' => 'http://www.apache.org/',
112
                'published' => true,
113
                'sectionId' => 2,
114
            ],
115
            [
116
                'name' => 'Nginx',
117
                'url' => 'http://www.nginx.com/',
118
                'published' => true,
119
                'sectionId' => 2,
120
            ],
121
            [
122
                'name' => 'Microsoft.com',
123
                'url' => 'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
124
                'published' => true,
125
                'sectionId' => 3,
126
            ],
127
            [
128
                'name' => 'Dropbox',
129
                'url' => 'http://www.dropbox.com/',
130
                'published' => false,
131
                'sectionId' => 3,
132
            ],
133
            [
134
                'name' => 'Google [DE]',
135
                'url' => 'http://www.google.de/',
136
                'published' => true,
137
                'sectionId' => 3,
138
            ],
139
        ];
140
141
        $repository = $this->getRepository();
142
143
        $parentLocationId = $this->generateId('location', 2);
144
145
        $contentService = $repository->getContentService();
146
        $locationService = $repository->getLocationService();
147
148
        $contentType = $repository->getContentTypeService()->loadContentTypeByIdentifier('url');
149
        foreach ($urls as $data) {
150
            $struct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
151
            $struct->setField('name', $data['name']);
152
            $struct->setField('url', $data['url']);
153
            $struct->sectionId = $data['sectionId'];
154
155
            $location = $locationService->newLocationCreateStruct($parentLocationId);
156
157
            $draft = $contentService->createContent($struct, [$location]);
158
            if ($data['published']) {
159
                $contentService->publishVersion($draft->versionInfo);
160
            }
161
        }
162
    }
163
164
    /**
165
     * Test for URLService::findUrls() method.
166
     *
167
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
168
     */
169 View Code Duplication
    public function testFindUrls()
170
    {
171
        $expectedUrls = [
172
            'http://www.apache.org/',
173
            'http://calendar.google.com/calendar/render',
174
            'http://www.dropbox.com/',
175
            '/content/view/sitemap/2',
176
            'http://support.google.com/chrome/answer/95647?hl=es',
177
            'http://www.nazwa.pl/',
178
            'http://www.facebook.com/sharer.php',
179
            'http://www.wikipedia.org/',
180
            'http://www.google.de/',
181
            'http://www.google.com/',
182
            'http://www.nginx.com/',
183
            '/content/view/tagcloud/2',
184
            'http://www.youtube.com/',
185
            'http://vimeo.com/',
186
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
187
            'http://twitter.com/',
188
            'http://www.google.com/analytics/',
189
            'http://www.facebook.com/',
190
            'http://www.discuz.net/forum.php',
191
            'http://instagram.com/',
192
        ];
193
194
        $query = new URLQuery();
195
        $query->filter = new Criterion\MatchAll();
196
197
        $this->doTestFindUrls($query, $expectedUrls);
198
    }
199
200
    /**
201
     * Test for URLService::findUrls() method.
202
     *
203
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
204
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
205
     */
206
    public function testFindUrlsUsingMatchNone()
207
    {
208
        $query = new URLQuery();
209
        $query->filter = new Criterion\MatchNone();
210
211
        $this->doTestFindUrls($query, []);
212
    }
213
214
    /**
215
     * Test for URLService::findUrls() method.
216
     *
217
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
218
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
219
     */
220 View Code Duplication
    public function testFindUrlsUsingPatternCriterion()
221
    {
222
        $expectedUrls = [
223
            'http://www.google.de/',
224
            'http://www.google.com/',
225
            'http://support.google.com/chrome/answer/95647?hl=es',
226
            'http://calendar.google.com/calendar/render',
227
            'http://www.google.com/analytics/',
228
        ];
229
230
        $query = new URLQuery();
231
        $query->filter = new Criterion\Pattern('google');
232
233
        $this->doTestFindUrls($query, $expectedUrls);
234
    }
235
236
    /**
237
     * Test for URLService::findUrls() method.
238
     *
239
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
240
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
241
     */
242 View Code Duplication
    public function testFindUrlsUsingValidityCriterionValid()
243
    {
244
        $expectedUrls = [
245
            'http://www.google.com/',
246
            '/content/view/sitemap/2',
247
            'http://support.google.com/chrome/answer/95647?hl=es',
248
            'http://www.google.de/',
249
            'http://www.nginx.com/',
250
            'http://www.google.com/analytics/',
251
            'http://www.discuz.net/forum.php',
252
            'http://www.wikipedia.org/',
253
            'http://www.facebook.com/sharer.php',
254
            'http://twitter.com/',
255
            'http://www.nazwa.pl/',
256
            'http://instagram.com/',
257
            'http://www.apache.org/',
258
            'http://www.dropbox.com/',
259
            'http://www.facebook.com/',
260
            'http://www.youtube.com/',
261
            'http://calendar.google.com/calendar/render',
262
            'http://vimeo.com/',
263
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
264
        ];
265
266
        $query = new URLQuery();
267
        $query->filter = new Criterion\Validity(true);
268
269
        $this->doTestFindUrls($query, $expectedUrls);
270
    }
271
272
    /**
273
     * Test for URLService::findUrls() method.
274
     *
275
     * @covers \eZ\Publish\Core\Repository\URLService::findUrls
276
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
277
     */
278 View Code Duplication
    public function testFindUrlsUsingSectionIdCriterion(): void
279
    {
280
        $expectedUrls = [
281
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
282
            'http://www.dropbox.com/',
283
            'http://www.google.de/',
284
        ];
285
286
        $query = new URLQuery();
287
        $query->filter = new Criterion\SectionId([3]);
288
289
        $this->doTestFindUrls($query, $expectedUrls);
290
    }
291
292
    /**
293
     * Test for URLService::findUrls() method.
294
     *
295
     * @covers \eZ\Publish\Core\Repository\URLService::findUrls()
296
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
297
     */
298
    public function testFindUrlsUsingSectionIdAndValidityCriterionValid(): void
299
    {
300
        $expectedUrls = [
301
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
302
            'http://www.dropbox.com/',
303
            'http://www.google.de/',
304
        ];
305
306
        $query = new URLQuery();
307
        $query->filter = new Criterion\LogicalAnd([
308
            new Criterion\SectionId([3]),
309
            new Criterion\Validity(true),
310
        ]);
311
312
        $this->doTestFindUrls($query, $expectedUrls);
313
    }
314
315
    /**
316
     * Test for URLService::findUrls() method.
317
     *
318
     * @covers \eZ\Publish\Core\Repository\URLService::findUrls
319
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
320
     */
321 View Code Duplication
    public function testFindUrlsUsingSectionIdentifierCriterion(): void
322
    {
323
        $expectedUrls = [
324
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
325
            'http://www.dropbox.com/',
326
            'http://www.google.de/',
327
        ];
328
329
        $query = new URLQuery();
330
        $query->filter = new Criterion\SectionIdentifier(['media']);
331
332
        $this->doTestFindUrls($query, $expectedUrls);
333
    }
334
335
    /**
336
     * Test for URLService::findUrls() method.
337
     *
338
     * @covers \eZ\Publish\Core\Repository\URLService::findUrls()
339
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
340
     */
341 View Code Duplication
    public function testFindUrlsUsingSectionIdentifierAndValidityCriterionValid(): void
342
    {
343
        $expectedUrls = [
344
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
345
            'http://www.dropbox.com/',
346
            'http://www.google.de/',
347
            'http://www.apache.org/',
348
            'http://www.nginx.com/',
349
        ];
350
351
        $query = new URLQuery();
352
        $query->filter = new Criterion\LogicalAnd([
353
            new Criterion\SectionIdentifier(['media', 'users']),
354
            new Criterion\Validity(true),
355
        ]);
356
357
        $this->doTestFindUrls($query, $expectedUrls);
358
    }
359
360
    /**
361
     * Test for URLService::findUrls() method.
362
     *
363
     * @covers \eZ\Publish\Core\Repository\URLService::findUrls()
364
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
365
     */
366 View Code Duplication
    public function testFindUrlsUsingSectionIdentifierOrSectionIdCriterion(): void
367
    {
368
        $expectedUrls = [
369
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
370
            'http://www.dropbox.com/',
371
            'http://www.google.de/',
372
            'http://www.apache.org/',
373
            'http://www.nginx.com/',
374
        ];
375
376
        $query = new URLQuery();
377
        $query->filter = new Criterion\LogicalOr([
378
            new Criterion\SectionIdentifier(['media']),
379
            new Criterion\SectionId([2]),
380
        ]);
381
382
        $this->doTestFindUrls($query, $expectedUrls);
383
    }
384
385
    /**
386
     * Test for URLService::findUrls() method.
387
     *
388
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
389
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
390
     */
391
    public function testFindUrlsUsingValidityCriterionInvalid()
392
    {
393
        $expectedUrls = [
394
            '/content/view/tagcloud/2',
395
        ];
396
397
        $query = new URLQuery();
398
        $query->filter = new Criterion\Validity(false);
399
400
        $this->doTestFindUrls($query, $expectedUrls);
401
    }
402
403
    /**
404
     * Test for URLService::findUrls() method.
405
     *
406
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
407
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
408
     */
409 View Code Duplication
    public function testFindUrlsUsingVisibleOnlyCriterion()
410
    {
411
        $expectedUrls = [
412
            'http://vimeo.com/',
413
            'http://calendar.google.com/calendar/render',
414
            'http://www.facebook.com/',
415
            'http://www.google.com/',
416
            'http://www.google.com/analytics/',
417
            'http://www.facebook.com/sharer.php',
418
            'http://www.apache.org/',
419
            'http://www.nginx.com/',
420
            'http://www.wikipedia.org/',
421
            'http://www.youtube.com/',
422
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
423
            'http://www.google.de/',
424
            'http://instagram.com/',
425
            'http://www.nazwa.pl/',
426
            '/content/view/tagcloud/2',
427
            'http://www.discuz.net/forum.php',
428
            'http://support.google.com/chrome/answer/95647?hl=es',
429
            'http://twitter.com/',
430
            '/content/view/sitemap/2',
431
        ];
432
433
        $query = new URLQuery();
434
        $query->filter = new Criterion\VisibleOnly();
435
436
        $this->doTestFindUrls($query, $expectedUrls);
437
    }
438
439
    /**
440
     * @see https://jira.ez.no/browse/EZP-31059
441
     */
442
    public function testFindUrlsUsingVisibleOnlyCriterionReturnsUniqueItems(): void
443
    {
444
        $exampleUrl = 'http://ezplatform.com';
445
446
        $this->createContentWithLink('A', $exampleUrl);
447
        $this->createContentWithLink('B', $exampleUrl);
448
449
        $urlService = $this->getRepository()->getURLService();
450
451
        $query = new URLQuery();
452
        $query->filter = new Criterion\VisibleOnly();
453
        $query->limit = -1;
454
455
        $results = $urlService->findUrls($query);
456
457
        $this->assertSearchResultItemsAreUnique($results);
458
    }
459
460
    /**
461
     * Test for URLService::findUrls() method.
462
     *
463
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
464
     */
465 View Code Duplication
    public function testFindUrlsWithInvalidOffsetThrowsInvalidArgumentException()
466
    {
467
        $this->expectException(\eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue::class);
468
469
        $query = new URLQuery();
470
        $query->filter = new Criterion\MatchAll();
471
        $query->offset = 'invalid!';
0 ignored issues
show
Documentation Bug introduced by
The property $offset was declared of type integer, but 'invalid!' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
472
473
        $repository = $this->getRepository();
474
475
        /* BEGIN: Use Case */
476
        $urlService = $repository->getURLService();
477
        // This call will fail with a InvalidArgumentException
478
        $urlService->findUrls($query);
479
        /* END: Use Case */
480
    }
481
482
    /**
483
     * Test for URLService::findUrls() method.
484
     *
485
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
486
     */
487 View Code Duplication
    public function testFindUrlsWithInvalidLimitThrowsInvalidArgumentException()
488
    {
489
        $this->expectException(\eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue::class);
490
491
        $query = new URLQuery();
492
        $query->filter = new Criterion\MatchAll();
493
        $query->limit = 'invalid!';
0 ignored issues
show
Documentation Bug introduced by
The property $limit was declared of type integer, but 'invalid!' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
494
495
        $repository = $this->getRepository();
496
497
        /* BEGIN: Use Case */
498
        $urlService = $repository->getURLService();
499
        // This call will fail with a InvalidArgumentException
500
        $urlService->findUrls($query);
501
        /* END: Use Case */
502
    }
503
504
    /**
505
     * Test for URLService::findUrls() method.
506
     *
507
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
508
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
509
     */
510
    public function testFindUrlsWithOffset()
511
    {
512
        $expectedUrls = [
513
            'http://www.discuz.net/forum.php',
514
            'http://calendar.google.com/calendar/render',
515
            'http://www.wikipedia.org/',
516
            'http://www.google.com/analytics/',
517
            'http://www.nazwa.pl/',
518
            'http://www.apache.org/',
519
            'http://www.nginx.com/',
520
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
521
            'http://www.dropbox.com/',
522
            'http://www.google.de/',
523
        ];
524
525
        $query = new URLQuery();
526
        $query->filter = new Criterion\MatchAll();
527
        $query->offset = 10;
528
        $query->sortClauses = [new SortClause\Id()];
529
530
        $this->doTestFindUrls($query, $expectedUrls, 20);
531
    }
532
533
    /**
534
     * Test for URLService::findUrls() method.
535
     *
536
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
537
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
538
     */
539
    public function testFindUrlsWithOffsetAndLimit()
540
    {
541
        $expectedUrls = [
542
            'http://www.discuz.net/forum.php',
543
            'http://calendar.google.com/calendar/render',
544
            'http://www.wikipedia.org/',
545
        ];
546
547
        $query = new URLQuery();
548
        $query->filter = new Criterion\MatchAll();
549
        $query->offset = 10;
550
        $query->limit = 3;
551
        $query->sortClauses = [new SortClause\Id()];
552
553
        $this->doTestFindUrls($query, $expectedUrls, 20);
554
    }
555
556
    /**
557
     * Test for URLService::findUrls() method.
558
     *
559
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
560
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
561
     */
562
    public function testFindUrlsWithLimitZero()
563
    {
564
        $query = new URLQuery();
565
        $query->filter = new Criterion\MatchAll();
566
        $query->limit = 0;
567
568
        $this->doTestFindUrls($query, [], 20);
569
    }
570
571
    /**
572
     * Test for URLService::findUrls() method.
573
     *
574
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
575
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
576
     * @dataProvider dataProviderForFindUrlsWithSorting
577
     */
578
    public function testFindUrlsWithSorting(SortClause $sortClause, array $expectedUrls)
579
    {
580
        $query = new URLQuery();
581
        $query->filter = new Criterion\MatchAll();
582
        $query->sortClauses = [$sortClause];
583
584
        $this->doTestFindUrls($query, $expectedUrls, null, false);
585
    }
586
587
    public function dataProviderForFindUrlsWithSorting()
588
    {
589
        $urlsSortedById = [
590
            '/content/view/sitemap/2',
591
            '/content/view/tagcloud/2',
592
            'http://twitter.com/',
593
            'http://www.facebook.com/',
594
            'http://www.google.com/',
595
            'http://vimeo.com/',
596
            'http://www.facebook.com/sharer.php',
597
            'http://www.youtube.com/',
598
            'http://support.google.com/chrome/answer/95647?hl=es',
599
            'http://instagram.com/',
600
            'http://www.discuz.net/forum.php',
601
            'http://calendar.google.com/calendar/render',
602
            'http://www.wikipedia.org/',
603
            'http://www.google.com/analytics/',
604
            'http://www.nazwa.pl/',
605
            'http://www.apache.org/',
606
            'http://www.nginx.com/',
607
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
608
            'http://www.dropbox.com/',
609
            'http://www.google.de/',
610
        ];
611
612
        $urlsSortedByURL = $urlsSortedById;
613
        sort($urlsSortedByURL);
614
615
        return [
616
            [new SortClause\Id(SortClause::SORT_ASC), $urlsSortedById],
617
            [new SortClause\Id(SortClause::SORT_DESC), array_reverse($urlsSortedById)],
618
            [new SortClause\URL(SortClause::SORT_ASC), $urlsSortedByURL],
619
            [new SortClause\URL(SortClause::SORT_DESC), array_reverse($urlsSortedByURL)],
620
        ];
621
    }
622
623
    /**
624
     * Test for URLService::updateUrl() method.
625
     *
626
     * @see \eZ\Publish\Core\Repository\URLService::updateUrl()
627
     */
628
    public function testUpdateUrl()
629
    {
630
        $repository = $this->getRepository();
631
632
        $id = $this->generateId('url', 23);
633
634
        /* BEGIN: Use Case */
635
        $urlService = $repository->getURLService();
636
637
        $urlBeforeUpdate = $urlService->loadById($id);
638
        $updateStruct = $urlService->createUpdateStruct();
639
        $updateStruct->url = 'https://someurl.com/';
640
641
        $urlAfterUpdate = $urlService->updateUrl($urlBeforeUpdate, $updateStruct);
642
        /* END: Use Case */
643
644
        $this->assertInstanceOf(URL::class, $urlAfterUpdate);
645
        $this->assertPropertiesCorrect([
646
            'id' => 23,
647
            'url' => 'https://someurl.com/',
648
            // (!) URL status should be reset to valid nad never checked
649
            'isValid' => true,
650
            'lastChecked' => null,
651
            'created' => new DateTime('@1343140541'),
652
        ], $urlAfterUpdate);
653
        $this->assertGreaterThanOrEqual($urlBeforeUpdate->modified, $urlAfterUpdate->modified);
654
    }
655
656
    /**
657
     * Test for URLService::updateUrl() method.
658
     *
659
     * @see \eZ\Publish\Core\Repository\URLService::updateUrl()
660
     */
661
    public function testUpdateUrlStatus()
662
    {
663
        $repository = $this->getRepository();
664
665
        $id = $this->generateId('url', 23);
666
        $checked = new DateTime('@' . time());
667
668
        /* BEGIN: Use Case */
669
        $urlService = $repository->getURLService();
670
671
        $urlBeforeUpdate = $urlService->loadById($id);
672
673
        $updateStruct = $urlService->createUpdateStruct();
674
        $updateStruct->isValid = false;
675
        $updateStruct->lastChecked = $checked;
676
677
        $urlAfterUpdate = $urlService->updateUrl($urlBeforeUpdate, $updateStruct);
678
        /* END: Use Case */
679
680
        $this->assertInstanceOf(URL::class, $urlAfterUpdate);
681
        $this->assertPropertiesCorrect([
682
            'id' => $id,
683
            'url' => '/content/view/sitemap/2',
684
            // (!) URL status should be reset to valid nad never checked
685
            'isValid' => false,
686
            'lastChecked' => $checked,
687
            'created' => new DateTime('@1343140541'),
688
        ], $urlAfterUpdate);
689
        $this->assertGreaterThanOrEqual($urlBeforeUpdate->modified, $urlAfterUpdate->modified);
690
    }
691
692
    /**
693
     * Test for URLService::updateUrl() method.
694
     *
695
     * @see \eZ\Publish\Core\Repository\URLService::updateUrl()
696
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testUpdateUrl
697
     */
698
    public function testUpdateUrlWithNonUniqueUrl()
699
    {
700
        $this->expectException(\eZ\Publish\Core\Base\Exceptions\InvalidArgumentException::class);
701
702
        $repository = $this->getRepository();
703
704
        $id = $this->generateId('url', 23);
705
706
        /* BEGIN: Use Case */
707
        $urlService = $repository->getURLService();
708
709
        $urlBeforeUpdate = $urlService->loadById($id);
710
        $updateStruct = $urlService->createUpdateStruct();
711
        $updateStruct->url = 'http://www.youtube.com/';
712
713
        // This call will fail with a InvalidArgumentException
714
        $urlService->updateUrl($urlBeforeUpdate, $updateStruct);
715
        /* END: Use Case */
716
    }
717
718
    /**
719
     * Test for URLService::loadById() method.
720
     *
721
     * @see \eZ\Publish\Core\Repository\URLService::loadById
722
     */
723 View Code Duplication
    public function testLoadById()
724
    {
725
        $repository = $this->getRepository();
726
727
        $id = $this->generateId('url', 23);
728
729
        /* BEGIN: Use Case */
730
        $urlService = $repository->getURLService();
731
732
        $url = $urlService->loadById($id);
733
        /* END: Use Case */
734
735
        $this->assertInstanceOf(URL::class, $url);
736
        $this->assertPropertiesCorrect([
737
            'id' => 23,
738
            'url' => '/content/view/sitemap/2',
739
            'isValid' => true,
740
            'lastChecked' => null,
741
            'created' => new DateTime('@1343140541'),
742
            'modified' => new DateTime('@1343140541'),
743
        ], $url);
744
    }
745
746
    /**
747
     * Test for URLService::loadById() method.
748
     *
749
     * @see \eZ\Publish\Core\Repository\URLService::loadById
750
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testLoadById
751
     */
752
    public function testLoadByIdThrowsNotFoundException()
753
    {
754
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
755
756
        $repository = $this->getRepository();
757
758
        $nonExistentUrlId = $this->generateId('url', self::DB_INT_MAX);
759
        /* BEGIN: Use Case */
760
        $urlService = $repository->getURLService();
761
762
        // This call will fail with a NotFoundException
763
        $url = $urlService->loadById($nonExistentUrlId);
0 ignored issues
show
Unused Code introduced by
$url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
764
        /* END: Use Case */
765
    }
766
767
    /**
768
     * Test for URLService::loadByUrl() method.
769
     *
770
     * @see \eZ\Publish\Core\Repository\URLService::loadByUrl
771
     */
772 View Code Duplication
    public function testLoadByUrl()
773
    {
774
        $repository = $this->getRepository();
775
776
        $urlAddr = '/content/view/sitemap/2';
777
        /* BEGIN: Use Case */
778
        $urlService = $repository->getURLService();
779
780
        $url = $urlService->loadByUrl($urlAddr);
781
782
        /* END: Use Case */
783
784
        $this->assertInstanceOf(URL::class, $url);
785
        $this->assertPropertiesCorrect([
786
            'id' => 23,
787
            'url' => '/content/view/sitemap/2',
788
            'isValid' => true,
789
            'lastChecked' => null,
790
            'created' => new DateTime('@1343140541'),
791
            'modified' => new DateTime('@1343140541'),
792
        ], $url);
793
    }
794
795
    /**
796
     * Test for URLService::loadByUrl() method.
797
     *
798
     * @see \eZ\Publish\Core\Repository\URLService::loadByUrl
799
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testLoadByUrl
800
     */
801
    public function testLoadByUrlThrowsNotFoundException()
802
    {
803
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
804
805
        $repository = $this->getRepository();
806
807
        $nonExistentUrl = 'https://laravel.com/';
808
        /* BEGIN: Use Case */
809
        $urlService = $repository->getURLService();
810
811
        // This call will fail with a NotFoundException
812
        $url = $urlService->loadByUrl($nonExistentUrl);
0 ignored issues
show
Unused Code introduced by
$url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
813
        /* END: Use Case */
814
    }
815
816
    /**
817
     * Test for URLService::createUpdateStruct() method.
818
     *
819
     * @see \eZ\Publish\API\Repository\URLService::createUpdateStruct
820
     *
821
     * @return \eZ\Publish\API\Repository\Values\URL\URLUpdateStruct
822
     */
823
    public function testCreateUpdateStruct()
824
    {
825
        $repository = $this->getRepository();
826
827
        /* BEGIN: Use Case */
828
        $urlService = $repository->getURLService();
829
        $updateStruct = $urlService->createUpdateStruct();
830
        /* END: Use Case */
831
832
        $this->assertInstanceOf(URLUpdateStruct::class, $updateStruct);
833
834
        return $updateStruct;
835
    }
836
837
    /**
838
     * Test for URLService::createUpdateStruct() method.
839
     *
840
     * @param \eZ\Publish\API\Repository\Values\URL\URLUpdateStruct $updateStruct
841
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testCreateUpdateStruct
842
     */
843
    public function testCreateUpdateStructValues(URLUpdateStruct $updateStruct)
844
    {
845
        $this->assertPropertiesCorrect([
846
            'url' => null,
847
            'isValid' => null,
848
            'lastChecked' => null,
849
        ], $updateStruct);
850
    }
851
852
    /**
853
     * Test for URLService::testFindUsages() method.
854
     *
855
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testLoadById
856
     * @dataProvider dataProviderForFindUsages
857
     */
858
    public function testFindUsages($urlId, $offset, $limit, array $expectedContentInfos, $expectedTotalCount = null)
859
    {
860
        $repository = $this->getRepository();
861
862
        $id = $this->generateId('url', $urlId);
863
        /* BEGIN: Use Case */
864
        $urlService = $repository->getURLService();
865
866
        $loadedUrl = $urlService->loadById($id);
867
868
        $usagesSearchResults = $urlService->findUsages($loadedUrl, $offset, $limit);
869
        /* END: Use Case */
870
871
        $this->assertInstanceOf(UsageSearchResult::class, $usagesSearchResults);
872
        $this->assertEquals($expectedTotalCount, $usagesSearchResults->totalCount);
873
        $this->assertUsagesSearchResultItems($usagesSearchResults, $expectedContentInfos);
874
    }
875
876
    public function dataProviderForFindUsages()
877
    {
878
        return [
879
            // findUsages($url, 0, -1)
880
            [23, 0, -1, [54], 1],
881
            // findUsages($url, 0, $limit)
882
            [23, 0, 1, [54], 1],
883
        ];
884
    }
885
886
    /**
887
     * Test for URLService::testFindUsages() method.
888
     *
889
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUsages
890
     */
891
    public function testFindUsagesReturnsEmptySearchResults()
892
    {
893
        $repository = $this->getRepository();
894
895
        /* BEGIN: Use Case */
896
        $urlService = $repository->getURLService();
897
898
        $loadedUrl = $urlService->loadByUrl('http://www.dropbox.com/');
899
900
        $usagesSearchResults = $urlService->findUsages($loadedUrl);
901
        /* END: Use Case */
902
903
        $this->assertInstanceOf(UsageSearchResult::class, $usagesSearchResults);
904
        $this->assertPropertiesCorrect([
905
            'totalCount' => 0,
906
            'items' => [],
907
        ], $usagesSearchResults);
908
    }
909
}
910