Completed
Push — ezp-24848_ct_removal_content_i... ( 8854bf...ebe765 )
by
unknown
21:25
created

testMoveSubtreeUpdatesSubtreePropertiesHidden()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 58
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 58
rs 9.6391
cc 2
eloc 26
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
984
     */
985
    public function testUpdateLocationThrowsInvalidArgumentException()
986
    {
987
        $repository = $this->getRepository();
988
989
        $locationId = $this->generateId('location', 5);
990
        /* BEGIN: Use Case */
991
        // $locationId is the ID of an existing location
992
        $locationService = $repository->getLocationService();
993
994
        $originalLocation = $locationService->loadLocation($locationId);
995
996
        $updateStruct = $locationService->newLocationUpdateStruct();
997
        // Remote ID of an existing location
998
        $updateStruct->remoteId = 'f3e90596361e31d496d4026eb624c983';
999
1000
        // Throws exception, since remote ID is already taken
1001
        $locationService->updateLocation($originalLocation, $updateStruct);
1002
        /* END: Use Case */
1003
    }
1004
1005
    /**
1006
     * Test for the updateLocation() method.
1007
     * Ref EZP-23302: Update Location fails if no change is performed with the update.
1008
     *
1009
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1010
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1011
     */
1012
    public function testUpdateLocationTwice()
1013
    {
1014
        $repository = $this->getRepository();
1015
1016
        $locationId = $this->generateId('location', 5);
1017
        /* BEGIN: Use Case */
1018
        $locationService = $repository->getLocationService();
1019
        $repository->setCurrentUser($repository->getUserService()->loadUser(14));
1020
1021
        $originalLocation = $locationService->loadLocation($locationId);
1022
1023
        $updateStruct = $locationService->newLocationUpdateStruct();
1024
        $updateStruct->priority = 42;
1025
1026
        $updatedLocation = $locationService->updateLocation($originalLocation, $updateStruct);
1027
1028
        // Repeated update with the same, unchanged struct
1029
        $secondUpdatedLocation = $locationService->updateLocation($updatedLocation, $updateStruct);
1030
        /* END: Use Case */
1031
1032
        $this->assertEquals($updatedLocation->priority, 42);
1033
        $this->assertEquals($secondUpdatedLocation->priority, 42);
1034
    }
1035
1036
    /**
1037
     * Test for the swapLocation() method.
1038
     *
1039
     * @see \eZ\Publish\API\Repository\LocationService::swapLocation()
1040
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1041
     */
1042
    public function testSwapLocation()
1043
    {
1044
        $repository = $this->getRepository();
1045
        $locationService = $repository->getLocationService();
1046
1047
        $mediaLocationId = $this->generateId('location', 43);
1048
        $demoDesignLocationId = $this->generateId('location', 56);
1049
1050
        $mediaContentInfo = $locationService->loadLocation($mediaLocationId)->getContentInfo();
1051
        $demoDesignContentInfo = $locationService->loadLocation($demoDesignLocationId)->getContentInfo();
1052
1053
        /* BEGIN: Use Case */
1054
        // $mediaLocationId is the ID of the "Media" page location in
1055
        // an eZ Publish demo installation
1056
1057
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1058
        // Publish demo installation
1059
1060
        // Load the location service
1061
        $locationService = $repository->getLocationService();
1062
1063
        $mediaLocation = $locationService->loadLocation($mediaLocationId);
1064
        $demoDesignLocation = $locationService->loadLocation($demoDesignLocationId);
1065
1066
        // Swaps the content referred to by the locations
1067
        $locationService->swapLocation($mediaLocation, $demoDesignLocation);
1068
        /* END: Use Case */
1069
1070
        $this->assertEquals(
1071
            $mediaContentInfo->id,
1072
            $locationService->loadLocation($demoDesignLocationId)->getContentInfo()->id
1073
        );
1074
        $this->assertEquals(
1075
            $demoDesignContentInfo->id,
1076
            $locationService->loadLocation($mediaLocationId)->getContentInfo()->id
1077
        );
1078
    }
1079
1080
    /**
1081
     * Test for the hideLocation() method.
1082
     *
1083
     * @see \eZ\Publish\API\Repository\LocationService::hideLocation()
1084
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1085
     */
1086
    public function testHideLocation()
