Completed
Push — location_content_property ( 26cc6c...3ff870 )
by André
23:55
created

LocationServiceTest::testLoadLocationChildren()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

Loading history...
344
        } catch (NotFoundException $e) {
345
            return;
346
        }
347
        /* END: Use Case */
348
349
        $this->fail('Objects still exists after rollback.');
350
    }
351
352
    /**
353
     * Test for the loadLocation() method.
354
     *
355
     * @return \eZ\Publish\API\Repository\Values\Content\Location
356
     *
357
     * @covers \eZ\Publish\API\Repository\LocationService::loadLocation
358
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
359
     */
360
    public function testLoadLocation()
361
    {
362
        $repository = $this->getRepository();
363
364
        $locationId = $this->generateId('location', 5);
365
        /* BEGIN: Use Case */
366
        // $locationId is the ID of an existing location
367
        $locationService = $repository->getLocationService();
368
369
        $location = $locationService->loadLocation($locationId);
370
        /* END: Use Case */
371
372
        $this->assertInstanceOf(
373
            Location::class,
374
            $location
375
        );
376
        self::assertEquals(5, $location->id);
377
378
        return $location;
379
    }
380
381
    /**
382
     * Test for the loadLocation() method.
383
     *
384
     * @see \eZ\Publish\API\Repository\LocationService::loadLocation()
385
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
386
     */
387
    public function testLoadLocationRootStructValues()
388
    {
389
        $repository = $this->getRepository();
390
        $locationService = $repository->getLocationService();
391
        $location = $locationService->loadLocation($this->generateId('location', 1));
392
393
        $legacyDateTime = new \DateTime();
394
        $legacyDateTime->setTimestamp(1030968000);
395
396
        // $location
397
        $this->assertPropertiesCorrect(
398
            array(
399
                'id' => $this->generateId('location', 1),
400
                'status' => 1,
401
                'priority' => 0,
402
                'hidden' => false,
403
                'invisible' => false,
404
                'remoteId' => '629709ba256fe317c3ddcee35453a96a',
405
                'parentLocationId' => $this->generateId('location', 1),
406
                'pathString' => '/1/',
407
                'depth' => 0,
408
                'sortField' => 1,
409
                'sortOrder' => 1,
410
            ),
411
            $location
412
        );
413
414
        // $location->contentInfo
415
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $location->contentInfo);
416
        $this->assertPropertiesCorrect(
417
            array(
418
                'id' => $this->generateId('content', 0),
419
                'name' => 'Top Level Nodes',
420
                'sectionId' => 1,
421
                'mainLocationId' => 1,
422
                'contentTypeId' => 1,
423
                'currentVersionNo' => 1,
424
                'published' => 1,
425
                'ownerId' => 14,
426
                'modificationDate' => $legacyDateTime,
427
                'publishedDate' => $legacyDateTime,
428
                'alwaysAvailable' => 1,
429
                'remoteId' => null,
430
                'mainLanguageCode' => 'eng-GB',
431
            ),
432
            $location->contentInfo
433
        );
434
    }
435
436
    /**
437
     * Test for the loadLocation() method.
438
     *
439
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
440
     *
441
     * @see \eZ\Publish\API\Repository\LocationService::loadLocation()
442
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
443
     */
444
    public function testLoadLocationStructValues(Location $location)
445
    {
446
        $this->assertPropertiesCorrect(
447
            array(
448
                'id' => $this->generateId('location', 5),
449
                'priority' => 0,
450
                'hidden' => false,
451
                'invisible' => false,
452
                'remoteId' => '3f6d92f8044aed134f32153517850f5a',
453
                'parentLocationId' => $this->generateId('location', 1),
454
                'pathString' => '/1/5/',
455
                'depth' => 1,
456
                'sortField' => 1,
457
                'sortOrder' => 1,
458
            ),
459
            $location
460
        );
461
462
        $this->assertInstanceOf(
463
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
464
            $location->contentInfo
465
        );
466
        $this->assertEquals($this->generateId('object', 4), $location->contentInfo->id);
467
468
        // Check lazy loaded proxy on ->content
469
        $this->assertInstanceOf(
470
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
471
            $content = $location->getContent()
472
        );
473
        $this->assertEquals(4, $content->contentInfo->id);
474
    }
475
476
    public function testLoadLocationPrioritizedLanguagesFallback()
477
    {
478
        $repository = $this->getRepository();
479
480
        // Add a language
481
        $languageService = $repository->getContentLanguageService();
482
        $languageStruct = $languageService->newLanguageCreateStruct();
483
        $languageStruct->name = 'Norsk';
484
        $languageStruct->languageCode = 'nor-NO';
485
        $languageService->createLanguage($languageStruct);
486
487
        $locationService = $repository->getLocationService();
488
        $contentService = $repository->getContentService();
489
        $location = $locationService->loadLocation(5);
490
491
        // Translate "Users"
492
        $draft = $contentService->createContentDraft($location->contentInfo);
493
        $struct = $contentService->newContentUpdateStruct();
494
        $struct->setField('name', 'Brukere', 'nor-NO');
495
        $draft = $contentService->updateContent($draft->getVersionInfo(), $struct);
496
        $contentService->publishVersion($draft->getVersionInfo());
497
498
        // Load with prioritc language (fallback will be the old one)
499
        $location = $locationService->loadLocation(5, ['nor-NO']);
500
501
        $this->assertInstanceOf(
502
            Location::class,
503
            $location
504
        );
505
        self::assertEquals(5, $location->id);
506
        $this->assertInstanceOf(
507
            Content::class,
508
            $content = $location->getContent()
509
        );
510
        $this->assertEquals(4, $content->contentInfo->id);
511
512
        $this->assertEquals($content->getVersionInfo()->getName(), 'Brukere');
513
        $this->assertEquals($content->getVersionInfo()->getName('eng-US'), 'Users');
514
    }
515
516
    /**
517
     * Test for the loadLocation() method.
518
     *
519
     * @see \eZ\Publish\API\Repository\LocationService::loadLocation()
520
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
521
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
522
     */
523
    public function testLoadLocationThrowsNotFoundException()
524
    {
525
        $repository = $this->getRepository();
526
527
        $nonExistentLocationId = $this->generateId('location', 2342);
528
        /* BEGIN: Use Case */
529
        $locationService = $repository->getLocationService();
530
531
        // Throws exception, if Location with $nonExistentLocationId does not
532
        // exist
533
        $location = $locationService->loadLocation($nonExistentLocationId);
0 ignored issues
show
Unused Code introduced by
$location is not used, you could remove the assignment.

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

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

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

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

Loading history...
534
        /* END: Use Case */
535
    }
536
537
    /**
538
     * Test for the loadLocationByRemoteId() method.
539
     *
540
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationByRemoteId()
541
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
542
     */
543 View Code Duplication
    public function testLoadLocationByRemoteId()
544
    {
545
        $repository = $this->getRepository();
546
547
        /* BEGIN: Use Case */
548
        $locationService = $repository->getLocationService();
549
550
        $location = $locationService->loadLocationByRemoteId(
551
            '3f6d92f8044aed134f32153517850f5a'
552
        );
553
        /* END: Use Case */
554
555
        $this->assertEquals(
556
            $locationService->loadLocation($this->generateId('location', 5)),
557
            $location
558
        );
559
    }
560
561
    /**
562
     * Test for the loadLocationByRemoteId() method.
563
     *
564
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationByRemoteId()
565
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
566
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
567
     */
568
    public function testLoadLocationByRemoteIdThrowsNotFoundException()
569
    {
570
        $repository = $this->getRepository();
571
572
        /* BEGIN: Use Case */
573
        $locationService = $repository->getLocationService();
574
575
        // Throws exception, since Location with remote ID does not exist
576
        $location = $locationService->loadLocationByRemoteId(
0 ignored issues
show
Unused Code introduced by
$location is not used, you could remove the assignment.

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

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

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

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

Loading history...
577
            'not-exists'
578
        );
579
        /* END: Use Case */
580
    }
581
582
    /**
583
     * Test for the loadLocations() method.
584
     *
585
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
586
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
587
     */
588
    public function testLoadLocations()
589
    {
590
        $repository = $this->getRepository();
591
592
        $contentId = $this->generateId('object', 4);
593
        /* BEGIN: Use Case */
594
        // $contentId contains the ID of an existing content object
595
        $contentService = $repository->getContentService();
596
        $locationService = $repository->getLocationService();
597
598
        $contentInfo = $contentService->loadContentInfo($contentId);
599
600
        $locations = $locationService->loadLocations($contentInfo);
601
        /* END: Use Case */
602
603
        $this->assertInternalType('array', $locations);
604
        self::assertNotEmpty($locations);
605
606
        foreach ($locations as $location) {
607
            self::assertInstanceOf(Location::class, $location);
608
            self::assertEquals($contentInfo->id, $location->getContentInfo()->id);
609
        }
610
611
        return $locations;
612
    }
