Completed
Push — master ( cf2d59...36802d )
by André
16:28
created

ContentServiceTest   F

Complexity

Total Complexity 206

Size/Duplication

Total Lines 5368
Duplicated Lines 23.17 %

Coupling/Cohesion

Components 1
Dependencies 29

Importance

Changes 0
Metric Value
dl 1244
loc 5368
rs 0.5217
c 0
b 0
f 0
wmc 206
lcom 1
cbo 29

149 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 17 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
A testLoadVersionInfoByIdSetsExpectedVersionInfo() 0 22 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 15 1
B testPublishVersionSetsExpectedContentInfo() 0 30 1
B testPublishVersionSetsExpectedVersionInfo() 0 25 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
A testCreateContentDraftSetsVersionInfo() 0 21 1
A testNewContentUpdateStruct() 0 23 1
A testUpdateContent() 0 19 1
A testUpdateContentWithDifferentUser() 0 19 1
B testUpdateContentSetsExpectedFields() 0 41 1
A testUpdateContentThrowsInvalidArgumentExceptionWhenFieldTypeDoesNotAccept() 22 22 1
B testUpdateContentWhenMandatoryFieldIsEmpty() 24 24 1
B testUpdateContentThrowsContentFieldValidationException() 0 24 1
B testUpdateContentWithNotUpdatingMandatoryField() 0 38 5
A testCreateContentDraftWithThirdParameter() 20 20 1
A testPublishVersionFromContentDraft() 23 23 1
A testPublishVersionFromContentDraftArchivesOldVersion() 23 23 1
A testPublishVersionFromContentDraftUpdatesContentInfoCurrentVersion() 0 8 1
B testPublishVersionFromOldContentDraftArchivesNewerVersionNo() 0 25 1
A testPublishVersionNotCreatingUnlimitedArchives() 0 57 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
B testLoadContentDraftsWithFirstParameter() 0 45 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 testLoadContentByVersionInfoWithSecondParameter() 17 82 4
B testLoadContentByContentInfoWithLanguageParameters() 0 129 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 19 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 36 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 18 1
B testPublishWorkflow() 0 38 1
A testPublishContentWithoutPublishPolicyThrowsException() 0 19 1
A testNewTranslationInfo() 0 16 2
A createRoleWithPolicies() 0 14 2
B assertAliasesCorrect() 0 38 5
A assertAllFieldsEquals() 0 7 1
A assertLocaleFieldsEquals() 0 14 3
B normalizeFields() 17 26 4
A createLocaleFieldsFixture() 0 11 4
A assertDefaultContentStates() 19 19 3
B createFieldsFixture() 0 37 1
A getExpectedMediaContentInfoProperties() 18 18 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 testUpdateContentThrowsBadStateException() 0 24 1
A testCreateContentDraftWithSecondParameter() 0 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\ContentService;
12
use eZ\Publish\API\Repository\Values\Content\Content;
13
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
14
use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct;
15
use eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct;
16
use eZ\Publish\API\Repository\Values\Content\Field;
17
use eZ\Publish\API\Repository\Values\Content\Location;
18
use eZ\Publish\API\Repository\Values\Content\TranslationInfo;
19
use eZ\Publish\API\Repository\Values\Content\URLAlias;
20
use eZ\Publish\API\Repository\Values\Content\Relation;
21
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
22
use eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation;
23
use eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation;
24
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation;
25
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
26
use Exception;
27
28
/**
29
 * Test case for operations in the ContentService using in memory storage.
30
 *
31
 * @see eZ\Publish\API\Repository\ContentService
32
 * @group content
33
 */
34
class ContentServiceTest extends BaseContentServiceTest
35
{
36
    /**
37
     * Test for the newContentCreateStruct() method.
38
     *
39
     * @see \eZ\Publish\API\Repository\ContentService::newContentCreateStruct()
40
     * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testLoadContentTypeByIdentifier
41
     * @group user
42
     * @group field-type
43
     */
44
    public function testNewContentCreateStruct()
45
    {
46
        $repository = $this->getRepository();
47
48
        /* BEGIN: Use Case */
49
        // Create a content type
50
        $contentTypeService = $repository->getContentTypeService();
51
52
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
53
54
        $contentService = $repository->getContentService();
55
56
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
57
        /* END: Use Case */
58
59
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentCreateStruct', $contentCreate);
60
    }
61
62
    /**
63
     * Test for the createContent() method.
64
     *
65
     * @return \eZ\Publish\API\Repository\Values\Content\Content
66
     *
67
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
68
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentCreateStruct
69
     * @group user
70
     * @group field-type
71
     */
72
    public function testCreateContent()
73
    {
74
        if ($this->isVersion4()) {
75
            $this->markTestSkipped('This test requires eZ Publish 5');
76
        }
77
78
        $repository = $this->getRepository();
79
80
        /* BEGIN: Use Case */
81
        $contentTypeService = $repository->getContentTypeService();
82
83
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
84
85
        $contentService = $repository->getContentService();
86
87
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
88
        $contentCreate->setField('name', 'My awesome forum');
89
90
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
91
        $contentCreate->alwaysAvailable = true;
92
93
        $content = $contentService->createContent($contentCreate);
94
        /* END: Use Case */
95
96
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content', $content);
97
98
        return $content;
99
    }
100
101
    /**
102
     * Test for the createContent() method.
103
     *
104
     * Tests made for issue #EZP-20955 where Anonymous user is granted access to create content
105
     * and should have access to do that.
106
     *
107
     * @return \eZ\Publish\API\Repository\Values\Content\Content
108
     *
109
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
110
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentCreateStruct
111
     * @group user
112
     * @group field-type
113
     */
114
    public function testCreateContentAndPublishWithPrivilegedAnonymousUser()
115
    {
116
        if ($this->isVersion4()) {
117
            $this->markTestSkipped('This test requires eZ Publish 5');
118
        }
119
120
        $anonymousUserId = $this->generateId('user', 10);
121
122
        $repository = $this->getRepository();
123
        $contentService = $repository->getContentService();
124
        $contentTypeService = $repository->getContentTypeService();
125
        $locationService = $repository->getLocationService();
126
        $roleService = $repository->getRoleService();
127
128
        // Give Anonymous user role additional rights
129
        $role = $roleService->loadRoleByIdentifier('Anonymous');
130
        $roleDraft = $roleService->createRoleDraft($role);
131
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'create');
132
        $policyCreateStruct->addLimitation(new SectionLimitation(array('limitationValues' => array(1))));
133
        $policyCreateStruct->addLimitation(new LocationLimitation(array('limitationValues' => array(2))));
134
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(array('limitationValues' => array(1))));
135
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
136
137
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'publish');
138
        $policyCreateStruct->addLimitation(new SectionLimitation(array('limitationValues' => array(1))));
139
        $policyCreateStruct->addLimitation(new LocationLimitation(array('limitationValues' => array(2))));
140
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(array('limitationValues' => array(1))));
141
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
142
        $roleService->publishRoleDraft($roleDraft);
143
144
        // Set Anonymous user as current
145
        $repository->getPermissionResolver()->setCurrentUserReference($repository->getUserService()->loadUser($anonymousUserId));
146
147
        // Create a new content object:
148
        $contentCreate = $contentService->newContentCreateStruct(
149
            $contentTypeService->loadContentTypeByIdentifier('folder'),
150
            'eng-GB'
151
        );
152
153
        $contentCreate->setField('name', 'Folder 1');
154
155
        $content = $contentService->createContent(
156
            $contentCreate,
157
            array($locationService->newLocationCreateStruct(2))
158
        );
159
160
        $contentService->publishVersion(
161
            $content->getVersionInfo()
162
        );
163
    }
164
165
    /**
166
     * Test for the createContent() method.
167
     *
168
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
169
     *
170
     * @return \eZ\Publish\API\Repository\Values\Content\Content
171
     *
172
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
173
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
174
     */
175
    public function testCreateContentSetsContentInfo($content)
176
    {
177
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $content->contentInfo);
178
179
        return $content;
180
    }
181
182
    /**
183
     * Test for the createContent() method.
184
     *
185
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
186
     *
187
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
188
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsContentInfo
189
     */
190
    public function testCreateContentSetsExpectedContentInfo($content)
191
    {
192
        $this->assertEquals(
193
            array(
194
                $content->id,
195
                28, // id of content type "forum"
196
                true,
197
                1,
198
                'abcdef0123456789abcdef0123456789',
199
                'eng-US',
200
                $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...
201
                false,
202
                null,
203
                // Main Location id for unpublished Content should be null
204
                null,
205
            ),
206
            array(
207
                $content->contentInfo->id,
208
                $content->contentInfo->contentTypeId,
209
                $content->contentInfo->alwaysAvailable,
210
                $content->contentInfo->currentVersionNo,
211
                $content->contentInfo->remoteId,
212
                $content->contentInfo->mainLanguageCode,
213
                $content->contentInfo->ownerId,
214
                $content->contentInfo->published,
215
                $content->contentInfo->publishedDate,
216
                $content->contentInfo->mainLocationId,
217
            )
218
        );
219
    }
220
221
    /**
222
     * Test for the createContent() method.
223
     *
224
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
225
     *
226
     * @return \eZ\Publish\API\Repository\Values\Content\Content
227
     *
228
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
229
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
230
     */
231
    public function testCreateContentSetsVersionInfo($content)
232
    {
233
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo', $content->getVersionInfo());
234
235
        return $content;
236
    }
237
238
    /**
239
     * Test for the createContent() method.
240
     *
241
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
242
     *
243
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
244
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsVersionInfo
245
     */
246
    public function testCreateContentSetsExpectedVersionInfo($content)
247
    {
248
        $this->assertEquals(
249
            array(
250
                'status' => VersionInfo::STATUS_DRAFT,
251
                'versionNo' => 1,
252
                '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...
253
                'initialLanguageCode' => 'eng-US',
254
            ),
255
            array(
256
                'status' => $content->getVersionInfo()->status,
257
                'versionNo' => $content->getVersionInfo()->versionNo,
258
                'creatorId' => $content->getVersionInfo()->creatorId,
259
                'initialLanguageCode' => $content->getVersionInfo()->initialLanguageCode,
260
            )
261
        );
262
    }
263
264
    /**
265
     * Test for the createContent() method.
266
     *
267
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
268
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
269
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
270
     */
271
    public function testCreateContentThrowsInvalidArgumentException()
272
    {
273
        if ($this->isVersion4()) {
274
            $this->markTestSkipped('This test requires eZ Publish 5');
275
        }
276
277
        $repository = $this->getRepository();
278
279
        /* BEGIN: Use Case */
280
        $contentTypeService = $repository->getContentTypeService();
281
        $contentService = $repository->getContentService();
282
283
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
284
285
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
286
        $contentCreate1->setField('name', 'An awesome Sidelfingen forum');
287
288
        $contentCreate1->remoteId = 'abcdef0123456789abcdef0123456789';
289
        $contentCreate1->alwaysAvailable = true;
290
291
        $draft = $contentService->createContent($contentCreate1);
292
        $contentService->publishVersion($draft->versionInfo);
293
294
        $contentCreate2 = $contentService->newContentCreateStruct($contentType, 'eng-GB');
295
        $contentCreate2->setField('name', 'An awesome Bielefeld forum');
296
297
        $contentCreate2->remoteId = 'abcdef0123456789abcdef0123456789';
298
        $contentCreate2->alwaysAvailable = false;
299
300
        // This call will fail with an "InvalidArgumentException", because the
301
        // remoteId is already in use.
302
        $contentService->createContent($contentCreate2);
303
        /* END: Use Case */
304
    }
305
306
    /**
307
     * Test for the createContent() method.
308
     *
309
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
310
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
311
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
312
     */
313 View Code Duplication
    public function testCreateContentThrowsInvalidArgumentExceptionOnFieldTypeNotAccept()
314
    {
315
        $repository = $this->getRepository();
316
317
        /* BEGIN: Use Case */
318
        $contentTypeService = $repository->getContentTypeService();
319
        $contentService = $repository->getContentService();
320
321
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
322
323
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
324
        // The name field does only accept strings and null as its values
325
        $contentCreate->setField('name', new \stdClass());
326
327
        // Throws InvalidArgumentException since the name field is filled
328
        // improperly
329
        $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...
330
        /* END: Use Case */
331
    }
332
333
    /**
334
     * Test for the createContent() method.
335
     *
336
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
337
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
338
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
339
     */
340
    public function testCreateContentThrowsContentFieldValidationException()
341
    {
342
        $repository = $this->getRepository();
343
344
        /* BEGIN: Use Case */
345
        $contentTypeService = $repository->getContentTypeService();
346
        $contentService = $repository->getContentService();
347
348
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
349
350
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
351
        $contentCreate1->setField('name', 'An awesome Sidelfingen folder');
352
        // Violates string length constraint
353
        $contentCreate1->setField('short_name', str_repeat('a', 200));
354
355
        // Throws ContentFieldValidationException, since short_name does not pass
356
        // validation of the string length validator
357
        $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...
358
        /* END: Use Case */
359
    }
360
361
    /**
362
     * Test for the createContent() method.
363
     *
364
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
365
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
366
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
367
     */
368 View Code Duplication
    public function testCreateContentRequiredFieldMissing()
369
    {
370
        $repository = $this->getRepository();
371
372
        /* BEGIN: Use Case */
373
        $contentTypeService = $repository->getContentTypeService();
374
        $contentService = $repository->getContentService();
375
376
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
377
378
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
379
        // Required field "name" is not set
380
381
        // Throws a ContentFieldValidationException, since a required field is
382
        // missing
383
        $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...
384
        /* END: Use Case */
385
    }
386
387
    /**
388
     * Test for the createContent() method.
389
     *
390
     * NOTE: We have bidirectional dependencies between the ContentService and
391
     * the LocationService, so that we cannot use PHPUnit's test dependencies
392
     * here.
393
     *
394
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
395
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
396
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationByRemoteId
397
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
398
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
399
     * @group user
400
     */
401
    public function testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately()
402
    {
403
        $repository = $this->getRepository();
404
405
        $locationService = $repository->getLocationService();
406
407
        /* BEGIN: Use Case */
408
        $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...
409
410
        // The location will not have been created, yet, so this throws an
411
        // exception
412
        $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...
413
            '0123456789abcdef0123456789abcdef'
414
        );
415
        /* END: Use Case */
416
    }
417
418
    /**
419
     * Test for the createContent() method.
420
     *
421
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
422
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
423
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
424
     */
425
    public function testCreateContentThrowsInvalidArgumentExceptionWithLocationCreateParameter()
426
    {
427
        $repository = $this->getRepository();
428
429
        $parentLocationId = $this->generateId('location', 56);
430
        /* BEGIN: Use Case */
431
        // $parentLocationId is a valid location ID
432
433
        $contentService = $repository->getContentService();
434
        $contentTypeService = $repository->getContentTypeService();
435
        $locationService = $repository->getLocationService();
436
437
        // Load content type
438
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
439
440
        // Configure new locations
441
        $locationCreate1 = $locationService->newLocationCreateStruct($parentLocationId);
442
443
        $locationCreate1->priority = 23;
444
        $locationCreate1->hidden = true;
445
        $locationCreate1->remoteId = '0123456789abcdef0123456789aaaaaa';
446
        $locationCreate1->sortField = Location::SORT_FIELD_NODE_ID;
447
        $locationCreate1->sortOrder = Location::SORT_ORDER_DESC;
448
449
        $locationCreate2 = $locationService->newLocationCreateStruct($parentLocationId);
450
451
        $locationCreate2->priority = 42;
452
        $locationCreate2->hidden = true;
453
        $locationCreate2->remoteId = '0123456789abcdef0123456789bbbbbb';
454
        $locationCreate2->sortField = Location::SORT_FIELD_NODE_ID;
455
        $locationCreate2->sortOrder = Location::SORT_ORDER_DESC;
456
457
        // Configure new content object
458
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
459
460
        $contentCreate->setField('name', 'A awesome Sindelfingen forum');
461
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
462
        $contentCreate->alwaysAvailable = true;
463
464
        // Create new content object under the specified location
465
        $draft = $contentService->createContent(
466
            $contentCreate,
467
            array($locationCreate1)
468
        );
469
        $contentService->publishVersion($draft->versionInfo);
470
471
        // This call will fail with an "InvalidArgumentException", because the
472
        // Content remoteId already exists,
473
        $contentService->createContent(
474
            $contentCreate,
475
            array($locationCreate2)
476
        );
477
        /* END: Use Case */
478
    }
479
480
    /**
481
     * Test for the loadContentInfo() method.
482
     *
483
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
484
     * @group user
485
     */
486 View Code Duplication
    public function testLoadContentInfo()
487
    {
488
        $repository = $this->getRepository();
489
490
        $mediaFolderId = $this->generateId('object', 41);
491
        /* BEGIN: Use Case */
492
        $contentService = $repository->getContentService();
493
494
        // Load the ContentInfo for "Media" folder
495
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
496
        /* END: Use Case */
497
498
        $this->assertInstanceOf(
499
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
500
            $contentInfo
501
        );
502
503
        return $contentInfo;
504
    }
505
506
    /**
507
     * Test for the returned value of the loadContentInfo() method.
508
     *
509
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
510
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfo
511
     *
512
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
513
     */
514
    public function testLoadContentInfoSetsExpectedContentInfo(ContentInfo $contentInfo)
515
    {
516
        $this->assertPropertiesCorrectUnsorted(
517
            $this->getExpectedMediaContentInfoProperties(),
518
            $contentInfo
519
        );
520
    }
521
522
    /**
523
     * Test for the loadContentInfo() method.
524
     *
525
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
526
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
527
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
528
     */
529 View Code Duplication
    public function testLoadContentInfoThrowsNotFoundException()
530
    {
531
        $repository = $this->getRepository();
532
533
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
534
        /* BEGIN: Use Case */
535
        $contentService = $repository->getContentService();
536
537
        // This call will fail with a NotFoundException
538
        $contentService->loadContentInfo($nonExistentContentId);
539
        /* END: Use Case */
540
    }
541
542
    /**
543
     * Test for the loadContentInfoByRemoteId() method.
544
     *
545
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
546
     */
547
    public function testLoadContentInfoByRemoteId()
548
    {
549
        $repository = $this->getRepository();
550
551
        /* BEGIN: Use Case */
552
        $contentService = $repository->getContentService();
553
554
        // Load the ContentInfo for "Media" folder
555
        $contentInfo = $contentService->loadContentInfoByRemoteId('faaeb9be3bd98ed09f606fc16d144eca');
556
        /* END: Use Case */
557
558
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $contentInfo);
559
560
        return $contentInfo;
561
    }
562
563
    /**
564
     * Test for the returned value of the loadContentInfoByRemoteId() method.
565
     *
566
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
567
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId
568
     *
569
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
570
     */
571 View Code Duplication
    public function testLoadContentInfoByRemoteIdSetsExpectedContentInfo(ContentInfo $contentInfo)
572
    {
573
        $this->assertPropertiesCorrectUnsorted(
574
            [
575
                'id' => 10,
576
                'contentTypeId' => 4,
577
                'name' => 'Anonymous User',
578
                'sectionId' => 2,
579
                'currentVersionNo' => 2,
580
                'published' => true,
581
                'ownerId' => 14,
582
                'modificationDate' => $this->createDateTime(1072180405),
583
                'publishedDate' => $this->createDateTime(1033920665),
584
                'alwaysAvailable' => 1,
585
                'remoteId' => 'faaeb9be3bd98ed09f606fc16d144eca',
586
                'mainLanguageCode' => 'eng-US',
587
                'mainLocationId' => 45,
588
            ],
589
            $contentInfo
590
        );
591
    }
592
593
    /**
594
     * Test for the loadContentInfoByRemoteId() method.
595
     *
596
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
597
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
598
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
599
     */
600
    public function testLoadContentInfoByRemoteIdThrowsNotFoundException()
