Completed
Push — EZP-30823_7.5_transaction_cach... ( f488df...fcac77 )
by André
16:30
created

testUpdateContentInTransactionWithRollback()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 3
nop 0
dl 0
loc 41
rs 9.264
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the ContentServiceTest 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\Exceptions\ContentFieldValidationException;
13
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException as APIInvalidArgumentException;
14
use eZ\Publish\API\Repository\Values\Content\Content;
15
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
16
use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct;
17
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
18
use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct;
19
use eZ\Publish\API\Repository\Values\Content\Field;
20
use eZ\Publish\API\Repository\Values\Content\Location;
21
use eZ\Publish\API\Repository\Values\Content\URLAlias;
22
use eZ\Publish\API\Repository\Values\Content\Relation;
23
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
24
use eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation;
25
use eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation;
26
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation;
27
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
28
use DOMDocument;
29
use Exception;
30
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException as CoreUnauthorizedException;
31
use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct;
32
33
/**
34
 * Test case for operations in the ContentService using in memory storage.
35
 *
36
 * @see \eZ\Publish\API\Repository\ContentService
37
 * @group content
38
 */
39
class ContentServiceTest extends BaseContentServiceTest
40
{
41
    private const ADMINISTRATORS_USER_GROUP_NAME = 'Administrators';
42
    private const ADMINISTRATORS_USER_GROUP_ID = 12;
43
    private const ADMINISTRATORS_USER_GROUP_LOCATION_ID = 13;
44
45
    private const WRITERS_USER_GROUP_NAME = 'Writers';
46
47
    private const MEMBERS_USER_GROUP_ID = 11;
48
49
    private const MEDIA_CONTENT_ID = 41;
50
51
    private const MEDIA_REMOTE_ID = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
52
    private const DEMO_DESIGN_REMOTE_ID = '8b8b22fe3c6061ed500fbd2b377b885f';
53
54
    private const FORUM_IDENTIFIER = 'forum';
55
56
    private const ENG_US = 'eng-US';
57
    private const GER_DE = 'ger-DE';
58
    private const ENG_GB = 'eng-GB';
59
60
    /** @var \eZ\Publish\API\Repository\PermissionResolver */
61
    private $permissionResolver;
62
63
    /** @var \eZ\Publish\API\Repository\ContentService */
64
    private $contentService;
65
66
    /** @var \eZ\Publish\API\Repository\LocationService */
67
    private $locationService;
68
69
    public function setUp(): void
70
    {
71
        parent::setUp();
72
73
        $repository = $this->getRepository();
74
        $this->permissionResolver = $repository->getPermissionResolver();
75
        $this->contentService = $repository->getContentService();
76
        $this->locationService = $repository->getLocationService();
77
    }
78
79
    /**
80
     * Test for the newContentCreateStruct() method.
81
     *
82
     * @see \eZ\Publish\API\Repository\ContentService::newContentCreateStruct()
83
     * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testLoadContentTypeByIdentifier
84
     * @group user
85
     * @group field-type
86
     */
87
    public function testNewContentCreateStruct()
88
    {
89
        $contentTypeService = $this->getRepository()->getContentTypeService();
90
        $contentType = $contentTypeService->loadContentTypeByIdentifier(self::FORUM_IDENTIFIER);
91
92
        $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
93
94
        $this->assertInstanceOf(ContentCreateStruct::class, $contentCreate);
95
    }
96
97
    /**
98
     * Test for the createContent() method.
99
     *
100
     * @return \eZ\Publish\API\Repository\Values\Content\Content
101
     *
102
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
103
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentCreateStruct
104
     * @group user
105
     * @group field-type
106
     */
107
    public function testCreateContent()
108
    {
109
        if ($this->isVersion4()) {
110
            $this->markTestSkipped('This test requires eZ Publish 5');
111
        }
112
113
        $contentTypeService = $this->getRepository()->getContentTypeService();
114
115
        $contentType = $contentTypeService->loadContentTypeByIdentifier(self::FORUM_IDENTIFIER);
116
117
        $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
118
        $contentCreate->setField('name', 'My awesome forum');
119
120
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
121
        $contentCreate->alwaysAvailable = true;
122
123
        $content = $this->contentService->createContent($contentCreate);
124
125
        $this->assertInstanceOf(Content::class, $content);
126
127
        return $content;
128
    }
129
130
    /**
131
     * Test for the createContent() method.
132
     *
133
     * Tests made for issue #EZP-20955 where Anonymous user is granted access to create content
134
     * and should have access to do that.
135
     *
136
     * @return \eZ\Publish\API\Repository\Values\Content\Content
137
     *
138
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
139
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentCreateStruct
140
     * @group user
141
     * @group field-type
142
     */
143
    public function testCreateContentAndPublishWithPrivilegedAnonymousUser()
144
    {
145
        if ($this->isVersion4()) {
146
            $this->markTestSkipped('This test requires eZ Publish 5');
147
        }
148
149
        $anonymousUserId = $this->generateId('user', 10);
150
151
        $repository = $this->getRepository();
152
        $contentTypeService = $this->getRepository()->getContentTypeService();
153
        $roleService = $repository->getRoleService();
154
155
        // Give Anonymous user role additional rights
156
        $role = $roleService->loadRoleByIdentifier('Anonymous');
157
        $roleDraft = $roleService->createRoleDraft($role);
158
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'create');
159
        $policyCreateStruct->addLimitation(new SectionLimitation(['limitationValues' => [1]]));
160
        $policyCreateStruct->addLimitation(new LocationLimitation(['limitationValues' => [2]]));
161
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(['limitationValues' => [1]]));
162
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
163
164
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'publish');
165
        $policyCreateStruct->addLimitation(new SectionLimitation(['limitationValues' => [1]]));
166
        $policyCreateStruct->addLimitation(new LocationLimitation(['limitationValues' => [2]]));
167
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(['limitationValues' => [1]]));
168
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
169
        $roleService->publishRoleDraft($roleDraft);
170
171
        // Set Anonymous user as current
172
        $repository->getPermissionResolver()->setCurrentUserReference($repository->getUserService()->loadUser($anonymousUserId));
173
174
        // Create a new content object:
175
        $contentCreate = $this->contentService->newContentCreateStruct(
176
            $contentTypeService->loadContentTypeByIdentifier('folder'),
177
            self::ENG_GB
178
        );
179
180
        $contentCreate->setField('name', 'Folder 1');
181
182
        $content = $this->contentService->createContent(
183
            $contentCreate,
184
            [$this->locationService->newLocationCreateStruct(2)]
185
        );
186
187
        $this->contentService->publishVersion(
188
            $content->getVersionInfo()
189
        );
190
    }
191
192
    /**
193
     * Test for the createContent() method.
194
     *
195
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
196
     *
197
     * @return \eZ\Publish\API\Repository\Values\Content\Content
198
     *
199
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
200
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
201
     */
202
    public function testCreateContentSetsContentInfo($content)
203
    {
204
        $this->assertInstanceOf(ContentInfo::class, $content->contentInfo);
205
206
        return $content;
207
    }
208
209
    /**
210
     * Test for the createContent() method.
211
     *
212
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
213
     *
214
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
215
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsContentInfo
216
     */
217
    public function testCreateContentSetsExpectedContentInfo($content)
218
    {
219
        $this->assertEquals(
220
            [
221
                $content->id,
222
                28, // id of content type "forum"
223
                true,
224
                1,
225
                'abcdef0123456789abcdef0123456789',
226
                self::ENG_US,
227
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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...
228
                false,
229
                null,
230
                // Main Location id for unpublished Content should be null
231
                null,
232
            ],
233
            [
234
                $content->contentInfo->id,
235
                $content->contentInfo->contentTypeId,
236
                $content->contentInfo->alwaysAvailable,
237
                $content->contentInfo->currentVersionNo,
238
                $content->contentInfo->remoteId,
239
                $content->contentInfo->mainLanguageCode,
240
                $content->contentInfo->ownerId,
241
                $content->contentInfo->published,
242
                $content->contentInfo->publishedDate,
243
                $content->contentInfo->mainLocationId,
244
            ]
245
        );
246
    }
247
248
    /**
249
     * Test for the createContent() method.
250
     *
251
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
252
     *
253
     * @return \eZ\Publish\API\Repository\Values\Content\Content
254
     *
255
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
256
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
257
     */
258
    public function testCreateContentSetsVersionInfo($content)
259
    {
260
        $this->assertInstanceOf(VersionInfo::class, $content->getVersionInfo());
261
262
        return $content;
263
    }
264
265
    /**
266
     * Test for the createContent() method.
267
     *
268
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
269
     *
270
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
271
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsVersionInfo
272
     */
273
    public function testCreateContentSetsExpectedVersionInfo($content)
274
    {
275
        $this->assertEquals(
276
            [
277
                'status' => VersionInfo::STATUS_DRAFT,
278
                'versionNo' => 1,
279
                'creatorId' => $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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...
280
                'initialLanguageCode' => self::ENG_US,
281
            ],
282
            [
283
                'status' => $content->getVersionInfo()->status,
284
                'versionNo' => $content->getVersionInfo()->versionNo,
285
                'creatorId' => $content->getVersionInfo()->creatorId,
286
                'initialLanguageCode' => $content->getVersionInfo()->initialLanguageCode,
287
            ]
288
        );
289
        $this->assertTrue($content->getVersionInfo()->isDraft());
290
        $this->assertFalse($content->getVersionInfo()->isPublished());
291
        $this->assertFalse($content->getVersionInfo()->isArchived());
292
    }
293
294
    /**
295
     * Test for the createContent() method.
296
     *
297
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
298
     *
299
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
300
     * @depends testCreateContent
301
     */
302
    public function testCreateContentSetsExpectedContentType($content)
303
    {
304
        $contentType = $content->getContentType();
305
306
        $this->assertEquals(
307
            [
308
                $contentType->id,
309
                // Won't match as it's set to true in createContentDraftVersion1()
310
                //$contentType->defaultAlwaysAvailable,
311
                //$contentType->defaultSortField,
312
                //$contentType->defaultSortOrder,
313
            ],
314
            [
315
                $content->contentInfo->contentTypeId,
316
                //$content->contentInfo->alwaysAvailable,
317
                //$location->sortField,
318
                //$location->sortOrder,
319
            ]
320
        );
321
    }
322
323
    /**
324
     * Test for the createContent() method.
325
     *
326
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
327
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
328
     */
329
    public function testCreateContentThrowsInvalidArgumentException()
330
    {
331
        if ($this->isVersion4()) {
332
            $this->markTestSkipped('This test requires eZ Publish 5');
333
        }
334
335
        $contentTypeService = $this->getRepository()->getContentTypeService();
336
337
        $contentType = $contentTypeService->loadContentTypeByIdentifier(self::FORUM_IDENTIFIER);
338
339
        $contentCreate1 = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
340
        $contentCreate1->setField('name', 'An awesome Sidelfingen forum');
341
342
        $contentCreate1->remoteId = 'abcdef0123456789abcdef0123456789';
343
        $contentCreate1->alwaysAvailable = true;
344
345
        $draft = $this->contentService->createContent($contentCreate1);
346
        $this->contentService->publishVersion($draft->versionInfo);
347
348
        $contentCreate2 = $this->contentService->newContentCreateStruct($contentType, self::ENG_GB);
349
        $contentCreate2->setField('name', 'An awesome Bielefeld forum');
350
351
        $contentCreate2->remoteId = 'abcdef0123456789abcdef0123456789';
352
        $contentCreate2->alwaysAvailable = false;
353
354
        $this->expectException(APIInvalidArgumentException::class);
355
        $this->contentService->createContent($contentCreate2);
356
    }
357
358
    /**
359
     * Test for the createContent() method.
360
     *
361
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
362
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
363
     */
364
    public function testCreateContentThrowsInvalidArgumentExceptionOnFieldTypeNotAccept()
365
    {
366
        $contentTypeService = $this->getRepository()->getContentTypeService();
367
368
        $contentType = $contentTypeService->loadContentTypeByIdentifier(self::FORUM_IDENTIFIER);
369
370
        $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
371
        // The name field does only accept strings and null as its values
372
        $contentCreate->setField('name', new \stdClass());
373
374
        $this->expectException(APIInvalidArgumentException::class);
375
        $this->contentService->createContent($contentCreate);
376
    }
377
378
    /**
379
     * Test for the createContent() method.
380
     *
381
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
382
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
383
     */
384
    public function testCreateContentThrowsContentFieldValidationException()
385
    {
386
        $contentTypeService = $this->getRepository()->getContentTypeService();
387
388
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
389
390
        $contentCreate1 = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
391
        $contentCreate1->setField('name', 'An awesome Sidelfingen folder');
392
        // Violates string length constraint
393
        $contentCreate1->setField('short_name', str_repeat('a', 200));
394
395
        $this->expectException(ContentFieldValidationException::class);
396
397
        // Throws ContentFieldValidationException, since short_name does not pass validation of the string length validator
398
        $this->contentService->createContent($contentCreate1);
399
    }
400
401
    /**
402
     * Test for the createContent() method.
403
     *
404
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
405
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
406
     */
407
    public function testCreateContentRequiredFieldMissing()
408
    {
409
        $contentTypeService = $this->getRepository()->getContentTypeService();
410
        $contentType = $contentTypeService->loadContentTypeByIdentifier(self::FORUM_IDENTIFIER);
411
412
        $contentCreate1 = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
413
        // Required field "name" is not set
414
415
        $this->expectException(ContentFieldValidationException::class);
416
417
        // Throws a ContentFieldValidationException, since a required field is missing
418
        $this->contentService->createContent($contentCreate1);
419
    }
420
421
    /**
422
     * Test for the createContent() method.
423
     *
424
     * NOTE: We have bidirectional dependencies between the ContentService and
425
     * the LocationService, so that we cannot use PHPUnit's test dependencies
426
     * here.
427
     *
428
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
429
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
430
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationByRemoteId
431
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
432
     * @group user
433
     */
434
    public function testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately()
435
    {
436
        $this->createContentDraftVersion1();
437
438
        $this->expectException(NotFoundException::class);
439
440
        // The location will not have been created, yet, so this throws an exception
441
        $this->locationService->loadLocationByRemoteId('0123456789abcdef0123456789abcdef');
442
    }
443
444
    /**
445
     * Test for the createContent() method.
446
     *
447
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
448
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
449
     */
450
    public function testCreateContentThrowsInvalidArgumentExceptionWithLocationCreateParameter()
451
    {
452
        $parentLocationId = $this->generateId('location', 56);
453
        // $parentLocationId is a valid location ID
454
455
        $contentTypeService = $this->getRepository()->getContentTypeService();
456
457
        $contentType = $contentTypeService->loadContentTypeByIdentifier(self::FORUM_IDENTIFIER);
458
459
        // Configure new locations
460
        $locationCreate1 = $this->locationService->newLocationCreateStruct($parentLocationId);
461
462
        $locationCreate1->priority = 23;
463
        $locationCreate1->hidden = true;
464
        $locationCreate1->remoteId = '0123456789abcdef0123456789aaaaaa';
465
        $locationCreate1->sortField = Location::SORT_FIELD_NODE_ID;
466
        $locationCreate1->sortOrder = Location::SORT_ORDER_DESC;
467
468
        $locationCreate2 = $this->locationService->newLocationCreateStruct($parentLocationId);
469
470
        $locationCreate2->priority = 42;
471
        $locationCreate2->hidden = true;
472
        $locationCreate2->remoteId = '0123456789abcdef0123456789bbbbbb';
473
        $locationCreate2->sortField = Location::SORT_FIELD_NODE_ID;
474
        $locationCreate2->sortOrder = Location::SORT_ORDER_DESC;
475
476
        // Configure new content object
477
        $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
478
479
        $contentCreate->setField('name', 'A awesome Sindelfingen forum');
480
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
481
        $contentCreate->alwaysAvailable = true;
482
483
        // Create new content object under the specified location
484
        $draft = $this->contentService->createContent(
485
            $contentCreate,
486
            [$locationCreate1]
487
        );
488
        $this->contentService->publishVersion($draft->versionInfo);
489
490
        $this->expectException(APIInvalidArgumentException::class);
491
        // Content remoteId already exists,
492
        $this->contentService->createContent(
493
            $contentCreate,
494
            [$locationCreate2]
495
        );
496
    }
497
498
    /**
499
     * Test for the loadContentInfo() method.
500
     *
501
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
502
     * @group user
503
     */
504
    public function testLoadContentInfo()
505
    {
506
        $mediaFolderId = $this->generateId('object', self::MEDIA_CONTENT_ID);
507
508
        // Load the ContentInfo for "Media" folder
509
        $contentInfo = $this->contentService->loadContentInfo($mediaFolderId);
510
511
        $this->assertInstanceOf(ContentInfo::class, $contentInfo);
512
513
        return $contentInfo;
514
    }
515
516
    /**
517
     * Test for the returned value of the loadContentInfo() method.
518
     *
519
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
520
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfo
521
     *
522
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
523
     */
524
    public function testLoadContentInfoSetsExpectedContentInfo(ContentInfo $contentInfo)
525
    {
526
        $this->assertPropertiesCorrectUnsorted(
527
            $this->getExpectedMediaContentInfoProperties(),
528
            $contentInfo
529
        );
530
    }
531
532
    /**
533
     * Test for the loadContentInfo() method.
534
     *
535
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
536
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
537
     */
538
    public function testLoadContentInfoThrowsNotFoundException()
539
    {
540
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
541
542
        $this->expectException(NotFoundException::class);
543
544
        // This call will fail with a NotFoundException
545
        $this->contentService->loadContentInfo($nonExistentContentId);
546
    }
547
548
    /**
549
     * Test for the loadContentInfoList() method.
550
     *
551
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoList()
552
     */
553
    public function testLoadContentInfoList()
554
    {
555
        $mediaFolderId = $this->generateId('object', self::MEDIA_CONTENT_ID);
556
        $list = $this->contentService->loadContentInfoList([$mediaFolderId]);
557
558
        $this->assertCount(1, $list);
559
        $this->assertEquals([$mediaFolderId], array_keys($list), 'Array key was not content id');
560
        $this->assertInstanceOf(
561
            ContentInfo::class,
562
            $list[$mediaFolderId]
563
        );
564
    }
565
566
    /**
567
     * Test for the loadContentInfoList() method.
568
     *
569
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoList()
570
     * @depends testLoadContentInfoList
571
     */
572
    public function testLoadContentInfoListSkipsNotFoundItems()
573
    {
574
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
575
        $list = $this->contentService->loadContentInfoList([$nonExistentContentId]);
576
577
        $this->assertCount(0, $list);
578
    }
579
580
    /**
581
     * Test for the loadContentInfoByRemoteId() method.
582
     *
583
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
584
     */
585
    public function testLoadContentInfoByRemoteId()
586
    {
587
        // Load the ContentInfo for "Media" folder
588
        $contentInfo = $this->contentService->loadContentInfoByRemoteId('faaeb9be3bd98ed09f606fc16d144eca');
589
590
        $this->assertInstanceOf(ContentInfo::class, $contentInfo);
591
592
        return $contentInfo;
593
    }
594
595
    /**
596
     * Test for the returned value of the loadContentInfoByRemoteId() method.
597
     *
598
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
599
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId
600
     *
601
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
602
     */
603
    public function testLoadContentInfoByRemoteIdSetsExpectedContentInfo(ContentInfo $contentInfo)
604
    {
605
        $this->assertPropertiesCorrectUnsorted(
606
            [
607
                'id' => 10,
608
                'contentTypeId' => 4,
609
                'name' => 'Anonymous User',
610
                'sectionId' => 2,
611
                'currentVersionNo' => 2,
612
                'published' => true,
613
                'ownerId' => 14,
614
                'modificationDate' => $this->createDateTime(1072180405),
615
                'publishedDate' => $this->createDateTime(1033920665),
616
                'alwaysAvailable' => 1,
617
                'remoteId' => 'faaeb9be3bd98ed09f606fc16d144eca',
618
                'mainLanguageCode' => self::ENG_US,
619
                'mainLocationId' => 45,
620
            ],
621
            $contentInfo
622
        );
623
    }
624
625
    /**
626
     * Test for the loadContentInfoByRemoteId() method.
627
     *
628
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
629
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
630
     */
631
    public function testLoadContentInfoByRemoteIdThrowsNotFoundException()
632
    {
633
        $this->expectException(NotFoundException::class);
634
635
        $this->contentService->loadContentInfoByRemoteId('abcdefghijklmnopqrstuvwxyz0123456789');
636
    }
637
638
    /**
639
     * Test for the loadVersionInfo() method.
640
     *
641
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
642
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
643
     * @group user
644
     */
645
    public function testLoadVersionInfo()
646
    {
647
        $mediaFolderId = $this->generateId('object', self::MEDIA_CONTENT_ID);
648
        // $mediaFolderId contains the ID of the "Media" folder
649
650
        // Load the ContentInfo for "Media" folder
651
        $contentInfo = $this->contentService->loadContentInfo($mediaFolderId);
652
653
        // Now load the current version info of the "Media" folder
654
        $versionInfo = $this->contentService->loadVersionInfo($contentInfo);
655
656
        $this->assertInstanceOf(
657
            VersionInfo::class,
658
            $versionInfo
659
        );
660
    }
661
662
    /**
663
     * Test for the loadVersionInfoById() method.
664
     *
665
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
666
     */
667
    public function testLoadVersionInfoById()
668
    {
669
        $mediaFolderId = $this->generateId('object', self::MEDIA_CONTENT_ID);
670
        // $mediaFolderId contains the ID of the "Media" folder
671
672
        // Load the VersionInfo for "Media" folder
673
        $versionInfo = $this->contentService->loadVersionInfoById($mediaFolderId);
674
675
        $this->assertInstanceOf(
676
            VersionInfo::class,
677
            $versionInfo
678
        );
679
680
        return $versionInfo;
681
    }
682
683
    /**
684
     * Test for the returned value of the loadVersionInfoById() method.
685
     *
686
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
687
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
688
     *
689
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
690
     */
691
    public function testLoadVersionInfoByIdSetsExpectedVersionInfo(VersionInfo $versionInfo)
692
    {
693
        $this->assertPropertiesCorrect(
694
            [
695
                'names' => [
696
                    self::ENG_US => 'Media',
697
                ],
698
                'contentInfo' => new ContentInfo($this->getExpectedMediaContentInfoProperties()),
699
                'id' => 472,
700
                'versionNo' => 1,
701
                'modificationDate' => $this->createDateTime(1060695457),
702
                'creatorId' => 14,
703
                'creationDate' => $this->createDateTime(1060695450),
704
                'status' => VersionInfo::STATUS_PUBLISHED,
705
                'initialLanguageCode' => self::ENG_US,
706
                'languageCodes' => [
707
                    self::ENG_US,
708
                ],
709
            ],
710
            $versionInfo
711
        );
712
        $this->assertTrue($versionInfo->isPublished());
713
        $this->assertFalse($versionInfo->isDraft());
714
        $this->assertFalse($versionInfo->isArchived());
715
    }
716
717
    /**
718
     * Test for the loadVersionInfoById() method.
719
     *
720
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
721
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
722
     */
723
    public function testLoadVersionInfoByIdThrowsNotFoundException()
724
    {
725
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
726
727
        $this->expectException(NotFoundException::class);
728
729
        $this->contentService->loadVersionInfoById($nonExistentContentId);
730
    }
731
732
    /**
733
     * Test for the loadContentByContentInfo() method.
734
     *
735
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
736
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
737
     */
738
    public function testLoadContentByContentInfo()
739
    {
740
        $mediaFolderId = $this->generateId('object', self::MEDIA_CONTENT_ID);
741
        // $mediaFolderId contains the ID of the "Media" folder
742
743
        // Load the ContentInfo for "Media" folder
744
        $contentInfo = $this->contentService->loadContentInfo($mediaFolderId);
745
746
        // Now load the current content version for the info instance
747
        $content = $this->contentService->loadContentByContentInfo($contentInfo);
748
749
        $this->assertInstanceOf(
750
            Content::class,
751
            $content
752
        );
753
    }
754
755
    /**
756
     * Test for the loadContentByVersionInfo() method.
757
     *
758
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
759
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
760
     */
761
    public function testLoadContentByVersionInfo()
762
    {
763
        $mediaFolderId = $this->generateId('object', self::MEDIA_CONTENT_ID);
764
        // $mediaFolderId contains the ID of the "Media" folder
765
766
        // Load the ContentInfo for "Media" folder
767
        $contentInfo = $this->contentService->loadContentInfo($mediaFolderId);
768
769
        // Load the current VersionInfo
770
        $versionInfo = $this->contentService->loadVersionInfo($contentInfo);
771
772
        // Now load the current content version for the info instance
773
        $content = $this->contentService->loadContentByVersionInfo($versionInfo);
774
775
        $this->assertInstanceOf(
776
            Content::class,
777
            $content
778
        );
779
    }
780
781
    /**
782
     * Test for the loadContent() method.
783
     *
784
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
785
     * @group user
786
     * @group field-type
787
     */
788
    public function testLoadContent()
