Completed
Push — EZP-30969-fetch-reverse-relati... ( d71d24...a0ef69 )
by
unknown
137:48 queued 117:08
created

testCountReverseRelationsForUnauthorizedUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
cc 1
nc 1
nop 0
rs 9.7998
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 loadReverseRelationList() method.
3371
     *
3372
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelationList()
3373
     */
3374
    public function testLoadReverseRelationList(): void
3375
    {
3376
        $draft1 = $this->contentService->createContentDraft(
3377
            $this->createFolder([self::ENG_GB => 'Foo'], 2)->contentInfo
3378
        );
3379
        $draft2 = $this->contentService->createContentDraft(
3380
            $this->createFolder([self::ENG_GB => 'Bar'], 2)->contentInfo
3381
        );
3382
        $draft3 = $this->contentService->createContentDraft(
3383
            $this->createFolder([self::ENG_GB => 'Baz'], 2)->contentInfo
3384
        );
3385
3386
        $contentWithReverseRelations = $this->createContentWithReverseRelations([
3387
            $draft1,
3388
            $draft2,
3389
            $draft3,
3390
        ]);
3391
3392
        $contentInfo = $contentWithReverseRelations->content->contentInfo;
3393
3394
        $reverseRelationList = $this->contentService->loadReverseRelationList($contentInfo);
3395
3396
        $this->assertSame(3, $reverseRelationList->totalCount);
3397
        $this->assertEquals(
3398
            $contentWithReverseRelations->reverseRelations[2]->contentInfo,
3399
            $reverseRelationList->items[0]->getRelation()->sourceContentInfo
3400
        );
3401
        $this->assertEquals(
3402
            $contentWithReverseRelations->reverseRelations[1]->contentInfo,
3403
            $reverseRelationList->items[1]->getRelation()->sourceContentInfo
3404
        );
3405
        $this->assertEquals(
3406
            $contentWithReverseRelations->reverseRelations[0]->contentInfo,
3407
            $reverseRelationList->items[2]->getRelation()->sourceContentInfo
3408
        );
3409
    }
3410
3411
    /**
3412
     * Test for the loadReverseRelationList() method.
3413
     *
3414
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelationList()
3415
     */
3416
    public function testLoadReverseRelationListWithPagination(): void
3417
    {
3418
        $draft1 = $this->contentService->createContentDraft(
3419
            $this->createFolder([self::ENG_GB => 'Foo'], 2)->contentInfo
3420
        );
3421
        $draft2 = $this->contentService->createContentDraft(
3422
            $this->createFolder([self::ENG_GB => 'Bar'], 2)->contentInfo
3423
        );
3424
        $draft3 = $this->contentService->createContentDraft(
3425
            $this->createFolder([self::ENG_GB => 'Baz'], 2)->contentInfo
3426
        );
3427
3428
        $contentWithReverseRelations = $this->createContentWithReverseRelations([
3429
            $draft1,
3430
            $draft2,
3431
            $draft3,
3432
        ]);
3433
3434
        $contentInfo = $contentWithReverseRelations->content->contentInfo;
3435
3436
        $reverseRelationPage1 = $this->contentService->loadReverseRelationList($contentInfo, 0, 2);
3437
        $reverseRelationPage2 = $this->contentService->loadReverseRelationList($contentInfo, 2, 2);
3438
        $this->assertSame(3, $reverseRelationPage1->totalCount);
3439
        $this->assertSame(3, $reverseRelationPage2->totalCount);
3440
        $this->assertEquals(
3441
            $contentWithReverseRelations->reverseRelations[2]->contentInfo,
3442
            $reverseRelationPage1->items[0]->getRelation()->sourceContentInfo
3443
        );
3444
        $this->assertEquals(
3445
            $contentWithReverseRelations->reverseRelations[1]->contentInfo,
3446
            $reverseRelationPage1->items[1]->getRelation()->sourceContentInfo
3447
        );
3448
        $this->assertEquals(
3449
            $contentWithReverseRelations->reverseRelations[0]->contentInfo,
3450
            $reverseRelationPage2->items[0]->getRelation()->sourceContentInfo
3451
        );
3452
    }
3453
3454
    /**
3455
     * Test for the loadReverseRelationList() method.
3456
     *
3457
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelationList()
3458
     */
3459
    public function testLoadReverseRelationListSkipsArchivedContent(): void
3460
    {
3461
        $trashService = $this->getRepository()->getTrashService();
3462
3463
        $draft1 = $this->contentService->createContentDraft(
3464
            $this->createFolder([self::ENG_GB => 'Foo'], 2)->contentInfo
3465
        );
3466
        $draft2 = $this->contentService->createContentDraft(
3467
            $this->createFolder([self::ENG_GB => 'Bar'], 2)->contentInfo
3468
        );
3469
        $draft3 = $this->contentService->createContentDraft(
3470
            $this->createFolder([self::ENG_GB => 'Baz'], 2)->contentInfo
3471
        );
3472
3473
        $contentWithReverseRelations = $this->createContentWithReverseRelations([
3474
            $draft1,
3475
            $draft2,
3476
            $draft3,
3477
        ]);
3478
3479
        $locationToTrash = $this->locationService->loadLocation($draft3->contentInfo->mainLocationId);
3480
3481
        // Trashing Content's last Location will change its status to archived, in this case relation from it will not be loaded.
3482
        $trashService->trash($locationToTrash);
3483
3484
        $contentInfo = $contentWithReverseRelations->content->contentInfo;
3485
        $reverseRelationList = $this->contentService->loadReverseRelationList($contentInfo);
3486
3487
        $this->assertSame(2, $reverseRelationList->totalCount);
3488
        $this->assertEquals(
3489
            $contentWithReverseRelations->reverseRelations[1]->contentInfo,
3490
            $reverseRelationList->items[0]->getRelation()->sourceContentInfo
3491
        );
3492
        $this->assertEquals(
3493
            $contentWithReverseRelations->reverseRelations[0]->contentInfo,
3494
            $reverseRelationList->items[1]->getRelation()->sourceContentInfo
3495
        );
3496
    }
3497
3498
    /**
3499
     * Test for the loadReverseRelationList() method.
3500
     *
3501
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelationList()
3502
     */
3503
    public function testLoadReverseRelationListSkipsDraftContent()
3504
    {
3505
        $draft1 = $this->contentService->createContentDraft(
3506
            $this->createFolder([self::ENG_GB => 'Foo'], 2)->contentInfo
3507
        );
3508
3509
        $contentWithReverseRelations = $this->createContentWithReverseRelations([$draft1]);
3510
3511
        $contentInfo = $contentWithReverseRelations->content->contentInfo;
3512
3513
        // create a relation, but without publishing it
3514
        $draft2 = $this->contentService->createContentDraft(
3515
            $this->createFolder([self::ENG_GB => 'Bar'], 2)->contentInfo
3516
        );
3517
        $this->contentService->addRelation(
3518
            $draft2->getVersionInfo(),
3519
            $contentInfo
3520
        );
3521
3522
        $reverseRelationList = $this->contentService->loadReverseRelationList($contentInfo);
3523
3524
        $this->assertSame(1, $reverseRelationList->totalCount);
3525
        $this->assertEquals(
3526
            $contentWithReverseRelations->reverseRelations[0]->contentInfo,
3527
            $reverseRelationList->items[0]->getRelation()->sourceContentInfo
3528
        );
3529
    }
3530
3531
    /**
3532
     * Test for the loadReverseRelations() method.
3533
     *
3534
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3535
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3536
     */
3537
    public function testLoadReverseRelations()
3538
    {
3539
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3540
        $contentInfo = $versionInfo->getContentInfo();
3541
3542
        // Create some drafts
3543
        $mediaDraft = $this->contentService->createContentDraft(
3544
            $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID)
3545
        );
3546
        $demoDesignDraft = $this->contentService->createContentDraft(
3547
            $this->contentService->loadContentInfoByRemoteId(self::DEMO_DESIGN_REMOTE_ID)
3548
        );
3549
3550
        // Create relation between new content object and "Media" page
3551
        $relation1 = $this->contentService->addRelation(
3552
            $mediaDraft->getVersionInfo(),
3553
            $contentInfo
3554
        );
3555
3556
        // Create another relation with the "Demo Design" page
3557
        $relation2 = $this->contentService->addRelation(
3558
            $demoDesignDraft->getVersionInfo(),
3559
            $contentInfo
3560
        );
3561
3562
        // Publish drafts, so relations become active
3563
        $this->contentService->publishVersion($mediaDraft->getVersionInfo());
3564
        $this->contentService->publishVersion($demoDesignDraft->getVersionInfo());
3565
3566
        // Load all relations
3567
        $relations = $this->contentService->loadRelations($versionInfo);
3568
        $reverseRelations = $this->contentService->loadReverseRelations($contentInfo);
3569
3570
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3571
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3572
3573
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3574
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3575
3576
        $this->assertEquals(0, count($relations));
3577
        $this->assertEquals(2, count($reverseRelations));
3578
3579
        usort(
3580
            $reverseRelations,
3581
            function ($rel1, $rel2) {
3582
                return strcasecmp(
3583
                    $rel2->getSourceContentInfo()->remoteId,
3584
                    $rel1->getSourceContentInfo()->remoteId
3585
                );
3586
            }
3587
        );
3588
3589
        $this->assertEquals(
3590
            [
3591
                [
3592
                    'sourceContentInfo' => self::MEDIA_REMOTE_ID,
3593
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3594
                ],
3595
                [
3596
                    'sourceContentInfo' => self::DEMO_DESIGN_REMOTE_ID,
3597
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3598
                ],
3599
            ],
3600
            [
3601
                [
3602
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3603
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3604
                ],
3605
                [
3606
                    'sourceContentInfo' => $reverseRelations[1]->sourceContentInfo->remoteId,
3607
                    'destinationContentInfo' => $reverseRelations[1]->destinationContentInfo->remoteId,
3608
                ],
3609
            ]
3610
        );
3611
    }
3612
3613
    /**
3614
     * Test for the loadReverseRelations() method.
3615
     *
3616
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3617
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3618
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3619
     */
3620
    public function testLoadReverseRelationsSkipsArchivedContent()
3621
    {
3622
        $trashService = $this->getRepository()->getTrashService();
3623
3624
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3625
        $contentInfo = $versionInfo->getContentInfo();
3626
3627
        // Create some drafts
3628
        $mediaDraft = $this->contentService->createContentDraft(
3629
            $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID)
3630
        );
3631
        $demoDesignDraft = $this->contentService->createContentDraft(
3632
            $this->contentService->loadContentInfoByRemoteId(self::DEMO_DESIGN_REMOTE_ID)
3633
        );
3634
3635
        // Create relation between new content object and "Media" page
3636
        $relation1 = $this->contentService->addRelation(
3637
            $mediaDraft->getVersionInfo(),
3638
            $contentInfo
3639
        );
3640
3641
        // Create another relation with the "Demo Design" page
3642
        $relation2 = $this->contentService->addRelation(
3643
            $demoDesignDraft->getVersionInfo(),
3644
            $contentInfo
3645
        );
3646
3647
        // Publish drafts, so relations become active
3648
        $this->contentService->publishVersion($mediaDraft->getVersionInfo());
3649
        $this->contentService->publishVersion($demoDesignDraft->getVersionInfo());
3650
3651
        $demoDesignLocation = $this->locationService->loadLocation($demoDesignDraft->contentInfo->mainLocationId);
3652
3653
        // Trashing Content's last Location will change its status to archived,
3654
        // in this case relation from it will not be loaded.
3655
        $trashService->trash($demoDesignLocation);
3656
3657
        // Load all relations
3658
        $relations = $this->contentService->loadRelations($versionInfo);
3659
        $reverseRelations = $this->contentService->loadReverseRelations($contentInfo);
3660
3661
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3662
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3663
3664
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3665
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3666
3667
        $this->assertEquals(0, count($relations));
3668
        $this->assertEquals(1, count($reverseRelations));
3669
3670
        $this->assertEquals(
3671
            [
3672
                [
3673
                    'sourceContentInfo' => self::MEDIA_REMOTE_ID,
3674
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3675
                ],
3676
            ],
3677
            [
3678
                [
3679
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3680
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3681
                ],
3682
            ]
3683
        );
3684
    }
3685
3686
    /**
3687
     * Test for the loadReverseRelations() method.
3688
     *
3689
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3690
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3691
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3692
     */
3693
    public function testLoadReverseRelationsSkipsDraftContent()
