Completed
Push — test-ezp29333-create_delete_wi... ( 1159e8 )
by
unknown
23:35
created

testCreateAndDeleteWithinSameTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

Loading history...
252
                'initialLanguageCode' => 'eng-US',
253
            ),
254
            array(
255
                'status' => $content->getVersionInfo()->status,
256
                'versionNo' => $content->getVersionInfo()->versionNo,
257
                'creatorId' => $content->getVersionInfo()->creatorId,
258
                'initialLanguageCode' => $content->getVersionInfo()->initialLanguageCode,
259
            )
260
        );
261
        $this->assertTrue($content->getVersionInfo()->isDraft());
262
        $this->assertFalse($content->getVersionInfo()->isPublished());
263
        $this->assertFalse($content->getVersionInfo()->isArchived());
264
    }
265
266
    /**
267
     * Test for the createContent() method.
268
     *
269
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
270
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
271
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
272
     */
273
    public function testCreateContentThrowsInvalidArgumentException()
274
    {
275
        if ($this->isVersion4()) {
276
            $this->markTestSkipped('This test requires eZ Publish 5');
277
        }
278
279
        $repository = $this->getRepository();
280
281
        /* BEGIN: Use Case */
282
        $contentTypeService = $repository->getContentTypeService();
283
        $contentService = $repository->getContentService();
284
285
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
286
287
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
288
        $contentCreate1->setField('name', 'An awesome Sidelfingen forum');
289
290
        $contentCreate1->remoteId = 'abcdef0123456789abcdef0123456789';
291
        $contentCreate1->alwaysAvailable = true;
292
293
        $draft = $contentService->createContent($contentCreate1);
294
        $contentService->publishVersion($draft->versionInfo);
295
296
        $contentCreate2 = $contentService->newContentCreateStruct($contentType, 'eng-GB');
297
        $contentCreate2->setField('name', 'An awesome Bielefeld forum');
298
299
        $contentCreate2->remoteId = 'abcdef0123456789abcdef0123456789';
300
        $contentCreate2->alwaysAvailable = false;
301
302
        // This call will fail with an "InvalidArgumentException", because the
303
        // remoteId is already in use.
304
        $contentService->createContent($contentCreate2);
305
        /* END: Use Case */
306
    }
307
308
    /**
309
     * Test for the createContent() method.
310
     *
311
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
312
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
313
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
314
     */
315 View Code Duplication
    public function testCreateContentThrowsInvalidArgumentExceptionOnFieldTypeNotAccept()
316
    {
317
        $repository = $this->getRepository();
318
319
        /* BEGIN: Use Case */
320
        $contentTypeService = $repository->getContentTypeService();
321
        $contentService = $repository->getContentService();
322
323
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
324
325
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
326
        // The name field does only accept strings and null as its values
327
        $contentCreate->setField('name', new \stdClass());
328
329
        // Throws InvalidArgumentException since the name field is filled
330
        // improperly
331
        $draft = $contentService->createContent($contentCreate);
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

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

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

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

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

Loading history...
332
        /* END: Use Case */
333
    }
334
335
    /**
336
     * Test for the createContent() method.
337
     *
338
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
339
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
340
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
341
     */
342
    public function testCreateContentThrowsContentFieldValidationException()
343
    {
344
        $repository = $this->getRepository();
345
346
        /* BEGIN: Use Case */
347
        $contentTypeService = $repository->getContentTypeService();
348
        $contentService = $repository->getContentService();
349
350
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
351
352
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
353
        $contentCreate1->setField('name', 'An awesome Sidelfingen folder');
354
        // Violates string length constraint
355
        $contentCreate1->setField('short_name', str_repeat('a', 200));
356
357
        // Throws ContentFieldValidationException, since short_name does not pass
358
        // validation of the string length validator
359
        $draft = $contentService->createContent($contentCreate1);
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

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

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

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

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

Loading history...
360
        /* END: Use Case */
361
    }
362
363
    /**
364
     * Test for the createContent() method.
365
     *
366
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
367
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
368
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
369
     */
370 View Code Duplication
    public function testCreateContentRequiredFieldMissing()
371
    {
372
        $repository = $this->getRepository();
373
374
        /* BEGIN: Use Case */
375
        $contentTypeService = $repository->getContentTypeService();
376
        $contentService = $repository->getContentService();
377
378
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
379
380
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
381
        // Required field "name" is not set
382
383
        // Throws a ContentFieldValidationException, since a required field is
384
        // missing
385
        $draft = $contentService->createContent($contentCreate1);
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

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

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

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

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

Loading history...
386
        /* END: Use Case */
387
    }
388
389
    /**
390
     * Test for the createContent() method.
391
     *
392
     * NOTE: We have bidirectional dependencies between the ContentService and
393
     * the LocationService, so that we cannot use PHPUnit's test dependencies
394
     * here.
395
     *
396
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
397
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
398
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationByRemoteId
399
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
400
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
401
     * @group user
402
     */
403
    public function testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately()
404
    {
405
        $repository = $this->getRepository();
406
407
        $locationService = $repository->getLocationService();
408
409
        /* BEGIN: Use Case */
410
        $draft = $this->createContentDraftVersion1();
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

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

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

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

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

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

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

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

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

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

Loading history...
415
            '0123456789abcdef0123456789abcdef'
416
        );
417
        /* END: Use Case */
418
    }
419
420
    /**
421
     * Test for the createContent() method.
422
     *
423
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
424
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
425
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
426
     */
427
    public function testCreateContentThrowsInvalidArgumentExceptionWithLocationCreateParameter()
428
    {
429
        $repository = $this->getRepository();
430
431
        $parentLocationId = $this->generateId('location', 56);
432
        /* BEGIN: Use Case */
433
        // $parentLocationId is a valid location ID
434
435
        $contentService = $repository->getContentService();
436
        $contentTypeService = $repository->getContentTypeService();
437
        $locationService = $repository->getLocationService();
438
439
        // Load content type
440
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
441
442
        // Configure new locations
443
        $locationCreate1 = $locationService->newLocationCreateStruct($parentLocationId);
444
445
        $locationCreate1->priority = 23;
446
        $locationCreate1->hidden = true;
447
        $locationCreate1->remoteId = '0123456789abcdef0123456789aaaaaa';
448
        $locationCreate1->sortField = Location::SORT_FIELD_NODE_ID;
449
        $locationCreate1->sortOrder = Location::SORT_ORDER_DESC;
450
451
        $locationCreate2 = $locationService->newLocationCreateStruct($parentLocationId);
452
453
        $locationCreate2->priority = 42;
454
        $locationCreate2->hidden = true;
455
        $locationCreate2->remoteId = '0123456789abcdef0123456789bbbbbb';
456
        $locationCreate2->sortField = Location::SORT_FIELD_NODE_ID;
457
        $locationCreate2->sortOrder = Location::SORT_ORDER_DESC;
458
459
        // Configure new content object
460
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
461
462
        $contentCreate->setField('name', 'A awesome Sindelfingen forum');
463
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
464
        $contentCreate->alwaysAvailable = true;
465
466
        // Create new content object under the specified location
467
        $draft = $contentService->createContent(
468
            $contentCreate,
469
            array($locationCreate1)
470
        );
471
        $contentService->publishVersion($draft->versionInfo);
472
473
        // This call will fail with an "InvalidArgumentException", because the
474
        // Content remoteId already exists,
475
        $contentService->createContent(
476
            $contentCreate,
477
            array($locationCreate2)
478
        );
479
        /* END: Use Case */
480
    }
481
482
    /**
483
     * Test for the loadContentInfo() method.
484
     *
485
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
486
     * @group user
487
     */
488 View Code Duplication
    public function testLoadContentInfo()
489
    {
490
        $repository = $this->getRepository();
491
492
        $mediaFolderId = $this->generateId('object', 41);
493
        /* BEGIN: Use Case */
494
        $contentService = $repository->getContentService();
495
496
        // Load the ContentInfo for "Media" folder
497
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
498
        /* END: Use Case */
499
500
        $this->assertInstanceOf(
501
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
502
            $contentInfo
503
        );
504
505
        return $contentInfo;
506
    }
507
508
    /**
509
     * Test for the returned value of the loadContentInfo() method.
510
     *
511
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
512
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfo
513
     *
514
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
515
     */
516
    public function testLoadContentInfoSetsExpectedContentInfo(ContentInfo $contentInfo)
517
    {
518
        $this->assertPropertiesCorrectUnsorted(
519
            $this->getExpectedMediaContentInfoProperties(),
520
            $contentInfo
521
        );
522
    }
523
524
    /**
525
     * Test for the loadContentInfo() method.
526
     *
527
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
528
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
529
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
530
     */
531 View Code Duplication
    public function testLoadContentInfoThrowsNotFoundException()
532
    {
533
        $repository = $this->getRepository();
534
535
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
536
        /* BEGIN: Use Case */
537
        $contentService = $repository->getContentService();
538
539
        // This call will fail with a NotFoundException
540
        $contentService->loadContentInfo($nonExistentContentId);
541
        /* END: Use Case */
542
    }
543
544
    /**
545
     * Test for the loadContentInfoByRemoteId() method.
546
     *
547
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
548
     */
549
    public function testLoadContentInfoByRemoteId()
550
    {
551
        $repository = $this->getRepository();
552
553
        /* BEGIN: Use Case */
554
        $contentService = $repository->getContentService();
555
556
        // Load the ContentInfo for "Media" folder
557
        $contentInfo = $contentService->loadContentInfoByRemoteId('faaeb9be3bd98ed09f606fc16d144eca');
558
        /* END: Use Case */
559
560
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $contentInfo);
561
562
        return $contentInfo;
563
    }
564
565
    /**
566
     * Test for the returned value of the loadContentInfoByRemoteId() method.
567
     *
568
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
569
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId
570
     *
571
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
572
     */
573 View Code Duplication
    public function testLoadContentInfoByRemoteIdSetsExpectedContentInfo(ContentInfo $contentInfo)
574
    {
575
        $this->assertPropertiesCorrectUnsorted(
576
            [
577
                'id' => 10,
578
                'contentTypeId' => 4,
579
                'name' => 'Anonymous User',
580
                'sectionId' => 2,
581
                'currentVersionNo' => 2,
582
                'published' => true,
583
                'ownerId' => 14,
584
                'modificationDate' => $this->createDateTime(1072180405),
585
                'publishedDate' => $this->createDateTime(1033920665),
586
                'alwaysAvailable' => 1,
587
                'remoteId' => 'faaeb9be3bd98ed09f606fc16d144eca',
588
                'mainLanguageCode' => 'eng-US',
589
                'mainLocationId' => 45,
590
            ],
591
            $contentInfo
592
        );
593
    }
594
595
    /**
596
     * Test for the loadContentInfoByRemoteId() method.
597
     *
598
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
599
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
600
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
601
     */
602
    public function testLoadContentInfoByRemoteIdThrowsNotFoundException()
603
    {
604
        $repository = $this->getRepository();
605
606
        /* BEGIN: Use Case */
607
        $contentService = $repository->getContentService();
608
609
        // This call will fail with a NotFoundException
610
        $contentService->loadContentInfoByRemoteId('abcdefghijklmnopqrstuvwxyz0123456789');
611
        /* END: Use Case */
612
    }
613
614
    /**
615
     * Test for the loadVersionInfo() method.
616
     *
617
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
618
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
619
     * @group user
620
     */
621
    public function testLoadVersionInfo()
622
    {
623
        $repository = $this->getRepository();
624
625
        $mediaFolderId = $this->generateId('object', 41);
626
        /* BEGIN: Use Case */
627
        // $mediaFolderId contains the ID of the "Media" folder
628
629
        $contentService = $repository->getContentService();
630
631
        // Load the ContentInfo for "Media" folder
632
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
633
634
        // Now load the current version info of the "Media" folder
635
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
636
        /* END: Use Case */
637
638
        $this->assertInstanceOf(
639
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
640
            $versionInfo
641
        );
642
    }
643
644
    /**
645
     * Test for the loadVersionInfoById() method.
646
     *
647
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
648
     */
649 View Code Duplication
    public function testLoadVersionInfoById()
650
    {
651
        $repository = $this->getRepository();
652
653
        $mediaFolderId = $this->generateId('object', 41);
654
        /* BEGIN: Use Case */
655
        // $mediaFolderId contains the ID of the "Media" folder
656
657
        $contentService = $repository->getContentService();
658
659
        // Load the VersionInfo for "Media" folder
660
        $versionInfo = $contentService->loadVersionInfoById($mediaFolderId);
661
        /* END: Use Case */
662
663
        $this->assertInstanceOf(
664
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
665
            $versionInfo
666
        );
667
668
        return $versionInfo;
669
    }
670
671
    /**
672
     * Test for the returned value of the loadVersionInfoById() method.
673
     *
674
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
675
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
676
     *
677
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
678
     */
679
    public function testLoadVersionInfoByIdSetsExpectedVersionInfo(VersionInfo $versionInfo)
680
    {
681
        $this->assertPropertiesCorrect(
682
            [
683
                'names' => [
684
                    'eng-US' => 'Media',
685
                ],
686
                'contentInfo' => new ContentInfo($this->getExpectedMediaContentInfoProperties()),
687
                'id' => 472,
688
                'versionNo' => 1,
689
                'modificationDate' => $this->createDateTime(1060695457),
690
                'creatorId' => 14,
691
                'creationDate' => $this->createDateTime(1060695450),
692
                'status' => VersionInfo::STATUS_PUBLISHED,
693
                'initialLanguageCode' => 'eng-US',
694
                'languageCodes' => [
695
                    'eng-US',
696
                ],
697
            ],
698
            $versionInfo
699
        );
700
        $this->assertTrue($versionInfo->isPublished());
701
        $this->assertFalse($versionInfo->isDraft());
702
        $this->assertFalse($versionInfo->isArchived());
703
    }
704
705
    /**
706
     * Test for the loadVersionInfoById() method.
707
     *
708
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
709
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
710
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
711
     */
712 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundException()
713
    {
714
        $repository = $this->getRepository();
715
716
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
717
        /* BEGIN: Use Case */
718
        $contentService = $repository->getContentService();
719
720
        // This call will fail with a "NotFoundException"
721
        $contentService->loadVersionInfoById($nonExistentContentId);
722
        /* END: Use Case */
723
    }
724
725
    /**
726
     * Test for the loadContentByContentInfo() method.
727
     *
728
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
729
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
730
     */
731
    public function testLoadContentByContentInfo()
732
    {
733
        $repository = $this->getRepository();
734
735
        $mediaFolderId = $this->generateId('object', 41);
736
        /* BEGIN: Use Case */
737
        // $mediaFolderId contains the ID of the "Media" folder
738
739
        $contentService = $repository->getContentService();
740
741
        // Load the ContentInfo for "Media" folder
742
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
743
744
        // Now load the current content version for the info instance
745
        $content = $contentService->loadContentByContentInfo($contentInfo);
746
        /* END: Use Case */
747
748
        $this->assertInstanceOf(
749
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
750
            $content
751
        );
752
    }
753
754
    /**
755
     * Test for the loadContentByVersionInfo() method.
756
     *
757
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
758
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
759
     */
760
    public function testLoadContentByVersionInfo()
761
    {
762
        $repository = $this->getRepository();
763
764
        $mediaFolderId = $this->generateId('object', 41);
765
        /* BEGIN: Use Case */
766
        // $mediaFolderId contains the ID of the "Media" folder
767
768
        $contentService = $repository->getContentService();
769
770
        // Load the ContentInfo for "Media" folder
771
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
772
773
        // Load the current VersionInfo
774
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
775
776
        // Now load the current content version for the info instance
777
        $content = $contentService->loadContentByVersionInfo($versionInfo);
778
        /* END: Use Case */
779
780
        $this->assertInstanceOf(
781
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
782
            $content
783
        );
784
    }
785
786
    /**
787
     * Test for the loadContent() method.
788
     *
789
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
790
     * @group user
791
     * @group field-type
792
     */
793 View Code Duplication
    public function testLoadContent()
794
    {
795
        $repository = $this->getRepository();
796
797
        $mediaFolderId = $this->generateId('object', 41);
798
        /* BEGIN: Use Case */
799
        // $mediaFolderId contains the ID of the "Media" folder
800
801
        $contentService = $repository->getContentService();
802
803
        // Load the Content for "Media" folder, any language and current version
804
        $content = $contentService->loadContent($mediaFolderId);
805
        /* END: Use Case */
806
807
        $this->assertInstanceOf(
808
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
809
            $content
810
        );
811
    }
812
813
    /**
814
     * Test for the loadContent() method.
815
     *
816
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
817
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
818
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
819
     */
820 View Code Duplication
    public function testLoadContentThrowsNotFoundException()
821
    {
822
        $repository = $this->getRepository();
823
824
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
825
        /* BEGIN: Use Case */
826
        $contentService = $repository->getContentService();
827
828
        // This call will fail with a "NotFoundException"
829
        $contentService->loadContent($nonExistentContentId);
830
        /* END: Use Case */
831
    }
832
833
    /**
834
     * Data provider for testLoadContentByRemoteId().
835
     *
836
     * @return array
837
     */
838
    public function contentRemoteIdVersionLanguageProvider()
839
    {
840
        return [
841
            ['f5c88a2209584891056f987fd965b0ba', null, null],
842
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], null],
843
            ['f5c88a2209584891056f987fd965b0ba', null, 1],
844
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], 1],
845
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, null],
846
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], null],
847
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, 1],
848
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], 1],
849
        ];
850
    }
851
852
    /**
853
     * Test for the loadContentByRemoteId() method.
854
     *
855
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId
856
     * @dataProvider contentRemoteIdVersionLanguageProvider
857
     *
858
     * @param string $remoteId
859
     * @param array|null $languages
860
     * @param int $versionNo
861
     */
862
    public function testLoadContentByRemoteId($remoteId, $languages, $versionNo)
863
    {
864
        $repository = $this->getRepository();
865
866
        $contentService = $repository->getContentService();
867
868
        $content = $contentService->loadContentByRemoteId($remoteId, $languages, $versionNo);
869
870
        $this->assertInstanceOf(
871
            Content::class,
872
            $content
873
        );
874
875
        $this->assertEquals($remoteId, $content->contentInfo->remoteId);
876
        if ($languages !== null) {
877
            $this->assertEquals($languages, $content->getVersionInfo()->languageCodes);
878
        }
879
        $this->assertEquals($versionNo ?: 1, $content->getVersionInfo()->versionNo);
880
    }
881
882
    /**
883
     * Test for the loadContentByRemoteId() method.
884
     *
885
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
886
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
887
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
888
     */
889
    public function testLoadContentByRemoteIdThrowsNotFoundException()
890
    {
891
        $repository = $this->getRepository();
892
893
        /* BEGIN: Use Case */
894
        $contentService = $repository->getContentService();
895
896
        // This call will fail with a "NotFoundException", because no content
897
        // object exists for the given remoteId
898
        $contentService->loadContentByRemoteId('a1b1c1d1e1f1a2b2c2d2e2f2a3b3c3d3');
899
        /* END: Use Case */
900
    }
901
902
    /**
903
     * Test for the publishVersion() method.
904
     *
905
     * @return \eZ\Publish\API\Repository\Values\Content\Content
906
     *
907
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
908
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
909
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
910
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
911
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
912
     * @group user
913
     * @group field-type
914
     */
915
    public function testPublishVersion()
916
    {
917
        $time = time();
918
        /* BEGIN: Use Case */
919
        $content = $this->createContentVersion1();
920
        /* END: Use Case */
921
922
        $this->assertInstanceOf(Content::class, $content);
923
        $this->assertTrue($content->contentInfo->published);
924
        $this->assertEquals(VersionInfo::STATUS_PUBLISHED, $content->versionInfo->status);
925
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->publishedDate->getTimestamp());
926
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->modificationDate->getTimestamp());
927
        $this->assertTrue($content->versionInfo->isPublished());
928
        $this->assertFalse($content->versionInfo->isDraft());
929
        $this->assertFalse($content->versionInfo->isArchived());
930
931
        return $content;
932
    }
933
934
    /**
935
     * Test for the publishVersion() method.
936
     *
937
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
938
     *
939
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
940
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
941
     */
942
    public function testPublishVersionSetsExpectedContentInfo($content)
943
    {
944
        $this->assertEquals(
945
            array(
946
                $content->id,
947
                true,
948
                1,
949
                'abcdef0123456789abcdef0123456789',
950
                'eng-US',
951
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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

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

Loading history...
952
                true,
953
            ),
954
            array(
955
                $content->contentInfo->id,
956
                $content->contentInfo->alwaysAvailable,
957
                $content->contentInfo->currentVersionNo,
958
                $content->contentInfo->remoteId,
959
                $content->contentInfo->mainLanguageCode,
960
                $content->contentInfo->ownerId,
961
                $content->contentInfo->published,
962
            )
963
        );
964
965
        $this->assertNotNull($content->contentInfo->mainLocationId);
966
        $date = new \DateTime('1984/01/01');
967
        $this->assertGreaterThan(
968
            $date->getTimestamp(),
969
            $content->contentInfo->publishedDate->getTimestamp()
970
        );
971
    }
972
973
    /**
974
     * Test for the publishVersion() method.
975
     *
976
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
977
     *
978
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
979
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
980
     */
981
    public function testPublishVersionSetsExpectedVersionInfo($content)
982
    {
983
        $this->assertEquals(
984
            array(
985
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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

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

Loading history...
986
                'eng-US',
987
                VersionInfo::STATUS_PUBLISHED,
988
                1,
989
            ),
990
            array(
991
                $content->getVersionInfo()->creatorId,
992
                $content->getVersionInfo()->initialLanguageCode,
993
                $content->getVersionInfo()->status,
994
                $content->getVersionInfo()->versionNo,
995
            )
996
        );
997
998
        $date = new \DateTime('1984/01/01');
999
        $this->assertGreaterThan(
1000
            $date->getTimestamp(),
1001
            $content->getVersionInfo()->modificationDate->getTimestamp()
1002
        );
1003
1004
        $this->assertNotNull($content->getVersionInfo()->modificationDate);
1005
        $this->assertTrue($content->getVersionInfo()->isPublished());
1006
        $this->assertFalse($content->getVersionInfo()->isDraft());
1007
        $this->assertFalse($content->getVersionInfo()->isArchived());
1008
    }