1087
    {
1088
        $repository = $this->getRepository();
1089
1090
        $locationId = $this->generateId('location', 5);
1091
        /* BEGIN: Use Case */
1092
        // $locationId is the ID of an existing location
1093
        $locationService = $repository->getLocationService();
1094
1095
        $visibleLocation = $locationService->loadLocation($locationId);
1096
1097
        $hiddenLocation = $locationService->hideLocation($visibleLocation);
1098
        /* END: Use Case */
1099
1100
        $this->assertInstanceOf(
1101
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1102
            $hiddenLocation
1103
        );
1104
1105
        $this->assertTrue(
1106
            $hiddenLocation->hidden,
1107
            sprintf(
1108
                'Location with ID "%s" not hidden.',
1109
                $hiddenLocation->id
1110
            )
1111
        );
1112
1113
        $this->refreshSearch($repository);
1114
1115
        foreach ($locationService->loadLocationChildren($hiddenLocation)->locations as $child) {
1116
            $this->assertSubtreeProperties(
1117
                array('invisible' => true),
1118
                $child
1119
            );
1120
        }
1121
    }
1122
1123
    /**
1124
     * Assert that $expectedValues are set in the subtree starting at $location.
1125
     *
1126
     * @param array $expectedValues
1127
     * @param Location $location
1128
     */
1129
    protected function assertSubtreeProperties(array $expectedValues, Location $location, $stopId = null)
1130
    {
1131
        $repository = $this->getRepository();
1132
        $locationService = $repository->getLocationService();
1133
1134
        if ($location->id === $stopId) {
1135
            return;
1136
        }
1137
1138
        foreach ($expectedValues as $propertyName => $propertyValue) {
1139
            $this->assertEquals(
1140
                $propertyValue,
1141
                $location->$propertyName
1142
            );
1143
1144
            foreach ($locationService->loadLocationChildren($location)->locations as $child) {
1145
                $this->assertSubtreeProperties($expectedValues, $child);
1146
            }
1147
        }
1148
    }
1149
1150
    /**
1151
     * Test for the unhideLocation() method.
1152
     *
1153
     * @see \eZ\Publish\API\Repository\LocationService::unhideLocation()
1154
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testHideLocation
1155
     */
1156
    public function testUnhideLocation()
1157
    {
1158
        $repository = $this->getRepository();
1159
1160
        $locationId = $this->generateId('location', 5);
1161
        /* BEGIN: Use Case */
1162
        // $locationId is the ID of an existing location
1163
        $locationService = $repository->getLocationService();
1164
1165
        $visibleLocation = $locationService->loadLocation($locationId);
1166
        $hiddenLocation = $locationService->hideLocation($visibleLocation);
1167
1168
        $unHiddenLocation = $locationService->unhideLocation($hiddenLocation);
1169
        /* END: Use Case */
1170
1171
        $this->assertInstanceOf(
1172
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1173
            $unHiddenLocation
1174
        );
1175
1176
        $this->assertFalse(
1177
            $unHiddenLocation->hidden,
1178
            sprintf(
1179
                'Location with ID "%s" not unhidden.',
1180
                $unHiddenLocation->id
1181
            )
1182
        );
1183
1184
        $this->refreshSearch($repository);
1185
1186
        foreach ($locationService->loadLocationChildren($unHiddenLocation)->locations as $child) {
1187
            $this->assertSubtreeProperties(
1188
                array('invisible' => false),
1189
                $child
1190
            );
1191
        }
1192
    }
1193
1194
    /**
1195
     * Test for the unhideLocation() method.
1196
     *
1197
     * @see \eZ\Publish\API\Repository\LocationService::unhideLocation()
1198
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testUnhideLocation
1199
     */
1200
    public function testUnhideLocationNotUnhidesHiddenSubtree()
