Completed
Push — master ( 65f512...fe0390 )
by Łukasz
26:26
created

LocationServiceTest::testUpdateLocationTwice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

Loading history...
344
        } catch (NotFoundException $e) {
345
            return;
346
        }
347
        /* END: Use Case */
348
349
        $this->fail('Objects still exists after rollback.');
350
    }
351
352
    /**
353
     * Test for the loadLocation() method.
354
     *
355
     * @return \eZ\Publish\API\Repository\Values\Content\Location
356
     *
357
     * @covers \eZ\Publish\API\Repository\LocationService::loadLocation
358
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
359
     */
360
    public function testLoadLocation()
361
    {
362
        $repository = $this->getRepository();
363
364
        $locationId = $this->generateId('location', 5);
365
        /* BEGIN: Use Case */
366
        // $locationId is the ID of an existing location
367
        $locationService = $repository->getLocationService();
368
369
        $location = $locationService->loadLocation($locationId);
370
        /* END: Use Case */
371
372
        $this->assertInstanceOf(
373
            Location::class,
374
            $location
375
        );
376
        self::assertEquals(5, $location->id);
377
378
        return $location;
379
    }
380
381
    /**
382
     * Test for the loadLocation() method.
383
     *
384
     * @see \eZ\Publish\API\Repository\LocationService::loadLocation()
385
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
386
     */
387
    public function testLoadLocationRootStructValues()
388
    {
389
        $repository = $this->getRepository();
390
        $locationService = $repository->getLocationService();
391
        $location = $locationService->loadLocation($this->generateId('location', 1));
392
393
        $legacyDateTime = new \DateTime();
394
        $legacyDateTime->setTimestamp(1030968000);
395
396
        // $location
397
        $this->assertPropertiesCorrect(
398
            array(
399
                'id' => $this->generateId('location', 1),
400
                'status' => 1,
401
                'priority' => 0,
402
                'hidden' => false,
403
                'invisible' => false,
404
                'remoteId' => '629709ba256fe317c3ddcee35453a96a',
405
                'parentLocationId' => $this->generateId('location', 1),
406
                'pathString' => '/1/',
407
                'depth' => 0,
408
                'sortField' => 1,
409
                'sortOrder' => 1,
410
            ),
411
            $location
412
        );
413
414
        // $location->contentInfo
415
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $location->contentInfo);
416
        $this->assertPropertiesCorrect(
417
            array(
418
                'id' => $this->generateId('content', 0),
419
                'name' => 'Top Level Nodes',
420
                'sectionId' => 1,
421
                'mainLocationId' => 1,
422
                'contentTypeId' => 1,
423
                'currentVersionNo' => 1,
424
                'published' => 1,
425
                'ownerId' => 14,
426
                'modificationDate' => $legacyDateTime,
427
                'publishedDate' => $legacyDateTime,
428
                'alwaysAvailable' => 1,
429
                'remoteId' => null,
430
                'mainLanguageCode' => 'eng-GB',
431
            ),
432
            $location->contentInfo
433
        );
434
    }
435
436
    /**
437
     * Test for the loadLocation() method.
438
     *
439
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
440
     *
441
     * @see \eZ\Publish\API\Repository\LocationService::loadLocation()
442
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
443
     */
444
    public function testLoadLocationStructValues(Location $location)
445
    {
446
        $this->assertPropertiesCorrect(
447
            array(
448
                'id' => $this->generateId('location', 5),
449
                'priority' => 0,
450
                'hidden' => false,
451
                'invisible' => false,
452
                'remoteId' => '3f6d92f8044aed134f32153517850f5a',
453
                'parentLocationId' => $this->generateId('location', 1),
454
                'pathString' => '/1/5/',
455
                'depth' => 1,
456
                'sortField' => 1,
457
                'sortOrder' => 1,
458
            ),
459
            $location
460
        );
461
462
        $this->assertInstanceOf(
463
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
464
            $location->contentInfo
465
        );
466
        $this->assertEquals($this->generateId('object', 4), $location->contentInfo->id);
467
468
        // Check lazy loaded proxy on ->content
469
        $this->assertInstanceOf(
470
            Content::class,
471
            $content = $location->getContent()
472
        );
473
        $this->assertEquals(4, $content->contentInfo->id);
474
    }
475
476
    public function testLoadLocationPrioritizedLanguagesFallback()
