Completed
Push — master ( 22e8a3...5822db )
by André
23:39
created

dataProviderForOutOfRangeLocationPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the LocationServiceTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\API\Repository\Tests;
10
11
use eZ\Publish\API\Repository\Exceptions\BadStateException;
12
use eZ\Publish\API\Repository\Values\Content\Content;
13
use eZ\Publish\API\Repository\Values\Content\Location;
14
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
15
use eZ\Publish\API\Repository\Values\Content\LocationList;
16
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
17
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
18
use Exception;
19
use eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct;
20
21
/**
22
 * Test case for operations in the LocationService using in memory storage.
23
 *
24
 * @see eZ\Publish\API\Repository\LocationService
25
 * @group location
26
 */
27
class LocationServiceTest extends BaseTest
28
{
29
    /**
30
     * Test for the newLocationCreateStruct() method.
31
     *
32
     * @return \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct
33
     *
34
     * @see \eZ\Publish\API\Repository\LocationService::newLocationCreateStruct()
35
     */
36 View Code Duplication
    public function testNewLocationCreateStruct()
37
    {
38
        $repository = $this->getRepository();
39
40
        $parentLocationId = $this->generateId('location', 1);
41
        /* BEGIN: Use Case */
42
        // $parentLocationId is the ID of an existing location
43
        $locationService = $repository->getLocationService();
44
45
        $locationCreate = $locationService->newLocationCreateStruct(
46
            $parentLocationId
47
        );
48
        /* END: Use Case */
49
50
        $this->assertInstanceOf(
51
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\LocationCreateStruct',
52
            $locationCreate
53
        );
54
55
        return $locationCreate;
56
    }
57
58
    /**
59
     * Test for the newLocationCreateStruct() method.
60
     *
61
     * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreate
62
     *
63
     * @see \eZ\Publish\API\Repository\LocationService::newLocationCreateStruct()
64
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
65
     */
66
    public function testNewLocationCreateStructValues(LocationCreateStruct $locationCreate)
67
    {
68
        $this->assertPropertiesCorrect(
69
            array(
70
                'priority' => 0,
71
                'hidden' => false,
72
                // remoteId should be initialized with a default value
73
                //'remoteId' => null,
74
                'sortField' => Location::SORT_FIELD_NAME,
75
                'sortOrder' => Location::SORT_ORDER_ASC,
76
                'parentLocationId' => $this->generateId('location', 1),
77
            ),
78
            $locationCreate
79
        );
80
    }
81
82
    /**
83
     * Test for the createLocation() method.
84
     *
85
     * @see \eZ\Publish\API\Repository\LocationService::createLocation()
86
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
87
     */
88
    public function testCreateLocation()
89
    {
90
        $repository = $this->getRepository();
91
92
        $contentId = $this->generateId('object', 41);
93
        $parentLocationId = $this->generateId('location', 5);
94
        /* BEGIN: Use Case */
95
        // $contentId is the ID of an existing content object
96
        // $parentLocationId is the ID of an existing location
97
        $contentService = $repository->getContentService();
98
        $locationService = $repository->getLocationService();
99
100
        // ContentInfo for "How to use eZ Publish"
101
        $contentInfo = $contentService->loadContentInfo($contentId);
102
103
        $locationCreate = $locationService->newLocationCreateStruct($parentLocationId);
104
        $locationCreate->priority = 23;
105
        $locationCreate->hidden = true;
106
        $locationCreate->remoteId = 'sindelfingen';
107
        $locationCreate->sortField = Location::SORT_FIELD_NODE_ID;
108
        $locationCreate->sortOrder = Location::SORT_ORDER_DESC;
109
110
        $location = $locationService->createLocation(
111
            $contentInfo,
112
            $locationCreate
113
        );
114
        /* END: Use Case */
115
116
        $this->assertInstanceOf(
117
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
118
            $location
119
        );
120
121
        return array(
122
            'locationCreate' => $locationCreate,
123
            'createdLocation' => $location,
124
            'contentInfo' => $contentInfo,
125
            'parentLocation' => $locationService->loadLocation($this->generateId('location', 5)),
126
        );
127
    }
128
129
    /**
130
     * Test for the createLocation() method.
131
     *
132
     * @see \eZ\Publish\API\Repository\LocationService::createLocation()
133
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
134
     */
135
    public function testCreateLocationStructValues(array $data)
136
    {
137
        $locationCreate = $data['locationCreate'];
138
        $createdLocation = $data['createdLocation'];
139
        $contentInfo = $data['contentInfo'];
140
141
        $this->assertPropertiesCorrect(
142
            array(
143
                'priority' => $locationCreate->priority,
144
                'hidden' => $locationCreate->hidden,
145
                'invisible' => $locationCreate->hidden,
146
                'remoteId' => $locationCreate->remoteId,
147
                'contentInfo' => $contentInfo,
148
                'parentLocationId' => $locationCreate->parentLocationId,
149
                'pathString' => '/1/5/' . $this->parseId('location', $createdLocation->id) . '/',
150
                'depth' => 2,
151
                'sortField' => $locationCreate->sortField,
152
                'sortOrder' => $locationCreate->sortOrder,
153
            ),
154
            $createdLocation
155
        );
156
157
        $this->assertNotNull($createdLocation->id);
158
    }
159
160
    /**
161
     * Test for the createLocation() method.
162
     *
163
     * @see \eZ\Publish\API\Repository\LocationService::createLocation()
164
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
165
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
166
     */
167 View Code Duplication
    public function testCreateLocationThrowsInvalidArgumentExceptionContentAlreadyBelowParent()
168
    {
169
        $repository = $this->getRepository();
170
171
        $contentId = $this->generateId('object', 11);
172
        $parentLocationId = $this->generateId('location', 5);
173
        /* BEGIN: Use Case */
174
        // $contentId is the ID of an existing content object
175
        // $parentLocationId is the ID of an existing location which already
176
        // has the content assigned to one of its descendant locations
177
        $contentService = $repository->getContentService();
178
        $locationService = $repository->getLocationService();
179
180
        // ContentInfo for "How to use eZ Publish"
181
        $contentInfo = $contentService->loadContentInfo($contentId);
182
183
        $locationCreate = $locationService->newLocationCreateStruct($parentLocationId);
184
185
        // Throws exception, since content is already located at "/1/2/107/110/"
186
        $locationService->createLocation(
187
            $contentInfo,
188
            $locationCreate
189
        );
190
        /* END: Use Case */
191
    }
192
193
    /**
194
     * Test for the createLocation() method.
195
     *
196
     * @see \eZ\Publish\API\Repository\LocationService::createLocation()
197
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
198
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
199
     */
200 View Code Duplication
    public function testCreateLocationThrowsInvalidArgumentExceptionParentIsSubLocationOfContent()
201
    {
202
        $repository = $this->getRepository();
203
204
        $contentId = $this->generateId('object', 4);
205
        $parentLocationId = $this->generateId('location', 12);
206
        /* BEGIN: Use Case */
207
        // $contentId is the ID of an existing content object
208
        // $parentLocationId is the ID of an existing location which is below a
209
        // location that is assigned to the content
210
        $contentService = $repository->getContentService();
211
        $locationService = $repository->getLocationService();
212
213
        // ContentInfo for "How to use eZ Publish"
214
        $contentInfo = $contentService->loadContentInfo($contentId);
215
216
        $locationCreate = $locationService->newLocationCreateStruct($parentLocationId);
217
218
        // Throws exception, since content is already located at "/1/2/"
219
        $locationService->createLocation(
220
            $contentInfo,
221
            $locationCreate
222
        );
223
        /* END: Use Case */
224
    }
225
226
    /**
227
     * Test for the createLocation() method.
228
     *
229
     * @see \eZ\Publish\API\Repository\LocationService::createLocation()
230
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
231
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
232
     */
233 View Code Duplication
    public function testCreateLocationThrowsInvalidArgumentExceptionRemoteIdExists()
234
    {
235
        $repository = $this->getRepository();
236
237
        $contentId = $this->generateId('object', 41);
238
        $parentLocationId = $this->generateId('location', 5);
239
        /* BEGIN: Use Case */
240
        // $contentId is the ID of an existing content object
241
        $contentService = $repository->getContentService();
242
        $locationService = $repository->getLocationService();
243
244
        // ContentInfo for "How to use eZ Publish"
245
        $contentInfo = $contentService->loadContentInfo($contentId);
246
247
        $locationCreate = $locationService->newLocationCreateStruct($parentLocationId);
248
        // This remote ID already exists
249
        $locationCreate->remoteId = 'f3e90596361e31d496d4026eb624c983';
250
251
        // Throws exception, since remote ID is already in use
252
        $locationService->createLocation(
253
            $contentInfo,
254
            $locationCreate
255
        );
256
        /* END: Use Case */
257
    }
258
259
    /**
260
     * Test for the createLocation() method.
261
     *
262
     * @covers \eZ\Publish\API\Repository\LocationService::createLocation()
263
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
264
     * @dataProvider dataProviderForOutOfRangeLocationPriority
265
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
266
     */
267 View Code Duplication
    public function testCreateLocationThrowsInvalidArgumentExceptionPriorityIsOutOfRange($priority)
268
    {
269
        $repository = $this->getRepository();
270
271
        $contentId = $this->generateId('object', 41);
272
        $parentLocationId = $this->generateId('location', 5);
273
        /* BEGIN: Use Case */
274
        // $contentId is the ID of an existing content object
275
        // $parentLocationId is the ID of an existing location
276
        $contentService = $repository->getContentService();
277
        $locationService = $repository->getLocationService();
278
279
        // ContentInfo for "How to use eZ Publish"
280
        $contentInfo = $contentService->loadContentInfo($contentId);
281
282
        $locationCreate = $locationService->newLocationCreateStruct($parentLocationId);
283
        $locationCreate->priority = $priority;
284
        $locationCreate->hidden = true;
285
        $locationCreate->remoteId = 'sindelfingen';
286
        $locationCreate->sortField = Location::SORT_FIELD_NODE_ID;
287
        $locationCreate->sortOrder = Location::SORT_ORDER_DESC;
288
289
        // Throws exception, since priority is out of range
290
        $locationService->createLocation(
291
            $contentInfo,
292
            $locationCreate
293
        );
294
        /* END: Use Case */
295
    }
296
297
    public function dataProviderForOutOfRangeLocationPriority()
298
    {
299
        return [[-2147483649], [2147483648]];
300
    }
301
302
    /**
303
     * Test for the createLocation() method.
304
     *
305
     * @see \eZ\Publish\API\Repository\LocationService::createLocation()
306
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
307
     */
308
    public function testCreateLocationInTransactionWithRollback()
309
    {
310
        $repository = $this->getRepository();
311
312
        $contentId = $this->generateId('object', 41);
313
        $parentLocationId = $this->generateId('location', 5);
314
        /* BEGIN: Use Case */
315
        // $contentId is the ID of an existing content object
316
        // $parentLocationId is the ID of an existing location
317
        $contentService = $repository->getContentService();
318
        $locationService = $repository->getLocationService();
319
320
        $repository->beginTransaction();
321
322
        try {
323
            // ContentInfo for "How to use eZ Publish"
324
            $contentInfo = $contentService->loadContentInfo($contentId);
325
326
            $locationCreate = $locationService->newLocationCreateStruct($parentLocationId);
327
            $locationCreate->remoteId = 'sindelfingen';
328
329
            $createdLocationId = $locationService->createLocation(
330
                $contentInfo,
331
                $locationCreate
332
            )->id;
333
        } catch (Exception $e) {
334
            // Cleanup hanging transaction on error
335
            $repository->rollback();
336
            throw $e;
337
        }
338
339
        $repository->rollback();
340
341
        try {
342
            // Throws exception since creation of location was rolled back
343
            $location = $locationService->loadLocation($createdLocationId);
0 ignored issues
show
Unused Code introduced by
$location 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...
344
        } catch (NotFoundException $e) {
345
            return;
346
        }
347
        /* END: Use Case */
348
349
        $this->fail('Objects still exists after rollback.');
350
    }
351
352
    /**
353
     * Test for the loadLocation() method.
354
     *
355
     * @return \eZ\Publish\API\Repository\Values\Content\Location
356
     *
357
     * @covers \eZ\Publish\API\Repository\LocationService::loadLocation
358
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
359
     */
360
    public function testLoadLocation()
361
    {
362
        $repository = $this->getRepository();
363
364
        $locationId = $this->generateId('location', 5);
365
        /* BEGIN: Use Case */
366
        // $locationId is the ID of an existing location
367
        $locationService = $repository->getLocationService();
368
369
        $location = $locationService->loadLocation($locationId);
370
        /* END: Use Case */
371
372
        $this->assertInstanceOf(
373
            Location::class,
374
            $location
375
        );
376
        self::assertEquals(5, $location->id);
377
378
        return $location;
379
    }
380
381
    /**
382
     * Test for the loadLocation() method.
383
     *
384
     * @see \eZ\Publish\API\Repository\LocationService::loadLocation()
385
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
386
     */
387
    public function testLoadLocationRootStructValues()
388
    {
389
        $repository = $this->getRepository();
390
        $locationService = $repository->getLocationService();
391
        $location = $locationService->loadLocation($this->generateId('location', 1));
392
393
        $legacyDateTime = new \DateTime();
394
        $legacyDateTime->setTimestamp(1030968000);
395
396
        // $location
397
        $this->assertPropertiesCorrect(
398
            array(
399
                'id' => $this->generateId('location', 1),
400
                'status' => 1,
401
                'priority' => 0,
402
                'hidden' => false,
403
                'invisible' => false,
404
                'remoteId' => '629709ba256fe317c3ddcee35453a96a',
405
                'parentLocationId' => $this->generateId('location', 1),
406
                'pathString' => '/1/',
407
                'depth' => 0,
408
                'sortField' => 1,
409
                'sortOrder' => 1,
410
            ),
411
            $location
412
        );
413
414
        // $location->contentInfo
415
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $location->contentInfo);
416
        $this->assertPropertiesCorrect(
417
            array(
418
                'id' => $this->generateId('content', 0),
419
                'name' => 'Top Level Nodes',
420
                'sectionId' => 1,
421
                'mainLocationId' => 1,
422
                'contentTypeId' => 1,
423
                'currentVersionNo' => 1,
424
                'published' => 1,
425
                'ownerId' => 14,
426
                'modificationDate' => $legacyDateTime,
427
                'publishedDate' => $legacyDateTime,
428
                'alwaysAvailable' => 1,
429
                'remoteId' => null,
430
                'mainLanguageCode' => 'eng-GB',
431
            ),
432
            $location->contentInfo
433
        );
434
    }