789
    {
790
        $mediaFolderId = $this->generateId('object', self::MEDIA_CONTENT_ID);
791
        // $mediaFolderId contains the ID of the "Media" folder
792
793
        // Load the Content for "Media" folder, any language and current version
794
        $content = $this->contentService->loadContent($mediaFolderId);
795
796
        $this->assertInstanceOf(
797
            Content::class,
798
            $content
799
        );
800
    }
801
802
    /**
803
     * Test for the loadContent() method.
804
     *
805
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
806
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
807
     */
808
    public function testLoadContentThrowsNotFoundException()
809
    {
810
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
811
812
        $this->expectException(NotFoundException::class);
813
814
        $this->contentService->loadContent($nonExistentContentId);
815
    }
816
817
    /**
818
     * Data provider for testLoadContentByRemoteId().
819
     *
820
     * @return array
821
     */
822
    public function contentRemoteIdVersionLanguageProvider()
823
    {
824
        return [
825
            ['f5c88a2209584891056f987fd965b0ba', null, null],
826
            ['f5c88a2209584891056f987fd965b0ba', [self::ENG_US], null],
827
            ['f5c88a2209584891056f987fd965b0ba', null, 1],
828
            ['f5c88a2209584891056f987fd965b0ba', [self::ENG_US], 1],
829
            [self::MEDIA_REMOTE_ID, null, null],
830
            [self::MEDIA_REMOTE_ID, [self::ENG_US], null],
831
            [self::MEDIA_REMOTE_ID, null, 1],
832
            [self::MEDIA_REMOTE_ID, [self::ENG_US], 1],
833
        ];
834
    }
835
836
    /**
837
     * Test for the loadContentByRemoteId() method.
838
     *
839
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId
840
     * @dataProvider contentRemoteIdVersionLanguageProvider
841
     *
842
     * @param string $remoteId
843
     * @param array|null $languages
844
     * @param int $versionNo
845
     */
846
    public function testLoadContentByRemoteId($remoteId, $languages, $versionNo)
847
    {
848
        $content = $this->contentService->loadContentByRemoteId($remoteId, $languages, $versionNo);
849
850
        $this->assertInstanceOf(
851
            Content::class,
852
            $content
853
        );
854
855
        $this->assertEquals($remoteId, $content->contentInfo->remoteId);
856
        if ($languages !== null) {
857
            $this->assertEquals($languages, $content->getVersionInfo()->languageCodes);
858
        }
859
        $this->assertEquals($versionNo ?: 1, $content->getVersionInfo()->versionNo);
860
    }
861
862
    /**
863
     * Test for the loadContentByRemoteId() method.
864
     *
865
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
866
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
867
     */
868
    public function testLoadContentByRemoteIdThrowsNotFoundException()
869
    {
870
        $this->expectException(NotFoundException::class);
871
872
        // This call will fail with a "NotFoundException", because no content object exists for the given remoteId
873
        $this->contentService->loadContentByRemoteId('a1b1c1d1e1f1a2b2c2d2e2f2a3b3c3d3');
874
    }
875
876
    /**
877
     * Test for the publishVersion() method.
878
     *
879
     * @return \eZ\Publish\API\Repository\Values\Content\Content
880
     *
881
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
882
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
883
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
884
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
885
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
886
     * @group user
887
     * @group field-type
888
     */
889
    public function testPublishVersion()
890
    {
891
        $time = time();
892
        $content = $this->createContentVersion1();
893
894
        $this->assertInstanceOf(Content::class, $content);
895
        $this->assertTrue($content->contentInfo->published);
896
        $this->assertEquals(VersionInfo::STATUS_PUBLISHED, $content->versionInfo->status);
897
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->publishedDate->getTimestamp());
898
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->modificationDate->getTimestamp());
899
        $this->assertTrue($content->versionInfo->isPublished());
900
        $this->assertFalse($content->versionInfo->isDraft());
901
        $this->assertFalse($content->versionInfo->isArchived());
902
903
        return $content;
904
    }
905
906
    /**
907
     * Test for the publishVersion() method.
908
     *
909
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
910
     *
911
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
912
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
913
     */
914
    public function testPublishVersionSetsExpectedContentInfo($content)
915
    {
916
        $this->assertEquals(
917
            [
918
                $content->id,
919
                true,
920
                1,
921
                'abcdef0123456789abcdef0123456789',
922
                self::ENG_US,
923
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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...
924
                true,
925
            ],
926
            [
927
                $content->contentInfo->id,
928
                $content->contentInfo->alwaysAvailable,
929
                $content->contentInfo->currentVersionNo,
930
                $content->contentInfo->remoteId,
931
                $content->contentInfo->mainLanguageCode,
932
                $content->contentInfo->ownerId,
933
                $content->contentInfo->published,
934
            ]
935
        );
936
937
        $this->assertNotNull($content->contentInfo->mainLocationId);
938
        $date = new \DateTime('1984/01/01');
939
        $this->assertGreaterThan(
940
            $date->getTimestamp(),
941
            $content->contentInfo->publishedDate->getTimestamp()
942
        );
943
    }
944
945
    /**
946
     * Test for the publishVersion() method.
947
     *
948
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
949
     *
950
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
951
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
952
     */
953
    public function testPublishVersionSetsExpectedVersionInfo($content)
954
    {
955
        $this->assertEquals(
956
            [
957
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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...
958
                self::ENG_US,
959
                VersionInfo::STATUS_PUBLISHED,
960
                1,
961
            ],
962
            [
963
                $content->getVersionInfo()->creatorId,
964
                $content->getVersionInfo()->initialLanguageCode,
965
                $content->getVersionInfo()->status,
966
                $content->getVersionInfo()->versionNo,
967
            ]
968
        );
969
970
        $date = new \DateTime('1984/01/01');
971
        $this->assertGreaterThan(
972
            $date->getTimestamp(),
973
            $content->getVersionInfo()->modificationDate->getTimestamp()
974
        );
975
976
        $this->assertNotNull($content->getVersionInfo()->modificationDate);
977
        $this->assertTrue($content->getVersionInfo()->isPublished());
978
        $this->assertFalse($content->getVersionInfo()->isDraft());
979
        $this->assertFalse($content->getVersionInfo()->isArchived());
980
    }
981
982
    /**
983
     * Test for the publishVersion() method.
984
     *
985
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
986
     *
987
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
988
     * @depends testPublishVersion
989
     */
990
    public function testPublishVersionSetsExpectedContentType($content)
991
    {
992
        $contentType = $content->getContentType();
993
994
        $this->assertEquals(
995
            [
996
                $contentType->id,
997
                // won't be a match as it's set to true in createContentDraftVersion1()
998
                //$contentType->defaultAlwaysAvailable,
999
                //$contentType->defaultSortField,
1000
                //$contentType->defaultSortOrder,
1001
            ],
1002
            [
1003
                $content->contentInfo->contentTypeId,
1004
                //$content->contentInfo->alwaysAvailable,
1005
                //$location->sortField,
1006
                //$location->sortOrder,
1007
            ]
1008
        );
1009
    }
1010
1011
    /**
1012
     * Test for the publishVersion() method.
1013
     *
1014
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1015
     *
1016
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1017
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
1018
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1019
     */
1020
    public function testPublishVersionCreatesLocationsDefinedOnCreate()
1021
    {
1022
        $content = $this->createContentVersion1();
1023
1024
        $location = $this->locationService->loadLocationByRemoteId(
1025
            '0123456789abcdef0123456789abcdef'
1026
        );
1027
1028
        $this->assertEquals(
1029
            $location->getContentInfo(),
1030
            $content->getVersionInfo()->getContentInfo()
1031
        );
1032
1033
        return [$content, $location];
1034
    }
1035
1036
    /**
1037
     * Test for the publishVersion() method.
1038
     *
1039
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1040
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionCreatesLocationsDefinedOnCreate
1041
     */
1042
    public function testCreateContentWithLocationCreateParameterCreatesExpectedLocation(array $testData)
1043
    {
1044
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
1045
        /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
1046
        list($content, $location) = $testData;
1047
1048
        $parentLocationId = $this->generateId('location', 56);
1049
        $parentLocation = $this->getRepository()->getLocationService()->loadLocation($parentLocationId);
1050
        $mainLocationId = $content->getVersionInfo()->getContentInfo()->mainLocationId;
1051
1052
        $this->assertPropertiesCorrect(
1053
            [
1054
                'id' => $mainLocationId,
1055
                'priority' => 23,
1056
                'hidden' => true,
1057
                'invisible' => true,
1058
                'remoteId' => '0123456789abcdef0123456789abcdef',
1059
                'parentLocationId' => $parentLocationId,
1060
                'pathString' => $parentLocation->pathString . $mainLocationId . '/',
1061
                'depth' => $parentLocation->depth + 1,
1062
                'sortField' => Location::SORT_FIELD_NODE_ID,
1063
                'sortOrder' => Location::SORT_ORDER_DESC,
1064
            ],
1065
            $location
1066
        );
1067
    }
1068
1069
    /**
1070
     * Test for the publishVersion() method.
1071
     *
1072
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1073
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1074
     */
1075
    public function testPublishVersionThrowsBadStateException()
1076
    {
1077
        $draft = $this->createContentDraftVersion1();
1078
1079
        // Publish the content draft
1080
        $this->contentService->publishVersion($draft->getVersionInfo());
1081
1082
        $this->expectException(BadStateException::class);
1083
1084
        // This call will fail with a "BadStateException", because the version is already published.
1085
        $this->contentService->publishVersion($draft->getVersionInfo());
1086
    }
1087
1088
    /**
1089
     * Test that publishVersion() does not affect publishedDate (assuming previous version exists).
1090
     *
1091
     * @covers \eZ\Publish\API\Repository\ContentService::publishVersion
1092
     */
1093
    public function testPublishVersionDoesNotChangePublishedDate()
1094
    {
1095
        $publishedContent = $this->createContentVersion1();
1096
1097
        // force timestamps to differ
1098
        sleep(1);
1099
1100
        $contentDraft = $this->contentService->createContentDraft($publishedContent->contentInfo);
1101
        $contentUpdateStruct = $this->contentService->newContentUpdateStruct();
1102
        $contentUpdateStruct->setField('name', 'New name');
1103
        $contentDraft = $this->contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
1104
        $republishedContent = $this->contentService->publishVersion($contentDraft->versionInfo);
1105
1106
        $this->assertEquals(
1107
            $publishedContent->contentInfo->publishedDate->getTimestamp(),
1108
            $republishedContent->contentInfo->publishedDate->getTimestamp()
1109
        );
1110
        $this->assertGreaterThan(
1111
            $publishedContent->contentInfo->modificationDate->getTimestamp(),
1112
            $republishedContent->contentInfo->modificationDate->getTimestamp()
1113
        );
1114
    }
1115
1116
    /**
1117
     * Test for the createContentDraft() method.
1118
     *
1119
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1120
     *
1121
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1122
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1123
     * @group user
1124
     */
1125
    public function testCreateContentDraft()
1126
    {
1127
        $content = $this->createContentVersion1();
1128
1129
        // Now we create a new draft from the published content
1130
        $draftedContent = $this->contentService->createContentDraft($content->contentInfo);
1131
1132
        $this->assertInstanceOf(
1133
            Content::class,
1134
            $draftedContent
1135
        );
1136
1137
        return $draftedContent;
1138
    }
1139
1140
    /**
1141
     * Test for the createContentDraft() method.
1142
     *
1143
     * Test that editor has access to edit own draft.
1144
     * Note: Editors have access to version_read, which is needed to load content drafts.
1145
     *
1146
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1147
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1148
     * @group user
1149
     */
1150
    public function testCreateContentDraftAndLoadAccess()
1151
    {
1152
        $user = $this->createUserVersion1();
1153
1154
        // Set new editor as user
1155
        $this->permissionResolver->setCurrentUserReference($user);
1156
1157
        // Create draft
1158
        $draft = $this->createContentDraftVersion1(2, 'folder');
1159
1160
        // Try to load the draft
1161
        $loadedDraft = $this->contentService->loadContent($draft->id);
1162
1163
        $this->assertEquals($draft->id, $loadedDraft->id);
1164
    }
1165
1166
    /**
1167
     * Test for the createContentDraft() method.
1168
     *
1169
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1170
     *
1171
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1172
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1173
     */
1174
    public function testCreateContentDraftSetsExpectedProperties($draft)
1175
    {
1176
        $this->assertEquals(
1177
            [
1178
                'fieldCount' => 2,
1179
                'relationCount' => 0,
1180
            ],
1181
            [
1182
                'fieldCount' => count($draft->getFields()),
1183
                'relationCount' => count($this->getRepository()->getContentService()->loadRelations($draft->getVersionInfo())),
1184
            ]
1185
        );
1186
    }
1187
1188
    /**
1189
     * Test for the createContentDraft() method.
1190
     *
1191
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1192
     *
1193
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1194
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1195
     */
1196
    public function testCreateContentDraftSetsContentInfo($draft)
1197
    {
1198
        $contentInfo = $draft->contentInfo;
1199
1200
        $this->assertEquals(
1201
            [
1202
                $draft->id,
1203
                true,
1204
                1,
1205
                self::ENG_US,
1206
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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...
1207
                'abcdef0123456789abcdef0123456789',
1208
                1,
1209
            ],
1210
            [
1211
                $contentInfo->id,
1212
                $contentInfo->alwaysAvailable,
1213
                $contentInfo->currentVersionNo,
1214
                $contentInfo->mainLanguageCode,
1215
                $contentInfo->ownerId,
1216
                $contentInfo->remoteId,
1217
                $contentInfo->sectionId,
1218
            ]
1219
        );
1220
    }
1221
1222
    /**
1223
     * Test for the createContentDraft() method.
1224
     *
1225
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1226
     *
1227
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1228
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1229
     */
1230
    public function testCreateContentDraftSetsVersionInfo($draft)
1231
    {
1232
        $versionInfo = $draft->getVersionInfo();
1233
1234
        $this->assertEquals(
1235
            [
1236
                'creatorId' => $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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...
1237
                'initialLanguageCode' => self::ENG_US,
1238
                'languageCodes' => [0 => self::ENG_US],
1239
                'status' => VersionInfo::STATUS_DRAFT,
1240
                'versionNo' => 2,
1241
            ],
1242
            [
1243
                'creatorId' => $versionInfo->creatorId,
1244
                'initialLanguageCode' => $versionInfo->initialLanguageCode,
1245
                'languageCodes' => $versionInfo->languageCodes,
1246
                'status' => $versionInfo->status,
1247
                'versionNo' => $versionInfo->versionNo,
1248
            ]
1249
        );
1250
        $this->assertTrue($versionInfo->isDraft());
1251
        $this->assertFalse($versionInfo->isPublished());
1252
        $this->assertFalse($versionInfo->isArchived());
1253
    }
1254
1255
    /**
1256
     * Test for the createContentDraft() method.
1257
     *
1258
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1259
     *
1260
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1261
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1262
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
1263
     */
1264
    public function testCreateContentDraftLoadVersionInfoStillLoadsPublishedVersion($draft)
0 ignored issues
show
Unused Code introduced by
The parameter $draft is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1265
    {
1266
        $content = $this->createContentVersion1();
1267
1268
        // Now we create a new draft from the published content
1269
        $this->contentService->createContentDraft($content->contentInfo);
1270
1271
        // This call will still load the published version
1272
        $versionInfoPublished = $this->contentService->loadVersionInfo($content->contentInfo);
1273
1274
        $this->assertEquals(1, $versionInfoPublished->versionNo);
1275
    }
1276
1277
    /**
1278
     * Test for the createContentDraft() method.
1279
     *
1280
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1281
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
1282
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1283
     */
1284
    public function testCreateContentDraftLoadContentStillLoadsPublishedVersion()
1285
    {
1286
        $content = $this->createContentVersion1();
1287
1288
        // Now we create a new draft from the published content
1289
        $this->contentService->createContentDraft($content->contentInfo);
1290
1291
        // This call will still load the published content version
1292
        $contentPublished = $this->contentService->loadContent($content->id);
1293
1294
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1295
    }
1296
1297
    /**
1298
     * Test for the createContentDraft() method.
1299
     *
1300
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1301
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
1302
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1303
     */
1304
    public function testCreateContentDraftLoadContentByRemoteIdStillLoadsPublishedVersion()
1305
    {
1306
        $content = $this->createContentVersion1();
1307
1308
        // Now we create a new draft from the published content
1309
        $this->contentService->createContentDraft($content->contentInfo);
1310
1311
        // This call will still load the published content version
1312
        $contentPublished = $this->contentService->loadContentByRemoteId('abcdef0123456789abcdef0123456789');
1313
1314
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1315
    }
1316
1317
    /**
1318
     * Test for the createContentDraft() method.
1319
     *
1320
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1321
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
1322
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1323
     */
1324
    public function testCreateContentDraftLoadContentByContentInfoStillLoadsPublishedVersion()
1325
    {
1326
        $content = $this->createContentVersion1();
1327
1328
        // Now we create a new draft from the published content
1329
        $this->contentService->createContentDraft($content->contentInfo);
1330
1331
        // This call will still load the published content version
1332
        $contentPublished = $this->contentService->loadContentByContentInfo($content->contentInfo);
1333
1334
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1335
    }
1336
1337
    /**
1338
     * Test for the newContentUpdateStruct() method.
1339
     *
1340
     * @covers \eZ\Publish\API\Repository\ContentService::newContentUpdateStruct
1341
     * @group user
1342
     */
1343
    public function testNewContentUpdateStruct()
1344
    {
1345
        $updateStruct = $this->contentService->newContentUpdateStruct();
1346
1347
        $this->assertInstanceOf(
1348
            ContentUpdateStruct::class,
1349
            $updateStruct
1350
        );
1351
1352
        $this->assertPropertiesCorrect(
1353
            [
1354
                'initialLanguageCode' => null,
1355
                'fields' => [],
1356
            ],
1357
            $updateStruct
1358
        );
1359
    }
1360
1361
    /**
1362
     * Test for the updateContent() method.
1363
     *
1364
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1365
     *
1366
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1367
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1368
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1369
     * @group user
1370
     * @group field-type
1371
     */
1372
    public function testUpdateContent()
1373
    {
1374
        $draftVersion2 = $this->createUpdatedDraftVersion2();
1375
1376
        $this->assertInstanceOf(
1377
            Content::class,
1378
            $draftVersion2
1379
        );
1380
1381
        $this->assertEquals(
1382
            $this->generateId('user', 10),
1383
            $draftVersion2->versionInfo->creatorId,
1384
            'creatorId is not properly set on new Version'
1385
        );
1386
1387
        return $draftVersion2;
1388
    }
1389
1390
    /**
1391
     * Test for the updateContent_WithDifferentUser() method.
1392
     *
1393
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1394
     *
1395
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1396
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1397
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1398
     * @group user
1399
     * @group field-type
1400
     */
1401
    public function testUpdateContentWithDifferentUser()
1402
    {
1403
        $arrayWithDraftVersion2 = $this->createUpdatedDraftVersion2NotAdmin();
1404
1405
        $this->assertInstanceOf(
1406
            Content::class,
1407
            $arrayWithDraftVersion2[0]
1408
        );
1409
1410
        $this->assertEquals(
1411
            $this->generateId('user', $arrayWithDraftVersion2[1]),
1412
            $arrayWithDraftVersion2[0]->versionInfo->creatorId,
1413
            'creatorId is not properly set on new Version'
1414
        );
1415
1416
        return $arrayWithDraftVersion2[0];
1417
    }
1418
1419
    /**
1420
     * Test for the updateContent() method.
1421
     *
1422
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1423
     *
1424
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1425
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1426
     */
1427
    public function testUpdateContentSetsExpectedFields($content)
1428
    {
1429
        $actual = $this->normalizeFields($content->getFields());
1430
1431
        $expected = [
1432
            new Field(
1433
                [
1434
                    'id' => 0,
1435
                    'value' => true,
1436
                    'languageCode' => self::ENG_GB,
1437
                    'fieldDefIdentifier' => 'description',
1438
                    'fieldTypeIdentifier' => 'ezrichtext',
1439
                ]
1440
            ),
1441
            new Field(
1442
                [
1443
                    'id' => 0,
1444
                    'value' => true,
1445
                    'languageCode' => self::ENG_US,
1446
                    'fieldDefIdentifier' => 'description',
1447
                    'fieldTypeIdentifier' => 'ezrichtext',
1448
                ]
1449
            ),
1450
            new Field(
1451
                [
1452
                    'id' => 0,
1453
                    'value' => true,
1454
                    'languageCode' => self::ENG_GB,
1455
                    'fieldDefIdentifier' => 'name',
1456
                    'fieldTypeIdentifier' => 'ezstring',
1457
                ]
1458
            ),
1459
            new Field(
1460
                [
1461
                    'id' => 0,
1462
                    'value' => true,
1463
                    'languageCode' => self::ENG_US,
1464
                    'fieldDefIdentifier' => 'name',
1465
                    'fieldTypeIdentifier' => 'ezstring',
1466
                ]
1467
            ),
1468
        ];
1469
1470
        $this->assertEquals($expected, $actual);
1471
    }
1472
1473
    /**
1474
     * Test for the updateContent() method.
1475
     *
1476
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1477
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1478
     */
1479
    public function testUpdateContentThrowsBadStateException()
1480
    {
1481
        $content = $this->createContentVersion1();
1482
1483
        // Now create an update struct and modify some fields
1484
        $contentUpdateStruct = $this->contentService->newContentUpdateStruct();
1485
        $contentUpdateStruct->setField('title', 'An awesome² story about ezp.');
1486
        $contentUpdateStruct->setField('title', 'An awesome²³ story about ezp.', self::ENG_GB);
1487
1488
        $contentUpdateStruct->initialLanguageCode = self::ENG_US;
1489
1490
        $this->expectException(BadStateException::class);
1491
1492
        // This call will fail with a "BadStateException", because $publishedContent is not a draft.
1493
        $this->contentService->updateContent(
1494
            $content->getVersionInfo(),
1495
            $contentUpdateStruct
1496
        );
1497
    }
1498
1499
    /**
1500
     * Test for the updateContent() method.
1501
     *
1502
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1503
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1504
     */
1505
    public function testUpdateContentThrowsInvalidArgumentExceptionWhenFieldTypeDoesNotAccept()
1506
    {
1507
        $draft = $this->createContentDraftVersion1();
1508
1509
        // Now create an update struct and modify some fields
1510
        $contentUpdateStruct = $this->contentService->newContentUpdateStruct();
1511
        // The name field does not accept a stdClass object as its input
1512
        $contentUpdateStruct->setField('name', new \stdClass(), self::ENG_US);
1513
1514
        $this->expectException(APIInvalidArgumentException::class);
1515
        // is not accepted
1516
        $this->contentService->updateContent(
1517
            $draft->getVersionInfo(),
1518
            $contentUpdateStruct
1519
        );
1520
    }
1521
1522
    /**
1523
     * Test for the updateContent() method.
1524
     *
1525
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1526
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1527
     */
1528
    public function testUpdateContentWhenMandatoryFieldIsEmpty()
1529
    {
1530
        $draft = $this->createContentDraftVersion1();
1531
1532
        // Now create an update struct and set a mandatory field to null
1533
        $contentUpdateStruct = $this->contentService->newContentUpdateStruct();
1534
        $contentUpdateStruct->setField('name', null);
1535
1536
        // Don't set this, then the above call without languageCode will fail
1537
        $contentUpdateStruct->initialLanguageCode = self::ENG_US;
1538
1539
        $this->expectException(ContentFieldValidationException::class);
1540
1541
        // This call will fail with a "ContentFieldValidationException", because the mandatory "name" field is empty.
1542
        $this->contentService->updateContent(
1543
            $draft->getVersionInfo(),
1544
            $contentUpdateStruct
1545
        );
1546
    }
1547
1548
    /**
1549
     * Test for the updateContent() method.
1550
     *
1551
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1552
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1553
     */
1554
    public function testUpdateContentThrowsContentFieldValidationException()
1555
    {
1556
        $contentTypeService = $this->getRepository()->getContentTypeService();
1557
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1558
1559
        $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
1560
        $contentCreate->setField('name', 'An awesome Sidelfingen folder');
1561
1562
        $draft = $this->contentService->createContent($contentCreate);
1563
1564
        $contentUpdate = $this->contentService->newContentUpdateStruct();
1565
        // Violates string length constraint
1566
        $contentUpdate->setField('short_name', str_repeat('a', 200), self::ENG_US);
1567
1568
        $this->expectException(ContentFieldValidationException::class);
1569
1570
        // Throws ContentFieldValidationException because the string length validation of the field "short_name" fails
1571
        $this->contentService->updateContent($draft->getVersionInfo(), $contentUpdate);
1572
    }
1573
1574
    /**
1575
     * Test for the updateContent() method.
1576
     *
1577
     * @covers \eZ\Publish\API\Repository\ContentService::updateContent()
1578
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1579
     */
1580
    public function testUpdateContentValidatorIgnoresRequiredFieldsOfNotUpdatedLanguages()