3694
    {
3695
        // Load "Media" page Content
3696
        $media = $this->contentService->loadContentByRemoteId(self::MEDIA_REMOTE_ID);
3697
3698
        // Create some drafts
3699
        $newDraftVersionInfo = $this->createContentDraftVersion1()->getVersionInfo();
3700
        $demoDesignDraft = $this->contentService->createContentDraft(
3701
            $this->contentService->loadContentInfoByRemoteId(self::DEMO_DESIGN_REMOTE_ID)
3702
        );
3703
3704
        // Create relation between "Media" page and new content object
3705
        $relation1 = $this->contentService->addRelation(
3706
            $newDraftVersionInfo,
3707
            $media->contentInfo
3708
        );
3709
3710
        // Create another relation with the "Demo Design" page
3711
        $relation2 = $this->contentService->addRelation(
3712
            $demoDesignDraft->getVersionInfo(),
3713
            $media->contentInfo
3714
        );
3715
3716
        // Publish drafts, so relations become active
3717
        $this->contentService->publishVersion($demoDesignDraft->getVersionInfo());
3718
        // We will not publish new Content draft, therefore relation from it
3719
        // will not be loaded as reverse relation for "Media" page
3720
3721
        // Load all relations
3722
        $relations = $this->contentService->loadRelations($media->versionInfo);
3723
        $reverseRelations = $this->contentService->loadReverseRelations($media->contentInfo);
3724
3725
        $this->assertEquals($media->contentInfo->id, $relation1->getDestinationContentInfo()->id);
3726
        $this->assertEquals($newDraftVersionInfo->contentInfo->id, $relation1->getSourceContentInfo()->id);
3727
3728
        $this->assertEquals($media->contentInfo->id, $relation2->getDestinationContentInfo()->id);
3729
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3730
3731
        $this->assertEquals(0, count($relations));
3732
        $this->assertEquals(1, count($reverseRelations));
3733
3734
        $this->assertEquals(
3735
            [
3736
                [
3737
                    'sourceContentInfo' => self::DEMO_DESIGN_REMOTE_ID,
3738
                    'destinationContentInfo' => self::MEDIA_REMOTE_ID,
3739
                ],
3740
            ],
3741
            [
3742
                [
3743
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3744
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3745
                ],
3746
            ]
3747
        );
3748
    }
3749
3750
    /**
3751
     * Test for the deleteRelation() method.
3752
     *
3753
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3754
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3755
     */
3756
    public function testDeleteRelation()
3757
    {
3758
        $draft = $this->createContentDraftVersion1();
3759
3760
        $media = $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID);
3761
        $demoDesign = $this->contentService->loadContentInfoByRemoteId(self::DEMO_DESIGN_REMOTE_ID);
3762
3763
        // Establish some relations
3764
        $this->contentService->addRelation($draft->getVersionInfo(), $media);
3765
        $this->contentService->addRelation($draft->getVersionInfo(), $demoDesign);
3766
3767
        // Delete one of the currently created relations
3768
        $this->contentService->deleteRelation($draft->getVersionInfo(), $media);
3769
3770
        // The relations array now contains only one element
3771
        $relations = $this->contentService->loadRelations($draft->getVersionInfo());
3772
3773
        $this->assertEquals(1, count($relations));
3774
    }
3775
3776
    /**
3777
     * Test for the deleteRelation() method.
3778
     *
3779
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3780
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3781
     */
3782
    public function testDeleteRelationThrowsBadStateException()
3783
    {
3784
        $content = $this->createContentVersion1();
3785
3786
        // Load the destination object
3787
        $media = $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID);
3788
3789
        // Create a new draft
3790
        $draftVersion2 = $this->contentService->createContentDraft($content->contentInfo);
3791
3792
        // Add a relation
3793
        $this->contentService->addRelation($draftVersion2->getVersionInfo(), $media);
3794
3795
        // Publish new version
3796
        $contentVersion2 = $this->contentService->publishVersion(
3797
            $draftVersion2->getVersionInfo()
3798
        );
3799
3800
        $this->expectException(BadStateException::class);
3801
3802
        // This call will fail with a "BadStateException", because content is published and not a draft.
3803
        $this->contentService->deleteRelation(
3804
            $contentVersion2->getVersionInfo(),
3805
            $media
3806
        );
3807
    }
3808
3809
    /**
3810
     * Test for the deleteRelation() method.
3811
     *
3812
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3813
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3814
     */
3815
    public function testDeleteRelationThrowsInvalidArgumentException()
3816
    {
3817
        $draft = $this->createContentDraftVersion1();
3818
3819
        // Load the destination object
3820
        $media = $this->contentService->loadContentInfoByRemoteId(self::MEDIA_REMOTE_ID);
3821
3822
        // This call will fail with a "InvalidArgumentException", because no relation exists between $draft and $media.
3823
        $this->expectException(APIInvalidArgumentException::class);
3824
        $this->contentService->deleteRelation(
3825
            $draft->getVersionInfo(),
3826
            $media
3827
        );
3828
    }
3829
3830
    /**
3831
     * Test for the createContent() method.
3832
     *
3833
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
3834
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3835
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3836
     */
3837
    public function testCreateContentInTransactionWithRollback()
3838
    {
3839
        if ($this->isVersion4()) {
3840
            $this->markTestSkipped('This test requires eZ Publish 5');
3841
        }
3842
3843
        $repository = $this->getRepository();
3844
3845
        $contentTypeService = $this->getRepository()->getContentTypeService();
3846
3847
        // Start a transaction
3848
        $repository->beginTransaction();
3849
3850
        try {
3851
            $contentType = $contentTypeService->loadContentTypeByIdentifier(self::FORUM_IDENTIFIER);
3852
3853
            // Get a content create struct and set mandatory properties
3854
            $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
3855
            $contentCreate->setField('name', 'Sindelfingen forum');
3856
3857
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
3858
            $contentCreate->alwaysAvailable = true;
3859
3860
            // Create a new content object
3861
            $contentId = $this->contentService->createContent($contentCreate)->id;
3862
        } catch (Exception $e) {
3863
            // Cleanup hanging transaction on error
3864
            $repository->rollback();
3865
            throw $e;
3866
        }
3867
3868
        // Rollback all changes
3869
        $repository->rollback();
3870
3871
        try {
3872
            // This call will fail with a "NotFoundException"
3873
            $this->contentService->loadContent($contentId);
3874
        } catch (NotFoundException $e) {
3875
            // This is expected
3876
            return;
3877
        }
3878
3879
        $this->fail('Content object still exists after rollback.');
3880
    }
3881
3882
    /**
3883
     * Test for the createContent() method.
3884
     *
3885
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
3886
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3887
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3888
     */
3889
    public function testCreateContentInTransactionWithCommit()
3890
    {
3891
        if ($this->isVersion4()) {
3892
            $this->markTestSkipped('This test requires eZ Publish 5');
3893
        }
3894
3895
        $repository = $this->getRepository();
3896
3897
        $contentTypeService = $repository->getContentTypeService();
3898
3899
        // Start a transaction
3900
        $repository->beginTransaction();
3901
3902
        try {
3903
            $contentType = $contentTypeService->loadContentTypeByIdentifier(self::FORUM_IDENTIFIER);
3904
3905
            // Get a content create struct and set mandatory properties
3906
            $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
3907
            $contentCreate->setField('name', 'Sindelfingen forum');
3908
3909
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
3910
            $contentCreate->alwaysAvailable = true;
3911
3912
            // Create a new content object
3913
            $contentId = $this->contentService->createContent($contentCreate)->id;
3914
3915
            // Commit changes
3916
            $repository->commit();
3917
        } catch (Exception $e) {
3918
            // Cleanup hanging transaction on error
3919
            $repository->rollback();
3920
            throw $e;
3921
        }
3922
3923
        // Load the new content object
3924
        $content = $this->contentService->loadContent($contentId);
3925
3926
        $this->assertEquals($contentId, $content->id);
3927
    }
3928
3929
    /**
3930
     * Test for the createContent() method.
3931
     *
3932
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
3933
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
3934
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
3935
     */
3936
    public function testCreateContentWithLocationCreateParameterInTransactionWithRollback()
3937
    {
3938
        $repository = $this->getRepository();
3939
3940
        // Start a transaction
3941
        $repository->beginTransaction();
3942
3943
        try {
3944
            $draft = $this->createContentDraftVersion1();
3945
        } catch (Exception $e) {
3946
            // Cleanup hanging transaction on error
3947
            $repository->rollback();
3948
            throw $e;
3949
        }
3950
3951
        $contentId = $draft->id;
3952
3953
        // Roleback the transaction
3954
        $repository->rollback();
3955
3956
        try {
3957
            // This call will fail with a "NotFoundException"
3958
            $this->contentService->loadContent($contentId);
3959
        } catch (NotFoundException $e) {
3960
            return;
3961
        }
3962
3963
        $this->fail('Can still load content object after rollback.');
3964
    }
3965
3966
    /**
3967
     * Test for the createContent() method.
3968
     *
3969
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
3970
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
3971
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
3972
     */
3973
    public function testCreateContentWithLocationCreateParameterInTransactionWithCommit()
3974
    {
3975
        $repository = $this->getRepository();
3976
3977
        // Start a transaction
3978
        $repository->beginTransaction();
3979
3980
        try {
3981
            $draft = $this->createContentDraftVersion1();
3982
3983
            $contentId = $draft->id;
3984
3985
            // Roleback the transaction
3986
            $repository->commit();
3987
        } catch (Exception $e) {
3988
            // Cleanup hanging transaction on error
3989
            $repository->rollback();
3990
            throw $e;
3991
        }
3992
3993
        // Load the new content object
3994
        $content = $this->contentService->loadContent($contentId);
3995
3996
        $this->assertEquals($contentId, $content->id);
3997
    }
3998
3999
    /**
4000
     * Test for the createContentDraft() method.
4001
     *
4002
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4003
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4004
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4005
     */
4006
    public function testCreateContentDraftInTransactionWithRollback()
4007
    {
4008
        $repository = $this->getRepository();
4009
4010
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
4011
4012
        // Load the user group content object
4013
        $content = $this->contentService->loadContent($contentId);
4014
4015
        // Start a new transaction
4016
        $repository->beginTransaction();
4017
4018
        try {
4019
            // Create a new draft
4020
            $drafted = $this->contentService->createContentDraft($content->contentInfo);
4021
4022
            // Store version number for later reuse
4023
            $versionNo = $drafted->versionInfo->versionNo;
4024
        } catch (Exception $e) {
4025
            // Cleanup hanging transaction on error
4026
            $repository->rollback();
4027
            throw $e;
4028
        }
4029
4030
        // Rollback
4031
        $repository->rollback();
4032
4033
        try {
4034
            // This call will fail with a "NotFoundException"
4035
            $this->contentService->loadContent($contentId, null, $versionNo);
4036
        } catch (NotFoundException $e) {
4037
            return;
4038
        }
4039
4040
        $this->fail('Can still load content draft after rollback');
4041
    }
4042
4043
    /**
4044
     * Test for the createContentDraft() method.
4045
     *
4046
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4047
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4048
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4049
     */
4050
    public function testCreateContentDraftInTransactionWithCommit()
4051
    {
4052
        $repository = $this->getRepository();
4053
4054
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
4055
4056
        // Load the user group content object
4057
        $content = $this->contentService->loadContent($contentId);
4058
4059
        // Start a new transaction
4060
        $repository->beginTransaction();
4061
4062
        try {
4063
            // Create a new draft
4064
            $drafted = $this->contentService->createContentDraft($content->contentInfo);
4065
4066
            // Store version number for later reuse
4067
            $versionNo = $drafted->versionInfo->versionNo;
4068
4069
            // Commit all changes
4070
            $repository->commit();
4071
        } catch (Exception $e) {
4072
            // Cleanup hanging transaction on error
4073
            $repository->rollback();
4074
            throw $e;
4075
        }
4076
4077
        $content = $this->contentService->loadContent($contentId, null, $versionNo);
4078
4079
        $this->assertEquals(
4080
            $versionNo,
4081
            $content->getVersionInfo()->versionNo
4082
        );
4083
    }
4084
4085
    /**
4086
     * Test for the publishVersion() method.
4087
     *
4088
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4089
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4090
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4091
     */
4092
    public function testPublishVersionInTransactionWithRollback()
