Completed
Push — 6.13 ( b9a79e )
by
unknown
25:06 queued 12:25
created

testLoadParentLocationsForDraftContentThrowsBadStateException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
rs 9.4285
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
     * @see \eZ\Publish\API\Repository\LocationService::createLocation()
263
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
264
     */
265
    public function testCreateLocationInTransactionWithRollback()
266
    {
267
        $repository = $this->getRepository();
268
269
        $contentId = $this->generateId('object', 41);
270
        $parentLocationId = $this->generateId('location', 5);
271
        /* BEGIN: Use Case */
272
        // $contentId is the ID of an existing content object
273
        // $parentLocationId is the ID of an existing location
274
        $contentService = $repository->getContentService();
275
        $locationService = $repository->getLocationService();
276
277
        $repository->beginTransaction();
278
279
        try {
280
            // ContentInfo for "How to use eZ Publish"
281
            $contentInfo = $contentService->loadContentInfo($contentId);
282
283
            $locationCreate = $locationService->newLocationCreateStruct($parentLocationId);
284
            $locationCreate->remoteId = 'sindelfingen';
285
286
            $createdLocationId = $locationService->createLocation(
287
                $contentInfo,
288
                $locationCreate
289
            )->id;
290
        } catch (Exception $e) {
291
            // Cleanup hanging transaction on error
292
            $repository->rollback();
293
            throw $e;
294
        }
295
296
        $repository->rollback();
297
298
        try {
299
            // Throws exception since creation of location was rolled back
300
            $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...
301
        } catch (NotFoundException $e) {
302
            return;
303
        }
304
        /* END: Use Case */
305
306
        $this->fail('Objects still exists after rollback.');
307
    }
308
309
    /**
310
     * Test for the loadLocation() method.
311
     *
312
     * @return \eZ\Publish\API\Repository\Values\Content\Location
313
     *
314
     * @covers \eZ\Publish\API\Repository\LocationService::loadLocation
315
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
316
     */
317
    public function testLoadLocation()
318
    {
319
        $repository = $this->getRepository();
320
321
        $locationId = $this->generateId('location', 5);
322
        /* BEGIN: Use Case */
323
        // $locationId is the ID of an existing location
324
        $locationService = $repository->getLocationService();
325
326
        $location = $locationService->loadLocation($locationId);
327
        /* END: Use Case */
328
329
        $this->assertInstanceOf(
330
            Location::class,
331
            $location
332
        );
333
        self::assertEquals(5, $location->id);
334
335
        return $location;
336
    }
337
338
    /**
339
     * Test for the loadLocation() method.
340
     *
341
     * @see \eZ\Publish\API\Repository\LocationService::loadLocation()
342
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
343
     */
344
    public function testLoadLocationRootStructValues()
345
    {
346
        $repository = $this->getRepository();
347
        $locationService = $repository->getLocationService();
348
        $location = $locationService->loadLocation($this->generateId('location', 1));
349
350
        $legacyDateTime = new \DateTime();
351
        $legacyDateTime->setTimestamp(1030968000);
352
353
        // $location
354
        $this->assertPropertiesCorrect(
355
            array(
356
                'id' => $this->generateId('location', 1),
357
                'status' => 1,
358
                'priority' => 0,
359
                'hidden' => false,
360
                'invisible' => false,
361
                'remoteId' => '629709ba256fe317c3ddcee35453a96a',
362
                'parentLocationId' => $this->generateId('location', 1),
363
                'pathString' => '/1/',
364
                'depth' => 0,
365
                'sortField' => 1,
366
                'sortOrder' => 1,
367
            ),
368
            $location
369
        );
370
371
        // $location->contentInfo
372
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $location->contentInfo);
373
        $this->assertPropertiesCorrect(
374
            array(
375
                'id' => $this->generateId('content', 0),
376
                'name' => 'Top Level Nodes',
377
                'sectionId' => 1,
378
                'mainLocationId' => 1,
379
                'contentTypeId' => 1,
380
                'currentVersionNo' => 1,
381
                'published' => 1,
382
                'ownerId' => 14,
383
                'modificationDate' => $legacyDateTime,
384
                'publishedDate' => $legacyDateTime,
385
                'alwaysAvailable' => 1,
386
                'remoteId' => null,
387
                'mainLanguageCode' => 'eng-GB',
388
            ),
389
            $location->contentInfo
390
        );
391
    }
392
393
    /**
394
     * Test for the loadLocation() method.
395
     *
396
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
397
     *
398
     * @see \eZ\Publish\API\Repository\LocationService::loadLocation()
399
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
400
     */
401
    public function testLoadLocationStructValues(Location $location)
402
    {
403
        $this->assertPropertiesCorrect(
404
            array(
405
                'id' => $this->generateId('location', 5),
406
                'priority' => 0,
407
                'hidden' => false,
408
                'invisible' => false,
409
                'remoteId' => '3f6d92f8044aed134f32153517850f5a',
410
                'parentLocationId' => $this->generateId('location', 1),
411
                'pathString' => '/1/5/',
412
                'depth' => 1,
413
                'sortField' => 1,
414
                'sortOrder' => 1,
415
            ),
416
            $location
417
        );
418
419
        $this->assertInstanceOf(
420
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
421
            $location->contentInfo
422
        );
423
        $this->assertEquals($this->generateId('object', 4), $location->contentInfo->id);
424
    }
425
426
    /**
427
     * Test for the loadLocation() method.
428
     *
429
     * @see \eZ\Publish\API\Repository\LocationService::loadLocation()
430
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
431
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
432
     */
433
    public function testLoadLocationThrowsNotFoundException()
434
    {
435
        $repository = $this->getRepository();
436
437
        $nonExistentLocationId = $this->generateId('location', 2342);
438
        /* BEGIN: Use Case */
439
        $locationService = $repository->getLocationService();
440
441
        // Throws exception, if Location with $nonExistentLocationId does not
442
        // exist
443
        $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...
444
        /* END: Use Case */
445
    }
446
447
    /**
448
     * Test for the loadLocationByRemoteId() method.
449
     *
450
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationByRemoteId()
451
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
452
     */
453 View Code Duplication
    public function testLoadLocationByRemoteId()
454
    {
455
        $repository = $this->getRepository();
456
457
        /* BEGIN: Use Case */
458
        $locationService = $repository->getLocationService();
459
460
        $location = $locationService->loadLocationByRemoteId(
461
            '3f6d92f8044aed134f32153517850f5a'
462
        );
463
        /* END: Use Case */
464
465
        $this->assertEquals(
466
            $locationService->loadLocation($this->generateId('location', 5)),
467
            $location
468
        );
469
    }
470
471
    /**
472
     * Test for the loadLocationByRemoteId() method.
473
     *
474
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationByRemoteId()
475
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
476
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
477
     */
478
    public function testLoadLocationByRemoteIdThrowsNotFoundException()
479
    {
480
        $repository = $this->getRepository();
481
482
        /* BEGIN: Use Case */
483
        $locationService = $repository->getLocationService();
484
485
        // Throws exception, since Location with remote ID does not exist
486
        $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...
487
            'not-exists'
488
        );
489
        /* END: Use Case */
490
    }
491
492
    /**
493
     * Test for the loadLocations() method.
494
     *
495
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
496
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
497
     */
498
    public function testLoadLocations()
499
    {
500
        $repository = $this->getRepository();
501
502
        $contentId = $this->generateId('object', 4);
503
        /* BEGIN: Use Case */
504
        // $contentId contains the ID of an existing content object
505
        $contentService = $repository->getContentService();
506
        $locationService = $repository->getLocationService();
507
508
        $contentInfo = $contentService->loadContentInfo($contentId);
509
510
        $locations = $locationService->loadLocations($contentInfo);
511
        /* END: Use Case */
512
513
        $this->assertInternalType('array', $locations);
514
        self::assertNotEmpty($locations);
515
516
        foreach ($locations as $location) {
517
            self::assertInstanceOf(Location::class, $location);
518
            self::assertEquals($contentInfo->id, $location->getContentInfo()->id);
519
        }
520
521
        return $locations;
522
    }
