Completed
Push — 7.0_deprecation_removal ( e8d685...a29025 )
by André
11:11
created

ContentServiceTest   F

Complexity

Total Complexity 228

Size/Duplication

Total Lines 5874
Duplicated Lines 21.84 %

Coupling/Cohesion

Components 1
Dependencies 30

Importance

Changes 0
Metric Value
dl 1283
loc 5874
rs 0.5217
c 0
b 0
f 0
wmc 228
lcom 1
cbo 30

164 Methods

Rating   Name   Duplication   Size   Complexity  
A testNewContentCreateStruct() 0 17 1
B testCreateContent() 0 28 2
A testCreateContentAndPublishWithPrivilegedAnonymousUser() 0 50 2
A testCreateContentSetsContentInfo() 0 6 1
B testCreateContentSetsExpectedContentInfo() 0 30 1
A testCreateContentSetsVersionInfo() 0 6 1
A testCreateContentSetsExpectedVersionInfo() 0 20 1
B 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 testLoadContentInfoByRemoteId() 0 15 1
A testLoadContentInfoByRemoteIdSetsExpectedContentInfo() 21 21 1
A testLoadContentInfoByRemoteIdThrowsNotFoundException() 0 11 1
A testLoadVersionInfoById() 21 21 1
B testLoadVersionInfoByIdSetsExpectedVersionInfo() 0 25 1
A testLoadVersionInfoByIdThrowsNotFoundException() 12 12 1
B 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
B testPublishVersionSetsExpectedContentInfo() 0 30 1
B testPublishVersionSetsExpectedVersionInfo() 0 28 1
A testPublishVersionCreatesLocationsDefinedOnCreate() 0 20 1
B testCreateContentWithLocationCreateParameterCreatesExpectedLocation() 0 26 1
A testPublishVersionThrowsBadStateException() 17 17 1
B testPublishVersionDoesNotChangePublishedDate() 0 26 1
A testCreateContentDraft() 0 20 1
A testCreateContentDraftAndLoadAccess() 21 21 1
A testCreateContentDraftSetsExpectedProperties() 0 13 1
B testCreateContentDraftSetsContentInfo() 0 25 1
B testCreateContentDraftSetsVersionInfo() 0 24 1
A testNewContentUpdateStruct() 0 23 1
A testUpdateContent() 0 19 1
A testUpdateContentWithDifferentUser() 0 19 1
A testLoadVersionInfo() 0 22 1
A testLoadContentByContentInfo() 0 22 1
A testCreateContentDraftLoadVersionInfoStillLoadsPublishedVersion() 0 18 1
A testCreateContentDraftLoadContentStillLoadsPublishedVersion() 0 18 1
A testCreateContentDraftLoadContentByRemoteIdStillLoadsPublishedVersion() 0 18 1
A testCreateContentDraftLoadContentByContentInfoStillLoadsPublishedVersion() 0 18 1
B testUpdateContentSetsExpectedFields() 0 45 1
B testUpdateContentThrowsBadStateException() 0 24 1
A testUpdateContentThrowsInvalidArgumentExceptionWhenFieldTypeDoesNotAccept() 22 22 1
B testUpdateContentWhenMandatoryFieldIsEmpty() 24 24 1
B testUpdateContentThrowsContentFieldValidationException() 0 24 1
B testUpdateContentWithNotUpdatingMandatoryField() 0 38 5
A testCreateContentDraftWithSecondParameter() 0 18 1
A testCreateContentDraftWithThirdParameter() 20 20 1
B testPublishVersionFromContentDraft() 26 26 1
B testPublishVersionFromContentDraftArchivesOldVersion() 26 26 1
A testPublishVersionFromContentDraftUpdatesContentInfoCurrentVersion() 0 8 1
B testPublishVersionFromOldContentDraftArchivesNewerVersionNo() 0 25 1
A testPublishVersionNotCreatingUnlimitedArchives() 0 62 1
B testNewContentMetadataUpdateStruct() 0 24 2
B testUpdateContentMetadata() 0 32 1
B testUpdateContentMetadataSetsExpectedProperties() 0 29 1
A testUpdateContentMetadataNotUpdatesContentVersion() 0 4 1
B testUpdateContentMetadataThrowsInvalidArgumentExceptionOnDuplicateRemoteId() 0 24 1
A testUpdateContentMetadataThrowsInvalidArgumentExceptionOnNoMetadataPropertiesSet() 0 12 1
A testDeleteContent() 21 21 2
A testDeleteContentWithEmptyBinaryField() 21 21 2
A testLoadContentDraftsReturnsEmptyArrayByDefault() 0 12 1
B testLoadContentDrafts() 0 44 1
A testLoadContentDraftsWithFirstParameter() 0 48 1
A testLoadVersionInfoWithSecondParameter() 0 23 1
A testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter() 0 14 1
B testLoadVersionInfoByIdWithSecondParameter() 0 28 1
B testLoadVersionInfoByIdWithSecondParameterSetsExpectedVersionInfo() 0 40 1
A testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter() 14 14 1
B testLoadContentByContentInfoWithLanguageParameters() 0 135 1
B 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
B testLoadContentByRemoteIdWithThirdParameter() 27 27 1
A testLoadContentByRemoteIdThrowsNotFoundExceptionWithThirdParameter() 0 18 1
A testLoadContentWithPrioritizedLanguagesList() 0 20 2
A getPrioritizedLanguageList() 0 9 1
B testDeleteVersion() 0 26 1
A testDeleteVersionThrowsBadStateExceptionOnPublishedVersion() 0 14 1
A testDeleteVersionThrowsBadStateExceptionOnLastVersion() 14 14 1
A testLoadVersions() 0 22 1
B testLoadVersionsSetsExpectedVersionInfo() 0 43 1
A testCopyContent() 59 59 1
A testCopyContentWithThirdParameter() 56 56 1
B 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
A testLoadRelations() 0 67 1
A testLoadRelationsSkipsArchivedContent() 0 58 1
A testLoadRelationsSkipsDraftContent() 0 54 1
B testLoadReverseRelations() 0 86 1
B testLoadReverseRelationsSkipsArchivedContent() 0 76 1
A testLoadReverseRelationsSkipsDraftContent() 0 68 1
B testDeleteRelation() 0 30 1
B testDeleteRelationThrowsBadStateException() 34 34 1
A testDeleteRelationThrowsInvalidArgumentException() 23 23 1
B testCreateContentInTransactionWithRollback() 0 47 4
B testCreateContentInTransactionWithCommit() 0 42 3
B testCreateContentWithLocationCreateParameterInTransactionWithRollback() 0 33 3
B testCreateContentWithLocationCreateParameterInTransactionWithCommit() 29 29 2
B testCreateContentDraftInTransactionWithRollback() 0 42 3
B testCreateContentDraftInTransactionWithCommit() 40 40 2
B testPublishVersionInTransactionWithRollback() 44 44 3
B testPublishVersionInTransactionWithCommit() 40 40 2
A testUpdateContentInTransactionWithRollback() 47 47 2
A testUpdateContentInTransactionWithCommit() 47 47 2
B testUpdateContentMetadataInTransactionWithRollback() 45 45 2
B testUpdateContentMetadataInTransactionWithCommit() 45 45 2
B testDeleteVersionInTransactionWithRollback() 0 36 2
B testDeleteVersionInTransactionWithCommit() 0 36 2
B testDeleteContentInTransactionWithRollback() 0 36 2
B testDeleteContentInTransactionWithCommit() 0 40 3
A testCopyContentInTransactionWithRollback() 50 50 2
A testCopyContentInTransactionWithCommit() 50 50 2
B testURLAliasesCreatedForNewContent() 0 36 1
A testURLAliasesCreatedForUpdatedContent() 0 72 1
A testCustomURLAliasesNotHistorizedOnUpdatedContent() 0 59 1
A testUpdatingDraftDoesNotUpdateOldVersions() 0 16 1
B testPublishWorkflow() 0 38 1
A testPublishContentWithoutPublishPolicyThrowsException() 0 19 1
A testDeleteTranslation() 21 21 2
B testDeleteTranslationUpdatesInitialLanguageCodeVersion() 0 31 1
B testDeleteTranslationUpdatesUrlAlias() 0 37 4
A testDeleteTranslationMainLanguageThrowsBadStateException() 0 12 1
B testDeleteTranslationDeletesSingleTranslationVersions() 0 24 1
A testDeleteTranslationThrowsUnauthorizedException() 0 23 1
A testDeleteTranslationThrowsInvalidArgumentException() 0 8 1
A testDeleteTranslationFromDraft() 0 15 1
A providerForDeleteTranslationFromDraftRemovesUrlAliasOnPublishing() 0 11 1
A testDeleteTranslationFromDraftRemovesUrlAliasOnPublishing() 0 60 3
B testDeleteTranslationFromDraftThrowsBadStateExceptionOnSingleTranslation() 0 31 1
A testDeleteTranslationFromDraftThrowsBadStateExceptionOnMainTranslation() 0 19 1
A testDeleteTranslationFromDraftThrowsBadStateExceptionOnPublishedVersion() 11 11 1
B testDeleteTranslationFromDraftThrowsUnauthorizedException() 0 26 1
A testDeleteTranslationFromDraftThrowsInvalidArgumentException() 0 10 1
B testLoadContentByVersionInfoWithSecondParameter() 17 82 4
A createRoleWithPolicies() 0 14 2
B assertAliasesCorrect() 0 38 5
A assertAllFieldsEquals() 0 7 1
A assertLocaleFieldsEquals() 0 14 3
B normalizeFields() 18 27 4
A createLocaleFieldsFixture() 0 11 4
A assertDefaultContentStates() 19 19 3
A assertTranslationDoesNotExist() 0 22 3
B createFieldsFixture() 0 41 1
A getExpectedMediaContentInfoProperties() 18 18 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\Values\Content\Content;
12
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
13
use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct;
14
use eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct;
15
use eZ\Publish\API\Repository\Values\Content\Field;
16
use eZ\Publish\API\Repository\Values\Content\Location;
17
use eZ\Publish\API\Repository\Values\Content\TranslationInfo;
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 Exception;
26
27
/**
28
 * Test case for operations in the ContentService using in memory storage.
29
 *
30
 * @see eZ\Publish\API\Repository\ContentService
31
 * @group content
32
 */
33
class ContentServiceTest extends BaseContentServiceTest
34
{
35
    /**
36
     * Test for the newContentCreateStruct() method.
37
     *
38
     * @see \eZ\Publish\API\Repository\ContentService::newContentCreateStruct()
39
     * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testLoadContentTypeByIdentifier
40
     * @group user
41
     * @group field-type
42
     */
43
    public function testNewContentCreateStruct()
44
    {
45
        $repository = $this->getRepository();
46
47
        /* BEGIN: Use Case */
48
        // Create a content type
49
        $contentTypeService = $repository->getContentTypeService();
50
51
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
52
53
        $contentService = $repository->getContentService();
54
55
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
56
        /* END: Use Case */
57
58
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentCreateStruct', $contentCreate);
59
    }
60
61
    /**
62
     * Test for the createContent() method.
63
     *
64
     * @return \eZ\Publish\API\Repository\Values\Content\Content
65
     *
66
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
67
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentCreateStruct
68
     * @group user
69
     * @group field-type
70
     */
71
    public function testCreateContent()
72
    {
73
        if ($this->isVersion4()) {
74
            $this->markTestSkipped('This test requires eZ Publish 5');
75
        }
76
77
        $repository = $this->getRepository();
78
79
        /* BEGIN: Use Case */
80
        $contentTypeService = $repository->getContentTypeService();
81
82
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
83
84
        $contentService = $repository->getContentService();
85
86
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
87
        $contentCreate->setField('name', 'My awesome forum');
88
89
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
90
        $contentCreate->alwaysAvailable = true;
91
92
        $content = $contentService->createContent($contentCreate);
93
        /* END: Use Case */
94
95
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content', $content);
96
97
        return $content;
98
    }
99
100
    /**
101
     * Test for the createContent() method.
102
     *
103
     * Tests made for issue #EZP-20955 where Anonymous user is granted access to create content
104
     * and should have access to do that.
105
     *
106
     * @return \eZ\Publish\API\Repository\Values\Content\Content
107
     *
108
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
109
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentCreateStruct
110
     * @group user
111
     * @group field-type
112
     */
113
    public function testCreateContentAndPublishWithPrivilegedAnonymousUser()
114
    {
115
        if ($this->isVersion4()) {
116
            $this->markTestSkipped('This test requires eZ Publish 5');
117
        }
118
119
        $anonymousUserId = $this->generateId('user', 10);
120
121
        $repository = $this->getRepository();
122
        $contentService = $repository->getContentService();
123
        $contentTypeService = $repository->getContentTypeService();
124
        $locationService = $repository->getLocationService();
125
        $roleService = $repository->getRoleService();
126
127
        // Give Anonymous user role additional rights
128
        $role = $roleService->loadRoleByIdentifier('Anonymous');
129
        $roleDraft = $roleService->createRoleDraft($role);
130
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'create');
131
        $policyCreateStruct->addLimitation(new SectionLimitation(array('limitationValues' => array(1))));
132
        $policyCreateStruct->addLimitation(new LocationLimitation(array('limitationValues' => array(2))));
133
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(array('limitationValues' => array(1))));
134
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
135
136
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'publish');
137
        $policyCreateStruct->addLimitation(new SectionLimitation(array('limitationValues' => array(1))));
138
        $policyCreateStruct->addLimitation(new LocationLimitation(array('limitationValues' => array(2))));
139
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(array('limitationValues' => array(1))));
140
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
141
        $roleService->publishRoleDraft($roleDraft);
142
143
        // Set Anonymous user as current
144
        $repository->getPermissionResolver()->setCurrentUserReference($repository->getUserService()->loadUser($anonymousUserId));
145
146
        // Create a new content object:
147
        $contentCreate = $contentService->newContentCreateStruct(
148
            $contentTypeService->loadContentTypeByIdentifier('folder'),
149
            'eng-GB'
150
        );
151
152
        $contentCreate->setField('name', 'Folder 1');
153
154
        $content = $contentService->createContent(
155
            $contentCreate,
156
            array($locationService->newLocationCreateStruct(2))
157
        );
158
159
        $contentService->publishVersion(
160
            $content->getVersionInfo()
161
        );
162
    }
163
164
    /**
165
     * Test for the createContent() method.
166
     *
167
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
168
     *
169
     * @return \eZ\Publish\API\Repository\Values\Content\Content
170
     *
171
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
172
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
173
     */
174
    public function testCreateContentSetsContentInfo($content)
175
    {
176
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $content->contentInfo);
177
178
        return $content;
179
    }
180
181
    /**
182
     * Test for the createContent() method.
183
     *
184
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
185
     *
186
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
187
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsContentInfo
188
     */
189
    public function testCreateContentSetsExpectedContentInfo($content)
190
    {
191
        $this->assertEquals(
192
            array(
193
                $content->id,
194
                28, // id of content type "forum"
195
                true,
196
                1,
197
                'abcdef0123456789abcdef0123456789',
198
                'eng-US',
199
                $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...
200
                false,
201
                null,
202
                // Main Location id for unpublished Content should be null
203
                null,
204
            ),
205
            array(
206
                $content->contentInfo->id,
207
                $content->contentInfo->contentTypeId,
208
                $content->contentInfo->alwaysAvailable,
209
                $content->contentInfo->currentVersionNo,
210
                $content->contentInfo->remoteId,
211
                $content->contentInfo->mainLanguageCode,
212
                $content->contentInfo->ownerId,
213
                $content->contentInfo->published,
214
                $content->contentInfo->publishedDate,
215
                $content->contentInfo->mainLocationId,
216
            )
217
        );
218
    }
219
220
    /**
221
     * Test for the createContent() method.
222
     *
223
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
224
     *
225
     * @return \eZ\Publish\API\Repository\Values\Content\Content
226
     *
227
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
228
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
229
     */
230
    public function testCreateContentSetsVersionInfo($content)
231
    {
232
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo', $content->getVersionInfo());
233
234
        return $content;
235
    }
236
237
    /**
238
     * Test for the createContent() method.
239
     *
240
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
241
     *
242
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
243
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsVersionInfo
244
     */
245
    public function testCreateContentSetsExpectedVersionInfo($content)
246
    {
247
        $this->assertEquals(
248
            array(
249
                'status' => VersionInfo::STATUS_DRAFT,
250
                'versionNo' => 1,
251
                '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...
252
                'initialLanguageCode' => 'eng-US',
253
            ),
254
            array(
255
                'status' => $content->getVersionInfo()->status,
256
                'versionNo' => $content->getVersionInfo()->versionNo,
257
                'creatorId' => $content->getVersionInfo()->creatorId,
258
                'initialLanguageCode' => $content->getVersionInfo()->initialLanguageCode,
259
            )
260
        );
261
        $this->assertTrue($content->getVersionInfo()->isDraft());
262
        $this->assertFalse($content->getVersionInfo()->isPublished());
263
        $this->assertFalse($content->getVersionInfo()->isArchived());
264
    }
265
266
    /**
267
     * Test for the createContent() method.
268
     *
269
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
270
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
271
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
272
     */
273
    public function testCreateContentThrowsInvalidArgumentException()
274
    {
275
        if ($this->isVersion4()) {
276
            $this->markTestSkipped('This test requires eZ Publish 5');
277
        }
278
279
        $repository = $this->getRepository();
280
281
        /* BEGIN: Use Case */
282
        $contentTypeService = $repository->getContentTypeService();
283
        $contentService = $repository->getContentService();
284
285
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
286
287
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
288
        $contentCreate1->setField('name', 'An awesome Sidelfingen forum');
289
290
        $contentCreate1->remoteId = 'abcdef0123456789abcdef0123456789';
291
        $contentCreate1->alwaysAvailable = true;
292
293
        $draft = $contentService->createContent($contentCreate1);
294
        $contentService->publishVersion($draft->versionInfo);
295
296
        $contentCreate2 = $contentService->newContentCreateStruct($contentType, 'eng-GB');
297
        $contentCreate2->setField('name', 'An awesome Bielefeld forum');
298
299
        $contentCreate2->remoteId = 'abcdef0123456789abcdef0123456789';
300
        $contentCreate2->alwaysAvailable = false;
301
302
        // This call will fail with an "InvalidArgumentException", because the
303
        // remoteId is already in use.
304
        $contentService->createContent($contentCreate2);
305
        /* END: Use Case */
306
    }
307
308
    /**
309
     * Test for the createContent() method.
310
     *
311
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
312
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
313
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
314
     */
315 View Code Duplication
    public function testCreateContentThrowsInvalidArgumentExceptionOnFieldTypeNotAccept()
316
    {
317
        $repository = $this->getRepository();
318
319
        /* BEGIN: Use Case */
320
        $contentTypeService = $repository->getContentTypeService();
321
        $contentService = $repository->getContentService();
322
323
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
324
325
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
326
        // The name field does only accept strings and null as its values
327
        $contentCreate->setField('name', new \stdClass());
328
329
        // Throws InvalidArgumentException since the name field is filled
330
        // improperly
331
        $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...
332
        /* END: Use Case */
333
    }
334
335
    /**
336
     * Test for the createContent() method.
337
     *
338
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
339
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
340
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
341
     */
342
    public function testCreateContentThrowsContentFieldValidationException()
343
    {
344
        $repository = $this->getRepository();
345
346
        /* BEGIN: Use Case */
347
        $contentTypeService = $repository->getContentTypeService();
348
        $contentService = $repository->getContentService();
349
350
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
351
352
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
353
        $contentCreate1->setField('name', 'An awesome Sidelfingen folder');
354
        // Violates string length constraint
355
        $contentCreate1->setField('short_name', str_repeat('a', 200));
356
357
        // Throws ContentFieldValidationException, since short_name does not pass
358
        // validation of the string length validator
359
        $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...
360
        /* END: Use Case */
361
    }
362
363
    /**
364
     * Test for the createContent() method.
365
     *
366
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
367
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
368
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
369
     */
370 View Code Duplication
    public function testCreateContentRequiredFieldMissing()
371
    {
372
        $repository = $this->getRepository();
373
374
        /* BEGIN: Use Case */
375
        $contentTypeService = $repository->getContentTypeService();
376
        $contentService = $repository->getContentService();
377
378
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
379
380
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
381
        // Required field "name" is not set
382
383
        // Throws a ContentFieldValidationException, since a required field is
384
        // missing
385
        $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...
386
        /* END: Use Case */
387
    }
388
389
    /**
390
     * Test for the createContent() method.
391
     *
392
     * NOTE: We have bidirectional dependencies between the ContentService and
393
     * the LocationService, so that we cannot use PHPUnit's test dependencies
394
     * here.
395
     *
396
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
397
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
398
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationByRemoteId
399
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
400
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
401
     * @group user
402
     */
403
    public function testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately()
404
    {
405
        $repository = $this->getRepository();
406
407
        $locationService = $repository->getLocationService();
408
409
        /* BEGIN: Use Case */
410
        $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...
411
412
        // The location will not have been created, yet, so this throws an
413
        // exception
414
        $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...
415
            '0123456789abcdef0123456789abcdef'
416
        );
417
        /* END: Use Case */
418
    }
419
420
    /**
421
     * Test for the createContent() method.
422
     *
423
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
424
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
425
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
426
     */
427
    public function testCreateContentThrowsInvalidArgumentExceptionWithLocationCreateParameter()
428
    {
429
        $repository = $this->getRepository();
430
431
        $parentLocationId = $this->generateId('location', 56);
432
        /* BEGIN: Use Case */
433
        // $parentLocationId is a valid location ID
434
435
        $contentService = $repository->getContentService();
436
        $contentTypeService = $repository->getContentTypeService();
437
        $locationService = $repository->getLocationService();
438
439
        // Load content type
440
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
441
442
        // Configure new locations
443
        $locationCreate1 = $locationService->newLocationCreateStruct($parentLocationId);
444
445
        $locationCreate1->priority = 23;
446
        $locationCreate1->hidden = true;
447
        $locationCreate1->remoteId = '0123456789abcdef0123456789aaaaaa';
448
        $locationCreate1->sortField = Location::SORT_FIELD_NODE_ID;
449
        $locationCreate1->sortOrder = Location::SORT_ORDER_DESC;
450
451
        $locationCreate2 = $locationService->newLocationCreateStruct($parentLocationId);
452
453
        $locationCreate2->priority = 42;
454
        $locationCreate2->hidden = true;
455
        $locationCreate2->remoteId = '0123456789abcdef0123456789bbbbbb';
456
        $locationCreate2->sortField = Location::SORT_FIELD_NODE_ID;
457
        $locationCreate2->sortOrder = Location::SORT_ORDER_DESC;
458
459
        // Configure new content object
460
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
461
462
        $contentCreate->setField('name', 'A awesome Sindelfingen forum');
463
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
464
        $contentCreate->alwaysAvailable = true;
465
466
        // Create new content object under the specified location
467
        $draft = $contentService->createContent(
468
            $contentCreate,
469
            array($locationCreate1)
470
        );
471
        $contentService->publishVersion($draft->versionInfo);
472
473
        // This call will fail with an "InvalidArgumentException", because the
474
        // Content remoteId already exists,
475
        $contentService->createContent(
476
            $contentCreate,
477
            array($locationCreate2)
478
        );
479
        /* END: Use Case */
480
    }
481
482
    /**
483
     * Test for the loadContentInfo() method.
484
     *
485
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
486
     * @group user
487
     */
