Completed
Push — EZP-30823_7.5_transaction_cach... ( 493d32 )
by André
19:29
created

ContentServiceTest   F

Complexity

Total Complexity 259

Size/Duplication

Total Lines 6758
Duplicated Lines 24.92 %

Coupling/Cohesion

Components 1
Dependencies 34

Importance

Changes 0
Metric Value
dl 1684
loc 6758
rs 0.8
c 0
b 0
f 0
wmc 259
lcom 1
cbo 34

186 Methods

Rating   Name   Duplication   Size   Complexity  
A testNewContentCreateStruct() 0 17 1
A testCreateContent() 0 28 2
A testCreateContentAndPublishWithPrivilegedAnonymousUser() 0 50 2
A testCreateContentSetsContentInfo() 0 6 1
A testCreateContentSetsExpectedContentInfo() 0 30 1
A testCreateContentSetsVersionInfo() 0 6 1
A testCreateContentSetsExpectedVersionInfo() 0 20 1
A testCreateContentSetsExpectedContentType() 0 20 1
A testCreateContentThrowsInvalidArgumentException() 0 34 2
A testCreateContentThrowsInvalidArgumentExceptionOnFieldTypeNotAccept() 19 19 1
A testCreateContentThrowsContentFieldValidationException() 0 20 1
A testCreateContentRequiredFieldMissing() 18 18 1
A testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately() 0 16 1
A testCreateContentThrowsInvalidArgumentExceptionWithLocationCreateParameter() 0 54 1
A testLoadContentInfo() 19 19 1
A testLoadContentInfoSetsExpectedContentInfo() 0 7 1
A testLoadContentInfoThrowsNotFoundException() 12 12 1
A testLoadContentInfoList() 0 15 1
A testLoadContentInfoListSkipsNotFoundItems() 10 10 1
A testLoadContentInfoByRemoteId() 0 15 1
A testLoadContentInfoByRemoteIdSetsExpectedContentInfo() 21 21 1
A testLoadContentInfoByRemoteIdThrowsNotFoundException() 0 11 1
A testLoadVersionInfoById() 21 21 1
A testLoadVersionInfoByIdSetsExpectedVersionInfo() 0 25 1
A testLoadVersionInfoByIdThrowsNotFoundException() 12 12 1
A testLoadContentByVersionInfo() 0 25 1
A testLoadContent() 19 19 1
A testLoadContentThrowsNotFoundException() 12 12 1
A contentRemoteIdVersionLanguageProvider() 0 13 1
A testLoadContentByRemoteId() 0 19 3
A testLoadContentByRemoteIdThrowsNotFoundException() 0 12 1
A testPublishVersion() 0 18 1
A testPublishVersionSetsExpectedContentInfo() 0 30 1
A testPublishVersionSetsExpectedVersionInfo() 0 28 1
A testPublishVersionSetsExpectedContentType() 0 20 1
A testPublishVersionCreatesLocationsDefinedOnCreate() 0 20 1
A testCreateContentWithLocationCreateParameterCreatesExpectedLocation() 0 26 1
A testPublishVersionThrowsBadStateException() 17 17 1
A testPublishVersionDoesNotChangePublishedDate() 0 26 1
A testCreateContentDraft() 0 20 1
A testCreateContentDraftAndLoadAccess() 21 21 1
A testCreateContentDraftSetsExpectedProperties() 0 13 1
A testCreateContentDraftSetsContentInfo() 0 25 1
A testCreateContentDraftSetsVersionInfo() 0 24 1
A testCreateContentDraftLoadVersionInfoStillLoadsPublishedVersion() 18 18 1
A testCreateContentDraftLoadContentStillLoadsPublishedVersion() 18 18 1
A testCreateContentDraftLoadContentByRemoteIdStillLoadsPublishedVersion() 18 18 1
A testCreateContentDraftLoadContentByContentInfoStillLoadsPublishedVersion() 18 18 1
A testNewContentUpdateStruct() 0 23 1
A testUpdateContent() 0 19 1
A testUpdateContentWithDifferentUser() 0 19 1
A testUpdateContentSetsExpectedFields() 0 45 1
A testUpdateContentThrowsInvalidArgumentExceptionWhenFieldTypeDoesNotAccept() 22 22 1
A testUpdateContentWhenMandatoryFieldIsEmpty() 24 24 1
A testUpdateContentValidatorIgnoresRequiredFieldsOfNotUpdatedLanguages() 0 47 1
A testUpdateContentWithNotUpdatingMandatoryField() 0 38 5
A testCreateContentDraftWithSecondParameter() 18 18 1
A testCreateContentDraftWithThirdParameter() 20 20 1
A testPublishVersionFromContentDraft() 26 26 1
A testPublishVersionFromContentDraftArchivesOldVersion() 26 26 1
A testPublishVersionFromContentDraftUpdatesContentInfoCurrentVersion() 0 8 1
A testPublishVersionFromOldContentDraftArchivesNewerVersionNo() 25 25 1
B testPublishVersionNotCreatingUnlimitedArchives() 0 62 1
A testNewContentMetadataUpdateStruct() 0 24 2
A testUpdateContentMetadata() 0 32 1
A testUpdateContentMetadataSetsExpectedProperties() 0 29 1
A testUpdateContentMetadataNotUpdatesContentVersion() 0 4 1
A testUpdateContentMetadataThrowsInvalidArgumentExceptionOnDuplicateRemoteId() 0 24 1
A testUpdateContentMetadataThrowsInvalidArgumentExceptionOnNoMetadataPropertiesSet() 0 12 1
A testDeleteContent() 21 21 2
A testDeleteContentWithEmptyBinaryField() 21 21 2
A testLoadContentDraftsReturnsEmptyArrayByDefault() 0 12 1
A testLoadContentDrafts() 0 44 1
A testLoadContentDraftsWithFirstParameter() 0 48 1
A testLoadVersionInfoWithSecondParameter() 0 23 1
A testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter() 0 14 1
A testLoadVersionInfoByIdWithSecondParameter() 0 28 1
A testLoadVersionInfoByIdWithSecondParameterSetsExpectedVersionInfo() 0 41 1
A testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter() 14 14 1
B testLoadContentByVersionInfoWithSecondParameter() 17 82 4
B testLoadContentByContentInfoWithLanguageParameters() 0 135 1
A testLoadContentByContentInfoWithVersionNumberParameter() 30 30 1
A testLoadContentByContentInfoThrowsNotFoundExceptionWithVersionNumberParameter() 14 14 1
A testLoadContentWithSecondParameter() 17 17 1
A testLoadContentWithSecondParameterThrowsNotFoundException() 0 8 1
A testLoadContentWithThirdParameter() 23 23 1
A testLoadContentThrowsNotFoundExceptionWithThirdParameter() 14 14 1
A testLoadContentByRemoteIdWithSecondParameter() 22 22 1
A testLoadContentByRemoteIdWithThirdParameter() 27 27 1
A testLoadContentByRemoteIdThrowsNotFoundExceptionWithThirdParameter() 0 18 1
A testLoadContentWithPrioritizedLanguagesList() 0 20 2
A getPrioritizedLanguageList() 0 9 1
A testDeleteVersion() 0 26 1
A testDeleteVersionThrowsBadStateExceptionOnPublishedVersion() 0 14 1
A testDeleteVersionWorksIfOnlyVersionIsDraft() 0 16 1
A testLoadVersions() 0 22 1
A testLoadVersionsSetsExpectedVersionInfo() 0 43 1
B testCopyContent() 59 59 1
A testCopyContentWithNewOwner() 0 45 1
B testCopyContentWithThirdParameter() 56 56 1
A testAddRelation() 0 28 1
A testAddRelationAddsRelationToContent() 0 7 1
A assertExpectedRelations() 0 17 1
A testAddRelationSetsExpectedRelations() 0 4 1
A testCreateContentDraftWithRelations() 22 22 1
A testCreateContentDraftWithRelationsCreatesRelations() 0 9 1
A testCreateContentDraftWithRelationsCreatesExpectedRelations() 0 4 1
A testAddRelationThrowsBadStateException() 22 22 1
B testLoadRelations() 0 67 1
B testLoadRelationsSkipsArchivedContent() 0 58 1
A testLoadRelationsSkipsDraftContent() 0 54 1
B testLoadReverseRelations() 0 86 1
B testLoadReverseRelationsSkipsArchivedContent() 0 76 1
B testLoadReverseRelationsSkipsDraftContent() 0 68 1
A testDeleteRelation() 0 30 1
A testDeleteRelationThrowsBadStateException() 34 34 1
A testDeleteRelationThrowsInvalidArgumentException() 23 23 1
A testCreateContentInTransactionWithRollback() 0 47 4
A testCreateContentInTransactionWithCommit() 0 42 3
A testCreateContentWithLocationCreateParameterInTransactionWithCommit() 29 29 2
A testCreateContentDraftInTransactionWithRollback() 0 42 3
A testCreateContentDraftInTransactionWithCommit() 40 40 2
A testPublishVersionInTransactionWithRollback() 44 44 3
A testPublishVersionInTransactionWithCommit() 40 40 2
A testUpdateContentInTransactionWithRollback() 47 47 2
A testUpdateContentInTransactionWithCommit() 47 47 2
A testUpdateContentMetadataInTransactionWithRollback() 45 45 2
A testUpdateContentMetadataInTransactionWithCommit() 45 45 2
A testUpdateContentThrowsContentFieldValidationException() 24 24 1
A testUpdateContentThrowsBadStateException() 24 24 1
A testLoadVersionInfo() 0 22 1
A testLoadContentByContentInfo() 0 22 1
A testCreateContentWithLocationCreateParameterInTransactionWithRollback() 33 33 3
A testUpdateContentMetadataCheckWithinTransaction() 38 38 2
A testDeleteVersionInTransactionWithRollback() 36 36 2
A testDeleteVersionInTransactionWithCommit() 36 36 2
A testDeleteContentInTransactionWithRollback() 36 36 2
A testDeleteContentInTransactionWithCommit() 40 40 3
A testCopyContentInTransactionWithRollback() 50 50 2
A testCopyContentInTransactionWithCommit() 50 50 2
A testURLAliasesCreatedForNewContent() 0 36 1
B testURLAliasesCreatedForUpdatedContent() 0 72 1
B testCustomURLAliasesNotHistorizedOnUpdatedContent() 0 59 1
A testUpdatingDraftDoesNotUpdateOldVersions() 0 16 1
A testPublishWorkflow() 0 38 1
A testPublishContentWithoutPublishPolicyThrowsException() 0 19 1
A testDeleteTranslation() 21 21 2
A testDeleteTranslationUpdatesInitialLanguageCodeVersion() 0 31 1
A testDeleteTranslationUpdatesUrlAlias() 0 37 4
A testDeleteTranslationMainLanguageThrowsBadStateException() 0 12 1
A testDeleteTranslationDeletesSingleTranslationVersions() 0 24 1
A testDeleteTranslationThrowsUnauthorizedException() 0 23 1
A testDeleteTranslationThrowsInvalidArgumentException() 0 8 1
A testDeleteTranslationFromDraft() 0 15 1
A providerForDeleteTranslationFromDraftRemovesUrlAliasOnPublishing() 0 11 1
B testDeleteTranslationFromDraftRemovesUrlAliasOnPublishing() 0 60 3
B testDeleteTranslationFromDraftArchivesUrlAliasOnPublishing() 0 55 1
A testDeleteTranslationFromDraftThrowsBadStateExceptionOnSingleTranslation() 0 31 1
A testDeleteTranslationFromDraftThrowsBadStateExceptionOnMainTranslation() 0 19 1
A testDeleteTranslationFromDraftThrowsBadStateExceptionOnPublishedVersion() 11 11 1
A testDeleteTranslationFromDraftThrowsUnauthorizedException() 0 26 1
A testDeleteTranslationFromDraftThrowsInvalidArgumentException() 0 10 1
A testLoadContentListByContentInfo() 0 27 4
A testLoadVersionsAfterDeletingTwoDrafts() 0 30 3
A testLoadVersionsOfStatusDraft() 0 16 1
A testLoadVersionsOfStatusArchived() 0 18 1
A assertAliasesCorrect() 0 38 5
A assertAllFieldsEquals() 0 7 1
A assertLocaleFieldsEquals() 0 14 3
A normalizeFields() 18 27 4
A createLocaleFieldsFixture() 0 11 4
A assertDefaultContentStates() 19 19 3
A assertTranslationDoesNotExist() 0 22 3
A createFieldsFixture() 0 41 1
A getExpectedMediaContentInfoProperties() 19 19 1
A testHideContent() 0 39 1
A testRevealContent() 0 52 1
B testRevealContentWithHiddenParent() 11 58 3
B testRevealContentWithHiddenChildren() 11 68 3
A testHideContentWithParentLocation() 0 52 1
A testChangeContentName() 0 24 1
B testCopyTranslationsFromPublishedToDraft() 0 66 1
A createParentLocationsForHideReveal() 0 10 1
A filterHiddenLocations() 0 11 1
A testPublishVersionWithSelectedLanguages() 0 33 1
A testCreateContentWithRomanianSpecialCharsInTitle() 0 13 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ContentServiceTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ContentServiceTest, and based on these observations, apply Extract Interface, too.

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\LocationService;
12
use eZ\Publish\API\Repository\Values\Content\Content;
13
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
14
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
15
use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct;
16
use eZ\Publish\API\Repository\Values\Content\Field;
17
use eZ\Publish\API\Repository\Values\Content\Location;
18
use eZ\Publish\API\Repository\Values\Content\URLAlias;
19
use eZ\Publish\API\Repository\Values\Content\Relation;
20
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
21
use eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation;
22
use eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation;
23
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation;
24
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
25
use DOMDocument;
26
use Exception;
27
use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct;
28
29
/**
30
 * Test case for operations in the ContentService using in memory storage.
31
 *
32
 * @see \eZ\Publish\API\Repository\ContentService
33
 * @group content
34
 */
35
class ContentServiceTest extends BaseContentServiceTest
36
{
37
    /**
38
     * Test for the newContentCreateStruct() method.
39
     *
40
     * @see \eZ\Publish\API\Repository\ContentService::newContentCreateStruct()
41
     * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testLoadContentTypeByIdentifier
42
     * @group user
43
     * @group field-type
44
     */
45
    public function testNewContentCreateStruct()
46
    {
47
        $repository = $this->getRepository();
48
49
        /* BEGIN: Use Case */
50
        // Create a content type
51
        $contentTypeService = $repository->getContentTypeService();
52
53
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
54
55
        $contentService = $repository->getContentService();
56
57
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
58
        /* END: Use Case */
59
60
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentCreateStruct', $contentCreate);
61
    }
62
63
    /**
64
     * Test for the createContent() method.
65
     *
66
     * @return \eZ\Publish\API\Repository\Values\Content\Content
67
     *
68
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
69
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentCreateStruct
70
     * @group user
71
     * @group field-type
72
     */
73
    public function testCreateContent()
74
    {
75
        if ($this->isVersion4()) {
76
            $this->markTestSkipped('This test requires eZ Publish 5');
77
        }
78
79
        $repository = $this->getRepository();
80
81
        /* BEGIN: Use Case */
82
        $contentTypeService = $repository->getContentTypeService();
83
84
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
85
86
        $contentService = $repository->getContentService();
87
88
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
89
        $contentCreate->setField('name', 'My awesome forum');
90
91
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
92
        $contentCreate->alwaysAvailable = true;
93
94
        $content = $contentService->createContent($contentCreate);
95
        /* END: Use Case */
96
97
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content', $content);
98
99
        return $content;
100
    }
101
102
    /**
103
     * Test for the createContent() method.
104
     *
105
     * Tests made for issue #EZP-20955 where Anonymous user is granted access to create content
106
     * and should have access to do that.
107
     *
108
     * @return \eZ\Publish\API\Repository\Values\Content\Content
109
     *
110
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
111
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentCreateStruct
112
     * @group user
113
     * @group field-type
114
     */
115
    public function testCreateContentAndPublishWithPrivilegedAnonymousUser()
116
    {
117
        if ($this->isVersion4()) {
118
            $this->markTestSkipped('This test requires eZ Publish 5');
119
        }
120
121
        $anonymousUserId = $this->generateId('user', 10);
122
123
        $repository = $this->getRepository();
124
        $contentService = $repository->getContentService();
125
        $contentTypeService = $repository->getContentTypeService();
126
        $locationService = $repository->getLocationService();
127
        $roleService = $repository->getRoleService();
128
129
        // Give Anonymous user role additional rights
130
        $role = $roleService->loadRoleByIdentifier('Anonymous');
131
        $roleDraft = $roleService->createRoleDraft($role);
132
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'create');
133
        $policyCreateStruct->addLimitation(new SectionLimitation(['limitationValues' => [1]]));
134
        $policyCreateStruct->addLimitation(new LocationLimitation(['limitationValues' => [2]]));
135
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(['limitationValues' => [1]]));
136
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
137
138
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'publish');
139
        $policyCreateStruct->addLimitation(new SectionLimitation(['limitationValues' => [1]]));
140
        $policyCreateStruct->addLimitation(new LocationLimitation(['limitationValues' => [2]]));
141
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(['limitationValues' => [1]]));
142
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
143
        $roleService->publishRoleDraft($roleDraft);
144
145
        // Set Anonymous user as current
146
        $repository->getPermissionResolver()->setCurrentUserReference($repository->getUserService()->loadUser($anonymousUserId));
147
148
        // Create a new content object:
149
        $contentCreate = $contentService->newContentCreateStruct(
150
            $contentTypeService->loadContentTypeByIdentifier('folder'),
151
            'eng-GB'
152
        );
153
154
        $contentCreate->setField('name', 'Folder 1');
155
156
        $content = $contentService->createContent(
157
            $contentCreate,
158
            [$locationService->newLocationCreateStruct(2)]
159
        );
160
161
        $contentService->publishVersion(
162
            $content->getVersionInfo()
163
        );
164
    }
165
166
    /**
167
     * Test for the createContent() method.
168
     *
169
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
170
     *
171
     * @return \eZ\Publish\API\Repository\Values\Content\Content
172
     *
173
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
174
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
175
     */
176
    public function testCreateContentSetsContentInfo($content)
177
    {
178
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $content->contentInfo);
179
180
        return $content;
181
    }
182
183
    /**
184
     * Test for the createContent() method.
185
     *
186
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
187
     *
188
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
189
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsContentInfo
190
     */
191
    public function testCreateContentSetsExpectedContentInfo($content)
192
    {
193
        $this->assertEquals(
194
            [
195
                $content->id,
196
                28, // id of content type "forum"
197
                true,
198
                1,
199
                'abcdef0123456789abcdef0123456789',
200
                'eng-US',
201
                $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...
202
                false,
203
                null,
204
                // Main Location id for unpublished Content should be null
205
                null,
206
            ],
207
            [
208
                $content->contentInfo->id,
209
                $content->contentInfo->contentTypeId,
210
                $content->contentInfo->alwaysAvailable,
211
                $content->contentInfo->currentVersionNo,
212
                $content->contentInfo->remoteId,
213
                $content->contentInfo->mainLanguageCode,
214
                $content->contentInfo->ownerId,
215
                $content->contentInfo->published,
216
                $content->contentInfo->publishedDate,
217
                $content->contentInfo->mainLocationId,
218
            ]
219
        );
220
    }
221
222
    /**
223
     * Test for the createContent() method.
224
     *
225
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
226
     *
227
     * @return \eZ\Publish\API\Repository\Values\Content\Content
228
     *
229
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
230
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
231
     */
232
    public function testCreateContentSetsVersionInfo($content)
233
    {
234
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo', $content->getVersionInfo());
235
236
        return $content;
237
    }
238
239
    /**
240
     * Test for the createContent() method.
241
     *
242
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
243
     *
244
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
245
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsVersionInfo
246
     */
247
    public function testCreateContentSetsExpectedVersionInfo($content)
248
    {
249
        $this->assertEquals(
250
            [
251
                'status' => VersionInfo::STATUS_DRAFT,
252
                'versionNo' => 1,
253
                '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...
254
                'initialLanguageCode' => 'eng-US',
255
            ],
256
            [
257
                'status' => $content->getVersionInfo()->status,
258
                'versionNo' => $content->getVersionInfo()->versionNo,
259
                'creatorId' => $content->getVersionInfo()->creatorId,
260
                'initialLanguageCode' => $content->getVersionInfo()->initialLanguageCode,
261
            ]
262
        );
263
        $this->assertTrue($content->getVersionInfo()->isDraft());
264
        $this->assertFalse($content->getVersionInfo()->isPublished());
265
        $this->assertFalse($content->getVersionInfo()->isArchived());
266
    }
267
268
    /**
269
     * Test for the createContent() method.
270
     *
271
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
272
     *
273
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
274
     * @depends testCreateContent
275
     */
276
    public function testCreateContentSetsExpectedContentType($content)
277
    {
278
        $contentType = $content->getContentType();
279
280
        $this->assertEquals(
281
            [
282
                $contentType->id,
283
                // Won't match as it's set to true in createContentDraftVersion1()
284
                //$contentType->defaultAlwaysAvailable,
285
                //$contentType->defaultSortField,
286
                //$contentType->defaultSortOrder,
287
            ],
288
            [
289
                $content->contentInfo->contentTypeId,
290
                //$content->contentInfo->alwaysAvailable,
291
                //$location->sortField,
292
                //$location->sortOrder,
293
            ]
294
        );
295
    }
296
297
    /**
298
     * Test for the createContent() method.
299
     *
300
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
301
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
302
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
303
     */
304
    public function testCreateContentThrowsInvalidArgumentException()
305
    {
306
        if ($this->isVersion4()) {
307
            $this->markTestSkipped('This test requires eZ Publish 5');
308
        }
309
310
        $repository = $this->getRepository();
311
312
        /* BEGIN: Use Case */
313
        $contentTypeService = $repository->getContentTypeService();
314
        $contentService = $repository->getContentService();
315
316
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
317
318
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
319
        $contentCreate1->setField('name', 'An awesome Sidelfingen forum');
320
321
        $contentCreate1->remoteId = 'abcdef0123456789abcdef0123456789';
322
        $contentCreate1->alwaysAvailable = true;
323
324
        $draft = $contentService->createContent($contentCreate1);
325
        $contentService->publishVersion($draft->versionInfo);
326
327
        $contentCreate2 = $contentService->newContentCreateStruct($contentType, 'eng-GB');
328
        $contentCreate2->setField('name', 'An awesome Bielefeld forum');
329
330
        $contentCreate2->remoteId = 'abcdef0123456789abcdef0123456789';
331
        $contentCreate2->alwaysAvailable = false;
332
333
        // This call will fail with an "InvalidArgumentException", because the
334
        // remoteId is already in use.
335
        $contentService->createContent($contentCreate2);
336
        /* END: Use Case */
337
    }
338
339
    /**
340
     * Test for the createContent() method.
341
     *
342
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
343
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
344
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
345
     */
346 View Code Duplication
    public function testCreateContentThrowsInvalidArgumentExceptionOnFieldTypeNotAccept()
347
    {
348
        $repository = $this->getRepository();
349
350
        /* BEGIN: Use Case */
351
        $contentTypeService = $repository->getContentTypeService();
352
        $contentService = $repository->getContentService();
353
354
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
355
356
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
357
        // The name field does only accept strings and null as its values
358
        $contentCreate->setField('name', new \stdClass());
359
360
        // Throws InvalidArgumentException since the name field is filled
361
        // improperly
362
        $draft = $contentService->createContent($contentCreate);
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

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

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

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

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

Loading history...
363
        /* END: Use Case */
364
    }
365
366
    /**
367
     * Test for the createContent() method.
368
     *
369
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
370
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
371
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
372
     */
373
    public function testCreateContentThrowsContentFieldValidationException()
374
    {
375
        $repository = $this->getRepository();
376
377
        /* BEGIN: Use Case */
378
        $contentTypeService = $repository->getContentTypeService();
379
        $contentService = $repository->getContentService();
380
381
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
382
383
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
384
        $contentCreate1->setField('name', 'An awesome Sidelfingen folder');
385
        // Violates string length constraint
386
        $contentCreate1->setField('short_name', str_repeat('a', 200));
387
388
        // Throws ContentFieldValidationException, since short_name does not pass
389
        // validation of the string length validator
390
        $draft = $contentService->createContent($contentCreate1);
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

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

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

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

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

Loading history...
391
        /* END: Use Case */
392
    }
393
394
    /**
395
     * Test for the createContent() method.
396
     *
397
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
398
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
399
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
400
     */
401 View Code Duplication
    public function testCreateContentRequiredFieldMissing()
402
    {
403
        $repository = $this->getRepository();
404
405
        /* BEGIN: Use Case */
406
        $contentTypeService = $repository->getContentTypeService();
407
        $contentService = $repository->getContentService();
408
409
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
410
411
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
412
        // Required field "name" is not set
413
414
        // Throws a ContentFieldValidationException, since a required field is
415
        // missing
416
        $draft = $contentService->createContent($contentCreate1);
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

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

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

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

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

Loading history...
417
        /* END: Use Case */
418
    }
419
420
    /**
421
     * Test for the createContent() method.
422
     *
423
     * NOTE: We have bidirectional dependencies between the ContentService and
424
     * the LocationService, so that we cannot use PHPUnit's test dependencies
425
     * here.
426
     *
427
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
428
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
429
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationByRemoteId
430
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
431
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
432
     * @group user
433
     */
434
    public function testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately()
435
    {
436
        $repository = $this->getRepository();
437
438
        $locationService = $repository->getLocationService();
439
440
        /* BEGIN: Use Case */
441
        $draft = $this->createContentDraftVersion1();
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

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

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

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

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

Loading history...
442
443
        // The location will not have been created, yet, so this throws an
444
        // exception
445
        $location = $locationService->loadLocationByRemoteId(
0 ignored issues
show
Unused Code introduced by
$location is not used, you could remove the assignment.

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

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

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

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

Loading history...
446
            '0123456789abcdef0123456789abcdef'
447
        );
448
        /* END: Use Case */
449
    }
450
451
    /**
452
     * Test for the createContent() method.
453
     *
454
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
455
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
456
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
457
     */
458
    public function testCreateContentThrowsInvalidArgumentExceptionWithLocationCreateParameter()
459
    {
460
        $repository = $this->getRepository();
461
462
        $parentLocationId = $this->generateId('location', 56);
463
        /* BEGIN: Use Case */
464
        // $parentLocationId is a valid location ID
465
466
        $contentService = $repository->getContentService();
467
        $contentTypeService = $repository->getContentTypeService();
468
        $locationService = $repository->getLocationService();
469
470
        // Load content type
471
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
472
473
        // Configure new locations
474
        $locationCreate1 = $locationService->newLocationCreateStruct($parentLocationId);
475
476
        $locationCreate1->priority = 23;
477
        $locationCreate1->hidden = true;
478
        $locationCreate1->remoteId = '0123456789abcdef0123456789aaaaaa';
479
        $locationCreate1->sortField = Location::SORT_FIELD_NODE_ID;
480
        $locationCreate1->sortOrder = Location::SORT_ORDER_DESC;
481
482
        $locationCreate2 = $locationService->newLocationCreateStruct($parentLocationId);
483
484
        $locationCreate2->priority = 42;
485
        $locationCreate2->hidden = true;
486
        $locationCreate2->remoteId = '0123456789abcdef0123456789bbbbbb';
487
        $locationCreate2->sortField = Location::SORT_FIELD_NODE_ID;
488
        $locationCreate2->sortOrder = Location::SORT_ORDER_DESC;
489
490
        // Configure new content object
491
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
492
493
        $contentCreate->setField('name', 'A awesome Sindelfingen forum');
494
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
495
        $contentCreate->alwaysAvailable = true;
496
497
        // Create new content object under the specified location
498
        $draft = $contentService->createContent(
499
            $contentCreate,
500
            [$locationCreate1]
501
        );
502
        $contentService->publishVersion($draft->versionInfo);
503
504
        // This call will fail with an "InvalidArgumentException", because the
505
        // Content remoteId already exists,
506
        $contentService->createContent(
507
            $contentCreate,
508
            [$locationCreate2]
509
        );
510
        /* END: Use Case */
511
    }