477
    {
478
        $repository = $this->getRepository();
479
480
        // Add a language
481
        $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 deleteLocation() method.
1723
     *
1724
     * @covers  \eZ\Publish\API\Repository\LocationService::deleteLocation()
1725
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testDeleteLocation
1726
     */
1727
    public function testDeleteLocationDeletesRelatedBookmarks()
1728
    {
1729
        $repository = $this->getRepository();
1730
1731
        $parentLocationId = $this->generateId('location', 43);
1732
        $childLocationId = $this->generateId('location', 53);
1733
1734
        /* BEGIN: Use Case */
1735
        $locationService = $repository->getLocationService();
1736
        $bookmarkService = $repository->getBookmarkService();
1737
1738
        // Load location
1739
        $childLocation = $locationService->loadLocation($childLocationId);
1740
        // Add location to bookmarks
1741
        $bookmarkService->createBookmark($childLocation);
1742
        // Load parent location
1743
        $parentLocation = $locationService->loadLocation($parentLocationId);
1744
        // Delete parent location
1745
        $locationService->deleteLocation($parentLocation);
1746
        /* END: Use Case */
1747
1748
        // Location isn't bookmarked anymore
1749
        foreach ($bookmarkService->loadBookmarks(0, 9999) as $bookmarkedLocation) {
1750
            $this->assertNotEquals($childLocation->id, $bookmarkedLocation->id);
1751
        }
1752
    }
1753
1754
    /**
1755
     * Test for the copySubtree() method.
1756
     *
1757
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1758
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1759
     */
1760
    public function testCopySubtree()
1761
    {
1762
        $repository = $this->getRepository();
1763
1764
        $mediaLocationId = $this->generateId('location', 43);
1765
        $demoDesignLocationId = $this->generateId('location', 56);
1766
        /* BEGIN: Use Case */
1767
        // $mediaLocationId is the ID of the "Media" page location in
1768
        // an eZ Publish demo installation
1769
1770
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1771
        // Publish demo installation
1772
1773
        // Load the location service
1774
        $locationService = $repository->getLocationService();
1775
1776
        // Load location to copy
1777
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1778
1779
        // Load new parent location
1780
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1781
1782
        // Copy location "Media" to "Demo Design"
1783
        $copiedLocation = $locationService->copySubtree(
1784
            $locationToCopy,
1785
            $newParentLocation
1786
        );
1787
        /* END: Use Case */
1788
1789
        $this->assertInstanceOf(
1790
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location',
1791
            $copiedLocation
1792
        );
1793
1794
        $this->assertPropertiesCorrect(
1795
            array(
1796
                'depth' => $newParentLocation->depth + 1,
1797
                'parentLocationId' => $newParentLocation->id,
1798
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $copiedLocation->id) . '/',
1799
            ),
1800
            $copiedLocation
1801
        );
1802
1803
        $this->assertDefaultContentStates($copiedLocation->contentInfo);
1804
    }
1805
1806
    /**
1807
     * Test for the copySubtree() method.
1808
     *
1809
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1810
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
1811
     */
1812
    public function testCopySubtreeWithAliases()
1813
    {
1814
        $repository = $this->getRepository();
1815
        $urlAliasService = $repository->getURLAliasService();
1816
1817
        // $mediaLocationId is the ID of the "Media" page location in
1818
        // an eZ Publish demo installation
1819
1820
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1821
        // Publish demo installation
1822
        $mediaLocationId = $this->generateId('location', 43);
1823
        $demoDesignLocationId = $this->generateId('location', 56);
1824
1825
        $locationService = $repository->getLocationService();
1826
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1827
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1828
1829
        $expectedSubItemAliases = [
1830
            '/Design/Plain-site/Media/Multimedia',
1831
            '/Design/Plain-site/Media/Images',
1832
            '/Design/Plain-site/Media/Files',
1833
        ];
1834
1835
        $this->assertAliasesBeforeCopy($urlAliasService, $expectedSubItemAliases);
1836
1837
        // Copy location "Media" to "Design"
1838
        $locationService->copySubtree(
1839
            $locationToCopy,
1840
            $newParentLocation
1841
        );
1842
1843
        $this->assertGeneratedAliases($urlAliasService, $expectedSubItemAliases);
1844
    }
1845
1846
    /**
1847
     * Asserts that given Content has default ContentStates.
1848
     *
1849
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
1850
     */