613
614
    /**
615
     * Test for the loadLocations() method.
616
     *
617
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
618
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
619
     */
620
    public function testLoadLocationsContent(array $locations)
621
    {
622
        $repository = $this->getRepository();
623
        $locationService = $repository->getLocationService();
0 ignored issues
show
Unused Code introduced by
$locationService is not used, you could remove the assignment.

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

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

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

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

Loading history...
624
625
        $this->assertEquals(1, count($locations));
626
        foreach ($locations as $loadedLocation) {
627
            $this->assertInstanceOf(
628
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
629
                $loadedLocation
630
            );
631
        }
632
633
        usort(
634
            $locations,
635
            function ($a, $b) {
636
                strcmp($a->id, $b->id);
637
            }
638
        );
639
640
        $this->assertEquals(
641
            array($this->generateId('location', 5)),
642
            array_map(
643
                function (Location $location) {
644
                    return $location->id;
645
                },
646
                $locations
647
            )
648
        );
649
    }
650
651
    /**
652
     * Test for the loadLocations() method.
653
     *
654
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
655
     *
656
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations($contentInfo, $rootLocation)
657
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
658
     */
659
    public function testLoadLocationsLimitedSubtree()
660
    {
661
        $repository = $this->getRepository();
662
663
        $originalLocationId = $this->generateId('location', 54);
664
        $originalParentLocationId = $this->generateId('location', 48);
665
        $newParentLocationId = $this->generateId('location', 43);
666
        /* BEGIN: Use Case */
667
        // $originalLocationId is the ID of an existing location
668
        // $originalParentLocationId is the ID of the parent location of
669
        //     $originalLocationId
670
        // $newParentLocationId is the ID of an existing location outside the tree
671
        // of $originalLocationId and $originalParentLocationId
672
        $locationService = $repository->getLocationService();
673
674
        // Location at "/1/48/54"
675
        $originalLocation = $locationService->loadLocation($originalLocationId);
676
677
        // Create location under "/1/43/"
678
        $locationCreate = $locationService->newLocationCreateStruct($newParentLocationId);
679
        $locationService->createLocation(
680
            $originalLocation->contentInfo,
681
            $locationCreate
682
        );
683
684
        $findRootLocation = $locationService->loadLocation($originalParentLocationId);
685
686
        // Returns an array with only $originalLocation
687
        $locations = $locationService->loadLocations(
688
            $originalLocation->contentInfo,
689
            $findRootLocation
690
        );
691
        /* END: Use Case */
692
693
        $this->assertInternalType('array', $locations);
694
695
        return $locations;
696
    }
697
698
    /**
699
     * Test for the loadLocations() method.
700
     *
701
     * @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
702
     *
703
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
704
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationsLimitedSubtree
705
     */
706
    public function testLoadLocationsLimitedSubtreeContent(array $locations)
707
    {
708
        $this->assertEquals(1, count($locations));
709
710
        $this->assertEquals(
711
            $this->generateId('location', 54),
712
            reset($locations)->id
713
        );
714
    }
715
716
    /**
717
     * Test for the loadLocations() method.
718
     *
719
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
720
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
721
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
722
     */
723 View Code Duplication
    public function testLoadLocationsThrowsBadStateException()
724
    {
725
        $repository = $this->getRepository();
726
727
        /* BEGIN: Use Case */
728
        $contentTypeService = $repository->getContentTypeService();
729
        $contentService = $repository->getContentService();
730
        $locationService = $repository->getLocationService();
731
732
        // Create new content, which is not published
733
        $folderType = $contentTypeService->loadContentTypeByIdentifier('folder');
734
        $contentCreate = $contentService->newContentCreateStruct($folderType, 'eng-US');
735
        $contentCreate->setField('name', 'New Folder');
736
        $content = $contentService->createContent($contentCreate);
737
738
        // Throws Exception, since $content has no published version, yet
739
        $locationService->loadLocations(
740
            $content->contentInfo
741
        );
742
        /* END: Use Case */
743
    }
744
745
    /**
746
     * Test for the loadLocations() method.
747
     *
748
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations($contentInfo, $rootLocation)
749
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocations
750
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
751
     */
752
    public function testLoadLocationsThrowsBadStateExceptionLimitedSubtree()
753
    {
754
        $repository = $this->getRepository();
755
756
        $someLocationId = $this->generateId('location', 2);
757
        /* BEGIN: Use Case */
758
        // $someLocationId is the ID of an existing location
759
        $contentTypeService = $repository->getContentTypeService();
760
        $contentService = $repository->getContentService();
761
        $locationService = $repository->getLocationService();
762
763
        // Create new content, which is not published
764
        $folderType = $contentTypeService->loadContentTypeByIdentifier('folder');
765
        $contentCreate = $contentService->newContentCreateStruct($folderType, 'eng-US');
766
        $contentCreate->setField('name', 'New Folder');
767
        $content = $contentService->createContent($contentCreate);
768
769
        $findRootLocation = $locationService->loadLocation($someLocationId);
770
771
        // Throws Exception, since $content has no published version, yet
772
        $locationService->loadLocations(
773
            $content->contentInfo,
774
            $findRootLocation
775
        );
776
        /* END: Use Case */
777
    }
778
779
    /**
780
     * Test for the loadLocationChildren() method.
781
     *
782
     * @covers \eZ\Publish\API\Repository\LocationService::loadLocationChildren
783
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
784
     */
785
    public function testLoadLocationChildren()
786
    {
787
        $repository = $this->getRepository();
788
789
        $locationId = $this->generateId('location', 5);
790
        /* BEGIN: Use Case */
791
        // $locationId is the ID of an existing location
792
        $locationService = $repository->getLocationService();
793
794
        $location = $locationService->loadLocation($locationId);
795
796
        $childLocations = $locationService->loadLocationChildren($location);
797
        /* END: Use Case */
798
799
        $this->assertInstanceOf(LocationList::class, $childLocations);
800
        $this->assertInternalType('array', $childLocations->locations);
801
        $this->assertNotEmpty($childLocations->locations);
802
        $this->assertInternalType('int', $childLocations->totalCount);
803
804
        foreach ($childLocations->locations as $childLocation) {
805
            $this->assertInstanceOf(Location::class, $childLocation);
806
            $this->assertEquals($location->id, $childLocation->parentLocationId);
807
        }
808
809
        return $childLocations;
810
    }
811
812
    /**
813
     * Test loading parent Locations for draft Content.
814
     *
815
     * @covers \eZ\Publish\API\Repository\LocationService::loadParentLocationsForDraftContent
816
     */
817
    public function testLoadParentLocationsForDraftContent()
818
    {
819
        $repository = $this->getRepository();
820
        $locationService = $repository->getLocationService();
821
        $contentService = $repository->getContentService();
822
        $contentTypeService = $repository->getContentTypeService();
823
824
        // prepare locations
825
        $locationCreateStructs = [
826
            $locationService->newLocationCreateStruct(2),
827
            $locationService->newLocationCreateStruct(5),
828
        ];
829
830
        // Create new content
831
        $folderType = $contentTypeService->loadContentTypeByIdentifier('folder');
832
        $contentCreate = $contentService->newContentCreateStruct($folderType, 'eng-US');
833
        $contentCreate->setField('name', 'New Folder');
834
        $contentDraft = $contentService->createContent($contentCreate, $locationCreateStructs);
835
836
        // Test loading parent Locations
837
        $locations = $locationService->loadParentLocationsForDraftContent($contentDraft->versionInfo);
838
839
        self::assertCount(2, $locations);
840
        foreach ($locations as $location) {
841
            // test it is one of the given parent locations
842
            self::assertTrue($location->id === 2 || $location->id === 5);
843
        }
844
845
        return $contentDraft;
846
    }
847
848
    /**
849
     * Test that trying to load parent Locations throws Exception if Content is not a draft.
850
     *
851
     * @depends testLoadParentLocationsForDraftContent
852
     *
853
     * @param \eZ\Publish\API\Repository\Values\Content\Content $contentDraft
854
     */
855
    public function testLoadParentLocationsForDraftContentThrowsBadStateException(Content $contentDraft)
856
    {
857
        $this->expectException(BadStateException::class);
858
        $this->expectExceptionMessageRegExp('/has been already published/');
859
860
        $repository = $this->getRepository(false);
861
        $locationService = $repository->getLocationService();
862
        $contentService = $repository->getContentService();
863
864
        $content = $contentService->publishVersion($contentDraft->versionInfo);
865
866
        $locationService->loadParentLocationsForDraftContent($content->versionInfo);
867
    }
868
869
    /**
870
     * Test for the getLocationChildCount() method.
871
     *
872
     * @see \eZ\Publish\API\Repository\LocationService::getLocationChildCount()
873
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
874
     */
875
    public function testGetLocationChildCount()