512
513
    /**
514
     * Test for the loadContentInfo() method.
515
     *
516
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
517
     * @group user
518
     */
519 View Code Duplication
    public function testLoadContentInfo()
520
    {
521
        $repository = $this->getRepository();
522
523
        $mediaFolderId = $this->generateId('object', 41);
524
        /* BEGIN: Use Case */
525
        $contentService = $repository->getContentService();
526
527
        // Load the ContentInfo for "Media" folder
528
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
529
        /* END: Use Case */
530
531
        $this->assertInstanceOf(
532
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
533
            $contentInfo
534
        );
535
536
        return $contentInfo;
537
    }
538
539
    /**
540
     * Test for the returned value of the loadContentInfo() method.
541
     *
542
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
543
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfo
544
     *
545
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
546
     */
547
    public function testLoadContentInfoSetsExpectedContentInfo(ContentInfo $contentInfo)
548
    {
549
        $this->assertPropertiesCorrectUnsorted(
550
            $this->getExpectedMediaContentInfoProperties(),
551
            $contentInfo
552
        );
553
    }
554
555
    /**
556
     * Test for the loadContentInfo() method.
557
     *
558
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
559
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
560
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
561
     */
562 View Code Duplication
    public function testLoadContentInfoThrowsNotFoundException()
563
    {
564
        $repository = $this->getRepository();
565
566
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
567
        /* BEGIN: Use Case */
568
        $contentService = $repository->getContentService();
569
570
        // This call will fail with a NotFoundException
571
        $contentService->loadContentInfo($nonExistentContentId);
572
        /* END: Use Case */
573
    }
574
575
    /**
576
     * Test for the loadContentInfoList() method.
577
     *
578
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoList()
579
     */
580
    public function testLoadContentInfoList()
581
    {
582
        $repository = $this->getRepository();
583
584
        $mediaFolderId = $this->generateId('object', 41);
585
        $contentService = $repository->getContentService();
586
        $list = $contentService->loadContentInfoList([$mediaFolderId]);
587
588
        $this->assertCount(1, $list);
589
        $this->assertEquals([$mediaFolderId], array_keys($list), 'Array key was not content id');
590
        $this->assertInstanceOf(
591
            ContentInfo::class,
592
            $list[$mediaFolderId]
593
        );
594
    }
595
596
    /**
597
     * Test for the loadContentInfoList() method.
598
     *
599
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoList()
600
     * @depends testLoadContentInfoList
601
     */
602 View Code Duplication
    public function testLoadContentInfoListSkipsNotFoundItems()
603
    {
604
        $repository = $this->getRepository();
605
606
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
607
        $contentService = $repository->getContentService();
608
        $list = $contentService->loadContentInfoList([$nonExistentContentId]);
609
610
        $this->assertCount(0, $list);
611
    }
612
613
    /**
614
     * Test for the loadContentInfoByRemoteId() method.
615
     *
616
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
617
     */
618
    public function testLoadContentInfoByRemoteId()
619
    {
620
        $repository = $this->getRepository();
621
622
        /* BEGIN: Use Case */
623
        $contentService = $repository->getContentService();
624
625
        // Load the ContentInfo for "Media" folder
626
        $contentInfo = $contentService->loadContentInfoByRemoteId('faaeb9be3bd98ed09f606fc16d144eca');
627
        /* END: Use Case */
628
629
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $contentInfo);
630
631
        return $contentInfo;
632
    }
633
634
    /**
635
     * Test for the returned value of the loadContentInfoByRemoteId() method.
636
     *
637
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
638
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId
639
     *
640
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
641
     */
642 View Code Duplication
    public function testLoadContentInfoByRemoteIdSetsExpectedContentInfo(ContentInfo $contentInfo)
643
    {
644
        $this->assertPropertiesCorrectUnsorted(
645
            [
646
                'id' => 10,
647
                'contentTypeId' => 4,
648
                'name' => 'Anonymous User',
649
                'sectionId' => 2,
650
                'currentVersionNo' => 2,
651
                'published' => true,
652
                'ownerId' => 14,
653
                'modificationDate' => $this->createDateTime(1072180405),
654
                'publishedDate' => $this->createDateTime(1033920665),
655
                'alwaysAvailable' => 1,
656
                'remoteId' => 'faaeb9be3bd98ed09f606fc16d144eca',
657
                'mainLanguageCode' => 'eng-US',
658
                'mainLocationId' => 45,
659
            ],
660
            $contentInfo
661
        );
662
    }
663
664
    /**
665
     * Test for the loadContentInfoByRemoteId() method.
666
     *
667
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
668
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
669
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
670
     */
671
    public function testLoadContentInfoByRemoteIdThrowsNotFoundException()
672
    {
673
        $repository = $this->getRepository();
674
675
        /* BEGIN: Use Case */
676
        $contentService = $repository->getContentService();
677
678
        // This call will fail with a NotFoundException
679
        $contentService->loadContentInfoByRemoteId('abcdefghijklmnopqrstuvwxyz0123456789');
680
        /* END: Use Case */
681
    }
682
683
    /**
684
     * Test for the loadVersionInfo() method.
685
     *
686
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
687
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
688
     * @group user
689
     */
690
    public function testLoadVersionInfo()
691
    {
692
        $repository = $this->getRepository();
693
694
        $mediaFolderId = $this->generateId('object', 41);
695
        /* BEGIN: Use Case */
696
        // $mediaFolderId contains the ID of the "Media" folder
697
698
        $contentService = $repository->getContentService();
699
700
        // Load the ContentInfo for "Media" folder
701
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
702
703
        // Now load the current version info of the "Media" folder
704
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
705
        /* END: Use Case */
706
707
        $this->assertInstanceOf(
708
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
709
            $versionInfo
710
        );
711
    }
712
713
    /**
714
     * Test for the loadVersionInfoById() method.
715
     *
716
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
717
     */
718 View Code Duplication
    public function testLoadVersionInfoById()
719
    {
720
        $repository = $this->getRepository();
721
722
        $mediaFolderId = $this->generateId('object', 41);
723
        /* BEGIN: Use Case */
724
        // $mediaFolderId contains the ID of the "Media" folder
725
726
        $contentService = $repository->getContentService();
727
728
        // Load the VersionInfo for "Media" folder
729
        $versionInfo = $contentService->loadVersionInfoById($mediaFolderId);
730
        /* END: Use Case */
731
732
        $this->assertInstanceOf(
733
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
734
            $versionInfo
735
        );
736
737
        return $versionInfo;
738
    }
739
740
    /**
741
     * Test for the returned value of the loadVersionInfoById() method.
742
     *
743
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
744
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
745
     *
746
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
747
     */
748
    public function testLoadVersionInfoByIdSetsExpectedVersionInfo(VersionInfo $versionInfo)
749
    {
750
        $this->assertPropertiesCorrect(
751
            [
752
                'names' => [
753
                    'eng-US' => 'Media',
754
                ],
755
                'contentInfo' => new ContentInfo($this->getExpectedMediaContentInfoProperties()),
756
                'id' => 472,
757
                'versionNo' => 1,
758
                'modificationDate' => $this->createDateTime(1060695457),
759
                'creatorId' => 14,
760
                'creationDate' => $this->createDateTime(1060695450),
761
                'status' => VersionInfo::STATUS_PUBLISHED,
762
                'initialLanguageCode' => 'eng-US',
763
                'languageCodes' => [
764
                    'eng-US',
765
                ],
766
            ],
767
            $versionInfo
768
        );
769
        $this->assertTrue($versionInfo->isPublished());
770
        $this->assertFalse($versionInfo->isDraft());
771
        $this->assertFalse($versionInfo->isArchived());
772
    }
773
774
    /**
775
     * Test for the loadVersionInfoById() method.
776
     *
777
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
778
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
779
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
780
     */
781 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundException()
782
    {
783
        $repository = $this->getRepository();
784
785
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
786
        /* BEGIN: Use Case */
787
        $contentService = $repository->getContentService();
788
789
        // This call will fail with a "NotFoundException"
790
        $contentService->loadVersionInfoById($nonExistentContentId);
791
        /* END: Use Case */
792
    }
793
794
    /**
795
     * Test for the loadContentByContentInfo() method.
796
     *
797
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
798
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
799
     */
800
    public function testLoadContentByContentInfo()
801
    {
802
        $repository = $this->getRepository();
803
804
        $mediaFolderId = $this->generateId('object', 41);
805
        /* BEGIN: Use Case */
806
        // $mediaFolderId contains the ID of the "Media" folder
807
808
        $contentService = $repository->getContentService();
809
810
        // Load the ContentInfo for "Media" folder
811
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
812
813
        // Now load the current content version for the info instance
814
        $content = $contentService->loadContentByContentInfo($contentInfo);
815
        /* END: Use Case */
816
817
        $this->assertInstanceOf(
818
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
819
            $content
820
        );
821
    }
822
823
    /**
824
     * Test for the loadContentByVersionInfo() method.
825
     *
826
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
827
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
828
     */
829
    public function testLoadContentByVersionInfo()
830
    {
831
        $repository = $this->getRepository();
832
833
        $mediaFolderId = $this->generateId('object', 41);
834
        /* BEGIN: Use Case */
835
        // $mediaFolderId contains the ID of the "Media" folder
836
837
        $contentService = $repository->getContentService();
838
839
        // Load the ContentInfo for "Media" folder
840
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
841
842
        // Load the current VersionInfo
843
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
844
845
        // Now load the current content version for the info instance
846
        $content = $contentService->loadContentByVersionInfo($versionInfo);
847
        /* END: Use Case */
848
849
        $this->assertInstanceOf(
850
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
851
            $content
852
        );
853
    }
854
855
    /**
856
     * Test for the loadContent() method.
857
     *
858
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
859
     * @group user
860
     * @group field-type
861
     */
862 View Code Duplication
    public function testLoadContent()
863
    {
864
        $repository = $this->getRepository();
865
866
        $mediaFolderId = $this->generateId('object', 41);
867
        /* BEGIN: Use Case */
868
        // $mediaFolderId contains the ID of the "Media" folder
869
870
        $contentService = $repository->getContentService();
871
872
        // Load the Content for "Media" folder, any language and current version
873
        $content = $contentService->loadContent($mediaFolderId);
874
        /* END: Use Case */
875
876
        $this->assertInstanceOf(
877
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
878
            $content
879
        );
880
    }
881
882
    /**
883
     * Test for the loadContent() method.
884
     *
885
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
886
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
887
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
888
     */
889 View Code Duplication
    public function testLoadContentThrowsNotFoundException()
890
    {
891
        $repository = $this->getRepository();
892
893
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
894
        /* BEGIN: Use Case */
895
        $contentService = $repository->getContentService();
896
897
        // This call will fail with a "NotFoundException"
898
        $contentService->loadContent($nonExistentContentId);
899
        /* END: Use Case */
900
    }
901
902
    /**
903
     * Data provider for testLoadContentByRemoteId().
904
     *
905
     * @return array
906
     */
907
    public function contentRemoteIdVersionLanguageProvider()
908
    {
909
        return [
910
            ['f5c88a2209584891056f987fd965b0ba', null, null],
911
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], null],
912
            ['f5c88a2209584891056f987fd965b0ba', null, 1],
913
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], 1],
914
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, null],
915
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], null],
916
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, 1],
917
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], 1],
918
        ];
919
    }
920
921
    /**
922
     * Test for the loadContentByRemoteId() method.
923
     *
924
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId
925
     * @dataProvider contentRemoteIdVersionLanguageProvider
926
     *
927
     * @param string $remoteId
928
     * @param array|null $languages
929
     * @param int $versionNo
930
     */
931
    public function testLoadContentByRemoteId($remoteId, $languages, $versionNo)
932
    {
933
        $repository = $this->getRepository();
934
935
        $contentService = $repository->getContentService();
936
937
        $content = $contentService->loadContentByRemoteId($remoteId, $languages, $versionNo);
938
939
        $this->assertInstanceOf(
940
            Content::class,
941
            $content
942
        );
943
944
        $this->assertEquals($remoteId, $content->contentInfo->remoteId);
945
        if ($languages !== null) {
946
            $this->assertEquals($languages, $content->getVersionInfo()->languageCodes);
947
        }
948
        $this->assertEquals($versionNo ?: 1, $content->getVersionInfo()->versionNo);
949
    }
950
951
    /**
952
     * Test for the loadContentByRemoteId() method.
953
     *
954
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
955
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
956
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
957
     */
958
    public function testLoadContentByRemoteIdThrowsNotFoundException()
959
    {
960
        $repository = $this->getRepository();
961
962
        /* BEGIN: Use Case */
963
        $contentService = $repository->getContentService();
964
965
        // This call will fail with a "NotFoundException", because no content
966
        // object exists for the given remoteId
967
        $contentService->loadContentByRemoteId('a1b1c1d1e1f1a2b2c2d2e2f2a3b3c3d3');
968
        /* END: Use Case */
969
    }
970
971
    /**
972
     * Test for the publishVersion() method.
973
     *
974
     * @return \eZ\Publish\API\Repository\Values\Content\Content
975
     *
976
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
977
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
978
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
979
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
980
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
981
     * @group user
982
     * @group field-type
983
     */
984
    public function testPublishVersion()
985
    {
986
        $time = time();
987
        /* BEGIN: Use Case */
988
        $content = $this->createContentVersion1();
989
        /* END: Use Case */
990
991
        $this->assertInstanceOf(Content::class, $content);
992
        $this->assertTrue($content->contentInfo->published);
993
        $this->assertEquals(VersionInfo::STATUS_PUBLISHED, $content->versionInfo->status);
994
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->publishedDate->getTimestamp());
995
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->modificationDate->getTimestamp());
996
        $this->assertTrue($content->versionInfo->isPublished());
997
        $this->assertFalse($content->versionInfo->isDraft());
998
        $this->assertFalse($content->versionInfo->isArchived());
999
1000
        return $content;
1001
    }
1002
1003
    /**
1004
     * Test for the publishVersion() method.
1005
     *
1006
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1007
     *
1008
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1009
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1010
     */
1011
    public function testPublishVersionSetsExpectedContentInfo($content)
1012
    {
1013
        $this->assertEquals(
1014
            [
1015
                $content->id,
1016
                true,
1017
                1,
1018
                'abcdef0123456789abcdef0123456789',
1019
                'eng-US',
1020
                $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...
1021
                true,
1022
            ],
1023
            [
1024
                $content->contentInfo->id,
1025
                $content->contentInfo->alwaysAvailable,
1026
                $content->contentInfo->currentVersionNo,
1027
                $content->contentInfo->remoteId,
1028
                $content->contentInfo->mainLanguageCode,
1029
                $content->contentInfo->ownerId,
1030
                $content->contentInfo->published,
1031
            ]
1032
        );
1033
1034
        $this->assertNotNull($content->contentInfo->mainLocationId);
1035
        $date = new \DateTime('1984/01/01');
1036
        $this->assertGreaterThan(
1037
            $date->getTimestamp(),
1038
            $content->contentInfo->publishedDate->getTimestamp()
1039
        );
1040
    }
1041
1042
    /**
1043
     * Test for the publishVersion() method.
1044
     *
1045
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1046
     *
1047
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1048
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1049
     */
1050
    public function testPublishVersionSetsExpectedVersionInfo($content)
1051
    {
1052
        $this->assertEquals(
1053
            [
1054
                $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...
1055
                'eng-US',
1056
                VersionInfo::STATUS_PUBLISHED,
1057
                1,
1058
            ],
1059
            [
1060
                $content->getVersionInfo()->creatorId,
1061
                $content->getVersionInfo()->initialLanguageCode,
1062
                $content->getVersionInfo()->status,
1063
                $content->getVersionInfo()->versionNo,
1064
            ]
1065
        );
1066
1067
        $date = new \DateTime('1984/01/01');
1068
        $this->assertGreaterThan(
1069
            $date->getTimestamp(),
1070
            $content->getVersionInfo()->modificationDate->getTimestamp()
1071
        );
1072
1073
        $this->assertNotNull($content->getVersionInfo()->modificationDate);
1074
        $this->assertTrue($content->getVersionInfo()->isPublished());
1075
        $this->assertFalse($content->getVersionInfo()->isDraft());
1076
        $this->assertFalse($content->getVersionInfo()->isArchived());
1077
    }
1078
1079
    /**
1080
     * Test for the publishVersion() method.
1081
     *
1082
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1083
     *
1084
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1085
     * @depends testPublishVersion
1086
     */
1087
    public function testPublishVersionSetsExpectedContentType($content)
1088
    {
1089
        $contentType = $content->getContentType();
1090
1091
        $this->assertEquals(
1092
            [
1093
                $contentType->id,
1094
                // won't be a match as it's set to true in createContentDraftVersion1()
1095
                //$contentType->defaultAlwaysAvailable,
1096
                //$contentType->defaultSortField,
1097
                //$contentType->defaultSortOrder,
1098
            ],
1099
            [
1100
                $content->contentInfo->contentTypeId,
1101
                //$content->contentInfo->alwaysAvailable,
1102
                //$location->sortField,
1103
                //$location->sortOrder,
1104
            ]
1105
        );
1106
    }
1107
1108
    /**
1109
     * Test for the publishVersion() method.
1110
     *
1111
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1112
     *
1113
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1114
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
1115
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1116
     */
1117
    public function testPublishVersionCreatesLocationsDefinedOnCreate()
1118
    {
1119
        $repository = $this->getRepository();
1120
1121
        /* BEGIN: Use Case */
1122
        $content = $this->createContentVersion1();
1123
        /* END: Use Case */
1124
1125
        $locationService = $repository->getLocationService();
1126
        $location = $locationService->loadLocationByRemoteId(
1127
            '0123456789abcdef0123456789abcdef'
1128
        );
1129
1130
        $this->assertEquals(
1131
            $location->getContentInfo(),
1132
            $content->getVersionInfo()->getContentInfo()
1133
        );
1134
1135
        return [$content, $location];
1136
    }
1137
1138
    /**
1139
     * Test for the publishVersion() method.
1140
     *
1141
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1142
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionCreatesLocationsDefinedOnCreate
1143
     */
1144
    public function testCreateContentWithLocationCreateParameterCreatesExpectedLocation(array $testData)
1145
    {
1146
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
1147
        /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
1148
        list($content, $location) = $testData;
1149
1150
        $parentLocationId = $this->generateId('location', 56);
1151
        $parentLocation = $this->getRepository()->getLocationService()->loadLocation($parentLocationId);
1152
        $mainLocationId = $content->getVersionInfo()->getContentInfo()->mainLocationId;
1153
1154
        $this->assertPropertiesCorrect(
1155
            [
1156
                'id' => $mainLocationId,
1157
                'priority' => 23,
1158
                'hidden' => true,
1159
                'invisible' => true,
1160
                'remoteId' => '0123456789abcdef0123456789abcdef',
1161
                'parentLocationId' => $parentLocationId,
1162
                'pathString' => $parentLocation->pathString . $mainLocationId . '/',
1163
                'depth' => $parentLocation->depth + 1,
1164
                'sortField' => Location::SORT_FIELD_NODE_ID,
1165
                'sortOrder' => Location::SORT_ORDER_DESC,
1166
            ],
1167
            $location
1168
        );
1169
    }
1170
1171
    /**
1172
     * Test for the publishVersion() method.
1173
     *
1174
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1175
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1176
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1177
     */
1178 View Code Duplication
    public function testPublishVersionThrowsBadStateException()
1179
    {
1180
        $repository = $this->getRepository();
1181
1182
        $contentService = $repository->getContentService();
1183
1184
        /* BEGIN: Use Case */
1185
        $draft = $this->createContentDraftVersion1();
1186
1187
        // Publish the content draft
1188
        $contentService->publishVersion($draft->getVersionInfo());
1189
1190
        // This call will fail with a "BadStateException", because the version
1191
        // is already published.
1192
        $contentService->publishVersion($draft->getVersionInfo());
1193
        /* END: Use Case */
1194
    }
1195
1196
    /**
1197
     * Test that publishVersion() does not affect publishedDate (assuming previous version exists).
1198
     *
1199
     * @covers \eZ\Publish\API\Repository\ContentService::publishVersion
1200
     */
1201
    public function testPublishVersionDoesNotChangePublishedDate()
1202
    {
1203
        $repository = $this->getRepository();
1204
1205
        $contentService = $repository->getContentService();
1206
1207
        $publishedContent = $this->createContentVersion1();
1208
1209
        // force timestamps to differ
1210
        sleep(1);
1211
1212
        $contentDraft = $contentService->createContentDraft($publishedContent->contentInfo);
1213
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1214
        $contentUpdateStruct->setField('name', 'New name');
1215
        $contentDraft = $contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
1216
        $republishedContent = $contentService->publishVersion($contentDraft->versionInfo);
1217
1218
        $this->assertEquals(
1219
            $publishedContent->contentInfo->publishedDate->getTimestamp(),
1220
            $republishedContent->contentInfo->publishedDate->getTimestamp()
1221
        );
1222
        $this->assertGreaterThan(
1223
            $publishedContent->contentInfo->modificationDate->getTimestamp(),
1224
            $republishedContent->contentInfo->modificationDate->getTimestamp()
1225
        );
1226
    }
1227
1228
    /**
1229
     * Test for the createContentDraft() method.
1230
     *
1231
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1232
     *
1233
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1234
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1235
     * @group user
1236
     */
1237
    public function testCreateContentDraft()
1238
    {
1239
        $repository = $this->getRepository();
1240
1241
        $contentService = $repository->getContentService();
1242
1243
        /* BEGIN: Use Case */
1244
        $content = $this->createContentVersion1();
1245
1246
        // Now we create a new draft from the published content
1247
        $draftedContent = $contentService->createContentDraft($content->contentInfo);
1248
        /* END: Use Case */
1249
1250
        $this->assertInstanceOf(
1251
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1252
            $draftedContent
1253
        );
1254
1255
        return $draftedContent;
1256
    }
1257
1258
    /**
1259
     * Test for the createContentDraft() method.
1260
     *
1261
     * Test that editor has access to edit own draft.
1262
     * Note: Editors have access to version_read, which is needed to load content drafts.
1263
     *
1264
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1265
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1266
     * @group user
1267
     */
1268 View Code Duplication
    public function testCreateContentDraftAndLoadAccess()
1269
    {
1270
        $repository = $this->getRepository();
1271
1272
        /* BEGIN: Use Case */
1273
        $user = $this->createUserVersion1();
1274
1275
        // Set new editor as user
1276
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
1277
1278
        // Create draft
1279
        $draft = $this->createContentDraftVersion1(2, 'folder');
1280
1281
        // Try to load the draft
1282
        $contentService = $repository->getContentService();
1283
        $loadedDraft = $contentService->loadContent($draft->id);
1284
1285
        /* END: Use Case */
1286
1287
        $this->assertEquals($draft->id, $loadedDraft->id);
1288
    }
1289
1290
    /**
1291
     * Test for the createContentDraft() method.
1292
     *
1293
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1294
     *
1295
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1296
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1297
     */
1298
    public function testCreateContentDraftSetsExpectedProperties($draft)
1299
    {
1300
        $this->assertEquals(
1301
            [
1302
                'fieldCount' => 2,
1303
                'relationCount' => 0,
1304
            ],
1305
            [
1306
                'fieldCount' => count($draft->getFields()),
1307
                'relationCount' => count($this->getRepository()->getContentService()->loadRelations($draft->getVersionInfo())),
1308
            ]
1309
        );
1310
    }
1311
1312
    /**
1313
     * Test for the createContentDraft() method.
1314
     *
1315
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1316
     *
1317
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1318
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1319
     */
1320
    public function testCreateContentDraftSetsContentInfo($draft)
1321
    {
1322
        $contentInfo = $draft->contentInfo;
1323
1324
        $this->assertEquals(
1325
            [
1326
                $draft->id,
1327
                true,
1328
                1,
1329
                'eng-US',
1330
                $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...
1331
                'abcdef0123456789abcdef0123456789',
1332
                1,
1333
            ],
1334
            [
1335
                $contentInfo->id,
1336
                $contentInfo->alwaysAvailable,
1337
                $contentInfo->currentVersionNo,
1338
                $contentInfo->mainLanguageCode,
1339
                $contentInfo->ownerId,
1340
                $contentInfo->remoteId,
1341
                $contentInfo->sectionId,
1342
            ]
1343
        );
1344
    }
1345
1346
    /**
1347
     * Test for the createContentDraft() method.
1348
     *
1349
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1350
     *
1351
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1352
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1353
     */
1354
    public function testCreateContentDraftSetsVersionInfo($draft)
1355
    {
1356
        $versionInfo = $draft->getVersionInfo();
1357
1358
        $this->assertEquals(
1359
            [
1360
                '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...
1361
                'initialLanguageCode' => 'eng-US',
1362
                'languageCodes' => [0 => 'eng-US'],
1363
                'status' => VersionInfo::STATUS_DRAFT,
1364
                'versionNo' => 2,
1365
            ],
1366
            [
1367
                'creatorId' => $versionInfo->creatorId,
1368
                'initialLanguageCode' => $versionInfo->initialLanguageCode,
1369
                'languageCodes' => $versionInfo->languageCodes,
1370
                'status' => $versionInfo->status,
1371
                'versionNo' => $versionInfo->versionNo,
1372
            ]
1373
        );
1374
        $this->assertTrue($versionInfo->isDraft());
1375
        $this->assertFalse($versionInfo->isPublished());
1376
        $this->assertFalse($versionInfo->isArchived());
1377
    }
1378
1379
    /**
1380
     * Test for the createContentDraft() method.
1381
     *
1382
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1383
     *
1384
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1385
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1386
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
1387
     */
1388 View Code Duplication
    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...
1389
    {
1390
        $repository = $this->getRepository();
1391
1392
        $contentService = $repository->getContentService();
1393
1394
        /* BEGIN: Use Case */
1395
        $content = $this->createContentVersion1();
1396
1397
        // Now we create a new draft from the published content
1398
        $contentService->createContentDraft($content->contentInfo);
1399
1400
        // This call will still load the published version
1401
        $versionInfoPublished = $contentService->loadVersionInfo($content->contentInfo);
1402
        /* END: Use Case */
1403
1404
        $this->assertEquals(1, $versionInfoPublished->versionNo);
1405
    }
1406
1407
    /**
1408
     * Test for the createContentDraft() method.
1409
     *
1410
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1411
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
1412
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1413
     */
1414 View Code Duplication
    public function testCreateContentDraftLoadContentStillLoadsPublishedVersion()
1415
    {
1416
        $repository = $this->getRepository();
1417
1418
        $contentService = $repository->getContentService();
1419
1420
        /* BEGIN: Use Case */
1421
        $content = $this->createContentVersion1();
1422
1423
        // Now we create a new draft from the published content
1424
        $contentService->createContentDraft($content->contentInfo);
1425
1426
        // This call will still load the published content version
1427
        $contentPublished = $contentService->loadContent($content->id);
1428
        /* END: Use Case */
1429
1430
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1431
    }
1432
1433
    /**
1434
     * Test for the createContentDraft() method.
1435
     *
1436
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1437
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
1438
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1439
     */
1440 View Code Duplication
    public function testCreateContentDraftLoadContentByRemoteIdStillLoadsPublishedVersion()
1441
    {
1442
        $repository = $this->getRepository();
1443
1444
        $contentService = $repository->getContentService();
1445
1446
        /* BEGIN: Use Case */
1447
        $content = $this->createContentVersion1();
1448
1449
        // Now we create a new draft from the published content
1450
        $contentService->createContentDraft($content->contentInfo);
1451
1452
        // This call will still load the published content version
1453
        $contentPublished = $contentService->loadContentByRemoteId('abcdef0123456789abcdef0123456789');
1454
        /* END: Use Case */
1455
1456
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1457
    }
1458
1459
    /**
1460
     * Test for the createContentDraft() method.
1461
     *
1462
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1463
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
1464
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1465
     */
1466 View Code Duplication
    public function testCreateContentDraftLoadContentByContentInfoStillLoadsPublishedVersion()