601
    {
602
        $repository = $this->getRepository();
603
604
        /* BEGIN: Use Case */
605
        $contentService = $repository->getContentService();
606
607
        // This call will fail with a NotFoundException
608
        $contentService->loadContentInfoByRemoteId('abcdefghijklmnopqrstuvwxyz0123456789');
609
        /* END: Use Case */
610
    }
611
612
    /**
613
     * Test for the loadVersionInfo() method.
614
     *
615
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
616
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
617
     * @group user
618
     */
619
    public function testLoadVersionInfo()
620
    {
621
        $repository = $this->getRepository();
622
623
        $mediaFolderId = $this->generateId('object', 41);
624
        /* BEGIN: Use Case */
625
        // $mediaFolderId contains the ID of the "Media" folder
626
627
        $contentService = $repository->getContentService();
628
629
        // Load the ContentInfo for "Media" folder
630
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
631
632
        // Now load the current version info of the "Media" folder
633
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
634
        /* END: Use Case */
635
636
        $this->assertInstanceOf(
637
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
638
            $versionInfo
639
        );
640
    }
641
642
    /**
643
     * Test for the loadVersionInfoById() method.
644
     *
645
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
646
     */
647 View Code Duplication
    public function testLoadVersionInfoById()
648
    {
649
        $repository = $this->getRepository();
650
651
        $mediaFolderId = $this->generateId('object', 41);
652
        /* BEGIN: Use Case */
653
        // $mediaFolderId contains the ID of the "Media" folder
654
655
        $contentService = $repository->getContentService();
656
657
        // Load the VersionInfo for "Media" folder
658
        $versionInfo = $contentService->loadVersionInfoById($mediaFolderId);
659
        /* END: Use Case */
660
661
        $this->assertInstanceOf(
662
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
663
            $versionInfo
664
        );
665
666
        return $versionInfo;
667
    }
668
669
    /**
670
     * Test for the returned value of the loadVersionInfoById() method.
671
     *
672
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
673
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
674
     *
675
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
676
     */
677
    public function testLoadVersionInfoByIdSetsExpectedVersionInfo(VersionInfo $versionInfo)
678
    {
679
        $this->assertPropertiesCorrect(
680
            [
681
                'names' => [
682
                    'eng-US' => 'Media',
683
                ],
684
                'contentInfo' => new ContentInfo($this->getExpectedMediaContentInfoProperties()),
685
                'id' => 472,
686
                'versionNo' => 1,
687
                'modificationDate' => $this->createDateTime(1060695457),
688
                'creatorId' => 14,
689
                'creationDate' => $this->createDateTime(1060695450),
690
                'status' => 1,
691
                'initialLanguageCode' => 'eng-US',
692
                'languageCodes' => [
693
                    'eng-US',
694
                ],
695
            ],
696
            $versionInfo
697
        );
698
    }
699
700
    /**
701
     * Test for the loadVersionInfoById() method.
702
     *
703
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
704
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
705
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
706
     */
707 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundException()
708
    {
709
        $repository = $this->getRepository();
710
711
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
712
        /* BEGIN: Use Case */
713
        $contentService = $repository->getContentService();
714
715
        // This call will fail with a "NotFoundException"
716
        $contentService->loadVersionInfoById($nonExistentContentId);
717
        /* END: Use Case */
718
    }
719
720
    /**
721
     * Test for the loadContentByContentInfo() method.
722
     *
723
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
724
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
725
     */
726
    public function testLoadContentByContentInfo()
727
    {
728
        $repository = $this->getRepository();
729
730
        $mediaFolderId = $this->generateId('object', 41);
731
        /* BEGIN: Use Case */
732
        // $mediaFolderId contains the ID of the "Media" folder
733
734
        $contentService = $repository->getContentService();
735
736
        // Load the ContentInfo for "Media" folder
737
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
738
739
        // Now load the current content version for the info instance
740
        $content = $contentService->loadContentByContentInfo($contentInfo);
741
        /* END: Use Case */
742
743
        $this->assertInstanceOf(
744
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
745
            $content
746
        );
747
    }
748
749
    /**
750
     * Test for the loadContentByVersionInfo() method.
751
     *
752
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
753
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
754
     */
755
    public function testLoadContentByVersionInfo()
756
    {
757
        $repository = $this->getRepository();
758
759
        $mediaFolderId = $this->generateId('object', 41);
760
        /* BEGIN: Use Case */
761
        // $mediaFolderId contains the ID of the "Media" folder
762
763
        $contentService = $repository->getContentService();
764
765
        // Load the ContentInfo for "Media" folder
766
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
767
768
        // Load the current VersionInfo
769
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
770
771
        // Now load the current content version for the info instance
772
        $content = $contentService->loadContentByVersionInfo($versionInfo);
773
        /* END: Use Case */
774
775
        $this->assertInstanceOf(
776
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
777
            $content
778
        );
779
    }
780
781
    /**
782
     * Test for the loadContent() method.
783
     *
784
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
785
     * @group user
786
     * @group field-type
787
     */
788 View Code Duplication
    public function testLoadContent()
789
    {
790
        $repository = $this->getRepository();
791
792
        $mediaFolderId = $this->generateId('object', 41);
793
        /* BEGIN: Use Case */
794
        // $mediaFolderId contains the ID of the "Media" folder
795
796
        $contentService = $repository->getContentService();
797
798
        // Load the Content for "Media" folder, any language and current version
799
        $content = $contentService->loadContent($mediaFolderId);
800
        /* END: Use Case */
801
802
        $this->assertInstanceOf(
803
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
804
            $content
805
        );
806
    }
807
808
    /**
809
     * Test for the loadContent() method.
810
     *
811
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
812
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
813
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
814
     */
815 View Code Duplication
    public function testLoadContentThrowsNotFoundException()
816
    {
817
        $repository = $this->getRepository();
818
819
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
820
        /* BEGIN: Use Case */
821
        $contentService = $repository->getContentService();
822
823
        // This call will fail with a "NotFoundException"
824
        $contentService->loadContent($nonExistentContentId);
825
        /* END: Use Case */
826
    }
827
828
    /**
829
     * Data provider for testLoadContentByRemoteId().
830
     *
831
     * @return array
832
     */
833
    public function contentRemoteIdVersionLanguageProvider()
834
    {
835
        return [
836
            ['f5c88a2209584891056f987fd965b0ba', null, null],
837
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], null],
838
            ['f5c88a2209584891056f987fd965b0ba', null, 1],
839
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], 1],
840
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, null],
841
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], null],
842
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, 1],
843
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], 1],
844
        ];
845
    }
846
847
    /**
848
     * Test for the loadContentByRemoteId() method.
849
     *
850
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId
851
     * @dataProvider contentRemoteIdVersionLanguageProvider
852
     *
853
     * @param string $remoteId
854
     * @param array|null $languages
855
     * @param int $versionNo
856
     */
857
    public function testLoadContentByRemoteId($remoteId, $languages, $versionNo)
858
    {
859
        $repository = $this->getRepository();
860
861
        $contentService = $repository->getContentService();
862
863
        $content = $contentService->loadContentByRemoteId($remoteId, $languages, $versionNo);
864
865
        $this->assertInstanceOf(
866
            Content::class,
867
            $content
868
        );
869
870
        $this->assertEquals($remoteId, $content->contentInfo->remoteId);
871
        if ($languages !== null) {
872
            $this->assertEquals($languages, $content->getVersionInfo()->languageCodes);
873
        }
874
        $this->assertEquals($versionNo ?: 1, $content->getVersionInfo()->versionNo);
875
    }
876
877
    /**
878
     * Test for the loadContentByRemoteId() method.
879
     *
880
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
881
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
882
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
883
     */
884
    public function testLoadContentByRemoteIdThrowsNotFoundException()
885
    {
886
        $repository = $this->getRepository();
887
888
        /* BEGIN: Use Case */
889
        $contentService = $repository->getContentService();
890
891
        // This call will fail with a "NotFoundException", because no content
892
        // object exists for the given remoteId
893
        $contentService->loadContentByRemoteId('a1b1c1d1e1f1a2b2c2d2e2f2a3b3c3d3');
894
        /* END: Use Case */
895
    }
896
897
    /**
898
     * Test for the publishVersion() method.
899
     *
900
     * @return \eZ\Publish\API\Repository\Values\Content\Content
901
     *
902
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
903
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
904
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
905
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
906
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
907
     * @group user
908
     * @group field-type
909
     */
910
    public function testPublishVersion()
911
    {
912
        $time = time();
913
        /* BEGIN: Use Case */
914
        $content = $this->createContentVersion1();
915
        /* END: Use Case */
916
917
        $this->assertInstanceOf(Content::class, $content);
918
        $this->assertTrue($content->contentInfo->published);
919
        $this->assertEquals(VersionInfo::STATUS_PUBLISHED, $content->versionInfo->status);
920
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->publishedDate->getTimestamp());
921
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->modificationDate->getTimestamp());
922
923
        return $content;
924
    }
925
926
    /**
927
     * Test for the publishVersion() method.
928
     *
929
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
930
     *
931
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
932
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
933
     */
934
    public function testPublishVersionSetsExpectedContentInfo($content)
935
    {
936
        $this->assertEquals(
937
            array(
938
                $content->id,
939
                true,
940
                1,
941
                'abcdef0123456789abcdef0123456789',
942
                'eng-US',
943
                $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...
944
                true,
945
            ),
946
            array(
947
                $content->contentInfo->id,
948
                $content->contentInfo->alwaysAvailable,
949
                $content->contentInfo->currentVersionNo,
950
                $content->contentInfo->remoteId,
951
                $content->contentInfo->mainLanguageCode,
952
                $content->contentInfo->ownerId,
953
                $content->contentInfo->published,
954
            )
955
        );
956
957
        $this->assertNotNull($content->contentInfo->mainLocationId);
958
        $date = new \DateTime('1984/01/01');
959
        $this->assertGreaterThan(
960
            $date->getTimestamp(),
961
            $content->contentInfo->publishedDate->getTimestamp()
962
        );
963
    }
964
965
    /**
966
     * Test for the publishVersion() method.
967
     *
968
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
969
     *
970
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
971
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
972
     */
973
    public function testPublishVersionSetsExpectedVersionInfo($content)
974
    {
975
        $this->assertEquals(
976
            array(
977
                $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...
978
                'eng-US',
979
                VersionInfo::STATUS_PUBLISHED,
980
                1,
981
            ),
982
            array(
983
                $content->getVersionInfo()->creatorId,
984
                $content->getVersionInfo()->initialLanguageCode,
985
                $content->getVersionInfo()->status,
986
                $content->getVersionInfo()->versionNo,
987
            )
988
        );
989
990
        $date = new \DateTime('1984/01/01');
991
        $this->assertGreaterThan(
992
            $date->getTimestamp(),
993
            $content->getVersionInfo()->modificationDate->getTimestamp()
994
        );
995
996
        $this->assertNotNull($content->getVersionInfo()->modificationDate);
997
    }
998
999
    /**
1000
     * Test for the publishVersion() method.
1001
     *
1002
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1003
     *
1004
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1005
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
1006
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1007
     */
1008
    public function testPublishVersionCreatesLocationsDefinedOnCreate()
1009
    {
1010
        $repository = $this->getRepository();
1011
1012
        /* BEGIN: Use Case */
1013
        $content = $this->createContentVersion1();
1014
        /* END: Use Case */
1015
1016
        $locationService = $repository->getLocationService();
1017
        $location = $locationService->loadLocationByRemoteId(
1018
            '0123456789abcdef0123456789abcdef'
1019
        );
1020
1021
        $this->assertEquals(
1022
            $location->getContentInfo(),
1023
            $content->getVersionInfo()->getContentInfo()
1024
        );
1025
1026
        return array($content, $location);
1027
    }
1028
1029
    /**
1030
     * Test for the publishVersion() method.
1031
     *
1032
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1033
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionCreatesLocationsDefinedOnCreate
1034
     */
1035
    public function testCreateContentWithLocationCreateParameterCreatesExpectedLocation(array $testData)
1036
    {
1037
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
1038
        /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
1039
        list($content, $location) = $testData;
1040
1041
        $parentLocationId = $this->generateId('location', 56);
1042
        $parentLocation = $this->getRepository()->getLocationService()->loadLocation($parentLocationId);
1043
        $mainLocationId = $content->getVersionInfo()->getContentInfo()->mainLocationId;
1044
1045
        $this->assertPropertiesCorrect(
1046
            array(
1047
                'id' => $mainLocationId,
1048
                'priority' => 23,
1049
                'hidden' => true,
1050
                'invisible' => true,
1051
                'remoteId' => '0123456789abcdef0123456789abcdef',
1052
                'parentLocationId' => $parentLocationId,
1053
                'pathString' => $parentLocation->pathString . $mainLocationId . '/',
1054
                'depth' => $parentLocation->depth + 1,
1055
                'sortField' => Location::SORT_FIELD_NODE_ID,
1056
                'sortOrder' => Location::SORT_ORDER_DESC,
1057
            ),
1058
            $location
1059
        );
1060
    }
1061
1062
    /**
1063
     * Test for the publishVersion() method.
1064
     *
1065
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1066
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1067
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1068
     */
1069 View Code Duplication
    public function testPublishVersionThrowsBadStateException()
1070
    {
1071
        $repository = $this->getRepository();
1072
1073
        $contentService = $repository->getContentService();
1074
1075
        /* BEGIN: Use Case */
1076
        $draft = $this->createContentDraftVersion1();
1077
1078
        // Publish the content draft
1079
        $contentService->publishVersion($draft->getVersionInfo());
1080
1081
        // This call will fail with a "BadStateException", because the version
1082
        // is already published.
1083
        $contentService->publishVersion($draft->getVersionInfo());
1084
        /* END: Use Case */
1085
    }
1086
1087
    /**
1088
     * Test that publishVersion() does not affect publishedDate (assuming previous version exists).
1089
     *
1090
     * @covers \eZ\Publish\API\Repository\ContentService::publishVersion
1091
     */
1092
    public function testPublishVersionDoesNotChangePublishedDate()
1093
    {
1094
        $repository = $this->getRepository();
1095
1096
        $contentService = $repository->getContentService();
1097
1098
        $publishedContent = $this->createContentVersion1();
1099
1100
        // force timestamps to differ
1101
        sleep(1);
1102
1103
        $contentDraft = $contentService->createContentDraft($publishedContent->contentInfo);
1104
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1105
        $contentUpdateStruct->setField('name', 'New name');
1106
        $contentDraft = $contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
1107
        $republishedContent = $contentService->publishVersion($contentDraft->versionInfo);
1108
1109
        $this->assertEquals(
1110
            $publishedContent->contentInfo->publishedDate->getTimestamp(),
1111
            $republishedContent->contentInfo->publishedDate->getTimestamp()
1112
        );
1113
        $this->assertGreaterThan(
1114
            $publishedContent->contentInfo->modificationDate->getTimestamp(),
1115
            $republishedContent->contentInfo->modificationDate->getTimestamp()
1116
        );
1117
    }
1118
1119
    /**
1120
     * Test for the createContentDraft() method.
1121
     *
1122
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1123
     *
1124
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1125
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1126
     * @group user
1127
     */
1128
    public function testCreateContentDraft()
1129
    {
1130
        $repository = $this->getRepository();
1131
1132
        $contentService = $repository->getContentService();
1133
1134
        /* BEGIN: Use Case */
1135
        $content = $this->createContentVersion1();
1136
1137
        // Now we create a new draft from the published content
1138
        $draftedContent = $contentService->createContentDraft($content->contentInfo);
1139
        /* END: Use Case */
1140
1141
        $this->assertInstanceOf(
1142
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1143
            $draftedContent
1144
        );
1145
1146
        return $draftedContent;
1147
    }
1148
1149
    /**
1150
     * Test for the createContentDraft() method.
1151
     *
1152
     * Test that editor has access to edit own draft.
1153
     * Note: Editors have access to version_read, which is needed to load content drafts.
1154
     *
1155
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1156
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1157
     * @group user
1158
     */
1159 View Code Duplication
    public function testCreateContentDraftAndLoadAccess()
1160
    {
1161
        $repository = $this->getRepository();
1162
1163
        /* BEGIN: Use Case */
1164
        $user = $this->createUserVersion1();
1165
1166
        // Set new editor as user
1167
        $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...
1168
1169
        // Create draft
1170
        $draft = $this->createContentDraftVersion1(2, 'folder');
1171
1172
        // Try to load the draft
1173
        $contentService = $repository->getContentService();
1174
        $loadedDraft = $contentService->loadContent($draft->id);
1175
1176
        /* END: Use Case */
1177
1178
        $this->assertEquals($draft->id, $loadedDraft->id);
1179
    }
1180
1181
    /**
1182
     * Test for the createContentDraft() method.
1183
     *
1184
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1185
     *
1186
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1187
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1188
     */
1189
    public function testCreateContentDraftSetsExpectedProperties($draft)
1190
    {
1191
        $this->assertEquals(
1192
            array(
1193
                'fieldCount' => 2,
1194
                'relationCount' => 0,
1195
            ),
1196
            array(
1197
                'fieldCount' => count($draft->getFields()),
1198
                'relationCount' => count($this->getRepository()->getContentService()->loadRelations($draft->getVersionInfo())),
1199
            )
1200
        );
1201
    }
1202
1203
    /**
1204
     * Test for the createContentDraft() method.
1205
     *
1206
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1207
     *
1208
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1209
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1210
     */
1211
    public function testCreateContentDraftSetsContentInfo($draft)
1212
    {
1213
        $contentInfo = $draft->contentInfo;
1214
1215
        $this->assertEquals(
1216
            array(
1217
                $draft->id,
1218
                true,
1219
                1,
1220
                'eng-US',
1221
                $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...
1222
                'abcdef0123456789abcdef0123456789',
1223
                1,
1224
            ),
1225
            array(
1226
                $contentInfo->id,
1227
                $contentInfo->alwaysAvailable,
1228
                $contentInfo->currentVersionNo,
1229
                $contentInfo->mainLanguageCode,
1230
                $contentInfo->ownerId,
1231
                $contentInfo->remoteId,
1232
                $contentInfo->sectionId,
1233
            )
1234
        );
1235
    }
1236
1237
    /**
1238
     * Test for the createContentDraft() method.
1239
     *
1240
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1241
     *
1242
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1243
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1244
     */
1245
    public function testCreateContentDraftSetsVersionInfo($draft)
1246
    {
1247
        $versionInfo = $draft->getVersionInfo();
1248
1249
        $this->assertEquals(
1250
            array(
1251
                '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...
1252
                'initialLanguageCode' => 'eng-US',
1253
                'languageCodes' => array(0 => 'eng-US'),
1254
                'status' => VersionInfo::STATUS_DRAFT,
1255
                'versionNo' => 2,
1256
            ),
1257
            array(
1258
                'creatorId' => $versionInfo->creatorId,
1259
                'initialLanguageCode' => $versionInfo->initialLanguageCode,
1260
                'languageCodes' => $versionInfo->languageCodes,
1261
                'status' => $versionInfo->status,
1262
                'versionNo' => $versionInfo->versionNo,
1263
            )
1264
        );
1265
    }
1266
1267
    /**
1268
     * Test for the createContentDraft() method.
1269
     *
1270
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1271
     *
1272
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1273
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1274
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
1275
     */
1276
    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...
1277
    {
1278
        $repository = $this->getRepository();
1279
1280
        $contentService = $repository->getContentService();
1281
1282
        /* BEGIN: Use Case */
1283
        $content = $this->createContentVersion1();
1284
1285
        // Now we create a new draft from the published content
1286
        $contentService->createContentDraft($content->contentInfo);
1287
1288
        // This call will still load the published version
1289
        $versionInfoPublished = $contentService->loadVersionInfo($content->contentInfo);
1290
        /* END: Use Case */
1291
1292
        $this->assertEquals(1, $versionInfoPublished->versionNo);
1293
    }
1294
1295
    /**
1296
     * Test for the createContentDraft() method.
1297
     *
1298
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1299
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
1300
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1301
     */
1302
    public function testCreateContentDraftLoadContentStillLoadsPublishedVersion()