523
524
    /**
525
     * Test for the loadLocations() method.
526
     *
527
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
528
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
529
     */
530
    public function testLoadLocationsContent(array $locations)
531
    {
532
        $repository = $this->getRepository();
533
        $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...
534
535
        $this->assertEquals(1, count($locations));
536
        foreach ($locations as $loadedLocation) {
537
            $this->assertInstanceOf(
538
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
539
                $loadedLocation
540
            );
541
        }
542
543
        usort(
544
            $locations,
545
            function ($a, $b) {
546
                strcmp($a->id, $b->id);
547
            }
548
        );
549
550
        $this->assertEquals(
551
            array($this->generateId('location', 5)),
552
            array_map(
553
                function (Location $location) {
554
                    return $location->id;
555
                },
556
                $locations
557
            )
558
        );
559
    }
560
561
    /**
562
     * Test for the loadLocations() method.
563
     *
564
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
565
     *
566
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations($contentInfo, $rootLocation)
567
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
568
     */
569
    public function testLoadLocationsLimitedSubtree()
570
    {
571
        $repository = $this->getRepository();
572
573
        $originalLocationId = $this->generateId('location', 54);
574
        $originalParentLocationId = $this->generateId('location', 48);
575
        $newParentLocationId = $this->generateId('location', 43);
576
        /* BEGIN: Use Case */
577
        // $originalLocationId is the ID of an existing location
578
        // $originalParentLocationId is the ID of the parent location of
579
        //     $originalLocationId
580
        // $newParentLocationId is the ID of an existing location outside the tree
581
        // of $originalLocationId and $originalParentLocationId
582
        $locationService = $repository->getLocationService();
583
584
        // Location at "/1/48/54"
585
        $originalLocation = $locationService->loadLocation($originalLocationId);
586
587
        // Create location under "/1/43/"
588
        $locationCreate = $locationService->newLocationCreateStruct($newParentLocationId);
589
        $locationService->createLocation(
590
            $originalLocation->contentInfo,
591
            $locationCreate
592
        );
593
594
        $findRootLocation = $locationService->loadLocation($originalParentLocationId);
595
596
        // Returns an array with only $originalLocation
597
        $locations = $locationService->loadLocations(
598
            $originalLocation->contentInfo,
599
            $findRootLocation
600
        );
601
        /* END: Use Case */
602
603
        $this->assertInternalType('array', $locations);
604
605
        return $locations;
606
    }
607
608
    /**
609
     * Test for the loadLocations() method.
610
     *
611
     * @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
612
     *
613
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
614
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationsLimitedSubtree
615
     */
616
    public function testLoadLocationsLimitedSubtreeContent(array $locations)
617
    {
618
        $this->assertEquals(1, count($locations));
619
620
        $this->assertEquals(
621
            $this->generateId('location', 54),
622
            reset($locations)->id
623
        );
624
    }
625
626
    /**
627
     * Test for the loadLocations() method.
628
     *
629
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
630
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
631
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
632
     */
633
    public function testLoadLocationsThrowsBadStateException()
634
    {
635
        $repository = $this->getRepository();
636
637
        /* BEGIN: Use Case */
638
        $contentTypeService = $repository->getContentTypeService();
639
        $contentService = $repository->getContentService();
640
        $locationService = $repository->getLocationService();
641
642
        // Create new content, which is not published
643
        $folderType = $contentTypeService->loadContentTypeByIdentifier('folder');
644
        $contentCreate = $contentService->newContentCreateStruct($folderType, 'eng-US');
645
        $contentCreate->setField('name', 'New Folder');
646
        $content = $contentService->createContent($contentCreate);
647
648
        // Throws Exception, since $content has no published version, yet
649
        $locationService->loadLocations(
650
            $content->contentInfo
651
        );
652
        /* END: Use Case */
653
    }
654
655
    /**
656
     * Test for the loadLocations() method.
657
     *
658
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations($contentInfo, $rootLocation)
659
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
660
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
661
     */
662
    public function testLoadLocationsThrowsBadStateExceptionLimitedSubtree()
663
    {
664
        $repository = $this->getRepository();
665
666
        $someLocationId = $this->generateId('location', 2);
667
        /* BEGIN: Use Case */
668
        // $someLocationId is the ID of an existing location
669
        $contentTypeService = $repository->getContentTypeService();
670
        $contentService = $repository->getContentService();
671
        $locationService = $repository->getLocationService();
672
673
        // Create new content, which is not published
674
        $folderType = $contentTypeService->loadContentTypeByIdentifier('folder');
675
        $contentCreate = $contentService->newContentCreateStruct($folderType, 'eng-US');
676
        $contentCreate->setField('name', 'New Folder');
677
        $content = $contentService->createContent($contentCreate);
678
679
        $findRootLocation = $locationService->loadLocation($someLocationId);
680
681
        // Throws Exception, since $content has no published version, yet
682
        $locationService->loadLocations(
683
            $content->contentInfo,
684
            $findRootLocation
685
        );
686
        /* END: Use Case */
687
    }
688
689
    /**
690
     * Test for the loadLocationChildren() method.
691
     *
692
     * @covers \eZ\Publish\API\Repository\LocationService::loadLocationChildren
693
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
694
     */
695
    public function testLoadLocationChildren()
696
    {
697
        $repository = $this->getRepository();
698
699
        $locationId = $this->generateId('location', 5);
700
        /* BEGIN: Use Case */
701
        // $locationId is the ID of an existing location
702
        $locationService = $repository->getLocationService();
703
704
        $location = $locationService->loadLocation($locationId);
705
706
        $childLocations = $locationService->loadLocationChildren($location);
707
        /* END: Use Case */
708
709
        $this->assertInstanceOf(LocationList::class, $childLocations);
710
        $this->assertInternalType('array', $childLocations->locations);
711
        $this->assertNotEmpty($childLocations->locations);
712
        $this->assertInternalType('int', $childLocations->totalCount);
713
714
        foreach ($childLocations->locations as $childLocation) {
715
            $this->assertInstanceOf(Location::class, $childLocation);
716
            $this->assertEquals($location->id, $childLocation->parentLocationId);
717
        }
718
719
        return $childLocations;
720
    }
721
722
    /**
723
     * Test loading parent Locations for draft Content.
724
     *
725
     * @covers \eZ\Publish\API\Repository\LocationService::loadParentLocationsForDraftContent
726
     */
727
    public function testLoadParentLocationsForDraftContent()
728
    {
729
        $repository = $this->getRepository();
730
        $locationService = $repository->getLocationService();
731
        $contentService = $repository->getContentService();
732
        $contentTypeService = $repository->getContentTypeService();
733
734
        // prepare locations
735
        $locationCreateStructs = [
736
            $locationService->newLocationCreateStruct(2),
737
            $locationService->newLocationCreateStruct(5),
738
        ];
739
740
        // Create new content
741
        $folderType = $contentTypeService->loadContentTypeByIdentifier('folder');
742
        $contentCreate = $contentService->newContentCreateStruct($folderType, 'eng-US');
743
        $contentCreate->setField('name', 'New Folder');
744
        $contentDraft = $contentService->createContent($contentCreate, $locationCreateStructs);
745
746
        // Test loading parent Locations
747
        $locations = $locationService->loadParentLocationsForDraftContent($contentDraft->versionInfo);
748
749
        self::assertCount(2, $locations);
750
        foreach ($locations as $location) {
751
            // test it is one of the given parent locations
752
            self::assertTrue($location->id === 2 || $location->id === 5);
753
        }
754
755
        return $contentDraft;
756
    }
757
758
    /**
759
     * Test that trying to load parent Locations throws Exception if Content is not a draft.
760
     *
761
     * @depends testLoadParentLocationsForDraftContent
762
     *
763
     * @param \eZ\Publish\API\Repository\Values\Content\Content $contentDraft
764
     */