1467
    {
1468
        $repository = $this->getRepository();
1469
1470
        $contentService = $repository->getContentService();
1471
1472
        /* BEGIN: Use Case */
1473
        $content = $this->createContentVersion1();
1474
1475
        // Now we create a new draft from the published content
1476
        $contentService->createContentDraft($content->contentInfo);
1477
1478
        // This call will still load the published content version
1479
        $contentPublished = $contentService->loadContentByContentInfo($content->contentInfo);
1480
        /* END: Use Case */
1481
1482
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1483
    }
1484
1485
    /**
1486
     * Test for the newContentUpdateStruct() method.
1487
     *
1488
     * @covers \eZ\Publish\API\Repository\ContentService::newContentUpdateStruct
1489
     * @group user
1490
     */
1491
    public function testNewContentUpdateStruct()
1492
    {
1493
        $repository = $this->getRepository();
1494
1495
        /* BEGIN: Use Case */
1496
        $contentService = $repository->getContentService();
1497
1498
        $updateStruct = $contentService->newContentUpdateStruct();
1499
        /* END: Use Case */
1500
1501
        $this->assertInstanceOf(
1502
            ContentUpdateStruct::class,
1503
            $updateStruct
1504
        );
1505
1506
        $this->assertPropertiesCorrect(
1507
            [
1508
                'initialLanguageCode' => null,
1509
                'fields' => [],
1510
            ],
1511
            $updateStruct
1512
        );
1513
    }
1514
1515
    /**
1516
     * Test for the updateContent() method.
1517
     *
1518
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1519
     *
1520
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1521
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1522
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1523
     * @group user
1524
     * @group field-type
1525
     */
1526
    public function testUpdateContent()
1527
    {
1528
        /* BEGIN: Use Case */
1529
        $draftVersion2 = $this->createUpdatedDraftVersion2();
1530
        /* END: Use Case */
1531
1532
        $this->assertInstanceOf(
1533
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1534
            $draftVersion2
1535
        );
1536
1537
        $this->assertEquals(
1538
            $this->generateId('user', 10),
1539
            $draftVersion2->versionInfo->creatorId,
1540
            'creatorId is not properly set on new Version'
1541
        );
1542
1543
        return $draftVersion2;
1544
    }
1545
1546
    /**
1547
     * Test for the updateContent_WithDifferentUser() method.
1548
     *
1549
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1550
     *
1551
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1552
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1553
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1554
     * @group user
1555
     * @group field-type
1556
     */
1557
    public function testUpdateContentWithDifferentUser()
1558
    {
1559
        /* BEGIN: Use Case */
1560
        $arrayWithDraftVersion2 = $this->createUpdatedDraftVersion2NotAdmin();
1561
        /* END: Use Case */
1562
1563
        $this->assertInstanceOf(
1564
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1565
            $arrayWithDraftVersion2[0]
1566
        );
1567
1568
        $this->assertEquals(
1569
            $this->generateId('user', $arrayWithDraftVersion2[1]),
1570
            $arrayWithDraftVersion2[0]->versionInfo->creatorId,
1571
            'creatorId is not properly set on new Version'
1572
        );
1573
1574
        return $arrayWithDraftVersion2[0];
1575
    }
1576
1577
    /**
1578
     * Test for the updateContent() method.
1579
     *
1580
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1581
     *
1582
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1583
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1584
     */
1585
    public function testUpdateContentSetsExpectedFields($content)
1586
    {
1587
        $actual = $this->normalizeFields($content->getFields());
1588
1589
        $expected = [
1590
            new Field(
1591
                [
1592
                    'id' => 0,
1593
                    'value' => true,
1594
                    'languageCode' => 'eng-GB',
1595
                    'fieldDefIdentifier' => 'description',
1596
                    'fieldTypeIdentifier' => 'ezrichtext',
1597
                ]
1598
            ),
1599
            new Field(
1600
                [
1601
                    'id' => 0,
1602
                    'value' => true,
1603
                    'languageCode' => 'eng-US',
1604
                    'fieldDefIdentifier' => 'description',
1605
                    'fieldTypeIdentifier' => 'ezrichtext',
1606
                ]
1607
            ),
1608
            new Field(
1609
                [
1610
                    'id' => 0,
1611
                    'value' => true,
1612
                    'languageCode' => 'eng-GB',
1613
                    'fieldDefIdentifier' => 'name',
1614
                    'fieldTypeIdentifier' => 'ezstring',
1615
                ]
1616
            ),
1617
            new Field(
1618
                [
1619
                    'id' => 0,
1620
                    'value' => true,
1621
                    'languageCode' => 'eng-US',
1622
                    'fieldDefIdentifier' => 'name',
1623
                    'fieldTypeIdentifier' => 'ezstring',
1624
                ]
1625
            ),
1626
        ];
1627
1628
        $this->assertEquals($expected, $actual);
1629
    }
1630
1631
    /**
1632
     * Test for the updateContent() method.
1633
     *
1634
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1635
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1636
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1637
     */
1638 View Code Duplication
    public function testUpdateContentThrowsBadStateException()
1639
    {
1640
        $repository = $this->getRepository();
1641
1642
        $contentService = $repository->getContentService();
1643
1644
        /* BEGIN: Use Case */
1645
        $content = $this->createContentVersion1();
1646
1647
        // Now create an update struct and modify some fields
1648
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1649
        $contentUpdateStruct->setField('title', 'An awesome² story about ezp.');
1650
        $contentUpdateStruct->setField('title', 'An awesome²³ story about ezp.', 'eng-GB');
1651
1652
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1653
1654
        // This call will fail with a "BadStateException", because $publishedContent
1655
        // is not a draft.
1656
        $contentService->updateContent(
1657
            $content->getVersionInfo(),
1658
            $contentUpdateStruct
1659
        );
1660
        /* END: Use Case */
1661
    }
1662
1663
    /**
1664
     * Test for the updateContent() method.
1665
     *
1666
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1667
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1668
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1669
     */
1670 View Code Duplication
    public function testUpdateContentThrowsInvalidArgumentExceptionWhenFieldTypeDoesNotAccept()
1671
    {
1672
        $repository = $this->getRepository();
1673
1674
        $contentService = $repository->getContentService();
1675
1676
        /* BEGIN: Use Case */
1677
        $draft = $this->createContentDraftVersion1();
1678
1679
        // Now create an update struct and modify some fields
1680
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1681
        // The name field does not accept a stdClass object as its input
1682
        $contentUpdateStruct->setField('name', new \stdClass(), 'eng-US');
1683
1684
        // Throws an InvalidArgumentException, since the value for field "name"
1685
        // is not accepted
1686
        $contentService->updateContent(
1687
            $draft->getVersionInfo(),
1688
            $contentUpdateStruct
1689
        );
1690
        /* END: Use Case */
1691
    }
1692
1693
    /**
1694
     * Test for the updateContent() method.
1695
     *
1696
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1697
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1698
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1699
     */
1700 View Code Duplication
    public function testUpdateContentWhenMandatoryFieldIsEmpty()
1701
    {
1702
        $repository = $this->getRepository();
1703
1704
        $contentService = $repository->getContentService();
1705
1706
        /* BEGIN: Use Case */
1707
        $draft = $this->createContentDraftVersion1();
1708
1709
        // Now create an update struct and set a mandatory field to null
1710
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1711
        $contentUpdateStruct->setField('name', null);
1712
1713
        // Don't set this, then the above call without languageCode will fail
1714
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1715
1716
        // This call will fail with a "ContentFieldValidationException", because the
1717
        // mandatory "name" field is empty.
1718
        $contentService->updateContent(
1719
            $draft->getVersionInfo(),
1720
            $contentUpdateStruct
1721
        );
1722
        /* END: Use Case */
1723
    }
1724
1725
    /**
1726
     * Test for the updateContent() method.
1727
     *
1728
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1729
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1730
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1731
     */
1732 View Code Duplication
    public function testUpdateContentThrowsContentFieldValidationException()
1733
    {
1734
        $repository = $this->getRepository();
1735
1736
        /* BEGIN: Use Case */
1737
        $contentTypeService = $repository->getContentTypeService();
1738
        $contentService = $repository->getContentService();
1739
1740
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1741
1742
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1743
        $contentCreate->setField('name', 'An awesome Sidelfingen folder');
1744
1745
        $draft = $contentService->createContent($contentCreate);
1746
1747
        $contentUpdate = $contentService->newContentUpdateStruct();
1748
        // Violates string length constraint
1749
        $contentUpdate->setField('short_name', str_repeat('a', 200), 'eng-US');
1750
1751
        // Throws ContentFieldValidationException because the string length
1752
        // validation of the field "short_name" fails
1753
        $contentService->updateContent($draft->getVersionInfo(), $contentUpdate);
1754
        /* END: Use Case */
1755
    }
1756
1757
    /**
1758
     * Test for the updateContent() method.
1759
     *
1760
     * @covers \eZ\Publish\API\Repository\ContentService::updateContent()
1761
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1762
     */
1763
    public function testUpdateContentValidatorIgnoresRequiredFieldsOfNotUpdatedLanguages()
1764
    {
1765
        $repository = $this->getRepository();
1766
        /* BEGIN: Use Case */
1767
        $contentTypeService = $repository->getContentTypeService();
1768
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1769
1770
        // Create multilangual content
1771
        $contentService = $repository->getContentService();
1772
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1773
        $contentCreate->setField('name', 'An awesome Sidelfingen folder', 'eng-US');
1774
        $contentCreate->setField('name', 'An awesome Sidelfingen folder', 'eng-GB');
1775
1776
        $contentDraft = $contentService->createContent($contentCreate);
1777
1778
        // 2. Update content type definition
1779
        $contentTypeDraft = $contentTypeService->createContentTypeDraft($contentType);
1780
1781
        $fieldDefinition = $contentType->getFieldDefinition('description');
1782
        $fieldDefinitionUpdate = $contentTypeService->newFieldDefinitionUpdateStruct();
1783
        $fieldDefinitionUpdate->identifier = 'description';
1784
        $fieldDefinitionUpdate->isRequired = true;
1785
1786
        $contentTypeService->updateFieldDefinition(
1787
            $contentTypeDraft,
1788
            $fieldDefinition,
1789
            $fieldDefinitionUpdate
1790
        );
1791
        $contentTypeService->publishContentTypeDraft($contentTypeDraft);
1792
1793
        // 3. Update only eng-US translation
1794
        $description = new DOMDocument();
1795
        $description->loadXML(<<<XML
1796
<?xml version="1.0" encoding="UTF-8"?>
1797
<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">
1798
    <para>Lorem ipsum dolor</para>
1799
</section>
1800
XML
1801
        );
1802
1803
        $contentUpdate = $contentService->newContentUpdateStruct();
1804
        $contentUpdate->setField('name', 'An awesome Sidelfingen folder (updated)', 'eng-US');
1805
        $contentUpdate->setField('description', $description);
1806
1807
        $contentService->updateContent($contentDraft->getVersionInfo(), $contentUpdate);
1808
        /* END: Use Case */
1809
    }
1810
1811
    /**
1812
     * Test for the updateContent() method.
1813
     *
1814
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1815
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1816
     */
1817
    public function testUpdateContentWithNotUpdatingMandatoryField()
1818
    {
1819
        $repository = $this->getRepository();
1820
1821
        $contentService = $repository->getContentService();
1822
1823
        /* BEGIN: Use Case */
1824
        $draft = $this->createContentDraftVersion1();
1825
1826
        // Now create an update struct which does not overwrite mandatory
1827
        // fields
1828
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1829
        $contentUpdateStruct->setField(
1830
            'description',
1831
            '<?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"/>'
1832
        );
1833
1834
        // Don't set this, then the above call without languageCode will fail
1835
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1836
1837
        // This will only update the "description" field in the "eng-US"
1838
        // language
1839
        $updatedDraft = $contentService->updateContent(
1840
            $draft->getVersionInfo(),
1841
            $contentUpdateStruct
1842
        );
1843
        /* END: Use Case */
1844
1845
        foreach ($updatedDraft->getFields() as $field) {
1846
            if ($field->languageCode === 'eng-US' && $field->fieldDefIdentifier === 'name' && $field->value !== null) {
1847
                // Found field
1848
                return;
1849
            }
1850
        }
1851
        $this->fail(
1852
            'Field with identifier "name" in language "eng-US" could not be found or has empty value.'
1853
        );
1854
    }
1855
1856
    /**
1857
     * Test for the createContentDraft() method.
1858
     *
1859
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
1860
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1861
     */
1862 View Code Duplication
    public function testCreateContentDraftWithSecondParameter()
1863
    {
1864
        $repository = $this->getRepository();
1865
1866
        $contentService = $repository->getContentService();
1867
1868
        /* BEGIN: Use Case */
1869
        $contentVersion2 = $this->createContentVersion2();
1870
1871
        // Now we create a new draft from the initial version
1872
        $draftedContentReloaded = $contentService->createContentDraft(
1873
            $contentVersion2->contentInfo,
1874
            $contentVersion2->getVersionInfo()
1875
        );
1876
        /* END: Use Case */
1877
1878
        $this->assertEquals(3, $draftedContentReloaded->getVersionInfo()->versionNo);
1879
    }
1880
1881
    /**
1882
     * Test for the createContentDraft() method with third parameter.
1883
     *
1884
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1885
     */
1886 View Code Duplication
    public function testCreateContentDraftWithThirdParameter()
1887
    {
1888
        $repository = $this->getRepository();
1889
1890
        $contentService = $repository->getContentService();
1891
1892
        $content = $contentService->loadContent(4);
1893
        $user = $this->createUserVersion1();
1894
1895
        $draftContent = $contentService->createContentDraft(
1896
            $content->contentInfo,
1897
            $content->getVersionInfo(),
1898
            $user
1899
        );
1900
1901
        $this->assertInstanceOf(
1902
            Content::class,
1903
            $draftContent
1904
        );
1905
    }
1906
1907
    /**
1908
     * Test for the publishVersion() method.
1909
     *
1910
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1911
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1912
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1913
     */
1914 View Code Duplication
    public function testPublishVersionFromContentDraft()
1915
    {
1916
        $repository = $this->getRepository();
1917
1918
        $contentService = $repository->getContentService();
1919
1920
        /* BEGIN: Use Case */
1921
        $contentVersion2 = $this->createContentVersion2();
1922
        /* END: Use Case */
1923
1924
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo);
1925
1926
        $this->assertEquals(
1927
            [
1928
                'status' => VersionInfo::STATUS_PUBLISHED,
1929
                'versionNo' => 2,
1930
            ],
1931
            [
1932
                'status' => $versionInfo->status,
1933
                'versionNo' => $versionInfo->versionNo,
1934
            ]
1935
        );
1936
        $this->assertTrue($versionInfo->isPublished());
1937
        $this->assertFalse($versionInfo->isDraft());
1938
        $this->assertFalse($versionInfo->isArchived());
1939
    }
1940
1941
    /**
1942
     * Test for the publishVersion() method.
1943
     *
1944
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1945
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1946
     */
1947 View Code Duplication
    public function testPublishVersionFromContentDraftArchivesOldVersion()
1948
    {
1949
        $repository = $this->getRepository();
1950
1951
        $contentService = $repository->getContentService();
1952
1953
        /* BEGIN: Use Case */
1954
        $contentVersion2 = $this->createContentVersion2();
1955
        /* END: Use Case */
1956
1957
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo, 1);
1958
1959
        $this->assertEquals(
1960
            [
1961
                'status' => VersionInfo::STATUS_ARCHIVED,
1962
                'versionNo' => 1,
1963
            ],
1964
            [
1965
                'status' => $versionInfo->status,
1966
                'versionNo' => $versionInfo->versionNo,
1967
            ]
1968
        );
1969
        $this->assertTrue($versionInfo->isArchived());
1970
        $this->assertFalse($versionInfo->isDraft());
1971
        $this->assertFalse($versionInfo->isPublished());
1972
    }
1973
1974
    /**
1975
     * Test for the publishVersion() method.
1976
     *
1977
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1978
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1979
     */
1980
    public function testPublishVersionFromContentDraftUpdatesContentInfoCurrentVersion()
1981
    {
1982
        /* BEGIN: Use Case */
1983
        $contentVersion2 = $this->createContentVersion2();
1984
        /* END: Use Case */
1985
1986
        $this->assertEquals(2, $contentVersion2->contentInfo->currentVersionNo);
1987
    }
1988
1989
    /**
1990
     * Test for the publishVersion() method.
1991
     *
1992
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1993
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1994
     */
1995 View Code Duplication
    public function testPublishVersionFromOldContentDraftArchivesNewerVersionNo()
1996
    {
1997
        $repository = $this->getRepository();
1998
1999
        $contentService = $repository->getContentService();
2000
2001
        /* BEGIN: Use Case */
2002
        $content = $this->createContentVersion1();
2003
2004
        // Create a new draft with versionNo = 2
2005
        $draftedContentVersion2 = $contentService->createContentDraft($content->contentInfo);
2006
2007
        // Create another new draft with versionNo = 3
2008
        $draftedContentVersion3 = $contentService->createContentDraft($content->contentInfo);
2009
2010
        // Publish draft with versionNo = 3
2011
        $contentService->publishVersion($draftedContentVersion3->getVersionInfo());
2012
2013
        // Publish the first draft with versionNo = 2
2014
        // currentVersionNo is now 2, versionNo 3 will be archived
2015
        $publishedDraft = $contentService->publishVersion($draftedContentVersion2->getVersionInfo());
2016
        /* END: Use Case */
2017
2018
        $this->assertEquals(2, $publishedDraft->contentInfo->currentVersionNo);
2019
    }
2020
2021
    /**
2022
     * Test for the publishVersion() method, and that it creates limited archives.
2023
     *
2024
     * @todo Adapt this when per content type archive limited is added on repository Content Type model.
2025
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
2026
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2027
     */
2028
    public function testPublishVersionNotCreatingUnlimitedArchives()
2029
    {
2030
        $repository = $this->getRepository();
2031
2032
        $contentService = $repository->getContentService();
2033
2034
        $content = $this->createContentVersion1();
2035
2036
        // load first to make sure list gets updated also (cache)
2037
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
2038
        $this->assertEquals(1, count($versionInfoList));
2039
        $this->assertEquals(1, $versionInfoList[0]->versionNo);
2040
2041
        // Create a new draft with versionNo = 2
2042
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
2043
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
2044
2045
        // Create a new draft with versionNo = 3
2046
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
2047
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
2048
2049
        // Create a new draft with versionNo = 4
2050
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
2051
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
2052
2053
        // Create a new draft with versionNo = 5
2054
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
2055
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
2056
2057
        // Create a new draft with versionNo = 6
2058
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
2059
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
2060
2061
        // Create a new draft with versionNo = 7
2062
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
2063
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
2064
2065
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
2066
2067
        $this->assertEquals(6, count($versionInfoList));
2068
        $this->assertEquals(2, $versionInfoList[0]->versionNo);
2069
        $this->assertEquals(7, $versionInfoList[5]->versionNo);
2070
2071
        $this->assertEquals(
2072
            [
2073
                VersionInfo::STATUS_ARCHIVED,
2074
                VersionInfo::STATUS_ARCHIVED,
2075
                VersionInfo::STATUS_ARCHIVED,
2076
                VersionInfo::STATUS_ARCHIVED,
2077
                VersionInfo::STATUS_ARCHIVED,
2078
                VersionInfo::STATUS_PUBLISHED,
2079
            ],
2080
            [
2081
                $versionInfoList[0]->status,
2082
                $versionInfoList[1]->status,
2083
                $versionInfoList[2]->status,
2084
                $versionInfoList[3]->status,
2085
                $versionInfoList[4]->status,
2086
                $versionInfoList[5]->status,
2087
            ]
2088
        );
2089
    }
2090
2091
    /**
2092
     * Test for the newContentMetadataUpdateStruct() method.
2093
     *
2094
     * @covers \eZ\Publish\API\Repository\ContentService::newContentMetadataUpdateStruct
2095
     * @group user
2096
     */
2097
    public function testNewContentMetadataUpdateStruct()
2098
    {
2099
        $repository = $this->getRepository();
2100
2101
        /* BEGIN: Use Case */
2102
        $contentService = $repository->getContentService();
2103
2104
        // Creates a new metadata update struct
2105
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2106
2107
        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...
2108
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
2109
        }
2110
2111
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
2112
        $metadataUpdate->mainLanguageCode = 'eng-GB';
2113
        $metadataUpdate->alwaysAvailable = false;
2114
        /* END: Use Case */
2115
2116
        $this->assertInstanceOf(
2117
            ContentMetadataUpdateStruct::class,
2118
            $metadataUpdate
2119
        );
2120
    }
2121
2122
    /**
2123
     * Test for the updateContentMetadata() method.
2124
     *
2125
     * @return \eZ\Publish\API\Repository\Values\Content\Content
2126
     *
2127
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2128
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2129
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentMetadataUpdateStruct
2130
     * @group user
2131
     */
2132
    public function testUpdateContentMetadata()
2133
    {
2134
        $repository = $this->getRepository();
2135
2136
        $contentService = $repository->getContentService();
2137
2138
        /* BEGIN: Use Case */
2139
        $content = $this->createContentVersion1();
2140
2141
        // Creates a metadata update struct
2142
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2143
2144
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
2145
        $metadataUpdate->mainLanguageCode = 'eng-GB';
2146
        $metadataUpdate->alwaysAvailable = false;
2147
        $metadataUpdate->publishedDate = $this->createDateTime(441759600); // 1984/01/01
2148
        $metadataUpdate->modificationDate = $this->createDateTime(441759600); // 1984/01/01
2149
2150
        // Update the metadata of the published content object
2151
        $content = $contentService->updateContentMetadata(
2152
            $content->contentInfo,
2153
            $metadataUpdate
2154
        );
2155
        /* END: Use Case */
2156
2157
        $this->assertInstanceOf(
2158
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
2159
            $content
2160
        );
2161
2162
        return $content;
2163
    }
2164
2165
    /**
2166
     * Test for the updateContentMetadata() method.
2167
     *
2168
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2169
     *
2170
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2171
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2172
     */
2173
    public function testUpdateContentMetadataSetsExpectedProperties($content)
2174
    {
2175
        $contentInfo = $content->contentInfo;
2176
2177
        $this->assertEquals(
2178
            [
2179
                'remoteId' => 'aaaabbbbccccddddeeeeffff11112222',
2180
                'sectionId' => $this->generateId('section', 1),
2181
                'alwaysAvailable' => false,
2182
                'currentVersionNo' => 1,
2183
                'mainLanguageCode' => 'eng-GB',
2184
                'modificationDate' => $this->createDateTime(441759600),
2185
                '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...
2186
                'published' => true,
2187
                'publishedDate' => $this->createDateTime(441759600),
2188
            ],
2189
            [
2190
                'remoteId' => $contentInfo->remoteId,
2191
                'sectionId' => $contentInfo->sectionId,
2192
                'alwaysAvailable' => $contentInfo->alwaysAvailable,
2193
                'currentVersionNo' => $contentInfo->currentVersionNo,
2194
                'mainLanguageCode' => $contentInfo->mainLanguageCode,
2195
                'modificationDate' => $contentInfo->modificationDate,
2196
                'ownerId' => $contentInfo->ownerId,
2197
                'published' => $contentInfo->published,
2198
                'publishedDate' => $contentInfo->publishedDate,
2199
            ]
2200
        );
2201
    }
2202
2203
    /**
2204
     * Test for the updateContentMetadata() method.
2205
     *
2206
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2207
     *
2208
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2209
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2210
     */
2211
    public function testUpdateContentMetadataNotUpdatesContentVersion($content)
2212
    {
2213
        $this->assertEquals(1, $content->getVersionInfo()->versionNo);
2214
    }
2215
2216
    /**
2217
     * Test for the updateContentMetadata() method.
2218
     *
2219
     * @covers \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2220
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2221
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2222
     */
2223
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnDuplicateRemoteId()
2224
    {
2225
        $repository = $this->getRepository();
2226
2227
        $contentService = $repository->getContentService();
2228
2229
        /* BEGIN: Use Case */
2230
        // RemoteId of the "Media" page of an eZ Publish demo installation
2231
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2232
2233
        $content = $this->createContentVersion1();
2234
2235
        // Creates a metadata update struct
2236
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2237
        $metadataUpdate->remoteId = $mediaRemoteId;
2238
2239
        // This call will fail with an "InvalidArgumentException", because the
2240
        // specified remoteId is already used by the "Media" page.
2241
        $contentService->updateContentMetadata(
2242
            $content->contentInfo,
2243
            $metadataUpdate
2244
        );
2245
        /* END: Use Case */
2246
    }
2247
2248
    /**
2249
     * Test for the updateContentMetadata() method.
2250
     *
2251
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContentMetadata
2252
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2253
     */
2254
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnNoMetadataPropertiesSet()
2255
    {
2256
        $repository = $this->getRepository();
2257
2258
        $contentService = $repository->getContentService();
2259
2260
        $contentInfo = $contentService->loadContentInfo(4);
2261
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
2262
2263
        // Throws an exception because no properties are set in $contentMetadataUpdateStruct
2264
        $contentService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
2265
    }
2266
2267
    /**
2268
     * Test for the deleteContent() method.
2269
     *
2270
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2271
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2272
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2273
     */
2274 View Code Duplication
    public function testDeleteContent()
2275
    {
2276
        $repository = $this->getRepository();
2277
2278
        $contentService = $repository->getContentService();
2279
        $locationService = $repository->getLocationService();
2280
2281
        /* BEGIN: Use Case */
2282
        $contentVersion2 = $this->createContentVersion2();
2283
2284
        // Load the locations for this content object
2285
        $locations = $locationService->loadLocations($contentVersion2->contentInfo);
2286
2287
        // This will delete the content, all versions and the associated locations
2288
        $contentService->deleteContent($contentVersion2->contentInfo);
2289
        /* END: Use Case */
2290
2291
        foreach ($locations as $location) {
2292
            $locationService->loadLocation($location->id);
2293
        }
2294
    }
2295
2296
    /**
2297
     * Test for the deleteContent() method.
2298
     *
2299
     * Test for issue EZP-21057:
2300
     * "contentService: Unable to delete a content with an empty file attribute"
2301
     *
2302
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2303
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2304
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2305
     */
2306 View Code Duplication
    public function testDeleteContentWithEmptyBinaryField()
2307
    {
2308
        $repository = $this->getRepository();
2309
2310
        $contentService = $repository->getContentService();
2311
        $locationService = $repository->getLocationService();
2312
2313
        /* BEGIN: Use Case */
2314
        $contentVersion = $this->createContentVersion1EmptyBinaryField();
2315
2316
        // Load the locations for this content object
2317
        $locations = $locationService->loadLocations($contentVersion->contentInfo);
2318
2319
        // This will delete the content, all versions and the associated locations
2320
        $contentService->deleteContent($contentVersion->contentInfo);
2321
        /* END: Use Case */
2322
2323
        foreach ($locations as $location) {
2324
            $locationService->loadLocation($location->id);
2325
        }
2326
    }
2327
2328
    /**
2329
     * Test for the loadContentDrafts() method.
2330
     *
2331
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2332
     */
2333
    public function testLoadContentDraftsReturnsEmptyArrayByDefault()
2334
    {
2335
        $repository = $this->getRepository();
2336
2337
        /* BEGIN: Use Case */
2338
        $contentService = $repository->getContentService();
2339
2340
        $contentDrafts = $contentService->loadContentDrafts();
2341
        /* END: Use Case */
2342
2343
        $this->assertSame([], $contentDrafts);
2344
    }
2345
2346
    /**
2347
     * Test for the loadContentDrafts() method.
2348
     *
2349
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2350
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2351
     */
2352
    public function testLoadContentDrafts()