1303
    {
1304
        $repository = $this->getRepository();
1305
1306
        $contentService = $repository->getContentService();
1307
1308
        /* BEGIN: Use Case */
1309
        $content = $this->createContentVersion1();
1310
1311
        // Now we create a new draft from the published content
1312
        $contentService->createContentDraft($content->contentInfo);
1313
1314
        // This call will still load the published content version
1315
        $contentPublished = $contentService->loadContent($content->id);
1316
        /* END: Use Case */
1317
1318
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1319
    }
1320
1321
    /**
1322
     * Test for the createContentDraft() method.
1323
     *
1324
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1325
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
1326
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1327
     */
1328
    public function testCreateContentDraftLoadContentByRemoteIdStillLoadsPublishedVersion()
1329
    {
1330
        $repository = $this->getRepository();
1331
1332
        $contentService = $repository->getContentService();
1333
1334
        /* BEGIN: Use Case */
1335
        $content = $this->createContentVersion1();
1336
1337
        // Now we create a new draft from the published content
1338
        $contentService->createContentDraft($content->contentInfo);
1339
1340
        // This call will still load the published content version
1341
        $contentPublished = $contentService->loadContentByRemoteId('abcdef0123456789abcdef0123456789');
1342
        /* END: Use Case */
1343
1344
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1345
    }
1346
1347
    /**
1348
     * Test for the createContentDraft() method.
1349
     *
1350
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1351
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
1352
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1353
     */
1354
    public function testCreateContentDraftLoadContentByContentInfoStillLoadsPublishedVersion()
1355
    {
1356
        $repository = $this->getRepository();
1357
1358
        $contentService = $repository->getContentService();
1359
1360
        /* BEGIN: Use Case */
1361
        $content = $this->createContentVersion1();
1362
1363
        // Now we create a new draft from the published content
1364
        $contentService->createContentDraft($content->contentInfo);
1365
1366
        // This call will still load the published content version
1367
        $contentPublished = $contentService->loadContentByContentInfo($content->contentInfo);
1368
        /* END: Use Case */
1369
1370
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1371
    }
1372
1373
    /**
1374
     * Test for the newContentUpdateStruct() method.
1375
     *
1376
     * @covers \eZ\Publish\API\Repository\ContentService::newContentUpdateStruct
1377
     * @group user
1378
     */
1379
    public function testNewContentUpdateStruct()
1380
    {
1381
        $repository = $this->getRepository();
1382
1383
        /* BEGIN: Use Case */
1384
        $contentService = $repository->getContentService();
1385
1386
        $updateStruct = $contentService->newContentUpdateStruct();
1387
        /* END: Use Case */
1388
1389
        $this->assertInstanceOf(
1390
            ContentUpdateStruct::class,
1391
            $updateStruct
1392
        );
1393
1394
        $this->assertPropertiesCorrect(
1395
            [
1396
                'initialLanguageCode' => null,
1397
                'fields' => [],
1398
            ],
1399
            $updateStruct
1400
        );
1401
    }
1402
1403
    /**
1404
     * Test for the updateContent() method.
1405
     *
1406
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1407
     *
1408
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1409
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1410
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1411
     * @group user
1412
     * @group field-type
1413
     */
1414
    public function testUpdateContent()
1415
    {
1416
        /* BEGIN: Use Case */
1417
        $draftVersion2 = $this->createUpdatedDraftVersion2();
1418
        /* END: Use Case */
1419
1420
        $this->assertInstanceOf(
1421
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1422
            $draftVersion2
1423
        );
1424
1425
        $this->assertEquals(
1426
            $this->generateId('user', 10),
1427
            $draftVersion2->versionInfo->creatorId,
1428
            'creatorId is not properly set on new Version'
1429
        );
1430
1431
        return $draftVersion2;
1432
    }
1433
1434
    /**
1435
     * Test for the updateContent_WithDifferentUser() method.
1436
     *
1437
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1438
     *
1439
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1440
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1441
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1442
     * @group user
1443
     * @group field-type
1444
     */
1445
    public function testUpdateContentWithDifferentUser()
1446
    {
1447
        /* BEGIN: Use Case */
1448
        $arrayWithDraftVersion2 = $this->createUpdatedDraftVersion2NotAdmin();
1449
        /* END: Use Case */
1450
1451
        $this->assertInstanceOf(
1452
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1453
            $arrayWithDraftVersion2[0]
1454
        );
1455
1456
        $this->assertEquals(
1457
            $this->generateId('user', $arrayWithDraftVersion2[1]),
1458
            $arrayWithDraftVersion2[0]->versionInfo->creatorId,
1459
            'creatorId is not properly set on new Version'
1460
        );
1461
1462
        return $arrayWithDraftVersion2[0];
1463
    }
1464
1465
    /**
1466
     * Test for the updateContent() method.
1467
     *
1468
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1469
     *
1470
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1471
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1472
     */
1473
    public function testUpdateContentSetsExpectedFields($content)
1474
    {
1475
        $actual = $this->normalizeFields($content->getFields());
1476
1477
        $expected = array(
1478
            new Field(
1479
                array(
1480
                    'id' => 0,
1481
                    'value' => true,
1482
                    'languageCode' => 'eng-GB',
1483
                    'fieldDefIdentifier' => 'description',
1484
                )
1485
            ),
1486
            new Field(
1487
                array(
1488
                    'id' => 0,
1489
                    'value' => true,
1490
                    'languageCode' => 'eng-US',
1491
                    'fieldDefIdentifier' => 'description',
1492
                )
1493
            ),
1494
            new Field(
1495
                array(
1496
                    'id' => 0,
1497
                    'value' => true,
1498
                    'languageCode' => 'eng-GB',
1499
                    'fieldDefIdentifier' => 'name',
1500
                )
1501
            ),
1502
            new Field(
1503
                array(
1504
                    'id' => 0,
1505
                    'value' => true,
1506
                    'languageCode' => 'eng-US',
1507
                    'fieldDefIdentifier' => 'name',
1508
                )
1509
            ),
1510
        );
1511
1512
        $this->assertEquals($expected, $actual);
1513
    }
1514
1515
    /**
1516
     * Test for the updateContent() method.
1517
     *
1518
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1519
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1520
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1521
     */
1522
    public function testUpdateContentThrowsBadStateException()
1523
    {
1524
        $repository = $this->getRepository();
1525
1526
        $contentService = $repository->getContentService();
1527
1528
        /* BEGIN: Use Case */
1529
        $content = $this->createContentVersion1();
1530
1531
        // Now create an update struct and modify some fields
1532
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1533
        $contentUpdateStruct->setField('title', 'An awesome² story about ezp.');
1534
        $contentUpdateStruct->setField('title', 'An awesome²³ story about ezp.', 'eng-GB');
1535
1536
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1537
1538
        // This call will fail with a "BadStateException", because $publishedContent
1539
        // is not a draft.
1540
        $contentService->updateContent(
1541
            $content->getVersionInfo(),
1542
            $contentUpdateStruct
1543
        );
1544
        /* END: Use Case */
1545
    }
1546
1547
    /**
1548
     * Test for the updateContent() method.
1549
     *
1550
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1551
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1552
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1553
     */
1554 View Code Duplication
    public function testUpdateContentThrowsInvalidArgumentExceptionWhenFieldTypeDoesNotAccept()
1555
    {
1556
        $repository = $this->getRepository();
1557
1558
        $contentService = $repository->getContentService();
1559
1560
        /* BEGIN: Use Case */
1561
        $draft = $this->createContentDraftVersion1();
1562
1563
        // Now create an update struct and modify some fields
1564
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1565
        // The name field does not accept a stdClass object as its input
1566
        $contentUpdateStruct->setField('name', new \stdClass(), 'eng-US');
1567
1568
        // Throws an InvalidArgumentException, since the value for field "name"
1569
        // is not accepted
1570
        $contentService->updateContent(
1571
            $draft->getVersionInfo(),
1572
            $contentUpdateStruct
1573
        );
1574
        /* END: Use Case */
1575
    }
1576
1577
    /**
1578
     * Test for the updateContent() method.
1579
     *
1580
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1581
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1582
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1583
     */
1584 View Code Duplication
    public function testUpdateContentWhenMandatoryFieldIsEmpty()
1585
    {
1586
        $repository = $this->getRepository();
1587
1588
        $contentService = $repository->getContentService();
1589
1590
        /* BEGIN: Use Case */
1591
        $draft = $this->createContentDraftVersion1();
1592
1593
        // Now create an update struct and set a mandatory field to null
1594
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1595
        $contentUpdateStruct->setField('name', null);
1596
1597
        // Don't set this, then the above call without languageCode will fail
1598
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1599
1600
        // This call will fail with a "ContentFieldValidationException", because the
1601
        // mandatory "name" field is empty.
1602
        $contentService->updateContent(
1603
            $draft->getVersionInfo(),
1604
            $contentUpdateStruct
1605
        );
1606
        /* END: Use Case */
1607
    }
1608
1609
    /**
1610
     * Test for the updateContent() method.
1611
     *
1612
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1613
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1614
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1615
     */
1616
    public function testUpdateContentThrowsContentFieldValidationException()
1617
    {
1618
        $repository = $this->getRepository();
1619
1620
        /* BEGIN: Use Case */
1621
        $contentTypeService = $repository->getContentTypeService();
1622
        $contentService = $repository->getContentService();
1623
1624
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1625
1626
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1627
        $contentCreate->setField('name', 'An awesome Sidelfingen folder');
1628
1629
        $draft = $contentService->createContent($contentCreate);
1630
1631
        $contentUpdate = $contentService->newContentUpdateStruct();
1632
        // Violates string length constraint
1633
        $contentUpdate->setField('short_name', str_repeat('a', 200), 'eng-US');
1634
1635
        // Throws ContentFieldValidationException because the string length
1636
        // validation of the field "short_name" fails
1637
        $contentService->updateContent($draft->getVersionInfo(), $contentUpdate);
1638
        /* END: Use Case */
1639
    }
1640
1641
    /**
1642
     * Test for the updateContent() method.
1643
     *
1644
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1645
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1646
     */
1647
    public function testUpdateContentWithNotUpdatingMandatoryField()
1648
    {
1649
        $repository = $this->getRepository();
1650
1651
        $contentService = $repository->getContentService();
1652
1653
        /* BEGIN: Use Case */
1654
        $draft = $this->createContentDraftVersion1();
1655
1656
        // Now create an update struct which does not overwrite mandatory
1657
        // fields
1658
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1659
        $contentUpdateStruct->setField(
1660
            'description',
1661
            '<?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"/>'
1662
        );
1663
1664
        // Don't set this, then the above call without languageCode will fail
1665
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1666
1667
        // This will only update the "description" field in the "eng-US"
1668
        // language
1669
        $updatedDraft = $contentService->updateContent(
1670
            $draft->getVersionInfo(),
1671
            $contentUpdateStruct
1672
        );
1673
        /* END: Use Case */
1674
1675
        foreach ($updatedDraft->getFields() as $field) {
1676
            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...
1677
                // Found field
1678
                return;
1679
            }
1680
        }
1681
        $this->fail(
1682
            'Field with identifier "name" in language "eng-US" could not be found or has empty value.'
1683
        );
1684
    }
1685
1686
    /**
1687
     * Test for the createContentDraft() method.
1688
     *
1689
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
1690
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1691
     */
1692
    public function testCreateContentDraftWithSecondParameter()
1693
    {
1694
        $repository = $this->getRepository();
1695
1696
        $contentService = $repository->getContentService();
1697
1698
        /* BEGIN: Use Case */
1699
        $contentVersion2 = $this->createContentVersion2();
1700
1701
        // Now we create a new draft from the initial version
1702
        $draftedContentReloaded = $contentService->createContentDraft(
1703
            $contentVersion2->contentInfo,
1704
            $contentVersion2->getVersionInfo()
1705
        );
1706
        /* END: Use Case */
1707
1708
        $this->assertEquals(3, $draftedContentReloaded->getVersionInfo()->versionNo);
1709
    }
1710
1711
    /**
1712
     * Test for the createContentDraft() method with third parameter.
1713
     *
1714
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1715
     */
1716 View Code Duplication
    public function testCreateContentDraftWithThirdParameter()
1717
    {
1718
        $repository = $this->getRepository();
1719
1720
        $contentService = $repository->getContentService();
1721
1722
        $content = $contentService->loadContent(4);
1723
        $user = $this->createUserVersion1();
1724
1725
        $draftContent = $contentService->createContentDraft(
1726
            $content->contentInfo,
1727
            $content->getVersionInfo(),
1728
            $user
1729
        );
1730
1731
        $this->assertInstanceOf(
1732
            Content::class,
1733
            $draftContent
1734
        );
1735
    }
1736
1737
    /**
1738
     * Test for the publishVersion() method.
1739
     *
1740
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1741
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1742
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1743
     */
1744 View Code Duplication
    public function testPublishVersionFromContentDraft()
1745
    {
1746
        $repository = $this->getRepository();
1747
1748
        $contentService = $repository->getContentService();
1749
1750
        /* BEGIN: Use Case */
1751
        $contentVersion2 = $this->createContentVersion2();
1752
        /* END: Use Case */
1753
1754
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo);
1755
1756
        $this->assertEquals(
1757
            array(
1758
                'status' => VersionInfo::STATUS_PUBLISHED,
1759
                'versionNo' => 2,
1760
            ),
1761
            array(
1762
                'status' => $versionInfo->status,
1763
                'versionNo' => $versionInfo->versionNo,
1764
            )
1765
        );
1766
    }
1767
1768
    /**
1769
     * Test for the publishVersion() method.
1770
     *
1771
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1772
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1773
     */
1774 View Code Duplication
    public function testPublishVersionFromContentDraftArchivesOldVersion()
1775
    {
1776
        $repository = $this->getRepository();
1777
1778
        $contentService = $repository->getContentService();
1779
1780
        /* BEGIN: Use Case */
1781
        $contentVersion2 = $this->createContentVersion2();
1782
        /* END: Use Case */
1783
1784
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo, 1);
1785
1786
        $this->assertEquals(
1787
            array(
1788
                'status' => VersionInfo::STATUS_ARCHIVED,
1789
                'versionNo' => 1,
1790
            ),
1791
            array(
1792
                'status' => $versionInfo->status,
1793
                'versionNo' => $versionInfo->versionNo,
1794
            )
1795
        );
1796
    }
1797
1798
    /**
1799
     * Test for the publishVersion() method.
1800
     *
1801
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1802
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1803
     */
1804
    public function testPublishVersionFromContentDraftUpdatesContentInfoCurrentVersion()
1805
    {
1806
        /* BEGIN: Use Case */
1807
        $contentVersion2 = $this->createContentVersion2();
1808
        /* END: Use Case */
1809
1810
        $this->assertEquals(2, $contentVersion2->contentInfo->currentVersionNo);
1811
    }
1812
1813
    /**
1814
     * Test for the publishVersion() method.
1815
     *
1816
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1817
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1818
     */
1819
    public function testPublishVersionFromOldContentDraftArchivesNewerVersionNo()
1820
    {
1821
        $repository = $this->getRepository();
1822
1823
        $contentService = $repository->getContentService();
1824
1825
        /* BEGIN: Use Case */
1826
        $content = $this->createContentVersion1();
1827
1828
        // Create a new draft with versionNo = 2
1829
        $draftedContentVersion2 = $contentService->createContentDraft($content->contentInfo);
1830
1831
        // Create another new draft with versionNo = 3
1832
        $draftedContentVersion3 = $contentService->createContentDraft($content->contentInfo);
1833
1834
        // Publish draft with versionNo = 3
1835
        $contentService->publishVersion($draftedContentVersion3->getVersionInfo());
1836
1837
        // Publish the first draft with versionNo = 2
1838
        // currentVersionNo is now 2, versionNo 3 will be archived
1839
        $publishedDraft = $contentService->publishVersion($draftedContentVersion2->getVersionInfo());
1840
        /* END: Use Case */
1841
1842
        $this->assertEquals(2, $publishedDraft->contentInfo->currentVersionNo);
1843
    }
1844
1845
    /**
1846
     * Test for the publishVersion() method, and that it creates limited archives.
1847
     *
1848
     * @todo Adapt this when per content type archive limited is added on repository Content Type model.
1849
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1850
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1851
     */
1852
    public function testPublishVersionNotCreatingUnlimitedArchives()
1853
    {
1854
        $repository = $this->getRepository();
1855
1856
        $contentService = $repository->getContentService();
1857
1858
        $content = $this->createContentVersion1();
1859
1860
        // Create a new draft with versionNo = 2
1861
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1862
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1863
1864
        // Create a new draft with versionNo = 3
1865
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1866
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1867
1868
        // Create a new draft with versionNo = 4
1869
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1870
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1871
1872
        // Create a new draft with versionNo = 5
1873
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1874
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1875
1876
        // Create a new draft with versionNo = 6
1877
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1878
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1879
1880
        // Create a new draft with versionNo = 7
1881
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1882
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1883
1884
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
1885
1886
        $this->assertEquals(6, count($versionInfoList));
1887
        $this->assertEquals(2, $versionInfoList[0]->versionNo);
1888
        $this->assertEquals(7, $versionInfoList[5]->versionNo);
1889
1890
        $this->assertEquals(
1891
            [
1892
                VersionInfo::STATUS_ARCHIVED,
1893
                VersionInfo::STATUS_ARCHIVED,
1894
                VersionInfo::STATUS_ARCHIVED,
1895
                VersionInfo::STATUS_ARCHIVED,
1896
                VersionInfo::STATUS_ARCHIVED,
1897
                VersionInfo::STATUS_PUBLISHED,
1898
            ],
1899
            [
1900
                $versionInfoList[0]->status,
1901
                $versionInfoList[1]->status,
1902
                $versionInfoList[2]->status,
1903
                $versionInfoList[3]->status,
1904
                $versionInfoList[4]->status,
1905
                $versionInfoList[5]->status,
1906
            ]
1907
        );
1908
    }
1909
1910
    /**
1911
     * Test for the newContentMetadataUpdateStruct() method.
1912
     *
1913
     * @covers \eZ\Publish\API\Repository\ContentService::newContentMetadataUpdateStruct
1914
     * @group user
1915
     */
1916
    public function testNewContentMetadataUpdateStruct()
1917
    {
1918
        $repository = $this->getRepository();
1919
1920
        /* BEGIN: Use Case */
1921
        $contentService = $repository->getContentService();
1922
1923
        // Creates a new metadata update struct
1924
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1925
1926
        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...
1927
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
1928
        }
1929
1930
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1931
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1932
        $metadataUpdate->alwaysAvailable = false;
1933
        /* END: Use Case */
1934
1935
        $this->assertInstanceOf(
1936
            ContentMetadataUpdateStruct::class,
1937
            $metadataUpdate
1938
        );
1939
    }
1940
1941
    /**
1942
     * Test for the updateContentMetadata() method.
1943
     *
1944
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1945
     *
1946
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1947
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1948
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentMetadataUpdateStruct
1949
     * @group user
1950
     */
1951
    public function testUpdateContentMetadata()
1952
    {
1953
        $repository = $this->getRepository();
1954
1955
        $contentService = $repository->getContentService();
1956
1957
        /* BEGIN: Use Case */
1958
        $content = $this->createContentVersion1();
1959
1960
        // Creates a metadata update struct
1961
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1962
1963
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1964
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1965
        $metadataUpdate->alwaysAvailable = false;
1966
        $metadataUpdate->publishedDate = $this->createDateTime(441759600); // 1984/01/01
1967
        $metadataUpdate->modificationDate = $this->createDateTime(441759600); // 1984/01/01
1968
1969
        // Update the metadata of the published content object
1970
        $content = $contentService->updateContentMetadata(
1971
            $content->contentInfo,
1972
            $metadataUpdate
1973
        );
1974
        /* END: Use Case */
1975
1976
        $this->assertInstanceOf(
1977
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1978
            $content
1979
        );
1980
1981
        return $content;
1982
    }