435
436
    /**
437
     * Test for the loadLocation() method.
438
     *
439
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
440
     *
441
     * @see \eZ\Publish\API\Repository\LocationService::loadLocation()
442
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
443
     */
444
    public function testLoadLocationStructValues(Location $location)
445
    {
446
        $this->assertPropertiesCorrect(
447
            array(
448
                'id' => $this->generateId('location', 5),
449
                'priority' => 0,
450
                'hidden' => false,
451
                'invisible' => false,
452
                'remoteId' => '3f6d92f8044aed134f32153517850f5a',
453
                'parentLocationId' => $this->generateId('location', 1),
454
                'pathString' => '/1/5/',
455
                'depth' => 1,
456
                'sortField' => 1,
457
                'sortOrder' => 1,
458
            ),
459
            $location
460
        );
461
462
        $this->assertInstanceOf(
463
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
464
            $location->contentInfo
465
        );
466
        $this->assertEquals($this->generateId('object', 4), $location->contentInfo->id);
467
    }
468
469
    /**
470
     * Test for the loadLocation() method.
471
     *
472
     * @see \eZ\Publish\API\Repository\LocationService::loadLocation()
473
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
474
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
475
     */
476
    public function testLoadLocationThrowsNotFoundException()
477
    {
478
        $repository = $this->getRepository();
479
480
        $nonExistentLocationId = $this->generateId('location', 2342);
481
        /* BEGIN: Use Case */
482
        $locationService = $repository->getLocationService();
483
484
        // Throws exception, if Location with $nonExistentLocationId does not
485
        // exist
486
        $location = $locationService->loadLocation($nonExistentLocationId);
0 ignored issues
show
Unused Code introduced by
$location 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...
487
        /* END: Use Case */
488
    }
489
490
    /**
491
     * Test for the loadLocationByRemoteId() method.
492
     *
493
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationByRemoteId()
494
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
495
     */
496 View Code Duplication
    public function testLoadLocationByRemoteId()
497
    {
498
        $repository = $this->getRepository();
499
500
        /* BEGIN: Use Case */
501
        $locationService = $repository->getLocationService();
502
503
        $location = $locationService->loadLocationByRemoteId(
504
            '3f6d92f8044aed134f32153517850f5a'
505
        );
506
        /* END: Use Case */
507
508
        $this->assertEquals(
509
            $locationService->loadLocation($this->generateId('location', 5)),
510
            $location
511
        );
512
    }
513
514
    /**
515
     * Test for the loadLocationByRemoteId() method.
516
     *
517
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationByRemoteId()
518
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
519
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
520
     */
521
    public function testLoadLocationByRemoteIdThrowsNotFoundException()
522
    {
523
        $repository = $this->getRepository();
524
525
        /* BEGIN: Use Case */
526
        $locationService = $repository->getLocationService();
527
528
        // Throws exception, since Location with remote ID does not exist
529
        $location = $locationService->loadLocationByRemoteId(
0 ignored issues
show
Unused Code introduced by
$location 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...
530
            'not-exists'
531
        );
532
        /* END: Use Case */
533
    }
534
535
    /**
536
     * Test for the loadLocations() method.
537
     *
538
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
539
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
540
     */
541
    public function testLoadLocations()
542
    {
543
        $repository = $this->getRepository();
544
545
        $contentId = $this->generateId('object', 4);
546
        /* BEGIN: Use Case */
547
        // $contentId contains the ID of an existing content object
548
        $contentService = $repository->getContentService();
549
        $locationService = $repository->getLocationService();
550
551
        $contentInfo = $contentService->loadContentInfo($contentId);
552
553
        $locations = $locationService->loadLocations($contentInfo);
554
        /* END: Use Case */
555
556
        $this->assertInternalType('array', $locations);
557
        self::assertNotEmpty($locations);
558
559
        foreach ($locations as $location) {
560
            self::assertInstanceOf(Location::class, $location);
561
            self::assertEquals($contentInfo->id, $location->getContentInfo()->id);
562
        }
563
564
        return $locations;
565
    }
566
567
    /**
568
     * Test for the loadLocations() method.
569
     *
570
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
571
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
572
     */
573
    public function testLoadLocationsContent(array $locations)
574
    {
575
        $repository = $this->getRepository();
576
        $locationService = $repository->getLocationService();
0 ignored issues
show
Unused Code introduced by
$locationService 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...
577
578
        $this->assertEquals(1, count($locations));
579
        foreach ($locations as $loadedLocation) {
580
            $this->assertInstanceOf(
581
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
582
                $loadedLocation
583
            );
584
        }
585
586
        usort(
587
            $locations,
588
            function ($a, $b) {
589
                strcmp($a->id, $b->id);
590
            }
591
        );
592
593
        $this->assertEquals(
594
            array($this->generateId('location', 5)),
595
            array_map(
596
                function (Location $location) {
597
                    return $location->id;
598
                },
599
                $locations
600
            )
601
        );
602
    }
603
604
    /**
605
     * Test for the loadLocations() method.
606
     *
607
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
608
     *
609
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations($contentInfo, $rootLocation)
610
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
611
     */