2353
    {
2354
        $repository = $this->getRepository();
2355
2356
        /* BEGIN: Use Case */
2357
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
2358
        // of a eZ Publish demo installation.
2359
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2360
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
2361
2362
        $contentService = $repository->getContentService();
2363
2364
        // "Media" content object
2365
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2366
2367
        // "eZ Publish Demo Design ..." content object
2368
        $demoDesignContentInfo = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
2369
2370
        // Create some drafts
2371
        $contentService->createContentDraft($mediaContentInfo);
2372
        $contentService->createContentDraft($demoDesignContentInfo);
2373
2374
        // Now $contentDrafts should contain two drafted versions
2375
        $draftedVersions = $contentService->loadContentDrafts();
2376
        /* END: Use Case */
2377
2378
        $actual = [
2379
            $draftedVersions[0]->status,
2380
            $draftedVersions[0]->getContentInfo()->remoteId,
2381
            $draftedVersions[1]->status,
2382
            $draftedVersions[1]->getContentInfo()->remoteId,
2383
        ];
2384
        sort($actual, SORT_STRING);
2385
2386
        $this->assertEquals(
2387
            [
2388
                VersionInfo::STATUS_DRAFT,
2389
                VersionInfo::STATUS_DRAFT,
2390
                $demoDesignRemoteId,
2391
                $mediaRemoteId,
2392
            ],
2393
            $actual
2394
        );
2395
    }
2396
2397
    /**
2398
     * Test for the loadContentDrafts() method.
2399
     *
2400
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
2401
     */
2402
    public function testLoadContentDraftsWithFirstParameter()
2403
    {
2404
        $repository = $this->getRepository();
2405
2406
        /* BEGIN: Use Case */
2407
        $user = $this->createUserVersion1();
2408
2409
        // Get current user
2410
        $oldCurrentUser = $repository->getCurrentUser();
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...
2411
2412
        // Set new editor as user
2413
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
2414
2415
        // Remote id of the "Media" content object in an eZ Publish demo installation.
2416
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2417
2418
        $contentService = $repository->getContentService();
2419
2420
        // "Media" content object
2421
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2422
2423
        // Create a content draft
2424
        $contentService->createContentDraft($mediaContentInfo);
2425
2426
        // Reset to previous current user
2427
        $repository->setCurrentUser($oldCurrentUser);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
2428
2429
        // Now $contentDrafts for the previous current user and the new user
2430
        $newCurrentUserDrafts = $contentService->loadContentDrafts($user);
2431
        $oldCurrentUserDrafts = $contentService->loadContentDrafts($oldCurrentUser);
2432
        /* END: Use Case */
2433
2434
        $this->assertSame([], $oldCurrentUserDrafts);
2435
2436
        $this->assertEquals(
2437
            [
2438
                VersionInfo::STATUS_DRAFT,
2439
                $mediaRemoteId,
2440
            ],
2441
            [
2442
                $newCurrentUserDrafts[0]->status,
2443
                $newCurrentUserDrafts[0]->getContentInfo()->remoteId,
2444
            ]
2445
        );
2446
        $this->assertTrue($newCurrentUserDrafts[0]->isDraft());
2447
        $this->assertFalse($newCurrentUserDrafts[0]->isArchived());
2448
        $this->assertFalse($newCurrentUserDrafts[0]->isPublished());
2449
    }
2450
2451
    /**
2452
     * Test for the loadVersionInfo() method.
2453
     *
2454
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2455
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2456
     */
2457
    public function testLoadVersionInfoWithSecondParameter()
2458
    {
2459
        $repository = $this->getRepository();
2460
2461
        $contentService = $repository->getContentService();
2462
2463
        /* BEGIN: Use Case */
2464
        $publishedContent = $this->createContentVersion1();
2465
2466
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
0 ignored issues
show
Unused Code introduced by
$draftContent is not used, you could remove the assignment.

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

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

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

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

Loading history...
2467
2468
        // Will return the VersionInfo of the $draftContent
2469
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2470
        /* END: Use Case */
2471
2472
        $this->assertEquals(2, $versionInfo->versionNo);
2473
2474
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2475
        $this->assertEquals(
2476
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2477
            $versionInfo->getContentInfo()->mainLocationId
2478
        );
2479
    }
2480
2481
    /**
2482
     * Test for the loadVersionInfo() method.
2483
     *
2484
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2485
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2486
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2487
     */
2488
    public function testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter()
2489
    {
2490
        $repository = $this->getRepository();
2491
2492
        $contentService = $repository->getContentService();
2493
2494
        /* BEGIN: Use Case */
2495
        $draft = $this->createContentDraftVersion1();
2496
2497
        // This call will fail with a "NotFoundException", because not versionNo
2498
        // 2 exists for this content object.
2499
        $contentService->loadVersionInfo($draft->contentInfo, 2);
2500
        /* END: Use Case */
2501
    }
2502
2503
    /**
2504
     * Test for the loadVersionInfoById() method.
2505
     *
2506
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2507
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2508
     */
2509
    public function testLoadVersionInfoByIdWithSecondParameter()
2510
    {
2511
        $repository = $this->getRepository();
2512
2513
        $contentService = $repository->getContentService();
2514
2515
        /* BEGIN: Use Case */
2516
        $publishedContent = $this->createContentVersion1();
2517
2518
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
2519
2520
        // Will return the VersionInfo of the $draftContent
2521
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2522
        /* END: Use Case */
2523
2524
        $this->assertEquals(2, $versionInfo->versionNo);
2525
2526
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2527
        $this->assertEquals(
2528
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2529
            $versionInfo->getContentInfo()->mainLocationId
2530
        );
2531
2532
        return [
2533
            'versionInfo' => $versionInfo,
2534
            'draftContent' => $draftContent,
2535
        ];
2536
    }
2537
2538
    /**
2539
     * Test for the returned value of the loadVersionInfoById() method.
2540
     *
2541
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter
2542
     * @covers \eZ\Publish\API\Repository\ContentService::loadVersionInfoById
2543
     *
2544
     * @param array $data
2545
     */
2546
    public function testLoadVersionInfoByIdWithSecondParameterSetsExpectedVersionInfo(array $data)
2547
    {
2548
        /** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo */
2549
        $versionInfo = $data['versionInfo'];
2550
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $draftContent */
2551
        $draftContent = $data['draftContent'];
2552
2553
        $this->assertPropertiesCorrect(
2554
            [
2555
                'names' => [
2556
                    'eng-US' => 'An awesome forum',
2557
                ],
2558
                'contentInfo' => new ContentInfo([
2559
                    'id' => $draftContent->contentInfo->id,
2560
                    'contentTypeId' => 28,
2561
                    'name' => 'An awesome forum',
2562
                    'sectionId' => 1,
2563
                    'currentVersionNo' => 1,
2564
                    'published' => true,
2565
                    'ownerId' => 14,
2566
                    // this Content Object is created at the test runtime
2567
                    'modificationDate' => $versionInfo->contentInfo->modificationDate,
2568
                    'publishedDate' => $versionInfo->contentInfo->publishedDate,
2569
                    'alwaysAvailable' => 1,
2570
                    'remoteId' => 'abcdef0123456789abcdef0123456789',
2571
                    'mainLanguageCode' => 'eng-US',
2572
                    'mainLocationId' => $draftContent->contentInfo->mainLocationId,
2573
                    'status' => ContentInfo::STATUS_PUBLISHED,
2574
                ]),
2575
                'id' => $draftContent->versionInfo->id,
2576
                'versionNo' => 2,
2577
                'creatorId' => 14,
2578
                'status' => 0,
2579
                'initialLanguageCode' => 'eng-US',
2580
                'languageCodes' => [
2581
                    'eng-US',
2582
                ],
2583
            ],
2584
            $versionInfo
2585
        );
2586
    }
2587
2588
    /**
2589
     * Test for the loadVersionInfoById() method.
2590
     *
2591
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2592
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2593
     */
2594 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter()
2595
    {
2596
        $repository = $this->getRepository();
2597
2598
        $contentService = $repository->getContentService();
2599
2600
        /* BEGIN: Use Case */
2601
        $content = $this->createContentVersion1();
2602
2603
        // This call will fail with a "NotFoundException", because not versionNo
2604
        // 2 exists for this content object.
2605
        $contentService->loadVersionInfoById($content->id, 2);
2606
        /* END: Use Case */
2607
    }
2608
2609
    /**
2610
     * Test for the loadContentByVersionInfo() method.
2611
     *
2612
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
2613
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2614
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
2615
     */
2616
    public function testLoadContentByVersionInfoWithSecondParameter()
2617
    {
2618
        $repository = $this->getRepository();
2619
2620
        $sectionId = $this->generateId('section', 1);
2621
        /* BEGIN: Use Case */
2622
        $contentTypeService = $repository->getContentTypeService();
2623
2624
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2625
2626
        $contentService = $repository->getContentService();
2627
2628
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2629
2630
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2631
2632
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2633
2634
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2635
        // $sectionId contains the ID of section 1
2636
        $contentCreateStruct->sectionId = $sectionId;
2637
        $contentCreateStruct->alwaysAvailable = true;
2638
2639
        // Create a new content draft
2640
        $content = $contentService->createContent($contentCreateStruct);
2641
2642
        // Now publish this draft
2643
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2644
2645
        // Will return a content instance with fields in "eng-US"
2646
        $reloadedContent = $contentService->loadContentByVersionInfo(
2647
            $publishedContent->getVersionInfo(),
2648
            [
2649
                'eng-GB',
2650
            ],
2651
            false
2652
        );
2653
        /* END: Use Case */
2654
2655
        $actual = [];
2656 View Code Duplication
        foreach ($reloadedContent->getFields() as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2657
            $actual[] = new Field(
2658
                [
2659
                    'id' => 0,
2660
                    'value' => ($field->value !== null ? true : null), // Actual value tested by FieldType integration tests
2661
                    'languageCode' => $field->languageCode,
2662
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
2663
                ]
2664
            );
2665
        }
2666
        usort(
2667
            $actual,
2668 View Code Duplication
            function ($field1, $field2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2669
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
2670
                    return strcasecmp($field1->languageCode, $field2->languageCode);
2671
                }
2672
2673
                return $return;
2674
            }
2675
        );
2676
2677
        $expected = [
2678
            new Field(
2679
                [
2680
                    'id' => 0,
2681
                    'value' => true,
2682
                    'languageCode' => 'eng-GB',
2683
                    'fieldDefIdentifier' => 'description',
2684
                ]
2685
            ),
2686
            new Field(
2687
                [
2688
                    'id' => 0,
2689
                    'value' => true,
2690
                    'languageCode' => 'eng-GB',
2691
                    'fieldDefIdentifier' => 'name',
2692
                ]
2693
            ),
2694
        ];
2695
2696
        $this->assertEquals($expected, $actual);
2697
    }
2698
2699
    /**
2700
     * Test for the loadContentByContentInfo() method.
2701
     *
2702
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages)
2703
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2704
     */
2705
    public function testLoadContentByContentInfoWithLanguageParameters()
2706
    {
2707
        $repository = $this->getRepository();
2708
2709
        $sectionId = $this->generateId('section', 1);
2710
        /* BEGIN: Use Case */
2711
        $contentTypeService = $repository->getContentTypeService();
2712
2713
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2714
2715
        $contentService = $repository->getContentService();
2716
2717
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2718
2719
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2720
2721
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2722
2723
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2724
        // $sectionId contains the ID of section 1
2725
        $contentCreateStruct->sectionId = $sectionId;
2726
        $contentCreateStruct->alwaysAvailable = true;
2727
2728
        // Create a new content draft
2729
        $content = $contentService->createContent($contentCreateStruct);
2730
2731
        // Now publish this draft
2732
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2733
2734
        // Will return a content instance with fields in "eng-US"
2735
        $reloadedContent = $contentService->loadContentByContentInfo(
2736
            $publishedContent->contentInfo,
2737
            [
2738
                'eng-US',
2739
            ],
2740
            null,
2741
            false
2742
        );
2743
        /* END: Use Case */
2744
2745
        $actual = $this->normalizeFields($reloadedContent->getFields());
2746
2747
        $expected = [
2748
            new Field(
2749
                [
2750
                    'id' => 0,
2751
                    'value' => true,
2752
                    'languageCode' => 'eng-US',
2753
                    'fieldDefIdentifier' => 'description',
2754
                    'fieldTypeIdentifier' => 'ezrichtext',
2755
                ]
2756
            ),
2757
            new Field(
2758
                [
2759
                    'id' => 0,
2760
                    'value' => true,
2761
                    'languageCode' => 'eng-US',
2762
                    'fieldDefIdentifier' => 'name',
2763
                    'fieldTypeIdentifier' => 'ezstring',
2764
                ]
2765
            ),
2766
        ];
2767
2768
        $this->assertEquals($expected, $actual);
2769
2770
        // Will return a content instance with fields in "eng-GB" (versions prior to 6.0.0-beta9 returned "eng-US" also)
2771
        $reloadedContent = $contentService->loadContentByContentInfo(
2772
            $publishedContent->contentInfo,
2773
            [
2774
                'eng-GB',
2775
            ],
2776
            null,
2777
            true
2778
        );
2779
2780
        $actual = $this->normalizeFields($reloadedContent->getFields());
2781
2782
        $expected = [
2783
            new Field(
2784
                [
2785
                    'id' => 0,
2786
                    'value' => true,
2787
                    'languageCode' => 'eng-GB',
2788
                    'fieldDefIdentifier' => 'description',
2789
                    'fieldTypeIdentifier' => 'ezrichtext',
2790
                ]
2791
            ),
2792
            new Field(
2793
                [
2794
                    'id' => 0,
2795
                    'value' => true,
2796
                    'languageCode' => 'eng-GB',
2797
                    'fieldDefIdentifier' => 'name',
2798
                    'fieldTypeIdentifier' => 'ezstring',
2799
                ]
2800
            ),
2801
        ];
2802
2803
        $this->assertEquals($expected, $actual);
2804
2805
        // Will return a content instance with fields in main language "eng-US", as "fre-FR" does not exists
2806
        $reloadedContent = $contentService->loadContentByContentInfo(
2807
            $publishedContent->contentInfo,
2808
            [
2809
                'fre-FR',
2810
            ],
2811
            null,
2812
            true
2813
        );
2814
2815
        $actual = $this->normalizeFields($reloadedContent->getFields());
2816
2817
        $expected = [
2818
            new Field(
2819
                [
2820
                    'id' => 0,
2821
                    'value' => true,
2822
                    'languageCode' => 'eng-US',
2823
                    'fieldDefIdentifier' => 'description',
2824
                    'fieldTypeIdentifier' => 'ezrichtext',
2825
                ]
2826
            ),
2827
            new Field(
2828
                [
2829
                    'id' => 0,
2830
                    'value' => true,
2831
                    'languageCode' => 'eng-US',
2832
                    'fieldDefIdentifier' => 'name',
2833
                    'fieldTypeIdentifier' => 'ezstring',
2834
                ]
2835
            ),
2836
        ];
2837
2838
        $this->assertEquals($expected, $actual);
2839
    }
2840
2841
    /**
2842
     * Test for the loadContentByContentInfo() method.
2843
     *
2844
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2845
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2846
     */
2847 View Code Duplication
    public function testLoadContentByContentInfoWithVersionNumberParameter()
2848
    {
2849
        $repository = $this->getRepository();
2850
2851
        $contentService = $repository->getContentService();
2852
2853
        /* BEGIN: Use Case */
2854
        $publishedContent = $this->createContentVersion1();
2855
2856
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
0 ignored issues
show
Unused Code introduced by
$draftContent is not used, you could remove the assignment.

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

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

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

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

Loading history...
2857
2858
        // This content instance is identical to $draftContent
2859
        $draftContentReloaded = $contentService->loadContentByContentInfo(
2860
            $publishedContent->contentInfo,
2861
            null,
2862
            2
2863
        );
2864
        /* END: Use Case */
2865
2866
        $this->assertEquals(
2867
            2,
2868
            $draftContentReloaded->getVersionInfo()->versionNo
2869
        );
2870
2871
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2872
        $this->assertEquals(
2873
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2874
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2875
        );
2876
    }
2877
2878
    /**
2879
     * Test for the loadContentByContentInfo() method.
2880
     *
2881
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2882
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2883
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithVersionNumberParameter
2884
     */
2885 View Code Duplication
    public function testLoadContentByContentInfoThrowsNotFoundExceptionWithVersionNumberParameter()
2886
    {
2887
        $repository = $this->getRepository();
2888
2889
        $contentService = $repository->getContentService();
2890
2891
        /* BEGIN: Use Case */
2892
        $content = $this->createContentVersion1();
2893
2894
        // This call will fail with a "NotFoundException", because no content
2895
        // with versionNo = 2 exists.
2896
        $contentService->loadContentByContentInfo($content->contentInfo, null, 2);
2897
        /* END: Use Case */
2898
    }
2899
2900
    /**
2901
     * Test for the loadContent() method.
2902
     *
2903
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages)
2904
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2905
     */
2906 View Code Duplication
    public function testLoadContentWithSecondParameter()
2907
    {
2908
        $repository = $this->getRepository();
2909
2910
        $contentService = $repository->getContentService();
2911
2912
        /* BEGIN: Use Case */
2913
        $draft = $this->createMultipleLanguageDraftVersion1();
2914
2915
        // This draft contains those fields localized with "eng-GB"
2916
        $draftLocalized = $contentService->loadContent($draft->id, ['eng-GB'], null, false);
2917
        /* END: Use Case */
2918
2919
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2920
2921
        return $draft;
2922
    }
2923
2924
    /**
2925
     * Test for the loadContent() method using undefined translation.
2926
     *
2927
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithSecondParameter
2928
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2929
     *
2930
     * @param \eZ\Publish\API\Repository\Values\Content\Content $contentDraft
2931
     */
2932
    public function testLoadContentWithSecondParameterThrowsNotFoundException(Content $contentDraft)
2933
    {
2934
        $repository = $this->getRepository();
2935
2936
        $contentService = $repository->getContentService();
2937
2938
        $contentService->loadContent($contentDraft->id, ['ger-DE'], null, false);
2939
    }
2940
2941
    /**
2942
     * Test for the loadContent() method.
2943
     *
2944
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2945
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2946
     */
2947 View Code Duplication
    public function testLoadContentWithThirdParameter()
2948
    {
2949
        $repository = $this->getRepository();
2950
2951
        $contentService = $repository->getContentService();
2952
2953
        /* BEGIN: Use Case */
2954
        $publishedContent = $this->createContentVersion1();
2955
2956
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
0 ignored issues
show
Unused Code introduced by
$draftContent is not used, you could remove the assignment.

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

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

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

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

Loading history...
2957
2958
        // This content instance is identical to $draftContent
2959
        $draftContentReloaded = $contentService->loadContent($publishedContent->id, null, 2);
2960
        /* END: Use Case */
2961
2962
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2963
2964
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2965
        $this->assertEquals(
2966
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2967
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2968
        );
2969
    }
2970
2971
    /**
2972
     * Test for the loadContent() method.
2973
     *
2974
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2975
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2976
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithThirdParameter
2977
     */
2978 View Code Duplication
    public function testLoadContentThrowsNotFoundExceptionWithThirdParameter()
2979
    {
2980
        $repository = $this->getRepository();
2981
2982
        $contentService = $repository->getContentService();
2983
2984
        /* BEGIN: Use Case */
2985
        $content = $this->createContentVersion1();
2986
2987
        // This call will fail with a "NotFoundException", because for this
2988
        // content object no versionNo=2 exists.
2989
        $contentService->loadContent($content->id, null, 2);
2990
        /* END: Use Case */
2991
    }
2992
2993
    /**
2994
     * Test for the loadContentByRemoteId() method.
2995
     *
2996
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages)
2997
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2998
     */
2999 View Code Duplication
    public function testLoadContentByRemoteIdWithSecondParameter()
3000
    {
3001
        $repository = $this->getRepository();
3002
3003
        $contentService = $repository->getContentService();
3004
3005
        /* BEGIN: Use Case */
3006
        $draft = $this->createMultipleLanguageDraftVersion1();
3007
3008
        $contentService->publishVersion($draft->versionInfo);
3009
3010
        // This draft contains those fields localized with "eng-GB"
3011
        $draftLocalized = $contentService->loadContentByRemoteId(
3012
            $draft->contentInfo->remoteId,
3013
            ['eng-GB'],
3014
            null,
3015
            false
3016
        );
3017
        /* END: Use Case */
3018
3019
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
3020
    }
3021
3022
    /**
3023
     * Test for the loadContentByRemoteId() method.
3024
     *
3025
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
3026
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3027
     */
3028 View Code Duplication
    public function testLoadContentByRemoteIdWithThirdParameter()
3029
    {
3030
        $repository = $this->getRepository();
3031
3032
        $contentService = $repository->getContentService();
3033
3034
        /* BEGIN: Use Case */
3035
        $publishedContent = $this->createContentVersion1();
3036
3037
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
0 ignored issues
show
Unused Code introduced by
$draftContent is not used, you could remove the assignment.

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

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

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

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

Loading history...
3038
3039
        // This content instance is identical to $draftContent
3040
        $draftContentReloaded = $contentService->loadContentByRemoteId(
3041
            $publishedContent->contentInfo->remoteId,
3042
            null,
3043
            2
3044
        );
3045
        /* END: Use Case */
3046
3047
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
3048
3049
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
3050
        $this->assertEquals(
3051
            $publishedContent->versionInfo->contentInfo->mainLocationId,
3052
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
3053
        );
3054
    }
3055
3056
    /**
3057
     * Test for the loadContentByRemoteId() method.
3058
     *
3059
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
3060
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
3061
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithThirdParameter
3062
     */
3063
    public function testLoadContentByRemoteIdThrowsNotFoundExceptionWithThirdParameter()
3064
    {
3065
        $repository = $this->getRepository();
3066
3067
        $contentService = $repository->getContentService();
3068
3069
        /* BEGIN: Use Case */
3070
        $content = $this->createContentVersion1();
3071
3072
        // This call will fail with a "NotFoundException", because for this
3073
        // content object no versionNo=2 exists.
3074
        $contentService->loadContentByRemoteId(
3075
            $content->contentInfo->remoteId,
3076
            null,
3077
            2
3078
        );
3079
        /* END: Use Case */
3080
    }
3081
3082
    /**
3083
     * Test that retrieval of translated name field respects prioritized language list.
3084
     *
3085
     * @dataProvider getPrioritizedLanguageList
3086
     * @param string[]|null $languageCodes
3087
     */
3088
    public function testLoadContentWithPrioritizedLanguagesList($languageCodes)
3089
    {
3090
        $repository = $this->getRepository();
3091
3092
        $contentService = $repository->getContentService();
3093
3094
        $content = $this->createContentVersion2();
3095
3096
        $content = $contentService->loadContent($content->id, $languageCodes);
3097
3098
        $expectedName = $content->getVersionInfo()->getName(
3099
            isset($languageCodes[0]) ? $languageCodes[0] : null
3100
        );
3101
        $nameValue = $content->getFieldValue('name');
3102
        /** @var \eZ\Publish\Core\FieldType\TextLine\Value $nameValue */
3103
        self::assertEquals($expectedName, $nameValue->text);
3104
        self::assertEquals($expectedName, $content->getVersionInfo()->getName());
3105
        // Also check value on shortcut method on content
3106
        self::assertEquals($expectedName, $content->getName());
3107
    }
3108
3109
    /**
3110
     * @return array
3111
     */
3112
    public function getPrioritizedLanguageList()
3113
    {
3114
        return [
3115
            [['eng-US']],
3116
            [['eng-GB']],
3117
            [['eng-GB', 'eng-US']],
3118
            [['eng-US', 'eng-GB']],
3119
        ];
3120
    }
3121
3122
    /**
3123
     * Test for the deleteVersion() method.
3124
     *
3125
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
3126
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3127
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3128
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3129
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
3130
     */
3131
    public function testDeleteVersion()
3132
    {
3133
        $repository = $this->getRepository();
3134
3135
        $contentService = $repository->getContentService();
3136
3137
        /* BEGIN: Use Case */
3138
        $content = $this->createContentVersion1();
3139
3140
        // Create new draft, because published or last version of the Content can't be deleted
3141
        $draft = $contentService->createContentDraft(
3142
            $content->getVersionInfo()->getContentInfo()
3143
        );
3144
3145
        // Delete the previously created draft
3146
        $contentService->deleteVersion($draft->getVersionInfo());
3147
        /* END: Use Case */
3148
3149
        $versions = $contentService->loadVersions($content->getVersionInfo()->getContentInfo());
3150
3151
        $this->assertCount(1, $versions);
3152
        $this->assertEquals(
3153
            $content->getVersionInfo()->id,
3154
            $versions[0]->id
3155
        );
3156
    }
3157
3158
    /**
3159
     * Test for the deleteVersion() method.
3160
     *
3161
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
3162
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3163
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3164
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3165
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3166
     */
3167
    public function testDeleteVersionThrowsBadStateExceptionOnPublishedVersion()
3168
    {
3169
        $repository = $this->getRepository();
3170
3171
        $contentService = $repository->getContentService();
3172
3173
        /* BEGIN: Use Case */
3174
        $content = $this->createContentVersion1();
3175
3176
        // This call will fail with a "BadStateException", because the content
3177
        // version is currently published.
3178
        $contentService->deleteVersion($content->getVersionInfo());
3179
        /* END: Use Case */
3180
    }
3181
3182
    /**
3183
     * Test for the deleteVersion() method.
3184
     *
3185
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
3186
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
3187
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3188
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3189
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3190
     */
3191
    public function testDeleteVersionWorksIfOnlyVersionIsDraft()
3192
    {
3193
        $repository = $this->getRepository();
3194
3195
        $contentService = $repository->getContentService();
3196
3197
        /* BEGIN: Use Case */
3198
        $draft = $this->createContentDraftVersion1();
3199
3200
        $contentService->deleteVersion($draft->getVersionInfo());
3201
3202
        // This call will fail with a "NotFound", because we allow to delete content if remaining version is draft.
3203
        // Can normally only happen if there where always only a draft to begin with, simplifies UI edit API usage.
3204
        $contentService->loadContent($draft->id);
3205
        /* END: Use Case */
3206
    }
3207
3208
    /**
3209
     * Test for the loadVersions() method.
3210
     *
3211
     * @see \eZ\Publish\API\Repository\ContentService::loadVersions()
3212
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3213
     *
3214
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[]
3215
     */
3216
    public function testLoadVersions()
3217
    {
3218
        $repository = $this->getRepository();
3219
3220
        $contentService = $repository->getContentService();
3221
3222
        /* BEGIN: Use Case */
3223
        $contentVersion2 = $this->createContentVersion2();
3224
3225
        // Load versions of this ContentInfo instance
3226
        $versions = $contentService->loadVersions($contentVersion2->contentInfo);
3227
        /* END: Use Case */
3228
3229
        $expectedVersionsOrder = [
3230
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1),
3231
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 2),
3232
        ];
3233
3234
        $this->assertEquals($expectedVersionsOrder, $versions);
3235
3236
        return $versions;
3237
    }
3238
3239
    /**
3240
     * Test for the loadVersions() method.
3241
     *
3242
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersions
3243
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersions
3244
     *
3245
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo[] $versions
3246
     */
3247
    public function testLoadVersionsSetsExpectedVersionInfo(array $versions)
3248
    {
3249
        $this->assertCount(2, $versions);
3250
3251
        $expectedVersions = [
3252
            [
3253
                'versionNo' => 1,
3254
                'creatorId' => 14,
3255
                'status' => VersionInfo::STATUS_ARCHIVED,
3256
                'initialLanguageCode' => 'eng-US',
3257
                'languageCodes' => ['eng-US'],
3258
            ],
3259
            [
3260
                'versionNo' => 2,
3261
                'creatorId' => 10,
3262
                'status' => VersionInfo::STATUS_PUBLISHED,
3263
                'initialLanguageCode' => 'eng-US',
3264
                'languageCodes' => ['eng-US', 'eng-GB'],
3265
            ],
3266
        ];
3267
3268
        $this->assertPropertiesCorrect($expectedVersions[0], $versions[0]);
3269
        $this->assertPropertiesCorrect($expectedVersions[1], $versions[1]);
3270
        $this->assertEquals(
3271
            $versions[0]->creationDate->getTimestamp(),
3272
            $versions[1]->creationDate->getTimestamp(),
3273
            'Creation time did not match within delta of 2 seconds',
3274
            2
3275
        );
3276
        $this->assertEquals(
3277
            $versions[0]->modificationDate->getTimestamp(),
3278
            $versions[1]->modificationDate->getTimestamp(),
3279
            'Creation time did not match within delta of 2 seconds',
3280
            2
3281
        );
3282
        $this->assertTrue($versions[0]->isArchived());
3283
        $this->assertFalse($versions[0]->isDraft());
3284
        $this->assertFalse($versions[0]->isPublished());
3285
3286
        $this->assertTrue($versions[1]->isPublished());
3287
        $this->assertFalse($versions[1]->isDraft());
3288
        $this->assertFalse($versions[1]->isArchived());
3289
    }
