Completed
Push — test-EZP-26707-issearchable-fu... ( 0d4c60 )
by
unknown
30:03 queued 14:08
created

LocationServiceTest::testSwapLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 25
nc 1
nop 0
dl 0
loc 52
rs 9.4929
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading history...
526
527
        $this->assertEquals(1, count($locations));
528
        foreach ($locations as $loadedLocation) {
529
            $this->assertInstanceOf(
530
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
531
                $loadedLocation
532
            );
533
        }
534
535
        usort(
536
            $locations,
537
            function ($a, $b) {
538
                strcmp($a->id, $b->id);
539
            }
540
        );
541
542
        $this->assertEquals(
543
            array($this->generateId('location', 5)),
544
            array_map(
545
                function (Location $location) {
546
                    return $location->id;
547
                },
548
                $locations
549
            )
550
        );
551
    }
552
553
    /**
554
     * Test for the loadLocations() method.
555
     *
556
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
557
     *
558
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations($contentInfo, $rootLocation)
559
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
560
     */
561
    public function testLoadLocationsLimitedSubtree()
562
    {
563
        $repository = $this->getRepository();
564
565
        $originalLocationId = $this->generateId('location', 54);
566
        $originalParentLocationId = $this->generateId('location', 48);
567
        $newParentLocationId = $this->generateId('location', 43);
568
        /* BEGIN: Use Case */
569
        // $originalLocationId is the ID of an existing location
570
        // $originalParentLocationId is the ID of the parent location of
571
        //     $originalLocationId
572
        // $newParentLocationId is the ID of an existing location outside the tree
573
        // of $originalLocationId and $originalParentLocationId
574
        $locationService = $repository->getLocationService();
575
576
        // Location at "/1/48/54"
577
        $originalLocation = $locationService->loadLocation($originalLocationId);
578
579
        // Create location under "/1/43/"
580
        $locationCreate = $locationService->newLocationCreateStruct($newParentLocationId);
581
        $locationService->createLocation(
582
            $originalLocation->contentInfo,
583
            $locationCreate
584
        );
585
586
        $findRootLocation = $locationService->loadLocation($originalParentLocationId);
587
588
        // Returns an array with only $originalLocation
589
        $locations = $locationService->loadLocations(
590
            $originalLocation->contentInfo,
591
            $findRootLocation
592
        );
593
        /* END: Use Case */
594
595
        $this->assertInternalType('array', $locations);
596
597
        return $locations;
598
    }
599
600
    /**
601
     * Test for the loadLocations() method.
602
     *
603
     * @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
604
     *
605
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
606
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationsLimitedSubtree
607
     */
608
    public function testLoadLocationsLimitedSubtreeContent(array $locations)
609
    {
610
        $this->assertEquals(1, count($locations));
611
612
        $this->assertEquals(
613
            $this->generateId('location', 54),
614
            reset($locations)->id
615
        );
616
    }
617
618
    /**
619
     * Test for the loadLocations() method.
620
     *
621
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
622
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
623
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
624
     */
625
    public function testLoadLocationsThrowsBadStateException()
626
    {
627
        $repository = $this->getRepository();
628
629
        /* BEGIN: Use Case */
630
        $contentTypeService = $repository->getContentTypeService();
631
        $contentService = $repository->getContentService();
632
        $locationService = $repository->getLocationService();
633
634
        // Create new content, which is not published
635
        $folderType = $contentTypeService->loadContentTypeByIdentifier('folder');
636
        $contentCreate = $contentService->newContentCreateStruct($folderType, 'eng-US');
637
        $contentCreate->setField('name', 'New Folder');
638
        $content = $contentService->createContent($contentCreate);
639
640
        // Throws Exception, since $content has no published version, yet
641
        $locationService->loadLocations(
642
            $content->contentInfo
643
        );
644
        /* END: Use Case */
645
    }
646
647
    /**
648
     * Test for the loadLocations() method.
649
     *
650
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations($contentInfo, $rootLocation)
651
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
652
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
653
     */
654
    public function testLoadLocationsThrowsBadStateExceptionLimitedSubtree()
655
    {
656
        $repository = $this->getRepository();
657
658
        $someLocationId = $this->generateId('location', 2);
659
        /* BEGIN: Use Case */
660
        // $someLocationId is the ID of an existing location
661
        $contentTypeService = $repository->getContentTypeService();
662
        $contentService = $repository->getContentService();
663
        $locationService = $repository->getLocationService();
664
665
        // Create new content, which is not published
666
        $folderType = $contentTypeService->loadContentTypeByIdentifier('folder');
667
        $contentCreate = $contentService->newContentCreateStruct($folderType, 'eng-US');
668
        $contentCreate->setField('name', 'New Folder');
669
        $content = $contentService->createContent($contentCreate);
670
671
        $findRootLocation = $locationService->loadLocation($someLocationId);
672
673
        // Throws Exception, since $content has no published version, yet
674
        $locationService->loadLocations(
675
            $content->contentInfo,
676
            $findRootLocation
677
        );
678
        /* END: Use Case */
679
    }