4093
    {
4094
        $repository = $this->getRepository();
4095
4096
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
4097
4098
        // Load the user group content object
4099
        $content = $this->contentService->loadContent($contentId);
4100
4101
        // Start a new transaction
4102
        $repository->beginTransaction();
4103
4104
        try {
4105
            $draftVersion = $this->contentService->createContentDraft($content->contentInfo)->getVersionInfo();
4106
4107
            // Publish a new version
4108
            $content = $this->contentService->publishVersion($draftVersion);
4109
4110
            // Store version number for later reuse
4111
            $versionNo = $content->versionInfo->versionNo;
4112
        } catch (Exception $e) {
4113
            // Cleanup hanging transaction on error
4114
            $repository->rollback();
4115
            throw $e;
4116
        }
4117
4118
        // Rollback
4119
        $repository->rollback();
4120
4121
        try {
4122
            // This call will fail with a "NotFoundException"
4123
            $this->contentService->loadContent($contentId, null, $versionNo);
4124
        } catch (NotFoundException $e) {
4125
            return;
4126
        }
4127
4128
        $this->fail('Can still load content draft after rollback');
4129
    }
4130
4131
    /**
4132
     * Test for the publishVersion() method.
4133
     *
4134
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4135
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4136
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
4137
     */
4138
    public function testPublishVersionInTransactionWithCommit()
4139
    {
4140
        $repository = $this->getRepository();
4141
4142
        // Load the user group content object
4143
        $template = $this->contentService->loadContent(self::ADMINISTRATORS_USER_GROUP_ID);
4144
4145
        // Start a new transaction
4146
        $repository->beginTransaction();
4147
4148
        try {
4149
            // Publish a new version
4150
            $content = $this->contentService->publishVersion(
4151
                $this->contentService->createContentDraft($template->contentInfo)->getVersionInfo()
4152
            );
4153
4154
            // Store version number for later reuse
4155
            $versionNo = $content->versionInfo->versionNo;
4156
4157
            // Commit all changes
4158
            $repository->commit();
4159
        } catch (Exception $e) {
4160
            // Cleanup hanging transaction on error
4161
            $repository->rollback();
4162
            throw $e;
4163
        }
4164
4165
        // Load current version info
4166
        $versionInfo = $this->contentService->loadVersionInfo($content->contentInfo);
4167
4168
        $this->assertEquals($versionNo, $versionInfo->versionNo);
4169
    }
4170
4171
    /**
4172
     * Test for the updateContent() method.
4173
     *
4174
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4175
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4176
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4177
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4178
     */
4179
    public function testUpdateContentInTransactionWithRollback()
4180
    {
4181
        $repository = $this->getRepository();
4182
4183
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
4184
4185
        // Create a new user group draft
4186
        $draft = $this->contentService->createContentDraft(
4187
            $this->contentService->loadContentInfo($contentId)
4188
        );
4189
4190
        // Get an update struct and change the group name
4191
        $contentUpdate = $this->contentService->newContentUpdateStruct();
4192
        $contentUpdate->setField('name', self::ADMINISTRATORS_USER_GROUP_NAME, self::ENG_US);
4193
4194
        // Start a transaction
4195
        $repository->beginTransaction();
4196
4197
        try {
4198
            // Update the group name
4199
            $draft = $this->contentService->updateContent(
4200
                $draft->getVersionInfo(),
4201
                $contentUpdate
4202
            );
4203
4204
            // Publish updated version
4205
            $this->contentService->publishVersion($draft->getVersionInfo());
4206
        } catch (Exception $e) {
4207
            // Cleanup hanging transaction on error
4208
            $repository->rollback();
4209
            throw $e;
4210
        }
4211
4212
        // Rollback all changes.
4213
        $repository->rollback();
4214
4215
        // Name will still be "Administrator users"
4216
        $name = $this->contentService->loadContent($contentId)->getFieldValue('name');
4217
4218
        $this->assertEquals('Administrator users', $name);
4219
    }
4220
4221
    /**
4222
     * Test for the updateContent() method.
4223
     *
4224
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4225
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4226
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4227
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4228
     */
4229
    public function testUpdateContentInTransactionWithCommit()
4230
    {
4231
        $repository = $this->getRepository();
4232
4233
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
4234
4235
        // Create a new user group draft
4236
        $draft = $this->contentService->createContentDraft(
4237
            $this->contentService->loadContentInfo($contentId)
4238
        );
4239
4240
        // Get an update struct and change the group name
4241
        $contentUpdate = $this->contentService->newContentUpdateStruct();
4242
        $contentUpdate->setField('name', self::ADMINISTRATORS_USER_GROUP_NAME, self::ENG_US);
4243
4244
        // Start a transaction
4245
        $repository->beginTransaction();
4246
4247
        try {
4248
            // Update the group name
4249
            $draft = $this->contentService->updateContent(
4250
                $draft->getVersionInfo(),
4251
                $contentUpdate
4252
            );
4253
4254
            // Publish updated version
4255
            $this->contentService->publishVersion($draft->getVersionInfo());
4256
4257
            // Commit all changes.
4258
            $repository->commit();
4259
        } catch (Exception $e) {
4260
            // Cleanup hanging transaction on error
4261
            $repository->rollback();
4262
            throw $e;
4263
        }
4264
4265
        // Name is now "Administrators"
4266
        $name = $this->contentService->loadContent($contentId)->getFieldValue('name', self::ENG_US);
4267
4268
        $this->assertEquals(self::ADMINISTRATORS_USER_GROUP_NAME, $name);
4269
    }
4270
4271
    /**
4272
     * Test for the updateContentMetadata() method.
4273
     *
4274
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4275
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4276
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4277
     */
4278
    public function testUpdateContentMetadataInTransactionWithRollback()
4279
    {
4280
        $repository = $this->getRepository();
4281
4282
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
4283
4284
        // Load a ContentInfo object
4285
        $contentInfo = $this->contentService->loadContentInfo($contentId);
4286
4287
        // Store remoteId for later testing
4288
        $remoteId = $contentInfo->remoteId;
4289
4290
        // Start a transaction
4291
        $repository->beginTransaction();
4292
4293
        try {
4294
            // Get metadata update struct and change remoteId
4295
            $metadataUpdate = $this->contentService->newContentMetadataUpdateStruct();
4296
            $metadataUpdate->remoteId = md5(microtime(true));
4297
4298
            // Update the metadata of the published content object
4299
            $this->contentService->updateContentMetadata(
4300
                $contentInfo,
4301
                $metadataUpdate
4302
            );
4303
        } catch (Exception $e) {
4304
            // Cleanup hanging transaction on error
4305
            $repository->rollback();
4306
            throw $e;
4307
        }
4308
4309
        // Rollback all changes.
4310
        $repository->rollback();
4311
4312
        // Load current remoteId
4313
        $remoteIdReloaded = $this->contentService->loadContentInfo($contentId)->remoteId;
4314
4315
        $this->assertEquals($remoteId, $remoteIdReloaded);
4316
    }
4317
4318
    /**
4319
     * Test for the updateContentMetadata() method.
4320
     *
4321
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4322
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4323
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4324
     */
4325
    public function testUpdateContentMetadataInTransactionWithCommit()
4326
    {
4327
        $repository = $this->getRepository();
4328
4329
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
4330
4331
        // Load a ContentInfo object
4332
        $contentInfo = $this->contentService->loadContentInfo($contentId);
4333
4334
        // Store remoteId for later testing
4335
        $remoteId = $contentInfo->remoteId;
4336
4337
        // Start a transaction
4338
        $repository->beginTransaction();
4339
4340
        try {
4341
            // Get metadata update struct and change remoteId
4342
            $metadataUpdate = $this->contentService->newContentMetadataUpdateStruct();
4343
            $metadataUpdate->remoteId = md5(microtime(true));
4344
4345
            // Update the metadata of the published content object
4346
            $this->contentService->updateContentMetadata(
4347
                $contentInfo,
4348
                $metadataUpdate
4349
            );
4350
4351
            // Commit all changes.
4352
            $repository->commit();
4353
        } catch (Exception $e) {
4354
            // Cleanup hanging transaction on error
4355
            $repository->rollback();
4356
            throw $e;
4357
        }
4358
4359
        // Load current remoteId
4360
        $remoteIdReloaded = $this->contentService->loadContentInfo($contentId)->remoteId;
4361
4362
        $this->assertNotEquals($remoteId, $remoteIdReloaded);
4363
    }
4364
4365
    /**
4366
     * Test for the deleteVersion() method.
4367
     *
4368
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4369
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4370
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4371
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4372
     */
4373
    public function testDeleteVersionInTransactionWithRollback()
4374
    {
4375
        $repository = $this->getRepository();
4376
4377
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
4378
4379
        // Start a new transaction
4380
        $repository->beginTransaction();
4381
4382
        try {
4383
            // Create a new draft
4384
            $draft = $this->contentService->createContentDraft(
4385
                $this->contentService->loadContentInfo($contentId)
4386
            );
4387
4388
            $this->contentService->deleteVersion($draft->getVersionInfo());
4389
        } catch (Exception $e) {
4390
            // Cleanup hanging transaction on error
4391
            $repository->rollback();
4392
            throw $e;
4393
        }
4394
4395
        // Rollback all changes.
4396
        $repository->rollback();
4397
4398
        // This array will be empty
4399
        $drafts = $this->contentService->loadContentDrafts();
4400
4401
        $this->assertSame([], $drafts);
4402
    }
4403
4404
    /**
4405
     * Test for the deleteVersion() method.
4406
     *
4407
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4408
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4409
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4410
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4411
     */
4412
    public function testDeleteVersionInTransactionWithCommit()
4413
    {
4414
        $repository = $this->getRepository();
4415
4416
        $contentId = $this->generateId('object', self::ADMINISTRATORS_USER_GROUP_ID);
4417
4418
        // Start a new transaction
4419
        $repository->beginTransaction();
4420
4421
        try {
4422
            // Create a new draft
4423
            $draft = $this->contentService->createContentDraft(
4424
                $this->contentService->loadContentInfo($contentId)
4425
            );
4426
4427
            $this->contentService->deleteVersion($draft->getVersionInfo());
4428
4429
            // Commit all changes.
4430
            $repository->commit();
4431
        } catch (Exception $e) {
4432
            // Cleanup hanging transaction on error
4433
            $repository->rollback();
4434
            throw $e;
4435
        }
4436
4437
        // This array will contain no element
4438
        $drafts = $this->contentService->loadContentDrafts();
4439
4440
        $this->assertSame([], $drafts);
4441
    }
4442
4443
    /**
4444
     * Test for the deleteContent() method.
4445
     *
4446
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4447
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4448
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4449
     */
4450
    public function testDeleteContentInTransactionWithRollback()
4451
    {
4452
        $repository = $this->getRepository();
4453
4454
        $contentId = $this->generateId('object', self::MEMBERS_USER_GROUP_ID);
4455
4456
        // Load a ContentInfo instance
4457
        $contentInfo = $this->contentService->loadContentInfo($contentId);
4458
4459
        // Start a new transaction
4460
        $repository->beginTransaction();
4461
4462
        try {
4463
            // Delete content object
4464
            $this->contentService->deleteContent($contentInfo);
4465
        } catch (Exception $e) {
4466
            // Cleanup hanging transaction on error
4467
            $repository->rollback();
4468
            throw $e;
4469
        }
4470
4471
        // Rollback all changes
4472
        $repository->rollback();
4473
4474
        // This call will return the original content object
4475
        $contentInfo = $this->contentService->loadContentInfo($contentId);
4476
4477
        $this->assertEquals($contentId, $contentInfo->id);
4478
    }
4479
4480
    /**
4481
     * Test for the deleteContent() method.
4482
     *
4483
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4484
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4485
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4486
     */
4487
    public function testDeleteContentInTransactionWithCommit()
4488
    {
4489
        $repository = $this->getRepository();
4490
4491
        $contentId = $this->generateId('object', self::MEMBERS_USER_GROUP_ID);
4492
4493
        // Load a ContentInfo instance
4494
        $contentInfo = $this->contentService->loadContentInfo($contentId);
4495
4496
        // Start a new transaction
4497
        $repository->beginTransaction();
4498
4499
        try {
4500
            // Delete content object
4501
            $this->contentService->deleteContent($contentInfo);
4502
4503
            // Commit all changes
4504
            $repository->commit();
4505
        } catch (Exception $e) {
4506
            // Cleanup hanging transaction on error
4507
            $repository->rollback();
4508
            throw $e;
4509
        }
4510
4511
        // Deleted content info is not found anymore
4512
        try {
4513
            $this->contentService->loadContentInfo($contentId);
4514
        } catch (NotFoundException $e) {
4515
            return;
4516
        }
4517
4518
        $this->fail('Can still load ContentInfo after commit.');
4519
    }