765
    public function testLoadParentLocationsForDraftContentThrowsBadStateException(Content $contentDraft)
766
    {
767
        $this->expectException(BadStateException::class);
768
        $this->expectExceptionMessageRegExp('/has been already published/');
769
770
        $repository = $this->getRepository(false);
771
        $locationService = $repository->getLocationService();
772
        $contentService = $repository->getContentService();
773
774
        $content = $contentService->publishVersion($contentDraft->versionInfo);
775
776
        $locationService->loadParentLocationsForDraftContent($content->versionInfo);
777
    }
778
779
    /**
780
     * Test for the getLocationChildCount() method.
781
     *
782
     * @see \eZ\Publish\API\Repository\LocationService::getLocationChildCount()
783
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
784
     */
785
    public function testGetLocationChildCount()
786
    {
787
        // $locationId is the ID of an existing location
788
        $locationService = $this->getRepository()->getLocationService();
789
790
        $this->assertSame(
791
            5,
792
            $locationService->getLocationChildCount(
793
                $locationService->loadLocation($this->generateId('location', 5))
794
            )
795
        );
796
    }
797
798
    /**
799
     * Test for the loadLocationChildren() method.
800
     *
801
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren()
802
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
803
     */
804
    public function testLoadLocationChildrenData(LocationList $locations)
805
    {
806
        $this->assertEquals(5, count($locations->locations));
807
        $this->assertEquals(5, $locations->totalCount);
808
809
        foreach ($locations->locations as $location) {
810
            $this->assertInstanceOf(
811
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
812
                $location
813
            );
814
        }
815
816
        $this->assertEquals(
817
            array(
818
                $this->generateId('location', 12),
819
                $this->generateId('location', 13),
820
                $this->generateId('location', 14),
821
                $this->generateId('location', 44),
822
                $this->generateId('location', 61),
823
            ),
824
            array_map(
825
                function (Location $location) {
826
                    return $location->id;
827
                },
828
                $locations->locations
829
            )
830
        );
831
    }
832
833
    /**
834
     * Test for the loadLocationChildren() method.
835
     *
836
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
837
     *
838
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset)
839
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
840
     */
841 View Code Duplication
    public function testLoadLocationChildrenWithOffset()
842
    {
843
        $repository = $this->getRepository();
844
845
        $locationId = $this->generateId('location', 5);
846
        /* BEGIN: Use Case */
847
        // $locationId is the ID of an existing location
848
        $locationService = $repository->getLocationService();
849
850
        $location = $locationService->loadLocation($locationId);
851
852
        $childLocations = $locationService->loadLocationChildren($location, 2);
853
        /* END: Use Case */
854
855
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\LocationList', $childLocations);
856
        $this->assertInternalType('array', $childLocations->locations);
857
        $this->assertInternalType('int', $childLocations->totalCount);
858
859
        return $childLocations;
860
    }
861
862
    /**
863
     * Test for the loadLocationChildren() method.
864
     *
865
     * @param \eZ\Publish\API\Repository\Values\Content\LocationList $locations
866
     *
867
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset)
868
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildrenWithOffset
869
     */
870 View Code Duplication
    public function testLoadLocationChildrenDataWithOffset(LocationList $locations)
871
    {
872
        $this->assertEquals(3, count($locations->locations));
873
        $this->assertEquals(5, $locations->totalCount);
874
875
        foreach ($locations->locations as $location) {
876
            $this->assertInstanceOf(
877
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
878
                $location
879
            );
880
        }
881
882
        $this->assertEquals(
883
            array(
884
                $this->generateId('location', 14),
885
                $this->generateId('location', 44),
886
                $this->generateId('location', 61),
887
            ),
888
            array_map(
889
                function (Location $location) {
890
                    return $location->id;
891
                },
892
                $locations->locations
893
            )
894
        );
895
    }
896
897
    /**
898
     * Test for the loadLocationChildren() method.
899
     *
900
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
901
     *
902
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset, $limit)
903
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
904
     */
905 View Code Duplication
    public function testLoadLocationChildrenWithOffsetAndLimit()
906
    {
907
        $repository = $this->getRepository();
908
909
        $locationId = $this->generateId('location', 5);
910
        /* BEGIN: Use Case */
911
        // $locationId is the ID of an existing location
912
        $locationService = $repository->getLocationService();
913
914
        $location = $locationService->loadLocation($locationId);
915
916
        $childLocations = $locationService->loadLocationChildren($location, 2, 2);
917
        /* END: Use Case */
918
919
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\LocationList', $childLocations);
920
        $this->assertInternalType('array', $childLocations->locations);
921
        $this->assertInternalType('int', $childLocations->totalCount);
922
923
        return $childLocations;
924
    }
925
926
    /**
927
     * Test for the loadLocationChildren() method.
928
     *
929
     * @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
930
     *
931
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset, $limit)
932
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildrenWithOffsetAndLimit
933
     */
934 View Code Duplication
    public function testLoadLocationChildrenDataWithOffsetAndLimit(LocationList $locations)
935
    {
936
        $this->assertEquals(2, count($locations->locations));
937
        $this->assertEquals(5, $locations->totalCount);
938
939
        foreach ($locations->locations as $location) {
940
            $this->assertInstanceOf(
941
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
942
                $location
943
            );
944
        }
945
946
        $this->assertEquals(
947
            array(
948
                $this->generateId('location', 14),
949
                $this->generateId('location', 44),
950
            ),
951
            array_map(
952
                function (Location $location) {
953
                    return $location->id;
954
                },
955
                $locations->locations
956
            )
957
        );
958
    }
959
960
    /**
961
     * Test for the newLocationUpdateStruct() method.
962
     *
963
     * @covers \eZ\Publish\API\Repository\LocationService::newLocationUpdateStruct
964
     */
965 View Code Duplication
    public function testNewLocationUpdateStruct()
966
    {
967
        $repository = $this->getRepository();
968
969
        /* BEGIN: Use Case */
970
        $locationService = $repository->getLocationService();
971
972
        $updateStruct = $locationService->newLocationUpdateStruct();
973
        /* END: Use Case */
974
975
        $this->assertInstanceOf(
976
            LocationUpdateStruct::class,
977
            $updateStruct
978
        );
979
980
        $this->assertPropertiesCorrect(
981
            [
982
                'priority' => null,
983
                'remoteId' => null,
984
                'sortField' => null,
985
                'sortOrder' => null,
986
            ],
987
            $updateStruct
988
        );
989
    }
990
991
    /**
992
     * Test for the updateLocation() method.
993
     *
994
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
995
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
996
     */
997
    public function testUpdateLocation()
998
    {
999
        $repository = $this->getRepository();
1000
1001
        $originalLocationId = $this->generateId('location', 5);
1002
        /* BEGIN: Use Case */
1003
        // $originalLocationId is the ID of an existing location
1004
        $locationService = $repository->getLocationService();
1005
1006
        $originalLocation = $locationService->loadLocation($originalLocationId);
1007
1008
        $updateStruct = $locationService->newLocationUpdateStruct();
1009
        $updateStruct->priority = 3;
1010
        $updateStruct->remoteId = 'c7adcbf1e96bc29bca28c2d809d0c7ef69272651';
1011
        $updateStruct->sortField = Location::SORT_FIELD_PRIORITY;
1012
        $updateStruct->sortOrder = Location::SORT_ORDER_DESC;
1013
1014
        $updatedLocation = $locationService->updateLocation($originalLocation, $updateStruct);
1015
        /* END: Use Case */
1016
1017
        $this->assertInstanceOf(
1018
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1019
            $updatedLocation
1020
        );
1021
1022
        return array(
1023
            'originalLocation' => $originalLocation,
1024
            'updateStruct' => $updateStruct,
1025
            'updatedLocation' => $updatedLocation,
1026
        );
1027
    }
1028
1029
    /**
1030
     * Test for the updateLocation() method.
1031
     *
1032
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1033
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testUpdateLocation
1034
     */