1201
    {
1202
        $repository = $this->getRepository();
1203
1204
        $higherLocationId = $this->generateId('location', 5);
1205
        $lowerLocationId = $this->generateId('location', 13);
1206
        /* BEGIN: Use Case */
1207
        // $higherLocationId is the ID of a location
1208
        // $lowerLocationId is the ID of a location below $higherLocationId
1209
        $locationService = $repository->getLocationService();
1210
1211
        $higherLocation = $locationService->loadLocation($higherLocationId);
1212
        $hiddenHigherLocation = $locationService->hideLocation($higherLocation);
1213
1214
        $lowerLocation = $locationService->loadLocation($lowerLocationId);
1215
        $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...
1216
1217
        $unHiddenHigherLocation = $locationService->unhideLocation($hiddenHigherLocation);
1218
        /* END: Use Case */
1219
1220
        $this->assertInstanceOf(
1221
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1222
            $unHiddenHigherLocation
1223
        );
1224
1225
        $this->assertFalse(
1226
            $unHiddenHigherLocation->hidden,
1227
            sprintf(
1228
                'Location with ID "%s" not unhidden.',
1229
                $unHiddenHigherLocation->id
1230
            )
1231
        );
1232
1233
        $this->refreshSearch($repository);
1234
1235
        foreach ($locationService->loadLocationChildren($unHiddenHigherLocation)->locations as $child) {
1236
            $this->assertSubtreeProperties(
1237
                array('invisible' => false),
1238
                $child,
1239
                $this->generateId('location', 13)
1240
            );
1241
        }
1242
1243
        $stillHiddenLocation = $locationService->loadLocation($this->generateId('location', 13));
1244
        $this->assertTrue(
1245
            $stillHiddenLocation->hidden,
1246
            sprintf(
1247
                'Hidden sub-location with ID %s accidentally unhidden.',
1248
                $stillHiddenLocation->id
1249
            )
1250
        );
1251
        foreach ($locationService->loadLocationChildren($stillHiddenLocation)->locations as $child) {
1252
            $this->assertSubtreeProperties(
1253
                array('invisible' => true),
1254
                $child
1255
            );
1256
        }
1257
    }
1258
1259
    /**
1260
     * Test for the deleteLocation() method.
1261
     *
1262
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1263
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1264
     */
1265
    public function testDeleteLocation()
1266
    {
1267
        $repository = $this->getRepository();
1268
1269
        $mediaLocationId = $this->generateId('location', 43);
1270
        /* BEGIN: Use Case */
1271
        // $mediaLocationId is the ID of the location of the
1272
        // "Media" location in an eZ Publish demo installation
1273
        $locationService = $repository->getLocationService();
1274
1275
        $location = $locationService->loadLocation($mediaLocationId);
1276
1277
        $locationService->deleteLocation($location);
1278
        /* END: Use Case */
1279
1280
        try {
1281
            $locationService->loadLocation($mediaLocationId);
1282
            $this->fail("Location $mediaLocationId not deleted.");
1283
        } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1284
        }
1285
1286
        // The following IDs are IDs of child locations of $mediaLocationId location
1287
        // ( Media/Images, Media/Files, Media/Multimedia respectively )
1288
        foreach (array(51, 52, 53) as $childLocationId) {
1289
            try {
1290
                $locationService->loadLocation($this->generateId('location', $childLocationId));
1291
                $this->fail("Location $childLocationId not deleted.");
1292
            } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1293
            }
1294
        }
1295
1296
        // The following IDs are IDs of content below $mediaLocationId location
1297
        // ( Media/Images, Media/Files, Media/Multimedia respectively )
1298
        $contentService = $this->getRepository()->getContentService();
1299
        foreach (array(49, 50, 51) as $childContentId) {
1300
            try {
1301
                $contentService->loadContentInfo($this->generateId('object', $childContentId));
1302
                $this->fail("Content $childContentId not deleted.");
1303
            } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1304
            }
1305
        }
1306
    }
1307
1308
    /**
1309
     * Test for the deleteLocation() method.
1310
     *
1311
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1312
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testDeleteLocation
1313
     */
1314
    public function testDeleteLocationDecrementsChildCountOnParent()
1315
    {
1316
        $repository = $this->getRepository();
1317
1318
        $mediaLocationId = $this->generateId('location', 43);
1319
        /* BEGIN: Use Case */
1320
        // $mediaLocationId is the ID of the location of the
1321
        // "Media" location in an eZ Publish demo installation
1322
1323
        $locationService = $repository->getLocationService();
1324
1325
        // Load the current the user group location
1326
        $location = $locationService->loadLocation($mediaLocationId);
1327
1328
        // Load the parent location
1329
        $parentLocation = $locationService->loadLocation(
1330
            $location->parentLocationId
1331
        );
1332
1333
        // Get child count
1334
        $childCountBefore = $locationService->getLocationChildCount($parentLocation);
1335
1336
        // Delete the user group location
1337
        $locationService->deleteLocation($location);
1338
1339
        $this->refreshSearch($repository);
1340
1341
        // Reload parent location
1342
        $parentLocation = $locationService->loadLocation(
1343
            $location->parentLocationId
1344
        );
1345
1346
        // This will be $childCountBefore - 1
1347
        $childCountAfter = $locationService->getLocationChildCount($parentLocation);
1348
        /* END: Use Case */
1349
1350
        $this->assertEquals($childCountBefore - 1, $childCountAfter);
1351
    }