4520
4521
    /**
4522
     * Test for the copyContent() method.
4523
     *
4524
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4525
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4526
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4527
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4528
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4529
     */
4530
    public function testCopyContentInTransactionWithRollback()
4531
    {
4532
        $repository = $this->getRepository();
4533
4534
        $contentId = $this->generateId('object', self::MEMBERS_USER_GROUP_ID);
4535
        $locationId = $this->generateId('location', self::ADMINISTRATORS_USER_GROUP_LOCATION_ID);
4536
4537
        // Load content object to copy
4538
        $content = $this->contentService->loadContent($contentId);
4539
4540
        // Create new target location
4541
        $locationCreate = $this->locationService->newLocationCreateStruct($locationId);
4542
4543
        // Start a new transaction
4544
        $repository->beginTransaction();
4545
4546
        try {
4547
            // Copy content with all versions and drafts
4548
            $this->contentService->copyContent(
4549
                $content->contentInfo,
4550
                $locationCreate
4551
            );
4552
        } catch (Exception $e) {
4553
            // Cleanup hanging transaction on error
4554
            $repository->rollback();
4555
            throw $e;
4556
        }
4557
4558
        // Rollback all changes
4559
        $repository->rollback();
4560
4561
        $this->refreshSearch($repository);
4562
4563
        // This array will only contain a single admin user object
4564
        $locations = $this->locationService->loadLocationChildren(
4565
            $this->locationService->loadLocation($locationId)
4566
        )->locations;
4567
4568
        $this->assertEquals(1, count($locations));
4569
    }
4570
4571
    /**
4572
     * Test for the copyContent() method.
4573
     *
4574
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4575
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4576
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4577
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4578
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4579
     */
4580
    public function testCopyContentInTransactionWithCommit()
4581
    {
4582
        $repository = $this->getRepository();
4583
4584
        $contentId = $this->generateId('object', self::MEMBERS_USER_GROUP_ID);
4585
        $locationId = $this->generateId('location', self::ADMINISTRATORS_USER_GROUP_LOCATION_ID);
4586
4587
        // Load content object to copy
4588
        $content = $this->contentService->loadContent($contentId);
4589
4590
        // Create new target location
4591
        $locationCreate = $this->locationService->newLocationCreateStruct($locationId);
4592
4593
        // Start a new transaction
4594
        $repository->beginTransaction();
4595
4596
        try {
4597
            // Copy content with all versions and drafts
4598
            $this->contentService->copyContent(
4599
                $content->contentInfo,
4600
                $locationCreate
4601
            );
4602
4603
            // Commit all changes
4604
            $repository->commit();
4605
        } catch (Exception $e) {
4606
            // Cleanup hanging transaction on error
4607
            $repository->rollback();
4608
            throw $e;
4609
        }
4610
4611
        $this->refreshSearch($repository);
4612
4613
        // This will contain the admin user and the new child location
4614
        $locations = $this->locationService->loadLocationChildren(
4615
            $this->locationService->loadLocation($locationId)
4616
        )->locations;
4617
4618
        $this->assertEquals(2, count($locations));
4619
    }
4620
4621
    public function testURLAliasesCreatedForNewContent()
4622
    {
4623
        $urlAliasService = $this->getRepository()->getURLAliasService();
4624
4625
        $draft = $this->createContentDraftVersion1();
4626
4627
        // Automatically creates a new URLAlias for the content
4628
        $liveContent = $this->contentService->publishVersion($draft->getVersionInfo());
4629
4630
        $location = $this->locationService->loadLocation(
4631
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4632
        );
4633
4634
        $aliases = $urlAliasService->listLocationAliases($location, false);
4635
4636
        $this->assertAliasesCorrect(
4637
            [
4638
                '/Design/Plain-site/An-awesome-forum' => [
4639
                    'type' => URLAlias::LOCATION,
4640
                    'destination' => $location->id,
4641
                    'path' => '/Design/Plain-site/An-awesome-forum',
4642
                    'languageCodes' => [self::ENG_US],
4643
                    'isHistory' => false,
4644
                    'isCustom' => false,
4645
                    'forward' => false,
4646
                ],
4647
            ],
4648
            $aliases
4649
        );
4650
    }
4651
4652
    public function testURLAliasesCreatedForUpdatedContent()
4653
    {
4654
        $urlAliasService = $this->getRepository()->getURLAliasService();
4655
4656
        $draft = $this->createUpdatedDraftVersion2();
4657
4658
        $location = $this->locationService->loadLocation(
4659
            $draft->getVersionInfo()->getContentInfo()->mainLocationId
4660
        );
4661
4662
        // Load and assert URL aliases before publishing updated Content, so that
4663
        // SPI cache is warmed up and cache invalidation is also tested.
4664
        $aliases = $urlAliasService->listLocationAliases($location, false);
4665
4666
        $this->assertAliasesCorrect(
4667
            [
4668
                '/Design/Plain-site/An-awesome-forum' => [
4669
                    'type' => URLAlias::LOCATION,
4670
                    'destination' => $location->id,
4671
                    'path' => '/Design/Plain-site/An-awesome-forum',
4672
                    'languageCodes' => [self::ENG_US],
4673
                    'alwaysAvailable' => true,
4674
                    'isHistory' => false,
4675
                    'isCustom' => false,
4676
                    'forward' => false,
4677
                ],
4678
            ],
4679
            $aliases
4680
        );
4681
4682
        // Automatically marks old aliases for the content as history
4683
        // and creates new aliases, based on the changes
4684
        $liveContent = $this->contentService->publishVersion($draft->getVersionInfo());
4685
4686
        $location = $this->locationService->loadLocation(
4687
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4688
        );
4689
4690
        $aliases = $urlAliasService->listLocationAliases($location, false);
4691
4692
        $this->assertAliasesCorrect(
4693
            [
4694
                '/Design/Plain-site/An-awesome-forum2' => [
4695
                    'type' => URLAlias::LOCATION,
4696
                    'destination' => $location->id,
4697
                    'path' => '/Design/Plain-site/An-awesome-forum2',
4698
                    'languageCodes' => [self::ENG_US],
4699
                    'alwaysAvailable' => true,
4700
                    'isHistory' => false,
4701
                    'isCustom' => false,
4702
                    'forward' => false,
4703
                ],
4704
                '/Design/Plain-site/An-awesome-forum23' => [
4705
                    'type' => URLAlias::LOCATION,
4706
                    'destination' => $location->id,
4707
                    'path' => '/Design/Plain-site/An-awesome-forum23',
4708
                    'languageCodes' => [self::ENG_GB],
4709
                    'alwaysAvailable' => true,
4710
                    'isHistory' => false,
4711
                    'isCustom' => false,
4712
                    'forward' => false,
4713
                ],
4714
            ],
4715
            $aliases
4716
        );
4717
    }
4718
4719
    public function testCustomURLAliasesNotHistorizedOnUpdatedContent()
4720
    {
4721
        $urlAliasService = $this->getRepository()->getURLAliasService();
4722
4723
        $content = $this->createContentVersion1();
4724
4725
        // Create a custom URL alias
4726
        $urlAliasService->createUrlAlias(
4727
            $this->locationService->loadLocation(
4728
                $content->getVersionInfo()->getContentInfo()->mainLocationId
4729
            ),
4730
            '/my/fancy/story-about-ez-publish',
4731
            self::ENG_US
4732
        );
4733
4734
        $draftVersion2 = $this->contentService->createContentDraft($content->contentInfo);
4735
4736
        $contentUpdate = $this->contentService->newContentUpdateStruct();
4737
        $contentUpdate->initialLanguageCode = self::ENG_US;
4738
        $contentUpdate->setField('name', 'Amazing Bielefeld forum');
4739
4740
        $draftVersion2 = $this->contentService->updateContent(
4741
            $draftVersion2->getVersionInfo(),
4742
            $contentUpdate
4743
        );
4744
4745
        // Only marks auto-generated aliases as history
4746
        // the custom one is left untouched
4747
        $liveContent = $this->contentService->publishVersion($draftVersion2->getVersionInfo());
4748
4749
        $location = $this->locationService->loadLocation(
4750
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4751
        );
4752
4753
        $aliases = $urlAliasService->listLocationAliases($location);
4754
4755
        $this->assertAliasesCorrect(
4756
            [
4757
                '/my/fancy/story-about-ez-publish' => [
4758
                    'type' => URLAlias::LOCATION,
4759
                    'destination' => $location->id,
4760
                    'path' => '/my/fancy/story-about-ez-publish',
4761
                    'languageCodes' => [self::ENG_US],
4762
                    'isHistory' => false,
4763
                    'isCustom' => true,
4764
                    'forward' => false,
4765
                    'alwaysAvailable' => false,
4766
                ],
4767
            ],
4768
            $aliases
4769
        );
4770
    }
4771
4772
    /**
4773
     * Test to ensure that old versions are not affected by updates to newer
4774
     * drafts.
4775
     */
4776
    public function testUpdatingDraftDoesNotUpdateOldVersions()
4777
    {
4778
        $contentVersion2 = $this->createContentVersion2();
4779
4780
        $loadedContent1 = $this->contentService->loadContent($contentVersion2->id, null, 1);
4781
        $loadedContent2 = $this->contentService->loadContent($contentVersion2->id, null, 2);
4782
4783
        $this->assertNotEquals(
4784
            $loadedContent1->getFieldValue('name', self::ENG_US),
4785
            $loadedContent2->getFieldValue('name', self::ENG_US)
4786
        );
4787
    }
4788
4789
    /**
4790
     * Test scenario with writer and publisher users.
4791
     * Writer can only create content. Publisher can publish this content.
4792
     */
4793
    public function testPublishWorkflow()
4794
    {
4795
        $this->createRoleWithPolicies('Publisher', [
4796
            ['module' => 'content', 'function' => 'read'],
4797
            ['module' => 'content', 'function' => 'create'],
4798
            ['module' => 'content', 'function' => 'publish'],
4799
        ]);
4800
4801
        $this->createRoleWithPolicies('Writer', [
4802
            ['module' => 'content', 'function' => 'read'],
4803
            ['module' => 'content', 'function' => 'create'],
4804
        ]);
4805
4806
        $writerUser = $this->createCustomUserWithLogin(
4807
            'writer',
4808
            '[email protected]',
4809
            self::WRITERS_USER_GROUP_NAME,
4810
            'Writer'
4811
        );
4812
4813
        $publisherUser = $this->createCustomUserWithLogin(
4814
            'publisher',
4815
            '[email protected]',
4816
            'Publishers',
4817
            'Publisher'
4818
        );
4819
4820
        $this->permissionResolver->setCurrentUserReference($writerUser);
4821
        $draft = $this->createContentDraftVersion1();
4822
4823
        $this->permissionResolver->setCurrentUserReference($publisherUser);
4824
        $content = $this->contentService->publishVersion($draft->versionInfo);
4825
4826
        $this->contentService->loadContent($content->id);
4827
    }
4828
4829
    /**
4830
     * Test publish / content policy is required to be able to publish content.
4831
     */
4832
    public function testPublishContentWithoutPublishPolicyThrowsException()
4833
    {
4834
        $this->createRoleWithPolicies('Writer', [
4835
            ['module' => 'content', 'function' => 'read'],
4836
            ['module' => 'content', 'function' => 'create'],
4837
            ['module' => 'content', 'function' => 'edit'],
4838
        ]);
4839
        $writerUser = $this->createCustomUserWithLogin(
4840
            'writer',
4841
            '[email protected]',
4842
            self::WRITERS_USER_GROUP_NAME,
4843
            'Writer'
4844
        );
4845
        $this->permissionResolver->setCurrentUserReference($writerUser);
4846
4847
        $this->expectException(CoreUnauthorizedException::class);
4848
        $this->expectExceptionMessageRegExp('/User does not have access to \'publish\' \'content\'/');
4849
4850
        $this->createContentVersion1();
4851
    }
4852
4853
    /**
4854
     * Test removal of the specific translation from all the Versions of a Content Object.
4855
     *
4856
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
4857
     */
4858
    public function testDeleteTranslation()