876
    {
877
        // $locationId is the ID of an existing location
878
        $locationService = $this->getRepository()->getLocationService();
879
880
        $this->assertSame(
881
            5,
882
            $locationService->getLocationChildCount(
883
                $locationService->loadLocation($this->generateId('location', 5))
884
            )
885
        );
886
    }
887
888
    /**
889
     * Test for the loadLocationChildren() method.
890
     *
891
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren()
892
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
893
     */
894
    public function testLoadLocationChildrenData(LocationList $locations)
895
    {
896
        $this->assertEquals(5, count($locations->locations));
897
        $this->assertEquals(5, $locations->totalCount);
898
899
        foreach ($locations->locations as $location) {
900
            $this->assertInstanceOf(
901
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
902
                $location
903
            );
904
        }
905
906
        $this->assertEquals(
907
            array(
908
                $this->generateId('location', 12),
909
                $this->generateId('location', 13),
910
                $this->generateId('location', 14),
911
                $this->generateId('location', 44),
912
                $this->generateId('location', 61),
913
            ),
914
            array_map(
915
                function (Location $location) {
916
                    return $location->id;
917
                },
918
                $locations->locations
919
            )
920
        );
921
    }
922
923
    /**
924
     * Test for the loadLocationChildren() method.
925
     *
926
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
927
     *
928
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset)
929
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
930
     */
931 View Code Duplication
    public function testLoadLocationChildrenWithOffset()
932
    {
933
        $repository = $this->getRepository();
934
935
        $locationId = $this->generateId('location', 5);
936
        /* BEGIN: Use Case */
937
        // $locationId is the ID of an existing location
938
        $locationService = $repository->getLocationService();
939
940
        $location = $locationService->loadLocation($locationId);
941
942
        $childLocations = $locationService->loadLocationChildren($location, 2);
943
        /* END: Use Case */
944
945
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\LocationList', $childLocations);
946
        $this->assertInternalType('array', $childLocations->locations);
947
        $this->assertInternalType('int', $childLocations->totalCount);
948
949
        return $childLocations;
950
    }
951
952
    /**
953
     * Test for the loadLocationChildren() method.
954
     *
955
     * @param \eZ\Publish\API\Repository\Values\Content\LocationList $locations
956
     *
957
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset)
958
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildrenWithOffset
959
     */
960 View Code Duplication
    public function testLoadLocationChildrenDataWithOffset(LocationList $locations)
961
    {
962
        $this->assertEquals(3, count($locations->locations));
963
        $this->assertEquals(5, $locations->totalCount);
964
965
        foreach ($locations->locations as $location) {
966
            $this->assertInstanceOf(
967
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
968
                $location
969
            );
970
        }
971
972
        $this->assertEquals(
973
            array(
974
                $this->generateId('location', 14),
975
                $this->generateId('location', 44),
976
                $this->generateId('location', 61),
977
            ),
978
            array_map(
979
                function (Location $location) {
980
                    return $location->id;
981
                },
982
                $locations->locations
983
            )
984
        );
985
    }
986
987
    /**
988
     * Test for the loadLocationChildren() method.
989
     *
990
     * @return \eZ\Publish\API\Repository\Values\Content\Location[]
991
     *
992
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset, $limit)
993
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
994
     */
995 View Code Duplication
    public function testLoadLocationChildrenWithOffsetAndLimit()
996
    {
997
        $repository = $this->getRepository();
998
999
        $locationId = $this->generateId('location', 5);
1000
        /* BEGIN: Use Case */
1001
        // $locationId is the ID of an existing location
1002
        $locationService = $repository->getLocationService();
1003
1004
        $location = $locationService->loadLocation($locationId);
1005
1006
        $childLocations = $locationService->loadLocationChildren($location, 2, 2);
1007
        /* END: Use Case */
1008
1009
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\LocationList', $childLocations);
1010
        $this->assertInternalType('array', $childLocations->locations);
1011
        $this->assertInternalType('int', $childLocations->totalCount);
1012
1013
        return $childLocations;
1014
    }
1015
1016
    /**
1017
     * Test for the loadLocationChildren() method.
1018
     *
1019
     * @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
1020
     *
1021
     * @see \eZ\Publish\API\Repository\LocationService::loadLocationChildren($location, $offset, $limit)
1022
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildrenWithOffsetAndLimit
1023
     */
1024 View Code Duplication
    public function testLoadLocationChildrenDataWithOffsetAndLimit(LocationList $locations)
1025
    {
1026
        $this->assertEquals(2, count($locations->locations));
1027
        $this->assertEquals(5, $locations->totalCount);
1028
1029
        foreach ($locations->locations as $location) {
1030
            $this->assertInstanceOf(
1031
                '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1032
                $location
1033
            );
1034
        }
1035
1036
        $this->assertEquals(
1037
            array(
1038
                $this->generateId('location', 14),
1039
                $this->generateId('location', 44),
1040
            ),
1041
            array_map(
1042
                function (Location $location) {
1043
                    return $location->id;
1044
                },
1045
                $locations->locations
1046
            )
1047
        );
1048
    }
1049
1050
    /**
1051
     * Test for the newLocationUpdateStruct() method.
1052
     *
1053
     * @covers \eZ\Publish\API\Repository\LocationService::newLocationUpdateStruct
1054
     */
1055 View Code Duplication
    public function testNewLocationUpdateStruct()
1056
    {
1057
        $repository = $this->getRepository();
1058
1059
        /* BEGIN: Use Case */
1060
        $locationService = $repository->getLocationService();
1061
1062
        $updateStruct = $locationService->newLocationUpdateStruct();
1063
        /* END: Use Case */
1064
1065
        $this->assertInstanceOf(
1066
            LocationUpdateStruct::class,
1067
            $updateStruct
1068
        );
1069
1070
        $this->assertPropertiesCorrect(
1071
            [
1072
                'priority' => null,
1073
                'remoteId' => null,
1074
                'sortField' => null,
1075
                'sortOrder' => null,
1076
            ],
1077
            $updateStruct
1078
        );
1079
    }
1080
1081
    /**
1082
     * Test for the updateLocation() method.
1083
     *
1084
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1085
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1086
     */
1087
    public function testUpdateLocation()
1088
    {
1089
        $repository = $this->getRepository();
1090
1091
        $originalLocationId = $this->generateId('location', 5);
1092
        /* BEGIN: Use Case */
1093
        // $originalLocationId is the ID of an existing location
1094
        $locationService = $repository->getLocationService();
1095
1096
        $originalLocation = $locationService->loadLocation($originalLocationId);
1097
1098
        $updateStruct = $locationService->newLocationUpdateStruct();
1099
        $updateStruct->priority = 3;
1100
        $updateStruct->remoteId = 'c7adcbf1e96bc29bca28c2d809d0c7ef69272651';
1101
        $updateStruct->sortField = Location::SORT_FIELD_PRIORITY;
1102
        $updateStruct->sortOrder = Location::SORT_ORDER_DESC;
1103
1104
        $updatedLocation = $locationService->updateLocation($originalLocation, $updateStruct);
1105
        /* END: Use Case */
1106
1107
        $this->assertInstanceOf(
1108
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1109
            $updatedLocation
1110
        );
1111
1112
        return array(
1113
            'originalLocation' => $originalLocation,
1114
            'updateStruct' => $updateStruct,
1115
            'updatedLocation' => $updatedLocation,
1116
        );
1117
    }
1118
1119
    /**
1120
     * Test for the updateLocation() method.
1121
     *
1122
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1123
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testUpdateLocation
1124
     */
1125
    public function testUpdateLocationStructValues(array $data)
1126
    {
1127
        $originalLocation = $data['originalLocation'];
1128
        $updateStruct = $data['updateStruct'];
1129
        $updatedLocation = $data['updatedLocation'];
1130
1131
        $this->assertPropertiesCorrect(
1132
            array(
1133
                'id' => $originalLocation->id,
1134
                'priority' => $updateStruct->priority,
1135
                'hidden' => $originalLocation->hidden,
1136
                'invisible' => $originalLocation->invisible,
1137
                'remoteId' => $updateStruct->remoteId,
1138
                'contentInfo' => $originalLocation->contentInfo,
1139
                'parentLocationId' => $originalLocation->parentLocationId,
1140
                'pathString' => $originalLocation->pathString,
1141
                'depth' => $originalLocation->depth,
1142
                'sortField' => $updateStruct->sortField,
1143
                'sortOrder' => $updateStruct->sortOrder,
1144
            ),
1145
            $updatedLocation
1146
        );
1147
    }
1148
1149
    /**
1150
     * Test for the updateLocation() method.
1151
     *
1152
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1153
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1154
     */
1155
    public function testUpdateLocationWithSameRemoteId()