488 View Code Duplication
    public function testLoadContentInfo()
489
    {
490
        $repository = $this->getRepository();
491
492
        $mediaFolderId = $this->generateId('object', 41);
493
        /* BEGIN: Use Case */
494
        $contentService = $repository->getContentService();
495
496
        // Load the ContentInfo for "Media" folder
497
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
498
        /* END: Use Case */
499
500
        $this->assertInstanceOf(
501
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
502
            $contentInfo
503
        );
504
505
        return $contentInfo;
506
    }
507
508
    /**
509
     * Test for the returned value of the loadContentInfo() method.
510
     *
511
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
512
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfo
513
     *
514
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
515
     */
516
    public function testLoadContentInfoSetsExpectedContentInfo(ContentInfo $contentInfo)
517
    {
518
        $this->assertPropertiesCorrectUnsorted(
519
            $this->getExpectedMediaContentInfoProperties(),
520
            $contentInfo
521
        );
522
    }
523
524
    /**
525
     * Test for the loadContentInfo() method.
526
     *
527
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
528
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
529
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
530
     */
531 View Code Duplication
    public function testLoadContentInfoThrowsNotFoundException()
532
    {
533
        $repository = $this->getRepository();
534
535
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
536
        /* BEGIN: Use Case */
537
        $contentService = $repository->getContentService();
538
539
        // This call will fail with a NotFoundException
540
        $contentService->loadContentInfo($nonExistentContentId);
541
        /* END: Use Case */
542
    }
543
544
    /**
545
     * Test for the loadContentInfoByRemoteId() method.
546
     *
547
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
548
     */
549
    public function testLoadContentInfoByRemoteId()
550
    {
551
        $repository = $this->getRepository();
552
553
        /* BEGIN: Use Case */
554
        $contentService = $repository->getContentService();
555
556
        // Load the ContentInfo for "Media" folder
557
        $contentInfo = $contentService->loadContentInfoByRemoteId('faaeb9be3bd98ed09f606fc16d144eca');
558
        /* END: Use Case */
559
560
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $contentInfo);
561
562
        return $contentInfo;
563
    }
564
565
    /**
566
     * Test for the returned value of the loadContentInfoByRemoteId() method.
567
     *
568
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
569
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId
570
     *
571
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
572
     */
573 View Code Duplication
    public function testLoadContentInfoByRemoteIdSetsExpectedContentInfo(ContentInfo $contentInfo)
574
    {
575
        $this->assertPropertiesCorrectUnsorted(
576
            [
577
                'id' => 10,
578
                'contentTypeId' => 4,
579
                'name' => 'Anonymous User',
580
                'sectionId' => 2,
581
                'currentVersionNo' => 2,
582
                'published' => true,
583
                'ownerId' => 14,
584
                'modificationDate' => $this->createDateTime(1072180405),
585
                'publishedDate' => $this->createDateTime(1033920665),
586
                'alwaysAvailable' => 1,
587
                'remoteId' => 'faaeb9be3bd98ed09f606fc16d144eca',
588
                'mainLanguageCode' => 'eng-US',
589
                'mainLocationId' => 45,
590
            ],
591
            $contentInfo
592
        );
593
    }
594
595
    /**
596
     * Test for the loadContentInfoByRemoteId() method.
597
     *
598
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
599
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
600
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
601
     */
602
    public function testLoadContentInfoByRemoteIdThrowsNotFoundException()
603
    {
604
        $repository = $this->getRepository();
605
606
        /* BEGIN: Use Case */
607
        $contentService = $repository->getContentService();
608
609
        // This call will fail with a NotFoundException
610
        $contentService->loadContentInfoByRemoteId('abcdefghijklmnopqrstuvwxyz0123456789');
611
        /* END: Use Case */
612
    }
613
614
    /**
615
     * Test for the loadVersionInfo() method.
616
     *
617
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
618
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
619
     * @group user
620
     */
621
    public function testLoadVersionInfo()
622
    {
623
        $repository = $this->getRepository();
624
625
        $mediaFolderId = $this->generateId('object', 41);
626
        /* BEGIN: Use Case */
627
        // $mediaFolderId contains the ID of the "Media" folder
628
629
        $contentService = $repository->getContentService();
630
631
        // Load the ContentInfo for "Media" folder
632
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
633
634
        // Now load the current version info of the "Media" folder
635
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
636
        /* END: Use Case */
637
638
        $this->assertInstanceOf(
639
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
640
            $versionInfo
641
        );
642
    }
643
644
    /**
645
     * Test for the loadVersionInfoById() method.
646
     *
647
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
648
     */
649 View Code Duplication
    public function testLoadVersionInfoById()
650
    {
651
        $repository = $this->getRepository();
652
653
        $mediaFolderId = $this->generateId('object', 41);
654
        /* BEGIN: Use Case */
655
        // $mediaFolderId contains the ID of the "Media" folder
656
657
        $contentService = $repository->getContentService();
658
659
        // Load the VersionInfo for "Media" folder
660
        $versionInfo = $contentService->loadVersionInfoById($mediaFolderId);
661
        /* END: Use Case */
662
663
        $this->assertInstanceOf(
664
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
665
            $versionInfo
666
        );
667
668
        return $versionInfo;
669
    }
670
671
    /**
672
     * Test for the returned value of the loadVersionInfoById() method.
673
     *
674
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
675
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
676
     *
677
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
678
     */
679
    public function testLoadVersionInfoByIdSetsExpectedVersionInfo(VersionInfo $versionInfo)
680
    {
681
        $this->assertPropertiesCorrect(
682
            [
683
                'names' => [
684
                    'eng-US' => 'Media',
685
                ],
686
                'contentInfo' => new ContentInfo($this->getExpectedMediaContentInfoProperties()),
687
                'id' => 472,
688
                'versionNo' => 1,
689
                'modificationDate' => $this->createDateTime(1060695457),
690
                'creatorId' => 14,
691
                'creationDate' => $this->createDateTime(1060695450),
692
                'status' => VersionInfo::STATUS_PUBLISHED,
693
                'initialLanguageCode' => 'eng-US',
694
                'languageCodes' => [
695
                    'eng-US',
696
                ],
697
            ],
698
            $versionInfo
699
        );
700
        $this->assertTrue($versionInfo->isPublished());
701
        $this->assertFalse($versionInfo->isDraft());
702
        $this->assertFalse($versionInfo->isArchived());
703
    }
704
705
    /**
706
     * Test for the loadVersionInfoById() method.
707
     *
708
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
709
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
710
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
711
     */
712 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundException()
713
    {
714
        $repository = $this->getRepository();
715
716
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
717
        /* BEGIN: Use Case */
718
        $contentService = $repository->getContentService();
719
720
        // This call will fail with a "NotFoundException"
721
        $contentService->loadVersionInfoById($nonExistentContentId);
722
        /* END: Use Case */
723
    }
724
725
    /**
726
     * Test for the loadContentByContentInfo() method.
727
     *
728
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
729
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
730
     */
731
    public function testLoadContentByContentInfo()
732
    {
733
        $repository = $this->getRepository();
734
735
        $mediaFolderId = $this->generateId('object', 41);
736
        /* BEGIN: Use Case */
737
        // $mediaFolderId contains the ID of the "Media" folder
738
739
        $contentService = $repository->getContentService();
740
741
        // Load the ContentInfo for "Media" folder
742
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
743
744
        // Now load the current content version for the info instance
745
        $content = $contentService->loadContentByContentInfo($contentInfo);
746
        /* END: Use Case */
747
748
        $this->assertInstanceOf(
749
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
750
            $content
751
        );
752
    }
753
754
    /**
755
     * Test for the loadContentByVersionInfo() method.
756
     *
757
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
758
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
759
     */
760
    public function testLoadContentByVersionInfo()
761
    {
762
        $repository = $this->getRepository();
763
764
        $mediaFolderId = $this->generateId('object', 41);
765
        /* BEGIN: Use Case */
766
        // $mediaFolderId contains the ID of the "Media" folder
767
768
        $contentService = $repository->getContentService();
769
770
        // Load the ContentInfo for "Media" folder
771
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
772
773
        // Load the current VersionInfo
774
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
775
776
        // Now load the current content version for the info instance
777
        $content = $contentService->loadContentByVersionInfo($versionInfo);
778
        /* END: Use Case */
779
780
        $this->assertInstanceOf(
781
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
782
            $content
783
        );
784
    }
785
786
    /**
787
     * Test for the loadContent() method.
788
     *
789
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
790
     * @group user
791
     * @group field-type
792
     */
793 View Code Duplication
    public function testLoadContent()
794
    {
795
        $repository = $this->getRepository();
796
797
        $mediaFolderId = $this->generateId('object', 41);
798
        /* BEGIN: Use Case */
799
        // $mediaFolderId contains the ID of the "Media" folder
800
801
        $contentService = $repository->getContentService();
802
803
        // Load the Content for "Media" folder, any language and current version
804
        $content = $contentService->loadContent($mediaFolderId);
805
        /* END: Use Case */
806
807
        $this->assertInstanceOf(
808
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
809
            $content
810
        );
811
    }
812
813
    /**
814
     * Test for the loadContent() method.
815
     *
816
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
817
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
818
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
819
     */
820 View Code Duplication
    public function testLoadContentThrowsNotFoundException()
821
    {
822
        $repository = $this->getRepository();
823
824
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
825
        /* BEGIN: Use Case */
826
        $contentService = $repository->getContentService();
827
828
        // This call will fail with a "NotFoundException"
829
        $contentService->loadContent($nonExistentContentId);
830
        /* END: Use Case */
831
    }
832
833
    /**
834
     * Data provider for testLoadContentByRemoteId().
835
     *
836
     * @return array
837
     */
838
    public function contentRemoteIdVersionLanguageProvider()
839
    {
840
        return [
841
            ['f5c88a2209584891056f987fd965b0ba', null, null],
842
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], null],
843
            ['f5c88a2209584891056f987fd965b0ba', null, 1],
844
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], 1],
845
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, null],
846
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], null],
847
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, 1],
848
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], 1],
849
        ];
850
    }
851
852
    /**
853
     * Test for the loadContentByRemoteId() method.
854
     *
855
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId
856
     * @dataProvider contentRemoteIdVersionLanguageProvider
857
     *
858
     * @param string $remoteId
859
     * @param array|null $languages
860
     * @param int $versionNo
861
     */
862
    public function testLoadContentByRemoteId($remoteId, $languages, $versionNo)
863
    {
864
        $repository = $this->getRepository();
865
866
        $contentService = $repository->getContentService();
867
868
        $content = $contentService->loadContentByRemoteId($remoteId, $languages, $versionNo);
869
870
        $this->assertInstanceOf(
871
            Content::class,
872
            $content
873
        );
874
875
        $this->assertEquals($remoteId, $content->contentInfo->remoteId);
876
        if ($languages !== null) {
877
            $this->assertEquals($languages, $content->getVersionInfo()->languageCodes);
878
        }
879
        $this->assertEquals($versionNo ?: 1, $content->getVersionInfo()->versionNo);
880
    }
881
882
    /**
883
     * Test for the loadContentByRemoteId() method.
884
     *
885
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
886
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
887
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
888
     */
889
    public function testLoadContentByRemoteIdThrowsNotFoundException()
890
    {
891
        $repository = $this->getRepository();
892
893
        /* BEGIN: Use Case */
894
        $contentService = $repository->getContentService();
895
896
        // This call will fail with a "NotFoundException", because no content
897
        // object exists for the given remoteId
898
        $contentService->loadContentByRemoteId('a1b1c1d1e1f1a2b2c2d2e2f2a3b3c3d3');
899
        /* END: Use Case */
900
    }
901
902
    /**
903
     * Test for the publishVersion() method.
904
     *
905
     * @return \eZ\Publish\API\Repository\Values\Content\Content
906
     *
907
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
908
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
909
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
910
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
911
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
912
     * @group user
913
     * @group field-type
914
     */
915
    public function testPublishVersion()
916
    {
917
        $time = time();
918
        /* BEGIN: Use Case */
919
        $content = $this->createContentVersion1();
920
        /* END: Use Case */
921
922
        $this->assertInstanceOf(Content::class, $content);
923
        $this->assertTrue($content->contentInfo->published);
924
        $this->assertEquals(VersionInfo::STATUS_PUBLISHED, $content->versionInfo->status);
925
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->publishedDate->getTimestamp());
926
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->modificationDate->getTimestamp());
927
        $this->assertTrue($content->versionInfo->isPublished());
928
        $this->assertFalse($content->versionInfo->isDraft());
929
        $this->assertFalse($content->versionInfo->isArchived());
930
931
        return $content;
932
    }
933
934
    /**
935
     * Test for the publishVersion() method.
936
     *
937
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
938
     *
939
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
940
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
941
     */
942
    public function testPublishVersionSetsExpectedContentInfo($content)
943
    {
944
        $this->assertEquals(
945
            array(
946
                $content->id,
947
                true,
948
                1,
949
                'abcdef0123456789abcdef0123456789',
950
                'eng-US',
951
                $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...
952
                true,
953
            ),
954
            array(
955
                $content->contentInfo->id,
956
                $content->contentInfo->alwaysAvailable,
957
                $content->contentInfo->currentVersionNo,
958
                $content->contentInfo->remoteId,
959
                $content->contentInfo->mainLanguageCode,
960
                $content->contentInfo->ownerId,
961
                $content->contentInfo->published,
962
            )
963
        );
964
965
        $this->assertNotNull($content->contentInfo->mainLocationId);
966
        $date = new \DateTime('1984/01/01');
967
        $this->assertGreaterThan(
968
            $date->getTimestamp(),
969
            $content->contentInfo->publishedDate->getTimestamp()
970
        );
971
    }
972
973
    /**
974
     * Test for the publishVersion() method.
975
     *
976
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
977
     *
978
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
979
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
980
     */
981
    public function testPublishVersionSetsExpectedVersionInfo($content)
982
    {
983
        $this->assertEquals(
984
            array(
985
                $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...
986
                'eng-US',
987
                VersionInfo::STATUS_PUBLISHED,
988
                1,
989
            ),
990
            array(
991
                $content->getVersionInfo()->creatorId,
992
                $content->getVersionInfo()->initialLanguageCode,
993
                $content->getVersionInfo()->status,
994
                $content->getVersionInfo()->versionNo,
995
            )
996
        );
997
998
        $date = new \DateTime('1984/01/01');
999
        $this->assertGreaterThan(
1000
            $date->getTimestamp(),
1001
            $content->getVersionInfo()->modificationDate->getTimestamp()
1002
        );
1003
1004
        $this->assertNotNull($content->getVersionInfo()->modificationDate);
1005
        $this->assertTrue($content->getVersionInfo()->isPublished());
1006
        $this->assertFalse($content->getVersionInfo()->isDraft());
1007
        $this->assertFalse($content->getVersionInfo()->isArchived());
1008
    }
1009
1010
    /**
1011
     * Test for the publishVersion() method.
1012
     *
1013
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1014
     *
1015
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1016
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
1017
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1018
     */
1019
    public function testPublishVersionCreatesLocationsDefinedOnCreate()
1020
    {
1021
        $repository = $this->getRepository();
1022
1023
        /* BEGIN: Use Case */
1024
        $content = $this->createContentVersion1();
1025
        /* END: Use Case */
1026
1027
        $locationService = $repository->getLocationService();
1028
        $location = $locationService->loadLocationByRemoteId(
1029
            '0123456789abcdef0123456789abcdef'
1030
        );
1031
1032
        $this->assertEquals(
1033
            $location->getContentInfo(),
1034
            $content->getVersionInfo()->getContentInfo()
1035
        );
1036
1037
        return array($content, $location);
1038
    }
1039
1040
    /**
1041
     * Test for the publishVersion() method.
1042
     *
1043
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1044
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionCreatesLocationsDefinedOnCreate
1045
     */
1046
    public function testCreateContentWithLocationCreateParameterCreatesExpectedLocation(array $testData)
1047
    {
1048
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
1049
        /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
1050
        list($content, $location) = $testData;
1051
1052
        $parentLocationId = $this->generateId('location', 56);
1053
        $parentLocation = $this->getRepository()->getLocationService()->loadLocation($parentLocationId);
1054
        $mainLocationId = $content->getVersionInfo()->getContentInfo()->mainLocationId;
1055
1056
        $this->assertPropertiesCorrect(
1057
            array(
1058
                'id' => $mainLocationId,
1059
                'priority' => 23,
1060
                'hidden' => true,
1061
                'invisible' => true,
1062
                'remoteId' => '0123456789abcdef0123456789abcdef',
1063
                'parentLocationId' => $parentLocationId,
1064
                'pathString' => $parentLocation->pathString . $mainLocationId . '/',
1065
                'depth' => $parentLocation->depth + 1,
1066
                'sortField' => Location::SORT_FIELD_NODE_ID,
1067
                'sortOrder' => Location::SORT_ORDER_DESC,
1068
            ),
1069
            $location
1070
        );
1071
    }
1072
1073
    /**
1074
     * Test for the publishVersion() method.
1075
     *
1076
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1077
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1078
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1079
     */
1080 View Code Duplication
    public function testPublishVersionThrowsBadStateException()
1081
    {
1082
        $repository = $this->getRepository();
1083
1084
        $contentService = $repository->getContentService();
1085
1086
        /* BEGIN: Use Case */
1087
        $draft = $this->createContentDraftVersion1();
1088
1089
        // Publish the content draft
1090
        $contentService->publishVersion($draft->getVersionInfo());
1091
1092
        // This call will fail with a "BadStateException", because the version
1093
        // is already published.
1094
        $contentService->publishVersion($draft->getVersionInfo());
1095
        /* END: Use Case */
1096
    }
1097
1098
    /**
1099
     * Test that publishVersion() does not affect publishedDate (assuming previous version exists).
1100
     *
1101
     * @covers \eZ\Publish\API\Repository\ContentService::publishVersion
1102
     */
1103
    public function testPublishVersionDoesNotChangePublishedDate()
1104
    {
1105
        $repository = $this->getRepository();
1106
1107
        $contentService = $repository->getContentService();
1108
1109
        $publishedContent = $this->createContentVersion1();
1110
1111
        // force timestamps to differ
1112
        sleep(1);
1113
1114
        $contentDraft = $contentService->createContentDraft($publishedContent->contentInfo);
1115
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1116
        $contentUpdateStruct->setField('name', 'New name');
1117
        $contentDraft = $contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
1118
        $republishedContent = $contentService->publishVersion($contentDraft->versionInfo);
1119
1120
        $this->assertEquals(
1121
            $publishedContent->contentInfo->publishedDate->getTimestamp(),
1122
            $republishedContent->contentInfo->publishedDate->getTimestamp()
1123
        );
1124
        $this->assertGreaterThan(
1125
            $publishedContent->contentInfo->modificationDate->getTimestamp(),
1126
            $republishedContent->contentInfo->modificationDate->getTimestamp()
1127
        );
1128
    }
1129
1130
    /**
1131
     * Test for the createContentDraft() method.
1132
     *
1133
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1134
     *
1135
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1136
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1137
     * @group user
1138
     */
1139
    public function testCreateContentDraft()
1140
    {
1141
        $repository = $this->getRepository();
1142
1143
        $contentService = $repository->getContentService();
1144
1145
        /* BEGIN: Use Case */
1146
        $content = $this->createContentVersion1();
1147
1148
        // Now we create a new draft from the published content
1149
        $draftedContent = $contentService->createContentDraft($content->contentInfo);
1150
        /* END: Use Case */
1151
1152
        $this->assertInstanceOf(
1153
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1154
            $draftedContent
1155
        );
1156
1157
        return $draftedContent;
1158
    }
1159
1160
    /**
1161
     * Test for the createContentDraft() method.
1162
     *
1163
     * Test that editor has access to edit own draft.
1164
     * Note: Editors have access to version_read, which is needed to load content drafts.
1165
     *
1166
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1167
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1168
     * @group user
1169
     */
1170 View Code Duplication
    public function testCreateContentDraftAndLoadAccess()
1171
    {
1172
        $repository = $this->getRepository();
1173
1174
        /* BEGIN: Use Case */
1175
        $user = $this->createUserVersion1();
1176
1177
        // Set new editor as user
1178
        $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...
1179
1180
        // Create draft
1181
        $draft = $this->createContentDraftVersion1(2, 'folder');
1182
1183
        // Try to load the draft
1184
        $contentService = $repository->getContentService();
1185
        $loadedDraft = $contentService->loadContent($draft->id);
1186
1187
        /* END: Use Case */
1188
1189
        $this->assertEquals($draft->id, $loadedDraft->id);
1190
    }
1191
1192
    /**
1193
     * Test for the createContentDraft() method.
1194
     *
1195
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1196
     *
1197
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1198
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1199
     */
1200
    public function testCreateContentDraftSetsExpectedProperties($draft)
1201
    {
1202
        $this->assertEquals(
1203
            array(
1204
                'fieldCount' => 2,
1205
                'relationCount' => 0,
1206
            ),
1207
            array(
1208
                'fieldCount' => count($draft->getFields()),
1209
                'relationCount' => count($this->getRepository()->getContentService()->loadRelations($draft->getVersionInfo())),
1210
            )
1211
        );
1212
    }
1213
1214
    /**
1215
     * Test for the createContentDraft() method.
1216
     *
1217
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1218
     *
1219
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1220
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1221
     */
1222
    public function testCreateContentDraftSetsContentInfo($draft)
1223
    {
1224
        $contentInfo = $draft->contentInfo;
1225
1226
        $this->assertEquals(
1227
            array(
1228
                $draft->id,
1229
                true,
1230
                1,
1231
                'eng-US',
1232
                $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...
1233
                'abcdef0123456789abcdef0123456789',
1234
                1,
1235
            ),
1236
            array(
1237
                $contentInfo->id,
1238
                $contentInfo->alwaysAvailable,
1239
                $contentInfo->currentVersionNo,
1240
                $contentInfo->mainLanguageCode,
1241
                $contentInfo->ownerId,
1242
                $contentInfo->remoteId,
1243
                $contentInfo->sectionId,
1244
            )
1245
        );
1246
    }
1247
1248
    /**
1249
     * Test for the createContentDraft() method.
1250
     *
1251
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1252
     *
1253
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1254
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1255
     */
1256
    public function testCreateContentDraftSetsVersionInfo($draft)
1257
    {
1258
        $versionInfo = $draft->getVersionInfo();
1259
1260
        $this->assertEquals(
1261
            array(
1262
                '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...
1263
                'initialLanguageCode' => 'eng-US',
1264
                'languageCodes' => array(0 => 'eng-US'),
1265
                'status' => VersionInfo::STATUS_DRAFT,
1266
                'versionNo' => 2,
1267
            ),
1268
            array(
1269
                'creatorId' => $versionInfo->creatorId,
1270
                'initialLanguageCode' => $versionInfo->initialLanguageCode,
1271
                'languageCodes' => $versionInfo->languageCodes,
1272
                'status' => $versionInfo->status,
1273
                'versionNo' => $versionInfo->versionNo,
1274
            )
1275
        );
1276
        $this->assertTrue($versionInfo->isDraft());
1277
        $this->assertFalse($versionInfo->isPublished());
1278
        $this->assertFalse($versionInfo->isArchived());
1279
    }
1280
1281
    /**
1282
     * Test for the createContentDraft() method.
1283
     *
1284
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1285
     *
1286
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1287
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1288
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
1289
     */
1290
    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...