1581
    {
1582
        $contentTypeService = $this->getRepository()->getContentTypeService();
1583
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1584
1585
        // Create multilangual content
1586
        $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
1587
        $contentCreate->setField('name', 'An awesome Sidelfingen folder', self::ENG_US);
1588
        $contentCreate->setField('name', 'An awesome Sidelfingen folder', self::ENG_GB);
1589
1590
        $contentDraft = $this->contentService->createContent($contentCreate);
1591
1592
        // 2. Update content type definition
1593
        $contentTypeDraft = $contentTypeService->createContentTypeDraft($contentType);
1594
1595
        $fieldDefinition = $contentType->getFieldDefinition('description');
1596
        $fieldDefinitionUpdate = $contentTypeService->newFieldDefinitionUpdateStruct();
1597
        $fieldDefinitionUpdate->identifier = 'description';
1598
        $fieldDefinitionUpdate->isRequired = true;
1599
1600
        $contentTypeService->updateFieldDefinition(
1601
            $contentTypeDraft,
1602
            $fieldDefinition,
1603
            $fieldDefinitionUpdate
1604
        );
1605
        $contentTypeService->publishContentTypeDraft($contentTypeDraft);
1606
1607
        // 3. Update only eng-US translation
1608
        $description = new DOMDocument();
1609
        $description->loadXML(<<<XML
1610
<?xml version="1.0" encoding="UTF-8"?>
1611
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
1612
    <para>Lorem ipsum dolor</para>
1613
</section>
1614
XML
1615
        );
1616
1617
        $contentUpdate = $this->contentService->newContentUpdateStruct();
1618
        $contentUpdate->setField('name', 'An awesome Sidelfingen folder (updated)', self::ENG_US);
1619
        $contentUpdate->setField('description', $description);
1620
1621
        $this->contentService->updateContent($contentDraft->getVersionInfo(), $contentUpdate);
1622
    }
1623
1624
    /**
1625
     * Test for the updateContent() method.
1626
     *
1627
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1628
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1629
     */
1630
    public function testUpdateContentWithNotUpdatingMandatoryField()
1631
    {
1632
        $draft = $this->createContentDraftVersion1();
1633
1634
        // Now create an update struct which does not overwrite mandatory
1635
        // fields
1636
        $contentUpdateStruct = $this->contentService->newContentUpdateStruct();
1637
        $contentUpdateStruct->setField(
1638
            'description',
1639
            '<?xml version="1.0" encoding="UTF-8"?><section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0-variant ezpublish-1.0"/>'
1640
        );
1641
1642
        // Don't set this, then the above call without languageCode will fail
1643
        $contentUpdateStruct->initialLanguageCode = self::ENG_US;
1644
1645
        // This will only update the "description" field in the "eng-US" language
1646
        $updatedDraft = $this->contentService->updateContent(
1647
            $draft->getVersionInfo(),
1648
            $contentUpdateStruct
1649
        );
1650
1651
        foreach ($updatedDraft->getFields() as $field) {
1652
            if ($field->languageCode === self::ENG_US && $field->fieldDefIdentifier === 'name' && $field->value !== null) {
1653
                // Found field
1654
                return;
1655
            }
1656
        }
1657
        $this->fail(
1658
            'Field with identifier "name" in language "eng-US" could not be found or has empty value.'
1659
        );
1660
    }
1661
1662
    /**
1663
     * Test for the createContentDraft() method.
1664
     *
1665
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
1666
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1667
     */
1668
    public function testCreateContentDraftWithSecondParameter()
1669
    {
1670
        $contentVersion2 = $this->createContentVersion2();
1671
1672
        // Now we create a new draft from the initial version
1673
        $draftedContentReloaded = $this->contentService->createContentDraft(
1674
            $contentVersion2->contentInfo,
1675
            $contentVersion2->getVersionInfo()
1676
        );
1677
1678
        $this->assertEquals(3, $draftedContentReloaded->getVersionInfo()->versionNo);
1679
    }
1680
1681
    /**
1682
     * Test for the createContentDraft() method with third parameter.
1683
     *
1684
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1685
     */
1686
    public function testCreateContentDraftWithThirdParameter()
1687
    {
1688
        $content = $this->contentService->loadContent(4);
1689
        $user = $this->createUserVersion1();
1690
1691
        $draftContent = $this->contentService->createContentDraft(
1692
            $content->contentInfo,
1693
            $content->getVersionInfo(),
1694
            $user
1695
        );
1696
1697
        $this->assertInstanceOf(
1698
            Content::class,
1699
            $draftContent
1700
        );
1701
    }
1702
1703
    /**
1704
     * Test for the publishVersion() method.
1705
     *
1706
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1707
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1708
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1709
     */
1710
    public function testPublishVersionFromContentDraft()
1711
    {
1712
        $contentVersion2 = $this->createContentVersion2();
1713
1714
        $versionInfo = $this->contentService->loadVersionInfo($contentVersion2->contentInfo);
1715
1716
        $this->assertEquals(
1717
            [
1718
                'status' => VersionInfo::STATUS_PUBLISHED,
1719
                'versionNo' => 2,
1720
            ],
1721
            [
1722
                'status' => $versionInfo->status,
1723
                'versionNo' => $versionInfo->versionNo,
1724
            ]
1725
        );
1726
        $this->assertTrue($versionInfo->isPublished());
1727
        $this->assertFalse($versionInfo->isDraft());
1728
        $this->assertFalse($versionInfo->isArchived());
1729
    }
1730
1731
    /**
1732
     * Test for the publishVersion() method.
1733
     *
1734
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1735
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1736
     */
1737
    public function testPublishVersionFromContentDraftArchivesOldVersion()
1738
    {
1739
        $contentVersion2 = $this->createContentVersion2();
1740
1741
        $versionInfo = $this->contentService->loadVersionInfo($contentVersion2->contentInfo, 1);
1742
1743
        $this->assertEquals(
1744
            [
1745
                'status' => VersionInfo::STATUS_ARCHIVED,
1746
                'versionNo' => 1,
1747
            ],
1748
            [
1749
                'status' => $versionInfo->status,
1750
                'versionNo' => $versionInfo->versionNo,
1751
            ]
1752
        );
1753
        $this->assertTrue($versionInfo->isArchived());
1754
        $this->assertFalse($versionInfo->isDraft());
1755
        $this->assertFalse($versionInfo->isPublished());
1756
    }
1757
1758
    /**
1759
     * Test for the publishVersion() method.
1760
     *
1761
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1762
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1763
     */
1764
    public function testPublishVersionFromContentDraftUpdatesContentInfoCurrentVersion()
1765
    {
1766
        $contentVersion2 = $this->createContentVersion2();
1767
1768
        $this->assertEquals(2, $contentVersion2->contentInfo->currentVersionNo);
1769
    }
1770
1771
    /**
1772
     * Test for the publishVersion() method.
1773
     *
1774
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1775
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1776
     */
1777
    public function testPublishVersionFromOldContentDraftArchivesNewerVersionNo()
1778
    {
1779
        $content = $this->createContentVersion1();
1780
1781
        // Create a new draft with versionNo = 2
1782
        $draftedContentVersion2 = $this->contentService->createContentDraft($content->contentInfo);
1783
1784
        // Create another new draft with versionNo = 3
1785
        $draftedContentVersion3 = $this->contentService->createContentDraft($content->contentInfo);
1786
1787
        // Publish draft with versionNo = 3
1788
        $this->contentService->publishVersion($draftedContentVersion3->getVersionInfo());
1789
1790
        // Publish the first draft with versionNo = 2
1791
        // currentVersionNo is now 2, versionNo 3 will be archived
1792
        $publishedDraft = $this->contentService->publishVersion($draftedContentVersion2->getVersionInfo());
1793
1794
        $this->assertEquals(2, $publishedDraft->contentInfo->currentVersionNo);
1795
    }
1796
1797
    /**
1798
     * Test for the publishVersion() method, and that it creates limited archives.
1799
     *
1800
     * @todo Adapt this when per content type archive limited is added on repository Content Type model.
1801
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1802
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1803
     */
1804
    public function testPublishVersionNotCreatingUnlimitedArchives()
1805
    {
1806
        $content = $this->createContentVersion1();
1807
1808
        // load first to make sure list gets updated also (cache)
1809
        $versionInfoList = $this->contentService->loadVersions($content->contentInfo);
1810
        $this->assertEquals(1, count($versionInfoList));
1811
        $this->assertEquals(1, $versionInfoList[0]->versionNo);
1812
1813
        // Create a new draft with versionNo = 2
1814
        $draftedContentVersion = $this->contentService->createContentDraft($content->contentInfo);
1815
        $this->contentService->publishVersion($draftedContentVersion->getVersionInfo());
1816
1817
        // Create a new draft with versionNo = 3
1818
        $draftedContentVersion = $this->contentService->createContentDraft($content->contentInfo);
1819
        $this->contentService->publishVersion($draftedContentVersion->getVersionInfo());
1820
1821
        // Create a new draft with versionNo = 4
1822
        $draftedContentVersion = $this->contentService->createContentDraft($content->contentInfo);
1823
        $this->contentService->publishVersion($draftedContentVersion->getVersionInfo());
1824
1825
        // Create a new draft with versionNo = 5
1826
        $draftedContentVersion = $this->contentService->createContentDraft($content->contentInfo);
1827
        $this->contentService->publishVersion($draftedContentVersion->getVersionInfo());
1828
1829
        // Create a new draft with versionNo = 6
1830
        $draftedContentVersion = $this->contentService->createContentDraft($content->contentInfo);
1831
        $this->contentService->publishVersion($draftedContentVersion->getVersionInfo());
1832
1833
        // Create a new draft with versionNo = 7
1834
        $draftedContentVersion = $this->contentService->createContentDraft($content->contentInfo);
1835
        $this->contentService->publishVersion($draftedContentVersion->getVersionInfo());
1836
1837
        $versionInfoList = $this->contentService->loadVersions($content->contentInfo);
1838
1839
        $this->assertEquals(6, count($versionInfoList));
1840
        $this->assertEquals(2, $versionInfoList[0]->versionNo);
1841
        $this->assertEquals(7, $versionInfoList[5]->versionNo);
1842
1843
        $this->assertEquals(
1844
            [
1845
                VersionInfo::STATUS_ARCHIVED,
1846
                VersionInfo::STATUS_ARCHIVED,
1847
                VersionInfo::STATUS_ARCHIVED,
1848
                VersionInfo::STATUS_ARCHIVED,
1849
                VersionInfo::STATUS_ARCHIVED,
1850
                VersionInfo::STATUS_PUBLISHED,
1851
            ],
1852
            [
1853
                $versionInfoList[0]->status,
1854
                $versionInfoList[1]->status,
1855
                $versionInfoList[2]->status,
1856
                $versionInfoList[3]->status,
1857
                $versionInfoList[4]->status,
1858
                $versionInfoList[5]->status,
1859
            ]
1860
        );
1861
    }
1862
1863
    /**
1864
     * Test for the newContentMetadataUpdateStruct() method.
1865
     *
1866
     * @covers \eZ\Publish\API\Repository\ContentService::newContentMetadataUpdateStruct
1867
     * @group user
1868
     */
1869
    public function testNewContentMetadataUpdateStruct()
1870
    {
1871
        // Creates a new metadata update struct
1872
        $metadataUpdate = $this->contentService->newContentMetadataUpdateStruct();
1873
1874
        foreach ($metadataUpdate as $propertyName => $propertyValue) {
0 ignored issues
show
Bug introduced by
The expression $metadataUpdate of type object<eZ\Publish\API\Re...ntMetadataUpdateStruct> is not traversable.
Loading history...
1875
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
1876
        }
1877
1878
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1879
        $metadataUpdate->mainLanguageCode = self::ENG_GB;
1880
        $metadataUpdate->alwaysAvailable = false;
1881
1882
        $this->assertInstanceOf(
1883
            ContentMetadataUpdateStruct::class,
1884
            $metadataUpdate
1885
        );
1886
    }
1887
1888
    /**
1889
     * Test for the updateContentMetadata() method.
1890
     *
1891
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1892
     *
1893
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1894
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1895
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentMetadataUpdateStruct
1896
     * @group user
1897
     */
1898
    public function testUpdateContentMetadata()
1899
    {
1900
        $content = $this->createContentVersion1();
1901
1902
        // Creates a metadata update struct
1903
        $metadataUpdate = $this->contentService->newContentMetadataUpdateStruct();
1904
1905
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1906
        $metadataUpdate->mainLanguageCode = self::ENG_GB;
1907
        $metadataUpdate->alwaysAvailable = false;
1908
        $metadataUpdate->publishedDate = $this->createDateTime(441759600); // 1984/01/01
1909
        $metadataUpdate->modificationDate = $this->createDateTime(441759600); // 1984/01/01
1910
1911
        // Update the metadata of the published content object
1912
        $content = $this->contentService->updateContentMetadata(
1913
            $content->contentInfo,
1914
            $metadataUpdate
1915
        );
1916
1917
        $this->assertInstanceOf(
1918
            Content::class,
1919
            $content
1920
        );
1921
1922
        return $content;
1923
    }
1924
1925
    /**
1926
     * Test for the updateContentMetadata() method.
1927
     *
1928
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1929
     *
1930
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1931
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
1932
     */
1933
    public function testUpdateContentMetadataSetsExpectedProperties($content)
1934
    {
1935
        $contentInfo = $content->contentInfo;
1936
1937
        $this->assertEquals(
1938
            [
1939
                'remoteId' => 'aaaabbbbccccddddeeeeffff11112222',
1940
                'sectionId' => $this->generateId('section', 1),
1941
                'alwaysAvailable' => false,
1942
                'currentVersionNo' => 1,
1943
                'mainLanguageCode' => self::ENG_GB,
1944
                'modificationDate' => $this->createDateTime(441759600),
1945
                'ownerId' => $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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...
1946
                'published' => true,
1947
                'publishedDate' => $this->createDateTime(441759600),
1948
            ],
1949
            [
1950
                'remoteId' => $contentInfo->remoteId,
1951
                'sectionId' => $contentInfo->sectionId,
1952
                'alwaysAvailable' => $contentInfo->alwaysAvailable,
1953
                'currentVersionNo' => $contentInfo->currentVersionNo,
1954
                'mainLanguageCode' => $contentInfo->mainLanguageCode,
1955
                'modificationDate' => $contentInfo->modificationDate,
1956
                'ownerId' => $contentInfo->ownerId,
1957
                'published' => $contentInfo->published,
1958
                'publishedDate' => $contentInfo->publishedDate,
1959
            ]
1960
        );
1961
    }
1962
1963
    /**
1964
     * Test for the updateContentMetadata() method.
1965
     *
1966
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1967
     *
1968
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1969
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
1970
     */
1971
    public function testUpdateContentMetadataNotUpdatesContentVersion($content)
1972
    {
1973
        $this->assertEquals(1, $content->getVersionInfo()->versionNo);
1974
    }
1975
1976
    /**
1977
     * Test for the updateContentMetadata() method.
1978
     *
1979
     * @covers \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1980
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
1981
     */
1982
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnDuplicateRemoteId()
1983
    {
1984
        $content = $this->createContentVersion1();
1985
1986
        // Creates a metadata update struct
1987
        $metadataUpdate = $this->contentService->newContentMetadataUpdateStruct();
1988
        $metadataUpdate->remoteId = self::MEDIA_REMOTE_ID;
1989
1990
        $this->expectException(APIInvalidArgumentException::class);
1991
        // specified remoteId is already used by the "Media" page.
1992
        $this->contentService->updateContentMetadata(
1993
            $content->contentInfo,
1994
            $metadataUpdate
1995
        );
1996
    }
1997
1998
    /**
1999
     * Test for the updateContentMetadata() method.
2000
     *
2001
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContentMetadata
2002
     */
2003
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnNoMetadataPropertiesSet()
2004
    {
2005
        $contentInfo = $this->contentService->loadContentInfo(4);
2006
        $contentMetadataUpdateStruct = $this->contentService->newContentMetadataUpdateStruct();
2007
2008
        $this->expectException(APIInvalidArgumentException::class);
2009
        $this->contentService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
2010
    }
2011
2012
    /**
2013
     * Test for the deleteContent() method.
2014
     *
2015
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2016
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2017
     */
2018
    public function testDeleteContent()
2019
    {
2020
        $contentVersion2 = $this->createContentVersion2();
2021
2022
        // Load the locations for this content object
2023
        $locations = $this->locationService->loadLocations($contentVersion2->contentInfo);
2024
2025
        // This will delete the content, all versions and the associated locations
2026
        $this->contentService->deleteContent($contentVersion2->contentInfo);
2027
2028
        $this->expectException(NotFoundException::class);
2029
2030
        foreach ($locations as $location) {
2031
            $this->locationService->loadLocation($location->id);
2032
        }
2033
    }
2034
2035
    /**
2036
     * Test for the deleteContent() method.
2037
     *
2038
     * Test for issue EZP-21057:
2039
     * "contentService: Unable to delete a content with an empty file attribute"
2040
     *
2041
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2042
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2043
     */
2044
    public function testDeleteContentWithEmptyBinaryField()
2045
    {
2046
        $contentVersion = $this->createContentVersion1EmptyBinaryField();
2047
2048
        // Load the locations for this content object
2049
        $locations = $this->locationService->loadLocations($contentVersion->contentInfo);
2050
2051
        // This will delete the content, all versions and the associated locations
2052
        $this->contentService->deleteContent($contentVersion->contentInfo);
2053
2054
        $this->expectException(NotFoundException::class);
2055
2056
        foreach ($locations as $location) {
2057
            $this->locationService->loadLocation($location->id);
2058
        }
2059
    }
2060
2061
    /**
2062
     * Test for the loadContentDrafts() method.
2063
     *
2064
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2065
     */
2066
    public function testLoadContentDraftsReturnsEmptyArrayByDefault()
2067
    {
2068
        $contentDrafts = $this->contentService->loadContentDrafts();
2069
2070
        $this->assertSame([], $contentDrafts);
2071
    }
2072
2073
    /**
2074
     * Test for the loadContentDrafts() method.
2075
     *
2076
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2077
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2078
     */
2079
    public function testLoadContentDrafts()
2080
    {
2081
        // "Media" content object
2082
        $mediaContentInfo = $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID);
2083
2084
        // "eZ Publish Demo Design ..." content object
2085
        $demoDesignContentInfo = $this->contentService->loadContentInfoByRemoteId(self::DEMO_DESIGN_REMOTE_ID);
2086
2087
        // Create some drafts
2088
        $this->contentService->createContentDraft($mediaContentInfo);
2089
        $this->contentService->createContentDraft($demoDesignContentInfo);
2090
2091
        // Now $contentDrafts should contain two drafted versions
2092
        $draftedVersions = $this->contentService->loadContentDrafts();
2093
2094
        $actual = [
2095
            $draftedVersions[0]->status,
2096
            $draftedVersions[0]->getContentInfo()->remoteId,
2097
            $draftedVersions[1]->status,
2098
            $draftedVersions[1]->getContentInfo()->remoteId,
2099
        ];
2100
        sort($actual, SORT_STRING);
2101
2102
        $this->assertEquals(
2103
            [
2104
                VersionInfo::STATUS_DRAFT,
2105
                VersionInfo::STATUS_DRAFT,
2106
                self::DEMO_DESIGN_REMOTE_ID,
2107
                self::MEDIA_REMOTE_ID,
2108
            ],
2109
            $actual
2110
        );
2111
    }
2112
2113
    /**
2114
     * Test for the loadContentDrafts() method.
2115
     *
2116
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
2117
     */
2118
    public function testLoadContentDraftsWithFirstParameter()
2119
    {
2120
        $user = $this->createUserVersion1();
2121
2122
        // Get current user
2123
        $oldCurrentUser = $this->permissionResolver->getCurrentUserReference();
2124
2125
        // Set new editor as user
2126
        $this->permissionResolver->setCurrentUserReference($user);
2127
2128
        // "Media" content object
2129
        $mediaContentInfo = $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID);
2130
2131
        // Create a content draft
2132
        $this->contentService->createContentDraft($mediaContentInfo);
2133
2134
        // Reset to previous current user
2135
        $this->permissionResolver->setCurrentUserReference($oldCurrentUser);
2136
2137
        // Now $contentDrafts for the previous current user and the new user
2138
        $newCurrentUserDrafts = $this->contentService->loadContentDrafts($user);
2139
        $oldCurrentUserDrafts = $this->contentService->loadContentDrafts();
2140
2141
        $this->assertSame([], $oldCurrentUserDrafts);
2142
2143
        $this->assertEquals(
2144
            [
2145
                VersionInfo::STATUS_DRAFT,
2146
                self::MEDIA_REMOTE_ID,
2147
            ],
2148
            [
2149
                $newCurrentUserDrafts[0]->status,
2150
                $newCurrentUserDrafts[0]->getContentInfo()->remoteId,
2151
            ]
2152
        );
2153
        $this->assertTrue($newCurrentUserDrafts[0]->isDraft());
2154
        $this->assertFalse($newCurrentUserDrafts[0]->isArchived());
2155
        $this->assertFalse($newCurrentUserDrafts[0]->isPublished());
2156
    }
2157
2158
    /**
2159
     * Test for the loadVersionInfo() method.
2160
     *
2161
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2162
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2163
     */
2164
    public function testLoadVersionInfoWithSecondParameter()
2165
    {
2166
        $publishedContent = $this->createContentVersion1();
2167
2168
        $this->contentService->createContentDraft($publishedContent->contentInfo);
2169
2170
        // Will return the VersionInfo of the $draftContent
2171
        $versionInfo = $this->contentService->loadVersionInfoById($publishedContent->id, 2);
2172
2173
        $this->assertEquals(2, $versionInfo->versionNo);
2174
2175
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2176
        $this->assertEquals(
2177
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2178
            $versionInfo->getContentInfo()->mainLocationId
2179
        );
2180
    }
2181
2182
    /**
2183
     * Test for the loadVersionInfo() method.
2184
     *
2185
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2186
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2187
     */
2188
    public function testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter()
2189
    {
2190
        $draft = $this->createContentDraftVersion1();
2191
2192
        $this->expectException(NotFoundException::class);
2193
2194
        // This call will fail with a "NotFoundException", because not versionNo 2 exists for this content object.
2195
        $this->contentService->loadVersionInfo($draft->contentInfo, 2);
2196
    }
2197
2198
    /**
2199
     * Test for the loadVersionInfoById() method.
2200
     *
2201
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2202
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2203
     */
2204
    public function testLoadVersionInfoByIdWithSecondParameter()
2205
    {
2206
        $publishedContent = $this->createContentVersion1();
2207
2208
        $draftContent = $this->contentService->createContentDraft($publishedContent->contentInfo);
2209
2210
        // Will return the VersionInfo of the $draftContent
2211
        $versionInfo = $this->contentService->loadVersionInfoById($publishedContent->id, 2);
2212
2213
        $this->assertEquals(2, $versionInfo->versionNo);
2214
2215
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2216
        $this->assertEquals(
2217
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2218
            $versionInfo->getContentInfo()->mainLocationId
2219
        );
2220
2221
        return [
2222
            'versionInfo' => $versionInfo,
2223
            'draftContent' => $draftContent,
2224
        ];
2225
    }
2226
2227
    /**
2228
     * Test for the returned value of the loadVersionInfoById() method.
2229
     *
2230
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter
2231
     * @covers \eZ\Publish\API\Repository\ContentService::loadVersionInfoById
2232
     *
2233
     * @param array $data
2234
     */
2235
    public function testLoadVersionInfoByIdWithSecondParameterSetsExpectedVersionInfo(array $data)
2236
    {
2237
        /** @var VersionInfo $versionInfo */
2238
        $versionInfo = $data['versionInfo'];
2239
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $draftContent */
2240
        $draftContent = $data['draftContent'];
2241
2242
        $this->assertPropertiesCorrect(
2243
            [
2244
                'names' => [
2245
                    self::ENG_US => 'An awesome forum',
2246
                ],
2247
                'contentInfo' => new ContentInfo([
2248
                    'id' => $draftContent->contentInfo->id,
2249
                    'contentTypeId' => 28,
2250
                    'name' => 'An awesome forum',
2251
                    'sectionId' => 1,
2252
                    'currentVersionNo' => 1,
2253
                    'published' => true,
2254
                    'ownerId' => 14,
2255
                    // this Content Object is created at the test runtime
2256
                    'modificationDate' => $versionInfo->contentInfo->modificationDate,
2257
                    'publishedDate' => $versionInfo->contentInfo->publishedDate,
2258
                    'alwaysAvailable' => 1,
2259
                    'remoteId' => 'abcdef0123456789abcdef0123456789',
2260
                    'mainLanguageCode' => self::ENG_US,
2261
                    'mainLocationId' => $draftContent->contentInfo->mainLocationId,
2262
                    'status' => ContentInfo::STATUS_PUBLISHED,
2263
                ]),
2264
                'id' => $draftContent->versionInfo->id,
2265
                'versionNo' => 2,
2266
                'creatorId' => 14,
2267
                'status' => 0,
2268
                'initialLanguageCode' => self::ENG_US,
2269
                'languageCodes' => [
2270
                    self::ENG_US,
2271
                ],
2272
            ],
2273
            $versionInfo
2274
        );
2275
    }
