Completed
Push — master ( 4ee1cb...fdb650 )
by André
23:50
created

LocationServiceTest   F

Complexity

Total Complexity 86

Size/Duplication

Total Lines 2130
Duplicated Lines 16.29 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 347
loc 2130
rs 0.6314
wmc 86
lcom 1
cbo 17

58 Methods

Rating   Name   Duplication   Size   Complexity  
A testLoadLocationRootStructValues() 0 48 1
A testNewLocationCreateStruct() 21 21 1
A testNewLocationCreateStructValues() 0 15 1
B testCreateLocation() 0 40 1
B testCreateLocationStructValues() 0 24 1
B testCreateLocationThrowsInvalidArgumentExceptionContentAlreadyBelowParent() 25 25 1
B testCreateLocationThrowsInvalidArgumentExceptionParentIsSubLocationOfContent() 25 25 1
B testCreateLocationThrowsInvalidArgumentExceptionRemoteIdExists() 25 25 1
B testCreateLocationInTransactionWithRollback() 0 43 3
A testLoadLocation() 19 19 1
B testLoadLocationStructValues() 0 24 1
A testLoadLocationThrowsNotFoundException() 0 13 1
A testLoadLocationByRemoteId() 0 17 1
A testLoadLocationByRemoteIdThrowsNotFoundException() 0 13 1
A testLoadLocations() 0 19 1
B testLoadLocationsContent() 0 30 2
B testLoadLocationsLimitedSubtree() 0 38 1
A testLoadLocationsLimitedSubtreeContent() 0 9 1
A testLoadLocationsThrowsBadStateException() 0 21 1
B testLoadLocationsThrowsBadStateExceptionLimitedSubtree() 0 26 1
A testLoadLocationChildren() 20 20 1
A testGetLocationChildCount() 0 12 1
B testLoadLocationChildrenData() 0 28 2
A testLoadLocationChildrenWithOffset() 20 20 1
B testLoadLocationChildrenDataWithOffset() 26 26 2
A testLoadLocationChildrenWithOffsetAndLimit() 20 20 1
B testLoadLocationChildrenDataWithOffsetAndLimit() 25 25 2
A testNewLocationUpdateStruct() 0 15 1
B testUpdateLocation() 0 31 1
A testUpdateLocationStructValues() 0 23 1
B testUpdateLocationWithSameRemoteId() 0 28 1
A testUpdateLocationThrowsInvalidArgumentException() 0 20 1
A testUpdateLocationTwice() 0 23 1
B testSwapLocation() 0 37 1
B testHideLocation() 0 36 2
A assertSubtreeProperties() 0 20 4
B testUnhideLocation() 0 37 2
A testUnhideLocationNotUnhidesHiddenSubtree() 0 58 3
B testDeleteLocation() 0 42 6
B testDeleteLocationDecrementsChildCountOnParent() 0 38 1
B testDeleteContentObjectLastLocation() 0 36 1
B testCopySubtree() 0 45 1
B testCopySubtreeWithAliases() 0 33 1
A assertDefaultContentStates() 19 19 3
A testCopySubtreeUpdatesSubtreeProperties() 0 59 3
B testCopySubtreeIncrementsChildCountOfNewParent() 0 38 1
B testCopySubtreeThrowsInvalidArgumentException() 0 27 1
B testMoveSubtree() 0 43 1
B testMoveSubtreeHidden() 0 46 1
A testMoveSubtreeUpdatesSubtreeProperties() 0 54 2
A testMoveSubtreeUpdatesSubtreePropertiesHidden() 0 58 2
A testMoveSubtreeIncrementsChildCountOfNewParent() 51 51 1
A testMoveSubtreeDecrementsChildCountOfOldParent() 51 51 1
B testMoveSubtreeThrowsInvalidArgumentException() 0 29 1
A loadSubtreeProperties() 0 12 2
A loadLocationProperties() 0 18 1
A assertGeneratedAliases() 0 7 2
A assertAliasesBeforeCopy() 0 11 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like LocationServiceTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use LocationServiceTest, and based on these observations, apply Extract Interface, too.

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
     * Test for the copySubtree() method.
1488
     *
1489
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1490
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1491
     */
1492
    public function testCopySubtreeWithAliases()
