Completed
Push — master ( 4af5d3...2fef72 )
by André
38:49 queued 20:27
created

testLoadLocationThrowsNotFoundExceptionForNotAvailableContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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