2276
2277
    /**
2278
     * Test for the loadVersionInfoById() method.
2279
     *
2280
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2281
     */
2282
    public function testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter()
2283
    {
2284
        $content = $this->createContentVersion1();
2285
2286
        $this->expectException(NotFoundException::class);
2287
2288
        // This call will fail with a "NotFoundException", because not versionNo 2 exists for this content object.
2289
        $this->contentService->loadVersionInfoById($content->id, 2);
2290
    }
2291
2292
    /**
2293
     * Test for the loadContentByVersionInfo() method.
2294
     *
2295
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
2296
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2297
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
2298
     */
2299
    public function testLoadContentByVersionInfoWithSecondParameter()
2300
    {
2301
        $sectionId = $this->generateId('section', 1);
2302
        $contentTypeService = $this->getRepository()->getContentTypeService();
2303
2304
        $contentType = $contentTypeService->loadContentTypeByIdentifier(self::FORUM_IDENTIFIER);
2305
2306
        $contentCreateStruct = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
2307
2308
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2309
2310
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', self::ENG_GB);
2311
2312
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2313
        // $sectionId contains the ID of section 1
2314
        $contentCreateStruct->sectionId = $sectionId;
2315
        $contentCreateStruct->alwaysAvailable = true;
2316
2317
        // Create a new content draft
2318
        $content = $this->contentService->createContent($contentCreateStruct);
2319
2320
        // Now publish this draft
2321
        $publishedContent = $this->contentService->publishVersion($content->getVersionInfo());
2322
2323
        // Will return a content instance with fields in "eng-US"
2324
        $reloadedContent = $this->contentService->loadContentByVersionInfo(
2325
            $publishedContent->getVersionInfo(),
2326
            [
2327
                self::ENG_GB,
2328
            ],
2329
            false
2330
        );
2331
2332
        $actual = [];
2333
        foreach ($reloadedContent->getFields() as $field) {
2334
            $actual[] = new Field(
2335
                [
2336
                    'id' => 0,
2337
                    'value' => $field->value !== null, // Actual value tested by FieldType integration tests
2338
                    'languageCode' => $field->languageCode,
2339
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
2340
                ]
2341
            );
2342
        }
2343
        usort(
2344
            $actual,
2345
            function ($field1, $field2) {
2346
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
2347
                    return strcasecmp($field1->languageCode, $field2->languageCode);
2348
                }
2349
2350
                return $return;
2351
            }
2352
        );
2353
2354
        $expected = [
2355
            new Field(
2356
                [
2357
                    'id' => 0,
2358
                    'value' => true,
2359
                    'languageCode' => self::ENG_GB,
2360
                    'fieldDefIdentifier' => 'description',
2361
                ]
2362
            ),
2363
            new Field(
2364
                [
2365
                    'id' => 0,
2366
                    'value' => true,
2367
                    'languageCode' => self::ENG_GB,
2368
                    'fieldDefIdentifier' => 'name',
2369
                ]
2370
            ),
2371
        ];
2372
2373
        $this->assertEquals($expected, $actual);
2374
    }
2375
2376
    /**
2377
     * Test for the loadContentByContentInfo() method.
2378
     *
2379
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages)
2380
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2381
     */
2382
    public function testLoadContentByContentInfoWithLanguageParameters()
2383
    {
2384
        $sectionId = $this->generateId('section', 1);
2385
        $contentTypeService = $this->getRepository()->getContentTypeService();
2386
2387
        $contentType = $contentTypeService->loadContentTypeByIdentifier(self::FORUM_IDENTIFIER);
2388
2389
        $contentCreateStruct = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
2390
2391
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2392
2393
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', self::ENG_GB);
2394
2395
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2396
        // $sectionId contains the ID of section 1
2397
        $contentCreateStruct->sectionId = $sectionId;
2398
        $contentCreateStruct->alwaysAvailable = true;
2399
2400
        // Create a new content draft
2401
        $content = $this->contentService->createContent($contentCreateStruct);
2402
2403
        // Now publish this draft
2404
        $publishedContent = $this->contentService->publishVersion($content->getVersionInfo());
2405
2406
        // Will return a content instance with fields in "eng-US"
2407
        $reloadedContent = $this->contentService->loadContentByContentInfo(
2408
            $publishedContent->contentInfo,
2409
            [
2410
                self::ENG_US,
2411
            ],
2412
            null,
2413
            false
2414
        );
2415
2416
        $actual = $this->normalizeFields($reloadedContent->getFields());
2417
2418
        $expected = [
2419
            new Field(
2420
                [
2421
                    'id' => 0,
2422
                    'value' => true,
2423
                    'languageCode' => self::ENG_US,
2424
                    'fieldDefIdentifier' => 'description',
2425
                    'fieldTypeIdentifier' => 'ezrichtext',
2426
                ]
2427
            ),
2428
            new Field(
2429
                [
2430
                    'id' => 0,
2431
                    'value' => true,
2432
                    'languageCode' => self::ENG_US,
2433
                    'fieldDefIdentifier' => 'name',
2434
                    'fieldTypeIdentifier' => 'ezstring',
2435
                ]
2436
            ),
2437
        ];
2438
2439
        $this->assertEquals($expected, $actual);
2440
2441
        // Will return a content instance with fields in "eng-GB" (versions prior to 6.0.0-beta9 returned "eng-US" also)
2442
        $reloadedContent = $this->contentService->loadContentByContentInfo(
2443
            $publishedContent->contentInfo,
2444
            [
2445
                self::ENG_GB,
2446
            ],
2447
            null,
2448
            true
2449
        );
2450
2451
        $actual = $this->normalizeFields($reloadedContent->getFields());
2452
2453
        $expected = [
2454
            new Field(
2455
                [
2456
                    'id' => 0,
2457
                    'value' => true,
2458
                    'languageCode' => self::ENG_GB,
2459
                    'fieldDefIdentifier' => 'description',
2460
                    'fieldTypeIdentifier' => 'ezrichtext',
2461
                ]
2462
            ),
2463
            new Field(
2464
                [
2465
                    'id' => 0,
2466
                    'value' => true,
2467
                    'languageCode' => self::ENG_GB,
2468
                    'fieldDefIdentifier' => 'name',
2469
                    'fieldTypeIdentifier' => 'ezstring',
2470
                ]
2471
            ),
2472
        ];
2473
2474
        $this->assertEquals($expected, $actual);
2475
2476
        // Will return a content instance with fields in main language "eng-US", as "fre-FR" does not exists
2477
        $reloadedContent = $this->contentService->loadContentByContentInfo(
2478
            $publishedContent->contentInfo,
2479
            [
2480
                'fre-FR',
2481
            ],
2482
            null,
2483
            true
2484
        );
2485
2486
        $actual = $this->normalizeFields($reloadedContent->getFields());
2487
2488
        $expected = [
2489
            new Field(
2490
                [
2491
                    'id' => 0,
2492
                    'value' => true,
2493
                    'languageCode' => self::ENG_US,
2494
                    'fieldDefIdentifier' => 'description',
2495
                    'fieldTypeIdentifier' => 'ezrichtext',
2496
                ]
2497
            ),
2498
            new Field(
2499
                [
2500
                    'id' => 0,
2501
                    'value' => true,
2502
                    'languageCode' => self::ENG_US,
2503
                    'fieldDefIdentifier' => 'name',
2504
                    'fieldTypeIdentifier' => 'ezstring',
2505
                ]
2506
            ),
2507
        ];
2508
2509
        $this->assertEquals($expected, $actual);
2510
    }
2511
2512
    /**
2513
     * Test for the loadContentByContentInfo() method.
2514
     *
2515
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2516
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2517
     */
2518
    public function testLoadContentByContentInfoWithVersionNumberParameter()
2519
    {
2520
        $publishedContent = $this->createContentVersion1();
2521
2522
        $this->contentService->createContentDraft($publishedContent->contentInfo);
2523
2524
        // This content instance is identical to $draftContent
2525
        $draftContentReloaded = $this->contentService->loadContentByContentInfo(
2526
            $publishedContent->contentInfo,
2527
            null,
2528
            2
2529
        );
2530
2531
        $this->assertEquals(
2532
            2,
2533
            $draftContentReloaded->getVersionInfo()->versionNo
2534
        );
2535
2536
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2537
        $this->assertEquals(
2538
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2539
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2540
        );
2541
    }
2542
2543
    /**
2544
     * Test for the loadContentByContentInfo() method.
2545
     *
2546
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2547
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithVersionNumberParameter
2548
     */
2549
    public function testLoadContentByContentInfoThrowsNotFoundExceptionWithVersionNumberParameter()
2550
    {
2551
        $content = $this->createContentVersion1();
2552
2553
        $this->expectException(NotFoundException::class);
2554
2555
        // This call will fail with a "NotFoundException", because no content with versionNo = 2 exists.
2556
        $this->contentService->loadContentByContentInfo($content->contentInfo, null, 2);
2557
    }
2558
2559
    /**
2560
     * Test for the loadContent() method.
2561
     *
2562
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages)
2563
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2564
     */
2565
    public function testLoadContentWithSecondParameter()
2566
    {
2567
        $draft = $this->createMultipleLanguageDraftVersion1();
2568
2569
        // This draft contains those fields localized with "eng-GB"
2570
        $draftLocalized = $this->contentService->loadContent($draft->id, [self::ENG_GB], null, false);
2571
2572
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), self::ENG_GB);
2573
2574
        return $draft;
2575
    }
2576
2577
    /**
2578
     * Test for the loadContent() method using undefined translation.
2579
     *
2580
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithSecondParameter
2581
     *
2582
     * @param \eZ\Publish\API\Repository\Values\Content\Content $contentDraft
2583
     */
2584
    public function testLoadContentWithSecondParameterThrowsNotFoundException(Content $contentDraft)
2585
    {
2586
        $this->expectException(NotFoundException::class);
2587
2588
        $this->contentService->loadContent($contentDraft->id, [self::GER_DE], null, false);
2589
    }
2590
2591
    /**
2592
     * Test for the loadContent() method.
2593
     *
2594
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2595
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2596
     */
2597
    public function testLoadContentWithThirdParameter()
2598
    {
2599
        $publishedContent = $this->createContentVersion1();
2600
2601
        $this->contentService->createContentDraft($publishedContent->contentInfo);
2602
2603
        // This content instance is identical to $draftContent
2604
        $draftContentReloaded = $this->contentService->loadContent($publishedContent->id, null, 2);
2605
2606
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2607
2608
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2609
        $this->assertEquals(
2610
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2611
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2612
        );
2613
    }
2614
2615
    /**
2616
     * Test for the loadContent() method.
2617
     *
2618
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2619
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithThirdParameter
2620
     */
2621
    public function testLoadContentThrowsNotFoundExceptionWithThirdParameter()
2622
    {
2623
        $content = $this->createContentVersion1();
2624
2625
        $this->expectException(NotFoundException::class);
2626
2627
        // This call will fail with a "NotFoundException", because for this content object no versionNo=2 exists.
2628
        $this->contentService->loadContent($content->id, null, 2);
2629
    }
2630
2631
    /**
2632
     * Test for the loadContentByRemoteId() method.
2633
     *
2634
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages)
2635
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2636
     */
2637
    public function testLoadContentByRemoteIdWithSecondParameter()
2638
    {
2639
        $draft = $this->createMultipleLanguageDraftVersion1();
2640
2641
        $this->contentService->publishVersion($draft->versionInfo);
2642
2643
        // This draft contains those fields localized with "eng-GB"
2644
        $draftLocalized = $this->contentService->loadContentByRemoteId(
2645
            $draft->contentInfo->remoteId,
2646
            [self::ENG_GB],
2647
            null,
2648
            false
2649
        );
2650
2651
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), self::ENG_GB);
2652
    }
2653
2654
    /**
2655
     * Test for the loadContentByRemoteId() method.
2656
     *
2657
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2658
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2659
     */
2660
    public function testLoadContentByRemoteIdWithThirdParameter()
2661
    {
2662
        $publishedContent = $this->createContentVersion1();
2663
2664
        $this->contentService->createContentDraft($publishedContent->contentInfo);
2665
2666
        // This content instance is identical to $draftContent
2667
        $draftContentReloaded = $this->contentService->loadContentByRemoteId(
2668
            $publishedContent->contentInfo->remoteId,
2669
            null,
2670
            2
2671
        );
2672
2673
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2674
2675
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2676
        $this->assertEquals(
2677
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2678
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2679
        );
2680
    }
2681
2682
    /**
2683
     * Test for the loadContentByRemoteId() method.
2684
     *
2685
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2686
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithThirdParameter
2687
     */
2688
    public function testLoadContentByRemoteIdThrowsNotFoundExceptionWithThirdParameter()
2689
    {
2690
        $content = $this->createContentVersion1();
2691
2692
        $this->expectException(NotFoundException::class);
2693
2694
        // This call will fail with a "NotFoundException", because for this content object no versionNo=2 exists.
2695
        $this->contentService->loadContentByRemoteId(
2696
            $content->contentInfo->remoteId,
2697
            null,
2698
            2
2699
        );
2700
    }
2701
2702
    /**
2703
     * Test that retrieval of translated name field respects prioritized language list.
2704
     *
2705
     * @dataProvider getPrioritizedLanguageList
2706
     * @param string[]|null $languageCodes
2707
     */
2708
    public function testLoadContentWithPrioritizedLanguagesList($languageCodes)
2709
    {
2710
        $content = $this->createContentVersion2();
2711
2712
        $content = $this->contentService->loadContent($content->id, $languageCodes);
2713
2714
        $expectedName = $content->getVersionInfo()->getName(
2715
            isset($languageCodes[0]) ? $languageCodes[0] : null
2716
        );
2717
        $nameValue = $content->getFieldValue('name');
2718
        /** @var \eZ\Publish\Core\FieldType\TextLine\Value $nameValue */
2719
        self::assertEquals($expectedName, $nameValue->text);
2720
        self::assertEquals($expectedName, $content->getVersionInfo()->getName());
2721
        // Also check value on shortcut method on content
2722
        self::assertEquals($expectedName, $content->getName());
2723
    }
2724
2725
    /**
2726
     * @return array
2727
     */
2728
    public function getPrioritizedLanguageList()
2729
    {
2730
        return [
2731
            [[self::ENG_US]],
2732
            [[self::ENG_GB]],
2733
            [[self::ENG_GB, self::ENG_US]],
2734
            [[self::ENG_US, self::ENG_GB]],
2735
        ];
2736
    }
2737
2738
    /**
2739
     * Test for the deleteVersion() method.
2740
     *
2741
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2742
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2743
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2744
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2745
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2746
     */
2747
    public function testDeleteVersion()
2748
    {
2749
        $content = $this->createContentVersion1();
2750
2751
        // Create new draft, because published or last version of the Content can't be deleted
2752
        $draft = $this->contentService->createContentDraft(
2753
            $content->getVersionInfo()->getContentInfo()
2754
        );
2755
2756
        // Delete the previously created draft
2757
        $this->contentService->deleteVersion($draft->getVersionInfo());
2758
2759
        $versions = $this->contentService->loadVersions($content->getVersionInfo()->getContentInfo());
2760
2761
        $this->assertCount(1, $versions);
2762
        $this->assertEquals(
2763
            $content->getVersionInfo()->id,
2764
            $versions[0]->id
2765
        );
2766
    }
2767
2768
    /**
2769
     * Test for the deleteVersion() method.
2770
     *
2771
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2772
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2773
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2774
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2775
     */
2776
    public function testDeleteVersionThrowsBadStateExceptionOnPublishedVersion()
2777
    {
2778
        $content = $this->createContentVersion1();
2779
2780
        $this->expectException(BadStateException::class);
2781
2782
        // This call will fail with a "BadStateException", because the content version is currently published.
2783
        $this->contentService->deleteVersion($content->getVersionInfo());
2784
    }
2785
2786
    /**
2787
     * Test for the deleteVersion() method.
2788
     *
2789
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2790
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2791
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2792
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2793
     */
2794
    public function testDeleteVersionWorksIfOnlyVersionIsDraft()
2795
    {
2796
        $draft = $this->createContentDraftVersion1();
2797
2798
        $this->contentService->deleteVersion($draft->getVersionInfo());
2799
2800
        $this->expectException(NotFoundException::class);
2801
2802
        // This call will fail with a "NotFound", because we allow to delete content if remaining version is draft.
2803
        // Can normally only happen if there where always only a draft to begin with, simplifies UI edit API usage.
2804
        $this->contentService->loadContent($draft->id);
2805
    }
2806
2807
    /**
2808
     * Test for the loadVersions() method.
2809
     *
2810
     * @see \eZ\Publish\API\Repository\ContentService::loadVersions()
2811
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2812
     *
2813
     * @return VersionInfo[]
2814
     */
2815
    public function testLoadVersions()
2816
    {
2817
        $contentVersion2 = $this->createContentVersion2();
2818
2819
        // Load versions of this ContentInfo instance
2820
        $versions = $this->contentService->loadVersions($contentVersion2->contentInfo);
2821
2822
        $expectedVersionsOrder = [
2823
            $this->contentService->loadVersionInfo($contentVersion2->contentInfo, 1),
2824
            $this->contentService->loadVersionInfo($contentVersion2->contentInfo, 2),
2825
        ];
2826
2827
        $this->assertEquals($expectedVersionsOrder, $versions);
2828
2829
        return $versions;
2830
    }
2831
2832
    /**
2833
     * Test for the loadVersions() method.
2834
     *
2835
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersions
2836
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersions
2837
     *
2838
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo[] $versions
2839
     */
2840
    public function testLoadVersionsSetsExpectedVersionInfo(array $versions)
2841
    {
2842
        $this->assertCount(2, $versions);
2843
2844
        $expectedVersions = [
2845
            [
2846
                'versionNo' => 1,
2847
                'creatorId' => 14,
2848
                'status' => VersionInfo::STATUS_ARCHIVED,
2849
                'initialLanguageCode' => self::ENG_US,
2850
                'languageCodes' => [self::ENG_US],
2851
            ],
2852
            [
2853
                'versionNo' => 2,
2854
                'creatorId' => 10,
2855
                'status' => VersionInfo::STATUS_PUBLISHED,
2856
                'initialLanguageCode' => self::ENG_US,
2857
                'languageCodes' => [self::ENG_US, self::ENG_GB],
2858
            ],
2859
        ];
2860
2861
        $this->assertPropertiesCorrect($expectedVersions[0], $versions[0]);
2862
        $this->assertPropertiesCorrect($expectedVersions[1], $versions[1]);
2863
        $this->assertEquals(
2864
            $versions[0]->creationDate->getTimestamp(),
2865
            $versions[1]->creationDate->getTimestamp(),
2866
            'Creation time did not match within delta of 2 seconds',
2867
            2
2868
        );
2869
        $this->assertEquals(
2870
            $versions[0]->modificationDate->getTimestamp(),
2871
            $versions[1]->modificationDate->getTimestamp(),
2872
            'Creation time did not match within delta of 2 seconds',
2873
            2
2874
        );
2875
        $this->assertTrue($versions[0]->isArchived());
2876
        $this->assertFalse($versions[0]->isDraft());
2877
        $this->assertFalse($versions[0]->isPublished());
2878
2879
        $this->assertTrue($versions[1]->isPublished());
2880
        $this->assertFalse($versions[1]->isDraft());
2881
        $this->assertFalse($versions[1]->isArchived());
2882
    }
2883
2884
    /**
2885
     * Test for the copyContent() method.
2886
     *
2887
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
2888
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2889
     * @group field-type
2890
     */
2891
    public function testCopyContent()
2892
    {
2893
        $parentLocationId = $this->generateId('location', 56);
2894
2895
        $contentVersion2 = $this->createMultipleLanguageContentVersion2();
2896
2897
        // Configure new target location
2898
        $targetLocationCreate = $this->locationService->newLocationCreateStruct($parentLocationId);
2899
2900
        $targetLocationCreate->priority = 42;
2901
        $targetLocationCreate->hidden = true;
2902
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
2903
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
2904
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
2905
2906
        // Copy content with all versions and drafts
2907
        $contentCopied = $this->contentService->copyContent(
2908
            $contentVersion2->contentInfo,
2909
            $targetLocationCreate
2910
        );
2911
2912
        $this->assertInstanceOf(
2913
            Content::class,
2914
            $contentCopied
2915
        );
2916
2917
        $this->assertNotEquals(
2918
            $contentVersion2->contentInfo->remoteId,
2919
            $contentCopied->contentInfo->remoteId
2920
        );
2921
2922
        $this->assertNotEquals(
2923
            $contentVersion2->id,
2924
            $contentCopied->id
2925
        );
2926
2927
        $this->assertEquals(
2928
            2,
2929
            count($this->contentService->loadVersions($contentCopied->contentInfo))
2930
        );
2931
2932
        $this->assertEquals(2, $contentCopied->getVersionInfo()->versionNo);
2933
2934
        $this->assertAllFieldsEquals($contentCopied->getFields());
2935
2936
        $this->assertDefaultContentStates($contentCopied->contentInfo);
2937
2938
        $this->assertNotNull(
2939
            $contentCopied->contentInfo->mainLocationId,
2940
            'Expected main location to be set given we provided a LocationCreateStruct'
2941
        );
2942
    }
2943
2944
    /**
2945
     * Test for the copyContent() method with ezsettings.default.content.retain_owner_on_copy set to false
2946
     * See settings/test/integration_legacy.yml for service override.
2947
     *
2948
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
2949
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2950
     * @group field-type
2951
     */
2952
    public function testCopyContentWithNewOwner()
2953
    {
2954
        $parentLocationId = $this->generateId('location', 56);
2955
2956
        $userService = $this->getRepository()->getUserService();
2957
2958
        $newOwner = $this->createUser('new_owner', 'foo', 'bar');
2959
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $contentVersion2 */
2960
        $contentVersion2 = $this->createContentDraftVersion1(
2961
            $parentLocationId,
2962
            self::FORUM_IDENTIFIER,
2963
            'name',
2964
            $newOwner
2965
        );
2966
2967
        // Configure new target location
2968
        $targetLocationCreate = $this->locationService->newLocationCreateStruct($parentLocationId);
2969
2970
        $targetLocationCreate->priority = 42;
2971
        $targetLocationCreate->hidden = true;
2972
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
2973
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
2974
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
2975
2976
        // Copy content with all versions and drafts
2977
        $contentCopied = $this->contentService->copyContent(
2978
            $contentVersion2->contentInfo,
2979
            $targetLocationCreate
2980
        );
2981
2982
        $this->assertEquals(
2983
            $newOwner->id,
2984
            $contentVersion2->contentInfo->ownerId
2985
        );
2986
        $this->assertEquals(
2987
            $userService->loadUserByLogin('admin')->getUserId(),
2988
            $contentCopied->contentInfo->ownerId
2989
        );
2990
    }
2991
2992
    /**
2993
     * Test for the copyContent() method.
2994
     *
2995
     * @see \eZ\Publish\API\Repository\ContentService::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo)
2996
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
2997
     */
2998
    public function testCopyContentWithGivenVersion()
2999
    {
3000
        $parentLocationId = $this->generateId('location', 56);
3001
3002
        $contentVersion2 = $this->createContentVersion2();
3003
3004
        // Configure new target location
3005
        $targetLocationCreate = $this->locationService->newLocationCreateStruct($parentLocationId);
3006
3007
        $targetLocationCreate->priority = 42;
3008
        $targetLocationCreate->hidden = true;
3009
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3010
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3011
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3012
3013
        // Copy only the initial version
3014
        $contentCopied = $this->contentService->copyContent(
3015
            $contentVersion2->contentInfo,
3016
            $targetLocationCreate,
3017
            $this->contentService->loadVersionInfo($contentVersion2->contentInfo, 1)
3018
        );
3019
3020
        $this->assertInstanceOf(
3021
            Content::class,
3022
            $contentCopied
3023
        );
3024
3025
        $this->assertNotEquals(
3026
            $contentVersion2->contentInfo->remoteId,
3027
            $contentCopied->contentInfo->remoteId
3028
        );
3029
3030
        $this->assertNotEquals(
3031
            $contentVersion2->id,
3032
            $contentCopied->id
3033
        );
3034
3035
        $this->assertEquals(
3036
            1,
3037
            count($this->contentService->loadVersions($contentCopied->contentInfo))
3038
        );
3039
3040
        $this->assertEquals(1, $contentCopied->getVersionInfo()->versionNo);
3041
3042
        $this->assertNotNull(
3043
            $contentCopied->contentInfo->mainLocationId,
3044
            'Expected main location to be set given we provided a LocationCreateStruct'
3045
        );
3046
    }
3047
3048
    /**
3049
     * Test for the addRelation() method.
3050
     *
3051
     * @return \eZ\Publish\API\Repository\Values\Content\Content
3052
     *
3053
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3054
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3055
     */
3056
    public function testAddRelation()
3057
    {
3058
        $draft = $this->createContentDraftVersion1();
3059
3060
        $media = $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID);
3061
3062
        // Create relation between new content object and "Media" page
