Completed
Push — master ( b2a1ff...4b823c )
by Łukasz
19:14
created

testUnhideLocationNotUnhidesHiddenSubtree()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 34
nc 4
nop 0
dl 0
loc 58
rs 9.639
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

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