Completed
Push — master ( b83501...acd6b4 )
by André
31:22 queued 12:05
created

testSwapLocationUpdatesMainLocation()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the LocationServiceTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\API\Repository\Tests;
10
11
use eZ\Publish\API\Repository\Exceptions\BadStateException;
12
use eZ\Publish\API\Repository\Values\Content\Content;
13
use eZ\Publish\API\Repository\Values\Content\Location;
14
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
15
use eZ\Publish\API\Repository\Values\Content\LocationList;
16
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
17
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
18
use Exception;
19
use eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct;
20
21
/**
22
 * Test case for operations in the LocationService using in memory storage.
23
 *
24
 * @see eZ\Publish\API\Repository\LocationService
25
 * @group location
26
 */
27
class LocationServiceTest extends BaseTest
28
{
29
    /**
30
     * Test for the newLocationCreateStruct() method.
31
     *
32
     * @return \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct
33
     *
34
     * @see \eZ\Publish\API\Repository\LocationService::newLocationCreateStruct()
35
     */
36 View Code Duplication
    public function testNewLocationCreateStruct()
37
    {
38
        $repository = $this->getRepository();
39
40
        $parentLocationId = $this->generateId('location', 1);
41
        /* BEGIN: Use Case */
42
        // $parentLocationId is the ID of an existing location
43
        $locationService = $repository->getLocationService();
44
45
        $locationCreate = $locationService->newLocationCreateStruct(
46
            $parentLocationId
47
        );
48
        /* END: Use Case */
49
50
        $this->assertInstanceOf(
51
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\LocationCreateStruct',
52
            $locationCreate
53
        );
54
55
        return $locationCreate;
56
    }
57
58
    /**
59
     * Test for the newLocationCreateStruct() method.
60
     *
61
     * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreate
62
     *
63
     * @see \eZ\Publish\API\Repository\LocationService::newLocationCreateStruct()
64
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
65
     */
66
    public function testNewLocationCreateStructValues(LocationCreateStruct $locationCreate)
67
    {
68
        $this->assertPropertiesCorrect(
69
            array(
70
                'priority' => 0,
71
                'hidden' => false,
72
                // remoteId should be initialized with a default value
73
                //'remoteId' => null,
74
                'sortField' => Location::SORT_FIELD_NAME,
75
                'sortOrder' => Location::SORT_ORDER_ASC,
76
                'parentLocationId' => $this->generateId('location', 1),
77
            ),
78
            $locationCreate
79
        );
80
    }
81
82
    /**
83
     * Test for the createLocation() method.
84
     *
85
     * @see \eZ\Publish\API\Repository\LocationService::createLocation()
86
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
87
     */
88
    public function testCreateLocation()
89
    {
90
        $repository = $this->getRepository();
91
92
        $contentId = $this->generateId('object', 41);
93
        $parentLocationId = $this->generateId('location', 5);
94
        /* BEGIN: Use Case */
95
        // $contentId is the ID of an existing content object
96
        // $parentLocationId is the ID of an existing location
97
        $contentService = $repository->getContentService();
98
        $locationService = $repository->getLocationService();
99
100
        // ContentInfo for "How to use eZ Publish"
101
        $contentInfo = $contentService->loadContentInfo($contentId);
102
103
        $locationCreate = $locationService->newLocationCreateStruct($parentLocationId);
104
        $locationCreate->priority = 23;
105
        $locationCreate->hidden = true;
106
        $locationCreate->remoteId = 'sindelfingen';
107
        $locationCreate->sortField = Location::SORT_FIELD_NODE_ID;
108
        $locationCreate->sortOrder = Location::SORT_ORDER_DESC;
109
110
        $location = $locationService->createLocation(
111
            $contentInfo,
112
            $locationCreate
113
        );
114
        /* END: Use Case */
115
116
        $this->assertInstanceOf(
117
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
118
            $location
119
        );
120
121
        return array(
122
            'locationCreate' => $locationCreate,
123
            'createdLocation' => $location,
124
            'contentInfo' => $contentInfo,
125
            'parentLocation' => $locationService->loadLocation($this->generateId('location', 5)),
126
        );
127
    }
128
129
    /**
130
     * Test for the createLocation() method.
131
     *
132
     * @see \eZ\Publish\API\Repository\LocationService::createLocation()
133
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
134
     */
135
    public function testCreateLocationStructValues(array $data)
136
    {
137
        $locationCreate = $data['locationCreate'];
138
        $createdLocation = $data['createdLocation'];
139
        $contentInfo = $data['contentInfo'];
140
141
        $this->assertPropertiesCorrect(
142
            array(
143
                'priority' => $locationCreate->priority,
144
                'hidden' => $locationCreate->hidden,
145
                'invisible' => $locationCreate->hidden,
146
                'remoteId' => $locationCreate->remoteId,
147
                'contentInfo' => $contentInfo,
148
                'parentLocationId' => $locationCreate->parentLocationId,
149
                'pathString' => '/1/5/' . $this->parseId('location', $createdLocation->id) . '/',
150
                'depth' => 2,
151
                'sortField' => $locationCreate->sortField,
152
                'sortOrder' => $locationCreate->sortOrder,
153
            ),
154
            $createdLocation
155
        );
156
157
        $this->assertNotNull($createdLocation->id);
158
    }
159
160
    /**
161
     * Test for the createLocation() method.
162
     *
163
     * @see \eZ\Publish\API\Repository\LocationService::createLocation()
164
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
165
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
166
     */
167 View Code Duplication
    public function testCreateLocationThrowsInvalidArgumentExceptionContentAlreadyBelowParent()
168
    {
169
        $repository = $this->getRepository();
170
171
        $contentId = $this->generateId('object', 11);
172
        $parentLocationId = $this->generateId('location', 5);
173
        /* BEGIN: Use Case */
174
        // $contentId is the ID of an existing content object
175
        // $parentLocationId is the ID of an existing location which already
176
        // has the content assigned to one of its descendant locations
177
        $contentService = $repository->getContentService();
178
        $locationService = $repository->getLocationService();
179
180
        // ContentInfo for "How to use eZ Publish"
181
        $contentInfo = $contentService->loadContentInfo($contentId);
182
183
        $locationCreate = $locationService->newLocationCreateStruct($parentLocationId);
184
185
        // Throws exception, since content is already located at "/1/2/107/110/"
186
        $locationService->createLocation(
187
            $contentInfo,
188
            $locationCreate
189
        );
190
        /* END: Use Case */
191
    }
192
193
    /**
194
     * Test for the createLocation() method.
195
     *
196
     * @see \eZ\Publish\API\Repository\LocationService::createLocation()
197
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
198
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
199
     */
200 View Code Duplication
    public function testCreateLocationThrowsInvalidArgumentExceptionParentIsSubLocationOfContent()
201
    {
202
        $repository = $this->getRepository();
203
204
        $contentId = $this->generateId('object', 4);
205
        $parentLocationId = $this->generateId('location', 12);
206
        /* BEGIN: Use Case */
207
        // $contentId is the ID of an existing content object
208
        // $parentLocationId is the ID of an existing location which is below a
209
        // location that is assigned to the content
210
        $contentService = $repository->getContentService();
211
        $locationService = $repository->getLocationService();
212
213
        // ContentInfo for "How to use eZ Publish"
214
        $contentInfo = $contentService->loadContentInfo($contentId);
215
216
        $locationCreate = $locationService->newLocationCreateStruct($parentLocationId);
217
218
        // Throws exception, since content is already located at "/1/2/"
219
        $locationService->createLocation(
220
            $contentInfo,
221
            $locationCreate
222
        );
223
        /* END: Use Case */
224
    }
225
226
    /**
227
     * Test for the createLocation() method.
228
     *
229
     * @see \eZ\Publish\API\Repository\LocationService::createLocation()
230
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
231
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
232
     */
233 View Code Duplication
    public function testCreateLocationThrowsInvalidArgumentExceptionRemoteIdExists()
234
    {
235
        $repository = $this->getRepository();
236
237
        $contentId = $this->generateId('object', 41);
238
        $parentLocationId = $this->generateId('location', 5);
239
        /* BEGIN: Use Case */
240
        // $contentId is the ID of an existing content object
241
        $contentService = $repository->getContentService();
242
        $locationService = $repository->getLocationService();
243
244
        // ContentInfo for "How to use eZ Publish"
245
        $contentInfo = $contentService->loadContentInfo($contentId);
246
247
        $locationCreate = $locationService->newLocationCreateStruct($parentLocationId);
248
        // This remote ID already exists
249
        $locationCreate->remoteId = 'f3e90596361e31d496d4026eb624c983';
250
251
        // Throws exception, since remote ID is already in use
252
        $locationService->createLocation(
253
            $contentInfo,
254
            $locationCreate
255
        );
256
        /* END: Use Case */
257
    }
258
259
    /**
260
     * Test for the createLocation() method.
261
     *
262
     * @see \eZ\Publish\API\Repository\LocationService::createLocation()
263
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
264
     */
265
    public function testCreateLocationInTransactionWithRollback()
266
    {
267
        $repository = $this->getRepository();
268
269
        $contentId = $this->generateId('object', 41);
270
        $parentLocationId = $this->generateId('location', 5);
271
        /* BEGIN: Use Case */
272
        // $contentId is the ID of an existing content object
273
        // $parentLocationId is the ID of an existing location
274
        $contentService = $repository->getContentService();
275
        $locationService = $repository->getLocationService();
276
277
        $repository->beginTransaction();
278
279
        try {
280
            // ContentInfo for "How to use eZ Publish"
281
            $contentInfo = $contentService->loadContentInfo($contentId);
282
283
            $locationCreate = $locationService->newLocationCreateStruct($parentLocationId);
284
            $locationCreate->remoteId = 'sindelfingen';
285
286
            $createdLocationId = $locationService->createLocation(
287
                $contentInfo,
288
                $locationCreate
289
            )->id;
290
        } catch (Exception $e) {
291
            // Cleanup hanging transaction on error
292
            $repository->rollback();
293
            throw $e;
294
        }
295
296
        $repository->rollback();
297
298
        try {
299
            // Throws exception since creation of location was rolled back
300
            $location = $locationService->loadLocation($createdLocationId);
0 ignored issues
show
Unused Code introduced by
$location is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
301
        } catch (NotFoundException $e) {
302
            return;
303
        }
304
        /* END: Use Case */
305
306
        $this->fail('Objects still exists after rollback.');
307
    }
308
309
    /**
310
     * Test for the loadLocation() method.
311
     *
312
     * @return \eZ\Publish\API\Repository\Values\Content\Location
313
     *
314
     * @covers \eZ\Publish\API\Repository\LocationService::loadLocation
315
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
316
     */
317
    public function testLoadLocation()
318
    {
319
        $repository = $this->getRepository();
320
321
        $locationId = $this->generateId('location', 5);
322
        /* BEGIN: Use Case */
323
        // $locationId is the ID of an existing location
324
        $locationService = $repository->getLocationService();
325
326
        $location = $locationService->loadLocation($locationId);
327
        /* END: Use Case */
328
329
        $this->assertInstanceOf(
330
            Location::class,
331
            $location
332
        );
333
        self::assertEquals(5, $location->id);
334
335
        return $location;
336
    }
337
338
    /**
339
     * Test for the loadLocation() method.
340
     *
341
     * @see \eZ\Publish\API\Repository\LocationService::loadLocation()
342
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
343
     */
344
    public function testLoadLocationRootStructValues()
345
    {
346
        $repository = $this->getRepository();
347
        $locationService = $repository->getLocationService();
348
        $location = $locationService->loadLocation($this->generateId('location', 1));
349
350
        $legacyDateTime = new \DateTime();
351
        $legacyDateTime->setTimestamp(1030968000);
352
353
        // $location
354
        $this->assertPropertiesCorrect(
355
            array(
356
                'id' => $this->generateId('location', 1),
357
                'status' => 1,
358
                'priority' => 0,
359
                'hidden' => false,
360
                'invisible' => false,
361
                'remoteId' => '629709ba256fe317c3ddcee35453a96a',
362
                'parentLocationId' => $this->generateId('location', 1),
363
                'pathString' => '/1/',
364
                'depth' => 0,
365
                'sortField' => 1,
366
                'sortOrder' => 1,
367
            ),
368
            $location
369
        );
370
371
        // $location->contentInfo
372
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $location->contentInfo);
373
        $this->assertPropertiesCorrect(
374
            array(
375
                'id' => $this->generateId('content', 0),
376
                'name' => 'Top Level Nodes',
377
                'sectionId' => 1,
378
                'mainLocationId' => 1,
379
                'contentTypeId' => 1,
380
                'currentVersionNo' => 1,
381
                'published' => 1,
382
                'ownerId' => 14,
383
                'modificationDate' => $legacyDateTime,
384
                'publishedDate' => $legacyDateTime,
385
                'alwaysAvailable' => 1,
386
                'remoteId' => null,
387
                'mainLanguageCode' => 'eng-GB',
388
            ),
389
            $location->contentInfo
390
        );
391
    }