1352
1353
    /**
1354
     * Test for the deleteLocation() method.
1355
     *
1356
     * Related issue: EZP-21904
1357
     *
1358
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1359
     * @expectedException eZ\Publish\API\Repository\Exceptions\NotFoundException
1360
     */
1361
    public function testDeleteContentObjectLastLocation()
1362
    {
1363
        $repository = $this->getRepository();
1364
1365
        /* BEGIN: Use case */
1366
        $contentService = $repository->getContentService();
1367
        $locationService = $repository->getLocationService();
1368
        $contentTypeService = $repository->getContentTypeService();
1369
        $urlAliasService = $repository->getURLAliasService();
1370
1371
        // prepare Content object
1372
        $createStruct = $contentService->newContentCreateStruct(
1373
            $contentTypeService->loadContentTypeByIdentifier('folder'),
1374
            'eng-GB'
1375
        );
1376
        $createStruct->setField('name', 'Test folder');
1377
1378
        // creata Content object
1379
        $content = $contentService->publishVersion(
1380
            $contentService->createContent(
1381
                $createStruct,
1382
                array($locationService->newLocationCreateStruct(2))
1383
            )->versionInfo
1384
        );
1385
1386
        // delete location
1387
        $locationService->deleteLocation(
1388
            $locationService->loadLocation(
1389
                $urlAliasService->lookup('/Test-folder')->destination
1390
            )
1391
        );
1392
1393
        // this should throw a not found exception
1394
        $contentService->loadContent($content->versionInfo->contentInfo->id);
1395
        /* END: Use case*/
1396
    }
1397
1398
    /**
1399
     * Test for the copySubtree() method.
1400
     *
1401
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1402
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1403
     */
1404
    public function testCopySubtree()
1405
    {
1406
        $repository = $this->getRepository();
1407
1408
        $mediaLocationId = $this->generateId('location', 43);
1409
        $demoDesignLocationId = $this->generateId('location', 56);
1410
        /* BEGIN: Use Case */
1411
        // $mediaLocationId is the ID of the "Media" page location in
1412
        // an eZ Publish demo installation
1413
1414
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1415
        // Publish demo installation
1416
1417
        // Load the location service
1418
        $locationService = $repository->getLocationService();
1419
1420
        // Load location to copy
1421
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1422
1423
        // Load new parent location
1424
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1425
1426
        // Copy location "Media" to "Demo Design"
1427
        $copiedLocation = $locationService->copySubtree(
1428
            $locationToCopy,
1429
            $newParentLocation
1430
        );
1431
        /* END: Use Case */
1432
1433
        $this->assertInstanceOf(
1434
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1435
            $copiedLocation
1436
        );
1437
1438
        $this->assertPropertiesCorrect(
1439
            array(
1440
                'depth' => $newParentLocation->depth + 1,
1441
                'parentLocationId' => $newParentLocation->id,
1442
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $copiedLocation->id) . '/',
1443
            ),
1444
            $copiedLocation
1445
        );
1446
1447
        $this->assertDefaultContentStates($copiedLocation->contentInfo);
1448
    }
1449
1450
    /**
1451
     * Asserts that given Content has default ContentStates.
1452
     *
1453
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
1454
     */
1455 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
1456
    {
1457
        $repository = $this->getRepository();
1458
        $objectStateService = $repository->getObjectStateService();
1459
1460
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
1461
1462
        foreach ($objectStateGroups as $objectStateGroup) {
1463
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
1464
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
1465
                // Only check the first object state which is the default one.
1466
                $this->assertEquals(
1467
                    $objectState,
1468
                    $contentState
1469
                );
1470
                break;
1471
            }
1472
        }
1473
    }
1474
1475
    /**
1476
     * Test for the copySubtree() method.
1477
     *
1478
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1479
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1480
     */
1481
    public function testCopySubtreeUpdatesSubtreeProperties()