3063
        $relation = $this->contentService->addRelation(
3064
            $draft->getVersionInfo(),
3065
            $media
3066
        );
3067
3068
        $this->assertInstanceOf(
3069
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Relation',
3070
            $relation
3071
        );
3072
3073
        return $this->contentService->loadRelations($draft->getVersionInfo());
3074
    }
3075
3076
    /**
3077
     * Test for the addRelation() method.
3078
     *
3079
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3080
     *
3081
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3082
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3083
     */
3084
    public function testAddRelationAddsRelationToContent($relations)
3085
    {
3086
        $this->assertEquals(
3087
            1,
3088
            count($relations)
3089
        );
3090
    }
3091
3092
    /**
3093
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3094
     */
3095
    protected function assertExpectedRelations($relations)
3096
    {
3097
        $this->assertEquals(
3098
            [
3099
                'type' => Relation::COMMON,
3100
                'sourceFieldDefinitionIdentifier' => null,
3101
                'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3102
                'destinationContentInfo' => self::MEDIA_REMOTE_ID,
3103
            ],
3104
            [
3105
                'type' => $relations[0]->type,
3106
                'sourceFieldDefinitionIdentifier' => $relations[0]->sourceFieldDefinitionIdentifier,
3107
                'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3108
                'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3109
            ]
3110
        );
3111
    }
3112
3113
    /**
3114
     * Test for the addRelation() method.
3115
     *
3116
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3117
     *
3118
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3119
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3120
     */
3121
    public function testAddRelationSetsExpectedRelations($relations)
3122
    {
3123
        $this->assertExpectedRelations($relations);
3124
    }
3125
3126
    /**
3127
     * Test for the createContentDraft() method.
3128
     *
3129
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3130
     *
3131
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
3132
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelationSetsExpectedRelations
3133
     */
3134
    public function testCreateContentDraftWithRelations()
3135
    {
3136
        $draft = $this->createContentDraftVersion1();
3137
        $media = $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID);
3138
3139
        // Create relation between new content object and "Media" page
3140
        $this->contentService->addRelation(
3141
            $draft->getVersionInfo(),
3142
            $media
3143
        );
3144
3145
        $content = $this->contentService->publishVersion($draft->versionInfo);
3146
        $newDraft = $this->contentService->createContentDraft($content->contentInfo);
3147
3148
        return $this->contentService->loadRelations($newDraft->getVersionInfo());
3149
    }
3150
3151
    /**
3152
     * Test for the createContentDraft() method.
3153
     *
3154
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3155
     *
3156
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3157
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelations
3158
     */
3159
    public function testCreateContentDraftWithRelationsCreatesRelations($relations)
3160
    {
3161
        $this->assertEquals(
3162
            1,
3163
            count($relations)
3164
        );
3165
3166
        return $relations;
3167
    }
3168
3169
    /**
3170
     * Test for the createContentDraft() method.
3171
     *
3172
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3173
     *
3174
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelationsCreatesRelations
3175
     */
3176
    public function testCreateContentDraftWithRelationsCreatesExpectedRelations($relations)
3177
    {
3178
        $this->assertExpectedRelations($relations);
3179
    }
3180
3181
    /**
3182
     * Test for the addRelation() method.
3183
     *
3184
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3185
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3186
     */
3187
    public function testAddRelationThrowsBadStateException()
3188
    {
3189
        $content = $this->createContentVersion1();
3190
3191
        $media = $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID);
3192
3193
        $this->expectException(BadStateException::class);
3194
3195
        // This call will fail with a "BadStateException", because content is published and not a draft.
3196
        $this->contentService->addRelation(
3197
            $content->getVersionInfo(),
3198
            $media
3199
        );
3200
    }
3201
3202
    /**
3203
     * Test for the loadRelations() method.
3204
     *
3205
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3206
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3207
     */
3208
    public function testLoadRelations()
3209
    {
3210
        $draft = $this->createContentDraftVersion1();
3211
3212
        $media = $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID);
3213
        $demoDesign = $this->contentService->loadContentInfoByRemoteId(self::DEMO_DESIGN_REMOTE_ID);
3214
3215
        // Create relation between new content object and "Media" page
3216
        $this->contentService->addRelation(
3217
            $draft->getVersionInfo(),
3218
            $media
3219
        );
3220
3221
        // Create another relation with the "Demo Design" page
3222
        $this->contentService->addRelation(
3223
            $draft->getVersionInfo(),
3224
            $demoDesign
3225
        );
3226
3227
        $relations = $this->contentService->loadRelations($draft->getVersionInfo());
3228
3229
        usort(
3230
            $relations,
3231
            function ($rel1, $rel2) {
3232
                return strcasecmp(
3233
                    $rel2->getDestinationContentInfo()->remoteId,
3234
                    $rel1->getDestinationContentInfo()->remoteId
3235
                );
3236
            }
3237
        );
3238
3239
        $this->assertEquals(
3240
            [
3241
                [
3242
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3243
                    'destinationContentInfo' => self::MEDIA_REMOTE_ID,
3244
                ],
3245
                [
3246
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3247
                    'destinationContentInfo' => self::DEMO_DESIGN_REMOTE_ID,
3248
                ],
3249
            ],
3250
            [
3251
                [
3252
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3253
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3254
                ],
3255
                [
3256
                    'sourceContentInfo' => $relations[1]->sourceContentInfo->remoteId,
3257
                    'destinationContentInfo' => $relations[1]->destinationContentInfo->remoteId,
3258
                ],
3259
            ]
3260
        );
3261
    }
3262
3263
    /**
3264
     * Test for the loadRelations() method.
3265
     *
3266
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3267
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3268
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3269
     */
3270
    public function testLoadRelationsSkipsArchivedContent()
3271
    {
3272
        $trashService = $this->getRepository()->getTrashService();
3273
3274
        $draft = $this->createContentDraftVersion1();
3275
3276
        // Load other content objects
3277
        $media = $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID);
3278
        $demoDesign = $this->contentService->loadContentInfoByRemoteId(self::DEMO_DESIGN_REMOTE_ID);
3279
3280
        // Create relation between new content object and "Media" page
3281
        $this->contentService->addRelation(
3282
            $draft->getVersionInfo(),
3283
            $media
3284
        );
3285
3286
        // Create another relation with the "Demo Design" page
3287
        $this->contentService->addRelation(
3288
            $draft->getVersionInfo(),
3289
            $demoDesign
3290
        );
3291
3292
        $demoDesignLocation = $this->locationService->loadLocation($demoDesign->mainLocationId);
3293
3294
        // Trashing Content's last Location will change its status to archived,
3295
        // in this case relation towards it will not be loaded.
3296
        $trashService->trash($demoDesignLocation);
3297
3298
        // Load all relations
3299
        $relations = $this->contentService->loadRelations($draft->getVersionInfo());
3300
3301
        $this->assertCount(1, $relations);
3302
        $this->assertEquals(
3303
            [
3304
                [
3305
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3306
                    'destinationContentInfo' => self::MEDIA_REMOTE_ID,
3307
                ],
3308
            ],
3309
            [
3310
                [
3311
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3312
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3313
                ],
3314
            ]
3315
        );
3316
    }
3317
3318
    /**
3319
     * Test for the loadRelations() method.
3320
     *
3321
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3322
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3323
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3324
     */
3325
    public function testLoadRelationsSkipsDraftContent()
3326
    {
3327
        $draft = $this->createContentDraftVersion1();
3328
3329
        // Load other content objects
3330
        $media = $this->contentService->loadContentByRemoteId(self::MEDIA_REMOTE_ID);
3331
        $demoDesign = $this->contentService->loadContentInfoByRemoteId(self::DEMO_DESIGN_REMOTE_ID);
3332
3333
        // Create draft of "Media" page
3334
        $mediaDraft = $this->contentService->createContentDraft($media->contentInfo);
3335
3336
        // Create relation between "Media" page and new content object draft.
3337
        // This relation will not be loaded before the draft is published.
3338
        $this->contentService->addRelation(
3339
            $mediaDraft->getVersionInfo(),
3340
            $draft->getVersionInfo()->getContentInfo()
3341
        );
3342
3343
        // Create another relation with the "Demo Design" page
3344
        $this->contentService->addRelation(
3345
            $mediaDraft->getVersionInfo(),
3346
            $demoDesign
3347
        );
3348
3349
        // Load all relations
3350
        $relations = $this->contentService->loadRelations($mediaDraft->getVersionInfo());
3351
3352
        $this->assertCount(1, $relations);
3353
        $this->assertEquals(
3354
            [
3355
                [
3356
                    'sourceContentInfo' => self::MEDIA_REMOTE_ID,
3357
                    'destinationContentInfo' => self::DEMO_DESIGN_REMOTE_ID,
3358
                ],
3359
            ],
3360
            [
3361
                [
3362
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3363
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3364
                ],
3365
            ]
3366
        );
3367
    }
3368
3369
    /**
3370
     * Test for the loadReverseRelations() method.
3371
     *
3372
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3373
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3374
     */
3375
    public function testLoadReverseRelations()
3376
    {
3377
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3378
        $contentInfo = $versionInfo->getContentInfo();
3379
3380
        // Create some drafts
3381
        $mediaDraft = $this->contentService->createContentDraft(
3382
            $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID)
3383
        );
3384
        $demoDesignDraft = $this->contentService->createContentDraft(
3385
            $this->contentService->loadContentInfoByRemoteId(self::DEMO_DESIGN_REMOTE_ID)
3386
        );
3387
3388
        // Create relation between new content object and "Media" page
3389
        $relation1 = $this->contentService->addRelation(
3390
            $mediaDraft->getVersionInfo(),
3391
            $contentInfo
3392
        );
3393
3394
        // Create another relation with the "Demo Design" page
3395
        $relation2 = $this->contentService->addRelation(
3396
            $demoDesignDraft->getVersionInfo(),
3397
            $contentInfo
3398
        );
3399
3400
        // Publish drafts, so relations become active
3401
        $this->contentService->publishVersion($mediaDraft->getVersionInfo());
3402
        $this->contentService->publishVersion($demoDesignDraft->getVersionInfo());
3403
3404
        // Load all relations
3405
        $relations = $this->contentService->loadRelations($versionInfo);
3406
        $reverseRelations = $this->contentService->loadReverseRelations($contentInfo);
3407
3408
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3409
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3410
3411
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3412
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3413
3414
        $this->assertEquals(0, count($relations));
3415
        $this->assertEquals(2, count($reverseRelations));
3416
3417
        usort(
3418
            $reverseRelations,
3419
            function ($rel1, $rel2) {
3420
                return strcasecmp(
3421
                    $rel2->getSourceContentInfo()->remoteId,
3422
                    $rel1->getSourceContentInfo()->remoteId
3423
                );
3424
            }
3425
        );
3426
3427
        $this->assertEquals(
3428
            [
3429
                [
3430
                    'sourceContentInfo' => self::MEDIA_REMOTE_ID,
3431
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3432
                ],
3433
                [
3434
                    'sourceContentInfo' => self::DEMO_DESIGN_REMOTE_ID,
3435
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3436
                ],
3437
            ],
3438
            [
3439
                [
3440
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3441
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3442
                ],
3443
                [
3444
                    'sourceContentInfo' => $reverseRelations[1]->sourceContentInfo->remoteId,
3445
                    'destinationContentInfo' => $reverseRelations[1]->destinationContentInfo->remoteId,
3446
                ],
3447
            ]
3448
        );
3449
    }
3450
3451
    /**
3452
     * Test for the loadReverseRelations() method.
3453
     *
3454
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3455
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3456
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3457
     */
3458
    public function testLoadReverseRelationsSkipsArchivedContent()
3459
    {
3460
        $trashService = $this->getRepository()->getTrashService();
3461
3462
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3463
        $contentInfo = $versionInfo->getContentInfo();
3464
3465
        // Create some drafts
3466
        $mediaDraft = $this->contentService->createContentDraft(
3467
            $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID)
3468
        );
3469
        $demoDesignDraft = $this->contentService->createContentDraft(
3470
            $this->contentService->loadContentInfoByRemoteId(self::DEMO_DESIGN_REMOTE_ID)
3471
        );
3472
3473
        // Create relation between new content object and "Media" page
3474
        $relation1 = $this->contentService->addRelation(
3475
            $mediaDraft->getVersionInfo(),
3476
            $contentInfo
3477
        );
3478
3479
        // Create another relation with the "Demo Design" page
3480
        $relation2 = $this->contentService->addRelation(
3481
            $demoDesignDraft->getVersionInfo(),
3482
            $contentInfo
3483
        );
3484
3485
        // Publish drafts, so relations become active
3486
        $this->contentService->publishVersion($mediaDraft->getVersionInfo());
3487
        $this->contentService->publishVersion($demoDesignDraft->getVersionInfo());
3488
3489
        $demoDesignLocation = $this->locationService->loadLocation($demoDesignDraft->contentInfo->mainLocationId);
3490
3491
        // Trashing Content's last Location will change its status to archived,
3492
        // in this case relation from it will not be loaded.
3493
        $trashService->trash($demoDesignLocation);
3494
3495
        // Load all relations
3496
        $relations = $this->contentService->loadRelations($versionInfo);
3497
        $reverseRelations = $this->contentService->loadReverseRelations($contentInfo);
3498
3499
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3500
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3501
3502
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3503
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3504
3505
        $this->assertEquals(0, count($relations));
3506
        $this->assertEquals(1, count($reverseRelations));
3507
3508
        $this->assertEquals(
3509
            [
3510
                [
3511
                    'sourceContentInfo' => self::MEDIA_REMOTE_ID,
3512
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3513
                ],
3514
            ],
3515
            [
3516
                [
3517
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3518
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3519
                ],
3520
            ]
3521
        );
3522
    }
3523
3524
    /**
3525
     * Test for the loadReverseRelations() method.
3526
     *
3527
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3528
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3529
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3530
     */
3531
    public function testLoadReverseRelationsSkipsDraftContent()
3532
    {
3533
        // Load "Media" page Content
3534
        $media = $this->contentService->loadContentByRemoteId(self::MEDIA_REMOTE_ID);
3535
3536
        // Create some drafts
3537
        $newDraftVersionInfo = $this->createContentDraftVersion1()->getVersionInfo();
3538
        $demoDesignDraft = $this->contentService->createContentDraft(
3539
            $this->contentService->loadContentInfoByRemoteId(self::DEMO_DESIGN_REMOTE_ID)
3540
        );
3541
3542
        // Create relation between "Media" page and new content object
3543
        $relation1 = $this->contentService->addRelation(
3544
            $newDraftVersionInfo,
3545
            $media->contentInfo
3546
        );
3547
3548
        // Create another relation with the "Demo Design" page
3549
        $relation2 = $this->contentService->addRelation(
3550
            $demoDesignDraft->getVersionInfo(),
3551
            $media->contentInfo
3552
        );
3553
3554
        // Publish drafts, so relations become active
3555
        $this->contentService->publishVersion($demoDesignDraft->getVersionInfo());
3556
        // We will not publish new Content draft, therefore relation from it
3557
        // will not be loaded as reverse relation for "Media" page
3558
3559
        // Load all relations
3560
        $relations = $this->contentService->loadRelations($media->versionInfo);
3561
        $reverseRelations = $this->contentService->loadReverseRelations($media->contentInfo);
3562
3563
        $this->assertEquals($media->contentInfo->id, $relation1->getDestinationContentInfo()->id);
3564
        $this->assertEquals($newDraftVersionInfo->contentInfo->id, $relation1->getSourceContentInfo()->id);
3565
3566
        $this->assertEquals($media->contentInfo->id, $relation2->getDestinationContentInfo()->id);
3567
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3568
3569
        $this->assertEquals(0, count($relations));
3570
        $this->assertEquals(1, count($reverseRelations));
3571
3572
        $this->assertEquals(
3573
            [
3574
                [
3575
                    'sourceContentInfo' => self::DEMO_DESIGN_REMOTE_ID,
3576
                    'destinationContentInfo' => self::MEDIA_REMOTE_ID,
3577
                ],
3578
            ],
3579
            [
3580
                [
3581
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3582
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3583
                ],
3584
            ]
3585
        );
3586
    }
3587
3588
    /**
3589
     * Test for the deleteRelation() method.
3590
     *
3591
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3592
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3593
     */
3594
    public function testDeleteRelation()
3595
    {
3596
        $draft = $this->createContentDraftVersion1();
3597
3598
        $media = $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID);
3599
        $demoDesign = $this->contentService->loadContentInfoByRemoteId(self::DEMO_DESIGN_REMOTE_ID);
3600
3601
        // Establish some relations
3602
        $this->contentService->addRelation($draft->getVersionInfo(), $media);
3603
        $this->contentService->addRelation($draft->getVersionInfo(), $demoDesign);
3604
3605
        // Delete one of the currently created relations
3606
        $this->contentService->deleteRelation($draft->getVersionInfo(), $media);
3607
3608
        // The relations array now contains only one element
3609
        $relations = $this->contentService->loadRelations($draft->getVersionInfo());
3610
3611
        $this->assertEquals(1, count($relations));
3612
    }
3613
3614
    /**
3615
     * Test for the deleteRelation() method.
3616
     *
3617
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3618
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3619
     */
3620
    public function testDeleteRelationThrowsBadStateException()
3621
    {
3622
        $content = $this->createContentVersion1();
3623
3624
        // Load the destination object
3625
        $media = $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID);
3626
3627
        // Create a new draft
3628
        $draftVersion2 = $this->contentService->createContentDraft($content->contentInfo);
3629
3630
        // Add a relation
3631
        $this->contentService->addRelation($draftVersion2->getVersionInfo(), $media);
3632
3633
        // Publish new version
3634
        $contentVersion2 = $this->contentService->publishVersion(
3635
            $draftVersion2->getVersionInfo()
3636
        );
3637
3638
        $this->expectException(BadStateException::class);
3639
3640
        // This call will fail with a "BadStateException", because content is published and not a draft.
3641
        $this->contentService->deleteRelation(
3642
            $contentVersion2->getVersionInfo(),
3643
            $media
3644
        );
3645
    }
3646
3647
    /**
3648
     * Test for the deleteRelation() method.
3649
     *
3650
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3651
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3652
     */
3653
    public function testDeleteRelationThrowsInvalidArgumentException()
3654
    {
3655
        $draft = $this->createContentDraftVersion1();
3656
3657
        // Load the destination object
3658
        $media = $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID);
3659
3660
        // This call will fail with a "InvalidArgumentException", because no relation exists between $draft and $media.
3661
        $this->expectException(APIInvalidArgumentException::class);
3662
        $this->contentService->deleteRelation(
3663
            $draft->getVersionInfo(),
3664
            $media
3665
        );
3666
    }
3667
3668
    /**
3669
     * Test for the createContent() method.
3670
     *
3671
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
3672
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3673
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3674
     */
3675
    public function testCreateContentInTransactionWithRollback()
3676
    {
3677
        if ($this->isVersion4()) {
3678
            $this->markTestSkipped('This test requires eZ Publish 5');
3679
        }
3680
3681
        $repository = $this->getRepository();
3682
3683
        $contentTypeService = $this->getRepository()->getContentTypeService();
3684
3685
        // Start a transaction
3686
        $repository->beginTransaction();
3687
3688
        try {
3689
            $contentType = $contentTypeService->loadContentTypeByIdentifier(self::FORUM_IDENTIFIER);
3690
3691
            // Get a content create struct and set mandatory properties
3692
            $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
3693
            $contentCreate->setField('name', 'Sindelfingen forum');
3694
3695
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
3696
            $contentCreate->alwaysAvailable = true;
3697
3698
            // Create a new content object
3699
            $contentId = $this->contentService->createContent($contentCreate)->id;
3700
        } catch (Exception $e) {
3701
            // Cleanup hanging transaction on error
3702
            $repository->rollback();
3703
            throw $e;
3704
        }
3705
3706
        // Rollback all changes
3707
        $repository->rollback();
3708
3709
        try {
3710
            // This call will fail with a "NotFoundException"
3711
            $this->contentService->loadContent($contentId);
3712
        } catch (NotFoundException $e) {
3713
            // This is expected
3714
            return;
3715
        }
3716
3717
        $this->fail('Content object still exists after rollback.');
3718
    }
3719
3720
    /**
3721
     * Test for the createContent() method.
3722
     *
3723
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
3724
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3725
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3726
     */
3727
    public function testCreateContentInTransactionWithCommit()
3728
    {
3729
        if ($this->isVersion4()) {
3730
            $this->markTestSkipped('This test requires eZ Publish 5');
3731
        }
3732
3733
        $repository = $this->getRepository();
3734
3735
        $contentTypeService = $repository->getContentTypeService();
3736
3737
        // Start a transaction
3738
        $repository->beginTransaction();
3739
3740
        try {
3741
            $contentType = $contentTypeService->loadContentTypeByIdentifier(self::FORUM_IDENTIFIER);
3742
3743
            // Get a content create struct and set mandatory properties
3744
            $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
3745
            $contentCreate->setField('name', 'Sindelfingen forum');
3746
3747
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
3748
            $contentCreate->alwaysAvailable = true;
3749
3750
            // Create a new content object
3751
            $contentId = $this->contentService->createContent($contentCreate)->id;
3752
3753
            // Commit changes
3754
            $repository->commit();
3755
        } catch (Exception $e) {
3756
            // Cleanup hanging transaction on error
3757
            $repository->rollback();
3758
            throw $e;
3759
        }
3760
3761
        // Load the new content object
3762
        $content = $this->contentService->loadContent($contentId);
3763
3764
        $this->assertEquals($contentId, $content->id);
3765
    }
3766
3767
    /**
3768
     * Test for the createContent() method.
3769
     *
3770
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
3771
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
3772
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
3773
     */
3774
    public function testCreateContentWithLocationCreateParameterInTransactionWithRollback()
3775
    {
3776
        $repository = $this->getRepository();
3777
3778
        // Start a transaction
3779
        $repository->beginTransaction();
3780
3781
        try {
3782
            $draft = $this->createContentDraftVersion1();
3783
        } catch (Exception $e) {
3784
            // Cleanup hanging transaction on error
3785
            $repository->rollback();
3786
            throw $e;
3787
        }
3788
3789
        $contentId = $draft->id;
3790
3791
        // Roleback the transaction
3792
        $repository->rollback();
3793
3794
        try {
3795
            // This call will fail with a "NotFoundException"
3796
            $this->contentService->loadContent($contentId);
3797
        } catch (NotFoundException $e) {
3798
            return;
3799
        }
3800
3801
        $this->fail('Can still load content object after rollback.');
3802
    }
3803
3804
    /**
3805
     * Test for the createContent() method.
3806
     *
3807
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
3808
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
3809
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
3810
     */
3811
    public function testCreateContentWithLocationCreateParameterInTransactionWithCommit()
3812
    {
3813
        $repository = $this->getRepository();
3814
3815
        // Start a transaction
3816
        $repository->beginTransaction();
3817
3818
        try {
3819
            $draft = $this->createContentDraftVersion1();
3820
3821
            $contentId = $draft->id;
3822
3823
            // Roleback the transaction
3824
            $repository->commit();
3825
        } catch (Exception $e) {
3826
            // Cleanup hanging transaction on error
3827
            $repository->rollback();
3828
            throw $e;
3829
        }
3830
3831
        // Load the new content object
3832
        $content = $this->contentService->loadContent($contentId);
3833
3834
        $this->assertEquals($contentId, $content->id);
3835
    }
3836
3837
    /**
3838
     * Test for the createContentDraft() method.
3839
     *
3840
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
3841
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
3842
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3843
     */
3844
    public function testCreateContentDraftInTransactionWithRollback()
3845
    {
3846
        $repository = $this->getRepository();
3847
3848
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
3849
3850
        // Load the user group content object
3851
        $content = $this->contentService->loadContent($contentId);
3852
3853
        // Start a new transaction
3854
        $repository->beginTransaction();
3855
3856
        try {
3857
            // Create a new draft
3858
            $drafted = $this->contentService->createContentDraft($content->contentInfo);
3859
3860
            // Store version number for later reuse
3861
            $versionNo = $drafted->versionInfo->versionNo;
3862
        } catch (Exception $e) {
3863
            // Cleanup hanging transaction on error
3864
            $repository->rollback();
3865
            throw $e;
3866
        }
3867
3868
        // Rollback
3869
        $repository->rollback();
3870
3871
        try {
3872
            // This call will fail with a "NotFoundException"
3873
            $this->contentService->loadContent($contentId, null, $versionNo);
3874
        } catch (NotFoundException $e) {
3875
            return;
3876
        }
3877
3878
        $this->fail('Can still load content draft after rollback');
3879
    }
3880
3881
    /**
3882
     * Test for the createContentDraft() method.
3883
     *
3884
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
3885
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
3886
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3887
     */
3888
    public function testCreateContentDraftInTransactionWithCommit()