392
393
    /**
394
     * Test for the loadLocation() method.
395
     *
396
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
397
     *
398
     * @see \eZ\Publish\API\Repository\LocationService::loadLocation()
399
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
400
     */
401
    public function testLoadLocationStructValues(Location $location)
402
    {
403
        $this->assertPropertiesCorrect(
404
            array(
405
                'id' => $this->generateId('location', 5),
406
                'priority' => 0,
407
                'hidden' => false,
408
                'invisible' => false,
409
                'remoteId' => '3f6d92f8044aed134f32153517850f5a',
410
                'parentLocationId' => $this->generateId('location', 1),
411
                'pathString' => '/1/5/',
412
                'depth' => 1,
413
                'sortField' => 1,
414
                'sortOrder' => 1,
415
            ),
416
            $location
417
        );
418
419
        $this->assertInstanceOf(
420
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
421
            $location->contentInfo
422
        );
423
        $this->assertEquals($this->generateId('object', 4), $location->contentInfo->id);
424
    }
425
426
    /**
427
     * Test for the loadLocation() method.
428
     *
429
     * @see \eZ\Publish\API\Repository\LocationService::loadLocation()
430
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
431
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
432
     */
433
    public function testLoadLocationThrowsNotFoundException()
434
    {
435
        $repository = $this->getRepository();
436
437
        $nonExistentLocationId = $this->generateId('location', 2342);
438
        /* BEGIN: Use Case */
439
        $locationService = $repository->getLocationService();
440
441
        // Throws exception, if Location with $nonExistentLocationId does not
442
        // exist
443
        $location = $locationService->loadLocation($nonExistentLocationId);
0 ignored issues
show
Unused Code introduced by
$location is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
444
        /* END: Use Case */
445
    }
446
447
    /**
448
     * Test for the loadLocationByRemoteId() method.
449
     *
450
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationByRemoteId()
451
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
452
     */
453 View Code Duplication
    public function testLoadLocationByRemoteId()
454
    {
455
        $repository = $this->getRepository();
456
457
        /* BEGIN: Use Case */
458
        $locationService = $repository->getLocationService();
459
460
        $location = $locationService->loadLocationByRemoteId(
461
            '3f6d92f8044aed134f32153517850f5a'
462
        );
463
        /* END: Use Case */
464
465
        $this->assertEquals(
466
            $locationService->loadLocation($this->generateId('location', 5)),
467
            $location
468
        );
469
    }
470
471
    /**
472
     * Test for the loadLocationByRemoteId() method.
473
     *
474
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationByRemoteId()
475
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
476
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
477
     */
478
    public function testLoadLocationByRemoteIdThrowsNotFoundException()
479
    {
480
        $repository = $this->getRepository();
481
482
        /* BEGIN: Use Case */
483
        $locationService = $repository->getLocationService();
484
485
        // Throws exception, since Location with remote ID does not exist
486
        $location = $locationService->loadLocationByRemoteId(
0 ignored issues
show
Unused Code introduced by
$location is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
487
            'not-exists'
488
        );
489
        /* END: Use Case */
490
    }
491
492
    /**
493
     * Test for the loadLocations() method.
494
     *
495
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
496
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
497
     */
498
    public function testLoadLocations()
499
    {
500
        $repository = $this->getRepository();
501
502
        $contentId = $this->generateId('object', 4);
503
        /* BEGIN: Use Case */
504
        // $contentId contains the ID of an existing content object
505
        $contentService = $repository->getContentService();
506
        $locationService = $repository->getLocationService();
507
508
        $contentInfo = $contentService->loadContentInfo($contentId);
509
510
        $locations = $locationService->loadLocations($contentInfo);
511
        /* END: Use Case */
512
513
        $this->assertInternalType('array', $locations);
514
        self::assertNotEmpty($locations);
515
516
        foreach ($locations as $location) {
517
            self::assertInstanceOf(Location::class, $location);
518
            self::assertEquals($contentInfo->id, $location->getContentInfo()->id);
519
        }
520
521
        return $locations;
522
    }
523
524
    /**
525
     * Test for the loadLocations() method.
526
     *
527
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
528
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
529
     */
530
    public function testLoadLocationsContent(array $locations)
531
    {
532
        $repository = $this->getRepository();
533
        $locationService = $repository->getLocationService();
0 ignored issues
show
Unused Code introduced by
$locationService is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
534
535
        $this->assertEquals(1, count($locations));
536
        foreach ($locations as $loadedLocation) {
537
            $this->assertInstanceOf(
538
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
539
                $loadedLocation
540
            );
541
        }
542
543
        usort(
544
            $locations,
545
            function ($a, $b) {
546
                strcmp($a->id, $b->id);
547
            }
548
        );
549
550
        $this->assertEquals(
551
            array($this->generateId('location', 5)),
552
            array_map(
553
                function (Location $location) {
554
                    return $location->id;
555
                },
556
                $locations
557
            )
558
        );
559
    }
560
561
    /**
562
     * Test for the loadLocations() method.
563
     *
564
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
565
     *
566
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations($contentInfo, $rootLocation)
567
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
568
     */
569
    public function testLoadLocationsLimitedSubtree()