4859
    {
4860
        $content = $this->createContentVersion2();
4861
4862
        // create multiple versions to exceed archive limit
4863
        for ($i = 0; $i < 5; ++$i) {
4864
            $contentDraft = $this->contentService->createContentDraft($content->contentInfo);
4865
            $contentUpdateStruct = $this->contentService->newContentUpdateStruct();
4866
            $contentDraft = $this->contentService->updateContent(
4867
                $contentDraft->versionInfo,
4868
                $contentUpdateStruct
4869
            );
4870
            $this->contentService->publishVersion($contentDraft->versionInfo);
4871
        }
4872
4873
        $this->contentService->deleteTranslation($content->contentInfo, self::ENG_GB);
4874
4875
        $this->assertTranslationDoesNotExist(self::ENG_GB, $content->id);
4876
    }
4877
4878
    /**
4879
     * Test deleting a Translation which is initial for some Version, updates initialLanguageCode
4880
     * with mainLanguageCode (assuming they are different).
4881
     */
4882
    public function testDeleteTranslationUpdatesInitialLanguageCodeVersion()
4883
    {
4884
        $content = $this->createContentVersion2();
4885
        // create another, copied, version
4886
        $contentDraft = $this->contentService->updateContent(
4887
            $this->contentService->createContentDraft($content->contentInfo)->versionInfo,
4888
            $this->contentService->newContentUpdateStruct()
4889
        );
4890
        $publishedContent = $this->contentService->publishVersion($contentDraft->versionInfo);
4891
4892
        // remove first version with only one translation as it is not the subject of this test
4893
        $this->contentService->deleteVersion(
4894
            $this->contentService->loadVersionInfo($publishedContent->contentInfo, 1)
4895
        );
4896
4897
        // sanity check
4898
        self::assertEquals(self::ENG_US, $content->contentInfo->mainLanguageCode);
4899
        self::assertEquals(self::ENG_US, $content->versionInfo->initialLanguageCode);
4900
4901
        // update mainLanguageCode so it is different than initialLanguageCode for Version
4902
        $contentMetadataUpdateStruct = $this->contentService->newContentMetadataUpdateStruct();
4903
        $contentMetadataUpdateStruct->mainLanguageCode = self::ENG_GB;
4904
        $content = $this->contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
4905
4906
        $this->contentService->deleteTranslation($content->contentInfo, self::ENG_US);
4907
4908
        $this->assertTranslationDoesNotExist(self::ENG_US, $content->id);
4909
    }
4910
4911
    /**
4912
     * Test removal of the specific translation properly updates languages of the URL alias.
4913
     *
4914
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
4915
     */
4916
    public function testDeleteTranslationUpdatesUrlAlias()
4917
    {
4918
        $urlAliasService = $this->getRepository()->getURLAliasService();
4919
4920
        $content = $this->createContentVersion2();
4921
        $mainLocation = $this->locationService->loadLocation($content->contentInfo->mainLocationId);
4922
4923
        // create custom URL alias for Content main Location
4924
        $urlAliasService->createUrlAlias($mainLocation, '/my-custom-url', self::ENG_GB);
4925
4926
        // create secondary Location for Content
4927
        $secondaryLocation = $this->locationService->createLocation(
4928
            $content->contentInfo,
4929
            $this->locationService->newLocationCreateStruct(2)
4930
        );
4931
4932
        // create custom URL alias for Content secondary Location
4933
        $urlAliasService->createUrlAlias($secondaryLocation, '/my-secondary-url', self::ENG_GB);
4934
4935
        // delete Translation
4936
        $this->contentService->deleteTranslation($content->contentInfo, self::ENG_GB);
4937
4938
        foreach ([$mainLocation, $secondaryLocation] as $location) {
4939
            // check auto-generated URL aliases
4940
            foreach ($urlAliasService->listLocationAliases($location, false) as $alias) {
4941
                self::assertNotContains(self::ENG_GB, $alias->languageCodes);
4942
            }
4943
4944
            // check custom URL aliases
4945
            foreach ($urlAliasService->listLocationAliases($location) as $alias) {
4946
                self::assertNotContains(self::ENG_GB, $alias->languageCodes);
4947
            }
4948
        }
4949
    }
4950
4951
    /**
4952
     * Test removal of a main translation throws BadStateException.
4953
     *
4954
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
4955
     */
4956
    public function testDeleteTranslationMainLanguageThrowsBadStateException()
4957
    {
4958
        $content = $this->createContentVersion2();
4959
4960
        // delete first version which has only one translation
4961
        $this->contentService->deleteVersion($this->contentService->loadVersionInfo($content->contentInfo, 1));
4962
4963
        // try to delete main translation
4964
        $this->expectException(BadStateException::class);
4965
        $this->expectExceptionMessage('Specified translation is the main translation of the Content Object');
4966
4967
        $this->contentService->deleteTranslation($content->contentInfo, $content->contentInfo->mainLanguageCode);
4968
    }
4969
4970
    /**
4971
     * Test removal of a Translation is possible when some archived Versions have only this Translation.
4972
     *
4973
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
4974
     */
4975
    public function testDeleteTranslationDeletesSingleTranslationVersions()
4976
    {
4977
        // content created by the createContentVersion1 method has eng-US translation only.
4978
        $content = $this->createContentVersion1();
4979
4980
        // create new version and add eng-GB translation
4981
        $contentDraft = $this->contentService->createContentDraft($content->contentInfo);
4982
        $contentUpdateStruct = $this->contentService->newContentUpdateStruct();
4983
        $contentUpdateStruct->setField('name', 'Awesome Board', self::ENG_GB);
4984
        $contentDraft = $this->contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
4985
        $publishedContent = $this->contentService->publishVersion($contentDraft->versionInfo);
4986
4987
        // update mainLanguageCode to avoid exception related to that
4988
        $contentMetadataUpdateStruct = $this->contentService->newContentMetadataUpdateStruct();
4989
        $contentMetadataUpdateStruct->mainLanguageCode = self::ENG_GB;
4990
4991
        $content = $this->contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
4992
4993
        $this->contentService->deleteTranslation($content->contentInfo, self::ENG_US);
4994
4995
        $this->assertTranslationDoesNotExist(self::ENG_US, $content->id);
4996
    }
4997
4998
    /**
4999
     * Test removal of the translation by the user who is not allowed to delete a content
5000
     * throws UnauthorizedException.
5001
     *
5002
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5003
     */
5004
    public function testDeleteTranslationThrowsUnauthorizedException()
5005
    {
5006
        $content = $this->createContentVersion2();
5007
5008
        // create user that can read/create/edit but cannot delete content
5009
        $this->createRoleWithPolicies('Writer', [
5010
            ['module' => 'content', 'function' => 'read'],
5011
            ['module' => 'content', 'function' => 'versionread'],
5012
            ['module' => 'content', 'function' => 'create'],
5013
            ['module' => 'content', 'function' => 'edit'],
5014
        ]);
5015
        $writerUser = $this->createCustomUserWithLogin(
5016
            'writer',
5017
            '[email protected]',
5018
            self::WRITERS_USER_GROUP_NAME,
5019
            'Writer'
5020
        );
5021
        $this->permissionResolver->setCurrentUserReference($writerUser);
5022
5023
        $this->expectException(UnauthorizedException::class);
5024
        $this->expectExceptionMessage('User does not have access to \'remove\' \'content\'');
5025
5026
        $this->contentService->deleteTranslation($content->contentInfo, self::ENG_GB);
5027
    }
5028
5029
    /**
5030
     * Test removal of a non-existent translation throws InvalidArgumentException.
5031
     *
5032
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5033
     */
5034
    public function testDeleteTranslationThrowsInvalidArgumentException()
5035
    {
5036
        // content created by the createContentVersion1 method has eng-US translation only.
5037
        $content = $this->createContentVersion1();
5038
5039
        $this->expectException(APIInvalidArgumentException::class);
5040
        $this->expectExceptionMessage('Argument \'$languageCode\' is invalid: ger-DE does not exist in the Content item');
5041
5042
        $this->contentService->deleteTranslation($content->contentInfo, self::GER_DE);
5043
    }
5044
5045
    /**
5046
     * Test deleting a Translation from Draft.
5047
     *
5048
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5049
     */
5050
    public function testDeleteTranslationFromDraft()
5051
    {
5052
        $languageCode = self::ENG_GB;
5053
        $content = $this->createMultipleLanguageContentVersion2();
5054
        $draft = $this->contentService->createContentDraft($content->contentInfo);
5055
        $draft = $this->contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5056
        $content = $this->contentService->publishVersion($draft->versionInfo);
5057
5058
        $loadedContent = $this->contentService->loadContent($content->id);
5059
        self::assertNotContains($languageCode, $loadedContent->versionInfo->languageCodes);
5060
        self::assertEmpty($loadedContent->getFieldsByLanguage($languageCode));
5061
    }
5062
5063
    /**
5064
     * Get values for multilingual field.
5065
     *
5066
     * @return array
5067
     */
5068
    public function providerForDeleteTranslationFromDraftRemovesUrlAliasOnPublishing()
5069
    {
5070
        return [
5071
            [
5072
                [self::ENG_US => 'US Name', self::ENG_GB => 'GB Name'],
5073
            ],
5074
            [
5075
                [self::ENG_US => 'Same Name', self::ENG_GB => 'Same Name'],
5076
            ],
5077
        ];
5078
    }
5079
5080
    /**
5081
     * Test deleting a Translation from Draft removes previously stored URL aliases for published Content.
5082
     *
5083
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5084
     *
5085
     * @dataProvider providerForDeleteTranslationFromDraftRemovesUrlAliasOnPublishing
5086
     *
5087
     * @param string[] $fieldValues translated field values
5088
     *
5089
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
5090
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
5091
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
5092
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5093
     */
5094
    public function testDeleteTranslationFromDraftRemovesUrlAliasOnPublishing(array $fieldValues)
5095
    {
5096
        $urlAliasService = $this->getRepository()->getURLAliasService();
5097
5098
        // set language code to be removed
5099
        $languageCode = self::ENG_GB;
5100
        $draft = $this->createMultilingualContentDraft(
5101
            'folder',
5102
            2,
5103
            self::ENG_US,
5104
            [
5105
                'name' => [
5106
                    self::ENG_GB => $fieldValues[self::ENG_GB],
5107
                    self::ENG_US => $fieldValues[self::ENG_US],
5108
                ],
5109
            ]
5110
        );
5111
        $content = $this->contentService->publishVersion($draft->versionInfo);
5112
5113
        // create secondary location
5114
        $this->locationService->createLocation(
5115
            $content->contentInfo,
5116
            $this->locationService->newLocationCreateStruct(5)
5117
        );
5118
5119
        // sanity check
5120
        $locations = $this->locationService->loadLocations($content->contentInfo);
5121
        self::assertCount(2, $locations, 'Sanity check: Expected to find 2 Locations');
5122
        foreach ($locations as $location) {
5123
            $urlAliasService->createUrlAlias($location, '/us-custom_' . $location->id, self::ENG_US);
5124
            $urlAliasService->createUrlAlias($location, '/gb-custom_' . $location->id, self::ENG_GB);
5125
5126
            // check default URL aliases
5127
            $aliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
5128
            self::assertNotEmpty($aliases, 'Sanity check: URL alias for the translation does not exist');
5129
5130
            // check custom URL aliases
5131
            $aliases = $urlAliasService->listLocationAliases($location, true, $languageCode);
5132
            self::assertNotEmpty($aliases, 'Sanity check: Custom URL alias for the translation does not exist');
5133
        }
5134
5135
        // delete translation and publish new version
5136
        $draft = $this->contentService->createContentDraft($content->contentInfo);
5137
        $draft = $this->contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5138
        $this->contentService->publishVersion($draft->versionInfo);
5139
5140
        // check that aliases does not exist
5141
        foreach ($locations as $location) {
5142
            // check default URL aliases
5143
            $aliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
5144
            self::assertEmpty($aliases, 'URL alias for the deleted translation still exists');
5145
5146
            // check custom URL aliases
5147
            $aliases = $urlAliasService->listLocationAliases($location, true, $languageCode);
5148
            self::assertEmpty($aliases, 'Custom URL alias for the deleted translation still exists');
5149
        }
5150
    }
5151
5152
    /**
5153
     * Test that URL aliases for deleted Translations are properly archived.
5154
     */
5155
    public function testDeleteTranslationFromDraftArchivesUrlAliasOnPublishing()