1009
1010
    /**
1011
     * Test for the publishVersion() method.
1012
     *
1013
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1014
     *
1015
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1016
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
1017
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1018
     */
1019
    public function testPublishVersionCreatesLocationsDefinedOnCreate()
1020
    {
1021
        $repository = $this->getRepository();
1022
1023
        /* BEGIN: Use Case */
1024
        $content = $this->createContentVersion1();
1025
        /* END: Use Case */
1026
1027
        $locationService = $repository->getLocationService();
1028
        $location = $locationService->loadLocationByRemoteId(
1029
            '0123456789abcdef0123456789abcdef'
1030
        );
1031
1032
        $this->assertEquals(
1033
            $location->getContentInfo(),
1034
            $content->getVersionInfo()->getContentInfo()
1035
        );
1036
1037
        return array($content, $location);
1038
    }
1039
1040
    /**
1041
     * Test for the publishVersion() method.
1042
     *
1043
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1044
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionCreatesLocationsDefinedOnCreate
1045
     */
1046
    public function testCreateContentWithLocationCreateParameterCreatesExpectedLocation(array $testData)
1047
    {
1048
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
1049
        /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
1050
        list($content, $location) = $testData;
1051
1052
        $parentLocationId = $this->generateId('location', 56);
1053
        $parentLocation = $this->getRepository()->getLocationService()->loadLocation($parentLocationId);
1054
        $mainLocationId = $content->getVersionInfo()->getContentInfo()->mainLocationId;
1055
1056
        $this->assertPropertiesCorrect(
1057
            array(
1058
                'id' => $mainLocationId,
1059
                'priority' => 23,
1060
                'hidden' => true,
1061
                'invisible' => true,
1062
                'remoteId' => '0123456789abcdef0123456789abcdef',
1063
                'parentLocationId' => $parentLocationId,
1064
                'pathString' => $parentLocation->pathString . $mainLocationId . '/',
1065
                'depth' => $parentLocation->depth + 1,
1066
                'sortField' => Location::SORT_FIELD_NODE_ID,
1067
                'sortOrder' => Location::SORT_ORDER_DESC,
1068
            ),
1069
            $location
1070
        );
1071
    }
1072
1073
    /**
1074
     * Test for the publishVersion() method.
1075
     *
1076
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1077
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1078
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1079
     */
1080 View Code Duplication
    public function testPublishVersionThrowsBadStateException()
1081
    {
1082
        $repository = $this->getRepository();
1083
1084
        $contentService = $repository->getContentService();
1085
1086
        /* BEGIN: Use Case */
1087
        $draft = $this->createContentDraftVersion1();
1088
1089
        // Publish the content draft
1090
        $contentService->publishVersion($draft->getVersionInfo());
1091
1092
        // This call will fail with a "BadStateException", because the version
1093
        // is already published.
1094
        $contentService->publishVersion($draft->getVersionInfo());
1095
        /* END: Use Case */
1096
    }
1097
1098
    /**
1099
     * Test that publishVersion() does not affect publishedDate (assuming previous version exists).
1100
     *
1101
     * @covers \eZ\Publish\API\Repository\ContentService::publishVersion
1102
     */
1103
    public function testPublishVersionDoesNotChangePublishedDate()
1104
    {
1105
        $repository = $this->getRepository();
1106
1107
        $contentService = $repository->getContentService();
1108
1109
        $publishedContent = $this->createContentVersion1();
1110
1111
        // force timestamps to differ
1112
        sleep(1);
1113
1114
        $contentDraft = $contentService->createContentDraft($publishedContent->contentInfo);
1115
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1116
        $contentUpdateStruct->setField('name', 'New name');
1117
        $contentDraft = $contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
1118
        $republishedContent = $contentService->publishVersion($contentDraft->versionInfo);
1119
1120
        $this->assertEquals(
1121
            $publishedContent->contentInfo->publishedDate->getTimestamp(),
1122
            $republishedContent->contentInfo->publishedDate->getTimestamp()
1123
        );
1124
        $this->assertGreaterThan(
1125
            $publishedContent->contentInfo->modificationDate->getTimestamp(),
1126
            $republishedContent->contentInfo->modificationDate->getTimestamp()
1127
        );
1128
    }
1129
1130
    /**
1131
     * Test for the createContentDraft() method.
1132
     *
1133
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1134
     *
1135
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1136
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1137
     * @group user
1138
     */
1139
    public function testCreateContentDraft()
1140
    {
1141
        $repository = $this->getRepository();
1142
1143
        $contentService = $repository->getContentService();
1144
1145
        /* BEGIN: Use Case */
1146
        $content = $this->createContentVersion1();
1147
1148
        // Now we create a new draft from the published content
1149
        $draftedContent = $contentService->createContentDraft($content->contentInfo);
1150
        /* END: Use Case */
1151
1152
        $this->assertInstanceOf(
1153
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1154
            $draftedContent
1155
        );
1156
1157
        return $draftedContent;
1158
    }
1159
1160
    /**
1161
     * Test for the createContentDraft() method.
1162
     *
1163
     * Test that editor has access to edit own draft.
1164
     * Note: Editors have access to version_read, which is needed to load content drafts.
1165
     *
1166
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1167
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1168
     * @group user
1169
     */
1170 View Code Duplication
    public function testCreateContentDraftAndLoadAccess()
1171
    {
1172
        $repository = $this->getRepository();
1173
1174
        /* BEGIN: Use Case */
1175
        $user = $this->createUserVersion1();
1176
1177
        // Set new editor as user
1178
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
1179
1180
        // Create draft
1181
        $draft = $this->createContentDraftVersion1(2, 'folder');
1182
1183
        // Try to load the draft
1184
        $contentService = $repository->getContentService();
1185
        $loadedDraft = $contentService->loadContent($draft->id);
1186
1187
        /* END: Use Case */
1188
1189
        $this->assertEquals($draft->id, $loadedDraft->id);
1190
    }
1191
1192
    /**
1193
     * Test for the createContentDraft() method.
1194
     *
1195
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1196
     *
1197
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1198
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1199
     */
1200
    public function testCreateContentDraftSetsExpectedProperties($draft)
1201
    {
1202
        $this->assertEquals(
1203
            array(
1204
                'fieldCount' => 2,
1205
                'relationCount' => 0,
1206
            ),
1207
            array(
1208
                'fieldCount' => count($draft->getFields()),
1209
                'relationCount' => count($this->getRepository()->getContentService()->loadRelations($draft->getVersionInfo())),
1210
            )
1211
        );
1212
    }
1213
1214
    /**
1215
     * Test for the createContentDraft() method.
1216
     *
1217
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1218
     *
1219
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1220
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1221
     */
1222
    public function testCreateContentDraftSetsContentInfo($draft)
1223
    {
1224
        $contentInfo = $draft->contentInfo;
1225
1226
        $this->assertEquals(
1227
            array(
1228
                $draft->id,
1229
                true,
1230
                1,
1231
                'eng-US',
1232
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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

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

Loading history...
1233
                'abcdef0123456789abcdef0123456789',
1234
                1,
1235
            ),
1236
            array(
1237
                $contentInfo->id,
1238
                $contentInfo->alwaysAvailable,
1239
                $contentInfo->currentVersionNo,
1240
                $contentInfo->mainLanguageCode,
1241
                $contentInfo->ownerId,
1242
                $contentInfo->remoteId,
1243
                $contentInfo->sectionId,
1244
            )
1245
        );
1246
    }
1247
1248
    /**
1249
     * Test for the createContentDraft() method.
1250
     *
1251
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1252
     *
1253
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1254
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1255
     */
1256
    public function testCreateContentDraftSetsVersionInfo($draft)
1257
    {
1258
        $versionInfo = $draft->getVersionInfo();
1259
1260
        $this->assertEquals(
1261
            array(
1262
                'creatorId' => $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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

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

Loading history...
1263
                'initialLanguageCode' => 'eng-US',
1264
                'languageCodes' => array(0 => 'eng-US'),
1265
                'status' => VersionInfo::STATUS_DRAFT,
1266
                'versionNo' => 2,
1267
            ),
1268
            array(
1269
                'creatorId' => $versionInfo->creatorId,
1270
                'initialLanguageCode' => $versionInfo->initialLanguageCode,
1271
                'languageCodes' => $versionInfo->languageCodes,
1272
                'status' => $versionInfo->status,
1273
                'versionNo' => $versionInfo->versionNo,
1274
            )
1275
        );
1276
        $this->assertTrue($versionInfo->isDraft());
1277
        $this->assertFalse($versionInfo->isPublished());
1278
        $this->assertFalse($versionInfo->isArchived());
1279
    }
1280
1281
    /**
1282
     * Test for the createContentDraft() method.
1283
     *
1284
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1285
     *
1286
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1287
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1288
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
1289
     */
1290
    public function testCreateContentDraftLoadVersionInfoStillLoadsPublishedVersion($draft)
0 ignored issues
show
Unused Code introduced by
The parameter $draft is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1291
    {
1292
        $repository = $this->getRepository();
1293
1294
        $contentService = $repository->getContentService();
1295
1296
        /* BEGIN: Use Case */
1297
        $content = $this->createContentVersion1();
1298
1299
        // Now we create a new draft from the published content
1300
        $contentService->createContentDraft($content->contentInfo);
1301
1302
        // This call will still load the published version
1303
        $versionInfoPublished = $contentService->loadVersionInfo($content->contentInfo);
1304
        /* END: Use Case */
1305
1306
        $this->assertEquals(1, $versionInfoPublished->versionNo);
1307
    }
1308
1309
    /**
1310
     * Test for the createContentDraft() method.
1311
     *
1312
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1313
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
1314
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1315
     */
1316
    public function testCreateContentDraftLoadContentStillLoadsPublishedVersion()
1317
    {
1318
        $repository = $this->getRepository();
1319
1320
        $contentService = $repository->getContentService();
1321
1322
        /* BEGIN: Use Case */
1323
        $content = $this->createContentVersion1();
1324
1325
        // Now we create a new draft from the published content
1326
        $contentService->createContentDraft($content->contentInfo);
1327
1328
        // This call will still load the published content version
1329
        $contentPublished = $contentService->loadContent($content->id);
1330
        /* END: Use Case */
1331
1332
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1333
    }
1334
1335
    /**
1336
     * Test for the createContentDraft() method.
1337
     *
1338
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1339
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
1340
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1341
     */
1342
    public function testCreateContentDraftLoadContentByRemoteIdStillLoadsPublishedVersion()
1343
    {
1344
        $repository = $this->getRepository();
1345
1346
        $contentService = $repository->getContentService();
1347
1348
        /* BEGIN: Use Case */
1349
        $content = $this->createContentVersion1();
1350
1351
        // Now we create a new draft from the published content
1352
        $contentService->createContentDraft($content->contentInfo);
1353
1354
        // This call will still load the published content version
1355
        $contentPublished = $contentService->loadContentByRemoteId('abcdef0123456789abcdef0123456789');
1356
        /* END: Use Case */
1357
1358
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1359
    }
1360
1361
    /**
1362
     * Test for the createContentDraft() method.
1363
     *
1364
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1365
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
1366
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1367
     */
1368
    public function testCreateContentDraftLoadContentByContentInfoStillLoadsPublishedVersion()
1369
    {
1370
        $repository = $this->getRepository();
1371
1372
        $contentService = $repository->getContentService();
1373
1374
        /* BEGIN: Use Case */
1375
        $content = $this->createContentVersion1();
1376
1377
        // Now we create a new draft from the published content
1378
        $contentService->createContentDraft($content->contentInfo);
1379
1380
        // This call will still load the published content version
1381
        $contentPublished = $contentService->loadContentByContentInfo($content->contentInfo);
1382
        /* END: Use Case */
1383
1384
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1385
    }
1386
1387
    /**
1388
     * Test for the newContentUpdateStruct() method.
1389
     *
1390
     * @covers \eZ\Publish\API\Repository\ContentService::newContentUpdateStruct
1391
     * @group user
1392
     */
1393
    public function testNewContentUpdateStruct()
1394
    {
1395
        $repository = $this->getRepository();
1396
1397
        /* BEGIN: Use Case */
1398
        $contentService = $repository->getContentService();
1399
1400
        $updateStruct = $contentService->newContentUpdateStruct();
1401
        /* END: Use Case */
1402
1403
        $this->assertInstanceOf(
1404
            ContentUpdateStruct::class,
1405
            $updateStruct
1406
        );
1407
1408
        $this->assertPropertiesCorrect(
1409
            [
1410
                'initialLanguageCode' => null,
1411
                'fields' => [],
1412
            ],
1413
            $updateStruct
1414
        );
1415
    }
1416
1417
    /**
1418
     * Test for the updateContent() method.
1419
     *
1420
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1421
     *
1422
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1423
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1424
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1425
     * @group user
1426
     * @group field-type
1427
     */
1428
    public function testUpdateContent()
1429
    {
1430
        /* BEGIN: Use Case */
1431
        $draftVersion2 = $this->createUpdatedDraftVersion2();
1432
        /* END: Use Case */
1433
1434
        $this->assertInstanceOf(
1435
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1436
            $draftVersion2
1437
        );
1438
1439
        $this->assertEquals(
1440
            $this->generateId('user', 10),
1441
            $draftVersion2->versionInfo->creatorId,
1442
            'creatorId is not properly set on new Version'
1443
        );
1444
1445
        return $draftVersion2;
1446
    }
1447
1448
    /**
1449
     * Test for the updateContent_WithDifferentUser() method.
1450
     *
1451
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1452
     *
1453
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1454
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1455
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1456
     * @group user
1457
     * @group field-type
1458
     */
1459
    public function testUpdateContentWithDifferentUser()
1460
    {
1461
        /* BEGIN: Use Case */
1462
        $arrayWithDraftVersion2 = $this->createUpdatedDraftVersion2NotAdmin();
1463
        /* END: Use Case */
1464
1465
        $this->assertInstanceOf(
1466
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1467
            $arrayWithDraftVersion2[0]
1468
        );
1469
1470
        $this->assertEquals(
1471
            $this->generateId('user', $arrayWithDraftVersion2[1]),
1472
            $arrayWithDraftVersion2[0]->versionInfo->creatorId,
1473
            'creatorId is not properly set on new Version'
1474
        );
1475
1476
        return $arrayWithDraftVersion2[0];
1477
    }
1478
1479
    /**
1480
     * Test for the updateContent() method.
1481
     *
1482
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1483
     *
1484
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1485
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1486
     */
1487
    public function testUpdateContentSetsExpectedFields($content)
1488
    {
1489
        $actual = $this->normalizeFields($content->getFields());
1490
1491
        $expected = array(
1492
            new Field(
1493
                array(
1494
                    'id' => 0,
1495
                    'value' => true,
1496
                    'languageCode' => 'eng-GB',
1497
                    'fieldDefIdentifier' => 'description',
1498
                    'fieldTypeIdentifier' => 'ezrichtext',
1499
                )
1500
            ),
1501
            new Field(
1502
                array(
1503
                    'id' => 0,
1504
                    'value' => true,
1505
                    'languageCode' => 'eng-US',
1506
                    'fieldDefIdentifier' => 'description',
1507
                    'fieldTypeIdentifier' => 'ezrichtext',
1508
                )
1509
            ),
1510
            new Field(
1511
                array(
1512
                    'id' => 0,
1513
                    'value' => true,
1514
                    'languageCode' => 'eng-GB',
1515
                    'fieldDefIdentifier' => 'name',
1516
                    'fieldTypeIdentifier' => 'ezstring',
1517
                )
1518
            ),
1519
            new Field(
1520
                array(
1521
                    'id' => 0,
1522
                    'value' => true,
1523
                    'languageCode' => 'eng-US',
1524
                    'fieldDefIdentifier' => 'name',
1525
                    'fieldTypeIdentifier' => 'ezstring',
1526
                )
1527
            ),
1528
        );
1529
1530
        $this->assertEquals($expected, $actual);
1531
    }
1532
1533
    /**
1534
     * Test for the updateContent() method.
1535
     *
1536
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1537
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1538
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1539
     */
1540
    public function testUpdateContentThrowsBadStateException()
1541
    {
1542
        $repository = $this->getRepository();
1543
1544
        $contentService = $repository->getContentService();
1545
1546
        /* BEGIN: Use Case */
1547
        $content = $this->createContentVersion1();
1548
1549
        // Now create an update struct and modify some fields
1550
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1551
        $contentUpdateStruct->setField('title', 'An awesome² story about ezp.');
1552
        $contentUpdateStruct->setField('title', 'An awesome²³ story about ezp.', 'eng-GB');
1553
1554
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1555
1556
        // This call will fail with a "BadStateException", because $publishedContent
1557
        // is not a draft.
1558
        $contentService->updateContent(
1559
            $content->getVersionInfo(),
1560
            $contentUpdateStruct
1561
        );
1562
        /* END: Use Case */
1563
    }
1564
1565
    /**
1566
     * Test for the updateContent() method.
1567
     *
1568
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1569
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1570
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1571
     */
1572 View Code Duplication
    public function testUpdateContentThrowsInvalidArgumentExceptionWhenFieldTypeDoesNotAccept()
1573
    {
1574
        $repository = $this->getRepository();
1575
1576
        $contentService = $repository->getContentService();
1577
1578
        /* BEGIN: Use Case */
1579
        $draft = $this->createContentDraftVersion1();
1580
1581
        // Now create an update struct and modify some fields
1582
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1583
        // The name field does not accept a stdClass object as its input
1584
        $contentUpdateStruct->setField('name', new \stdClass(), 'eng-US');
1585
1586
        // Throws an InvalidArgumentException, since the value for field "name"
1587
        // is not accepted
1588
        $contentService->updateContent(
1589
            $draft->getVersionInfo(),
1590
            $contentUpdateStruct
1591
        );
1592
        /* END: Use Case */
1593
    }
1594
1595
    /**
1596
     * Test for the updateContent() method.
1597
     *
1598
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1599
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1600
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1601
     */
1602 View Code Duplication
    public function testUpdateContentWhenMandatoryFieldIsEmpty()
1603
    {
1604
        $repository = $this->getRepository();
1605
1606
        $contentService = $repository->getContentService();
1607
1608
        /* BEGIN: Use Case */
1609
        $draft = $this->createContentDraftVersion1();
1610
1611
        // Now create an update struct and set a mandatory field to null
1612
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1613
        $contentUpdateStruct->setField('name', null);
1614
1615
        // Don't set this, then the above call without languageCode will fail
1616
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1617
1618
        // This call will fail with a "ContentFieldValidationException", because the
1619
        // mandatory "name" field is empty.
1620
        $contentService->updateContent(
1621
            $draft->getVersionInfo(),
1622
            $contentUpdateStruct
1623
        );
1624
        /* END: Use Case */
1625
    }
1626
1627
    /**
1628
     * Test for the updateContent() method.
1629
     *
1630
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1631
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1632
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1633
     */
1634
    public function testUpdateContentThrowsContentFieldValidationException()
1635
    {
1636
        $repository = $this->getRepository();
1637
1638
        /* BEGIN: Use Case */
1639
        $contentTypeService = $repository->getContentTypeService();
1640
        $contentService = $repository->getContentService();
1641
1642
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1643
1644
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1645
        $contentCreate->setField('name', 'An awesome Sidelfingen folder');
1646
1647
        $draft = $contentService->createContent($contentCreate);
1648
1649
        $contentUpdate = $contentService->newContentUpdateStruct();
1650
        // Violates string length constraint
1651
        $contentUpdate->setField('short_name', str_repeat('a', 200), 'eng-US');
1652
1653
        // Throws ContentFieldValidationException because the string length
1654
        // validation of the field "short_name" fails
1655
        $contentService->updateContent($draft->getVersionInfo(), $contentUpdate);
1656
        /* END: Use Case */
1657
    }
1658
1659
    /**
1660
     * Test for the updateContent() method.
1661
     *
1662
     * @covers \eZ\Publish\API\Repository\ContentService::updateContent()
1663
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1664
     */
1665
    public function testUpdateContentValidatorIgnoresRequiredFieldsOfNotUpdatedLanguages()
1666
    {
1667
        $repository = $this->getRepository();
1668
        /* BEGIN: Use Case */
1669
        $contentTypeService = $repository->getContentTypeService();
1670
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1671
1672
        // Create multilangual content
1673
        $contentService = $repository->getContentService();
1674
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1675
        $contentCreate->setField('name', 'An awesome Sidelfingen folder', 'eng-US');
1676
        $contentCreate->setField('name', 'An awesome Sidelfingen folder', 'eng-GB');
1677
1678
        $contentDraft = $contentService->createContent($contentCreate);
1679
1680
        // 2. Update content type definition
1681
        $contentTypeDraft = $contentTypeService->createContentTypeDraft($contentType);
1682
1683
        $fieldDefinition = $contentType->getFieldDefinition('description');
1684
        $fieldDefinitionUpdate = $contentTypeService->newFieldDefinitionUpdateStruct();
1685
        $fieldDefinitionUpdate->identifier = 'description';
1686
        $fieldDefinitionUpdate->isRequired = true;
1687
1688
        $contentTypeService->updateFieldDefinition(
1689
            $contentTypeDraft,
1690
            $fieldDefinition,
1691
            $fieldDefinitionUpdate
1692
        );
1693
        $contentTypeService->publishContentTypeDraft($contentTypeDraft);
1694
1695
        // 3. Update only eng-US translation
1696
        $description = new DOMDocument();
1697
        $description->loadXML(<<<XML
1698
<?xml version="1.0" encoding="UTF-8"?>
1699
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
1700
    <para>Lorem ipsum dolor</para>
1701
</section>
1702
XML
1703
        );
1704
1705
        $contentUpdate = $contentService->newContentUpdateStruct();
1706
        $contentUpdate->setField('name', 'An awesome Sidelfingen folder (updated)', 'eng-US');
1707
        $contentUpdate->setField('description', $description);
1708
1709
        $contentService->updateContent($contentDraft->getVersionInfo(), $contentUpdate);
1710
        /* END: Use Case */
1711
    }
