Completed
Push — 6.13 ( 675f4b...da0549 )
by André
133:47 queued 84:36
created

LocationServiceTest::testNewLocationCreateStruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1348
            },
1349
            $searchResult->searchHits
1350
        );
1351
        sort($contentIds);
1352
        sort($foundContentIds);
1353
        self::assertSame(
1354
            $contentIds,
1355
            $foundContentIds,
1356
            'Got different than expected Content item Ids'
1357
        );
1358
    }
1359
1360
    /**
1361
     * Test swapping two secondary (non-main) Locations.
1362
     *
1363
     * @covers \eZ\Publish\API\Repository\LocationService::swapLocation
1364
     *
1365
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
1366
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1367
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
1368
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1369
     */
1370
    public function testSwapLocationForSecondaryLocations()
1371
    {
1372
        $repository = $this->getRepository();
1373
        $locationService = $repository->getLocationService();
1374
        $contentService = $repository->getContentService();
1375
1376
        $folder1 = $this->createFolder(['eng-GB' => 'Folder1'], 2);
1377
        $folder2 = $this->createFolder(['eng-GB' => 'Folder2'], 2);
1378
        $parentFolder1 = $this->createFolder(['eng-GB' => 'Parent1'], 2);
1379
        $parentFolder2 = $this->createFolder(['eng-GB' => 'Parent2'], 2);
1380
1381
        $parentLocation1 = $locationService->loadLocation($parentFolder1->contentInfo->mainLocationId);
1382
        $parentLocation2 = $locationService->loadLocation($parentFolder2->contentInfo->mainLocationId);
1383
        $secondaryLocation1 = $locationService->createLocation(
1384
            $folder1->contentInfo,
1385
            $locationService->newLocationCreateStruct($parentLocation1->id)
1386
        );
1387
        $secondaryLocation2 = $locationService->createLocation(
1388
            $folder2->contentInfo,
1389
            $locationService->newLocationCreateStruct($parentLocation2->id)
1390
        );
1391
1392
        // begin use case
1393
        $locationService->swapLocation($secondaryLocation1, $secondaryLocation2);
1394
1395
        // test results
1396
        $secondaryLocation1 = $locationService->loadLocation($secondaryLocation1->id);
1397
        $secondaryLocation2 = $locationService->loadLocation($secondaryLocation2->id);
1398
1399
        self::assertEquals($folder2->id, $secondaryLocation1->contentInfo->id);
1400
        self::assertEquals($folder1->id, $secondaryLocation2->contentInfo->id);
1401
1402
        self::assertEquals(
1403
            $folder1,
1404
            $contentService->loadContent($folder1->id)
1405
        );
1406
1407
        self::assertEquals(
1408
            $folder2,
1409
            $contentService->loadContent($folder2->id)
1410
        );
1411
    }
1412
1413
    /**
1414
     * Test for the hideLocation() method.
1415
     *
1416
     * @see \eZ\Publish\API\Repository\LocationService::hideLocation()
1417
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1418
     */
1419
    public function testHideLocation()
1420
    {
1421
        $repository = $this->getRepository();
1422
1423
        $locationId = $this->generateId('location', 5);
1424
        /* BEGIN: Use Case */
1425
        // $locationId is the ID of an existing location
1426
        $locationService = $repository->getLocationService();
1427
1428
        $visibleLocation = $locationService->loadLocation($locationId);
1429
1430
        $hiddenLocation = $locationService->hideLocation($visibleLocation);
1431
        /* END: Use Case */
1432
1433
        $this->assertInstanceOf(
1434
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1435
            $hiddenLocation
1436
        );
1437
1438
        $this->assertTrue(
1439
            $hiddenLocation->hidden,
1440
            sprintf(
1441
                'Location with ID "%s" not hidden.',
1442
                $hiddenLocation->id
1443
            )
1444
        );
1445
1446
        $this->refreshSearch($repository);
1447
1448
        foreach ($locationService->loadLocationChildren($hiddenLocation)->locations as $child) {
1449
            $this->assertSubtreeProperties(
1450
                array('invisible' => true),
1451
                $child
1452
            );
1453
        }
1454
    }
1455
1456
    /**
1457
     * Assert that $expectedValues are set in the subtree starting at $location.
1458
     *
1459
     * @param array $expectedValues
1460
     * @param Location $location
1461
     */
1462
    protected function assertSubtreeProperties(array $expectedValues, Location $location, $stopId = null)
1463
    {
1464
        $repository = $this->getRepository();
1465
        $locationService = $repository->getLocationService();
1466
1467
        if ($location->id === $stopId) {
1468
            return;
1469
        }
1470
1471
        foreach ($expectedValues as $propertyName => $propertyValue) {
1472
            $this->assertEquals(
1473
                $propertyValue,
1474
                $location->$propertyName
1475
            );
1476
1477
            foreach ($locationService->loadLocationChildren($location)->locations as $child) {
1478
                $this->assertSubtreeProperties($expectedValues, $child);
1479
            }
1480
        }
1481
    }