1493
    {
1494
        $repository = $this->getRepository();
1495
        $urlAliasService = $repository->getURLAliasService();
1496
1497
        // $mediaLocationId is the ID of the "Media" page location in
1498
        // an eZ Publish demo installation
1499
1500
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1501
        // Publish demo installation
1502
        $mediaLocationId = $this->generateId('location', 43);
1503
        $demoDesignLocationId = $this->generateId('location', 56);
1504
1505
        $locationService = $repository->getLocationService();
1506
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1507
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1508
1509
        $expectedSubItemAliases = [
1510
            '/Design/Plain-site/Media/Multimedia',
1511
            '/Design/Plain-site/Media/Images',
1512
            '/Design/Plain-site/Media/Files',
1513
        ];
1514
1515
        $this->assertAliasesBeforeCopy($urlAliasService, $expectedSubItemAliases);
1516
1517
        // Copy location "Media" to "Design"
1518
        $locationService->copySubtree(
1519
            $locationToCopy,
1520
            $newParentLocation
1521
        );
1522
1523
        $this->assertGeneratedAliases($urlAliasService, $expectedSubItemAliases);
1524
    }
1525
1526
    /**
1527
     * Asserts that given Content has default ContentStates.
1528
     *
1529
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
1530
     */
1531 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
1532
    {
1533
        $repository = $this->getRepository();
1534
        $objectStateService = $repository->getObjectStateService();
1535
1536
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
1537
1538
        foreach ($objectStateGroups as $objectStateGroup) {
1539
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
1540
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
1541
                // Only check the first object state which is the default one.
1542
                $this->assertEquals(
1543
                    $objectState,
1544
                    $contentState
1545
                );
1546
                break;
1547
            }
1548
        }
1549
    }
1550
1551
    /**
1552
     * Test for the copySubtree() method.
1553
     *
1554
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1555
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1556
     */
1557
    public function testCopySubtreeUpdatesSubtreeProperties()
1558
    {
1559
        $repository = $this->getRepository();
1560
        $locationService = $repository->getLocationService();
1561
1562
        $locationToCopy = $locationService->loadLocation($this->generateId('location', 43));
1563
1564
        // Load Subtree properties before copy
1565
        $expected = $this->loadSubtreeProperties($locationToCopy);
1566
1567
        $mediaLocationId = $this->generateId('location', 43);
1568
        $demoDesignLocationId = $this->generateId('location', 56);
1569
        /* BEGIN: Use Case */
1570
        // $mediaLocationId is the ID of the "Media" page location in
1571
        // an eZ Publish demo installation
1572
1573
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1574
        // Publish demo installation
1575
1576
        // Load the location service
1577
        $locationService = $repository->getLocationService();
1578
1579
        // Load location to copy
1580
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1581
1582
        // Load new parent location
1583
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1584
1585
        // Copy location "Media" to "Demo Design"
1586
        $copiedLocation = $locationService->copySubtree(
1587
            $locationToCopy,
1588
            $newParentLocation
1589
        );
1590
        /* END: Use Case */
1591
1592
        $beforeIds = array();
1593
        foreach ($expected as $properties) {
1594
            $beforeIds[] = $properties['id'];
1595
        }
1596
1597
        $this->refreshSearch($repository);
1598
1599
        // Load Subtree properties after copy
1600
        $actual = $this->loadSubtreeProperties($copiedLocation);
1601
1602
        $this->assertEquals(count($expected), count($actual));
1603
1604
        foreach ($actual as $properties) {
1605
            $this->assertNotContains($properties['id'], $beforeIds);
1606
            $this->assertStringStartsWith(
1607
                "{$newParentLocation->pathString}" . $this->parseId('location', $copiedLocation->id) . '/',
1608
                $properties['pathString']
1609
            );
1610
            $this->assertStringEndsWith(
1611
                '/' . $this->parseId('location', $properties['id']) . '/',
1612
                $properties['pathString']
1613
            );
1614
        }
1615
    }
1616
1617
    /**
1618
     * Test for the copySubtree() method.
1619
     *
1620
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1621
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1622
     */
1623
    public function testCopySubtreeIncrementsChildCountOfNewParent()