570
    {
571
        $repository = $this->getRepository();
572
573
        $originalLocationId = $this->generateId('location', 54);
574
        $originalParentLocationId = $this->generateId('location', 48);
575
        $newParentLocationId = $this->generateId('location', 43);
576
        /* BEGIN: Use Case */
577
        // $originalLocationId is the ID of an existing location
578
        // $originalParentLocationId is the ID of the parent location of
579
        //     $originalLocationId
580
        // $newParentLocationId is the ID of an existing location outside the tree
581
        // of $originalLocationId and $originalParentLocationId
582
        $locationService = $repository->getLocationService();
583
584
        // Location at "/1/48/54"
585
        $originalLocation = $locationService->loadLocation($originalLocationId);
586
587
        // Create location under "/1/43/"
588
        $locationCreate = $locationService->newLocationCreateStruct($newParentLocationId);
589
        $locationService->createLocation(
590
            $originalLocation->contentInfo,
591
            $locationCreate
592
        );
593
594
        $findRootLocation = $locationService->loadLocation($originalParentLocationId);
595
596
        // Returns an array with only $originalLocation
597
        $locations = $locationService->loadLocations(
598
            $originalLocation->contentInfo,
599
            $findRootLocation
600
        );
601
        /* END: Use Case */
602
603
        $this->assertInternalType('array', $locations);
604
605
        return $locations;
606
    }
607
608
    /**
609
     * Test for the loadLocations() method.
610
     *
611
     * @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
612
     *
613
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
614
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationsLimitedSubtree
615
     */
616
    public function testLoadLocationsLimitedSubtreeContent(array $locations)
617
    {
618
        $this->assertEquals(1, count($locations));
619
620
        $this->assertEquals(
621
            $this->generateId('location', 54),
622
            reset($locations)->id
623
        );
624
    }
625
626
    /**
627
     * Test for the loadLocations() method.
628
     *
629
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
630
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
631
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
632
     */
633 View Code Duplication
    public function testLoadLocationsThrowsBadStateException()
634
    {
635
        $repository = $this->getRepository();
636
637
        /* BEGIN: Use Case */
638
        $contentTypeService = $repository->getContentTypeService();
639
        $contentService = $repository->getContentService();
640
        $locationService = $repository->getLocationService();
641
642
        // Create new content, which is not published
643
        $folderType = $contentTypeService->loadContentTypeByIdentifier('folder');
644
        $contentCreate = $contentService->newContentCreateStruct($folderType, 'eng-US');
645
        $contentCreate->setField('name', 'New Folder');
646
        $content = $contentService->createContent($contentCreate);
647
648
        // Throws Exception, since $content has no published version, yet
649
        $locationService->loadLocations(
650
            $content->contentInfo
651
        );
652
        /* END: Use Case */
653
    }
654
655
    /**
656
     * Test for the loadLocations() method.
657
     *
658
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations($contentInfo, $rootLocation)
659
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
660
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
661
     */
662
    public function testLoadLocationsThrowsBadStateExceptionLimitedSubtree()
663
    {
664
        $repository = $this->getRepository();
665
666
        $someLocationId = $this->generateId('location', 2);
667
        /* BEGIN: Use Case */
668
        // $someLocationId is the ID of an existing location
669
        $contentTypeService = $repository->getContentTypeService();
670
        $contentService = $repository->getContentService();
671
        $locationService = $repository->getLocationService();
672
673
        // Create new content, which is not published
674
        $folderType = $contentTypeService->loadContentTypeByIdentifier('folder');
675
        $contentCreate = $contentService->newContentCreateStruct($folderType, 'eng-US');
676
        $contentCreate->setField('name', 'New Folder');
677
        $content = $contentService->createContent($contentCreate);
678
679
        $findRootLocation = $locationService->loadLocation($someLocationId);
680
681
        // Throws Exception, since $content has no published version, yet
682
        $locationService->loadLocations(
683
            $content->contentInfo,
684
            $findRootLocation
685
        );
686
        /* END: Use Case */
687
    }
688
689
    /**
690
     * Test for the loadLocationChildren() method.
691
     *
692
     * @covers \eZ\Publish\API\Repository\LocationService::loadLocationChildren
693
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
694
     */
695
    public function testLoadLocationChildren()
696
    {
697
        $repository = $this->getRepository();
698
699
        $locationId = $this->generateId('location', 5);
700
        /* BEGIN: Use Case */
701
        // $locationId is the ID of an existing location
702
        $locationService = $repository->getLocationService();
703
704
        $location = $locationService->loadLocation($locationId);
705
706
        $childLocations = $locationService->loadLocationChildren($location);
707
        /* END: Use Case */
708
709
        $this->assertInstanceOf(LocationList::class, $childLocations);
710
        $this->assertInternalType('array', $childLocations->locations);
711
        $this->assertNotEmpty($childLocations->locations);
712
        $this->assertInternalType('int', $childLocations->totalCount);
713
714
        foreach ($childLocations->locations as $childLocation) {
715
            $this->assertInstanceOf(Location::class, $childLocation);
716
            $this->assertEquals($location->id, $childLocation->parentLocationId);
717
        }
718
719
        return $childLocations;
720
    }
721
722
    /**
723
     * Test loading parent Locations for draft Content.
724
     *
725
     * @covers \eZ\Publish\API\Repository\LocationService::loadParentLocationsForDraftContent
726
     */
727
    public function testLoadParentLocationsForDraftContent()
728
    {
729
        $repository = $this->getRepository();
730
        $locationService = $repository->getLocationService();
731
        $contentService = $repository->getContentService();
732
        $contentTypeService = $repository->getContentTypeService();
733
734
        // prepare locations
735
        $locationCreateStructs = [
736
            $locationService->newLocationCreateStruct(2),
737
            $locationService->newLocationCreateStruct(5),
738
        ];
739
740
        // Create new content
741
        $folderType = $contentTypeService->loadContentTypeByIdentifier('folder');
742
        $contentCreate = $contentService->newContentCreateStruct($folderType, 'eng-US');
743
        $contentCreate->setField('name', 'New Folder');
744
        $contentDraft = $contentService->createContent($contentCreate, $locationCreateStructs);
745
746
        // Test loading parent Locations
747
        $locations = $locationService->loadParentLocationsForDraftContent($contentDraft->versionInfo);
748
749
        self::assertCount(2, $locations);
750
        foreach ($locations as $location) {
751
            // test it is one of the given parent locations
752
            self::assertTrue($location->id === 2 || $location->id === 5);
753
        }
754
755
        return $contentDraft;
756
    }
757
758
    /**
759
     * Test that trying to load parent Locations throws Exception if Content is not a draft.
760
     *
761
     * @depends testLoadParentLocationsForDraftContent
762
     *
763
     * @param \eZ\Publish\API\Repository\Values\Content\Content $contentDraft
764
     */
765
    public function testLoadParentLocationsForDraftContentThrowsBadStateException(Content $contentDraft)
766
    {
767
        $this->expectException(BadStateException::class);
768
        $this->expectExceptionMessageRegExp('/has been already published/');
769
770
        $repository = $this->getRepository(false);
771
        $locationService = $repository->getLocationService();
772
        $contentService = $repository->getContentService();
773
774
        $content = $contentService->publishVersion($contentDraft->versionInfo);
775
776
        $locationService->loadParentLocationsForDraftContent($content->versionInfo);
777
    }
778
779
    /**
780
     * Test for the getLocationChildCount() method.
781
     *
782
     * @see \eZ\Publish\API\Repository\LocationService::getLocationChildCount()
783
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
784
     */
785
    public function testGetLocationChildCount()
786
    {
787
        // $locationId is the ID of an existing location
788
        $locationService = $this->getRepository()->getLocationService();
789
790
        $this->assertSame(
791
            5,
792
            $locationService->getLocationChildCount(
793
                $locationService->loadLocation($this->generateId('location', 5))
794
            )
795
        );
796
    }
797
798
    /**
799
     * Test for the loadLocationChildren() method.
800
     *
801
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren()
802
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
803
     */
804
    public function testLoadLocationChildrenData(LocationList $locations)
805
    {
806
        $this->assertEquals(5, count($locations->locations));
807
        $this->assertEquals(5, $locations->totalCount);
808
809
        foreach ($locations->locations as $location) {
810
            $this->assertInstanceOf(
811
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
812
                $location
813
            );
814
        }
815
816
        $this->assertEquals(
817
            array(
818
                $this->generateId('location', 12),
819
                $this->generateId('location', 13),
820
                $this->generateId('location', 14),
821
                $this->generateId('location', 44),
822
                $this->generateId('location', 61),
823
            ),
824
            array_map(
825
                function (Location $location) {
826
                    return $location->id;
827
                },
828
                $locations->locations
829
            )
830
        );
831
    }
832
833
    /**
834
     * Test for the loadLocationChildren() method.
835
     *
836
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
837
     *
838
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset)
839
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
840
     */
841 View Code Duplication
    public function testLoadLocationChildrenWithOffset()
842
    {
843
        $repository = $this->getRepository();
844
845
        $locationId = $this->generateId('location', 5);
846
        /* BEGIN: Use Case */
847
        // $locationId is the ID of an existing location
848
        $locationService = $repository->getLocationService();
849
850
        $location = $locationService->loadLocation($locationId);
851
852
        $childLocations = $locationService->loadLocationChildren($location, 2);
853
        /* END: Use Case */
854
855
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\LocationList', $childLocations);
856
        $this->assertInternalType('array', $childLocations->locations);
857
        $this->assertInternalType('int', $childLocations->totalCount);
858
859
        return $childLocations;
860
    }
861
862
    /**
863
     * Test for the loadLocationChildren() method.
864
     *
865
     * @param \eZ\Publish\API\Repository\Values\Content\LocationList $locations
866
     *
867
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset)
868
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildrenWithOffset
869
     */
870 View Code Duplication
    public function testLoadLocationChildrenDataWithOffset(LocationList $locations)