1983
1984
    /**
1985
     * Test for the updateContentMetadata() method.
1986
     *
1987
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1988
     *
1989
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1990
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
1991
     */
1992
    public function testUpdateContentMetadataSetsExpectedProperties($content)
1993
    {
1994
        $contentInfo = $content->contentInfo;
1995
1996
        $this->assertEquals(
1997
            array(
1998
                'remoteId' => 'aaaabbbbccccddddeeeeffff11112222',
1999
                'sectionId' => $this->generateId('section', 1),
2000
                'alwaysAvailable' => false,
2001
                'currentVersionNo' => 1,
2002
                'mainLanguageCode' => 'eng-GB',
2003
                'modificationDate' => $this->createDateTime(441759600),
2004
                '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...
2005
                'published' => true,
2006
                'publishedDate' => $this->createDateTime(441759600),
2007
            ),
2008
            array(
2009
                'remoteId' => $contentInfo->remoteId,
2010
                'sectionId' => $contentInfo->sectionId,
2011
                'alwaysAvailable' => $contentInfo->alwaysAvailable,
2012
                'currentVersionNo' => $contentInfo->currentVersionNo,
2013
                'mainLanguageCode' => $contentInfo->mainLanguageCode,
2014
                'modificationDate' => $contentInfo->modificationDate,
2015
                'ownerId' => $contentInfo->ownerId,
2016
                'published' => $contentInfo->published,
2017
                'publishedDate' => $contentInfo->publishedDate,
2018
            )
2019
        );
2020
    }
2021
2022
    /**
2023
     * Test for the updateContentMetadata() method.
2024
     *
2025
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2026
     *
2027
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2028
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2029
     */
2030
    public function testUpdateContentMetadataNotUpdatesContentVersion($content)
2031
    {
2032
        $this->assertEquals(1, $content->getVersionInfo()->versionNo);
2033
    }
2034
2035
    /**
2036
     * Test for the updateContentMetadata() method.
2037
     *
2038
     * @covers \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2039
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2040
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2041
     */
2042
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnDuplicateRemoteId()
2043
    {
2044
        $repository = $this->getRepository();
2045
2046
        $contentService = $repository->getContentService();
2047
2048
        /* BEGIN: Use Case */
2049
        // RemoteId of the "Media" page of an eZ Publish demo installation
2050
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2051
2052
        $content = $this->createContentVersion1();
2053
2054
        // Creates a metadata update struct
2055
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2056
        $metadataUpdate->remoteId = $mediaRemoteId;
2057
2058
        // This call will fail with an "InvalidArgumentException", because the
2059
        // specified remoteId is already used by the "Media" page.
2060
        $contentService->updateContentMetadata(
2061
            $content->contentInfo,
2062
            $metadataUpdate
2063
        );
2064
        /* END: Use Case */
2065
    }
2066
2067
    /**
2068
     * Test for the updateContentMetadata() method.
2069
     *
2070
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContentMetadata
2071
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2072
     */
2073
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnNoMetadataPropertiesSet()
2074
    {
2075
        $repository = $this->getRepository();
2076
2077
        $contentService = $repository->getContentService();
2078
2079
        $contentInfo = $contentService->loadContentInfo(4);
2080
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
2081
2082
        // Throws an exception because no properties are set in $contentMetadataUpdateStruct
2083
        $contentService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
2084
    }
2085
2086
    /**
2087
     * Test for the deleteContent() method.
2088
     *
2089
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2090
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2091
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2092
     */
2093 View Code Duplication
    public function testDeleteContent()
2094
    {
2095
        $repository = $this->getRepository();
2096
2097
        $contentService = $repository->getContentService();
2098
        $locationService = $repository->getLocationService();
2099
2100
        /* BEGIN: Use Case */
2101
        $contentVersion2 = $this->createContentVersion2();
2102
2103
        // Load the locations for this content object
2104
        $locations = $locationService->loadLocations($contentVersion2->contentInfo);
2105
2106
        // This will delete the content, all versions and the associated locations
2107
        $contentService->deleteContent($contentVersion2->contentInfo);
2108
        /* END: Use Case */
2109
2110
        foreach ($locations as $location) {
2111
            $locationService->loadLocation($location->id);
2112
        }
2113
    }
2114
2115
    /**
2116
     * Test for the deleteContent() method.
2117
     *
2118
     * Test for issue EZP-21057:
2119
     * "contentService: Unable to delete a content with an empty file attribute"
2120
     *
2121
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2122
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2123
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2124
     */
2125 View Code Duplication
    public function testDeleteContentWithEmptyBinaryField()
2126
    {
2127
        $repository = $this->getRepository();
2128
2129
        $contentService = $repository->getContentService();
2130
        $locationService = $repository->getLocationService();
2131
2132
        /* BEGIN: Use Case */
2133
        $contentVersion = $this->createContentVersion1EmptyBinaryField();
2134
2135
        // Load the locations for this content object
2136
        $locations = $locationService->loadLocations($contentVersion->contentInfo);
2137
2138
        // This will delete the content, all versions and the associated locations
2139
        $contentService->deleteContent($contentVersion->contentInfo);
2140
        /* END: Use Case */
2141
2142
        foreach ($locations as $location) {
2143
            $locationService->loadLocation($location->id);
2144
        }
2145
    }
2146
2147
    /**
2148
     * Test for the loadContentDrafts() method.
2149
     *
2150
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2151
     */
2152
    public function testLoadContentDraftsReturnsEmptyArrayByDefault()
2153
    {
2154
        $repository = $this->getRepository();
2155
2156
        /* BEGIN: Use Case */
2157
        $contentService = $repository->getContentService();
2158
2159
        $contentDrafts = $contentService->loadContentDrafts();
2160
        /* END: Use Case */
2161
2162
        $this->assertSame(array(), $contentDrafts);
2163
    }
2164
2165
    /**
2166
     * Test for the loadContentDrafts() method.
2167
     *
2168
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2169
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2170
     */
2171
    public function testLoadContentDrafts()
2172
    {
2173
        $repository = $this->getRepository();
2174
2175
        /* BEGIN: Use Case */
2176
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
2177
        // of a eZ Publish demo installation.
2178
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2179
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
2180
2181
        $contentService = $repository->getContentService();
2182
2183
        // "Media" content object
2184
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2185
2186
        // "eZ Publish Demo Design ..." content object
2187
        $demoDesignContentInfo = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
2188
2189
        // Create some drafts
2190
        $contentService->createContentDraft($mediaContentInfo);
2191
        $contentService->createContentDraft($demoDesignContentInfo);
2192
2193
        // Now $contentDrafts should contain two drafted versions
2194
        $draftedVersions = $contentService->loadContentDrafts();
2195
        /* END: Use Case */
2196
2197
        $actual = array(
2198
            $draftedVersions[0]->status,
2199
            $draftedVersions[0]->getContentInfo()->remoteId,
2200
            $draftedVersions[1]->status,
2201
            $draftedVersions[1]->getContentInfo()->remoteId,
2202
        );
2203
        sort($actual, SORT_STRING);
2204
2205
        $this->assertEquals(
2206
            array(
2207
                VersionInfo::STATUS_DRAFT,
2208
                VersionInfo::STATUS_DRAFT,
2209
                $demoDesignRemoteId,
2210
                $mediaRemoteId,
2211
            ),
2212
            $actual
2213
        );
2214
    }
2215
2216
    /**
2217
     * Test for the loadContentDrafts() method.
2218
     *
2219
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
2220
     */
2221
    public function testLoadContentDraftsWithFirstParameter()
2222
    {
2223
        $repository = $this->getRepository();
2224
2225
        /* BEGIN: Use Case */
2226
        $user = $this->createUserVersion1();
2227
2228
        // Get current user
2229
        $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...
2230
2231
        // Set new editor as user
2232
        $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...
2233
2234
        // Remote id of the "Media" content object in an eZ Publish demo installation.
2235
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2236
2237
        $contentService = $repository->getContentService();
2238
2239
        // "Media" content object
2240
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2241
2242
        // Create a content draft
2243
        $contentService->createContentDraft($mediaContentInfo);
2244
2245
        // Reset to previous current user
2246
        $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...
2247
2248
        // Now $contentDrafts for the previous current user and the new user
2249
        $newCurrentUserDrafts = $contentService->loadContentDrafts($user);
2250
        $oldCurrentUserDrafts = $contentService->loadContentDrafts($oldCurrentUser);
2251
        /* END: Use Case */
2252
2253
        $this->assertSame(array(), $oldCurrentUserDrafts);
2254
2255
        $this->assertEquals(
2256
            array(
2257
                VersionInfo::STATUS_DRAFT,
2258
                $mediaRemoteId,
2259
            ),
2260
            array(
2261
                $newCurrentUserDrafts[0]->status,
2262
                $newCurrentUserDrafts[0]->getContentInfo()->remoteId,
2263
            )
2264
        );
2265
    }
2266
2267
    /**
2268
     * Test for the loadVersionInfo() method.
2269
     *
2270
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2271
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2272
     */
2273
    public function testLoadVersionInfoWithSecondParameter()
2274
    {
2275
        $repository = $this->getRepository();
2276
2277
        $contentService = $repository->getContentService();
2278
2279
        /* BEGIN: Use Case */
2280
        $publishedContent = $this->createContentVersion1();
2281
2282
        $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...
2283
2284
        // Will return the VersionInfo of the $draftContent
2285
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2286
        /* END: Use Case */
2287
2288
        $this->assertEquals(2, $versionInfo->versionNo);
2289
2290
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2291
        $this->assertEquals(
2292
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2293
            $versionInfo->getContentInfo()->mainLocationId
2294
        );
2295
    }
2296
2297
    /**
2298
     * Test for the loadVersionInfo() method.
2299
     *
2300
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2301
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2302
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2303
     */
2304
    public function testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter()
2305
    {
2306
        $repository = $this->getRepository();
2307
2308
        $contentService = $repository->getContentService();
2309
2310
        /* BEGIN: Use Case */
2311
        $draft = $this->createContentDraftVersion1();
2312
2313
        // This call will fail with a "NotFoundException", because not versionNo
2314
        // 2 exists for this content object.
2315
        $contentService->loadVersionInfo($draft->contentInfo, 2);
2316
        /* END: Use Case */
2317
    }
2318
2319
    /**
2320
     * Test for the loadVersionInfoById() method.
2321
     *
2322
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2323
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2324
     */
2325
    public function testLoadVersionInfoByIdWithSecondParameter()
2326
    {
2327
        $repository = $this->getRepository();
2328
2329
        $contentService = $repository->getContentService();
2330
2331
        /* BEGIN: Use Case */
2332
        $publishedContent = $this->createContentVersion1();
2333
2334
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
2335
2336
        // Will return the VersionInfo of the $draftContent
2337
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2338
        /* END: Use Case */
2339
2340
        $this->assertEquals(2, $versionInfo->versionNo);
2341
2342
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2343
        $this->assertEquals(
2344
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2345
            $versionInfo->getContentInfo()->mainLocationId
2346
        );
2347
2348
        return [
2349
            'versionInfo' => $versionInfo,
2350
            'draftContent' => $draftContent,
2351
        ];
2352
    }
2353
2354
    /**
2355
     * Test for the returned value of the loadVersionInfoById() method.
2356
     *
2357
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter
2358
     * @covers \eZ\Publish\API\Repository\ContentService::loadVersionInfoById
2359
     *
2360
     * @param array $data
2361
     */
2362
    public function testLoadVersionInfoByIdWithSecondParameterSetsExpectedVersionInfo(array $data)
2363
    {
2364
        /** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo */
2365
        $versionInfo = $data['versionInfo'];
2366
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $draftContent */
2367
        $draftContent = $data['draftContent'];
2368
2369
        $this->assertPropertiesCorrect(
2370
            [
2371
                'names' => [
2372
                    'eng-US' => 'An awesome forum',
2373
                ],
2374
                'contentInfo' => new ContentInfo([
2375
                    'id' => $draftContent->contentInfo->id,
2376
                    'contentTypeId' => 28,
2377
                    'name' => 'An awesome forum',
2378
                    'sectionId' => 1,
2379
                    'currentVersionNo' => 1,
2380
                    'published' => true,
2381
                    'ownerId' => 14,
2382
                    // this Content Object is created at the test runtime
2383
                    'modificationDate' => $versionInfo->contentInfo->modificationDate,
2384
                    'publishedDate' => $versionInfo->contentInfo->publishedDate,
2385
                    'alwaysAvailable' => 1,
2386
                    'remoteId' => 'abcdef0123456789abcdef0123456789',
2387
                    'mainLanguageCode' => 'eng-US',
2388
                    'mainLocationId' => $draftContent->contentInfo->mainLocationId,
2389
                ]),
2390
                'id' => $draftContent->versionInfo->id,
2391
                'versionNo' => 2,
2392
                'creatorId' => 14,
2393
                'status' => 0,
2394
                'initialLanguageCode' => 'eng-US',
2395
                'languageCodes' => [
2396
                    'eng-US',
2397
                ],
2398
            ],
2399
            $versionInfo
2400
        );
2401
    }
2402
2403
    /**
2404
     * Test for the loadVersionInfoById() method.
2405
     *
2406
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2407
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2408
     */
2409 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter()
2410
    {
2411
        $repository = $this->getRepository();
2412
2413
        $contentService = $repository->getContentService();
2414
2415
        /* BEGIN: Use Case */
2416
        $content = $this->createContentVersion1();
2417
2418
        // This call will fail with a "NotFoundException", because not versionNo
2419
        // 2 exists for this content object.
2420
        $contentService->loadVersionInfoById($content->id, 2);
2421
        /* END: Use Case */
2422
    }
2423
2424
    /**
2425
     * Test for the loadContentByVersionInfo() method.
2426
     *
2427
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
2428
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2429
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
2430
     */
2431
    public function testLoadContentByVersionInfoWithSecondParameter()
2432
    {
2433
        $repository = $this->getRepository();
2434
2435
        $sectionId = $this->generateId('section', 1);
2436
        /* BEGIN: Use Case */
2437
        $contentTypeService = $repository->getContentTypeService();
2438
2439
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2440
2441
        $contentService = $repository->getContentService();
2442
2443
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2444
2445
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2446
2447
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2448
2449
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2450
        // $sectionId contains the ID of section 1
2451
        $contentCreateStruct->sectionId = $sectionId;
2452
        $contentCreateStruct->alwaysAvailable = true;
2453
2454
        // Create a new content draft
2455
        $content = $contentService->createContent($contentCreateStruct);
2456
2457
        // Now publish this draft
2458
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2459
2460
        // Will return a content instance with fields in "eng-US"
2461
        $reloadedContent = $contentService->loadContentByVersionInfo(
2462
            $publishedContent->getVersionInfo(),
2463
            array(
2464
                'eng-GB',
2465
            ),
2466
            false
2467
        );
2468
        /* END: Use Case */
2469
2470
        $actual = array();
2471 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...
2472
            $actual[] = new Field(
2473
                array(
2474
                    'id' => 0,
2475
                    '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...
2476
                    'languageCode' => $field->languageCode,
2477
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
2478
                )
2479
            );
2480
        }
2481
        usort(
2482
            $actual,
2483 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...
2484
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
2485
                    return strcasecmp($field1->languageCode, $field2->languageCode);
2486
                }
2487
2488
                return $return;
2489
            }
2490
        );
2491
2492
        $expected = array(
2493
            new Field(
2494
                array(
2495
                    'id' => 0,
2496
                    'value' => true,
2497
                    'languageCode' => 'eng-GB',
2498
                    'fieldDefIdentifier' => 'description',
2499
                )
2500
            ),
2501
            new Field(
2502
                array(
2503
                    'id' => 0,
2504
                    'value' => true,
2505
                    'languageCode' => 'eng-GB',
2506
                    'fieldDefIdentifier' => 'name',
2507
                )
2508
            ),
2509
        );
2510
2511
        $this->assertEquals($expected, $actual);
2512
    }
2513
2514
    /**
2515
     * Test for the loadContentByContentInfo() method.
2516
     *
2517
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages)
2518
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2519
     */
2520
    public function testLoadContentByContentInfoWithLanguageParameters()
2521
    {
2522
        $repository = $this->getRepository();
2523
2524
        $sectionId = $this->generateId('section', 1);
2525
        /* BEGIN: Use Case */
2526
        $contentTypeService = $repository->getContentTypeService();
2527
2528
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2529
2530
        $contentService = $repository->getContentService();
2531
2532
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2533
2534
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2535
2536
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2537
2538
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2539
        // $sectionId contains the ID of section 1
2540
        $contentCreateStruct->sectionId = $sectionId;
2541
        $contentCreateStruct->alwaysAvailable = true;
2542
2543
        // Create a new content draft
2544
        $content = $contentService->createContent($contentCreateStruct);
2545
2546
        // Now publish this draft
2547
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2548
2549
        // Will return a content instance with fields in "eng-US"
2550
        $reloadedContent = $contentService->loadContentByContentInfo(
2551
            $publishedContent->contentInfo,
2552
            array(
2553
                'eng-US',
2554
            ),
2555
            null,
2556
            false
2557
        );
2558
        /* END: Use Case */
2559
2560
        $actual = $this->normalizeFields($reloadedContent->getFields());
2561
2562
        $expected = array(
2563
            new Field(
2564
                array(
2565
                    'id' => 0,
2566
                    'value' => true,
2567
                    'languageCode' => 'eng-US',
2568
                    'fieldDefIdentifier' => 'description',
2569
                )
2570
            ),
2571
            new Field(
2572
                array(
2573
                    'id' => 0,
2574
                    'value' => true,
2575
                    'languageCode' => 'eng-US',
2576
                    'fieldDefIdentifier' => 'name',
2577
                )
2578
            ),
2579
        );
2580
2581
        $this->assertEquals($expected, $actual);
2582
2583
        // Will return a content instance with fields in "eng-GB" (versions prior to 6.0.0-beta9 returned "eng-US" also)
2584
        $reloadedContent = $contentService->loadContentByContentInfo(
2585
            $publishedContent->contentInfo,
2586
            array(
2587
                'eng-GB',
2588
            ),
2589
            null,
2590
            true
2591
        );
2592
2593
        $actual = $this->normalizeFields($reloadedContent->getFields());
2594
2595
        $expected = array(
2596
            new Field(
2597
                array(
2598
                    'id' => 0,
2599
                    'value' => true,
2600
                    'languageCode' => 'eng-GB',
2601
                    'fieldDefIdentifier' => 'description',
2602
                )
2603
            ),
2604
            new Field(
2605
                array(
2606
                    'id' => 0,
2607
                    'value' => true,
2608
                    'languageCode' => 'eng-GB',
2609
                    'fieldDefIdentifier' => 'name',
2610
                )
2611
            ),
2612
        );
2613
2614
        $this->assertEquals($expected, $actual);
2615
2616
        // Will return a content instance with fields in main language "eng-US", as "fre-FR" does not exists
2617
        $reloadedContent = $contentService->loadContentByContentInfo(
2618
            $publishedContent->contentInfo,
2619
            array(
2620
                'fre-FR',
2621
            ),
2622
            null,
2623
            true
2624
        );
2625
2626
        $actual = $this->normalizeFields($reloadedContent->getFields());
2627
2628
        $expected = array(
2629
            new Field(
2630
                array(
2631
                    'id' => 0,
2632
                    'value' => true,
2633
                    'languageCode' => 'eng-US',
2634
                    'fieldDefIdentifier' => 'description',
2635
                )
2636
            ),
2637
            new Field(
2638
                array(
2639
                    'id' => 0,
2640
                    'value' => true,
2641
                    'languageCode' => 'eng-US',
2642
                    'fieldDefIdentifier' => 'name',
2643
                )
2644
            ),
2645
        );
2646
2647
        $this->assertEquals($expected, $actual);
2648
    }
2649
2650
    /**
2651
     * Test for the loadContentByContentInfo() method.
2652
     *
2653
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2654
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2655
     */
2656 View Code Duplication
    public function testLoadContentByContentInfoWithVersionNumberParameter()