612
    public function testLoadLocationsLimitedSubtree()
613
    {
614
        $repository = $this->getRepository();
615
616
        $originalLocationId = $this->generateId('location', 54);
617
        $originalParentLocationId = $this->generateId('location', 48);
618
        $newParentLocationId = $this->generateId('location', 43);
619
        /* BEGIN: Use Case */
620
        // $originalLocationId is the ID of an existing location
621
        // $originalParentLocationId is the ID of the parent location of
622
        //     $originalLocationId
623
        // $newParentLocationId is the ID of an existing location outside the tree
624
        // of $originalLocationId and $originalParentLocationId
625
        $locationService = $repository->getLocationService();
626
627
        // Location at "/1/48/54"
628
        $originalLocation = $locationService->loadLocation($originalLocationId);
629
630
        // Create location under "/1/43/"
631
        $locationCreate = $locationService->newLocationCreateStruct($newParentLocationId);
632
        $locationService->createLocation(
633
            $originalLocation->contentInfo,
634
            $locationCreate
635
        );
636
637
        $findRootLocation = $locationService->loadLocation($originalParentLocationId);
638
639
        // Returns an array with only $originalLocation
640
        $locations = $locationService->loadLocations(
641
            $originalLocation->contentInfo,
642
            $findRootLocation
643
        );
644
        /* END: Use Case */
645
646
        $this->assertInternalType('array', $locations);
647
648
        return $locations;
649
    }
650
651
    /**
652
     * Test for the loadLocations() method.
653
     *
654
     * @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
655
     *
656
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
657
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationsLimitedSubtree
658
     */
659
    public function testLoadLocationsLimitedSubtreeContent(array $locations)
660
    {
661
        $this->assertEquals(1, count($locations));
662
663
        $this->assertEquals(
664
            $this->generateId('location', 54),
665
            reset($locations)->id
666
        );
667
    }
668
669
    /**
670
     * Test for the loadLocations() method.
671
     *
672
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
673
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
674
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
675
     */
676 View Code Duplication
    public function testLoadLocationsThrowsBadStateException()
677
    {
678
        $repository = $this->getRepository();
679
680
        /* BEGIN: Use Case */
681
        $contentTypeService = $repository->getContentTypeService();
682
        $contentService = $repository->getContentService();
683
        $locationService = $repository->getLocationService();
684
685
        // Create new content, which is not published
686
        $folderType = $contentTypeService->loadContentTypeByIdentifier('folder');
687
        $contentCreate = $contentService->newContentCreateStruct($folderType, 'eng-US');
688
        $contentCreate->setField('name', 'New Folder');
689
        $content = $contentService->createContent($contentCreate);
690
691
        // Throws Exception, since $content has no published version, yet
692
        $locationService->loadLocations(
693
            $content->contentInfo
694
        );
695
        /* END: Use Case */
696
    }
697
698
    /**
699
     * Test for the loadLocations() method.
700
     *
701
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations($contentInfo, $rootLocation)
702
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
703
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
704
     */
705
    public function testLoadLocationsThrowsBadStateExceptionLimitedSubtree()
706
    {
707
        $repository = $this->getRepository();
708
709
        $someLocationId = $this->generateId('location', 2);
710
        /* BEGIN: Use Case */
711
        // $someLocationId is the ID of an existing location
712
        $contentTypeService = $repository->getContentTypeService();
713
        $contentService = $repository->getContentService();
714
        $locationService = $repository->getLocationService();
715
716
        // Create new content, which is not published
717
        $folderType = $contentTypeService->loadContentTypeByIdentifier('folder');
718
        $contentCreate = $contentService->newContentCreateStruct($folderType, 'eng-US');
719
        $contentCreate->setField('name', 'New Folder');
720
        $content = $contentService->createContent($contentCreate);
721
722
        $findRootLocation = $locationService->loadLocation($someLocationId);
723
724
        // Throws Exception, since $content has no published version, yet
725
        $locationService->loadLocations(
726
            $content->contentInfo,
727
            $findRootLocation
728
        );
729
        /* END: Use Case */
730
    }
731
732
    /**
733
     * Test for the loadLocationChildren() method.
734
     *
735
     * @covers \eZ\Publish\API\Repository\LocationService::loadLocationChildren
736
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
737
     */
738
    public function testLoadLocationChildren()
739
    {
740
        $repository = $this->getRepository();
741
742
        $locationId = $this->generateId('location', 5);
743
        /* BEGIN: Use Case */
744
        // $locationId is the ID of an existing location
745
        $locationService = $repository->getLocationService();
746
747
        $location = $locationService->loadLocation($locationId);
748
749
        $childLocations = $locationService->loadLocationChildren($location);
750
        /* END: Use Case */
751
752
        $this->assertInstanceOf(LocationList::class, $childLocations);
753
        $this->assertInternalType('array', $childLocations->locations);
754
        $this->assertNotEmpty($childLocations->locations);
755
        $this->assertInternalType('int', $childLocations->totalCount);
756
757
        foreach ($childLocations->locations as $childLocation) {
758
            $this->assertInstanceOf(Location::class, $childLocation);
759
            $this->assertEquals($location->id, $childLocation->parentLocationId);
760
        }
761
762
        return $childLocations;
763
    }
764
765
    /**
766
     * Test loading parent Locations for draft Content.
767
     *
768
     * @covers \eZ\Publish\API\Repository\LocationService::loadParentLocationsForDraftContent
769
     */
770
    public function testLoadParentLocationsForDraftContent()
771
    {
772
        $repository = $this->getRepository();
773
        $locationService = $repository->getLocationService();
774
        $contentService = $repository->getContentService();
775
        $contentTypeService = $repository->getContentTypeService();
776
777
        // prepare locations
778
        $locationCreateStructs = [
779
            $locationService->newLocationCreateStruct(2),
780
            $locationService->newLocationCreateStruct(5),
781
        ];
782
783
        // Create new content
784
        $folderType = $contentTypeService->loadContentTypeByIdentifier('folder');
785
        $contentCreate = $contentService->newContentCreateStruct($folderType, 'eng-US');
786
        $contentCreate->setField('name', 'New Folder');
787
        $contentDraft = $contentService->createContent($contentCreate, $locationCreateStructs);
788
789
        // Test loading parent Locations
790
        $locations = $locationService->loadParentLocationsForDraftContent($contentDraft->versionInfo);
791
792
        self::assertCount(2, $locations);
793
        foreach ($locations as $location) {
794
            // test it is one of the given parent locations
795
            self::assertTrue($location->id === 2 || $location->id === 5);
796
        }
797
798
        return $contentDraft;
799
    }
800
801
    /**
802
     * Test that trying to load parent Locations throws Exception if Content is not a draft.
803
     *
804
     * @depends testLoadParentLocationsForDraftContent
805
     *
806
     * @param \eZ\Publish\API\Repository\Values\Content\Content $contentDraft
807
     */
808
    public function testLoadParentLocationsForDraftContentThrowsBadStateException(Content $contentDraft)
809
    {
810
        $this->expectException(BadStateException::class);
811
        $this->expectExceptionMessageRegExp('/has been already published/');
812
813
        $repository = $this->getRepository(false);
814
        $locationService = $repository->getLocationService();
815
        $contentService = $repository->getContentService();
816
817
        $content = $contentService->publishVersion($contentDraft->versionInfo);
818
819
        $locationService->loadParentLocationsForDraftContent($content->versionInfo);
820
    }
821
822
    /**
823
     * Test for the getLocationChildCount() method.
824
     *
825
     * @see \eZ\Publish\API\Repository\LocationService::getLocationChildCount()
826
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
827
     */
828
    public function testGetLocationChildCount()
829
    {
830
        // $locationId is the ID of an existing location
831
        $locationService = $this->getRepository()->getLocationService();
832
833
        $this->assertSame(
834
            5,
835
            $locationService->getLocationChildCount(
836
                $locationService->loadLocation($this->generateId('location', 5))
837
            )
838
        );
839
    }
840
841
    /**
842
     * Test for the loadLocationChildren() method.
843
     *
844
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren()
845
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
846
     */
847
    public function testLoadLocationChildrenData(LocationList $locations)
848
    {
849
        $this->assertEquals(5, count($locations->locations));
850
        $this->assertEquals(5, $locations->totalCount);
851
852
        foreach ($locations->locations as $location) {
853
            $this->assertInstanceOf(
854
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
855
                $location
856
            );
857
        }
858
859
        $this->assertEquals(
860
            array(
861
                $this->generateId('location', 12),
862
                $this->generateId('location', 13),
863
                $this->generateId('location', 14),
864
                $this->generateId('location', 44),
865
                $this->generateId('location', 61),
866
            ),
867
            array_map(
868
                function (Location $location) {
869
                    return $location->id;
870
                },
871
                $locations->locations
872
            )
873
        );
874
    }
875
876
    /**
877
     * Test for the loadLocationChildren() method.
878
     *
879
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
880
     *
881
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset)
882
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
883
     */
884 View Code Duplication
    public function testLoadLocationChildrenWithOffset()
885
    {
886
        $repository = $this->getRepository();
887
888
        $locationId = $this->generateId('location', 5);
889
        /* BEGIN: Use Case */
890
        // $locationId is the ID of an existing location
891
        $locationService = $repository->getLocationService();
892
893
        $location = $locationService->loadLocation($locationId);
894
895
        $childLocations = $locationService->loadLocationChildren($location, 2);
896
        /* END: Use Case */
897
898
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\LocationList', $childLocations);
899
        $this->assertInternalType('array', $childLocations->locations);
900
        $this->assertInternalType('int', $childLocations->totalCount);
901
902
        return $childLocations;
903
    }
904
905
    /**
906
     * Test for the loadLocationChildren() method.
907
     *
908
     * @param \eZ\Publish\API\Repository\Values\Content\LocationList $locations
909
     *
910
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset)
911
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildrenWithOffset
912
     */