1291
    {
1292
        $repository = $this->getRepository();
1293
1294
        $contentService = $repository->getContentService();
1295
1296
        /* BEGIN: Use Case */
1297
        $content = $this->createContentVersion1();
1298
1299
        // Now we create a new draft from the published content
1300
        $contentService->createContentDraft($content->contentInfo);
1301
1302
        // This call will still load the published version
1303
        $versionInfoPublished = $contentService->loadVersionInfo($content->contentInfo);
1304
        /* END: Use Case */
1305
1306
        $this->assertEquals(1, $versionInfoPublished->versionNo);
1307
    }
1308
1309
    /**
1310
     * Test for the createContentDraft() method.
1311
     *
1312
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1313
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
1314
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1315
     */
1316
    public function testCreateContentDraftLoadContentStillLoadsPublishedVersion()
1317
    {
1318
        $repository = $this->getRepository();
1319
1320
        $contentService = $repository->getContentService();
1321
1322
        /* BEGIN: Use Case */
1323
        $content = $this->createContentVersion1();
1324
1325
        // Now we create a new draft from the published content
1326
        $contentService->createContentDraft($content->contentInfo);
1327
1328
        // This call will still load the published content version
1329
        $contentPublished = $contentService->loadContent($content->id);
1330
        /* END: Use Case */
1331
1332
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1333
    }
1334
1335
    /**
1336
     * Test for the createContentDraft() method.
1337
     *
1338
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1339
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
1340
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1341
     */
1342
    public function testCreateContentDraftLoadContentByRemoteIdStillLoadsPublishedVersion()
1343
    {
1344
        $repository = $this->getRepository();
1345
1346
        $contentService = $repository->getContentService();
1347
1348
        /* BEGIN: Use Case */
1349
        $content = $this->createContentVersion1();
1350
1351
        // Now we create a new draft from the published content
1352
        $contentService->createContentDraft($content->contentInfo);
1353
1354
        // This call will still load the published content version
1355
        $contentPublished = $contentService->loadContentByRemoteId('abcdef0123456789abcdef0123456789');
1356
        /* END: Use Case */
1357
1358
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1359
    }
1360
1361
    /**
1362
     * Test for the createContentDraft() method.
1363
     *
1364
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1365
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
1366
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1367
     */
1368
    public function testCreateContentDraftLoadContentByContentInfoStillLoadsPublishedVersion()
1369
    {
1370
        $repository = $this->getRepository();
1371
1372
        $contentService = $repository->getContentService();
1373
1374
        /* BEGIN: Use Case */
1375
        $content = $this->createContentVersion1();
1376
1377
        // Now we create a new draft from the published content
1378
        $contentService->createContentDraft($content->contentInfo);
1379
1380
        // This call will still load the published content version
1381
        $contentPublished = $contentService->loadContentByContentInfo($content->contentInfo);
1382
        /* END: Use Case */
1383
1384
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1385
    }
1386
1387
    /**
1388
     * Test for the newContentUpdateStruct() method.
1389
     *
1390
     * @covers \eZ\Publish\API\Repository\ContentService::newContentUpdateStruct
1391
     * @group user
1392
     */
1393
    public function testNewContentUpdateStruct()
1394
    {
1395
        $repository = $this->getRepository();
1396
1397
        /* BEGIN: Use Case */
1398
        $contentService = $repository->getContentService();
1399
1400
        $updateStruct = $contentService->newContentUpdateStruct();
1401
        /* END: Use Case */
1402
1403
        $this->assertInstanceOf(
1404
            ContentUpdateStruct::class,
1405
            $updateStruct
1406
        );
1407
1408
        $this->assertPropertiesCorrect(
1409
            [
1410
                'initialLanguageCode' => null,
1411
                'fields' => [],
1412
            ],
1413
            $updateStruct
1414
        );
1415
    }
1416
1417
    /**
1418
     * Test for the updateContent() method.
1419
     *
1420
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1421
     *
1422
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1423
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1424
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1425
     * @group user
1426
     * @group field-type
1427
     */
1428
    public function testUpdateContent()
1429
    {
1430
        /* BEGIN: Use Case */
1431
        $draftVersion2 = $this->createUpdatedDraftVersion2();
1432
        /* END: Use Case */
1433
1434
        $this->assertInstanceOf(
1435
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1436
            $draftVersion2
1437
        );
1438
1439
        $this->assertEquals(
1440
            $this->generateId('user', 10),
1441
            $draftVersion2->versionInfo->creatorId,
1442
            'creatorId is not properly set on new Version'
1443
        );
1444
1445
        return $draftVersion2;
1446
    }
1447
1448
    /**
1449
     * Test for the updateContent_WithDifferentUser() method.
1450
     *
1451
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1452
     *
1453
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1454
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1455
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1456
     * @group user
1457
     * @group field-type
1458
     */
1459
    public function testUpdateContentWithDifferentUser()
1460
    {
1461
        /* BEGIN: Use Case */
1462
        $arrayWithDraftVersion2 = $this->createUpdatedDraftVersion2NotAdmin();
1463
        /* END: Use Case */
1464
1465
        $this->assertInstanceOf(
1466
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1467
            $arrayWithDraftVersion2[0]
1468
        );
1469
1470
        $this->assertEquals(
1471
            $this->generateId('user', $arrayWithDraftVersion2[1]),
1472
            $arrayWithDraftVersion2[0]->versionInfo->creatorId,
1473
            'creatorId is not properly set on new Version'
1474
        );
1475
1476
        return $arrayWithDraftVersion2[0];
1477
    }
1478
1479
    /**
1480
     * Test for the updateContent() method.
1481
     *
1482
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1483
     *
1484
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1485
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1486
     */
1487
    public function testUpdateContentSetsExpectedFields($content)
1488
    {
1489
        $actual = $this->normalizeFields($content->getFields());
1490
1491
        $expected = array(
1492
            new Field(
1493
                array(
1494
                    'id' => 0,
1495
                    'value' => true,
1496
                    'languageCode' => 'eng-GB',
1497
                    'fieldDefIdentifier' => 'description',
1498
                    'fieldTypeIdentifier' => 'ezrichtext',
1499
                )
1500
            ),
1501
            new Field(
1502
                array(
1503
                    'id' => 0,
1504
                    'value' => true,
1505
                    'languageCode' => 'eng-US',
1506
                    'fieldDefIdentifier' => 'description',
1507
                    'fieldTypeIdentifier' => 'ezrichtext',
1508
                )
1509
            ),
1510
            new Field(
1511
                array(
1512
                    'id' => 0,
1513
                    'value' => true,
1514
                    'languageCode' => 'eng-GB',
1515
                    'fieldDefIdentifier' => 'name',
1516
                    'fieldTypeIdentifier' => 'ezstring',
1517
                )
1518
            ),
1519
            new Field(
1520
                array(
1521
                    'id' => 0,
1522
                    'value' => true,
1523
                    'languageCode' => 'eng-US',
1524
                    'fieldDefIdentifier' => 'name',
1525
                    'fieldTypeIdentifier' => 'ezstring',
1526
                )
1527
            ),
1528
        );
1529
1530
        $this->assertEquals($expected, $actual);
1531
    }
1532
1533
    /**
1534
     * Test for the updateContent() method.
1535
     *
1536
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1537
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1538
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1539
     */
1540
    public function testUpdateContentThrowsBadStateException()
1541
    {
1542
        $repository = $this->getRepository();
1543
1544
        $contentService = $repository->getContentService();
1545
1546
        /* BEGIN: Use Case */
1547
        $content = $this->createContentVersion1();
1548
1549
        // Now create an update struct and modify some fields
1550
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1551
        $contentUpdateStruct->setField('title', 'An awesome² story about ezp.');
1552
        $contentUpdateStruct->setField('title', 'An awesome²³ story about ezp.', 'eng-GB');
1553
1554
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1555
1556
        // This call will fail with a "BadStateException", because $publishedContent
1557
        // is not a draft.
1558
        $contentService->updateContent(
1559
            $content->getVersionInfo(),
1560
            $contentUpdateStruct
1561
        );
1562
        /* END: Use Case */
1563
    }
1564
1565
    /**
1566
     * Test for the updateContent() method.
1567
     *
1568
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1569
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1570
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1571
     */
1572 View Code Duplication
    public function testUpdateContentThrowsInvalidArgumentExceptionWhenFieldTypeDoesNotAccept()
1573
    {
1574
        $repository = $this->getRepository();
1575
1576
        $contentService = $repository->getContentService();
1577
1578
        /* BEGIN: Use Case */
1579
        $draft = $this->createContentDraftVersion1();
1580
1581
        // Now create an update struct and modify some fields
1582
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1583
        // The name field does not accept a stdClass object as its input
1584
        $contentUpdateStruct->setField('name', new \stdClass(), 'eng-US');
1585
1586
        // Throws an InvalidArgumentException, since the value for field "name"
1587
        // is not accepted
1588
        $contentService->updateContent(
1589
            $draft->getVersionInfo(),
1590
            $contentUpdateStruct
1591
        );
1592
        /* END: Use Case */
1593
    }
1594
1595
    /**
1596
     * Test for the updateContent() method.
1597
     *
1598
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1599
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1600
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1601
     */
1602 View Code Duplication
    public function testUpdateContentWhenMandatoryFieldIsEmpty()
1603
    {
1604
        $repository = $this->getRepository();
1605
1606
        $contentService = $repository->getContentService();
1607
1608
        /* BEGIN: Use Case */
1609
        $draft = $this->createContentDraftVersion1();
1610
1611
        // Now create an update struct and set a mandatory field to null
1612
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1613
        $contentUpdateStruct->setField('name', null);
1614
1615
        // Don't set this, then the above call without languageCode will fail
1616
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1617
1618
        // This call will fail with a "ContentFieldValidationException", because the
1619
        // mandatory "name" field is empty.
1620
        $contentService->updateContent(
1621
            $draft->getVersionInfo(),
1622
            $contentUpdateStruct
1623
        );
1624
        /* END: Use Case */
1625
    }
1626
1627
    /**
1628
     * Test for the updateContent() method.
1629
     *
1630
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1631
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1632
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1633
     */
1634
    public function testUpdateContentThrowsContentFieldValidationException()
1635
    {
1636
        $repository = $this->getRepository();
1637
1638
        /* BEGIN: Use Case */
1639
        $contentTypeService = $repository->getContentTypeService();
1640
        $contentService = $repository->getContentService();
1641
1642
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1643
1644
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1645
        $contentCreate->setField('name', 'An awesome Sidelfingen folder');
1646
1647
        $draft = $contentService->createContent($contentCreate);
1648
1649
        $contentUpdate = $contentService->newContentUpdateStruct();
1650
        // Violates string length constraint
1651
        $contentUpdate->setField('short_name', str_repeat('a', 200), 'eng-US');
1652
1653
        // Throws ContentFieldValidationException because the string length
1654
        // validation of the field "short_name" fails
1655
        $contentService->updateContent($draft->getVersionInfo(), $contentUpdate);
1656
        /* END: Use Case */
1657
    }
1658
1659
    /**
1660
     * Test for the updateContent() method.
1661
     *
1662
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1663
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1664
     */
1665
    public function testUpdateContentWithNotUpdatingMandatoryField()
1666
    {
1667
        $repository = $this->getRepository();
1668
1669
        $contentService = $repository->getContentService();
1670
1671
        /* BEGIN: Use Case */
1672
        $draft = $this->createContentDraftVersion1();
1673
1674
        // Now create an update struct which does not overwrite mandatory
1675
        // fields
1676
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1677
        $contentUpdateStruct->setField(
1678
            'description',
1679
            '<?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"/>'
1680
        );
1681
1682
        // Don't set this, then the above call without languageCode will fail
1683
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1684
1685
        // This will only update the "description" field in the "eng-US"
1686
        // language
1687
        $updatedDraft = $contentService->updateContent(
1688
            $draft->getVersionInfo(),
1689
            $contentUpdateStruct
1690
        );
1691
        /* END: Use Case */
1692
1693
        foreach ($updatedDraft->getFields() as $field) {
1694
            if ($field->languageCode === 'eng-US' && $field->fieldDefIdentifier === 'name' && $field->value !== null) {
0 ignored issues
show
Documentation introduced by
The property $value is declared protected in eZ\Publish\API\Repository\Values\Content\Field. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1695
                // Found field
1696
                return;
1697
            }
1698
        }
1699
        $this->fail(
1700
            'Field with identifier "name" in language "eng-US" could not be found or has empty value.'
1701
        );
1702
    }
1703
1704
    /**
1705
     * Test for the createContentDraft() method.
1706
     *
1707
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
1708
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1709
     */
1710
    public function testCreateContentDraftWithSecondParameter()
1711
    {
1712
        $repository = $this->getRepository();
1713
1714
        $contentService = $repository->getContentService();
1715
1716
        /* BEGIN: Use Case */
1717
        $contentVersion2 = $this->createContentVersion2();
1718
1719
        // Now we create a new draft from the initial version
1720
        $draftedContentReloaded = $contentService->createContentDraft(
1721
            $contentVersion2->contentInfo,
1722
            $contentVersion2->getVersionInfo()
1723
        );
1724
        /* END: Use Case */
1725
1726
        $this->assertEquals(3, $draftedContentReloaded->getVersionInfo()->versionNo);
1727
    }
1728
1729
    /**
1730
     * Test for the createContentDraft() method with third parameter.
1731
     *
1732
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1733
     */
1734 View Code Duplication
    public function testCreateContentDraftWithThirdParameter()
1735
    {
1736
        $repository = $this->getRepository();
1737
1738
        $contentService = $repository->getContentService();
1739
1740
        $content = $contentService->loadContent(4);
1741
        $user = $this->createUserVersion1();
1742
1743
        $draftContent = $contentService->createContentDraft(
1744
            $content->contentInfo,
1745
            $content->getVersionInfo(),
1746
            $user
1747
        );
1748
1749
        $this->assertInstanceOf(
1750
            Content::class,
1751
            $draftContent
1752
        );
1753
    }
1754
1755
    /**
1756
     * Test for the publishVersion() method.
1757
     *
1758
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1759
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1760
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1761
     */
1762 View Code Duplication
    public function testPublishVersionFromContentDraft()
1763
    {
1764
        $repository = $this->getRepository();
1765
1766
        $contentService = $repository->getContentService();
1767
1768
        /* BEGIN: Use Case */
1769
        $contentVersion2 = $this->createContentVersion2();
1770
        /* END: Use Case */
1771
1772
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo);
1773
1774
        $this->assertEquals(
1775
            array(
1776
                'status' => VersionInfo::STATUS_PUBLISHED,
1777
                'versionNo' => 2,
1778
            ),
1779
            array(
1780
                'status' => $versionInfo->status,
1781
                'versionNo' => $versionInfo->versionNo,
1782
            )
1783
        );
1784
        $this->assertTrue($versionInfo->isPublished());
1785
        $this->assertFalse($versionInfo->isDraft());
1786
        $this->assertFalse($versionInfo->isArchived());
1787
    }
1788
1789
    /**
1790
     * Test for the publishVersion() method.
1791
     *
1792
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1793
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1794
     */
1795 View Code Duplication
    public function testPublishVersionFromContentDraftArchivesOldVersion()
1796
    {
1797
        $repository = $this->getRepository();
1798
1799
        $contentService = $repository->getContentService();
1800
1801
        /* BEGIN: Use Case */
1802
        $contentVersion2 = $this->createContentVersion2();
1803
        /* END: Use Case */
1804
1805
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo, 1);
1806
1807
        $this->assertEquals(
1808
            array(
1809
                'status' => VersionInfo::STATUS_ARCHIVED,
1810
                'versionNo' => 1,
1811
            ),
1812
            array(
1813
                'status' => $versionInfo->status,
1814
                'versionNo' => $versionInfo->versionNo,
1815
            )
1816
        );
1817
        $this->assertTrue($versionInfo->isArchived());
1818
        $this->assertFalse($versionInfo->isDraft());
1819
        $this->assertFalse($versionInfo->isPublished());
1820
    }
1821
1822
    /**
1823
     * Test for the publishVersion() method.
1824
     *
1825
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1826
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1827
     */
1828
    public function testPublishVersionFromContentDraftUpdatesContentInfoCurrentVersion()
1829
    {
1830
        /* BEGIN: Use Case */
1831
        $contentVersion2 = $this->createContentVersion2();
1832
        /* END: Use Case */
1833
1834
        $this->assertEquals(2, $contentVersion2->contentInfo->currentVersionNo);
1835
    }
1836
1837
    /**
1838
     * Test for the publishVersion() method.
1839
     *
1840
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1841
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1842
     */
1843
    public function testPublishVersionFromOldContentDraftArchivesNewerVersionNo()
1844
    {
1845
        $repository = $this->getRepository();
1846
1847
        $contentService = $repository->getContentService();
1848
1849
        /* BEGIN: Use Case */
1850
        $content = $this->createContentVersion1();
1851
1852
        // Create a new draft with versionNo = 2
1853
        $draftedContentVersion2 = $contentService->createContentDraft($content->contentInfo);
1854
1855
        // Create another new draft with versionNo = 3
1856
        $draftedContentVersion3 = $contentService->createContentDraft($content->contentInfo);
1857
1858
        // Publish draft with versionNo = 3
1859
        $contentService->publishVersion($draftedContentVersion3->getVersionInfo());
1860
1861
        // Publish the first draft with versionNo = 2
1862
        // currentVersionNo is now 2, versionNo 3 will be archived
1863
        $publishedDraft = $contentService->publishVersion($draftedContentVersion2->getVersionInfo());
1864
        /* END: Use Case */
1865
1866
        $this->assertEquals(2, $publishedDraft->contentInfo->currentVersionNo);
1867
    }
1868
1869
    /**
1870
     * Test for the publishVersion() method, and that it creates limited archives.
1871
     *
1872
     * @todo Adapt this when per content type archive limited is added on repository Content Type model.
1873
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1874
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1875
     */
1876
    public function testPublishVersionNotCreatingUnlimitedArchives()
1877
    {
1878
        $repository = $this->getRepository();
1879
1880
        $contentService = $repository->getContentService();
1881
1882
        $content = $this->createContentVersion1();
1883
1884
        // load first to make sure list gets updated also (cache)
1885
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
1886
        $this->assertEquals(1, count($versionInfoList));
1887
        $this->assertEquals(1, $versionInfoList[0]->versionNo);
1888
1889
        // Create a new draft with versionNo = 2
1890
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1891
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1892
1893
        // Create a new draft with versionNo = 3
1894
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1895
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1896
1897
        // Create a new draft with versionNo = 4
1898
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1899
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1900
1901
        // Create a new draft with versionNo = 5
1902
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1903
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1904
1905
        // Create a new draft with versionNo = 6
1906
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1907
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1908
1909
        // Create a new draft with versionNo = 7
1910
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1911
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1912
1913
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
1914
1915
        $this->assertEquals(6, count($versionInfoList));
1916
        $this->assertEquals(2, $versionInfoList[0]->versionNo);
1917
        $this->assertEquals(7, $versionInfoList[5]->versionNo);
1918
1919
        $this->assertEquals(
1920
            [
1921
                VersionInfo::STATUS_ARCHIVED,
1922
                VersionInfo::STATUS_ARCHIVED,
1923
                VersionInfo::STATUS_ARCHIVED,
1924
                VersionInfo::STATUS_ARCHIVED,
1925
                VersionInfo::STATUS_ARCHIVED,
1926
                VersionInfo::STATUS_PUBLISHED,
1927
            ],
1928
            [
1929
                $versionInfoList[0]->status,
1930
                $versionInfoList[1]->status,
1931
                $versionInfoList[2]->status,
1932
                $versionInfoList[3]->status,
1933
                $versionInfoList[4]->status,
1934
                $versionInfoList[5]->status,
1935
            ]
1936
        );
1937
    }
1938
1939
    /**
1940
     * Test for the newContentMetadataUpdateStruct() method.
1941
     *
1942
     * @covers \eZ\Publish\API\Repository\ContentService::newContentMetadataUpdateStruct
1943
     * @group user
1944
     */
1945
    public function testNewContentMetadataUpdateStruct()
1946
    {
1947
        $repository = $this->getRepository();
1948
1949
        /* BEGIN: Use Case */
1950
        $contentService = $repository->getContentService();
1951
1952
        // Creates a new metadata update struct
1953
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1954
1955
        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...
1956
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
1957
        }
1958
1959
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1960
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1961
        $metadataUpdate->alwaysAvailable = false;
1962
        /* END: Use Case */
1963
1964
        $this->assertInstanceOf(
1965
            ContentMetadataUpdateStruct::class,
1966
            $metadataUpdate
1967
        );
1968
    }
1969
1970
    /**
1971
     * Test for the updateContentMetadata() method.
1972
     *
1973
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1974
     *
1975
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1976
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1977
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentMetadataUpdateStruct
1978
     * @group user
1979
     */
1980
    public function testUpdateContentMetadata()
1981
    {
1982
        $repository = $this->getRepository();
1983
1984
        $contentService = $repository->getContentService();
1985
1986
        /* BEGIN: Use Case */
1987
        $content = $this->createContentVersion1();
1988
1989
        // Creates a metadata update struct
1990
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1991
1992
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1993
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1994
        $metadataUpdate->alwaysAvailable = false;
1995
        $metadataUpdate->publishedDate = $this->createDateTime(441759600); // 1984/01/01
1996
        $metadataUpdate->modificationDate = $this->createDateTime(441759600); // 1984/01/01
1997
1998
        // Update the metadata of the published content object
1999
        $content = $contentService->updateContentMetadata(
2000
            $content->contentInfo,
2001
            $metadataUpdate
2002
        );
2003
        /* END: Use Case */
2004
2005
        $this->assertInstanceOf(
2006
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
2007
            $content
2008
        );
2009
2010
        return $content;
2011
    }
2012
2013
    /**
2014
     * Test for the updateContentMetadata() method.
2015
     *
2016
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2017
     *
2018
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2019
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2020
     */
2021
    public function testUpdateContentMetadataSetsExpectedProperties($content)
2022
    {
2023
        $contentInfo = $content->contentInfo;
2024
2025
        $this->assertEquals(
2026
            array(
2027
                'remoteId' => 'aaaabbbbccccddddeeeeffff11112222',
2028
                'sectionId' => $this->generateId('section', 1),
2029
                'alwaysAvailable' => false,
2030
                'currentVersionNo' => 1,
2031
                'mainLanguageCode' => 'eng-GB',
2032
                'modificationDate' => $this->createDateTime(441759600),
2033
                '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...
2034
                'published' => true,
2035
                'publishedDate' => $this->createDateTime(441759600),
2036
            ),
2037
            array(
2038
                'remoteId' => $contentInfo->remoteId,
2039
                'sectionId' => $contentInfo->sectionId,
2040
                'alwaysAvailable' => $contentInfo->alwaysAvailable,
2041
                'currentVersionNo' => $contentInfo->currentVersionNo,
2042
                'mainLanguageCode' => $contentInfo->mainLanguageCode,
2043
                'modificationDate' => $contentInfo->modificationDate,
2044
                'ownerId' => $contentInfo->ownerId,
2045
                'published' => $contentInfo->published,
2046
                'publishedDate' => $contentInfo->publishedDate,
2047
            )
2048
        );
2049
    }
2050
2051
    /**
2052
     * Test for the updateContentMetadata() method.
2053
     *
2054
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2055
     *
2056
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2057
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2058
     */
2059
    public function testUpdateContentMetadataNotUpdatesContentVersion($content)