1156
    {
1157
        $repository = $this->getRepository();
1158
1159
        $locationId = $this->generateId('location', 5);
1160
        /* BEGIN: Use Case */
1161
        // $locationId and remote ID is the IDs of the same, existing location
1162
        $locationService = $repository->getLocationService();
1163
1164
        $originalLocation = $locationService->loadLocation($locationId);
1165
1166
        $updateStruct = $locationService->newLocationUpdateStruct();
1167
1168
        // Remote ID of an existing location with the same locationId
1169
        $updateStruct->remoteId = $originalLocation->remoteId;
1170
1171
        // Sets one of the properties to be able to confirm location gets updated, here: priority
1172
        $updateStruct->priority = 2;
1173
1174
        $location = $locationService->updateLocation($originalLocation, $updateStruct);
1175
1176
        // Checks that the location was updated
1177
        $this->assertEquals(2, $location->priority);
1178
1179
        // Checks that remoteId remains the same
1180
        $this->assertEquals($originalLocation->remoteId, $location->remoteId);
1181
        /* END: Use Case */
1182
    }
1183
1184
    /**
1185
     * Test for the updateLocation() method.
1186
     *
1187
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1188
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1189
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1190
     */
1191 View Code Duplication
    public function testUpdateLocationThrowsInvalidArgumentException()
1192
    {
1193
        $repository = $this->getRepository();
1194
1195
        $locationId = $this->generateId('location', 5);
1196
        /* BEGIN: Use Case */
1197
        // $locationId and remoteId is the IDs of an existing, but not the same, location
1198
        $locationService = $repository->getLocationService();
1199
1200
        $originalLocation = $locationService->loadLocation($locationId);
1201
1202
        $updateStruct = $locationService->newLocationUpdateStruct();
1203
1204
        // Remote ID of an existing location with a different locationId
1205
        $updateStruct->remoteId = 'f3e90596361e31d496d4026eb624c983';
1206
1207
        // Throws exception, since remote ID is already taken
1208
        $locationService->updateLocation($originalLocation, $updateStruct);
1209
        /* END: Use Case */
1210
    }
1211
1212
    /**
1213
     * Test for the updateLocation() method.
1214
     *
1215
     * @covers \eZ\Publish\API\Repository\LocationService::updateLocation()
1216
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1217
     * @dataProvider dataProviderForOutOfRangeLocationPriority
1218
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1219
     */
1220
    public function testUpdateLocationThrowsInvalidArgumentExceptionPriorityIsOutOfRange($priority)
1221
    {
1222
        $repository = $this->getRepository();
1223
1224
        $locationId = $this->generateId('location', 5);
1225
        /* BEGIN: Use Case */
1226
        // $locationId and remoteId is the IDs of an existing, but not the same, location
1227
        $locationService = $repository->getLocationService();
1228
1229
        $originalLocation = $locationService->loadLocation($locationId);
1230
1231
        $updateStruct = $locationService->newLocationUpdateStruct();
1232
1233
        // Priority value is out of range
1234
        $updateStruct->priority = $priority;
1235
1236
        // Throws exception, since remote ID is already taken
1237
        $locationService->updateLocation($originalLocation, $updateStruct);
1238
        /* END: Use Case */
1239
    }
1240
1241
    /**
1242
     * Test for the updateLocation() method.
1243
     * Ref EZP-23302: Update Location fails if no change is performed with the update.
1244
     *
1245
     * @see \eZ\Publish\API\Repository\LocationService::updateLocation()
1246
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1247
     */
1248
    public function testUpdateLocationTwice()
1249
    {
1250
        $repository = $this->getRepository();
1251
1252
        $locationId = $this->generateId('location', 5);
1253
        /* BEGIN: Use Case */
1254
        $locationService = $repository->getLocationService();
1255
        $repository->setCurrentUser($repository->getUserService()->loadUser(14));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
1256
1257
        $originalLocation = $locationService->loadLocation($locationId);
1258
1259
        $updateStruct = $locationService->newLocationUpdateStruct();
1260
        $updateStruct->priority = 42;
1261
1262
        $updatedLocation = $locationService->updateLocation($originalLocation, $updateStruct);
1263
1264
        // Repeated update with the same, unchanged struct
1265
        $secondUpdatedLocation = $locationService->updateLocation($updatedLocation, $updateStruct);
1266
        /* END: Use Case */
1267
1268
        $this->assertEquals($updatedLocation->priority, 42);
1269
        $this->assertEquals($secondUpdatedLocation->priority, 42);
1270
    }
1271
1272
    /**
1273
     * Test for the swapLocation() method.
1274
     *
1275
     * @see \eZ\Publish\API\Repository\LocationService::swapLocation()
1276
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1277
     */
1278
    public function testSwapLocation()
1279
    {
1280
        $repository = $this->getRepository();
1281
        $locationService = $repository->getLocationService();
1282
1283
        $mediaLocationId = $this->generateId('location', 43);
1284
        $demoDesignLocationId = $this->generateId('location', 56);
1285
1286
        $mediaContentInfo = $locationService->loadLocation($mediaLocationId)->getContentInfo();
1287
        $demoDesignContentInfo = $locationService->loadLocation($demoDesignLocationId)->getContentInfo();
1288
1289
        /* BEGIN: Use Case */
1290
        // $mediaLocationId is the ID of the "Media" page location in
1291
        // an eZ Publish demo installation
1292
1293
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1294
        // Publish demo installation
1295
1296
        // Load the location service
1297
        $locationService = $repository->getLocationService();
1298
1299
        $mediaLocation = $locationService->loadLocation($mediaLocationId);
1300
        $demoDesignLocation = $locationService->loadLocation($demoDesignLocationId);
1301
1302
        // Swaps the content referred to by the locations
1303
        $locationService->swapLocation($mediaLocation, $demoDesignLocation);
1304
        /* END: Use Case */
1305
1306
        // Reload Locations, IDs swapped
1307
        $demoDesignLocation = $locationService->loadLocation($mediaLocationId);
1308
        $mediaLocation = $locationService->loadLocation($demoDesignLocationId);
1309
1310
        // Assert Location's Content is updated
1311
        $this->assertEquals(
1312
            $mediaContentInfo->id,
1313
            $mediaLocation->getContentInfo()->id
1314
        );
1315
        $this->assertEquals(
1316
            $demoDesignContentInfo->id,
1317
            $demoDesignLocation->getContentInfo()->id
1318
        );
1319
1320
        // Assert URL aliases are updated
1321
        $this->assertEquals(
1322
            $mediaLocation->id,
1323
            $repository->getURLAliasService()->lookup('/Design/Media')->destination
1324
        );
1325
        $this->assertEquals(
1326
            $demoDesignLocation->id,
1327
            $repository->getURLAliasService()->lookup('/eZ-Publish-Demo-Design-without-demo-content')->destination
1328
        );
1329
    }
1330
1331
    /**
1332
     * Test swapping Main Location of a Content with another one updates Content item Main Location.
1333
     *
1334
     * @covers \eZ\Publish\API\Repository\LocationService::swapLocation
1335
     */
1336
    public function testSwapLocationUpdatesMainLocation()
1337
    {
1338
        $repository = $this->getRepository();
1339
        $locationService = $repository->getLocationService();
1340
        $contentService = $repository->getContentService();
1341
1342
        $mainLocationParentId = 60;
1343
        $secondaryLocationId = 43;
1344
1345
        $publishedContent = $this->publishContentWithParentLocation(
1346
            'Content for Swap Location Test', $mainLocationParentId
1347
        );
1348
1349
        // sanity check
1350
        $mainLocation = $locationService->loadLocation($publishedContent->contentInfo->mainLocationId);
1351
        self::assertEquals($mainLocationParentId, $mainLocation->parentLocationId);
1352
1353
        // load another pre-existing location
1354
        $secondaryLocation = $locationService->loadLocation($secondaryLocationId);
1355
1356
        // swap the Main Location with a secondary one
1357
        $locationService->swapLocation($mainLocation, $secondaryLocation);
1358
1359
        // check if Main Location has been updated
1360
        $mainLocation = $locationService->loadLocation($secondaryLocation->id);
1361
        self::assertEquals($publishedContent->contentInfo->id, $mainLocation->contentInfo->id);
1362
        self::assertEquals($mainLocation->id, $mainLocation->contentInfo->mainLocationId);
1363
1364
        $reloadedContent = $contentService->loadContentByContentInfo($publishedContent->contentInfo);
1365
        self::assertEquals($mainLocation->id, $reloadedContent->contentInfo->mainLocationId);
1366
    }
1367
1368
    /**
1369
     * Test if location swap affects related bookmarks.
1370
     *
1371
     * @covers \eZ\Publish\API\Repository\LocationService::swapLocation
1372
     */
1373
    public function testBookmarksAreSwappedAfterSwapLocation()