3290
3291
    /**
3292
     * Test for the copyContent() method.
3293
     *
3294
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
3295
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3296
     * @group field-type
3297
     */
3298 View Code Duplication
    public function testCopyContent()
3299
    {
3300
        $parentLocationId = $this->generateId('location', 56);
3301
3302
        $repository = $this->getRepository();
3303
3304
        $contentService = $repository->getContentService();
3305
        $locationService = $repository->getLocationService();
3306
3307
        /* BEGIN: Use Case */
3308
        $contentVersion2 = $this->createMultipleLanguageContentVersion2();
3309
3310
        // Configure new target location
3311
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
3312
3313
        $targetLocationCreate->priority = 42;
3314
        $targetLocationCreate->hidden = true;
3315
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3316
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3317
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3318
3319
        // Copy content with all versions and drafts
3320
        $contentCopied = $contentService->copyContent(
3321
            $contentVersion2->contentInfo,
3322
            $targetLocationCreate
3323
        );
3324
        /* END: Use Case */
3325
3326
        $this->assertInstanceOf(
3327
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
3328
            $contentCopied
3329
        );
3330
3331
        $this->assertNotEquals(
3332
            $contentVersion2->contentInfo->remoteId,
3333
            $contentCopied->contentInfo->remoteId
3334
        );
3335
3336
        $this->assertNotEquals(
3337
            $contentVersion2->id,
3338
            $contentCopied->id
3339
        );
3340
3341
        $this->assertEquals(
3342
            2,
3343
            count($contentService->loadVersions($contentCopied->contentInfo))
3344
        );
3345
3346
        $this->assertEquals(2, $contentCopied->getVersionInfo()->versionNo);
3347
3348
        $this->assertAllFieldsEquals($contentCopied->getFields());
3349
3350
        $this->assertDefaultContentStates($contentCopied->contentInfo);
3351
3352
        $this->assertNotNull(
3353
            $contentCopied->contentInfo->mainLocationId,
3354
            'Expected main location to be set given we provided a LocationCreateStruct'
3355
        );
3356
    }
3357
3358
    /**
3359
     * Test for the copyContent() method with ezsettings.default.content.retain_owner_on_copy set to false
3360
     * See settings/test/integration_legacy.yml for service override.
3361
     *
3362
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
3363
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3364
     * @group field-type
3365
     */
3366
    public function testCopyContentWithNewOwner()
3367
    {
3368
        $parentLocationId = $this->generateId('location', 56);
3369
3370
        $repository = $this->getRepository();
3371
3372
        $contentService = $repository->getContentService();
3373
        $locationService = $repository->getLocationService();
3374
        $userService = $repository->getUserService();
3375
3376
        $newOwner = $this->createUser('new_owner', 'foo', 'bar');
3377
        /* BEGIN: Use Case */
3378
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $contentVersion2 */
3379
        $contentVersion2 = $this->createContentDraftVersion1(
3380
            $parentLocationId,
3381
            'forum',
3382
            'name',
3383
            $newOwner
3384
        );
3385
3386
        // Configure new target location
3387
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
3388
3389
        $targetLocationCreate->priority = 42;
3390
        $targetLocationCreate->hidden = true;
3391
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3392
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3393
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3394
3395
        // Copy content with all versions and drafts
3396
        $contentCopied = $contentService->copyContent(
3397
            $contentVersion2->contentInfo,
3398
            $targetLocationCreate
3399
        );
3400
        /* END: Use Case */
3401
3402
        $this->assertEquals(
3403
            $newOwner->id,
3404
            $contentVersion2->contentInfo->ownerId
3405
        );
3406
        $this->assertEquals(
3407
            $userService->loadUserByLogin('admin')->getUserId(),
3408
            $contentCopied->contentInfo->ownerId
3409
        );
3410
    }
3411
3412
    /**
3413
     * Test for the copyContent() method.
3414
     *
3415
     * @see \eZ\Publish\API\Repository\ContentService::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo)
3416
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
3417
     *
3418
     * @todo Fix to more descriptive name
3419
     */
3420 View Code Duplication
    public function testCopyContentWithThirdParameter()
3421
    {
3422
        $parentLocationId = $this->generateId('location', 56);
3423
3424
        $repository = $this->getRepository();
3425
3426
        $contentService = $repository->getContentService();
3427
        $locationService = $repository->getLocationService();
3428
3429
        /* BEGIN: Use Case */
3430
        $contentVersion2 = $this->createContentVersion2();
3431
3432
        // Configure new target location
3433
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
3434
3435
        $targetLocationCreate->priority = 42;
3436
        $targetLocationCreate->hidden = true;
3437
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3438
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3439
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3440
3441
        // Copy only the initial version
3442
        $contentCopied = $contentService->copyContent(
3443
            $contentVersion2->contentInfo,
3444
            $targetLocationCreate,
3445
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1)
3446
        );
3447
        /* END: Use Case */
3448
3449
        $this->assertInstanceOf(
3450
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
3451
            $contentCopied
3452
        );
3453
3454
        $this->assertNotEquals(
3455
            $contentVersion2->contentInfo->remoteId,
3456
            $contentCopied->contentInfo->remoteId
3457
        );
3458
3459
        $this->assertNotEquals(
3460
            $contentVersion2->id,
3461
            $contentCopied->id
3462
        );
3463
3464
        $this->assertEquals(
3465
            1,
3466
            count($contentService->loadVersions($contentCopied->contentInfo))
3467
        );
3468
3469
        $this->assertEquals(1, $contentCopied->getVersionInfo()->versionNo);
3470
3471
        $this->assertNotNull(
3472
            $contentCopied->contentInfo->mainLocationId,
3473
            'Expected main location to be set given we provided a LocationCreateStruct'
3474
        );
3475
    }
3476
3477
    /**
3478
     * Test for the addRelation() method.
3479
     *
3480
     * @return \eZ\Publish\API\Repository\Values\Content\Content
3481
     *
3482
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3483
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3484
     */
3485
    public function testAddRelation()
3486
    {
3487
        $repository = $this->getRepository();
3488
3489
        $contentService = $repository->getContentService();
3490
3491
        /* BEGIN: Use Case */
3492
        // RemoteId of the "Media" content of an eZ Publish demo installation
3493
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3494
3495
        $draft = $this->createContentDraftVersion1();
3496
3497
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3498
3499
        // Create relation between new content object and "Media" page
3500
        $relation = $contentService->addRelation(
3501
            $draft->getVersionInfo(),
3502
            $media
3503
        );
3504
        /* END: Use Case */
3505
3506
        $this->assertInstanceOf(
3507
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Relation',
3508
            $relation
3509
        );
3510
3511
        return $contentService->loadRelations($draft->getVersionInfo());
3512
    }
3513
3514
    /**
3515
     * Test for the addRelation() method.
3516
     *
3517
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3518
     *
3519
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3520
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3521
     */
3522
    public function testAddRelationAddsRelationToContent($relations)
3523
    {
3524
        $this->assertEquals(
3525
            1,
3526
            count($relations)
3527
        );
3528
    }
3529
3530
    /**
3531
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3532
     */
3533
    protected function assertExpectedRelations($relations)
3534
    {
3535
        $this->assertEquals(
3536
            [
3537
                'type' => Relation::COMMON,
3538
                'sourceFieldDefinitionIdentifier' => null,
3539
                'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3540
                'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3541
            ],
3542
            [
3543
                'type' => $relations[0]->type,
3544
                'sourceFieldDefinitionIdentifier' => $relations[0]->sourceFieldDefinitionIdentifier,
3545
                'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3546
                'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3547
            ]
3548
        );
3549
    }
3550
3551
    /**
3552
     * Test for the addRelation() method.
3553
     *
3554
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3555
     *
3556
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3557
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3558
     */
3559
    public function testAddRelationSetsExpectedRelations($relations)
3560
    {
3561
        $this->assertExpectedRelations($relations);
3562
    }
3563
3564
    /**
3565
     * Test for the createContentDraft() method.
3566
     *
3567
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3568
     *
3569
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
3570
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelationSetsExpectedRelations
3571
     */
3572 View Code Duplication
    public function testCreateContentDraftWithRelations()
3573
    {
3574
        $repository = $this->getRepository();
3575
3576
        $contentService = $repository->getContentService();
3577
3578
        // RemoteId of the "Media" content of an eZ Publish demo installation
3579
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3580
        $draft = $this->createContentDraftVersion1();
3581
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3582
3583
        // Create relation between new content object and "Media" page
3584
        $contentService->addRelation(
3585
            $draft->getVersionInfo(),
3586
            $media
3587
        );
3588
3589
        $content = $contentService->publishVersion($draft->versionInfo);
3590
        $newDraft = $contentService->createContentDraft($content->contentInfo);
3591
3592
        return $contentService->loadRelations($newDraft->getVersionInfo());
3593
    }
3594
3595
    /**
3596
     * Test for the createContentDraft() method.
3597
     *
3598
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3599
     *
3600
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3601
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelations
3602
     */
3603
    public function testCreateContentDraftWithRelationsCreatesRelations($relations)
3604
    {
3605
        $this->assertEquals(
3606
            1,
3607
            count($relations)
3608
        );
3609
3610
        return $relations;
3611
    }
3612
3613
    /**
3614
     * Test for the createContentDraft() method.
3615
     *
3616
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3617
     *
3618
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelationsCreatesRelations
3619
     */
3620
    public function testCreateContentDraftWithRelationsCreatesExpectedRelations($relations)
3621
    {
3622
        $this->assertExpectedRelations($relations);
3623
    }
3624
3625
    /**
3626
     * Test for the addRelation() method.
3627
     *
3628
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3629
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3630
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3631
     */
3632 View Code Duplication
    public function testAddRelationThrowsBadStateException()
3633
    {
3634
        $repository = $this->getRepository();
3635
3636
        $contentService = $repository->getContentService();
3637
3638
        /* BEGIN: Use Case */
3639
        // RemoteId of the "Media" page of an eZ Publish demo installation
3640
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3641
3642
        $content = $this->createContentVersion1();
3643
3644
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3645
3646
        // This call will fail with a "BadStateException", because content is
3647
        // published and not a draft.
3648
        $contentService->addRelation(
3649
            $content->getVersionInfo(),
3650
            $media
3651
        );
3652
        /* END: Use Case */
3653
    }
3654
3655
    /**
3656
     * Test for the loadRelations() method.
3657
     *
3658
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3659
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3660
     */
3661
    public function testLoadRelations()
3662
    {
3663
        $repository = $this->getRepository();
3664
3665
        $contentService = $repository->getContentService();
3666
3667
        /* BEGIN: Use Case */
3668
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3669
        // of a eZ Publish demo installation.
3670
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3671
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3672
3673
        $draft = $this->createContentDraftVersion1();
3674
3675
        // Load other content objects
3676
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3677
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3678
3679
        // Create relation between new content object and "Media" page
3680
        $contentService->addRelation(
3681
            $draft->getVersionInfo(),
3682
            $media
3683
        );
3684
3685
        // Create another relation with the "Demo Design" page
3686
        $contentService->addRelation(
3687
            $draft->getVersionInfo(),
3688
            $demoDesign
3689
        );
3690
3691
        // Load all relations
3692
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3693
        /* END: Use Case */
3694
3695
        usort(
3696
            $relations,
3697
            function ($rel1, $rel2) {
3698
                return strcasecmp(
3699
                    $rel2->getDestinationContentInfo()->remoteId,
3700
                    $rel1->getDestinationContentInfo()->remoteId
3701
                );
3702
            }
3703
        );
3704
3705
        $this->assertEquals(
3706
            [
3707
                [
3708
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3709
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3710
                ],
3711
                [
3712
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3713
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3714
                ],
3715
            ],
3716
            [
3717
                [
3718
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3719
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3720
                ],
3721
                [
3722
                    'sourceContentInfo' => $relations[1]->sourceContentInfo->remoteId,
3723
                    'destinationContentInfo' => $relations[1]->destinationContentInfo->remoteId,
3724
                ],
3725
            ]
3726
        );
3727
    }
3728
3729
    /**
3730
     * Test for the loadRelations() method.
3731
     *
3732
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3733
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3734
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3735
     */
3736
    public function testLoadRelationsSkipsArchivedContent()
3737
    {
3738
        $repository = $this->getRepository();
3739
3740
        $contentService = $repository->getContentService();
3741
3742
        /* BEGIN: Use Case */
3743
        $trashService = $repository->getTrashService();
3744
        $locationService = $repository->getLocationService();
3745
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3746
        // of a eZ Publish demo installation.
3747
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3748
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3749
3750
        $draft = $this->createContentDraftVersion1();
3751
3752
        // Load other content objects
3753
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3754
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3755
3756
        // Create relation between new content object and "Media" page
3757
        $contentService->addRelation(
3758
            $draft->getVersionInfo(),
3759
            $media
3760
        );
3761
3762
        // Create another relation with the "Demo Design" page
3763
        $contentService->addRelation(
3764
            $draft->getVersionInfo(),
3765
            $demoDesign
3766
        );
3767
3768
        $demoDesignLocation = $locationService->loadLocation($demoDesign->mainLocationId);
3769
3770
        // Trashing Content's last Location will change its status to archived,
3771
        // in this case relation towards it will not be loaded.
3772
        $trashService->trash($demoDesignLocation);
3773
3774
        // Load all relations
3775
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3776
        /* END: Use Case */
3777
3778
        $this->assertCount(1, $relations);
3779
        $this->assertEquals(
3780
            [
3781
                [
3782
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3783
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3784
                ],
3785
            ],
3786
            [
3787
                [
3788
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3789
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3790
                ],
3791
            ]
3792
        );
3793
    }
3794
3795
    /**
3796
     * Test for the loadRelations() method.
3797
     *
3798
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3799
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3800
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3801
     */
3802
    public function testLoadRelationsSkipsDraftContent()
3803
    {
3804
        $repository = $this->getRepository();
3805
3806
        $contentService = $repository->getContentService();
3807
3808
        /* BEGIN: Use Case */
3809
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3810
        // of a eZ Publish demo installation.
3811
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3812
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3813
3814
        $draft = $this->createContentDraftVersion1();
3815
3816
        // Load other content objects
3817
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3818
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3819
3820
        // Create draft of "Media" page
3821
        $mediaDraft = $contentService->createContentDraft($media->contentInfo);
3822
3823
        // Create relation between "Media" page and new content object draft.
3824
        // This relation will not be loaded before the draft is published.
3825
        $contentService->addRelation(
3826
            $mediaDraft->getVersionInfo(),
3827
            $draft->getVersionInfo()->getContentInfo()
3828
        );
3829
3830
        // Create another relation with the "Demo Design" page
3831
        $contentService->addRelation(
3832
            $mediaDraft->getVersionInfo(),
3833
            $demoDesign
3834
        );
3835
3836
        // Load all relations
3837
        $relations = $contentService->loadRelations($mediaDraft->getVersionInfo());
3838
        /* END: Use Case */
3839
3840
        $this->assertCount(1, $relations);
3841
        $this->assertEquals(
3842
            [
3843
                [
3844
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3845
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3846
                ],
3847
            ],
3848
            [
3849
                [
3850
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3851
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3852
                ],
3853
            ]
3854
        );
3855
    }
3856
3857
    /**
3858
     * Test for the loadReverseRelations() method.
3859
     *
3860
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3861
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3862
     */
3863
    public function testLoadReverseRelations()
3864
    {
3865
        $repository = $this->getRepository();
3866
3867
        $contentService = $repository->getContentService();
3868
3869
        /* BEGIN: Use Case */
3870
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3871
        // of a eZ Publish demo installation.
3872
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3873
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3874
3875
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3876
        $contentInfo = $versionInfo->getContentInfo();
3877
3878
        // Create some drafts
3879
        $mediaDraft = $contentService->createContentDraft(
3880
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3881
        );
3882
        $demoDesignDraft = $contentService->createContentDraft(
3883
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3884
        );
3885
3886
        // Create relation between new content object and "Media" page
3887
        $relation1 = $contentService->addRelation(
3888
            $mediaDraft->getVersionInfo(),
3889
            $contentInfo
3890
        );
3891
3892
        // Create another relation with the "Demo Design" page
3893
        $relation2 = $contentService->addRelation(
3894
            $demoDesignDraft->getVersionInfo(),
3895
            $contentInfo
3896
        );
3897
3898
        // Publish drafts, so relations become active
3899
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3900
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3901
3902
        // Load all relations
3903
        $relations = $contentService->loadRelations($versionInfo);
3904
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3905
        /* END: Use Case */
3906
3907
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3908
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3909
3910
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3911
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3912
3913
        $this->assertEquals(0, count($relations));
3914
        $this->assertEquals(2, count($reverseRelations));
3915
3916
        usort(
3917
            $reverseRelations,
3918
            function ($rel1, $rel2) {
3919
                return strcasecmp(
3920
                    $rel2->getSourceContentInfo()->remoteId,
3921
                    $rel1->getSourceContentInfo()->remoteId
3922
                );
3923
            }
3924
        );
3925
3926
        $this->assertEquals(
3927
            [
3928
                [
3929
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3930
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3931
                ],
3932
                [
3933
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3934
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3935
                ],
3936
            ],
3937
            [
3938
                [
3939
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3940
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3941
                ],
3942
                [
3943
                    'sourceContentInfo' => $reverseRelations[1]->sourceContentInfo->remoteId,
3944
                    'destinationContentInfo' => $reverseRelations[1]->destinationContentInfo->remoteId,
3945
                ],
3946
            ]
3947
        );
3948
    }
3949
3950
    /**
3951
     * Test for the loadReverseRelations() method.
3952
     *
3953
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3954
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3955
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3956
     */
3957
    public function testLoadReverseRelationsSkipsArchivedContent()
3958
    {
3959
        $repository = $this->getRepository();
3960
3961
        $contentService = $repository->getContentService();
3962
3963
        /* BEGIN: Use Case */
3964
        $trashService = $repository->getTrashService();
3965
        $locationService = $repository->getLocationService();
3966
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3967
        // of a eZ Publish demo installation.
3968
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3969
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3970
3971
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3972
        $contentInfo = $versionInfo->getContentInfo();
3973
3974
        // Create some drafts
3975
        $mediaDraft = $contentService->createContentDraft(
3976
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3977
        );
3978
        $demoDesignDraft = $contentService->createContentDraft(
3979
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3980
        );
3981
3982
        // Create relation between new content object and "Media" page
3983
        $relation1 = $contentService->addRelation(
3984
            $mediaDraft->getVersionInfo(),
3985
            $contentInfo
3986
        );
3987
3988
        // Create another relation with the "Demo Design" page
3989
        $relation2 = $contentService->addRelation(
3990
            $demoDesignDraft->getVersionInfo(),
3991
            $contentInfo
3992
        );
3993
3994
        // Publish drafts, so relations become active
3995
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3996
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3997
3998
        $demoDesignLocation = $locationService->loadLocation($demoDesignDraft->contentInfo->mainLocationId);
3999
4000
        // Trashing Content's last Location will change its status to archived,
4001
        // in this case relation from it will not be loaded.
4002
        $trashService->trash($demoDesignLocation);
4003
4004
        // Load all relations
4005
        $relations = $contentService->loadRelations($versionInfo);
4006
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
4007
        /* END: Use Case */
4008
4009
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
4010
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
4011
4012
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
4013
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
4014
4015
        $this->assertEquals(0, count($relations));
4016
        $this->assertEquals(1, count($reverseRelations));
4017
4018
        $this->assertEquals(
4019
            [
4020
                [
4021
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
4022
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
4023
                ],
4024
            ],
4025
            [
4026
                [
4027
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
4028
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
4029
                ],
4030
            ]
4031
        );
4032
    }
4033
4034
    /**
4035
     * Test for the loadReverseRelations() method.
4036
     *
4037
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
4038
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
4039
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
4040
     */
4041
    public function testLoadReverseRelationsSkipsDraftContent()
4042
    {
4043
        $repository = $this->getRepository();
4044
4045
        $contentService = $repository->getContentService();
4046
4047
        /* BEGIN: Use Case */
4048
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
4049
        // of a eZ Publish demo installation.
4050
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
4051
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
4052
4053
        // Load "Media" page Content
4054
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
4055
4056
        // Create some drafts
4057
        $newDraftVersionInfo = $this->createContentDraftVersion1()->getVersionInfo();
4058
        $demoDesignDraft = $contentService->createContentDraft(
4059
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
4060
        );
4061
4062
        // Create relation between "Media" page and new content object
4063
        $relation1 = $contentService->addRelation(
4064
            $newDraftVersionInfo,
4065
            $media->contentInfo
4066
        );
4067
4068
        // Create another relation with the "Demo Design" page
4069
        $relation2 = $contentService->addRelation(
4070
            $demoDesignDraft->getVersionInfo(),
4071
            $media->contentInfo
4072
        );
4073
4074
        // Publish drafts, so relations become active
4075
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
4076
        // We will not publish new Content draft, therefore relation from it
4077
        // will not be loaded as reverse relation for "Media" page
4078
        //$contentService->publishVersion( $newDraftVersionInfo );
4079
4080
        // Load all relations
4081
        $relations = $contentService->loadRelations($media->versionInfo);
4082
        $reverseRelations = $contentService->loadReverseRelations($media->contentInfo);
4083
        /* END: Use Case */
4084
4085
        $this->assertEquals($media->contentInfo->id, $relation1->getDestinationContentInfo()->id);
4086
        $this->assertEquals($newDraftVersionInfo->contentInfo->id, $relation1->getSourceContentInfo()->id);
4087
4088
        $this->assertEquals($media->contentInfo->id, $relation2->getDestinationContentInfo()->id);
4089
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
4090
4091
        $this->assertEquals(0, count($relations));
4092
        $this->assertEquals(1, count($reverseRelations));
4093
4094
        $this->assertEquals(
4095
            [
4096
                [
4097
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
4098
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
4099
                ],
4100
            ],
4101
            [
4102
                [
4103
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
4104
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
4105
                ],
4106
            ]
4107
        );
4108
    }
4109
4110
    /**
4111
     * Test for the deleteRelation() method.
4112
     *
4113
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
4114
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
4115
     */
4116
    public function testDeleteRelation()
4117
    {
4118
        $repository = $this->getRepository();
4119
4120
        $contentService = $repository->getContentService();
4121
4122
        /* BEGIN: Use Case */
4123
        // Remote ids of the "Media" and the "Demo Design" page of a eZ Publish
4124
        // demo installation.
4125
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
4126
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
4127
4128
        $draft = $this->createContentDraftVersion1();
4129
4130
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
4131
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
4132
4133
        // Establish some relations
4134
        $contentService->addRelation($draft->getVersionInfo(), $media);
4135
        $contentService->addRelation($draft->getVersionInfo(), $demoDesign);
4136
4137
        // Delete one of the currently created relations
4138
        $contentService->deleteRelation($draft->getVersionInfo(), $media);
4139
4140
        // The relations array now contains only one element
4141
        $relations = $contentService->loadRelations($draft->getVersionInfo());
4142
        /* END: Use Case */
4143
4144
        $this->assertEquals(1, count($relations));
4145
    }
4146
4147
    /**
4148
     * Test for the deleteRelation() method.
4149
     *
4150
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
4151
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
4152
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
4153
     */
4154 View Code Duplication
    public function testDeleteRelationThrowsBadStateException()
4155
    {
4156
        $repository = $this->getRepository();
4157
4158
        $contentService = $repository->getContentService();
4159
4160
        /* BEGIN: Use Case */
4161
        // RemoteId of the "Media" page of an eZ Publish demo installation
4162
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
4163
4164
        $content = $this->createContentVersion1();
4165
4166
        // Load the destination object
4167
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
4168
4169
        // Create a new draft
4170
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
4171
4172
        // Add a relation
4173
        $contentService->addRelation($draftVersion2->getVersionInfo(), $media);
4174
4175
        // Publish new version
4176
        $contentVersion2 = $contentService->publishVersion(
4177
            $draftVersion2->getVersionInfo()
4178
        );
4179
4180
        // This call will fail with a "BadStateException", because content is
4181
        // published and not a draft.
4182
        $contentService->deleteRelation(
4183
            $contentVersion2->getVersionInfo(),
4184
            $media
4185
        );
4186
        /* END: Use Case */
4187
    }
4188
4189
    /**
4190
     * Test for the deleteRelation() method.
4191
     *
4192
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
4193
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
4194
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
4195
     */
4196 View Code Duplication
    public function testDeleteRelationThrowsInvalidArgumentException()
4197
    {
4198
        $repository = $this->getRepository();
4199
4200
        $contentService = $repository->getContentService();
4201
4202
        /* BEGIN: Use Case */
4203
        // RemoteId of the "Media" page of an eZ Publish demo installation
4204
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
4205
4206
        $draft = $this->createContentDraftVersion1();
4207
4208
        // Load the destination object
4209
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
4210
4211
        // This call will fail with a "InvalidArgumentException", because no
4212
        // relation exists between $draft and $media.
4213
        $contentService->deleteRelation(
4214
            $draft->getVersionInfo(),
4215
            $media
4216
        );
4217
        /* END: Use Case */
4218
    }
4219
4220
    /**
4221
     * Test for the createContent() method.
4222
     *
4223
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
4224
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4225
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4226
     */
4227
    public function testCreateContentInTransactionWithRollback()
4228
    {
4229
        if ($this->isVersion4()) {
4230
            $this->markTestSkipped('This test requires eZ Publish 5');
4231
        }
4232
4233
        $repository = $this->getRepository();
4234
4235
        /* BEGIN: Use Case */
4236
        $contentTypeService = $repository->getContentTypeService();
4237
        $contentService = $repository->getContentService();
4238
4239
        // Start a transaction
4240
        $repository->beginTransaction();
4241
4242
        try {
4243
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
4244
4245
            // Get a content create struct and set mandatory properties
4246
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
4247
            $contentCreate->setField('name', 'Sindelfingen forum');
4248
4249
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
4250
            $contentCreate->alwaysAvailable = true;
4251
4252
            // Create a new content object
4253
            $contentId = $contentService->createContent($contentCreate)->id;
4254
        } catch (Exception $e) {
4255
            // Cleanup hanging transaction on error
4256
            $repository->rollback();
4257
            throw $e;
4258
        }
4259
4260
        // Rollback all changes
4261
        $repository->rollback();
4262
4263
        try {
4264
            // This call will fail with a "NotFoundException"
4265
            $contentService->loadContent($contentId);
4266
        } catch (NotFoundException $e) {
4267
            // This is expected
4268
            return;
4269
        }
4270
        /* END: Use Case */
4271
4272
        $this->fail('Content object still exists after rollback.');
4273
    }
4274
4275
    /**
4276
     * Test for the createContent() method.
4277
     *
4278
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
4279
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4280
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4281
     */
4282
    public function testCreateContentInTransactionWithCommit()
4283
    {
4284
        if ($this->isVersion4()) {
4285
            $this->markTestSkipped('This test requires eZ Publish 5');
4286
        }
4287
4288
        $repository = $this->getRepository();
4289
4290
        /* BEGIN: Use Case */
4291
        $contentTypeService = $repository->getContentTypeService();
4292
        $contentService = $repository->getContentService();
4293
4294
        // Start a transaction
4295
        $repository->beginTransaction();
4296
4297
        try {
4298
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
4299
4300
            // Get a content create struct and set mandatory properties
4301
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
4302
            $contentCreate->setField('name', 'Sindelfingen forum');
4303
4304
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
4305
            $contentCreate->alwaysAvailable = true;
4306
4307
            // Create a new content object
4308
            $contentId = $contentService->createContent($contentCreate)->id;
4309
4310
            // Commit changes
4311
            $repository->commit();
4312
        } catch (Exception $e) {
4313
            // Cleanup hanging transaction on error
4314
            $repository->rollback();
4315
            throw $e;
4316
        }
4317
4318
        // Load the new content object
4319
        $content = $contentService->loadContent($contentId);
4320
        /* END: Use Case */
4321
4322
        $this->assertEquals($contentId, $content->id);
4323
    }