680
681
    /**
682
     * Test for the loadLocationChildren() method.
683
     *
684
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren()
685
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
686
     */
687 View Code Duplication
    public function testLoadLocationChildren()
688
    {
689
        $repository = $this->getRepository();
690
691
        $locationId = $this->generateId('location', 5);
692
        /* BEGIN: Use Case */
693
        // $locationId is the ID of an existing location
694
        $locationService = $repository->getLocationService();
695
696
        $location = $locationService->loadLocation($locationId);
697
698
        $childLocations = $locationService->loadLocationChildren($location);
699
        /* END: Use Case */
700
701
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\LocationList', $childLocations);
702
        $this->assertInternalType('array', $childLocations->locations);
703
        $this->assertInternalType('int', $childLocations->totalCount);
704
705
        return $childLocations;
706
    }
707
708
    /**
709
     * Test for the getLocationChildCount() method.
710
     *
711
     * @see \eZ\Publish\API\Repository\LocationService::getLocationChildCount()
712
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
713
     */
714
    public function testGetLocationChildCount()
715
    {
716
        // $locationId is the ID of an existing location
717
        $locationService = $this->getRepository()->getLocationService();
718
719
        $this->assertSame(
720
            5,
721
            $locationService->getLocationChildCount(
722
                $locationService->loadLocation($this->generateId('location', 5))
723
            )
724
        );
725
    }
726
727
    /**
728
     * Test for the loadLocationChildren() method.
729
     *
730
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren()
731
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
732
     */
733
    public function testLoadLocationChildrenData(LocationList $locations)
734
    {
735
        $this->assertEquals(5, count($locations->locations));
736
        $this->assertEquals(5, $locations->totalCount);
737
738
        foreach ($locations->locations as $location) {
739
            $this->assertInstanceOf(
740
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
741
                $location
742
            );
743
        }
744
745
        $this->assertEquals(
746
            array(
747
                $this->generateId('location', 12),
748
                $this->generateId('location', 13),
749
                $this->generateId('location', 14),
750
                $this->generateId('location', 44),
751
                $this->generateId('location', 61),
752
            ),
753
            array_map(
754
                function (Location $location) {
755
                    return $location->id;
756
                },
757
                $locations->locations
758
            )
759
        );
760
    }
761
762
    /**
763
     * Test for the loadLocationChildren() method.
764
     *
765
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
766
     *
767
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset)
768
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
769
     */
770 View Code Duplication
    public function testLoadLocationChildrenWithOffset()
771
    {
772
        $repository = $this->getRepository();
773
774
        $locationId = $this->generateId('location', 5);
775
        /* BEGIN: Use Case */
776
        // $locationId is the ID of an existing location
777
        $locationService = $repository->getLocationService();
778
779
        $location = $locationService->loadLocation($locationId);
780
781
        $childLocations = $locationService->loadLocationChildren($location, 2);
782
        /* END: Use Case */
783
784
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\LocationList', $childLocations);
785
        $this->assertInternalType('array', $childLocations->locations);
786
        $this->assertInternalType('int', $childLocations->totalCount);
787
788
        return $childLocations;
789
    }
790
791
    /**
792
     * Test for the loadLocationChildren() method.
793
     *
794
     * @param \eZ\Publish\API\Repository\Values\Content\LocationList $locations
795
     *
796
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset)
797
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildrenWithOffset
798
     */
799 View Code Duplication
    public function testLoadLocationChildrenDataWithOffset(LocationList $locations)
800
    {
801
        $this->assertEquals(3, count($locations->locations));
802
        $this->assertEquals(5, $locations->totalCount);
803
804
        foreach ($locations->locations as $location) {
805
            $this->assertInstanceOf(
806
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
807
                $location
808
            );
809
        }
810
811
        $this->assertEquals(
812
            array(
813
                $this->generateId('location', 14),
814
                $this->generateId('location', 44),
815
                $this->generateId('location', 61),
816
            ),
817
            array_map(
818
                function (Location $location) {
819
                    return $location->id;
820
                },
821
                $locations->locations
822
            )
823
        );
824
    }
825
826
    /**
827
     * Test for the loadLocationChildren() method.
828
     *
829
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
830
     *
831
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset, $limit)
832
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
833
     */
834 View Code Duplication
    public function testLoadLocationChildrenWithOffsetAndLimit()