1482
1483
    /**
1484
     * Test for the unhideLocation() method.
1485
     *
1486
     * @see \eZ\Publish\API\Repository\LocationService::unhideLocation()
1487
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testHideLocation
1488
     */
1489
    public function testUnhideLocation()
1490
    {
1491
        $repository = $this->getRepository();
1492
1493
        $locationId = $this->generateId('location', 5);
1494
        /* BEGIN: Use Case */
1495
        // $locationId is the ID of an existing location
1496
        $locationService = $repository->getLocationService();
1497
1498
        $visibleLocation = $locationService->loadLocation($locationId);
1499
        $hiddenLocation = $locationService->hideLocation($visibleLocation);
1500
1501
        $unHiddenLocation = $locationService->unhideLocation($hiddenLocation);
1502
        /* END: Use Case */
1503
1504
        $this->assertInstanceOf(
1505
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1506
            $unHiddenLocation
1507
        );
1508
1509
        $this->assertFalse(
1510
            $unHiddenLocation->hidden,
1511
            sprintf(
1512
                'Location with ID "%s" not unhidden.',
1513
                $unHiddenLocation->id
1514
            )
1515
        );
1516
1517
        $this->refreshSearch($repository);
1518
1519
        foreach ($locationService->loadLocationChildren($unHiddenLocation)->locations as $child) {
1520
            $this->assertSubtreeProperties(
1521
                array('invisible' => false),
1522
                $child
1523
            );
1524
        }
1525
    }
1526
1527
    /**
1528
     * Test for the unhideLocation() method.
1529
     *
1530
     * @see \eZ\Publish\API\Repository\LocationService::unhideLocation()
1531
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testUnhideLocation
1532
     */
1533
    public function testUnhideLocationNotUnhidesHiddenSubtree()
1534
    {
1535
        $repository = $this->getRepository();
1536
1537
        $higherLocationId = $this->generateId('location', 5);
1538
        $lowerLocationId = $this->generateId('location', 13);
1539
        /* BEGIN: Use Case */
1540
        // $higherLocationId is the ID of a location
1541
        // $lowerLocationId is the ID of a location below $higherLocationId
1542
        $locationService = $repository->getLocationService();
1543
1544
        $higherLocation = $locationService->loadLocation($higherLocationId);
1545
        $hiddenHigherLocation = $locationService->hideLocation($higherLocation);
1546
1547
        $lowerLocation = $locationService->loadLocation($lowerLocationId);
1548
        $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...
1549
1550
        $unHiddenHigherLocation = $locationService->unhideLocation($hiddenHigherLocation);
1551
        /* END: Use Case */
1552
1553
        $this->assertInstanceOf(
1554
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1555
            $unHiddenHigherLocation
1556
        );
1557
1558
        $this->assertFalse(
1559
            $unHiddenHigherLocation->hidden,
1560
            sprintf(
1561
                'Location with ID "%s" not unhidden.',
1562
                $unHiddenHigherLocation->id
1563
            )
1564
        );
1565
1566
        $this->refreshSearch($repository);
1567
1568
        foreach ($locationService->loadLocationChildren($unHiddenHigherLocation)->locations as $child) {
1569
            $this->assertSubtreeProperties(
1570
                array('invisible' => false),
1571
                $child,
1572
                $this->generateId('location', 13)
1573
            );
1574
        }
1575
1576
        $stillHiddenLocation = $locationService->loadLocation($this->generateId('location', 13));
1577
        $this->assertTrue(
1578
            $stillHiddenLocation->hidden,
1579
            sprintf(
1580
                'Hidden sub-location with ID %s accidentally unhidden.',
1581
                $stillHiddenLocation->id
1582
            )
1583
        );
1584
        foreach ($locationService->loadLocationChildren($stillHiddenLocation)->locations as $child) {
1585
            $this->assertSubtreeProperties(
1586
                array('invisible' => true),
1587
                $child
1588
            );
1589
        }
1590
    }
1591
1592
    /**
1593
     * Test for the deleteLocation() method.
1594
     *
1595
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1596
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1597
     */
1598
    public function testDeleteLocation()
1599
    {
1600
        $repository = $this->getRepository();
1601
1602
        $mediaLocationId = $this->generateId('location', 43);
1603
        /* BEGIN: Use Case */
1604
        // $mediaLocationId is the ID of the location of the
1605
        // "Media" location in an eZ Publish demo installation
1606
        $locationService = $repository->getLocationService();
1607
1608
        $location = $locationService->loadLocation($mediaLocationId);
1609
1610
        $locationService->deleteLocation($location);
1611
        /* END: Use Case */
1612
1613
        try {
1614
            $locationService->loadLocation($mediaLocationId);
1615
            $this->fail("Location $mediaLocationId not deleted.");
1616
        } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1617
        }