1851 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
1852
    {
1853
        $repository = $this->getRepository();
1854
        $objectStateService = $repository->getObjectStateService();
1855
1856
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
1857
1858
        foreach ($objectStateGroups as $objectStateGroup) {
1859
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
1860
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
1861
                // Only check the first object state which is the default one.
1862
                $this->assertEquals(
1863
                    $objectState,
1864
                    $contentState
1865
                );
1866
                break;
1867
            }
1868
        }
1869
    }
1870
1871
    /**
1872
     * Test for the copySubtree() method.
1873
     *
1874
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1875
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1876
     */
1877
    public function testCopySubtreeUpdatesSubtreeProperties()
1878
    {
1879
        $repository = $this->getRepository();
1880
        $locationService = $repository->getLocationService();
1881
1882
        $locationToCopy = $locationService->loadLocation($this->generateId('location', 43));
1883
1884
        // Load Subtree properties before copy
1885
        $expected = $this->loadSubtreeProperties($locationToCopy);
1886
1887
        $mediaLocationId = $this->generateId('location', 43);
1888
        $demoDesignLocationId = $this->generateId('location', 56);
1889
        /* BEGIN: Use Case */
1890
        // $mediaLocationId is the ID of the "Media" page location in
1891
        // an eZ Publish demo installation
1892
1893
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1894
        // Publish demo installation
1895
1896
        // Load the location service
1897
        $locationService = $repository->getLocationService();
1898
1899
        // Load location to copy
1900
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1901
1902
        // Load new parent location
1903
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1904
1905
        // Copy location "Media" to "Demo Design"
1906
        $copiedLocation = $locationService->copySubtree(
1907
            $locationToCopy,
1908
            $newParentLocation
1909
        );
1910
        /* END: Use Case */
1911
1912
        $beforeIds = array();
1913
        foreach ($expected as $properties) {
1914
            $beforeIds[] = $properties['id'];
1915
        }
1916
1917
        $this->refreshSearch($repository);
1918
1919
        // Load Subtree properties after copy
1920
        $actual = $this->loadSubtreeProperties($copiedLocation);
1921
1922
        $this->assertEquals(count($expected), count($actual));
1923
1924
        foreach ($actual as $properties) {
1925
            $this->assertNotContains($properties['id'], $beforeIds);
1926
            $this->assertStringStartsWith(
1927
                "{$newParentLocation->pathString}" . $this->parseId('location', $copiedLocation->id) . '/',
1928
                $properties['pathString']
1929
            );
1930
            $this->assertStringEndsWith(
1931
                '/' . $this->parseId('location', $properties['id']) . '/',
1932
                $properties['pathString']
1933
            );
1934
        }
1935
    }
1936
1937
    /**
1938
     * Test for the copySubtree() method.
1939
     *
1940
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1941
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1942
     */
1943
    public function testCopySubtreeIncrementsChildCountOfNewParent()
1944
    {
1945
        $repository = $this->getRepository();
1946
        $locationService = $repository->getLocationService();
1947
1948
        $childCountBefore = $locationService->getLocationChildCount($locationService->loadLocation(56));
1949
1950
        $mediaLocationId = $this->generateId('location', 43);
1951
        $demoDesignLocationId = $this->generateId('location', 56);
1952
        /* BEGIN: Use Case */
1953
        // $mediaLocationId is the ID of the "Media" page location in
1954
        // an eZ Publish demo installation
1955
1956
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
1957
        // Publish demo installation
1958
1959
        // Load the location service
1960
        $locationService = $repository->getLocationService();
1961
1962
        // Load location to copy
1963
        $locationToCopy = $locationService->loadLocation($mediaLocationId);
1964
1965
        // Load new parent location
1966
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
1967
1968
        // Copy location "Media" to "Demo Design"
1969
        $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...
1970
            $locationToCopy,
1971
            $newParentLocation
1972
        );
1973
        /* END: Use Case */
1974
1975
        $this->refreshSearch($repository);
1976
1977
        $childCountAfter = $locationService->getLocationChildCount($locationService->loadLocation($demoDesignLocationId));
1978
1979
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
1980
    }
1981
1982
    /**
1983
     * Test for the copySubtree() method.
1984
     *
1985
     * @see \eZ\Publish\API\Repository\LocationService::copySubtree()
1986
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1987
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCopySubtree
1988
     */