2060
    {
2061
        $this->assertEquals(1, $content->getVersionInfo()->versionNo);
2062
    }
2063
2064
    /**
2065
     * Test for the updateContentMetadata() method.
2066
     *
2067
     * @covers \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2068
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2069
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2070
     */
2071
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnDuplicateRemoteId()
2072
    {
2073
        $repository = $this->getRepository();
2074
2075
        $contentService = $repository->getContentService();
2076
2077
        /* BEGIN: Use Case */
2078
        // RemoteId of the "Media" page of an eZ Publish demo installation
2079
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2080
2081
        $content = $this->createContentVersion1();
2082
2083
        // Creates a metadata update struct
2084
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2085
        $metadataUpdate->remoteId = $mediaRemoteId;
2086
2087
        // This call will fail with an "InvalidArgumentException", because the
2088
        // specified remoteId is already used by the "Media" page.
2089
        $contentService->updateContentMetadata(
2090
            $content->contentInfo,
2091
            $metadataUpdate
2092
        );
2093
        /* END: Use Case */
2094
    }
2095
2096
    /**
2097
     * Test for the updateContentMetadata() method.
2098
     *
2099
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContentMetadata
2100
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2101
     */
2102
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnNoMetadataPropertiesSet()
2103
    {
2104
        $repository = $this->getRepository();
2105
2106
        $contentService = $repository->getContentService();
2107
2108
        $contentInfo = $contentService->loadContentInfo(4);
2109
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
2110
2111
        // Throws an exception because no properties are set in $contentMetadataUpdateStruct
2112
        $contentService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
2113
    }
2114
2115
    /**
2116
     * Test for the deleteContent() method.
2117
     *
2118
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2119
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2120
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2121
     */
2122 View Code Duplication
    public function testDeleteContent()
2123
    {
2124
        $repository = $this->getRepository();
2125
2126
        $contentService = $repository->getContentService();
2127
        $locationService = $repository->getLocationService();
2128
2129
        /* BEGIN: Use Case */
2130
        $contentVersion2 = $this->createContentVersion2();
2131
2132
        // Load the locations for this content object
2133
        $locations = $locationService->loadLocations($contentVersion2->contentInfo);
2134
2135
        // This will delete the content, all versions and the associated locations
2136
        $contentService->deleteContent($contentVersion2->contentInfo);
2137
        /* END: Use Case */
2138
2139
        foreach ($locations as $location) {
2140
            $locationService->loadLocation($location->id);
2141
        }
2142
    }
2143
2144
    /**
2145
     * Test for the deleteContent() method.
2146
     *
2147
     * Test for issue EZP-21057:
2148
     * "contentService: Unable to delete a content with an empty file attribute"
2149
     *
2150
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2151
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2152
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2153
     */
2154 View Code Duplication
    public function testDeleteContentWithEmptyBinaryField()
2155
    {
2156
        $repository = $this->getRepository();
2157
2158
        $contentService = $repository->getContentService();
2159
        $locationService = $repository->getLocationService();
2160
2161
        /* BEGIN: Use Case */
2162
        $contentVersion = $this->createContentVersion1EmptyBinaryField();
2163
2164
        // Load the locations for this content object
2165
        $locations = $locationService->loadLocations($contentVersion->contentInfo);
2166
2167
        // This will delete the content, all versions and the associated locations
2168
        $contentService->deleteContent($contentVersion->contentInfo);
2169
        /* END: Use Case */
2170
2171
        foreach ($locations as $location) {
2172
            $locationService->loadLocation($location->id);
2173
        }
2174
    }
2175
2176
    /**
2177
     * Test for the loadContentDrafts() method.
2178
     *
2179
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2180
     */
2181
    public function testLoadContentDraftsReturnsEmptyArrayByDefault()
2182
    {
2183
        $repository = $this->getRepository();
2184
2185
        /* BEGIN: Use Case */
2186
        $contentService = $repository->getContentService();
2187
2188
        $contentDrafts = $contentService->loadContentDrafts();
2189
        /* END: Use Case */
2190
2191
        $this->assertSame(array(), $contentDrafts);
2192
    }
2193
2194
    /**
2195
     * Test for the loadContentDrafts() method.
2196
     *
2197
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2198
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2199
     */
2200
    public function testLoadContentDrafts()
2201
    {
2202
        $repository = $this->getRepository();
2203
2204
        /* BEGIN: Use Case */
2205
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
2206
        // of a eZ Publish demo installation.
2207
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2208
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
2209
2210
        $contentService = $repository->getContentService();
2211
2212
        // "Media" content object
2213
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2214
2215
        // "eZ Publish Demo Design ..." content object
2216
        $demoDesignContentInfo = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
2217
2218
        // Create some drafts
2219
        $contentService->createContentDraft($mediaContentInfo);
2220
        $contentService->createContentDraft($demoDesignContentInfo);
2221
2222
        // Now $contentDrafts should contain two drafted versions
2223
        $draftedVersions = $contentService->loadContentDrafts();
2224
        /* END: Use Case */
2225
2226
        $actual = array(
2227
            $draftedVersions[0]->status,
2228
            $draftedVersions[0]->getContentInfo()->remoteId,
2229
            $draftedVersions[1]->status,
2230
            $draftedVersions[1]->getContentInfo()->remoteId,
2231
        );
2232
        sort($actual, SORT_STRING);
2233
2234
        $this->assertEquals(
2235
            array(
2236
                VersionInfo::STATUS_DRAFT,
2237
                VersionInfo::STATUS_DRAFT,
2238
                $demoDesignRemoteId,
2239
                $mediaRemoteId,
2240
            ),
2241
            $actual
2242
        );
2243
    }
2244
2245
    /**
2246
     * Test for the loadContentDrafts() method.
2247
     *
2248
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
2249
     */
2250
    public function testLoadContentDraftsWithFirstParameter()
2251
    {
2252
        $repository = $this->getRepository();
2253
2254
        /* BEGIN: Use Case */
2255
        $user = $this->createUserVersion1();
2256
2257
        // Get current user
2258
        $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...
2259
2260
        // Set new editor as user
2261
        $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...
2262
2263
        // Remote id of the "Media" content object in an eZ Publish demo installation.
2264
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2265
2266
        $contentService = $repository->getContentService();
2267
2268
        // "Media" content object
2269
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2270
2271
        // Create a content draft
2272
        $contentService->createContentDraft($mediaContentInfo);
2273
2274
        // Reset to previous current user
2275
        $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...
2276
2277
        // Now $contentDrafts for the previous current user and the new user
2278
        $newCurrentUserDrafts = $contentService->loadContentDrafts($user);
2279
        $oldCurrentUserDrafts = $contentService->loadContentDrafts($oldCurrentUser);
2280
        /* END: Use Case */
2281
2282
        $this->assertSame(array(), $oldCurrentUserDrafts);
2283
2284
        $this->assertEquals(
2285
            array(
2286
                VersionInfo::STATUS_DRAFT,
2287
                $mediaRemoteId,
2288
            ),
2289
            array(
2290
                $newCurrentUserDrafts[0]->status,
2291
                $newCurrentUserDrafts[0]->getContentInfo()->remoteId,
2292
            )
2293
        );
2294
        $this->assertTrue($newCurrentUserDrafts[0]->isDraft());
2295
        $this->assertFalse($newCurrentUserDrafts[0]->isArchived());
2296
        $this->assertFalse($newCurrentUserDrafts[0]->isPublished());
2297
    }
2298
2299
    /**
2300
     * Test for the loadVersionInfo() method.
2301
     *
2302
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2303
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2304
     */
2305
    public function testLoadVersionInfoWithSecondParameter()
2306
    {
2307
        $repository = $this->getRepository();
2308
2309
        $contentService = $repository->getContentService();
2310
2311
        /* BEGIN: Use Case */
2312
        $publishedContent = $this->createContentVersion1();
2313
2314
        $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...
2315
2316
        // Will return the VersionInfo of the $draftContent
2317
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2318
        /* END: Use Case */
2319
2320
        $this->assertEquals(2, $versionInfo->versionNo);
2321
2322
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2323
        $this->assertEquals(
2324
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2325
            $versionInfo->getContentInfo()->mainLocationId
2326
        );
2327
    }
2328
2329
    /**
2330
     * Test for the loadVersionInfo() method.
2331
     *
2332
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2333
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2334
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2335
     */
2336
    public function testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter()
2337
    {
2338
        $repository = $this->getRepository();
2339
2340
        $contentService = $repository->getContentService();
2341
2342
        /* BEGIN: Use Case */
2343
        $draft = $this->createContentDraftVersion1();
2344
2345
        // This call will fail with a "NotFoundException", because not versionNo
2346
        // 2 exists for this content object.
2347
        $contentService->loadVersionInfo($draft->contentInfo, 2);
2348
        /* END: Use Case */
2349
    }
2350
2351
    /**
2352
     * Test for the loadVersionInfoById() method.
2353
     *
2354
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2355
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2356
     */
2357
    public function testLoadVersionInfoByIdWithSecondParameter()
2358
    {
2359
        $repository = $this->getRepository();
2360
2361
        $contentService = $repository->getContentService();
2362
2363
        /* BEGIN: Use Case */
2364
        $publishedContent = $this->createContentVersion1();
2365
2366
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
2367
2368
        // Will return the VersionInfo of the $draftContent
2369
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2370
        /* END: Use Case */
2371
2372
        $this->assertEquals(2, $versionInfo->versionNo);
2373
2374
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2375
        $this->assertEquals(
2376
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2377
            $versionInfo->getContentInfo()->mainLocationId
2378
        );
2379
2380
        return [
2381
            'versionInfo' => $versionInfo,
2382
            'draftContent' => $draftContent,
2383
        ];
2384
    }
2385
2386
    /**
2387
     * Test for the returned value of the loadVersionInfoById() method.
2388
     *
2389
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter
2390
     * @covers \eZ\Publish\API\Repository\ContentService::loadVersionInfoById
2391
     *
2392
     * @param array $data
2393
     */
2394
    public function testLoadVersionInfoByIdWithSecondParameterSetsExpectedVersionInfo(array $data)
2395
    {
2396
        /** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo */
2397
        $versionInfo = $data['versionInfo'];
2398
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $draftContent */
2399
        $draftContent = $data['draftContent'];
2400
2401
        $this->assertPropertiesCorrect(
2402
            [
2403
                'names' => [
2404
                    'eng-US' => 'An awesome forum',
2405
                ],
2406
                'contentInfo' => new ContentInfo([
2407
                    'id' => $draftContent->contentInfo->id,
2408
                    'contentTypeId' => 28,
2409
                    'name' => 'An awesome forum',
2410
                    'sectionId' => 1,
2411
                    'currentVersionNo' => 1,
2412
                    'published' => true,
2413
                    'ownerId' => 14,
2414
                    // this Content Object is created at the test runtime
2415
                    'modificationDate' => $versionInfo->contentInfo->modificationDate,
2416
                    'publishedDate' => $versionInfo->contentInfo->publishedDate,
2417
                    'alwaysAvailable' => 1,
2418
                    'remoteId' => 'abcdef0123456789abcdef0123456789',
2419
                    'mainLanguageCode' => 'eng-US',
2420
                    'mainLocationId' => $draftContent->contentInfo->mainLocationId,
2421
                ]),
2422
                'id' => $draftContent->versionInfo->id,
2423
                'versionNo' => 2,
2424
                'creatorId' => 14,
2425
                'status' => 0,
2426
                'initialLanguageCode' => 'eng-US',
2427
                'languageCodes' => [
2428
                    'eng-US',
2429
                ],
2430
            ],
2431
            $versionInfo
2432
        );
2433
    }
2434
2435
    /**
2436
     * Test for the loadVersionInfoById() method.
2437
     *
2438
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2439
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2440
     */
2441 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter()
2442
    {
2443
        $repository = $this->getRepository();
2444
2445
        $contentService = $repository->getContentService();
2446
2447
        /* BEGIN: Use Case */
2448
        $content = $this->createContentVersion1();
2449
2450
        // This call will fail with a "NotFoundException", because not versionNo
2451
        // 2 exists for this content object.
2452
        $contentService->loadVersionInfoById($content->id, 2);
2453
        /* END: Use Case */
2454
    }
2455
2456
    /**
2457
     * Test for the loadContentByVersionInfo() method.
2458
     *
2459
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
2460
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2461
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
2462
     */
2463
    public function testLoadContentByVersionInfoWithSecondParameter()
2464
    {
2465
        $repository = $this->getRepository();
2466
2467
        $sectionId = $this->generateId('section', 1);
2468
        /* BEGIN: Use Case */
2469
        $contentTypeService = $repository->getContentTypeService();
2470
2471
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2472
2473
        $contentService = $repository->getContentService();
2474
2475
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2476
2477
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2478
2479
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2480
2481
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2482
        // $sectionId contains the ID of section 1
2483
        $contentCreateStruct->sectionId = $sectionId;
2484
        $contentCreateStruct->alwaysAvailable = true;
2485
2486
        // Create a new content draft
2487
        $content = $contentService->createContent($contentCreateStruct);
2488
2489
        // Now publish this draft
2490
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2491
2492
        // Will return a content instance with fields in "eng-US"
2493
        $reloadedContent = $contentService->loadContentByVersionInfo(
2494
            $publishedContent->getVersionInfo(),
2495
            array(
2496
                'eng-GB',
2497
            ),
2498
            false
2499
        );
2500
        /* END: Use Case */
2501
2502
        $actual = array();
2503 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...
2504
            $actual[] = new Field(
2505
                array(
2506
                    'id' => 0,
2507
                    'value' => ($field->value !== null ? true : null), // Actual value tested by FieldType integration tests
0 ignored issues
show
Documentation introduced by
The property $value is declared protected in eZ\Publish\API\Repository\Values\Content\Field. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
2508
                    'languageCode' => $field->languageCode,
2509
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
2510
                )
2511
            );
2512
        }
2513
        usort(
2514
            $actual,
2515 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...
2516
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
2517
                    return strcasecmp($field1->languageCode, $field2->languageCode);
2518
                }
2519
2520
                return $return;
2521
            }
2522
        );
2523
2524
        $expected = array(
2525
            new Field(
2526
                array(
2527
                    'id' => 0,
2528
                    'value' => true,
2529
                    'languageCode' => 'eng-GB',
2530
                    'fieldDefIdentifier' => 'description',
2531
                )
2532
            ),
2533
            new Field(
2534
                array(
2535
                    'id' => 0,
2536
                    'value' => true,
2537
                    'languageCode' => 'eng-GB',
2538
                    'fieldDefIdentifier' => 'name',
2539
                )
2540
            ),
2541
        );
2542
2543
        $this->assertEquals($expected, $actual);
2544
    }
2545
2546
    /**
2547
     * Test for the loadContentByContentInfo() method.
2548
     *
2549
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages)
2550
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2551
     */
2552
    public function testLoadContentByContentInfoWithLanguageParameters()
2553
    {
2554
        $repository = $this->getRepository();
2555
2556
        $sectionId = $this->generateId('section', 1);
2557
        /* BEGIN: Use Case */
2558
        $contentTypeService = $repository->getContentTypeService();
2559
2560
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2561
2562
        $contentService = $repository->getContentService();
2563
2564
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2565
2566
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2567
2568
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2569
2570
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2571
        // $sectionId contains the ID of section 1
2572
        $contentCreateStruct->sectionId = $sectionId;
2573
        $contentCreateStruct->alwaysAvailable = true;
2574
2575
        // Create a new content draft
2576
        $content = $contentService->createContent($contentCreateStruct);
2577
2578
        // Now publish this draft
2579
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2580
2581
        // Will return a content instance with fields in "eng-US"
2582
        $reloadedContent = $contentService->loadContentByContentInfo(
2583
            $publishedContent->contentInfo,
2584
            array(
2585
                'eng-US',
2586
            ),
2587
            null,
2588
            false
2589
        );
2590
        /* END: Use Case */
2591
2592
        $actual = $this->normalizeFields($reloadedContent->getFields());
2593
2594
        $expected = array(
2595
            new Field(
2596
                array(
2597
                    'id' => 0,
2598
                    'value' => true,
2599
                    'languageCode' => 'eng-US',
2600
                    'fieldDefIdentifier' => 'description',
2601
                    'fieldTypeIdentifier' => 'ezrichtext',
2602
                )
2603
            ),
2604
            new Field(
2605
                array(
2606
                    'id' => 0,
2607
                    'value' => true,
2608
                    'languageCode' => 'eng-US',
2609
                    'fieldDefIdentifier' => 'name',
2610
                    'fieldTypeIdentifier' => 'ezstring',
2611
                )
2612
            ),
2613
        );
2614
2615
        $this->assertEquals($expected, $actual);
2616
2617
        // Will return a content instance with fields in "eng-GB" (versions prior to 6.0.0-beta9 returned "eng-US" also)
2618
        $reloadedContent = $contentService->loadContentByContentInfo(
2619
            $publishedContent->contentInfo,
2620
            array(
2621
                'eng-GB',
2622
            ),
2623
            null,
2624
            true
2625
        );
2626
2627
        $actual = $this->normalizeFields($reloadedContent->getFields());
2628
2629
        $expected = array(
2630
            new Field(
2631
                array(
2632
                    'id' => 0,
2633
                    'value' => true,
2634
                    'languageCode' => 'eng-GB',
2635
                    'fieldDefIdentifier' => 'description',
2636
                    'fieldTypeIdentifier' => 'ezrichtext',
2637
                )
2638
            ),
2639
            new Field(
2640
                array(
2641
                    'id' => 0,
2642
                    'value' => true,
2643
                    'languageCode' => 'eng-GB',
2644
                    'fieldDefIdentifier' => 'name',
2645
                    'fieldTypeIdentifier' => 'ezstring',
2646
                )
2647
            ),
2648
        );
2649
2650
        $this->assertEquals($expected, $actual);
2651
2652
        // Will return a content instance with fields in main language "eng-US", as "fre-FR" does not exists
2653
        $reloadedContent = $contentService->loadContentByContentInfo(
2654
            $publishedContent->contentInfo,
2655
            array(
2656
                'fre-FR',
2657
            ),
2658
            null,
2659
            true
2660
        );
2661
2662
        $actual = $this->normalizeFields($reloadedContent->getFields());
2663
2664
        $expected = array(
2665
            new Field(
2666
                array(
2667
                    'id' => 0,
2668
                    'value' => true,
2669
                    'languageCode' => 'eng-US',
2670
                    'fieldDefIdentifier' => 'description',
2671
                    'fieldTypeIdentifier' => 'ezrichtext',
2672
                )
2673
            ),
2674
            new Field(
2675
                array(
2676
                    'id' => 0,
2677
                    'value' => true,
2678
                    'languageCode' => 'eng-US',
2679
                    'fieldDefIdentifier' => 'name',
2680
                    'fieldTypeIdentifier' => 'ezstring',
2681
                )
2682
            ),
2683
        );
2684
2685
        $this->assertEquals($expected, $actual);
2686
    }
2687
2688
    /**
2689
     * Test for the loadContentByContentInfo() method.
2690
     *
2691
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2692
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2693
     */
2694 View Code Duplication
    public function testLoadContentByContentInfoWithVersionNumberParameter()
2695
    {
2696
        $repository = $this->getRepository();
2697
2698
        $contentService = $repository->getContentService();
2699
2700
        /* BEGIN: Use Case */
2701
        $publishedContent = $this->createContentVersion1();
2702
2703
        $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...
2704
2705
        // This content instance is identical to $draftContent
2706
        $draftContentReloaded = $contentService->loadContentByContentInfo(
2707
            $publishedContent->contentInfo,
2708
            null,
2709
            2
2710
        );
2711
        /* END: Use Case */
2712
2713
        $this->assertEquals(
2714
            2,
2715
            $draftContentReloaded->getVersionInfo()->versionNo
2716
        );
2717
2718
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2719
        $this->assertEquals(
2720
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2721
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2722
        );
2723
    }
2724
2725
    /**
2726
     * Test for the loadContentByContentInfo() method.
2727
     *
2728
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2729
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2730
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithVersionNumberParameter
2731
     */
2732 View Code Duplication
    public function testLoadContentByContentInfoThrowsNotFoundExceptionWithVersionNumberParameter()
2733
    {
2734
        $repository = $this->getRepository();
2735
2736
        $contentService = $repository->getContentService();
2737
2738
        /* BEGIN: Use Case */
2739
        $content = $this->createContentVersion1();
2740
2741
        // This call will fail with a "NotFoundException", because no content
2742
        // with versionNo = 2 exists.
2743
        $contentService->loadContentByContentInfo($content->contentInfo, null, 2);
2744
        /* END: Use Case */
2745
    }
2746
2747
    /**
2748
     * Test for the loadContent() method.
2749
     *
2750
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages)
2751
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2752
     */
2753 View Code Duplication
    public function testLoadContentWithSecondParameter()
2754
    {
2755
        $repository = $this->getRepository();
2756
2757
        $contentService = $repository->getContentService();
2758
2759
        /* BEGIN: Use Case */
2760
        $draft = $this->createMultipleLanguageDraftVersion1();
2761
2762
        // This draft contains those fields localized with "eng-GB"
2763
        $draftLocalized = $contentService->loadContent($draft->id, array('eng-GB'), null, false);
2764
        /* END: Use Case */
2765
2766
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2767
2768
        return $draft;
2769
    }
2770
2771
    /**
2772
     * Test for the loadContent() method using undefined translation.
2773
     *
2774
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithSecondParameter
2775
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2776
     *
2777
     * @param \eZ\Publish\API\Repository\Values\Content\Content $contentDraft
2778
     */
2779
    public function testLoadContentWithSecondParameterThrowsNotFoundException(Content $contentDraft)
2780
    {
2781
        $repository = $this->getRepository();
2782
2783
        $contentService = $repository->getContentService();
2784
2785
        $contentService->loadContent($contentDraft->id, array('ger-DE'), null, false);
2786
    }
2787
2788
    /**
2789
     * Test for the loadContent() method.
2790
     *
2791
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2792
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2793
     */
2794 View Code Duplication
    public function testLoadContentWithThirdParameter()
2795
    {
2796
        $repository = $this->getRepository();
2797
2798
        $contentService = $repository->getContentService();
2799
2800
        /* BEGIN: Use Case */
2801
        $publishedContent = $this->createContentVersion1();
2802
2803
        $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...
2804
2805
        // This content instance is identical to $draftContent
2806
        $draftContentReloaded = $contentService->loadContent($publishedContent->id, null, 2);
2807
        /* END: Use Case */
2808
2809
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2810
2811
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2812
        $this->assertEquals(
2813
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2814
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2815
        );
2816
    }
2817
2818
    /**
2819
     * Test for the loadContent() method.
2820
     *
2821
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2822
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2823
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithThirdParameter
2824
     */
2825 View Code Duplication
    public function testLoadContentThrowsNotFoundExceptionWithThirdParameter()
2826
    {
2827
        $repository = $this->getRepository();
2828
2829
        $contentService = $repository->getContentService();
2830
2831
        /* BEGIN: Use Case */
2832
        $content = $this->createContentVersion1();
2833
2834
        // This call will fail with a "NotFoundException", because for this
2835
        // content object no versionNo=2 exists.
2836
        $contentService->loadContent($content->id, null, 2);
2837
        /* END: Use Case */
2838
    }
2839
2840
    /**
2841
     * Test for the loadContentByRemoteId() method.
2842
     *
2843
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages)
2844
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2845
     */