1618
1619
        // The following IDs are IDs of child locations of $mediaLocationId location
1620
        // ( Media/Images, Media/Files, Media/Multimedia respectively )
1621
        foreach (array(51, 52, 53) as $childLocationId) {
1622
            try {
1623
                $locationService->loadLocation($this->generateId('location', $childLocationId));
1624
                $this->fail("Location $childLocationId not deleted.");
1625
            } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1626
            }
1627
        }
1628
1629
        // The following IDs are IDs of content below $mediaLocationId location
1630
        // ( Media/Images, Media/Files, Media/Multimedia respectively )
1631
        $contentService = $this->getRepository()->getContentService();
1632
        foreach (array(49, 50, 51) as $childContentId) {
1633
            try {
1634
                $contentService->loadContentInfo($this->generateId('object', $childContentId));
1635
                $this->fail("Content $childContentId not deleted.");
1636
            } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1637
            }
1638
        }
1639
    }
1640
1641
    /**
1642
     * Test for the deleteLocation() method.
1643
     *
1644
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1645
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testDeleteLocation
1646
     */
1647
    public function testDeleteLocationDecrementsChildCountOnParent()
1648
    {
1649
        $repository = $this->getRepository();
1650
1651
        $mediaLocationId = $this->generateId('location', 43);
1652
        /* BEGIN: Use Case */
1653
        // $mediaLocationId is the ID of the location of the
1654
        // "Media" location in an eZ Publish demo installation
1655
1656
        $locationService = $repository->getLocationService();
1657
1658
        // Load the current the user group location
1659
        $location = $locationService->loadLocation($mediaLocationId);
1660
1661
        // Load the parent location
1662
        $parentLocation = $locationService->loadLocation(
1663
            $location->parentLocationId
1664
        );
1665
1666
        // Get child count
1667
        $childCountBefore = $locationService->getLocationChildCount($parentLocation);
1668
1669
        // Delete the user group location
1670
        $locationService->deleteLocation($location);
1671
1672
        $this->refreshSearch($repository);
1673
1674
        // Reload parent location
1675
        $parentLocation = $locationService->loadLocation(
1676
            $location->parentLocationId
1677
        );
1678
1679
        // This will be $childCountBefore - 1
1680
        $childCountAfter = $locationService->getLocationChildCount($parentLocation);
1681
        /* END: Use Case */
1682
1683
        $this->assertEquals($childCountBefore - 1, $childCountAfter);
1684
    }
1685
1686
    /**
1687
     * Test for the deleteLocation() method.
1688
     *
1689
     * Related issue: EZP-21904
1690
     *
1691
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1692
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1693
     */
1694
    public function testDeleteContentObjectLastLocation()
1695
    {
1696
        $repository = $this->getRepository();
1697
1698
        /* BEGIN: Use case */
1699
        $contentService = $repository->getContentService();
1700
        $locationService = $repository->getLocationService();
1701
        $contentTypeService = $repository->getContentTypeService();
1702
        $urlAliasService = $repository->getURLAliasService();
1703
1704
        // prepare Content object
1705
        $createStruct = $contentService->newContentCreateStruct(
1706
            $contentTypeService->loadContentTypeByIdentifier('folder'),
1707
            'eng-GB'
1708
        );
1709
        $createStruct->setField('name', 'Test folder');
1710
1711
        // creata Content object
1712
        $content = $contentService->publishVersion(
1713
            $contentService->createContent(
1714
                $createStruct,
1715
                array($locationService->newLocationCreateStruct(2))
1716
            )->versionInfo
1717
        );
1718
1719
        // delete location
1720
        $locationService->deleteLocation(
1721
            $locationService->loadLocation(
1722
                $urlAliasService->lookup('/Test-folder')->destination
1723
            )
1724
        );
1725
1726
        // this should throw a not found exception
1727
        $contentService->loadContent($content->versionInfo->contentInfo->id);
1728
        /* END: Use case*/
1729
    }
1730
1731
    /**
1732
     * Test for the copySubtree() method.
1733
     *
1734
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1735
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1736
     */
1737
    public function testCopySubtree()