1712
1713
    /**
1714
     * Test for the updateContent() method.
1715
     *
1716
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1717
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1718
     */
1719
    public function testUpdateContentWithNotUpdatingMandatoryField()
1720
    {
1721
        $repository = $this->getRepository();
1722
1723
        $contentService = $repository->getContentService();
1724
1725
        /* BEGIN: Use Case */
1726
        $draft = $this->createContentDraftVersion1();
1727
1728
        // Now create an update struct which does not overwrite mandatory
1729
        // fields
1730
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1731
        $contentUpdateStruct->setField(
1732
            'description',
1733
            '<?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"/>'
1734
        );
1735
1736
        // Don't set this, then the above call without languageCode will fail
1737
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1738
1739
        // This will only update the "description" field in the "eng-US"
1740
        // language
1741
        $updatedDraft = $contentService->updateContent(
1742
            $draft->getVersionInfo(),
1743
            $contentUpdateStruct
1744
        );
1745
        /* END: Use Case */
1746
1747
        foreach ($updatedDraft->getFields() as $field) {
1748
            if ($field->languageCode === 'eng-US' && $field->fieldDefIdentifier === 'name' && $field->value !== null) {
1749
                // Found field
1750
                return;
1751
            }
1752
        }
1753
        $this->fail(
1754
            'Field with identifier "name" in language "eng-US" could not be found or has empty value.'
1755
        );
1756
    }
1757
1758
    /**
1759
     * Test for the createContentDraft() method.
1760
     *
1761
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
1762
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1763
     */
1764
    public function testCreateContentDraftWithSecondParameter()
1765
    {
1766
        $repository = $this->getRepository();
1767
1768
        $contentService = $repository->getContentService();
1769
1770
        /* BEGIN: Use Case */
1771
        $contentVersion2 = $this->createContentVersion2();
1772
1773
        // Now we create a new draft from the initial version
1774
        $draftedContentReloaded = $contentService->createContentDraft(
1775
            $contentVersion2->contentInfo,
1776
            $contentVersion2->getVersionInfo()
1777
        );
1778
        /* END: Use Case */
1779
1780
        $this->assertEquals(3, $draftedContentReloaded->getVersionInfo()->versionNo);
1781
    }
1782
1783
    /**
1784
     * Test for the createContentDraft() method with third parameter.
1785
     *
1786
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1787
     */
1788 View Code Duplication
    public function testCreateContentDraftWithThirdParameter()
1789
    {
1790
        $repository = $this->getRepository();
1791
1792
        $contentService = $repository->getContentService();
1793
1794
        $content = $contentService->loadContent(4);
1795
        $user = $this->createUserVersion1();
1796
1797
        $draftContent = $contentService->createContentDraft(
1798
            $content->contentInfo,
1799
            $content->getVersionInfo(),
1800
            $user
1801
        );
1802
1803
        $this->assertInstanceOf(
1804
            Content::class,
1805
            $draftContent
1806
        );
1807
    }
1808
1809
    /**
1810
     * Test for the publishVersion() method.
1811
     *
1812
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1813
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1814
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1815
     */
1816 View Code Duplication
    public function testPublishVersionFromContentDraft()
1817
    {
1818
        $repository = $this->getRepository();
1819
1820
        $contentService = $repository->getContentService();
1821
1822
        /* BEGIN: Use Case */
1823
        $contentVersion2 = $this->createContentVersion2();
1824
        /* END: Use Case */
1825
1826
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo);
1827
1828
        $this->assertEquals(
1829
            array(
1830
                'status' => VersionInfo::STATUS_PUBLISHED,
1831
                'versionNo' => 2,
1832
            ),
1833
            array(
1834
                'status' => $versionInfo->status,
1835
                'versionNo' => $versionInfo->versionNo,
1836
            )
1837
        );
1838
        $this->assertTrue($versionInfo->isPublished());
1839
        $this->assertFalse($versionInfo->isDraft());
1840
        $this->assertFalse($versionInfo->isArchived());
1841
    }
1842
1843
    /**
1844
     * Test for the publishVersion() method.
1845
     *
1846
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1847
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1848
     */
1849 View Code Duplication
    public function testPublishVersionFromContentDraftArchivesOldVersion()
1850
    {
1851
        $repository = $this->getRepository();
1852
1853
        $contentService = $repository->getContentService();
1854
1855
        /* BEGIN: Use Case */
1856
        $contentVersion2 = $this->createContentVersion2();
1857
        /* END: Use Case */
1858
1859
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo, 1);
1860
1861
        $this->assertEquals(
1862
            array(
1863
                'status' => VersionInfo::STATUS_ARCHIVED,
1864
                'versionNo' => 1,
1865
            ),
1866
            array(
1867
                'status' => $versionInfo->status,
1868
                'versionNo' => $versionInfo->versionNo,
1869
            )
1870
        );
1871
        $this->assertTrue($versionInfo->isArchived());
1872
        $this->assertFalse($versionInfo->isDraft());
1873
        $this->assertFalse($versionInfo->isPublished());
1874
    }
1875
1876
    /**
1877
     * Test for the publishVersion() method.
1878
     *
1879
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1880
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1881
     */
1882
    public function testPublishVersionFromContentDraftUpdatesContentInfoCurrentVersion()
1883
    {
1884
        /* BEGIN: Use Case */
1885
        $contentVersion2 = $this->createContentVersion2();
1886
        /* END: Use Case */
1887
1888
        $this->assertEquals(2, $contentVersion2->contentInfo->currentVersionNo);
1889
    }
1890
1891
    /**
1892
     * Test for the publishVersion() method.
1893
     *
1894
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1895
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1896
     */
1897 View Code Duplication
    public function testPublishVersionFromOldContentDraftArchivesNewerVersionNo()
1898
    {
1899
        $repository = $this->getRepository();
1900
1901
        $contentService = $repository->getContentService();
1902
1903
        /* BEGIN: Use Case */
1904
        $content = $this->createContentVersion1();
1905
1906
        // Create a new draft with versionNo = 2
1907
        $draftedContentVersion2 = $contentService->createContentDraft($content->contentInfo);
1908
1909
        // Create another new draft with versionNo = 3
1910
        $draftedContentVersion3 = $contentService->createContentDraft($content->contentInfo);
1911
1912
        // Publish draft with versionNo = 3
1913
        $contentService->publishVersion($draftedContentVersion3->getVersionInfo());
1914
1915
        // Publish the first draft with versionNo = 2
1916
        // currentVersionNo is now 2, versionNo 3 will be archived
1917
        $publishedDraft = $contentService->publishVersion($draftedContentVersion2->getVersionInfo());
1918
        /* END: Use Case */
1919
1920
        $this->assertEquals(2, $publishedDraft->contentInfo->currentVersionNo);
1921
    }
1922
1923
    /**
1924
     * Test for the publishVersion() method, and that it creates limited archives.
1925
     *
1926
     * @todo Adapt this when per content type archive limited is added on repository Content Type model.
1927
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1928
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1929
     */
1930
    public function testPublishVersionNotCreatingUnlimitedArchives()
1931
    {
1932
        $repository = $this->getRepository();
1933
1934
        $contentService = $repository->getContentService();
1935
1936
        $content = $this->createContentVersion1();
1937
1938
        // load first to make sure list gets updated also (cache)
1939
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
1940
        $this->assertEquals(1, count($versionInfoList));
1941
        $this->assertEquals(1, $versionInfoList[0]->versionNo);
1942
1943
        // Create a new draft with versionNo = 2
1944
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1945
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1946
1947
        // Create a new draft with versionNo = 3
1948
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1949
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1950
1951
        // Create a new draft with versionNo = 4
1952
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1953
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1954
1955
        // Create a new draft with versionNo = 5
1956
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1957
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1958
1959
        // Create a new draft with versionNo = 6
1960
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1961
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1962
1963
        // Create a new draft with versionNo = 7
1964
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1965
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1966
1967
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
1968
1969
        $this->assertEquals(6, count($versionInfoList));
1970
        $this->assertEquals(2, $versionInfoList[0]->versionNo);
1971
        $this->assertEquals(7, $versionInfoList[5]->versionNo);
1972
1973
        $this->assertEquals(
1974
            [
1975
                VersionInfo::STATUS_ARCHIVED,
1976
                VersionInfo::STATUS_ARCHIVED,
1977
                VersionInfo::STATUS_ARCHIVED,
1978
                VersionInfo::STATUS_ARCHIVED,
1979
                VersionInfo::STATUS_ARCHIVED,
1980
                VersionInfo::STATUS_PUBLISHED,
1981
            ],
1982
            [
1983
                $versionInfoList[0]->status,
1984
                $versionInfoList[1]->status,
1985
                $versionInfoList[2]->status,
1986
                $versionInfoList[3]->status,
1987
                $versionInfoList[4]->status,
1988
                $versionInfoList[5]->status,
1989
            ]
1990
        );
1991
    }
1992
1993
    /**
1994
     * Test for the newContentMetadataUpdateStruct() method.
1995
     *
1996
     * @covers \eZ\Publish\API\Repository\ContentService::newContentMetadataUpdateStruct
1997
     * @group user
1998
     */
1999
    public function testNewContentMetadataUpdateStruct()
2000
    {
2001
        $repository = $this->getRepository();
2002
2003
        /* BEGIN: Use Case */
2004
        $contentService = $repository->getContentService();
2005
2006
        // Creates a new metadata update struct
2007
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2008
2009
        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...
2010
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
2011
        }
2012
2013
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
2014
        $metadataUpdate->mainLanguageCode = 'eng-GB';
2015
        $metadataUpdate->alwaysAvailable = false;
2016
        /* END: Use Case */
2017
2018
        $this->assertInstanceOf(
2019
            ContentMetadataUpdateStruct::class,
2020
            $metadataUpdate
2021
        );
2022
    }
2023
2024
    /**
2025
     * Test for the updateContentMetadata() method.
2026
     *
2027
     * @return \eZ\Publish\API\Repository\Values\Content\Content
2028
     *
2029
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2030
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2031
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentMetadataUpdateStruct
2032
     * @group user
2033
     */
2034
    public function testUpdateContentMetadata()
2035
    {
2036
        $repository = $this->getRepository();
2037
2038
        $contentService = $repository->getContentService();
2039
2040
        /* BEGIN: Use Case */
2041
        $content = $this->createContentVersion1();
2042
2043
        // Creates a metadata update struct
2044
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2045
2046
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
2047
        $metadataUpdate->mainLanguageCode = 'eng-GB';
2048
        $metadataUpdate->alwaysAvailable = false;
2049
        $metadataUpdate->publishedDate = $this->createDateTime(441759600); // 1984/01/01
2050
        $metadataUpdate->modificationDate = $this->createDateTime(441759600); // 1984/01/01
2051
2052
        // Update the metadata of the published content object
2053
        $content = $contentService->updateContentMetadata(
2054
            $content->contentInfo,
2055
            $metadataUpdate
2056
        );
2057
        /* END: Use Case */
2058
2059
        $this->assertInstanceOf(
2060
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
2061
            $content
2062
        );
2063
2064
        return $content;
2065
    }
2066
2067
    /**
2068
     * Test for the updateContentMetadata() method.
2069
     *
2070
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2071
     *
2072
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2073
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2074
     */
2075
    public function testUpdateContentMetadataSetsExpectedProperties($content)
2076
    {
2077
        $contentInfo = $content->contentInfo;
2078
2079
        $this->assertEquals(
2080
            array(
2081
                'remoteId' => 'aaaabbbbccccddddeeeeffff11112222',
2082
                'sectionId' => $this->generateId('section', 1),
2083
                'alwaysAvailable' => false,
2084
                'currentVersionNo' => 1,
2085
                'mainLanguageCode' => 'eng-GB',
2086
                'modificationDate' => $this->createDateTime(441759600),
2087
                '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...
2088
                'published' => true,
2089
                'publishedDate' => $this->createDateTime(441759600),
2090
            ),
2091
            array(
2092
                'remoteId' => $contentInfo->remoteId,
2093
                'sectionId' => $contentInfo->sectionId,
2094
                'alwaysAvailable' => $contentInfo->alwaysAvailable,
2095
                'currentVersionNo' => $contentInfo->currentVersionNo,
2096
                'mainLanguageCode' => $contentInfo->mainLanguageCode,
2097
                'modificationDate' => $contentInfo->modificationDate,
2098
                'ownerId' => $contentInfo->ownerId,
2099
                'published' => $contentInfo->published,
2100
                'publishedDate' => $contentInfo->publishedDate,
2101
            )
2102
        );
2103
    }
2104
2105
    /**
2106
     * Test for the updateContentMetadata() method.
2107
     *
2108
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2109
     *
2110
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2111
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2112
     */
2113
    public function testUpdateContentMetadataNotUpdatesContentVersion($content)
2114
    {
2115
        $this->assertEquals(1, $content->getVersionInfo()->versionNo);
2116
    }
2117
2118
    /**
2119
     * Test for the updateContentMetadata() method.
2120
     *
2121
     * @covers \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2122
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2123
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2124
     */
2125
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnDuplicateRemoteId()
2126
    {
2127
        $repository = $this->getRepository();
2128
2129
        $contentService = $repository->getContentService();
2130
2131
        /* BEGIN: Use Case */
2132
        // RemoteId of the "Media" page of an eZ Publish demo installation
2133
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2134
2135
        $content = $this->createContentVersion1();
2136
2137
        // Creates a metadata update struct
2138
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2139
        $metadataUpdate->remoteId = $mediaRemoteId;
2140
2141
        // This call will fail with an "InvalidArgumentException", because the
2142
        // specified remoteId is already used by the "Media" page.
2143
        $contentService->updateContentMetadata(
2144
            $content->contentInfo,
2145
            $metadataUpdate
2146
        );
2147
        /* END: Use Case */
2148
    }
2149
2150
    /**
2151
     * Test for the updateContentMetadata() method.
2152
     *
2153
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContentMetadata
2154
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2155
     */
2156
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnNoMetadataPropertiesSet()
2157
    {
2158
        $repository = $this->getRepository();
2159
2160
        $contentService = $repository->getContentService();
2161
2162
        $contentInfo = $contentService->loadContentInfo(4);
2163
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
2164
2165
        // Throws an exception because no properties are set in $contentMetadataUpdateStruct
2166
        $contentService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
2167
    }
2168
2169
    /**
2170
     * Test for the deleteContent() method.
2171
     *
2172
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2173
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2174
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2175
     */
2176 View Code Duplication
    public function testDeleteContent()
2177
    {
2178
        $repository = $this->getRepository();
2179
2180
        $contentService = $repository->getContentService();
2181
        $locationService = $repository->getLocationService();
2182
2183
        /* BEGIN: Use Case */
2184
        $contentVersion2 = $this->createContentVersion2();
2185
2186
        // Load the locations for this content object
2187
        $locations = $locationService->loadLocations($contentVersion2->contentInfo);
2188
2189
        // This will delete the content, all versions and the associated locations
2190
        $contentService->deleteContent($contentVersion2->contentInfo);
2191
        /* END: Use Case */
2192
2193
        foreach ($locations as $location) {
2194
            $locationService->loadLocation($location->id);
2195
        }
2196
    }
2197
2198
    /**
2199
     * Test for the deleteContent() method.
2200
     *
2201
     * Test for issue EZP-21057:
2202
     * "contentService: Unable to delete a content with an empty file attribute"
2203
     *
2204
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2205
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2206
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2207
     */
2208 View Code Duplication
    public function testDeleteContentWithEmptyBinaryField()
2209
    {
2210
        $repository = $this->getRepository();
2211
2212
        $contentService = $repository->getContentService();
2213
        $locationService = $repository->getLocationService();
2214
2215
        /* BEGIN: Use Case */
2216
        $contentVersion = $this->createContentVersion1EmptyBinaryField();
2217
2218
        // Load the locations for this content object
2219
        $locations = $locationService->loadLocations($contentVersion->contentInfo);
2220
2221
        // This will delete the content, all versions and the associated locations
2222
        $contentService->deleteContent($contentVersion->contentInfo);
2223
        /* END: Use Case */
2224
2225
        foreach ($locations as $location) {
2226
            $locationService->loadLocation($location->id);
2227
        }
2228
    }
2229
2230
    /**
2231
     * Test for the loadContentDrafts() method.
2232
     *
2233
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2234
     */
2235
    public function testLoadContentDraftsReturnsEmptyArrayByDefault()
2236
    {
2237
        $repository = $this->getRepository();
2238
2239
        /* BEGIN: Use Case */
2240
        $contentService = $repository->getContentService();
2241
2242
        $contentDrafts = $contentService->loadContentDrafts();
2243
        /* END: Use Case */
2244
2245
        $this->assertSame(array(), $contentDrafts);
2246
    }
2247
2248
    /**
2249
     * Test for the loadContentDrafts() method.
2250
     *
2251
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2252
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2253
     */
2254
    public function testLoadContentDrafts()
2255
    {
2256
        $repository = $this->getRepository();
2257
2258
        /* BEGIN: Use Case */
2259
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
2260
        // of a eZ Publish demo installation.
2261
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2262
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
2263
2264
        $contentService = $repository->getContentService();
2265
2266
        // "Media" content object
2267
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2268
2269
        // "eZ Publish Demo Design ..." content object
2270
        $demoDesignContentInfo = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
2271
2272
        // Create some drafts
2273
        $contentService->createContentDraft($mediaContentInfo);
2274
        $contentService->createContentDraft($demoDesignContentInfo);
2275
2276
        // Now $contentDrafts should contain two drafted versions
2277
        $draftedVersions = $contentService->loadContentDrafts();
2278
        /* END: Use Case */
2279
2280
        $actual = array(
2281
            $draftedVersions[0]->status,
2282
            $draftedVersions[0]->getContentInfo()->remoteId,
2283
            $draftedVersions[1]->status,
2284
            $draftedVersions[1]->getContentInfo()->remoteId,
2285
        );
2286
        sort($actual, SORT_STRING);
2287
2288
        $this->assertEquals(
2289
            array(
2290
                VersionInfo::STATUS_DRAFT,
2291
                VersionInfo::STATUS_DRAFT,
2292
                $demoDesignRemoteId,
2293
                $mediaRemoteId,
2294
            ),
2295
            $actual
2296
        );
2297
    }
2298
2299
    /**
2300
     * Test for the loadContentDrafts() method.
2301
     *
2302
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
2303
     */
2304
    public function testLoadContentDraftsWithFirstParameter()
2305
    {
2306
        $repository = $this->getRepository();
2307
2308
        /* BEGIN: Use Case */
2309
        $user = $this->createUserVersion1();
2310
2311
        // Get current user
2312
        $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...
2313
2314
        // Set new editor as user
2315
        $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...
2316
2317
        // Remote id of the "Media" content object in an eZ Publish demo installation.
2318
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2319
2320
        $contentService = $repository->getContentService();
2321
2322
        // "Media" content object
2323
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2324
2325
        // Create a content draft
2326
        $contentService->createContentDraft($mediaContentInfo);
2327
2328
        // Reset to previous current user
2329
        $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...
2330
2331
        // Now $contentDrafts for the previous current user and the new user
2332
        $newCurrentUserDrafts = $contentService->loadContentDrafts($user);
2333
        $oldCurrentUserDrafts = $contentService->loadContentDrafts($oldCurrentUser);
2334
        /* END: Use Case */
2335
2336
        $this->assertSame(array(), $oldCurrentUserDrafts);
2337
2338
        $this->assertEquals(
2339
            array(
2340
                VersionInfo::STATUS_DRAFT,
2341
                $mediaRemoteId,
2342
            ),
2343
            array(
2344
                $newCurrentUserDrafts[0]->status,
2345
                $newCurrentUserDrafts[0]->getContentInfo()->remoteId,
2346
            )
2347
        );
2348
        $this->assertTrue($newCurrentUserDrafts[0]->isDraft());
2349
        $this->assertFalse($newCurrentUserDrafts[0]->isArchived());
2350
        $this->assertFalse($newCurrentUserDrafts[0]->isPublished());
2351
    }
2352
2353
    /**
2354
     * Test for the loadVersionInfo() method.
2355
     *
2356
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2357
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2358
     */
2359
    public function testLoadVersionInfoWithSecondParameter()
2360
    {
2361
        $repository = $this->getRepository();
2362
2363
        $contentService = $repository->getContentService();
2364
2365
        /* BEGIN: Use Case */
2366
        $publishedContent = $this->createContentVersion1();
2367
2368
        $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...
2369
2370
        // Will return the VersionInfo of the $draftContent
2371
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2372
        /* END: Use Case */
2373
2374
        $this->assertEquals(2, $versionInfo->versionNo);
2375
2376
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2377
        $this->assertEquals(
2378
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2379
            $versionInfo->getContentInfo()->mainLocationId
2380
        );
2381
    }
2382
2383
    /**
2384
     * Test for the loadVersionInfo() method.
2385
     *
2386
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2387
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2388
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2389
     */
2390
    public function testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter()
2391
    {
2392
        $repository = $this->getRepository();
2393
2394
        $contentService = $repository->getContentService();
2395
2396
        /* BEGIN: Use Case */
2397
        $draft = $this->createContentDraftVersion1();
2398
2399
        // This call will fail with a "NotFoundException", because not versionNo
2400
        // 2 exists for this content object.
2401
        $contentService->loadVersionInfo($draft->contentInfo, 2);
2402
        /* END: Use Case */
2403
    }
2404
2405
    /**
2406
     * Test for the loadVersionInfoById() method.
2407
     *
2408
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2409
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2410
     */
2411
    public function testLoadVersionInfoByIdWithSecondParameter()
2412
    {
2413
        $repository = $this->getRepository();
2414
2415
        $contentService = $repository->getContentService();
2416
2417
        /* BEGIN: Use Case */
2418
        $publishedContent = $this->createContentVersion1();
2419
2420
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
2421
2422
        // Will return the VersionInfo of the $draftContent
2423
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2424
        /* END: Use Case */
2425
2426
        $this->assertEquals(2, $versionInfo->versionNo);
2427
2428
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2429
        $this->assertEquals(
2430
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2431
            $versionInfo->getContentInfo()->mainLocationId
2432
        );
2433
2434
        return [
2435
            'versionInfo' => $versionInfo,
2436
            'draftContent' => $draftContent,
2437
        ];
2438
    }