1482
    {
1483
        $repository = $this->getRepository();
1484
        $locationService = $repository->getLocationService();
1485
1486
        $locationToCopy = $locationService->loadLocation($this->generateId('location', 43));
1487
1488
        // Load Subtree properties before copy
1489
        $expected = $this->loadSubtreeProperties($locationToCopy);
1490
1491
        $mediaLocationId = $this->generateId('location', 43);
1492
        $demoDesignLocationId = $this->generateId('location', 56);
1493
        /* BEGIN: Use Case */
1494
        // $mediaLocationId is the ID of the "Media" page location in
1495
        // an eZ Publish demo installation
1496
1497
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1498
        // Publish demo installation
1499
1500
        // Load the location service
1501
        $locationService = $repository->getLocationService();
1502
1503
        // Load location to copy
1504
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1505
1506
        // Load new parent location
1507
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1508
1509
        // Copy location "Media" to "Demo Design"
1510
        $copiedLocation = $locationService->copySubtree(
1511
            $locationToCopy,
1512
            $newParentLocation
1513
        );
1514
        /* END: Use Case */
1515
1516
        $beforeIds = array();
1517
        foreach ($expected as $properties) {
1518
            $beforeIds[] = $properties['id'];
1519
        }
1520
1521
        $this->refreshSearch($repository);
1522
1523
        // Load Subtree properties after copy
1524
        $actual = $this->loadSubtreeProperties($copiedLocation);
1525
1526
        $this->assertEquals(count($expected), count($actual));
1527
1528
        foreach ($actual as $properties) {
1529
            $this->assertNotContains($properties['id'], $beforeIds);
1530
            $this->assertStringStartsWith(
1531
                "{$newParentLocation->pathString}" . $this->parseId('location', $copiedLocation->id) . '/',
1532
                $properties['pathString']
1533
            );
1534
            $this->assertStringEndsWith(
1535
                '/' . $this->parseId('location', $properties['id']) . '/',
1536
                $properties['pathString']
1537
            );
1538
        }
1539
    }
1540
1541
    /**
1542
     * Test for the copySubtree() method.
1543
     *
1544
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1545
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1546
     */
1547
    public function testCopySubtreeIncrementsChildCountOfNewParent()
1548
    {
1549
        $repository = $this->getRepository();
1550
        $locationService = $repository->getLocationService();
1551
1552
        $childCountBefore = $locationService->getLocationChildCount($locationService->loadLocation(56));
1553
1554
        $mediaLocationId = $this->generateId('location', 43);
1555
        $demoDesignLocationId = $this->generateId('location', 56);
1556
        /* BEGIN: Use Case */
1557
        // $mediaLocationId is the ID of the "Media" page location in
1558
        // an eZ Publish demo installation
1559
1560
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1561
        // Publish demo installation
1562
1563
        // Load the location service
1564
        $locationService = $repository->getLocationService();
1565
1566
        // Load location to copy
1567
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1568
1569
        // Load new parent location
1570
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1571
1572
        // Copy location "Media" to "Demo Design"
1573
        $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...
1574
            $locationToCopy,
1575
            $newParentLocation
1576
        );
1577
        /* END: Use Case */
1578
1579
        $this->refreshSearch($repository);
1580
1581
        $childCountAfter = $locationService->getLocationChildCount($locationService->loadLocation($demoDesignLocationId));
1582
1583
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
1584
    }
1585
1586
    /**
1587
     * Test for the copySubtree() method.
1588
     *
1589
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1590
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1591
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1592
     */
1593
    public function testCopySubtreeThrowsInvalidArgumentException()
1594
    {
1595
        $repository = $this->getRepository();
1596
1597
        $communityLocationId = $this->generateId('location', 5);
1598
        /* BEGIN: Use Case */
1599
        // $communityLocationId is the ID of the "Community" page location in
1600
        // an eZ Publish demo installation
1601
1602
        // Load the location service
1603
        $locationService = $repository->getLocationService();
1604
1605
        // Load location to copy
1606
        $locationToCopy = $locationService->loadLocation($communityLocationId);
1607
1608
        // Use a child as new parent
1609
        $childLocations = $locationService->loadLocationChildren($locationToCopy)->locations;
1610
        $newParentLocation = end($childLocations);
1611
1612
        // This call will fail with an "InvalidArgumentException", because the
1613
        // new parent is a child location of the subtree to copy.
1614
        $locationService->copySubtree(
1615
            $locationToCopy,
1616
            $newParentLocation
0 ignored issues
show
Security Bug introduced by
It seems like $newParentLocation defined by end($childLocations) on line 1610 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...
1617
        );
1618
        /* END: Use Case */
1619
    }