2846 View Code Duplication
    public function testLoadContentByRemoteIdWithSecondParameter()
2847
    {
2848
        $repository = $this->getRepository();
2849
2850
        $contentService = $repository->getContentService();
2851
2852
        /* BEGIN: Use Case */
2853
        $draft = $this->createMultipleLanguageDraftVersion1();
2854
2855
        $contentService->publishVersion($draft->versionInfo);
2856
2857
        // This draft contains those fields localized with "eng-GB"
2858
        $draftLocalized = $contentService->loadContentByRemoteId(
2859
            $draft->contentInfo->remoteId,
2860
            array('eng-GB'),
2861
            null,
2862
            false
2863
        );
2864
        /* END: Use Case */
2865
2866
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2867
    }
2868
2869
    /**
2870
     * Test for the loadContentByRemoteId() method.
2871
     *
2872
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2873
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2874
     */
2875 View Code Duplication
    public function testLoadContentByRemoteIdWithThirdParameter()
2876
    {
2877
        $repository = $this->getRepository();
2878
2879
        $contentService = $repository->getContentService();
2880
2881
        /* BEGIN: Use Case */
2882
        $publishedContent = $this->createContentVersion1();
2883
2884
        $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...
2885
2886
        // This content instance is identical to $draftContent
2887
        $draftContentReloaded = $contentService->loadContentByRemoteId(
2888
            $publishedContent->contentInfo->remoteId,
2889
            null,
2890
            2
2891
        );
2892
        /* END: Use Case */
2893
2894
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2895
2896
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2897
        $this->assertEquals(
2898
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2899
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2900
        );
2901
    }
2902
2903
    /**
2904
     * Test for the loadContentByRemoteId() method.
2905
     *
2906
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2907
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2908
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithThirdParameter
2909
     */
2910
    public function testLoadContentByRemoteIdThrowsNotFoundExceptionWithThirdParameter()
2911
    {
2912
        $repository = $this->getRepository();
2913
2914
        $contentService = $repository->getContentService();
2915
2916
        /* BEGIN: Use Case */
2917
        $content = $this->createContentVersion1();
2918
2919
        // This call will fail with a "NotFoundException", because for this
2920
        // content object no versionNo=2 exists.
2921
        $contentService->loadContentByRemoteId(
2922
            $content->contentInfo->remoteId,
2923
            null,
2924
            2
2925
        );
2926
        /* END: Use Case */
2927
    }
2928
2929
    /**
2930
     * Test that retrieval of translated name field respects prioritized language list.
2931
     *
2932
     * @dataProvider getPrioritizedLanguageList
2933
     * @param string[]|null $languageCodes
2934
     */
2935
    public function testLoadContentWithPrioritizedLanguagesList($languageCodes)
2936
    {
2937
        $repository = $this->getRepository();
2938
2939
        $contentService = $repository->getContentService();
2940
2941
        $content = $this->createContentVersion2();
2942
2943
        $content = $contentService->loadContent($content->id, $languageCodes);
2944
2945
        $expectedName = $content->getVersionInfo()->getName(
2946
            isset($languageCodes[0]) ? $languageCodes[0] : null
2947
        );
2948
        $nameValue = $content->getFieldValue('name');
2949
        /** @var \eZ\Publish\Core\FieldType\TextLine\Value $nameValue */
2950
        self::assertEquals($expectedName, $nameValue->text);
2951
        self::assertEquals($expectedName, $content->getVersionInfo()->getName());
2952
        // Also check value on shortcut method on content
2953
        self::assertEquals($expectedName, $content->getName());
2954
    }
2955
2956
    /**
2957
     * @return array
2958
     */
2959
    public function getPrioritizedLanguageList()
2960
    {
2961
        return [
2962
            [['eng-US']],
2963
            [['eng-GB']],
2964
            [['eng-GB', 'eng-US']],
2965
            [['eng-US', 'eng-GB']],
2966
        ];
2967
    }
2968
2969
    /**
2970
     * Test for the deleteVersion() method.
2971
     *
2972
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2973
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2974
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2975
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2976
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2977
     */
2978
    public function testDeleteVersion()
2979
    {
2980
        $repository = $this->getRepository();
2981
2982
        $contentService = $repository->getContentService();
2983
2984
        /* BEGIN: Use Case */
2985
        $content = $this->createContentVersion1();
2986
2987
        // Create new draft, because published or last version of the Content can't be deleted
2988
        $draft = $contentService->createContentDraft(
2989
            $content->getVersionInfo()->getContentInfo()
2990
        );
2991
2992
        // Delete the previously created draft
2993
        $contentService->deleteVersion($draft->getVersionInfo());
2994
        /* END: Use Case */
2995
2996
        $versions = $contentService->loadVersions($content->getVersionInfo()->getContentInfo());
2997
2998
        $this->assertCount(1, $versions);
2999
        $this->assertEquals(
3000
            $content->getVersionInfo()->id,
3001
            $versions[0]->id
3002
        );
3003
    }
3004
3005
    /**
3006
     * Test for the deleteVersion() method.
3007
     *
3008
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
3009
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3010
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3011
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3012
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3013
     */
3014
    public function testDeleteVersionThrowsBadStateExceptionOnPublishedVersion()
3015
    {
3016
        $repository = $this->getRepository();
3017
3018
        $contentService = $repository->getContentService();
3019
3020
        /* BEGIN: Use Case */
3021
        $content = $this->createContentVersion1();
3022
3023
        // This call will fail with a "BadStateException", because the content
3024
        // version is currently published.
3025
        $contentService->deleteVersion($content->getVersionInfo());
3026
        /* END: Use Case */
3027
    }
3028
3029
    /**
3030
     * Test for the deleteVersion() method.
3031
     *
3032
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
3033
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3034
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3035
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3036
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3037
     */
3038 View Code Duplication
    public function testDeleteVersionThrowsBadStateExceptionOnLastVersion()
3039
    {
3040
        $repository = $this->getRepository();
3041
3042
        $contentService = $repository->getContentService();
3043
3044
        /* BEGIN: Use Case */
3045
        $draft = $this->createContentDraftVersion1();
3046
3047
        // This call will fail with a "BadStateException", because the Content
3048
        // version is the last version of the Content.
3049
        $contentService->deleteVersion($draft->getVersionInfo());
3050
        /* END: Use Case */
3051
    }
3052
3053
    /**
3054
     * Test for the loadVersions() method.
3055
     *
3056
     * @see \eZ\Publish\API\Repository\ContentService::loadVersions()
3057
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3058
     *
3059
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[]
3060
     */
3061
    public function testLoadVersions()
3062
    {
3063
        $repository = $this->getRepository();
3064
3065
        $contentService = $repository->getContentService();
3066
3067
        /* BEGIN: Use Case */
3068
        $contentVersion2 = $this->createContentVersion2();
3069
3070
        // Load versions of this ContentInfo instance
3071
        $versions = $contentService->loadVersions($contentVersion2->contentInfo);
3072
        /* END: Use Case */
3073
3074
        $expectedVersionsOrder = [
3075
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1),
3076
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 2),
3077
        ];
3078
3079
        $this->assertEquals($expectedVersionsOrder, $versions);
3080
3081
        return $versions;
3082
    }
3083
3084
    /**
3085
     * Test for the loadVersions() method.
3086
     *
3087
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersions
3088
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersions
3089
     *
3090
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo[] $versions
3091
     */
3092
    public function testLoadVersionsSetsExpectedVersionInfo(array $versions)
3093
    {
3094
        $this->assertCount(2, $versions);
3095
3096
        $expectedVersions = [
3097
            [
3098
                'versionNo' => 1,
3099
                'creatorId' => 14,
3100
                'status' => VersionInfo::STATUS_ARCHIVED,
3101
                'initialLanguageCode' => 'eng-US',
3102
                'languageCodes' => ['eng-US'],
3103
            ],
3104
            [
3105
                'versionNo' => 2,
3106
                'creatorId' => 10,
3107
                'status' => VersionInfo::STATUS_PUBLISHED,
3108
                'initialLanguageCode' => 'eng-US',
3109
                'languageCodes' => ['eng-US', 'eng-GB'],
3110
            ],
3111
        ];
3112
3113
        $this->assertPropertiesCorrect($expectedVersions[0], $versions[0]);
3114
        $this->assertPropertiesCorrect($expectedVersions[1], $versions[1]);
3115
        $this->assertEquals(
3116
            $versions[0]->creationDate->getTimestamp(),
3117
            $versions[1]->creationDate->getTimestamp(),
3118
            'Creation time did not match within delta of 2 seconds',
3119
            2
3120
        );
3121
        $this->assertEquals(
3122
            $versions[0]->modificationDate->getTimestamp(),
3123
            $versions[1]->modificationDate->getTimestamp(),
3124
            'Creation time did not match within delta of 2 seconds',
3125
            2
3126
        );
3127
        $this->assertTrue($versions[0]->isArchived());
3128
        $this->assertFalse($versions[0]->isDraft());
3129
        $this->assertFalse($versions[0]->isPublished());
3130
3131
        $this->assertTrue($versions[1]->isPublished());
3132
        $this->assertFalse($versions[1]->isDraft());
3133
        $this->assertFalse($versions[1]->isArchived());
3134
    }
3135
3136
    /**
3137
     * Test for the copyContent() method.
3138
     *
3139
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
3140
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3141
     * @group field-type
3142
     */
3143 View Code Duplication
    public function testCopyContent()
3144
    {
3145
        $parentLocationId = $this->generateId('location', 56);
3146
3147
        $repository = $this->getRepository();
3148
3149
        $contentService = $repository->getContentService();
3150
        $locationService = $repository->getLocationService();
3151
3152
        /* BEGIN: Use Case */
3153
        $contentVersion2 = $this->createMultipleLanguageContentVersion2();
3154
3155
        // Configure new target location
3156
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
3157
3158
        $targetLocationCreate->priority = 42;
3159
        $targetLocationCreate->hidden = true;
3160
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3161
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3162
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3163
3164
        // Copy content with all versions and drafts
3165
        $contentCopied = $contentService->copyContent(
3166
            $contentVersion2->contentInfo,
3167
            $targetLocationCreate
3168
        );
3169
        /* END: Use Case */
3170
3171
        $this->assertInstanceOf(
3172
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
3173
            $contentCopied
3174
        );
3175
3176
        $this->assertNotEquals(
3177
            $contentVersion2->contentInfo->remoteId,
3178
            $contentCopied->contentInfo->remoteId
3179
        );
3180
3181
        $this->assertNotEquals(
3182
            $contentVersion2->id,
3183
            $contentCopied->id
3184
        );
3185
3186
        $this->assertEquals(
3187
            2,
3188
            count($contentService->loadVersions($contentCopied->contentInfo))
3189
        );
3190
3191
        $this->assertEquals(2, $contentCopied->getVersionInfo()->versionNo);
3192
3193
        $this->assertAllFieldsEquals($contentCopied->getFields());
3194
3195
        $this->assertDefaultContentStates($contentCopied->contentInfo);
3196
3197
        $this->assertNotNull(
3198
            $contentCopied->contentInfo->mainLocationId,
3199
            'Expected main location to be set given we provided a LocationCreateStruct'
3200
        );
3201
    }
3202
3203
    /**
3204
     * Test for the copyContent() method.
3205
     *
3206
     * @see \eZ\Publish\API\Repository\ContentService::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo)
3207
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
3208
     *
3209
     * @todo Fix to more descriptive name
3210
     */
3211 View Code Duplication
    public function testCopyContentWithThirdParameter()
3212
    {
3213
        $parentLocationId = $this->generateId('location', 56);
3214
3215
        $repository = $this->getRepository();
3216
3217
        $contentService = $repository->getContentService();
3218
        $locationService = $repository->getLocationService();
3219
3220
        /* BEGIN: Use Case */
3221
        $contentVersion2 = $this->createContentVersion2();
3222
3223
        // Configure new target location
3224
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
3225
3226
        $targetLocationCreate->priority = 42;
3227
        $targetLocationCreate->hidden = true;
3228
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3229
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3230
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3231
3232
        // Copy only the initial version
3233
        $contentCopied = $contentService->copyContent(
3234
            $contentVersion2->contentInfo,
3235
            $targetLocationCreate,
3236
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1)
3237
        );
3238
        /* END: Use Case */
3239
3240
        $this->assertInstanceOf(
3241
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
3242
            $contentCopied
3243
        );
3244
3245
        $this->assertNotEquals(
3246
            $contentVersion2->contentInfo->remoteId,
3247
            $contentCopied->contentInfo->remoteId
3248
        );
3249
3250
        $this->assertNotEquals(
3251
            $contentVersion2->id,
3252
            $contentCopied->id
3253
        );
3254
3255
        $this->assertEquals(
3256
            1,
3257
            count($contentService->loadVersions($contentCopied->contentInfo))
3258
        );
3259
3260
        $this->assertEquals(1, $contentCopied->getVersionInfo()->versionNo);
3261
3262
        $this->assertNotNull(
3263
            $contentCopied->contentInfo->mainLocationId,
3264
            'Expected main location to be set given we provided a LocationCreateStruct'
3265
        );
3266
    }
3267
3268
    /**
3269
     * Test for the addRelation() method.
3270
     *
3271
     * @return \eZ\Publish\API\Repository\Values\Content\Content
3272
     *
3273
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3274
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3275
     */
3276
    public function testAddRelation()
3277
    {
3278
        $repository = $this->getRepository();
3279
3280
        $contentService = $repository->getContentService();
3281
3282
        /* BEGIN: Use Case */
3283
        // RemoteId of the "Media" content of an eZ Publish demo installation
3284
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3285
3286
        $draft = $this->createContentDraftVersion1();
3287
3288
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3289
3290
        // Create relation between new content object and "Media" page
3291
        $relation = $contentService->addRelation(
3292
            $draft->getVersionInfo(),
3293
            $media
3294
        );
3295
        /* END: Use Case */
3296
3297
        $this->assertInstanceOf(
3298
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Relation',
3299
            $relation
3300
        );
3301
3302
        return $contentService->loadRelations($draft->getVersionInfo());
3303
    }
3304
3305
    /**
3306
     * Test for the addRelation() method.
3307
     *
3308
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3309
     *
3310
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3311
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3312
     */
3313
    public function testAddRelationAddsRelationToContent($relations)
3314
    {
3315
        $this->assertEquals(
3316
            1,
3317
            count($relations)
3318
        );
3319
    }
3320
3321
    /**
3322
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3323
     */
3324
    protected function assertExpectedRelations($relations)
3325
    {
3326
        $this->assertEquals(
3327
            array(
3328
                'type' => Relation::COMMON,
3329
                'sourceFieldDefinitionIdentifier' => null,
3330
                'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3331
                'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3332
            ),
3333
            array(
3334
                'type' => $relations[0]->type,
3335
                'sourceFieldDefinitionIdentifier' => $relations[0]->sourceFieldDefinitionIdentifier,
3336
                'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3337
                'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3338
            )
3339
        );
3340
    }
3341
3342
    /**
3343
     * Test for the addRelation() method.
3344
     *
3345
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3346
     *
3347
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3348
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3349
     */
3350
    public function testAddRelationSetsExpectedRelations($relations)
3351
    {
3352
        $this->assertExpectedRelations($relations);
3353
    }
3354
3355
    /**
3356
     * Test for the createContentDraft() method.
3357
     *
3358
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3359
     *
3360
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
3361
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelationSetsExpectedRelations
3362
     */
3363 View Code Duplication
    public function testCreateContentDraftWithRelations()
3364
    {
3365
        $repository = $this->getRepository();
3366
3367
        $contentService = $repository->getContentService();
3368
3369
        // RemoteId of the "Media" content of an eZ Publish demo installation
3370
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3371
        $draft = $this->createContentDraftVersion1();
3372
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3373
3374
        // Create relation between new content object and "Media" page
3375
        $contentService->addRelation(
3376
            $draft->getVersionInfo(),
3377
            $media
3378
        );
3379
3380
        $content = $contentService->publishVersion($draft->versionInfo);
3381
        $newDraft = $contentService->createContentDraft($content->contentInfo);
3382
3383
        return $contentService->loadRelations($newDraft->getVersionInfo());
3384
    }
3385
3386
    /**
3387
     * Test for the createContentDraft() method.
3388
     *
3389
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3390
     *
3391
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3392
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelations
3393
     */
3394
    public function testCreateContentDraftWithRelationsCreatesRelations($relations)
3395
    {
3396
        $this->assertEquals(
3397
            1,
3398
            count($relations)
3399
        );
3400
3401
        return $relations;
3402
    }
3403
3404
    /**
3405
     * Test for the createContentDraft() method.
3406
     *
3407
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3408
     *
3409
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelationsCreatesRelations
3410
     */
3411
    public function testCreateContentDraftWithRelationsCreatesExpectedRelations($relations)
3412
    {
3413
        $this->assertExpectedRelations($relations);
3414
    }
3415
3416
    /**
3417
     * Test for the addRelation() method.
3418
     *
3419
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3420
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3421
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3422
     */
3423 View Code Duplication
    public function testAddRelationThrowsBadStateException()
3424
    {
3425
        $repository = $this->getRepository();
3426
3427
        $contentService = $repository->getContentService();
3428
3429
        /* BEGIN: Use Case */
3430
        // RemoteId of the "Media" page of an eZ Publish demo installation
3431
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3432
3433
        $content = $this->createContentVersion1();
3434
3435
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3436
3437
        // This call will fail with a "BadStateException", because content is
3438
        // published and not a draft.
3439
        $contentService->addRelation(
3440
            $content->getVersionInfo(),
3441
            $media
3442
        );
3443
        /* END: Use Case */
3444
    }
3445
3446
    /**
3447
     * Test for the loadRelations() method.
3448
     *
3449
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3450
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3451
     */
3452
    public function testLoadRelations()
3453
    {
3454
        $repository = $this->getRepository();
3455
3456
        $contentService = $repository->getContentService();
3457
3458
        /* BEGIN: Use Case */
3459
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3460
        // of a eZ Publish demo installation.
3461
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3462
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3463
3464
        $draft = $this->createContentDraftVersion1();
3465
3466
        // Load other content objects
3467
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3468
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3469
3470
        // Create relation between new content object and "Media" page
3471
        $contentService->addRelation(
3472
            $draft->getVersionInfo(),
3473
            $media
3474
        );
3475
3476
        // Create another relation with the "Demo Design" page
3477
        $contentService->addRelation(
3478
            $draft->getVersionInfo(),
3479
            $demoDesign
3480
        );
3481
3482
        // Load all relations
3483
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3484
        /* END: Use Case */
3485
3486
        usort(
3487
            $relations,
3488
            function ($rel1, $rel2) {
3489
                return strcasecmp(
3490
                    $rel2->getDestinationContentInfo()->remoteId,
3491
                    $rel1->getDestinationContentInfo()->remoteId
3492
                );
3493
            }
3494
        );
3495
3496
        $this->assertEquals(
3497
            array(
3498
                array(
3499
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3500
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3501
                ),
3502
                array(
3503
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3504
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3505
                ),
3506
            ),
3507
            array(
3508
                array(
3509
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3510
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3511
                ),
3512
                array(
3513
                    'sourceContentInfo' => $relations[1]->sourceContentInfo->remoteId,
3514
                    'destinationContentInfo' => $relations[1]->destinationContentInfo->remoteId,
3515
                ),
3516
            )
3517
        );
3518
    }
3519
3520
    /**
3521
     * Test for the loadRelations() method.
3522
     *
3523
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3524
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3525
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3526
     */
3527
    public function testLoadRelationsSkipsArchivedContent()
3528
    {
3529
        $repository = $this->getRepository();
3530
3531
        $contentService = $repository->getContentService();
3532
3533
        /* BEGIN: Use Case */
3534
        $trashService = $repository->getTrashService();
3535
        $locationService = $repository->getLocationService();
3536
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3537
        // of a eZ Publish demo installation.
3538
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3539
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3540
3541
        $draft = $this->createContentDraftVersion1();
3542
3543
        // Load other content objects
3544
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3545
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3546
3547
        // Create relation between new content object and "Media" page
3548
        $contentService->addRelation(
3549
            $draft->getVersionInfo(),
3550
            $media
3551
        );
3552
3553
        // Create another relation with the "Demo Design" page
3554
        $contentService->addRelation(
3555
            $draft->getVersionInfo(),
3556
            $demoDesign
3557
        );
3558
3559
        $demoDesignLocation = $locationService->loadLocation($demoDesign->mainLocationId);
3560
3561
        // Trashing Content's last Location will change its status to archived,
3562
        // in this case relation towards it will not be loaded.
3563
        $trashService->trash($demoDesignLocation);
3564
3565
        // Load all relations
3566
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3567
        /* END: Use Case */
3568
3569
        $this->assertCount(1, $relations);
3570
        $this->assertEquals(
3571
            array(
3572
                array(
3573
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3574
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3575
                ),
3576
            ),
3577
            array(
3578
                array(
3579
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3580
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3581
                ),
3582
            )
3583
        );
3584
    }
3585
3586
    /**
3587
     * Test for the loadRelations() method.
3588
     *
3589
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3590
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3591
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3592
     */
3593
    public function testLoadRelationsSkipsDraftContent()
3594
    {
3595
        $repository = $this->getRepository();
3596
3597
        $contentService = $repository->getContentService();
3598
3599
        /* BEGIN: Use Case */
3600
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3601
        // of a eZ Publish demo installation.
3602
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3603
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3604
3605
        $draft = $this->createContentDraftVersion1();
3606
3607
        // Load other content objects
3608
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3609
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3610
3611
        // Create draft of "Media" page
3612
        $mediaDraft = $contentService->createContentDraft($media->contentInfo);
3613
3614
        // Create relation between "Media" page and new content object draft.
3615
        // This relation will not be loaded before the draft is published.
3616
        $contentService->addRelation(
3617
            $mediaDraft->getVersionInfo(),
3618
            $draft->getVersionInfo()->getContentInfo()
3619
        );
3620
3621
        // Create another relation with the "Demo Design" page
3622
        $contentService->addRelation(
3623
            $mediaDraft->getVersionInfo(),
3624
            $demoDesign
3625
        );
3626
3627
        // Load all relations
3628
        $relations = $contentService->loadRelations($mediaDraft->getVersionInfo());
3629
        /* END: Use Case */
3630
3631
        $this->assertCount(1, $relations);
3632
        $this->assertEquals(
3633
            array(
3634
                array(
3635
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3636
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3637
                ),
3638
            ),
3639
            array(
3640
                array(
3641
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3642
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3643
                ),
3644
            )
3645
        );
3646
    }
3647
3648
    /**
3649
     * Test for the loadReverseRelations() method.
3650
     *
3651
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3652
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3653
     */
3654
    public function testLoadReverseRelations()