1989 View Code Duplication
    public function testCopySubtreeThrowsInvalidArgumentException()
1990
    {
1991
        $repository = $this->getRepository();
1992
1993
        $communityLocationId = $this->generateId('location', 5);
1994
        /* BEGIN: Use Case */
1995
        // $communityLocationId is the ID of the "Community" page location in
1996
        // an eZ Publish demo installation
1997
1998
        // Load the location service
1999
        $locationService = $repository->getLocationService();
2000
2001
        // Load location to copy
2002
        $locationToCopy = $locationService->loadLocation($communityLocationId);
2003
2004
        // Use a child as new parent
2005
        $childLocations = $locationService->loadLocationChildren($locationToCopy)->locations;
2006
        $newParentLocation = end($childLocations);
2007
2008
        // This call will fail with an "InvalidArgumentException", because the
2009
        // new parent is a child location of the subtree to copy.
2010
        $locationService->copySubtree(
2011
            $locationToCopy,
2012
            $newParentLocation
0 ignored issues
show
Security Bug introduced by
It seems like $newParentLocation defined by end($childLocations) on line 2006 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...
2013
        );
2014
        /* END: Use Case */
2015
    }
2016
2017
    /**
2018
     * Test for the moveSubtree() method.
2019
     *
2020
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2021
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
2022
     */
2023
    public function testMoveSubtree()
2024
    {
2025
        $repository = $this->getRepository();
2026
2027
        $mediaLocationId = $this->generateId('location', 43);
2028
        $demoDesignLocationId = $this->generateId('location', 56);
2029
        /* BEGIN: Use Case */
2030
        // $mediaLocationId is the ID of the "Media" page location in
2031
        // an eZ Publish demo installation
2032
2033
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2034
        // Publish demo installation
2035
2036
        // Load the location service
2037
        $locationService = $repository->getLocationService();
2038
2039
        // Load location to move
2040
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2041
2042
        // Load new parent location
2043
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2044
2045
        // Move location from "Home" to "Demo Design"
2046
        $locationService->moveSubtree(
2047
            $locationToMove,
2048
            $newParentLocation
2049
        );
2050
2051
        // Load moved location
2052
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2053
        /* END: Use Case */
2054
2055
        $this->assertPropertiesCorrect(
2056
            array(
2057
                'hidden' => false,
2058
                'invisible' => false,
2059
                'depth' => $newParentLocation->depth + 1,
2060
                'parentLocationId' => $newParentLocation->id,
2061
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
2062
            ),
2063
            $movedLocation
2064
        );
2065
    }
2066
2067
    /**
2068
     * Test for the moveSubtree() method.
2069
     *
2070
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2071
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2072
     */
2073
    public function testMoveSubtreeHidden()
2074
    {
2075
        $repository = $this->getRepository();
2076
2077
        $mediaLocationId = $this->generateId('location', 43);
2078
        $demoDesignLocationId = $this->generateId('location', 56);
2079
        /* BEGIN: Use Case */
2080
        // $mediaLocationId is the ID of the "Media" page location in
2081
        // an eZ Publish demo installation
2082
2083
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2084
        // Publish demo installation
2085
2086
        // Load the location service
2087
        $locationService = $repository->getLocationService();
2088
2089
        // Load location to move
2090
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2091
2092
        // Load new parent location
2093
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2094
2095
        // Hide the target location before we move
2096
        $newParentLocation = $locationService->hideLocation($newParentLocation);
2097
2098
        // Move location from "Home" to "Demo Design"
2099
        $locationService->moveSubtree(
2100
            $locationToMove,
2101
            $newParentLocation
2102
        );
2103
2104
        // Load moved location
2105
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2106
        /* END: Use Case */
2107
2108
        $this->assertPropertiesCorrect(
2109
            array(
2110
                'hidden' => false,
2111
                'invisible' => true,
2112
                'depth' => $newParentLocation->depth + 1,
2113
                'parentLocationId' => $newParentLocation->id,
2114
                'pathString' => "{$newParentLocation->pathString}" . $this->parseId('location', $movedLocation->id) . '/',
2115
            ),
2116
            $movedLocation
2117
        );
2118
    }
2119
2120
    /**
2121
     * Test for the moveSubtree() method.
2122
     *
2123
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2124
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2125
     */