2657
    {
2658
        $repository = $this->getRepository();
2659
2660
        $contentService = $repository->getContentService();
2661
2662
        /* BEGIN: Use Case */
2663
        $publishedContent = $this->createContentVersion1();
2664
2665
        $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...
2666
2667
        // This content instance is identical to $draftContent
2668
        $draftContentReloaded = $contentService->loadContentByContentInfo(
2669
            $publishedContent->contentInfo,
2670
            null,
2671
            2
2672
        );
2673
        /* END: Use Case */
2674
2675
        $this->assertEquals(
2676
            2,
2677
            $draftContentReloaded->getVersionInfo()->versionNo
2678
        );
2679
2680
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2681
        $this->assertEquals(
2682
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2683
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2684
        );
2685
    }
2686
2687
    /**
2688
     * Test for the loadContentByContentInfo() method.
2689
     *
2690
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2691
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2692
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithVersionNumberParameter
2693
     */
2694 View Code Duplication
    public function testLoadContentByContentInfoThrowsNotFoundExceptionWithVersionNumberParameter()
2695
    {
2696
        $repository = $this->getRepository();
2697
2698
        $contentService = $repository->getContentService();
2699
2700
        /* BEGIN: Use Case */
2701
        $content = $this->createContentVersion1();
2702
2703
        // This call will fail with a "NotFoundException", because no content
2704
        // with versionNo = 2 exists.
2705
        $contentService->loadContentByContentInfo($content->contentInfo, null, 2);
2706
        /* END: Use Case */
2707
    }
2708
2709
    /**
2710
     * Test for the loadContent() method.
2711
     *
2712
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages)
2713
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2714
     */
2715 View Code Duplication
    public function testLoadContentWithSecondParameter()
2716
    {
2717
        $repository = $this->getRepository();
2718
2719
        $contentService = $repository->getContentService();
2720
2721
        /* BEGIN: Use Case */
2722
        $draft = $this->createMultipleLanguageDraftVersion1();
2723
2724
        // This draft contains those fields localized with "eng-GB"
2725
        $draftLocalized = $contentService->loadContent($draft->id, array('eng-GB'), null, false);
2726
        /* END: Use Case */
2727
2728
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2729
2730
        return $draft;
2731
    }
2732
2733
    /**
2734
     * Test for the loadContent() method using undefined translation.
2735
     *
2736
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithSecondParameter
2737
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2738
     *
2739
     * @param \eZ\Publish\API\Repository\Values\Content\Content $contentDraft
2740
     */
2741
    public function testLoadContentWithSecondParameterThrowsNotFoundException(Content $contentDraft)
2742
    {
2743
        $repository = $this->getRepository();
2744
2745
        $contentService = $repository->getContentService();
2746
2747
        $contentService->loadContent($contentDraft->id, array('ger-DE'), null, false);
2748
    }
2749
2750
    /**
2751
     * Test for the loadContent() method.
2752
     *
2753
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2754
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2755
     */
2756 View Code Duplication
    public function testLoadContentWithThirdParameter()
2757
    {
2758
        $repository = $this->getRepository();
2759
2760
        $contentService = $repository->getContentService();
2761
2762
        /* BEGIN: Use Case */
2763
        $publishedContent = $this->createContentVersion1();
2764
2765
        $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...
2766
2767
        // This content instance is identical to $draftContent
2768
        $draftContentReloaded = $contentService->loadContent($publishedContent->id, null, 2);
2769
        /* END: Use Case */
2770
2771
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2772
2773
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2774
        $this->assertEquals(
2775
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2776
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2777
        );
2778
    }
2779
2780
    /**
2781
     * Test for the loadContent() method.
2782
     *
2783
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2784
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2785
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithThirdParameter
2786
     */
2787 View Code Duplication
    public function testLoadContentThrowsNotFoundExceptionWithThirdParameter()
2788
    {
2789
        $repository = $this->getRepository();
2790
2791
        $contentService = $repository->getContentService();
2792
2793
        /* BEGIN: Use Case */
2794
        $content = $this->createContentVersion1();
2795
2796
        // This call will fail with a "NotFoundException", because for this
2797
        // content object no versionNo=2 exists.
2798
        $contentService->loadContent($content->id, null, 2);
2799
        /* END: Use Case */
2800
    }
2801
2802
    /**
2803
     * Test for the loadContentByRemoteId() method.
2804
     *
2805
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages)
2806
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2807
     */
2808 View Code Duplication
    public function testLoadContentByRemoteIdWithSecondParameter()
2809
    {
2810
        $repository = $this->getRepository();
2811
2812
        $contentService = $repository->getContentService();
2813
2814
        /* BEGIN: Use Case */
2815
        $draft = $this->createMultipleLanguageDraftVersion1();
2816
2817
        $contentService->publishVersion($draft->versionInfo);
2818
2819
        // This draft contains those fields localized with "eng-GB"
2820
        $draftLocalized = $contentService->loadContentByRemoteId(
2821
            $draft->contentInfo->remoteId,
2822
            array('eng-GB'),
2823
            null,
2824
            false
2825
        );
2826
        /* END: Use Case */
2827
2828
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2829
    }
2830
2831
    /**
2832
     * Test for the loadContentByRemoteId() method.
2833
     *
2834
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2835
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2836
     */
2837 View Code Duplication
    public function testLoadContentByRemoteIdWithThirdParameter()
2838
    {
2839
        $repository = $this->getRepository();
2840
2841
        $contentService = $repository->getContentService();
2842
2843
        /* BEGIN: Use Case */
2844
        $publishedContent = $this->createContentVersion1();
2845
2846
        $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...
2847
2848
        // This content instance is identical to $draftContent
2849
        $draftContentReloaded = $contentService->loadContentByRemoteId(
2850
            $publishedContent->contentInfo->remoteId,
2851
            null,
2852
            2
2853
        );
2854
        /* END: Use Case */
2855
2856
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2857
2858
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2859
        $this->assertEquals(
2860
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2861
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2862
        );
2863
    }
2864
2865
    /**
2866
     * Test for the loadContentByRemoteId() method.
2867
     *
2868
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2869
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2870
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithThirdParameter
2871
     */
2872
    public function testLoadContentByRemoteIdThrowsNotFoundExceptionWithThirdParameter()
2873
    {
2874
        $repository = $this->getRepository();
2875
2876
        $contentService = $repository->getContentService();
2877
2878
        /* BEGIN: Use Case */
2879
        $content = $this->createContentVersion1();
2880
2881
        // This call will fail with a "NotFoundException", because for this
2882
        // content object no versionNo=2 exists.
2883
        $contentService->loadContentByRemoteId(
2884
            $content->contentInfo->remoteId,
2885
            null,
2886
            2
2887
        );
2888
        /* END: Use Case */
2889
    }
2890
2891
    /**
2892
     * Test that retrieval of translated name field respects prioritized language list.
2893
     *
2894
     * @dataProvider getPrioritizedLanguageList
2895
     * @param string[]|null $languageCodes
2896
     */
2897
    public function testLoadContentWithPrioritizedLanguagesList($languageCodes)
2898
    {
2899
        $repository = $this->getRepository();
2900
2901
        $contentService = $repository->getContentService();
2902
2903
        $content = $this->createContentVersion2();
2904
2905
        $content = $contentService->loadContent($content->id, $languageCodes);
2906
2907
        $expectedName = $content->getVersionInfo()->getName(
2908
            isset($languageCodes[0]) ? $languageCodes[0] : null
2909
        );
2910
        $nameValue = $content->getFieldValue('name');
2911
        $name = $content->getVersionInfo()->getName();
2912
        /** @var \eZ\Publish\Core\FieldType\TextLine\Value $nameValue */
2913
        self::assertEquals($expectedName, $nameValue->text);
2914
        self::assertEquals($expectedName, $name);
2915
    }
2916
2917
    /**
2918
     * @return array
2919
     */
2920
    public function getPrioritizedLanguageList()
2921
    {
2922
        return [
2923
            [['eng-US']],
2924
            [['eng-GB']],
2925
            [['eng-GB', 'eng-US']],
2926
            [['eng-US', 'eng-GB']],
2927
        ];
2928
    }
2929
2930
    /**
2931
     * Test for the deleteVersion() method.
2932
     *
2933
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2934
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2935
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2936
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2937
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2938
     */
2939
    public function testDeleteVersion()
2940
    {
2941
        $repository = $this->getRepository();
2942
2943
        $contentService = $repository->getContentService();
2944
2945
        /* BEGIN: Use Case */
2946
        $content = $this->createContentVersion1();
2947
2948
        // Create new draft, because published or last version of the Content can't be deleted
2949
        $draft = $contentService->createContentDraft(
2950
            $content->getVersionInfo()->getContentInfo()
2951
        );
2952
2953
        // Delete the previously created draft
2954
        $contentService->deleteVersion($draft->getVersionInfo());
2955
        /* END: Use Case */
2956
2957
        $versions = $contentService->loadVersions($content->getVersionInfo()->getContentInfo());
2958
2959
        $this->assertCount(1, $versions);
2960
        $this->assertEquals(
2961
            $content->getVersionInfo()->id,
2962
            $versions[0]->id
2963
        );
2964
    }
2965
2966
    /**
2967
     * Test for the deleteVersion() method.
2968
     *
2969
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2970
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
2971
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2972
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2973
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2974
     */
2975
    public function testDeleteVersionThrowsBadStateExceptionOnPublishedVersion()
2976
    {
2977
        $repository = $this->getRepository();
2978
2979
        $contentService = $repository->getContentService();
2980
2981
        /* BEGIN: Use Case */
2982
        $content = $this->createContentVersion1();
2983
2984
        // This call will fail with a "BadStateException", because the content
2985
        // version is currently published.
2986
        $contentService->deleteVersion($content->getVersionInfo());
2987
        /* END: Use Case */
2988
    }
2989
2990
    /**
2991
     * Test for the deleteVersion() method.
2992
     *
2993
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2994
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
2995
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2996
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2997
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2998
     */
2999 View Code Duplication
    public function testDeleteVersionThrowsBadStateExceptionOnLastVersion()
3000
    {
3001
        $repository = $this->getRepository();
3002
3003
        $contentService = $repository->getContentService();
3004
3005
        /* BEGIN: Use Case */
3006
        $draft = $this->createContentDraftVersion1();
3007
3008
        // This call will fail with a "BadStateException", because the Content
3009
        // version is the last version of the Content.
3010
        $contentService->deleteVersion($draft->getVersionInfo());
3011
        /* END: Use Case */
3012
    }
3013
3014
    /**
3015
     * Test for the loadVersions() method.
3016
     *
3017
     * @see \eZ\Publish\API\Repository\ContentService::loadVersions()
3018
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3019
     *
3020
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[]
3021
     */
3022
    public function testLoadVersions()
3023
    {
3024
        $repository = $this->getRepository();
3025
3026
        $contentService = $repository->getContentService();
3027
3028
        /* BEGIN: Use Case */
3029
        $contentVersion2 = $this->createContentVersion2();
3030
3031
        // Load versions of this ContentInfo instance
3032
        $versions = $contentService->loadVersions($contentVersion2->contentInfo);
3033
        /* END: Use Case */
3034
3035
        $expectedVersionsOrder = [
3036
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1),
3037
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 2),
3038
        ];
3039
3040
        $this->assertEquals($expectedVersionsOrder, $versions);
3041
3042
        return $versions;
3043
    }
3044
3045
    /**
3046
     * Test for the loadVersions() method.
3047
     *
3048
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersions
3049
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersions
3050
     *
3051
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo[] $versions
3052
     */
3053
    public function testLoadVersionsSetsExpectedVersionInfo(array $versions)
3054
    {
3055
        $this->assertCount(2, $versions);
3056
3057
        $expectedVersions = [
3058
            [
3059
                'versionNo' => 1,
3060
                'creatorId' => 14,
3061
                'status' => VersionInfo::STATUS_ARCHIVED,
3062
                'initialLanguageCode' => 'eng-US',
3063
                'languageCodes' => ['eng-US'],
3064
            ],
3065
            [
3066
                'versionNo' => 2,
3067
                'creatorId' => 10,
3068
                'status' => VersionInfo::STATUS_PUBLISHED,
3069
                'initialLanguageCode' => 'eng-US',
3070
                'languageCodes' => ['eng-US', 'eng-GB'],
3071
            ],
3072
        ];
3073
3074
        $this->assertPropertiesCorrect($expectedVersions[0], $versions[0]);
3075
        $this->assertPropertiesCorrect($expectedVersions[1], $versions[1]);
3076
        $this->assertEquals(
3077
            $versions[0]->creationDate->getTimestamp(),
3078
            $versions[1]->creationDate->getTimestamp(),
3079
            'Creation time did not match within delta of 2 seconds',
3080
            2
3081
        );
3082
        $this->assertEquals(
3083
            $versions[0]->modificationDate->getTimestamp(),
3084
            $versions[1]->modificationDate->getTimestamp(),
3085
            'Creation time did not match within delta of 2 seconds',
3086
            2
3087
        );
3088
    }
3089
3090
    /**
3091
     * Test for the copyContent() method.
3092
     *
3093
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
3094
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3095
     * @group field-type
3096
     */
3097 View Code Duplication
    public function testCopyContent()
3098
    {
3099
        $parentLocationId = $this->generateId('location', 56);
3100
3101
        $repository = $this->getRepository();
3102
3103
        $contentService = $repository->getContentService();
3104
        $locationService = $repository->getLocationService();
3105
3106
        /* BEGIN: Use Case */
3107
        $contentVersion2 = $this->createMultipleLanguageContentVersion2();
3108
3109
        // Configure new target location
3110
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
3111
3112
        $targetLocationCreate->priority = 42;
3113
        $targetLocationCreate->hidden = true;
3114
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3115
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3116
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3117
3118
        // Copy content with all versions and drafts
3119
        $contentCopied = $contentService->copyContent(
3120
            $contentVersion2->contentInfo,
3121
            $targetLocationCreate
3122
        );
3123
        /* END: Use Case */
3124
3125
        $this->assertInstanceOf(
3126
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
3127
            $contentCopied
3128
        );
3129
3130
        $this->assertNotEquals(
3131
            $contentVersion2->contentInfo->remoteId,
3132
            $contentCopied->contentInfo->remoteId
3133
        );
3134
3135
        $this->assertNotEquals(
3136
            $contentVersion2->id,
3137
            $contentCopied->id
3138
        );
3139
3140
        $this->assertEquals(
3141
            2,
3142
            count($contentService->loadVersions($contentCopied->contentInfo))
3143
        );
3144
3145
        $this->assertEquals(2, $contentCopied->getVersionInfo()->versionNo);
3146
3147
        $this->assertAllFieldsEquals($contentCopied->getFields());
3148
3149
        $this->assertDefaultContentStates($contentCopied->contentInfo);
3150
3151
        $this->assertNotNull(
3152
            $contentCopied->contentInfo->mainLocationId,
3153
            'Expected main location to be set given we provided a LocationCreateStruct'
3154
        );
3155
    }
3156
3157
    /**
3158
     * Test for the copyContent() method.
3159
     *
3160
     * @see \eZ\Publish\API\Repository\ContentService::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo)
3161
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
3162
     *
3163
     * @todo Fix to more descriptive name
3164
     */
3165 View Code Duplication
    public function testCopyContentWithThirdParameter()
3166
    {
3167
        $parentLocationId = $this->generateId('location', 56);
3168
3169
        $repository = $this->getRepository();
3170
3171
        $contentService = $repository->getContentService();
3172
        $locationService = $repository->getLocationService();
3173
3174
        /* BEGIN: Use Case */
3175
        $contentVersion2 = $this->createContentVersion2();
3176
3177
        // Configure new target location
3178
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
3179
3180
        $targetLocationCreate->priority = 42;
3181
        $targetLocationCreate->hidden = true;
3182
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3183
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3184
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3185
3186
        // Copy only the initial version
3187
        $contentCopied = $contentService->copyContent(
3188
            $contentVersion2->contentInfo,
3189
            $targetLocationCreate,
3190
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1)
3191
        );
3192
        /* END: Use Case */
3193
3194
        $this->assertInstanceOf(
3195
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
3196
            $contentCopied
3197
        );
3198
3199
        $this->assertNotEquals(
3200
            $contentVersion2->contentInfo->remoteId,
3201
            $contentCopied->contentInfo->remoteId
3202
        );
3203
3204
        $this->assertNotEquals(
3205
            $contentVersion2->id,
3206
            $contentCopied->id
3207
        );
3208
3209
        $this->assertEquals(
3210
            1,
3211
            count($contentService->loadVersions($contentCopied->contentInfo))
3212
        );
3213
3214
        $this->assertEquals(1, $contentCopied->getVersionInfo()->versionNo);
3215
3216
        $this->assertNotNull(
3217
            $contentCopied->contentInfo->mainLocationId,
3218
            'Expected main location to be set given we provided a LocationCreateStruct'
3219
        );
3220
    }
3221
3222
    /**
3223
     * Test for the addRelation() method.
3224
     *
3225
     * @return \eZ\Publish\API\Repository\Values\Content\Content
3226
     *
3227
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3228
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3229
     */
3230
    public function testAddRelation()
3231
    {
3232
        $repository = $this->getRepository();
3233
3234
        $contentService = $repository->getContentService();
3235
3236
        /* BEGIN: Use Case */
3237
        // RemoteId of the "Media" content of an eZ Publish demo installation
3238
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3239
3240
        $draft = $this->createContentDraftVersion1();
3241
3242
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3243
3244
        // Create relation between new content object and "Media" page
3245
        $relation = $contentService->addRelation(
3246
            $draft->getVersionInfo(),
3247
            $media
3248
        );
3249
        /* END: Use Case */
3250
3251
        $this->assertInstanceOf(
3252
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Relation',
3253
            $relation
3254
        );
3255
3256
        return $contentService->loadRelations($draft->getVersionInfo());
3257
    }
3258
3259
    /**
3260
     * Test for the addRelation() method.
3261
     *
3262
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3263
     *
3264
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3265
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3266
     */
3267
    public function testAddRelationAddsRelationToContent($relations)
3268
    {
3269
        $this->assertEquals(
3270
            1,
3271
            count($relations)
3272
        );
3273
    }
3274
3275
    /**
3276
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3277
     */
3278
    protected function assertExpectedRelations($relations)
3279
    {
3280
        $this->assertEquals(
3281
            array(
3282
                'type' => Relation::COMMON,
3283
                'sourceFieldDefinitionIdentifier' => null,
3284
                'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3285
                'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3286
            ),
3287
            array(
3288
                'type' => $relations[0]->type,
3289
                'sourceFieldDefinitionIdentifier' => $relations[0]->sourceFieldDefinitionIdentifier,
3290
                'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3291
                'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3292
            )
3293
        );
3294
    }
3295
3296
    /**
3297
     * Test for the addRelation() method.
3298
     *
3299
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3300
     *
3301
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3302
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3303
     */
3304
    public function testAddRelationSetsExpectedRelations($relations)
3305
    {
3306
        $this->assertExpectedRelations($relations);
3307
    }
3308
3309
    /**
3310
     * Test for the createContentDraft() method.
3311
     *
3312
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3313
     *
3314
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
3315
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelationSetsExpectedRelations
3316
     */
3317 View Code Duplication
    public function testCreateContentDraftWithRelations()
3318
    {
3319
        $repository = $this->getRepository();
3320
3321
        $contentService = $repository->getContentService();
3322
3323
        // RemoteId of the "Media" content of an eZ Publish demo installation
3324
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3325
        $draft = $this->createContentDraftVersion1();
3326
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3327
3328
        // Create relation between new content object and "Media" page
3329
        $contentService->addRelation(
3330
            $draft->getVersionInfo(),
3331
            $media
3332
        );
3333
3334
        $content = $contentService->publishVersion($draft->versionInfo);
3335
        $newDraft = $contentService->createContentDraft($content->contentInfo);
3336
3337
        return $contentService->loadRelations($newDraft->getVersionInfo());
3338
    }