1035
    public function testUpdateLocationStructValues(array $data)
1036
    {
1037
        $originalLocation = $data['originalLocation'];
1038
        $updateStruct = $data['updateStruct'];
1039
        $updatedLocation = $data['updatedLocation'];
1040
1041
        $this->assertPropertiesCorrect(
1042
            array(
1043
                'id' => $originalLocation->id,
1044
                'priority' => $updateStruct->priority,
1045
                'hidden' => $originalLocation->hidden,
1046
                'invisible' => $originalLocation->invisible,
1047
                'remoteId' => $updateStruct->remoteId,
1048
                'contentInfo' => $originalLocation->contentInfo,
1049
                'parentLocationId' => $originalLocation->parentLocationId,
1050
                'pathString' => $originalLocation->pathString,
1051
                'depth' => $originalLocation->depth,
1052
                'sortField' => $updateStruct->sortField,
1053
                'sortOrder' => $updateStruct->sortOrder,
1054
            ),
1055
            $updatedLocation
1056
        );
1057
    }
1058
1059
    /**
1060
     * Test for the updateLocation() method.
1061
     *
1062
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1063
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1064
     */
1065
    public function testUpdateLocationWithSameRemoteId()
1066
    {
1067
        $repository = $this->getRepository();
1068
1069
        $locationId = $this->generateId('location', 5);
1070
        /* BEGIN: Use Case */
1071
        // $locationId and remote ID is the IDs of the same, existing location
1072
        $locationService = $repository->getLocationService();
1073
1074
        $originalLocation = $locationService->loadLocation($locationId);
1075
1076
        $updateStruct = $locationService->newLocationUpdateStruct();
1077
1078
        // Remote ID of an existing location with the same locationId
1079
        $updateStruct->remoteId = $originalLocation->remoteId;
1080
1081
        // Sets one of the properties to be able to confirm location gets updated, here: priority
1082
        $updateStruct->priority = 2;
1083
1084
        $location = $locationService->updateLocation($originalLocation, $updateStruct);
1085
1086
        // Checks that the location was updated
1087
        $this->assertEquals(2, $location->priority);
1088
1089
        // Checks that remoteId remains the same
1090
        $this->assertEquals($originalLocation->remoteId, $location->remoteId);
1091
        /* END: Use Case */
1092
    }
1093
1094
    /**
1095
     * Test for the updateLocation() method.
1096
     *
1097
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1098
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1099
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1100
     */
1101
    public function testUpdateLocationThrowsInvalidArgumentException()
1102
    {
1103
        $repository = $this->getRepository();
1104
1105
        $locationId = $this->generateId('location', 5);
1106
        /* BEGIN: Use Case */
1107
        // $locationId and remoteId is the IDs of an existing, but not the same, location
1108
        $locationService = $repository->getLocationService();
1109
1110
        $originalLocation = $locationService->loadLocation($locationId);
1111
1112
        $updateStruct = $locationService->newLocationUpdateStruct();
1113
1114
        // Remote ID of an existing location with a different locationId
1115
        $updateStruct->remoteId = 'f3e90596361e31d496d4026eb624c983';
1116
1117
        // Throws exception, since remote ID is already taken
1118
        $locationService->updateLocation($originalLocation, $updateStruct);
1119
        /* END: Use Case */
1120
    }
1121
1122
    /**
1123
     * Test for the updateLocation() method.
1124
     * Ref EZP-23302: Update Location fails if no change is performed with the update.
1125
     *
1126
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1127
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1128
     */
1129
    public function testUpdateLocationTwice()
1130
    {
1131
        $repository = $this->getRepository();
1132
1133
        $locationId = $this->generateId('location', 5);
1134
        /* BEGIN: Use Case */
1135
        $locationService = $repository->getLocationService();
1136
        $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...
1137
1138
        $originalLocation = $locationService->loadLocation($locationId);
1139
1140
        $updateStruct = $locationService->newLocationUpdateStruct();
1141
        $updateStruct->priority = 42;
1142
1143
        $updatedLocation = $locationService->updateLocation($originalLocation, $updateStruct);
1144
1145
        // Repeated update with the same, unchanged struct
1146
        $secondUpdatedLocation = $locationService->updateLocation($updatedLocation, $updateStruct);
1147
        /* END: Use Case */
1148
1149
        $this->assertEquals($updatedLocation->priority, 42);
1150
        $this->assertEquals($secondUpdatedLocation->priority, 42);
1151
    }
1152
1153
    /**
1154
     * Test for the swapLocation() method.
1155
     *
1156
     * @see \eZ\Publish\API\Repository\LocationService::swapLocation()
1157
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1158
     */
1159
    public function testSwapLocation()
1160
    {
1161
        $repository = $this->getRepository();
1162
        $locationService = $repository->getLocationService();
1163
1164
        $mediaLocationId = $this->generateId('location', 43);
1165
        $demoDesignLocationId = $this->generateId('location', 56);
1166
1167
        $mediaContentInfo = $locationService->loadLocation($mediaLocationId)->getContentInfo();
1168
        $demoDesignContentInfo = $locationService->loadLocation($demoDesignLocationId)->getContentInfo();
1169
1170
        /* BEGIN: Use Case */
1171
        // $mediaLocationId is the ID of the "Media" page location in
1172
        // an eZ Publish demo installation
1173
1174
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1175
        // Publish demo installation
1176
1177
        // Load the location service
1178
        $locationService = $repository->getLocationService();
1179
1180
        $mediaLocation = $locationService->loadLocation($mediaLocationId);
1181
        $demoDesignLocation = $locationService->loadLocation($demoDesignLocationId);
1182
1183
        // Swaps the content referred to by the locations
1184
        $locationService->swapLocation($mediaLocation, $demoDesignLocation);
1185
        /* END: Use Case */
1186
1187
        // Reload Locations, IDs swapped
1188
        $demoDesignLocation = $locationService->loadLocation($mediaLocationId);
1189
        $mediaLocation = $locationService->loadLocation($demoDesignLocationId);
1190
1191
        // Assert Location's Content is updated
1192
        $this->assertEquals(
1193
            $mediaContentInfo->id,
1194
            $mediaLocation->getContentInfo()->id
1195
        );
1196
        $this->assertEquals(
1197
            $demoDesignContentInfo->id,
1198
            $demoDesignLocation->getContentInfo()->id
1199
        );
1200
1201
        // Assert URL aliases are updated
1202
        $this->assertEquals(
1203
            $mediaLocation->id,
1204
            $repository->getURLAliasService()->lookup('/Design/Media')->destination
1205
        );
1206
        $this->assertEquals(
1207
            $demoDesignLocation->id,
1208
            $repository->getURLAliasService()->lookup('/Plain-site')->destination
1209
        );
1210
    }
1211
1212
    /**
1213
     * Test for the hideLocation() method.
1214
     *
1215
     * @see \eZ\Publish\API\Repository\LocationService::hideLocation()
1216
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1217
     */
1218
    public function testHideLocation()
1219
    {
1220
        $repository = $this->getRepository();
1221
1222
        $locationId = $this->generateId('location', 5);
1223
        /* BEGIN: Use Case */
1224
        // $locationId is the ID of an existing location
1225
        $locationService = $repository->getLocationService();
1226
1227
        $visibleLocation = $locationService->loadLocation($locationId);
1228
1229
        $hiddenLocation = $locationService->hideLocation($visibleLocation);
1230
        /* END: Use Case */
1231
1232
        $this->assertInstanceOf(
1233
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1234
            $hiddenLocation
1235
        );
1236
1237
        $this->assertTrue(
1238
            $hiddenLocation->hidden,
1239
            sprintf(
1240
                'Location with ID "%s" not hidden.',
1241
                $hiddenLocation->id
1242
            )
1243
        );
1244
1245
        $this->refreshSearch($repository);
1246
1247
        foreach ($locationService->loadLocationChildren($hiddenLocation)->locations as $child) {
1248
            $this->assertSubtreeProperties(
1249
                array('invisible' => true),
1250
                $child
1251
            );
1252
        }
1253
    }