1374
    {
1375
        $repository = $this->getRepository();
1376
1377
        $mediaLocationId = $this->generateId('location', 43);
1378
        $demoDesignLocationId = $this->generateId('location', 56);
1379
1380
        /* BEGIN: Use Case */
1381
        $locationService = $repository->getLocationService();
1382
        $bookmarkService = $repository->getBookmarkService();
1383
1384
        $mediaLocation = $locationService->loadLocation($mediaLocationId);
1385
        $demoDesignLocation = $locationService->loadLocation($demoDesignLocationId);
1386
1387
        // Bookmark locations
1388
        $bookmarkService->createBookmark($mediaLocation);
1389
        $bookmarkService->createBookmark($demoDesignLocation);
1390
1391
        $beforeSwap = $bookmarkService->loadBookmarks();
1392
1393
        // Swaps the content referred to by the locations
1394
        $locationService->swapLocation($mediaLocation, $demoDesignLocation);
1395
1396
        $afterSwap = $bookmarkService->loadBookmarks();
1397
        /* END: Use Case */
1398
1399
        $this->assertEquals($beforeSwap->items[0]->id, $afterSwap->items[1]->id);
1400
        $this->assertEquals($beforeSwap->items[1]->id, $afterSwap->items[0]->id);
1401
    }
1402
1403
    /**
1404
     * Test for the hideLocation() method.
1405
     *
1406
     * @see \eZ\Publish\API\Repository\LocationService::hideLocation()
1407
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1408
     */
1409
    public function testHideLocation()
1410
    {
1411
        $repository = $this->getRepository();
1412
1413
        $locationId = $this->generateId('location', 5);
1414
        /* BEGIN: Use Case */
1415
        // $locationId is the ID of an existing location
1416
        $locationService = $repository->getLocationService();
1417
1418
        $visibleLocation = $locationService->loadLocation($locationId);
1419
1420
        $hiddenLocation = $locationService->hideLocation($visibleLocation);
1421
        /* END: Use Case */
1422
1423
        $this->assertInstanceOf(
1424
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1425
            $hiddenLocation
1426
        );
1427
1428
        $this->assertTrue(
1429
            $hiddenLocation->hidden,
1430
            sprintf(
1431
                'Location with ID "%s" not hidden.',
1432
                $hiddenLocation->id
1433
            )
1434
        );
1435
1436
        $this->refreshSearch($repository);
1437
1438
        foreach ($locationService->loadLocationChildren($hiddenLocation)->locations as $child) {
1439
            $this->assertSubtreeProperties(
1440
                array('invisible' => true),
1441
                $child
1442
            );
1443
        }
1444
    }
1445
1446
    /**
1447
     * Assert that $expectedValues are set in the subtree starting at $location.
1448
     *
1449
     * @param array $expectedValues
1450
     * @param Location $location
1451
     */
1452
    protected function assertSubtreeProperties(array $expectedValues, Location $location, $stopId = null)
1453
    {
1454
        $repository = $this->getRepository();
1455
        $locationService = $repository->getLocationService();
1456
1457
        if ($location->id === $stopId) {
1458
            return;
1459
        }
1460
1461
        foreach ($expectedValues as $propertyName => $propertyValue) {
1462
            $this->assertEquals(
1463
                $propertyValue,
1464
                $location->$propertyName
1465
            );
1466
1467
            foreach ($locationService->loadLocationChildren($location)->locations as $child) {
1468
                $this->assertSubtreeProperties($expectedValues, $child);
1469
            }
1470
        }
1471
    }
1472
1473
    /**
1474
     * Test for the unhideLocation() method.
1475
     *
1476
     * @see \eZ\Publish\API\Repository\LocationService::unhideLocation()
1477
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testHideLocation
1478
     */
1479
    public function testUnhideLocation()
1480
    {
1481
        $repository = $this->getRepository();
1482
1483
        $locationId = $this->generateId('location', 5);
1484
        /* BEGIN: Use Case */
1485
        // $locationId is the ID of an existing location
1486
        $locationService = $repository->getLocationService();
1487
1488
        $visibleLocation = $locationService->loadLocation($locationId);
1489
        $hiddenLocation = $locationService->hideLocation($visibleLocation);
1490
1491
        $unHiddenLocation = $locationService->unhideLocation($hiddenLocation);
1492
        /* END: Use Case */
1493
1494
        $this->assertInstanceOf(
1495
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1496
            $unHiddenLocation
1497
        );
1498
1499
        $this->assertFalse(
1500
            $unHiddenLocation->hidden,
1501
            sprintf(
1502
                'Location with ID "%s" not unhidden.',
1503
                $unHiddenLocation->id
1504
            )
1505
        );
1506
1507
        $this->refreshSearch($repository);
1508
1509
        foreach ($locationService->loadLocationChildren($unHiddenLocation)->locations as $child) {
1510
            $this->assertSubtreeProperties(
1511
                array('invisible' => false),
1512
                $child
1513
            );
1514
        }
1515
    }
1516
1517
    /**
1518
     * Test for the unhideLocation() method.
1519
     *
1520
     * @see \eZ\Publish\API\Repository\LocationService::unhideLocation()
1521
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testUnhideLocation
1522
     */
1523
    public function testUnhideLocationNotUnhidesHiddenSubtree()
1524
    {
1525
        $repository = $this->getRepository();
1526
1527
        $higherLocationId = $this->generateId('location', 5);
1528
        $lowerLocationId = $this->generateId('location', 13);
1529
        /* BEGIN: Use Case */
1530
        // $higherLocationId is the ID of a location
1531
        // $lowerLocationId is the ID of a location below $higherLocationId
1532
        $locationService = $repository->getLocationService();
1533
1534
        $higherLocation = $locationService->loadLocation($higherLocationId);
1535
        $hiddenHigherLocation = $locationService->hideLocation($higherLocation);
1536
1537
        $lowerLocation = $locationService->loadLocation($lowerLocationId);
1538
        $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...
1539
1540
        $unHiddenHigherLocation = $locationService->unhideLocation($hiddenHigherLocation);
1541
        /* END: Use Case */
1542
1543
        $this->assertInstanceOf(
1544
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1545
            $unHiddenHigherLocation
1546
        );
1547
1548
        $this->assertFalse(
1549
            $unHiddenHigherLocation->hidden,
1550
            sprintf(
1551
                'Location with ID "%s" not unhidden.',
1552
                $unHiddenHigherLocation->id
1553
            )
1554
        );
1555
1556
        $this->refreshSearch($repository);
1557
1558
        foreach ($locationService->loadLocationChildren($unHiddenHigherLocation)->locations as $child) {
1559
            $this->assertSubtreeProperties(
1560
                array('invisible' => false),
1561
                $child,
1562
                $this->generateId('location', 13)
1563
            );
1564
        }
1565
1566
        $stillHiddenLocation = $locationService->loadLocation($this->generateId('location', 13));
1567
        $this->assertTrue(
1568
            $stillHiddenLocation->hidden,
1569
            sprintf(
1570
                'Hidden sub-location with ID %s accidentally unhidden.',
1571
                $stillHiddenLocation->id
1572
            )
1573
        );
1574
        foreach ($locationService->loadLocationChildren($stillHiddenLocation)->locations as $child) {
1575
            $this->assertSubtreeProperties(
1576
                array('invisible' => true),
1577
                $child
1578
            );
1579
        }
1580
    }
1581
1582
    /**
1583
     * Test for the deleteLocation() method.
1584
     *
1585
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1586
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1587
     */
1588
    public function testDeleteLocation()
1589
    {
1590
        $repository = $this->getRepository();
1591
1592
        $mediaLocationId = $this->generateId('location', 43);
1593
        /* BEGIN: Use Case */
1594
        // $mediaLocationId is the ID of the location of the
1595
        // "Media" location in an eZ Publish demo installation
1596
        $locationService = $repository->getLocationService();
1597
1598
        $location = $locationService->loadLocation($mediaLocationId);
1599
1600
        $locationService->deleteLocation($location);
1601
        /* END: Use Case */
1602
1603
        try {
1604
            $locationService->loadLocation($mediaLocationId);
1605
            $this->fail("Location $mediaLocationId not deleted.");
1606
        } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1607
        }
1608
1609
        // The following IDs are IDs of child locations of $mediaLocationId location
1610
        // ( Media/Images, Media/Files, Media/Multimedia respectively )
1611
        foreach (array(51, 52, 53) as $childLocationId) {
1612
            try {
1613
                $locationService->loadLocation($this->generateId('location', $childLocationId));
1614
                $this->fail("Location $childLocationId not deleted.");
1615
            } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1616
            }
1617
        }
1618
1619
        // The following IDs are IDs of content below $mediaLocationId location
1620
        // ( Media/Images, Media/Files, Media/Multimedia respectively )
1621
        $contentService = $this->getRepository()->getContentService();
1622
        foreach (array(49, 50, 51) as $childContentId) {
1623
            try {
1624
                $contentService->loadContentInfo($this->generateId('object', $childContentId));
1625
                $this->fail("Content $childContentId not deleted.");
1626
            } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1627
            }