1624
    {
1625
        $repository = $this->getRepository();
1626
        $locationService = $repository->getLocationService();
1627
1628
        $childCountBefore = $locationService->getLocationChildCount($locationService->loadLocation(56));
1629
1630
        $mediaLocationId = $this->generateId('location', 43);
1631
        $demoDesignLocationId = $this->generateId('location', 56);
1632
        /* BEGIN: Use Case */
1633
        // $mediaLocationId is the ID of the "Media" page location in
1634
        // an eZ Publish demo installation
1635
1636
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1637
        // Publish demo installation
1638
1639
        // Load the location service
1640
        $locationService = $repository->getLocationService();
1641
1642
        // Load location to copy
1643
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1644
1645
        // Load new parent location
1646
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1647
1648
        // Copy location "Media" to "Demo Design"
1649
        $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...
1650
            $locationToCopy,
1651
            $newParentLocation
1652
        );
1653
        /* END: Use Case */
1654
1655
        $this->refreshSearch($repository);
1656
1657
        $childCountAfter = $locationService->getLocationChildCount($locationService->loadLocation($demoDesignLocationId));
1658
1659
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
1660
    }
1661
1662
    /**
1663
     * Test for the copySubtree() method.
1664
     *
1665
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1666
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1667
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1668
     */
1669
    public function testCopySubtreeThrowsInvalidArgumentException()
1670
    {
1671
        $repository = $this->getRepository();
1672
1673
        $communityLocationId = $this->generateId('location', 5);
1674
        /* BEGIN: Use Case */
1675
        // $communityLocationId is the ID of the "Community" page location in
1676
        // an eZ Publish demo installation
1677
1678
        // Load the location service
1679
        $locationService = $repository->getLocationService();
1680
1681
        // Load location to copy
1682
        $locationToCopy = $locationService->loadLocation($communityLocationId);
1683
1684
        // Use a child as new parent
1685
        $childLocations = $locationService->loadLocationChildren($locationToCopy)->locations;
1686
        $newParentLocation = end($childLocations);
1687
1688
        // This call will fail with an "InvalidArgumentException", because the
1689
        // new parent is a child location of the subtree to copy.
1690
        $locationService->copySubtree(
1691
            $locationToCopy,
1692
            $newParentLocation
0 ignored issues
show
Security Bug introduced by
It seems like $newParentLocation defined by end($childLocations) on line 1686 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...
1693
        );
1694
        /* END: Use Case */
1695
    }
1696
1697
    /**
1698
     * Test for the moveSubtree() method.
1699
     *
1700
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1701
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1702
     */
1703
    public function testMoveSubtree()
1704
    {
1705
        $repository = $this->getRepository();
1706
1707
        $mediaLocationId = $this->generateId('location', 43);
1708
        $demoDesignLocationId = $this->generateId('location', 56);
1709
        /* BEGIN: Use Case */
1710
        // $mediaLocationId is the ID of the "Media" page location in
1711
        // an eZ Publish demo installation
1712
1713
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1714
        // Publish demo installation
1715
1716
        // Load the location service
1717
        $locationService = $repository->getLocationService();
1718
1719
        // Load location to move
1720
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1721
1722
        // Load new parent location
1723
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1724
1725
        // Move location from "Home" to "Demo Design"
1726
        $locationService->moveSubtree(
1727
            $locationToMove,
1728
            $newParentLocation
1729
        );
1730
1731
        // Load moved location
1732
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1733
        /* END: Use Case */
1734
1735
        $this->assertPropertiesCorrect(
1736
            array(
1737
                'hidden' => false,
1738
                'invisible' => false,
1739
                'depth' => $newParentLocation->depth + 1,
1740
                'parentLocationId' => $newParentLocation->id,
1741
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
1742
            ),
1743
            $movedLocation
1744
        );
1745
    }
1746
1747
    /**
1748
     * Test for the moveSubtree() method.
1749
     *
1750
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1751
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
1752
     */
1753
    public function testMoveSubtreeHidden()
