Completed
Push — 7.5 ( d9c892...05f391 )
by
unknown
53:52 queued 33:37
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
    public function setUp()
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
     * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue
465
     */
466 View Code Duplication
    public function testFindUrlsWithInvalidOffsetThrowsInvalidArgumentException()
467
    {
468
        $query = new URLQuery();
469
        $query->filter = new Criterion\MatchAll();
470
        $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...
471
472
        $repository = $this->getRepository();
473
474
        /* BEGIN: Use Case */
475
        $urlService = $repository->getURLService();
476
        // This call will fail with a InvalidArgumentException
477
        $urlService->findUrls($query);
478
        /* END: Use Case */
479
    }
480
481
    /**
482
     * Test for URLService::findUrls() method.
483
     *
484
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
485
     * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue
486
     */
487 View Code Duplication
    public function testFindUrlsWithInvalidLimitThrowsInvalidArgumentException()
488
    {
489
        $query = new URLQuery();
490
        $query->filter = new Criterion\MatchAll();
491
        $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...
492
493
        $repository = $this->getRepository();
494
495
        /* BEGIN: Use Case */
496
        $urlService = $repository->getURLService();
497
        // This call will fail with a InvalidArgumentException
498
        $urlService->findUrls($query);
499
        /* END: Use Case */
500
    }
501
502
    /**
503
     * Test for URLService::findUrls() method.
504
     *
505
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
506
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
507
     */
508
    public function testFindUrlsWithOffset()
509
    {
510
        $expectedUrls = [
511
            'http://www.discuz.net/forum.php',
512
            'http://calendar.google.com/calendar/render',
513
            'http://www.wikipedia.org/',
514
            'http://www.google.com/analytics/',
515
            'http://www.nazwa.pl/',
516
            'http://www.apache.org/',
517
            'http://www.nginx.com/',
518
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
519
            'http://www.dropbox.com/',
520
            'http://www.google.de/',
521
        ];
522
523
        $query = new URLQuery();
524
        $query->filter = new Criterion\MatchAll();
525
        $query->offset = 10;
526
        $query->sortClauses = [new SortClause\Id()];
527
528
        $this->doTestFindUrls($query, $expectedUrls, 20);
529
    }
530
531
    /**
532
     * Test for URLService::findUrls() method.
533
     *
534
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
535
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
536
     */
537
    public function testFindUrlsWithOffsetAndLimit()
538
    {
539
        $expectedUrls = [
540
            'http://www.discuz.net/forum.php',
541
            'http://calendar.google.com/calendar/render',
542
            'http://www.wikipedia.org/',
543
        ];
544
545
        $query = new URLQuery();
546
        $query->filter = new Criterion\MatchAll();
547
        $query->offset = 10;
548
        $query->limit = 3;
549
        $query->sortClauses = [new SortClause\Id()];
550
551
        $this->doTestFindUrls($query, $expectedUrls, 20);
552
    }
553
554
    /**
555
     * Test for URLService::findUrls() method.
556
     *
557
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
558
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
559
     */
560
    public function testFindUrlsWithLimitZero()
561
    {
562
        $query = new URLQuery();
563
        $query->filter = new Criterion\MatchAll();
564
        $query->limit = 0;
565
566
        $this->doTestFindUrls($query, [], 20);
567
    }
568
569
    /**
570
     * Test for URLService::findUrls() method.
571
     *
572
     * @see \eZ\Publish\Core\Repository\URLService::findUrls()
573
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUrls
574
     * @dataProvider dataProviderForFindUrlsWithSorting
575
     */
576
    public function testFindUrlsWithSorting(SortClause $sortClause, array $expectedUrls)
577
    {
578
        $query = new URLQuery();
579
        $query->filter = new Criterion\MatchAll();
580
        $query->sortClauses = [$sortClause];
581
582
        $this->doTestFindUrls($query, $expectedUrls, null, false);
583
    }