2439
2440
    /**
2441
     * Test for the returned value of the loadVersionInfoById() method.
2442
     *
2443
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter
2444
     * @covers \eZ\Publish\API\Repository\ContentService::loadVersionInfoById
2445
     *
2446
     * @param array $data
2447
     */
2448
    public function testLoadVersionInfoByIdWithSecondParameterSetsExpectedVersionInfo(array $data)
2449
    {
2450
        /** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo */
2451
        $versionInfo = $data['versionInfo'];
2452
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $draftContent */
2453
        $draftContent = $data['draftContent'];
2454
2455
        $this->assertPropertiesCorrect(
2456
            [
2457
                'names' => [
2458
                    'eng-US' => 'An awesome forum',
2459
                ],
2460
                'contentInfo' => new ContentInfo([
2461
                    'id' => $draftContent->contentInfo->id,
2462
                    'contentTypeId' => 28,
2463
                    'name' => 'An awesome forum',
2464
                    'sectionId' => 1,
2465
                    'currentVersionNo' => 1,
2466
                    'published' => true,
2467
                    'ownerId' => 14,
2468
                    // this Content Object is created at the test runtime
2469
                    'modificationDate' => $versionInfo->contentInfo->modificationDate,
2470
                    'publishedDate' => $versionInfo->contentInfo->publishedDate,
2471
                    'alwaysAvailable' => 1,
2472
                    'remoteId' => 'abcdef0123456789abcdef0123456789',
2473
                    'mainLanguageCode' => 'eng-US',
2474
                    'mainLocationId' => $draftContent->contentInfo->mainLocationId,
2475
                    'status' => ContentInfo::STATUS_PUBLISHED,
2476
                ]),
2477
                'id' => $draftContent->versionInfo->id,
2478
                'versionNo' => 2,
2479
                'creatorId' => 14,
2480
                'status' => 0,
2481
                'initialLanguageCode' => 'eng-US',
2482
                'languageCodes' => [
2483
                    'eng-US',
2484
                ],
2485
            ],
2486
            $versionInfo
2487
        );
2488
    }
2489
2490
    /**
2491
     * Test for the loadVersionInfoById() method.
2492
     *
2493
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2494
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2495
     */
2496 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter()
2497
    {
2498
        $repository = $this->getRepository();
2499
2500
        $contentService = $repository->getContentService();
2501
2502
        /* BEGIN: Use Case */
2503
        $content = $this->createContentVersion1();
2504
2505
        // This call will fail with a "NotFoundException", because not versionNo
2506
        // 2 exists for this content object.
2507
        $contentService->loadVersionInfoById($content->id, 2);
2508
        /* END: Use Case */
2509
    }
2510
2511
    /**
2512
     * Test for the loadContentByVersionInfo() method.
2513
     *
2514
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
2515
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2516
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
2517
     */
2518
    public function testLoadContentByVersionInfoWithSecondParameter()
2519
    {
2520
        $repository = $this->getRepository();
2521
2522
        $sectionId = $this->generateId('section', 1);
2523
        /* BEGIN: Use Case */
2524
        $contentTypeService = $repository->getContentTypeService();
2525
2526
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2527
2528
        $contentService = $repository->getContentService();
2529
2530
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2531
2532
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2533
2534
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2535
2536
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2537
        // $sectionId contains the ID of section 1
2538
        $contentCreateStruct->sectionId = $sectionId;
2539
        $contentCreateStruct->alwaysAvailable = true;
2540
2541
        // Create a new content draft
2542
        $content = $contentService->createContent($contentCreateStruct);
2543
2544
        // Now publish this draft
2545
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2546
2547
        // Will return a content instance with fields in "eng-US"
2548
        $reloadedContent = $contentService->loadContentByVersionInfo(
2549
            $publishedContent->getVersionInfo(),
2550
            array(
2551
                'eng-GB',
2552
            ),
2553
            false
2554
        );
2555
        /* END: Use Case */
2556
2557
        $actual = array();
2558 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...
2559
            $actual[] = new Field(
2560
                array(
2561
                    'id' => 0,
2562
                    'value' => ($field->value !== null ? true : null), // Actual value tested by FieldType integration tests
2563
                    'languageCode' => $field->languageCode,
2564
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
2565
                )
2566
            );
2567
        }
2568
        usort(
2569
            $actual,
2570 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...
2571
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
2572
                    return strcasecmp($field1->languageCode, $field2->languageCode);
2573
                }
2574
2575
                return $return;
2576
            }
2577
        );
2578
2579
        $expected = array(
2580
            new Field(
2581
                array(
2582
                    'id' => 0,
2583
                    'value' => true,
2584
                    'languageCode' => 'eng-GB',
2585
                    'fieldDefIdentifier' => 'description',
2586
                )
2587
            ),
2588
            new Field(
2589
                array(
2590
                    'id' => 0,
2591
                    'value' => true,
2592
                    'languageCode' => 'eng-GB',
2593
                    'fieldDefIdentifier' => 'name',
2594
                )
2595
            ),
2596
        );
2597
2598
        $this->assertEquals($expected, $actual);
2599
    }
2600
2601
    /**
2602
     * Test for the loadContentByContentInfo() method.
2603
     *
2604
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages)
2605
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2606
     */
2607
    public function testLoadContentByContentInfoWithLanguageParameters()
2608
    {
2609
        $repository = $this->getRepository();
2610
2611
        $sectionId = $this->generateId('section', 1);
2612
        /* BEGIN: Use Case */
2613
        $contentTypeService = $repository->getContentTypeService();
2614
2615
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2616
2617
        $contentService = $repository->getContentService();
2618
2619
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2620
2621
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2622
2623
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2624
2625
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2626
        // $sectionId contains the ID of section 1
2627
        $contentCreateStruct->sectionId = $sectionId;
2628
        $contentCreateStruct->alwaysAvailable = true;
2629
2630
        // Create a new content draft
2631
        $content = $contentService->createContent($contentCreateStruct);
2632
2633
        // Now publish this draft
2634
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2635
2636
        // Will return a content instance with fields in "eng-US"
2637
        $reloadedContent = $contentService->loadContentByContentInfo(
2638
            $publishedContent->contentInfo,
2639
            array(
2640
                'eng-US',
2641
            ),
2642
            null,
2643
            false
2644
        );
2645
        /* END: Use Case */
2646
2647
        $actual = $this->normalizeFields($reloadedContent->getFields());
2648
2649
        $expected = array(
2650
            new Field(
2651
                array(
2652
                    'id' => 0,
2653
                    'value' => true,
2654
                    'languageCode' => 'eng-US',
2655
                    'fieldDefIdentifier' => 'description',
2656
                    'fieldTypeIdentifier' => 'ezrichtext',
2657
                )
2658
            ),
2659
            new Field(
2660
                array(
2661
                    'id' => 0,
2662
                    'value' => true,
2663
                    'languageCode' => 'eng-US',
2664
                    'fieldDefIdentifier' => 'name',
2665
                    'fieldTypeIdentifier' => 'ezstring',
2666
                )
2667
            ),
2668
        );
2669
2670
        $this->assertEquals($expected, $actual);
2671
2672
        // Will return a content instance with fields in "eng-GB" (versions prior to 6.0.0-beta9 returned "eng-US" also)
2673
        $reloadedContent = $contentService->loadContentByContentInfo(
2674
            $publishedContent->contentInfo,
2675
            array(
2676
                'eng-GB',
2677
            ),
2678
            null,
2679
            true
2680
        );
2681
2682
        $actual = $this->normalizeFields($reloadedContent->getFields());
2683
2684
        $expected = array(
2685
            new Field(
2686
                array(
2687
                    'id' => 0,
2688
                    'value' => true,
2689
                    'languageCode' => 'eng-GB',
2690
                    'fieldDefIdentifier' => 'description',
2691
                    'fieldTypeIdentifier' => 'ezrichtext',
2692
                )
2693
            ),
2694
            new Field(
2695
                array(
2696
                    'id' => 0,
2697
                    'value' => true,
2698
                    'languageCode' => 'eng-GB',
2699
                    'fieldDefIdentifier' => 'name',
2700
                    'fieldTypeIdentifier' => 'ezstring',
2701
                )
2702
            ),
2703
        );
2704
2705
        $this->assertEquals($expected, $actual);
2706
2707
        // Will return a content instance with fields in main language "eng-US", as "fre-FR" does not exists
2708
        $reloadedContent = $contentService->loadContentByContentInfo(
2709
            $publishedContent->contentInfo,
2710
            array(
2711
                'fre-FR',
2712
            ),
2713
            null,
2714
            true
2715
        );
2716
2717
        $actual = $this->normalizeFields($reloadedContent->getFields());
2718
2719
        $expected = array(
2720
            new Field(
2721
                array(
2722
                    'id' => 0,
2723
                    'value' => true,
2724
                    'languageCode' => 'eng-US',
2725
                    'fieldDefIdentifier' => 'description',
2726
                    'fieldTypeIdentifier' => 'ezrichtext',
2727
                )
2728
            ),
2729
            new Field(
2730
                array(
2731
                    'id' => 0,
2732
                    'value' => true,
2733
                    'languageCode' => 'eng-US',
2734
                    'fieldDefIdentifier' => 'name',
2735
                    'fieldTypeIdentifier' => 'ezstring',
2736
                )
2737
            ),
2738
        );
2739
2740
        $this->assertEquals($expected, $actual);
2741
    }
2742
2743
    /**
2744
     * Test for the loadContentByContentInfo() method.
2745
     *
2746
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2747
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2748
     */
2749 View Code Duplication
    public function testLoadContentByContentInfoWithVersionNumberParameter()
2750
    {
2751
        $repository = $this->getRepository();
2752
2753
        $contentService = $repository->getContentService();
2754
2755
        /* BEGIN: Use Case */
2756
        $publishedContent = $this->createContentVersion1();
2757
2758
        $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...
2759
2760
        // This content instance is identical to $draftContent
2761
        $draftContentReloaded = $contentService->loadContentByContentInfo(
2762
            $publishedContent->contentInfo,
2763
            null,
2764
            2
2765
        );
2766
        /* END: Use Case */
2767
2768
        $this->assertEquals(
2769
            2,
2770
            $draftContentReloaded->getVersionInfo()->versionNo
2771
        );
2772
2773
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2774
        $this->assertEquals(
2775
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2776
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2777
        );
2778
    }
2779
2780
    /**
2781
     * Test for the loadContentByContentInfo() method.
2782
     *
2783
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2784
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2785
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithVersionNumberParameter
2786
     */
2787 View Code Duplication
    public function testLoadContentByContentInfoThrowsNotFoundExceptionWithVersionNumberParameter()
2788
    {
2789
        $repository = $this->getRepository();
2790
2791
        $contentService = $repository->getContentService();
2792
2793
        /* BEGIN: Use Case */
2794
        $content = $this->createContentVersion1();
2795
2796
        // This call will fail with a "NotFoundException", because no content
2797
        // with versionNo = 2 exists.
2798
        $contentService->loadContentByContentInfo($content->contentInfo, null, 2);
2799
        /* END: Use Case */
2800
    }
2801
2802
    /**
2803
     * Test for the loadContent() method.
2804
     *
2805
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages)
2806
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2807
     */
2808 View Code Duplication
    public function testLoadContentWithSecondParameter()
2809
    {
2810
        $repository = $this->getRepository();
2811
2812
        $contentService = $repository->getContentService();
2813
2814
        /* BEGIN: Use Case */
2815
        $draft = $this->createMultipleLanguageDraftVersion1();
2816
2817
        // This draft contains those fields localized with "eng-GB"
2818
        $draftLocalized = $contentService->loadContent($draft->id, array('eng-GB'), null, false);
2819
        /* END: Use Case */
2820
2821
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2822
2823
        return $draft;
2824
    }
2825
2826
    /**
2827
     * Test for the loadContent() method using undefined translation.
2828
     *
2829
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithSecondParameter
2830
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2831
     *
2832
     * @param \eZ\Publish\API\Repository\Values\Content\Content $contentDraft
2833
     */
2834
    public function testLoadContentWithSecondParameterThrowsNotFoundException(Content $contentDraft)
2835
    {
2836
        $repository = $this->getRepository();
2837
2838
        $contentService = $repository->getContentService();
2839
2840
        $contentService->loadContent($contentDraft->id, array('ger-DE'), null, false);
2841
    }
2842
2843
    /**
2844
     * Test for the loadContent() method.
2845
     *
2846
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2847
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2848
     */
2849 View Code Duplication
    public function testLoadContentWithThirdParameter()
2850
    {
2851
        $repository = $this->getRepository();
2852
2853
        $contentService = $repository->getContentService();
2854
2855
        /* BEGIN: Use Case */
2856
        $publishedContent = $this->createContentVersion1();
2857
2858
        $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...
2859
2860
        // This content instance is identical to $draftContent
2861
        $draftContentReloaded = $contentService->loadContent($publishedContent->id, null, 2);
2862
        /* END: Use Case */
2863
2864
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2865
2866
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2867
        $this->assertEquals(
2868
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2869
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2870
        );
2871
    }
2872
2873
    /**
2874
     * Test for the loadContent() method.
2875
     *
2876
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2877
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2878
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithThirdParameter
2879
     */
2880 View Code Duplication
    public function testLoadContentThrowsNotFoundExceptionWithThirdParameter()
2881
    {
2882
        $repository = $this->getRepository();
2883
2884
        $contentService = $repository->getContentService();
2885
2886
        /* BEGIN: Use Case */
2887
        $content = $this->createContentVersion1();
2888
2889
        // This call will fail with a "NotFoundException", because for this
2890
        // content object no versionNo=2 exists.
2891
        $contentService->loadContent($content->id, null, 2);
2892
        /* END: Use Case */
2893
    }
2894
2895
    /**
2896
     * Test for the loadContentByRemoteId() method.
2897
     *
2898
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages)
2899
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2900
     */
2901 View Code Duplication
    public function testLoadContentByRemoteIdWithSecondParameter()
2902
    {
2903
        $repository = $this->getRepository();
2904
2905
        $contentService = $repository->getContentService();
2906
2907
        /* BEGIN: Use Case */
2908
        $draft = $this->createMultipleLanguageDraftVersion1();
2909
2910
        $contentService->publishVersion($draft->versionInfo);
2911
2912
        // This draft contains those fields localized with "eng-GB"
2913
        $draftLocalized = $contentService->loadContentByRemoteId(
2914
            $draft->contentInfo->remoteId,
2915
            array('eng-GB'),
2916
            null,
2917
            false
2918
        );
2919
        /* END: Use Case */
2920
2921
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2922
    }
2923
2924
    /**
2925
     * Test for the loadContentByRemoteId() method.
2926
     *
2927
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2928
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2929
     */
2930 View Code Duplication
    public function testLoadContentByRemoteIdWithThirdParameter()
2931
    {
2932
        $repository = $this->getRepository();
2933
2934
        $contentService = $repository->getContentService();
2935
2936
        /* BEGIN: Use Case */
2937
        $publishedContent = $this->createContentVersion1();
2938
2939
        $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...
2940
2941
        // This content instance is identical to $draftContent
2942
        $draftContentReloaded = $contentService->loadContentByRemoteId(
2943
            $publishedContent->contentInfo->remoteId,
2944
            null,
2945
            2
2946
        );
2947
        /* END: Use Case */
2948
2949
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2950
2951
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2952
        $this->assertEquals(
2953
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2954
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2955
        );
2956
    }
2957
2958
    /**
2959
     * Test for the loadContentByRemoteId() method.
2960
     *
2961
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2962
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2963
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithThirdParameter
2964
     */
2965
    public function testLoadContentByRemoteIdThrowsNotFoundExceptionWithThirdParameter()
2966
    {
2967
        $repository = $this->getRepository();
2968
2969
        $contentService = $repository->getContentService();
2970
2971
        /* BEGIN: Use Case */
2972
        $content = $this->createContentVersion1();
2973
2974
        // This call will fail with a "NotFoundException", because for this
2975
        // content object no versionNo=2 exists.
2976
        $contentService->loadContentByRemoteId(
2977
            $content->contentInfo->remoteId,
2978
            null,
2979
            2
2980
        );
2981
        /* END: Use Case */
2982
    }
2983
2984
    /**
2985
     * Test that retrieval of translated name field respects prioritized language list.
2986
     *
2987
     * @dataProvider getPrioritizedLanguageList
2988
     * @param string[]|null $languageCodes
2989
     */
2990
    public function testLoadContentWithPrioritizedLanguagesList($languageCodes)
2991
    {
2992
        $repository = $this->getRepository();
2993
2994
        $contentService = $repository->getContentService();
2995
2996
        $content = $this->createContentVersion2();
2997
2998
        $content = $contentService->loadContent($content->id, $languageCodes);
2999
3000
        $expectedName = $content->getVersionInfo()->getName(
3001
            isset($languageCodes[0]) ? $languageCodes[0] : null
3002
        );
3003
        $nameValue = $content->getFieldValue('name');
3004
        /** @var \eZ\Publish\Core\FieldType\TextLine\Value $nameValue */
3005
        self::assertEquals($expectedName, $nameValue->text);
3006
        self::assertEquals($expectedName, $content->getVersionInfo()->getName());
3007
        // Also check value on shortcut method on content
3008
        self::assertEquals($expectedName, $content->getName());
3009
    }
3010
3011
    /**
3012
     * @return array
3013
     */
3014
    public function getPrioritizedLanguageList()
3015
    {
3016
        return [
3017
            [['eng-US']],
3018
            [['eng-GB']],
3019
            [['eng-GB', 'eng-US']],
3020
            [['eng-US', 'eng-GB']],
3021
        ];
3022
    }
3023
3024
    /**
3025
     * Test for the deleteVersion() method.
3026
     *
3027
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
3028
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3029
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3030
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3031
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
3032
     */
3033
    public function testDeleteVersion()
3034
    {
3035
        $repository = $this->getRepository();
3036
3037
        $contentService = $repository->getContentService();
3038
3039
        /* BEGIN: Use Case */
3040
        $content = $this->createContentVersion1();
3041
3042
        // Create new draft, because published or last version of the Content can't be deleted
3043
        $draft = $contentService->createContentDraft(
3044
            $content->getVersionInfo()->getContentInfo()
3045
        );
3046
3047
        // Delete the previously created draft
3048
        $contentService->deleteVersion($draft->getVersionInfo());
3049
        /* END: Use Case */
3050
3051
        $versions = $contentService->loadVersions($content->getVersionInfo()->getContentInfo());
3052
3053
        $this->assertCount(1, $versions);
3054
        $this->assertEquals(
3055
            $content->getVersionInfo()->id,
3056
            $versions[0]->id
3057
        );
3058
    }
3059
3060
    /**
3061
     * Test for the deleteVersion() method.
3062
     *
3063
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
3064
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3065
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3066
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3067
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3068
     */
3069
    public function testDeleteVersionThrowsBadStateExceptionOnPublishedVersion()
3070
    {
3071
        $repository = $this->getRepository();
3072
3073
        $contentService = $repository->getContentService();
3074
3075
        /* BEGIN: Use Case */
3076
        $content = $this->createContentVersion1();
3077
3078
        // This call will fail with a "BadStateException", because the content
3079
        // version is currently published.
3080
        $contentService->deleteVersion($content->getVersionInfo());
3081
        /* END: Use Case */
3082
    }
3083
3084
    /**
3085
     * Test for the deleteVersion() method.
3086
     *
3087
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
3088
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3089
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3090
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3091
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3092
     */
3093 View Code Duplication
    public function testDeleteVersionThrowsBadStateExceptionOnLastVersion()
3094
    {
3095
        $repository = $this->getRepository();
3096
3097
        $contentService = $repository->getContentService();
3098
3099
        /* BEGIN: Use Case */
3100
        $draft = $this->createContentDraftVersion1();
3101
3102
        // This call will fail with a "BadStateException", because the Content
3103
        // version is the last version of the Content.
3104
        $contentService->deleteVersion($draft->getVersionInfo());
3105
        /* END: Use Case */
3106
    }
3107
3108
    /**
3109
     * Test for the loadVersions() method.
3110
     *
3111
     * @see \eZ\Publish\API\Repository\ContentService::loadVersions()
3112
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3113
     *
3114
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[]
3115
     */
3116
    public function testLoadVersions()
3117
    {
3118
        $repository = $this->getRepository();
3119
3120
        $contentService = $repository->getContentService();
3121
3122
        /* BEGIN: Use Case */
3123
        $contentVersion2 = $this->createContentVersion2();
3124
3125
        // Load versions of this ContentInfo instance
3126
        $versions = $contentService->loadVersions($contentVersion2->contentInfo);
3127
        /* END: Use Case */
3128
3129
        $expectedVersionsOrder = [
3130
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1),
3131
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 2),
3132
        ];
3133
3134
        $this->assertEquals($expectedVersionsOrder, $versions);
3135
3136
        return $versions;
3137
    }
3138
3139
    /**
3140
     * Test for the loadVersions() method.
3141
     *
3142
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersions
3143
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersions
3144
     *
3145
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo[] $versions
3146
     */
3147
    public function testLoadVersionsSetsExpectedVersionInfo(array $versions)