3655
    {
3656
        $repository = $this->getRepository();
3657
3658
        $contentService = $repository->getContentService();
3659
3660
        /* BEGIN: Use Case */
3661
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3662
        // of a eZ Publish demo installation.
3663
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3664
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3665
3666
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3667
        $contentInfo = $versionInfo->getContentInfo();
3668
3669
        // Create some drafts
3670
        $mediaDraft = $contentService->createContentDraft(
3671
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3672
        );
3673
        $demoDesignDraft = $contentService->createContentDraft(
3674
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3675
        );
3676
3677
        // Create relation between new content object and "Media" page
3678
        $relation1 = $contentService->addRelation(
3679
            $mediaDraft->getVersionInfo(),
3680
            $contentInfo
3681
        );
3682
3683
        // Create another relation with the "Demo Design" page
3684
        $relation2 = $contentService->addRelation(
3685
            $demoDesignDraft->getVersionInfo(),
3686
            $contentInfo
3687
        );
3688
3689
        // Publish drafts, so relations become active
3690
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3691
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3692
3693
        // Load all relations
3694
        $relations = $contentService->loadRelations($versionInfo);
3695
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3696
        /* END: Use Case */
3697
3698
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3699
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3700
3701
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3702
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3703
3704
        $this->assertEquals(0, count($relations));
3705
        $this->assertEquals(2, count($reverseRelations));
3706
3707
        usort(
3708
            $reverseRelations,
3709
            function ($rel1, $rel2) {
3710
                return strcasecmp(
3711
                    $rel2->getSourceContentInfo()->remoteId,
3712
                    $rel1->getSourceContentInfo()->remoteId
3713
                );
3714
            }
3715
        );
3716
3717
        $this->assertEquals(
3718
            array(
3719
                array(
3720
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3721
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3722
                ),
3723
                array(
3724
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3725
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3726
                ),
3727
            ),
3728
            array(
3729
                array(
3730
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3731
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3732
                ),
3733
                array(
3734
                    'sourceContentInfo' => $reverseRelations[1]->sourceContentInfo->remoteId,
3735
                    'destinationContentInfo' => $reverseRelations[1]->destinationContentInfo->remoteId,
3736
                ),
3737
            )
3738
        );
3739
    }
3740
3741
    /**
3742
     * Test for the loadReverseRelations() method.
3743
     *
3744
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3745
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3746
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3747
     */
3748
    public function testLoadReverseRelationsSkipsArchivedContent()
3749
    {
3750
        $repository = $this->getRepository();
3751
3752
        $contentService = $repository->getContentService();
3753
3754
        /* BEGIN: Use Case */
3755
        $trashService = $repository->getTrashService();
3756
        $locationService = $repository->getLocationService();
3757
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3758
        // of a eZ Publish demo installation.
3759
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3760
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3761
3762
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3763
        $contentInfo = $versionInfo->getContentInfo();
3764
3765
        // Create some drafts
3766
        $mediaDraft = $contentService->createContentDraft(
3767
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3768
        );
3769
        $demoDesignDraft = $contentService->createContentDraft(
3770
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3771
        );
3772
3773
        // Create relation between new content object and "Media" page
3774
        $relation1 = $contentService->addRelation(
3775
            $mediaDraft->getVersionInfo(),
3776
            $contentInfo
3777
        );
3778
3779
        // Create another relation with the "Demo Design" page
3780
        $relation2 = $contentService->addRelation(
3781
            $demoDesignDraft->getVersionInfo(),
3782
            $contentInfo
3783
        );
3784
3785
        // Publish drafts, so relations become active
3786
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3787
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3788
3789
        $demoDesignLocation = $locationService->loadLocation($demoDesignDraft->contentInfo->mainLocationId);
3790
3791
        // Trashing Content's last Location will change its status to archived,
3792
        // in this case relation from it will not be loaded.
3793
        $trashService->trash($demoDesignLocation);
3794
3795
        // Load all relations
3796
        $relations = $contentService->loadRelations($versionInfo);
3797
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3798
        /* END: Use Case */
3799
3800
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3801
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3802
3803
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3804
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3805
3806
        $this->assertEquals(0, count($relations));
3807
        $this->assertEquals(1, count($reverseRelations));
3808
3809
        $this->assertEquals(
3810
            array(
3811
                array(
3812
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3813
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3814
                ),
3815
            ),
3816
            array(
3817
                array(
3818
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3819
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3820
                ),
3821
            )
3822
        );
3823
    }
3824
3825
    /**
3826
     * Test for the loadReverseRelations() method.
3827
     *
3828
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3829
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3830
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3831
     */
3832
    public function testLoadReverseRelationsSkipsDraftContent()
3833
    {
3834
        $repository = $this->getRepository();
3835
3836
        $contentService = $repository->getContentService();
3837
3838
        /* BEGIN: Use Case */
3839
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3840
        // of a eZ Publish demo installation.
3841
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3842
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3843
3844
        // Load "Media" page Content
3845
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3846
3847
        // Create some drafts
3848
        $newDraftVersionInfo = $this->createContentDraftVersion1()->getVersionInfo();
3849
        $demoDesignDraft = $contentService->createContentDraft(
3850
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3851
        );
3852
3853
        // Create relation between "Media" page and new content object
3854
        $relation1 = $contentService->addRelation(
3855
            $newDraftVersionInfo,
3856
            $media->contentInfo
3857
        );
3858
3859
        // Create another relation with the "Demo Design" page
3860
        $relation2 = $contentService->addRelation(
3861
            $demoDesignDraft->getVersionInfo(),
3862
            $media->contentInfo
3863
        );
3864
3865
        // Publish drafts, so relations become active
3866
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3867
        // We will not publish new Content draft, therefore relation from it
3868
        // will not be loaded as reverse relation for "Media" page
3869
        //$contentService->publishVersion( $newDraftVersionInfo );
3870
3871
        // Load all relations
3872
        $relations = $contentService->loadRelations($media->versionInfo);
3873
        $reverseRelations = $contentService->loadReverseRelations($media->contentInfo);
3874
        /* END: Use Case */
3875
3876
        $this->assertEquals($media->contentInfo->id, $relation1->getDestinationContentInfo()->id);
3877
        $this->assertEquals($newDraftVersionInfo->contentInfo->id, $relation1->getSourceContentInfo()->id);
3878
3879
        $this->assertEquals($media->contentInfo->id, $relation2->getDestinationContentInfo()->id);
3880
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3881
3882
        $this->assertEquals(0, count($relations));
3883
        $this->assertEquals(1, count($reverseRelations));
3884
3885
        $this->assertEquals(
3886
            array(
3887
                array(
3888
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3889
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3890
                ),
3891
            ),
3892
            array(
3893
                array(
3894
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3895
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3896
                ),
3897
            )
3898
        );
3899
    }
3900
3901
    /**
3902
     * Test for the deleteRelation() method.
3903
     *
3904
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3905
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3906
     */
3907
    public function testDeleteRelation()
3908
    {
3909
        $repository = $this->getRepository();
3910
3911
        $contentService = $repository->getContentService();
3912
3913
        /* BEGIN: Use Case */
3914
        // Remote ids of the "Media" and the "Demo Design" page of a eZ Publish
3915
        // demo installation.
3916
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3917
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3918
3919
        $draft = $this->createContentDraftVersion1();
3920
3921
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3922
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3923
3924
        // Establish some relations
3925
        $contentService->addRelation($draft->getVersionInfo(), $media);
3926
        $contentService->addRelation($draft->getVersionInfo(), $demoDesign);
3927
3928
        // Delete one of the currently created relations
3929
        $contentService->deleteRelation($draft->getVersionInfo(), $media);
3930
3931
        // The relations array now contains only one element
3932
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3933
        /* END: Use Case */
3934
3935
        $this->assertEquals(1, count($relations));
3936
    }
3937
3938
    /**
3939
     * Test for the deleteRelation() method.
3940
     *
3941
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3942
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3943
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3944
     */
3945 View Code Duplication
    public function testDeleteRelationThrowsBadStateException()
3946
    {
3947
        $repository = $this->getRepository();
3948
3949
        $contentService = $repository->getContentService();
3950
3951
        /* BEGIN: Use Case */
3952
        // RemoteId of the "Media" page of an eZ Publish demo installation
3953
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3954
3955
        $content = $this->createContentVersion1();
3956
3957
        // Load the destination object
3958
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3959
3960
        // Create a new draft
3961
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
3962
3963
        // Add a relation
3964
        $contentService->addRelation($draftVersion2->getVersionInfo(), $media);
3965
3966
        // Publish new version
3967
        $contentVersion2 = $contentService->publishVersion(
3968
            $draftVersion2->getVersionInfo()
3969
        );
3970
3971
        // This call will fail with a "BadStateException", because content is
3972
        // published and not a draft.
3973
        $contentService->deleteRelation(
3974
            $contentVersion2->getVersionInfo(),
3975
            $media
3976
        );
3977
        /* END: Use Case */
3978
    }
3979
3980
    /**
3981
     * Test for the deleteRelation() method.
3982
     *
3983
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3984
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
3985
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3986
     */
3987 View Code Duplication
    public function testDeleteRelationThrowsInvalidArgumentException()
3988
    {
3989
        $repository = $this->getRepository();
3990
3991
        $contentService = $repository->getContentService();
3992
3993
        /* BEGIN: Use Case */
3994
        // RemoteId of the "Media" page of an eZ Publish demo installation
3995
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3996
3997
        $draft = $this->createContentDraftVersion1();
3998
3999
        // Load the destination object
4000
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
4001
4002
        // This call will fail with a "InvalidArgumentException", because no
4003
        // relation exists between $draft and $media.
4004
        $contentService->deleteRelation(
4005
            $draft->getVersionInfo(),
4006
            $media
4007
        );
4008
        /* END: Use Case */
4009
    }
4010
4011
    /**
4012
     * Test for the createContent() method.
4013
     *
4014
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
4015
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4016
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4017
     */
4018
    public function testCreateContentInTransactionWithRollback()
4019
    {
4020
        if ($this->isVersion4()) {
4021
            $this->markTestSkipped('This test requires eZ Publish 5');
4022
        }
4023
4024
        $repository = $this->getRepository();
4025
4026
        /* BEGIN: Use Case */
4027
        $contentTypeService = $repository->getContentTypeService();
4028
        $contentService = $repository->getContentService();
4029
4030
        // Start a transaction
4031
        $repository->beginTransaction();
4032
4033
        try {
4034
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
4035
4036
            // Get a content create struct and set mandatory properties
4037
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
4038
            $contentCreate->setField('name', 'Sindelfingen forum');
4039
4040
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
4041
            $contentCreate->alwaysAvailable = true;
4042
4043
            // Create a new content object
4044
            $contentId = $contentService->createContent($contentCreate)->id;
4045
        } catch (Exception $e) {
4046
            // Cleanup hanging transaction on error
4047
            $repository->rollback();
4048
            throw $e;
4049
        }
4050
4051
        // Rollback all changes
4052
        $repository->rollback();
4053
4054
        try {
4055
            // This call will fail with a "NotFoundException"
4056
            $contentService->loadContent($contentId);
4057
        } catch (NotFoundException $e) {
4058
            // This is expected
4059
            return;
4060
        }
4061
        /* END: Use Case */
4062
4063
        $this->fail('Content object still exists after rollback.');
4064
    }
4065
4066
    /**
4067
     * Test for the createContent() method.
4068
     *
4069
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
4070
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4071
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4072
     */
4073
    public function testCreateContentInTransactionWithCommit()
4074
    {
4075
        if ($this->isVersion4()) {
4076
            $this->markTestSkipped('This test requires eZ Publish 5');
4077
        }
4078
4079
        $repository = $this->getRepository();
4080
4081
        /* BEGIN: Use Case */
4082
        $contentTypeService = $repository->getContentTypeService();
4083
        $contentService = $repository->getContentService();
4084
4085
        // Start a transaction
4086
        $repository->beginTransaction();
4087
4088
        try {
4089
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
4090
4091
            // Get a content create struct and set mandatory properties
4092
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
4093
            $contentCreate->setField('name', 'Sindelfingen forum');
4094
4095
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
4096
            $contentCreate->alwaysAvailable = true;
4097
4098
            // Create a new content object
4099
            $contentId = $contentService->createContent($contentCreate)->id;
4100
4101
            // Commit changes
4102
            $repository->commit();
4103
        } catch (Exception $e) {
4104
            // Cleanup hanging transaction on error
4105
            $repository->rollback();
4106
            throw $e;
4107
        }
4108
4109
        // Load the new content object
4110
        $content = $contentService->loadContent($contentId);
4111
        /* END: Use Case */
4112
4113
        $this->assertEquals($contentId, $content->id);
4114
    }
4115
4116
    /**
4117
     * Test for the createContent() method.
4118
     *
4119
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
4120
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
4121
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
4122
     */
4123
    public function testCreateContentWithLocationCreateParameterInTransactionWithRollback()
4124
    {
4125
        $repository = $this->getRepository();
4126
4127
        $contentService = $repository->getContentService();
4128
4129
        /* BEGIN: Use Case */
4130
        // Start a transaction
4131
        $repository->beginTransaction();
4132
4133
        try {
4134
            $draft = $this->createContentDraftVersion1();
4135
        } catch (Exception $e) {
4136
            // Cleanup hanging transaction on error
4137
            $repository->rollback();
4138
            throw $e;
4139
        }
4140
4141
        $contentId = $draft->id;
4142
4143
        // Roleback the transaction
4144
        $repository->rollback();
4145
4146
        try {
4147
            // This call will fail with a "NotFoundException"
4148
            $contentService->loadContent($contentId);
4149
        } catch (NotFoundException $e) {
4150
            return;
4151
        }
4152
        /* END: Use Case */
4153
4154
        $this->fail('Can still load content object after rollback.');
4155
    }
4156
4157
    /**
4158
     * Test for the createContent() method.
4159
     *
4160
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
4161
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
4162
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
4163
     */
4164 View Code Duplication
    public function testCreateContentWithLocationCreateParameterInTransactionWithCommit()
4165
    {
4166
        $repository = $this->getRepository();
4167
4168
        $contentService = $repository->getContentService();
4169
4170
        /* BEGIN: Use Case */
4171
        // Start a transaction
4172
        $repository->beginTransaction();
4173
4174
        try {
4175
            $draft = $this->createContentDraftVersion1();
4176
4177
            $contentId = $draft->id;
4178
4179
            // Roleback the transaction
4180
            $repository->commit();
4181
        } catch (Exception $e) {
4182
            // Cleanup hanging transaction on error
4183
            $repository->rollback();
4184
            throw $e;
4185
        }
4186
4187
        // Load the new content object
4188
        $content = $contentService->loadContent($contentId);
4189
        /* END: Use Case */
4190
4191
        $this->assertEquals($contentId, $content->id);
4192
    }
4193
4194
    /**
4195
     * Test for the createContentDraft() method.
4196
     *
4197
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4198
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4199
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4200
     */
4201
    public function testCreateContentDraftInTransactionWithRollback()
4202
    {
4203
        $repository = $this->getRepository();
4204
4205
        $contentId = $this->generateId('object', 12);
4206
        /* BEGIN: Use Case */
4207
        // $contentId is the ID of the "Administrator users" user group
4208
4209
        // Get the content service
4210
        $contentService = $repository->getContentService();
4211
4212
        // Load the user group content object
4213
        $content = $contentService->loadContent($contentId);
4214
4215
        // Start a new transaction
4216
        $repository->beginTransaction();
4217
4218
        try {
4219
            // Create a new draft
4220
            $drafted = $contentService->createContentDraft($content->contentInfo);
4221
4222
            // Store version number for later reuse
4223
            $versionNo = $drafted->versionInfo->versionNo;
4224
        } catch (Exception $e) {
4225
            // Cleanup hanging transaction on error
4226
            $repository->rollback();
4227
            throw $e;
4228
        }
4229
4230
        // Rollback
4231
        $repository->rollback();
4232
4233
        try {
4234
            // This call will fail with a "NotFoundException"
4235
            $contentService->loadContent($contentId, null, $versionNo);
4236
        } catch (NotFoundException $e) {
4237
            return;
4238
        }
4239
        /* END: Use Case */
4240
4241
        $this->fail('Can still load content draft after rollback');
4242
    }
4243
4244
    /**
4245
     * Test for the createContentDraft() method.
4246
     *
4247
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4248
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4249
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4250
     */
4251 View Code Duplication
    public function testCreateContentDraftInTransactionWithCommit()
4252
    {
4253
        $repository = $this->getRepository();
4254
4255
        $contentId = $this->generateId('object', 12);
4256
        /* BEGIN: Use Case */
4257
        // $contentId is the ID of the "Administrator users" user group
4258
4259
        // Get the content service
4260
        $contentService = $repository->getContentService();
4261
4262
        // Load the user group content object
4263
        $content = $contentService->loadContent($contentId);
4264
4265
        // Start a new transaction
4266
        $repository->beginTransaction();
4267
4268
        try {
4269
            // Create a new draft
4270
            $drafted = $contentService->createContentDraft($content->contentInfo);
4271
4272
            // Store version number for later reuse
4273
            $versionNo = $drafted->versionInfo->versionNo;
4274
4275
            // Commit all changes
4276
            $repository->commit();
4277
        } catch (Exception $e) {
4278
            // Cleanup hanging transaction on error
4279
            $repository->rollback();
4280
            throw $e;
4281
        }
4282
4283
        $content = $contentService->loadContent($contentId, null, $versionNo);
4284
        /* END: Use Case */
4285
4286
        $this->assertEquals(
4287
            $versionNo,
4288
            $content->getVersionInfo()->versionNo
4289
        );
4290
    }
4291
4292
    /**
4293
     * Test for the publishVersion() method.
4294
     *
4295
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4296
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4297
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4298
     */
4299 View Code Duplication
    public function testPublishVersionInTransactionWithRollback()
4300
    {
4301
        $repository = $this->getRepository();
4302
4303
        $contentId = $this->generateId('object', 12);
4304
        /* BEGIN: Use Case */
4305
        // $contentId is the ID of the "Administrator users" user group
4306
4307
        // Get the content service
4308
        $contentService = $repository->getContentService();
4309
4310
        // Load the user group content object
4311
        $content = $contentService->loadContent($contentId);
4312
4313
        // Start a new transaction
4314
        $repository->beginTransaction();
4315
4316
        try {
4317
            $draftVersion = $contentService->createContentDraft($content->contentInfo)->getVersionInfo();
4318
4319
            // Publish a new version
4320
            $content = $contentService->publishVersion($draftVersion);
4321
4322
            // Store version number for later reuse
4323
            $versionNo = $content->versionInfo->versionNo;
4324
        } catch (Exception $e) {
4325
            // Cleanup hanging transaction on error
4326
            $repository->rollback();
4327
            throw $e;
4328
        }
4329
4330
        // Rollback
4331
        $repository->rollback();
4332
4333
        try {
4334
            // This call will fail with a "NotFoundException"
4335
            $contentService->loadContent($contentId, null, $versionNo);
4336
        } catch (NotFoundException $e) {
4337
            return;
4338
        }
4339
        /* END: Use Case */
4340
4341
        $this->fail('Can still load content draft after rollback');
4342
    }
4343
4344
    /**
4345
     * Test for the publishVersion() method.
4346
     *
4347
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4348
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4349
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
4350
     */
4351 View Code Duplication
    public function testPublishVersionInTransactionWithCommit()
4352
    {
4353
        $repository = $this->getRepository();
4354
4355
        /* BEGIN: Use Case */
4356
        // ID of the "Administrator users" user group
4357
        $contentId = 12;
4358
4359
        // Get the content service
4360
        $contentService = $repository->getContentService();
4361
4362
        // Load the user group content object
4363
        $template = $contentService->loadContent($contentId);
4364
4365
        // Start a new transaction
4366
        $repository->beginTransaction();
4367
4368
        try {
4369
            // Publish a new version
4370
            $content = $contentService->publishVersion(
4371
                $contentService->createContentDraft($template->contentInfo)->getVersionInfo()
4372
            );
4373
4374
            // Store version number for later reuse
4375
            $versionNo = $content->versionInfo->versionNo;
4376
4377
            // Commit all changes
4378
            $repository->commit();
4379
        } catch (Exception $e) {
4380
            // Cleanup hanging transaction on error
4381
            $repository->rollback();
4382
            throw $e;
4383
        }
4384
4385
        // Load current version info
4386
        $versionInfo = $contentService->loadVersionInfo($content->contentInfo);
4387
        /* END: Use Case */
4388
4389
        $this->assertEquals($versionNo, $versionInfo->versionNo);
4390
    }
4391
4392
    /**
4393
     * Test for the updateContent() method.
4394
     *
4395
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4396
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4397
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4398
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4399
     */
4400 View Code Duplication
    public function testUpdateContentInTransactionWithRollback()
4401
    {
4402
        $repository = $this->getRepository();
4403
4404
        $contentId = $this->generateId('object', 12);
4405
        /* BEGIN: Use Case */
4406
        // $contentId is the ID of the "Administrator users" user group
4407
4408
        // Load content service
4409
        $contentService = $repository->getContentService();
4410
4411
        // Create a new user group draft
4412
        $draft = $contentService->createContentDraft(
4413
            $contentService->loadContentInfo($contentId)
4414
        );
4415
4416
        // Get an update struct and change the group name
4417
        $contentUpdate = $contentService->newContentUpdateStruct();
4418
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4419
4420
        // Start a transaction
4421
        $repository->beginTransaction();
4422
4423
        try {
4424
            // Update the group name
4425
            $draft = $contentService->updateContent(
4426
                $draft->getVersionInfo(),
4427
                $contentUpdate
4428
            );
4429
4430
            // Publish updated version
4431
            $contentService->publishVersion($draft->getVersionInfo());
4432
        } catch (Exception $e) {
4433
            // Cleanup hanging transaction on error
4434
            $repository->rollback();
4435
            throw $e;
4436
        }
4437
4438
        // Rollback all changes.
4439
        $repository->rollback();
4440
4441
        // Name will still be "Administrator users"
4442
        $name = $contentService->loadContent($contentId)->getFieldValue('name');
4443
        /* END: Use Case */
4444
4445
        $this->assertEquals('Administrator users', $name);
4446
    }
4447
4448
    /**
4449
     * Test for the updateContent() method.
4450
     *
4451
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4452
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4453
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4454
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4455
     */
4456 View Code Duplication
    public function testUpdateContentInTransactionWithCommit()
4457
    {
4458
        $repository = $this->getRepository();
4459
4460
        $contentId = $this->generateId('object', 12);
4461
        /* BEGIN: Use Case */
4462
        // $contentId is the ID of the "Administrator users" user group
4463
4464
        // Load content service
4465
        $contentService = $repository->getContentService();
4466
4467
        // Create a new user group draft
4468
        $draft = $contentService->createContentDraft(
4469
            $contentService->loadContentInfo($contentId)
4470
        );
4471
4472
        // Get an update struct and change the group name
4473
        $contentUpdate = $contentService->newContentUpdateStruct();
4474
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4475
4476
        // Start a transaction
4477
        $repository->beginTransaction();
4478
4479
        try {
4480
            // Update the group name
4481
            $draft = $contentService->updateContent(
4482
                $draft->getVersionInfo(),
4483
                $contentUpdate
4484
            );
4485
4486
            // Publish updated version
4487
            $contentService->publishVersion($draft->getVersionInfo());
4488
4489
            // Commit all changes.
4490
            $repository->commit();
4491
        } catch (Exception $e) {
4492
            // Cleanup hanging transaction on error
4493
            $repository->rollback();
4494
            throw $e;
4495
        }
4496
4497
        // Name is now "Administrators"
4498
        $name = $contentService->loadContent($contentId)->getFieldValue('name', 'eng-US');
4499
        /* END: Use Case */
4500
4501
        $this->assertEquals('Administrators', $name);
4502
    }
4503
4504
    /**
4505
     * Test for the updateContentMetadata() method.
4506
     *
4507
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4508
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4509
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4510
     */