584
585
    public function dataProviderForFindUrlsWithSorting()
586
    {
587
        $urlsSortedById = [
588
            '/content/view/sitemap/2',
589
            '/content/view/tagcloud/2',
590
            'http://twitter.com/',
591
            'http://www.facebook.com/',
592
            'http://www.google.com/',
593
            'http://vimeo.com/',
594
            'http://www.facebook.com/sharer.php',
595
            'http://www.youtube.com/',
596
            'http://support.google.com/chrome/answer/95647?hl=es',
597
            'http://instagram.com/',
598
            'http://www.discuz.net/forum.php',
599
            'http://calendar.google.com/calendar/render',
600
            'http://www.wikipedia.org/',
601
            'http://www.google.com/analytics/',
602
            'http://www.nazwa.pl/',
603
            'http://www.apache.org/',
604
            'http://www.nginx.com/',
605
            'http://windows.microsoft.com/en-US/internet-explorer/products/ie/home',
606
            'http://www.dropbox.com/',
607
            'http://www.google.de/',
608
        ];
609
610
        $urlsSortedByURL = $urlsSortedById;
611
        sort($urlsSortedByURL);
612
613
        return [
614
            [new SortClause\Id(SortClause::SORT_ASC), $urlsSortedById],
615
            [new SortClause\Id(SortClause::SORT_DESC), array_reverse($urlsSortedById)],
616
            [new SortClause\URL(SortClause::SORT_ASC), $urlsSortedByURL],
617
            [new SortClause\URL(SortClause::SORT_DESC), array_reverse($urlsSortedByURL)],
618
        ];
619
    }
620
621
    /**
622
     * Test for URLService::updateUrl() method.
623
     *
624
     * @see \eZ\Publish\Core\Repository\URLService::updateUrl()
625
     */
626
    public function testUpdateUrl()
627
    {
628
        $repository = $this->getRepository();
629
630
        $id = $this->generateId('url', 23);
631
632
        /* BEGIN: Use Case */
633
        $urlService = $repository->getURLService();
634
635
        $urlBeforeUpdate = $urlService->loadById($id);
636
        $updateStruct = $urlService->createUpdateStruct();
637
        $updateStruct->url = 'https://someurl.com/';
638
639
        $urlAfterUpdate = $urlService->updateUrl($urlBeforeUpdate, $updateStruct);
640
        /* END: Use Case */
641
642
        $this->assertInstanceOf(URL::class, $urlAfterUpdate);
643
        $this->assertPropertiesCorrect([
644
            'id' => 23,
645
            'url' => 'https://someurl.com/',
646
            // (!) URL status should be reset to valid nad never checked
647
            'isValid' => true,
648
            'lastChecked' => null,
649
            'created' => new DateTime('@1343140541'),
650
        ], $urlAfterUpdate);
651
        $this->assertGreaterThanOrEqual($urlBeforeUpdate->modified, $urlAfterUpdate->modified);
652
    }
653
654
    /**
655
     * Test for URLService::updateUrl() method.
656
     *
657
     * @see \eZ\Publish\Core\Repository\URLService::updateUrl()
658
     */
659
    public function testUpdateUrlStatus()
660
    {
661
        $repository = $this->getRepository();
662
663
        $id = $this->generateId('url', 23);
664
        $checked = new DateTime('@' . time());
665
666
        /* BEGIN: Use Case */
667
        $urlService = $repository->getURLService();
668
669
        $urlBeforeUpdate = $urlService->loadById($id);
670
        $updateStruct = $urlService->createUpdateStruct();
671
        $updateStruct->isValid = false;
672
        $updateStruct->lastChecked = $checked;
673
674
        $urlAfterUpdate = $urlService->updateUrl($urlBeforeUpdate, $updateStruct);
675
        /* END: Use Case */
676
677
        $this->assertInstanceOf(URL::class, $urlAfterUpdate);
678
        $this->assertPropertiesCorrect([
679
            'id' => $id,
680
            'url' => '/content/view/sitemap/2',
681
            // (!) URL status should be reset to valid nad never checked
682
            'isValid' => false,
683
            'lastChecked' => $checked,
684
            'created' => new DateTime('@1343140541'),
685
        ], $urlAfterUpdate);
686
        $this->assertGreaterThanOrEqual($urlBeforeUpdate->modified, $urlAfterUpdate->modified);
687
    }