871
    {
872
        $this->assertEquals(3, count($locations->locations));
873
        $this->assertEquals(5, $locations->totalCount);
874
875
        foreach ($locations->locations as $location) {
876
            $this->assertInstanceOf(
877
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
878
                $location
879
            );
880
        }
881
882
        $this->assertEquals(
883
            array(
884
                $this->generateId('location', 14),
885
                $this->generateId('location', 44),
886
                $this->generateId('location', 61),
887
            ),
888
            array_map(
889
                function (Location $location) {
890
                    return $location->id;
891
                },
892
                $locations->locations
893
            )
894
        );
895
    }
896
897
    /**
898
     * Test for the loadLocationChildren() method.
899
     *
900
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
901
     *
902
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset, $limit)
903
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
904
     */
905 View Code Duplication
    public function testLoadLocationChildrenWithOffsetAndLimit()
906
    {
907
        $repository = $this->getRepository();
908
909
        $locationId = $this->generateId('location', 5);
910
        /* BEGIN: Use Case */
911
        // $locationId is the ID of an existing location
912
        $locationService = $repository->getLocationService();
913
914
        $location = $locationService->loadLocation($locationId);
915
916
        $childLocations = $locationService->loadLocationChildren($location, 2, 2);
917
        /* END: Use Case */
918
919
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\LocationList', $childLocations);
920
        $this->assertInternalType('array', $childLocations->locations);
921
        $this->assertInternalType('int', $childLocations->totalCount);
922
923
        return $childLocations;
924
    }
925
926
    /**
927
     * Test for the loadLocationChildren() method.
928
     *
929
     * @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
930
     *
931
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset, $limit)
932
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildrenWithOffsetAndLimit
933
     */
934 View Code Duplication
    public function testLoadLocationChildrenDataWithOffsetAndLimit(LocationList $locations)
935
    {
936
        $this->assertEquals(2, count($locations->locations));
937
        $this->assertEquals(5, $locations->totalCount);
938
939
        foreach ($locations->locations as $location) {
940
            $this->assertInstanceOf(
941
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
942
                $location
943
            );
944
        }
945
946
        $this->assertEquals(
947
            array(
948
                $this->generateId('location', 14),
949
                $this->generateId('location', 44),
950
            ),
951
            array_map(
952
                function (Location $location) {
953
                    return $location->id;
954
                },
955
                $locations->locations
956
            )
957
        );
958
    }
959
960
    /**
961
     * Test for the newLocationUpdateStruct() method.
962
     *
963
     * @covers \eZ\Publish\API\Repository\LocationService::newLocationUpdateStruct
964
     */
965 View Code Duplication
    public function testNewLocationUpdateStruct()
966
    {
967
        $repository = $this->getRepository();
968
969
        /* BEGIN: Use Case */
970
        $locationService = $repository->getLocationService();
971
972
        $updateStruct = $locationService->newLocationUpdateStruct();
973
        /* END: Use Case */
974
975
        $this->assertInstanceOf(
976
            LocationUpdateStruct::class,
977
            $updateStruct
978
        );
979
980
        $this->assertPropertiesCorrect(
981
            [
982
                'priority' => null,
983
                'remoteId' => null,
984
                'sortField' => null,
985
                'sortOrder' => null,
986
            ],
987
            $updateStruct
988
        );
989
    }
990
991
    /**
992
     * Test for the updateLocation() method.
993
     *
994
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
995
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
996
     */
997
    public function testUpdateLocation()
998
    {
999
        $repository = $this->getRepository();
1000
1001
        $originalLocationId = $this->generateId('location', 5);
1002
        /* BEGIN: Use Case */
1003
        // $originalLocationId is the ID of an existing location
1004
        $locationService = $repository->getLocationService();
1005
1006
        $originalLocation = $locationService->loadLocation($originalLocationId);
1007
1008
        $updateStruct = $locationService->newLocationUpdateStruct();
1009
        $updateStruct->priority = 3;
1010
        $updateStruct->remoteId = 'c7adcbf1e96bc29bca28c2d809d0c7ef69272651';
1011
        $updateStruct->sortField = Location::SORT_FIELD_PRIORITY;
1012
        $updateStruct->sortOrder = Location::SORT_ORDER_DESC;
1013
1014
        $updatedLocation = $locationService->updateLocation($originalLocation, $updateStruct);
1015
        /* END: Use Case */
1016
1017
        $this->assertInstanceOf(
1018
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1019
            $updatedLocation
1020
        );
1021
1022
        return array(
1023
            'originalLocation' => $originalLocation,
1024
            'updateStruct' => $updateStruct,
1025
            'updatedLocation' => $updatedLocation,
1026
        );
1027
    }
1028
1029
    /**
1030
     * Test for the updateLocation() method.
1031
     *
1032
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1033
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testUpdateLocation
1034
     */
1035
    public function testUpdateLocationStructValues(array $data)
1036
    {
1037
        $originalLocation = $data['originalLocation'];
1038
        $updateStruct = $data['updateStruct'];
1039
        $updatedLocation = $data['updatedLocation'];
1040
1041
        $this->assertPropertiesCorrect(
1042
            array(
1043
                'id' => $originalLocation->id,
1044
                'priority' => $updateStruct->priority,
1045
                'hidden' => $originalLocation->hidden,
1046
                'invisible' => $originalLocation->invisible,
1047
                'remoteId' => $updateStruct->remoteId,
1048
                'contentInfo' => $originalLocation->contentInfo,
1049
                'parentLocationId' => $originalLocation->parentLocationId,
1050
                'pathString' => $originalLocation->pathString,
1051
                'depth' => $originalLocation->depth,
1052
                'sortField' => $updateStruct->sortField,
1053
                'sortOrder' => $updateStruct->sortOrder,
1054
            ),
1055
            $updatedLocation
1056
        );
1057
    }
1058
1059
    /**
1060
     * Test for the updateLocation() method.
1061
     *
1062
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1063
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1064
     */
1065
    public function testUpdateLocationWithSameRemoteId()
1066
    {
1067
        $repository = $this->getRepository();
1068
1069
        $locationId = $this->generateId('location', 5);
1070
        /* BEGIN: Use Case */
1071
        // $locationId and remote ID is the IDs of the same, existing location
1072
        $locationService = $repository->getLocationService();
1073
1074
        $originalLocation = $locationService->loadLocation($locationId);
1075
1076
        $updateStruct = $locationService->newLocationUpdateStruct();
1077
1078
        // Remote ID of an existing location with the same locationId
1079
        $updateStruct->remoteId = $originalLocation->remoteId;
1080
1081
        // Sets one of the properties to be able to confirm location gets updated, here: priority
1082
        $updateStruct->priority = 2;
1083
1084
        $location = $locationService->updateLocation($originalLocation, $updateStruct);
1085
1086
        // Checks that the location was updated
1087
        $this->assertEquals(2, $location->priority);
1088
1089
        // Checks that remoteId remains the same
1090
        $this->assertEquals($originalLocation->remoteId, $location->remoteId);
1091
        /* END: Use Case */
1092
    }
1093
1094
    /**
1095
     * Test for the updateLocation() method.
1096
     *
1097
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1098
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1099
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1100
     */
1101
    public function testUpdateLocationThrowsInvalidArgumentException()
1102
    {
1103
        $repository = $this->getRepository();
1104
1105
        $locationId = $this->generateId('location', 5);
1106
        /* BEGIN: Use Case */
1107
        // $locationId and remoteId is the IDs of an existing, but not the same, location
1108
        $locationService = $repository->getLocationService();
1109
1110
        $originalLocation = $locationService->loadLocation($locationId);
1111
1112
        $updateStruct = $locationService->newLocationUpdateStruct();
1113
1114
        // Remote ID of an existing location with a different locationId
1115
        $updateStruct->remoteId = 'f3e90596361e31d496d4026eb624c983';
1116
1117
        // Throws exception, since remote ID is already taken
1118
        $locationService->updateLocation($originalLocation, $updateStruct);
1119
        /* END: Use Case */
1120
    }
1121
1122
    /**
1123
     * Test for the updateLocation() method.
1124
     * Ref EZP-23302: Update Location fails if no change is performed with the update.
1125
     *
1126
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1127
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1128
     */
1129
    public function testUpdateLocationTwice()
1130
    {
1131
        $repository = $this->getRepository();
1132
1133
        $locationId = $this->generateId('location', 5);
1134
        /* BEGIN: Use Case */
1135
        $locationService = $repository->getLocationService();
1136
        $repository->setCurrentUser($repository->getUserService()->loadUser(14));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
1137
1138
        $originalLocation = $locationService->loadLocation($locationId);
1139
1140
        $updateStruct = $locationService->newLocationUpdateStruct();
1141
        $updateStruct->priority = 42;
1142
1143
        $updatedLocation = $locationService->updateLocation($originalLocation, $updateStruct);
1144
1145
        // Repeated update with the same, unchanged struct
1146
        $secondUpdatedLocation = $locationService->updateLocation($updatedLocation, $updateStruct);
1147
        /* END: Use Case */
1148
1149
        $this->assertEquals($updatedLocation->priority, 42);
1150
        $this->assertEquals($secondUpdatedLocation->priority, 42);
1151
    }
1152
1153
    /**
1154
     * Test for the swapLocation() method.
1155
     *
1156
     * @see \eZ\Publish\API\Repository\LocationService::swapLocation()
1157
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1158
     */
1159
    public function testSwapLocation()