1738
    {
1739
        $repository = $this->getRepository();
1740
1741
        $mediaLocationId = $this->generateId('location', 43);
1742
        $demoDesignLocationId = $this->generateId('location', 56);
1743
        /* BEGIN: Use Case */
1744
        // $mediaLocationId is the ID of the "Media" page location in
1745
        // an eZ Publish demo installation
1746
1747
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1748
        // Publish demo installation
1749
1750
        // Load the location service
1751
        $locationService = $repository->getLocationService();
1752
1753
        // Load location to copy
1754
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1755
1756
        // Load new parent location
1757
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1758
1759
        // Copy location "Media" to "Demo Design"
1760
        $copiedLocation = $locationService->copySubtree(
1761
            $locationToCopy,
1762
            $newParentLocation
1763
        );
1764
        /* END: Use Case */
1765
1766
        $this->assertInstanceOf(
1767
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1768
            $copiedLocation
1769
        );
1770
1771
        $this->assertPropertiesCorrect(
1772
            array(
1773
                'depth' => $newParentLocation->depth + 1,
1774
                'parentLocationId' => $newParentLocation->id,
1775
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $copiedLocation->id) . '/',
1776
            ),
1777
            $copiedLocation
1778
        );
1779
1780
        $this->assertDefaultContentStates($copiedLocation->contentInfo);
1781
    }
1782
1783
    /**
1784
     * Test for the copySubtree() method.
1785
     *
1786
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1787
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1788
     */
1789
    public function testCopySubtreeWithAliases()
1790
    {
1791
        $repository = $this->getRepository();
1792
        $urlAliasService = $repository->getURLAliasService();
1793
1794
        // $mediaLocationId is the ID of the "Media" page location in
1795
        // an eZ Publish demo installation
1796
1797
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1798
        // Publish demo installation
1799
        $mediaLocationId = $this->generateId('location', 43);
1800
        $demoDesignLocationId = $this->generateId('location', 56);
1801
1802
        $locationService = $repository->getLocationService();
1803
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1804
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1805
1806
        $expectedSubItemAliases = [
1807
            '/Design/Plain-site/Media/Multimedia',
1808
            '/Design/Plain-site/Media/Images',
1809
            '/Design/Plain-site/Media/Files',
1810
        ];
1811
1812
        $this->assertAliasesBeforeCopy($urlAliasService, $expectedSubItemAliases);
1813
1814
        // Copy location "Media" to "Design"
1815
        $locationService->copySubtree(
1816
            $locationToCopy,
1817
            $newParentLocation
1818
        );
1819
1820
        $this->assertGeneratedAliases($urlAliasService, $expectedSubItemAliases);
1821
    }
1822
1823
    /**
1824
     * Asserts that given Content has default ContentStates.
1825
     *
1826
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
1827
     */
1828 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
1829
    {
1830
        $repository = $this->getRepository();
1831
        $objectStateService = $repository->getObjectStateService();
1832
1833
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
1834
1835
        foreach ($objectStateGroups as $objectStateGroup) {
1836
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
1837
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
1838
                // Only check the first object state which is the default one.
1839
                $this->assertEquals(
1840
                    $objectState,
1841
                    $contentState
1842
                );
1843
                break;
1844
            }
1845
        }
1846
    }
1847
1848
    /**
1849
     * Test for the copySubtree() method.
1850
     *
1851
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1852
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1853
     */
1854
    public function testCopySubtreeUpdatesSubtreeProperties()
1855
    {
1856
        $repository = $this->getRepository();
1857
        $locationService = $repository->getLocationService();
1858
1859
        $locationToCopy = $locationService->loadLocation($this->generateId('location', 43));
1860
1861
        // Load Subtree properties before copy
1862
        $expected = $this->loadSubtreeProperties($locationToCopy);
1863
1864
        $mediaLocationId = $this->generateId('location', 43);
1865
        $demoDesignLocationId = $this->generateId('location', 56);
1866
        /* BEGIN: Use Case */
1867
        // $mediaLocationId is the ID of the "Media" page location in
1868
        // an eZ Publish demo installation
1869
1870
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1871
        // Publish demo installation
1872
1873
        // Load the location service
1874
        $locationService = $repository->getLocationService();
1875
1876
        // Load location to copy
1877
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1878
1879
        // Load new parent location
1880
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1881
1882
        // Copy location "Media" to "Demo Design"
1883
        $copiedLocation = $locationService->copySubtree(
1884
            $locationToCopy,
1885
            $newParentLocation
1886
        );
1887
        /* END: Use Case */
1888
1889
        $beforeIds = array();
1890
        foreach ($expected as $properties) {
1891
            $beforeIds[] = $properties['id'];
1892
        }
1893
1894
        $this->refreshSearch($repository);
1895
1896
        // Load Subtree properties after copy
1897
        $actual = $this->loadSubtreeProperties($copiedLocation);
1898
1899
        $this->assertEquals(count($expected), count($actual));
1900
1901
        foreach ($actual as $properties) {
1902
            $this->assertNotContains($properties['id'], $beforeIds);
1903
            $this->assertStringStartsWith(
1904
                "{$newParentLocation->pathString}" . $this->parseId('location', $copiedLocation->id) . '/',
1905
                $properties['pathString']
1906
            );
1907
            $this->assertStringEndsWith(
1908
                '/' . $this->parseId('location', $properties['id']) . '/',
1909
                $properties['pathString']
1910
            );
1911
        }
1912
    }