913 View Code Duplication
    public function testLoadLocationChildrenDataWithOffset(LocationList $locations)
914
    {
915
        $this->assertEquals(3, count($locations->locations));
916
        $this->assertEquals(5, $locations->totalCount);
917
918
        foreach ($locations->locations as $location) {
919
            $this->assertInstanceOf(
920
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
921
                $location
922
            );
923
        }
924
925
        $this->assertEquals(
926
            array(
927
                $this->generateId('location', 14),
928
                $this->generateId('location', 44),
929
                $this->generateId('location', 61),
930
            ),
931
            array_map(
932
                function (Location $location) {
933
                    return $location->id;
934
                },
935
                $locations->locations
936
            )
937
        );
938
    }
939
940
    /**
941
     * Test for the loadLocationChildren() method.
942
     *
943
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
944
     *
945
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset, $limit)
946
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
947
     */
948 View Code Duplication
    public function testLoadLocationChildrenWithOffsetAndLimit()
949
    {
950
        $repository = $this->getRepository();
951
952
        $locationId = $this->generateId('location', 5);
953
        /* BEGIN: Use Case */
954
        // $locationId is the ID of an existing location
955
        $locationService = $repository->getLocationService();
956
957
        $location = $locationService->loadLocation($locationId);
958
959
        $childLocations = $locationService->loadLocationChildren($location, 2, 2);
960
        /* END: Use Case */
961
962
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\LocationList', $childLocations);
963
        $this->assertInternalType('array', $childLocations->locations);
964
        $this->assertInternalType('int', $childLocations->totalCount);
965
966
        return $childLocations;
967
    }
968
969
    /**
970
     * Test for the loadLocationChildren() method.
971
     *
972
     * @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
973
     *
974
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset, $limit)
975
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildrenWithOffsetAndLimit
976
     */
977 View Code Duplication
    public function testLoadLocationChildrenDataWithOffsetAndLimit(LocationList $locations)
978
    {
979
        $this->assertEquals(2, count($locations->locations));
980
        $this->assertEquals(5, $locations->totalCount);
981
982
        foreach ($locations->locations as $location) {
983
            $this->assertInstanceOf(
984
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
985
                $location
986
            );
987
        }
988
989
        $this->assertEquals(
990
            array(
991
                $this->generateId('location', 14),
992
                $this->generateId('location', 44),
993
            ),
994
            array_map(
995
                function (Location $location) {
996
                    return $location->id;
997
                },
998
                $locations->locations
999
            )
1000
        );
1001
    }
1002
1003
    /**
1004
     * Test for the newLocationUpdateStruct() method.
1005
     *
1006
     * @covers \eZ\Publish\API\Repository\LocationService::newLocationUpdateStruct
1007
     */
1008 View Code Duplication
    public function testNewLocationUpdateStruct()
1009
    {
1010
        $repository = $this->getRepository();
1011
1012
        /* BEGIN: Use Case */
1013
        $locationService = $repository->getLocationService();
1014
1015
        $updateStruct = $locationService->newLocationUpdateStruct();
1016
        /* END: Use Case */
1017
1018
        $this->assertInstanceOf(
1019
            LocationUpdateStruct::class,
1020
            $updateStruct
1021
        );
1022
1023
        $this->assertPropertiesCorrect(
1024
            [
1025
                'priority' => null,
1026
                'remoteId' => null,
1027
                'sortField' => null,
1028
                'sortOrder' => null,
1029
            ],
1030
            $updateStruct
1031
        );
1032
    }
1033
1034
    /**
1035
     * Test for the updateLocation() method.
1036
     *
1037
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1038
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1039
     */
1040
    public function testUpdateLocation()
1041
    {
1042
        $repository = $this->getRepository();
1043
1044
        $originalLocationId = $this->generateId('location', 5);
1045
        /* BEGIN: Use Case */
1046
        // $originalLocationId is the ID of an existing location
1047
        $locationService = $repository->getLocationService();
1048
1049
        $originalLocation = $locationService->loadLocation($originalLocationId);
1050
1051
        $updateStruct = $locationService->newLocationUpdateStruct();
1052
        $updateStruct->priority = 3;
1053
        $updateStruct->remoteId = 'c7adcbf1e96bc29bca28c2d809d0c7ef69272651';
1054
        $updateStruct->sortField = Location::SORT_FIELD_PRIORITY;
1055
        $updateStruct->sortOrder = Location::SORT_ORDER_DESC;
1056
1057
        $updatedLocation = $locationService->updateLocation($originalLocation, $updateStruct);
1058
        /* END: Use Case */
1059
1060
        $this->assertInstanceOf(
1061
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1062
            $updatedLocation
1063
        );
1064
1065
        return array(
1066
            'originalLocation' => $originalLocation,
1067
            'updateStruct' => $updateStruct,
1068
            'updatedLocation' => $updatedLocation,
1069
        );
1070
    }
1071
1072
    /**
1073
     * Test for the updateLocation() method.
1074
     *
1075
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1076
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testUpdateLocation
1077
     */
1078
    public function testUpdateLocationStructValues(array $data)
1079
    {
1080
        $originalLocation = $data['originalLocation'];
1081
        $updateStruct = $data['updateStruct'];
1082
        $updatedLocation = $data['updatedLocation'];
1083
1084
        $this->assertPropertiesCorrect(
1085
            array(
1086
                'id' => $originalLocation->id,
1087
                'priority' => $updateStruct->priority,
1088
                'hidden' => $originalLocation->hidden,
1089
                'invisible' => $originalLocation->invisible,
1090
                'remoteId' => $updateStruct->remoteId,
1091
                'contentInfo' => $originalLocation->contentInfo,
1092
                'parentLocationId' => $originalLocation->parentLocationId,
1093
                'pathString' => $originalLocation->pathString,
1094
                'depth' => $originalLocation->depth,
1095
                'sortField' => $updateStruct->sortField,
1096
                'sortOrder' => $updateStruct->sortOrder,
1097
            ),
1098
            $updatedLocation
1099
        );
1100
    }
1101
1102
    /**
1103
     * Test for the updateLocation() method.
1104
     *
1105
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1106
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1107
     */
1108
    public function testUpdateLocationWithSameRemoteId()
1109
    {
1110
        $repository = $this->getRepository();
1111
1112
        $locationId = $this->generateId('location', 5);
1113
        /* BEGIN: Use Case */
1114
        // $locationId and remote ID is the IDs of the same, existing location
1115
        $locationService = $repository->getLocationService();
1116
1117
        $originalLocation = $locationService->loadLocation($locationId);
1118
1119
        $updateStruct = $locationService->newLocationUpdateStruct();
1120
1121
        // Remote ID of an existing location with the same locationId
1122
        $updateStruct->remoteId = $originalLocation->remoteId;
1123
1124
        // Sets one of the properties to be able to confirm location gets updated, here: priority
1125
        $updateStruct->priority = 2;
1126
1127
        $location = $locationService->updateLocation($originalLocation, $updateStruct);
1128
1129
        // Checks that the location was updated
1130
        $this->assertEquals(2, $location->priority);
1131
1132
        // Checks that remoteId remains the same
1133
        $this->assertEquals($originalLocation->remoteId, $location->remoteId);
1134
        /* END: Use Case */
1135
    }
1136
1137
    /**
1138
     * Test for the updateLocation() method.
1139
     *
1140
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1141
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1142
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1143
     */
1144 View Code Duplication
    public function testUpdateLocationThrowsInvalidArgumentException()
1145
    {
1146
        $repository = $this->getRepository();
1147
1148
        $locationId = $this->generateId('location', 5);
1149
        /* BEGIN: Use Case */
1150
        // $locationId and remoteId is the IDs of an existing, but not the same, location
1151
        $locationService = $repository->getLocationService();
1152
1153
        $originalLocation = $locationService->loadLocation($locationId);
1154
1155
        $updateStruct = $locationService->newLocationUpdateStruct();
1156
1157
        // Remote ID of an existing location with a different locationId
1158
        $updateStruct->remoteId = 'f3e90596361e31d496d4026eb624c983';
1159
1160
        // Throws exception, since remote ID is already taken
1161
        $locationService->updateLocation($originalLocation, $updateStruct);
1162
        /* END: Use Case */
1163
    }
1164
1165
    /**
1166
     * Test for the updateLocation() method.
1167
     *
1168
     * @covers \eZ\Publish\API\Repository\LocationService::updateLocation()
1169
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1170
     * @dataProvider dataProviderForOutOfRangeLocationPriority
1171
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1172
     */
1173 View Code Duplication
    public function testUpdateLocationThrowsInvalidArgumentExceptionPriorityIsOutOfRange($priority)
1174
    {
1175
        $repository = $this->getRepository();
1176
1177
        $locationId = $this->generateId('location', 5);
1178
        /* BEGIN: Use Case */
1179
        // $locationId and remoteId is the IDs of an existing, but not the same, location
1180
        $locationService = $repository->getLocationService();
1181
1182
        $originalLocation = $locationService->loadLocation($locationId);
1183
1184
        $updateStruct = $locationService->newLocationUpdateStruct();
1185
1186
        // Priority value is out of range
1187
        $updateStruct->priority = $priority;
1188
1189
        // Throws exception, since remote ID is already taken
1190
        $locationService->updateLocation($originalLocation, $updateStruct);
1191
        /* END: Use Case */
1192
    }
1193
1194
    /**
1195
     * Test for the updateLocation() method.
1196
     * Ref EZP-23302: Update Location fails if no change is performed with the update.
1197
     *
1198
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1199
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1200
     */
1201
    public function testUpdateLocationTwice()