3339
3340
    /**
3341
     * Test for the createContentDraft() method.
3342
     *
3343
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3344
     *
3345
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3346
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelations
3347
     */
3348
    public function testCreateContentDraftWithRelationsCreatesRelations($relations)
3349
    {
3350
        $this->assertEquals(
3351
            1,
3352
            count($relations)
3353
        );
3354
3355
        return $relations;
3356
    }
3357
3358
    /**
3359
     * Test for the createContentDraft() method.
3360
     *
3361
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3362
     *
3363
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelationsCreatesRelations
3364
     */
3365
    public function testCreateContentDraftWithRelationsCreatesExpectedRelations($relations)
3366
    {
3367
        $this->assertExpectedRelations($relations);
3368
    }
3369
3370
    /**
3371
     * Test for the addRelation() method.
3372
     *
3373
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3374
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3375
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3376
     */
3377 View Code Duplication
    public function testAddRelationThrowsBadStateException()
3378
    {
3379
        $repository = $this->getRepository();
3380
3381
        $contentService = $repository->getContentService();
3382
3383
        /* BEGIN: Use Case */
3384
        // RemoteId of the "Media" page of an eZ Publish demo installation
3385
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3386
3387
        $content = $this->createContentVersion1();
3388
3389
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3390
3391
        // This call will fail with a "BadStateException", because content is
3392
        // published and not a draft.
3393
        $contentService->addRelation(
3394
            $content->getVersionInfo(),
3395
            $media
3396
        );
3397
        /* END: Use Case */
3398
    }
3399
3400
    /**
3401
     * Test for the loadRelations() method.
3402
     *
3403
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3404
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3405
     */
3406
    public function testLoadRelations()
3407
    {
3408
        $repository = $this->getRepository();
3409
3410
        $contentService = $repository->getContentService();
3411
3412
        /* BEGIN: Use Case */
3413
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3414
        // of a eZ Publish demo installation.
3415
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3416
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3417
3418
        $draft = $this->createContentDraftVersion1();
3419
3420
        // Load other content objects
3421
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3422
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3423
3424
        // Create relation between new content object and "Media" page
3425
        $contentService->addRelation(
3426
            $draft->getVersionInfo(),
3427
            $media
3428
        );
3429
3430
        // Create another relation with the "Demo Design" page
3431
        $contentService->addRelation(
3432
            $draft->getVersionInfo(),
3433
            $demoDesign
3434
        );
3435
3436
        // Load all relations
3437
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3438
        /* END: Use Case */
3439
3440
        usort(
3441
            $relations,
3442
            function ($rel1, $rel2) {
3443
                return strcasecmp(
3444
                    $rel2->getDestinationContentInfo()->remoteId,
3445
                    $rel1->getDestinationContentInfo()->remoteId
3446
                );
3447
            }
3448
        );
3449
3450
        $this->assertEquals(
3451
            array(
3452
                array(
3453
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3454
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3455
                ),
3456
                array(
3457
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3458
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3459
                ),
3460
            ),
3461
            array(
3462
                array(
3463
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3464
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3465
                ),
3466
                array(
3467
                    'sourceContentInfo' => $relations[1]->sourceContentInfo->remoteId,
3468
                    'destinationContentInfo' => $relations[1]->destinationContentInfo->remoteId,
3469
                ),
3470
            )
3471
        );
3472
    }
3473
3474
    /**
3475
     * Test for the loadRelations() method.
3476
     *
3477
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3478
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3479
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3480
     */
3481
    public function testLoadRelationsSkipsArchivedContent()
3482
    {
3483
        $repository = $this->getRepository();
3484
3485
        $contentService = $repository->getContentService();
3486
3487
        /* BEGIN: Use Case */
3488
        $trashService = $repository->getTrashService();
3489
        $locationService = $repository->getLocationService();
3490
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3491
        // of a eZ Publish demo installation.
3492
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3493
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3494
3495
        $draft = $this->createContentDraftVersion1();
3496
3497
        // Load other content objects
3498
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3499
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3500
3501
        // Create relation between new content object and "Media" page
3502
        $contentService->addRelation(
3503
            $draft->getVersionInfo(),
3504
            $media
3505
        );
3506
3507
        // Create another relation with the "Demo Design" page
3508
        $contentService->addRelation(
3509
            $draft->getVersionInfo(),
3510
            $demoDesign
3511
        );
3512
3513
        $demoDesignLocation = $locationService->loadLocation($demoDesign->mainLocationId);
3514
3515
        // Trashing Content's last Location will change its status to archived,
3516
        // in this case relation towards it will not be loaded.
3517
        $trashService->trash($demoDesignLocation);
3518
3519
        // Load all relations
3520
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3521
        /* END: Use Case */
3522
3523
        $this->assertCount(1, $relations);
3524
        $this->assertEquals(
3525
            array(
3526
                array(
3527
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3528
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3529
                ),
3530
            ),
3531
            array(
3532
                array(
3533
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3534
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3535
                ),
3536
            )
3537
        );
3538
    }
3539
3540
    /**
3541
     * Test for the loadRelations() method.
3542
     *
3543
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3544
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3545
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3546
     */
3547
    public function testLoadRelationsSkipsDraftContent()
3548
    {
3549
        $repository = $this->getRepository();
3550
3551
        $contentService = $repository->getContentService();
3552
3553
        /* BEGIN: Use Case */
3554
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3555
        // of a eZ Publish demo installation.
3556
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3557
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3558
3559
        $draft = $this->createContentDraftVersion1();
3560
3561
        // Load other content objects
3562
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3563
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3564
3565
        // Create draft of "Media" page
3566
        $mediaDraft = $contentService->createContentDraft($media->contentInfo);
3567
3568
        // Create relation between "Media" page and new content object draft.
3569
        // This relation will not be loaded before the draft is published.
3570
        $contentService->addRelation(
3571
            $mediaDraft->getVersionInfo(),
3572
            $draft->getVersionInfo()->getContentInfo()
3573
        );
3574
3575
        // Create another relation with the "Demo Design" page
3576
        $contentService->addRelation(
3577
            $mediaDraft->getVersionInfo(),
3578
            $demoDesign
3579
        );
3580
3581
        // Load all relations
3582
        $relations = $contentService->loadRelations($mediaDraft->getVersionInfo());
3583
        /* END: Use Case */
3584
3585
        $this->assertCount(1, $relations);
3586
        $this->assertEquals(
3587
            array(
3588
                array(
3589
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3590
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3591
                ),
3592
            ),
3593
            array(
3594
                array(
3595
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3596
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3597
                ),
3598
            )
3599
        );
3600
    }
3601
3602
    /**
3603
     * Test for the loadReverseRelations() method.
3604
     *
3605
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3606
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3607
     */
3608
    public function testLoadReverseRelations()
3609
    {
3610
        $repository = $this->getRepository();
3611
3612
        $contentService = $repository->getContentService();
3613
3614
        /* BEGIN: Use Case */
3615
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3616
        // of a eZ Publish demo installation.
3617
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3618
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3619
3620
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3621
        $contentInfo = $versionInfo->getContentInfo();
3622
3623
        // Create some drafts
3624
        $mediaDraft = $contentService->createContentDraft(
3625
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3626
        );
3627
        $demoDesignDraft = $contentService->createContentDraft(
3628
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3629
        );
3630
3631
        // Create relation between new content object and "Media" page
3632
        $relation1 = $contentService->addRelation(
3633
            $mediaDraft->getVersionInfo(),
3634
            $contentInfo
3635
        );
3636
3637
        // Create another relation with the "Demo Design" page
3638
        $relation2 = $contentService->addRelation(
3639
            $demoDesignDraft->getVersionInfo(),
3640
            $contentInfo
3641
        );
3642
3643
        // Publish drafts, so relations become active
3644
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3645
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3646
3647
        // Load all relations
3648
        $relations = $contentService->loadRelations($versionInfo);
3649
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3650
        /* END: Use Case */
3651
3652
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3653
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3654
3655
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3656
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3657
3658
        $this->assertEquals(0, count($relations));
3659
        $this->assertEquals(2, count($reverseRelations));
3660
3661
        usort(
3662
            $reverseRelations,
3663
            function ($rel1, $rel2) {
3664
                return strcasecmp(
3665
                    $rel2->getSourceContentInfo()->remoteId,
3666
                    $rel1->getSourceContentInfo()->remoteId
3667
                );
3668
            }
3669
        );
3670
3671
        $this->assertEquals(
3672
            array(
3673
                array(
3674
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3675
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3676
                ),
3677
                array(
3678
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3679
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3680
                ),
3681
            ),
3682
            array(
3683
                array(
3684
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3685
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3686
                ),
3687
                array(
3688
                    'sourceContentInfo' => $reverseRelations[1]->sourceContentInfo->remoteId,
3689
                    'destinationContentInfo' => $reverseRelations[1]->destinationContentInfo->remoteId,
3690
                ),
3691
            )
3692
        );
3693
    }
3694
3695
    /**
3696
     * Test for the loadReverseRelations() method.
3697
     *
3698
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3699
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3700
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3701
     */
3702
    public function testLoadReverseRelationsSkipsArchivedContent()
3703
    {
3704
        $repository = $this->getRepository();
3705
3706
        $contentService = $repository->getContentService();
3707
3708
        /* BEGIN: Use Case */
3709
        $trashService = $repository->getTrashService();
3710
        $locationService = $repository->getLocationService();
3711
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3712
        // of a eZ Publish demo installation.
3713
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3714
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3715
3716
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3717
        $contentInfo = $versionInfo->getContentInfo();
3718
3719
        // Create some drafts
3720
        $mediaDraft = $contentService->createContentDraft(
3721
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3722
        );
3723
        $demoDesignDraft = $contentService->createContentDraft(
3724
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3725
        );
3726
3727
        // Create relation between new content object and "Media" page
3728
        $relation1 = $contentService->addRelation(
3729
            $mediaDraft->getVersionInfo(),
3730
            $contentInfo
3731
        );
3732
3733
        // Create another relation with the "Demo Design" page
3734
        $relation2 = $contentService->addRelation(
3735
            $demoDesignDraft->getVersionInfo(),
3736
            $contentInfo
3737
        );
3738
3739
        // Publish drafts, so relations become active
3740
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3741
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3742
3743
        $demoDesignLocation = $locationService->loadLocation($demoDesignDraft->contentInfo->mainLocationId);
3744
3745
        // Trashing Content's last Location will change its status to archived,
3746
        // in this case relation from it will not be loaded.
3747
        $trashService->trash($demoDesignLocation);
3748
3749
        // Load all relations
3750
        $relations = $contentService->loadRelations($versionInfo);
3751
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3752
        /* END: Use Case */
3753
3754
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3755
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3756
3757
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3758
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3759
3760
        $this->assertEquals(0, count($relations));
3761
        $this->assertEquals(1, count($reverseRelations));
3762
3763
        $this->assertEquals(
3764
            array(
3765
                array(
3766
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3767
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3768
                ),
3769
            ),
3770
            array(
3771
                array(
3772
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3773
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3774
                ),
3775
            )
3776
        );
3777
    }
3778
3779
    /**
3780
     * Test for the loadReverseRelations() method.
3781
     *
3782
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3783
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3784
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3785
     */
3786
    public function testLoadReverseRelationsSkipsDraftContent()
3787
    {
3788
        $repository = $this->getRepository();
3789
3790
        $contentService = $repository->getContentService();
3791
3792
        /* BEGIN: Use Case */
3793
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3794
        // of a eZ Publish demo installation.
3795
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3796
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3797
3798
        // Load "Media" page Content
3799
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3800
3801
        // Create some drafts
3802
        $newDraftVersionInfo = $this->createContentDraftVersion1()->getVersionInfo();
3803
        $demoDesignDraft = $contentService->createContentDraft(
3804
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3805
        );
3806
3807
        // Create relation between "Media" page and new content object
3808
        $relation1 = $contentService->addRelation(
3809
            $newDraftVersionInfo,
3810
            $media->contentInfo
3811
        );
3812
3813
        // Create another relation with the "Demo Design" page
3814
        $relation2 = $contentService->addRelation(
3815
            $demoDesignDraft->getVersionInfo(),
3816
            $media->contentInfo
3817
        );
3818
3819
        // Publish drafts, so relations become active
3820
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3821
        // We will not publish new Content draft, therefore relation from it
3822
        // will not be loaded as reverse relation for "Media" page
3823
        //$contentService->publishVersion( $newDraftVersionInfo );
3824
3825
        // Load all relations
3826
        $relations = $contentService->loadRelations($media->versionInfo);
3827
        $reverseRelations = $contentService->loadReverseRelations($media->contentInfo);
3828
        /* END: Use Case */
3829
3830
        $this->assertEquals($media->contentInfo->id, $relation1->getDestinationContentInfo()->id);
3831
        $this->assertEquals($newDraftVersionInfo->contentInfo->id, $relation1->getSourceContentInfo()->id);
3832
3833
        $this->assertEquals($media->contentInfo->id, $relation2->getDestinationContentInfo()->id);
3834
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3835
3836
        $this->assertEquals(0, count($relations));
3837
        $this->assertEquals(1, count($reverseRelations));
3838
3839
        $this->assertEquals(
3840
            array(
3841
                array(
3842
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3843
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3844
                ),
3845
            ),
3846
            array(
3847
                array(
3848
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3849
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3850
                ),
3851
            )
3852
        );
3853
    }
3854
3855
    /**
3856
     * Test for the deleteRelation() method.
3857
     *
3858
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3859
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3860
     */
3861
    public function testDeleteRelation()
3862
    {
3863
        $repository = $this->getRepository();
3864
3865
        $contentService = $repository->getContentService();
3866
3867
        /* BEGIN: Use Case */
3868
        // Remote ids of the "Media" and the "Demo Design" page of a eZ Publish
3869
        // demo installation.
3870
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3871
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3872
3873
        $draft = $this->createContentDraftVersion1();
3874
3875
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3876
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3877
3878
        // Establish some relations
3879
        $contentService->addRelation($draft->getVersionInfo(), $media);
3880
        $contentService->addRelation($draft->getVersionInfo(), $demoDesign);
3881
3882
        // Delete one of the currently created relations
3883
        $contentService->deleteRelation($draft->getVersionInfo(), $media);
3884
3885
        // The relations array now contains only one element
3886
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3887
        /* END: Use Case */
3888
3889
        $this->assertEquals(1, count($relations));
3890
    }
3891
3892
    /**
3893
     * Test for the deleteRelation() method.
3894
     *
3895
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3896
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3897
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3898
     */
3899 View Code Duplication
    public function testDeleteRelationThrowsBadStateException()
3900
    {
3901
        $repository = $this->getRepository();
3902
3903
        $contentService = $repository->getContentService();
3904
3905
        /* BEGIN: Use Case */
3906
        // RemoteId of the "Media" page of an eZ Publish demo installation
3907
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3908
3909
        $content = $this->createContentVersion1();
3910
3911
        // Load the destination object
3912
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3913
3914
        // Create a new draft
3915
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
3916
3917
        // Add a relation
3918
        $contentService->addRelation($draftVersion2->getVersionInfo(), $media);
3919
3920
        // Publish new version
3921
        $contentVersion2 = $contentService->publishVersion(
3922
            $draftVersion2->getVersionInfo()
3923
        );
3924
3925
        // This call will fail with a "BadStateException", because content is
3926
        // published and not a draft.
3927
        $contentService->deleteRelation(
3928
            $contentVersion2->getVersionInfo(),
3929
            $media
3930
        );
3931
        /* END: Use Case */
3932
    }
3933
3934
    /**
3935
     * Test for the deleteRelation() method.
3936
     *
3937
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3938
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
3939
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3940
     */
3941 View Code Duplication
    public function testDeleteRelationThrowsInvalidArgumentException()
3942
    {
3943
        $repository = $this->getRepository();
3944
3945
        $contentService = $repository->getContentService();
3946
3947
        /* BEGIN: Use Case */
3948
        // RemoteId of the "Media" page of an eZ Publish demo installation
3949
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3950
3951
        $draft = $this->createContentDraftVersion1();
3952
3953
        // Load the destination object
3954
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3955
3956
        // This call will fail with a "InvalidArgumentException", because no
3957
        // relation exists between $draft and $media.
3958
        $contentService->deleteRelation(
3959
            $draft->getVersionInfo(),
3960
            $media
3961
        );
3962
        /* END: Use Case */
3963
    }
3964
3965
    /**
3966
     * Test for the createContent() method.
3967
     *
3968
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
3969
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3970
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3971
     */
3972
    public function testCreateContentInTransactionWithRollback()
3973
    {
3974
        if ($this->isVersion4()) {
3975
            $this->markTestSkipped('This test requires eZ Publish 5');
3976
        }
3977
3978
        $repository = $this->getRepository();
3979
3980
        /* BEGIN: Use Case */
3981
        $contentTypeService = $repository->getContentTypeService();
3982
        $contentService = $repository->getContentService();
3983
3984
        // Start a transaction
3985
        $repository->beginTransaction();
3986
3987
        try {
3988
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
3989
3990
            // Get a content create struct and set mandatory properties
3991
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
3992
            $contentCreate->setField('name', 'Sindelfingen forum');
3993
3994
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
3995
            $contentCreate->alwaysAvailable = true;
3996
3997
            // Create a new content object
3998
            $contentId = $contentService->createContent($contentCreate)->id;
3999
        } catch (Exception $e) {
4000
            // Cleanup hanging transaction on error
4001
            $repository->rollback();
4002
            throw $e;
4003
        }
4004
4005
        // Rollback all changes
4006
        $repository->rollback();
4007
4008
        try {
4009
            // This call will fail with a "NotFoundException"
4010
            $contentService->loadContent($contentId);
4011
        } catch (NotFoundException $e) {
4012
            // This is expected
4013
            return;
4014
        }
4015
        /* END: Use Case */
4016
4017
        $this->fail('Content object still exists after rollback.');
4018
    }
4019
4020
    /**
4021
     * Test for the createContent() method.
4022
     *
4023
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
4024
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4025
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4026
     */
4027
    public function testCreateContentInTransactionWithCommit()
4028
    {
4029
        if ($this->isVersion4()) {
4030
            $this->markTestSkipped('This test requires eZ Publish 5');
4031
        }
4032
4033
        $repository = $this->getRepository();
4034
4035
        /* BEGIN: Use Case */
4036
        $contentTypeService = $repository->getContentTypeService();
4037
        $contentService = $repository->getContentService();
4038
4039
        // Start a transaction
4040
        $repository->beginTransaction();
4041
4042
        try {
4043
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
4044
4045
            // Get a content create struct and set mandatory properties
4046
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
4047
            $contentCreate->setField('name', 'Sindelfingen forum');
4048
4049
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
4050
            $contentCreate->alwaysAvailable = true;
4051
4052
            // Create a new content object
4053
            $contentId = $contentService->createContent($contentCreate)->id;
4054
4055
            // Commit changes
4056
            $repository->commit();
4057
        } catch (Exception $e) {
4058
            // Cleanup hanging transaction on error
4059
            $repository->rollback();
4060
            throw $e;
4061
        }
4062
4063
        // Load the new content object
4064
        $content = $contentService->loadContent($contentId);
4065
        /* END: Use Case */
4066
4067
        $this->assertEquals($contentId, $content->id);
4068
    }
4069
4070
    /**
4071
     * Test for the createContent() method.
4072
     *
4073
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
4074
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
4075
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
4076
     */
4077
    public function testCreateContentWithLocationCreateParameterInTransactionWithRollback()
4078
    {
4079
        $repository = $this->getRepository();
4080
4081
        $contentService = $repository->getContentService();
4082
4083
        /* BEGIN: Use Case */
4084
        // Start a transaction
4085
        $repository->beginTransaction();
4086
4087
        try {
4088
            $draft = $this->createContentDraftVersion1();
4089
        } catch (Exception $e) {
4090
            // Cleanup hanging transaction on error
4091
            $repository->rollback();
4092
            throw $e;
4093
        }
4094
4095
        $contentId = $draft->id;
4096
4097
        // Roleback the transaction
4098
        $repository->rollback();
4099
4100
        try {
4101
            // This call will fail with a "NotFoundException"
4102
            $contentService->loadContent($contentId);
4103
        } catch (NotFoundException $e) {
4104
            return;
4105
        }
4106
        /* END: Use Case */
4107
4108
        $this->fail('Can still load content object after rollback.');
4109
    }