1913
1914
    /**
1915
     * Test for the copySubtree() method.
1916
     *
1917
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1918
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1919
     */
1920
    public function testCopySubtreeIncrementsChildCountOfNewParent()
1921
    {
1922
        $repository = $this->getRepository();
1923
        $locationService = $repository->getLocationService();
1924
1925
        $childCountBefore = $locationService->getLocationChildCount($locationService->loadLocation(56));
1926
1927
        $mediaLocationId = $this->generateId('location', 43);
1928
        $demoDesignLocationId = $this->generateId('location', 56);
1929
        /* BEGIN: Use Case */
1930
        // $mediaLocationId is the ID of the "Media" page location in
1931
        // an eZ Publish demo installation
1932
1933
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1934
        // Publish demo installation
1935
1936
        // Load the location service
1937
        $locationService = $repository->getLocationService();
1938
1939
        // Load location to copy
1940
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1941
1942
        // Load new parent location
1943
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1944
1945
        // Copy location "Media" to "Demo Design"
1946
        $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...
1947
            $locationToCopy,
1948
            $newParentLocation
1949
        );
1950
        /* END: Use Case */
1951
1952
        $this->refreshSearch($repository);
1953
1954
        $childCountAfter = $locationService->getLocationChildCount($locationService->loadLocation($demoDesignLocationId));
1955
1956
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
1957
    }
1958
1959
    /**
1960
     * Test for the copySubtree() method.
1961
     *
1962
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1963
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1964
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1965
     */
1966 View Code Duplication
    public function testCopySubtreeThrowsInvalidArgumentException()
1967
    {
1968
        $repository = $this->getRepository();
1969
1970
        $communityLocationId = $this->generateId('location', 5);
1971
        /* BEGIN: Use Case */
1972
        // $communityLocationId is the ID of the "Community" page location in
1973
        // an eZ Publish demo installation
1974
1975
        // Load the location service
1976
        $locationService = $repository->getLocationService();
1977
1978
        // Load location to copy
1979
        $locationToCopy = $locationService->loadLocation($communityLocationId);
1980
1981
        // Use a child as new parent
1982
        $childLocations = $locationService->loadLocationChildren($locationToCopy)->locations;
1983
        $newParentLocation = end($childLocations);
1984
1985
        // This call will fail with an "InvalidArgumentException", because the
1986
        // new parent is a child location of the subtree to copy.
1987
        $locationService->copySubtree(
1988
            $locationToCopy,
1989
            $newParentLocation
0 ignored issues
show
Security Bug introduced by
It seems like $newParentLocation defined by end($childLocations) on line 1983 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...
1990
        );
1991
        /* END: Use Case */
1992
    }
1993
1994
    /**
1995
     * Test for the moveSubtree() method.
1996
     *
1997
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1998
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1999
     */
2000
    public function testMoveSubtree()
2001
    {
2002
        $repository = $this->getRepository();
2003
2004
        $mediaLocationId = $this->generateId('location', 43);
2005
        $demoDesignLocationId = $this->generateId('location', 56);
2006
        /* BEGIN: Use Case */
2007
        // $mediaLocationId is the ID of the "Media" page location in
2008
        // an eZ Publish demo installation
2009
2010
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2011
        // Publish demo installation
2012
2013
        // Load the location service
2014
        $locationService = $repository->getLocationService();
2015
2016
        // Load location to move
2017
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2018
2019
        // Load new parent location
2020
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2021
2022
        // Move location from "Home" to "Demo Design"
2023
        $locationService->moveSubtree(
2024
            $locationToMove,
2025
            $newParentLocation
2026
        );
2027
2028
        // Load moved location
2029
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2030
        /* END: Use Case */
2031
2032
        $this->assertPropertiesCorrect(
2033
            array(
2034
                'hidden' => false,
2035
                'invisible' => false,
2036
                'depth' => $newParentLocation->depth + 1,
2037
                'parentLocationId' => $newParentLocation->id,
2038
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
2039
            ),
2040
            $movedLocation
2041
        );
2042
    }
2043
2044
    /**
2045
     * Test for the moveSubtree() method.
2046
     *
2047
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2048
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2049
     */
2050
    public function testMoveSubtreeHidden()