1254
1255
    /**
1256
     * Assert that $expectedValues are set in the subtree starting at $location.
1257
     *
1258
     * @param array $expectedValues
1259
     * @param Location $location
1260
     */
1261
    protected function assertSubtreeProperties(array $expectedValues, Location $location, $stopId = null)
1262
    {
1263
        $repository = $this->getRepository();
1264
        $locationService = $repository->getLocationService();
1265
1266
        if ($location->id === $stopId) {
1267
            return;
1268
        }
1269
1270
        foreach ($expectedValues as $propertyName => $propertyValue) {
1271
            $this->assertEquals(
1272
                $propertyValue,
1273
                $location->$propertyName
1274
            );
1275
1276
            foreach ($locationService->loadLocationChildren($location)->locations as $child) {
1277
                $this->assertSubtreeProperties($expectedValues, $child);
1278
            }
1279
        }
1280
    }
1281
1282
    /**
1283
     * Test for the unhideLocation() method.
1284
     *
1285
     * @see \eZ\Publish\API\Repository\LocationService::unhideLocation()
1286
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testHideLocation
1287
     */
1288
    public function testUnhideLocation()
1289
    {
1290
        $repository = $this->getRepository();
1291
1292
        $locationId = $this->generateId('location', 5);
1293
        /* BEGIN: Use Case */
1294
        // $locationId is the ID of an existing location
1295
        $locationService = $repository->getLocationService();
1296
1297
        $visibleLocation = $locationService->loadLocation($locationId);
1298
        $hiddenLocation = $locationService->hideLocation($visibleLocation);
1299
1300
        $unHiddenLocation = $locationService->unhideLocation($hiddenLocation);
1301
        /* END: Use Case */
1302
1303
        $this->assertInstanceOf(
1304
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1305
            $unHiddenLocation
1306
        );
1307
1308
        $this->assertFalse(
1309
            $unHiddenLocation->hidden,
1310
            sprintf(
1311
                'Location with ID "%s" not unhidden.',
1312
                $unHiddenLocation->id
1313
            )
1314
        );
1315
1316
        $this->refreshSearch($repository);
1317
1318
        foreach ($locationService->loadLocationChildren($unHiddenLocation)->locations as $child) {
1319
            $this->assertSubtreeProperties(
1320
                array('invisible' => false),
1321
                $child
1322
            );
1323
        }
1324
    }
1325
1326
    /**
1327
     * Test for the unhideLocation() method.
1328
     *
1329
     * @see \eZ\Publish\API\Repository\LocationService::unhideLocation()
1330
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testUnhideLocation
1331
     */
1332
    public function testUnhideLocationNotUnhidesHiddenSubtree()
1333
    {
1334
        $repository = $this->getRepository();
1335
1336
        $higherLocationId = $this->generateId('location', 5);
1337
        $lowerLocationId = $this->generateId('location', 13);
1338
        /* BEGIN: Use Case */
1339
        // $higherLocationId is the ID of a location
1340
        // $lowerLocationId is the ID of a location below $higherLocationId
1341
        $locationService = $repository->getLocationService();
1342
1343
        $higherLocation = $locationService->loadLocation($higherLocationId);
1344
        $hiddenHigherLocation = $locationService->hideLocation($higherLocation);
1345
1346
        $lowerLocation = $locationService->loadLocation($lowerLocationId);
1347
        $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...
1348
1349
        $unHiddenHigherLocation = $locationService->unhideLocation($hiddenHigherLocation);
1350
        /* END: Use Case */
1351
1352
        $this->assertInstanceOf(
1353
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1354
            $unHiddenHigherLocation
1355
        );
1356
1357
        $this->assertFalse(
1358
            $unHiddenHigherLocation->hidden,
1359
            sprintf(
1360
                'Location with ID "%s" not unhidden.',
1361
                $unHiddenHigherLocation->id
1362
            )
1363
        );
1364
1365
        $this->refreshSearch($repository);
1366
1367
        foreach ($locationService->loadLocationChildren($unHiddenHigherLocation)->locations as $child) {
1368
            $this->assertSubtreeProperties(
1369
                array('invisible' => false),
1370
                $child,
1371
                $this->generateId('location', 13)
1372
            );
1373
        }
1374
1375
        $stillHiddenLocation = $locationService->loadLocation($this->generateId('location', 13));
1376
        $this->assertTrue(
1377
            $stillHiddenLocation->hidden,
1378
            sprintf(
1379
                'Hidden sub-location with ID %s accidentally unhidden.',
1380
                $stillHiddenLocation->id
1381
            )
1382
        );
1383
        foreach ($locationService->loadLocationChildren($stillHiddenLocation)->locations as $child) {
1384
            $this->assertSubtreeProperties(
1385
                array('invisible' => true),
1386
                $child
1387
            );
1388
        }
1389
    }
1390
1391
    /**
1392
     * Test for the deleteLocation() method.
1393
     *
1394
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1395
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1396
     */
1397
    public function testDeleteLocation()
1398
    {
1399
        $repository = $this->getRepository();
1400
1401
        $mediaLocationId = $this->generateId('location', 43);
1402
        /* BEGIN: Use Case */
1403
        // $mediaLocationId is the ID of the location of the
1404
        // "Media" location in an eZ Publish demo installation
1405
        $locationService = $repository->getLocationService();
1406
1407
        $location = $locationService->loadLocation($mediaLocationId);
1408
1409
        $locationService->deleteLocation($location);
1410
        /* END: Use Case */
1411
1412
        try {
1413
            $locationService->loadLocation($mediaLocationId);
1414
            $this->fail("Location $mediaLocationId not deleted.");
1415
        } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1416
        }
1417
1418
        // The following IDs are IDs of child locations of $mediaLocationId location
1419
        // ( Media/Images, Media/Files, Media/Multimedia respectively )
1420
        foreach (array(51, 52, 53) as $childLocationId) {
1421
            try {
1422
                $locationService->loadLocation($this->generateId('location', $childLocationId));
1423
                $this->fail("Location $childLocationId not deleted.");
1424
            } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1425
            }
1426
        }
1427
1428
        // The following IDs are IDs of content below $mediaLocationId location
1429
        // ( Media/Images, Media/Files, Media/Multimedia respectively )
1430
        $contentService = $this->getRepository()->getContentService();
1431
        foreach (array(49, 50, 51) as $childContentId) {
1432
            try {
1433
                $contentService->loadContentInfo($this->generateId('object', $childContentId));
1434
                $this->fail("Content $childContentId not deleted.");
1435
            } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1436
            }
1437
        }
1438
    }
1439
1440
    /**
1441
     * Test for the deleteLocation() method.
1442
     *
1443
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1444
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testDeleteLocation
1445
     */
1446
    public function testDeleteLocationDecrementsChildCountOnParent()
1447
    {
1448
        $repository = $this->getRepository();
1449
1450
        $mediaLocationId = $this->generateId('location', 43);
1451
        /* BEGIN: Use Case */
1452
        // $mediaLocationId is the ID of the location of the
1453
        // "Media" location in an eZ Publish demo installation
1454
1455
        $locationService = $repository->getLocationService();
1456
1457
        // Load the current the user group location
1458
        $location = $locationService->loadLocation($mediaLocationId);
1459
1460
        // Load the parent location
1461
        $parentLocation = $locationService->loadLocation(
1462
            $location->parentLocationId
1463
        );
1464
1465
        // Get child count
1466
        $childCountBefore = $locationService->getLocationChildCount($parentLocation);
1467
1468
        // Delete the user group location
1469
        $locationService->deleteLocation($location);
1470
1471
        $this->refreshSearch($repository);
1472
1473
        // Reload parent location
1474
        $parentLocation = $locationService->loadLocation(
1475
            $location->parentLocationId
1476
        );
1477
1478
        // This will be $childCountBefore - 1
1479
        $childCountAfter = $locationService->getLocationChildCount($parentLocation);
1480
        /* END: Use Case */
1481
1482
        $this->assertEquals($childCountBefore - 1, $childCountAfter);
1483
    }