4324
4325
    /**
4326
     * Test for the createContent() method.
4327
     *
4328
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
4329
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
4330
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
4331
     */
4332 View Code Duplication
    public function testCreateContentWithLocationCreateParameterInTransactionWithRollback()
4333
    {
4334
        $repository = $this->getRepository();
4335
4336
        $contentService = $repository->getContentService();
4337
4338
        /* BEGIN: Use Case */
4339
        // Start a transaction
4340
        $repository->beginTransaction();
4341
4342
        try {
4343
            $draft = $this->createContentDraftVersion1();
4344
        } catch (Exception $e) {
4345
            // Cleanup hanging transaction on error
4346
            $repository->rollback();
4347
            throw $e;
4348
        }
4349
4350
        $contentId = $draft->id;
4351
4352
        // Roleback the transaction
4353
        $repository->rollback();
4354
4355
        try {
4356
            // This call will fail with a "NotFoundException"
4357
            $contentService->loadContent($contentId);
4358
        } catch (NotFoundException $e) {
4359
            return;
4360
        }
4361
        /* END: Use Case */
4362
4363
        $this->fail('Can still load content object after rollback.');
4364
    }
4365
4366
    /**
4367
     * Test for the createContent() method.
4368
     *
4369
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
4370
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
4371
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
4372
     */
4373 View Code Duplication
    public function testCreateContentWithLocationCreateParameterInTransactionWithCommit()
4374
    {
4375
        $repository = $this->getRepository();
4376
4377
        $contentService = $repository->getContentService();
4378
4379
        /* BEGIN: Use Case */
4380
        // Start a transaction
4381
        $repository->beginTransaction();
4382
4383
        try {
4384
            $draft = $this->createContentDraftVersion1();
4385
4386
            $contentId = $draft->id;
4387
4388
            // Roleback the transaction
4389
            $repository->commit();
4390
        } catch (Exception $e) {
4391
            // Cleanup hanging transaction on error
4392
            $repository->rollback();
4393
            throw $e;
4394
        }
4395
4396
        // Load the new content object
4397
        $content = $contentService->loadContent($contentId);
4398
        /* END: Use Case */
4399
4400
        $this->assertEquals($contentId, $content->id);
4401
    }
4402
4403
    /**
4404
     * Test for the createContentDraft() method.
4405
     *
4406
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4407
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4408
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4409
     */
4410
    public function testCreateContentDraftInTransactionWithRollback()
4411
    {
4412
        $repository = $this->getRepository();
4413
4414
        $contentId = $this->generateId('object', 12);
4415
        /* BEGIN: Use Case */
4416
        // $contentId is the ID of the "Administrator users" user group
4417
4418
        // Get the content service
4419
        $contentService = $repository->getContentService();
4420
4421
        // Load the user group content object
4422
        $content = $contentService->loadContent($contentId);
4423
4424
        // Start a new transaction
4425
        $repository->beginTransaction();
4426
4427
        try {
4428
            // Create a new draft
4429
            $drafted = $contentService->createContentDraft($content->contentInfo);
4430
4431
            // Store version number for later reuse
4432
            $versionNo = $drafted->versionInfo->versionNo;
4433
        } catch (Exception $e) {
4434
            // Cleanup hanging transaction on error
4435
            $repository->rollback();
4436
            throw $e;
4437
        }
4438
4439
        // Rollback
4440
        $repository->rollback();
4441
4442
        try {
4443
            // This call will fail with a "NotFoundException"
4444
            $contentService->loadContent($contentId, null, $versionNo);
4445
        } catch (NotFoundException $e) {
4446
            return;
4447
        }
4448
        /* END: Use Case */
4449
4450
        $this->fail('Can still load content draft after rollback');
4451
    }
4452
4453
    /**
4454
     * Test for the createContentDraft() method.
4455
     *
4456
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4457
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4458
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4459
     */
4460 View Code Duplication
    public function testCreateContentDraftInTransactionWithCommit()
4461
    {
4462
        $repository = $this->getRepository();
4463
4464
        $contentId = $this->generateId('object', 12);
4465
        /* BEGIN: Use Case */
4466
        // $contentId is the ID of the "Administrator users" user group
4467
4468
        // Get the content service
4469
        $contentService = $repository->getContentService();
4470
4471
        // Load the user group content object
4472
        $content = $contentService->loadContent($contentId);
4473
4474
        // Start a new transaction
4475
        $repository->beginTransaction();
4476
4477
        try {
4478
            // Create a new draft
4479
            $drafted = $contentService->createContentDraft($content->contentInfo);
4480
4481
            // Store version number for later reuse
4482
            $versionNo = $drafted->versionInfo->versionNo;
4483
4484
            // Commit all changes
4485
            $repository->commit();
4486
        } catch (Exception $e) {
4487
            // Cleanup hanging transaction on error
4488
            $repository->rollback();
4489
            throw $e;
4490
        }
4491
4492
        $content = $contentService->loadContent($contentId, null, $versionNo);
4493
        /* END: Use Case */
4494
4495
        $this->assertEquals(
4496
            $versionNo,
4497
            $content->getVersionInfo()->versionNo
4498
        );
4499
    }
4500
4501
    /**
4502
     * Test for the publishVersion() method.
4503
     *
4504
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4505
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4506
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4507
     */
4508 View Code Duplication
    public function testPublishVersionInTransactionWithRollback()
4509
    {
4510
        $repository = $this->getRepository();
4511
4512
        $contentId = $this->generateId('object', 12);
4513
        /* BEGIN: Use Case */
4514
        // $contentId is the ID of the "Administrator users" user group
4515
4516
        // Get the content service
4517
        $contentService = $repository->getContentService();
4518
4519
        // Load the user group content object
4520
        $content = $contentService->loadContent($contentId);
4521
4522
        // Start a new transaction
4523
        $repository->beginTransaction();
4524
4525
        try {
4526
            $draftVersion = $contentService->createContentDraft($content->contentInfo)->getVersionInfo();
4527
4528
            // Publish a new version
4529
            $content = $contentService->publishVersion($draftVersion);
4530
4531
            // Store version number for later reuse
4532
            $versionNo = $content->versionInfo->versionNo;
4533
        } catch (Exception $e) {
4534
            // Cleanup hanging transaction on error
4535
            $repository->rollback();
4536
            throw $e;
4537
        }
4538
4539
        // Rollback
4540
        $repository->rollback();
4541
4542
        try {
4543
            // This call will fail with a "NotFoundException"
4544
            $contentService->loadContent($contentId, null, $versionNo);
4545
        } catch (NotFoundException $e) {
4546
            return;
4547
        }
4548
        /* END: Use Case */
4549
4550
        $this->fail('Can still load content draft after rollback');
4551
    }
4552
4553
    /**
4554
     * Test for the publishVersion() method.
4555
     *
4556
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4557
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4558
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
4559
     */
4560 View Code Duplication
    public function testPublishVersionInTransactionWithCommit()
4561
    {
4562
        $repository = $this->getRepository();
4563
4564
        /* BEGIN: Use Case */
4565
        // ID of the "Administrator users" user group
4566
        $contentId = 12;
4567
4568
        // Get the content service
4569
        $contentService = $repository->getContentService();
4570
4571
        // Load the user group content object
4572
        $template = $contentService->loadContent($contentId);
4573
4574
        // Start a new transaction
4575
        $repository->beginTransaction();
4576
4577
        try {
4578
            // Publish a new version
4579
            $content = $contentService->publishVersion(
4580
                $contentService->createContentDraft($template->contentInfo)->getVersionInfo()
4581
            );
4582
4583
            // Store version number for later reuse
4584
            $versionNo = $content->versionInfo->versionNo;
4585
4586
            // Commit all changes
4587
            $repository->commit();
4588
        } catch (Exception $e) {
4589
            // Cleanup hanging transaction on error
4590
            $repository->rollback();
4591
            throw $e;
4592
        }
4593
4594
        // Load current version info
4595
        $versionInfo = $contentService->loadVersionInfo($content->contentInfo);
4596
        /* END: Use Case */
4597
4598
        $this->assertEquals($versionNo, $versionInfo->versionNo);
4599
    }
4600
4601
    /**
4602
     * Test for the updateContent() method.
4603
     *
4604
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4605
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4606
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4607
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4608
     */
4609 View Code Duplication
    public function testUpdateContentInTransactionWithRollback()
4610
    {
4611
        $repository = $this->getRepository();
4612
4613
        $contentId = $this->generateId('object', 12);
4614
        /* BEGIN: Use Case */
4615
        // $contentId is the ID of the "Administrator users" user group
4616
4617
        // Load content service
4618
        $contentService = $repository->getContentService();
4619
4620
        // Create a new user group draft
4621
        $draft = $contentService->createContentDraft(
4622
            $contentService->loadContentInfo($contentId)
4623
        );
4624
4625
        // Get an update struct and change the group name
4626
        $contentUpdate = $contentService->newContentUpdateStruct();
4627
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4628
4629
        // Start a transaction
4630
        $repository->beginTransaction();
4631
4632
        try {
4633
            // Update the group name
4634
            $draft = $contentService->updateContent(
4635
                $draft->getVersionInfo(),
4636
                $contentUpdate
4637
            );
4638
4639
            // Publish updated version
4640
            $contentService->publishVersion($draft->getVersionInfo());
4641
        } catch (Exception $e) {
4642
            // Cleanup hanging transaction on error
4643
            $repository->rollback();
4644
            throw $e;
4645
        }
4646
4647
        // Rollback all changes.
4648
        $repository->rollback();
4649
4650
        // Name will still be "Administrator users"
4651
        $name = $contentService->loadContent($contentId)->getFieldValue('name');
4652
        /* END: Use Case */
4653
4654
        $this->assertEquals('Administrator users', $name);
4655
    }
4656
4657
    /**
4658
     * Test for the updateContent() method.
4659
     *
4660
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4661
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4662
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4663
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4664
     */
4665 View Code Duplication
    public function testUpdateContentInTransactionWithCommit()
4666
    {
4667
        $repository = $this->getRepository();
4668
4669
        $contentId = $this->generateId('object', 12);
4670
        /* BEGIN: Use Case */
4671
        // $contentId is the ID of the "Administrator users" user group
4672
4673
        // Load content service
4674
        $contentService = $repository->getContentService();
4675
4676
        // Create a new user group draft
4677
        $draft = $contentService->createContentDraft(
4678
            $contentService->loadContentInfo($contentId)
4679
        );
4680
4681
        // Get an update struct and change the group name
4682
        $contentUpdate = $contentService->newContentUpdateStruct();
4683
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4684
4685
        // Start a transaction
4686
        $repository->beginTransaction();
4687
4688
        try {
4689
            // Update the group name
4690
            $draft = $contentService->updateContent(
4691
                $draft->getVersionInfo(),
4692
                $contentUpdate
4693
            );
4694
4695
            // Publish updated version
4696
            $contentService->publishVersion($draft->getVersionInfo());
4697
4698
            // Commit all changes.
4699
            $repository->commit();
4700
        } catch (Exception $e) {
4701
            // Cleanup hanging transaction on error
4702
            $repository->rollback();
4703
            throw $e;
4704
        }
4705
4706
        // Name is now "Administrators"
4707
        $name = $contentService->loadContent($contentId)->getFieldValue('name', 'eng-US');
4708
        /* END: Use Case */
4709
4710
        $this->assertEquals('Administrators', $name);
4711
    }
4712
4713
    /**
4714
     * Test for the updateContentMetadata() method.
4715
     *
4716
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4717
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4718
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4719
     */
4720 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithRollback()
4721
    {
4722
        $repository = $this->getRepository();
4723
4724
        $contentId = $this->generateId('object', 12);
4725
        /* BEGIN: Use Case */
4726
        // $contentId is the ID of the "Administrator users" user group
4727
4728
        // Get the content service
4729
        $contentService = $repository->getContentService();
4730
4731
        // Load a ContentInfo object
4732
        $contentInfo = $contentService->loadContentInfo($contentId);
4733
4734
        // Store remoteId for later testing
4735
        $remoteId = $contentInfo->remoteId;
4736
4737
        // Start a transaction
4738
        $repository->beginTransaction();
4739
4740
        try {
4741
            // Get metadata update struct and change remoteId
4742
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4743
            $metadataUpdate->remoteId = md5(microtime(true));
4744
4745
            // Update the metadata of the published content object
4746
            $contentService->updateContentMetadata(
4747
                $contentInfo,
4748
                $metadataUpdate
4749
            );
4750
        } catch (Exception $e) {
4751
            // Cleanup hanging transaction on error
4752
            $repository->rollback();
4753
            throw $e;
4754
        }
4755
4756
        // Rollback all changes.
4757
        $repository->rollback();
4758
4759
        // Load current remoteId
4760
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4761
        /* END: Use Case */
4762
4763
        $this->assertEquals($remoteId, $remoteIdReloaded);
4764
    }
4765
4766
    /**
4767
     * Test for the updateContentMetadata() method.
4768
     *
4769
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4770
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4771
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4772
     */
4773 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithCommit()
4774
    {
4775
        $repository = $this->getRepository();
4776
4777
        $contentId = $this->generateId('object', 12);
4778
        /* BEGIN: Use Case */
4779
        // $contentId is the ID of the "Administrator users" user group
4780
4781
        // Get the content service
4782
        $contentService = $repository->getContentService();
4783
4784
        // Load a ContentInfo object
4785
        $contentInfo = $contentService->loadContentInfo($contentId);
4786
4787
        // Store remoteId for later testing
4788
        $remoteId = $contentInfo->remoteId;
4789
4790
        // Start a transaction
4791
        $repository->beginTransaction();
4792
4793
        try {
4794
            // Get metadata update struct and change remoteId
4795
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4796
            $metadataUpdate->remoteId = md5(microtime(true));
4797
4798
            // Update the metadata of the published content object
4799
            $contentService->updateContentMetadata(
4800
                $contentInfo,
4801
                $metadataUpdate
4802
            );
4803
4804
            // Commit all changes.
4805
            $repository->commit();
4806
        } catch (Exception $e) {
4807
            // Cleanup hanging transaction on error
4808
            $repository->rollback();
4809
            throw $e;
4810
        }
4811
4812
        // Load current remoteId
4813
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4814
        /* END: Use Case */
4815
4816
        $this->assertNotEquals($remoteId, $remoteIdReloaded);
4817
    }
4818
4819
    /**
4820
     * Test for the updateContentMetadata() method, and how cache + transactions play together.
4821
     *
4822
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4823
     * @depends testUpdateContentMetadata
4824
     * @depends testLoadContentInfo
4825
     */
4826 View Code Duplication
    public function testUpdateContentMetadataCheckWithinTransaction()
4827
    {
4828
        $repository = $this->getRepository();
4829
        $contentService = $repository->getContentService();
4830
        $contentId = $this->generateId('object', 12);
4831
4832
        // Load a ContentInfo object, and warmup cache
4833
        $contentInfo = $contentService->loadContentInfo($contentId);
4834
4835
        // Store remoteId for later testing
4836
        $remoteId = $contentInfo->remoteId;
4837
4838
        // Start a transaction
4839
        $repository->beginTransaction();
4840
4841
        try {
4842
            // Get metadata update struct and change remoteId
4843
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4844
            $metadataUpdate->remoteId = md5(microtime(true));
4845
4846
            // Update the metadata of the published content object
4847
            $contentService->updateContentMetadata(
4848
                $contentInfo,
4849
                $metadataUpdate
4850
            );
4851
4852
            // Check that it's been updated
4853
            $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4854
            $this->assertNotEquals($remoteId, $remoteIdReloaded);
4855
4856
            // Commit all changes.
4857
            $repository->commit();
4858
        } catch (Exception $e) {
4859
            // Cleanup hanging transaction on error
4860
            $repository->rollback();
4861
            throw $e;
4862
        }
4863
    }
4864
4865
    /**
4866
     * Test for the deleteVersion() method.
4867
     *
4868
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4869
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4870
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4871
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4872
     */
4873 View Code Duplication
    public function testDeleteVersionInTransactionWithRollback()
4874
    {
4875
        $repository = $this->getRepository();
4876
4877
        $contentId = $this->generateId('object', 12);
4878
        /* BEGIN: Use Case */
4879
        // $contentId is the ID of the "Administrator users" user group
4880
4881
        // Get the content service
4882
        $contentService = $repository->getContentService();
4883
4884
        // Start a new transaction
4885
        $repository->beginTransaction();
4886
4887
        try {
4888
            // Create a new draft
4889
            $draft = $contentService->createContentDraft(
4890
                $contentService->loadContentInfo($contentId)
4891
            );
4892
4893
            $contentService->deleteVersion($draft->getVersionInfo());
4894
        } catch (Exception $e) {
4895
            // Cleanup hanging transaction on error
4896
            $repository->rollback();
4897
            throw $e;
4898
        }
4899
4900
        // Rollback all changes.
4901
        $repository->rollback();
4902
4903
        // This array will be empty
4904
        $drafts = $contentService->loadContentDrafts();
4905
        /* END: Use Case */
4906
4907
        $this->assertSame([], $drafts);
4908
    }
4909
4910
    /**
4911
     * Test for the deleteVersion() method.
4912
     *
4913
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4914
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4915
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4916
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4917
     */
4918 View Code Duplication
    public function testDeleteVersionInTransactionWithCommit()
4919
    {
4920
        $repository = $this->getRepository();
4921
4922
        $contentId = $this->generateId('object', 12);
4923
        /* BEGIN: Use Case */
4924
        // $contentId is the ID of the "Administrator users" user group
4925
4926
        // Get the content service
4927
        $contentService = $repository->getContentService();
4928
4929
        // Start a new transaction
4930
        $repository->beginTransaction();
4931
4932
        try {
4933
            // Create a new draft
4934
            $draft = $contentService->createContentDraft(
4935
                $contentService->loadContentInfo($contentId)
4936
            );
4937
4938
            $contentService->deleteVersion($draft->getVersionInfo());
4939
4940
            // Commit all changes.
4941
            $repository->commit();
4942
        } catch (Exception $e) {
4943
            // Cleanup hanging transaction on error
4944
            $repository->rollback();
4945
            throw $e;
4946
        }
4947
4948
        // This array will contain no element
4949
        $drafts = $contentService->loadContentDrafts();
4950
        /* END: Use Case */
4951
4952
        $this->assertSame([], $drafts);
4953
    }
4954
4955
    /**
4956
     * Test for the deleteContent() method.
4957
     *
4958
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4959
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4960
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4961
     */
4962 View Code Duplication
    public function testDeleteContentInTransactionWithRollback()
4963
    {
4964
        $repository = $this->getRepository();
4965
4966
        $contentId = $this->generateId('object', 11);
4967
        /* BEGIN: Use Case */
4968
        // $contentId is the ID of the "Members" user group in an eZ Publish
4969
        // demo installation
4970
4971
        // Get content service
4972
        $contentService = $repository->getContentService();
4973
4974
        // Load a ContentInfo instance
4975
        $contentInfo = $contentService->loadContentInfo($contentId);
4976
4977
        // Start a new transaction
4978
        $repository->beginTransaction();
4979
4980
        try {
4981
            // Delete content object
4982
            $contentService->deleteContent($contentInfo);
4983
        } catch (Exception $e) {
4984
            // Cleanup hanging transaction on error
4985
            $repository->rollback();
4986
            throw $e;
4987
        }
4988
4989
        // Rollback all changes
4990
        $repository->rollback();
4991
4992
        // This call will return the original content object
4993
        $contentInfo = $contentService->loadContentInfo($contentId);
4994
        /* END: Use Case */
4995
4996
        $this->assertEquals($contentId, $contentInfo->id);
4997
    }
4998
4999
    /**
5000
     * Test for the deleteContent() method.
5001
     *
5002
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
5003
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
5004
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
5005
     */
5006 View Code Duplication
    public function testDeleteContentInTransactionWithCommit()
5007
    {
5008
        $repository = $this->getRepository();
5009
5010
        $contentId = $this->generateId('object', 11);
5011
        /* BEGIN: Use Case */
5012
        // $contentId is the ID of the "Members" user group in an eZ Publish
5013
        // demo installation
5014
5015
        // Get content service
5016
        $contentService = $repository->getContentService();
5017
5018
        // Load a ContentInfo instance
5019
        $contentInfo = $contentService->loadContentInfo($contentId);
5020
5021
        // Start a new transaction
5022
        $repository->beginTransaction();
5023
5024
        try {
5025
            // Delete content object
5026
            $contentService->deleteContent($contentInfo);
5027
5028
            // Commit all changes
5029
            $repository->commit();
5030
        } catch (Exception $e) {
5031
            // Cleanup hanging transaction on error
5032
            $repository->rollback();
5033
            throw $e;
5034
        }
5035
5036
        // Deleted content info is not found anymore
5037
        try {
5038
            $contentService->loadContentInfo($contentId);
5039
        } catch (NotFoundException $e) {
5040
            return;
5041
        }
5042
        /* END: Use Case */
5043
5044
        $this->fail('Can still load ContentInfo after commit.');
5045
    }
5046
5047
    /**
5048
     * Test for the copyContent() method.
5049
     *
5050
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
5051
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
5052
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
5053
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
5054
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
5055
     */
5056 View Code Duplication
    public function testCopyContentInTransactionWithRollback()
5057
    {
5058
        $repository = $this->getRepository();
5059
5060
        $contentId = $this->generateId('object', 11);
5061
        $locationId = $this->generateId('location', 13);
5062
        /* BEGIN: Use Case */
5063
        // $contentId is the ID of the "Members" user group in an eZ Publish
5064
        // demo installation
5065
5066
        // $locationId is the ID of the "Administrator users" group location
5067
5068
        // Get services
5069
        $contentService = $repository->getContentService();
5070
        $locationService = $repository->getLocationService();
5071
5072
        // Load content object to copy
5073
        $content = $contentService->loadContent($contentId);
5074
5075
        // Create new target location
5076
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
5077
5078
        // Start a new transaction
5079
        $repository->beginTransaction();
5080
5081
        try {
5082
            // Copy content with all versions and drafts
5083
            $contentService->copyContent(
5084
                $content->contentInfo,
5085
                $locationCreate
5086
            );
5087
        } catch (Exception $e) {
5088
            // Cleanup hanging transaction on error
5089
            $repository->rollback();
5090
            throw $e;
5091
        }
5092
5093
        // Rollback all changes
5094
        $repository->rollback();
5095
5096
        $this->refreshSearch($repository);
5097
5098
        // This array will only contain a single admin user object
5099
        $locations = $locationService->loadLocationChildren(
5100
            $locationService->loadLocation($locationId)
5101
        )->locations;
5102
        /* END: Use Case */
5103
5104
        $this->assertEquals(1, count($locations));
5105
    }
5106
5107
    /**
5108
     * Test for the copyContent() method.
5109
     *
5110
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
5111
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
5112
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
5113
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
5114
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
5115
     */
5116 View Code Duplication
    public function testCopyContentInTransactionWithCommit()
5117
    {
5118
        $repository = $this->getRepository();
5119
5120
        $contentId = $this->generateId('object', 11);
5121
        $locationId = $this->generateId('location', 13);
5122
        /* BEGIN: Use Case */
5123
        // $contentId is the ID of the "Members" user group in an eZ Publish
5124
        // demo installation
5125
5126
        // $locationId is the ID of the "Administrator users" group location
5127
5128
        // Get services
5129
        $contentService = $repository->getContentService();
5130
        $locationService = $repository->getLocationService();
5131
5132
        // Load content object to copy
5133
        $content = $contentService->loadContent($contentId);
5134
5135
        // Create new target location
5136
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
5137
5138
        // Start a new transaction
5139
        $repository->beginTransaction();
5140
5141
        try {
5142
            // Copy content with all versions and drafts
5143
            $contentCopied = $contentService->copyContent(
0 ignored issues
show
Unused Code introduced by
$contentCopied is not used, you could remove the assignment.

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

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

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

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

Loading history...
5144
                $content->contentInfo,
5145
                $locationCreate
5146
            );
5147
5148
            // Commit all changes
5149
            $repository->commit();
5150
        } catch (Exception $e) {
5151
            // Cleanup hanging transaction on error
5152
            $repository->rollback();
5153
            throw $e;
5154
        }
5155
5156
        $this->refreshSearch($repository);
5157
5158
        // This will contain the admin user and the new child location
5159
        $locations = $locationService->loadLocationChildren(
5160
            $locationService->loadLocation($locationId)
5161
        )->locations;
5162
        /* END: Use Case */
5163
5164
        $this->assertEquals(2, count($locations));
5165
    }
5166
5167
    public function testURLAliasesCreatedForNewContent()
5168
    {
5169
        $repository = $this->getRepository();
5170
5171
        $contentService = $repository->getContentService();
5172
        $locationService = $repository->getLocationService();
5173
        $urlAliasService = $repository->getURLAliasService();
5174
5175
        /* BEGIN: Use Case */
5176
        $draft = $this->createContentDraftVersion1();
5177
5178
        // Automatically creates a new URLAlias for the content
5179
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
5180
        /* END: Use Case */
5181
5182
        $location = $locationService->loadLocation(
5183
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
5184
        );
5185
5186
        $aliases = $urlAliasService->listLocationAliases($location, false);
5187
5188
        $this->assertAliasesCorrect(
5189
            [
5190
                '/Design/Plain-site/An-awesome-forum' => [
5191
                    'type' => URLAlias::LOCATION,
5192
                    'destination' => $location->id,
5193
                    'path' => '/Design/Plain-site/An-awesome-forum',
5194
                    'languageCodes' => ['eng-US'],
5195
                    'isHistory' => false,
5196
                    'isCustom' => false,
5197
                    'forward' => false,
5198
                ],
5199
            ],
5200
            $aliases
5201
        );
5202
    }
5203
5204
    public function testURLAliasesCreatedForUpdatedContent()
5205
    {
5206
        $repository = $this->getRepository();
5207
5208
        $contentService = $repository->getContentService();
5209
        $locationService = $repository->getLocationService();
5210
        $urlAliasService = $repository->getURLAliasService();
5211
5212
        /* BEGIN: Use Case */
5213
        $draft = $this->createUpdatedDraftVersion2();
5214
5215
        $location = $locationService->loadLocation(
5216
            $draft->getVersionInfo()->getContentInfo()->mainLocationId
5217
        );
5218
5219
        // Load and assert URL aliases before publishing updated Content, so that
5220
        // SPI cache is warmed up and cache invalidation is also tested.
5221
        $aliases = $urlAliasService->listLocationAliases($location, false);
5222
5223
        $this->assertAliasesCorrect(
5224
            [
5225
                '/Design/Plain-site/An-awesome-forum' => [
5226
                    'type' => URLAlias::LOCATION,
5227
                    'destination' => $location->id,
5228
                    'path' => '/Design/Plain-site/An-awesome-forum',
5229
                    'languageCodes' => ['eng-US'],
5230
                    'alwaysAvailable' => true,
5231
                    'isHistory' => false,
5232
                    'isCustom' => false,
5233
                    'forward' => false,
5234
                ],
5235
            ],
5236
            $aliases
5237
        );
5238
5239
        // Automatically marks old aliases for the content as history
5240
        // and creates new aliases, based on the changes
5241
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
5242
        /* END: Use Case */
5243
5244
        $location = $locationService->loadLocation(
5245
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
5246
        );
5247
5248
        $aliases = $urlAliasService->listLocationAliases($location, false);
5249
5250
        $this->assertAliasesCorrect(
5251
            [
5252
                '/Design/Plain-site/An-awesome-forum2' => [
5253
                    'type' => URLAlias::LOCATION,
5254
                    'destination' => $location->id,
5255
                    'path' => '/Design/Plain-site/An-awesome-forum2',
5256
                    'languageCodes' => ['eng-US'],
5257
                    'alwaysAvailable' => true,
5258
                    'isHistory' => false,
5259
                    'isCustom' => false,
5260
                    'forward' => false,
5261
                ],
5262
                '/Design/Plain-site/An-awesome-forum23' => [
5263
                    'type' => URLAlias::LOCATION,
5264
                    'destination' => $location->id,
5265
                    'path' => '/Design/Plain-site/An-awesome-forum23',
5266
                    'languageCodes' => ['eng-GB'],
5267
                    'alwaysAvailable' => true,
5268
                    'isHistory' => false,
5269
                    'isCustom' => false,
5270
                    'forward' => false,
5271
                ],
5272
            ],
5273
            $aliases
5274
        );
5275
    }