3148
    {
3149
        $this->assertCount(2, $versions);
3150
3151
        $expectedVersions = [
3152
            [
3153
                'versionNo' => 1,
3154
                'creatorId' => 14,
3155
                'status' => VersionInfo::STATUS_ARCHIVED,
3156
                'initialLanguageCode' => 'eng-US',
3157
                'languageCodes' => ['eng-US'],
3158
            ],
3159
            [
3160
                'versionNo' => 2,
3161
                'creatorId' => 10,
3162
                'status' => VersionInfo::STATUS_PUBLISHED,
3163
                'initialLanguageCode' => 'eng-US',
3164
                'languageCodes' => ['eng-US', 'eng-GB'],
3165
            ],
3166
        ];
3167
3168
        $this->assertPropertiesCorrect($expectedVersions[0], $versions[0]);
3169
        $this->assertPropertiesCorrect($expectedVersions[1], $versions[1]);
3170
        $this->assertEquals(
3171
            $versions[0]->creationDate->getTimestamp(),
3172
            $versions[1]->creationDate->getTimestamp(),
3173
            'Creation time did not match within delta of 2 seconds',
3174
            2
3175
        );
3176
        $this->assertEquals(
3177
            $versions[0]->modificationDate->getTimestamp(),
3178
            $versions[1]->modificationDate->getTimestamp(),
3179
            'Creation time did not match within delta of 2 seconds',
3180
            2
3181
        );
3182
        $this->assertTrue($versions[0]->isArchived());
3183
        $this->assertFalse($versions[0]->isDraft());
3184
        $this->assertFalse($versions[0]->isPublished());
3185
3186
        $this->assertTrue($versions[1]->isPublished());
3187
        $this->assertFalse($versions[1]->isDraft());
3188
        $this->assertFalse($versions[1]->isArchived());
3189
    }
3190
3191
    /**
3192
     * Test for the copyContent() method.
3193
     *
3194
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
3195
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3196
     * @group field-type
3197
     */
3198 View Code Duplication
    public function testCopyContent()
3199
    {
3200
        $parentLocationId = $this->generateId('location', 56);
3201
3202
        $repository = $this->getRepository();
3203
3204
        $contentService = $repository->getContentService();
3205
        $locationService = $repository->getLocationService();
3206
3207
        /* BEGIN: Use Case */
3208
        $contentVersion2 = $this->createMultipleLanguageContentVersion2();
3209
3210
        // Configure new target location
3211
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
3212
3213
        $targetLocationCreate->priority = 42;
3214
        $targetLocationCreate->hidden = true;
3215
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3216
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3217
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3218
3219
        // Copy content with all versions and drafts
3220
        $contentCopied = $contentService->copyContent(
3221
            $contentVersion2->contentInfo,
3222
            $targetLocationCreate
3223
        );
3224
        /* END: Use Case */
3225
3226
        $this->assertInstanceOf(
3227
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
3228
            $contentCopied
3229
        );
3230
3231
        $this->assertNotEquals(
3232
            $contentVersion2->contentInfo->remoteId,
3233
            $contentCopied->contentInfo->remoteId
3234
        );
3235
3236
        $this->assertNotEquals(
3237
            $contentVersion2->id,
3238
            $contentCopied->id
3239
        );
3240
3241
        $this->assertEquals(
3242
            2,
3243
            count($contentService->loadVersions($contentCopied->contentInfo))
3244
        );
3245
3246
        $this->assertEquals(2, $contentCopied->getVersionInfo()->versionNo);
3247
3248
        $this->assertAllFieldsEquals($contentCopied->getFields());
3249
3250
        $this->assertDefaultContentStates($contentCopied->contentInfo);
3251
3252
        $this->assertNotNull(
3253
            $contentCopied->contentInfo->mainLocationId,
3254
            'Expected main location to be set given we provided a LocationCreateStruct'
3255
        );
3256
    }
3257
3258
    /**
3259
     * Test for the copyContent() method.
3260
     *
3261
     * @see \eZ\Publish\API\Repository\ContentService::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo)
3262
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
3263
     *
3264
     * @todo Fix to more descriptive name
3265
     */
3266 View Code Duplication
    public function testCopyContentWithThirdParameter()
3267
    {
3268
        $parentLocationId = $this->generateId('location', 56);
3269
3270
        $repository = $this->getRepository();
3271
3272
        $contentService = $repository->getContentService();
3273
        $locationService = $repository->getLocationService();
3274
3275
        /* BEGIN: Use Case */
3276
        $contentVersion2 = $this->createContentVersion2();
3277
3278
        // Configure new target location
3279
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
3280
3281
        $targetLocationCreate->priority = 42;
3282
        $targetLocationCreate->hidden = true;
3283
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3284
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3285
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3286
3287
        // Copy only the initial version
3288
        $contentCopied = $contentService->copyContent(
3289
            $contentVersion2->contentInfo,
3290
            $targetLocationCreate,
3291
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1)
3292
        );
3293
        /* END: Use Case */
3294
3295
        $this->assertInstanceOf(
3296
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
3297
            $contentCopied
3298
        );
3299
3300
        $this->assertNotEquals(
3301
            $contentVersion2->contentInfo->remoteId,
3302
            $contentCopied->contentInfo->remoteId
3303
        );
3304
3305
        $this->assertNotEquals(
3306
            $contentVersion2->id,
3307
            $contentCopied->id
3308
        );
3309
3310
        $this->assertEquals(
3311
            1,
3312
            count($contentService->loadVersions($contentCopied->contentInfo))
3313
        );
3314
3315
        $this->assertEquals(1, $contentCopied->getVersionInfo()->versionNo);
3316
3317
        $this->assertNotNull(
3318
            $contentCopied->contentInfo->mainLocationId,
3319
            'Expected main location to be set given we provided a LocationCreateStruct'
3320
        );
3321
    }
3322
3323
    /**
3324
     * Test for the addRelation() method.
3325
     *
3326
     * @return \eZ\Publish\API\Repository\Values\Content\Content
3327
     *
3328
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3329
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3330
     */
3331
    public function testAddRelation()
3332
    {
3333
        $repository = $this->getRepository();
3334
3335
        $contentService = $repository->getContentService();
3336
3337
        /* BEGIN: Use Case */
3338
        // RemoteId of the "Media" content of an eZ Publish demo installation
3339
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3340
3341
        $draft = $this->createContentDraftVersion1();
3342
3343
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3344
3345
        // Create relation between new content object and "Media" page
3346
        $relation = $contentService->addRelation(
3347
            $draft->getVersionInfo(),
3348
            $media
3349
        );
3350
        /* END: Use Case */
3351
3352
        $this->assertInstanceOf(
3353
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Relation',
3354
            $relation
3355
        );
3356
3357
        return $contentService->loadRelations($draft->getVersionInfo());
3358
    }
3359
3360
    /**
3361
     * Test for the addRelation() method.
3362
     *
3363
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3364
     *
3365
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3366
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3367
     */
3368
    public function testAddRelationAddsRelationToContent($relations)
3369
    {
3370
        $this->assertEquals(
3371
            1,
3372
            count($relations)
3373
        );
3374
    }
3375
3376
    /**
3377
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3378
     */
3379
    protected function assertExpectedRelations($relations)
3380
    {
3381
        $this->assertEquals(
3382
            array(
3383
                'type' => Relation::COMMON,
3384
                'sourceFieldDefinitionIdentifier' => null,
3385
                'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3386
                'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3387
            ),
3388
            array(
3389
                'type' => $relations[0]->type,
3390
                'sourceFieldDefinitionIdentifier' => $relations[0]->sourceFieldDefinitionIdentifier,
3391
                'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3392
                'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3393
            )
3394
        );
3395
    }
3396
3397
    /**
3398
     * Test for the addRelation() method.
3399
     *
3400
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3401
     *
3402
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3403
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3404
     */
3405
    public function testAddRelationSetsExpectedRelations($relations)
3406
    {
3407
        $this->assertExpectedRelations($relations);
3408
    }
3409
3410
    /**
3411
     * Test for the createContentDraft() method.
3412
     *
3413
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3414
     *
3415
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
3416
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelationSetsExpectedRelations
3417
     */
3418 View Code Duplication
    public function testCreateContentDraftWithRelations()
3419
    {
3420
        $repository = $this->getRepository();
3421
3422
        $contentService = $repository->getContentService();
3423
3424
        // RemoteId of the "Media" content of an eZ Publish demo installation
3425
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3426
        $draft = $this->createContentDraftVersion1();
3427
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3428
3429
        // Create relation between new content object and "Media" page
3430
        $contentService->addRelation(
3431
            $draft->getVersionInfo(),
3432
            $media
3433
        );
3434
3435
        $content = $contentService->publishVersion($draft->versionInfo);
3436
        $newDraft = $contentService->createContentDraft($content->contentInfo);
3437
3438
        return $contentService->loadRelations($newDraft->getVersionInfo());
3439
    }
3440
3441
    /**
3442
     * Test for the createContentDraft() method.
3443
     *
3444
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3445
     *
3446
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3447
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelations
3448
     */
3449
    public function testCreateContentDraftWithRelationsCreatesRelations($relations)
3450
    {
3451
        $this->assertEquals(
3452
            1,
3453
            count($relations)
3454
        );
3455
3456
        return $relations;
3457
    }
3458
3459
    /**
3460
     * Test for the createContentDraft() method.
3461
     *
3462
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3463
     *
3464
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelationsCreatesRelations
3465
     */
3466
    public function testCreateContentDraftWithRelationsCreatesExpectedRelations($relations)
3467
    {
3468
        $this->assertExpectedRelations($relations);
3469
    }
3470
3471
    /**
3472
     * Test for the addRelation() method.
3473
     *
3474
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3475
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3476
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3477
     */
3478 View Code Duplication
    public function testAddRelationThrowsBadStateException()
3479
    {
3480
        $repository = $this->getRepository();
3481
3482
        $contentService = $repository->getContentService();
3483
3484
        /* BEGIN: Use Case */
3485
        // RemoteId of the "Media" page of an eZ Publish demo installation
3486
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3487
3488
        $content = $this->createContentVersion1();
3489
3490
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3491
3492
        // This call will fail with a "BadStateException", because content is
3493
        // published and not a draft.
3494
        $contentService->addRelation(
3495
            $content->getVersionInfo(),
3496
            $media
3497
        );
3498
        /* END: Use Case */
3499
    }
3500
3501
    /**
3502
     * Test for the loadRelations() method.
3503
     *
3504
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3505
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3506
     */
3507
    public function testLoadRelations()
3508
    {
3509
        $repository = $this->getRepository();
3510
3511
        $contentService = $repository->getContentService();
3512
3513
        /* BEGIN: Use Case */
3514
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3515
        // of a eZ Publish demo installation.
3516
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3517
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3518
3519
        $draft = $this->createContentDraftVersion1();
3520
3521
        // Load other content objects
3522
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3523
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3524
3525
        // Create relation between new content object and "Media" page
3526
        $contentService->addRelation(
3527
            $draft->getVersionInfo(),
3528
            $media
3529
        );
3530
3531
        // Create another relation with the "Demo Design" page
3532
        $contentService->addRelation(
3533
            $draft->getVersionInfo(),
3534
            $demoDesign
3535
        );
3536
3537
        // Load all relations
3538
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3539
        /* END: Use Case */
3540
3541
        usort(
3542
            $relations,
3543
            function ($rel1, $rel2) {
3544
                return strcasecmp(
3545
                    $rel2->getDestinationContentInfo()->remoteId,
3546
                    $rel1->getDestinationContentInfo()->remoteId
3547
                );
3548
            }
3549
        );
3550
3551
        $this->assertEquals(
3552
            array(
3553
                array(
3554
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3555
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3556
                ),
3557
                array(
3558
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3559
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3560
                ),
3561
            ),
3562
            array(
3563
                array(
3564
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3565
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3566
                ),
3567
                array(
3568
                    'sourceContentInfo' => $relations[1]->sourceContentInfo->remoteId,
3569
                    'destinationContentInfo' => $relations[1]->destinationContentInfo->remoteId,
3570
                ),
3571
            )
3572
        );
3573
    }
3574
3575
    /**
3576
     * Test for the loadRelations() method.
3577
     *
3578
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3579
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3580
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3581
     */
3582
    public function testLoadRelationsSkipsArchivedContent()
3583
    {
3584
        $repository = $this->getRepository();
3585
3586
        $contentService = $repository->getContentService();
3587
3588
        /* BEGIN: Use Case */
3589
        $trashService = $repository->getTrashService();
3590
        $locationService = $repository->getLocationService();
3591
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3592
        // of a eZ Publish demo installation.
3593
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3594
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3595
3596
        $draft = $this->createContentDraftVersion1();
3597
3598
        // Load other content objects
3599
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3600
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3601
3602
        // Create relation between new content object and "Media" page
3603
        $contentService->addRelation(
3604
            $draft->getVersionInfo(),
3605
            $media
3606
        );
3607
3608
        // Create another relation with the "Demo Design" page
3609
        $contentService->addRelation(
3610
            $draft->getVersionInfo(),
3611
            $demoDesign
3612
        );
3613
3614
        $demoDesignLocation = $locationService->loadLocation($demoDesign->mainLocationId);
3615
3616
        // Trashing Content's last Location will change its status to archived,
3617
        // in this case relation towards it will not be loaded.
3618
        $trashService->trash($demoDesignLocation);
3619
3620
        // Load all relations
3621
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3622
        /* END: Use Case */
3623
3624
        $this->assertCount(1, $relations);
3625
        $this->assertEquals(
3626
            array(
3627
                array(
3628
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3629
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3630
                ),
3631
            ),
3632
            array(
3633
                array(
3634
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3635
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3636
                ),
3637
            )
3638
        );
3639
    }
3640
3641
    /**
3642
     * Test for the loadRelations() method.
3643
     *
3644
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3645
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3646
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3647
     */
3648
    public function testLoadRelationsSkipsDraftContent()
3649
    {
3650
        $repository = $this->getRepository();
3651
3652
        $contentService = $repository->getContentService();
3653
3654
        /* BEGIN: Use Case */
3655
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3656
        // of a eZ Publish demo installation.
3657
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3658
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3659
3660
        $draft = $this->createContentDraftVersion1();
3661
3662
        // Load other content objects
3663
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3664
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3665
3666
        // Create draft of "Media" page
3667
        $mediaDraft = $contentService->createContentDraft($media->contentInfo);
3668
3669
        // Create relation between "Media" page and new content object draft.
3670
        // This relation will not be loaded before the draft is published.
3671
        $contentService->addRelation(
3672
            $mediaDraft->getVersionInfo(),
3673
            $draft->getVersionInfo()->getContentInfo()
3674
        );
3675
3676
        // Create another relation with the "Demo Design" page
3677
        $contentService->addRelation(
3678
            $mediaDraft->getVersionInfo(),
3679
            $demoDesign
3680
        );
3681
3682
        // Load all relations
3683
        $relations = $contentService->loadRelations($mediaDraft->getVersionInfo());
3684
        /* END: Use Case */
3685
3686
        $this->assertCount(1, $relations);
3687
        $this->assertEquals(
3688
            array(
3689
                array(
3690
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3691
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3692
                ),
3693
            ),
3694
            array(
3695
                array(
3696
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3697
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3698
                ),
3699
            )
3700
        );
3701
    }
3702
3703
    /**
3704
     * Test for the loadReverseRelations() method.
3705
     *
3706
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3707
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3708
     */
3709
    public function testLoadReverseRelations()
3710
    {
3711
        $repository = $this->getRepository();
3712
3713
        $contentService = $repository->getContentService();
3714
3715
        /* BEGIN: Use Case */
3716
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3717
        // of a eZ Publish demo installation.
3718
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3719
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3720
3721
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3722
        $contentInfo = $versionInfo->getContentInfo();
3723
3724
        // Create some drafts
3725
        $mediaDraft = $contentService->createContentDraft(
3726
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3727
        );
3728
        $demoDesignDraft = $contentService->createContentDraft(
3729
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3730
        );
3731
3732
        // Create relation between new content object and "Media" page
3733
        $relation1 = $contentService->addRelation(
3734
            $mediaDraft->getVersionInfo(),
3735
            $contentInfo
3736
        );
3737
3738
        // Create another relation with the "Demo Design" page
3739
        $relation2 = $contentService->addRelation(
3740
            $demoDesignDraft->getVersionInfo(),
3741
            $contentInfo
3742
        );
3743
3744
        // Publish drafts, so relations become active
3745
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3746
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3747
3748
        // Load all relations
3749
        $relations = $contentService->loadRelations($versionInfo);
3750
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3751
        /* END: Use Case */
3752
3753
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3754
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3755
3756
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3757
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3758
3759
        $this->assertEquals(0, count($relations));
3760
        $this->assertEquals(2, count($reverseRelations));
3761
3762
        usort(
3763
            $reverseRelations,
3764
            function ($rel1, $rel2) {
3765
                return strcasecmp(
3766
                    $rel2->getSourceContentInfo()->remoteId,
3767
                    $rel1->getSourceContentInfo()->remoteId
3768
                );
3769
            }
3770
        );
3771
3772
        $this->assertEquals(
3773
            array(
3774
                array(
3775
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3776
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3777
                ),
3778
                array(
3779
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3780
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3781
                ),
3782
            ),
3783
            array(
3784
                array(
3785
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3786
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3787
                ),
3788
                array(
3789
                    'sourceContentInfo' => $reverseRelations[1]->sourceContentInfo->remoteId,
3790
                    'destinationContentInfo' => $reverseRelations[1]->destinationContentInfo->remoteId,
3791
                ),
3792
            )
3793
        );
3794
    }
3795
3796
    /**
3797
     * Test for the loadReverseRelations() method.
3798
     *
3799
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3800
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3801
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3802
     */
3803
    public function testLoadReverseRelationsSkipsArchivedContent()
3804
    {
3805
        $repository = $this->getRepository();
3806
3807
        $contentService = $repository->getContentService();
3808
3809
        /* BEGIN: Use Case */
3810
        $trashService = $repository->getTrashService();
3811
        $locationService = $repository->getLocationService();
3812
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3813
        // of a eZ Publish demo installation.
3814
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3815
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3816
3817
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3818
        $contentInfo = $versionInfo->getContentInfo();
3819
3820
        // Create some drafts
3821
        $mediaDraft = $contentService->createContentDraft(
3822
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3823
        );
3824
        $demoDesignDraft = $contentService->createContentDraft(
3825
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3826
        );
3827
3828
        // Create relation between new content object and "Media" page
3829
        $relation1 = $contentService->addRelation(
3830
            $mediaDraft->getVersionInfo(),
3831
            $contentInfo
3832
        );
3833
3834
        // Create another relation with the "Demo Design" page
3835
        $relation2 = $contentService->addRelation(
3836
            $demoDesignDraft->getVersionInfo(),
3837
            $contentInfo
3838
        );
3839
3840
        // Publish drafts, so relations become active
3841
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3842
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3843
3844
        $demoDesignLocation = $locationService->loadLocation($demoDesignDraft->contentInfo->mainLocationId);
3845
3846
        // Trashing Content's last Location will change its status to archived,
3847
        // in this case relation from it will not be loaded.
3848
        $trashService->trash($demoDesignLocation);
3849
3850
        // Load all relations
3851
        $relations = $contentService->loadRelations($versionInfo);
3852
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3853
        /* END: Use Case */
3854
3855
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3856
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3857
3858
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3859
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3860
3861
        $this->assertEquals(0, count($relations));
3862
        $this->assertEquals(1, count($reverseRelations));
3863
3864
        $this->assertEquals(
3865
            array(
3866
                array(
3867
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3868
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3869
                ),
3870
            ),
3871
            array(
3872
                array(
3873
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3874
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3875
                ),
3876
            )
3877
        );
3878
    }
3879
3880
    /**
3881
     * Test for the loadReverseRelations() method.
3882
     *
3883
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3884
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3885
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3886
     */
3887
    public function testLoadReverseRelationsSkipsDraftContent()
3888
    {
3889
        $repository = $this->getRepository();
3890
3891
        $contentService = $repository->getContentService();
3892
3893
        /* BEGIN: Use Case */
3894
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3895
        // of a eZ Publish demo installation.
3896
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3897
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3898
3899
        // Load "Media" page Content
3900
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3901
3902
        // Create some drafts
3903
        $newDraftVersionInfo = $this->createContentDraftVersion1()->getVersionInfo();
3904
        $demoDesignDraft = $contentService->createContentDraft(
3905
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3906
        );
3907
3908
        // Create relation between "Media" page and new content object
3909
        $relation1 = $contentService->addRelation(
3910
            $newDraftVersionInfo,
3911
            $media->contentInfo
3912
        );
3913
3914
        // Create another relation with the "Demo Design" page
3915
        $relation2 = $contentService->addRelation(
3916
            $demoDesignDraft->getVersionInfo(),
3917
            $media->contentInfo
3918
        );
3919
3920
        // Publish drafts, so relations become active
3921
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3922
        // We will not publish new Content draft, therefore relation from it
3923
        // will not be loaded as reverse relation for "Media" page
3924
        //$contentService->publishVersion( $newDraftVersionInfo );
3925
3926
        // Load all relations
3927
        $relations = $contentService->loadRelations($media->versionInfo);
3928
        $reverseRelations = $contentService->loadReverseRelations($media->contentInfo);
3929
        /* END: Use Case */
3930
3931
        $this->assertEquals($media->contentInfo->id, $relation1->getDestinationContentInfo()->id);
3932
        $this->assertEquals($newDraftVersionInfo->contentInfo->id, $relation1->getSourceContentInfo()->id);
3933
3934
        $this->assertEquals($media->contentInfo->id, $relation2->getDestinationContentInfo()->id);
3935
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3936
3937
        $this->assertEquals(0, count($relations));
3938
        $this->assertEquals(1, count($reverseRelations));
3939
3940
        $this->assertEquals(
3941
            array(
3942
                array(
3943
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3944
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3945
                ),
3946
            ),
3947
            array(
3948
                array(
3949
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3950
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3951
                ),
3952
            )
3953
        );
3954
    }
3955
3956
    /**
3957
     * Test for the deleteRelation() method.
3958
     *
3959
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3960
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3961
     */
3962
    public function testDeleteRelation()
3963
    {
3964
        $repository = $this->getRepository();
3965
3966
        $contentService = $repository->getContentService();
3967
3968
        /* BEGIN: Use Case */
3969
        // Remote ids of the "Media" and the "Demo Design" page of a eZ Publish
3970
        // demo installation.
3971
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3972
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3973
3974
        $draft = $this->createContentDraftVersion1();
3975
3976
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3977
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3978
3979
        // Establish some relations
3980
        $contentService->addRelation($draft->getVersionInfo(), $media);
3981
        $contentService->addRelation($draft->getVersionInfo(), $demoDesign);
3982
3983
        // Delete one of the currently created relations
3984
        $contentService->deleteRelation($draft->getVersionInfo(), $media);
3985
3986
        // The relations array now contains only one element
3987
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3988
        /* END: Use Case */
3989
3990
        $this->assertEquals(1, count($relations));
3991
    }