1160
    {
1161
        $repository = $this->getRepository();
1162
        $locationService = $repository->getLocationService();
1163
1164
        $mediaLocationId = $this->generateId('location', 43);
1165
        $demoDesignLocationId = $this->generateId('location', 56);
1166
1167
        $mediaContentInfo = $locationService->loadLocation($mediaLocationId)->getContentInfo();
1168
        $demoDesignContentInfo = $locationService->loadLocation($demoDesignLocationId)->getContentInfo();
1169
1170
        /* BEGIN: Use Case */
1171
        // $mediaLocationId is the ID of the "Media" page location in
1172
        // an eZ Publish demo installation
1173
1174
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1175
        // Publish demo installation
1176
1177
        // Load the location service
1178
        $locationService = $repository->getLocationService();
1179
1180
        $mediaLocation = $locationService->loadLocation($mediaLocationId);
1181
        $demoDesignLocation = $locationService->loadLocation($demoDesignLocationId);
1182
1183
        // Swaps the content referred to by the locations
1184
        $locationService->swapLocation($mediaLocation, $demoDesignLocation);
1185
        /* END: Use Case */
1186
1187
        // Reload Locations, IDs swapped
1188
        $demoDesignLocation = $locationService->loadLocation($mediaLocationId);
1189
        $mediaLocation = $locationService->loadLocation($demoDesignLocationId);
1190
1191
        // Assert Location's Content is updated
1192
        $this->assertEquals(
1193
            $mediaContentInfo->id,
1194
            $mediaLocation->getContentInfo()->id
1195
        );
1196
        $this->assertEquals(
1197
            $demoDesignContentInfo->id,
1198
            $demoDesignLocation->getContentInfo()->id
1199
        );
1200
1201
        // Assert URL aliases are updated
1202
        $this->assertEquals(
1203
            $mediaLocation->id,
1204
            $repository->getURLAliasService()->lookup('/Design/Media')->destination
1205
        );
1206
        $this->assertEquals(
1207
            $demoDesignLocation->id,
1208
            $repository->getURLAliasService()->lookup('/Plain-site')->destination
1209
        );
1210
    }
1211
1212
    /**
1213
     * Test swapping Main Location of a Content with another one updates Content item Main Location.
1214
     *
1215
     * @covers \eZ\Publish\API\Repository\LocationService::swapLocation
1216
     */
1217
    public function testSwapLocationUpdatesMainLocation()
1218
    {
1219
        $repository = $this->getRepository();
1220
        $locationService = $repository->getLocationService();
1221
        $contentService = $repository->getContentService();
1222
1223
        $mainLocationParentId = 60;
1224
        $secondaryLocationId = 43;
1225
1226
        $publishedContent = $this->publishContentWithParentLocation(
1227
            'Content for Swap Location Test', $mainLocationParentId
1228
        );
1229
1230
        // sanity check
1231
        $mainLocation = $locationService->loadLocation($publishedContent->contentInfo->mainLocationId);
1232
        self::assertEquals($mainLocationParentId, $mainLocation->parentLocationId);
1233
1234
        // load another pre-existing location
1235
        $secondaryLocation = $locationService->loadLocation($secondaryLocationId);
1236
1237
        // swap the Main Location with a secondary one
1238
        $locationService->swapLocation($mainLocation, $secondaryLocation);
1239
1240
        // check if Main Location has been updated
1241
        $mainLocation = $locationService->loadLocation($secondaryLocation->id);
1242
        self::assertEquals($publishedContent->contentInfo->id, $mainLocation->contentInfo->id);
1243
        self::assertEquals($mainLocation->id, $mainLocation->contentInfo->mainLocationId);
1244
1245
        $reloadedContent = $contentService->loadContentByContentInfo($publishedContent->contentInfo);
1246
        self::assertEquals($mainLocation->id, $reloadedContent->contentInfo->mainLocationId);
1247
    }
1248
1249
    /**
1250
     * Test for the hideLocation() method.
1251
     *
1252
     * @see \eZ\Publish\API\Repository\LocationService::hideLocation()
1253
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1254
     */
1255
    public function testHideLocation()
1256
    {
1257
        $repository = $this->getRepository();
1258
1259
        $locationId = $this->generateId('location', 5);
1260
        /* BEGIN: Use Case */
1261
        // $locationId is the ID of an existing location
1262
        $locationService = $repository->getLocationService();
1263
1264
        $visibleLocation = $locationService->loadLocation($locationId);
1265
1266
        $hiddenLocation = $locationService->hideLocation($visibleLocation);
1267
        /* END: Use Case */
1268
1269
        $this->assertInstanceOf(
1270
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1271
            $hiddenLocation
1272
        );
1273
1274
        $this->assertTrue(
1275
            $hiddenLocation->hidden,
1276
            sprintf(
1277
                'Location with ID "%s" not hidden.',
1278
                $hiddenLocation->id
1279
            )
1280
        );
1281
1282
        $this->refreshSearch($repository);
1283
1284
        foreach ($locationService->loadLocationChildren($hiddenLocation)->locations as $child) {
1285
            $this->assertSubtreeProperties(
1286
                array('invisible' => true),
1287
                $child
1288
            );
1289
        }
1290
    }
1291
1292
    /**
1293
     * Assert that $expectedValues are set in the subtree starting at $location.
1294
     *
1295
     * @param array $expectedValues
1296
     * @param Location $location
1297
     */
1298
    protected function assertSubtreeProperties(array $expectedValues, Location $location, $stopId = null)
1299
    {
1300
        $repository = $this->getRepository();
1301
        $locationService = $repository->getLocationService();
1302
1303
        if ($location->id === $stopId) {
1304
            return;
1305
        }
1306
1307
        foreach ($expectedValues as $propertyName => $propertyValue) {
1308
            $this->assertEquals(
1309
                $propertyValue,
1310
                $location->$propertyName
1311
            );
1312
1313
            foreach ($locationService->loadLocationChildren($location)->locations as $child) {
1314
                $this->assertSubtreeProperties($expectedValues, $child);
1315
            }
1316
        }
1317
    }
1318
1319
    /**
1320
     * Test for the unhideLocation() method.
1321
     *
1322
     * @see \eZ\Publish\API\Repository\LocationService::unhideLocation()
1323
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testHideLocation
1324
     */
1325
    public function testUnhideLocation()
1326
    {
1327
        $repository = $this->getRepository();
1328
1329
        $locationId = $this->generateId('location', 5);
1330
        /* BEGIN: Use Case */
1331
        // $locationId is the ID of an existing location
1332
        $locationService = $repository->getLocationService();
1333
1334
        $visibleLocation = $locationService->loadLocation($locationId);
1335
        $hiddenLocation = $locationService->hideLocation($visibleLocation);
1336
1337
        $unHiddenLocation = $locationService->unhideLocation($hiddenLocation);
1338
        /* END: Use Case */
1339
1340
        $this->assertInstanceOf(
1341
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1342
            $unHiddenLocation
1343
        );
1344
1345
        $this->assertFalse(
1346
            $unHiddenLocation->hidden,
1347
            sprintf(
1348
                'Location with ID "%s" not unhidden.',
1349
                $unHiddenLocation->id
1350
            )
1351
        );
1352
1353
        $this->refreshSearch($repository);
1354
1355
        foreach ($locationService->loadLocationChildren($unHiddenLocation)->locations as $child) {
1356
            $this->assertSubtreeProperties(
1357
                array('invisible' => false),
1358
                $child
1359
            );
1360
        }
1361
    }
1362
1363
    /**
1364
     * Test for the unhideLocation() method.
1365
     *
1366
     * @see \eZ\Publish\API\Repository\LocationService::unhideLocation()
1367
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testUnhideLocation
1368
     */
1369
    public function testUnhideLocationNotUnhidesHiddenSubtree()
1370
    {
1371
        $repository = $this->getRepository();
1372
1373
        $higherLocationId = $this->generateId('location', 5);
1374
        $lowerLocationId = $this->generateId('location', 13);
1375
        /* BEGIN: Use Case */
1376
        // $higherLocationId is the ID of a location
1377
        // $lowerLocationId is the ID of a location below $higherLocationId
1378
        $locationService = $repository->getLocationService();
1379
1380
        $higherLocation = $locationService->loadLocation($higherLocationId);
1381
        $hiddenHigherLocation = $locationService->hideLocation($higherLocation);
1382
1383
        $lowerLocation = $locationService->loadLocation($lowerLocationId);
1384
        $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...
1385
1386
        $unHiddenHigherLocation = $locationService->unhideLocation($hiddenHigherLocation);
1387
        /* END: Use Case */
1388
1389
        $this->assertInstanceOf(
1390
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1391
            $unHiddenHigherLocation
1392
        );
1393
1394
        $this->assertFalse(
1395
            $unHiddenHigherLocation->hidden,
1396
            sprintf(
1397
                'Location with ID "%s" not unhidden.',
1398
                $unHiddenHigherLocation->id
1399
            )
1400
        );
1401
1402
        $this->refreshSearch($repository);
1403
1404
        foreach ($locationService->loadLocationChildren($unHiddenHigherLocation)->locations as $child) {
1405
            $this->assertSubtreeProperties(
1406
                array('invisible' => false),
1407
                $child,
1408
                $this->generateId('location', 13)
1409
            );
1410
        }
1411
1412
        $stillHiddenLocation = $locationService->loadLocation($this->generateId('location', 13));
1413
        $this->assertTrue(
1414
            $stillHiddenLocation->hidden,
1415
            sprintf(
1416
                'Hidden sub-location with ID %s accidentally unhidden.',
1417
                $stillHiddenLocation->id
1418
            )
1419
        );
1420
        foreach ($locationService->loadLocationChildren($stillHiddenLocation)->locations as $child) {
1421
            $this->assertSubtreeProperties(
1422
                array('invisible' => true),
1423
                $child
1424
            );
1425
        }
1426
    }