1484
1485
    /**
1486
     * Test for the deleteLocation() method.
1487
     *
1488
     * Related issue: EZP-21904
1489
     *
1490
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1491
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1492
     */
1493
    public function testDeleteContentObjectLastLocation()
1494
    {
1495
        $repository = $this->getRepository();
1496
1497
        /* BEGIN: Use case */
1498
        $contentService = $repository->getContentService();
1499
        $locationService = $repository->getLocationService();
1500
        $contentTypeService = $repository->getContentTypeService();
1501
        $urlAliasService = $repository->getURLAliasService();
1502
1503
        // prepare Content object
1504
        $createStruct = $contentService->newContentCreateStruct(
1505
            $contentTypeService->loadContentTypeByIdentifier('folder'),
1506
            'eng-GB'
1507
        );
1508
        $createStruct->setField('name', 'Test folder');
1509
1510
        // creata Content object
1511
        $content = $contentService->publishVersion(
1512
            $contentService->createContent(
1513
                $createStruct,
1514
                array($locationService->newLocationCreateStruct(2))
1515
            )->versionInfo
1516
        );
1517
1518
        // delete location
1519
        $locationService->deleteLocation(
1520
            $locationService->loadLocation(
1521
                $urlAliasService->lookup('/Test-folder')->destination
1522
            )
1523
        );
1524
1525
        // this should throw a not found exception
1526
        $contentService->loadContent($content->versionInfo->contentInfo->id);
1527
        /* END: Use case*/
1528
    }
1529
1530
    /**
1531
     * Test for the copySubtree() method.
1532
     *
1533
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1534
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1535
     */
1536
    public function testCopySubtree()
1537
    {
1538
        $repository = $this->getRepository();
1539
1540
        $mediaLocationId = $this->generateId('location', 43);
1541
        $demoDesignLocationId = $this->generateId('location', 56);
1542
        /* BEGIN: Use Case */
1543
        // $mediaLocationId is the ID of the "Media" page location in
1544
        // an eZ Publish demo installation
1545
1546
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1547
        // Publish demo installation
1548
1549
        // Load the location service
1550
        $locationService = $repository->getLocationService();
1551
1552
        // Load location to copy
1553
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1554
1555
        // Load new parent location
1556
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1557
1558
        // Copy location "Media" to "Demo Design"
1559
        $copiedLocation = $locationService->copySubtree(
1560
            $locationToCopy,
1561
            $newParentLocation
1562
        );
1563
        /* END: Use Case */
1564
1565
        $this->assertInstanceOf(
1566
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1567
            $copiedLocation
1568
        );
1569
1570
        $this->assertPropertiesCorrect(
1571
            array(
1572
                'depth' => $newParentLocation->depth + 1,
1573
                'parentLocationId' => $newParentLocation->id,
1574
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $copiedLocation->id) . '/',
1575
            ),
1576
            $copiedLocation
1577
        );
1578
1579
        $this->assertDefaultContentStates($copiedLocation->contentInfo);
1580
    }
1581
1582
    /**
1583
     * Test for the copySubtree() method.
1584
     *
1585
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1586
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1587
     */
1588
    public function testCopySubtreeWithAliases()
1589
    {
1590
        $repository = $this->getRepository();
1591
        $urlAliasService = $repository->getURLAliasService();
1592
1593
        // $mediaLocationId is the ID of the "Media" page location in
1594
        // an eZ Publish demo installation
1595
1596
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1597
        // Publish demo installation
1598
        $mediaLocationId = $this->generateId('location', 43);
1599
        $demoDesignLocationId = $this->generateId('location', 56);
1600
1601
        $locationService = $repository->getLocationService();
1602
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1603
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1604
1605
        $expectedSubItemAliases = [
1606
            '/Design/Plain-site/Media/Multimedia',
1607
            '/Design/Plain-site/Media/Images',
1608
            '/Design/Plain-site/Media/Files',
1609
        ];
1610
1611
        $this->assertAliasesBeforeCopy($urlAliasService, $expectedSubItemAliases);
1612
1613
        // Copy location "Media" to "Design"
1614
        $locationService->copySubtree(
1615
            $locationToCopy,
1616
            $newParentLocation
1617
        );
1618
1619
        $this->assertGeneratedAliases($urlAliasService, $expectedSubItemAliases);
1620
    }
1621
1622
    /**
1623
     * Asserts that given Content has default ContentStates.
1624
     *
1625
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
1626
     */
1627 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
1628
    {
1629
        $repository = $this->getRepository();
1630
        $objectStateService = $repository->getObjectStateService();
1631
1632
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
1633
1634
        foreach ($objectStateGroups as $objectStateGroup) {
1635
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
1636
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
1637
                // Only check the first object state which is the default one.
1638
                $this->assertEquals(
1639
                    $objectState,
1640
                    $contentState
1641
                );
1642
                break;
1643
            }
1644
        }
1645
    }
1646
1647
    /**
1648
     * Test for the copySubtree() method.
1649
     *
1650
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1651
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1652
     */
1653
    public function testCopySubtreeUpdatesSubtreeProperties()
1654
    {
1655
        $repository = $this->getRepository();
1656
        $locationService = $repository->getLocationService();
1657
1658
        $locationToCopy = $locationService->loadLocation($this->generateId('location', 43));
1659
1660
        // Load Subtree properties before copy
1661
        $expected = $this->loadSubtreeProperties($locationToCopy);
1662
1663
        $mediaLocationId = $this->generateId('location', 43);
1664
        $demoDesignLocationId = $this->generateId('location', 56);
1665
        /* BEGIN: Use Case */
1666
        // $mediaLocationId is the ID of the "Media" page location in
1667
        // an eZ Publish demo installation
1668
1669
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1670
        // Publish demo installation
1671
1672
        // Load the location service
1673
        $locationService = $repository->getLocationService();
1674
1675
        // Load location to copy
1676
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1677
1678
        // Load new parent location
1679
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1680
1681
        // Copy location "Media" to "Demo Design"
1682
        $copiedLocation = $locationService->copySubtree(
1683
            $locationToCopy,
1684
            $newParentLocation
1685
        );
1686
        /* END: Use Case */
1687
1688
        $beforeIds = array();
1689
        foreach ($expected as $properties) {
1690
            $beforeIds[] = $properties['id'];
1691
        }
1692
1693
        $this->refreshSearch($repository);
1694
1695
        // Load Subtree properties after copy
1696
        $actual = $this->loadSubtreeProperties($copiedLocation);
1697
1698
        $this->assertEquals(count($expected), count($actual));
1699
1700
        foreach ($actual as $properties) {
1701
            $this->assertNotContains($properties['id'], $beforeIds);
1702
            $this->assertStringStartsWith(
1703
                "{$newParentLocation->pathString}" . $this->parseId('location', $copiedLocation->id) . '/',
1704
                $properties['pathString']
1705
            );
1706
            $this->assertStringEndsWith(
1707
                '/' . $this->parseId('location', $properties['id']) . '/',
1708
                $properties['pathString']
1709
            );
1710
        }
1711
    }
1712
1713
    /**
1714
     * Test for the copySubtree() method.
1715
     *
1716
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1717
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1718
     */
1719
    public function testCopySubtreeIncrementsChildCountOfNewParent()
