Completed
Push — travis_trusty ( a0f759...e78e49 )
by André
37:53 queued 08:54
created

LocationServiceTest::testLoadLocationChildren()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 20
Ratio 100 %

Importance

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