3889
    {
3890
        $repository = $this->getRepository();
3891
3892
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
3893
3894
        // Load the user group content object
3895
        $content = $this->contentService->loadContent($contentId);
3896
3897
        // Start a new transaction
3898
        $repository->beginTransaction();
3899
3900
        try {
3901
            // Create a new draft
3902
            $drafted = $this->contentService->createContentDraft($content->contentInfo);
3903
3904
            // Store version number for later reuse
3905
            $versionNo = $drafted->versionInfo->versionNo;
3906
3907
            // Commit all changes
3908
            $repository->commit();
3909
        } catch (Exception $e) {
3910
            // Cleanup hanging transaction on error
3911
            $repository->rollback();
3912
            throw $e;
3913
        }
3914
3915
        $content = $this->contentService->loadContent($contentId, null, $versionNo);
3916
3917
        $this->assertEquals(
3918
            $versionNo,
3919
            $content->getVersionInfo()->versionNo
3920
        );
3921
    }
3922
3923
    /**
3924
     * Test for the publishVersion() method.
3925
     *
3926
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
3927
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3928
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3929
     */
3930
    public function testPublishVersionInTransactionWithRollback()
3931
    {
3932
        $repository = $this->getRepository();
3933
3934
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
3935
3936
        // Load the user group content object
3937
        $content = $this->contentService->loadContent($contentId);
3938
3939
        // Start a new transaction
3940
        $repository->beginTransaction();
3941
3942
        try {
3943
            $draftVersion = $this->contentService->createContentDraft($content->contentInfo)->getVersionInfo();
3944
3945
            // Publish a new version
3946
            $content = $this->contentService->publishVersion($draftVersion);
3947
3948
            // Store version number for later reuse
3949
            $versionNo = $content->versionInfo->versionNo;
3950
        } catch (Exception $e) {
3951
            // Cleanup hanging transaction on error
3952
            $repository->rollback();
3953
            throw $e;
3954
        }
3955
3956
        // Rollback
3957
        $repository->rollback();
3958
3959
        try {
3960
            // This call will fail with a "NotFoundException"
3961
            $this->contentService->loadContent($contentId, null, $versionNo);
3962
        } catch (NotFoundException $e) {
3963
            return;
3964
        }
3965
3966
        $this->fail('Can still load content draft after rollback');
3967
    }
3968
3969
    /**
3970
     * Test for the publishVersion() method.
3971
     *
3972
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
3973
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3974
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
3975
     */
3976
    public function testPublishVersionInTransactionWithCommit()
3977
    {
3978
        $repository = $this->getRepository();
3979
3980
        // Load the user group content object
3981
        $template = $this->contentService->loadContent(self::ADMINISTRATORS_USER_GROUP_ID);
3982
3983
        // Start a new transaction
3984
        $repository->beginTransaction();
3985
3986
        try {
3987
            // Publish a new version
3988
            $content = $this->contentService->publishVersion(
3989
                $this->contentService->createContentDraft($template->contentInfo)->getVersionInfo()
3990
            );
3991
3992
            // Store version number for later reuse
3993
            $versionNo = $content->versionInfo->versionNo;
3994
3995
            // Commit all changes
3996
            $repository->commit();
3997
        } catch (Exception $e) {
3998
            // Cleanup hanging transaction on error
3999
            $repository->rollback();
4000
            throw $e;
4001
        }
4002
4003
        // Load current version info
4004
        $versionInfo = $this->contentService->loadVersionInfo($content->contentInfo);
4005
4006
        $this->assertEquals($versionNo, $versionInfo->versionNo);
4007
    }
4008
4009
    /**
4010
     * Test for the updateContent() method.
4011
     *
4012
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4013
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4014
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4015
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4016
     */
4017
    public function testUpdateContentInTransactionWithRollback()
4018
    {
4019
        $repository = $this->getRepository();
4020
4021
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
4022
4023
        // Create a new user group draft
4024
        $draft = $this->contentService->createContentDraft(
4025
            $this->contentService->loadContentInfo($contentId)
4026
        );
4027
4028
        // Get an update struct and change the group name
4029
        $contentUpdate = $this->contentService->newContentUpdateStruct();
4030
        $contentUpdate->setField('name', self::ADMINISTRATORS_USER_GROUP_NAME, self::ENG_US);
4031
4032
        // Start a transaction
4033
        $repository->beginTransaction();
4034
4035
        try {
4036
            // Update the group name
4037
            $draft = $this->contentService->updateContent(
4038
                $draft->getVersionInfo(),
4039
                $contentUpdate
4040
            );
4041
4042
            // Publish updated version
4043
            $this->contentService->publishVersion($draft->getVersionInfo());
4044
        } catch (Exception $e) {
4045
            // Cleanup hanging transaction on error
4046
            $repository->rollback();
4047
            throw $e;
4048
        }
4049
4050
        // Rollback all changes.
4051
        $repository->rollback();
4052
4053
        // Name will still be "Administrator users"
4054
        $name = $this->contentService->loadContent($contentId)->getFieldValue('name');
4055
4056
        $this->assertEquals('Administrator users', $name);
4057
    }
4058
4059
    /**
4060
     * Test for the updateContent() method.
4061
     *
4062
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4063
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4064
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4065
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4066
     */
4067
    public function testUpdateContentInTransactionWithCommit()
4068
    {
4069
        $repository = $this->getRepository();
4070
4071
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
4072
4073
        // Create a new user group draft
4074
        $draft = $this->contentService->createContentDraft(
4075
            $this->contentService->loadContentInfo($contentId)
4076
        );
4077
4078
        // Get an update struct and change the group name
4079
        $contentUpdate = $this->contentService->newContentUpdateStruct();
4080
        $contentUpdate->setField('name', self::ADMINISTRATORS_USER_GROUP_NAME, self::ENG_US);
4081
4082
        // Start a transaction
4083
        $repository->beginTransaction();
4084
4085
        try {
4086
            // Update the group name
4087
            $draft = $this->contentService->updateContent(
4088
                $draft->getVersionInfo(),
4089
                $contentUpdate
4090
            );
4091
4092
            // Publish updated version
4093
            $this->contentService->publishVersion($draft->getVersionInfo());
4094
4095
            // Commit all changes.
4096
            $repository->commit();
4097
        } catch (Exception $e) {
4098
            // Cleanup hanging transaction on error
4099
            $repository->rollback();
4100
            throw $e;
4101
        }
4102
4103
        // Name is now "Administrators"
4104
        $name = $this->contentService->loadContent($contentId)->getFieldValue('name', self::ENG_US);
4105
4106
        $this->assertEquals(self::ADMINISTRATORS_USER_GROUP_NAME, $name);
4107
    }
4108
4109
    /**
4110
     * Test for the updateContentMetadata() method.
4111
     *
4112
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4113
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4114
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4115
     */
4116
    public function testUpdateContentMetadataInTransactionWithRollback()
4117
    {
4118
        $repository = $this->getRepository();
4119
4120
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
4121
4122
        // Load a ContentInfo object
4123
        $contentInfo = $this->contentService->loadContentInfo($contentId);
4124
4125
        // Store remoteId for later testing
4126
        $remoteId = $contentInfo->remoteId;
4127
4128
        // Start a transaction
4129
        $repository->beginTransaction();
4130
4131
        try {
4132
            // Get metadata update struct and change remoteId
4133
            $metadataUpdate = $this->contentService->newContentMetadataUpdateStruct();
4134
            $metadataUpdate->remoteId = md5(microtime(true));
4135
4136
            // Update the metadata of the published content object
4137
            $this->contentService->updateContentMetadata(
4138
                $contentInfo,
4139
                $metadataUpdate
4140
            );
4141
        } catch (Exception $e) {
4142
            // Cleanup hanging transaction on error
4143
            $repository->rollback();
4144
            throw $e;
4145
        }
4146
4147
        // Rollback all changes.
4148
        $repository->rollback();
4149
4150
        // Load current remoteId
4151
        $remoteIdReloaded = $this->contentService->loadContentInfo($contentId)->remoteId;
4152
4153
        $this->assertEquals($remoteId, $remoteIdReloaded);
4154
    }
4155
4156
    /**
4157
     * Test for the updateContentMetadata() method.
4158
     *
4159
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4160
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4161
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4162
     */
4163
    public function testUpdateContentMetadataInTransactionWithCommit()
4164
    {
4165
        $repository = $this->getRepository();
4166
4167
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
4168
4169
        // Load a ContentInfo object
4170
        $contentInfo = $this->contentService->loadContentInfo($contentId);
4171
4172
        // Store remoteId for later testing
4173
        $remoteId = $contentInfo->remoteId;
4174
4175
        // Start a transaction
4176
        $repository->beginTransaction();
4177
4178
        try {
4179
            // Get metadata update struct and change remoteId
4180
            $metadataUpdate = $this->contentService->newContentMetadataUpdateStruct();
4181
            $metadataUpdate->remoteId = md5(microtime(true));
4182
4183
            // Update the metadata of the published content object
4184
            $this->contentService->updateContentMetadata(
4185
                $contentInfo,
4186
                $metadataUpdate
4187
            );
4188
4189
            // Commit all changes.
4190
            $repository->commit();
4191
        } catch (Exception $e) {
4192
            // Cleanup hanging transaction on error
4193
            $repository->rollback();
4194
            throw $e;
4195
        }
4196
4197
        // Load current remoteId
4198
        $remoteIdReloaded = $this->contentService->loadContentInfo($contentId)->remoteId;
4199
4200
        $this->assertNotEquals($remoteId, $remoteIdReloaded);
4201
    }
4202
4203
    /**
4204
     * Test for the updateContentMetadata() method, and how cache + transactions play together.
4205
     *
4206
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4207
     * @depends testUpdateContentMetadata
4208
     * @depends testLoadContentInfo
4209
     */
4210
    public function testUpdateContentMetadataCheckWithinTransaction()
4211
    {
4212
        $repository = $this->getRepository();
4213
        $contentService = $repository->getContentService();
4214
        $contentId = $this->generateId('object', 12);
4215
4216
        // Load a ContentInfo object, and warmup cache
4217
        $contentInfo = $contentService->loadContentInfo($contentId);
4218
4219
        // Store remoteId for later testing
4220
        $remoteId = $contentInfo->remoteId;
4221
4222
        // Start a transaction
4223
        $repository->beginTransaction();
4224
4225
        try {
4226
            // Get metadata update struct and change remoteId
4227
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4228
            $metadataUpdate->remoteId = md5(microtime(true));
4229
4230
            // Update the metadata of the published content object
4231
            $contentService->updateContentMetadata(
4232
                $contentInfo,
4233
                $metadataUpdate
4234
            );
4235
4236
            // Check that it's been updated
4237
            $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4238
            $this->assertNotEquals($remoteId, $remoteIdReloaded);
4239
4240
            // Commit all changes.
4241
            $repository->commit();
4242
        } catch (Exception $e) {
4243
            // Cleanup hanging transaction on error
4244
            $repository->rollback();
4245
            throw $e;
4246
        }
4247
    }
4248
4249
    /**
4250
     * Test for the deleteVersion() method.
4251
     *
4252
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4253
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4254
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4255
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4256
     */
4257
    public function testDeleteVersionInTransactionWithRollback()
4258
    {
4259
        $repository = $this->getRepository();
4260
4261
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
4262
4263
        // Start a new transaction
4264
        $repository->beginTransaction();
4265
4266
        try {
4267
            // Create a new draft
4268
            $draft = $this->contentService->createContentDraft(
4269
                $this->contentService->loadContentInfo($contentId)
4270
            );
4271
4272
            $this->contentService->deleteVersion($draft->getVersionInfo());
4273
        } catch (Exception $e) {
4274
            // Cleanup hanging transaction on error
4275
            $repository->rollback();
4276
            throw $e;
4277
        }
4278
4279
        // Rollback all changes.
4280
        $repository->rollback();
4281
4282
        // This array will be empty
4283
        $drafts = $this->contentService->loadContentDrafts();
4284
4285
        $this->assertSame([], $drafts);
4286
    }
4287
4288
    /**
4289
     * Test for the deleteVersion() method.
4290
     *
4291
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4292
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4293
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4294
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4295
     */
4296
    public function testDeleteVersionInTransactionWithCommit()
4297
    {
4298
        $repository = $this->getRepository();
4299
4300
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
4301
4302
        // Start a new transaction
4303
        $repository->beginTransaction();
4304
4305
        try {
4306
            // Create a new draft
4307
            $draft = $this->contentService->createContentDraft(
4308
                $this->contentService->loadContentInfo($contentId)
4309
            );
4310
4311
            $this->contentService->deleteVersion($draft->getVersionInfo());
4312
4313
            // Commit all changes.
4314
            $repository->commit();
4315
        } catch (Exception $e) {
4316
            // Cleanup hanging transaction on error
4317
            $repository->rollback();
4318
            throw $e;
4319
        }
4320
4321
        // This array will contain no element
4322
        $drafts = $this->contentService->loadContentDrafts();
4323
4324
        $this->assertSame([], $drafts);
4325
    }
4326
4327
    /**
4328
     * Test for the deleteContent() method.
4329
     *
4330
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4331
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4332
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4333
     */
4334
    public function testDeleteContentInTransactionWithRollback()
4335
    {
4336
        $repository = $this->getRepository();
4337
4338
        $contentId = $this->generateId('object', self::MEMBERS_USER_GROUP_ID);
4339
4340
        // Load a ContentInfo instance
4341
        $contentInfo = $this->contentService->loadContentInfo($contentId);
4342
4343
        // Start a new transaction
4344
        $repository->beginTransaction();
4345
4346
        try {
4347
            // Delete content object
4348
            $this->contentService->deleteContent($contentInfo);
4349
        } catch (Exception $e) {
4350
            // Cleanup hanging transaction on error
4351
            $repository->rollback();
4352
            throw $e;
4353
        }
4354
4355
        // Rollback all changes
4356
        $repository->rollback();
4357
4358
        // This call will return the original content object
4359
        $contentInfo = $this->contentService->loadContentInfo($contentId);
4360
4361
        $this->assertEquals($contentId, $contentInfo->id);
4362
    }
4363
4364
    /**
4365
     * Test for the deleteContent() method.
4366
     *
4367
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4368
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4369
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4370
     */
4371
    public function testDeleteContentInTransactionWithCommit()
4372
    {
4373
        $repository = $this->getRepository();
4374
4375
        $contentId = $this->generateId('object', self::MEMBERS_USER_GROUP_ID);
4376
4377
        // Load a ContentInfo instance
4378
        $contentInfo = $this->contentService->loadContentInfo($contentId);
4379
4380
        // Start a new transaction
4381
        $repository->beginTransaction();
4382
4383
        try {
4384
            // Delete content object
4385
            $this->contentService->deleteContent($contentInfo);
4386
4387
            // Commit all changes
4388
            $repository->commit();
4389
        } catch (Exception $e) {
4390
            // Cleanup hanging transaction on error
4391
            $repository->rollback();
4392
            throw $e;
4393
        }
4394
4395
        // Deleted content info is not found anymore
4396
        try {
4397
            $this->contentService->loadContentInfo($contentId);
4398
        } catch (NotFoundException $e) {
4399
            return;
4400
        }
4401
4402
        $this->fail('Can still load ContentInfo after commit.');
4403
    }
4404
4405
    /**
4406
     * Test for the copyContent() method.
4407
     *
4408
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4409
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4410
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4411
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4412
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4413
     */
4414
    public function testCopyContentInTransactionWithRollback()
4415
    {
4416
        $repository = $this->getRepository();
4417
4418
        $contentId = $this->generateId('object', self::MEMBERS_USER_GROUP_ID);
4419
        $locationId = $this->generateId('location', self::ADMINISTRATORS_USER_GROUP_LOCATION_ID);
4420
4421
        // Load content object to copy
4422
        $content = $this->contentService->loadContent($contentId);
4423
4424
        // Create new target location
4425
        $locationCreate = $this->locationService->newLocationCreateStruct($locationId);
4426
4427
        // Start a new transaction
4428
        $repository->beginTransaction();
4429
4430
        try {
4431
            // Copy content with all versions and drafts
4432
            $this->contentService->copyContent(
4433
                $content->contentInfo,
4434
                $locationCreate
4435
            );
4436
        } catch (Exception $e) {
4437
            // Cleanup hanging transaction on error
4438
            $repository->rollback();
4439
            throw $e;
4440
        }
4441
4442
        // Rollback all changes
4443
        $repository->rollback();
4444
4445
        $this->refreshSearch($repository);
4446
4447
        // This array will only contain a single admin user object
4448
        $locations = $this->locationService->loadLocationChildren(
4449
            $this->locationService->loadLocation($locationId)
4450
        )->locations;
4451
4452
        $this->assertEquals(1, count($locations));
4453
    }
4454
4455
    /**
4456
     * Test for the copyContent() method.
4457
     *
4458
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4459
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4460
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4461
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4462
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4463
     */
4464
    public function testCopyContentInTransactionWithCommit()
4465
    {
4466
        $repository = $this->getRepository();
4467
4468
        $contentId = $this->generateId('object', self::MEMBERS_USER_GROUP_ID);
4469
        $locationId = $this->generateId('location', self::ADMINISTRATORS_USER_GROUP_LOCATION_ID);
4470
4471
        // Load content object to copy
4472
        $content = $this->contentService->loadContent($contentId);
4473
4474
        // Create new target location
4475
        $locationCreate = $this->locationService->newLocationCreateStruct($locationId);
4476
4477
        // Start a new transaction
4478
        $repository->beginTransaction();
4479
4480
        try {
4481
            // Copy content with all versions and drafts
4482
            $this->contentService->copyContent(
4483
                $content->contentInfo,
4484
                $locationCreate
4485
            );
4486
4487
            // Commit all changes
4488
            $repository->commit();
4489
        } catch (Exception $e) {
4490
            // Cleanup hanging transaction on error
4491
            $repository->rollback();
4492
            throw $e;
4493
        }
4494
4495
        $this->refreshSearch($repository);
4496
4497
        // This will contain the admin user and the new child location
4498
        $locations = $this->locationService->loadLocationChildren(
4499
            $this->locationService->loadLocation($locationId)
4500
        )->locations;
4501
4502
        $this->assertEquals(2, count($locations));
4503
    }
4504
4505
    public function testURLAliasesCreatedForNewContent()
4506
    {
4507
        $urlAliasService = $this->getRepository()->getURLAliasService();
4508
4509
        $draft = $this->createContentDraftVersion1();
4510
4511
        // Automatically creates a new URLAlias for the content
4512
        $liveContent = $this->contentService->publishVersion($draft->getVersionInfo());
4513
4514
        $location = $this->locationService->loadLocation(
4515
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4516
        );
4517
4518
        $aliases = $urlAliasService->listLocationAliases($location, false);
4519
4520
        $this->assertAliasesCorrect(
4521
            [
4522
                '/Design/Plain-site/An-awesome-forum' => [
4523
                    'type' => URLAlias::LOCATION,
4524
                    'destination' => $location->id,
4525
                    'path' => '/Design/Plain-site/An-awesome-forum',
4526
                    'languageCodes' => [self::ENG_US],
4527
                    'isHistory' => false,
4528
                    'isCustom' => false,
4529
                    'forward' => false,
4530
                ],
4531
            ],
4532
            $aliases
4533
        );
4534
    }
4535
4536
    public function testURLAliasesCreatedForUpdatedContent()
4537
    {
4538
        $urlAliasService = $this->getRepository()->getURLAliasService();
4539
4540
        $draft = $this->createUpdatedDraftVersion2();
4541
4542
        $location = $this->locationService->loadLocation(
4543
            $draft->getVersionInfo()->getContentInfo()->mainLocationId
4544
        );
4545
4546
        // Load and assert URL aliases before publishing updated Content, so that
4547
        // SPI cache is warmed up and cache invalidation is also tested.
4548
        $aliases = $urlAliasService->listLocationAliases($location, false);
4549
4550
        $this->assertAliasesCorrect(
4551
            [
4552
                '/Design/Plain-site/An-awesome-forum' => [
4553
                    'type' => URLAlias::LOCATION,
4554
                    'destination' => $location->id,
4555
                    'path' => '/Design/Plain-site/An-awesome-forum',
4556
                    'languageCodes' => [self::ENG_US],
4557
                    'alwaysAvailable' => true,
4558
                    'isHistory' => false,
4559
                    'isCustom' => false,
4560
                    'forward' => false,
4561
                ],
4562
            ],
4563
            $aliases
4564
        );
4565
4566
        // Automatically marks old aliases for the content as history
4567
        // and creates new aliases, based on the changes
4568
        $liveContent = $this->contentService->publishVersion($draft->getVersionInfo());
4569
4570
        $location = $this->locationService->loadLocation(
4571
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4572
        );
4573
4574
        $aliases = $urlAliasService->listLocationAliases($location, false);
4575
4576
        $this->assertAliasesCorrect(
4577
            [
4578
                '/Design/Plain-site/An-awesome-forum2' => [
4579
                    'type' => URLAlias::LOCATION,
4580
                    'destination' => $location->id,
4581
                    'path' => '/Design/Plain-site/An-awesome-forum2',
4582
                    'languageCodes' => [self::ENG_US],
4583
                    'alwaysAvailable' => true,
4584
                    'isHistory' => false,
4585
                    'isCustom' => false,
4586
                    'forward' => false,
4587
                ],
4588
                '/Design/Plain-site/An-awesome-forum23' => [
4589
                    'type' => URLAlias::LOCATION,
4590
                    'destination' => $location->id,
4591
                    'path' => '/Design/Plain-site/An-awesome-forum23',
4592
                    'languageCodes' => [self::ENG_GB],
4593
                    'alwaysAvailable' => true,
4594
                    'isHistory' => false,
4595
                    'isCustom' => false,
4596
                    'forward' => false,
4597
                ],
4598
            ],
4599
            $aliases
4600
        );
4601
    }
4602
4603
    public function testCustomURLAliasesNotHistorizedOnUpdatedContent()
4604
    {
4605
        $urlAliasService = $this->getRepository()->getURLAliasService();
4606
4607
        $content = $this->createContentVersion1();
4608
4609
        // Create a custom URL alias
4610
        $urlAliasService->createUrlAlias(
4611
            $this->locationService->loadLocation(
4612
                $content->getVersionInfo()->getContentInfo()->mainLocationId
4613
            ),
4614
            '/my/fancy/story-about-ez-publish',
4615
            self::ENG_US
4616
        );
4617
4618
        $draftVersion2 = $this->contentService->createContentDraft($content->contentInfo);
4619
4620
        $contentUpdate = $this->contentService->newContentUpdateStruct();
4621
        $contentUpdate->initialLanguageCode = self::ENG_US;
4622
        $contentUpdate->setField('name', 'Amazing Bielefeld forum');
4623
4624
        $draftVersion2 = $this->contentService->updateContent(
4625
            $draftVersion2->getVersionInfo(),
4626
            $contentUpdate
4627
        );
4628
4629
        // Only marks auto-generated aliases as history
4630
        // the custom one is left untouched
4631
        $liveContent = $this->contentService->publishVersion($draftVersion2->getVersionInfo());
4632
4633
        $location = $this->locationService->loadLocation(
4634
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4635
        );
4636
4637
        $aliases = $urlAliasService->listLocationAliases($location);
4638
4639
        $this->assertAliasesCorrect(
4640
            [
4641
                '/my/fancy/story-about-ez-publish' => [
4642
                    'type' => URLAlias::LOCATION,
4643
                    'destination' => $location->id,
4644
                    'path' => '/my/fancy/story-about-ez-publish',
4645
                    'languageCodes' => [self::ENG_US],
4646
                    'isHistory' => false,
4647
                    'isCustom' => true,
4648
                    'forward' => false,
4649
                    'alwaysAvailable' => false,
4650
                ],
4651
            ],
4652
            $aliases
4653
        );
4654
    }
4655
4656
    /**
4657
     * Test to ensure that old versions are not affected by updates to newer
4658
     * drafts.
4659
     */
4660
    public function testUpdatingDraftDoesNotUpdateOldVersions()
4661
    {
4662
        $contentVersion2 = $this->createContentVersion2();
4663
4664
        $loadedContent1 = $this->contentService->loadContent($contentVersion2->id, null, 1);
4665
        $loadedContent2 = $this->contentService->loadContent($contentVersion2->id, null, 2);
4666
4667
        $this->assertNotEquals(
4668
            $loadedContent1->getFieldValue('name', self::ENG_US),
4669
            $loadedContent2->getFieldValue('name', self::ENG_US)
4670
        );
4671
    }
4672
4673
    /**
4674
     * Test scenario with writer and publisher users.
4675
     * Writer can only create content. Publisher can publish this content.
4676
     */
4677
    public function testPublishWorkflow()
4678
    {
4679
        $this->createRoleWithPolicies('Publisher', [
4680
            ['module' => 'content', 'function' => 'read'],
4681
            ['module' => 'content', 'function' => 'create'],
4682
            ['module' => 'content', 'function' => 'publish'],
4683
        ]);
4684
4685
        $this->createRoleWithPolicies('Writer', [
4686
            ['module' => 'content', 'function' => 'read'],
4687
            ['module' => 'content', 'function' => 'create'],
4688
        ]);
4689
4690
        $writerUser = $this->createCustomUserWithLogin(
4691
            'writer',
4692
            '[email protected]',
4693
            self::WRITERS_USER_GROUP_NAME,
4694
            'Writer'
4695
        );
4696
4697
        $publisherUser = $this->createCustomUserWithLogin(
4698
            'publisher',
4699
            '[email protected]',
4700
            'Publishers',
4701
            'Publisher'
4702
        );
4703
4704
        $this->permissionResolver->setCurrentUserReference($writerUser);
4705
        $draft = $this->createContentDraftVersion1();
4706
4707
        $this->permissionResolver->setCurrentUserReference($publisherUser);
4708
        $content = $this->contentService->publishVersion($draft->versionInfo);
4709
4710
        $this->contentService->loadContent($content->id);
4711
    }