4110
4111
    /**
4112
     * Test for the createContent() method.
4113
     *
4114
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
4115
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
4116
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
4117
     */
4118 View Code Duplication
    public function testCreateContentWithLocationCreateParameterInTransactionWithCommit()
4119
    {
4120
        $repository = $this->getRepository();
4121
4122
        $contentService = $repository->getContentService();
4123
4124
        /* BEGIN: Use Case */
4125
        // Start a transaction
4126
        $repository->beginTransaction();
4127
4128
        try {
4129
            $draft = $this->createContentDraftVersion1();
4130
4131
            $contentId = $draft->id;
4132
4133
            // Roleback the transaction
4134
            $repository->commit();
4135
        } catch (Exception $e) {
4136
            // Cleanup hanging transaction on error
4137
            $repository->rollback();
4138
            throw $e;
4139
        }
4140
4141
        // Load the new content object
4142
        $content = $contentService->loadContent($contentId);
4143
        /* END: Use Case */
4144
4145
        $this->assertEquals($contentId, $content->id);
4146
    }
4147
4148
    /**
4149
     * Test for the createContentDraft() method.
4150
     *
4151
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4152
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4153
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4154
     */
4155
    public function testCreateContentDraftInTransactionWithRollback()
4156
    {
4157
        $repository = $this->getRepository();
4158
4159
        $contentId = $this->generateId('object', 12);
4160
        /* BEGIN: Use Case */
4161
        // $contentId is the ID of the "Administrator users" user group
4162
4163
        // Get the content service
4164
        $contentService = $repository->getContentService();
4165
4166
        // Load the user group content object
4167
        $content = $contentService->loadContent($contentId);
4168
4169
        // Start a new transaction
4170
        $repository->beginTransaction();
4171
4172
        try {
4173
            // Create a new draft
4174
            $drafted = $contentService->createContentDraft($content->contentInfo);
4175
4176
            // Store version number for later reuse
4177
            $versionNo = $drafted->versionInfo->versionNo;
4178
        } catch (Exception $e) {
4179
            // Cleanup hanging transaction on error
4180
            $repository->rollback();
4181
            throw $e;
4182
        }
4183
4184
        // Rollback
4185
        $repository->rollback();
4186
4187
        try {
4188
            // This call will fail with a "NotFoundException"
4189
            $contentService->loadContent($contentId, null, $versionNo);
4190
        } catch (NotFoundException $e) {
4191
            return;
4192
        }
4193
        /* END: Use Case */
4194
4195
        $this->fail('Can still load content draft after rollback');
4196
    }
4197
4198
    /**
4199
     * Test for the createContentDraft() method.
4200
     *
4201
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4202
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4203
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4204
     */
4205 View Code Duplication
    public function testCreateContentDraftInTransactionWithCommit()
4206
    {
4207
        $repository = $this->getRepository();
4208
4209
        $contentId = $this->generateId('object', 12);
4210
        /* BEGIN: Use Case */
4211
        // $contentId is the ID of the "Administrator users" user group
4212
4213
        // Get the content service
4214
        $contentService = $repository->getContentService();
4215
4216
        // Load the user group content object
4217
        $content = $contentService->loadContent($contentId);
4218
4219
        // Start a new transaction
4220
        $repository->beginTransaction();
4221
4222
        try {
4223
            // Create a new draft
4224
            $drafted = $contentService->createContentDraft($content->contentInfo);
4225
4226
            // Store version number for later reuse
4227
            $versionNo = $drafted->versionInfo->versionNo;
4228
4229
            // Commit all changes
4230
            $repository->commit();
4231
        } catch (Exception $e) {
4232
            // Cleanup hanging transaction on error
4233
            $repository->rollback();
4234
            throw $e;
4235
        }
4236
4237
        $content = $contentService->loadContent($contentId, null, $versionNo);
4238
        /* END: Use Case */
4239
4240
        $this->assertEquals(
4241
            $versionNo,
4242
            $content->getVersionInfo()->versionNo
4243
        );
4244
    }
4245
4246
    /**
4247
     * Test for the publishVersion() method.
4248
     *
4249
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4250
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4251
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4252
     */
4253 View Code Duplication
    public function testPublishVersionInTransactionWithRollback()
4254
    {
4255
        $repository = $this->getRepository();
4256
4257
        $contentId = $this->generateId('object', 12);
4258
        /* BEGIN: Use Case */
4259
        // $contentId is the ID of the "Administrator users" user group
4260
4261
        // Get the content service
4262
        $contentService = $repository->getContentService();
4263
4264
        // Load the user group content object
4265
        $content = $contentService->loadContent($contentId);
4266
4267
        // Start a new transaction
4268
        $repository->beginTransaction();
4269
4270
        try {
4271
            $draftVersion = $contentService->createContentDraft($content->contentInfo)->getVersionInfo();
4272
4273
            // Publish a new version
4274
            $content = $contentService->publishVersion($draftVersion);
4275
4276
            // Store version number for later reuse
4277
            $versionNo = $content->versionInfo->versionNo;
4278
        } catch (Exception $e) {
4279
            // Cleanup hanging transaction on error
4280
            $repository->rollback();
4281
            throw $e;
4282
        }
4283
4284
        // Rollback
4285
        $repository->rollback();
4286
4287
        try {
4288
            // This call will fail with a "NotFoundException"
4289
            $contentService->loadContent($contentId, null, $versionNo);
4290
        } catch (NotFoundException $e) {
4291
            return;
4292
        }
4293
        /* END: Use Case */
4294
4295
        $this->fail('Can still load content draft after rollback');
4296
    }
4297
4298
    /**
4299
     * Test for the publishVersion() method.
4300
     *
4301
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4302
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4303
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
4304
     */
4305 View Code Duplication
    public function testPublishVersionInTransactionWithCommit()
4306
    {
4307
        $repository = $this->getRepository();
4308
4309
        /* BEGIN: Use Case */
4310
        // ID of the "Administrator users" user group
4311
        $contentId = 12;
4312
4313
        // Get the content service
4314
        $contentService = $repository->getContentService();
4315
4316
        // Load the user group content object
4317
        $template = $contentService->loadContent($contentId);
4318
4319
        // Start a new transaction
4320
        $repository->beginTransaction();
4321
4322
        try {
4323
            // Publish a new version
4324
            $content = $contentService->publishVersion(
4325
                $contentService->createContentDraft($template->contentInfo)->getVersionInfo()
4326
            );
4327
4328
            // Store version number for later reuse
4329
            $versionNo = $content->versionInfo->versionNo;
4330
4331
            // Commit all changes
4332
            $repository->commit();
4333
        } catch (Exception $e) {
4334
            // Cleanup hanging transaction on error
4335
            $repository->rollback();
4336
            throw $e;
4337
        }
4338
4339
        // Load current version info
4340
        $versionInfo = $contentService->loadVersionInfo($content->contentInfo);
4341
        /* END: Use Case */
4342
4343
        $this->assertEquals($versionNo, $versionInfo->versionNo);
4344
    }
4345
4346
    /**
4347
     * Test for the updateContent() method.
4348
     *
4349
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4350
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4351
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4352
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4353
     */
4354 View Code Duplication
    public function testUpdateContentInTransactionWithRollback()
4355
    {
4356
        $repository = $this->getRepository();
4357
4358
        $contentId = $this->generateId('object', 12);
4359
        /* BEGIN: Use Case */
4360
        // $contentId is the ID of the "Administrator users" user group
4361
4362
        // Load content service
4363
        $contentService = $repository->getContentService();
4364
4365
        // Create a new user group draft
4366
        $draft = $contentService->createContentDraft(
4367
            $contentService->loadContentInfo($contentId)
4368
        );
4369
4370
        // Get an update struct and change the group name
4371
        $contentUpdate = $contentService->newContentUpdateStruct();
4372
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4373
4374
        // Start a transaction
4375
        $repository->beginTransaction();
4376
4377
        try {
4378
            // Update the group name
4379
            $draft = $contentService->updateContent(
4380
                $draft->getVersionInfo(),
4381
                $contentUpdate
4382
            );
4383
4384
            // Publish updated version
4385
            $contentService->publishVersion($draft->getVersionInfo());
4386
        } catch (Exception $e) {
4387
            // Cleanup hanging transaction on error
4388
            $repository->rollback();
4389
            throw $e;
4390
        }
4391
4392
        // Rollback all changes.
4393
        $repository->rollback();
4394
4395
        // Name will still be "Administrator users"
4396
        $name = $contentService->loadContent($contentId)->getFieldValue('name');
4397
        /* END: Use Case */
4398
4399
        $this->assertEquals('Administrator users', $name);
4400
    }
4401
4402
    /**
4403
     * Test for the updateContent() method.
4404
     *
4405
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4406
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4407
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4408
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4409
     */
4410 View Code Duplication
    public function testUpdateContentInTransactionWithCommit()
4411
    {
4412
        $repository = $this->getRepository();
4413
4414
        $contentId = $this->generateId('object', 12);
4415
        /* BEGIN: Use Case */
4416
        // $contentId is the ID of the "Administrator users" user group
4417
4418
        // Load content service
4419
        $contentService = $repository->getContentService();
4420
4421
        // Create a new user group draft
4422
        $draft = $contentService->createContentDraft(
4423
            $contentService->loadContentInfo($contentId)
4424
        );
4425
4426
        // Get an update struct and change the group name
4427
        $contentUpdate = $contentService->newContentUpdateStruct();
4428
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4429
4430
        // Start a transaction
4431
        $repository->beginTransaction();
4432
4433
        try {
4434
            // Update the group name
4435
            $draft = $contentService->updateContent(
4436
                $draft->getVersionInfo(),
4437
                $contentUpdate
4438
            );
4439
4440
            // Publish updated version
4441
            $contentService->publishVersion($draft->getVersionInfo());
4442
4443
            // Commit all changes.
4444
            $repository->commit();
4445
        } catch (Exception $e) {
4446
            // Cleanup hanging transaction on error
4447
            $repository->rollback();
4448
            throw $e;
4449
        }
4450
4451
        // Name is now "Administrators"
4452
        $name = $contentService->loadContent($contentId)->getFieldValue('name', 'eng-US');
4453
        /* END: Use Case */
4454
4455
        $this->assertEquals('Administrators', $name);
4456
    }
4457
4458
    /**
4459
     * Test for the updateContentMetadata() method.
4460
     *
4461
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4462
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4463
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4464
     */
4465 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithRollback()
4466
    {
4467
        $repository = $this->getRepository();
4468
4469
        $contentId = $this->generateId('object', 12);
4470
        /* BEGIN: Use Case */
4471
        // $contentId is the ID of the "Administrator users" user group
4472
4473
        // Get the content service
4474
        $contentService = $repository->getContentService();
4475
4476
        // Load a ContentInfo object
4477
        $contentInfo = $contentService->loadContentInfo($contentId);
4478
4479
        // Store remoteId for later testing
4480
        $remoteId = $contentInfo->remoteId;
4481
4482
        // Start a transaction
4483
        $repository->beginTransaction();
4484
4485
        try {
4486
            // Get metadata update struct and change remoteId
4487
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4488
            $metadataUpdate->remoteId = md5(microtime(true));
4489
4490
            // Update the metadata of the published content object
4491
            $contentService->updateContentMetadata(
4492
                $contentInfo,
4493
                $metadataUpdate
4494
            );
4495
        } catch (Exception $e) {
4496
            // Cleanup hanging transaction on error
4497
            $repository->rollback();
4498
            throw $e;
4499
        }
4500
4501
        // Rollback all changes.
4502
        $repository->rollback();
4503
4504
        // Load current remoteId
4505
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4506
        /* END: Use Case */
4507
4508
        $this->assertEquals($remoteId, $remoteIdReloaded);
4509
    }
4510
4511
    /**
4512
     * Test for the updateContentMetadata() method.
4513
     *
4514
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4515
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4516
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4517
     */
4518 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithCommit()
4519
    {
4520
        $repository = $this->getRepository();
4521
4522
        $contentId = $this->generateId('object', 12);
4523
        /* BEGIN: Use Case */
4524
        // $contentId is the ID of the "Administrator users" user group
4525
4526
        // Get the content service
4527
        $contentService = $repository->getContentService();
4528
4529
        // Load a ContentInfo object
4530
        $contentInfo = $contentService->loadContentInfo($contentId);
4531
4532
        // Store remoteId for later testing
4533
        $remoteId = $contentInfo->remoteId;
4534
4535
        // Start a transaction
4536
        $repository->beginTransaction();
4537
4538
        try {
4539
            // Get metadata update struct and change remoteId
4540
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4541
            $metadataUpdate->remoteId = md5(microtime(true));
4542
4543
            // Update the metadata of the published content object
4544
            $contentService->updateContentMetadata(
4545
                $contentInfo,
4546
                $metadataUpdate
4547
            );
4548
4549
            // Commit all changes.
4550
            $repository->commit();
4551
        } catch (Exception $e) {
4552
            // Cleanup hanging transaction on error
4553
            $repository->rollback();
4554
            throw $e;
4555
        }
4556
4557
        // Load current remoteId
4558
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4559
        /* END: Use Case */
4560
4561
        $this->assertNotEquals($remoteId, $remoteIdReloaded);
4562
    }
4563
4564
    /**
4565
     * Test for the deleteVersion() method.
4566
     *
4567
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4568
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4569
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4570
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4571
     */
4572
    public function testDeleteVersionInTransactionWithRollback()
4573
    {
4574
        $repository = $this->getRepository();
4575
4576
        $contentId = $this->generateId('object', 12);
4577
        /* BEGIN: Use Case */
4578
        // $contentId is the ID of the "Administrator users" user group
4579
4580
        // Get the content service
4581
        $contentService = $repository->getContentService();
4582
4583
        // Start a new transaction
4584
        $repository->beginTransaction();
4585
4586
        try {
4587
            // Create a new draft
4588
            $draft = $contentService->createContentDraft(
4589
                $contentService->loadContentInfo($contentId)
4590
            );
4591
4592
            $contentService->deleteVersion($draft->getVersionInfo());
4593
        } catch (Exception $e) {
4594
            // Cleanup hanging transaction on error
4595
            $repository->rollback();
4596
            throw $e;
4597
        }
4598
4599
        // Rollback all changes.
4600
        $repository->rollback();
4601
4602
        // This array will be empty
4603
        $drafts = $contentService->loadContentDrafts();
4604
        /* END: Use Case */
4605
4606
        $this->assertSame(array(), $drafts);
4607
    }
4608
4609
    /**
4610
     * Test for the deleteVersion() method.
4611
     *
4612
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4613
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4614
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4615
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4616
     */
4617
    public function testDeleteVersionInTransactionWithCommit()
4618
    {
4619
        $repository = $this->getRepository();
4620
4621
        $contentId = $this->generateId('object', 12);
4622
        /* BEGIN: Use Case */
4623
        // $contentId is the ID of the "Administrator users" user group
4624
4625
        // Get the content service
4626
        $contentService = $repository->getContentService();
4627
4628
        // Start a new transaction
4629
        $repository->beginTransaction();
4630
4631
        try {
4632
            // Create a new draft
4633
            $draft = $contentService->createContentDraft(
4634
                $contentService->loadContentInfo($contentId)
4635
            );
4636
4637
            $contentService->deleteVersion($draft->getVersionInfo());
4638
4639
            // Commit all changes.
4640
            $repository->commit();
4641
        } catch (Exception $e) {
4642
            // Cleanup hanging transaction on error
4643
            $repository->rollback();
4644
            throw $e;
4645
        }
4646
4647
        // This array will contain no element
4648
        $drafts = $contentService->loadContentDrafts();
4649
        /* END: Use Case */
4650
4651
        $this->assertSame(array(), $drafts);
4652
    }
4653
4654
    /**
4655
     * Test for the deleteContent() method.
4656
     *
4657
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4658
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4659
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4660
     */
4661
    public function testDeleteContentInTransactionWithRollback()
4662
    {
4663
        $repository = $this->getRepository();
4664
4665
        $contentId = $this->generateId('object', 11);
4666
        /* BEGIN: Use Case */
4667
        // $contentId is the ID of the "Members" user group in an eZ Publish
4668
        // demo installation
4669
4670
        // Get content service
4671
        $contentService = $repository->getContentService();
4672
4673
        // Load a ContentInfo instance
4674
        $contentInfo = $contentService->loadContentInfo($contentId);
4675
4676
        // Start a new transaction
4677
        $repository->beginTransaction();
4678
4679
        try {
4680
            // Delete content object
4681
            $contentService->deleteContent($contentInfo);
4682
        } catch (Exception $e) {
4683
            // Cleanup hanging transaction on error
4684
            $repository->rollback();
4685
            throw $e;
4686
        }
4687
4688
        // Rollback all changes
4689
        $repository->rollback();
4690
4691
        // This call will return the original content object
4692
        $contentInfo = $contentService->loadContentInfo($contentId);
4693
        /* END: Use Case */
4694
4695
        $this->assertEquals($contentId, $contentInfo->id);
4696
    }
4697
4698
    /**
4699
     * Test for the deleteContent() method.
4700
     *
4701
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4702
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4703
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4704
     */
4705
    public function testDeleteContentInTransactionWithCommit()
4706
    {
4707
        $repository = $this->getRepository();
4708
4709
        $contentId = $this->generateId('object', 11);
4710
        /* BEGIN: Use Case */
4711
        // $contentId is the ID of the "Members" user group in an eZ Publish
4712
        // demo installation
4713
4714
        // Get content service
4715
        $contentService = $repository->getContentService();
4716
4717
        // Load a ContentInfo instance
4718
        $contentInfo = $contentService->loadContentInfo($contentId);
4719
4720
        // Start a new transaction
4721
        $repository->beginTransaction();
4722
4723
        try {
4724
            // Delete content object
4725
            $contentService->deleteContent($contentInfo);
4726
4727
            // Commit all changes
4728
            $repository->commit();
4729
        } catch (Exception $e) {
4730
            // Cleanup hanging transaction on error
4731
            $repository->rollback();
4732
            throw $e;
4733
        }
4734
4735
        // Deleted content info is not found anymore
4736
        try {
4737
            $contentService->loadContentInfo($contentId);
4738
        } catch (NotFoundException $e) {
4739
            return;
4740
        }
4741
        /* END: Use Case */
4742
4743
        $this->fail('Can still load ContentInfo after commit.');
4744
    }
4745
4746
    /**
4747
     * Test for the copyContent() method.
4748
     *
4749
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4750
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4751
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4752
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4753
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4754
     */
4755 View Code Duplication
    public function testCopyContentInTransactionWithRollback()
4756
    {
4757
        $repository = $this->getRepository();
4758
4759
        $contentId = $this->generateId('object', 11);
4760
        $locationId = $this->generateId('location', 13);
4761
        /* BEGIN: Use Case */
4762
        // $contentId is the ID of the "Members" user group in an eZ Publish
4763
        // demo installation
4764
4765
        // $locationId is the ID of the "Administrator users" group location
4766
4767
        // Get services
4768
        $contentService = $repository->getContentService();
4769
        $locationService = $repository->getLocationService();
4770
4771
        // Load content object to copy
4772
        $content = $contentService->loadContent($contentId);
4773
4774
        // Create new target location
4775
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4776
4777
        // Start a new transaction
4778
        $repository->beginTransaction();
4779
4780
        try {
4781
            // Copy content with all versions and drafts
4782
            $contentService->copyContent(
4783
                $content->contentInfo,
4784
                $locationCreate
4785
            );
4786
        } catch (Exception $e) {
4787
            // Cleanup hanging transaction on error
4788
            $repository->rollback();
4789
            throw $e;
4790
        }
4791
4792
        // Rollback all changes
4793
        $repository->rollback();
4794
4795
        $this->refreshSearch($repository);
4796
4797
        // This array will only contain a single admin user object
4798
        $locations = $locationService->loadLocationChildren(
4799
            $locationService->loadLocation($locationId)
4800
        )->locations;
4801
        /* END: Use Case */
4802
4803
        $this->assertEquals(1, count($locations));
4804
    }