1628
        }
1629
    }
1630
1631
    /**
1632
     * Test for the deleteLocation() method.
1633
     *
1634
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1635
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testDeleteLocation
1636
     */
1637
    public function testDeleteLocationDecrementsChildCountOnParent()
1638
    {
1639
        $repository = $this->getRepository();
1640
1641
        $mediaLocationId = $this->generateId('location', 43);
1642
        /* BEGIN: Use Case */
1643
        // $mediaLocationId is the ID of the location of the
1644
        // "Media" location in an eZ Publish demo installation
1645
1646
        $locationService = $repository->getLocationService();
1647
1648
        // Load the current the user group location
1649
        $location = $locationService->loadLocation($mediaLocationId);
1650
1651
        // Load the parent location
1652
        $parentLocation = $locationService->loadLocation(
1653
            $location->parentLocationId
1654
        );
1655
1656
        // Get child count
1657
        $childCountBefore = $locationService->getLocationChildCount($parentLocation);
1658
1659
        // Delete the user group location
1660
        $locationService->deleteLocation($location);
1661
1662
        $this->refreshSearch($repository);
1663
1664
        // Reload parent location
1665
        $parentLocation = $locationService->loadLocation(
1666
            $location->parentLocationId
1667
        );
1668
1669
        // This will be $childCountBefore - 1
1670
        $childCountAfter = $locationService->getLocationChildCount($parentLocation);
1671
        /* END: Use Case */
1672
1673
        $this->assertEquals($childCountBefore - 1, $childCountAfter);
1674
    }
1675
1676
    /**
1677
     * Test for the deleteLocation() method.
1678
     *
1679
     * Related issue: EZP-21904
1680
     *
1681
     * @see \eZ\Publish\API\Repository\LocationService::deleteLocation()
1682
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1683
     */
1684
    public function testDeleteContentObjectLastLocation()
1685
    {
1686
        $repository = $this->getRepository();
1687
1688
        /* BEGIN: Use case */
1689
        $contentService = $repository->getContentService();
1690
        $locationService = $repository->getLocationService();
1691
        $contentTypeService = $repository->getContentTypeService();
1692
        $urlAliasService = $repository->getURLAliasService();
1693
1694
        // prepare Content object
1695
        $createStruct = $contentService->newContentCreateStruct(
1696
            $contentTypeService->loadContentTypeByIdentifier('folder'),
1697
            'eng-GB'
1698
        );
1699
        $createStruct->setField('name', 'Test folder');
1700
1701
        // creata Content object
1702
        $content = $contentService->publishVersion(
1703
            $contentService->createContent(
1704
                $createStruct,
1705
                array($locationService->newLocationCreateStruct(2))
1706
            )->versionInfo
1707
        );
1708
1709
        // delete location
1710
        $locationService->deleteLocation(
1711
            $locationService->loadLocation(
1712
                $urlAliasService->lookup('/Test-folder')->destination
1713
            )
1714
        );
1715
1716
        // this should throw a not found exception
1717
        $contentService->loadContent($content->versionInfo->contentInfo->id);
1718
        /* END: Use case*/
1719
    }
1720
1721
    /**
1722
     * Test for the copySubtree() method.
1723
     *
1724
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1725
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1726
     */
1727
    public function testCopySubtree()
1728
    {
1729
        $repository = $this->getRepository();
1730
1731
        $mediaLocationId = $this->generateId('location', 43);
1732
        $demoDesignLocationId = $this->generateId('location', 56);
1733
        /* BEGIN: Use Case */
1734
        // $mediaLocationId is the ID of the "Media" page location in
1735
        // an eZ Publish demo installation
1736
1737
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1738
        // Publish demo installation
1739
1740
        // Load the location service
1741
        $locationService = $repository->getLocationService();
1742
1743
        // Load location to copy
1744
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1745
1746
        // Load new parent location
1747
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1748
1749
        // Copy location "Media" to "Demo Design"
1750
        $copiedLocation = $locationService->copySubtree(
1751
            $locationToCopy,
1752
            $newParentLocation
1753
        );
1754
        /* END: Use Case */
1755
1756
        $this->assertInstanceOf(
1757
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1758
            $copiedLocation
1759
        );
1760
1761
        $this->assertPropertiesCorrect(
1762
            array(
1763
                'depth' => $newParentLocation->depth + 1,
1764
                'parentLocationId' => $newParentLocation->id,
1765
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $copiedLocation->id) . '/',
1766
            ),
1767
            $copiedLocation
1768
        );
1769
1770
        $this->assertDefaultContentStates($copiedLocation->contentInfo);
1771
    }
1772
1773
    /**
1774
     * Test for the copySubtree() method.
1775
     *
1776
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1777
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1778
     */
1779
    public function testCopySubtreeWithAliases()
1780
    {
1781
        $repository = $this->getRepository();
1782
        $urlAliasService = $repository->getURLAliasService();
1783
1784
        // $mediaLocationId is the ID of the "Media" page location in
1785
        // an eZ Publish demo installation
1786
1787
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1788
        // Publish demo installation
1789
        $mediaLocationId = $this->generateId('location', 43);
1790
        $demoDesignLocationId = $this->generateId('location', 56);
1791
1792
        $locationService = $repository->getLocationService();
1793
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1794
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1795
1796
        $expectedSubItemAliases = [
1797
            '/Design/Plain-site/Media/Multimedia',
1798
            '/Design/Plain-site/Media/Images',
1799
            '/Design/Plain-site/Media/Files',
1800
        ];
1801
1802
        $this->assertAliasesBeforeCopy($urlAliasService, $expectedSubItemAliases);
1803
1804
        // Copy location "Media" to "Design"
1805
        $locationService->copySubtree(
1806
            $locationToCopy,
1807
            $newParentLocation
1808
        );
1809
1810
        $this->assertGeneratedAliases($urlAliasService, $expectedSubItemAliases);
1811
    }
1812
1813
    /**
1814
     * Asserts that given Content has default ContentStates.
1815
     *
1816
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
1817
     */
1818 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
1819
    {
1820
        $repository = $this->getRepository();
1821
        $objectStateService = $repository->getObjectStateService();
1822
1823
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
1824
1825
        foreach ($objectStateGroups as $objectStateGroup) {
1826
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
1827
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
1828
                // Only check the first object state which is the default one.
1829
                $this->assertEquals(
1830
                    $objectState,
1831
                    $contentState
1832
                );
1833
                break;
1834
            }
1835
        }
1836
    }
1837
1838
    /**
1839
     * Test for the copySubtree() method.
1840
     *
1841
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1842
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1843
     */
1844
    public function testCopySubtreeUpdatesSubtreeProperties()
1845
    {
1846
        $repository = $this->getRepository();
1847
        $locationService = $repository->getLocationService();
1848
1849
        $locationToCopy = $locationService->loadLocation($this->generateId('location', 43));
1850
1851
        // Load Subtree properties before copy
1852
        $expected = $this->loadSubtreeProperties($locationToCopy);
1853
1854
        $mediaLocationId = $this->generateId('location', 43);
1855
        $demoDesignLocationId = $this->generateId('location', 56);
1856
        /* BEGIN: Use Case */
1857
        // $mediaLocationId is the ID of the "Media" page location in
1858
        // an eZ Publish demo installation
1859
1860
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1861
        // Publish demo installation
1862
1863
        // Load the location service
1864
        $locationService = $repository->getLocationService();
1865
1866
        // Load location to copy
1867
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1868
1869
        // Load new parent location
1870
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1871
1872
        // Copy location "Media" to "Demo Design"
1873
        $copiedLocation = $locationService->copySubtree(
1874
            $locationToCopy,
1875
            $newParentLocation
1876
        );
1877
        /* END: Use Case */
1878
1879
        $beforeIds = array();
1880
        foreach ($expected as $properties) {
1881
            $beforeIds[] = $properties['id'];
1882
        }
1883
1884
        $this->refreshSearch($repository);
1885
1886
        // Load Subtree properties after copy
1887
        $actual = $this->loadSubtreeProperties($copiedLocation);
1888
1889
        $this->assertEquals(count($expected), count($actual));
1890
1891
        foreach ($actual as $properties) {
1892
            $this->assertNotContains($properties['id'], $beforeIds);
1893
            $this->assertStringStartsWith(
1894
                "{$newParentLocation->pathString}" . $this->parseId('location', $copiedLocation->id) . '/',
1895
                $properties['pathString']
1896
            );
1897
            $this->assertStringEndsWith(
1898
                '/' . $this->parseId('location', $properties['id']) . '/',
1899
                $properties['pathString']
1900
            );
1901
        }
1902
    }
1903
1904
    /**
1905
     * Test for the copySubtree() method.
1906
     *
1907
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1908
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1909
     */
1910
    public function testCopySubtreeIncrementsChildCountOfNewParent()
