Completed
Push — 7.5 ( ffc5d4...888411 )
by Łukasz
23:14
created

testFindUrlsUsingSectionIdentifierOrSectionIdCriterion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

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