4712
4713
    /**
4714
     * Test publish / content policy is required to be able to publish content.
4715
     */
4716
    public function testPublishContentWithoutPublishPolicyThrowsException()
4717
    {
4718
        $this->createRoleWithPolicies('Writer', [
4719
            ['module' => 'content', 'function' => 'read'],
4720
            ['module' => 'content', 'function' => 'create'],
4721
            ['module' => 'content', 'function' => 'edit'],
4722
        ]);
4723
        $writerUser = $this->createCustomUserWithLogin(
4724
            'writer',
4725
            '[email protected]',
4726
            self::WRITERS_USER_GROUP_NAME,
4727
            'Writer'
4728
        );
4729
        $this->permissionResolver->setCurrentUserReference($writerUser);
4730
4731
        $this->expectException(CoreUnauthorizedException::class);
4732
        $this->expectExceptionMessageRegExp('/User does not have access to \'publish\' \'content\'/');
4733
4734
        $this->createContentVersion1();
4735
    }
4736
4737
    /**
4738
     * Test removal of the specific translation from all the Versions of a Content Object.
4739
     *
4740
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
4741
     */
4742
    public function testDeleteTranslation()
4743
    {
4744
        $content = $this->createContentVersion2();
4745
4746
        // create multiple versions to exceed archive limit
4747
        for ($i = 0; $i < 5; ++$i) {
4748
            $contentDraft = $this->contentService->createContentDraft($content->contentInfo);
4749
            $contentUpdateStruct = $this->contentService->newContentUpdateStruct();
4750
            $contentDraft = $this->contentService->updateContent(
4751
                $contentDraft->versionInfo,
4752
                $contentUpdateStruct
4753
            );
4754
            $this->contentService->publishVersion($contentDraft->versionInfo);
4755
        }
4756
4757
        $this->contentService->deleteTranslation($content->contentInfo, self::ENG_GB);
4758
4759
        $this->assertTranslationDoesNotExist(self::ENG_GB, $content->id);
4760
    }
4761
4762
    /**
4763
     * Test deleting a Translation which is initial for some Version, updates initialLanguageCode
4764
     * with mainLanguageCode (assuming they are different).
4765
     */
4766
    public function testDeleteTranslationUpdatesInitialLanguageCodeVersion()
4767
    {
4768
        $content = $this->createContentVersion2();
4769
        // create another, copied, version
4770
        $contentDraft = $this->contentService->updateContent(
4771
            $this->contentService->createContentDraft($content->contentInfo)->versionInfo,
4772
            $this->contentService->newContentUpdateStruct()
4773
        );
4774
        $publishedContent = $this->contentService->publishVersion($contentDraft->versionInfo);
4775
4776
        // remove first version with only one translation as it is not the subject of this test
4777
        $this->contentService->deleteVersion(
4778
            $this->contentService->loadVersionInfo($publishedContent->contentInfo, 1)
4779
        );
4780
4781
        // sanity check
4782
        self::assertEquals(self::ENG_US, $content->contentInfo->mainLanguageCode);
4783
        self::assertEquals(self::ENG_US, $content->versionInfo->initialLanguageCode);
4784
4785
        // update mainLanguageCode so it is different than initialLanguageCode for Version
4786
        $contentMetadataUpdateStruct = $this->contentService->newContentMetadataUpdateStruct();
4787
        $contentMetadataUpdateStruct->mainLanguageCode = self::ENG_GB;
4788
        $content = $this->contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
4789
4790
        $this->contentService->deleteTranslation($content->contentInfo, self::ENG_US);
4791
4792
        $this->assertTranslationDoesNotExist(self::ENG_US, $content->id);
4793
    }
4794
4795
    /**
4796
     * Test removal of the specific translation properly updates languages of the URL alias.
4797
     *
4798
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
4799
     */
4800
    public function testDeleteTranslationUpdatesUrlAlias()
4801
    {
4802
        $urlAliasService = $this->getRepository()->getURLAliasService();
4803
4804
        $content = $this->createContentVersion2();
4805
        $mainLocation = $this->locationService->loadLocation($content->contentInfo->mainLocationId);
4806
4807
        // create custom URL alias for Content main Location
4808
        $urlAliasService->createUrlAlias($mainLocation, '/my-custom-url', self::ENG_GB);
4809
4810
        // create secondary Location for Content
4811
        $secondaryLocation = $this->locationService->createLocation(
4812
            $content->contentInfo,
4813
            $this->locationService->newLocationCreateStruct(2)
4814
        );
4815
4816
        // create custom URL alias for Content secondary Location
4817
        $urlAliasService->createUrlAlias($secondaryLocation, '/my-secondary-url', self::ENG_GB);
4818
4819
        // delete Translation
4820
        $this->contentService->deleteTranslation($content->contentInfo, self::ENG_GB);
4821
4822
        foreach ([$mainLocation, $secondaryLocation] as $location) {
4823
            // check auto-generated URL aliases
4824
            foreach ($urlAliasService->listLocationAliases($location, false) as $alias) {
4825
                self::assertNotContains(self::ENG_GB, $alias->languageCodes);
4826
            }
4827
4828
            // check custom URL aliases
4829
            foreach ($urlAliasService->listLocationAliases($location) as $alias) {
4830
                self::assertNotContains(self::ENG_GB, $alias->languageCodes);
4831
            }
4832
        }
4833
    }
4834
4835
    /**
4836
     * Test removal of a main translation throws BadStateException.
4837
     *
4838
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
4839
     */
4840
    public function testDeleteTranslationMainLanguageThrowsBadStateException()
4841
    {
4842
        $content = $this->createContentVersion2();
4843
4844
        // delete first version which has only one translation
4845
        $this->contentService->deleteVersion($this->contentService->loadVersionInfo($content->contentInfo, 1));
4846
4847
        // try to delete main translation
4848
        $this->expectException(BadStateException::class);
4849
        $this->expectExceptionMessage('Specified translation is the main translation of the Content Object');
4850
4851
        $this->contentService->deleteTranslation($content->contentInfo, $content->contentInfo->mainLanguageCode);
4852
    }
4853
4854
    /**
4855
     * Test removal of a Translation is possible when some archived Versions have only this Translation.
4856
     *
4857
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
4858
     */
4859
    public function testDeleteTranslationDeletesSingleTranslationVersions()
4860
    {
4861
        // content created by the createContentVersion1 method has eng-US translation only.
4862
        $content = $this->createContentVersion1();
4863
4864
        // create new version and add eng-GB translation
4865
        $contentDraft = $this->contentService->createContentDraft($content->contentInfo);
4866
        $contentUpdateStruct = $this->contentService->newContentUpdateStruct();
4867
        $contentUpdateStruct->setField('name', 'Awesome Board', self::ENG_GB);
4868
        $contentDraft = $this->contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
4869
        $publishedContent = $this->contentService->publishVersion($contentDraft->versionInfo);
4870
4871
        // update mainLanguageCode to avoid exception related to that
4872
        $contentMetadataUpdateStruct = $this->contentService->newContentMetadataUpdateStruct();
4873
        $contentMetadataUpdateStruct->mainLanguageCode = self::ENG_GB;
4874
4875
        $content = $this->contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
4876
4877
        $this->contentService->deleteTranslation($content->contentInfo, self::ENG_US);
4878
4879
        $this->assertTranslationDoesNotExist(self::ENG_US, $content->id);
4880
    }
4881
4882
    /**
4883
     * Test removal of the translation by the user who is not allowed to delete a content
4884
     * throws UnauthorizedException.
4885
     *
4886
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
4887
     */
4888
    public function testDeleteTranslationThrowsUnauthorizedException()
4889
    {
4890
        $content = $this->createContentVersion2();
4891
4892
        // create user that can read/create/edit but cannot delete content
4893
        $this->createRoleWithPolicies('Writer', [
4894
            ['module' => 'content', 'function' => 'read'],
4895
            ['module' => 'content', 'function' => 'versionread'],
4896
            ['module' => 'content', 'function' => 'create'],
4897
            ['module' => 'content', 'function' => 'edit'],
4898
        ]);
4899
        $writerUser = $this->createCustomUserWithLogin(
4900
            'writer',
4901
            '[email protected]',
4902
            self::WRITERS_USER_GROUP_NAME,
4903
            'Writer'
4904
        );
4905
        $this->permissionResolver->setCurrentUserReference($writerUser);
4906
4907
        $this->expectException(UnauthorizedException::class);
4908
        $this->expectExceptionMessage('User does not have access to \'remove\' \'content\'');
4909
4910
        $this->contentService->deleteTranslation($content->contentInfo, self::ENG_GB);
4911
    }
4912
4913
    /**
4914
     * Test removal of a non-existent translation throws InvalidArgumentException.
4915
     *
4916
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
4917
     */
4918
    public function testDeleteTranslationThrowsInvalidArgumentException()
4919
    {
4920
        // content created by the createContentVersion1 method has eng-US translation only.
4921
        $content = $this->createContentVersion1();
4922
4923
        $this->expectException(APIInvalidArgumentException::class);
4924
        $this->expectExceptionMessage('Argument \'$languageCode\' is invalid: ger-DE does not exist in the Content item');
4925
4926
        $this->contentService->deleteTranslation($content->contentInfo, self::GER_DE);
4927
    }
4928
4929
    /**
4930
     * Test deleting a Translation from Draft.
4931
     *
4932
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
4933
     */
4934
    public function testDeleteTranslationFromDraft()
4935
    {
4936
        $languageCode = self::ENG_GB;
4937
        $content = $this->createMultipleLanguageContentVersion2();
4938
        $draft = $this->contentService->createContentDraft($content->contentInfo);
4939
        $draft = $this->contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
4940
        $content = $this->contentService->publishVersion($draft->versionInfo);
4941
4942
        $loadedContent = $this->contentService->loadContent($content->id);
4943
        self::assertNotContains($languageCode, $loadedContent->versionInfo->languageCodes);
4944
        self::assertEmpty($loadedContent->getFieldsByLanguage($languageCode));
4945
    }
4946
4947
    /**
4948
     * Get values for multilingual field.
4949
     *
4950
     * @return array
4951
     */
4952
    public function providerForDeleteTranslationFromDraftRemovesUrlAliasOnPublishing()
4953
    {
4954
        return [
4955
            [
4956
                [self::ENG_US => 'US Name', self::ENG_GB => 'GB Name'],
4957
            ],
4958
            [
4959
                [self::ENG_US => 'Same Name', self::ENG_GB => 'Same Name'],
4960
            ],
4961
        ];
4962
    }
4963
4964
    /**
4965
     * Test deleting a Translation from Draft removes previously stored URL aliases for published Content.
4966
     *
4967
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
4968
     *
4969
     * @dataProvider providerForDeleteTranslationFromDraftRemovesUrlAliasOnPublishing
4970
     *
4971
     * @param string[] $fieldValues translated field values
4972
     *
4973
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
4974
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
4975
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
4976
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
4977
     */
4978
    public function testDeleteTranslationFromDraftRemovesUrlAliasOnPublishing(array $fieldValues)
4979
    {
4980
        $urlAliasService = $this->getRepository()->getURLAliasService();
4981
4982
        // set language code to be removed
4983
        $languageCode = self::ENG_GB;
4984
        $draft = $this->createMultilingualContentDraft(
4985
            'folder',
4986
            2,
4987
            self::ENG_US,
4988
            [
4989
                'name' => [
4990
                    self::ENG_GB => $fieldValues[self::ENG_GB],
4991
                    self::ENG_US => $fieldValues[self::ENG_US],
4992
                ],
4993
            ]
4994
        );
4995
        $content = $this->contentService->publishVersion($draft->versionInfo);
4996
4997
        // create secondary location
4998
        $this->locationService->createLocation(
4999
            $content->contentInfo,
5000
            $this->locationService->newLocationCreateStruct(5)
5001
        );
5002
5003
        // sanity check
5004
        $locations = $this->locationService->loadLocations($content->contentInfo);
5005
        self::assertCount(2, $locations, 'Sanity check: Expected to find 2 Locations');
5006
        foreach ($locations as $location) {
5007
            $urlAliasService->createUrlAlias($location, '/us-custom_' . $location->id, self::ENG_US);
5008
            $urlAliasService->createUrlAlias($location, '/gb-custom_' . $location->id, self::ENG_GB);
5009
5010
            // check default URL aliases
5011
            $aliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
5012
            self::assertNotEmpty($aliases, 'Sanity check: URL alias for the translation does not exist');
5013
5014
            // check custom URL aliases
5015
            $aliases = $urlAliasService->listLocationAliases($location, true, $languageCode);
5016
            self::assertNotEmpty($aliases, 'Sanity check: Custom URL alias for the translation does not exist');
5017
        }
5018
5019
        // delete translation and publish new version
5020
        $draft = $this->contentService->createContentDraft($content->contentInfo);
5021
        $draft = $this->contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5022
        $this->contentService->publishVersion($draft->versionInfo);
5023
5024
        // check that aliases does not exist
5025
        foreach ($locations as $location) {
5026
            // check default URL aliases
5027
            $aliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
5028
            self::assertEmpty($aliases, 'URL alias for the deleted translation still exists');
5029
5030
            // check custom URL aliases
5031
            $aliases = $urlAliasService->listLocationAliases($location, true, $languageCode);
5032
            self::assertEmpty($aliases, 'Custom URL alias for the deleted translation still exists');
5033
        }
5034
    }
5035
5036
    /**
5037
     * Test that URL aliases for deleted Translations are properly archived.
5038
     */
5039
    public function testDeleteTranslationFromDraftArchivesUrlAliasOnPublishing()
5040
    {
5041
        $urlAliasService = $this->getRepository()->getURLAliasService();
5042
5043
        $content = $this->contentService->publishVersion(
5044
            $this->createMultilingualContentDraft(
5045
                'folder',
5046
                2,
5047
                self::ENG_US,
5048
                [
5049
                    'name' => [
5050
                        self::ENG_GB => 'BritishEnglishContent',
5051
                        self::ENG_US => 'AmericanEnglishContent',
5052
                    ],
5053
                ]
5054
            )->versionInfo
5055
        );
5056
5057
        $unrelatedContent = $this->contentService->publishVersion(
5058
            $this->createMultilingualContentDraft(
5059
                'folder',
5060
                2,
5061
                self::ENG_US,
5062
                [
5063
                    'name' => [
5064
                        self::ENG_GB => 'AnotherBritishContent',
5065
                        self::ENG_US => 'AnotherAmericanContent',
5066
                    ],
5067
                ]
5068
            )->versionInfo
5069
        );
5070
5071
        $urlAlias = $urlAliasService->lookup('/BritishEnglishContent');
5072
        self::assertFalse($urlAlias->isHistory);
5073
        self::assertEquals($urlAlias->path, '/BritishEnglishContent');
5074
        self::assertEquals($urlAlias->destination, $content->contentInfo->mainLocationId);
5075
5076
        $draft = $this->contentService->deleteTranslationFromDraft(
5077
            $this->contentService->createContentDraft($content->contentInfo)->versionInfo,
5078
            self::ENG_GB
5079
        );
5080
        $content = $this->contentService->publishVersion($draft->versionInfo);
5081
5082
        $urlAlias = $urlAliasService->lookup('/BritishEnglishContent');
5083
        self::assertTrue($urlAlias->isHistory);
5084
        self::assertEquals($urlAlias->path, '/BritishEnglishContent');
5085
        self::assertEquals($urlAlias->destination, $content->contentInfo->mainLocationId);
5086
5087
        $unrelatedUrlAlias = $urlAliasService->lookup('/AnotherBritishContent');
5088
        self::assertFalse($unrelatedUrlAlias->isHistory);
5089
        self::assertEquals($unrelatedUrlAlias->path, '/AnotherBritishContent');
5090
        self::assertEquals($unrelatedUrlAlias->destination, $unrelatedContent->contentInfo->mainLocationId);
5091
    }
5092
5093
    /**
5094
     * Test deleting a Translation from Draft which has single Translation throws BadStateException.
5095
     *
5096
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5097
     */
5098
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnSingleTranslation()
5099
    {
5100
        // create Content with single Translation
5101
        $publishedContent = $this->contentService->publishVersion(
5102
            $this->createContentDraft(
5103
                self::FORUM_IDENTIFIER,
5104
                2,
5105
                ['name' => 'Eng-US Version name']
5106
            )->versionInfo
5107
        );
5108
5109
        // update mainLanguageCode to avoid exception related to trying to delete main Translation
5110
        $contentMetadataUpdateStruct = $this->contentService->newContentMetadataUpdateStruct();
5111
        $contentMetadataUpdateStruct->mainLanguageCode = self::ENG_GB;
5112
        $publishedContent = $this->contentService->updateContentMetadata(
5113
            $publishedContent->contentInfo,
5114
            $contentMetadataUpdateStruct
5115
        );
5116
5117
        // create single Translation Version from the first one
5118
        $draft = $this->contentService->createContentDraft(
5119
            $publishedContent->contentInfo,
5120
            $publishedContent->versionInfo
5121
        );
5122
5123
        $this->expectException(BadStateException::class);
5124
        $this->expectExceptionMessage('Specified Translation is the only one Content Object Version has');
5125
5126
        // attempt to delete Translation
5127
        $this->contentService->deleteTranslationFromDraft($draft->versionInfo, self::ENG_US);
5128
    }
5129
5130
    /**
5131
     * Test deleting the Main Translation from Draft throws BadStateException.
5132
     *
5133
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5134
     */
5135
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnMainTranslation()
5136
    {
5137
        $mainLanguageCode = self::ENG_US;
5138
        $draft = $this->createMultilingualContentDraft(
5139
            self::FORUM_IDENTIFIER,
5140
            2,
5141
            $mainLanguageCode,
5142
            [
5143
                'name' => [
5144
                    self::ENG_US => 'An awesome eng-US forum',
5145
                    self::ENG_GB => 'An awesome eng-GB forum',
5146
                ],
5147
            ]
5148
        );
5149
5150
        $this->expectException(BadStateException::class);
5151
        $this->expectExceptionMessage('Specified Translation is the main Translation of the Content Object');
5152
5153
        $this->contentService->deleteTranslationFromDraft($draft->versionInfo, $mainLanguageCode);
5154
    }
5155
5156
    /**
5157
     * Test deleting the Translation from Published Version throws BadStateException.
5158
     *
5159
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5160
     */
5161
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnPublishedVersion()
5162
    {
5163
        $languageCode = self::ENG_US;
5164
        $content = $this->createMultipleLanguageContentVersion2();
5165
        $draft = $this->contentService->createContentDraft($content->contentInfo);
5166
        $publishedContent = $this->contentService->publishVersion($draft->versionInfo);
5167
5168
        $this->expectException(BadStateException::class);
5169
        $this->expectExceptionMessage('Version is not a draft');
5170
5171
        $this->contentService->deleteTranslationFromDraft($publishedContent->versionInfo, $languageCode);
5172
    }
5173
5174
    /**
5175
     * Test deleting a Translation from Draft throws UnauthorizedException if user cannot edit Content.
5176
     *
5177
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5178
     */
5179
    public function testDeleteTranslationFromDraftThrowsUnauthorizedException()
5180
    {
5181
        $languageCode = self::ENG_GB;
5182
        $content = $this->createMultipleLanguageContentVersion2();
5183
        $draft = $this->contentService->createContentDraft($content->contentInfo);
5184
5185
        // create user that can read/create/delete but cannot edit or content
5186
        $this->createRoleWithPolicies('Writer', [
5187
            ['module' => 'content', 'function' => 'read'],
5188
            ['module' => 'content', 'function' => 'versionread'],
5189
            ['module' => 'content', 'function' => 'create'],
5190
            ['module' => 'content', 'function' => 'delete'],
5191
        ]);
5192
        $writerUser = $this->createCustomUserWithLogin(
5193
            'user',
5194
            '[email protected]',
5195
            self::WRITERS_USER_GROUP_NAME,
5196
            'Writer'
5197
        );
5198
        $this->permissionResolver->setCurrentUserReference($writerUser);
5199
5200
        $this->expectException(UnauthorizedException::class);
5201
        $this->expectExceptionMessage('User does not have access to \'edit\' \'content\'');
5202
5203
        $this->contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5204
    }
5205
5206
    /**
5207
     * Test deleting a non-existent Translation from Draft throws InvalidArgumentException.
5208
     *
5209
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5210
     */
5211
    public function testDeleteTranslationFromDraftThrowsInvalidArgumentException()
5212
    {
5213
        $languageCode = self::GER_DE;
5214
        $content = $this->createMultipleLanguageContentVersion2();
5215
        $draft = $this->contentService->createContentDraft($content->contentInfo);
5216
        $this->expectException(APIInvalidArgumentException::class);
5217
        $this->expectExceptionMessageRegExp('/The Version \(ContentId=\d+, VersionNo=\d+\) is not translated into ger-DE/');
5218
        $this->contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5219
    }
5220
5221
    /**
5222
     * Test loading list of Content items.
5223
     */
5224
    public function testLoadContentListByContentInfo()
5225
    {
5226
        $allLocationsCount = $this->locationService->getAllLocationsCount();
5227
        $contentInfoList = array_map(
5228
            function (Location $location) {
5229
                return $location->contentInfo;
5230
            },
5231
            $this->locationService->loadAllLocations(0, $allLocationsCount)
5232
        );
5233
5234
        $contentList = $this->contentService->loadContentListByContentInfo($contentInfoList);
5235
        self::assertCount(count($contentInfoList), $contentList);
5236
        foreach ($contentList as $content) {
5237
            try {
5238
                $loadedContent = $this->contentService->loadContent($content->id);
5239
                self::assertEquals($loadedContent, $content, "Failed to properly bulk-load Content {$content->id}");
5240
            } catch (NotFoundException $e) {
5241
                self::fail("Failed to load Content {$content->id}: {$e->getMessage()}");
5242
            } catch (UnauthorizedException $e) {
5243
                self::fail("Failed to load Content {$content->id}: {$e->getMessage()}");
5244
            }
5245
        }
5246
    }
5247
5248
    /**
5249
     * Test loading content versions after removing exactly two drafts.
5250
     *
5251
     * @see https://jira.ez.no/browse/EZP-30271
5252
     *
5253
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteVersion
5254
     */
5255
    public function testLoadVersionsAfterDeletingTwoDrafts()
5256
    {
5257
        $content = $this->createFolder([self::ENG_GB => 'Foo'], 2);
5258
5259
        // First update and publish
5260
        $modifiedContent = $this->updateFolder($content, [self::ENG_GB => 'Foo1']);
5261
        $content = $this->contentService->publishVersion($modifiedContent->versionInfo);
5262
5263
        // Second update and publish
5264
        $modifiedContent = $this->updateFolder($content, [self::ENG_GB => 'Foo2']);
5265
        $content = $this->contentService->publishVersion($modifiedContent->versionInfo);
5266
5267
        // Create drafts
5268
        $this->updateFolder($content, [self::ENG_GB => 'Foo3']);
5269
        $this->updateFolder($content, [self::ENG_GB => 'Foo4']);
5270
5271
        $versions = $this->contentService->loadVersions($content->contentInfo);
5272
5273
        foreach ($versions as $key => $version) {
5274
            if ($version->isDraft()) {
5275
                $this->contentService->deleteVersion($version);
5276
                unset($versions[$key]);
5277
            }
5278
        }
5279
5280
        $this->assertEquals($versions, $this->contentService->loadVersions($content->contentInfo));
5281
    }
5282
5283
    /**
5284
     * Tests loading list of content versions of status draft.
5285
     */
5286
    public function testLoadVersionsOfStatusDraft()
5287
    {
5288
        $content = $this->createContentVersion1();
5289
5290
        $this->contentService->createContentDraft($content->contentInfo);
5291
        $this->contentService->createContentDraft($content->contentInfo);
5292
        $this->contentService->createContentDraft($content->contentInfo);
5293
5294
        $versions = $this->contentService->loadVersions($content->contentInfo, VersionInfo::STATUS_DRAFT);
5295
5296
        $this->assertSame(\count($versions), 3);
5297
    }
5298
5299
    /**
5300
     * Tests loading list of content versions of status archived.
5301
     */
5302
    public function testLoadVersionsOfStatusArchived()
5303
    {
5304
        $content = $this->createContentVersion1();
5305
5306
        $draft1 = $this->contentService->createContentDraft($content->contentInfo);
5307
        $this->contentService->publishVersion($draft1->versionInfo);
5308
5309
        $draft2 = $this->contentService->createContentDraft($content->contentInfo);
5310
        $this->contentService->publishVersion($draft2->versionInfo);
5311
5312
        $versions = $this->contentService->loadVersions($content->contentInfo, VersionInfo::STATUS_ARCHIVED);
5313
5314
        $this->assertSame(\count($versions), 2);
5315
    }
