Completed
Push — master ( 7c0e42...8c347f )
by André
14:17
created

testCreateLocationInTransactionWithRollback()   B

Complexity

Conditions 3
Paths 5

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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