2126
    public function testMoveSubtreeUpdatesSubtreeProperties()
2127
    {
2128
        $repository = $this->getRepository();
2129
        $locationService = $repository->getLocationService();
2130
2131
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
2132
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
2133
2134
        // Load Subtree properties before move
2135
        $expected = $this->loadSubtreeProperties($locationToMove);
2136
        foreach ($expected as $id => $properties) {
2137
            $expected[$id]['depth'] = $properties['depth'] + 2;
2138
            $expected[$id]['pathString'] = str_replace(
2139
                $locationToMove->pathString,
2140
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
2141
                $properties['pathString']
2142
            );
2143
        }
2144
2145
        $mediaLocationId = $this->generateId('location', 43);
2146
        $demoDesignLocationId = $this->generateId('location', 56);
2147
        /* BEGIN: Use Case */
2148
        // $mediaLocationId is the ID of the "Media" page location in
2149
        // an eZ Publish demo installation
2150
2151
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2152
        // Publish demo installation
2153
2154
        // Load the location service
2155
        $locationService = $repository->getLocationService();
2156
2157
        // Load location to move
2158
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2159
2160
        // Load new parent location
2161
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2162
2163
        // Move location from "Home" to "Demo Design"
2164
        $locationService->moveSubtree(
2165
            $locationToMove,
2166
            $newParentLocation
2167
        );
2168
2169
        // Load moved location
2170
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2171
        /* END: Use Case */
2172
2173
        $this->refreshSearch($repository);
2174
2175
        // Load Subtree properties after move
2176
        $actual = $this->loadSubtreeProperties($movedLocation);
2177
2178
        $this->assertEquals($expected, $actual);
2179
    }
2180
2181
    /**
2182
     * Test for the moveSubtree() method.
2183
     *
2184
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2185
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtreeUpdatesSubtreeProperties
2186
     */
2187
    public function testMoveSubtreeUpdatesSubtreePropertiesHidden()
2188
    {
2189
        $repository = $this->getRepository();
2190
        $locationService = $repository->getLocationService();
2191
2192
        $locationToMove = $locationService->loadLocation($this->generateId('location', 43));
2193
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
2194
2195
        // Hide the target location before we move
2196
        $newParentLocation = $locationService->hideLocation($newParentLocation);
2197
2198
        // Load Subtree properties before move
2199
        $expected = $this->loadSubtreeProperties($locationToMove);
2200
        foreach ($expected as $id => $properties) {
2201
            $expected[$id]['invisible'] = true;
2202
            $expected[$id]['depth'] = $properties['depth'] + 2;
2203
            $expected[$id]['pathString'] = str_replace(
2204
                $locationToMove->pathString,
2205
                "{$newParentLocation->pathString}" . $this->parseId('location', $locationToMove->id) . '/',
2206
                $properties['pathString']
2207
            );
2208
        }
2209
2210
        $mediaLocationId = $this->generateId('location', 43);
2211
        $demoDesignLocationId = $this->generateId('location', 56);
2212
        /* BEGIN: Use Case */
2213
        // $mediaLocationId is the ID of the "Media" page location in
2214
        // an eZ Publish demo installation
2215
2216
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2217
        // Publish demo installation
2218
2219
        // Load the location service
2220
        $locationService = $repository->getLocationService();
2221
2222
        // Load location to move
2223
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2224
2225
        // Load new parent location
2226
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2227
2228
        // Move location from "Home" to "Demo Design"
2229
        $locationService->moveSubtree(
2230
            $locationToMove,
2231
            $newParentLocation
2232
        );
2233
2234
        // Load moved location
2235
        $movedLocation = $locationService->loadLocation($mediaLocationId);
2236
        /* END: Use Case */
2237
2238
        $this->refreshSearch($repository);
2239
2240
        // Load Subtree properties after move
2241
        $actual = $this->loadSubtreeProperties($movedLocation);
2242
2243
        $this->assertEquals($expected, $actual);
2244
    }
2245
2246
    /**
2247
     * Test for the moveSubtree() method.
2248
     *
2249
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2250
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2251
     */
2252 View Code Duplication
    public function testMoveSubtreeIncrementsChildCountOfNewParent()