5276
5277
    public function testCustomURLAliasesNotHistorizedOnUpdatedContent()
5278
    {
5279
        $repository = $this->getRepository();
5280
5281
        $contentService = $repository->getContentService();
5282
5283
        /* BEGIN: Use Case */
5284
        $urlAliasService = $repository->getURLAliasService();
5285
        $locationService = $repository->getLocationService();
5286
5287
        $content = $this->createContentVersion1();
5288
5289
        // Create a custom URL alias
5290
        $urlAliasService->createUrlAlias(
5291
            $locationService->loadLocation(
5292
                $content->getVersionInfo()->getContentInfo()->mainLocationId
5293
            ),
5294
            '/my/fancy/story-about-ez-publish',
5295
            'eng-US'
5296
        );
5297
5298
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
5299
5300
        $contentUpdate = $contentService->newContentUpdateStruct();
5301
        $contentUpdate->initialLanguageCode = 'eng-US';
5302
        $contentUpdate->setField('name', 'Amazing Bielefeld forum');
5303
5304
        $draftVersion2 = $contentService->updateContent(
5305
            $draftVersion2->getVersionInfo(),
5306
            $contentUpdate
5307
        );
5308
5309
        // Only marks auto-generated aliases as history
5310
        // the custom one is left untouched
5311
        $liveContent = $contentService->publishVersion($draftVersion2->getVersionInfo());
5312
        /* END: Use Case */
5313
5314
        $location = $locationService->loadLocation(
5315
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
5316
        );
5317
5318
        $aliases = $urlAliasService->listLocationAliases($location);
5319
5320
        $this->assertAliasesCorrect(
5321
            [
5322
                '/my/fancy/story-about-ez-publish' => [
5323
                    'type' => URLAlias::LOCATION,
5324
                    'destination' => $location->id,
5325
                    'path' => '/my/fancy/story-about-ez-publish',
5326
                    'languageCodes' => ['eng-US'],
5327
                    'isHistory' => false,
5328
                    'isCustom' => true,
5329
                    'forward' => false,
5330
                    'alwaysAvailable' => false,
5331
                ],
5332
            ],
5333
            $aliases
5334
        );
5335
    }
5336
5337
    /**
5338
     * Test to ensure that old versions are not affected by updates to newer
5339
     * drafts.
5340
     */
5341
    public function testUpdatingDraftDoesNotUpdateOldVersions()
5342
    {
5343
        $repository = $this->getRepository();
5344
5345
        $contentService = $repository->getContentService();
5346
5347
        $contentVersion2 = $this->createContentVersion2();
5348
5349
        $loadedContent1 = $contentService->loadContent($contentVersion2->id, null, 1);
5350
        $loadedContent2 = $contentService->loadContent($contentVersion2->id, null, 2);
5351
5352
        $this->assertNotEquals(
5353
            $loadedContent1->getFieldValue('name', 'eng-US'),
5354
            $loadedContent2->getFieldValue('name', 'eng-US')
5355
        );
5356
    }
5357
5358
    /**
5359
     * Test scenario with writer and publisher users.
5360
     * Writer can only create content. Publisher can publish this content.
5361
     */
5362
    public function testPublishWorkflow()
5363
    {
5364
        $repository = $this->getRepository();
5365
        $contentService = $repository->getContentService();
5366
5367
        $this->createRoleWithPolicies('Publisher', [
5368
            ['module' => 'content', 'function' => 'read'],
5369
            ['module' => 'content', 'function' => 'create'],
5370
            ['module' => 'content', 'function' => 'publish'],
5371
        ]);
5372
5373
        $this->createRoleWithPolicies('Writer', [
5374
            ['module' => 'content', 'function' => 'read'],
5375
            ['module' => 'content', 'function' => 'create'],
5376
        ]);
5377
5378
        $writerUser = $this->createCustomUserWithLogin(
5379
            'writer',
5380
            '[email protected]',
5381
            'Writers',
5382
            'Writer'
5383
        );
5384
5385
        $publisherUser = $this->createCustomUserWithLogin(
5386
            'publisher',
5387
            '[email protected]',
5388
            'Publishers',
5389
            'Publisher'
5390
        );
5391
5392
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5393
        $draft = $this->createContentDraftVersion1();
5394
5395
        $repository->getPermissionResolver()->setCurrentUserReference($publisherUser);
5396
        $content = $contentService->publishVersion($draft->versionInfo);
5397
5398
        $contentService->loadContent($content->id);
5399
    }
5400
5401
    /**
5402
     * Test publish / content policy is required to be able to publish content.
5403
     *
5404
     * @expectedException \eZ\Publish\Core\Base\Exceptions\UnauthorizedException
5405
     * @expectedExceptionMessageRegExp /User does not have access to 'publish' 'content'/
5406
     */
5407
    public function testPublishContentWithoutPublishPolicyThrowsException()
5408
    {
5409
        $repository = $this->getRepository();
5410
5411
        $this->createRoleWithPolicies('Writer', [
5412
            ['module' => 'content', 'function' => 'read'],
5413
            ['module' => 'content', 'function' => 'create'],
5414
            ['module' => 'content', 'function' => 'edit'],
5415
        ]);
5416
        $writerUser = $this->createCustomUserWithLogin(
5417
            'writer',
5418
            '[email protected]',
5419
            'Writers',
5420
            'Writer'
5421
        );
5422
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5423
5424
        $this->createContentVersion1();
5425
    }
5426
5427
    /**
5428
     * Test removal of the specific translation from all the Versions of a Content Object.
5429
     *
5430
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5431
     */
5432 View Code Duplication
    public function testDeleteTranslation()
5433
    {
5434
        $repository = $this->getRepository();
5435
        $contentService = $repository->getContentService();
5436
        $content = $this->createContentVersion2();
5437
5438
        // create multiple versions to exceed archive limit
5439
        for ($i = 0; $i < 5; ++$i) {
5440
            $contentDraft = $contentService->createContentDraft($content->contentInfo);
5441
            $contentUpdateStruct = $contentService->newContentUpdateStruct();
5442
            $contentDraft = $contentService->updateContent(
5443
                $contentDraft->versionInfo,
5444
                $contentUpdateStruct
5445
            );
5446
            $contentService->publishVersion($contentDraft->versionInfo);
5447
        }
5448
5449
        $contentService->deleteTranslation($content->contentInfo, 'eng-GB');
5450
5451
        $this->assertTranslationDoesNotExist('eng-GB', $content->id);
5452
    }
5453
5454
    /**
5455
     * Test deleting a Translation which is initial for some Version, updates initialLanguageCode
5456
     * with mainLanguageCode (assuming they are different).
5457
     */
5458
    public function testDeleteTranslationUpdatesInitialLanguageCodeVersion()
5459
    {
5460
        $repository = $this->getRepository();
5461
        $contentService = $repository->getContentService();
5462
5463
        $content = $this->createContentVersion2();
5464
        // create another, copied, version
5465
        $contentDraft = $contentService->updateContent(
5466
            $contentService->createContentDraft($content->contentInfo)->versionInfo,
5467
            $contentService->newContentUpdateStruct()
5468
        );
5469
        $publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
5470
5471
        // remove first version with only one translation as it is not the subject of this test
5472
        $contentService->deleteVersion(
5473
            $contentService->loadVersionInfo($publishedContent->contentInfo, 1)
5474
        );
5475
5476
        // sanity check
5477
        self::assertEquals('eng-US', $content->contentInfo->mainLanguageCode);
5478
        self::assertEquals('eng-US', $content->versionInfo->initialLanguageCode);
5479
5480
        // update mainLanguageCode so it is different than initialLanguageCode for Version
5481
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
5482
        $contentMetadataUpdateStruct->mainLanguageCode = 'eng-GB';
5483
        $content = $contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
5484
5485
        $contentService->deleteTranslation($content->contentInfo, 'eng-US');
5486
5487
        $this->assertTranslationDoesNotExist('eng-US', $content->id);
5488
    }
5489
5490
    /**
5491
     * Test removal of the specific translation properly updates languages of the URL alias.
5492
     *
5493
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5494
     */
5495
    public function testDeleteTranslationUpdatesUrlAlias()
5496
    {
5497
        $repository = $this->getRepository();
5498
        $contentService = $repository->getContentService();
5499
        $locationService = $repository->getLocationService();
5500
        $urlAliasService = $repository->getURLAliasService();
5501
5502
        $content = $this->createContentVersion2();
5503
        $mainLocation = $locationService->loadLocation($content->contentInfo->mainLocationId);
5504
5505
        // create custom URL alias for Content main Location
5506
        $urlAliasService->createUrlAlias($mainLocation, '/my-custom-url', 'eng-GB');
5507
5508
        // create secondary Location for Content
5509
        $secondaryLocation = $locationService->createLocation(
5510
            $content->contentInfo,
5511
            $locationService->newLocationCreateStruct(2)
5512
        );
5513
5514
        // create custom URL alias for Content secondary Location
5515
        $urlAliasService->createUrlAlias($secondaryLocation, '/my-secondary-url', 'eng-GB');
5516
5517
        // delete Translation
5518
        $contentService->deleteTranslation($content->contentInfo, 'eng-GB');
5519
5520
        foreach ([$mainLocation, $secondaryLocation] as $location) {
5521
            // check auto-generated URL aliases
5522
            foreach ($urlAliasService->listLocationAliases($location, false) as $alias) {
5523
                self::assertNotContains('eng-GB', $alias->languageCodes);
5524
            }
5525
5526
            // check custom URL aliases
5527
            foreach ($urlAliasService->listLocationAliases($location) as $alias) {
5528
                self::assertNotContains('eng-GB', $alias->languageCodes);
5529
            }
5530
        }
5531
    }
5532
5533
    /**
5534
     * Test removal of a main translation throws BadStateException.
5535
     *
5536
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5537
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5538
     * @expectedExceptionMessage Specified translation is the main translation of the Content Object
5539
     */
5540
    public function testDeleteTranslationMainLanguageThrowsBadStateException()
5541
    {
5542
        $repository = $this->getRepository();
5543
        $contentService = $repository->getContentService();
5544
        $content = $this->createContentVersion2();
5545
5546
        // delete first version which has only one translation
5547
        $contentService->deleteVersion($contentService->loadVersionInfo($content->contentInfo, 1));
5548
5549
        // try to delete main translation
5550
        $contentService->deleteTranslation($content->contentInfo, $content->contentInfo->mainLanguageCode);
5551
    }
5552
5553
    /**
5554
     * Test removal of a Translation is possible when some archived Versions have only this Translation.
5555
     *
5556
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5557
     */
5558
    public function testDeleteTranslationDeletesSingleTranslationVersions()
5559
    {
5560
        $repository = $this->getRepository();
5561
        $contentService = $repository->getContentService();
5562
        // content created by the createContentVersion1 method has eng-US translation only.
5563
        $content = $this->createContentVersion1();
5564
5565
        // create new version and add eng-GB translation
5566
        $contentDraft = $contentService->createContentDraft($content->contentInfo);
5567
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
5568
        $contentUpdateStruct->setField('name', 'Awesome Board', 'eng-GB');
5569
        $contentDraft = $contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
5570
        $publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
5571
5572
        // update mainLanguageCode to avoid exception related to that
5573
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
5574
        $contentMetadataUpdateStruct->mainLanguageCode = 'eng-GB';
5575
5576
        $content = $contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
5577
5578
        $contentService->deleteTranslation($content->contentInfo, 'eng-US');
5579
5580
        $this->assertTranslationDoesNotExist('eng-US', $content->id);
5581
    }
5582
5583
    /**
5584
     * Test removal of the translation by the user who is not allowed to delete a content
5585
     * throws UnauthorizedException.
5586
     *
5587
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5588
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5589
     * @expectedExceptionMessage User does not have access to 'remove' 'content'
5590
     */
5591
    public function testDeleteTranslationThrowsUnauthorizedException()
5592
    {
5593
        $repository = $this->getRepository();
5594
        $contentService = $repository->getContentService();
5595
5596
        $content = $this->createContentVersion2();
5597
5598
        // create user that can read/create/edit but cannot delete content
5599
        $this->createRoleWithPolicies('Writer', [
5600
            ['module' => 'content', 'function' => 'read'],
5601
            ['module' => 'content', 'function' => 'versionread'],
5602
            ['module' => 'content', 'function' => 'create'],
5603
            ['module' => 'content', 'function' => 'edit'],
5604
        ]);
5605
        $writerUser = $this->createCustomUserWithLogin(
5606
            'writer',
5607
            '[email protected]',
5608
            'Writers',
5609
            'Writer'
5610
        );
5611
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5612
        $contentService->deleteTranslation($content->contentInfo, 'eng-GB');
5613
    }
5614
5615
    /**
5616
     * Test removal of a non-existent translation throws InvalidArgumentException.
5617
     *
5618
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5619
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
5620
     * @expectedExceptionMessage Argument '$languageCode' is invalid: ger-DE does not exist in the Content item
5621
     */
5622
    public function testDeleteTranslationThrowsInvalidArgumentException()
5623
    {
5624
        $repository = $this->getRepository();
5625
        $contentService = $repository->getContentService();
5626
        // content created by the createContentVersion1 method has eng-US translation only.
5627
        $content = $this->createContentVersion1();
5628
        $contentService->deleteTranslation($content->contentInfo, 'ger-DE');
5629
    }
5630
5631
    /**
5632
     * Test deleting a Translation from Draft.
5633
     *
5634
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5635
     */
5636
    public function testDeleteTranslationFromDraft()
5637
    {
5638
        $repository = $this->getRepository();
5639
        $contentService = $repository->getContentService();
5640
5641
        $languageCode = 'eng-GB';
5642
        $content = $this->createMultipleLanguageContentVersion2();
5643
        $draft = $contentService->createContentDraft($content->contentInfo);
5644
        $draft = $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5645
        $content = $contentService->publishVersion($draft->versionInfo);
5646
5647
        $loadedContent = $contentService->loadContent($content->id);
5648
        self::assertNotContains($languageCode, $loadedContent->versionInfo->languageCodes);
5649
        self::assertEmpty($loadedContent->getFieldsByLanguage($languageCode));
5650
    }
5651
5652
    /**
5653
     * Get values for multilingual field.
5654
     *
5655
     * @return array
5656
     */
5657
    public function providerForDeleteTranslationFromDraftRemovesUrlAliasOnPublishing()
5658
    {
5659
        return [
5660
            [
5661
                ['eng-US' => 'US Name', 'eng-GB' => 'GB Name'],
5662
            ],
5663
            [
5664
                ['eng-US' => 'Same Name', 'eng-GB' => 'Same Name'],
5665
            ],
5666
        ];
5667
    }
5668
5669
    /**
5670
     * Test deleting a Translation from Draft removes previously stored URL aliases for published Content.
5671
     *
5672
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5673
     *
5674
     * @dataProvider providerForDeleteTranslationFromDraftRemovesUrlAliasOnPublishing
5675
     *
5676
     * @param string[] $fieldValues translated field values
5677
     *
5678
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
5679
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
5680
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
5681
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5682
     */
5683
    public function testDeleteTranslationFromDraftRemovesUrlAliasOnPublishing(array $fieldValues)
5684
    {
5685
        $repository = $this->getRepository();
5686
        $contentService = $repository->getContentService();
5687
        $locationService = $repository->getLocationService();
5688
        $urlAliasService = $repository->getURLAliasService();
5689
5690
        // set language code to be removed
5691
        $languageCode = 'eng-GB';
5692
        $draft = $this->createMultilingualContentDraft(
5693
            'folder',
5694
            2,
5695
            'eng-US',
5696
            [
5697
                'name' => [
5698
                    'eng-GB' => $fieldValues['eng-GB'],
5699
                    'eng-US' => $fieldValues['eng-US'],
5700
                ],
5701
            ]
5702
        );
5703
        $content = $contentService->publishVersion($draft->versionInfo);
5704
5705
        // create secondary location
5706
        $locationService->createLocation(
5707
            $content->contentInfo,
5708
            $locationService->newLocationCreateStruct(5)
5709
        );
5710
5711
        // sanity check
5712
        $locations = $locationService->loadLocations($content->contentInfo);
5713
        self::assertCount(2, $locations, 'Sanity check: Expected to find 2 Locations');
5714
        foreach ($locations as $location) {
5715
            $urlAliasService->createUrlAlias($location, '/us-custom_' . $location->id, 'eng-US');
5716
            $urlAliasService->createUrlAlias($location, '/gb-custom_' . $location->id, 'eng-GB');
5717
5718
            // check default URL aliases
5719
            $aliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
5720
            self::assertNotEmpty($aliases, 'Sanity check: URL alias for the translation does not exist');
5721
5722
            // check custom URL aliases
5723
            $aliases = $urlAliasService->listLocationAliases($location, true, $languageCode);
5724
            self::assertNotEmpty($aliases, 'Sanity check: Custom URL alias for the translation does not exist');
5725
        }
5726
5727
        // delete translation and publish new version
5728
        $draft = $contentService->createContentDraft($content->contentInfo);
5729
        $draft = $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5730
        $contentService->publishVersion($draft->versionInfo);
5731
5732
        // check that aliases does not exist
5733
        foreach ($locations as $location) {
5734
            // check default URL aliases
5735
            $aliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
5736
            self::assertEmpty($aliases, 'URL alias for the deleted translation still exists');
5737
5738
            // check custom URL aliases
5739
            $aliases = $urlAliasService->listLocationAliases($location, true, $languageCode);
5740
            self::assertEmpty($aliases, 'Custom URL alias for the deleted translation still exists');
5741
        }
5742
    }
5743
5744
    /**
5745
     * Test that URL aliases for deleted Translations are properly archived.
5746
     */
5747
    public function testDeleteTranslationFromDraftArchivesUrlAliasOnPublishing()
5748
    {
5749
        $repository = $this->getRepository();
5750
        $contentService = $repository->getContentService();
5751
        $urlAliasService = $repository->getURLAliasService();
5752
5753
        $content = $contentService->publishVersion(
5754
            $this->createMultilingualContentDraft(
5755
                'folder',
5756
                2,
5757
                'eng-US',
5758
                [
5759
                    'name' => [
5760
                        'eng-GB' => 'BritishEnglishContent',
5761
                        'eng-US' => 'AmericanEnglishContent',
5762
                    ],
5763
                ]
5764
            )->versionInfo
5765
        );
5766
5767
        $unrelatedContent = $contentService->publishVersion(
5768
            $this->createMultilingualContentDraft(
5769
                'folder',
5770
                2,
5771
                'eng-US',
5772
                [
5773
                    'name' => [
5774
                        'eng-GB' => 'AnotherBritishContent',
5775
                        'eng-US' => 'AnotherAmericanContent',
5776
                    ],
5777
                ]
5778
            )->versionInfo
5779
        );
5780
5781
        $urlAlias = $urlAliasService->lookup('/BritishEnglishContent');
5782
        self::assertFalse($urlAlias->isHistory);
5783
        self::assertEquals($urlAlias->path, '/BritishEnglishContent');
5784
        self::assertEquals($urlAlias->destination, $content->contentInfo->mainLocationId);
5785
5786
        $draft = $contentService->deleteTranslationFromDraft(
5787
            $contentService->createContentDraft($content->contentInfo)->versionInfo,
5788
            'eng-GB'
5789
        );
5790
        $content = $contentService->publishVersion($draft->versionInfo);
5791
5792
        $urlAlias = $urlAliasService->lookup('/BritishEnglishContent');
5793
        self::assertTrue($urlAlias->isHistory);
5794
        self::assertEquals($urlAlias->path, '/BritishEnglishContent');
5795
        self::assertEquals($urlAlias->destination, $content->contentInfo->mainLocationId);
5796
5797
        $unrelatedUrlAlias = $urlAliasService->lookup('/AnotherBritishContent');
5798
        self::assertFalse($unrelatedUrlAlias->isHistory);
5799
        self::assertEquals($unrelatedUrlAlias->path, '/AnotherBritishContent');
5800
        self::assertEquals($unrelatedUrlAlias->destination, $unrelatedContent->contentInfo->mainLocationId);
5801
    }
5802
5803
    /**
5804
     * Test deleting a Translation from Draft which has single Translation throws BadStateException.
5805
     *
5806
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5807
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5808
     * @expectedExceptionMessage Specified Translation is the only one Content Object Version has
5809
     */
5810
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnSingleTranslation()
5811
    {
5812
        $repository = $this->getRepository();
5813
        $contentService = $repository->getContentService();
5814
5815
        // create Content with single Translation
5816
        $publishedContent = $contentService->publishVersion(
5817
            $this->createContentDraft(
5818
                'forum',
5819
                2,
5820
                ['name' => 'Eng-US Version name']
5821
            )->versionInfo
5822
        );
5823
5824
        // update mainLanguageCode to avoid exception related to trying to delete main Translation
5825
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
5826
        $contentMetadataUpdateStruct->mainLanguageCode = 'eng-GB';
5827
        $publishedContent = $contentService->updateContentMetadata(
5828
            $publishedContent->contentInfo,
5829
            $contentMetadataUpdateStruct
5830
        );
5831
5832
        // create single Translation Version from the first one
5833
        $draft = $contentService->createContentDraft(
5834
            $publishedContent->contentInfo,
5835
            $publishedContent->versionInfo
5836
        );
5837
5838
        // attempt to delete Translation
5839
        $contentService->deleteTranslationFromDraft($draft->versionInfo, 'eng-US');
5840
    }
5841
5842
    /**
5843
     * Test deleting the Main Translation from Draft throws BadStateException.
5844
     *
5845
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5846
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5847
     * @expectedExceptionMessage Specified Translation is the main Translation of the Content Object
5848
     */
5849
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnMainTranslation()
5850
    {
5851
        $repository = $this->getRepository();
5852
        $contentService = $repository->getContentService();
5853
5854
        $mainLanguageCode = 'eng-US';
5855
        $draft = $this->createMultilingualContentDraft(
5856
            'forum',
5857
            2,
5858
            $mainLanguageCode,
5859
            [
5860
                'name' => [
5861
                    'eng-US' => 'An awesome eng-US forum',
5862
                    'eng-GB' => 'An awesome eng-GB forum',
5863
                ],
5864
            ]
5865
        );
5866
        $contentService->deleteTranslationFromDraft($draft->versionInfo, $mainLanguageCode);
5867
    }
5868
5869
    /**
5870
     * Test deleting the Translation from Published Version throws BadStateException.
5871
     *
5872
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5873
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5874
     * @expectedExceptionMessage Version is not a draft
5875
     */
5876 View Code Duplication
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnPublishedVersion()
5877
    {
5878
        $repository = $this->getRepository();
5879
        $contentService = $repository->getContentService();
5880
5881
        $languageCode = 'eng-US';
5882
        $content = $this->createMultipleLanguageContentVersion2();
5883
        $draft = $contentService->createContentDraft($content->contentInfo);
5884
        $publishedContent = $contentService->publishVersion($draft->versionInfo);
5885
        $contentService->deleteTranslationFromDraft($publishedContent->versionInfo, $languageCode);
5886
    }
5887
5888
    /**
5889
     * Test deleting a Translation from Draft throws UnauthorizedException if user cannot edit Content.
5890
     *
5891
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5892
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5893
     * @expectedExceptionMessage User does not have access to 'edit' 'content'
5894
     */
5895
    public function testDeleteTranslationFromDraftThrowsUnauthorizedException()
5896
    {
5897
        $repository = $this->getRepository();
5898
        $contentService = $repository->getContentService();
5899
5900
        $languageCode = 'eng-GB';
5901
        $content = $this->createMultipleLanguageContentVersion2();
5902
        $draft = $contentService->createContentDraft($content->contentInfo);
5903
5904
        // create user that can read/create/delete but cannot edit or content
5905
        $this->createRoleWithPolicies('Writer', [
5906
            ['module' => 'content', 'function' => 'read'],
5907
            ['module' => 'content', 'function' => 'versionread'],
5908
            ['module' => 'content', 'function' => 'create'],
5909
            ['module' => 'content', 'function' => 'delete'],
5910
        ]);
5911
        $writerUser = $this->createCustomUserWithLogin(
5912
            'user',
5913
            '[email protected]',
5914
            'Writers',
5915
            'Writer'
5916
        );
5917
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5918
5919
        $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5920
    }
5921
5922
    /**
5923
     * Test deleting a non-existent Translation from Draft throws InvalidArgumentException.
5924
     *
5925
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5926
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
5927
     * @expectedExceptionMessageRegExp /The Version \(ContentId=\d+, VersionNo=\d+\) is not translated into ger-DE/
5928
     */
5929
    public function testDeleteTranslationFromDraftThrowsInvalidArgumentException()
5930
    {
5931
        $repository = $this->getRepository();
5932
        $contentService = $repository->getContentService();
5933
5934
        $languageCode = 'ger-DE';
5935
        $content = $this->createMultipleLanguageContentVersion2();
5936
        $draft = $contentService->createContentDraft($content->contentInfo);
5937
        $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5938
    }
5939
5940
    /**
5941
     * Test loading list of Content items.
5942
     */
5943
    public function testLoadContentListByContentInfo()
5944
    {
5945
        $repository = $this->getRepository();
5946
        $contentService = $repository->getContentService();
5947
        $locationService = $repository->getLocationService();
5948
5949
        $allLocationsCount = $locationService->getAllLocationsCount();
5950
        $contentInfoList = array_map(
5951
            function (Location $location) {
5952
                return $location->contentInfo;
5953
            },
5954
            $locationService->loadAllLocations(0, $allLocationsCount)
5955
        );
5956
5957
        $contentList = $contentService->loadContentListByContentInfo($contentInfoList);
5958
        self::assertCount(count($contentInfoList), $contentList);
5959
        foreach ($contentList as $content) {
5960
            try {
5961
                $loadedContent = $contentService->loadContent($content->id);
5962
                self::assertEquals($loadedContent, $content, "Failed to properly bulk-load Content {$content->id}");
5963
            } catch (NotFoundException $e) {
5964
                self::fail("Failed to load Content {$content->id}: {$e->getMessage()}");
5965
            } catch (UnauthorizedException $e) {
5966
                self::fail("Failed to load Content {$content->id}: {$e->getMessage()}");
5967
            }
5968
        }
5969
    }
5970
5971
    /**
5972
     * Test loading content versions after removing exactly two drafts.
5973
     *
5974
     * @see https://jira.ez.no/browse/EZP-30271
5975
     *
5976
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteVersion
5977
     */
5978
    public function testLoadVersionsAfterDeletingTwoDrafts()
5979
    {
5980
        $repository = $this->getRepository();
5981
        $contentService = $repository->getContentService();
5982
5983
        $content = $this->createFolder(['eng-GB' => 'Foo'], 2);
5984
5985
        // First update and publish
5986
        $modifiedContent = $this->updateFolder($content, ['eng-GB' => 'Foo1']);
5987
        $content = $contentService->publishVersion($modifiedContent->versionInfo);
5988
5989
        // Second update and publish
5990
        $modifiedContent = $this->updateFolder($content, ['eng-GB' => 'Foo2']);
5991
        $content = $contentService->publishVersion($modifiedContent->versionInfo);
5992
5993
        // Create drafts
5994
        $this->updateFolder($content, ['eng-GB' => 'Foo3']);
5995
        $this->updateFolder($content, ['eng-GB' => 'Foo4']);
5996
5997
        $versions = $contentService->loadVersions($content->contentInfo);
5998
5999
        foreach ($versions as $key => $version) {
6000
            if ($version->isDraft()) {
6001
                $contentService->deleteVersion($version);
6002
                unset($versions[$key]);
6003
            }
6004
        }
6005
6006
        $this->assertEquals($versions, $contentService->loadVersions($content->contentInfo));
6007
    }