1427
1428
    /**
1429
     * Test for the deleteLocation() method.
1430
     *
1431
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1432
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1433
     */
1434
    public function testDeleteLocation()
1435
    {
1436
        $repository = $this->getRepository();
1437
1438
        $mediaLocationId = $this->generateId('location', 43);
1439
        /* BEGIN: Use Case */
1440
        // $mediaLocationId is the ID of the location of the
1441
        // "Media" location in an eZ Publish demo installation
1442
        $locationService = $repository->getLocationService();
1443
1444
        $location = $locationService->loadLocation($mediaLocationId);
1445
1446
        $locationService->deleteLocation($location);
1447
        /* END: Use Case */
1448
1449
        try {
1450
            $locationService->loadLocation($mediaLocationId);
1451
            $this->fail("Location $mediaLocationId not deleted.");
1452
        } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1453
        }
1454
1455
        // The following IDs are IDs of child locations of $mediaLocationId location
1456
        // ( Media/Images, Media/Files, Media/Multimedia respectively )
1457
        foreach (array(51, 52, 53) as $childLocationId) {
1458
            try {
1459
                $locationService->loadLocation($this->generateId('location', $childLocationId));
1460
                $this->fail("Location $childLocationId not deleted.");
1461
            } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1462
            }
1463
        }
1464
1465
        // The following IDs are IDs of content below $mediaLocationId location
1466
        // ( Media/Images, Media/Files, Media/Multimedia respectively )
1467
        $contentService = $this->getRepository()->getContentService();
1468
        foreach (array(49, 50, 51) as $childContentId) {
1469
            try {
1470
                $contentService->loadContentInfo($this->generateId('object', $childContentId));
1471
                $this->fail("Content $childContentId not deleted.");
1472
            } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1473
            }
1474
        }
1475
    }
1476
1477
    /**
1478
     * Test for the deleteLocation() method.
1479
     *
1480
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1481
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testDeleteLocation
1482
     */
1483
    public function testDeleteLocationDecrementsChildCountOnParent()
1484
    {
1485
        $repository = $this->getRepository();
1486
1487
        $mediaLocationId = $this->generateId('location', 43);
1488
        /* BEGIN: Use Case */
1489
        // $mediaLocationId is the ID of the location of the
1490
        // "Media" location in an eZ Publish demo installation
1491
1492
        $locationService = $repository->getLocationService();
1493
1494
        // Load the current the user group location
1495
        $location = $locationService->loadLocation($mediaLocationId);
1496
1497
        // Load the parent location
1498
        $parentLocation = $locationService->loadLocation(
1499
            $location->parentLocationId
1500
        );
1501
1502
        // Get child count
1503
        $childCountBefore = $locationService->getLocationChildCount($parentLocation);
1504
1505
        // Delete the user group location
1506
        $locationService->deleteLocation($location);
1507
1508
        $this->refreshSearch($repository);
1509
1510
        // Reload parent location
1511
        $parentLocation = $locationService->loadLocation(
1512
            $location->parentLocationId
1513
        );
1514
1515
        // This will be $childCountBefore - 1
1516
        $childCountAfter = $locationService->getLocationChildCount($parentLocation);
1517
        /* END: Use Case */
1518
1519
        $this->assertEquals($childCountBefore - 1, $childCountAfter);
1520
    }
1521
1522
    /**
1523
     * Test for the deleteLocation() method.
1524
     *
1525
     * Related issue: EZP-21904
1526
     *
1527
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1528
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1529
     */
1530
    public function testDeleteContentObjectLastLocation()
1531
    {
1532
        $repository = $this->getRepository();
1533
1534
        /* BEGIN: Use case */
1535
        $contentService = $repository->getContentService();
1536
        $locationService = $repository->getLocationService();
1537
        $contentTypeService = $repository->getContentTypeService();
1538
        $urlAliasService = $repository->getURLAliasService();
1539
1540
        // prepare Content object
1541
        $createStruct = $contentService->newContentCreateStruct(
1542
            $contentTypeService->loadContentTypeByIdentifier('folder'),
1543
            'eng-GB'
1544
        );
1545
        $createStruct->setField('name', 'Test folder');
1546
1547
        // creata Content object
1548
        $content = $contentService->publishVersion(
1549
            $contentService->createContent(
1550
                $createStruct,
1551
                array($locationService->newLocationCreateStruct(2))
1552
            )->versionInfo
1553
        );
1554
1555
        // delete location
1556
        $locationService->deleteLocation(
1557
            $locationService->loadLocation(
1558
                $urlAliasService->lookup('/Test-folder')->destination
1559
            )
1560
        );
1561
1562
        // this should throw a not found exception
1563
        $contentService->loadContent($content->versionInfo->contentInfo->id);
1564
        /* END: Use case*/
1565
    }
1566
1567
    /**
1568
     * Test for the copySubtree() method.
1569
     *
1570
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1571
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1572
     */
1573
    public function testCopySubtree()
1574
    {
1575
        $repository = $this->getRepository();
1576
1577
        $mediaLocationId = $this->generateId('location', 43);
1578
        $demoDesignLocationId = $this->generateId('location', 56);
1579
        /* BEGIN: Use Case */
1580
        // $mediaLocationId is the ID of the "Media" page location in
1581
        // an eZ Publish demo installation
1582
1583
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1584
        // Publish demo installation
1585
1586
        // Load the location service
1587
        $locationService = $repository->getLocationService();
1588
1589
        // Load location to copy
1590
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1591
1592
        // Load new parent location
1593
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1594
1595
        // Copy location "Media" to "Demo Design"
1596
        $copiedLocation = $locationService->copySubtree(
1597
            $locationToCopy,
1598
            $newParentLocation
1599
        );
1600
        /* END: Use Case */
1601
1602
        $this->assertInstanceOf(
1603
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1604
            $copiedLocation
1605
        );
1606
1607
        $this->assertPropertiesCorrect(
1608
            array(
1609
                'depth' => $newParentLocation->depth + 1,
1610
                'parentLocationId' => $newParentLocation->id,
1611
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $copiedLocation->id) . '/',
1612
            ),
1613
            $copiedLocation
1614
        );
1615
1616
        $this->assertDefaultContentStates($copiedLocation->contentInfo);
1617
    }
1618
1619
    /**
1620
     * Test for the copySubtree() method.
1621
     *
1622
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1623
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1624
     */
1625
    public function testCopySubtreeWithAliases()
1626
    {
1627
        $repository = $this->getRepository();
1628
        $urlAliasService = $repository->getURLAliasService();
1629
1630
        // $mediaLocationId is the ID of the "Media" page location in
1631
        // an eZ Publish demo installation
1632
1633
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1634
        // Publish demo installation
1635
        $mediaLocationId = $this->generateId('location', 43);
1636
        $demoDesignLocationId = $this->generateId('location', 56);
1637
1638
        $locationService = $repository->getLocationService();
1639
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1640
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1641
1642
        $expectedSubItemAliases = [
1643
            '/Design/Plain-site/Media/Multimedia',
1644
            '/Design/Plain-site/Media/Images',
1645
            '/Design/Plain-site/Media/Files',
1646
        ];
1647
1648
        $this->assertAliasesBeforeCopy($urlAliasService, $expectedSubItemAliases);
1649
1650
        // Copy location "Media" to "Design"
1651
        $locationService->copySubtree(
1652
            $locationToCopy,
1653
            $newParentLocation
1654
        );
1655
1656
        $this->assertGeneratedAliases($urlAliasService, $expectedSubItemAliases);
1657
    }
1658
1659
    /**
1660
     * Asserts that given Content has default ContentStates.
1661
     *
1662
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
1663
     */
1664 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
1665
    {
1666
        $repository = $this->getRepository();
1667
        $objectStateService = $repository->getObjectStateService();
1668
1669
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
1670
1671
        foreach ($objectStateGroups as $objectStateGroup) {
1672
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
1673
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
1674
                // Only check the first object state which is the default one.
1675
                $this->assertEquals(
1676
                    $objectState,
1677
                    $contentState
1678
                );
1679
                break;
1680
            }
1681
        }
1682
    }
1683
1684
    /**
1685
     * Test for the copySubtree() method.
1686
     *
1687
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1688
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1689
     */
1690
    public function testCopySubtreeUpdatesSubtreeProperties()