1202
    {
1203
        $repository = $this->getRepository();
1204
1205
        $locationId = $this->generateId('location', 5);
1206
        /* BEGIN: Use Case */
1207
        $locationService = $repository->getLocationService();
1208
        $repository->setCurrentUser($repository->getUserService()->loadUser(14));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1209
1210
        $originalLocation = $locationService->loadLocation($locationId);
1211
1212
        $updateStruct = $locationService->newLocationUpdateStruct();
1213
        $updateStruct->priority = 42;
1214
1215
        $updatedLocation = $locationService->updateLocation($originalLocation, $updateStruct);
1216
1217
        // Repeated update with the same, unchanged struct
1218
        $secondUpdatedLocation = $locationService->updateLocation($updatedLocation, $updateStruct);
1219
        /* END: Use Case */
1220
1221
        $this->assertEquals($updatedLocation->priority, 42);
1222
        $this->assertEquals($secondUpdatedLocation->priority, 42);
1223
    }
1224
1225
    /**
1226
     * Test for the swapLocation() method.
1227
     *
1228
     * @see \eZ\Publish\API\Repository\LocationService::swapLocation()
1229
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1230
     */
1231
    public function testSwapLocation()
1232
    {
1233
        $repository = $this->getRepository();
1234
        $locationService = $repository->getLocationService();
1235
1236
        $mediaLocationId = $this->generateId('location', 43);
1237
        $demoDesignLocationId = $this->generateId('location', 56);
1238
1239
        $mediaContentInfo = $locationService->loadLocation($mediaLocationId)->getContentInfo();
1240
        $demoDesignContentInfo = $locationService->loadLocation($demoDesignLocationId)->getContentInfo();
1241
1242
        /* BEGIN: Use Case */
1243
        // $mediaLocationId is the ID of the "Media" page location in
1244
        // an eZ Publish demo installation
1245
1246
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1247
        // Publish demo installation
1248
1249
        // Load the location service
1250
        $locationService = $repository->getLocationService();
1251
1252
        $mediaLocation = $locationService->loadLocation($mediaLocationId);
1253
        $demoDesignLocation = $locationService->loadLocation($demoDesignLocationId);
1254
1255
        // Swaps the content referred to by the locations
1256
        $locationService->swapLocation($mediaLocation, $demoDesignLocation);
1257
        /* END: Use Case */
1258
1259
        // Reload Locations, IDs swapped
1260
        $demoDesignLocation = $locationService->loadLocation($mediaLocationId);
1261
        $mediaLocation = $locationService->loadLocation($demoDesignLocationId);
1262
1263
        // Assert Location's Content is updated
1264
        $this->assertEquals(
1265
            $mediaContentInfo->id,
1266
            $mediaLocation->getContentInfo()->id
1267
        );
1268
        $this->assertEquals(
1269
            $demoDesignContentInfo->id,
1270
            $demoDesignLocation->getContentInfo()->id
1271
        );
1272
1273
        // Assert URL aliases are updated
1274
        $this->assertEquals(
1275
            $mediaLocation->id,
1276
            $repository->getURLAliasService()->lookup('/Design/Media')->destination
1277
        );
1278
        $this->assertEquals(
1279
            $demoDesignLocation->id,
1280
            $repository->getURLAliasService()->lookup('/Plain-site')->destination
1281
        );
1282
    }
1283
1284
    /**
1285
     * Test swapping Main Location of a Content with another one updates Content item Main Location.
1286
     *
1287
     * @covers \eZ\Publish\API\Repository\LocationService::swapLocation
1288
     */
1289
    public function testSwapLocationUpdatesMainLocation()
1290
    {
1291
        $repository = $this->getRepository();
1292
        $locationService = $repository->getLocationService();
1293
        $contentService = $repository->getContentService();
1294
1295
        $mainLocationParentId = 60;
1296
        $secondaryLocationId = 43;
1297
1298
        $publishedContent = $this->publishContentWithParentLocation(
1299
            'Content for Swap Location Test', $mainLocationParentId
1300
        );
1301
1302
        // sanity check
1303
        $mainLocation = $locationService->loadLocation($publishedContent->contentInfo->mainLocationId);
1304
        self::assertEquals($mainLocationParentId, $mainLocation->parentLocationId);
1305
1306
        // load another pre-existing location
1307
        $secondaryLocation = $locationService->loadLocation($secondaryLocationId);
1308
1309
        // swap the Main Location with a secondary one
1310
        $locationService->swapLocation($mainLocation, $secondaryLocation);
1311
1312
        // check if Main Location has been updated
1313
        $mainLocation = $locationService->loadLocation($secondaryLocation->id);
1314
        self::assertEquals($publishedContent->contentInfo->id, $mainLocation->contentInfo->id);
1315
        self::assertEquals($mainLocation->id, $mainLocation->contentInfo->mainLocationId);
1316
1317
        $reloadedContent = $contentService->loadContentByContentInfo($publishedContent->contentInfo);
1318
        self::assertEquals($mainLocation->id, $reloadedContent->contentInfo->mainLocationId);
1319
    }
1320
1321
    /**
1322
     * Test for the hideLocation() method.
1323
     *
1324
     * @see \eZ\Publish\API\Repository\LocationService::hideLocation()
1325
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1326
     */
1327
    public function testHideLocation()
1328
    {
1329
        $repository = $this->getRepository();
1330
1331
        $locationId = $this->generateId('location', 5);
1332
        /* BEGIN: Use Case */
1333
        // $locationId is the ID of an existing location
1334
        $locationService = $repository->getLocationService();
1335
1336
        $visibleLocation = $locationService->loadLocation($locationId);
1337
1338
        $hiddenLocation = $locationService->hideLocation($visibleLocation);
1339
        /* END: Use Case */
1340
1341
        $this->assertInstanceOf(
1342
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1343
            $hiddenLocation
1344
        );
1345
1346
        $this->assertTrue(
1347
            $hiddenLocation->hidden,
1348
            sprintf(
1349
                'Location with ID "%s" not hidden.',
1350
                $hiddenLocation->id
1351
            )
1352
        );
1353
1354
        $this->refreshSearch($repository);
1355
1356
        foreach ($locationService->loadLocationChildren($hiddenLocation)->locations as $child) {
1357
            $this->assertSubtreeProperties(
1358
                array('invisible' => true),
1359
                $child
1360
            );
1361
        }
1362
    }
1363
1364
    /**
1365
     * Assert that $expectedValues are set in the subtree starting at $location.
1366
     *
1367
     * @param array $expectedValues
1368
     * @param Location $location
1369
     */
1370
    protected function assertSubtreeProperties(array $expectedValues, Location $location, $stopId = null)
1371
    {
1372
        $repository = $this->getRepository();
1373
        $locationService = $repository->getLocationService();
1374
1375
        if ($location->id === $stopId) {
1376
            return;
1377
        }
1378
1379
        foreach ($expectedValues as $propertyName => $propertyValue) {
1380
            $this->assertEquals(
1381
                $propertyValue,
1382
                $location->$propertyName
1383
            );
1384
1385
            foreach ($locationService->loadLocationChildren($location)->locations as $child) {
1386
                $this->assertSubtreeProperties($expectedValues, $child);
1387
            }
1388
        }
1389
    }
1390
1391
    /**
1392
     * Test for the unhideLocation() method.
1393
     *
1394
     * @see \eZ\Publish\API\Repository\LocationService::unhideLocation()
1395
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testHideLocation
1396
     */
1397
    public function testUnhideLocation()
1398
    {
1399
        $repository = $this->getRepository();
1400
1401
        $locationId = $this->generateId('location', 5);
1402
        /* BEGIN: Use Case */
1403
        // $locationId is the ID of an existing location
1404
        $locationService = $repository->getLocationService();
1405
1406
        $visibleLocation = $locationService->loadLocation($locationId);
1407
        $hiddenLocation = $locationService->hideLocation($visibleLocation);
1408
1409
        $unHiddenLocation = $locationService->unhideLocation($hiddenLocation);
1410
        /* END: Use Case */
1411
1412
        $this->assertInstanceOf(
1413
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1414
            $unHiddenLocation
1415
        );
1416
1417
        $this->assertFalse(
1418
            $unHiddenLocation->hidden,
1419
            sprintf(
1420
                'Location with ID "%s" not unhidden.',
1421
                $unHiddenLocation->id
1422
            )
1423
        );
1424
1425
        $this->refreshSearch($repository);
1426
1427
        foreach ($locationService->loadLocationChildren($unHiddenLocation)->locations as $child) {
1428
            $this->assertSubtreeProperties(
1429
                array('invisible' => false),
1430
                $child
1431
            );
1432
        }
1433
    }
1434
1435
    /**
1436
     * Test for the unhideLocation() method.
1437
     *
1438
     * @see \eZ\Publish\API\Repository\LocationService::unhideLocation()
1439
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testUnhideLocation
1440
     */
1441
    public function testUnhideLocationNotUnhidesHiddenSubtree()
1442
    {
1443
        $repository = $this->getRepository();
1444
1445
        $higherLocationId = $this->generateId('location', 5);
1446
        $lowerLocationId = $this->generateId('location', 13);
1447
        /* BEGIN: Use Case */
1448
        // $higherLocationId is the ID of a location
1449
        // $lowerLocationId is the ID of a location below $higherLocationId
1450
        $locationService = $repository->getLocationService();
1451
1452
        $higherLocation = $locationService->loadLocation($higherLocationId);
1453
        $hiddenHigherLocation = $locationService->hideLocation($higherLocation);
1454
1455
        $lowerLocation = $locationService->loadLocation($lowerLocationId);
1456
        $hiddenLowerLocation = $locationService->hideLocation($lowerLocation);
0 ignored issues
show
Unused Code introduced by
$hiddenLowerLocation 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...
1457
1458
        $unHiddenHigherLocation = $locationService->unhideLocation($hiddenHigherLocation);
1459
        /* END: Use Case */
1460
1461
        $this->assertInstanceOf(
1462
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1463
            $unHiddenHigherLocation
1464
        );
1465
1466
        $this->assertFalse(
1467
            $unHiddenHigherLocation->hidden,
1468
            sprintf(
1469
                'Location with ID "%s" not unhidden.',
1470
                $unHiddenHigherLocation->id
1471
            )
1472
        );
1473
1474
        $this->refreshSearch($repository);
1475
1476
        foreach ($locationService->loadLocationChildren($unHiddenHigherLocation)->locations as $child) {
1477
            $this->assertSubtreeProperties(
1478
                array('invisible' => false),
1479
                $child,
1480
                $this->generateId('location', 13)
1481
            );
1482
        }
1483
1484
        $stillHiddenLocation = $locationService->loadLocation($this->generateId('location', 13));
1485
        $this->assertTrue(
1486
            $stillHiddenLocation->hidden,
1487
            sprintf(
1488
                'Hidden sub-location with ID %s accidentally unhidden.',
1489
                $stillHiddenLocation->id
1490
            )
1491
        );
1492
        foreach ($locationService->loadLocationChildren($stillHiddenLocation)->locations as $child) {
1493
            $this->assertSubtreeProperties(
1494
                array('invisible' => true),
1495
                $child
1496
            );
1497
        }
1498
    }
