Completed
Push — location_content_property ( b180d5 )
by André
16:28
created

testLoadLocationPrioritizedLanguagesFallback()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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