1754
    {
1755
        $repository = $this->getRepository();
1756
1757
        $mediaLocationId = $this->generateId('location', 43);
1758
        $demoDesignLocationId = $this->generateId('location', 56);
1759
        /* BEGIN: Use Case */
1760
        // $mediaLocationId is the ID of the "Media" page location in
1761
        // an eZ Publish demo installation
1762
1763
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1764
        // Publish demo installation
1765
1766
        // Load the location service
1767
        $locationService = $repository->getLocationService();
1768
1769
        // Load location to move
1770
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1771
1772
        // Load new parent location
1773
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1774
1775
        // Hide the target location before we move
1776
        $newParentLocation = $locationService->hideLocation($newParentLocation);
1777
1778
        // Move location from "Home" to "Demo Design"
1779
        $locationService->moveSubtree(
1780
            $locationToMove,
1781
            $newParentLocation
1782
        );
1783
1784
        // Load moved location
1785
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1786
        /* END: Use Case */
1787
1788
        $this->assertPropertiesCorrect(
1789
            array(
1790
                'hidden' => false,
1791
                'invisible' => true,
1792
                'depth' => $newParentLocation->depth + 1,
1793
                'parentLocationId' => $newParentLocation->id,
1794
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
1795
            ),
1796
            $movedLocation
1797
        );
1798
    }
1799
1800
    /**
1801
     * Test for the moveSubtree() method.
1802
     *
1803
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1804
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
1805
     */
1806
    public function testMoveSubtreeUpdatesSubtreeProperties()
1807
    {
1808
        $repository = $this->getRepository();
1809
        $locationService = $repository->getLocationService();
1810
1811
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
1812
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
1813
1814
        // Load Subtree properties before move
1815
        $expected = $this->loadSubtreeProperties($locationToMove);
1816
        foreach ($expected as $id => $properties) {
1817
            $expected[$id]['depth'] = $properties['depth'] + 2;
1818
            $expected[$id]['pathString'] = str_replace(
1819
                $locationToMove->pathString,
1820
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
1821
                $properties['pathString']
1822
            );
1823
        }
1824
1825
        $mediaLocationId = $this->generateId('location', 43);
1826
        $demoDesignLocationId = $this->generateId('location', 56);
1827
        /* BEGIN: Use Case */
1828
        // $mediaLocationId is the ID of the "Media" page location in
1829
        // an eZ Publish demo installation
1830
1831
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1832
        // Publish demo installation
1833
1834
        // Load the location service
1835
        $locationService = $repository->getLocationService();
1836
1837
        // Load location to move
1838
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1839
1840
        // Load new parent location
1841
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1842
1843
        // Move location from "Home" to "Demo Design"
1844
        $locationService->moveSubtree(
1845
            $locationToMove,
1846
            $newParentLocation
1847
        );
1848
1849
        // Load moved location
1850
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1851
        /* END: Use Case */
1852
1853
        $this->refreshSearch($repository);
1854
1855
        // Load Subtree properties after move
1856
        $actual = $this->loadSubtreeProperties($movedLocation);
1857
1858
        $this->assertEquals($expected, $actual);
1859
    }
1860
1861
    /**
1862
     * Test for the moveSubtree() method.
1863
     *
1864
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1865
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtreeUpdatesSubtreeProperties
1866
     */
1867
    public function testMoveSubtreeUpdatesSubtreePropertiesHidden()
1868
    {
1869
        $repository = $this->getRepository();
1870
        $locationService = $repository->getLocationService();
1871
1872
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
1873
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
1874
1875
        // Hide the target location before we move
1876
        $newParentLocation = $locationService->hideLocation($newParentLocation);
1877
1878
        // Load Subtree properties before move
1879
        $expected = $this->loadSubtreeProperties($locationToMove);
1880
        foreach ($expected as $id => $properties) {
1881
            $expected[$id]['invisible'] = true;
1882
            $expected[$id]['depth'] = $properties['depth'] + 2;
1883
            $expected[$id]['pathString'] = str_replace(
1884
                $locationToMove->pathString,
1885
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
1886
                $properties['pathString']
1887
            );
1888
        }
1889
1890
        $mediaLocationId = $this->generateId('location', 43);
1891
        $demoDesignLocationId = $this->generateId('location', 56);
1892
        /* BEGIN: Use Case */
1893
        // $mediaLocationId is the ID of the "Media" page location in
1894
        // an eZ Publish demo installation
1895
1896
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1897
        // Publish demo installation
1898
1899
        // Load the location service
1900
        $locationService = $repository->getLocationService();
1901
1902
        // Load location to move
1903
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1904
1905
        // Load new parent location
1906
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1907
1908
        // Move location from "Home" to "Demo Design"
1909
        $locationService->moveSubtree(
1910
            $locationToMove,
1911
            $newParentLocation
1912
        );
1913
1914
        // Load moved location
1915
        $movedLocation = $locationService->loadLocation($mediaLocationId);
1916
        /* END: Use Case */
1917
1918
        $this->refreshSearch($repository);
1919
1920
        // Load Subtree properties after move
1921
        $actual = $this->loadSubtreeProperties($movedLocation);
1922
1923
        $this->assertEquals($expected, $actual);
1924
    }