2051
    {
2052
        $repository = $this->getRepository();
2053
2054
        $mediaLocationId = $this->generateId('location', 43);
2055
        $demoDesignLocationId = $this->generateId('location', 56);
2056
        /* BEGIN: Use Case */
2057
        // $mediaLocationId is the ID of the "Media" page location in
2058
        // an eZ Publish demo installation
2059
2060
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2061
        // Publish demo installation
2062
2063
        // Load the location service
2064
        $locationService = $repository->getLocationService();
2065
2066
        // Load location to move
2067
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2068
2069
        // Load new parent location
2070
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2071
2072
        // Hide the target location before we move
2073
        $newParentLocation = $locationService->hideLocation($newParentLocation);
2074
2075
        // Move location from "Home" to "Demo Design"
2076
        $locationService->moveSubtree(
2077
            $locationToMove,
2078
            $newParentLocation
2079
        );
2080
2081
        // Load moved location
2082
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2083
        /* END: Use Case */
2084
2085
        $this->assertPropertiesCorrect(
2086
            array(
2087
                'hidden' => false,
2088
                'invisible' => true,
2089
                'depth' => $newParentLocation->depth + 1,
2090
                'parentLocationId' => $newParentLocation->id,
2091
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
2092
            ),
2093
            $movedLocation
2094
        );
2095
    }
2096
2097
    /**
2098
     * Test for the moveSubtree() method.
2099
     *
2100
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2101
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2102
     */
2103
    public function testMoveSubtreeUpdatesSubtreeProperties()
2104
    {
2105
        $repository = $this->getRepository();
2106
        $locationService = $repository->getLocationService();
2107
2108
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
2109
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
2110
2111
        // Load Subtree properties before move
2112
        $expected = $this->loadSubtreeProperties($locationToMove);
2113
        foreach ($expected as $id => $properties) {
2114
            $expected[$id]['depth'] = $properties['depth'] + 2;
2115
            $expected[$id]['pathString'] = str_replace(
2116
                $locationToMove->pathString,
2117
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
2118
                $properties['pathString']
2119
            );
2120
        }
2121
2122
        $mediaLocationId = $this->generateId('location', 43);
2123
        $demoDesignLocationId = $this->generateId('location', 56);
2124
        /* BEGIN: Use Case */
2125
        // $mediaLocationId is the ID of the "Media" page location in
2126
        // an eZ Publish demo installation
2127
2128
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2129
        // Publish demo installation
2130
2131
        // Load the location service
2132
        $locationService = $repository->getLocationService();
2133
2134
        // Load location to move
2135
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2136
2137
        // Load new parent location
2138
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2139
2140
        // Move location from "Home" to "Demo Design"
2141
        $locationService->moveSubtree(
2142
            $locationToMove,
2143
            $newParentLocation
2144
        );
2145
2146
        // Load moved location
2147
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2148
        /* END: Use Case */
2149
2150
        $this->refreshSearch($repository);
2151
2152
        // Load Subtree properties after move
2153
        $actual = $this->loadSubtreeProperties($movedLocation);
2154
2155
        $this->assertEquals($expected, $actual);
2156
    }
2157
2158
    /**
2159
     * Test for the moveSubtree() method.
2160
     *
2161
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2162
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtreeUpdatesSubtreeProperties
2163
     */
2164
    public function testMoveSubtreeUpdatesSubtreePropertiesHidden()
2165
    {
2166
        $repository = $this->getRepository();
2167
        $locationService = $repository->getLocationService();
2168
2169
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
2170
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
2171
2172
        // Hide the target location before we move
2173
        $newParentLocation = $locationService->hideLocation($newParentLocation);
2174
2175
        // Load Subtree properties before move
2176
        $expected = $this->loadSubtreeProperties($locationToMove);
2177
        foreach ($expected as $id => $properties) {
2178
            $expected[$id]['invisible'] = true;
2179
            $expected[$id]['depth'] = $properties['depth'] + 2;
2180
            $expected[$id]['pathString'] = str_replace(
2181
                $locationToMove->pathString,
2182
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
2183
                $properties['pathString']
2184
            );
2185
        }
2186
2187
        $mediaLocationId = $this->generateId('location', 43);
2188
        $demoDesignLocationId = $this->generateId('location', 56);
2189
        /* BEGIN: Use Case */
2190
        // $mediaLocationId is the ID of the "Media" page location in
2191
        // an eZ Publish demo installation
2192
2193
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2194
        // Publish demo installation
2195
2196
        // Load the location service
2197
        $locationService = $repository->getLocationService();
2198
2199
        // Load location to move
2200
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2201
2202
        // Load new parent location
2203
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2204
2205
        // Move location from "Home" to "Demo Design"
2206
        $locationService->moveSubtree(
2207
            $locationToMove,
2208
            $newParentLocation
2209
        );
2210
2211
        // Load moved location
2212
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2213
        /* END: Use Case */
2214
2215
        $this->refreshSearch($repository);
2216
2217
        // Load Subtree properties after move
2218
        $actual = $this->loadSubtreeProperties($movedLocation);
2219
2220
        $this->assertEquals($expected, $actual);
2221
    }