3992
3993
    /**
3994
     * Test for the deleteRelation() method.
3995
     *
3996
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3997
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3998
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3999
     */
4000 View Code Duplication
    public function testDeleteRelationThrowsBadStateException()
4001
    {
4002
        $repository = $this->getRepository();
4003
4004
        $contentService = $repository->getContentService();
4005
4006
        /* BEGIN: Use Case */
4007
        // RemoteId of the "Media" page of an eZ Publish demo installation
4008
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
4009
4010
        $content = $this->createContentVersion1();
4011
4012
        // Load the destination object
4013
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
4014
4015
        // Create a new draft
4016
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
4017
4018
        // Add a relation
4019
        $contentService->addRelation($draftVersion2->getVersionInfo(), $media);
4020
4021
        // Publish new version
4022
        $contentVersion2 = $contentService->publishVersion(
4023
            $draftVersion2->getVersionInfo()
4024
        );
4025
4026
        // This call will fail with a "BadStateException", because content is
4027
        // published and not a draft.
4028
        $contentService->deleteRelation(
4029
            $contentVersion2->getVersionInfo(),
4030
            $media
4031
        );
4032
        /* END: Use Case */
4033
    }
4034
4035
    /**
4036
     * Test for the deleteRelation() method.
4037
     *
4038
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
4039
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
4040
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
4041
     */
4042 View Code Duplication
    public function testDeleteRelationThrowsInvalidArgumentException()
4043
    {
4044
        $repository = $this->getRepository();
4045
4046
        $contentService = $repository->getContentService();
4047
4048
        /* BEGIN: Use Case */
4049
        // RemoteId of the "Media" page of an eZ Publish demo installation
4050
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
4051
4052
        $draft = $this->createContentDraftVersion1();
4053
4054
        // Load the destination object
4055
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
4056
4057
        // This call will fail with a "InvalidArgumentException", because no
4058
        // relation exists between $draft and $media.
4059
        $contentService->deleteRelation(
4060
            $draft->getVersionInfo(),
4061
            $media
4062
        );
4063
        /* END: Use Case */
4064
    }
4065
4066
    /**
4067
     * Test for the createContent() method.
4068
     *
4069
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
4070
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4071
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4072
     */
4073
    public function testCreateContentInTransactionWithRollback()
4074
    {
4075
        if ($this->isVersion4()) {
4076
            $this->markTestSkipped('This test requires eZ Publish 5');
4077
        }
4078
4079
        $repository = $this->getRepository();
4080
4081
        /* BEGIN: Use Case */
4082
        $contentTypeService = $repository->getContentTypeService();
4083
        $contentService = $repository->getContentService();
4084
4085
        // Start a transaction
4086
        $repository->beginTransaction();
4087
4088
        try {
4089
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
4090
4091
            // Get a content create struct and set mandatory properties
4092
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
4093
            $contentCreate->setField('name', 'Sindelfingen forum');
4094
4095
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
4096
            $contentCreate->alwaysAvailable = true;
4097
4098
            // Create a new content object
4099
            $contentId = $contentService->createContent($contentCreate)->id;
4100
        } catch (Exception $e) {
4101
            // Cleanup hanging transaction on error
4102
            $repository->rollback();
4103
            throw $e;
4104
        }
4105
4106
        // Rollback all changes
4107
        $repository->rollback();
4108
4109
        try {
4110
            // This call will fail with a "NotFoundException"
4111
            $contentService->loadContent($contentId);
4112
        } catch (NotFoundException $e) {
4113
            // This is expected
4114
            return;
4115
        }
4116
        /* END: Use Case */
4117
4118
        $this->fail('Content object still exists after rollback.');
4119
    }
4120
4121
    /**
4122
     * Test for the createContent() method.
4123
     *
4124
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
4125
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4126
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4127
     */
4128
    public function testCreateContentInTransactionWithCommit()
4129
    {
4130
        if ($this->isVersion4()) {
4131
            $this->markTestSkipped('This test requires eZ Publish 5');
4132
        }
4133
4134
        $repository = $this->getRepository();
4135
4136
        /* BEGIN: Use Case */
4137
        $contentTypeService = $repository->getContentTypeService();
4138
        $contentService = $repository->getContentService();
4139
4140
        // Start a transaction
4141
        $repository->beginTransaction();
4142
4143
        try {
4144
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
4145
4146
            // Get a content create struct and set mandatory properties
4147
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
4148
            $contentCreate->setField('name', 'Sindelfingen forum');
4149
4150
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
4151
            $contentCreate->alwaysAvailable = true;
4152
4153
            // Create a new content object
4154
            $contentId = $contentService->createContent($contentCreate)->id;
4155
4156
            // Commit changes
4157
            $repository->commit();
4158
        } catch (Exception $e) {
4159
            // Cleanup hanging transaction on error
4160
            $repository->rollback();
4161
            throw $e;
4162
        }
4163
4164
        // Load the new content object
4165
        $content = $contentService->loadContent($contentId);
4166
        /* END: Use Case */
4167
4168
        $this->assertEquals($contentId, $content->id);
4169
    }
4170
4171
    /**
4172
     * Test for the createContent() method.
4173
     *
4174
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
4175
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
4176
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
4177
     */
4178
    public function testCreateContentWithLocationCreateParameterInTransactionWithRollback()
4179
    {
4180
        $repository = $this->getRepository();
4181
4182
        $contentService = $repository->getContentService();
4183
4184
        /* BEGIN: Use Case */
4185
        // Start a transaction
4186
        $repository->beginTransaction();
4187
4188
        try {
4189
            $draft = $this->createContentDraftVersion1();
4190
        } catch (Exception $e) {
4191
            // Cleanup hanging transaction on error
4192
            $repository->rollback();
4193
            throw $e;
4194
        }
4195
4196
        $contentId = $draft->id;
4197
4198
        // Roleback the transaction
4199
        $repository->rollback();
4200
4201
        try {
4202
            // This call will fail with a "NotFoundException"
4203
            $contentService->loadContent($contentId);
4204
        } catch (NotFoundException $e) {
4205
            return;
4206
        }
4207
        /* END: Use Case */
4208
4209
        $this->fail('Can still load content object after rollback.');
4210
    }
4211
4212
    /**
4213
     * Test for the createContent() method.
4214
     *
4215
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
4216
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
4217
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
4218
     */
4219 View Code Duplication
    public function testCreateContentWithLocationCreateParameterInTransactionWithCommit()
4220
    {
4221
        $repository = $this->getRepository();
4222
4223
        $contentService = $repository->getContentService();
4224
4225
        /* BEGIN: Use Case */
4226
        // Start a transaction
4227
        $repository->beginTransaction();
4228
4229
        try {
4230
            $draft = $this->createContentDraftVersion1();
4231
4232
            $contentId = $draft->id;
4233
4234
            // Roleback the transaction
4235
            $repository->commit();
4236
        } catch (Exception $e) {
4237
            // Cleanup hanging transaction on error
4238
            $repository->rollback();
4239
            throw $e;
4240
        }
4241
4242
        // Load the new content object
4243
        $content = $contentService->loadContent($contentId);
4244
        /* END: Use Case */
4245
4246
        $this->assertEquals($contentId, $content->id);
4247
    }
4248
4249
    /**
4250
     * Test for the createContentDraft() method.
4251
     *
4252
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4253
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4254
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4255
     */
4256
    public function testCreateContentDraftInTransactionWithRollback()
4257
    {
4258
        $repository = $this->getRepository();
4259
4260
        $contentId = $this->generateId('object', 12);
4261
        /* BEGIN: Use Case */
4262
        // $contentId is the ID of the "Administrator users" user group
4263
4264
        // Get the content service
4265
        $contentService = $repository->getContentService();
4266
4267
        // Load the user group content object
4268
        $content = $contentService->loadContent($contentId);
4269
4270
        // Start a new transaction
4271
        $repository->beginTransaction();
4272
4273
        try {
4274
            // Create a new draft
4275
            $drafted = $contentService->createContentDraft($content->contentInfo);
4276
4277
            // Store version number for later reuse
4278
            $versionNo = $drafted->versionInfo->versionNo;
4279
        } catch (Exception $e) {
4280
            // Cleanup hanging transaction on error
4281
            $repository->rollback();
4282
            throw $e;
4283
        }
4284
4285
        // Rollback
4286
        $repository->rollback();
4287
4288
        try {
4289
            // This call will fail with a "NotFoundException"
4290
            $contentService->loadContent($contentId, null, $versionNo);
4291
        } catch (NotFoundException $e) {
4292
            return;
4293
        }
4294
        /* END: Use Case */
4295
4296
        $this->fail('Can still load content draft after rollback');
4297
    }
4298
4299
    /**
4300
     * Test for the createContentDraft() method.
4301
     *
4302
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4303
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4304
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4305
     */
4306 View Code Duplication
    public function testCreateContentDraftInTransactionWithCommit()
4307
    {
4308
        $repository = $this->getRepository();
4309
4310
        $contentId = $this->generateId('object', 12);
4311
        /* BEGIN: Use Case */
4312
        // $contentId is the ID of the "Administrator users" user group
4313
4314
        // Get the content service
4315
        $contentService = $repository->getContentService();
4316
4317
        // Load the user group content object
4318
        $content = $contentService->loadContent($contentId);
4319
4320
        // Start a new transaction
4321
        $repository->beginTransaction();
4322
4323
        try {
4324
            // Create a new draft
4325
            $drafted = $contentService->createContentDraft($content->contentInfo);
4326
4327
            // Store version number for later reuse
4328
            $versionNo = $drafted->versionInfo->versionNo;
4329
4330
            // Commit all changes
4331
            $repository->commit();
4332
        } catch (Exception $e) {
4333
            // Cleanup hanging transaction on error
4334
            $repository->rollback();
4335
            throw $e;
4336
        }
4337
4338
        $content = $contentService->loadContent($contentId, null, $versionNo);
4339
        /* END: Use Case */
4340
4341
        $this->assertEquals(
4342
            $versionNo,
4343
            $content->getVersionInfo()->versionNo
4344
        );
4345
    }
4346
4347
    /**
4348
     * Test for the publishVersion() method.
4349
     *
4350
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4351
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4352
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4353
     */
4354 View Code Duplication
    public function testPublishVersionInTransactionWithRollback()
4355
    {
4356
        $repository = $this->getRepository();
4357
4358
        $contentId = $this->generateId('object', 12);
4359
        /* BEGIN: Use Case */
4360
        // $contentId is the ID of the "Administrator users" user group
4361
4362
        // Get the content service
4363
        $contentService = $repository->getContentService();
4364
4365
        // Load the user group content object
4366
        $content = $contentService->loadContent($contentId);
4367
4368
        // Start a new transaction
4369
        $repository->beginTransaction();
4370
4371
        try {
4372
            $draftVersion = $contentService->createContentDraft($content->contentInfo)->getVersionInfo();
4373
4374
            // Publish a new version
4375
            $content = $contentService->publishVersion($draftVersion);
4376
4377
            // Store version number for later reuse
4378
            $versionNo = $content->versionInfo->versionNo;
4379
        } catch (Exception $e) {
4380
            // Cleanup hanging transaction on error
4381
            $repository->rollback();
4382
            throw $e;
4383
        }
4384
4385
        // Rollback
4386
        $repository->rollback();
4387
4388
        try {
4389
            // This call will fail with a "NotFoundException"
4390
            $contentService->loadContent($contentId, null, $versionNo);
4391
        } catch (NotFoundException $e) {
4392
            return;
4393
        }
4394
        /* END: Use Case */
4395
4396
        $this->fail('Can still load content draft after rollback');
4397
    }
4398
4399
    /**
4400
     * Test for the publishVersion() method.
4401
     *
4402
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4403
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4404
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
4405
     */
4406 View Code Duplication
    public function testPublishVersionInTransactionWithCommit()
4407
    {
4408
        $repository = $this->getRepository();
4409
4410
        /* BEGIN: Use Case */
4411
        // ID of the "Administrator users" user group
4412
        $contentId = 12;
4413
4414
        // Get the content service
4415
        $contentService = $repository->getContentService();
4416
4417
        // Load the user group content object
4418
        $template = $contentService->loadContent($contentId);
4419
4420
        // Start a new transaction
4421
        $repository->beginTransaction();
4422
4423
        try {
4424
            // Publish a new version
4425
            $content = $contentService->publishVersion(
4426
                $contentService->createContentDraft($template->contentInfo)->getVersionInfo()
4427
            );
4428
4429
            // Store version number for later reuse
4430
            $versionNo = $content->versionInfo->versionNo;
4431
4432
            // Commit all changes
4433
            $repository->commit();
4434
        } catch (Exception $e) {
4435
            // Cleanup hanging transaction on error
4436
            $repository->rollback();
4437
            throw $e;
4438
        }
4439
4440
        // Load current version info
4441
        $versionInfo = $contentService->loadVersionInfo($content->contentInfo);
4442
        /* END: Use Case */
4443
4444
        $this->assertEquals($versionNo, $versionInfo->versionNo);
4445
    }
4446
4447
    /**
4448
     * Test for the updateContent() method.
4449
     *
4450
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4451
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4452
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4453
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4454
     */
4455 View Code Duplication
    public function testUpdateContentInTransactionWithRollback()
4456
    {
4457
        $repository = $this->getRepository();
4458
4459
        $contentId = $this->generateId('object', 12);
4460
        /* BEGIN: Use Case */
4461
        // $contentId is the ID of the "Administrator users" user group
4462
4463
        // Load content service
4464
        $contentService = $repository->getContentService();
4465
4466
        // Create a new user group draft
4467
        $draft = $contentService->createContentDraft(
4468
            $contentService->loadContentInfo($contentId)
4469
        );
4470
4471
        // Get an update struct and change the group name
4472
        $contentUpdate = $contentService->newContentUpdateStruct();
4473
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4474
4475
        // Start a transaction
4476
        $repository->beginTransaction();
4477
4478
        try {
4479
            // Update the group name
4480
            $draft = $contentService->updateContent(
4481
                $draft->getVersionInfo(),
4482
                $contentUpdate
4483
            );
4484
4485
            // Publish updated version
4486
            $contentService->publishVersion($draft->getVersionInfo());
4487
        } catch (Exception $e) {
4488
            // Cleanup hanging transaction on error
4489
            $repository->rollback();
4490
            throw $e;
4491
        }
4492
4493
        // Rollback all changes.
4494
        $repository->rollback();
4495
4496
        // Name will still be "Administrator users"
4497
        $name = $contentService->loadContent($contentId)->getFieldValue('name');
4498
        /* END: Use Case */
4499
4500
        $this->assertEquals('Administrator users', $name);
4501
    }
4502
4503
    /**
4504
     * Test for the updateContent() method.
4505
     *
4506
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4507
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4508
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4509
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4510
     */
4511 View Code Duplication
    public function testUpdateContentInTransactionWithCommit()
4512
    {
4513
        $repository = $this->getRepository();
4514
4515
        $contentId = $this->generateId('object', 12);
4516
        /* BEGIN: Use Case */
4517
        // $contentId is the ID of the "Administrator users" user group
4518
4519
        // Load content service
4520
        $contentService = $repository->getContentService();
4521
4522
        // Create a new user group draft
4523
        $draft = $contentService->createContentDraft(
4524
            $contentService->loadContentInfo($contentId)
4525
        );
4526
4527
        // Get an update struct and change the group name
4528
        $contentUpdate = $contentService->newContentUpdateStruct();
4529
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4530
4531
        // Start a transaction
4532
        $repository->beginTransaction();
4533
4534
        try {
4535
            // Update the group name
4536
            $draft = $contentService->updateContent(
4537
                $draft->getVersionInfo(),
4538
                $contentUpdate
4539
            );
4540
4541
            // Publish updated version
4542
            $contentService->publishVersion($draft->getVersionInfo());
4543
4544
            // Commit all changes.
4545
            $repository->commit();
4546
        } catch (Exception $e) {
4547
            // Cleanup hanging transaction on error
4548
            $repository->rollback();
4549
            throw $e;
4550
        }
4551
4552
        // Name is now "Administrators"
4553
        $name = $contentService->loadContent($contentId)->getFieldValue('name', 'eng-US');
4554
        /* END: Use Case */
4555
4556
        $this->assertEquals('Administrators', $name);
4557
    }
4558
4559
    /**
4560
     * Test for the updateContentMetadata() method.
4561
     *
4562
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4563
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4564
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4565
     */
4566 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithRollback()
4567
    {
4568
        $repository = $this->getRepository();
4569
4570
        $contentId = $this->generateId('object', 12);
4571
        /* BEGIN: Use Case */
4572
        // $contentId is the ID of the "Administrator users" user group
4573
4574
        // Get the content service
4575
        $contentService = $repository->getContentService();
4576
4577
        // Load a ContentInfo object
4578
        $contentInfo = $contentService->loadContentInfo($contentId);
4579
4580
        // Store remoteId for later testing
4581
        $remoteId = $contentInfo->remoteId;
4582
4583
        // Start a transaction
4584
        $repository->beginTransaction();
4585
4586
        try {
4587
            // Get metadata update struct and change remoteId
4588
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4589
            $metadataUpdate->remoteId = md5(microtime(true));
4590
4591
            // Update the metadata of the published content object
4592
            $contentService->updateContentMetadata(
4593
                $contentInfo,
4594
                $metadataUpdate
4595
            );
4596
        } catch (Exception $e) {
4597
            // Cleanup hanging transaction on error
4598
            $repository->rollback();
4599
            throw $e;
4600
        }
4601
4602
        // Rollback all changes.
4603
        $repository->rollback();
4604
4605
        // Load current remoteId
4606
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4607
        /* END: Use Case */
4608
4609
        $this->assertEquals($remoteId, $remoteIdReloaded);
4610
    }
4611
4612
    /**
4613
     * Test for the updateContentMetadata() method.
4614
     *
4615
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4616
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4617
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4618
     */
4619 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithCommit()
4620
    {
4621
        $repository = $this->getRepository();
4622
4623
        $contentId = $this->generateId('object', 12);
4624
        /* BEGIN: Use Case */
4625
        // $contentId is the ID of the "Administrator users" user group
4626
4627
        // Get the content service
4628
        $contentService = $repository->getContentService();
4629
4630
        // Load a ContentInfo object
4631
        $contentInfo = $contentService->loadContentInfo($contentId);
4632
4633
        // Store remoteId for later testing
4634
        $remoteId = $contentInfo->remoteId;
4635
4636
        // Start a transaction
4637
        $repository->beginTransaction();
4638
4639
        try {
4640
            // Get metadata update struct and change remoteId
4641
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4642
            $metadataUpdate->remoteId = md5(microtime(true));
4643
4644
            // Update the metadata of the published content object
4645
            $contentService->updateContentMetadata(
4646
                $contentInfo,
4647
                $metadataUpdate
4648
            );
4649
4650
            // Commit all changes.
4651
            $repository->commit();
4652
        } catch (Exception $e) {
4653
            // Cleanup hanging transaction on error
4654
            $repository->rollback();
4655
            throw $e;
4656
        }
4657
4658
        // Load current remoteId
4659
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4660
        /* END: Use Case */
4661
4662
        $this->assertNotEquals($remoteId, $remoteIdReloaded);
4663
    }
4664
4665
    /**
4666
     * Test for the deleteVersion() method.
4667
     *
4668
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4669
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4670
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4671
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4672
     */
4673
    public function testDeleteVersionInTransactionWithRollback()
4674
    {
4675
        $repository = $this->getRepository();
4676
4677
        $contentId = $this->generateId('object', 12);
4678
        /* BEGIN: Use Case */
4679
        // $contentId is the ID of the "Administrator users" user group
4680
4681
        // Get the content service
4682
        $contentService = $repository->getContentService();
4683
4684
        // Start a new transaction
4685
        $repository->beginTransaction();
4686
4687
        try {
4688
            // Create a new draft
4689
            $draft = $contentService->createContentDraft(
4690
                $contentService->loadContentInfo($contentId)
4691
            );
4692
4693
            $contentService->deleteVersion($draft->getVersionInfo());
4694
        } catch (Exception $e) {
4695
            // Cleanup hanging transaction on error
4696
            $repository->rollback();
4697
            throw $e;
4698
        }
4699
4700
        // Rollback all changes.
4701
        $repository->rollback();
4702
4703
        // This array will be empty
4704
        $drafts = $contentService->loadContentDrafts();
4705
        /* END: Use Case */
4706
4707
        $this->assertSame(array(), $drafts);
4708
    }
4709
4710
    /**
4711
     * Test for the deleteVersion() method.
4712
     *
4713
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4714
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4715
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4716
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4717
     */
4718
    public function testDeleteVersionInTransactionWithCommit()
4719
    {
4720
        $repository = $this->getRepository();
4721
4722
        $contentId = $this->generateId('object', 12);
4723
        /* BEGIN: Use Case */
4724
        // $contentId is the ID of the "Administrator users" user group
4725
4726
        // Get the content service
4727
        $contentService = $repository->getContentService();
4728
4729
        // Start a new transaction
4730
        $repository->beginTransaction();
4731
4732
        try {
4733
            // Create a new draft
4734
            $draft = $contentService->createContentDraft(
4735
                $contentService->loadContentInfo($contentId)
4736
            );
4737
4738
            $contentService->deleteVersion($draft->getVersionInfo());
4739
4740
            // Commit all changes.
4741
            $repository->commit();
4742
        } catch (Exception $e) {
4743
            // Cleanup hanging transaction on error
4744
            $repository->rollback();
4745
            throw $e;
4746
        }
4747
4748
        // This array will contain no element
4749
        $drafts = $contentService->loadContentDrafts();
4750
        /* END: Use Case */
4751
4752
        $this->assertSame(array(), $drafts);
4753
    }
4754
4755
    /**
4756
     * Test for the deleteContent() method.
4757
     *
4758
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4759
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4760
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4761
     */