4511 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithRollback()
4512
    {
4513
        $repository = $this->getRepository();
4514
4515
        $contentId = $this->generateId('object', 12);
4516
        /* BEGIN: Use Case */
4517
        // $contentId is the ID of the "Administrator users" user group
4518
4519
        // Get the content service
4520
        $contentService = $repository->getContentService();
4521
4522
        // Load a ContentInfo object
4523
        $contentInfo = $contentService->loadContentInfo($contentId);
4524
4525
        // Store remoteId for later testing
4526
        $remoteId = $contentInfo->remoteId;
4527
4528
        // Start a transaction
4529
        $repository->beginTransaction();
4530
4531
        try {
4532
            // Get metadata update struct and change remoteId
4533
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4534
            $metadataUpdate->remoteId = md5(microtime(true));
4535
4536
            // Update the metadata of the published content object
4537
            $contentService->updateContentMetadata(
4538
                $contentInfo,
4539
                $metadataUpdate
4540
            );
4541
        } catch (Exception $e) {
4542
            // Cleanup hanging transaction on error
4543
            $repository->rollback();
4544
            throw $e;
4545
        }
4546
4547
        // Rollback all changes.
4548
        $repository->rollback();
4549
4550
        // Load current remoteId
4551
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4552
        /* END: Use Case */
4553
4554
        $this->assertEquals($remoteId, $remoteIdReloaded);
4555
    }
4556
4557
    /**
4558
     * Test for the updateContentMetadata() method.
4559
     *
4560
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4561
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4562
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4563
     */
4564 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithCommit()
4565
    {
4566
        $repository = $this->getRepository();
4567
4568
        $contentId = $this->generateId('object', 12);
4569
        /* BEGIN: Use Case */
4570
        // $contentId is the ID of the "Administrator users" user group
4571
4572
        // Get the content service
4573
        $contentService = $repository->getContentService();
4574
4575
        // Load a ContentInfo object
4576
        $contentInfo = $contentService->loadContentInfo($contentId);
4577
4578
        // Store remoteId for later testing
4579
        $remoteId = $contentInfo->remoteId;
4580
4581
        // Start a transaction
4582
        $repository->beginTransaction();
4583
4584
        try {
4585
            // Get metadata update struct and change remoteId
4586
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4587
            $metadataUpdate->remoteId = md5(microtime(true));
4588
4589
            // Update the metadata of the published content object
4590
            $contentService->updateContentMetadata(
4591
                $contentInfo,
4592
                $metadataUpdate
4593
            );
4594
4595
            // Commit all changes.
4596
            $repository->commit();
4597
        } catch (Exception $e) {
4598
            // Cleanup hanging transaction on error
4599
            $repository->rollback();
4600
            throw $e;
4601
        }
4602
4603
        // Load current remoteId
4604
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4605
        /* END: Use Case */
4606
4607
        $this->assertNotEquals($remoteId, $remoteIdReloaded);
4608
    }
4609
4610
    /**
4611
     * Test for the deleteVersion() method.
4612
     *
4613
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4614
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4615
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4616
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4617
     */
4618
    public function testDeleteVersionInTransactionWithRollback()
4619
    {
4620
        $repository = $this->getRepository();
4621
4622
        $contentId = $this->generateId('object', 12);
4623
        /* BEGIN: Use Case */
4624
        // $contentId is the ID of the "Administrator users" user group
4625
4626
        // Get the content service
4627
        $contentService = $repository->getContentService();
4628
4629
        // Start a new transaction
4630
        $repository->beginTransaction();
4631
4632
        try {
4633
            // Create a new draft
4634
            $draft = $contentService->createContentDraft(
4635
                $contentService->loadContentInfo($contentId)
4636
            );
4637
4638
            $contentService->deleteVersion($draft->getVersionInfo());
4639
        } catch (Exception $e) {
4640
            // Cleanup hanging transaction on error
4641
            $repository->rollback();
4642
            throw $e;
4643
        }
4644
4645
        // Rollback all changes.
4646
        $repository->rollback();
4647
4648
        // This array will be empty
4649
        $drafts = $contentService->loadContentDrafts();
4650
        /* END: Use Case */
4651
4652
        $this->assertSame(array(), $drafts);
4653
    }
4654
4655
    /**
4656
     * Test for the deleteVersion() method.
4657
     *
4658
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4659
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4660
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4661
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4662
     */
4663
    public function testDeleteVersionInTransactionWithCommit()
4664
    {
4665
        $repository = $this->getRepository();
4666
4667
        $contentId = $this->generateId('object', 12);
4668
        /* BEGIN: Use Case */
4669
        // $contentId is the ID of the "Administrator users" user group
4670
4671
        // Get the content service
4672
        $contentService = $repository->getContentService();
4673
4674
        // Start a new transaction
4675
        $repository->beginTransaction();
4676
4677
        try {
4678
            // Create a new draft
4679
            $draft = $contentService->createContentDraft(
4680
                $contentService->loadContentInfo($contentId)
4681
            );
4682
4683
            $contentService->deleteVersion($draft->getVersionInfo());
4684
4685
            // Commit all changes.
4686
            $repository->commit();
4687
        } catch (Exception $e) {
4688
            // Cleanup hanging transaction on error
4689
            $repository->rollback();
4690
            throw $e;
4691
        }
4692
4693
        // This array will contain no element
4694
        $drafts = $contentService->loadContentDrafts();
4695
        /* END: Use Case */
4696
4697
        $this->assertSame(array(), $drafts);
4698
    }
4699
4700
    /**
4701
     * Test for the deleteContent() method.
4702
     *
4703
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4704
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4705
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4706
     */
4707
    public function testDeleteContentInTransactionWithRollback()
4708
    {
4709
        $repository = $this->getRepository();
4710
4711
        $contentId = $this->generateId('object', 11);
4712
        /* BEGIN: Use Case */
4713
        // $contentId is the ID of the "Members" user group in an eZ Publish
4714
        // demo installation
4715
4716
        // Get content service
4717
        $contentService = $repository->getContentService();
4718
4719
        // Load a ContentInfo instance
4720
        $contentInfo = $contentService->loadContentInfo($contentId);
4721
4722
        // Start a new transaction
4723
        $repository->beginTransaction();
4724
4725
        try {
4726
            // Delete content object
4727
            $contentService->deleteContent($contentInfo);
4728
        } catch (Exception $e) {
4729
            // Cleanup hanging transaction on error
4730
            $repository->rollback();
4731
            throw $e;
4732
        }
4733
4734
        // Rollback all changes
4735
        $repository->rollback();
4736
4737
        // This call will return the original content object
4738
        $contentInfo = $contentService->loadContentInfo($contentId);
4739
        /* END: Use Case */
4740
4741
        $this->assertEquals($contentId, $contentInfo->id);
4742
    }
4743
4744
    /**
4745
     * Test for the deleteContent() method.
4746
     *
4747
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4748
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4749
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4750
     */
4751
    public function testDeleteContentInTransactionWithCommit()
4752
    {
4753
        $repository = $this->getRepository();
4754
4755
        $contentId = $this->generateId('object', 11);
4756
        /* BEGIN: Use Case */
4757
        // $contentId is the ID of the "Members" user group in an eZ Publish
4758
        // demo installation
4759
4760
        // Get content service
4761
        $contentService = $repository->getContentService();
4762
4763
        // Load a ContentInfo instance
4764
        $contentInfo = $contentService->loadContentInfo($contentId);
4765
4766
        // Start a new transaction
4767
        $repository->beginTransaction();
4768
4769
        try {
4770
            // Delete content object
4771
            $contentService->deleteContent($contentInfo);
4772
4773
            // Commit all changes
4774
            $repository->commit();
4775
        } catch (Exception $e) {
4776
            // Cleanup hanging transaction on error
4777
            $repository->rollback();
4778
            throw $e;
4779
        }
4780
4781
        // Deleted content info is not found anymore
4782
        try {
4783
            $contentService->loadContentInfo($contentId);
4784
        } catch (NotFoundException $e) {
4785
            return;
4786
        }
4787
        /* END: Use Case */
4788
4789
        $this->fail('Can still load ContentInfo after commit.');
4790
    }
4791
4792
    /**
4793
     * Test for the copyContent() method.
4794
     *
4795
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4796
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4797
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4798
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4799
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4800
     */
4801 View Code Duplication
    public function testCopyContentInTransactionWithRollback()
4802
    {
4803
        $repository = $this->getRepository();
4804
4805
        $contentId = $this->generateId('object', 11);
4806
        $locationId = $this->generateId('location', 13);
4807
        /* BEGIN: Use Case */
4808
        // $contentId is the ID of the "Members" user group in an eZ Publish
4809
        // demo installation
4810
4811
        // $locationId is the ID of the "Administrator users" group location
4812
4813
        // Get services
4814
        $contentService = $repository->getContentService();
4815
        $locationService = $repository->getLocationService();
4816
4817
        // Load content object to copy
4818
        $content = $contentService->loadContent($contentId);
4819
4820
        // Create new target location
4821
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4822
4823
        // Start a new transaction
4824
        $repository->beginTransaction();
4825
4826
        try {
4827
            // Copy content with all versions and drafts
4828
            $contentService->copyContent(
4829
                $content->contentInfo,
4830
                $locationCreate
4831
            );
4832
        } catch (Exception $e) {
4833
            // Cleanup hanging transaction on error
4834
            $repository->rollback();
4835
            throw $e;
4836
        }
4837
4838
        // Rollback all changes
4839
        $repository->rollback();
4840
4841
        $this->refreshSearch($repository);
4842
4843
        // This array will only contain a single admin user object
4844
        $locations = $locationService->loadLocationChildren(
4845
            $locationService->loadLocation($locationId)
4846
        )->locations;
4847
        /* END: Use Case */
4848
4849
        $this->assertEquals(1, count($locations));
4850
    }
4851
4852
    /**
4853
     * Test for the copyContent() method.
4854
     *
4855
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4856
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4857
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4858
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4859
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4860
     */
4861 View Code Duplication
    public function testCopyContentInTransactionWithCommit()
4862
    {
4863
        $repository = $this->getRepository();
4864
4865
        $contentId = $this->generateId('object', 11);
4866
        $locationId = $this->generateId('location', 13);
4867
        /* BEGIN: Use Case */
4868
        // $contentId is the ID of the "Members" user group in an eZ Publish
4869
        // demo installation
4870
4871
        // $locationId is the ID of the "Administrator users" group location
4872
4873
        // Get services
4874
        $contentService = $repository->getContentService();
4875
        $locationService = $repository->getLocationService();
4876
4877
        // Load content object to copy
4878
        $content = $contentService->loadContent($contentId);
4879
4880
        // Create new target location
4881
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4882
4883
        // Start a new transaction
4884
        $repository->beginTransaction();
4885
4886
        try {
4887
            // Copy content with all versions and drafts
4888
            $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...
4889
                $content->contentInfo,
4890
                $locationCreate
4891
            );
4892
4893
            // Commit all changes
4894
            $repository->commit();
4895
        } catch (Exception $e) {
4896
            // Cleanup hanging transaction on error
4897
            $repository->rollback();
4898
            throw $e;
4899
        }
4900
4901
        $this->refreshSearch($repository);
4902
4903
        // This will contain the admin user and the new child location
4904
        $locations = $locationService->loadLocationChildren(
4905
            $locationService->loadLocation($locationId)
4906
        )->locations;
4907
        /* END: Use Case */
4908
4909
        $this->assertEquals(2, count($locations));
4910
    }
4911
4912
    public function testURLAliasesCreatedForNewContent()
4913
    {
4914
        $repository = $this->getRepository();
4915
4916
        $contentService = $repository->getContentService();
4917
        $locationService = $repository->getLocationService();
4918
        $urlAliasService = $repository->getURLAliasService();
4919
4920
        /* BEGIN: Use Case */
4921
        $draft = $this->createContentDraftVersion1();
4922
4923
        // Automatically creates a new URLAlias for the content
4924
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
4925
        /* END: Use Case */
4926
4927
        $location = $locationService->loadLocation(
4928
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4929
        );
4930
4931
        $aliases = $urlAliasService->listLocationAliases($location, false);
4932
4933
        $this->assertAliasesCorrect(
4934
            array(
4935
                '/Design/Plain-site/An-awesome-forum' => array(
4936
                    'type' => URLAlias::LOCATION,
4937
                    'destination' => $location->id,
4938
                    'path' => '/Design/Plain-site/An-awesome-forum',
4939
                    'languageCodes' => array('eng-US'),
4940
                    'isHistory' => false,
4941
                    'isCustom' => false,
4942
                    'forward' => false,
4943
                ),
4944
            ),
4945
            $aliases
4946
        );
4947
    }
4948
4949
    public function testURLAliasesCreatedForUpdatedContent()
4950
    {
4951
        $repository = $this->getRepository();
4952
4953
        $contentService = $repository->getContentService();
4954
        $locationService = $repository->getLocationService();
4955
        $urlAliasService = $repository->getURLAliasService();
4956
4957
        /* BEGIN: Use Case */
4958
        $draft = $this->createUpdatedDraftVersion2();
4959
4960
        $location = $locationService->loadLocation(
4961
            $draft->getVersionInfo()->getContentInfo()->mainLocationId
4962
        );
4963
4964
        // Load and assert URL aliases before publishing updated Content, so that
4965
        // SPI cache is warmed up and cache invalidation is also tested.
4966
        $aliases = $urlAliasService->listLocationAliases($location, false);
4967
4968
        $this->assertAliasesCorrect(
4969
            array(
4970
                '/Design/Plain-site/An-awesome-forum' => array(
4971
                    'type' => URLAlias::LOCATION,
4972
                    'destination' => $location->id,
4973
                    'path' => '/Design/Plain-site/An-awesome-forum',
4974
                    'languageCodes' => array('eng-US'),
4975
                    'alwaysAvailable' => true,
4976
                    'isHistory' => false,
4977
                    'isCustom' => false,
4978
                    'forward' => false,
4979
                ),
4980
            ),
4981
            $aliases
4982
        );
4983
4984
        // Automatically marks old aliases for the content as history
4985
        // and creates new aliases, based on the changes
4986
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
4987
        /* END: Use Case */
4988
4989
        $location = $locationService->loadLocation(
4990
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4991
        );
4992
4993
        $aliases = $urlAliasService->listLocationAliases($location, false);
4994
4995
        $this->assertAliasesCorrect(
4996
            array(
4997
                '/Design/Plain-site/An-awesome-forum2' => array(
4998
                    'type' => URLAlias::LOCATION,
4999
                    'destination' => $location->id,
5000
                    'path' => '/Design/Plain-site/An-awesome-forum2',
5001
                    'languageCodes' => array('eng-US'),
5002
                    'alwaysAvailable' => true,
5003
                    'isHistory' => false,
5004
                    'isCustom' => false,
5005
                    'forward' => false,
5006
                ),
5007
                '/Design/Plain-site/An-awesome-forum23' => array(
5008
                    'type' => URLAlias::LOCATION,
5009
                    'destination' => $location->id,
5010
                    'path' => '/Design/Plain-site/An-awesome-forum23',
5011
                    'languageCodes' => array('eng-GB'),
5012
                    'alwaysAvailable' => true,
5013
                    'isHistory' => false,
5014
                    'isCustom' => false,
5015
                    'forward' => false,
5016
                ),
5017
            ),
5018
            $aliases
5019
        );
5020
    }
5021
5022
    public function testCustomURLAliasesNotHistorizedOnUpdatedContent()
5023
    {
5024
        $repository = $this->getRepository();
5025
5026
        $contentService = $repository->getContentService();
5027
5028
        /* BEGIN: Use Case */
5029
        $urlAliasService = $repository->getURLAliasService();
5030
        $locationService = $repository->getLocationService();
5031
5032
        $content = $this->createContentVersion1();
5033
5034
        // Create a custom URL alias
5035
        $urlAliasService->createUrlAlias(
5036
            $locationService->loadLocation(
5037
                $content->getVersionInfo()->getContentInfo()->mainLocationId
5038
            ),
5039
            '/my/fancy/story-about-ez-publish',
5040
            'eng-US'
5041
        );
5042
5043
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
5044
5045
        $contentUpdate = $contentService->newContentUpdateStruct();
5046
        $contentUpdate->initialLanguageCode = 'eng-US';
5047
        $contentUpdate->setField('name', 'Amazing Bielefeld forum');
5048
5049
        $draftVersion2 = $contentService->updateContent(
5050
            $draftVersion2->getVersionInfo(),
5051
            $contentUpdate
5052
        );
5053
5054
        // Only marks auto-generated aliases as history
5055
        // the custom one is left untouched
5056
        $liveContent = $contentService->publishVersion($draftVersion2->getVersionInfo());
5057
        /* END: Use Case */
5058
5059
        $location = $locationService->loadLocation(
5060
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
5061
        );
5062
5063
        $aliases = $urlAliasService->listLocationAliases($location);
5064
5065
        $this->assertAliasesCorrect(
5066
            array(
5067
                '/my/fancy/story-about-ez-publish' => array(
5068
                    'type' => URLAlias::LOCATION,
5069
                    'destination' => $location->id,
5070
                    'path' => '/my/fancy/story-about-ez-publish',
5071
                    'languageCodes' => array('eng-US'),
5072
                    'isHistory' => false,
5073
                    'isCustom' => true,
5074
                    'forward' => false,
5075
                    'alwaysAvailable' => false,
5076
                ),
5077
            ),
5078
            $aliases
5079
        );
5080
    }
5081
5082
    /**
5083
     * Test to ensure that old versions are not affected by updates to newer
5084
     * drafts.
5085
     */
5086
    public function testUpdatingDraftDoesNotUpdateOldVersions()
5087
    {
5088
        $repository = $this->getRepository();
5089
5090
        $contentService = $repository->getContentService();
5091
5092
        $contentVersion2 = $this->createContentVersion2();
5093
5094
        $loadedContent1 = $contentService->loadContent($contentVersion2->id, null, 1);
5095
        $loadedContent2 = $contentService->loadContent($contentVersion2->id, null, 2);
5096
5097
        $this->assertNotEquals(
5098
            $loadedContent1->getFieldValue('name', 'eng-US'),
5099
            $loadedContent2->getFieldValue('name', 'eng-US')
5100
        );
5101
    }
5102
5103
    /**
5104
     * Test scenario with writer and publisher users.
5105
     * Writer can only create content. Publisher can publish this content.
5106
     */
5107
    public function testPublishWorkflow()
5108
    {
5109
        $repository = $this->getRepository();
5110
        $contentService = $repository->getContentService();
5111
5112
        $this->createRoleWithPolicies('Publisher', [
5113
            ['content', 'read'],
5114
            ['content', 'create'],
5115
            ['content', 'publish'],
5116
        ]);
5117
5118
        $this->createRoleWithPolicies('Writer', [
5119
            ['content', 'read'],
5120
            ['content', 'create'],
5121
        ]);
5122
5123
        $writerUser = $this->createCustomUserWithLogin(
5124
            'writer',
5125
            '[email protected]',
5126
            'Writers',
5127
            'Writer'
5128
        );
5129
5130
        $publisherUser = $this->createCustomUserWithLogin(
5131
            'publisher',
5132
            '[email protected]',
5133
            'Publishers',
5134
            'Publisher'
5135
        );
5136
5137
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5138
        $draft = $this->createContentDraftVersion1();
5139
5140
        $repository->getPermissionResolver()->setCurrentUserReference($publisherUser);
5141
        $content = $contentService->publishVersion($draft->versionInfo);
5142
5143
        $contentService->loadContent($content->id);
5144
    }
5145
5146
    /**
5147
     * Test publish / content policy is required to be able to publish content.
5148
     *
5149
     * @expectedException \eZ\Publish\Core\Base\Exceptions\UnauthorizedException
5150
     * @expectedExceptionMessageRegExp /User does not have access to 'publish' 'content'/
5151
     */
5152
    public function testPublishContentWithoutPublishPolicyThrowsException()
5153
    {
5154
        $repository = $this->getRepository();
5155
5156
        $this->createRoleWithPolicies('Writer', [
5157
            ['content', 'read'],
5158
            ['content', 'create'],
5159
            ['content', 'edit'],
5160
        ]);
5161
        $writerUser = $this->createCustomUserWithLogin(
5162
            'writer',
5163
            '[email protected]',
5164
            'Writers',
5165
            'Writer'
5166
        );
5167
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5168
5169
        $this->createContentVersion1();
5170
    }
5171
5172
    /**
5173
     * Test removal of the specific translation from all the Versions of a Content Object.
5174
     *
5175
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5176
     */
5177 View Code Duplication
    public function testDeleteTranslation()
5178
    {
5179
        $repository = $this->getRepository();
5180
        $contentService = $repository->getContentService();
5181
        $content = $this->createContentVersion2();
5182
5183
        // create multiple versions to exceed archive limit
5184
        for ($i = 0; $i < 5; ++$i) {
5185
            $contentDraft = $contentService->createContentDraft($content->contentInfo);
5186
            $contentUpdateStruct = $contentService->newContentUpdateStruct();
5187
            $contentDraft = $contentService->updateContent(
5188
                $contentDraft->versionInfo,
5189
                $contentUpdateStruct
5190
            );
5191
            $contentService->publishVersion($contentDraft->versionInfo);
5192
        }
5193
5194
        $contentService->deleteTranslation($content->contentInfo, 'eng-GB');
5195
5196
        $this->assertTranslationDoesNotExist('eng-GB', $content->id);
5197
    }
5198
5199
    /**
5200
     * Test deleting a Translation which is initial for some Version, updates initialLanguageCode
5201
     * with mainLanguageCode (assuming they are different).
5202
     */
5203
    public function testDeleteTranslationUpdatesInitialLanguageCodeVersion()
5204
    {
5205
        $repository = $this->getRepository();
5206
        $contentService = $repository->getContentService();
5207
5208
        $content = $this->createContentVersion2();
5209
        // create another, copied, version
5210
        $contentDraft = $contentService->updateContent(
5211
            $contentService->createContentDraft($content->contentInfo)->versionInfo,
5212
            $contentService->newContentUpdateStruct()
5213
        );
5214
        $publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
5215
5216
        // remove first version with only one translation as it is not the subject of this test
5217
        $contentService->deleteVersion(
5218
            $contentService->loadVersionInfo($publishedContent->contentInfo, 1)
5219
        );
5220
5221
        // sanity check
5222
        self::assertEquals('eng-US', $content->contentInfo->mainLanguageCode);
5223
        self::assertEquals('eng-US', $content->versionInfo->initialLanguageCode);
5224
5225
        // update mainLanguageCode so it is different than initialLanguageCode for Version
5226
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
5227
        $contentMetadataUpdateStruct->mainLanguageCode = 'eng-GB';
5228
        $content = $contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
5229
5230
        $contentService->deleteTranslation($content->contentInfo, 'eng-US');
5231
5232
        $this->assertTranslationDoesNotExist('eng-US', $content->id);
5233
    }
5234
5235
    /**
5236
     * Test removal of the specific translation properly updates languages of the URL alias.
5237
     *
5238
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5239
     */
5240
    public function testDeleteTranslationUpdatesUrlAlias()