5156
    {
5157
        $urlAliasService = $this->getRepository()->getURLAliasService();
5158
5159
        $content = $this->contentService->publishVersion(
5160
            $this->createMultilingualContentDraft(
5161
                'folder',
5162
                2,
5163
                self::ENG_US,
5164
                [
5165
                    'name' => [
5166
                        self::ENG_GB => 'BritishEnglishContent',
5167
                        self::ENG_US => 'AmericanEnglishContent',
5168
                    ],
5169
                ]
5170
            )->versionInfo
5171
        );
5172
5173
        $unrelatedContent = $this->contentService->publishVersion(
5174
            $this->createMultilingualContentDraft(
5175
                'folder',
5176
                2,
5177
                self::ENG_US,
5178
                [
5179
                    'name' => [
5180
                        self::ENG_GB => 'AnotherBritishContent',
5181
                        self::ENG_US => 'AnotherAmericanContent',
5182
                    ],
5183
                ]
5184
            )->versionInfo
5185
        );
5186
5187
        $urlAlias = $urlAliasService->lookup('/BritishEnglishContent');
5188
        self::assertFalse($urlAlias->isHistory);
5189
        self::assertEquals($urlAlias->path, '/BritishEnglishContent');
5190
        self::assertEquals($urlAlias->destination, $content->contentInfo->mainLocationId);
5191
5192
        $draft = $this->contentService->deleteTranslationFromDraft(
5193
            $this->contentService->createContentDraft($content->contentInfo)->versionInfo,
5194
            self::ENG_GB
5195
        );
5196
        $content = $this->contentService->publishVersion($draft->versionInfo);
5197
5198
        $urlAlias = $urlAliasService->lookup('/BritishEnglishContent');
5199
        self::assertTrue($urlAlias->isHistory);
5200
        self::assertEquals($urlAlias->path, '/BritishEnglishContent');
5201
        self::assertEquals($urlAlias->destination, $content->contentInfo->mainLocationId);
5202
5203
        $unrelatedUrlAlias = $urlAliasService->lookup('/AnotherBritishContent');
5204
        self::assertFalse($unrelatedUrlAlias->isHistory);
5205
        self::assertEquals($unrelatedUrlAlias->path, '/AnotherBritishContent');
5206
        self::assertEquals($unrelatedUrlAlias->destination, $unrelatedContent->contentInfo->mainLocationId);
5207
    }
5208
5209
    /**
5210
     * Test deleting a Translation from Draft which has single Translation throws BadStateException.
5211
     *
5212
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5213
     */
5214
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnSingleTranslation()
5215
    {
5216
        // create Content with single Translation
5217
        $publishedContent = $this->contentService->publishVersion(
5218
            $this->createContentDraft(
5219
                self::FORUM_IDENTIFIER,
5220
                2,
5221
                ['name' => 'Eng-US Version name']
5222
            )->versionInfo
5223
        );
5224
5225
        // update mainLanguageCode to avoid exception related to trying to delete main Translation
5226
        $contentMetadataUpdateStruct = $this->contentService->newContentMetadataUpdateStruct();
5227
        $contentMetadataUpdateStruct->mainLanguageCode = self::ENG_GB;
5228
        $publishedContent = $this->contentService->updateContentMetadata(
5229
            $publishedContent->contentInfo,
5230
            $contentMetadataUpdateStruct
5231
        );
5232
5233
        // create single Translation Version from the first one
5234
        $draft = $this->contentService->createContentDraft(
5235
            $publishedContent->contentInfo,
5236
            $publishedContent->versionInfo
5237
        );
5238
5239
        $this->expectException(BadStateException::class);
5240
        $this->expectExceptionMessage('Specified Translation is the only one Content Object Version has');
5241
5242
        // attempt to delete Translation
5243
        $this->contentService->deleteTranslationFromDraft($draft->versionInfo, self::ENG_US);
5244
    }
5245
5246
    /**
5247
     * Test deleting the Main Translation from Draft throws BadStateException.
5248
     *
5249
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5250
     */
5251
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnMainTranslation()
5252
    {
5253
        $mainLanguageCode = self::ENG_US;
5254
        $draft = $this->createMultilingualContentDraft(
5255
            self::FORUM_IDENTIFIER,
5256
            2,
5257
            $mainLanguageCode,
5258
            [
5259
                'name' => [
5260
                    self::ENG_US => 'An awesome eng-US forum',
5261
                    self::ENG_GB => 'An awesome eng-GB forum',
5262
                ],
5263
            ]
5264
        );
5265
5266
        $this->expectException(BadStateException::class);
5267
        $this->expectExceptionMessage('Specified Translation is the main Translation of the Content Object');
5268
5269
        $this->contentService->deleteTranslationFromDraft($draft->versionInfo, $mainLanguageCode);
5270
    }
5271
5272
    /**
5273
     * Test deleting the Translation from Published Version throws BadStateException.
5274
     *
5275
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5276
     */
5277
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnPublishedVersion()
5278
    {
5279
        $languageCode = self::ENG_US;
5280
        $content = $this->createMultipleLanguageContentVersion2();
5281
        $draft = $this->contentService->createContentDraft($content->contentInfo);
5282
        $publishedContent = $this->contentService->publishVersion($draft->versionInfo);
5283
5284
        $this->expectException(BadStateException::class);
5285
        $this->expectExceptionMessage('Version is not a draft');
5286
5287
        $this->contentService->deleteTranslationFromDraft($publishedContent->versionInfo, $languageCode);
5288
    }
5289
5290
    /**
5291
     * Test deleting a Translation from Draft throws UnauthorizedException if user cannot edit Content.
5292
     *
5293
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5294
     */
5295
    public function testDeleteTranslationFromDraftThrowsUnauthorizedException()
5296
    {
5297
        $languageCode = self::ENG_GB;
5298
        $content = $this->createMultipleLanguageContentVersion2();
5299
        $draft = $this->contentService->createContentDraft($content->contentInfo);
5300
5301
        // create user that can read/create/delete but cannot edit or content
5302
        $this->createRoleWithPolicies('Writer', [
5303
            ['module' => 'content', 'function' => 'read'],
5304
            ['module' => 'content', 'function' => 'versionread'],
5305
            ['module' => 'content', 'function' => 'create'],
5306
            ['module' => 'content', 'function' => 'delete'],
5307
        ]);
5308
        $writerUser = $this->createCustomUserWithLogin(
5309
            'user',
5310
            '[email protected]',
5311
            self::WRITERS_USER_GROUP_NAME,
5312
            'Writer'
5313
        );
5314
        $this->permissionResolver->setCurrentUserReference($writerUser);
5315
5316
        $this->expectException(UnauthorizedException::class);
5317
        $this->expectExceptionMessage('User does not have access to \'edit\' \'content\'');
5318
5319
        $this->contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5320
    }
5321
5322
    /**
5323
     * Test deleting a non-existent Translation from Draft throws InvalidArgumentException.
5324
     *
5325
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5326
     */
5327
    public function testDeleteTranslationFromDraftThrowsInvalidArgumentException()
5328
    {
5329
        $languageCode = self::GER_DE;
5330
        $content = $this->createMultipleLanguageContentVersion2();
5331
        $draft = $this->contentService->createContentDraft($content->contentInfo);
5332
        $this->expectException(APIInvalidArgumentException::class);
5333
        $this->expectExceptionMessageRegExp('/The Version \(ContentId=\d+, VersionNo=\d+\) is not translated into ger-DE/');
5334
        $this->contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5335
    }
5336
5337
    /**
5338
     * Test loading list of Content items.
5339
     */
5340
    public function testLoadContentListByContentInfo()
5341
    {
5342
        $allLocationsCount = $this->locationService->getAllLocationsCount();
5343
        $contentInfoList = array_map(
5344
            function (Location $location) {
5345
                return $location->contentInfo;
5346
            },
5347
            $this->locationService->loadAllLocations(0, $allLocationsCount)
5348
        );
5349
5350
        $contentList = $this->contentService->loadContentListByContentInfo($contentInfoList);
5351
        self::assertCount(count($contentInfoList), $contentList);
5352
        foreach ($contentList as $content) {
5353
            try {
5354
                $loadedContent = $this->contentService->loadContent($content->id);
5355
                self::assertEquals($loadedContent, $content, "Failed to properly bulk-load Content {$content->id}");
5356
            } catch (NotFoundException $e) {
5357
                self::fail("Failed to load Content {$content->id}: {$e->getMessage()}");
5358
            } catch (UnauthorizedException $e) {
5359
                self::fail("Failed to load Content {$content->id}: {$e->getMessage()}");
5360
            }
5361
        }
5362
    }
5363
5364
    /**
5365
     * Test loading content versions after removing exactly two drafts.
5366
     *
5367
     * @see https://jira.ez.no/browse/EZP-30271
5368
     *
5369
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteVersion
5370
     */
5371
    public function testLoadVersionsAfterDeletingTwoDrafts()
5372
    {
5373
        $content = $this->createFolder([self::ENG_GB => 'Foo'], 2);
5374
5375
        // First update and publish
5376
        $modifiedContent = $this->updateFolder($content, [self::ENG_GB => 'Foo1']);
5377
        $content = $this->contentService->publishVersion($modifiedContent->versionInfo);
5378
5379
        // Second update and publish
5380
        $modifiedContent = $this->updateFolder($content, [self::ENG_GB => 'Foo2']);
5381
        $content = $this->contentService->publishVersion($modifiedContent->versionInfo);
5382
5383
        // Create drafts
5384
        $this->updateFolder($content, [self::ENG_GB => 'Foo3']);
5385
        $this->updateFolder($content, [self::ENG_GB => 'Foo4']);
5386
5387
        $versions = $this->contentService->loadVersions($content->contentInfo);
5388
5389
        foreach ($versions as $key => $version) {
5390
            if ($version->isDraft()) {
5391
                $this->contentService->deleteVersion($version);
5392
                unset($versions[$key]);
5393
            }
5394
        }
5395
5396
        $this->assertEquals($versions, $this->contentService->loadVersions($content->contentInfo));
5397
    }
5398
5399
    /**
5400
     * Tests loading list of content versions of status draft.
5401
     */
5402
    public function testLoadVersionsOfStatusDraft()
5403
    {
5404
        $content = $this->createContentVersion1();
5405
5406
        $this->contentService->createContentDraft($content->contentInfo);
5407
        $this->contentService->createContentDraft($content->contentInfo);
5408
        $this->contentService->createContentDraft($content->contentInfo);
5409
5410
        $versions = $this->contentService->loadVersions($content->contentInfo, VersionInfo::STATUS_DRAFT);
5411
5412
        $this->assertSame(\count($versions), 3);
5413
    }
5414
5415
    /**
5416
     * Tests loading list of content versions of status archived.
5417
     */
5418
    public function testLoadVersionsOfStatusArchived()
5419
    {
5420
        $content = $this->createContentVersion1();
5421
5422
        $draft1 = $this->contentService->createContentDraft($content->contentInfo);
5423
        $this->contentService->publishVersion($draft1->versionInfo);
5424
5425
        $draft2 = $this->contentService->createContentDraft($content->contentInfo);
5426
        $this->contentService->publishVersion($draft2->versionInfo);
5427
5428
        $versions = $this->contentService->loadVersions($content->contentInfo, VersionInfo::STATUS_ARCHIVED);
5429
5430
        $this->assertSame(\count($versions), 2);
5431
    }
5432
5433
    /**
5434
     * Asserts that all aliases defined in $expectedAliasProperties with the
5435
     * given properties are available in $actualAliases and not more.
5436
     *
5437
     * @param array $expectedAliasProperties
5438
     * @param array $actualAliases
5439
     */
5440
    private function assertAliasesCorrect(array $expectedAliasProperties, array $actualAliases)
5441
    {
5442
        foreach ($actualAliases as $actualAlias) {
5443
            if (!isset($expectedAliasProperties[$actualAlias->path])) {
5444
                $this->fail(
5445
                    sprintf(
5446
                        'Alias with path "%s" in languages "%s" not expected.',
5447
                        $actualAlias->path,
5448
                        implode(', ', $actualAlias->languageCodes)
5449
                    )
5450
                );
5451
            }
5452
5453
            foreach ($expectedAliasProperties[$actualAlias->path] as $propertyName => $propertyValue) {
5454
                $this->assertEquals(
5455
                    $propertyValue,
5456
                    $actualAlias->$propertyName,
5457
                    sprintf(
5458
                        'Property $%s incorrect on alias with path "%s" in languages "%s".',
5459
                        $propertyName,
5460
                        $actualAlias->path,
5461
                        implode(', ', $actualAlias->languageCodes)
5462
                    )
5463
                );
5464
            }
5465
5466
            unset($expectedAliasProperties[$actualAlias->path]);
5467
        }
5468
5469
        if (!empty($expectedAliasProperties)) {
5470
            $this->fail(
5471
                sprintf(
5472
                    'Missing expected aliases with paths "%s".',
5473
                    implode('", "', array_keys($expectedAliasProperties))
5474
                )
5475
            );
5476
        }
5477
    }