1620
1621
    /**
1622
     * Test for the moveSubtree() method.
1623
     *
1624
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1625
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1626
     */
1627
    public function testMoveSubtree()
1628
    {
1629
        $repository = $this->getRepository();
1630
1631
        $mediaLocationId = $this->generateId('location', 43);
1632
        $demoDesignLocationId = $this->generateId('location', 56);
1633
        /* BEGIN: Use Case */
1634
        // $mediaLocationId is the ID of the "Media" page location in
1635
        // an eZ Publish demo installation
1636
1637
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1638
        // Publish demo installation
1639
1640
        // Load the location service
1641
        $locationService = $repository->getLocationService();
1642
1643
        // Load location to move
1644
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1645
1646
        // Load new parent location
1647
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1648
1649
        // Move location from "Home" to "Demo Design"
1650
        $locationService->moveSubtree(
1651
            $locationToMove,
1652
            $newParentLocation
1653
        );
1654
1655
        // Load moved location
1656
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1657
        /* END: Use Case */
1658
1659
        $this->assertPropertiesCorrect(
1660
            array(
1661
                'hidden' => false,
1662
                'invisible' => false,
1663
                'depth' => $newParentLocation->depth + 1,
1664
                'parentLocationId' => $newParentLocation->id,
1665
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
1666
            ),
1667
            $movedLocation
1668
        );
1669
    }
1670
1671
    /**
1672
     * Test for the moveSubtree() method.
1673
     *
1674
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1675
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
1676
     */
1677
    public function testMoveSubtreeHidden()
1678
    {
1679
        $repository = $this->getRepository();
1680
1681
        $mediaLocationId = $this->generateId('location', 43);
1682
        $demoDesignLocationId = $this->generateId('location', 56);
1683
        /* BEGIN: Use Case */
1684
        // $mediaLocationId is the ID of the "Media" page location in
1685
        // an eZ Publish demo installation
1686
1687
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1688
        // Publish demo installation
1689
1690
        // Load the location service
1691
        $locationService = $repository->getLocationService();
1692
1693
        // Load location to move
1694
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1695
1696
        // Load new parent location
1697
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1698
1699
        // Hide the target location before we move
1700
        $newParentLocation = $locationService->hideLocation($newParentLocation);
1701
1702
        // Move location from "Home" to "Demo Design"
1703
        $locationService->moveSubtree(
1704
            $locationToMove,
1705
            $newParentLocation
1706
        );
1707
1708
        // Load moved location
1709
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1710
        /* END: Use Case */
1711
1712
        $this->assertPropertiesCorrect(
1713
            array(
1714
                'hidden' => false,
1715
                'invisible' => true,
1716
                'depth' => $newParentLocation->depth + 1,
1717
                'parentLocationId' => $newParentLocation->id,
1718
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
1719
            ),
1720
            $movedLocation
1721
        );
1722
    }
1723
1724
    /**
1725
     * Test for the moveSubtree() method.
1726
     *
1727
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1728
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
1729
     */
1730
    public function testMoveSubtreeUpdatesSubtreeProperties()
1731
    {
1732
        $repository = $this->getRepository();
1733
        $locationService = $repository->getLocationService();
1734
1735
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
1736
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
1737
1738
        // Load Subtree properties before move
1739
        $expected = $this->loadSubtreeProperties($locationToMove);
1740
        foreach ($expected as $id => $properties) {
1741
            $expected[$id]['depth'] = $properties['depth'] + 2;
1742
            $expected[$id]['pathString'] = str_replace(
1743
                $locationToMove->pathString,
1744
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
1745
                $properties['pathString']
1746
            );
1747
        }
1748
1749
        $mediaLocationId = $this->generateId('location', 43);
1750
        $demoDesignLocationId = $this->generateId('location', 56);
1751
        /* BEGIN: Use Case */
1752
        // $mediaLocationId is the ID of the "Media" page location in
1753
        // an eZ Publish demo installation
1754
1755
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1756
        // Publish demo installation
1757
1758
        // Load the location service
1759
        $locationService = $repository->getLocationService();
1760
1761
        // Load location to move
1762
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1763
1764
        // Load new parent location
1765
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1766
1767
        // Move location from "Home" to "Demo Design"
1768
        $locationService->moveSubtree(
1769
            $locationToMove,
1770
            $newParentLocation
1771
        );
1772
1773
        // Load moved location
1774
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1775
        /* END: Use Case */
1776
1777
        $this->refreshSearch($repository);
1778
1779
        // Load Subtree properties after move
1780
        $actual = $this->loadSubtreeProperties($movedLocation);
1781
1782
        $this->assertEquals($expected, $actual);
1783
    }