4762
    public function testDeleteContentInTransactionWithRollback()
4763
    {
4764
        $repository = $this->getRepository();
4765
4766
        $contentId = $this->generateId('object', 11);
4767
        /* BEGIN: Use Case */
4768
        // $contentId is the ID of the "Members" user group in an eZ Publish
4769
        // demo installation
4770
4771
        // Get content service
4772
        $contentService = $repository->getContentService();
4773
4774
        // Load a ContentInfo instance
4775
        $contentInfo = $contentService->loadContentInfo($contentId);
4776
4777
        // Start a new transaction
4778
        $repository->beginTransaction();
4779
4780
        try {
4781
            // Delete content object
4782
            $contentService->deleteContent($contentInfo);
4783
        } catch (Exception $e) {
4784
            // Cleanup hanging transaction on error
4785
            $repository->rollback();
4786
            throw $e;
4787
        }
4788
4789
        // Rollback all changes
4790
        $repository->rollback();
4791
4792
        // This call will return the original content object
4793
        $contentInfo = $contentService->loadContentInfo($contentId);
4794
        /* END: Use Case */
4795
4796
        $this->assertEquals($contentId, $contentInfo->id);
4797
    }
4798
4799
    /**
4800
     * Test for the deleteContent() method.
4801
     *
4802
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4803
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4804
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4805
     */
4806
    public function testDeleteContentInTransactionWithCommit()
4807
    {
4808
        $repository = $this->getRepository();
4809
4810
        $contentId = $this->generateId('object', 11);
4811
        /* BEGIN: Use Case */
4812
        // $contentId is the ID of the "Members" user group in an eZ Publish
4813
        // demo installation
4814
4815
        // Get content service
4816
        $contentService = $repository->getContentService();
4817
4818
        // Load a ContentInfo instance
4819
        $contentInfo = $contentService->loadContentInfo($contentId);
4820
4821
        // Start a new transaction
4822
        $repository->beginTransaction();
4823
4824
        try {
4825
            // Delete content object
4826
            $contentService->deleteContent($contentInfo);
4827
4828
            // Commit all changes
4829
            $repository->commit();
4830
        } catch (Exception $e) {
4831
            // Cleanup hanging transaction on error
4832
            $repository->rollback();
4833
            throw $e;
4834
        }
4835
4836
        // Deleted content info is not found anymore
4837
        try {
4838
            $contentService->loadContentInfo($contentId);
4839
        } catch (NotFoundException $e) {
4840
            return;
4841
        }
4842
        /* END: Use Case */
4843
4844
        $this->fail('Can still load ContentInfo after commit.');
4845
    }
4846
4847
    /**
4848
     * Test for the copyContent() method.
4849
     *
4850
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4851
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4852
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4853
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4854
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4855
     */
4856 View Code Duplication
    public function testCopyContentInTransactionWithRollback()
4857
    {
4858
        $repository = $this->getRepository();
4859
4860
        $contentId = $this->generateId('object', 11);
4861
        $locationId = $this->generateId('location', 13);
4862
        /* BEGIN: Use Case */
4863
        // $contentId is the ID of the "Members" user group in an eZ Publish
4864
        // demo installation
4865
4866
        // $locationId is the ID of the "Administrator users" group location
4867
4868
        // Get services
4869
        $contentService = $repository->getContentService();
4870
        $locationService = $repository->getLocationService();
4871
4872
        // Load content object to copy
4873
        $content = $contentService->loadContent($contentId);
4874
4875
        // Create new target location
4876
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4877
4878
        // Start a new transaction
4879
        $repository->beginTransaction();
4880
4881
        try {
4882
            // Copy content with all versions and drafts
4883
            $contentService->copyContent(
4884
                $content->contentInfo,
4885
                $locationCreate
4886
            );
4887
        } catch (Exception $e) {
4888
            // Cleanup hanging transaction on error
4889
            $repository->rollback();
4890
            throw $e;
4891
        }
4892
4893
        // Rollback all changes
4894
        $repository->rollback();
4895
4896
        $this->refreshSearch($repository);
4897
4898
        // This array will only contain a single admin user object
4899
        $locations = $locationService->loadLocationChildren(
4900
            $locationService->loadLocation($locationId)
4901
        )->locations;
4902
        /* END: Use Case */
4903
4904
        $this->assertEquals(1, count($locations));
4905
    }
4906
4907
    /**
4908
     * Test for the copyContent() method.
4909
     *
4910
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4911
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4912
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4913
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4914
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4915
     */
4916 View Code Duplication
    public function testCopyContentInTransactionWithCommit()
4917
    {
4918
        $repository = $this->getRepository();
4919
4920
        $contentId = $this->generateId('object', 11);
4921
        $locationId = $this->generateId('location', 13);
4922
        /* BEGIN: Use Case */
4923
        // $contentId is the ID of the "Members" user group in an eZ Publish
4924
        // demo installation
4925
4926
        // $locationId is the ID of the "Administrator users" group location
4927
4928
        // Get services
4929
        $contentService = $repository->getContentService();
4930
        $locationService = $repository->getLocationService();
4931
4932
        // Load content object to copy
4933
        $content = $contentService->loadContent($contentId);
4934
4935
        // Create new target location
4936
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4937
4938
        // Start a new transaction
4939
        $repository->beginTransaction();
4940
4941
        try {
4942
            // Copy content with all versions and drafts
4943
            $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...
4944
                $content->contentInfo,
4945
                $locationCreate
4946
            );
4947
4948
            // Commit all changes
4949
            $repository->commit();
4950
        } catch (Exception $e) {
4951
            // Cleanup hanging transaction on error
4952
            $repository->rollback();
4953
            throw $e;
4954
        }
4955
4956
        $this->refreshSearch($repository);
4957
4958
        // This will contain the admin user and the new child location
4959
        $locations = $locationService->loadLocationChildren(
4960
            $locationService->loadLocation($locationId)
4961
        )->locations;
4962
        /* END: Use Case */
4963
4964
        $this->assertEquals(2, count($locations));
4965
    }
4966
4967
    public function testURLAliasesCreatedForNewContent()
4968
    {
4969
        $repository = $this->getRepository();
4970
4971
        $contentService = $repository->getContentService();
4972
        $locationService = $repository->getLocationService();
4973
        $urlAliasService = $repository->getURLAliasService();
4974
4975
        /* BEGIN: Use Case */
4976
        $draft = $this->createContentDraftVersion1();
4977
4978
        // Automatically creates a new URLAlias for the content
4979
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
4980
        /* END: Use Case */
4981
4982
        $location = $locationService->loadLocation(
4983
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4984
        );
4985
4986
        $aliases = $urlAliasService->listLocationAliases($location, false);
4987
4988
        $this->assertAliasesCorrect(
4989
            array(
4990
                '/Design/Plain-site/An-awesome-forum' => array(
4991
                    'type' => URLAlias::LOCATION,
4992
                    'destination' => $location->id,
4993
                    'path' => '/Design/Plain-site/An-awesome-forum',
4994
                    'languageCodes' => array('eng-US'),
4995
                    'isHistory' => false,
4996
                    'isCustom' => false,
4997
                    'forward' => false,
4998
                ),
4999
            ),
5000
            $aliases
5001
        );
5002
    }
5003
5004
    public function testURLAliasesCreatedForUpdatedContent()
5005
    {
5006
        $repository = $this->getRepository();
5007
5008
        $contentService = $repository->getContentService();
5009
        $locationService = $repository->getLocationService();
5010
        $urlAliasService = $repository->getURLAliasService();
5011
5012
        /* BEGIN: Use Case */
5013
        $draft = $this->createUpdatedDraftVersion2();
5014
5015
        $location = $locationService->loadLocation(
5016
            $draft->getVersionInfo()->getContentInfo()->mainLocationId
5017
        );
5018
5019
        // Load and assert URL aliases before publishing updated Content, so that
5020
        // SPI cache is warmed up and cache invalidation is also tested.
5021
        $aliases = $urlAliasService->listLocationAliases($location, false);
5022
5023
        $this->assertAliasesCorrect(
5024
            array(
5025
                '/Design/Plain-site/An-awesome-forum' => array(
5026
                    'type' => URLAlias::LOCATION,
5027
                    'destination' => $location->id,
5028
                    'path' => '/Design/Plain-site/An-awesome-forum',
5029
                    'languageCodes' => array('eng-US'),
5030
                    'alwaysAvailable' => true,
5031
                    'isHistory' => false,
5032
                    'isCustom' => false,
5033
                    'forward' => false,
5034
                ),
5035
            ),
5036
            $aliases
5037
        );
5038
5039
        // Automatically marks old aliases for the content as history
5040
        // and creates new aliases, based on the changes
5041
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
5042
        /* END: Use Case */
5043
5044
        $location = $locationService->loadLocation(
5045
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
5046
        );
5047
5048
        $aliases = $urlAliasService->listLocationAliases($location, false);
5049
5050
        $this->assertAliasesCorrect(
5051
            array(
5052
                '/Design/Plain-site/An-awesome-forum2' => array(
5053
                    'type' => URLAlias::LOCATION,
5054
                    'destination' => $location->id,
5055
                    'path' => '/Design/Plain-site/An-awesome-forum2',
5056
                    'languageCodes' => array('eng-US'),
5057
                    'alwaysAvailable' => true,
5058
                    'isHistory' => false,
5059
                    'isCustom' => false,
5060
                    'forward' => false,
5061
                ),
5062
                '/Design/Plain-site/An-awesome-forum23' => array(
5063
                    'type' => URLAlias::LOCATION,
5064
                    'destination' => $location->id,
5065
                    'path' => '/Design/Plain-site/An-awesome-forum23',
5066
                    'languageCodes' => array('eng-GB'),
5067
                    'alwaysAvailable' => true,
5068
                    'isHistory' => false,
5069
                    'isCustom' => false,
5070
                    'forward' => false,
5071
                ),
5072
            ),
5073
            $aliases
5074
        );
5075
    }
5076
5077
    public function testCustomURLAliasesNotHistorizedOnUpdatedContent()
5078
    {
5079
        $repository = $this->getRepository();
5080
5081
        $contentService = $repository->getContentService();
5082
5083
        /* BEGIN: Use Case */
5084
        $urlAliasService = $repository->getURLAliasService();
5085
        $locationService = $repository->getLocationService();
5086
5087
        $content = $this->createContentVersion1();
5088
5089
        // Create a custom URL alias
5090
        $urlAliasService->createUrlAlias(
5091
            $locationService->loadLocation(
5092
                $content->getVersionInfo()->getContentInfo()->mainLocationId
5093
            ),
5094
            '/my/fancy/story-about-ez-publish',
5095
            'eng-US'
5096
        );
5097
5098
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
5099
5100
        $contentUpdate = $contentService->newContentUpdateStruct();
5101
        $contentUpdate->initialLanguageCode = 'eng-US';
5102
        $contentUpdate->setField('name', 'Amazing Bielefeld forum');
5103
5104
        $draftVersion2 = $contentService->updateContent(
5105
            $draftVersion2->getVersionInfo(),
5106
            $contentUpdate
5107
        );
5108
5109
        // Only marks auto-generated aliases as history
5110
        // the custom one is left untouched
5111
        $liveContent = $contentService->publishVersion($draftVersion2->getVersionInfo());
5112
        /* END: Use Case */
5113
5114
        $location = $locationService->loadLocation(
5115
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
5116
        );
5117
5118
        $aliases = $urlAliasService->listLocationAliases($location);
5119
5120
        $this->assertAliasesCorrect(
5121
            array(
5122
                '/my/fancy/story-about-ez-publish' => array(
5123
                    'type' => URLAlias::LOCATION,
5124
                    'destination' => $location->id,
5125
                    'path' => '/my/fancy/story-about-ez-publish',
5126
                    'languageCodes' => array('eng-US'),
5127
                    'isHistory' => false,
5128
                    'isCustom' => true,
5129
                    'forward' => false,
5130
                    'alwaysAvailable' => false,
5131
                ),
5132
            ),
5133
            $aliases
5134
        );
5135
    }
5136
5137
    /**
5138
     * Test to ensure that old versions are not affected by updates to newer
5139
     * drafts.
5140
     */
5141
    public function testUpdatingDraftDoesNotUpdateOldVersions()
5142
    {
5143
        $repository = $this->getRepository();
5144
5145
        $contentService = $repository->getContentService();
5146
5147
        $contentVersion2 = $this->createContentVersion2();
5148
5149
        $loadedContent1 = $contentService->loadContent($contentVersion2->id, null, 1);
5150
        $loadedContent2 = $contentService->loadContent($contentVersion2->id, null, 2);
5151
5152
        $this->assertNotEquals(
5153
            $loadedContent1->getFieldValue('name', 'eng-US'),
5154
            $loadedContent2->getFieldValue('name', 'eng-US')
5155
        );
5156
    }
5157
5158
    /**
5159
     * Test scenario with writer and publisher users.
5160
     * Writer can only create content. Publisher can publish this content.
5161
     */
5162
    public function testPublishWorkflow()
5163
    {
5164
        $repository = $this->getRepository();
5165
        $contentService = $repository->getContentService();
5166
5167
        $this->createRoleWithPolicies('Publisher', [
5168
            ['module' => 'content', 'function' => 'read'],
5169
            ['module' => 'content', 'function' => 'create'],
5170
            ['module' => 'content', 'function' => 'publish'],
5171
        ]);
5172
5173
        $this->createRoleWithPolicies('Writer', [
5174
            ['module' => 'content', 'function' => 'read'],
5175
            ['module' => 'content', 'function' => 'create'],
5176
        ]);
5177
5178
        $writerUser = $this->createCustomUserWithLogin(
5179
            'writer',
5180
            '[email protected]',
5181
            'Writers',
5182
            'Writer'
5183
        );
5184
5185
        $publisherUser = $this->createCustomUserWithLogin(
5186
            'publisher',
5187
            '[email protected]',
5188
            'Publishers',
5189
            'Publisher'
5190
        );
5191
5192
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5193
        $draft = $this->createContentDraftVersion1();
5194
5195
        $repository->getPermissionResolver()->setCurrentUserReference($publisherUser);
5196
        $content = $contentService->publishVersion($draft->versionInfo);
5197
5198
        $contentService->loadContent($content->id);
5199
    }
5200
5201
    /**
5202
     * Test publish / content policy is required to be able to publish content.
5203
     *
5204
     * @expectedException \eZ\Publish\Core\Base\Exceptions\UnauthorizedException
5205
     * @expectedExceptionMessageRegExp /User does not have access to 'publish' 'content'/
5206
     */
5207
    public function testPublishContentWithoutPublishPolicyThrowsException()
5208
    {
5209
        $repository = $this->getRepository();
5210
5211
        $this->createRoleWithPolicies('Writer', [
5212
            ['module' => 'content', 'function' => 'read'],
5213
            ['module' => 'content', 'function' => 'create'],
5214
            ['module' => 'content', 'function' => 'edit'],
5215
        ]);
5216
        $writerUser = $this->createCustomUserWithLogin(
5217
            'writer',
5218
            '[email protected]',
5219
            'Writers',
5220
            'Writer'
5221
        );
5222
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5223
5224
        $this->createContentVersion1();
5225
    }
5226
5227
    /**
5228
     * Test removal of the specific translation from all the Versions of a Content Object.
5229
     *
5230
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5231
     */
5232 View Code Duplication
    public function testDeleteTranslation()
5233
    {
5234
        $repository = $this->getRepository();
5235
        $contentService = $repository->getContentService();
5236
        $content = $this->createContentVersion2();
5237
5238
        // create multiple versions to exceed archive limit
5239
        for ($i = 0; $i < 5; ++$i) {
5240
            $contentDraft = $contentService->createContentDraft($content->contentInfo);
5241
            $contentUpdateStruct = $contentService->newContentUpdateStruct();
5242
            $contentDraft = $contentService->updateContent(
5243
                $contentDraft->versionInfo,
5244
                $contentUpdateStruct
5245
            );
5246
            $contentService->publishVersion($contentDraft->versionInfo);
5247
        }
5248
5249
        $contentService->deleteTranslation($content->contentInfo, 'eng-GB');
5250
5251
        $this->assertTranslationDoesNotExist('eng-GB', $content->id);
5252
    }
5253
5254
    /**
5255
     * Test deleting a Translation which is initial for some Version, updates initialLanguageCode
5256
     * with mainLanguageCode (assuming they are different).
5257
     */
5258
    public function testDeleteTranslationUpdatesInitialLanguageCodeVersion()
5259
    {
5260
        $repository = $this->getRepository();
5261
        $contentService = $repository->getContentService();
5262
5263
        $content = $this->createContentVersion2();
5264
        // create another, copied, version
5265
        $contentDraft = $contentService->updateContent(
5266
            $contentService->createContentDraft($content->contentInfo)->versionInfo,
5267
            $contentService->newContentUpdateStruct()
5268
        );
5269
        $publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
5270
5271
        // remove first version with only one translation as it is not the subject of this test
5272
        $contentService->deleteVersion(
5273
            $contentService->loadVersionInfo($publishedContent->contentInfo, 1)
5274
        );
5275
5276
        // sanity check
5277
        self::assertEquals('eng-US', $content->contentInfo->mainLanguageCode);
5278
        self::assertEquals('eng-US', $content->versionInfo->initialLanguageCode);
5279
5280
        // update mainLanguageCode so it is different than initialLanguageCode for Version
5281
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
5282
        $contentMetadataUpdateStruct->mainLanguageCode = 'eng-GB';
5283
        $content = $contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
5284
5285
        $contentService->deleteTranslation($content->contentInfo, 'eng-US');
5286
5287
        $this->assertTranslationDoesNotExist('eng-US', $content->id);
5288
    }
5289
5290
    /**
5291
     * Test removal of the specific translation properly updates languages of the URL alias.
5292
     *
5293
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5294
     */
5295
    public function testDeleteTranslationUpdatesUrlAlias()
5296
    {
5297
        $repository = $this->getRepository();
5298
        $contentService = $repository->getContentService();
5299
        $locationService = $repository->getLocationService();
5300
        $urlAliasService = $repository->getURLAliasService();
5301
5302
        $content = $this->createContentVersion2();
5303
        $mainLocation = $locationService->loadLocation($content->contentInfo->mainLocationId);
5304
5305
        // create custom URL alias for Content main Location
5306
        $urlAliasService->createUrlAlias($mainLocation, '/my-custom-url', 'eng-GB');
5307
5308
        // create secondary Location for Content
5309
        $secondaryLocation = $locationService->createLocation(
5310
            $content->contentInfo,
5311
            $locationService->newLocationCreateStruct(2)
5312
        );
5313
5314
        // create custom URL alias for Content secondary Location
5315
        $urlAliasService->createUrlAlias($secondaryLocation, '/my-secondary-url', 'eng-GB');
5316
5317
        // delete Translation
5318
        $contentService->deleteTranslation($content->contentInfo, 'eng-GB');
5319
5320
        foreach ([$mainLocation, $secondaryLocation] as $location) {
5321
            // check auto-generated URL aliases
5322
            foreach ($urlAliasService->listLocationAliases($location, false) as $alias) {
5323
                self::assertNotContains('eng-GB', $alias->languageCodes);
5324
            }
5325
5326
            // check custom URL aliases
5327
            foreach ($urlAliasService->listLocationAliases($location) as $alias) {
5328
                self::assertNotContains('eng-GB', $alias->languageCodes);
5329
            }
5330
        }
5331
    }
5332
5333
    /**
5334
     * Test removal of a main translation throws BadStateException.
5335
     *
5336
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5337
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5338
     * @expectedExceptionMessage Specified translation is the main translation of the Content Object
5339
     */
5340
    public function testDeleteTranslationMainLanguageThrowsBadStateException()
5341
    {
5342
        $repository = $this->getRepository();
5343
        $contentService = $repository->getContentService();
5344
        $content = $this->createContentVersion2();
5345
5346
        // delete first version which has only one translation
5347
        $contentService->deleteVersion($contentService->loadVersionInfo($content->contentInfo, 1));
5348
5349
        // try to delete main translation
5350
        $contentService->deleteTranslation($content->contentInfo, $content->contentInfo->mainLanguageCode);
5351
    }
5352
5353
    /**
5354
     * Test removal of a Translation is possible when some archived Versions have only this Translation.
5355
     *
5356
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5357
     */
5358
    public function testDeleteTranslationDeletesSingleTranslationVersions()
5359
    {
5360
        $repository = $this->getRepository();
5361
        $contentService = $repository->getContentService();
5362
        // content created by the createContentVersion1 method has eng-US translation only.
5363
        $content = $this->createContentVersion1();
5364
5365
        // create new version and add eng-GB translation
5366
        $contentDraft = $contentService->createContentDraft($content->contentInfo);
5367
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
5368
        $contentUpdateStruct->setField('name', 'Awesome Board', 'eng-GB');
5369
        $contentDraft = $contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
5370
        $publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
5371
5372
        // update mainLanguageCode to avoid exception related to that
5373
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
5374
        $contentMetadataUpdateStruct->mainLanguageCode = 'eng-GB';
5375
5376
        $content = $contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
5377
5378
        $contentService->deleteTranslation($content->contentInfo, 'eng-US');
5379
5380
        $this->assertTranslationDoesNotExist('eng-US', $content->id);
5381
    }
5382
5383
    /**
5384
     * Test removal of the translation by the user who is not allowed to delete a content
5385
     * throws UnauthorizedException.
5386
     *
5387
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5388
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5389
     * @expectedExceptionMessage User does not have access to 'remove' 'content'
5390
     */
5391
    public function testDeleteTranslationThrowsUnauthorizedException()
5392
    {
5393
        $repository = $this->getRepository();
5394
        $contentService = $repository->getContentService();
5395
5396
        $content = $this->createContentVersion2();
5397
5398
        // create user that can read/create/edit but cannot delete content
5399
        $this->createRoleWithPolicies('Writer', [
5400
            ['module' => 'content', 'function' => 'read'],
5401
            ['module' => 'content', 'function' => 'versionread'],
5402
            ['module' => 'content', 'function' => 'create'],
5403
            ['module' => 'content', 'function' => 'edit'],
5404
        ]);
5405
        $writerUser = $this->createCustomUserWithLogin(
5406
            'writer',
5407
            '[email protected]',
5408
            'Writers',
5409
            'Writer'
5410
        );
5411
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5412
        $contentService->deleteTranslation($content->contentInfo, 'eng-GB');
5413
    }