1925
1926
    /**
1927
     * Test for the moveSubtree() method.
1928
     *
1929
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1930
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
1931
     */
1932 View Code Duplication
    public function testMoveSubtreeIncrementsChildCountOfNewParent()
1933
    {
1934
        $repository = $this->getRepository();
1935
        $locationService = $repository->getLocationService();
1936
1937
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
1938
1939
        // Load expected properties before move
1940
        $expected = $this->loadLocationProperties($newParentLocation);
1941
        $childCountBefore = $locationService->getLocationChildCount($newParentLocation);
1942
1943
        $mediaLocationId = $this->generateId('location', 43);
1944
        $demoDesignLocationId = $this->generateId('location', 56);
1945
        /* BEGIN: Use Case */
1946
        // $mediaLocationId is the ID of the "Media" page location in
1947
        // an eZ Publish demo installation
1948
1949
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1950
        // Publish demo installation
1951
1952
        // Load the location service
1953
        $locationService = $repository->getLocationService();
1954
1955
        // Load location to move
1956
        $locationToMove = $locationService->loadLocation($mediaLocationId);
1957
1958
        // Load new parent location
1959
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1960
1961
        // Move location from "Home" to "Demo Design"
1962
        $locationService->moveSubtree(
1963
            $locationToMove,
1964
            $newParentLocation
1965
        );
1966
1967
        // Load moved location
1968
        $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...
1969
1970
        // Reload new parent location
1971
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1972
        /* END: Use Case */
1973
1974
        $this->refreshSearch($repository);
1975
1976
        // Load Subtree properties after move
1977
        $actual = $this->loadLocationProperties($newParentLocation);
1978
        $childCountAfter = $locationService->getLocationChildCount($newParentLocation);
1979
1980
        $this->assertEquals($expected, $actual);
1981
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
1982
    }
1983
1984
    /**
1985
     * Test for the moveSubtree() method.
1986
     *
1987
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1988
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
1989
     */
1990 View Code Duplication
    public function testMoveSubtreeDecrementsChildCountOfOldParent()
1991
    {
1992
        $repository = $this->getRepository();
1993
        $locationService = $repository->getLocationService();
1994
1995
        $oldParentLocation = $locationService->loadLocation($this->generateId('location', 1));
1996
1997
        // Load expected properties before move
1998
        $expected = $this->loadLocationProperties($oldParentLocation);
1999
        $childCountBefore = $locationService->getLocationChildCount($oldParentLocation);
2000
2001
        $mediaLocationId = $this->generateId('location', 43);
2002
        $demoDesignLocationId = $this->generateId('location', 56);
2003
        /* BEGIN: Use Case */
2004
        // $mediaLocationId is the ID of the "Media" page location in
2005
        // an eZ Publish demo installation
2006
2007
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2008
        // Publish demo installation
2009
2010
        // Load the location service
2011
        $locationService = $repository->getLocationService();
2012
2013
        // Load location to move
2014
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2015
2016
        // Get the location id of the old parent
2017
        $oldParentLocationId = $locationToMove->parentLocationId;
2018
2019
        // Load new parent location
2020
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2021
2022
        // Move location from "Home" to "Demo Design"
2023
        $locationService->moveSubtree(
2024
            $locationToMove,
2025
            $newParentLocation
2026
        );
2027
2028
        // Reload old parent location
2029
        $oldParentLocation = $locationService->loadLocation($oldParentLocationId);
2030
        /* END: Use Case */
2031
2032
        $this->refreshSearch($repository);
2033
2034
        // Load Subtree properties after move
2035
        $actual = $this->loadLocationProperties($oldParentLocation);
2036
        $childCountAfter = $locationService->getLocationChildCount($oldParentLocation);
2037
2038
        $this->assertEquals($expected, $actual);
2039
        $this->assertEquals($childCountBefore - 1, $childCountAfter);
2040
    }