2253
    {
2254
        $repository = $this->getRepository();
2255
        $locationService = $repository->getLocationService();
2256
2257
        $newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
2258
2259
        // Load expected properties before move
2260
        $expected = $this->loadLocationProperties($newParentLocation);
2261
        $childCountBefore = $locationService->getLocationChildCount($newParentLocation);
2262
2263
        $mediaLocationId = $this->generateId('location', 43);
2264
        $demoDesignLocationId = $this->generateId('location', 56);
2265
        /* BEGIN: Use Case */
2266
        // $mediaLocationId is the ID of the "Media" page location in
2267
        // an eZ Publish demo installation
2268
2269
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2270
        // Publish demo installation
2271
2272
        // Load the location service
2273
        $locationService = $repository->getLocationService();
2274
2275
        // Load location to move
2276
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2277
2278
        // Load new parent location
2279
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2280
2281
        // Move location from "Home" to "Demo Design"
2282
        $locationService->moveSubtree(
2283
            $locationToMove,
2284
            $newParentLocation
2285
        );
2286
2287
        // Load moved location
2288
        $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...
2289
2290
        // Reload new parent location
2291
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2292
        /* END: Use Case */
2293
2294
        $this->refreshSearch($repository);
2295
2296
        // Load Subtree properties after move
2297
        $actual = $this->loadLocationProperties($newParentLocation);
2298
        $childCountAfter = $locationService->getLocationChildCount($newParentLocation);
2299
2300
        $this->assertEquals($expected, $actual);
2301
        $this->assertEquals($childCountBefore + 1, $childCountAfter);
2302
    }
2303
2304
    /**
2305
     * Test for the moveSubtree() method.
2306
     *
2307
     * @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
2308
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2309
     */
2310 View Code Duplication
    public function testMoveSubtreeDecrementsChildCountOfOldParent()
2311
    {
2312
        $repository = $this->getRepository();
2313
        $locationService = $repository->getLocationService();
2314
2315
        $oldParentLocation = $locationService->loadLocation($this->generateId('location', 1));
2316
2317
        // Load expected properties before move
2318
        $expected = $this->loadLocationProperties($oldParentLocation);
2319
        $childCountBefore = $locationService->getLocationChildCount($oldParentLocation);
2320
2321
        $mediaLocationId = $this->generateId('location', 43);
2322
        $demoDesignLocationId = $this->generateId('location', 56);
2323
        /* BEGIN: Use Case */
2324
        // $mediaLocationId is the ID of the "Media" page location in
2325
        // an eZ Publish demo installation
2326
2327
        // $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
2328
        // Publish demo installation
2329
2330
        // Load the location service
2331
        $locationService = $repository->getLocationService();
2332
2333
        // Load location to move
2334
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2335
2336
        // Get the location id of the old parent
2337
        $oldParentLocationId = $locationToMove->parentLocationId;
2338
2339
        // Load new parent location
2340
        $newParentLocation = $locationService->loadLocation($demoDesignLocationId);
2341
2342
        // Move location from "Home" to "Demo Design"
2343
        $locationService->moveSubtree(
2344
            $locationToMove,
2345
            $newParentLocation
2346
        );
2347
2348
        // Reload old parent location
2349
        $oldParentLocation = $locationService->loadLocation($oldParentLocationId);
2350
        /* END: Use Case */
2351
2352
        $this->refreshSearch($repository);
2353
2354
        // Load Subtree properties after move
2355
        $actual = $this->loadLocationProperties($oldParentLocation);
2356
        $childCountAfter = $locationService->getLocationChildCount($oldParentLocation);
2357
2358
        $this->assertEquals($expected, $actual);
2359
        $this->assertEquals($childCountBefore - 1, $childCountAfter);
2360
    }
2361
2362
    /**
2363
     * Test for the moveSubtree() method.
2364
     *
2365
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
2366
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2367
     */
2368 View Code Duplication
    public function testMoveSubtreeThrowsInvalidArgumentException()
2369
    {
2370
        $repository = $this->getRepository();
2371
        $mediaLocationId = $this->generateId('location', 43);
2372
        $multimediaLocationId = $this->generateId('location', 53);
2373
2374
        /* BEGIN: Use Case */
2375
        // $mediaLocationId is the ID of the "Media" page location in
2376
        // an eZ Publish demo installation
2377
2378
        // $multimediaLocationId is the ID of the "Multimedia" page location in an eZ
2379
        // Publish demo installation
2380
2381
        // Load the location service
2382
        $locationService = $repository->getLocationService();
2383
2384
        // Load location to move
2385
        $locationToMove = $locationService->loadLocation($mediaLocationId);
2386
2387
        // Load new parent location
2388
        $newParentLocation = $locationService->loadLocation($multimediaLocationId);
2389
2390
        // Throws an exception because new parent location is placed below location to move
2391
        $locationService->moveSubtree(
2392
            $locationToMove,
2393
            $newParentLocation
2394
        );
2395
        /* END: Use Case */
2396
    }