2222
2223
    /**
2224
     * Test for the moveSubtree() method.
2225
     *
2226
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2227
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2228
     */
2229 View Code Duplication
    public function testMoveSubtreeIncrementsChildCountOfNewParent()
2230
    {
2231
        $repository = $this->getRepository();
2232
        $locationService = $repository->getLocationService();
2233
2234
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
2235
2236
        // Load expected properties before move
2237
        $expected = $this->loadLocationProperties($newParentLocation);
2238
        $childCountBefore = $locationService->getLocationChildCount($newParentLocation);
2239
2240
        $mediaLocationId = $this->generateId('location', 43);
2241
        $demoDesignLocationId = $this->generateId('location', 56);
2242
        /* BEGIN: Use Case */
2243
        // $mediaLocationId is the ID of the "Media" page location in
2244
        // an eZ Publish demo installation
2245
2246
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2247
        // Publish demo installation
2248
2249
        // Load the location service
2250
        $locationService = $repository->getLocationService();
2251
2252
        // Load location to move
2253
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2254
2255
        // Load new parent location
2256
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2257
2258
        // Move location from "Home" to "Demo Design"
2259
        $locationService->moveSubtree(
2260
            $locationToMove,
2261
            $newParentLocation
2262
        );
2263
2264
        // Load moved location
2265
        $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...
2266
2267
        // Reload new parent location
2268
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2269
        /* END: Use Case */
2270
2271
        $this->refreshSearch($repository);
2272
2273
        // Load Subtree properties after move
2274
        $actual = $this->loadLocationProperties($newParentLocation);
2275
        $childCountAfter = $locationService->getLocationChildCount($newParentLocation);
2276
2277
        $this->assertEquals($expected, $actual);
2278
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
2279
    }
2280
2281
    /**
2282
     * Test for the moveSubtree() method.
2283
     *
2284
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2285
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2286
     */
2287 View Code Duplication
    public function testMoveSubtreeDecrementsChildCountOfOldParent()
2288
    {
2289
        $repository = $this->getRepository();
2290
        $locationService = $repository->getLocationService();
2291
2292
        $oldParentLocation = $locationService->loadLocation($this->generateId('location', 1));
2293
2294
        // Load expected properties before move
2295
        $expected = $this->loadLocationProperties($oldParentLocation);
2296
        $childCountBefore = $locationService->getLocationChildCount($oldParentLocation);
2297
2298
        $mediaLocationId = $this->generateId('location', 43);
2299
        $demoDesignLocationId = $this->generateId('location', 56);
2300
        /* BEGIN: Use Case */
2301
        // $mediaLocationId is the ID of the "Media" page location in
2302
        // an eZ Publish demo installation
2303
2304
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2305
        // Publish demo installation
2306
2307
        // Load the location service
2308
        $locationService = $repository->getLocationService();
2309
2310
        // Load location to move
2311
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2312
2313
        // Get the location id of the old parent
2314
        $oldParentLocationId = $locationToMove->parentLocationId;
2315
2316
        // Load new parent location
2317
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2318
2319
        // Move location from "Home" to "Demo Design"
2320
        $locationService->moveSubtree(
2321
            $locationToMove,
2322
            $newParentLocation
2323
        );
2324
2325
        // Reload old parent location
2326
        $oldParentLocation = $locationService->loadLocation($oldParentLocationId);
2327
        /* END: Use Case */
2328
2329
        $this->refreshSearch($repository);
2330
2331
        // Load Subtree properties after move
2332
        $actual = $this->loadLocationProperties($oldParentLocation);
2333
        $childCountAfter = $locationService->getLocationChildCount($oldParentLocation);
2334
2335
        $this->assertEquals($expected, $actual);
2336
        $this->assertEquals($childCountBefore - 1, $childCountAfter);
2337
    }
2338
2339
    /**
2340
     * Test moving invisible (hidden by parent) subtree.
2341
     *
2342
     * @covers \eZ\Publish\API\Repository\LocationService::moveSubtree
2343
     *
2344
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
2345
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
2346
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
2347
     */
2348
    public function testMoveInvisibleSubtree()