1784
1785
    /**
1786
     * Test for the moveSubtree() method.
1787
     *
1788
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1789
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtreeUpdatesSubtreeProperties
1790
     */
1791
    public function testMoveSubtreeUpdatesSubtreePropertiesHidden()
1792
    {
1793
        $repository = $this->getRepository();
1794
        $locationService = $repository->getLocationService();
1795
1796
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
1797
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
1798
1799
        // Hide the target location before we move
1800
        $newParentLocation = $locationService->hideLocation($newParentLocation);
1801
1802
        // Load Subtree properties before move
1803
        $expected = $this->loadSubtreeProperties($locationToMove);
1804
        foreach ($expected as $id => $properties) {
1805
            $expected[$id]['invisible'] = true;
1806
            $expected[$id]['depth'] = $properties['depth'] + 2;
1807
            $expected[$id]['pathString'] = str_replace(
1808
                $locationToMove->pathString,
1809
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
1810
                $properties['pathString']
1811
            );
1812
        }
1813
1814
        $mediaLocationId = $this->generateId('location', 43);
1815
        $demoDesignLocationId = $this->generateId('location', 56);
1816
        /* BEGIN: Use Case */
1817
        // $mediaLocationId is the ID of the "Media" page location in
1818
        // an eZ Publish demo installation
1819
1820
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1821
        // Publish demo installation
1822
1823
        // Load the location service
1824
        $locationService = $repository->getLocationService();
1825
1826
        // Load location to move
1827
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1828
1829
        // Load new parent location
1830
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1831
1832
        // Move location from "Home" to "Demo Design"
1833
        $locationService->moveSubtree(
1834
            $locationToMove,
1835
            $newParentLocation
1836
        );
1837
1838
        // Load moved location
1839
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1840
        /* END: Use Case */
1841
1842
        $this->refreshSearch($repository);
1843
1844
        // Load Subtree properties after move
1845
        $actual = $this->loadSubtreeProperties($movedLocation);
1846
1847
        $this->assertEquals($expected, $actual);
1848
    }
1849
1850
    /**
1851
     * Test for the moveSubtree() method.
1852
     *
1853
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1854
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
1855
     */
1856 View Code Duplication
    public function testMoveSubtreeIncrementsChildCountOfNewParent()
1857
    {
1858
        $repository = $this->getRepository();
1859
        $locationService = $repository->getLocationService();
1860
1861
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
1862
1863
        // Load expected properties before move
1864
        $expected = $this->loadLocationProperties($newParentLocation);
1865
        $childCountBefore = $locationService->getLocationChildCount($newParentLocation);
1866
1867
        $mediaLocationId = $this->generateId('location', 43);
1868
        $demoDesignLocationId = $this->generateId('location', 56);
1869
        /* BEGIN: Use Case */
1870
        // $mediaLocationId is the ID of the "Media" page location in
1871
        // an eZ Publish demo installation
1872
1873
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1874
        // Publish demo installation
1875
1876
        // Load the location service
1877
        $locationService = $repository->getLocationService();
1878
1879
        // Load location to move
1880
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1881
1882
        // Load new parent location
1883
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1884
1885
        // Move location from "Home" to "Demo Design"
1886
        $locationService->moveSubtree(
1887
            $locationToMove,
1888
            $newParentLocation
1889
        );
1890
1891
        // Load moved location
1892
        $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...
1893
1894
        // Reload new parent location
1895
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1896
        /* END: Use Case */
1897
1898
        $this->refreshSearch($repository);
1899
1900
        // Load Subtree properties after move
1901
        $actual = $this->loadLocationProperties($newParentLocation);
1902
        $childCountAfter = $locationService->getLocationChildCount($newParentLocation);
1903
1904
        $this->assertEquals($expected, $actual);
1905
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
1906
    }
1907
1908
    /**
1909
     * Test for the moveSubtree() method.
1910
     *
1911
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1912
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
1913
     */
1914 View Code Duplication
    public function testMoveSubtreeDecrementsChildCountOfOldParent()