5478
5479
    /**
5480
     * Asserts that the given fields are equal to the default fields fixture.
5481
     *
5482
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5483
     */
5484
    private function assertAllFieldsEquals(array $fields)
5485
    {
5486
        $actual = $this->normalizeFields($fields);
5487
        $expected = $this->normalizeFields($this->createFieldsFixture());
5488
5489
        $this->assertEquals($expected, $actual);
5490
    }
5491
5492
    /**
5493
     * Asserts that the given fields are equal to a language filtered set of the
5494
     * default fields fixture.
5495
     *
5496
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5497
     * @param string $languageCode
5498
     */
5499
    private function assertLocaleFieldsEquals(array $fields, $languageCode)
5500
    {
5501
        $actual = $this->normalizeFields($fields);
5502
5503
        $expected = [];
5504
        foreach ($this->normalizeFields($this->createFieldsFixture()) as $field) {
5505
            if ($field->languageCode !== $languageCode) {
5506
                continue;
5507
            }
5508
            $expected[] = $field;
5509
        }
5510
5511
        $this->assertEquals($expected, $actual);
5512
    }
5513
5514
    /**
5515
     * This method normalizes a set of fields and returns a normalized set.
5516
     *
5517
     * Normalization means it resets the storage specific field id to zero and
5518
     * it sorts the field by their identifier and their language code. In
5519
     * addition, the field value is removed, since this one depends on the
5520
     * specific FieldType, which is tested in a dedicated integration test.
5521
     *
5522
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5523
     *
5524
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5525
     */
5526
    private function normalizeFields(array $fields)
5527
    {
5528
        $normalized = [];
5529
        foreach ($fields as $field) {
5530
            $normalized[] = new Field(
5531
                [
5532
                    'id' => 0,
5533
                    'value' => $field->value !== null,
5534
                    'languageCode' => $field->languageCode,
5535
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
5536
                    'fieldTypeIdentifier' => $field->fieldTypeIdentifier,
5537
                ]
5538
            );
5539
        }
5540
        usort(
5541
            $normalized,
5542
            function ($field1, $field2) {
5543
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
5544
                    return strcasecmp($field1->languageCode, $field2->languageCode);
5545
                }
5546
5547
                return $return;
5548
            }
5549
        );
5550
5551
        return $normalized;
5552
    }
5553
5554
    /**
5555
     * Asserts that given Content has default ContentStates.
5556
     *
5557
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
5558
     */
5559
    private function assertDefaultContentStates(ContentInfo $contentInfo)
5560
    {
5561
        $objectStateService = $this->getRepository()->getObjectStateService();
5562
5563
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
5564
5565
        foreach ($objectStateGroups as $objectStateGroup) {
5566
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
5567
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
5568
                // Only check the first object state which is the default one.
5569
                $this->assertEquals(
5570
                    $objectState,
5571
                    $contentState
5572
                );
5573
                break;
5574
            }
5575
        }
5576
    }
5577
5578
    /**
5579
     * Assert that given Content has no references to a translation specified by the $languageCode.
5580
     *
5581
     * @param string $languageCode
5582
     * @param int $contentId
5583
     */
5584
    private function assertTranslationDoesNotExist($languageCode, $contentId)
5585
    {
5586
        $content = $this->contentService->loadContent($contentId);
5587
5588
        foreach ($content->fields as $field) {
5589
            /** @var array $field */
5590
            self::assertArrayNotHasKey($languageCode, $field);
5591
            self::assertNotEquals($languageCode, $content->contentInfo->mainLanguageCode);
5592
            self::assertArrayNotHasKey($languageCode, $content->versionInfo->getNames());
5593
            self::assertNotEquals($languageCode, $content->versionInfo->initialLanguageCode);
5594
            self::assertNotContains($languageCode, $content->versionInfo->languageCodes);
5595
        }
5596
        foreach ($this->contentService->loadVersions($content->contentInfo) as $versionInfo) {
5597
            self::assertArrayNotHasKey($languageCode, $versionInfo->getNames());
5598
            self::assertNotEquals($languageCode, $versionInfo->contentInfo->mainLanguageCode);
5599
            self::assertNotEquals($languageCode, $versionInfo->initialLanguageCode);
5600
            self::assertNotContains($languageCode, $versionInfo->languageCodes);
5601
        }
5602
    }
5603
5604
    /**
5605
     * Returns the default fixture of fields used in most tests.
5606
     *
5607
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5608
     */
5609
    private function createFieldsFixture()
5610
    {
5611
        return [
5612
            new Field(
5613
                [
5614
                    'id' => 0,
5615
                    'value' => 'Foo',
5616
                    'languageCode' => self::ENG_US,
5617
                    'fieldDefIdentifier' => 'description',
5618
                    'fieldTypeIdentifier' => 'ezrichtext',
5619
                ]
5620
            ),
5621
            new Field(
5622
                [
5623
                    'id' => 0,
5624
                    'value' => 'Bar',
5625
                    'languageCode' => self::ENG_GB,
5626
                    'fieldDefIdentifier' => 'description',
5627
                    'fieldTypeIdentifier' => 'ezrichtext',
5628
                ]
5629
            ),
5630
            new Field(
5631
                [
5632
                    'id' => 0,
5633
                    'value' => 'An awesome multi-lang forum²',
5634
                    'languageCode' => self::ENG_US,
5635
                    'fieldDefIdentifier' => 'name',
5636
                    'fieldTypeIdentifier' => 'ezstring',
5637
                ]
5638
            ),
5639
            new Field(
5640
                [
5641
                    'id' => 0,
5642
                    'value' => 'An awesome multi-lang forum²³',
5643
                    'languageCode' => self::ENG_GB,
5644
                    'fieldDefIdentifier' => 'name',
5645
                    'fieldTypeIdentifier' => 'ezstring',
5646
                ]
5647
            ),
5648
        ];
5649
    }
5650
5651
    /**
5652
     * Gets expected property values for the "Media" ContentInfo ValueObject.
5653
     *
5654
     * @return array
5655
     */
5656
    private function getExpectedMediaContentInfoProperties()
5657
    {
5658
        return [
5659
            'id' => self::MEDIA_CONTENT_ID,
5660
            'contentTypeId' => 1,
5661
            'name' => 'Media',
5662
            'sectionId' => 3,
5663
            'currentVersionNo' => 1,
5664
            'published' => true,
5665
            'ownerId' => 14,
5666
            'modificationDate' => $this->createDateTime(1060695457),
5667
            'publishedDate' => $this->createDateTime(1060695457),
5668
            'alwaysAvailable' => 1,
5669
            'remoteId' => self::MEDIA_REMOTE_ID,
5670
            'mainLanguageCode' => self::ENG_US,
5671
            'mainLocationId' => 43,
5672
            'status' => ContentInfo::STATUS_PUBLISHED,
5673
        ];
5674
    }
5675
5676
    /**
5677
     * @covers \eZ\Publish\API\Repository\ContentService::hideContent
5678
     *
5679
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
5680
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
5681
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException
5682
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
5683
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
5684
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
5685
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5686
     */
5687
    public function testHideContent(): void
5688
    {
5689
        $contentTypeService = $this->getRepository()->getContentTypeService();
5690
5691
        $locationCreateStructs = array_map(
5692
            function (Location $parentLocation) {
5693
                return $this->locationService->newLocationCreateStruct($parentLocation->id);
5694
            },
5695
            $this->createParentLocationsForHideReveal(2)
5696
        );
5697
5698
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
5699
5700
        $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
5701
        $contentCreate->setField('name', 'Folder to hide');
5702
5703
        $content = $this->contentService->createContent(
5704
            $contentCreate,
5705
            $locationCreateStructs
5706
        );
5707
5708
        $publishedContent = $this->contentService->publishVersion($content->versionInfo);
5709
        $locations = $this->locationService->loadLocations($publishedContent->contentInfo);
5710
5711
        // Sanity check
5712
        $this->assertCount(3, $locations);
5713
        $this->assertCount(0, $this->filterHiddenLocations($locations));
5714
5715
        $this->contentService->hideContent($publishedContent->contentInfo);
5716
5717
        $locations = $this->locationService->loadLocations($publishedContent->contentInfo);
5718
        $this->assertCount(3, $locations);
5719
        $this->assertCount(3, $this->filterHiddenLocations($locations));
5720
    }
5721
5722
    /**
5723
     * @covers \eZ\Publish\API\Repository\ContentService::revealContent
5724
     *
5725
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
5726
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
5727
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5728
     */
5729
    public function testRevealContent()
5730
    {
5731
        $contentTypeService = $this->getRepository()->getContentTypeService();
5732
5733
        $locationCreateStructs = array_map(
5734
            function (Location $parentLocation) {
5735
                return $this->locationService->newLocationCreateStruct($parentLocation->id);
5736
            },
5737
            $this->createParentLocationsForHideReveal(2)
5738
        );
5739
5740
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
5741
5742
        $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
5743
        $contentCreate->setField('name', 'Folder to hide');
5744
5745
        $locationCreateStructs[0]->hidden = true;
5746
5747
        $content = $this->contentService->createContent(
5748
            $contentCreate,
5749
            $locationCreateStructs
5750
        );
5751
5752
        $publishedContent = $this->contentService->publishVersion($content->versionInfo);
5753
        $locations = $this->locationService->loadLocations($publishedContent->contentInfo);
5754
5755
        // Sanity check
5756
        $hiddenLocations = $this->filterHiddenLocations($locations);
5757
        $this->assertCount(3, $locations);
5758
        $this->assertCount(1, $hiddenLocations);
5759
5760
        // BEGIN: Use Case
5761
        $this->contentService->hideContent($publishedContent->contentInfo);
5762
        $this->assertCount(
5763
            3,
5764
            $this->filterHiddenLocations(
5765
                $this->locationService->loadLocations($publishedContent->contentInfo)
5766
            )
5767
        );
5768
5769
        $this->contentService->revealContent($publishedContent->contentInfo);
5770
        // END: Use Case
5771
5772
        $locations = $this->locationService->loadLocations($publishedContent->contentInfo);
5773
        $hiddenLocationsAfterReveal = $this->filterHiddenLocations($locations);
5774
        $this->assertCount(3, $locations);
5775
        $this->assertCount(1, $hiddenLocationsAfterReveal);
5776
        $this->assertEquals($hiddenLocations, $hiddenLocationsAfterReveal);
5777
    }
5778
5779
    /**
5780
     * @depends testRevealContent
5781
     */
5782
    public function testRevealContentWithHiddenParent()
5783
    {
5784
        $contentTypeService = $this->getRepository()->getContentTypeService();
5785
5786
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
5787
5788
        $contentNames = [
5789
            'Parent Content',
5790
            'Child (Nesting 1)',
5791
            'Child (Nesting 2)',
5792
            'Child (Nesting 3)',
5793
            'Child (Nesting 4)',
5794
        ];
5795
5796
        $parentLocation = $this->locationService->newLocationCreateStruct(
5797
            $this->generateId('location', 2)
5798
        );
5799
5800
        /** @var Content[] $contents */
5801
        $contents = [];
5802
5803
        foreach ($contentNames as $contentName) {
5804
            $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
5805
            $contentCreate->setField('name', $contentName);
5806
5807
            $content = $this->contentService->createContent($contentCreate, [$parentLocation]);
5808
            $contents[] = $publishedContent = $this->contentService->publishVersion($content->versionInfo);
5809
5810
            $parentLocation = $this->locationService->newLocationCreateStruct(
5811
                $this->generateId('location', $publishedContent->contentInfo->mainLocationId)
5812
            );
5813
        }
5814
5815
        $this->contentService->hideContent($contents[0]->contentInfo);
5816
        $this->contentService->hideContent($contents[2]->contentInfo);
5817
        $this->contentService->revealContent($contents[2]->contentInfo);
5818
5819
        $parentContent = $this->contentService->loadContent($contents[0]->id);
5820
        $parentLocation = $this->locationService->loadLocation($parentContent->contentInfo->mainLocationId);
5821
        $parentSublocations = $this->locationService->loadLocationList([
5822
            $contents[1]->contentInfo->mainLocationId,
5823
            $contents[2]->contentInfo->mainLocationId,
5824
            $contents[3]->contentInfo->mainLocationId,
5825
            $contents[4]->contentInfo->mainLocationId,
5826
        ]);
5827
5828
        // Parent remains invisible
5829
        self::assertTrue($parentLocation->invisible);
5830
5831
        // All parent sublocations remain invisible as well
5832
        foreach ($parentSublocations as $parentSublocation) {
5833
            self::assertTrue($parentSublocation->invisible);
5834
        }
5835
    }