4805
4806
    /**
4807
     * Test for the copyContent() method.
4808
     *
4809
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4810
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4811
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4812
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4813
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4814
     */
4815 View Code Duplication
    public function testCopyContentInTransactionWithCommit()
4816
    {
4817
        $repository = $this->getRepository();
4818
4819
        $contentId = $this->generateId('object', 11);
4820
        $locationId = $this->generateId('location', 13);
4821
        /* BEGIN: Use Case */
4822
        // $contentId is the ID of the "Members" user group in an eZ Publish
4823
        // demo installation
4824
4825
        // $locationId is the ID of the "Administrator users" group location
4826
4827
        // Get services
4828
        $contentService = $repository->getContentService();
4829
        $locationService = $repository->getLocationService();
4830
4831
        // Load content object to copy
4832
        $content = $contentService->loadContent($contentId);
4833
4834
        // Create new target location
4835
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4836
4837
        // Start a new transaction
4838
        $repository->beginTransaction();
4839
4840
        try {
4841
            // Copy content with all versions and drafts
4842
            $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...
4843
                $content->contentInfo,
4844
                $locationCreate
4845
            );
4846
4847
            // Commit all changes
4848
            $repository->commit();
4849
        } catch (Exception $e) {
4850
            // Cleanup hanging transaction on error
4851
            $repository->rollback();
4852
            throw $e;
4853
        }
4854
4855
        $this->refreshSearch($repository);
4856
4857
        // This will contain the admin user and the new child location
4858
        $locations = $locationService->loadLocationChildren(
4859
            $locationService->loadLocation($locationId)
4860
        )->locations;
4861
        /* END: Use Case */
4862
4863
        $this->assertEquals(2, count($locations));
4864
    }
4865
4866
    public function testURLAliasesCreatedForNewContent()
4867
    {
4868
        $repository = $this->getRepository();
4869
4870
        $contentService = $repository->getContentService();
4871
        $locationService = $repository->getLocationService();
4872
        $urlAliasService = $repository->getURLAliasService();
4873
4874
        /* BEGIN: Use Case */
4875
        $draft = $this->createContentDraftVersion1();
4876
4877
        // Automatically creates a new URLAlias for the content
4878
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
4879
        /* END: Use Case */
4880
4881
        $location = $locationService->loadLocation(
4882
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4883
        );
4884
4885
        $aliases = $urlAliasService->listLocationAliases($location, false);
4886
4887
        $this->assertAliasesCorrect(
4888
            array(
4889
                '/Design/Plain-site/An-awesome-forum' => array(
4890
                    'type' => URLAlias::LOCATION,
4891
                    'destination' => $location->id,
4892
                    'path' => '/Design/Plain-site/An-awesome-forum',
4893
                    'languageCodes' => array('eng-US'),
4894
                    'isHistory' => false,
4895
                    'isCustom' => false,
4896
                    'forward' => false,
4897
                ),
4898
            ),
4899
            $aliases
4900
        );
4901
    }
4902
4903
    public function testURLAliasesCreatedForUpdatedContent()
4904
    {
4905
        $repository = $this->getRepository();
4906
4907
        $contentService = $repository->getContentService();
4908
        $locationService = $repository->getLocationService();
4909
        $urlAliasService = $repository->getURLAliasService();
4910
4911
        /* BEGIN: Use Case */
4912
        $draft = $this->createUpdatedDraftVersion2();
4913
4914
        $location = $locationService->loadLocation(
4915
            $draft->getVersionInfo()->getContentInfo()->mainLocationId
4916
        );
4917
4918
        // Load and assert URL aliases before publishing updated Content, so that
4919
        // SPI cache is warmed up and cache invalidation is also tested.
4920
        $aliases = $urlAliasService->listLocationAliases($location, false);
4921
4922
        $this->assertAliasesCorrect(
4923
            array(
4924
                '/Design/Plain-site/An-awesome-forum' => array(
4925
                    'type' => URLAlias::LOCATION,
4926
                    'destination' => $location->id,
4927
                    'path' => '/Design/Plain-site/An-awesome-forum',
4928
                    'languageCodes' => array('eng-US'),
4929
                    'alwaysAvailable' => true,
4930
                    'isHistory' => false,
4931
                    'isCustom' => false,
4932
                    'forward' => false,
4933
                ),
4934
            ),
4935
            $aliases
4936
        );
4937
4938
        // Automatically marks old aliases for the content as history
4939
        // and creates new aliases, based on the changes
4940
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
4941
        /* END: Use Case */
4942
4943
        $location = $locationService->loadLocation(
4944
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4945
        );
4946
4947
        $aliases = $urlAliasService->listLocationAliases($location, false);
4948
4949
        $this->assertAliasesCorrect(
4950
            array(
4951
                '/Design/Plain-site/An-awesome-forum2' => array(
4952
                    'type' => URLAlias::LOCATION,
4953
                    'destination' => $location->id,
4954
                    'path' => '/Design/Plain-site/An-awesome-forum2',
4955
                    'languageCodes' => array('eng-US'),
4956
                    'alwaysAvailable' => true,
4957
                    'isHistory' => false,
4958
                    'isCustom' => false,
4959
                    'forward' => false,
4960
                ),
4961
                '/Design/Plain-site/An-awesome-forum23' => array(
4962
                    'type' => URLAlias::LOCATION,
4963
                    'destination' => $location->id,
4964
                    'path' => '/Design/Plain-site/An-awesome-forum23',
4965
                    'languageCodes' => array('eng-GB'),
4966
                    'alwaysAvailable' => true,
4967
                    'isHistory' => false,
4968
                    'isCustom' => false,
4969
                    'forward' => false,
4970
                ),
4971
            ),
4972
            $aliases
4973
        );
4974
    }
4975
4976
    public function testCustomURLAliasesNotHistorizedOnUpdatedContent()
4977
    {
4978
        $repository = $this->getRepository();
4979
4980
        $contentService = $repository->getContentService();
4981
4982
        /* BEGIN: Use Case */
4983
        $urlAliasService = $repository->getURLAliasService();
4984
        $locationService = $repository->getLocationService();
4985
4986
        $content = $this->createContentVersion1();
4987
4988
        // Create a custom URL alias
4989
        $urlAliasService->createUrlAlias(
4990
            $locationService->loadLocation(
4991
                $content->getVersionInfo()->getContentInfo()->mainLocationId
4992
            ),
4993
            '/my/fancy/story-about-ez-publish',
4994
            'eng-US'
4995
        );
4996
4997
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
4998
4999
        $contentUpdate = $contentService->newContentUpdateStruct();
5000
        $contentUpdate->initialLanguageCode = 'eng-US';
5001
        $contentUpdate->setField('name', 'Amazing Bielefeld forum');
5002
5003
        $draftVersion2 = $contentService->updateContent(
5004
            $draftVersion2->getVersionInfo(),
5005
            $contentUpdate
5006
        );
5007
5008
        // Only marks auto-generated aliases as history
5009
        // the custom one is left untouched
5010
        $liveContent = $contentService->publishVersion($draftVersion2->getVersionInfo());
5011
        /* END: Use Case */
5012
5013
        $location = $locationService->loadLocation(
5014
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
5015
        );
5016
5017
        $aliases = $urlAliasService->listLocationAliases($location);
5018
5019
        $this->assertAliasesCorrect(
5020
            array(
5021
                '/my/fancy/story-about-ez-publish' => array(
5022
                    'type' => URLAlias::LOCATION,
5023
                    'destination' => $location->id,
5024
                    'path' => '/my/fancy/story-about-ez-publish',
5025
                    'languageCodes' => array('eng-US'),
5026
                    'isHistory' => false,
5027
                    'isCustom' => true,
5028
                    'forward' => false,
5029
                    'alwaysAvailable' => false,
5030
                ),
5031
            ),
5032
            $aliases
5033
        );
5034
    }
5035
5036
    /**
5037
     * Test to ensure that old versions are not affected by updates to newer
5038
     * drafts.
5039
     */
5040
    public function testUpdatingDraftDoesNotUpdateOldVersions()
5041
    {
5042
        $repository = $this->getRepository();
5043
5044
        $contentService = $repository->getContentService();
0 ignored issues
show
Unused Code introduced by
$contentService 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...
5045
5046
        $contentService = $repository->getContentService();
5047
5048
        $contentVersion2 = $this->createContentVersion2();
5049
5050
        $loadedContent1 = $contentService->loadContent($contentVersion2->id, null, 1);
5051
        $loadedContent2 = $contentService->loadContent($contentVersion2->id, null, 2);
5052
5053
        $this->assertNotEquals(
5054
            $loadedContent1->getFieldValue('name', 'eng-US'),
5055
            $loadedContent2->getFieldValue('name', 'eng-US')
5056
        );
5057
    }
5058
5059
    /**
5060
     * Test scenario with writer and publisher users.
5061
     * Writer can only create content. Publisher can publish this content.
5062
     */
5063
    public function testPublishWorkflow()
5064
    {
5065
        $repository = $this->getRepository();
5066
        $contentService = $repository->getContentService();
5067
5068
        $this->createRoleWithPolicies('Publisher', [
5069
            ['content', 'read'],
5070
            ['content', 'create'],
5071
            ['content', 'publish'],
5072
        ]);
5073
5074
        $this->createRoleWithPolicies('Writer', [
5075
            ['content', 'read'],
5076
            ['content', 'create'],
5077
        ]);
5078
5079
        $writerUser = $this->createCustomUserWithLogin(
5080
            'writer',
5081
            '[email protected]',
5082
            'Writers',
5083
            'Writer'
5084
        );
5085
5086
        $publisherUser = $this->createCustomUserWithLogin(
5087
            'publisher',
5088
            '[email protected]',
5089
            'Publishers',
5090
            'Publisher'
5091
        );
5092
5093
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5094
        $draft = $this->createContentDraftVersion1();
5095
5096
        $repository->getPermissionResolver()->setCurrentUserReference($publisherUser);
5097
        $content = $contentService->publishVersion($draft->versionInfo);
5098
5099
        $contentService->loadContent($content->id);
5100
    }
5101
5102
    /**
5103
     * Test publish / content policy is required to be able to publish content.
5104
     *
5105
     * @expectedException \eZ\Publish\Core\Base\Exceptions\UnauthorizedException
5106
     * @expectedExceptionMessageRegExp /User does not have access to 'publish' 'content'/
5107
     */
5108
    public function testPublishContentWithoutPublishPolicyThrowsException()
5109
    {
5110
        $repository = $this->getRepository();
5111
5112
        $this->createRoleWithPolicies('Writer', [
5113
            ['content', 'read'],
5114
            ['content', 'create'],
5115
            ['content', 'edit'],
5116
        ]);
5117
        $writerUser = $this->createCustomUserWithLogin(
5118
            'writer',
5119
            '[email protected]',
5120
            'Writers',
5121
            'Writer'
5122
        );
5123
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5124
5125
        $this->createContentVersion1();
5126
    }
5127
5128
    /**
5129
     * Test for the newTranslationInfo() method.
5130
     *
5131
     * @covers \eZ\Publish\Core\Repository\ContentService::newTranslationInfo
5132
     */
5133
    public function testNewTranslationInfo()
5134
    {
5135
        $repository = $this->getRepository();
5136
        $contentService = $repository->getContentService();
5137
5138
        $translationInfo = $contentService->newTranslationInfo();
5139
5140
        $this->assertInstanceOf(
5141
            TranslationInfo::class,
5142
            $translationInfo
5143
        );
5144
5145
        foreach ($translationInfo as $propertyName => $propertyValue) {
0 ignored issues
show
Bug introduced by
The expression $translationInfo of type object<eZ\Publish\API\Re...ontent\TranslationInfo> is not traversable.
Loading history...
5146
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
5147
        }
5148
    }
5149
5150
    /**
5151
     * Simplify creating custom role with limited set of policies.
5152
     *
5153
     * @param $roleName
5154
     * @param array $policies e.g. [ ['content', 'create'], ['content', 'edit'], ]
5155
     */
5156
    private function createRoleWithPolicies($roleName, array $policies)
5157
    {
5158
        $repository = $this->getRepository();
5159
        $roleService = $repository->getRoleService();
5160
5161
        $roleCreateStruct = $roleService->newRoleCreateStruct($roleName);
5162
        foreach ($policies as $policy) {
5163
            $policyCreateStruct = $roleService->newPolicyCreateStruct($policy[0], $policy[1]);
5164
            $roleCreateStruct->addPolicy($policyCreateStruct);
5165
        }
5166
5167
        $roleDraft = $roleService->createRole($roleCreateStruct);
5168
        $roleService->publishRoleDraft($roleDraft);
5169
    }
5170
5171
    /**
5172
     * Asserts that all aliases defined in $expectedAliasProperties with the
5173
     * given properties are available in $actualAliases and not more.
5174
     *
5175
     * @param array $expectedAliasProperties
5176
     * @param array $actualAliases
5177
     */
5178
    private function assertAliasesCorrect(array $expectedAliasProperties, array $actualAliases)
5179
    {
5180
        foreach ($actualAliases as $actualAlias) {
5181
            if (!isset($expectedAliasProperties[$actualAlias->path])) {
5182
                $this->fail(
5183
                    sprintf(
5184
                        'Alias with path "%s" in languages "%s" not expected.',
5185
                        $actualAlias->path,
5186
                        implode(', ', $actualAlias->languageCodes)
5187
                    )
5188
                );
5189
            }
5190
5191
            foreach ($expectedAliasProperties[$actualAlias->path] as $propertyName => $propertyValue) {
5192
                $this->assertEquals(
5193
                    $propertyValue,
5194
                    $actualAlias->$propertyName,
5195
                    sprintf(
5196
                        'Property $%s incorrect on alias with path "%s" in languages "%s".',
5197
                        $propertyName,
5198
                        $actualAlias->path,
5199
                        implode(', ', $actualAlias->languageCodes)
5200
                    )
5201
                );
5202
            }
5203
5204
            unset($expectedAliasProperties[$actualAlias->path]);
5205
        }
5206
5207
        if (!empty($expectedAliasProperties)) {
5208
            $this->fail(
5209
                sprintf(
5210
                    'Missing expected aliases with paths "%s".',
5211
                    implode('", "', array_keys($expectedAliasProperties))
5212
                )
5213
            );
5214
        }
5215
    }
5216
5217
    /**
5218
     * Asserts that the given fields are equal to the default fields fixture.
5219
     *
5220
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5221
     */
5222
    private function assertAllFieldsEquals(array $fields)
5223
    {
5224
        $actual = $this->normalizeFields($fields);
5225
        $expected = $this->normalizeFields($this->createFieldsFixture());
5226
5227
        $this->assertEquals($expected, $actual);
5228
    }
5229
5230
    /**
5231
     * Asserts that the given fields are equal to a language filtered set of the
5232
     * default fields fixture.
5233
     *
5234
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5235
     * @param string $languageCode
5236
     */
5237
    private function assertLocaleFieldsEquals(array $fields, $languageCode)
5238
    {
5239
        $actual = $this->normalizeFields($fields);
5240
5241
        $expected = array();
5242
        foreach ($this->normalizeFields($this->createFieldsFixture()) as $field) {
5243
            if ($field->languageCode !== $languageCode) {
5244
                continue;
5245
            }
5246
            $expected[] = $field;
5247
        }
5248
5249
        $this->assertEquals($expected, $actual);
5250
    }
5251
5252
    /**
5253
     * This method normalizes a set of fields and returns a normalized set.
5254
     *
5255
     * Normalization means it resets the storage specific field id to zero and
5256
     * it sorts the field by their identifier and their language code. In
5257
     * addition, the field value is removed, since this one depends on the
5258
     * specific FieldType, which is tested in a dedicated integration test.
5259
     *
5260
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5261
     *
5262
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5263
     */
5264
    private function normalizeFields(array $fields)
5265
    {
5266
        $normalized = array();
5267 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...
5268
            $normalized[] = new Field(
5269
                array(
5270
                    'id' => 0,
5271
                    '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...
5272
                    'languageCode' => $field->languageCode,
5273
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
5274
                )
5275
            );
5276
        }
5277
        usort(
5278
            $normalized,
5279 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...
5280
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
5281
                    return strcasecmp($field1->languageCode, $field2->languageCode);
5282
                }
5283
5284
                return $return;
5285
            }
5286
        );
5287
5288
        return $normalized;
5289
    }
5290
5291
    /**
5292
     * Returns a filtered set of the default fields fixture.
5293
     *
5294
     * @param string $languageCode
5295
     *
5296
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5297
     */
5298
    private function createLocaleFieldsFixture($languageCode)
5299
    {
5300
        $fields = array();
5301
        foreach ($this->createFieldsFixture() as $field) {
5302
            if (null === $field->languageCode || $languageCode === $field->languageCode) {
5303
                $fields[] = $field;
5304
            }
5305
        }
5306
5307
        return $fields;
5308
    }
5309
5310
    /**
5311
     * Asserts that given Content has default ContentStates.
5312
     *
5313
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
5314
     */
5315 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
5316
    {
5317
        $repository = $this->getRepository();
5318
        $objectStateService = $repository->getObjectStateService();
5319
5320
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
5321
5322
        foreach ($objectStateGroups as $objectStateGroup) {
5323
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
5324
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
5325
                // Only check the first object state which is the default one.
5326
                $this->assertEquals(
5327
                    $objectState,
5328
                    $contentState
5329
                );
5330
                break;
5331
            }
5332
        }
5333
    }
5334
5335
    /**
5336
     * Returns the default fixture of fields used in most tests.
5337
     *
5338
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5339
     */
5340
    private function createFieldsFixture()
5341
    {
5342
        return array(
5343
            new Field(
5344
                array(
5345
                    'id' => 0,
5346
                    'value' => 'Foo',
5347
                    'languageCode' => 'eng-US',
5348
                    'fieldDefIdentifier' => 'description',
5349
                )
5350
            ),
5351
            new Field(
5352
                array(
5353
                    'id' => 0,
5354
                    'value' => 'Bar',
5355
                    'languageCode' => 'eng-GB',
5356
                    'fieldDefIdentifier' => 'description',
5357
                )
5358
            ),
5359
            new Field(
5360
                array(
5361
                    'id' => 0,
5362
                    'value' => 'An awesome multi-lang forum²',
5363
                    'languageCode' => 'eng-US',
5364
                    'fieldDefIdentifier' => 'name',
5365
                )
5366
            ),
5367
            new Field(
5368
                array(
5369
                    'id' => 0,
5370
                    'value' => 'An awesome multi-lang forum²³',
5371
                    'languageCode' => 'eng-GB',
5372
                    'fieldDefIdentifier' => 'name',
5373
                )
5374
            ),
5375
        );
5376
    }
5377
5378
    /**
5379
     * Gets expected property values for the "Media" ContentInfo ValueObject.
5380
     *
5381
     * @return array
5382
     */
5383 View Code Duplication
    private function getExpectedMediaContentInfoProperties()
5384
    {
5385
        return [
5386
            'id' => 41,
5387
            'contentTypeId' => 1,
5388
            'name' => 'Media',
5389
            'sectionId' => 3,
5390
            'currentVersionNo' => 1,
5391
            'published' => true,
5392
            'ownerId' => 14,
5393
            'modificationDate' => $this->createDateTime(1060695457),
5394
            'publishedDate' => $this->createDateTime(1060695457),
5395
            'alwaysAvailable' => 1,
5396
            'remoteId' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
5397
            'mainLanguageCode' => 'eng-US',
5398
            'mainLocationId' => 43,
5399
        ];
5400
    }
5401
}
5402