Completed
Push — 6.13 ( d5b0d9...ad14d6 )
by André
118:31 queued 98:50
created

testUpdateContentValidatorIgnoresRequiredFieldsOfNotUpdatedLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

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