1911
    {
1912
        $repository = $this->getRepository();
1913
        $locationService = $repository->getLocationService();
1914
1915
        $childCountBefore = $locationService->getLocationChildCount($locationService->loadLocation(56));
1916
1917
        $mediaLocationId = $this->generateId('location', 43);
1918
        $demoDesignLocationId = $this->generateId('location', 56);
1919
        /* BEGIN: Use Case */
1920
        // $mediaLocationId is the ID of the "Media" page location in
1921
        // an eZ Publish demo installation
1922
1923
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1924
        // Publish demo installation
1925
1926
        // Load the location service
1927
        $locationService = $repository->getLocationService();
1928
1929
        // Load location to copy
1930
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1931
1932
        // Load new parent location
1933
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1934
1935
        // Copy location "Media" to "Demo Design"
1936
        $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...
1937
            $locationToCopy,
1938
            $newParentLocation
1939
        );
1940
        /* END: Use Case */
1941
1942
        $this->refreshSearch($repository);
1943
1944
        $childCountAfter = $locationService->getLocationChildCount($locationService->loadLocation($demoDesignLocationId));
1945
1946
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
1947
    }
1948
1949
    /**
1950
     * Test for the copySubtree() method.
1951
     *
1952
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1953
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1954
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1955
     */
1956 View Code Duplication
    public function testCopySubtreeThrowsInvalidArgumentException()
1957
    {
1958
        $repository = $this->getRepository();
1959
1960
        $communityLocationId = $this->generateId('location', 5);
1961
        /* BEGIN: Use Case */
1962
        // $communityLocationId is the ID of the "Community" page location in
1963
        // an eZ Publish demo installation
1964
1965
        // Load the location service
1966
        $locationService = $repository->getLocationService();
1967
1968
        // Load location to copy
1969
        $locationToCopy = $locationService->loadLocation($communityLocationId);
1970
1971
        // Use a child as new parent
1972
        $childLocations = $locationService->loadLocationChildren($locationToCopy)->locations;
1973
        $newParentLocation = end($childLocations);
1974
1975
        // This call will fail with an "InvalidArgumentException", because the
1976
        // new parent is a child location of the subtree to copy.
1977
        $locationService->copySubtree(
1978
            $locationToCopy,
1979
            $newParentLocation
0 ignored issues
show
Security Bug introduced by
It seems like $newParentLocation defined by end($childLocations) on line 1973 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...
1980
        );
1981
        /* END: Use Case */
1982
    }
1983
1984
    /**
1985
     * Test for the moveSubtree() method.
1986
     *
1987
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
1988
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1989
     */
1990
    public function testMoveSubtree()
1991
    {
1992
        $repository = $this->getRepository();
1993
1994
        $mediaLocationId = $this->generateId('location', 43);
1995
        $demoDesignLocationId = $this->generateId('location', 56);
1996
        /* BEGIN: Use Case */
1997
        // $mediaLocationId is the ID of the "Media" page location in
1998
        // an eZ Publish demo installation
1999
2000
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2001
        // Publish demo installation
2002
2003
        // Load the location service
2004
        $locationService = $repository->getLocationService();
2005
2006
        // Load location to move
2007
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2008
2009
        // Load new parent location
2010
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2011
2012
        // Move location from "Home" to "Demo Design"
2013
        $locationService->moveSubtree(
2014
            $locationToMove,
2015
            $newParentLocation
2016
        );
2017
2018
        // Load moved location
2019
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2020
        /* END: Use Case */
2021
2022
        $this->assertPropertiesCorrect(
2023
            array(
2024
                'hidden' => false,
2025
                'invisible' => false,
2026
                'depth' => $newParentLocation->depth + 1,
2027
                'parentLocationId' => $newParentLocation->id,
2028
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
2029
            ),
2030
            $movedLocation
2031
        );
2032
    }
2033
2034
    /**
2035
     * Test for the moveSubtree() method.
2036
     *
2037
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2038
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2039
     */
2040
    public function testMoveSubtreeHidden()
2041
    {
2042
        $repository = $this->getRepository();
2043
2044
        $mediaLocationId = $this->generateId('location', 43);
2045
        $demoDesignLocationId = $this->generateId('location', 56);
2046
        /* BEGIN: Use Case */
2047
        // $mediaLocationId is the ID of the "Media" page location in
2048
        // an eZ Publish demo installation
2049
2050
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2051
        // Publish demo installation
2052
2053
        // Load the location service
2054
        $locationService = $repository->getLocationService();
2055
2056
        // Load location to move
2057
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2058
2059
        // Load new parent location
2060
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2061
2062
        // Hide the target location before we move
2063
        $newParentLocation = $locationService->hideLocation($newParentLocation);
2064
2065
        // Move location from "Home" to "Demo Design"
2066
        $locationService->moveSubtree(
2067
            $locationToMove,
2068
            $newParentLocation
2069
        );
2070
2071
        // Load moved location
2072
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2073
        /* END: Use Case */
2074
2075
        $this->assertPropertiesCorrect(
2076
            array(
2077
                'hidden' => false,
2078
                'invisible' => true,
2079
                'depth' => $newParentLocation->depth + 1,
2080
                'parentLocationId' => $newParentLocation->id,
2081
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
2082
            ),
2083
            $movedLocation
2084
        );
2085
    }
2086
2087
    /**
2088
     * Test for the moveSubtree() method.
2089
     *
2090
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2091
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2092
     */
2093
    public function testMoveSubtreeUpdatesSubtreeProperties()
2094
    {
2095
        $repository = $this->getRepository();
2096
        $locationService = $repository->getLocationService();
2097
2098
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
2099
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
2100
2101
        // Load Subtree properties before move
2102
        $expected = $this->loadSubtreeProperties($locationToMove);
2103
        foreach ($expected as $id => $properties) {
2104
            $expected[$id]['depth'] = $properties['depth'] + 2;
2105
            $expected[$id]['pathString'] = str_replace(
2106
                $locationToMove->pathString,
2107
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
2108
                $properties['pathString']
2109
            );
2110
        }
2111
2112
        $mediaLocationId = $this->generateId('location', 43);
2113
        $demoDesignLocationId = $this->generateId('location', 56);
2114
        /* BEGIN: Use Case */
2115
        // $mediaLocationId is the ID of the "Media" page location in
2116
        // an eZ Publish demo installation
2117
2118
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2119
        // Publish demo installation
2120
2121
        // Load the location service
2122
        $locationService = $repository->getLocationService();
2123
2124
        // Load location to move
2125
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2126
2127
        // Load new parent location
2128
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2129
2130
        // Move location from "Home" to "Demo Design"
2131
        $locationService->moveSubtree(
2132
            $locationToMove,
2133
            $newParentLocation
2134
        );
2135
2136
        // Load moved location
2137
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2138
        /* END: Use Case */
2139
2140
        $this->refreshSearch($repository);
2141
2142
        // Load Subtree properties after move
2143
        $actual = $this->loadSubtreeProperties($movedLocation);
2144
2145
        $this->assertEquals($expected, $actual);
2146
    }
2147
2148
    /**
2149
     * Test for the moveSubtree() method.
2150
     *
2151
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2152
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtreeUpdatesSubtreeProperties
2153
     */
2154
    public function testMoveSubtreeUpdatesSubtreePropertiesHidden()
2155
    {
2156
        $repository = $this->getRepository();
2157
        $locationService = $repository->getLocationService();
2158
2159
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
2160
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
2161
2162
        // Hide the target location before we move
2163
        $newParentLocation = $locationService->hideLocation($newParentLocation);
2164
2165
        // Load Subtree properties before move
2166
        $expected = $this->loadSubtreeProperties($locationToMove);
2167
        foreach ($expected as $id => $properties) {
2168
            $expected[$id]['invisible'] = true;
2169
            $expected[$id]['depth'] = $properties['depth'] + 2;
2170
            $expected[$id]['pathString'] = str_replace(
2171
                $locationToMove->pathString,
2172
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
2173
                $properties['pathString']
2174
            );
2175
        }
2176
2177
        $mediaLocationId = $this->generateId('location', 43);
2178
        $demoDesignLocationId = $this->generateId('location', 56);
2179
        /* BEGIN: Use Case */
2180
        // $mediaLocationId is the ID of the "Media" page location in
2181
        // an eZ Publish demo installation
2182
2183
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2184
        // Publish demo installation
2185
2186
        // Load the location service
2187
        $locationService = $repository->getLocationService();
2188
2189
        // Load location to move
2190
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2191
2192
        // Load new parent location
2193
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2194
2195
        // Move location from "Home" to "Demo Design"
2196
        $locationService->moveSubtree(
2197
            $locationToMove,
2198
            $newParentLocation
2199
        );
2200
2201
        // Load moved location
2202
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2203
        /* END: Use Case */
2204
2205
        $this->refreshSearch($repository);
2206
2207
        // Load Subtree properties after move
2208
        $actual = $this->loadSubtreeProperties($movedLocation);
2209
2210
        $this->assertEquals($expected, $actual);
2211
    }