835
    {
836
        $repository = $this->getRepository();
837
838
        $locationId = $this->generateId('location', 5);
839
        /* BEGIN: Use Case */
840
        // $locationId is the ID of an existing location
841
        $locationService = $repository->getLocationService();
842
843
        $location = $locationService->loadLocation($locationId);
844
845
        $childLocations = $locationService->loadLocationChildren($location, 2, 2);
846
        /* END: Use Case */
847
848
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\LocationList', $childLocations);
849
        $this->assertInternalType('array', $childLocations->locations);
850
        $this->assertInternalType('int', $childLocations->totalCount);
851
852
        return $childLocations;
853
    }
854
855
    /**
856
     * Test for the loadLocationChildren() method.
857
     *
858
     * @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
859
     *
860
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset, $limit)
861
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildrenWithOffsetAndLimit
862
     */
863 View Code Duplication
    public function testLoadLocationChildrenDataWithOffsetAndLimit(LocationList $locations)
864
    {
865
        $this->assertEquals(2, count($locations->locations));
866
        $this->assertEquals(5, $locations->totalCount);
867
868
        foreach ($locations->locations as $location) {
869
            $this->assertInstanceOf(
870
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
871
                $location
872
            );
873
        }
874
875
        $this->assertEquals(
876
            array(
877
                $this->generateId('location', 14),
878
                $this->generateId('location', 44),
879
            ),
880
            array_map(
881
                function (Location $location) {
882
                    return $location->id;
883
                },
884
                $locations->locations
885
            )
886
        );
887
    }
888
889
    /**
890
     * Test for the newLocationUpdateStruct() method.
891
     *
892
     * @see \eZ\Publish\API\Repository\LocationService::newLocationUpdateStruct()
893
     */
894
    public function testNewLocationUpdateStruct()
895
    {
896
        $repository = $this->getRepository();
897
898
        /* BEGIN: Use Case */
899
        $locationService = $repository->getLocationService();
900
901
        $updateStruct = $locationService->newLocationUpdateStruct();
902
        /* END: Use Case */
903
904
        $this->assertInstanceOf(
905
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\LocationUpdateStruct',
906
            $updateStruct
907
        );
908
    }
909
910
    /**
911
     * Test for the updateLocation() method.
912
     *
913
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
914
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
915
     */
916
    public function testUpdateLocation()
917
    {
918
        $repository = $this->getRepository();
919
920
        $originalLocationId = $this->generateId('location', 5);
921
        /* BEGIN: Use Case */
922
        // $originalLocationId is the ID of an existing location
923
        $locationService = $repository->getLocationService();
924
925
        $originalLocation = $locationService->loadLocation($originalLocationId);
926
927
        $updateStruct = $locationService->newLocationUpdateStruct();
928
        $updateStruct->priority = 3;
929
        $updateStruct->remoteId = 'c7adcbf1e96bc29bca28c2d809d0c7ef69272651';
930
        $updateStruct->sortField = Location::SORT_FIELD_PRIORITY;
931
        $updateStruct->sortOrder = Location::SORT_ORDER_DESC;
932
933
        $updatedLocation = $locationService->updateLocation($originalLocation, $updateStruct);
934
        /* END: Use Case */
935
936
        $this->assertInstanceOf(
937
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
938
            $updatedLocation
939
        );
940
941
        return array(
942
            'originalLocation' => $originalLocation,
943
            'updateStruct' => $updateStruct,
944
            'updatedLocation' => $updatedLocation,
945
        );
946
    }
947
948
    /**
949
     * Test for the updateLocation() method.
950
     *
951
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
952
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testUpdateLocation
953
     */
954
    public function testUpdateLocationStructValues(array $data)
955
    {
956
        $originalLocation = $data['originalLocation'];
957
        $updateStruct = $data['updateStruct'];
958
        $updatedLocation = $data['updatedLocation'];
959
960
        $this->assertPropertiesCorrect(
961
            array(
962
                'id' => $originalLocation->id,
963
                'priority' => $updateStruct->priority,
964
                'hidden' => $originalLocation->hidden,
965
                'invisible' => $originalLocation->invisible,
966
                'remoteId' => $updateStruct->remoteId,
967
                'contentInfo' => $originalLocation->contentInfo,
968
                'parentLocationId' => $originalLocation->parentLocationId,
969
                'pathString' => $originalLocation->pathString,
970
                'depth' => $originalLocation->depth,
971
                'sortField' => $updateStruct->sortField,
972
                'sortOrder' => $updateStruct->sortOrder,
973
            ),
974
            $updatedLocation
975
        );
976
    }
977
978
    /**
979
     * Test for the updateLocation() method.
980
     *
981
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
982
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
983
     */
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));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

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