1691
    {
1692
        $repository = $this->getRepository();
1693
        $locationService = $repository->getLocationService();
1694
1695
        $locationToCopy = $locationService->loadLocation($this->generateId('location', 43));
1696
1697
        // Load Subtree properties before copy
1698
        $expected = $this->loadSubtreeProperties($locationToCopy);
1699
1700
        $mediaLocationId = $this->generateId('location', 43);
1701
        $demoDesignLocationId = $this->generateId('location', 56);
1702
        /* BEGIN: Use Case */
1703
        // $mediaLocationId is the ID of the "Media" page location in
1704
        // an eZ Publish demo installation
1705
1706
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1707
        // Publish demo installation
1708
1709
        // Load the location service
1710
        $locationService = $repository->getLocationService();
1711
1712
        // Load location to copy
1713
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1714
1715
        // Load new parent location
1716
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1717
1718
        // Copy location "Media" to "Demo Design"
1719
        $copiedLocation = $locationService->copySubtree(
1720
            $locationToCopy,
1721
            $newParentLocation
1722
        );
1723
        /* END: Use Case */
1724
1725
        $beforeIds = array();
1726
        foreach ($expected as $properties) {
1727
            $beforeIds[] = $properties['id'];
1728
        }
1729
1730
        $this->refreshSearch($repository);
1731
1732
        // Load Subtree properties after copy
1733
        $actual = $this->loadSubtreeProperties($copiedLocation);
1734
1735
        $this->assertEquals(count($expected), count($actual));
1736
1737
        foreach ($actual as $properties) {
1738
            $this->assertNotContains($properties['id'], $beforeIds);
1739
            $this->assertStringStartsWith(
1740
                "{$newParentLocation->pathString}" . $this->parseId('location', $copiedLocation->id) . '/',
1741
                $properties['pathString']
1742
            );
1743
            $this->assertStringEndsWith(
1744
                '/' . $this->parseId('location', $properties['id']) . '/',
1745
                $properties['pathString']
1746
            );
1747
        }
1748
    }
1749
1750
    /**
1751
     * Test for the copySubtree() method.
1752
     *
1753
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1754
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1755
     */
1756
    public function testCopySubtreeIncrementsChildCountOfNewParent()
1757
    {
1758
        $repository = $this->getRepository();
1759
        $locationService = $repository->getLocationService();
1760
1761
        $childCountBefore = $locationService->getLocationChildCount($locationService->loadLocation(56));
1762
1763
        $mediaLocationId = $this->generateId('location', 43);
1764
        $demoDesignLocationId = $this->generateId('location', 56);
1765
        /* BEGIN: Use Case */
1766
        // $mediaLocationId is the ID of the "Media" page location in
1767
        // an eZ Publish demo installation
1768
1769
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1770
        // Publish demo installation
1771
1772
        // Load the location service
1773
        $locationService = $repository->getLocationService();
1774
1775
        // Load location to copy
1776
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1777
1778
        // Load new parent location
1779
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1780
1781
        // Copy location "Media" to "Demo Design"
1782
        $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...
1783
            $locationToCopy,
1784
            $newParentLocation
1785
        );
1786
        /* END: Use Case */
1787
1788
        $this->refreshSearch($repository);
1789
1790
        $childCountAfter = $locationService->getLocationChildCount($locationService->loadLocation($demoDesignLocationId));
1791
1792
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
1793
    }
1794
1795
    /**
1796
     * Test for the copySubtree() method.
1797
     *
1798
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1799
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1800
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1801
     */
1802
    public function testCopySubtreeThrowsInvalidArgumentException()
1803
    {
1804
        $repository = $this->getRepository();
1805
1806
        $communityLocationId = $this->generateId('location', 5);
1807
        /* BEGIN: Use Case */
1808
        // $communityLocationId is the ID of the "Community" page location in
1809
        // an eZ Publish demo installation
1810
1811
        // Load the location service
1812
        $locationService = $repository->getLocationService();
1813
1814
        // Load location to copy
1815
        $locationToCopy = $locationService->loadLocation($communityLocationId);
1816
1817
        // Use a child as new parent
1818
        $childLocations = $locationService->loadLocationChildren($locationToCopy)->locations;
1819
        $newParentLocation = end($childLocations);
1820
1821
        // This call will fail with an "InvalidArgumentException", because the
1822
        // new parent is a child location of the subtree to copy.
1823
        $locationService->copySubtree(
1824
            $locationToCopy,
1825
            $newParentLocation
0 ignored issues
show
Security Bug introduced by
It seems like $newParentLocation defined by end($childLocations) on line 1819 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...
1826
        );
1827
        /* END: Use Case */
1828
    }
1829
1830
    /**
1831
     * Test for the moveSubtree() method.
1832
     *
1833
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1834
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1835
     */
1836
    public function testMoveSubtree()
1837
    {
1838
        $repository = $this->getRepository();
1839
1840
        $mediaLocationId = $this->generateId('location', 43);
1841
        $demoDesignLocationId = $this->generateId('location', 56);
1842
        /* BEGIN: Use Case */
1843
        // $mediaLocationId is the ID of the "Media" page location in
1844
        // an eZ Publish demo installation
1845
1846
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1847
        // Publish demo installation
1848
1849
        // Load the location service
1850
        $locationService = $repository->getLocationService();
1851
1852
        // Load location to move
1853
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1854
1855
        // Load new parent location
1856
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1857
1858
        // Move location from "Home" to "Demo Design"
1859
        $locationService->moveSubtree(
1860
            $locationToMove,
1861
            $newParentLocation
1862
        );
1863
1864
        // Load moved location
1865
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1866
        /* END: Use Case */
1867
1868
        $this->assertPropertiesCorrect(
1869
            array(
1870
                'hidden' => false,
1871
                'invisible' => false,
1872
                'depth' => $newParentLocation->depth + 1,
1873
                'parentLocationId' => $newParentLocation->id,
1874
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
1875
            ),
1876
            $movedLocation
1877
        );
1878
    }
1879
1880
    /**
1881
     * Test for the moveSubtree() method.
1882
     *
1883
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1884
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
1885
     */
1886
    public function testMoveSubtreeHidden()
1887
    {
1888
        $repository = $this->getRepository();
1889
1890
        $mediaLocationId = $this->generateId('location', 43);
1891
        $demoDesignLocationId = $this->generateId('location', 56);
1892
        /* BEGIN: Use Case */
1893
        // $mediaLocationId is the ID of the "Media" page location in
1894
        // an eZ Publish demo installation
1895
1896
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1897
        // Publish demo installation
1898
1899
        // Load the location service
1900
        $locationService = $repository->getLocationService();
1901
1902
        // Load location to move
1903
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1904
1905
        // Load new parent location
1906
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1907
1908
        // Hide the target location before we move
1909
        $newParentLocation = $locationService->hideLocation($newParentLocation);
1910
1911
        // Move location from "Home" to "Demo Design"
1912
        $locationService->moveSubtree(
1913
            $locationToMove,
1914
            $newParentLocation
1915
        );
1916
1917
        // Load moved location
1918
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1919
        /* END: Use Case */
1920
1921
        $this->assertPropertiesCorrect(
1922
            array(
1923
                'hidden' => false,
1924
                'invisible' => true,
1925
                'depth' => $newParentLocation->depth + 1,
1926
                'parentLocationId' => $newParentLocation->id,
1927
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
1928
            ),
1929
            $movedLocation
1930
        );
1931
    }
1932
1933
    /**
1934
     * Test for the moveSubtree() method.
1935
     *
1936
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1937
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
1938
     */
1939
    public function testMoveSubtreeUpdatesSubtreeProperties()