2212
2213
    /**
2214
     * Test for the moveSubtree() method.
2215
     *
2216
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2217
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2218
     */
2219 View Code Duplication
    public function testMoveSubtreeIncrementsChildCountOfNewParent()
2220
    {
2221
        $repository = $this->getRepository();
2222
        $locationService = $repository->getLocationService();
2223
2224
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
2225
2226
        // Load expected properties before move
2227
        $expected = $this->loadLocationProperties($newParentLocation);
2228
        $childCountBefore = $locationService->getLocationChildCount($newParentLocation);
2229
2230
        $mediaLocationId = $this->generateId('location', 43);
2231
        $demoDesignLocationId = $this->generateId('location', 56);
2232
        /* BEGIN: Use Case */
2233
        // $mediaLocationId is the ID of the "Media" page location in
2234
        // an eZ Publish demo installation
2235
2236
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2237
        // Publish demo installation
2238
2239
        // Load the location service
2240
        $locationService = $repository->getLocationService();
2241
2242
        // Load location to move
2243
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2244
2245
        // Load new parent location
2246
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2247
2248
        // Move location from "Home" to "Demo Design"
2249
        $locationService->moveSubtree(
2250
            $locationToMove,
2251
            $newParentLocation
2252
        );
2253
2254
        // Load moved location
2255
        $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...
2256
2257
        // Reload new parent location
2258
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2259
        /* END: Use Case */
2260
2261
        $this->refreshSearch($repository);
2262
2263
        // Load Subtree properties after move
2264
        $actual = $this->loadLocationProperties($newParentLocation);
2265
        $childCountAfter = $locationService->getLocationChildCount($newParentLocation);
2266
2267
        $this->assertEquals($expected, $actual);
2268
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
2269
    }
2270
2271
    /**
2272
     * Test for the moveSubtree() method.
2273
     *
2274
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2275
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2276
     */
2277 View Code Duplication
    public function testMoveSubtreeDecrementsChildCountOfOldParent()
2278
    {
2279
        $repository = $this->getRepository();
2280
        $locationService = $repository->getLocationService();
2281
2282
        $oldParentLocation = $locationService->loadLocation($this->generateId('location', 1));
2283
2284
        // Load expected properties before move
2285
        $expected = $this->loadLocationProperties($oldParentLocation);
2286
        $childCountBefore = $locationService->getLocationChildCount($oldParentLocation);
2287
2288
        $mediaLocationId = $this->generateId('location', 43);
2289
        $demoDesignLocationId = $this->generateId('location', 56);
2290
        /* BEGIN: Use Case */
2291
        // $mediaLocationId is the ID of the "Media" page location in
2292
        // an eZ Publish demo installation
2293
2294
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2295
        // Publish demo installation
2296
2297
        // Load the location service
2298
        $locationService = $repository->getLocationService();
2299
2300
        // Load location to move
2301
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2302
2303
        // Get the location id of the old parent
2304
        $oldParentLocationId = $locationToMove->parentLocationId;
2305
2306
        // Load new parent location
2307
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2308
2309
        // Move location from "Home" to "Demo Design"
2310
        $locationService->moveSubtree(
2311
            $locationToMove,
2312
            $newParentLocation
2313
        );
2314
2315
        // Reload old parent location
2316
        $oldParentLocation = $locationService->loadLocation($oldParentLocationId);
2317
        /* END: Use Case */
2318
2319
        $this->refreshSearch($repository);
2320
2321
        // Load Subtree properties after move
2322
        $actual = $this->loadLocationProperties($oldParentLocation);
2323
        $childCountAfter = $locationService->getLocationChildCount($oldParentLocation);
2324
2325
        $this->assertEquals($expected, $actual);
2326
        $this->assertEquals($childCountBefore - 1, $childCountAfter);
2327
    }
2328
2329
    /**
2330
     * Test for the moveSubtree() method.
2331
     *
2332
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2333
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2334
     */
2335 View Code Duplication
    public function testMoveSubtreeThrowsInvalidArgumentException()
2336
    {
2337
        $repository = $this->getRepository();
2338
        $mediaLocationId = $this->generateId('location', 43);
2339
        $multimediaLocationId = $this->generateId('location', 53);
2340
2341
        /* BEGIN: Use Case */
2342
        // $mediaLocationId is the ID of the "Media" page location in
2343
        // an eZ Publish demo installation
2344
2345
        // $multimediaLocationId is the ID of the "Multimedia" page location in an eZ
2346
        // Publish demo installation
2347
2348
        // Load the location service
2349
        $locationService = $repository->getLocationService();
2350
2351
        // Load location to move
2352
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2353
2354
        // Load new parent location
2355
        $newParentLocation = $locationService->loadLocation($multimediaLocationId);
2356
2357
        // Throws an exception because new parent location is placed below location to move
2358
        $locationService->moveSubtree(
2359
            $locationToMove,
2360
            $newParentLocation
2361
        );
2362
        /* END: Use Case */
2363
    }
2364
2365
    /**
2366
     * Loads properties from all locations in the $location's subtree.
2367
     *
2368
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2369
     * @param array $properties
2370
     *
2371
     * @return array
2372
     */
2373
    private function loadSubtreeProperties(Location $location, array $properties = array())
2374
    {
2375
        $locationService = $this->getRepository()->getLocationService();
2376
2377
        foreach ($locationService->loadLocationChildren($location)->locations as $childLocation) {
2378
            $properties[] = $this->loadLocationProperties($childLocation);
2379
2380
            $properties = $this->loadSubtreeProperties($childLocation, $properties);
2381
        }
2382
2383
        return $properties;
2384
    }
2385
2386
    /**
2387
     * Loads assertable properties from the given location.
2388
     *
2389
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2390
     * @param mixed[] $overwrite
2391
     *
2392
     * @return array
2393
     */
2394
    private function loadLocationProperties(Location $location, array $overwrite = array())
2395
    {
2396
        return array_merge(
2397
            array(
2398
                'id' => $location->id,
2399
                'depth' => $location->depth,
2400
                'parentLocationId' => $location->parentLocationId,
2401
                'pathString' => $location->pathString,
2402
                'remoteId' => $location->remoteId,
2403
                'hidden' => $location->hidden,
2404
                'invisible' => $location->invisible,
2405
                'priority' => $location->priority,
2406
                'sortField' => $location->sortField,
2407
                'sortOrder' => $location->sortOrder,
2408
            ),
2409
            $overwrite
2410
        );
2411
    }
2412
2413
    /**
2414
     * Assert generated aliases to expected alias return.
2415
     *
2416
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
2417
     * @param array $expectedAliases
2418
     */
2419
    protected function assertGeneratedAliases($urlAliasService, array $expectedAliases)
2420
    {
2421
        foreach ($expectedAliases as $expectedAlias) {
2422
            $urlAlias = $urlAliasService->lookup($expectedAlias);
2423
            $this->assertPropertiesCorrect(['type' => 0], $urlAlias);
2424
        }
2425
    }
2426
2427
    /**
2428
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
2429
     * @param array $expectedSubItemAliases
2430
     */
2431
    private function assertAliasesBeforeCopy($urlAliasService, array $expectedSubItemAliases)
2432
    {
2433
        foreach ($expectedSubItemAliases as $aliasUrl) {
2434
            try {
2435
                $urlAliasService->lookup($aliasUrl);
2436
                $this->fail('We didn\'t expect to find alias, but it was found');
2437
            } catch (\Exception $e) {
2438
                $this->assertTrue(true); // OK - alias was not found
2439
            }
2440
        }
2441
    }
2442
2443
    /**
2444
     * Create and publish Content with the given parent Location.
2445
     *
2446
     * @param string $contentName
2447
     * @param int $parentLocationId
2448
     *
2449
     * @return \eZ\Publish\API\Repository\Values\Content\Content published Content
2450
     */
2451 View Code Duplication
    private function publishContentWithParentLocation($contentName, $parentLocationId)
2452
    {
2453
        $repository = $this->getRepository(false);
2454
        $locationService = $repository->getLocationService();
2455
2456
        $contentService = $repository->getContentService();
2457
        $contentTypeService = $repository->getContentTypeService();
2458
2459
        $contentCreateStruct = $contentService->newContentCreateStruct(
2460
            $contentTypeService->loadContentTypeByIdentifier('folder'),
2461
            'eng-US'
2462
        );
2463
        $contentCreateStruct->setField('name', $contentName);
2464
        $contentDraft = $contentService->createContent(
2465
            $contentCreateStruct,
2466
            [
2467
                $locationService->newLocationCreateStruct($parentLocationId),
2468
            ]
2469
        );
2470
2471
        return $contentService->publishVersion($contentDraft->versionInfo);
2472
    }
2473
}
2474