1499
1500
    /**
1501
     * Test for the deleteLocation() method.
1502
     *
1503
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1504
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1505
     */
1506
    public function testDeleteLocation()
1507
    {
1508
        $repository = $this->getRepository();
1509
1510
        $mediaLocationId = $this->generateId('location', 43);
1511
        /* BEGIN: Use Case */
1512
        // $mediaLocationId is the ID of the location of the
1513
        // "Media" location in an eZ Publish demo installation
1514
        $locationService = $repository->getLocationService();
1515
1516
        $location = $locationService->loadLocation($mediaLocationId);
1517
1518
        $locationService->deleteLocation($location);
1519
        /* END: Use Case */
1520
1521
        try {
1522
            $locationService->loadLocation($mediaLocationId);
1523
            $this->fail("Location $mediaLocationId not deleted.");
1524
        } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1525
        }
1526
1527
        // The following IDs are IDs of child locations of $mediaLocationId location
1528
        // ( Media/Images, Media/Files, Media/Multimedia respectively )
1529
        foreach (array(51, 52, 53) as $childLocationId) {
1530
            try {
1531
                $locationService->loadLocation($this->generateId('location', $childLocationId));
1532
                $this->fail("Location $childLocationId not deleted.");
1533
            } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1534
            }
1535
        }
1536
1537
        // The following IDs are IDs of content below $mediaLocationId location
1538
        // ( Media/Images, Media/Files, Media/Multimedia respectively )
1539
        $contentService = $this->getRepository()->getContentService();
1540
        foreach (array(49, 50, 51) as $childContentId) {
1541
            try {
1542
                $contentService->loadContentInfo($this->generateId('object', $childContentId));
1543
                $this->fail("Content $childContentId not deleted.");
1544
            } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1545
            }
1546
        }
1547
    }
1548
1549
    /**
1550
     * Test for the deleteLocation() method.
1551
     *
1552
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1553
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testDeleteLocation
1554
     */
1555
    public function testDeleteLocationDecrementsChildCountOnParent()
1556
    {
1557
        $repository = $this->getRepository();
1558
1559
        $mediaLocationId = $this->generateId('location', 43);
1560
        /* BEGIN: Use Case */
1561
        // $mediaLocationId is the ID of the location of the
1562
        // "Media" location in an eZ Publish demo installation
1563
1564
        $locationService = $repository->getLocationService();
1565
1566
        // Load the current the user group location
1567
        $location = $locationService->loadLocation($mediaLocationId);
1568
1569
        // Load the parent location
1570
        $parentLocation = $locationService->loadLocation(
1571
            $location->parentLocationId
1572
        );
1573
1574
        // Get child count
1575
        $childCountBefore = $locationService->getLocationChildCount($parentLocation);
1576
1577
        // Delete the user group location
1578
        $locationService->deleteLocation($location);
1579
1580
        $this->refreshSearch($repository);
1581
1582
        // Reload parent location
1583
        $parentLocation = $locationService->loadLocation(
1584
            $location->parentLocationId
1585
        );
1586
1587
        // This will be $childCountBefore - 1
1588
        $childCountAfter = $locationService->getLocationChildCount($parentLocation);
1589
        /* END: Use Case */
1590
1591
        $this->assertEquals($childCountBefore - 1, $childCountAfter);
1592
    }
1593
1594
    /**
1595
     * Test for the deleteLocation() method.
1596
     *
1597
     * Related issue: EZP-21904
1598
     *
1599
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1600
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1601
     */
1602
    public function testDeleteContentObjectLastLocation()
1603
    {
1604
        $repository = $this->getRepository();
1605
1606
        /* BEGIN: Use case */
1607
        $contentService = $repository->getContentService();
1608
        $locationService = $repository->getLocationService();
1609
        $contentTypeService = $repository->getContentTypeService();
1610
        $urlAliasService = $repository->getURLAliasService();
1611
1612
        // prepare Content object
1613
        $createStruct = $contentService->newContentCreateStruct(
1614
            $contentTypeService->loadContentTypeByIdentifier('folder'),
1615
            'eng-GB'
1616
        );
1617
        $createStruct->setField('name', 'Test folder');
1618
1619
        // creata Content object
1620
        $content = $contentService->publishVersion(
1621
            $contentService->createContent(
1622
                $createStruct,
1623
                array($locationService->newLocationCreateStruct(2))
1624
            )->versionInfo
1625
        );
1626
1627
        // delete location
1628
        $locationService->deleteLocation(
1629
            $locationService->loadLocation(
1630
                $urlAliasService->lookup('/Test-folder')->destination
1631
            )
1632
        );
1633
1634
        // this should throw a not found exception
1635
        $contentService->loadContent($content->versionInfo->contentInfo->id);
1636
        /* END: Use case*/
1637
    }
1638
1639
    /**
1640
     * Test for the copySubtree() method.
1641
     *
1642
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1643
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1644
     */
1645
    public function testCopySubtree()
1646
    {
1647
        $repository = $this->getRepository();
1648
1649
        $mediaLocationId = $this->generateId('location', 43);
1650
        $demoDesignLocationId = $this->generateId('location', 56);
1651
        /* BEGIN: Use Case */
1652
        // $mediaLocationId is the ID of the "Media" page location in
1653
        // an eZ Publish demo installation
1654
1655
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1656
        // Publish demo installation
1657
1658
        // Load the location service
1659
        $locationService = $repository->getLocationService();
1660
1661
        // Load location to copy
1662
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1663
1664
        // Load new parent location
1665
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1666
1667
        // Copy location "Media" to "Demo Design"
1668
        $copiedLocation = $locationService->copySubtree(
1669
            $locationToCopy,
1670
            $newParentLocation
1671
        );
1672
        /* END: Use Case */
1673
1674
        $this->assertInstanceOf(
1675
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1676
            $copiedLocation
1677
        );
1678
1679
        $this->assertPropertiesCorrect(
1680
            array(
1681
                'depth' => $newParentLocation->depth + 1,
1682
                'parentLocationId' => $newParentLocation->id,
1683
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $copiedLocation->id) . '/',
1684
            ),
1685
            $copiedLocation
1686
        );
1687
1688
        $this->assertDefaultContentStates($copiedLocation->contentInfo);
1689
    }
1690
1691
    /**
1692
     * Test for the copySubtree() method.
1693
     *
1694
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1695
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1696
     */
1697
    public function testCopySubtreeWithAliases()
1698
    {
1699
        $repository = $this->getRepository();
1700
        $urlAliasService = $repository->getURLAliasService();
1701
1702
        // $mediaLocationId is the ID of the "Media" page location in
1703
        // an eZ Publish demo installation
1704
1705
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1706
        // Publish demo installation
1707
        $mediaLocationId = $this->generateId('location', 43);
1708
        $demoDesignLocationId = $this->generateId('location', 56);
1709
1710
        $locationService = $repository->getLocationService();
1711
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1712
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1713
1714
        $expectedSubItemAliases = [
1715
            '/Design/Plain-site/Media/Multimedia',
1716
            '/Design/Plain-site/Media/Images',
1717
            '/Design/Plain-site/Media/Files',
1718
        ];
1719
1720
        $this->assertAliasesBeforeCopy($urlAliasService, $expectedSubItemAliases);
1721
1722
        // Copy location "Media" to "Design"
1723
        $locationService->copySubtree(
1724
            $locationToCopy,
1725
            $newParentLocation
1726
        );
1727
1728
        $this->assertGeneratedAliases($urlAliasService, $expectedSubItemAliases);
1729
    }
1730
1731
    /**
1732
     * Asserts that given Content has default ContentStates.
1733
     *
1734
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
1735
     */
1736 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
1737
    {
1738
        $repository = $this->getRepository();
1739
        $objectStateService = $repository->getObjectStateService();
1740
1741
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
1742
1743
        foreach ($objectStateGroups as $objectStateGroup) {
1744
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
1745
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
1746
                // Only check the first object state which is the default one.
1747
                $this->assertEquals(
1748
                    $objectState,
1749
                    $contentState
1750
                );
1751
                break;
1752
            }
1753
        }
1754
    }
1755
1756
    /**
1757
     * Test for the copySubtree() method.
1758
     *
1759
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1760
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1761
     */
1762
    public function testCopySubtreeUpdatesSubtreeProperties()
