Completed
Push — master ( a6387c...f7252d )
by André
23:52 queued 09:38
created

testUpdateLocationWithSameRemoteId()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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