Completed
Push — master ( 7c0e42...8c347f )
by André
14:17
created

testCreateContentDraftSetsExpectedProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the ContentServiceTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\API\Repository\Tests;
10
11
use eZ\Publish\API\Repository\Values\Content\Content;
12
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
13
use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct;
14
use eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct;
15
use eZ\Publish\API\Repository\Values\Content\Field;
16
use eZ\Publish\API\Repository\Values\Content\Location;
17
use eZ\Publish\API\Repository\Values\Content\TranslationInfo;
18
use eZ\Publish\API\Repository\Values\Content\URLAlias;
19
use eZ\Publish\API\Repository\Values\Content\Relation;
20
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
21
use eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation;
22
use eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation;
23
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation;
24
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
25
use Exception;
26
27
/**
28
 * Test case for operations in the ContentService using in memory storage.
29
 *
30
 * @see eZ\Publish\API\Repository\ContentService
31
 * @group content
32
 */
33
class ContentServiceTest extends BaseContentServiceTest
34
{
35
    /**
36
     * Test for the newContentCreateStruct() method.
37
     *
38
     * @see \eZ\Publish\API\Repository\ContentService::newContentCreateStruct()
39
     * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testLoadContentTypeByIdentifier
40
     * @group user
41
     * @group field-type
42
     */
43
    public function testNewContentCreateStruct()
44
    {
45
        $repository = $this->getRepository();
46
47
        /* BEGIN: Use Case */
48
        // Create a content type
49
        $contentTypeService = $repository->getContentTypeService();
50
51
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
52
53
        $contentService = $repository->getContentService();
54
55
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
56
        /* END: Use Case */
57
58
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentCreateStruct', $contentCreate);
59
    }
60
61
    /**
62
     * Test for the createContent() method.
63
     *
64
     * @return \eZ\Publish\API\Repository\Values\Content\Content
65
     *
66
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
67
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentCreateStruct
68
     * @group user
69
     * @group field-type
70
     */
71
    public function testCreateContent()
72
    {
73
        if ($this->isVersion4()) {
74
            $this->markTestSkipped('This test requires eZ Publish 5');
75
        }
76
77
        $repository = $this->getRepository();
78
79
        /* BEGIN: Use Case */
80
        $contentTypeService = $repository->getContentTypeService();
81
82
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
83
84
        $contentService = $repository->getContentService();
85
86
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
87
        $contentCreate->setField('name', 'My awesome forum');
88
89
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
90
        $contentCreate->alwaysAvailable = true;
91
92
        $content = $contentService->createContent($contentCreate);
93
        /* END: Use Case */
94
95
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content', $content);
96
97
        return $content;
98
    }
99
100
    /**
101
     * Test for the createContent() method.
102
     *
103
     * Tests made for issue #EZP-20955 where Anonymous user is granted access to create content
104
     * and should have access to do that.
105
     *
106
     * @return \eZ\Publish\API\Repository\Values\Content\Content
107
     *
108
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
109
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentCreateStruct
110
     * @group user
111
     * @group field-type
112
     */
113
    public function testCreateContentAndPublishWithPrivilegedAnonymousUser()
114
    {
115
        if ($this->isVersion4()) {
116
            $this->markTestSkipped('This test requires eZ Publish 5');
117
        }
118
119
        $anonymousUserId = $this->generateId('user', 10);
120
121
        $repository = $this->getRepository();
122
        $contentService = $repository->getContentService();
123
        $contentTypeService = $repository->getContentTypeService();
124
        $locationService = $repository->getLocationService();
125
        $roleService = $repository->getRoleService();
126
127
        // Give Anonymous user role additional rights
128
        $role = $roleService->loadRoleByIdentifier('Anonymous');
129
        $roleDraft = $roleService->createRoleDraft($role);
130
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'create');
131
        $policyCreateStruct->addLimitation(new SectionLimitation(array('limitationValues' => array(1))));
132
        $policyCreateStruct->addLimitation(new LocationLimitation(array('limitationValues' => array(2))));
133
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(array('limitationValues' => array(1))));
134
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
135
136
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'publish');
137
        $policyCreateStruct->addLimitation(new SectionLimitation(array('limitationValues' => array(1))));
138
        $policyCreateStruct->addLimitation(new LocationLimitation(array('limitationValues' => array(2))));
139
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(array('limitationValues' => array(1))));
140
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
141
        $roleService->publishRoleDraft($roleDraft);
142
143
        // Set Anonymous user as current
144
        $repository->getPermissionResolver()->setCurrentUserReference($repository->getUserService()->loadUser($anonymousUserId));
145
146
        // Create a new content object:
147
        $contentCreate = $contentService->newContentCreateStruct(
148
            $contentTypeService->loadContentTypeByIdentifier('folder'),
149
            'eng-GB'
150
        );
151
152
        $contentCreate->setField('name', 'Folder 1');
153
154
        $content = $contentService->createContent(
155
            $contentCreate,
156
            array($locationService->newLocationCreateStruct(2))
157
        );
158
159
        $contentService->publishVersion(
160
            $content->getVersionInfo()
161
        );
162
    }
163
164
    /**
165
     * Test for the createContent() method.
166
     *
167
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
168
     *
169
     * @return \eZ\Publish\API\Repository\Values\Content\Content
170
     *
171
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
172
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
173
     */
174
    public function testCreateContentSetsContentInfo($content)
175
    {
176
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $content->contentInfo);
177
178
        return $content;
179
    }
180
181
    /**
182
     * Test for the createContent() method.
183
     *
184
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
185
     *
186
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
187
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsContentInfo
188
     */
189
    public function testCreateContentSetsExpectedContentInfo($content)
190
    {
191
        $this->assertEquals(
192
            array(
193
                $content->id,
194
                28, // id of content type "forum"
195
                true,
196
                1,
197
                'abcdef0123456789abcdef0123456789',
198
                'eng-US',
199
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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

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

Loading history...
200
                false,
201
                null,
202
                // Main Location id for unpublished Content should be null
203
                null,
204
            ),
205
            array(
206
                $content->contentInfo->id,
207
                $content->contentInfo->contentTypeId,
208
                $content->contentInfo->alwaysAvailable,
209
                $content->contentInfo->currentVersionNo,
210
                $content->contentInfo->remoteId,
211
                $content->contentInfo->mainLanguageCode,
212
                $content->contentInfo->ownerId,
213
                $content->contentInfo->published,
214
                $content->contentInfo->publishedDate,
215
                $content->contentInfo->mainLocationId,
216
            )
217
        );
218
    }
219
220
    /**
221
     * Test for the createContent() method.
222
     *
223
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
224
     *
225
     * @return \eZ\Publish\API\Repository\Values\Content\Content
226
     *
227
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
228
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
229
     */
230
    public function testCreateContentSetsVersionInfo($content)
231
    {
232
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo', $content->getVersionInfo());
233
234
        return $content;
235
    }
236
237
    /**
238
     * Test for the createContent() method.
239
     *
240
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
241
     *
242
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
243
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsVersionInfo
244
     */
245
    public function testCreateContentSetsExpectedVersionInfo($content)
246
    {
247
        $this->assertEquals(
248
            array(
249
                'status' => VersionInfo::STATUS_DRAFT,
250
                'versionNo' => 1,
251
                'creatorId' => $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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

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

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