688
689
    /**
690
     * Test for URLService::updateUrl() method.
691
     *
692
     * @see \eZ\Publish\Core\Repository\URLService::updateUrl()
693
     * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
694
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testUpdateUrl
695
     */
696
    public function testUpdateUrlWithNonUniqueUrl()
697
    {
698
        $repository = $this->getRepository();
699
700
        $id = $this->generateId('url', 23);
701
702
        /* BEGIN: Use Case */
703
        $urlService = $repository->getURLService();
704
705
        $urlBeforeUpdate = $urlService->loadById($id);
706
        $updateStruct = $urlService->createUpdateStruct();
707
        $updateStruct->url = 'http://www.youtube.com/';
708
709
        // This call will fail with a InvalidArgumentException
710
        $urlService->updateUrl($urlBeforeUpdate, $updateStruct);
711
        /* END: Use Case */
712
    }
713
714
    /**
715
     * Test for URLService::loadById() method.
716
     *
717
     * @see \eZ\Publish\Core\Repository\URLService::loadById
718
     */
719 View Code Duplication
    public function testLoadById()
720
    {
721
        $repository = $this->getRepository();
722
723
        $id = $this->generateId('url', 23);
724
725
        /* BEGIN: Use Case */
726
        $urlService = $repository->getURLService();
727
728
        $url = $urlService->loadById($id);
729
        /* END: Use Case */
730
731
        $this->assertInstanceOf(URL::class, $url);
732
        $this->assertPropertiesCorrect([
733
            'id' => 23,
734
            'url' => '/content/view/sitemap/2',
735
            'isValid' => true,
736
            'lastChecked' => null,
737
            'created' => new DateTime('@1343140541'),
738
            'modified' => new DateTime('@1343140541'),
739
        ], $url);
740
    }
741
742
    /**
743
     * Test for URLService::loadById() method.
744
     *
745
     * @see \eZ\Publish\Core\Repository\URLService::loadById
746
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
747
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testLoadById
748
     */
749
    public function testLoadByIdThrowsNotFoundException()
750
    {
751
        $repository = $this->getRepository();
752
753
        $nonExistentUrlId = $this->generateId('url', self::DB_INT_MAX);
754
        /* BEGIN: Use Case */
755
        $urlService = $repository->getURLService();
756
757
        // This call will fail with a NotFoundException
758
        $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...
759
        /* END: Use Case */
760
    }
761
762
    /**
763
     * Test for URLService::loadByUrl() method.
764
     *
765
     * @see \eZ\Publish\Core\Repository\URLService::loadByUrl
766
     */
767 View Code Duplication
    public function testLoadByUrl()
768
    {
769
        $repository = $this->getRepository();
770
771
        $urlAddr = '/content/view/sitemap/2';
772
        /* BEGIN: Use Case */
773
        $urlService = $repository->getURLService();
774
775
        $url = $urlService->loadByUrl($urlAddr);
776
777
        /* END: Use Case */
778
779
        $this->assertInstanceOf(URL::class, $url);
780
        $this->assertPropertiesCorrect([
781
            'id' => 23,
782
            'url' => '/content/view/sitemap/2',
783
            'isValid' => true,
784
            'lastChecked' => null,
785
            'created' => new DateTime('@1343140541'),
786
            'modified' => new DateTime('@1343140541'),
787
        ], $url);
788
    }
789
790
    /**
791
     * Test for URLService::loadByUrl() method.
792
     *
793
     * @see \eZ\Publish\Core\Repository\URLService::loadByUrl
794
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
795
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testLoadByUrl
796
     */