5316
5317
    /**
5318
     * Asserts that all aliases defined in $expectedAliasProperties with the
5319
     * given properties are available in $actualAliases and not more.
5320
     *
5321
     * @param array $expectedAliasProperties
5322
     * @param array $actualAliases
5323
     */
5324
    private function assertAliasesCorrect(array $expectedAliasProperties, array $actualAliases)
5325
    {
5326
        foreach ($actualAliases as $actualAlias) {
5327
            if (!isset($expectedAliasProperties[$actualAlias->path])) {
5328
                $this->fail(
5329
                    sprintf(
5330
                        'Alias with path "%s" in languages "%s" not expected.',
5331
                        $actualAlias->path,
5332
                        implode(', ', $actualAlias->languageCodes)
5333
                    )
5334
                );
5335
            }
5336
5337
            foreach ($expectedAliasProperties[$actualAlias->path] as $propertyName => $propertyValue) {
5338
                $this->assertEquals(
5339
                    $propertyValue,
5340
                    $actualAlias->$propertyName,
5341
                    sprintf(
5342
                        'Property $%s incorrect on alias with path "%s" in languages "%s".',
5343
                        $propertyName,
5344
                        $actualAlias->path,
5345
                        implode(', ', $actualAlias->languageCodes)
5346
                    )
5347
                );
5348
            }
5349
5350
            unset($expectedAliasProperties[$actualAlias->path]);
5351
        }
5352
5353
        if (!empty($expectedAliasProperties)) {
5354
            $this->fail(
5355
                sprintf(
5356
                    'Missing expected aliases with paths "%s".',
5357
                    implode('", "', array_keys($expectedAliasProperties))
5358
                )
5359
            );
5360
        }
5361
    }
5362
5363
    /**
5364
     * Asserts that the given fields are equal to the default fields fixture.
5365
     *
5366
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5367
     */
5368
    private function assertAllFieldsEquals(array $fields)
5369
    {
5370
        $actual = $this->normalizeFields($fields);
5371
        $expected = $this->normalizeFields($this->createFieldsFixture());
5372
5373
        $this->assertEquals($expected, $actual);
5374
    }
5375
5376
    /**
5377
     * Asserts that the given fields are equal to a language filtered set of the
5378
     * default fields fixture.
5379
     *
5380
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5381
     * @param string $languageCode
5382
     */
5383
    private function assertLocaleFieldsEquals(array $fields, $languageCode)
5384
    {
5385
        $actual = $this->normalizeFields($fields);
5386
5387
        $expected = [];
5388
        foreach ($this->normalizeFields($this->createFieldsFixture()) as $field) {
5389
            if ($field->languageCode !== $languageCode) {
5390
                continue;
5391
            }
5392
            $expected[] = $field;
5393
        }
5394
5395
        $this->assertEquals($expected, $actual);
5396
    }
5397
5398
    /**
5399
     * This method normalizes a set of fields and returns a normalized set.
5400
     *
5401
     * Normalization means it resets the storage specific field id to zero and
5402
     * it sorts the field by their identifier and their language code. In
5403
     * addition, the field value is removed, since this one depends on the
5404
     * specific FieldType, which is tested in a dedicated integration test.
5405
     *
5406
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5407
     *
5408
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5409
     */
5410
    private function normalizeFields(array $fields)
5411
    {
5412
        $normalized = [];
5413
        foreach ($fields as $field) {
5414
            $normalized[] = new Field(
5415
                [
5416
                    'id' => 0,
5417
                    'value' => $field->value !== null,
5418
                    'languageCode' => $field->languageCode,
5419
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
5420
                    'fieldTypeIdentifier' => $field->fieldTypeIdentifier,
5421
                ]
5422
            );
5423
        }
5424
        usort(
5425
            $normalized,
5426
            function ($field1, $field2) {
5427
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
5428
                    return strcasecmp($field1->languageCode, $field2->languageCode);
5429
                }
5430
5431
                return $return;
5432
            }
5433
        );
5434
5435
        return $normalized;
5436
    }
5437
5438
    /**
5439
     * Asserts that given Content has default ContentStates.
5440
     *
5441
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
5442
     */
5443
    private function assertDefaultContentStates(ContentInfo $contentInfo)
5444
    {
5445
        $objectStateService = $this->getRepository()->getObjectStateService();
5446
5447
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
5448
5449
        foreach ($objectStateGroups as $objectStateGroup) {
5450
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
5451
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
5452
                // Only check the first object state which is the default one.
5453
                $this->assertEquals(
5454
                    $objectState,
5455
                    $contentState
5456
                );
5457
                break;
5458
            }
5459
        }
5460
    }
5461
5462
    /**
5463
     * Assert that given Content has no references to a translation specified by the $languageCode.
5464
     *
5465
     * @param string $languageCode
5466
     * @param int $contentId
5467
     */
5468
    private function assertTranslationDoesNotExist($languageCode, $contentId)
5469
    {
5470
        $content = $this->contentService->loadContent($contentId);
5471
5472
        foreach ($content->fields as $field) {
5473
            /** @var array $field */
5474
            self::assertArrayNotHasKey($languageCode, $field);
5475
            self::assertNotEquals($languageCode, $content->contentInfo->mainLanguageCode);
5476
            self::assertArrayNotHasKey($languageCode, $content->versionInfo->getNames());
5477
            self::assertNotEquals($languageCode, $content->versionInfo->initialLanguageCode);
5478
            self::assertNotContains($languageCode, $content->versionInfo->languageCodes);
5479
        }
5480
        foreach ($this->contentService->loadVersions($content->contentInfo) as $versionInfo) {
5481
            self::assertArrayNotHasKey($languageCode, $versionInfo->getNames());
5482
            self::assertNotEquals($languageCode, $versionInfo->contentInfo->mainLanguageCode);
5483
            self::assertNotEquals($languageCode, $versionInfo->initialLanguageCode);
5484
            self::assertNotContains($languageCode, $versionInfo->languageCodes);
5485
        }
5486
    }
5487
5488
    /**
5489
     * Returns the default fixture of fields used in most tests.
5490
     *
5491
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5492
     */
5493
    private function createFieldsFixture()
5494
    {
5495
        return [
5496
            new Field(
5497
                [
5498
                    'id' => 0,
5499
                    'value' => 'Foo',
5500
                    'languageCode' => self::ENG_US,
5501
                    'fieldDefIdentifier' => 'description',
5502
                    'fieldTypeIdentifier' => 'ezrichtext',
5503
                ]
5504
            ),
5505
            new Field(
5506
                [
5507
                    'id' => 0,
5508
                    'value' => 'Bar',
5509
                    'languageCode' => self::ENG_GB,
5510
                    'fieldDefIdentifier' => 'description',
5511
                    'fieldTypeIdentifier' => 'ezrichtext',
5512
                ]
5513
            ),
5514
            new Field(
5515
                [
5516
                    'id' => 0,
5517
                    'value' => 'An awesome multi-lang forum²',
5518
                    'languageCode' => self::ENG_US,
5519
                    'fieldDefIdentifier' => 'name',
5520
                    'fieldTypeIdentifier' => 'ezstring',
5521
                ]
5522
            ),
5523
            new Field(
5524
                [
5525
                    'id' => 0,
5526
                    'value' => 'An awesome multi-lang forum²³',
5527
                    'languageCode' => self::ENG_GB,
5528
                    'fieldDefIdentifier' => 'name',
5529
                    'fieldTypeIdentifier' => 'ezstring',
5530
                ]
5531
            ),
5532
        ];
5533
    }
5534
5535
    /**
5536
     * Gets expected property values for the "Media" ContentInfo ValueObject.
5537
     *
5538
     * @return array
5539
     */
5540
    private function getExpectedMediaContentInfoProperties()
5541
    {
5542
        return [
5543
            'id' => self::MEDIA_CONTENT_ID,
5544
            'contentTypeId' => 1,
5545
            'name' => 'Media',
5546
            'sectionId' => 3,
5547
            'currentVersionNo' => 1,
5548
            'published' => true,
5549
            'ownerId' => 14,
5550
            'modificationDate' => $this->createDateTime(1060695457),
5551
            'publishedDate' => $this->createDateTime(1060695457),
5552
            'alwaysAvailable' => 1,
5553
            'remoteId' => self::MEDIA_REMOTE_ID,
5554
            'mainLanguageCode' => self::ENG_US,
5555
            'mainLocationId' => 43,
5556
            'status' => ContentInfo::STATUS_PUBLISHED,
5557
        ];
5558
    }
5559
5560
    /**
5561
     * @covers \eZ\Publish\API\Repository\ContentService::hideContent
5562
     *
5563
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
5564
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
5565
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException
5566
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
5567
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
5568
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
5569
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5570
     */
5571
    public function testHideContent(): void
5572
    {
5573
        $contentTypeService = $this->getRepository()->getContentTypeService();
5574
5575
        $locationCreateStructs = array_map(
5576
            function (Location $parentLocation) {
5577
                return $this->locationService->newLocationCreateStruct($parentLocation->id);
5578
            },
5579
            $this->createParentLocationsForHideReveal(2)
5580
        );
5581
5582
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
5583
5584
        $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
5585
        $contentCreate->setField('name', 'Folder to hide');
5586
5587
        $content = $this->contentService->createContent(
5588
            $contentCreate,
5589
            $locationCreateStructs
5590
        );
5591
5592
        $publishedContent = $this->contentService->publishVersion($content->versionInfo);
5593
        $locations = $this->locationService->loadLocations($publishedContent->contentInfo);
5594
5595
        // Sanity check
5596
        $this->assertCount(3, $locations);
5597
        $this->assertCount(0, $this->filterHiddenLocations($locations));
5598
5599
        $this->contentService->hideContent($publishedContent->contentInfo);
5600
5601
        $locations = $this->locationService->loadLocations($publishedContent->contentInfo);
5602
        $this->assertCount(3, $locations);
5603
        $this->assertCount(3, $this->filterHiddenLocations($locations));
5604
    }
5605
5606
    /**
5607
     * @covers \eZ\Publish\API\Repository\ContentService::revealContent
5608
     *
5609
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
5610
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
5611
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5612
     */
5613
    public function testRevealContent()
5614
    {
5615
        $contentTypeService = $this->getRepository()->getContentTypeService();
5616
5617
        $locationCreateStructs = array_map(
5618
            function (Location $parentLocation) {
5619
                return $this->locationService->newLocationCreateStruct($parentLocation->id);
5620
            },
5621
            $this->createParentLocationsForHideReveal(2)
5622
        );
5623
5624
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
5625
5626
        $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
5627
        $contentCreate->setField('name', 'Folder to hide');
5628
5629
        $locationCreateStructs[0]->hidden = true;
5630
5631
        $content = $this->contentService->createContent(
5632
            $contentCreate,
5633
            $locationCreateStructs
5634
        );
5635
5636
        $publishedContent = $this->contentService->publishVersion($content->versionInfo);
5637
        $locations = $this->locationService->loadLocations($publishedContent->contentInfo);
5638
5639
        // Sanity check
5640
        $hiddenLocations = $this->filterHiddenLocations($locations);
5641
        $this->assertCount(3, $locations);
5642
        $this->assertCount(1, $hiddenLocations);
5643
5644
        // BEGIN: Use Case
5645
        $this->contentService->hideContent($publishedContent->contentInfo);
5646
        $this->assertCount(
5647
            3,
5648
            $this->filterHiddenLocations(
5649
                $this->locationService->loadLocations($publishedContent->contentInfo)
5650
            )
5651
        );
5652
5653
        $this->contentService->revealContent($publishedContent->contentInfo);
5654
        // END: Use Case
5655
5656
        $locations = $this->locationService->loadLocations($publishedContent->contentInfo);
5657
        $hiddenLocationsAfterReveal = $this->filterHiddenLocations($locations);
5658
        $this->assertCount(3, $locations);
5659
        $this->assertCount(1, $hiddenLocationsAfterReveal);
5660
        $this->assertEquals($hiddenLocations, $hiddenLocationsAfterReveal);
5661
    }
5662
5663
    /**
5664
     * @depends testRevealContent
5665
     */
5666
    public function testRevealContentWithHiddenParent()
5667
    {
5668
        $contentTypeService = $this->getRepository()->getContentTypeService();
5669
5670
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
5671
5672
        $contentNames = [
5673
            'Parent Content',
5674
            'Child (Nesting 1)',
5675
            'Child (Nesting 2)',
5676
            'Child (Nesting 3)',
5677
            'Child (Nesting 4)',
5678
        ];
5679
5680
        $parentLocation = $this->locationService->newLocationCreateStruct(
5681
            $this->generateId('location', 2)
5682
        );
5683
5684
        /** @var Content[] $contents */
5685
        $contents = [];
5686
5687
        foreach ($contentNames as $contentName) {
5688
            $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
5689
            $contentCreate->setField('name', $contentName);
5690
5691
            $content = $this->contentService->createContent($contentCreate, [$parentLocation]);
5692
            $contents[] = $publishedContent = $this->contentService->publishVersion($content->versionInfo);
5693
5694
            $parentLocation = $this->locationService->newLocationCreateStruct(
5695
                $this->generateId('location', $publishedContent->contentInfo->mainLocationId)
5696
            );
5697
        }
5698
5699
        $this->contentService->hideContent($contents[0]->contentInfo);
5700
        $this->contentService->hideContent($contents[2]->contentInfo);
5701
        $this->contentService->revealContent($contents[2]->contentInfo);
5702
5703
        $parentContent = $this->contentService->loadContent($contents[0]->id);
5704
        $parentLocation = $this->locationService->loadLocation($parentContent->contentInfo->mainLocationId);
5705
        $parentSublocations = $this->locationService->loadLocationList([
5706
            $contents[1]->contentInfo->mainLocationId,
5707
            $contents[2]->contentInfo->mainLocationId,
5708
            $contents[3]->contentInfo->mainLocationId,
5709
            $contents[4]->contentInfo->mainLocationId,
5710
        ]);
5711
5712
        // Parent remains invisible
5713
        self::assertTrue($parentLocation->invisible);
5714
5715
        // All parent sublocations remain invisible as well
5716
        foreach ($parentSublocations as $parentSublocation) {
5717
            self::assertTrue($parentSublocation->invisible);
5718
        }
5719
    }
5720
5721
    /**
5722
     * @depends testRevealContent
5723
     */
5724
    public function testRevealContentWithHiddenChildren()
5725
    {
5726
        $contentTypeService = $this->getRepository()->getContentTypeService();
5727
5728
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
5729
5730
        $contentNames = [
5731
            'Parent Content',
5732
            'Child (Nesting 1)',
5733
            'Child (Nesting 2)',
5734
            'Child (Nesting 3)',
5735
            'Child (Nesting 4)',
5736
        ];
5737
5738
        $parentLocation = $this->locationService->newLocationCreateStruct(
5739
            $this->generateId('location', 2)
5740
        );
5741
5742
        /** @var Content[] $contents */
5743
        $contents = [];
5744
5745
        foreach ($contentNames as $contentName) {
5746
            $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
5747
            $contentCreate->setField('name', $contentName);
5748
5749
            $content = $this->contentService->createContent($contentCreate, [$parentLocation]);
5750
            $contents[] = $publishedContent = $this->contentService->publishVersion($content->versionInfo);
5751
5752
            $parentLocation = $this->locationService->newLocationCreateStruct(
5753
                $this->generateId('location', $publishedContent->contentInfo->mainLocationId)
5754
            );
5755
        }
5756
5757
        $this->contentService->hideContent($contents[0]->contentInfo);
5758
        $this->contentService->hideContent($contents[2]->contentInfo);
5759
        $this->contentService->revealContent($contents[0]->contentInfo);
5760
5761
        $directChildContent = $this->contentService->loadContent($contents[1]->id);
5762
        $directChildLocation = $this->locationService->loadLocation($directChildContent->contentInfo->mainLocationId);
5763
5764
        $childContent = $this->contentService->loadContent($contents[2]->id);
5765
        $childLocation = $this->locationService->loadLocation($childContent->contentInfo->mainLocationId);
5766
        $childSublocations = $this->locationService->loadLocationList([
5767
            $contents[3]->contentInfo->mainLocationId,
5768
            $contents[4]->contentInfo->mainLocationId,
5769
        ]);
5770
5771
        // Direct child content is not hidden
5772
        self::assertFalse($directChildContent->contentInfo->isHidden);
5773
5774
        // Direct child content location is still invisible
5775
        self::assertFalse($directChildLocation->invisible);
5776
5777
        // Child content is still hidden
5778
        self::assertTrue($childContent->contentInfo->isHidden);
5779
5780
        // Child content location is still invisible
5781
        self::assertTrue($childLocation->invisible);
5782
5783
        // All childs sublocations remain invisible as well
5784
        foreach ($childSublocations as $childSublocation) {
5785
            self::assertTrue($childSublocation->invisible);
5786
        }
5787
    }
5788
5789
    public function testHideContentWithParentLocation()
5790
    {
5791
        $contentTypeService = $this->getRepository()->getContentTypeService();
5792
5793
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
5794
5795
        $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
5796
        $contentCreate->setField('name', 'Parent');
5797
5798
        $content = $this->contentService->createContent(
5799
            $contentCreate,
5800
            [
5801
                $this->locationService->newLocationCreateStruct(
5802
                    $this->generateId('location', 2)
5803
                ),
5804
            ]
5805
        );
5806
5807
        $publishedContent = $this->contentService->publishVersion($content->versionInfo);
5808
5809
        $this->contentService->hideContent($publishedContent->contentInfo);
5810
5811
        $locations = $this->locationService->loadLocations($publishedContent->contentInfo);
5812
5813
        $childContentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
5814
        $childContentCreate->setField('name', 'Child');
5815
5816
        $childContent = $this->contentService->createContent(
5817
            $childContentCreate,
5818
            [
5819
                $this->locationService->newLocationCreateStruct(
5820
                    $locations[0]->id
5821
                ),
5822
            ]
5823
        );
5824
5825
        $publishedChildContent = $this->contentService->publishVersion($childContent->versionInfo);
5826
5827
        $childLocations = $this->locationService->loadLocations($publishedChildContent->contentInfo);
5828
5829
        $this->assertTrue($locations[0]->hidden);
5830
        $this->assertTrue($locations[0]->invisible);
5831
5832
        $this->assertFalse($childLocations[0]->hidden);
5833
        $this->assertTrue($childLocations[0]->invisible);
5834
    }
5835
5836
    public function testChangeContentName()
5837
    {
5838
        $contentDraft = $this->createContentDraft(
5839
            'folder',
5840
            $this->generateId('location', 2),
5841
            [
5842
                'name' => 'Marco',
5843
            ]
5844
        );
5845
5846
        $publishedContent = $this->contentService->publishVersion($contentDraft->versionInfo);
5847
        $contentMetadataUpdateStruct = new ContentMetadataUpdateStruct([
5848
            'name' => 'Polo',
5849
        ]);
5850
        $this->contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
5851
5852
        $updatedContent = $this->contentService->loadContent($publishedContent->id);
5853
5854
        $this->assertEquals('Marco', $publishedContent->contentInfo->name);
5855
        $this->assertEquals('Polo', $updatedContent->contentInfo->name);
5856
    }
5857
5858
    public function testCopyTranslationsFromPublishedToDraft()
5859
    {
5860
        $contentDraft = $this->createContentDraft(
5861
            'folder',
5862
            $this->generateId('location', 2),
5863
            [
5864
                'name' => 'Folder US',
5865
            ]
5866
        );
5867
5868
        $publishedContent = $this->contentService->publishVersion($contentDraft->versionInfo);
5869
5870
        $deDraft = $this->contentService->createContentDraft($publishedContent->contentInfo);
5871
5872
        $contentUpdateStruct = new ContentUpdateStruct([
5873
            'initialLanguageCode' => self::GER_DE,
5874
            'fields' => $contentDraft->getFields(),
5875
        ]);
5876
5877
        $contentUpdateStruct->setField('name', 'Folder GER', self::GER_DE);
5878
5879
        $deContent = $this->contentService->updateContent($deDraft->versionInfo, $contentUpdateStruct);
5880
5881
        $updatedContent = $this->contentService->loadContent($deContent->id, null, $deContent->versionInfo->versionNo);
5882
        $this->assertEquals(
5883
            [
5884
                self::ENG_US => 'Folder US',
5885
                self::GER_DE => 'Folder GER',
5886
            ],
5887
            $updatedContent->fields['name']
5888
        );
5889
5890
        $gbDraft = $this->contentService->createContentDraft($publishedContent->contentInfo);
5891
5892
        $contentUpdateStruct = new ContentUpdateStruct([
5893
            'initialLanguageCode' => self::ENG_GB,
5894
            'fields' => $contentDraft->getFields(),
5895
        ]);
5896
5897
        $contentUpdateStruct->setField('name', 'Folder GB', self::ENG_GB);
5898
5899
        $gbContent = $this->contentService->updateContent($gbDraft->versionInfo, $contentUpdateStruct);
5900
        $this->contentService->publishVersion($gbDraft->versionInfo);
5901
        $updatedContent = $this->contentService->loadContent($gbContent->id, null, $gbContent->versionInfo->versionNo);
5902
        $this->assertEquals(
5903
            [
5904
                self::ENG_US => 'Folder US',
5905
                self::ENG_GB => 'Folder GB',
5906
            ],
5907
            $updatedContent->fields['name']
5908
        );
5909
5910
        $dePublished = $this->contentService->publishVersion($deDraft->versionInfo);
5911
        $this->assertEquals(
5912
            [
5913
                self::ENG_US => 'Folder US',
5914
                self::GER_DE => 'Folder GER',
5915
                self::ENG_GB => 'Folder GB',
5916
            ],
5917
            $dePublished->fields['name']
5918
        );
5919
    }
5920
5921
    /**
5922
     * Create structure of parent folders with Locations to be used for Content hide/reveal tests.
5923
     *
5924
     * @param int $parentLocationId
5925
     *
5926
     * @return \eZ\Publish\API\Repository\Values\Content\Location[] A list of Locations aimed to be parents
5927
     *
5928
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
5929
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
5930
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5931
     */
5932
    private function createParentLocationsForHideReveal(int $parentLocationId): array
5933
    {
5934
        $parentFoldersLocationsIds = [
5935
            $this->createFolder([self::ENG_US => 'P1'], $parentLocationId)->contentInfo->mainLocationId,
5936
            $this->createFolder([self::ENG_US => 'P2'], $parentLocationId)->contentInfo->mainLocationId,
5937
            $this->createFolder([self::ENG_US => 'P3'], $parentLocationId)->contentInfo->mainLocationId,
5938
        ];
5939
5940
        return array_values($this->locationService->loadLocationList($parentFoldersLocationsIds));
5941
    }
5942
5943
    /**
5944
     * Filter Locations list by hidden only.
5945
     *
5946
     * @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
5947
     *
5948
     * @return array
5949
     */
5950
    private function filterHiddenLocations(array $locations): array
5951
    {
5952
        return array_values(
5953
            array_filter(
5954
                $locations,
5955
                function (Location $location) {
5956
                    return $location->hidden;
5957
                }
5958
            )
5959
        );
5960
    }
5961
5962
    public function testPublishVersionWithSelectedLanguages()
5963
    {
5964
        $publishedContent = $this->createFolder(
5965
            [
5966
                self::ENG_US => 'Published US',
5967
                self::GER_DE => 'Published DE',
5968
            ],
5969
            $this->generateId('location', 2)
5970
        );
5971
5972
        $draft = $this->contentService->createContentDraft($publishedContent->contentInfo);
5973
        $contentUpdateStruct = new ContentUpdateStruct([
5974
            'initialLanguageCode' => self::ENG_US,
5975
        ]);
5976
        $contentUpdateStruct->setField('name', 'Draft 1 US', self::ENG_US);
5977
        $contentUpdateStruct->setField('name', 'Draft 1 DE', self::GER_DE);
5978
5979
        $this->contentService->updateContent($draft->versionInfo, $contentUpdateStruct);
5980
5981
        $this->contentService->publishVersion($draft->versionInfo, [self::GER_DE]);
5982
        $content = $this->contentService->loadContent($draft->contentInfo->id);
5983
        $this->assertEquals(
5984
            [
5985
                self::ENG_US => 'Published US',
5986
                self::GER_DE => 'Draft 1 DE',
5987
            ],
5988
            $content->fields['name']
5989
        );
5990
    }
5991
5992
    public function testCreateContentWithRomanianSpecialCharsInTitle()
5993
    {
5994
        $baseName = 'ȘșțȚdfdf';
5995
        $expectedPath = '/SstTdfdf';
5996
5997
        $this->createFolder([self::ENG_US => $baseName], 2);
5998
5999
        $urlAliasService = $this->getRepository()->getURLAliasService();
6000
        $urlAlias = $urlAliasService->lookup($expectedPath);
6001
        $this->assertSame($expectedPath, $urlAlias->path);
6002
    }
6003
}
6004