5836
5837
    /**
5838
     * @depends testRevealContent
5839
     */
5840
    public function testRevealContentWithHiddenChildren()
5841
    {
5842
        $contentTypeService = $this->getRepository()->getContentTypeService();
5843
5844
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
5845
5846
        $contentNames = [
5847
            'Parent Content',
5848
            'Child (Nesting 1)',
5849
            'Child (Nesting 2)',
5850
            'Child (Nesting 3)',
5851
            'Child (Nesting 4)',
5852
        ];
5853
5854
        $parentLocation = $this->locationService->newLocationCreateStruct(
5855
            $this->generateId('location', 2)
5856
        );
5857
5858
        /** @var Content[] $contents */
5859
        $contents = [];
5860
5861
        foreach ($contentNames as $contentName) {
5862
            $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
5863
            $contentCreate->setField('name', $contentName);
5864
5865
            $content = $this->contentService->createContent($contentCreate, [$parentLocation]);
5866
            $contents[] = $publishedContent = $this->contentService->publishVersion($content->versionInfo);
5867
5868
            $parentLocation = $this->locationService->newLocationCreateStruct(
5869
                $this->generateId('location', $publishedContent->contentInfo->mainLocationId)
5870
            );
5871
        }
5872
5873
        $this->contentService->hideContent($contents[0]->contentInfo);
5874
        $this->contentService->hideContent($contents[2]->contentInfo);
5875
        $this->contentService->revealContent($contents[0]->contentInfo);
5876
5877
        $directChildContent = $this->contentService->loadContent($contents[1]->id);
5878
        $directChildLocation = $this->locationService->loadLocation($directChildContent->contentInfo->mainLocationId);
5879
5880
        $childContent = $this->contentService->loadContent($contents[2]->id);
5881
        $childLocation = $this->locationService->loadLocation($childContent->contentInfo->mainLocationId);
5882
        $childSublocations = $this->locationService->loadLocationList([
5883
            $contents[3]->contentInfo->mainLocationId,
5884
            $contents[4]->contentInfo->mainLocationId,
5885
        ]);
5886
5887
        // Direct child content is not hidden
5888
        self::assertFalse($directChildContent->contentInfo->isHidden);
5889
5890
        // Direct child content location is still invisible
5891
        self::assertFalse($directChildLocation->invisible);
5892
5893
        // Child content is still hidden
5894
        self::assertTrue($childContent->contentInfo->isHidden);
5895
5896
        // Child content location is still invisible
5897
        self::assertTrue($childLocation->invisible);
5898
5899
        // All childs sublocations remain invisible as well
5900
        foreach ($childSublocations as $childSublocation) {
5901
            self::assertTrue($childSublocation->invisible);
5902
        }
5903
    }
5904
5905
    public function testHideContentWithParentLocation()
5906
    {
5907
        $contentTypeService = $this->getRepository()->getContentTypeService();
5908
5909
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
5910
5911
        $contentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
5912
        $contentCreate->setField('name', 'Parent');
5913
5914
        $content = $this->contentService->createContent(
5915
            $contentCreate,
5916
            [
5917
                $this->locationService->newLocationCreateStruct(
5918
                    $this->generateId('location', 2)
5919
                ),
5920
            ]
5921
        );
5922
5923
        $publishedContent = $this->contentService->publishVersion($content->versionInfo);
5924
5925
        $this->contentService->hideContent($publishedContent->contentInfo);
5926
5927
        $locations = $this->locationService->loadLocations($publishedContent->contentInfo);
5928
5929
        $childContentCreate = $this->contentService->newContentCreateStruct($contentType, self::ENG_US);
5930
        $childContentCreate->setField('name', 'Child');
5931
5932
        $childContent = $this->contentService->createContent(
5933
            $childContentCreate,
5934
            [
5935
                $this->locationService->newLocationCreateStruct(
5936
                    $locations[0]->id
5937
                ),
5938
            ]
5939
        );
5940
5941
        $publishedChildContent = $this->contentService->publishVersion($childContent->versionInfo);
5942
5943
        $childLocations = $this->locationService->loadLocations($publishedChildContent->contentInfo);
5944
5945
        $this->assertTrue($locations[0]->hidden);
5946
        $this->assertTrue($locations[0]->invisible);
5947
5948
        $this->assertFalse($childLocations[0]->hidden);
5949
        $this->assertTrue($childLocations[0]->invisible);
5950
    }
5951
5952
    public function testChangeContentName()
5953
    {
5954
        $contentDraft = $this->createContentDraft(
5955
            'folder',
5956
            $this->generateId('location', 2),
5957
            [
5958
                'name' => 'Marco',
5959
            ]
5960
        );
5961
5962
        $publishedContent = $this->contentService->publishVersion($contentDraft->versionInfo);
5963
        $contentMetadataUpdateStruct = new ContentMetadataUpdateStruct([
5964
            'name' => 'Polo',
5965
        ]);
5966
        $this->contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
5967
5968
        $updatedContent = $this->contentService->loadContent($publishedContent->id);
5969
5970
        $this->assertEquals('Marco', $publishedContent->contentInfo->name);
5971
        $this->assertEquals('Polo', $updatedContent->contentInfo->name);
5972
    }
5973
5974
    public function testCopyTranslationsFromPublishedToDraft()
5975
    {
5976
        $contentDraft = $this->createContentDraft(
5977
            'folder',
5978
            $this->generateId('location', 2),
5979
            [
5980
                'name' => 'Folder US',
5981
            ]
5982
        );
5983
5984
        $publishedContent = $this->contentService->publishVersion($contentDraft->versionInfo);
5985
5986
        $deDraft = $this->contentService->createContentDraft($publishedContent->contentInfo);
5987
5988
        $contentUpdateStruct = new ContentUpdateStruct([
5989
            'initialLanguageCode' => self::GER_DE,
5990
            'fields' => $contentDraft->getFields(),
5991
        ]);
5992
5993
        $contentUpdateStruct->setField('name', 'Folder GER', self::GER_DE);
5994
5995
        $deContent = $this->contentService->updateContent($deDraft->versionInfo, $contentUpdateStruct);
5996
5997
        $updatedContent = $this->contentService->loadContent($deContent->id, null, $deContent->versionInfo->versionNo);
5998
        $this->assertEquals(
5999
            [
6000
                self::ENG_US => 'Folder US',
6001
                self::GER_DE => 'Folder GER',
6002
            ],
6003
            $updatedContent->fields['name']
6004
        );
6005
6006
        $gbDraft = $this->contentService->createContentDraft($publishedContent->contentInfo);
6007
6008
        $contentUpdateStruct = new ContentUpdateStruct([
6009
            'initialLanguageCode' => self::ENG_GB,
6010
            'fields' => $contentDraft->getFields(),
6011
        ]);
6012
6013
        $contentUpdateStruct->setField('name', 'Folder GB', self::ENG_GB);
6014
6015
        $gbContent = $this->contentService->updateContent($gbDraft->versionInfo, $contentUpdateStruct);
6016
        $this->contentService->publishVersion($gbDraft->versionInfo);
6017
        $updatedContent = $this->contentService->loadContent($gbContent->id, null, $gbContent->versionInfo->versionNo);
6018
        $this->assertEquals(
6019
            [
6020
                self::ENG_US => 'Folder US',
6021
                self::ENG_GB => 'Folder GB',
6022
            ],
6023
            $updatedContent->fields['name']
6024
        );
6025
6026
        $dePublished = $this->contentService->publishVersion($deDraft->versionInfo);
6027
        $this->assertEquals(
6028
            [
6029
                self::ENG_US => 'Folder US',
6030
                self::GER_DE => 'Folder GER',
6031
                self::ENG_GB => 'Folder GB',
6032
            ],
6033
            $dePublished->fields['name']
6034
        );
6035
    }
6036
6037
    /**
6038
     * Create structure of parent folders with Locations to be used for Content hide/reveal tests.
6039
     *
6040
     * @param int $parentLocationId
6041
     *
6042
     * @return \eZ\Publish\API\Repository\Values\Content\Location[] A list of Locations aimed to be parents
6043
     *
6044
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
6045
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
6046
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
6047
     */
6048
    private function createParentLocationsForHideReveal(int $parentLocationId): array
6049
    {
6050
        $parentFoldersLocationsIds = [
6051
            $this->createFolder([self::ENG_US => 'P1'], $parentLocationId)->contentInfo->mainLocationId,
6052
            $this->createFolder([self::ENG_US => 'P2'], $parentLocationId)->contentInfo->mainLocationId,
6053
            $this->createFolder([self::ENG_US => 'P3'], $parentLocationId)->contentInfo->mainLocationId,
6054
        ];
6055
6056
        return array_values($this->locationService->loadLocationList($parentFoldersLocationsIds));
6057
    }
6058
6059
    /**
6060
     * Filter Locations list by hidden only.
6061
     *
6062
     * @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
6063
     *
6064
     * @return array
6065
     */
6066
    private function filterHiddenLocations(array $locations): array
6067
    {
6068
        return array_values(
6069
            array_filter(
6070
                $locations,
6071
                function (Location $location) {
6072
                    return $location->hidden;
6073
                }
6074
            )
6075
        );
6076
    }
6077
6078
    public function testPublishVersionWithSelectedLanguages()
6079
    {
6080
        $publishedContent = $this->createFolder(
6081
            [
6082
                self::ENG_US => 'Published US',
6083
                self::GER_DE => 'Published DE',
6084
            ],
6085
            $this->generateId('location', 2)
6086
        );
6087
6088
        $draft = $this->contentService->createContentDraft($publishedContent->contentInfo);
6089
        $contentUpdateStruct = new ContentUpdateStruct([
6090
            'initialLanguageCode' => self::ENG_US,
6091
        ]);
6092
        $contentUpdateStruct->setField('name', 'Draft 1 US', self::ENG_US);
6093
        $contentUpdateStruct->setField('name', 'Draft 1 DE', self::GER_DE);
6094
6095
        $this->contentService->updateContent($draft->versionInfo, $contentUpdateStruct);
6096
6097
        $this->contentService->publishVersion($draft->versionInfo, [self::GER_DE]);
6098
        $content = $this->contentService->loadContent($draft->contentInfo->id);
6099
        $this->assertEquals(
6100
            [
6101
                self::ENG_US => 'Published US',
6102
                self::GER_DE => 'Draft 1 DE',
6103
            ],
6104
            $content->fields['name']
6105
        );
6106
    }
6107
6108
    public function testCreateContentWithRomanianSpecialCharsInTitle()
6109
    {
6110
        $baseName = 'ȘșțȚdfdf';
6111
        $expectedPath = '/SstTdfdf';
6112
6113
        $this->createFolder([self::ENG_US => $baseName], 2);
6114
6115
        $urlAliasService = $this->getRepository()->getURLAliasService();
6116
        $urlAlias = $urlAliasService->lookup($expectedPath);
6117
        $this->assertSame($expectedPath, $urlAlias->path);
6118
    }
6119
6120
    /**
6121
     * @param \eZ\Publish\API\Repository\Values\Content\Content[] $drafts
6122
     *
6123
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
6124
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
6125
     */
6126
    private function createContentWithReverseRelations(array $drafts)
6127
    {
6128
        $contentWithReverseRelations = new class() {
6129
            /** @var \eZ\Publish\API\Repository\Values\Content\Content */
6130
            public $content;
6131
6132
            /** @var \eZ\Publish\API\Repository\Values\Content\Content[] */
6133
            public $reverseRelations;
6134
        };
6135
        $content = $this->createContentVersion1();
6136
        $versionInfo = $content->getVersionInfo();
6137
        $contentInfo = $versionInfo->getContentInfo();
6138
        $contentWithReverseRelations->content = $content;
6139
6140
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $draft */
6141
        foreach ($drafts as $draft) {
6142
            $this->contentService->addRelation(
6143
                $draft->getVersionInfo(),
6144
                $contentInfo
6145
            );
6146
6147
            $contentWithReverseRelations->reverseRelations[] = $this->contentService->publishVersion($draft->getVersionInfo());
6148
        }
6149
6150
        return $contentWithReverseRelations;
6151
    }
6152
}
6153