1720
    {
1721
        $repository = $this->getRepository();
1722
        $locationService = $repository->getLocationService();
1723
1724
        $childCountBefore = $locationService->getLocationChildCount($locationService->loadLocation(56));
1725
1726
        $mediaLocationId = $this->generateId('location', 43);
1727
        $demoDesignLocationId = $this->generateId('location', 56);
1728
        /* BEGIN: Use Case */
1729
        // $mediaLocationId is the ID of the "Media" page location in
1730
        // an eZ Publish demo installation
1731
1732
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1733
        // Publish demo installation
1734
1735
        // Load the location service
1736
        $locationService = $repository->getLocationService();
1737
1738
        // Load location to copy
1739
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1740
1741
        // Load new parent location
1742
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1743
1744
        // Copy location "Media" to "Demo Design"
1745
        $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...
1746
            $locationToCopy,
1747
            $newParentLocation
1748
        );
1749
        /* END: Use Case */
1750
1751
        $this->refreshSearch($repository);
1752
1753
        $childCountAfter = $locationService->getLocationChildCount($locationService->loadLocation($demoDesignLocationId));
1754
1755
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
1756
    }
1757
1758
    /**
1759
     * Test for the copySubtree() method.
1760
     *
1761
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1762
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1763
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1764
     */
1765
    public function testCopySubtreeThrowsInvalidArgumentException()
1766
    {
1767
        $repository = $this->getRepository();
1768
1769
        $communityLocationId = $this->generateId('location', 5);
1770
        /* BEGIN: Use Case */
1771
        // $communityLocationId is the ID of the "Community" page location in
1772
        // an eZ Publish demo installation
1773
1774
        // Load the location service
1775
        $locationService = $repository->getLocationService();
1776
1777
        // Load location to copy
1778
        $locationToCopy = $locationService->loadLocation($communityLocationId);
1779
1780
        // Use a child as new parent
1781
        $childLocations = $locationService->loadLocationChildren($locationToCopy)->locations;
1782
        $newParentLocation = end($childLocations);
1783
1784
        // This call will fail with an "InvalidArgumentException", because the
1785
        // new parent is a child location of the subtree to copy.
1786
        $locationService->copySubtree(
1787
            $locationToCopy,
1788
            $newParentLocation
0 ignored issues
show
Security Bug introduced by
It seems like $newParentLocation defined by end($childLocations) on line 1782 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...
1789
        );
1790
        /* END: Use Case */
1791
    }
1792
1793
    /**
1794
     * Test for the moveSubtree() method.
1795
     *
1796
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1797
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1798
     */
1799
    public function testMoveSubtree()
1800
    {
1801
        $repository = $this->getRepository();
1802
1803
        $mediaLocationId = $this->generateId('location', 43);
1804
        $demoDesignLocationId = $this->generateId('location', 56);
1805
        /* BEGIN: Use Case */
1806
        // $mediaLocationId is the ID of the "Media" page location in
1807
        // an eZ Publish demo installation
1808
1809
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1810
        // Publish demo installation
1811
1812
        // Load the location service
1813
        $locationService = $repository->getLocationService();
1814
1815
        // Load location to move
1816
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1817
1818
        // Load new parent location
1819
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1820
1821
        // Move location from "Home" to "Demo Design"
1822
        $locationService->moveSubtree(
1823
            $locationToMove,
1824
            $newParentLocation
1825
        );
1826
1827
        // Load moved location
1828
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1829
        /* END: Use Case */
1830
1831
        $this->assertPropertiesCorrect(
1832
            array(
1833
                'hidden' => false,
1834
                'invisible' => false,
1835
                'depth' => $newParentLocation->depth + 1,
1836
                'parentLocationId' => $newParentLocation->id,
1837
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
1838
            ),
1839
            $movedLocation
1840
        );
1841
    }
1842
1843
    /**
1844
     * Test for the moveSubtree() method.
1845
     *
1846
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1847
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
1848
     */
1849
    public function testMoveSubtreeHidden()
1850
    {
1851
        $repository = $this->getRepository();
1852
1853
        $mediaLocationId = $this->generateId('location', 43);
1854
        $demoDesignLocationId = $this->generateId('location', 56);
1855
        /* BEGIN: Use Case */
1856
        // $mediaLocationId is the ID of the "Media" page location in
1857
        // an eZ Publish demo installation
1858
1859
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1860
        // Publish demo installation
1861
1862
        // Load the location service
1863
        $locationService = $repository->getLocationService();
1864
1865
        // Load location to move
1866
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1867
1868
        // Load new parent location
1869
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1870
1871
        // Hide the target location before we move
1872
        $newParentLocation = $locationService->hideLocation($newParentLocation);
1873
1874
        // Move location from "Home" to "Demo Design"
1875
        $locationService->moveSubtree(
1876
            $locationToMove,
1877
            $newParentLocation
1878
        );
1879
1880
        // Load moved location
1881
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1882
        /* END: Use Case */
1883
1884
        $this->assertPropertiesCorrect(
1885
            array(
1886
                'hidden' => false,
1887
                'invisible' => true,
1888
                'depth' => $newParentLocation->depth + 1,
1889
                'parentLocationId' => $newParentLocation->id,
1890
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
1891
            ),
1892
            $movedLocation
1893
        );
1894
    }
1895
1896
    /**
1897
     * Test for the moveSubtree() method.
1898
     *
1899
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1900
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
1901
     */
1902
    public function testMoveSubtreeUpdatesSubtreeProperties()
1903
    {
1904
        $repository = $this->getRepository();
1905
        $locationService = $repository->getLocationService();
1906
1907
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
1908
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
1909
1910
        // Load Subtree properties before move
1911
        $expected = $this->loadSubtreeProperties($locationToMove);
1912
        foreach ($expected as $id => $properties) {
1913
            $expected[$id]['depth'] = $properties['depth'] + 2;
1914
            $expected[$id]['pathString'] = str_replace(
1915
                $locationToMove->pathString,
1916
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
1917
                $properties['pathString']
1918
            );
1919
        }
1920
1921
        $mediaLocationId = $this->generateId('location', 43);
1922
        $demoDesignLocationId = $this->generateId('location', 56);
1923
        /* BEGIN: Use Case */
1924
        // $mediaLocationId is the ID of the "Media" page location in
1925
        // an eZ Publish demo installation
1926
1927
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1928
        // Publish demo installation
1929
1930
        // Load the location service
1931
        $locationService = $repository->getLocationService();
1932
1933
        // Load location to move
1934
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1935
1936
        // Load new parent location
1937
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1938
1939
        // Move location from "Home" to "Demo Design"
1940
        $locationService->moveSubtree(
1941
            $locationToMove,
1942
            $newParentLocation
1943
        );
1944
1945
        // Load moved location
1946
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1947
        /* END: Use Case */
1948
1949
        $this->refreshSearch($repository);
1950
1951
        // Load Subtree properties after move
1952
        $actual = $this->loadSubtreeProperties($movedLocation);
1953
1954
        $this->assertEquals($expected, $actual);
1955
    }
1956
1957
    /**
1958
     * Test for the moveSubtree() method.
1959
     *
1960
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1961
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtreeUpdatesSubtreeProperties
1962
     */
1963
    public function testMoveSubtreeUpdatesSubtreePropertiesHidden()
1964
    {
1965
        $repository = $this->getRepository();
1966
        $locationService = $repository->getLocationService();
1967
1968
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
1969
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
1970
1971
        // Hide the target location before we move
1972
        $newParentLocation = $locationService->hideLocation($newParentLocation);
1973
1974
        // Load Subtree properties before move
1975
        $expected = $this->loadSubtreeProperties($locationToMove);
1976
        foreach ($expected as $id => $properties) {
1977
            $expected[$id]['invisible'] = true;
1978
            $expected[$id]['depth'] = $properties['depth'] + 2;
1979
            $expected[$id]['pathString'] = str_replace(
1980
                $locationToMove->pathString,
1981
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
1982
                $properties['pathString']
1983
            );
1984
        }
1985
1986
        $mediaLocationId = $this->generateId('location', 43);
1987
        $demoDesignLocationId = $this->generateId('location', 56);
1988
        /* BEGIN: Use Case */
1989
        // $mediaLocationId is the ID of the "Media" page location in
1990
        // an eZ Publish demo installation
1991
1992
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1993
        // Publish demo installation
1994
1995
        // Load the location service
1996
        $locationService = $repository->getLocationService();
1997
1998
        // Load location to move
1999
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2000
2001
        // Load new parent location
2002
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2003
2004
        // Move location from "Home" to "Demo Design"
2005
        $locationService->moveSubtree(
2006
            $locationToMove,
2007
            $newParentLocation
2008
        );
2009
2010
        // Load moved location
2011
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2012
        /* END: Use Case */
2013
2014
        $this->refreshSearch($repository);
2015
2016
        // Load Subtree properties after move
2017
        $actual = $this->loadSubtreeProperties($movedLocation);
2018
2019
        $this->assertEquals($expected, $actual);
2020
    }