5241
    {
5242
        $repository = $this->getRepository();
5243
        $contentService = $repository->getContentService();
5244
        $locationService = $repository->getLocationService();
5245
        $urlAliasService = $repository->getURLAliasService();
5246
5247
        $content = $this->createContentVersion2();
5248
        $mainLocation = $locationService->loadLocation($content->contentInfo->mainLocationId);
5249
5250
        // create custom URL alias for Content main Location
5251
        $urlAliasService->createUrlAlias($mainLocation, '/my-custom-url', 'eng-GB');
5252
5253
        // create secondary Location for Content
5254
        $secondaryLocation = $locationService->createLocation(
5255
            $content->contentInfo,
5256
            $locationService->newLocationCreateStruct(2)
5257
        );
5258
5259
        // create custom URL alias for Content secondary Location
5260
        $urlAliasService->createUrlAlias($secondaryLocation, '/my-secondary-url', 'eng-GB');
5261
5262
        // delete Translation
5263
        $contentService->deleteTranslation($content->contentInfo, 'eng-GB');
5264
5265
        foreach ([$mainLocation, $secondaryLocation] as $location) {
5266
            // check auto-generated URL aliases
5267
            foreach ($urlAliasService->listLocationAliases($location, false) as $alias) {
5268
                self::assertNotContains('eng-GB', $alias->languageCodes);
5269
            }
5270
5271
            // check custom URL aliases
5272
            foreach ($urlAliasService->listLocationAliases($location) as $alias) {
5273
                self::assertNotContains('eng-GB', $alias->languageCodes);
5274
            }
5275
        }
5276
    }
5277
5278
    /**
5279
     * Test removal of a main translation throws BadStateException.
5280
     *
5281
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5282
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5283
     * @expectedExceptionMessage Specified translation is the main translation of the Content Object
5284
     */
5285
    public function testDeleteTranslationMainLanguageThrowsBadStateException()
5286
    {
5287
        $repository = $this->getRepository();
5288
        $contentService = $repository->getContentService();
5289
        $content = $this->createContentVersion2();
5290
5291
        // delete first version which has only one translation
5292
        $contentService->deleteVersion($contentService->loadVersionInfo($content->contentInfo, 1));
5293
5294
        // try to delete main translation
5295
        $contentService->deleteTranslation($content->contentInfo, $content->contentInfo->mainLanguageCode);
5296
    }
5297
5298
    /**
5299
     * Test removal of a Translation is possible when some archived Versions have only this Translation.
5300
     *
5301
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5302
     */
5303
    public function testDeleteTranslationDeletesSingleTranslationVersions()
5304
    {
5305
        $repository = $this->getRepository();
5306
        $contentService = $repository->getContentService();
5307
        // content created by the createContentVersion1 method has eng-US translation only.
5308
        $content = $this->createContentVersion1();
5309
5310
        // create new version and add eng-GB translation
5311
        $contentDraft = $contentService->createContentDraft($content->contentInfo);
5312
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
5313
        $contentUpdateStruct->setField('name', 'Awesome Board', 'eng-GB');
5314
        $contentDraft = $contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
5315
        $publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
5316
5317
        // update mainLanguageCode to avoid exception related to that
5318
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
5319
        $contentMetadataUpdateStruct->mainLanguageCode = 'eng-GB';
5320
5321
        $content = $contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
5322
5323
        $contentService->deleteTranslation($content->contentInfo, 'eng-US');
5324
5325
        $this->assertTranslationDoesNotExist('eng-US', $content->id);
5326
    }
5327
5328
    /**
5329
     * Test removal of the translation by the user who is not allowed to delete a content
5330
     * throws UnauthorizedException.
5331
     *
5332
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5333
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5334
     * @expectedExceptionMessage User does not have access to 'remove' 'content'
5335
     */
5336
    public function testDeleteTranslationThrowsUnauthorizedException()
5337
    {
5338
        $repository = $this->getRepository();
5339
        $contentService = $repository->getContentService();
5340
5341
        $content = $this->createContentVersion2();
5342
5343
        // create user that can read/create/edit but cannot delete content
5344
        $this->createRoleWithPolicies('Writer', [
5345
            ['content', 'read'],
5346
            ['content', 'versionread'],
5347
            ['content', 'create'],
5348
            ['content', 'edit'],
5349
        ]);
5350
        $writerUser = $this->createCustomUserWithLogin(
5351
            'writer',
5352
            '[email protected]',
5353
            'Writers',
5354
            'Writer'
5355
        );
5356
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5357
        $contentService->deleteTranslation($content->contentInfo, 'eng-GB');
5358
    }
5359
5360
    /**
5361
     * Test removal of a non-existent translation throws InvalidArgumentException.
5362
     *
5363
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5364
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
5365
     * @expectedExceptionMessage Argument '$languageCode' is invalid: ger-DE does not exist in the Content item
5366
     */
5367
    public function testDeleteTranslationThrowsInvalidArgumentException()
5368
    {
5369
        $repository = $this->getRepository();
5370
        $contentService = $repository->getContentService();
5371
        // content created by the createContentVersion1 method has eng-US translation only.
5372
        $content = $this->createContentVersion1();
5373
        $contentService->deleteTranslation($content->contentInfo, 'ger-DE');
5374
    }
5375
5376
    /**
5377
     * Test deleting a Translation from Draft.
5378
     *
5379
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5380
     */
5381
    public function testDeleteTranslationFromDraft()
5382
    {
5383
        $repository = $this->getRepository();
5384
        $contentService = $repository->getContentService();
5385
5386
        $languageCode = 'eng-GB';
5387
        $content = $this->createMultipleLanguageContentVersion2();
5388
        $draft = $contentService->createContentDraft($content->contentInfo);
5389
        $draft = $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5390
        $content = $contentService->publishVersion($draft->versionInfo);
5391
5392
        $loadedContent = $contentService->loadContent($content->id);
5393
        self::assertNotContains($languageCode, $loadedContent->versionInfo->languageCodes);
5394
        self::assertEmpty($loadedContent->getFieldsByLanguage($languageCode));
5395
    }
5396
5397
    /**
5398
     * Get values for multilingual field.
5399
     *
5400
     * @return array
5401
     */
5402
    public function providerForDeleteTranslationFromDraftRemovesUrlAliasOnPublishing()
5403
    {
5404
        return [
5405
            [
5406
                ['eng-US' => 'US Name', 'eng-GB' => 'GB Name'],
5407
            ],
5408
            [
5409
                ['eng-US' => 'Same Name', 'eng-GB' => 'Same Name'],
5410
            ],
5411
        ];
5412
    }
5413
5414
    /**
5415
     * Test deleting a Translation from Draft removes previously stored URL aliases for published Content.
5416
     *
5417
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5418
     *
5419
     * @dataProvider providerForDeleteTranslationFromDraftRemovesUrlAliasOnPublishing
5420
     *
5421
     * @param string[] $fieldValues translated field values
5422
     */
5423
    public function testDeleteTranslationFromDraftRemovesUrlAliasOnPublishing(array $fieldValues)
5424
    {
5425
        $repository = $this->getRepository();
5426
        $contentService = $repository->getContentService();
5427
        $locationService = $repository->getLocationService();
5428
        $urlAliasService = $repository->getURLAliasService();
5429
5430
        // set language code to be removed
5431
        $languageCode = 'eng-GB';
5432
        $draft = $this->createMultilingualContentDraft(
5433
            'folder',
5434
            2,
5435
            'eng-US',
5436
            [
5437
                'name' => [
5438
                    'eng-GB' => $fieldValues['eng-GB'],
5439
                    'eng-US' => $fieldValues['eng-US'],
5440
                ],
5441
            ]
5442
        );
5443
        $content = $contentService->publishVersion($draft->versionInfo);
5444
5445
        // create secondary location
5446
        $locationService->createLocation(
5447
            $content->contentInfo,
5448
            $locationService->newLocationCreateStruct(5)
5449
        );
5450
5451
        // sanity check
5452
        $locations = $locationService->loadLocations($content->contentInfo);
5453
        self::assertCount(2, $locations, 'Sanity check: Expected to find 2 Locations');
5454
        foreach ($locations as $location) {
5455
            $urlAliasService->createUrlAlias($location, '/us-custom_' . $location->id, 'eng-US');
5456
            $urlAliasService->createUrlAlias($location, '/gb-custom_' . $location->id, 'eng-GB');
5457
5458
            // check default URL aliases
5459
            $aliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
5460
            self::assertNotEmpty($aliases, 'Sanity check: URL alias for the translation does not exist');
5461
5462
            // check custom URL aliases
5463
            $aliases = $urlAliasService->listLocationAliases($location, true, $languageCode);
5464
            self::assertNotEmpty($aliases, 'Sanity check: Custom URL alias for the translation does not exist');
5465
        }
5466
5467
        // delete translation and publish new version
5468
        $draft = $contentService->createContentDraft($content->contentInfo);
5469
        $draft = $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5470
        $contentService->publishVersion($draft->versionInfo);
5471
5472
        // check that aliases does not exist
5473
        foreach ($locations as $location) {
5474
            // check default URL aliases
5475
            $aliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
5476
            self::assertEmpty($aliases, 'URL alias for the deleted translation still exists');
5477
5478
            // check custom URL aliases
5479
            $aliases = $urlAliasService->listLocationAliases($location, true, $languageCode);
5480
            self::assertEmpty($aliases, 'Custom URL alias for the deleted translation still exists');
5481
        }
5482
    }
5483
5484
    /**
5485
     * Test deleting a Translation from Draft which has single Translation throws BadStateException.
5486
     *
5487
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5488
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5489
     * @expectedExceptionMessage Specified Translation is the only one Content Object Version has
5490
     */
5491
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnSingleTranslation()
5492
    {
5493
        $repository = $this->getRepository();
5494
        $contentService = $repository->getContentService();
5495
5496
        // create Content with single Translation
5497
        $publishedContent = $contentService->publishVersion(
5498
            $this->createContentDraft(
5499
                'forum',
5500
                2,
5501
                ['name' => 'Eng-US Version name']
5502
            )->versionInfo
5503
        );
5504
5505
        // update mainLanguageCode to avoid exception related to trying to delete main Translation
5506
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
5507
        $contentMetadataUpdateStruct->mainLanguageCode = 'eng-GB';
5508
        $publishedContent = $contentService->updateContentMetadata(
5509
            $publishedContent->contentInfo,
5510
            $contentMetadataUpdateStruct
5511
        );
5512
5513
        // create single Translation Version from the first one
5514
        $draft = $contentService->createContentDraft(
5515
            $publishedContent->contentInfo,
5516
            $publishedContent->versionInfo
5517
        );
5518
5519
        // attempt to delete Translation
5520
        $contentService->deleteTranslationFromDraft($draft->versionInfo, 'eng-US');
5521
    }
5522
5523
    /**
5524
     * Test deleting the Main Translation from Draft throws BadStateException.
5525
     *
5526
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5527
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5528
     * @expectedExceptionMessage Specified Translation is the main Translation of the Content Object
5529
     */
5530
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnMainTranslation()
5531
    {
5532
        $repository = $this->getRepository();
5533
        $contentService = $repository->getContentService();
5534
5535
        $mainLanguageCode = 'eng-US';
5536
        $draft = $this->createMultilingualContentDraft(
5537
            'forum',
5538
            2,
5539
            $mainLanguageCode,
5540
            [
5541
                'name' => [
5542
                    'eng-US' => 'An awesome eng-US forum',
5543
                    'eng-GB' => 'An awesome eng-GB forum',
5544
                ],
5545
            ]
5546
        );
5547
        $contentService->deleteTranslationFromDraft($draft->versionInfo, $mainLanguageCode);
5548
    }
5549
5550
    /**
5551
     * Test deleting the Translation from Published Version throws BadStateException.
5552
     *
5553
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5554
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5555
     * @expectedExceptionMessage Version is not a draft
5556
     */
5557 View Code Duplication
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnPublishedVersion()
5558
    {
5559
        $repository = $this->getRepository();
5560
        $contentService = $repository->getContentService();
5561
5562
        $languageCode = 'eng-US';
5563
        $content = $this->createMultipleLanguageContentVersion2();
5564
        $draft = $contentService->createContentDraft($content->contentInfo);
5565
        $publishedContent = $contentService->publishVersion($draft->versionInfo);
5566
        $contentService->deleteTranslationFromDraft($publishedContent->versionInfo, $languageCode);
5567
    }
5568
5569
    /**
5570
     * Test deleting a Translation from Draft throws UnauthorizedException if user cannot edit Content.
5571
     *
5572
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5573
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5574
     * @expectedExceptionMessage User does not have access to 'edit' 'content'
5575
     */
5576
    public function testDeleteTranslationFromDraftThrowsUnauthorizedException()
5577
    {
5578
        $repository = $this->getRepository();
5579
        $contentService = $repository->getContentService();
5580
5581
        $languageCode = 'eng-GB';
5582
        $content = $this->createMultipleLanguageContentVersion2();
5583
        $draft = $contentService->createContentDraft($content->contentInfo);
5584
5585
        // create user that can read/create/delete but cannot edit or content
5586
        $this->createRoleWithPolicies('Writer', [
5587
            ['content', 'read'],
5588
            ['content', 'versionread'],
5589
            ['content', 'create'],
5590
            ['content', 'delete'],
5591
        ]);
5592
        $writerUser = $this->createCustomUserWithLogin(
5593
            'user',
5594
            '[email protected]',
5595
            'Writers',
5596
            'Writer'
5597
        );
5598
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5599
5600
        $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5601
    }
5602
5603
    /**
5604
     * Test deleting a non-existent Translation from Draft throws InvalidArgumentException.
5605
     *
5606
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5607
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
5608
     * @expectedExceptionMessageRegExp /The Version \(ContentId=\d+, VersionNo=\d+\) is not translated into ger-DE/
5609
     */
5610
    public function testDeleteTranslationFromDraftThrowsInvalidArgumentException()
5611
    {
5612
        $repository = $this->getRepository();
5613
        $contentService = $repository->getContentService();
5614
5615
        $languageCode = 'ger-DE';
5616
        $content = $this->createMultipleLanguageContentVersion2();
5617
        $draft = $contentService->createContentDraft($content->contentInfo);
5618
        $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5619
    }
5620
5621
    /**
5622
     * Simplify creating custom role with limited set of policies.
5623
     *
5624
     * @param $roleName
5625
     * @param array $policies e.g. [ ['content', 'create'], ['content', 'edit'], ]
5626
     */
5627
    private function createRoleWithPolicies($roleName, array $policies)
5628
    {
5629
        $repository = $this->getRepository();
5630
        $roleService = $repository->getRoleService();
5631
5632
        $roleCreateStruct = $roleService->newRoleCreateStruct($roleName);
5633
        foreach ($policies as $policy) {
5634
            $policyCreateStruct = $roleService->newPolicyCreateStruct($policy[0], $policy[1]);
5635
            $roleCreateStruct->addPolicy($policyCreateStruct);
5636
        }
5637
5638
        $roleDraft = $roleService->createRole($roleCreateStruct);
5639
        $roleService->publishRoleDraft($roleDraft);
5640
    }
5641
5642
    /**
5643
     * Asserts that all aliases defined in $expectedAliasProperties with the
5644
     * given properties are available in $actualAliases and not more.
5645
     *
5646
     * @param array $expectedAliasProperties
5647
     * @param array $actualAliases
5648
     */
5649
    private function assertAliasesCorrect(array $expectedAliasProperties, array $actualAliases)
5650
    {
5651
        foreach ($actualAliases as $actualAlias) {
5652
            if (!isset($expectedAliasProperties[$actualAlias->path])) {
5653
                $this->fail(
5654
                    sprintf(
5655
                        'Alias with path "%s" in languages "%s" not expected.',
5656
                        $actualAlias->path,
5657
                        implode(', ', $actualAlias->languageCodes)
5658
                    )
5659
                );
5660
            }
5661
5662
            foreach ($expectedAliasProperties[$actualAlias->path] as $propertyName => $propertyValue) {
5663
                $this->assertEquals(
5664
                    $propertyValue,
5665
                    $actualAlias->$propertyName,
5666
                    sprintf(
5667
                        'Property $%s incorrect on alias with path "%s" in languages "%s".',
5668
                        $propertyName,
5669
                        $actualAlias->path,
5670
                        implode(', ', $actualAlias->languageCodes)
5671
                    )
5672
                );
5673
            }
5674
5675
            unset($expectedAliasProperties[$actualAlias->path]);
5676
        }
5677
5678
        if (!empty($expectedAliasProperties)) {
5679
            $this->fail(
5680
                sprintf(
5681
                    'Missing expected aliases with paths "%s".',
5682
                    implode('", "', array_keys($expectedAliasProperties))
5683
                )
5684
            );
5685
        }
5686
    }
5687
5688
    /**
5689
     * Asserts that the given fields are equal to the default fields fixture.
5690
     *
5691
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5692
     */
5693
    private function assertAllFieldsEquals(array $fields)
5694
    {
5695
        $actual = $this->normalizeFields($fields);
5696
        $expected = $this->normalizeFields($this->createFieldsFixture());
5697
5698
        $this->assertEquals($expected, $actual);
5699
    }
5700
5701
    /**
5702
     * Asserts that the given fields are equal to a language filtered set of the
5703
     * default fields fixture.
5704
     *
5705
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5706
     * @param string $languageCode
5707
     */
5708
    private function assertLocaleFieldsEquals(array $fields, $languageCode)
5709
    {
5710
        $actual = $this->normalizeFields($fields);
5711
5712
        $expected = array();
5713
        foreach ($this->normalizeFields($this->createFieldsFixture()) as $field) {
5714
            if ($field->languageCode !== $languageCode) {
5715
                continue;
5716
            }
5717
            $expected[] = $field;
5718
        }
5719
5720
        $this->assertEquals($expected, $actual);
5721
    }
5722
5723
    /**
5724
     * This method normalizes a set of fields and returns a normalized set.
5725
     *
5726
     * Normalization means it resets the storage specific field id to zero and
5727
     * it sorts the field by their identifier and their language code. In
5728
     * addition, the field value is removed, since this one depends on the
5729
     * specific FieldType, which is tested in a dedicated integration test.
5730
     *
5731
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5732
     *
5733
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5734
     */
5735
    private function normalizeFields(array $fields)
5736
    {
5737
        $normalized = array();
5738 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...
5739
            $normalized[] = new Field(
5740
                array(
5741
                    'id' => 0,
5742
                    'value' => ($field->value !== null ? true : null),
0 ignored issues
show
Documentation introduced by
The property $value is declared protected in eZ\Publish\API\Repository\Values\Content\Field. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
5743
                    'languageCode' => $field->languageCode,
5744
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
5745
                    'fieldTypeIdentifier' => $field->fieldTypeIdentifier,
5746
                )
5747
            );
5748
        }
5749
        usort(
5750
            $normalized,
5751 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...
5752
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
5753
                    return strcasecmp($field1->languageCode, $field2->languageCode);
5754
                }
5755
5756
                return $return;
5757
            }
5758
        );
5759
5760
        return $normalized;
5761
    }
5762
5763
    /**
5764
     * Returns a filtered set of the default fields fixture.
5765
     *
5766
     * @param string $languageCode
5767
     *
5768
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5769
     */
5770
    private function createLocaleFieldsFixture($languageCode)
5771
    {
5772
        $fields = array();
5773
        foreach ($this->createFieldsFixture() as $field) {
5774
            if (null === $field->languageCode || $languageCode === $field->languageCode) {
5775
                $fields[] = $field;
5776
            }
5777
        }
5778
5779
        return $fields;
5780
    }
5781
5782
    /**
5783
     * Asserts that given Content has default ContentStates.
5784
     *
5785
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
5786
     */
5787 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
5788
    {
5789
        $repository = $this->getRepository();
5790
        $objectStateService = $repository->getObjectStateService();
5791
5792
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
5793
5794
        foreach ($objectStateGroups as $objectStateGroup) {
5795
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
5796
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
5797
                // Only check the first object state which is the default one.
5798
                $this->assertEquals(
5799
                    $objectState,
5800
                    $contentState
5801
                );
5802
                break;
5803
            }
5804
        }
5805
    }
5806
5807
    /**
5808
     * Assert that given Content has no references to a translation specified by the $languageCode.
5809
     *
5810
     * @param string $languageCode
5811
     * @param int $contentId
5812
     */
5813
    private function assertTranslationDoesNotExist($languageCode, $contentId)
5814
    {
5815
        $repository = $this->getRepository();
5816
        $contentService = $repository->getContentService();
5817
5818
        $content = $contentService->loadContent($contentId);
5819
5820
        foreach ($content->fields as $fieldIdentifier => $field) {
5821
            /** @var array $field */
5822
            self::assertArrayNotHasKey($languageCode, $field);
5823
            self::assertNotEquals($languageCode, $content->contentInfo->mainLanguageCode);
5824
            self::assertArrayNotHasKey($languageCode, $content->versionInfo->getNames());
5825
            self::assertNotEquals($languageCode, $content->versionInfo->initialLanguageCode);
5826
            self::assertNotContains($languageCode, $content->versionInfo->languageCodes);
5827
        }
5828
        foreach ($contentService->loadVersions($content->contentInfo) as $versionInfo) {
5829
            self::assertArrayNotHasKey($languageCode, $versionInfo->getNames());
5830
            self::assertNotEquals($languageCode, $versionInfo->contentInfo->mainLanguageCode);
5831
            self::assertNotEquals($languageCode, $versionInfo->initialLanguageCode);
5832
            self::assertNotContains($languageCode, $versionInfo->languageCodes);
5833
        }
5834
    }
5835
5836
    /**
5837
     * Returns the default fixture of fields used in most tests.
5838
     *
5839
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5840
     */
5841
    private function createFieldsFixture()
5842
    {
5843
        return array(
5844
            new Field(
5845
                array(
5846
                    'id' => 0,
5847
                    'value' => 'Foo',
5848
                    'languageCode' => 'eng-US',
5849
                    'fieldDefIdentifier' => 'description',
5850
                    'fieldTypeIdentifier' => 'ezrichtext',
5851
                )
5852
            ),
5853
            new Field(
5854
                array(
5855
                    'id' => 0,
5856
                    'value' => 'Bar',
5857
                    'languageCode' => 'eng-GB',
5858
                    'fieldDefIdentifier' => 'description',
5859
                    'fieldTypeIdentifier' => 'ezrichtext',
5860
                )
5861
            ),
5862
            new Field(
5863
                array(
5864
                    'id' => 0,
5865
                    'value' => 'An awesome multi-lang forum²',
5866
                    'languageCode' => 'eng-US',
5867
                    'fieldDefIdentifier' => 'name',
5868
                    'fieldTypeIdentifier' => 'ezstring',
5869
                )
5870
            ),
5871
            new Field(
5872
                array(
5873
                    'id' => 0,
5874
                    'value' => 'An awesome multi-lang forum²³',
5875
                    'languageCode' => 'eng-GB',
5876
                    'fieldDefIdentifier' => 'name',
5877
                    'fieldTypeIdentifier' => 'ezstring',
5878
                )
5879
            ),
5880
        );
5881
    }
5882
5883
    /**
5884
     * Gets expected property values for the "Media" ContentInfo ValueObject.
5885
     *
5886
     * @return array
5887
     */
5888 View Code Duplication
    private function getExpectedMediaContentInfoProperties()
5889
    {
5890
        return [
5891
            'id' => 41,
5892
            'contentTypeId' => 1,
5893
            'name' => 'Media',
5894
            'sectionId' => 3,
5895
            'currentVersionNo' => 1,
5896
            'published' => true,
5897
            'ownerId' => 14,
5898
            'modificationDate' => $this->createDateTime(1060695457),
5899
            'publishedDate' => $this->createDateTime(1060695457),
5900
            'alwaysAvailable' => 1,
5901
            'remoteId' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
5902
            'mainLanguageCode' => 'eng-US',
5903
            'mainLocationId' => 43,
5904
        ];
5905
    }
5906
}
5907