2041
2042
    /**
2043
     * Test for the moveSubtree() method.
2044
     *
2045
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2046
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2047
     */
2048
    public function testMoveSubtreeThrowsInvalidArgumentException()
2049
    {
2050
        $repository = $this->getRepository();
2051
        $mediaLocationId = $this->generateId('location', 43);
2052
        $multimediaLocationId = $this->generateId('location', 53);
2053
2054
        /* BEGIN: Use Case */
2055
        // $mediaLocationId is the ID of the "Media" page location in
2056
        // an eZ Publish demo installation
2057
2058
        // $multimediaLocationId is the ID of the "Multimedia" page location in an eZ
2059
        // Publish demo installation
2060
2061
        // Load the location service
2062
        $locationService = $repository->getLocationService();
2063
2064
        // Load location to move
2065
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2066
2067
        // Load new parent location
2068
        $newParentLocation = $locationService->loadLocation($multimediaLocationId);
2069
2070
        // Throws an exception because new parent location is placed below location to move
2071
        $locationService->moveSubtree(
2072
            $locationToMove,
2073
            $newParentLocation
2074
        );
2075
        /* END: Use Case */
2076
    }
2077
2078
    /**
2079
     * Loads properties from all locations in the $location's subtree.
2080
     *
2081
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2082
     * @param array $properties
2083
     *
2084
     * @return array
2085
     */
2086
    private function loadSubtreeProperties(Location $location, array $properties = array())
2087
    {
2088
        $locationService = $this->getRepository()->getLocationService();
2089
2090
        foreach ($locationService->loadLocationChildren($location)->locations as $childLocation) {
2091
            $properties[] = $this->loadLocationProperties($childLocation);
2092
2093
            $properties = $this->loadSubtreeProperties($childLocation, $properties);
2094
        }
2095
2096
        return $properties;
2097
    }
2098
2099
    /**
2100
     * Loads assertable properties from the given location.
2101
     *
2102
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2103
     * @param mixed[] $overwrite
2104
     *
2105
     * @return array
2106
     */
2107
    private function loadLocationProperties(Location $location, array $overwrite = array())
2108
    {
2109
        return array_merge(
2110
            array(
2111
                'id' => $location->id,
2112
                'depth' => $location->depth,
2113
                'parentLocationId' => $location->parentLocationId,
2114
                'pathString' => $location->pathString,
2115
                'remoteId' => $location->remoteId,
2116
                'hidden' => $location->hidden,
2117
                'invisible' => $location->invisible,
2118
                'priority' => $location->priority,
2119
                'sortField' => $location->sortField,
2120
                'sortOrder' => $location->sortOrder,
2121
            ),
2122
            $overwrite
2123
        );
2124
    }
2125
2126
    /**
2127
     * Assert generated aliases to expected alias return.
2128
     *
2129
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
2130
     * @param array $expectedAliases
2131
     */
2132
    protected function assertGeneratedAliases($urlAliasService, array $expectedAliases)
2133
    {
2134
        foreach ($expectedAliases as $expectedAlias) {
2135
            $urlAlias = $urlAliasService->lookup($expectedAlias);
2136
            $this->assertPropertiesCorrect(['type' => 0], $urlAlias);
2137
        }
2138
    }
2139
2140
    /**
2141
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
2142
     * @param array $expectedSubItemAliases
2143
     */
2144
    private function assertAliasesBeforeCopy($urlAliasService, array $expectedSubItemAliases)
2145
    {
2146
        foreach ($expectedSubItemAliases as $aliasUrl) {
2147
            try {
2148
                $urlAliasService->lookup($aliasUrl);
2149
                $this->fail('We didn\'t expect to find alias, but it was found');
2150
            } catch (\Exception $e) {
2151
                $this->assertTrue(true); // OK - alias was not found
2152
            }
2153
        }
2154
    }
2155
}
2156