797
    public function testLoadByUrlThrowsNotFoundException()
798
    {
799
        $repository = $this->getRepository();
800
801
        $nonExistentUrl = 'https://laravel.com/';
802
        /* BEGIN: Use Case */
803
        $urlService = $repository->getURLService();
804
805
        // This call will fail with a NotFoundException
806
        $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...
807
        /* END: Use Case */
808
    }
809
810
    /**
811
     * Test for URLService::createUpdateStruct() method.
812
     *
813
     * @see \eZ\Publish\API\Repository\URLService::createUpdateStruct
814
     *
815
     * @return \eZ\Publish\API\Repository\Values\URL\URLUpdateStruct
816
     */
817
    public function testCreateUpdateStruct()
818
    {
819
        $repository = $this->getRepository();
820
821
        /* BEGIN: Use Case */
822
        $urlService = $repository->getURLService();
823
        $updateStruct = $urlService->createUpdateStruct();
824
        /* END: Use Case */
825
826
        $this->assertInstanceOf(URLUpdateStruct::class, $updateStruct);
827
828
        return $updateStruct;
829
    }
830
831
    /**
832
     * Test for URLService::createUpdateStruct() method.
833
     *
834
     * @param \eZ\Publish\API\Repository\Values\URL\URLUpdateStruct $updateStruct
835
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testCreateUpdateStruct
836
     */
837
    public function testCreateUpdateStructValues(URLUpdateStruct $updateStruct)
838
    {
839
        $this->assertPropertiesCorrect([
840
            'url' => null,
841
            'isValid' => null,
842
            'lastChecked' => null,
843
        ], $updateStruct);
844
    }
845
846
    /**
847
     * Test for URLService::testFindUsages() method.
848
     *
849
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testLoadById
850
     * @dataProvider dataProviderForFindUsages
851
     */
852
    public function testFindUsages($urlId, $offset, $limit, array $expectedContentInfos, $expectedTotalCount = null)
853
    {
854
        $repository = $this->getRepository();
855
856
        $id = $this->generateId('url', $urlId);
857
        /* BEGIN: Use Case */
858
        $urlService = $repository->getURLService();
859
860
        $loadedUrl = $urlService->loadById($id);
861
862
        $usagesSearchResults = $urlService->findUsages($loadedUrl, $offset, $limit);
863
        /* END: Use Case */
864
865
        $this->assertInstanceOf(UsageSearchResult::class, $usagesSearchResults);
866
        $this->assertEquals($expectedTotalCount, $usagesSearchResults->totalCount);
867
        $this->assertUsagesSearchResultItems($usagesSearchResults, $expectedContentInfos);
868
    }
869
870 View Code Duplication
    public function dataProviderForFindUsages()
871
    {
872
        return [
873
            // findUsages($url, 0, -1)
874
            [23, 0, -1, [54], 1],
875
            // findUsages($url, 0, $limit)
876
            [23, 0, 1, [54], 1],
877
        ];
878
    }
879
880
    /**
881
     * Test for URLService::testFindUsages() method.
882
     *
883
     * @depends eZ\Publish\API\Repository\Tests\URLServiceTest::testFindUsages
884
     */
885
    public function testFindUsagesReturnsEmptySearchResults()
886
    {
887
        $repository = $this->getRepository();
888
889
        /* BEGIN: Use Case */
890
        $urlService = $repository->getURLService();
891
892
        $loadedUrl = $urlService->loadByUrl('http://www.dropbox.com/');
893
894
        $usagesSearchResults = $urlService->findUsages($loadedUrl);
895
        /* END: Use Case */
896
897
        $this->assertInstanceOf(UsageSearchResult::class, $usagesSearchResults);
898
        $this->assertPropertiesCorrect([
899
            'totalCount' => 0,
900
            'items' => [],
901
        ], $usagesSearchResults);
902
    }
903
}
904