1763
    {
1764
        $repository = $this->getRepository();
1765
        $locationService = $repository->getLocationService();
1766
1767
        $locationToCopy = $locationService->loadLocation($this->generateId('location', 43));
1768
1769
        // Load Subtree properties before copy
1770
        $expected = $this->loadSubtreeProperties($locationToCopy);
1771
1772
        $mediaLocationId = $this->generateId('location', 43);
1773
        $demoDesignLocationId = $this->generateId('location', 56);
1774
        /* BEGIN: Use Case */
1775
        // $mediaLocationId is the ID of the "Media" page location in
1776
        // an eZ Publish demo installation
1777
1778
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1779
        // Publish demo installation
1780
1781
        // Load the location service
1782
        $locationService = $repository->getLocationService();
1783
1784
        // Load location to copy
1785
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1786
1787
        // Load new parent location
1788
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1789
1790
        // Copy location "Media" to "Demo Design"
1791
        $copiedLocation = $locationService->copySubtree(
1792
            $locationToCopy,
1793
            $newParentLocation
1794
        );
1795
        /* END: Use Case */
1796
1797
        $beforeIds = array();
1798
        foreach ($expected as $properties) {
1799
            $beforeIds[] = $properties['id'];
1800
        }
1801
1802
        $this->refreshSearch($repository);
1803
1804
        // Load Subtree properties after copy
1805
        $actual = $this->loadSubtreeProperties($copiedLocation);
1806
1807
        $this->assertEquals(count($expected), count($actual));
1808
1809
        foreach ($actual as $properties) {
1810
            $this->assertNotContains($properties['id'], $beforeIds);
1811
            $this->assertStringStartsWith(
1812
                "{$newParentLocation->pathString}" . $this->parseId('location', $copiedLocation->id) . '/',
1813
                $properties['pathString']
1814
            );
1815
            $this->assertStringEndsWith(
1816
                '/' . $this->parseId('location', $properties['id']) . '/',
1817
                $properties['pathString']
1818
            );
1819
        }
1820
    }
1821
1822
    /**
1823
     * Test for the copySubtree() method.
1824
     *
1825
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1826
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1827
     */
1828
    public function testCopySubtreeIncrementsChildCountOfNewParent()
1829
    {
1830
        $repository = $this->getRepository();
1831
        $locationService = $repository->getLocationService();
1832
1833
        $childCountBefore = $locationService->getLocationChildCount($locationService->loadLocation(56));
1834
1835
        $mediaLocationId = $this->generateId('location', 43);
1836
        $demoDesignLocationId = $this->generateId('location', 56);
1837
        /* BEGIN: Use Case */
1838
        // $mediaLocationId is the ID of the "Media" page location in
1839
        // an eZ Publish demo installation
1840
1841
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1842
        // Publish demo installation
1843
1844
        // Load the location service
1845
        $locationService = $repository->getLocationService();
1846
1847
        // Load location to copy
1848
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1849
1850
        // Load new parent location
1851
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1852
1853
        // Copy location "Media" to "Demo Design"
1854
        $copiedLocation = $locationService->copySubtree(
0 ignored issues
show
Unused Code introduced by
$copiedLocation 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...
1855
            $locationToCopy,
1856
            $newParentLocation
1857
        );
1858
        /* END: Use Case */
1859
1860
        $this->refreshSearch($repository);
1861
1862
        $childCountAfter = $locationService->getLocationChildCount($locationService->loadLocation($demoDesignLocationId));
1863
1864
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
1865
    }
1866
1867
    /**
1868
     * Test for the copySubtree() method.
1869
     *
1870
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1871
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1872
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1873
     */
1874 View Code Duplication
    public function testCopySubtreeThrowsInvalidArgumentException()
1875
    {
1876
        $repository = $this->getRepository();
1877
1878
        $communityLocationId = $this->generateId('location', 5);
1879
        /* BEGIN: Use Case */
1880
        // $communityLocationId is the ID of the "Community" page location in
1881
        // an eZ Publish demo installation
1882
1883
        // Load the location service
1884
        $locationService = $repository->getLocationService();
1885
1886
        // Load location to copy
1887
        $locationToCopy = $locationService->loadLocation($communityLocationId);
1888
1889
        // Use a child as new parent
1890
        $childLocations = $locationService->loadLocationChildren($locationToCopy)->locations;
1891
        $newParentLocation = end($childLocations);
1892
1893
        // This call will fail with an "InvalidArgumentException", because the
1894
        // new parent is a child location of the subtree to copy.
1895
        $locationService->copySubtree(
1896
            $locationToCopy,
1897
            $newParentLocation
0 ignored issues
show
Security Bug introduced by
It seems like $newParentLocation defined by end($childLocations) on line 1891 can also be of type false; however, eZ\Publish\API\Repositor...nService::copySubtree() does only seem to accept object<eZ\Publish\API\Re...alues\Content\Location>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1898
        );
1899
        /* END: Use Case */
1900
    }
1901
1902
    /**
1903
     * Test for the moveSubtree() method.
1904
     *
1905
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1906
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1907
     */
1908
    public function testMoveSubtree()
1909
    {
1910
        $repository = $this->getRepository();
1911
1912
        $mediaLocationId = $this->generateId('location', 43);
1913
        $demoDesignLocationId = $this->generateId('location', 56);
1914
        /* BEGIN: Use Case */
1915
        // $mediaLocationId is the ID of the "Media" page location in
1916
        // an eZ Publish demo installation
1917
1918
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1919
        // Publish demo installation
1920
1921
        // Load the location service
1922
        $locationService = $repository->getLocationService();
1923
1924
        // Load location to move
1925
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1926
1927
        // Load new parent location
1928
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1929
1930
        // Move location from "Home" to "Demo Design"
1931
        $locationService->moveSubtree(
1932
            $locationToMove,
1933
            $newParentLocation
1934
        );
1935
1936
        // Load moved location
1937
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1938
        /* END: Use Case */
1939
1940
        $this->assertPropertiesCorrect(
1941
            array(
1942
                'hidden' => false,
1943
                'invisible' => false,
1944
                'depth' => $newParentLocation->depth + 1,
1945
                'parentLocationId' => $newParentLocation->id,
1946
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
1947
            ),
1948
            $movedLocation
1949
        );
1950
    }
1951
1952
    /**
1953
     * Test for the moveSubtree() method.
1954
     *
1955
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1956
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
1957
     */
1958
    public function testMoveSubtreeHidden()
1959
    {
1960
        $repository = $this->getRepository();
1961
1962
        $mediaLocationId = $this->generateId('location', 43);
1963
        $demoDesignLocationId = $this->generateId('location', 56);
1964
        /* BEGIN: Use Case */
1965
        // $mediaLocationId is the ID of the "Media" page location in
1966
        // an eZ Publish demo installation
1967
1968
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1969
        // Publish demo installation
1970
1971
        // Load the location service
1972
        $locationService = $repository->getLocationService();
1973
1974
        // Load location to move
1975
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1976
1977
        // Load new parent location
1978
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1979
1980
        // Hide the target location before we move
1981
        $newParentLocation = $locationService->hideLocation($newParentLocation);
1982
1983
        // Move location from "Home" to "Demo Design"
1984
        $locationService->moveSubtree(
1985
            $locationToMove,
1986
            $newParentLocation
1987
        );
1988
1989
        // Load moved location
1990
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1991
        /* END: Use Case */
1992
1993
        $this->assertPropertiesCorrect(
1994
            array(
1995
                'hidden' => false,
1996
                'invisible' => true,
1997
                'depth' => $newParentLocation->depth + 1,
1998
                'parentLocationId' => $newParentLocation->id,
1999
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
2000
            ),
2001
            $movedLocation
2002
        );
2003
    }
2004
2005
    /**
2006
     * Test for the moveSubtree() method.
2007
     *
2008
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2009
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2010
     */
2011
    public function testMoveSubtreeUpdatesSubtreeProperties()
2012
    {
2013
        $repository = $this->getRepository();
2014
        $locationService = $repository->getLocationService();
2015
2016
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
2017
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
2018
2019
        // Load Subtree properties before move
2020
        $expected = $this->loadSubtreeProperties($locationToMove);
2021
        foreach ($expected as $id => $properties) {
2022
            $expected[$id]['depth'] = $properties['depth'] + 2;
2023
            $expected[$id]['pathString'] = str_replace(
2024
                $locationToMove->pathString,
2025
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
2026
                $properties['pathString']
2027
            );
2028
        }
2029
2030
        $mediaLocationId = $this->generateId('location', 43);
2031
        $demoDesignLocationId = $this->generateId('location', 56);
2032
        /* BEGIN: Use Case */
2033
        // $mediaLocationId is the ID of the "Media" page location in
2034
        // an eZ Publish demo installation
2035
2036
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2037
        // Publish demo installation
2038
2039
        // Load the location service
2040
        $locationService = $repository->getLocationService();
2041
2042
        // Load location to move
2043
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2044
2045
        // Load new parent location
2046
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2047
2048
        // Move location from "Home" to "Demo Design"
2049
        $locationService->moveSubtree(
2050
            $locationToMove,
2051
            $newParentLocation
2052
        );
2053
2054
        // Load moved location
2055
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2056
        /* END: Use Case */
2057
2058
        $this->refreshSearch($repository);
2059
2060
        // Load Subtree properties after move
2061
        $actual = $this->loadSubtreeProperties($movedLocation);
2062
2063
        $this->assertEquals($expected, $actual);
2064
    }
2065
2066
    /**
2067
     * Test for the moveSubtree() method.
2068
     *
2069
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2070
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtreeUpdatesSubtreeProperties
2071
     */
2072
    public function testMoveSubtreeUpdatesSubtreePropertiesHidden()