1940
    {
1941
        $repository = $this->getRepository();
1942
        $locationService = $repository->getLocationService();
1943
1944
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
1945
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
1946
1947
        // Load Subtree properties before move
1948
        $expected = $this->loadSubtreeProperties($locationToMove);
1949
        foreach ($expected as $id => $properties) {
1950
            $expected[$id]['depth'] = $properties['depth'] + 2;
1951
            $expected[$id]['pathString'] = str_replace(
1952
                $locationToMove->pathString,
1953
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
1954
                $properties['pathString']
1955
            );
1956
        }
1957
1958
        $mediaLocationId = $this->generateId('location', 43);
1959
        $demoDesignLocationId = $this->generateId('location', 56);
1960
        /* BEGIN: Use Case */
1961
        // $mediaLocationId is the ID of the "Media" page location in
1962
        // an eZ Publish demo installation
1963
1964
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1965
        // Publish demo installation
1966
1967
        // Load the location service
1968
        $locationService = $repository->getLocationService();
1969
1970
        // Load location to move
1971
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1972
1973
        // Load new parent location
1974
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1975
1976
        // Move location from "Home" to "Demo Design"
1977
        $locationService->moveSubtree(
1978
            $locationToMove,
1979
            $newParentLocation
1980
        );
1981
1982
        // Load moved location
1983
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1984
        /* END: Use Case */
1985
1986
        $this->refreshSearch($repository);
1987
1988
        // Load Subtree properties after move
1989
        $actual = $this->loadSubtreeProperties($movedLocation);
1990
1991
        $this->assertEquals($expected, $actual);
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::testMoveSubtreeUpdatesSubtreeProperties
1999
     */
2000
    public function testMoveSubtreeUpdatesSubtreePropertiesHidden()
2001
    {
2002
        $repository = $this->getRepository();
2003
        $locationService = $repository->getLocationService();
2004
2005
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
2006
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
2007
2008
        // Hide the target location before we move
2009
        $newParentLocation = $locationService->hideLocation($newParentLocation);
2010
2011
        // Load Subtree properties before move
2012
        $expected = $this->loadSubtreeProperties($locationToMove);
2013
        foreach ($expected as $id => $properties) {
2014
            $expected[$id]['invisible'] = true;
2015
            $expected[$id]['depth'] = $properties['depth'] + 2;
2016
            $expected[$id]['pathString'] = str_replace(
2017
                $locationToMove->pathString,
2018
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
2019
                $properties['pathString']
2020
            );
2021
        }
2022
2023
        $mediaLocationId = $this->generateId('location', 43);
2024
        $demoDesignLocationId = $this->generateId('location', 56);
2025
        /* BEGIN: Use Case */
2026
        // $mediaLocationId is the ID of the "Media" page location in
2027
        // an eZ Publish demo installation
2028
2029
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2030
        // Publish demo installation
2031
2032
        // Load the location service
2033
        $locationService = $repository->getLocationService();
2034
2035
        // Load location to move
2036
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2037
2038
        // Load new parent location
2039
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2040
2041
        // Move location from "Home" to "Demo Design"
2042
        $locationService->moveSubtree(
2043
            $locationToMove,
2044
            $newParentLocation
2045
        );
2046
2047
        // Load moved location
2048
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2049
        /* END: Use Case */
2050
2051
        $this->refreshSearch($repository);
2052
2053
        // Load Subtree properties after move
2054
        $actual = $this->loadSubtreeProperties($movedLocation);
2055
2056
        $this->assertEquals($expected, $actual);
2057
    }
2058
2059
    /**
2060
     * Test for the moveSubtree() method.
2061
     *
2062
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2063
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2064
     */
2065 View Code Duplication
    public function testMoveSubtreeIncrementsChildCountOfNewParent()
2066
    {
2067
        $repository = $this->getRepository();
2068
        $locationService = $repository->getLocationService();
2069
2070
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
2071
2072
        // Load expected properties before move
2073
        $expected = $this->loadLocationProperties($newParentLocation);
2074
        $childCountBefore = $locationService->getLocationChildCount($newParentLocation);
2075
2076
        $mediaLocationId = $this->generateId('location', 43);
2077
        $demoDesignLocationId = $this->generateId('location', 56);
2078
        /* BEGIN: Use Case */
2079
        // $mediaLocationId is the ID of the "Media" page location in
2080
        // an eZ Publish demo installation
2081
2082
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2083
        // Publish demo installation
2084
2085
        // Load the location service
2086
        $locationService = $repository->getLocationService();
2087
2088
        // Load location to move
2089
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2090
2091
        // Load new parent location
2092
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2093
2094
        // Move location from "Home" to "Demo Design"
2095
        $locationService->moveSubtree(
2096
            $locationToMove,
2097
            $newParentLocation
2098
        );
2099
2100
        // Load moved location
2101
        $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...
2102
2103
        // Reload new parent location
2104
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2105
        /* END: Use Case */
2106
2107
        $this->refreshSearch($repository);
2108
2109
        // Load Subtree properties after move
2110
        $actual = $this->loadLocationProperties($newParentLocation);
2111
        $childCountAfter = $locationService->getLocationChildCount($newParentLocation);
2112
2113
        $this->assertEquals($expected, $actual);
2114
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
2115
    }
2116
2117
    /**
2118
     * Test for the moveSubtree() method.
2119
     *
2120
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2121
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2122
     */
2123 View Code Duplication
    public function testMoveSubtreeDecrementsChildCountOfOldParent()
2124
    {
2125
        $repository = $this->getRepository();
2126
        $locationService = $repository->getLocationService();
2127
2128
        $oldParentLocation = $locationService->loadLocation($this->generateId('location', 1));
2129
2130
        // Load expected properties before move
2131
        $expected = $this->loadLocationProperties($oldParentLocation);
2132
        $childCountBefore = $locationService->getLocationChildCount($oldParentLocation);
2133
2134
        $mediaLocationId = $this->generateId('location', 43);
2135
        $demoDesignLocationId = $this->generateId('location', 56);
2136
        /* BEGIN: Use Case */
2137
        // $mediaLocationId is the ID of the "Media" page location in
2138
        // an eZ Publish demo installation
2139
2140
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2141
        // Publish demo installation
2142
2143
        // Load the location service
2144
        $locationService = $repository->getLocationService();
2145
2146
        // Load location to move
2147
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2148
2149
        // Get the location id of the old parent
2150
        $oldParentLocationId = $locationToMove->parentLocationId;
2151
2152
        // Load new parent location
2153
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2154
2155
        // Move location from "Home" to "Demo Design"
2156
        $locationService->moveSubtree(
2157
            $locationToMove,
2158
            $newParentLocation
2159
        );
2160
2161
        // Reload old parent location
2162
        $oldParentLocation = $locationService->loadLocation($oldParentLocationId);
2163
        /* END: Use Case */
2164
2165
        $this->refreshSearch($repository);
2166
2167
        // Load Subtree properties after move
2168
        $actual = $this->loadLocationProperties($oldParentLocation);
2169
        $childCountAfter = $locationService->getLocationChildCount($oldParentLocation);
2170
2171
        $this->assertEquals($expected, $actual);
2172
        $this->assertEquals($childCountBefore - 1, $childCountAfter);
2173
    }
2174
2175
    /**
2176
     * Test for the moveSubtree() method.
2177
     *
2178
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2179
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2180
     */
2181
    public function testMoveSubtreeThrowsInvalidArgumentException()
2182
    {
2183
        $repository = $this->getRepository();
2184
        $mediaLocationId = $this->generateId('location', 43);
2185
        $multimediaLocationId = $this->generateId('location', 53);
2186
2187
        /* BEGIN: Use Case */
2188
        // $mediaLocationId is the ID of the "Media" page location in
2189
        // an eZ Publish demo installation
2190
2191
        // $multimediaLocationId is the ID of the "Multimedia" page location in an eZ
2192
        // Publish demo installation
2193
2194
        // Load the location service
2195
        $locationService = $repository->getLocationService();
2196
2197
        // Load location to move
2198
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2199
2200
        // Load new parent location
2201
        $newParentLocation = $locationService->loadLocation($multimediaLocationId);
2202
2203
        // Throws an exception because new parent location is placed below location to move
2204
        $locationService->moveSubtree(
2205
            $locationToMove,
2206
            $newParentLocation
2207
        );
2208
        /* END: Use Case */
2209
    }
2210
2211
    /**
2212
     * Loads properties from all locations in the $location's subtree.
2213
     *
2214
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2215
     * @param array $properties
2216
     *
2217
     * @return array
2218
     */
2219
    private function loadSubtreeProperties(Location $location, array $properties = array())
2220
    {
2221
        $locationService = $this->getRepository()->getLocationService();
2222
2223
        foreach ($locationService->loadLocationChildren($location)->locations as $childLocation) {
2224
            $properties[] = $this->loadLocationProperties($childLocation);
2225
2226
            $properties = $this->loadSubtreeProperties($childLocation, $properties);
2227
        }
2228
2229
        return $properties;
2230
    }
2231
2232
    /**
2233
     * Loads assertable properties from the given location.
2234
     *
2235
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2236
     * @param mixed[] $overwrite
2237
     *
2238
     * @return array
2239
     */
2240
    private function loadLocationProperties(Location $location, array $overwrite = array())
2241
    {
2242
        return array_merge(
2243
            array(
2244
                'id' => $location->id,
2245
                'depth' => $location->depth,
2246
                'parentLocationId' => $location->parentLocationId,
2247
                'pathString' => $location->pathString,
2248
                'remoteId' => $location->remoteId,
2249
                'hidden' => $location->hidden,
2250
                'invisible' => $location->invisible,
2251
                'priority' => $location->priority,
2252
                'sortField' => $location->sortField,
2253
                'sortOrder' => $location->sortOrder,
2254
            ),
2255
            $overwrite
2256
        );
2257
    }
2258
2259
    /**
2260
     * Assert generated aliases to expected alias return.
2261
     *
2262
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
2263
     * @param array $expectedAliases
2264
     */
2265
    protected function assertGeneratedAliases($urlAliasService, array $expectedAliases)
2266
    {
2267
        foreach ($expectedAliases as $expectedAlias) {
2268
            $urlAlias = $urlAliasService->lookup($expectedAlias);
2269
            $this->assertPropertiesCorrect(['type' => 0], $urlAlias);
2270
        }
2271
    }
2272
2273
    /**
2274
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
2275
     * @param array $expectedSubItemAliases
2276
     */
2277
    private function assertAliasesBeforeCopy($urlAliasService, array $expectedSubItemAliases)
2278
    {
2279
        foreach ($expectedSubItemAliases as $aliasUrl) {
2280
            try {
2281
                $urlAliasService->lookup($aliasUrl);
2282
                $this->fail('We didn\'t expect to find alias, but it was found');
2283
            } catch (\Exception $e) {
2284
                $this->assertTrue(true); // OK - alias was not found
2285
            }
2286
        }
2287
    }
2288
2289
    /**
2290
     * Create and publish Content with the given parent Location.
2291
     *
2292
     * @param string $contentName
2293
     * @param int $parentLocationId
2294
     *
2295
     * @return \eZ\Publish\API\Repository\Values\Content\Content published Content
2296
     */
2297 View Code Duplication
    private function publishContentWithParentLocation($contentName, $parentLocationId)
2298
    {
2299
        $repository = $this->getRepository(false);
2300
        $locationService = $repository->getLocationService();
2301
2302
        $contentService = $repository->getContentService();
2303
        $contentTypeService = $repository->getContentTypeService();
2304
2305
        $contentCreateStruct = $contentService->newContentCreateStruct(
2306
            $contentTypeService->loadContentTypeByIdentifier('folder'),
2307
            'eng-US'
2308
        );
2309
        $contentCreateStruct->setField('name', $contentName);
2310
        $contentDraft = $contentService->createContent(
2311
            $contentCreateStruct,
2312
            [
2313
                $locationService->newLocationCreateStruct($parentLocationId),
2314
            ]
2315
        );
2316
2317
        return $contentService->publishVersion($contentDraft->versionInfo);
2318
    }
2319
}
2320