5414
5415
    /**
5416
     * Test removal of a non-existent translation throws InvalidArgumentException.
5417
     *
5418
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5419
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
5420
     * @expectedExceptionMessage Argument '$languageCode' is invalid: ger-DE does not exist in the Content item
5421
     */
5422
    public function testDeleteTranslationThrowsInvalidArgumentException()
5423
    {
5424
        $repository = $this->getRepository();
5425
        $contentService = $repository->getContentService();
5426
        // content created by the createContentVersion1 method has eng-US translation only.
5427
        $content = $this->createContentVersion1();
5428
        $contentService->deleteTranslation($content->contentInfo, 'ger-DE');
5429
    }
5430
5431
    /**
5432
     * Test deleting a Translation from Draft.
5433
     *
5434
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5435
     */
5436
    public function testDeleteTranslationFromDraft()
5437
    {
5438
        $repository = $this->getRepository();
5439
        $contentService = $repository->getContentService();
5440
5441
        $languageCode = 'eng-GB';
5442
        $content = $this->createMultipleLanguageContentVersion2();
5443
        $draft = $contentService->createContentDraft($content->contentInfo);
5444
        $draft = $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5445
        $content = $contentService->publishVersion($draft->versionInfo);
5446
5447
        $loadedContent = $contentService->loadContent($content->id);
5448
        self::assertNotContains($languageCode, $loadedContent->versionInfo->languageCodes);
5449
        self::assertEmpty($loadedContent->getFieldsByLanguage($languageCode));
5450
    }
5451
5452
    /**
5453
     * Get values for multilingual field.
5454
     *
5455
     * @return array
5456
     */
5457
    public function providerForDeleteTranslationFromDraftRemovesUrlAliasOnPublishing()
5458
    {
5459
        return [
5460
            [
5461
                ['eng-US' => 'US Name', 'eng-GB' => 'GB Name'],
5462
            ],
5463
            [
5464
                ['eng-US' => 'Same Name', 'eng-GB' => 'Same Name'],
5465
            ],
5466
        ];
5467
    }
5468
5469
    /**
5470
     * Test deleting a Translation from Draft removes previously stored URL aliases for published Content.
5471
     *
5472
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5473
     *
5474
     * @dataProvider providerForDeleteTranslationFromDraftRemovesUrlAliasOnPublishing
5475
     *
5476
     * @param string[] $fieldValues translated field values
5477
     *
5478
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
5479
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
5480
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
5481
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5482
     */
5483
    public function testDeleteTranslationFromDraftRemovesUrlAliasOnPublishing(array $fieldValues)
5484
    {
5485
        $repository = $this->getRepository();
5486
        $contentService = $repository->getContentService();
5487
        $locationService = $repository->getLocationService();
5488
        $urlAliasService = $repository->getURLAliasService();
5489
5490
        // set language code to be removed
5491
        $languageCode = 'eng-GB';
5492
        $draft = $this->createMultilingualContentDraft(
5493
            'folder',
5494
            2,
5495
            'eng-US',
5496
            [
5497
                'name' => [
5498
                    'eng-GB' => $fieldValues['eng-GB'],
5499
                    'eng-US' => $fieldValues['eng-US'],
5500
                ],
5501
            ]
5502
        );
5503
        $content = $contentService->publishVersion($draft->versionInfo);
5504
5505
        // create secondary location
5506
        $locationService->createLocation(
5507
            $content->contentInfo,
5508
            $locationService->newLocationCreateStruct(5)
5509
        );
5510
5511
        // sanity check
5512
        $locations = $locationService->loadLocations($content->contentInfo);
5513
        self::assertCount(2, $locations, 'Sanity check: Expected to find 2 Locations');
5514
        foreach ($locations as $location) {
5515
            $urlAliasService->createUrlAlias($location, '/us-custom_' . $location->id, 'eng-US');
5516
            $urlAliasService->createUrlAlias($location, '/gb-custom_' . $location->id, 'eng-GB');
5517
5518
            // check default URL aliases
5519
            $aliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
5520
            self::assertNotEmpty($aliases, 'Sanity check: URL alias for the translation does not exist');
5521
5522
            // check custom URL aliases
5523
            $aliases = $urlAliasService->listLocationAliases($location, true, $languageCode);
5524
            self::assertNotEmpty($aliases, 'Sanity check: Custom URL alias for the translation does not exist');
5525
        }
5526
5527
        // delete translation and publish new version
5528
        $draft = $contentService->createContentDraft($content->contentInfo);
5529
        $draft = $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5530
        $contentService->publishVersion($draft->versionInfo);
5531
5532
        // check that aliases does not exist
5533
        foreach ($locations as $location) {
5534
            // check default URL aliases
5535
            $aliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
5536
            self::assertEmpty($aliases, 'URL alias for the deleted translation still exists');
5537
5538
            // check custom URL aliases
5539
            $aliases = $urlAliasService->listLocationAliases($location, true, $languageCode);
5540
            self::assertEmpty($aliases, 'Custom URL alias for the deleted translation still exists');
5541
        }
5542
    }
5543
5544
    /**
5545
     * Test that URL aliases for deleted Translations are properly archived.
5546
     */
5547
    public function testDeleteTranslationFromDraftArchivesUrlAliasOnPublishing()
5548
    {
5549
        $repository = $this->getRepository();
5550
        $contentService = $repository->getContentService();
5551
        $urlAliasService = $repository->getURLAliasService();
5552
5553
        $content = $contentService->publishVersion(
5554
            $this->createMultilingualContentDraft(
5555
                'folder',
5556
                2,
5557
                'eng-US',
5558
                [
5559
                    'name' => [
5560
                        'eng-GB' => 'BritishEnglishContent',
5561
                        'eng-US' => 'AmericanEnglishContent',
5562
                    ],
5563
                ]
5564
            )->versionInfo
5565
        );
5566
5567
        $unrelatedContent = $contentService->publishVersion(
5568
            $this->createMultilingualContentDraft(
5569
                'folder',
5570
                2,
5571
                'eng-US',
5572
                [
5573
                    'name' => [
5574
                        'eng-GB' => 'AnotherBritishContent',
5575
                        'eng-US' => 'AnotherAmericanContent',
5576
                    ],
5577
                ]
5578
            )->versionInfo
5579
        );
5580
5581
        $urlAlias = $urlAliasService->lookup('/BritishEnglishContent');
5582
        self::assertFalse($urlAlias->isHistory);
5583
        self::assertEquals($urlAlias->path, '/BritishEnglishContent');
5584
        self::assertEquals($urlAlias->destination, $content->contentInfo->mainLocationId);
5585
5586
        $draft = $contentService->deleteTranslationFromDraft(
5587
            $contentService->createContentDraft($content->contentInfo)->versionInfo,
5588
            'eng-GB'
5589
        );
5590
        $content = $contentService->publishVersion($draft->versionInfo);
5591
5592
        $urlAlias = $urlAliasService->lookup('/BritishEnglishContent');
5593
        self::assertTrue($urlAlias->isHistory);
5594
        self::assertEquals($urlAlias->path, '/BritishEnglishContent');
5595
        self::assertEquals($urlAlias->destination, $content->contentInfo->mainLocationId);
5596
5597
        $unrelatedUrlAlias = $urlAliasService->lookup('/AnotherBritishContent');
5598
        self::assertFalse($unrelatedUrlAlias->isHistory);
5599
        self::assertEquals($unrelatedUrlAlias->path, '/AnotherBritishContent');
5600
        self::assertEquals($unrelatedUrlAlias->destination, $unrelatedContent->contentInfo->mainLocationId);
5601
    }
5602
5603
    /**
5604
     * Test deleting a Translation from Draft which has single Translation throws BadStateException.
5605
     *
5606
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5607
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5608
     * @expectedExceptionMessage Specified Translation is the only one Content Object Version has
5609
     */
5610
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnSingleTranslation()
5611
    {
5612
        $repository = $this->getRepository();
5613
        $contentService = $repository->getContentService();
5614
5615
        // create Content with single Translation
5616
        $publishedContent = $contentService->publishVersion(
5617
            $this->createContentDraft(
5618
                'forum',
5619
                2,
5620
                ['name' => 'Eng-US Version name']
5621
            )->versionInfo
5622
        );
5623
5624
        // update mainLanguageCode to avoid exception related to trying to delete main Translation
5625
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
5626
        $contentMetadataUpdateStruct->mainLanguageCode = 'eng-GB';
5627
        $publishedContent = $contentService->updateContentMetadata(
5628
            $publishedContent->contentInfo,
5629
            $contentMetadataUpdateStruct
5630
        );
5631
5632
        // create single Translation Version from the first one
5633
        $draft = $contentService->createContentDraft(
5634
            $publishedContent->contentInfo,
5635
            $publishedContent->versionInfo
5636
        );
5637
5638
        // attempt to delete Translation
5639
        $contentService->deleteTranslationFromDraft($draft->versionInfo, 'eng-US');
5640
    }
5641
5642
    /**
5643
     * Test deleting the Main Translation from Draft throws BadStateException.
5644
     *
5645
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5646
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5647
     * @expectedExceptionMessage Specified Translation is the main Translation of the Content Object
5648
     */
5649
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnMainTranslation()
5650
    {
5651
        $repository = $this->getRepository();
5652
        $contentService = $repository->getContentService();
5653
5654
        $mainLanguageCode = 'eng-US';
5655
        $draft = $this->createMultilingualContentDraft(
5656
            'forum',
5657
            2,
5658
            $mainLanguageCode,
5659
            [
5660
                'name' => [
5661
                    'eng-US' => 'An awesome eng-US forum',
5662
                    'eng-GB' => 'An awesome eng-GB forum',
5663
                ],
5664
            ]
5665
        );
5666
        $contentService->deleteTranslationFromDraft($draft->versionInfo, $mainLanguageCode);
5667
    }
5668
5669
    /**
5670
     * Test deleting the Translation from Published Version throws BadStateException.
5671
     *
5672
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5673
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5674
     * @expectedExceptionMessage Version is not a draft
5675
     */
5676 View Code Duplication
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnPublishedVersion()
5677
    {
5678
        $repository = $this->getRepository();
5679
        $contentService = $repository->getContentService();
5680
5681
        $languageCode = 'eng-US';
5682
        $content = $this->createMultipleLanguageContentVersion2();
5683
        $draft = $contentService->createContentDraft($content->contentInfo);
5684
        $publishedContent = $contentService->publishVersion($draft->versionInfo);
5685
        $contentService->deleteTranslationFromDraft($publishedContent->versionInfo, $languageCode);
5686
    }
5687
5688
    /**
5689
     * Test deleting a Translation from Draft throws UnauthorizedException if user cannot edit Content.
5690
     *
5691
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5692
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5693
     * @expectedExceptionMessage User does not have access to 'edit' 'content'
5694
     */
5695
    public function testDeleteTranslationFromDraftThrowsUnauthorizedException()
5696
    {
5697
        $repository = $this->getRepository();
5698
        $contentService = $repository->getContentService();
5699
5700
        $languageCode = 'eng-GB';
5701
        $content = $this->createMultipleLanguageContentVersion2();
5702
        $draft = $contentService->createContentDraft($content->contentInfo);
5703
5704
        // create user that can read/create/delete but cannot edit or content
5705
        $this->createRoleWithPolicies('Writer', [
5706
            ['module' => 'content', 'function' => 'read'],
5707
            ['module' => 'content', 'function' => 'versionread'],
5708
            ['module' => 'content', 'function' => 'create'],
5709
            ['module' => 'content', 'function' => 'delete'],
5710
        ]);
5711
        $writerUser = $this->createCustomUserWithLogin(
5712
            'user',
5713
            '[email protected]',
5714
            'Writers',
5715
            'Writer'
5716
        );
5717
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5718
5719
        $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5720
    }
5721
5722
    /**
5723
     * Test deleting a non-existent Translation from Draft throws InvalidArgumentException.
5724
     *
5725
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5726
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
5727
     * @expectedExceptionMessageRegExp /The Version \(ContentId=\d+, VersionNo=\d+\) is not translated into ger-DE/
5728
     */
5729
    public function testDeleteTranslationFromDraftThrowsInvalidArgumentException()
5730
    {
5731
        $repository = $this->getRepository();
5732
        $contentService = $repository->getContentService();
5733
5734
        $languageCode = 'ger-DE';
5735
        $content = $this->createMultipleLanguageContentVersion2();
5736
        $draft = $contentService->createContentDraft($content->contentInfo);
5737
        $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5738
    }
5739
5740
    public function testCreateAndDeleteWithinSameTransaction()
5741
    {
5742
        $repository = $this->getRepository();
5743
        $contentService = $repository->getContentService();
5744
5745
        $repository->beginTransaction();
5746
        $content = $this->createContentDraft('folder', 2, ['name' => __METHOD__]);
5747
        $contentService->publishVersion($content->versionInfo);
5748
        $contentService->deleteContent($content->contentInfo);
5749
        $repository->commit();
5750
    }
5751
5752
    /**
5753
     * Asserts that all aliases defined in $expectedAliasProperties with the
5754
     * given properties are available in $actualAliases and not more.
5755
     *
5756
     * @param array $expectedAliasProperties
5757
     * @param array $actualAliases
5758
     */
5759
    private function assertAliasesCorrect(array $expectedAliasProperties, array $actualAliases)
5760
    {
5761
        foreach ($actualAliases as $actualAlias) {
5762
            if (!isset($expectedAliasProperties[$actualAlias->path])) {
5763
                $this->fail(
5764
                    sprintf(
5765
                        'Alias with path "%s" in languages "%s" not expected.',
5766
                        $actualAlias->path,
5767
                        implode(', ', $actualAlias->languageCodes)
5768
                    )
5769
                );
5770
            }
5771
5772
            foreach ($expectedAliasProperties[$actualAlias->path] as $propertyName => $propertyValue) {
5773
                $this->assertEquals(
5774
                    $propertyValue,
5775
                    $actualAlias->$propertyName,
5776
                    sprintf(
5777
                        'Property $%s incorrect on alias with path "%s" in languages "%s".',
5778
                        $propertyName,
5779
                        $actualAlias->path,
5780
                        implode(', ', $actualAlias->languageCodes)
5781
                    )
5782
                );
5783
            }
5784
5785
            unset($expectedAliasProperties[$actualAlias->path]);
5786
        }
5787
5788
        if (!empty($expectedAliasProperties)) {
5789
            $this->fail(
5790
                sprintf(
5791
                    'Missing expected aliases with paths "%s".',
5792
                    implode('", "', array_keys($expectedAliasProperties))
5793
                )
5794
            );
5795
        }
5796
    }
5797
5798
    /**
5799
     * Asserts that the given fields are equal to the default fields fixture.
5800
     *
5801
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5802
     */
5803
    private function assertAllFieldsEquals(array $fields)
5804
    {
5805
        $actual = $this->normalizeFields($fields);
5806
        $expected = $this->normalizeFields($this->createFieldsFixture());
5807
5808
        $this->assertEquals($expected, $actual);
5809
    }
5810
5811
    /**
5812
     * Asserts that the given fields are equal to a language filtered set of the
5813
     * default fields fixture.
5814
     *
5815
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5816
     * @param string $languageCode
5817
     */
5818
    private function assertLocaleFieldsEquals(array $fields, $languageCode)
5819
    {
5820
        $actual = $this->normalizeFields($fields);
5821
5822
        $expected = array();
5823
        foreach ($this->normalizeFields($this->createFieldsFixture()) as $field) {
5824
            if ($field->languageCode !== $languageCode) {
5825
                continue;
5826
            }
5827
            $expected[] = $field;
5828
        }
5829
5830
        $this->assertEquals($expected, $actual);
5831
    }
5832
5833
    /**
5834
     * This method normalizes a set of fields and returns a normalized set.
5835
     *
5836
     * Normalization means it resets the storage specific field id to zero and
5837
     * it sorts the field by their identifier and their language code. In
5838
     * addition, the field value is removed, since this one depends on the
5839
     * specific FieldType, which is tested in a dedicated integration test.
5840
     *
5841
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5842
     *
5843
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5844
     */
5845
    private function normalizeFields(array $fields)
5846
    {
5847
        $normalized = array();
5848 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...
5849
            $normalized[] = new Field(
5850
                array(
5851
                    'id' => 0,
5852
                    'value' => ($field->value !== null ? true : null),
5853
                    'languageCode' => $field->languageCode,
5854
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
5855
                    'fieldTypeIdentifier' => $field->fieldTypeIdentifier,
5856
                )
5857
            );
5858
        }
5859
        usort(
5860
            $normalized,
5861 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...
5862
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
5863
                    return strcasecmp($field1->languageCode, $field2->languageCode);
5864
                }
5865
5866
                return $return;
5867
            }
5868
        );
5869
5870
        return $normalized;
5871
    }
5872
5873
    /**
5874
     * Returns a filtered set of the default fields fixture.
5875
     *
5876
     * @param string $languageCode
5877
     *
5878
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5879
     */
5880
    private function createLocaleFieldsFixture($languageCode)
5881
    {
5882
        $fields = array();
5883
        foreach ($this->createFieldsFixture() as $field) {
5884
            if (null === $field->languageCode || $languageCode === $field->languageCode) {
5885
                $fields[] = $field;
5886
            }
5887
        }
5888
5889
        return $fields;
5890
    }
5891
5892
    /**
5893
     * Asserts that given Content has default ContentStates.
5894
     *
5895
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
5896
     */
5897 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
5898
    {
5899
        $repository = $this->getRepository();
5900
        $objectStateService = $repository->getObjectStateService();
5901
5902
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
5903
5904
        foreach ($objectStateGroups as $objectStateGroup) {
5905
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
5906
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
5907
                // Only check the first object state which is the default one.
5908
                $this->assertEquals(
5909
                    $objectState,
5910
                    $contentState
5911
                );
5912
                break;
5913
            }
5914
        }
5915
    }
5916
5917
    /**
5918
     * Assert that given Content has no references to a translation specified by the $languageCode.
5919
     *
5920
     * @param string $languageCode
5921
     * @param int $contentId
5922
     */
5923
    private function assertTranslationDoesNotExist($languageCode, $contentId)
5924
    {
5925
        $repository = $this->getRepository();
5926
        $contentService = $repository->getContentService();
5927
5928
        $content = $contentService->loadContent($contentId);
5929
5930
        foreach ($content->fields as $fieldIdentifier => $field) {
5931
            /** @var array $field */
5932
            self::assertArrayNotHasKey($languageCode, $field);
5933
            self::assertNotEquals($languageCode, $content->contentInfo->mainLanguageCode);
5934
            self::assertArrayNotHasKey($languageCode, $content->versionInfo->getNames());
5935
            self::assertNotEquals($languageCode, $content->versionInfo->initialLanguageCode);
5936
            self::assertNotContains($languageCode, $content->versionInfo->languageCodes);
5937
        }
5938
        foreach ($contentService->loadVersions($content->contentInfo) as $versionInfo) {
5939
            self::assertArrayNotHasKey($languageCode, $versionInfo->getNames());
5940
            self::assertNotEquals($languageCode, $versionInfo->contentInfo->mainLanguageCode);
5941
            self::assertNotEquals($languageCode, $versionInfo->initialLanguageCode);
5942
            self::assertNotContains($languageCode, $versionInfo->languageCodes);
5943
        }
5944
    }
5945
5946
    /**
5947
     * Returns the default fixture of fields used in most tests.
5948
     *
5949
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5950
     */
5951
    private function createFieldsFixture()
5952
    {
5953
        return array(
5954
            new Field(
5955
                array(
5956
                    'id' => 0,
5957
                    'value' => 'Foo',
5958
                    'languageCode' => 'eng-US',
5959
                    'fieldDefIdentifier' => 'description',
5960
                    'fieldTypeIdentifier' => 'ezrichtext',
5961
                )
5962
            ),
5963
            new Field(
5964
                array(
5965
                    'id' => 0,
5966
                    'value' => 'Bar',
5967
                    'languageCode' => 'eng-GB',
5968
                    'fieldDefIdentifier' => 'description',
5969
                    'fieldTypeIdentifier' => 'ezrichtext',
5970
                )
5971
            ),
5972
            new Field(
5973
                array(
5974
                    'id' => 0,
5975
                    'value' => 'An awesome multi-lang forum²',
5976
                    'languageCode' => 'eng-US',
5977
                    'fieldDefIdentifier' => 'name',
5978
                    'fieldTypeIdentifier' => 'ezstring',
5979
                )
5980
            ),
5981
            new Field(
5982
                array(
5983
                    'id' => 0,
5984
                    'value' => 'An awesome multi-lang forum²³',
5985
                    'languageCode' => 'eng-GB',
5986
                    'fieldDefIdentifier' => 'name',
5987
                    'fieldTypeIdentifier' => 'ezstring',
5988
                )
5989
            ),
5990
        );
5991
    }
5992
5993
    /**
5994
     * Gets expected property values for the "Media" ContentInfo ValueObject.
5995
     *
5996
     * @return array
5997
     */
5998 View Code Duplication
    private function getExpectedMediaContentInfoProperties()
5999
    {
6000
        return [
6001
            'id' => 41,
6002
            'contentTypeId' => 1,
6003
            'name' => 'Media',
6004
            'sectionId' => 3,
6005
            'currentVersionNo' => 1,
6006
            'published' => true,
6007
            'ownerId' => 14,
6008
            'modificationDate' => $this->createDateTime(1060695457),
6009
            'publishedDate' => $this->createDateTime(1060695457),
6010
            'alwaysAvailable' => 1,
6011
            'remoteId' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
6012
            'mainLanguageCode' => 'eng-US',
6013
            'mainLocationId' => 43,
6014
            'status' => ContentInfo::STATUS_PUBLISHED,
6015
        ];
6016
    }
6017
}
6018