forked from ezsystems/ezpublish-api
/
BaseContentServiceTest.php
329 lines (259 loc) · 11.1 KB
/
BaseContentServiceTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* File containing the BaseContentServiceTest class
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/
namespace eZ\Publish\API\Repository\Tests;
use eZ\Publish\API\Repository\Values\Content\Location;
/**
* Base class for content specific tests.
*/
abstract class BaseContentServiceTest extends BaseTest
{
/**
* Creates a fresh clean content draft.
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
protected function createContentVersion1EmptyBinaryField()
{
$repository = $this->getRepository();
$parentLocationId = $this->generateId( 'location', 56 );
$sectionId = $this->generateId( 'section', 1 );
/* BEGIN: Inline */
// $parentLocationId is the id of the /Design/eZ-publish node
$contentService = $repository->getContentService();
$contentTypeService = $repository->getContentTypeService();
$locationService = $repository->getLocationService();
// Configure new location
$locationCreate = $locationService->newLocationCreateStruct( $parentLocationId );
$locationCreate->priority = 23;
$locationCreate->hidden = true;
$locationCreate->remoteId = '0123456789abcdef0123456789abcdefgh';
$locationCreate->sortField = Location::SORT_FIELD_NODE_ID;
$locationCreate->sortOrder = Location::SORT_ORDER_DESC;
// Load content type
$contentType = $contentTypeService->loadContentTypeByIdentifier( 'video' );
// Configure new content object
$contentCreate = $contentService->newContentCreateStruct( $contentType, 'eng-US' );
$contentCreate->setField( 'name', 'An empty file' );
$contentCreate->remoteId = 'abcdef0123456789abcdef0123456789gh';
// $sectionId is the ID of section 1
$contentCreate->sectionId = $sectionId;
$contentCreate->alwaysAvailable = true;
// Create a draft
$draft = $contentService->createContent( $contentCreate, array( $locationCreate ) );
$content = $contentService->publishVersion( $draft->getVersionInfo() );
/* END: Inline */
return $content;
}
/**
* Creates a fresh clean content draft.
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
protected function createContentDraftVersion1( $locationId = 56, $contentTypeIdentifier = 'forum', $contentFieldNameIdentifier = 'name' )
{
$repository = $this->getRepository();
$parentLocationId = $this->generateId( 'location', $locationId );
$sectionId = $this->generateId( 'section', 1 );
/* BEGIN: Inline */
// $parentLocationId is the id of the /Design/eZ-publish node
$contentService = $repository->getContentService();
$contentTypeService = $repository->getContentTypeService();
$locationService = $repository->getLocationService();
// Configure new location
$locationCreate = $locationService->newLocationCreateStruct( $parentLocationId );
$locationCreate->priority = 23;
$locationCreate->hidden = true;
$locationCreate->remoteId = '0123456789abcdef0123456789abcdef';
$locationCreate->sortField = Location::SORT_FIELD_NODE_ID;
$locationCreate->sortOrder = Location::SORT_ORDER_DESC;
// Load content type
$contentType = $contentTypeService->loadContentTypeByIdentifier( $contentTypeIdentifier );
// Configure new content object
$contentCreate = $contentService->newContentCreateStruct( $contentType, 'eng-US' );
$contentCreate->setField( $contentFieldNameIdentifier, "An awesome {$contentTypeIdentifier}" );
$contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
// $sectionId is the ID of section 1
$contentCreate->sectionId = $sectionId;
$contentCreate->alwaysAvailable = true;
// Create a draft
$draft = $contentService->createContent( $contentCreate, array( $locationCreate ) );
/* END: Inline */
return $draft;
}
/**
* Creates a fresh clean published content instance.
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
protected function createContentVersion1()
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();
/* BEGIN: Inline */
$draft = $this->createContentDraftVersion1();
// Publish this draft
$content = $contentService->publishVersion( $draft->getVersionInfo() );
/* END: Inline */
return $content;
}
/**
* Creates a new content draft named <b>$draftVersion2</b> from a currently
* published content object.
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
protected function createContentDraftVersion2()
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();
/* BEGIN: Inline */
$content = $this->createContentVersion1();
// Create a new draft from the published content
$draftVersion2 = $contentService->createContentDraft( $content->contentInfo );
/* END: Inline */
return $draftVersion2;
}
/**
* Creates an updated content draft named <b>$draftVersion2</b> from
* a currently published content object.
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
protected function createUpdatedDraftVersion2()
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();
/* BEGIN: Inline */
$draftVersion2 = $this->createContentDraftVersion2();
// Create an update struct and modify some fields
$contentUpdate = $contentService->newContentUpdateStruct();
$contentUpdate->initialLanguageCode = 'eng-US';
$contentUpdate->creatorId = $this->generateId( 'user', 10 );
$contentUpdate->setField( 'name', 'An awesome forum²' );
$contentUpdate->setField( 'name', 'An awesome forum²³', 'eng-GB' );
// Update the content draft
$draftVersion2 = $contentService->updateContent(
$draftVersion2->getVersionInfo(),
$contentUpdate
);
/* END: Inline */
return $draftVersion2;
}
/**
* Creates an updated content draft named <b>$draftVersion2</b> from
* a currently published content object with a user different from the
* creator.
*
* @return array \eZ\Publish\API\Repository\Values\Content\Content, id
*/
protected function createUpdatedDraftVersion2NotAdmin()
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();
$userService = $repository->getUserService();
$mainLanguageCode = 'eng-US';
// Create a new user that belongs to the Administrator users group
$newUserCreateStruct = $userService->newUserCreateStruct( 'admin2', 'admin2@ez.no', "admin2", $mainLanguageCode );
$newUserCreateStruct->setField( 'first_name', 'Admin2', $mainLanguageCode );
$newUserCreateStruct->setField( 'last_name', 'Admin2', $mainLanguageCode );
// Load the Admin Group
$userAdminGroup = $userService->loadUserGroup( '12' );
$userAdmin2 = $userService->createUser( $newUserCreateStruct, array( $userAdminGroup ) );
/* BEGIN: Inline */
$draftVersion2 = $this->createContentDraftVersion2();
// Create an update struct and modify some fields
$contentUpdate = $contentService->newContentUpdateStruct();
$contentUpdate->initialLanguageCode = $mainLanguageCode;
$contentUpdate->creatorId = $this->generateId( 'user', $userAdmin2->id );
$contentUpdate->setField( 'name', 'An awesome forum²' );
$contentUpdate->setField( 'name', 'An awesome forum²³', 'eng-GB' );
// Update the content draft
$draftVersion2 = $contentService->updateContent(
$draftVersion2->getVersionInfo(),
$contentUpdate
);
/* END: Inline */
return array( $draftVersion2, $userAdmin2->id );
}
/**
* Creates an updated content object named <b>$contentVersion2</b> from
* a currently published content object.
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
protected function createContentVersion2()
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();
/* BEGIN: Inline */
$draftVersion2 = $this->createUpdatedDraftVersion2();
// Publish the updated draft
$contentVersion2 = $contentService->publishVersion( $draftVersion2->getVersionInfo() );
/* END: Inline */
return $contentVersion2;
}
/**
* Creates an updated content draft named <b>$draft</b>.
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
protected function createMultipleLanguageDraftVersion1()
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();
/* BEGIN: Inline */
$draft = $this->createContentDraftVersion1();
$contentUpdate = $contentService->newContentUpdateStruct();
$contentUpdate->initialLanguageCode = 'eng-US';
$contentUpdate->setField( 'name', 'An awesome multi-lang forum²' );
$contentUpdate->setField( 'name', 'An awesome multi-lang forum²³', 'eng-GB' );
$draft = $contentService->updateContent(
$draft->getVersionInfo(),
$contentUpdate
);
/* END: Inline */
return $draft;
}
/**
* Creates a published content object with versionNo=2 named
* <b>$contentVersion2</b>.
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
protected function createMultipleLanguageContentVersion2()
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();
/* BEGIN: Inline */
$draft = $this->createMultipleLanguageDraftVersion1();
// Publish this version.
$contentVersion1 = $contentService->publishVersion(
$draft->getVersionInfo()
);
// Create a new draft and update with same values
$draftVersion2 = $contentService->createContentDraft(
$contentVersion1->contentInfo
);
$contentUpdate = $contentService->newContentUpdateStruct();
foreach ( $draftVersion2->getFields() as $field )
{
$contentUpdate->setField( $field->fieldDefIdentifier, $field->value, $field->languageCode );
}
$contentService->updateContent(
$draftVersion2->getVersionInfo(),
$contentUpdate
);
// Finally publish version 2
$contentVersion2 = $contentService->publishVersion(
$draftVersion2->getVersionInfo()
);
/* END: Inline */
return $contentVersion2;
}
}