1915
    {
1916
        $repository = $this->getRepository();
1917
        $locationService = $repository->getLocationService();
1918
1919
        $oldParentLocation = $locationService->loadLocation($this->generateId('location', 1));
1920
1921
        // Load expected properties before move
1922
        $expected = $this->loadLocationProperties($oldParentLocation);
1923
        $childCountBefore = $locationService->getLocationChildCount($oldParentLocation);
1924
1925
        $mediaLocationId = $this->generateId('location', 43);
1926
        $demoDesignLocationId = $this->generateId('location', 56);
1927
        /* BEGIN: Use Case */
1928
        // $mediaLocationId is the ID of the "Media" page location in
1929
        // an eZ Publish demo installation
1930
1931
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1932
        // Publish demo installation
1933
1934
        // Load the location service
1935
        $locationService = $repository->getLocationService();
1936
1937
        // Load location to move
1938
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1939
1940
        // Get the location id of the old parent
1941
        $oldParentLocationId = $locationToMove->parentLocationId;
1942
1943
        // Load new parent location
1944
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1945
1946
        // Move location from "Home" to "Demo Design"
1947
        $locationService->moveSubtree(
1948
            $locationToMove,
1949
            $newParentLocation
1950
        );
1951
1952
        // Reload old parent location
1953
        $oldParentLocation = $locationService->loadLocation($oldParentLocationId);
1954
        /* END: Use Case */
1955
1956
        $this->refreshSearch($repository);
1957
1958
        // Load Subtree properties after move
1959
        $actual = $this->loadLocationProperties($oldParentLocation);
1960
        $childCountAfter = $locationService->getLocationChildCount($oldParentLocation);
1961
1962
        $this->assertEquals($expected, $actual);
1963
        $this->assertEquals($childCountBefore - 1, $childCountAfter);
1964
    }
1965
1966
    /**
1967
     * Test for the moveSubtree() method.
1968
     *
1969
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
1970
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1971
     */
1972
    public function testMoveSubtreeThrowsInvalidArgumentException()
1973
    {
1974
        $repository = $this->getRepository();
1975
        $mediaLocationId = $this->generateId('location', 43);
1976
        $multimediaLocationId = $this->generateId('location', 53);
1977
1978
        /* BEGIN: Use Case */
1979
        // $mediaLocationId is the ID of the "Media" page location in
1980
        // an eZ Publish demo installation
1981
1982
        // $multimediaLocationId is the ID of the "Multimedia" page location in an eZ
1983
        // Publish demo installation
1984
1985
        // Load the location service
1986
        $locationService = $repository->getLocationService();
1987
1988
        // Load location to move
1989
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1990
1991
        // Load new parent location
1992
        $newParentLocation = $locationService->loadLocation($multimediaLocationId);
1993
1994
        // Throws an exception because new parent location is placed below location to move
1995
        $locationService->moveSubtree(
1996
            $locationToMove,
1997
            $newParentLocation
1998
        );
1999
        /* END: Use Case */
2000
    }
2001
2002
    /**
2003
     * Loads properties from all locations in the $location's subtree.
2004
     *
2005
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2006
     * @param array $properties
2007
     *
2008
     * @return array
2009
     */
2010
    private function loadSubtreeProperties(Location $location, array $properties = array())
2011
    {
2012
        $locationService = $this->getRepository()->getLocationService();
2013
2014
        foreach ($locationService->loadLocationChildren($location)->locations as $childLocation) {
2015
            $properties[] = $this->loadLocationProperties($childLocation);
2016
2017
            $properties = $this->loadSubtreeProperties($childLocation, $properties);
2018
        }
2019
2020
        return $properties;
2021
    }
2022
2023
    /**
2024
     * Loads assertable properties from the given location.
2025
     *
2026
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2027
     * @param mixed[] $overwrite
2028
     *
2029
     * @return array
2030
     */
2031
    private function loadLocationProperties(Location $location, array $overwrite = array())
2032
    {
2033
        return array_merge(
2034
            array(
2035
                'id' => $location->id,
2036
                'depth' => $location->depth,
2037
                'parentLocationId' => $location->parentLocationId,
2038
                'pathString' => $location->pathString,
2039
                'remoteId' => $location->remoteId,
2040
                'hidden' => $location->hidden,
2041
                'invisible' => $location->invisible,
2042
                'priority' => $location->priority,
2043
                'sortField' => $location->sortField,
2044
                'sortOrder' => $location->sortOrder,
2045
            ),
2046
            $overwrite
2047
        );
2048
    }
2049
}
2050