2349
    {
2350
        $repository = $this->getRepository();
2351
        $locationService = $repository->getLocationService();
2352
2353
        $rootLocationId = 2;
2354
2355
        $folder = $this->createFolder(['eng-GB' => 'Folder'], $rootLocationId);
2356
        $child = $this->createFolder(['eng-GB' => 'Child'], $folder->contentInfo->mainLocationId);
2357
        $locationService->hideLocation(
2358
            $locationService->loadLocation($folder->contentInfo->mainLocationId)
2359
        );
2360
        // sanity check
2361
        $childLocation = $locationService->loadLocation($child->contentInfo->mainLocationId);
2362
        self::assertFalse($childLocation->hidden);
2363
        self::assertTrue($childLocation->invisible);
2364
        self::assertEquals($folder->contentInfo->mainLocationId, $childLocation->parentLocationId);
2365
2366
        $destination = $this->createFolder(['eng-GB' => 'Destination'], $rootLocationId);
2367
        $destinationLocation = $locationService->loadLocation(
2368
            $destination->contentInfo->mainLocationId
2369
        );
2370
2371
        $locationService->moveSubtree($childLocation, $destinationLocation);
2372
2373
        $childLocation = $locationService->loadLocation($child->contentInfo->mainLocationId);
2374
        // Business logic - Location moved to visible parent becomes visible
2375
        self::assertFalse($childLocation->hidden);
2376
        self::assertFalse($childLocation->invisible);
2377
        self::assertEquals($destinationLocation->id, $childLocation->parentLocationId);
2378
    }
2379
2380
    /**
2381
     * Test for the moveSubtree() method.
2382
     *
2383
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2384
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2385
     */
2386 View Code Duplication
    public function testMoveSubtreeThrowsInvalidArgumentException()
2387
    {
2388
        $repository = $this->getRepository();
2389
        $mediaLocationId = $this->generateId('location', 43);
2390
        $multimediaLocationId = $this->generateId('location', 53);
2391
2392
        /* BEGIN: Use Case */
2393
        // $mediaLocationId is the ID of the "Media" page location in
2394
        // an eZ Publish demo installation
2395
2396
        // $multimediaLocationId is the ID of the "Multimedia" page location in an eZ
2397
        // Publish demo installation
2398
2399
        // Load the location service
2400
        $locationService = $repository->getLocationService();
2401
2402
        // Load location to move
2403
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2404
2405
        // Load new parent location
2406
        $newParentLocation = $locationService->loadLocation($multimediaLocationId);
2407
2408
        // Throws an exception because new parent location is placed below location to move
2409
        $locationService->moveSubtree(
2410
            $locationToMove,
2411
            $newParentLocation
2412
        );
2413
        /* END: Use Case */
2414
    }
2415
2416
    /**
2417
     * Loads properties from all locations in the $location's subtree.
2418
     *
2419
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2420
     * @param array $properties
2421
     *
2422
     * @return array
2423
     */
2424
    private function loadSubtreeProperties(Location $location, array $properties = array())
2425
    {
2426
        $locationService = $this->getRepository()->getLocationService();
2427
2428
        foreach ($locationService->loadLocationChildren($location)->locations as $childLocation) {
2429
            $properties[] = $this->loadLocationProperties($childLocation);
2430
2431
            $properties = $this->loadSubtreeProperties($childLocation, $properties);
2432
        }
2433
2434
        return $properties;
2435
    }
2436
2437
    /**
2438
     * Loads assertable properties from the given location.
2439
     *
2440
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2441
     * @param mixed[] $overwrite
2442
     *
2443
     * @return array
2444
     */
2445 View Code Duplication
    private function loadLocationProperties(Location $location, array $overwrite = array())
2446
    {
2447
        return array_merge(
2448
            array(
2449
                'id' => $location->id,
2450
                'depth' => $location->depth,
2451
                'parentLocationId' => $location->parentLocationId,
2452
                'pathString' => $location->pathString,
2453
                'remoteId' => $location->remoteId,
2454
                'hidden' => $location->hidden,
2455
                'invisible' => $location->invisible,
2456
                'priority' => $location->priority,
2457
                'sortField' => $location->sortField,
2458
                'sortOrder' => $location->sortOrder,
2459
            ),
2460
            $overwrite
2461
        );
2462
    }
2463
2464
    /**
2465
     * Assert generated aliases to expected alias return.
2466
     *
2467
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
2468
     * @param array $expectedAliases
2469
     */
2470
    protected function assertGeneratedAliases($urlAliasService, array $expectedAliases)
2471
    {
2472
        foreach ($expectedAliases as $expectedAlias) {
2473
            $urlAlias = $urlAliasService->lookup($expectedAlias);
2474
            $this->assertPropertiesCorrect(['type' => 0], $urlAlias);
2475
        }
2476
    }
2477
2478
    /**
2479
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
2480
     * @param array $expectedSubItemAliases
2481
     */
2482
    private function assertAliasesBeforeCopy($urlAliasService, array $expectedSubItemAliases)
2483
    {
2484
        foreach ($expectedSubItemAliases as $aliasUrl) {
2485
            try {
2486
                $urlAliasService->lookup($aliasUrl);
2487
                $this->fail('We didn\'t expect to find alias, but it was found');
2488
            } catch (\Exception $e) {
2489
                $this->assertTrue(true); // OK - alias was not found
2490
            }
2491
        }
2492
    }
2493
}
2494