2021
2022
    /**
2023
     * Test for the moveSubtree() method.
2024
     *
2025
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2026
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2027
     */
2028 View Code Duplication
    public function testMoveSubtreeIncrementsChildCountOfNewParent()
2029
    {
2030
        $repository = $this->getRepository();
2031
        $locationService = $repository->getLocationService();
2032
2033
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
2034
2035
        // Load expected properties before move
2036
        $expected = $this->loadLocationProperties($newParentLocation);
2037
        $childCountBefore = $locationService->getLocationChildCount($newParentLocation);
2038
2039
        $mediaLocationId = $this->generateId('location', 43);
2040
        $demoDesignLocationId = $this->generateId('location', 56);
2041
        /* BEGIN: Use Case */
2042
        // $mediaLocationId is the ID of the "Media" page location in
2043
        // an eZ Publish demo installation
2044
2045
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2046
        // Publish demo installation
2047
2048
        // Load the location service
2049
        $locationService = $repository->getLocationService();
2050
2051
        // Load location to move
2052
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2053
2054
        // Load new parent location
2055
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2056
2057
        // Move location from "Home" to "Demo Design"
2058
        $locationService->moveSubtree(
2059
            $locationToMove,
2060
            $newParentLocation
2061
        );
2062
2063
        // Load moved location
2064
        $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...
2065
2066
        // Reload new parent location
2067
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2068
        /* END: Use Case */
2069
2070
        $this->refreshSearch($repository);
2071
2072
        // Load Subtree properties after move
2073
        $actual = $this->loadLocationProperties($newParentLocation);
2074
        $childCountAfter = $locationService->getLocationChildCount($newParentLocation);
2075
2076
        $this->assertEquals($expected, $actual);
2077
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
2078
    }
2079
2080
    /**
2081
     * Test for the moveSubtree() method.
2082
     *
2083
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2084
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2085
     */
2086 View Code Duplication
    public function testMoveSubtreeDecrementsChildCountOfOldParent()
2087
    {
2088
        $repository = $this->getRepository();
2089
        $locationService = $repository->getLocationService();
2090
2091
        $oldParentLocation = $locationService->loadLocation($this->generateId('location', 1));
2092
2093
        // Load expected properties before move
2094
        $expected = $this->loadLocationProperties($oldParentLocation);
2095
        $childCountBefore = $locationService->getLocationChildCount($oldParentLocation);
2096
2097
        $mediaLocationId = $this->generateId('location', 43);
2098
        $demoDesignLocationId = $this->generateId('location', 56);
2099
        /* BEGIN: Use Case */
2100
        // $mediaLocationId is the ID of the "Media" page location in
2101
        // an eZ Publish demo installation
2102
2103
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2104
        // Publish demo installation
2105
2106
        // Load the location service
2107
        $locationService = $repository->getLocationService();
2108
2109
        // Load location to move
2110
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2111
2112
        // Get the location id of the old parent
2113
        $oldParentLocationId = $locationToMove->parentLocationId;
2114
2115
        // Load new parent location
2116
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2117
2118
        // Move location from "Home" to "Demo Design"
2119
        $locationService->moveSubtree(
2120
            $locationToMove,
2121
            $newParentLocation
2122
        );
2123
2124
        // Reload old parent location
2125
        $oldParentLocation = $locationService->loadLocation($oldParentLocationId);
2126
        /* END: Use Case */
2127
2128
        $this->refreshSearch($repository);
2129
2130
        // Load Subtree properties after move
2131
        $actual = $this->loadLocationProperties($oldParentLocation);
2132
        $childCountAfter = $locationService->getLocationChildCount($oldParentLocation);
2133
2134
        $this->assertEquals($expected, $actual);
2135
        $this->assertEquals($childCountBefore - 1, $childCountAfter);
2136
    }
2137
2138
    /**
2139
     * Test for the moveSubtree() method.
2140
     *
2141
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2142
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2143
     */
2144
    public function testMoveSubtreeThrowsInvalidArgumentException()
2145
    {
2146
        $repository = $this->getRepository();
2147
        $mediaLocationId = $this->generateId('location', 43);
2148
        $multimediaLocationId = $this->generateId('location', 53);
2149
2150
        /* BEGIN: Use Case */
2151
        // $mediaLocationId is the ID of the "Media" page location in
2152
        // an eZ Publish demo installation
2153
2154
        // $multimediaLocationId is the ID of the "Multimedia" 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($multimediaLocationId);
2165
2166
        // Throws an exception because new parent location is placed below location to move
2167
        $locationService->moveSubtree(
2168
            $locationToMove,
2169
            $newParentLocation
2170
        );
2171
        /* END: Use Case */
2172
    }
2173
2174
    /**
2175
     * Loads properties from all locations in the $location's subtree.
2176
     *
2177
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2178
     * @param array $properties
2179
     *
2180
     * @return array
2181
     */
2182
    private function loadSubtreeProperties(Location $location, array $properties = array())
2183
    {
2184
        $locationService = $this->getRepository()->getLocationService();
2185
2186
        foreach ($locationService->loadLocationChildren($location)->locations as $childLocation) {
2187
            $properties[] = $this->loadLocationProperties($childLocation);
2188
2189
            $properties = $this->loadSubtreeProperties($childLocation, $properties);
2190
        }
2191
2192
        return $properties;
2193
    }
2194
2195
    /**
2196
     * Loads assertable properties from the given location.
2197
     *
2198
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2199
     * @param mixed[] $overwrite
2200
     *
2201
     * @return array
2202
     */
2203
    private function loadLocationProperties(Location $location, array $overwrite = array())
2204
    {
2205
        return array_merge(
2206
            array(
2207
                'id' => $location->id,
2208
                'depth' => $location->depth,
2209
                'parentLocationId' => $location->parentLocationId,
2210
                'pathString' => $location->pathString,
2211
                'remoteId' => $location->remoteId,
2212
                'hidden' => $location->hidden,
2213
                'invisible' => $location->invisible,
2214
                'priority' => $location->priority,
2215
                'sortField' => $location->sortField,
2216
                'sortOrder' => $location->sortOrder,
2217
            ),
2218
            $overwrite
2219
        );
2220
    }
2221
2222
    /**
2223
     * Assert generated aliases to expected alias return.
2224
     *
2225
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
2226
     * @param array $expectedAliases
2227
     */
2228
    protected function assertGeneratedAliases($urlAliasService, array $expectedAliases)
2229
    {
2230
        foreach ($expectedAliases as $expectedAlias) {
2231
            $urlAlias = $urlAliasService->lookup($expectedAlias);
2232
            $this->assertPropertiesCorrect(['type' => 0], $urlAlias);
2233
        }
2234
    }
2235
2236
    /**
2237
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
2238
     * @param array $expectedSubItemAliases
2239
     */
2240
    private function assertAliasesBeforeCopy($urlAliasService, array $expectedSubItemAliases)
2241
    {
2242
        foreach ($expectedSubItemAliases as $aliasUrl) {
2243
            try {
2244
                $urlAliasService->lookup($aliasUrl);
2245
                $this->fail('We didn\'t expect to find alias, but it was found');
2246
            } catch (\Exception $e) {
2247
                $this->assertTrue(true); // OK - alias was not found
2248
            }
2249
        }
2250
    }
2251
}
2252