2397
2398
    /**
2399
     * Loads properties from all locations in the $location's subtree.
2400
     *
2401
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2402
     * @param array $properties
2403
     *
2404
     * @return array
2405
     */
2406
    private function loadSubtreeProperties(Location $location, array $properties = array())
2407
    {
2408
        $locationService = $this->getRepository()->getLocationService();
2409
2410
        foreach ($locationService->loadLocationChildren($location)->locations as $childLocation) {
2411
            $properties[] = $this->loadLocationProperties($childLocation);
2412
2413
            $properties = $this->loadSubtreeProperties($childLocation, $properties);
2414
        }
2415
2416
        return $properties;
2417
    }
2418
2419
    /**
2420
     * Loads assertable properties from the given location.
2421
     *
2422
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
2423
     * @param mixed[] $overwrite
2424
     *
2425
     * @return array
2426
     */
2427
    private function loadLocationProperties(Location $location, array $overwrite = array())
2428
    {
2429
        return array_merge(
2430
            array(
2431
                'id' => $location->id,
2432
                'depth' => $location->depth,
2433
                'parentLocationId' => $location->parentLocationId,
2434
                'pathString' => $location->pathString,
2435
                'remoteId' => $location->remoteId,
2436
                'hidden' => $location->hidden,
2437
                'invisible' => $location->invisible,
2438
                'priority' => $location->priority,
2439
                'sortField' => $location->sortField,
2440
                'sortOrder' => $location->sortOrder,
2441
            ),
2442
            $overwrite
2443
        );
2444
    }
2445
2446
    /**
2447
     * Assert generated aliases to expected alias return.
2448
     *
2449
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
2450
     * @param array $expectedAliases
2451
     */
2452
    protected function assertGeneratedAliases($urlAliasService, array $expectedAliases)
2453
    {
2454
        foreach ($expectedAliases as $expectedAlias) {
2455
            $urlAlias = $urlAliasService->lookup($expectedAlias);
2456
            $this->assertPropertiesCorrect(['type' => 0], $urlAlias);
2457
        }
2458
    }
2459
2460
    /**
2461
     * @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
2462
     * @param array $expectedSubItemAliases
2463
     */
2464
    private function assertAliasesBeforeCopy($urlAliasService, array $expectedSubItemAliases)
2465
    {
2466
        foreach ($expectedSubItemAliases as $aliasUrl) {
2467
            try {
2468
                $urlAliasService->lookup($aliasUrl);
2469
                $this->fail('We didn\'t expect to find alias, but it was found');
2470
            } catch (\Exception $e) {
2471
                $this->assertTrue(true); // OK - alias was not found
2472
            }
2473
        }
2474
    }
2475
2476
    /**
2477
     * Create and publish Content with the given parent Location.
2478
     *
2479
     * @param string $contentName
2480
     * @param int $parentLocationId
2481
     *
2482
     * @return \eZ\Publish\API\Repository\Values\Content\Content published Content
2483
     */
2484 View Code Duplication
    private function publishContentWithParentLocation($contentName, $parentLocationId)
2485
    {
2486
        $repository = $this->getRepository(false);
2487
        $locationService = $repository->getLocationService();
2488
2489
        $contentService = $repository->getContentService();
2490
        $contentTypeService = $repository->getContentTypeService();
2491
2492
        $contentCreateStruct = $contentService->newContentCreateStruct(
2493
            $contentTypeService->loadContentTypeByIdentifier('folder'),
2494
            'eng-US'
2495
        );
2496
        $contentCreateStruct->setField('name', $contentName);
2497
        $contentDraft = $contentService->createContent(
2498
            $contentCreateStruct,
2499
            [
2500
                $locationService->newLocationCreateStruct($parentLocationId),
2501
            ]
2502
        );
2503
2504
        return $contentService->publishVersion($contentDraft->versionInfo);
2505
    }
2506
}
2507