2073
    {
2074
        $repository = $this->getRepository();
2075
        $locationService = $repository->getLocationService();
2076
2077
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
2078
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
2079
2080
        // Hide the target location before we move
2081
        $newParentLocation = $locationService->hideLocation($newParentLocation);
2082
2083
        // Load Subtree properties before move
2084
        $expected = $this->loadSubtreeProperties($locationToMove);
2085
        foreach ($expected as $id => $properties) {
2086
            $expected[$id]['invisible'] = true;
2087
            $expected[$id]['depth'] = $properties['depth'] + 2;
2088
            $expected[$id]['pathString'] = str_replace(
2089
                $locationToMove->pathString,
2090
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
2091
                $properties['pathString']
2092
            );
2093
        }
2094
2095
        $mediaLocationId = $this->generateId('location', 43);
2096
        $demoDesignLocationId = $this->generateId('location', 56);
2097
        /* BEGIN: Use Case */
2098
        // $mediaLocationId is the ID of the "Media" page location in
2099
        // an eZ Publish demo installation
2100
2101
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2102
        // Publish demo installation
2103
2104
        // Load the location service
2105
        $locationService = $repository->getLocationService();
2106
2107
        // Load location to move
2108
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2109
2110
        // Load new parent location
2111
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2112
2113
        // Move location from "Home" to "Demo Design"
2114
        $locationService->moveSubtree(
2115
            $locationToMove,
2116
            $newParentLocation
2117
        );
2118
2119
        // Load moved location
2120
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2121
        /* END: Use Case */
2122
2123
        $this->refreshSearch($repository);
2124
2125
        // Load Subtree properties after move
2126
        $actual = $this->loadSubtreeProperties($movedLocation);
2127
2128
        $this->assertEquals($expected, $actual);
2129
    }
2130
2131
    /**
2132
     * Test for the moveSubtree() method.
2133
     *
2134
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2135
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2136
     */
2137 View Code Duplication
    public function testMoveSubtreeIncrementsChildCountOfNewParent()
2138
    {
2139
        $repository = $this->getRepository();
2140
        $locationService = $repository->getLocationService();
2141
2142
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
2143
2144
        // Load expected properties before move
2145
        $expected = $this->loadLocationProperties($newParentLocation);
2146
        $childCountBefore = $locationService->getLocationChildCount($newParentLocation);
2147
2148
        $mediaLocationId = $this->generateId('location', 43);
2149
        $demoDesignLocationId = $this->generateId('location', 56);
2150
        /* BEGIN: Use Case */
2151
        // $mediaLocationId is the ID of the "Media" page location in
2152
        // an eZ Publish demo installation
2153
2154
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2155
        // Publish demo installation
2156
2157
        // Load the location service
2158
        $locationService = $repository->getLocationService();
2159
2160
        // Load location to move
2161
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2162
2163
        // Load new parent location
2164
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2165
2166
        // Move location from "Home" to "Demo Design"
2167
        $locationService->moveSubtree(
2168
            $locationToMove,
2169
            $newParentLocation
2170
        );
2171
2172
        // Load moved location
2173
        $movedLocation = $locationService->loadLocation($mediaLocationId);
0 ignored issues
show
Unused Code introduced by
$movedLocation 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...
2174
2175
        // Reload new parent location
2176
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2177
        /* END: Use Case */
2178
2179
        $this->refreshSearch($repository);
2180
2181
        // Load Subtree properties after move
2182
        $actual = $this->loadLocationProperties($newParentLocation);
2183
        $childCountAfter = $locationService->getLocationChildCount($newParentLocation);
2184
2185
        $this->assertEquals($expected, $actual);
2186
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
2187
    }
2188
2189
    /**
2190
     * Test for the moveSubtree() method.
2191
     *
2192
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2193
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2194
     */
2195 View Code Duplication
    public function testMoveSubtreeDecrementsChildCountOfOldParent()
2196
    {
2197
        $repository = $this->getRepository();
2198
        $locationService = $repository->getLocationService();
2199
2200
        $oldParentLocation = $locationService->loadLocation($this->generateId('location', 1));
2201
2202
        // Load expected properties before move
2203
        $expected = $this->loadLocationProperties($oldParentLocation);
2204
        $childCountBefore = $locationService->getLocationChildCount($oldParentLocation);
2205
2206
        $mediaLocationId = $this->generateId('location', 43);
2207
        $demoDesignLocationId = $this->generateId('location', 56);
2208
        /* BEGIN: Use Case */
2209
        // $mediaLocationId is the ID of the "Media" page location in
2210
        // an eZ Publish demo installation
2211
2212
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2213
        // Publish demo installation
2214
2215
        // Load the location service
2216
        $locationService = $repository->getLocationService();
2217
2218
        // Load location to move
2219
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2220
2221
        // Get the location id of the old parent
2222
        $oldParentLocationId = $locationToMove->parentLocationId;
2223
2224
        // Load new parent location
2225
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2226
2227
        // Move location from "Home" to "Demo Design"
2228
        $locationService->moveSubtree(
2229
            $locationToMove,
2230
            $newParentLocation
2231
        );
2232
2233
        // Reload old parent location
2234
        $oldParentLocation = $locationService->loadLocation($oldParentLocationId);
2235
        /* END: Use Case */
2236
2237
        $this->refreshSearch($repository);
2238
2239
        // Load Subtree properties after move
2240
        $actual = $this->loadLocationProperties($oldParentLocation);
2241
        $childCountAfter = $locationService->getLocationChildCount($oldParentLocation);
2242
2243
        $this->assertEquals($expected, $actual);
2244
        $this->assertEquals($childCountBefore - 1, $childCountAfter);
2245
    }
2246
2247
    /**
2248
     * Test for the moveSubtree() method.
2249
     *
2250
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2251
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2252
     */
2253 View Code Duplication
    public function testMoveSubtreeThrowsInvalidArgumentException()
2254
    {
2255
        $repository = $this->getRepository();
2256
        $mediaLocationId = $this->generateId('location', 43);
2257
        $multimediaLocationId = $this->generateId('location', 53);
2258
2259
        /* BEGIN: Use Case */
2260
        // $mediaLocationId is the ID of the "Media" page location in
2261
        // an eZ Publish demo installation
2262
2263
        // $multimediaLocationId is the ID of the "Multimedia" page location in an eZ
2264
        // Publish demo installation
2265
2266
        // Load the location service
2267
        $locationService = $repository->getLocationService();
2268
2269
        // Load location to move
2270
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2271
2272
        // Load new parent location
2273
        $newParentLocation = $locationService->loadLocation($multimediaLocationId);
2274
2275
        // Throws an exception because new parent location is placed below location to move
2276
        $locationService->moveSubtree(
2277
            $locationToMove,
2278
            $newParentLocation
2279
        );
2280
        /* END: Use Case */
2281
    }
2282
2283
    /**
2284
     * Loads properties from all locations in the $location's subtree.
2285
     *
2286
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2287
     * @param array $properties
2288
     *
2289
     * @return array
2290
     */
2291
    private function loadSubtreeProperties(Location $location, array $properties = array())
2292
    {
2293
        $locationService = $this->getRepository()->getLocationService();
2294
2295
        foreach ($locationService->loadLocationChildren($location)->locations as $childLocation) {
2296
            $properties[] = $this->loadLocationProperties($childLocation);
2297
2298
            $properties = $this->loadSubtreeProperties($childLocation, $properties);
2299
        }
2300
2301
        return $properties;
2302
    }
2303
2304
    /**
2305
     * Loads assertable properties from the given location.
2306
     *
2307
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2308
     * @param mixed[] $overwrite
2309
     *
2310
     * @return array
2311
     */
2312
    private function loadLocationProperties(Location $location, array $overwrite = array())
2313
    {
2314
        return array_merge(
2315
            array(
2316
                'id' => $location->id,
2317
                'depth' => $location->depth,
2318
                'parentLocationId' => $location->parentLocationId,
2319
                'pathString' => $location->pathString,
2320
                'remoteId' => $location->remoteId,
2321
                'hidden' => $location->hidden,
2322
                'invisible' => $location->invisible,
2323
                'priority' => $location->priority,
2324
                'sortField' => $location->sortField,
2325
                'sortOrder' => $location->sortOrder,
2326
            ),
2327
            $overwrite
2328
        );
2329
    }
2330
2331
    /**
2332
     * Assert generated aliases to expected alias return.
2333
     *
2334
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
2335
     * @param array $expectedAliases
2336
     */
2337
    protected function assertGeneratedAliases($urlAliasService, array $expectedAliases)
2338
    {
2339
        foreach ($expectedAliases as $expectedAlias) {
2340
            $urlAlias = $urlAliasService->lookup($expectedAlias);
2341
            $this->assertPropertiesCorrect(['type' => 0], $urlAlias);
2342
        }
2343
    }
2344
2345
    /**
2346
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
2347
     * @param array $expectedSubItemAliases
2348
     */
2349
    private function assertAliasesBeforeCopy($urlAliasService, array $expectedSubItemAliases)
2350
    {
2351
        foreach ($expectedSubItemAliases as $aliasUrl) {
2352
            try {
2353
                $urlAliasService->lookup($aliasUrl);
2354
                $this->fail('We didn\'t expect to find alias, but it was found');
2355
            } catch (\Exception $e) {
2356
                $this->assertTrue(true); // OK - alias was not found
2357
            }
2358
        }
2359
    }
2360
2361
    /**
2362
     * Create and publish Content with the given parent Location.
2363
     *
2364
     * @param string $contentName
2365
     * @param int $parentLocationId
2366
     *
2367
     * @return \eZ\Publish\API\Repository\Values\Content\Content published Content
2368
     */
2369 View Code Duplication
    private function publishContentWithParentLocation($contentName, $parentLocationId)
2370
    {
2371
        $repository = $this->getRepository(false);
2372
        $locationService = $repository->getLocationService();
2373
2374
        $contentService = $repository->getContentService();
2375
        $contentTypeService = $repository->getContentTypeService();
2376
2377
        $contentCreateStruct = $contentService->newContentCreateStruct(
2378
            $contentTypeService->loadContentTypeByIdentifier('folder'),
2379
            'eng-US'
2380
        );
2381
        $contentCreateStruct->setField('name', $contentName);
2382
        $contentDraft = $contentService->createContent(
2383
            $contentCreateStruct,
2384
            [
2385
                $locationService->newLocationCreateStruct($parentLocationId),
2386
            ]
2387
        );
2388
2389
        return $contentService->publishVersion($contentDraft->versionInfo);
2390
    }
2391
}
2392