6008
6009
    /**
6010
     * Tests loading list of content versions of status draft.
6011
     */
6012
    public function testLoadVersionsOfStatusDraft()
6013
    {
6014
        $repository = $this->getRepository();
6015
6016
        $contentService = $repository->getContentService();
6017
6018
        $content = $this->createContentVersion1();
6019
6020
        $contentService->createContentDraft($content->contentInfo);
6021
        $contentService->createContentDraft($content->contentInfo);
6022
        $contentService->createContentDraft($content->contentInfo);
6023
6024
        $versions = $contentService->loadVersions($content->contentInfo, VersionInfo::STATUS_DRAFT);
6025
6026
        $this->assertSame(\count($versions), 3);
6027
    }
6028
6029
    /**
6030
     * Tests loading list of content versions of status archived.
6031
     */
6032
    public function testLoadVersionsOfStatusArchived()
6033
    {
6034
        $repository = $this->getRepository();
6035
6036
        $contentService = $repository->getContentService();
6037
6038
        $content = $this->createContentVersion1();
6039
6040
        $draft1 = $contentService->createContentDraft($content->contentInfo);
6041
        $contentService->publishVersion($draft1->versionInfo);
6042
6043
        $draft2 = $contentService->createContentDraft($content->contentInfo);
6044
        $contentService->publishVersion($draft2->versionInfo);
6045
6046
        $versions = $contentService->loadVersions($content->contentInfo, VersionInfo::STATUS_ARCHIVED);
6047
6048
        $this->assertSame(\count($versions), 2);
6049
    }
6050
6051
    /**
6052
     * Asserts that all aliases defined in $expectedAliasProperties with the
6053
     * given properties are available in $actualAliases and not more.
6054
     *
6055
     * @param array $expectedAliasProperties
6056
     * @param array $actualAliases
6057
     */
6058
    private function assertAliasesCorrect(array $expectedAliasProperties, array $actualAliases)
6059
    {
6060
        foreach ($actualAliases as $actualAlias) {
6061
            if (!isset($expectedAliasProperties[$actualAlias->path])) {
6062
                $this->fail(
6063
                    sprintf(
6064
                        'Alias with path "%s" in languages "%s" not expected.',
6065
                        $actualAlias->path,
6066
                        implode(', ', $actualAlias->languageCodes)
6067
                    )
6068
                );
6069
            }
6070
6071
            foreach ($expectedAliasProperties[$actualAlias->path] as $propertyName => $propertyValue) {
6072
                $this->assertEquals(
6073
                    $propertyValue,
6074
                    $actualAlias->$propertyName,
6075
                    sprintf(
6076
                        'Property $%s incorrect on alias with path "%s" in languages "%s".',
6077
                        $propertyName,
6078
                        $actualAlias->path,
6079
                        implode(', ', $actualAlias->languageCodes)
6080
                    )
6081
                );
6082
            }
6083
6084
            unset($expectedAliasProperties[$actualAlias->path]);
6085
        }
6086
6087
        if (!empty($expectedAliasProperties)) {
6088
            $this->fail(
6089
                sprintf(
6090
                    'Missing expected aliases with paths "%s".',
6091
                    implode('", "', array_keys($expectedAliasProperties))
6092
                )
6093
            );
6094
        }
6095
    }
6096
6097
    /**
6098
     * Asserts that the given fields are equal to the default fields fixture.
6099
     *
6100
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
6101
     */
6102
    private function assertAllFieldsEquals(array $fields)
6103
    {
6104
        $actual = $this->normalizeFields($fields);
6105
        $expected = $this->normalizeFields($this->createFieldsFixture());
6106
6107
        $this->assertEquals($expected, $actual);
6108
    }
6109
6110
    /**
6111
     * Asserts that the given fields are equal to a language filtered set of the
6112
     * default fields fixture.
6113
     *
6114
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
6115
     * @param string $languageCode
6116
     */
6117
    private function assertLocaleFieldsEquals(array $fields, $languageCode)
6118
    {
6119
        $actual = $this->normalizeFields($fields);
6120
6121
        $expected = [];
6122
        foreach ($this->normalizeFields($this->createFieldsFixture()) as $field) {
6123
            if ($field->languageCode !== $languageCode) {
6124
                continue;
6125
            }
6126
            $expected[] = $field;
6127
        }
6128
6129
        $this->assertEquals($expected, $actual);
6130
    }
6131
6132
    /**
6133
     * This method normalizes a set of fields and returns a normalized set.
6134
     *
6135
     * Normalization means it resets the storage specific field id to zero and
6136
     * it sorts the field by their identifier and their language code. In
6137
     * addition, the field value is removed, since this one depends on the
6138
     * specific FieldType, which is tested in a dedicated integration test.
6139
     *
6140
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
6141
     *
6142
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
6143
     */
6144
    private function normalizeFields(array $fields)
6145
    {
6146
        $normalized = [];
6147 View Code Duplication
        foreach ($fields as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
6148
            $normalized[] = new Field(
6149
                [
6150
                    'id' => 0,
6151
                    'value' => ($field->value !== null ? true : null),
6152
                    'languageCode' => $field->languageCode,
6153
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
6154
                    'fieldTypeIdentifier' => $field->fieldTypeIdentifier,
6155
                ]
6156
            );
6157
        }
6158
        usort(
6159
            $normalized,
6160 View Code Duplication
            function ($field1, $field2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
6161
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
6162
                    return strcasecmp($field1->languageCode, $field2->languageCode);
6163
                }
6164
6165
                return $return;
6166
            }
6167
        );
6168
6169
        return $normalized;
6170
    }
6171
6172
    /**
6173
     * Returns a filtered set of the default fields fixture.
6174
     *
6175
     * @param string $languageCode
6176
     *
6177
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
6178
     */
6179
    private function createLocaleFieldsFixture($languageCode)
6180
    {
6181
        $fields = [];
6182
        foreach ($this->createFieldsFixture() as $field) {
6183
            if (null === $field->languageCode || $languageCode === $field->languageCode) {
6184
                $fields[] = $field;
6185
            }
6186
        }
6187
6188
        return $fields;
6189
    }
6190
6191
    /**
6192
     * Asserts that given Content has default ContentStates.
6193
     *
6194
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
6195
     */
6196 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
6197
    {
6198
        $repository = $this->getRepository();
6199
        $objectStateService = $repository->getObjectStateService();
6200
6201
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
6202
6203
        foreach ($objectStateGroups as $objectStateGroup) {
6204
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
6205
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
6206
                // Only check the first object state which is the default one.
6207
                $this->assertEquals(
6208
                    $objectState,
6209
                    $contentState
6210
                );
6211
                break;
6212
            }
6213
        }
6214
    }
6215
6216
    /**
6217
     * Assert that given Content has no references to a translation specified by the $languageCode.
6218
     *
6219
     * @param string $languageCode
6220
     * @param int $contentId
6221
     */
6222
    private function assertTranslationDoesNotExist($languageCode, $contentId)
6223
    {
6224
        $repository = $this->getRepository();
6225
        $contentService = $repository->getContentService();
6226
6227
        $content = $contentService->loadContent($contentId);
6228
6229
        foreach ($content->fields as $fieldIdentifier => $field) {
6230
            /** @var array $field */
6231
            self::assertArrayNotHasKey($languageCode, $field);
6232
            self::assertNotEquals($languageCode, $content->contentInfo->mainLanguageCode);
6233
            self::assertArrayNotHasKey($languageCode, $content->versionInfo->getNames());
6234
            self::assertNotEquals($languageCode, $content->versionInfo->initialLanguageCode);
6235
            self::assertNotContains($languageCode, $content->versionInfo->languageCodes);
6236
        }
6237
        foreach ($contentService->loadVersions($content->contentInfo) as $versionInfo) {
6238
            self::assertArrayNotHasKey($languageCode, $versionInfo->getNames());
6239
            self::assertNotEquals($languageCode, $versionInfo->contentInfo->mainLanguageCode);
6240
            self::assertNotEquals($languageCode, $versionInfo->initialLanguageCode);
6241
            self::assertNotContains($languageCode, $versionInfo->languageCodes);
6242
        }
6243
    }
6244
6245
    /**
6246
     * Returns the default fixture of fields used in most tests.
6247
     *
6248
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
6249
     */
6250
    private function createFieldsFixture()
6251
    {
6252
        return [
6253
            new Field(
6254
                [
6255
                    'id' => 0,
6256
                    'value' => 'Foo',
6257
                    'languageCode' => 'eng-US',
6258
                    'fieldDefIdentifier' => 'description',
6259
                    'fieldTypeIdentifier' => 'ezrichtext',
6260
                ]
6261
            ),
6262
            new Field(
6263
                [
6264
                    'id' => 0,
6265
                    'value' => 'Bar',
6266
                    'languageCode' => 'eng-GB',
6267
                    'fieldDefIdentifier' => 'description',
6268
                    'fieldTypeIdentifier' => 'ezrichtext',
6269
                ]
6270
            ),
6271
            new Field(
6272
                [
6273
                    'id' => 0,
6274
                    'value' => 'An awesome multi-lang forum²',
6275
                    'languageCode' => 'eng-US',
6276
                    'fieldDefIdentifier' => 'name',
6277
                    'fieldTypeIdentifier' => 'ezstring',
6278
                ]
6279
            ),
6280
            new Field(
6281
                [
6282
                    'id' => 0,
6283
                    'value' => 'An awesome multi-lang forum²³',
6284
                    'languageCode' => 'eng-GB',
6285
                    'fieldDefIdentifier' => 'name',
6286
                    'fieldTypeIdentifier' => 'ezstring',
6287
                ]
6288
            ),
6289
        ];
6290
    }
6291
6292
    /**
6293
     * Gets expected property values for the "Media" ContentInfo ValueObject.
6294
     *
6295
     * @return array
6296
     */
6297 View Code Duplication
    private function getExpectedMediaContentInfoProperties()
6298
    {
6299
        return [
6300
            'id' => 41,
6301
            'contentTypeId' => 1,
6302
            'name' => 'Media',
6303
            'sectionId' => 3,
6304
            'currentVersionNo' => 1,
6305
            'published' => true,
6306
            'ownerId' => 14,
6307
            'modificationDate' => $this->createDateTime(1060695457),
6308
            'publishedDate' => $this->createDateTime(1060695457),
6309
            'alwaysAvailable' => 1,
6310
            'remoteId' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
6311
            'mainLanguageCode' => 'eng-US',
6312
            'mainLocationId' => 43,
6313
            'status' => ContentInfo::STATUS_PUBLISHED,
6314
        ];
6315
    }
6316
6317
    /**
6318
     * @covers \eZ\Publish\API\Repository\ContentService::hideContent
6319
     *
6320
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
6321
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
6322
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
6323
     */
6324
    public function testHideContent(): void
6325
    {
6326
        $repository = $this->getRepository();
6327
        $contentTypeService = $repository->getContentTypeService();
6328
        $contentService = $repository->getContentService();
6329
        $locationService = $repository->getLocationService();
6330
6331
        $locationCreateStructs = array_map(
6332
            function (Location $parentLocation) use ($locationService) {
6333
                return $locationService->newLocationCreateStruct($parentLocation->id);
6334
            },
6335
            $this->createParentLocationsForHideReveal($locationService, 2)
6336
        );
6337
6338
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
6339
6340
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
6341
        $contentCreate->setField('name', 'Folder to hide');
6342
6343
        $content = $contentService->createContent(
6344
            $contentCreate,
6345
            $locationCreateStructs
6346
        );
6347
6348
        $publishedContent = $contentService->publishVersion($content->versionInfo);
6349
        $locations = $locationService->loadLocations($publishedContent->contentInfo);
6350
6351
        // Sanity check
6352
        $this->assertCount(3, $locations);
6353
        $this->assertCount(0, $this->filterHiddenLocations($locations));
6354
6355
        /* BEGIN: Use Case */
6356
        $contentService->hideContent($publishedContent->contentInfo);
6357
        /* END: Use Case */
6358
6359
        $locations = $locationService->loadLocations($publishedContent->contentInfo);
6360
        $this->assertCount(3, $locations);
6361
        $this->assertCount(3, $this->filterHiddenLocations($locations));
6362
    }
6363
6364
    /**
6365
     * @covers \eZ\Publish\API\Repository\ContentService::revealContent
6366
     *
6367
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
6368
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
6369
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
6370
     */
6371
    public function testRevealContent()
6372
    {
6373
        $repository = $this->getRepository();
6374
        $contentTypeService = $repository->getContentTypeService();
6375
        $contentService = $repository->getContentService();
6376
        $locationService = $repository->getLocationService();
6377
6378
        $locationCreateStructs = array_map(
6379
            function (Location $parentLocation) use ($locationService) {
6380
                return $locationService->newLocationCreateStruct($parentLocation->id);
6381
            },
6382
            $this->createParentLocationsForHideReveal($locationService, 2)
6383
        );
6384
6385
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
6386
6387
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
6388
        $contentCreate->setField('name', 'Folder to hide');
6389
6390
        $locationCreateStructs[0]->hidden = true;
6391
6392
        $content = $contentService->createContent(
6393
            $contentCreate,
6394
            $locationCreateStructs
6395
        );
6396
6397
        $publishedContent = $contentService->publishVersion($content->versionInfo);
6398
        $locations = $locationService->loadLocations($publishedContent->contentInfo);
6399
6400
        // Sanity check
6401
        $hiddenLocations = $this->filterHiddenLocations($locations);
6402
        $this->assertCount(3, $locations);
6403
        $this->assertCount(1, $hiddenLocations);
6404
6405
        // BEGIN: Use Case
6406
        $contentService->hideContent($publishedContent->contentInfo);
6407
        $this->assertCount(
6408
            3,
6409
            $this->filterHiddenLocations(
6410
                $locationService->loadLocations($publishedContent->contentInfo)
6411
            )
6412
        );
6413
6414
        $contentService->revealContent($publishedContent->contentInfo);
6415
        // END: Use Case
6416
6417
        $locations = $locationService->loadLocations($publishedContent->contentInfo);
6418
        $hiddenLocationsAfterReveal = $this->filterHiddenLocations($locations);
6419
        $this->assertCount(3, $locations);
6420
        $this->assertCount(1, $hiddenLocationsAfterReveal);
6421
        $this->assertEquals($hiddenLocations, $hiddenLocationsAfterReveal);
6422
    }
6423
6424
    /**
6425
     * @depends testRevealContent
6426
     */
6427
    public function testRevealContentWithHiddenParent()
6428
    {
6429
        $repository = $this->getRepository();
6430
6431
        $contentTypeService = $repository->getContentTypeService();
6432
        $contentService = $repository->getContentService();
6433
        $locationService = $repository->getLocationService();
6434
6435
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
6436
6437
        $contentNames = [
6438
            'Parent Content',
6439
            'Child (Nesting 1)',
6440
            'Child (Nesting 2)',
6441
            'Child (Nesting 3)',
6442
            'Child (Nesting 4)',
6443
        ];
6444
6445
        $parentLocation = $locationService->newLocationCreateStruct(
6446
            $this->generateId('location', 2)
6447
        );
6448
6449
        /** @var Content[] $contents */
6450
        $contents = [];
6451
6452 View Code Duplication
        foreach ($contentNames as $contentName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
6453
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
6454
            $contentCreate->setField('name', $contentName);
6455
6456
            $content = $contentService->createContent($contentCreate, [$parentLocation]);
6457
            $contents[] = $publishedContent = $contentService->publishVersion($content->versionInfo);
6458
6459
            $parentLocation = $locationService->newLocationCreateStruct(
6460
                $this->generateId('location', $publishedContent->contentInfo->mainLocationId)
6461
            );
6462
        }
6463
6464
        $contentService->hideContent($contents[0]->contentInfo);
6465
        $contentService->hideContent($contents[2]->contentInfo);
6466
        $contentService->revealContent($contents[2]->contentInfo);
6467
6468
        $parentContent = $contentService->loadContent($contents[0]->id);
6469
        $parentLocation = $locationService->loadLocation($parentContent->contentInfo->mainLocationId);
6470
        $parentSublocations = $locationService->loadLocationList([
6471
            $contents[1]->contentInfo->mainLocationId,
6472
            $contents[2]->contentInfo->mainLocationId,
6473
            $contents[3]->contentInfo->mainLocationId,
6474
            $contents[4]->contentInfo->mainLocationId,
6475
        ]);
6476
6477
        // Parent remains invisible
6478
        self::assertTrue($parentLocation->invisible);
6479
6480
        // All parent sublocations remain invisible as well
6481
        foreach ($parentSublocations as $parentSublocation) {
6482
            self::assertTrue($parentSublocation->invisible);
6483
        }
6484
    }
6485
6486
    /**
6487
     * @depends testRevealContent
6488
     */
6489
    public function testRevealContentWithHiddenChildren()
6490
    {
6491
        $repository = $this->getRepository();
6492
6493
        $contentTypeService = $repository->getContentTypeService();
6494
        $contentService = $repository->getContentService();
6495
        $locationService = $repository->getLocationService();
6496
6497
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
6498
6499
        $contentNames = [
6500
            'Parent Content',
6501
            'Child (Nesting 1)',
6502
            'Child (Nesting 2)',
6503
            'Child (Nesting 3)',
6504
            'Child (Nesting 4)',
6505
        ];
6506
6507
        $parentLocation = $locationService->newLocationCreateStruct(
6508
            $this->generateId('location', 2)
6509
        );
6510
6511
        /** @var Content[] $contents */
6512
        $contents = [];
6513
6514 View Code Duplication
        foreach ($contentNames as $contentName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
6515
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
6516
            $contentCreate->setField('name', $contentName);
6517
6518
            $content = $contentService->createContent($contentCreate, [$parentLocation]);
6519
            $contents[] = $publishedContent = $contentService->publishVersion($content->versionInfo);
6520
6521
            $parentLocation = $locationService->newLocationCreateStruct(
6522
                $this->generateId('location', $publishedContent->contentInfo->mainLocationId)
6523
            );
6524
        }
6525
6526
        $contentService->hideContent($contents[0]->contentInfo);
6527
        $contentService->hideContent($contents[2]->contentInfo);
6528
        $contentService->revealContent($contents[0]->contentInfo);
6529
6530
        $directChildContent = $contentService->loadContent($contents[1]->id);
6531
        $directChildLocation = $locationService->loadLocation($directChildContent->contentInfo->mainLocationId);
6532
6533
        $childContent = $contentService->loadContent($contents[2]->id);
6534
        $childLocation = $locationService->loadLocation($childContent->contentInfo->mainLocationId);
6535
        $childSublocations = $locationService->loadLocationList([
6536
            $contents[3]->contentInfo->mainLocationId,
6537
            $contents[4]->contentInfo->mainLocationId,
6538
        ]);
6539
6540
        // Direct child content is not hidden
6541
        self::assertFalse($directChildContent->contentInfo->isHidden);
6542
6543
        // Direct child content location is still invisible
6544
        self::assertFalse($directChildLocation->invisible);
6545
6546
        // Child content is still hidden
6547
        self::assertTrue($childContent->contentInfo->isHidden);
6548
6549
        // Child content location is still invisible
6550
        self::assertTrue($childLocation->invisible);
6551
6552
        // All childs sublocations remain invisible as well
6553
        foreach ($childSublocations as $childSublocation) {
6554
            self::assertTrue($childSublocation->invisible);
6555
        }
6556
    }
6557
6558
    public function testHideContentWithParentLocation()
6559
    {
6560
        $repository = $this->getRepository();
6561
        $contentTypeService = $repository->getContentTypeService();
6562
6563
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
6564
6565
        $contentService = $repository->getContentService();
6566
        $locationService = $repository->getLocationService();
6567
6568
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
6569
        $contentCreate->setField('name', 'Parent');
6570
6571
        $content = $contentService->createContent(
6572
            $contentCreate,
6573
            [
6574
                $locationService->newLocationCreateStruct(
6575
                    $this->generateId('location', 2)
6576
                ),
6577
            ]
6578
        );
6579
6580
        $publishedContent = $contentService->publishVersion($content->versionInfo);
6581
6582
        /* BEGIN: Use Case */
6583
        $contentService->hideContent($publishedContent->contentInfo);
6584
        /* END: Use Case */
6585
6586
        $locations = $locationService->loadLocations($publishedContent->contentInfo);
6587
6588
        $childContentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
6589
        $childContentCreate->setField('name', 'Child');
6590
6591
        $childContent = $contentService->createContent(
6592
            $childContentCreate,
6593
            [
6594
                $locationService->newLocationCreateStruct(
6595
                    $locations[0]->id
6596
                ),
6597
            ]
6598
        );
6599
6600
        $publishedChildContent = $contentService->publishVersion($childContent->versionInfo);
6601
6602
        $childLocations = $locationService->loadLocations($publishedChildContent->contentInfo);
6603
6604
        $this->assertTrue($locations[0]->hidden);
6605
        $this->assertTrue($locations[0]->invisible);
6606
6607
        $this->assertFalse($childLocations[0]->hidden);
6608
        $this->assertTrue($childLocations[0]->invisible);
6609
    }
6610
6611
    public function testChangeContentName()
6612
    {
6613
        $repository = $this->getRepository();
6614
6615
        $contentService = $repository->getContentService();
6616
        $contentDraft = $this->createContentDraft(
6617
            'folder',
6618
            $this->generateId('location', 2),
6619
            [
6620
                'name' => 'Marco',
6621
            ]
6622
        );
6623
6624
        $publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
6625
        $contentMetadataUpdateStruct = new ContentMetadataUpdateStruct([
6626
            'name' => 'Polo',
6627
        ]);
6628
        $contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
6629
6630
        $updatedContent = $contentService->loadContent($publishedContent->id);
6631
6632
        $this->assertEquals('Marco', $publishedContent->contentInfo->name);
6633
        $this->assertEquals('Polo', $updatedContent->contentInfo->name);
6634
    }
6635
6636
    public function testCopyTranslationsFromPublishedToDraft()
6637
    {
6638
        $repository = $this->getRepository();
6639
6640
        $contentService = $repository->getContentService();
6641
6642
        $contentDraft = $this->createContentDraft(
6643
            'folder',
6644
            $this->generateId('location', 2),
6645
            [
6646
                'name' => 'Folder US',
6647
            ]
6648
        );
6649
6650
        $publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
6651
6652
        $deDraft = $contentService->createContentDraft($publishedContent->contentInfo);
6653
6654
        $contentUpdateStruct = new ContentUpdateStruct([
6655
            'initialLanguageCode' => 'ger-DE',
6656
            'fields' => $contentDraft->getFields(),
6657
        ]);
6658
6659
        $contentUpdateStruct->setField('name', 'Folder GER', 'ger-DE');
6660
6661
        $deContent = $contentService->updateContent($deDraft->versionInfo, $contentUpdateStruct);
6662
6663
        $updatedContent = $contentService->loadContent($deContent->id, null, $deContent->versionInfo->versionNo);
6664
        $this->assertEquals(
6665
            [
6666
                'eng-US' => 'Folder US',
6667
                'ger-DE' => 'Folder GER',
6668
            ],
6669
            $updatedContent->fields['name']
6670
        );
6671
6672
        $gbDraft = $contentService->createContentDraft($publishedContent->contentInfo);
6673
6674
        $contentUpdateStruct = new ContentUpdateStruct([
6675
            'initialLanguageCode' => 'eng-GB',
6676
            'fields' => $contentDraft->getFields(),
6677
        ]);
6678
6679
        $contentUpdateStruct->setField('name', 'Folder GB', 'eng-GB');
6680
6681
        $gbContent = $contentService->updateContent($gbDraft->versionInfo, $contentUpdateStruct);
6682
        $contentService->publishVersion($gbDraft->versionInfo);
6683
        $updatedContent = $contentService->loadContent($gbContent->id, null, $gbContent->versionInfo->versionNo);
6684
        $this->assertEquals(
6685
            [
6686
                'eng-US' => 'Folder US',
6687
                'eng-GB' => 'Folder GB',
6688
            ],
6689
            $updatedContent->fields['name']
6690
        );
6691
6692
        $dePublished = $contentService->publishVersion($deDraft->versionInfo);
6693
        $this->assertEquals(
6694
            [
6695
                'eng-US' => 'Folder US',
6696
                'ger-DE' => 'Folder GER',
6697
                'eng-GB' => 'Folder GB',
6698
            ],
6699
            $dePublished->fields['name']
6700
        );
6701
    }
6702
6703
    /**
6704
     * Create structure of parent folders with Locations to be used for Content hide/reveal tests.
6705
     *
6706
     * @param \eZ\Publish\API\Repository\LocationService $locationService
6707
     * @param int $parentLocationId
6708
     *
6709
     * @return \eZ\Publish\API\Repository\Values\Content\Location[] A list of Locations aimed to be parents
6710
     *
6711
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
6712
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
6713
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
6714
     */
6715
    private function createParentLocationsForHideReveal(LocationService $locationService, int $parentLocationId): array
6716
    {
6717
        $parentFoldersLocationsIds = [
6718
            $this->createFolder(['eng-US' => 'P1'], $parentLocationId)->contentInfo->mainLocationId,
6719
            $this->createFolder(['eng-US' => 'P2'], $parentLocationId)->contentInfo->mainLocationId,
6720
            $this->createFolder(['eng-US' => 'P3'], $parentLocationId)->contentInfo->mainLocationId,
6721
        ];
6722
6723
        return array_values($locationService->loadLocationList($parentFoldersLocationsIds));
6724
    }
6725
6726
    /**
6727
     * Filter Locations list by hidden only.
6728
     *
6729
     * @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
6730
     *
6731
     * @return array
6732
     */
6733
    private function filterHiddenLocations(array $locations): array
6734
    {
6735
        return array_values(
6736
            array_filter(
6737
                $locations,
6738
                function (Location $location) {
6739
                    return $location->hidden;
6740
                }
6741
            )
6742
        );
6743
    }
6744
6745
    public function testPublishVersionWithSelectedLanguages()
6746
    {
6747
        $repository = $this->getRepository();
6748
6749
        $contentService = $repository->getContentService();
6750
6751
        $publishedContent = $this->createFolder(
6752
            [
6753
                'eng-US' => 'Published US',
6754
                'ger-DE' => 'Published DE',
6755
            ],
6756
            $this->generateId('location', 2)
6757
        );
6758
6759
        $draft = $contentService->createContentDraft($publishedContent->contentInfo);
6760
        $contentUpdateStruct = new ContentUpdateStruct([
6761
            'initialLanguageCode' => 'eng-US',
6762
        ]);
6763
        $contentUpdateStruct->setField('name', 'Draft 1 US', 'eng-US');
6764
        $contentUpdateStruct->setField('name', 'Draft 1 DE', 'ger-DE');
6765
6766
        $contentService->updateContent($draft->versionInfo, $contentUpdateStruct);
6767
6768
        $contentService->publishVersion($draft->versionInfo, ['ger-DE']);
6769
        $content = $contentService->loadContent($draft->contentInfo->id);
6770
        $this->assertEquals(
6771
            [
6772
                'eng-US' => 'Published US',
6773
                'ger-DE' => 'Draft 1 DE',
6774
            ],
6775
            $content->fields['name']
6776
        );
6777
    }
6778
6779
    public function testCreateContentWithRomanianSpecialCharsInTitle()
6780
    {
6781
        $repository = $this->getRepository();
6782
6783
        $baseName = 'ȘșțȚdfdf';
6784
        $expectedPath = '/SstTdfdf';
6785
6786
        $this->createFolder(['eng-US' => $baseName], 2);
6787
6788
        $urlAliasService = $repository->getURLAliasService();
6789
        $urlAlias = $urlAliasService->lookup($expectedPath);
6790
        $this->assertSame($expectedPath, $urlAlias->path);
6791
    }
6792
}
6793