Completed
Push — master ( 4339a8...5a694b )
by André
70:16 queued 52:05
created

ContentServiceTest::createLocaleFieldsFixture()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading history...
1291
    {
1292
        $repository = $this->getRepository();
1293
1294
        $contentService = $repository->getContentService();
1295
1296
        /* BEGIN: Use Case */
1297
        $content = $this->createContentVersion1();
1298
1299
        // Now we create a new draft from the published content
1300
        $contentService->createContentDraft($content->contentInfo);
1301
1302
        // This call will still load the published version
1303
        $versionInfoPublished = $contentService->loadVersionInfo($content->contentInfo);
1304
        /* END: Use Case */
1305
1306
        $this->assertEquals(1, $versionInfoPublished->versionNo);
1307
    }
1308
1309
    /**
1310
     * Test for the createContentDraft() method.
1311
     *
1312
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1313
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
1314
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1315
     */
1316
    public function testCreateContentDraftLoadContentStillLoadsPublishedVersion()
1317
    {
1318
        $repository = $this->getRepository();
1319
1320
        $contentService = $repository->getContentService();
1321
1322
        /* BEGIN: Use Case */
1323
        $content = $this->createContentVersion1();
1324
1325
        // Now we create a new draft from the published content
1326
        $contentService->createContentDraft($content->contentInfo);
1327
1328
        // This call will still load the published content version
1329
        $contentPublished = $contentService->loadContent($content->id);
1330
        /* END: Use Case */
1331
1332
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1333
    }
1334
1335
    /**
1336
     * Test for the createContentDraft() method.
1337
     *
1338
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1339
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
1340
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1341
     */
1342
    public function testCreateContentDraftLoadContentByRemoteIdStillLoadsPublishedVersion()
1343
    {
1344
        $repository = $this->getRepository();
1345
1346
        $contentService = $repository->getContentService();
1347
1348
        /* BEGIN: Use Case */
1349
        $content = $this->createContentVersion1();
1350
1351
        // Now we create a new draft from the published content
1352
        $contentService->createContentDraft($content->contentInfo);
1353
1354
        // This call will still load the published content version
1355
        $contentPublished = $contentService->loadContentByRemoteId('abcdef0123456789abcdef0123456789');
1356
        /* END: Use Case */
1357
1358
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1359
    }
1360
1361
    /**
1362
     * Test for the createContentDraft() method.
1363
     *
1364
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1365
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
1366
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1367
     */
1368
    public function testCreateContentDraftLoadContentByContentInfoStillLoadsPublishedVersion()
1369
    {
1370
        $repository = $this->getRepository();
1371
1372
        $contentService = $repository->getContentService();
1373
1374
        /* BEGIN: Use Case */
1375
        $content = $this->createContentVersion1();
1376
1377
        // Now we create a new draft from the published content
1378
        $contentService->createContentDraft($content->contentInfo);
1379
1380
        // This call will still load the published content version
1381
        $contentPublished = $contentService->loadContentByContentInfo($content->contentInfo);
1382
        /* END: Use Case */
1383
1384
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1385
    }
1386
1387
    /**
1388
     * Test for the newContentUpdateStruct() method.
1389
     *
1390
     * @covers \eZ\Publish\API\Repository\ContentService::newContentUpdateStruct
1391
     * @group user
1392
     */
1393
    public function testNewContentUpdateStruct()
1394
    {
1395
        $repository = $this->getRepository();
1396
1397
        /* BEGIN: Use Case */
1398
        $contentService = $repository->getContentService();
1399
1400
        $updateStruct = $contentService->newContentUpdateStruct();
1401
        /* END: Use Case */
1402
1403
        $this->assertInstanceOf(
1404
            ContentUpdateStruct::class,
1405
            $updateStruct
1406
        );
1407
1408
        $this->assertPropertiesCorrect(
1409
            [
1410
                'initialLanguageCode' => null,
1411
                'fields' => [],
1412
            ],
1413
            $updateStruct
1414
        );
1415
    }
1416
1417
    /**
1418
     * Test for the updateContent() method.
1419
     *
1420
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1421
     *
1422
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1423
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1424
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1425
     * @group user
1426
     * @group field-type
1427
     */
1428
    public function testUpdateContent()
1429
    {
1430
        /* BEGIN: Use Case */
1431
        $draftVersion2 = $this->createUpdatedDraftVersion2();
1432
        /* END: Use Case */
1433
1434
        $this->assertInstanceOf(
1435
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1436
            $draftVersion2
1437
        );
1438
1439
        $this->assertEquals(
1440
            $this->generateId('user', 10),
1441
            $draftVersion2->versionInfo->creatorId,
1442
            'creatorId is not properly set on new Version'
1443
        );
1444
1445
        return $draftVersion2;
1446
    }
1447
1448
    /**
1449
     * Test for the updateContent_WithDifferentUser() method.
1450
     *
1451
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1452
     *
1453
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1454
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1455
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1456
     * @group user
1457
     * @group field-type
1458
     */
1459
    public function testUpdateContentWithDifferentUser()
1460
    {
1461
        /* BEGIN: Use Case */
1462
        $arrayWithDraftVersion2 = $this->createUpdatedDraftVersion2NotAdmin();
1463
        /* END: Use Case */
1464
1465
        $this->assertInstanceOf(
1466
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1467
            $arrayWithDraftVersion2[0]
1468
        );
1469
1470
        $this->assertEquals(
1471
            $this->generateId('user', $arrayWithDraftVersion2[1]),
1472
            $arrayWithDraftVersion2[0]->versionInfo->creatorId,
1473
            'creatorId is not properly set on new Version'
1474
        );
1475
1476
        return $arrayWithDraftVersion2[0];
1477
    }
1478
1479
    /**
1480
     * Test for the updateContent() method.
1481
     *
1482
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1483
     *
1484
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1485
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1486
     */
1487
    public function testUpdateContentSetsExpectedFields($content)
1488
    {
1489
        $actual = $this->normalizeFields($content->getFields());
1490
1491
        $expected = array(
1492
            new Field(
1493
                array(
1494
                    'id' => 0,
1495
                    'value' => true,
1496
                    'languageCode' => 'eng-GB',
1497
                    'fieldDefIdentifier' => 'description',
1498
                    'fieldTypeIdentifier' => 'ezrichtext',
1499
                )
1500
            ),
1501
            new Field(
1502
                array(
1503
                    'id' => 0,
1504
                    'value' => true,
1505
                    'languageCode' => 'eng-US',
1506
                    'fieldDefIdentifier' => 'description',
1507
                    'fieldTypeIdentifier' => 'ezrichtext',
1508
                )
1509
            ),
1510
            new Field(
1511
                array(
1512
                    'id' => 0,
1513
                    'value' => true,
1514
                    'languageCode' => 'eng-GB',
1515
                    'fieldDefIdentifier' => 'name',
1516
                    'fieldTypeIdentifier' => 'ezstring',
1517
                )
1518
            ),
1519
            new Field(
1520
                array(
1521
                    'id' => 0,
1522
                    'value' => true,
1523
                    'languageCode' => 'eng-US',
1524
                    'fieldDefIdentifier' => 'name',
1525
                    'fieldTypeIdentifier' => 'ezstring',
1526
                )
1527
            ),
1528
        );
1529
1530
        $this->assertEquals($expected, $actual);
1531
    }
1532
1533
    /**
1534
     * Test for the updateContent() method.
1535
     *
1536
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1537
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1538
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1539
     */
1540
    public function testUpdateContentThrowsBadStateException()
1541
    {
1542
        $repository = $this->getRepository();
1543
1544
        $contentService = $repository->getContentService();
1545
1546
        /* BEGIN: Use Case */
1547
        $content = $this->createContentVersion1();
1548
1549
        // Now create an update struct and modify some fields
1550
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1551
        $contentUpdateStruct->setField('title', 'An awesome² story about ezp.');
1552
        $contentUpdateStruct->setField('title', 'An awesome²³ story about ezp.', 'eng-GB');
1553
1554
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1555
1556
        // This call will fail with a "BadStateException", because $publishedContent
1557
        // is not a draft.
1558
        $contentService->updateContent(
1559
            $content->getVersionInfo(),
1560
            $contentUpdateStruct
1561
        );
1562
        /* END: Use Case */
1563
    }
1564
1565
    /**
1566
     * Test for the updateContent() method.
1567
     *
1568
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1569
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1570
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1571
     */
1572 View Code Duplication
    public function testUpdateContentThrowsInvalidArgumentExceptionWhenFieldTypeDoesNotAccept()
1573
    {
1574
        $repository = $this->getRepository();
1575
1576
        $contentService = $repository->getContentService();
1577
1578
        /* BEGIN: Use Case */
1579
        $draft = $this->createContentDraftVersion1();
1580
1581
        // Now create an update struct and modify some fields
1582
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1583
        // The name field does not accept a stdClass object as its input
1584
        $contentUpdateStruct->setField('name', new \stdClass(), 'eng-US');
1585
1586
        // Throws an InvalidArgumentException, since the value for field "name"
1587
        // is not accepted
1588
        $contentService->updateContent(
1589
            $draft->getVersionInfo(),
1590
            $contentUpdateStruct
1591
        );
1592
        /* END: Use Case */
1593
    }
1594
1595
    /**
1596
     * Test for the updateContent() method.
1597
     *
1598
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1599
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1600
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1601
     */
1602 View Code Duplication
    public function testUpdateContentWhenMandatoryFieldIsEmpty()
1603
    {
1604
        $repository = $this->getRepository();
1605
1606
        $contentService = $repository->getContentService();
1607
1608
        /* BEGIN: Use Case */
1609
        $draft = $this->createContentDraftVersion1();
1610
1611
        // Now create an update struct and set a mandatory field to null
1612
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1613
        $contentUpdateStruct->setField('name', null);
1614
1615
        // Don't set this, then the above call without languageCode will fail
1616
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1617
1618
        // This call will fail with a "ContentFieldValidationException", because the
1619
        // mandatory "name" field is empty.
1620
        $contentService->updateContent(
1621
            $draft->getVersionInfo(),
1622
            $contentUpdateStruct
1623
        );
1624
        /* END: Use Case */
1625
    }
1626
1627
    /**
1628
     * Test for the updateContent() method.
1629
     *
1630
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1631
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1632
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1633
     */
1634
    public function testUpdateContentThrowsContentFieldValidationException()
1635
    {
1636
        $repository = $this->getRepository();
1637
1638
        /* BEGIN: Use Case */
1639
        $contentTypeService = $repository->getContentTypeService();
1640
        $contentService = $repository->getContentService();
1641
1642
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1643
1644
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1645
        $contentCreate->setField('name', 'An awesome Sidelfingen folder');
1646
1647
        $draft = $contentService->createContent($contentCreate);
1648
1649
        $contentUpdate = $contentService->newContentUpdateStruct();
1650
        // Violates string length constraint
1651
        $contentUpdate->setField('short_name', str_repeat('a', 200), 'eng-US');
1652
1653
        // Throws ContentFieldValidationException because the string length
1654
        // validation of the field "short_name" fails
1655
        $contentService->updateContent($draft->getVersionInfo(), $contentUpdate);
1656
        /* END: Use Case */
1657
    }
1658
1659
    /**
1660
     * Test for the updateContent() method.
1661
     *
1662
     * @covers \eZ\Publish\API\Repository\ContentService::updateContent()
1663
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1664
     */
1665
    public function testUpdateContentValidatorIgnoresRequiredFieldsOfNotUpdatedLanguages()
1666
    {
1667
        $repository = $this->getRepository();
1668
        /* BEGIN: Use Case */
1669
        $contentTypeService = $repository->getContentTypeService();
1670
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1671
1672
        // Create multilangual content
1673
        $contentService = $repository->getContentService();
1674
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1675
        $contentCreate->setField('name', 'An awesome Sidelfingen folder', 'eng-US');
1676
        $contentCreate->setField('name', 'An awesome Sidelfingen folder', 'eng-GB');
1677
1678
        $contentDraft = $contentService->createContent($contentCreate);
1679
1680
        // 2. Update content type definition
1681
        $contentTypeDraft = $contentTypeService->createContentTypeDraft($contentType);
1682
1683
        $fieldDefinition = $contentType->getFieldDefinition('description');
1684
        $fieldDefinitionUpdate = $contentTypeService->newFieldDefinitionUpdateStruct();
1685
        $fieldDefinitionUpdate->identifier = 'description';
1686
        $fieldDefinitionUpdate->isRequired = true;
1687
1688
        $contentTypeService->updateFieldDefinition(
1689
            $contentTypeDraft,
1690
            $fieldDefinition,
1691
            $fieldDefinitionUpdate
1692
        );
1693
        $contentTypeService->publishContentTypeDraft($contentTypeDraft);
1694
1695
        // 3. Update only eng-US translation
1696
        $description = new DOMDocument();
1697
        $description->loadXML(<<<XML
1698
<?xml version="1.0" encoding="UTF-8"?>
1699
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
1700
    <para>Lorem ipsum dolor</para>
1701
</section>
1702
XML
1703
        );
1704
1705
        $contentUpdate = $contentService->newContentUpdateStruct();
1706
        $contentUpdate->setField('name', 'An awesome Sidelfingen folder (updated)', 'eng-US');
1707
        $contentUpdate->setField('description', $description);
1708
1709
        $contentService->updateContent($contentDraft->getVersionInfo(), $contentUpdate);
1710
        /* END: Use Case */
1711
    }
1712
1713
    /**
1714
     * Test for the updateContent() method.
1715
     *
1716
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1717
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1718
     */
1719
    public function testUpdateContentWithNotUpdatingMandatoryField()
1720
    {
1721
        $repository = $this->getRepository();
1722
1723
        $contentService = $repository->getContentService();
1724
1725
        /* BEGIN: Use Case */
1726
        $draft = $this->createContentDraftVersion1();
1727
1728
        // Now create an update struct which does not overwrite mandatory
1729
        // fields
1730
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1731
        $contentUpdateStruct->setField(
1732
            'description',
1733
            '<?xml version="1.0" encoding="UTF-8"?><section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0-variant ezpublish-1.0"/>'
1734
        );
1735
1736
        // Don't set this, then the above call without languageCode will fail
1737
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1738
1739
        // This will only update the "description" field in the "eng-US"
1740
        // language
1741
        $updatedDraft = $contentService->updateContent(
1742
            $draft->getVersionInfo(),
1743
            $contentUpdateStruct
1744
        );
1745
        /* END: Use Case */
1746
1747
        foreach ($updatedDraft->getFields() as $field) {
1748
            if ($field->languageCode === 'eng-US' && $field->fieldDefIdentifier === 'name' && $field->value !== null) {
1749
                // Found field
1750
                return;
1751
            }
1752
        }
1753
        $this->fail(
1754
            'Field with identifier "name" in language "eng-US" could not be found or has empty value.'
1755
        );
1756
    }
1757
1758
    /**
1759
     * Test for the createContentDraft() method.
1760
     *
1761
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
1762
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1763
     */
1764
    public function testCreateContentDraftWithSecondParameter()
1765
    {
1766
        $repository = $this->getRepository();
1767
1768
        $contentService = $repository->getContentService();
1769
1770
        /* BEGIN: Use Case */
1771
        $contentVersion2 = $this->createContentVersion2();
1772
1773
        // Now we create a new draft from the initial version
1774
        $draftedContentReloaded = $contentService->createContentDraft(
1775
            $contentVersion2->contentInfo,
1776
            $contentVersion2->getVersionInfo()
1777
        );
1778
        /* END: Use Case */
1779
1780
        $this->assertEquals(3, $draftedContentReloaded->getVersionInfo()->versionNo);
1781
    }
1782
1783
    /**
1784
     * Test for the createContentDraft() method with third parameter.
1785
     *
1786
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1787
     */
1788 View Code Duplication
    public function testCreateContentDraftWithThirdParameter()
1789
    {
1790
        $repository = $this->getRepository();
1791
1792
        $contentService = $repository->getContentService();
1793
1794
        $content = $contentService->loadContent(4);
1795
        $user = $this->createUserVersion1();
1796
1797
        $draftContent = $contentService->createContentDraft(
1798
            $content->contentInfo,
1799
            $content->getVersionInfo(),
1800
            $user
1801
        );
1802
1803
        $this->assertInstanceOf(
1804
            Content::class,
1805
            $draftContent
1806
        );
1807
    }
1808
1809
    /**
1810
     * Test for the publishVersion() method.
1811
     *
1812
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1813
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1814
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1815
     */
1816 View Code Duplication
    public function testPublishVersionFromContentDraft()
1817
    {
1818
        $repository = $this->getRepository();
1819
1820
        $contentService = $repository->getContentService();
1821
1822
        /* BEGIN: Use Case */
1823
        $contentVersion2 = $this->createContentVersion2();
1824
        /* END: Use Case */
1825
1826
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo);
1827
1828
        $this->assertEquals(
1829
            array(
1830
                'status' => VersionInfo::STATUS_PUBLISHED,
1831
                'versionNo' => 2,
1832
            ),
1833
            array(
1834
                'status' => $versionInfo->status,
1835
                'versionNo' => $versionInfo->versionNo,
1836
            )
1837
        );
1838
        $this->assertTrue($versionInfo->isPublished());
1839
        $this->assertFalse($versionInfo->isDraft());
1840
        $this->assertFalse($versionInfo->isArchived());
1841
    }
1842
1843
    /**
1844
     * Test for the publishVersion() method.
1845
     *
1846
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1847
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1848
     */
1849 View Code Duplication
    public function testPublishVersionFromContentDraftArchivesOldVersion()
1850
    {
1851
        $repository = $this->getRepository();
1852
1853
        $contentService = $repository->getContentService();
1854
1855
        /* BEGIN: Use Case */
1856
        $contentVersion2 = $this->createContentVersion2();
1857
        /* END: Use Case */
1858
1859
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo, 1);
1860
1861
        $this->assertEquals(
1862
            array(
1863
                'status' => VersionInfo::STATUS_ARCHIVED,
1864
                'versionNo' => 1,
1865
            ),
1866
            array(
1867
                'status' => $versionInfo->status,
1868
                'versionNo' => $versionInfo->versionNo,
1869
            )
1870
        );
1871
        $this->assertTrue($versionInfo->isArchived());
1872
        $this->assertFalse($versionInfo->isDraft());
1873
        $this->assertFalse($versionInfo->isPublished());
1874
    }
1875
1876
    /**
1877
     * Test for the publishVersion() method.
1878
     *
1879
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1880
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1881
     */
1882
    public function testPublishVersionFromContentDraftUpdatesContentInfoCurrentVersion()
1883
    {
1884
        /* BEGIN: Use Case */
1885
        $contentVersion2 = $this->createContentVersion2();
1886
        /* END: Use Case */
1887
1888
        $this->assertEquals(2, $contentVersion2->contentInfo->currentVersionNo);
1889
    }
1890
1891
    /**
1892
     * Test for the publishVersion() method.
1893
     *
1894
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1895
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1896
     */
1897 View Code Duplication
    public function testPublishVersionFromOldContentDraftArchivesNewerVersionNo()
1898
    {
1899
        $repository = $this->getRepository();
1900
1901
        $contentService = $repository->getContentService();
1902
1903
        /* BEGIN: Use Case */
1904
        $content = $this->createContentVersion1();
1905
1906
        // Create a new draft with versionNo = 2
1907
        $draftedContentVersion2 = $contentService->createContentDraft($content->contentInfo);
1908
1909
        // Create another new draft with versionNo = 3
1910
        $draftedContentVersion3 = $contentService->createContentDraft($content->contentInfo);
1911
1912
        // Publish draft with versionNo = 3
1913
        $contentService->publishVersion($draftedContentVersion3->getVersionInfo());
1914
1915
        // Publish the first draft with versionNo = 2
1916
        // currentVersionNo is now 2, versionNo 3 will be archived
1917
        $publishedDraft = $contentService->publishVersion($draftedContentVersion2->getVersionInfo());
1918
        /* END: Use Case */
1919
1920
        $this->assertEquals(2, $publishedDraft->contentInfo->currentVersionNo);
1921
    }
1922
1923
    /**
1924
     * Test for the publishVersion() method, and that it creates limited archives.
1925
     *
1926
     * @todo Adapt this when per content type archive limited is added on repository Content Type model.
1927
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1928
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1929
     */
1930
    public function testPublishVersionNotCreatingUnlimitedArchives()
1931
    {
1932
        $repository = $this->getRepository();
1933
1934
        $contentService = $repository->getContentService();
1935
1936
        $content = $this->createContentVersion1();
1937
1938
        // load first to make sure list gets updated also (cache)
1939
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
1940
        $this->assertEquals(1, count($versionInfoList));
1941
        $this->assertEquals(1, $versionInfoList[0]->versionNo);
1942
1943
        // Create a new draft with versionNo = 2
1944
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1945
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1946
1947
        // Create a new draft with versionNo = 3
1948
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1949
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1950
1951
        // Create a new draft with versionNo = 4
1952
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1953
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1954
1955
        // Create a new draft with versionNo = 5
1956
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1957
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1958
1959
        // Create a new draft with versionNo = 6
1960
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1961
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1962
1963
        // Create a new draft with versionNo = 7
1964
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1965
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1966
1967
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
1968
1969
        $this->assertEquals(6, count($versionInfoList));
1970
        $this->assertEquals(2, $versionInfoList[0]->versionNo);
1971
        $this->assertEquals(7, $versionInfoList[5]->versionNo);
1972
1973
        $this->assertEquals(
1974
            [
1975
                VersionInfo::STATUS_ARCHIVED,
1976
                VersionInfo::STATUS_ARCHIVED,
1977
                VersionInfo::STATUS_ARCHIVED,
1978
                VersionInfo::STATUS_ARCHIVED,
1979
                VersionInfo::STATUS_ARCHIVED,
1980
                VersionInfo::STATUS_PUBLISHED,
1981
            ],
1982
            [
1983
                $versionInfoList[0]->status,
1984
                $versionInfoList[1]->status,
1985
                $versionInfoList[2]->status,
1986
                $versionInfoList[3]->status,
1987
                $versionInfoList[4]->status,
1988
                $versionInfoList[5]->status,
1989
            ]
1990
        );
1991
    }
1992
1993
    /**
1994
     * Test for the newContentMetadataUpdateStruct() method.
1995
     *
1996
     * @covers \eZ\Publish\API\Repository\ContentService::newContentMetadataUpdateStruct
1997
     * @group user
1998
     */
1999
    public function testNewContentMetadataUpdateStruct()
2000
    {
2001
        $repository = $this->getRepository();
2002
2003
        /* BEGIN: Use Case */
2004
        $contentService = $repository->getContentService();
2005
2006
        // Creates a new metadata update struct
2007
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2008
2009
        foreach ($metadataUpdate as $propertyName => $propertyValue) {
0 ignored issues
show
Bug introduced by
The expression $metadataUpdate of type object<eZ\Publish\API\Re...ntMetadataUpdateStruct> is not traversable.
Loading history...
2010
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
2011
        }
2012
2013
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
2014
        $metadataUpdate->mainLanguageCode = 'eng-GB';
2015
        $metadataUpdate->alwaysAvailable = false;
2016
        /* END: Use Case */
2017
2018
        $this->assertInstanceOf(
2019
            ContentMetadataUpdateStruct::class,
2020
            $metadataUpdate
2021
        );
2022
    }
2023
2024
    /**
2025
     * Test for the updateContentMetadata() method.
2026
     *
2027
     * @return \eZ\Publish\API\Repository\Values\Content\Content
2028
     *
2029
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2030
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2031
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentMetadataUpdateStruct
2032
     * @group user
2033
     */
2034
    public function testUpdateContentMetadata()
2035
    {
2036
        $repository = $this->getRepository();
2037
2038
        $contentService = $repository->getContentService();
2039
2040
        /* BEGIN: Use Case */
2041
        $content = $this->createContentVersion1();
2042
2043
        // Creates a metadata update struct
2044
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2045
2046
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
2047
        $metadataUpdate->mainLanguageCode = 'eng-GB';
2048
        $metadataUpdate->alwaysAvailable = false;
2049
        $metadataUpdate->publishedDate = $this->createDateTime(441759600); // 1984/01/01
2050
        $metadataUpdate->modificationDate = $this->createDateTime(441759600); // 1984/01/01
2051
2052
        // Update the metadata of the published content object
2053
        $content = $contentService->updateContentMetadata(
2054
            $content->contentInfo,
2055
            $metadataUpdate
2056
        );
2057
        /* END: Use Case */
2058
2059
        $this->assertInstanceOf(
2060
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
2061
            $content
2062
        );
2063
2064
        return $content;
2065
    }
2066
2067
    /**
2068
     * Test for the updateContentMetadata() method.
2069
     *
2070
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2071
     *
2072
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2073
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2074
     */
2075
    public function testUpdateContentMetadataSetsExpectedProperties($content)
2076
    {
2077
        $contentInfo = $content->contentInfo;
2078
2079
        $this->assertEquals(
2080
            array(
2081
                'remoteId' => 'aaaabbbbccccddddeeeeffff11112222',
2082
                'sectionId' => $this->generateId('section', 1),
2083
                'alwaysAvailable' => false,
2084
                'currentVersionNo' => 1,
2085
                'mainLanguageCode' => 'eng-GB',
2086
                'modificationDate' => $this->createDateTime(441759600),
2087
                'ownerId' => $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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

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

Loading history...
2088
                'published' => true,
2089
                'publishedDate' => $this->createDateTime(441759600),
2090
            ),
2091
            array(
2092
                'remoteId' => $contentInfo->remoteId,
2093
                'sectionId' => $contentInfo->sectionId,
2094
                'alwaysAvailable' => $contentInfo->alwaysAvailable,
2095
                'currentVersionNo' => $contentInfo->currentVersionNo,
2096
                'mainLanguageCode' => $contentInfo->mainLanguageCode,
2097
                'modificationDate' => $contentInfo->modificationDate,
2098
                'ownerId' => $contentInfo->ownerId,
2099
                'published' => $contentInfo->published,
2100
                'publishedDate' => $contentInfo->publishedDate,
2101
            )
2102
        );
2103
    }
2104
2105
    /**
2106
     * Test for the updateContentMetadata() method.
2107
     *
2108
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2109
     *
2110
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2111
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2112
     */
2113
    public function testUpdateContentMetadataNotUpdatesContentVersion($content)
2114
    {
2115
        $this->assertEquals(1, $content->getVersionInfo()->versionNo);
2116
    }
2117
2118
    /**
2119
     * Test for the updateContentMetadata() method.
2120
     *
2121
     * @covers \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2122
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2123
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2124
     */
2125
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnDuplicateRemoteId()
2126
    {
2127
        $repository = $this->getRepository();
2128
2129
        $contentService = $repository->getContentService();
2130
2131
        /* BEGIN: Use Case */
2132
        // RemoteId of the "Media" page of an eZ Publish demo installation
2133
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2134
2135
        $content = $this->createContentVersion1();
2136
2137
        // Creates a metadata update struct
2138
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2139
        $metadataUpdate->remoteId = $mediaRemoteId;
2140
2141
        // This call will fail with an "InvalidArgumentException", because the
2142
        // specified remoteId is already used by the "Media" page.
2143
        $contentService->updateContentMetadata(
2144
            $content->contentInfo,
2145
            $metadataUpdate
2146
        );
2147
        /* END: Use Case */
2148
    }
2149
2150
    /**
2151
     * Test for the updateContentMetadata() method.
2152
     *
2153
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContentMetadata
2154
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2155
     */
2156
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnNoMetadataPropertiesSet()
2157
    {
2158
        $repository = $this->getRepository();
2159
2160
        $contentService = $repository->getContentService();
2161
2162
        $contentInfo = $contentService->loadContentInfo(4);
2163
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
2164
2165
        // Throws an exception because no properties are set in $contentMetadataUpdateStruct
2166
        $contentService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
2167
    }
2168
2169
    /**
2170
     * Test for the deleteContent() method.
2171
     *
2172
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2173
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2174
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2175
     */
2176 View Code Duplication
    public function testDeleteContent()
2177
    {
2178
        $repository = $this->getRepository();
2179
2180
        $contentService = $repository->getContentService();
2181
        $locationService = $repository->getLocationService();
2182
2183
        /* BEGIN: Use Case */
2184
        $contentVersion2 = $this->createContentVersion2();
2185
2186
        // Load the locations for this content object
2187
        $locations = $locationService->loadLocations($contentVersion2->contentInfo);
2188
2189
        // This will delete the content, all versions and the associated locations
2190
        $contentService->deleteContent($contentVersion2->contentInfo);
2191
        /* END: Use Case */
2192
2193
        foreach ($locations as $location) {
2194
            $locationService->loadLocation($location->id);
2195
        }
2196
    }
2197
2198
    /**
2199
     * Test for the deleteContent() method.
2200
     *
2201
     * Test for issue EZP-21057:
2202
     * "contentService: Unable to delete a content with an empty file attribute"
2203
     *
2204
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2205
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2206
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2207
     */
2208 View Code Duplication
    public function testDeleteContentWithEmptyBinaryField()
2209
    {
2210
        $repository = $this->getRepository();
2211
2212
        $contentService = $repository->getContentService();
2213
        $locationService = $repository->getLocationService();
2214
2215
        /* BEGIN: Use Case */
2216
        $contentVersion = $this->createContentVersion1EmptyBinaryField();
2217
2218
        // Load the locations for this content object
2219
        $locations = $locationService->loadLocations($contentVersion->contentInfo);
2220
2221
        // This will delete the content, all versions and the associated locations
2222
        $contentService->deleteContent($contentVersion->contentInfo);
2223
        /* END: Use Case */
2224
2225
        foreach ($locations as $location) {
2226
            $locationService->loadLocation($location->id);
2227
        }
2228
    }
2229
2230
    /**
2231
     * Test for the loadContentDrafts() method.
2232
     *
2233
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2234
     */
2235
    public function testLoadContentDraftsReturnsEmptyArrayByDefault()
2236
    {
2237
        $repository = $this->getRepository();
2238
2239
        /* BEGIN: Use Case */
2240
        $contentService = $repository->getContentService();
2241
2242
        $contentDrafts = $contentService->loadContentDrafts();
2243
        /* END: Use Case */
2244
2245
        $this->assertSame(array(), $contentDrafts);
2246
    }
2247
2248
    /**
2249
     * Test for the loadContentDrafts() method.
2250
     *
2251
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2252
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2253
     */
2254
    public function testLoadContentDrafts()
2255
    {
2256
        $repository = $this->getRepository();
2257
2258
        /* BEGIN: Use Case */
2259
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
2260
        // of a eZ Publish demo installation.
2261
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2262
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
2263
2264
        $contentService = $repository->getContentService();
2265
2266
        // "Media" content object
2267
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2268
2269
        // "eZ Publish Demo Design ..." content object
2270
        $demoDesignContentInfo = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
2271
2272
        // Create some drafts
2273
        $contentService->createContentDraft($mediaContentInfo);
2274
        $contentService->createContentDraft($demoDesignContentInfo);
2275
2276
        // Now $contentDrafts should contain two drafted versions
2277
        $draftedVersions = $contentService->loadContentDrafts();
2278
        /* END: Use Case */
2279
2280
        $actual = array(
2281
            $draftedVersions[0]->status,
2282
            $draftedVersions[0]->getContentInfo()->remoteId,
2283
            $draftedVersions[1]->status,
2284
            $draftedVersions[1]->getContentInfo()->remoteId,
2285
        );
2286
        sort($actual, SORT_STRING);
2287
2288
        $this->assertEquals(
2289
            array(
2290
                VersionInfo::STATUS_DRAFT,
2291
                VersionInfo::STATUS_DRAFT,
2292
                $demoDesignRemoteId,
2293
                $mediaRemoteId,
2294
            ),
2295
            $actual
2296
        );
2297
    }
2298
2299
    /**
2300
     * Test for the loadContentDrafts() method.
2301
     *
2302
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
2303
     */
2304
    public function testLoadContentDraftsWithFirstParameter()
2305
    {
2306
        $repository = $this->getRepository();
2307
2308
        /* BEGIN: Use Case */
2309
        $user = $this->createUserVersion1();
2310
2311
        // Get current user
2312
        $oldCurrentUser = $repository->getCurrentUser();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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

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

Loading history...
2313
2314
        // Set new editor as user
2315
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
2316
2317
        // Remote id of the "Media" content object in an eZ Publish demo installation.
2318
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2319
2320
        $contentService = $repository->getContentService();
2321
2322
        // "Media" content object
2323
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2324
2325
        // Create a content draft
2326
        $contentService->createContentDraft($mediaContentInfo);
2327
2328
        // Reset to previous current user
2329
        $repository->setCurrentUser($oldCurrentUser);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
2330
2331
        // Now $contentDrafts for the previous current user and the new user
2332
        $newCurrentUserDrafts = $contentService->loadContentDrafts($user);
2333
        $oldCurrentUserDrafts = $contentService->loadContentDrafts($oldCurrentUser);
2334
        /* END: Use Case */
2335
2336
        $this->assertSame(array(), $oldCurrentUserDrafts);
2337
2338
        $this->assertEquals(
2339
            array(
2340
                VersionInfo::STATUS_DRAFT,
2341
                $mediaRemoteId,
2342
            ),
2343
            array(
2344
                $newCurrentUserDrafts[0]->status,
2345
                $newCurrentUserDrafts[0]->getContentInfo()->remoteId,
2346
            )
2347
        );
2348
        $this->assertTrue($newCurrentUserDrafts[0]->isDraft());
2349
        $this->assertFalse($newCurrentUserDrafts[0]->isArchived());
2350
        $this->assertFalse($newCurrentUserDrafts[0]->isPublished());
2351
    }
2352
2353
    /**
2354
     * Test for the loadVersionInfo() method.
2355
     *
2356
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2357
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2358
     */
2359
    public function testLoadVersionInfoWithSecondParameter()
2360
    {
2361
        $repository = $this->getRepository();
2362
2363
        $contentService = $repository->getContentService();
2364
2365
        /* BEGIN: Use Case */
2366
        $publishedContent = $this->createContentVersion1();
2367
2368
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
0 ignored issues
show
Unused Code introduced by
$draftContent is not used, you could remove the assignment.

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

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

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

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

Loading history...
2369
2370
        // Will return the VersionInfo of the $draftContent
2371
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2372
        /* END: Use Case */
2373
2374
        $this->assertEquals(2, $versionInfo->versionNo);
2375
2376
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2377
        $this->assertEquals(
2378
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2379
            $versionInfo->getContentInfo()->mainLocationId
2380
        );
2381
    }
2382
2383
    /**
2384
     * Test for the loadVersionInfo() method.
2385
     *
2386
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2387
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2388
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2389
     */
2390
    public function testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter()
2391
    {
2392
        $repository = $this->getRepository();
2393
2394
        $contentService = $repository->getContentService();
2395
2396
        /* BEGIN: Use Case */
2397
        $draft = $this->createContentDraftVersion1();
2398
2399
        // This call will fail with a "NotFoundException", because not versionNo
2400
        // 2 exists for this content object.
2401
        $contentService->loadVersionInfo($draft->contentInfo, 2);
2402
        /* END: Use Case */
2403
    }
2404
2405
    /**
2406
     * Test for the loadVersionInfoById() method.
2407
     *
2408
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2409
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2410
     */
2411
    public function testLoadVersionInfoByIdWithSecondParameter()
2412
    {
2413
        $repository = $this->getRepository();
2414
2415
        $contentService = $repository->getContentService();
2416
2417
        /* BEGIN: Use Case */
2418
        $publishedContent = $this->createContentVersion1();
2419
2420
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
2421
2422
        // Will return the VersionInfo of the $draftContent
2423
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2424
        /* END: Use Case */
2425
2426
        $this->assertEquals(2, $versionInfo->versionNo);
2427
2428
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2429
        $this->assertEquals(
2430
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2431
            $versionInfo->getContentInfo()->mainLocationId
2432
        );
2433
2434
        return [
2435
            'versionInfo' => $versionInfo,
2436
            'draftContent' => $draftContent,
2437
        ];
2438
    }
2439
2440
    /**
2441
     * Test for the returned value of the loadVersionInfoById() method.
2442
     *
2443
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter
2444
     * @covers \eZ\Publish\API\Repository\ContentService::loadVersionInfoById
2445
     *
2446
     * @param array $data
2447
     */
2448
    public function testLoadVersionInfoByIdWithSecondParameterSetsExpectedVersionInfo(array $data)
2449
    {
2450
        /** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo */
2451
        $versionInfo = $data['versionInfo'];
2452
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $draftContent */
2453
        $draftContent = $data['draftContent'];
2454
2455
        $this->assertPropertiesCorrect(
2456
            [
2457
                'names' => [
2458
                    'eng-US' => 'An awesome forum',
2459
                ],
2460
                'contentInfo' => new ContentInfo([
2461
                    'id' => $draftContent->contentInfo->id,
2462
                    'contentTypeId' => 28,
2463
                    'name' => 'An awesome forum',
2464
                    'sectionId' => 1,
2465
                    'currentVersionNo' => 1,
2466
                    'published' => true,
2467
                    'ownerId' => 14,
2468
                    // this Content Object is created at the test runtime
2469
                    'modificationDate' => $versionInfo->contentInfo->modificationDate,
2470
                    'publishedDate' => $versionInfo->contentInfo->publishedDate,
2471
                    'alwaysAvailable' => 1,
2472
                    'remoteId' => 'abcdef0123456789abcdef0123456789',
2473
                    'mainLanguageCode' => 'eng-US',
2474
                    'mainLocationId' => $draftContent->contentInfo->mainLocationId,
2475
                ]),
2476
                'id' => $draftContent->versionInfo->id,
2477
                'versionNo' => 2,
2478
                'creatorId' => 14,
2479
                'status' => 0,
2480
                'initialLanguageCode' => 'eng-US',
2481
                'languageCodes' => [
2482
                    'eng-US',
2483
                ],
2484
            ],
2485
            $versionInfo
2486
        );
2487
    }
2488
2489
    /**
2490
     * Test for the loadVersionInfoById() method.
2491
     *
2492
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2493
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2494
     */
2495 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter()
2496
    {
2497
        $repository = $this->getRepository();
2498
2499
        $contentService = $repository->getContentService();
2500
2501
        /* BEGIN: Use Case */
2502
        $content = $this->createContentVersion1();
2503
2504
        // This call will fail with a "NotFoundException", because not versionNo
2505
        // 2 exists for this content object.
2506
        $contentService->loadVersionInfoById($content->id, 2);
2507
        /* END: Use Case */
2508
    }
2509
2510
    /**
2511
     * Test for the loadContentByVersionInfo() method.
2512
     *
2513
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
2514
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2515
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
2516
     */
2517
    public function testLoadContentByVersionInfoWithSecondParameter()
2518
    {
2519
        $repository = $this->getRepository();
2520
2521
        $sectionId = $this->generateId('section', 1);
2522
        /* BEGIN: Use Case */
2523
        $contentTypeService = $repository->getContentTypeService();
2524
2525
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2526
2527
        $contentService = $repository->getContentService();
2528
2529
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2530
2531
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2532
2533
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2534
2535
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2536
        // $sectionId contains the ID of section 1
2537
        $contentCreateStruct->sectionId = $sectionId;
2538
        $contentCreateStruct->alwaysAvailable = true;
2539
2540
        // Create a new content draft
2541
        $content = $contentService->createContent($contentCreateStruct);
2542
2543
        // Now publish this draft
2544
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2545
2546
        // Will return a content instance with fields in "eng-US"
2547
        $reloadedContent = $contentService->loadContentByVersionInfo(
2548
            $publishedContent->getVersionInfo(),
2549
            array(
2550
                'eng-GB',
2551
            ),
2552
            false
2553
        );
2554
        /* END: Use Case */
2555
2556
        $actual = array();
2557 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...
2558
            $actual[] = new Field(
2559
                array(
2560
                    'id' => 0,
2561
                    'value' => ($field->value !== null ? true : null), // Actual value tested by FieldType integration tests
2562
                    'languageCode' => $field->languageCode,
2563
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
2564
                )
2565
            );
2566
        }
2567
        usort(
2568
            $actual,
2569 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...
2570
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
2571
                    return strcasecmp($field1->languageCode, $field2->languageCode);
2572
                }
2573
2574
                return $return;
2575
            }
2576
        );
2577
2578
        $expected = array(
2579
            new Field(
2580
                array(
2581
                    'id' => 0,
2582
                    'value' => true,
2583
                    'languageCode' => 'eng-GB',
2584
                    'fieldDefIdentifier' => 'description',
2585
                )
2586
            ),
2587
            new Field(
2588
                array(
2589
                    'id' => 0,
2590
                    'value' => true,
2591
                    'languageCode' => 'eng-GB',
2592
                    'fieldDefIdentifier' => 'name',
2593
                )
2594
            ),
2595
        );
2596
2597
        $this->assertEquals($expected, $actual);
2598
    }
2599
2600
    /**
2601
     * Test for the loadContentByContentInfo() method.
2602
     *
2603
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages)
2604
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2605
     */
2606
    public function testLoadContentByContentInfoWithLanguageParameters()
2607
    {
2608
        $repository = $this->getRepository();
2609
2610
        $sectionId = $this->generateId('section', 1);
2611
        /* BEGIN: Use Case */
2612
        $contentTypeService = $repository->getContentTypeService();
2613
2614
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2615
2616
        $contentService = $repository->getContentService();
2617
2618
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2619
2620
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2621
2622
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2623
2624
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2625
        // $sectionId contains the ID of section 1
2626
        $contentCreateStruct->sectionId = $sectionId;
2627
        $contentCreateStruct->alwaysAvailable = true;
2628
2629
        // Create a new content draft
2630
        $content = $contentService->createContent($contentCreateStruct);
2631
2632
        // Now publish this draft
2633
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2634
2635
        // Will return a content instance with fields in "eng-US"
2636
        $reloadedContent = $contentService->loadContentByContentInfo(
2637
            $publishedContent->contentInfo,
2638
            array(
2639
                'eng-US',
2640
            ),
2641
            null,
2642
            false
2643
        );
2644
        /* END: Use Case */
2645
2646
        $actual = $this->normalizeFields($reloadedContent->getFields());
2647
2648
        $expected = array(
2649
            new Field(
2650
                array(
2651
                    'id' => 0,
2652
                    'value' => true,
2653
                    'languageCode' => 'eng-US',
2654
                    'fieldDefIdentifier' => 'description',
2655
                    'fieldTypeIdentifier' => 'ezrichtext',
2656
                )
2657
            ),
2658
            new Field(
2659
                array(
2660
                    'id' => 0,
2661
                    'value' => true,
2662
                    'languageCode' => 'eng-US',
2663
                    'fieldDefIdentifier' => 'name',
2664
                    'fieldTypeIdentifier' => 'ezstring',
2665
                )
2666
            ),
2667
        );
2668
2669
        $this->assertEquals($expected, $actual);
2670
2671
        // Will return a content instance with fields in "eng-GB" (versions prior to 6.0.0-beta9 returned "eng-US" also)
2672
        $reloadedContent = $contentService->loadContentByContentInfo(
2673
            $publishedContent->contentInfo,
2674
            array(
2675
                'eng-GB',
2676
            ),
2677
            null,
2678
            true
2679
        );
2680
2681
        $actual = $this->normalizeFields($reloadedContent->getFields());
2682
2683
        $expected = array(
2684
            new Field(
2685
                array(
2686
                    'id' => 0,
2687
                    'value' => true,
2688
                    'languageCode' => 'eng-GB',
2689
                    'fieldDefIdentifier' => 'description',
2690
                    'fieldTypeIdentifier' => 'ezrichtext',
2691
                )
2692
            ),
2693
            new Field(
2694
                array(
2695
                    'id' => 0,
2696
                    'value' => true,
2697
                    'languageCode' => 'eng-GB',
2698
                    'fieldDefIdentifier' => 'name',
2699
                    'fieldTypeIdentifier' => 'ezstring',
2700
                )
2701
            ),
2702
        );
2703
2704
        $this->assertEquals($expected, $actual);
2705
2706
        // Will return a content instance with fields in main language "eng-US", as "fre-FR" does not exists
2707
        $reloadedContent = $contentService->loadContentByContentInfo(
2708
            $publishedContent->contentInfo,
2709
            array(
2710
                'fre-FR',
2711
            ),
2712
            null,
2713
            true
2714
        );
2715
2716
        $actual = $this->normalizeFields($reloadedContent->getFields());
2717
2718
        $expected = array(
2719
            new Field(
2720
                array(
2721
                    'id' => 0,
2722
                    'value' => true,
2723
                    'languageCode' => 'eng-US',
2724
                    'fieldDefIdentifier' => 'description',
2725
                    'fieldTypeIdentifier' => 'ezrichtext',
2726
                )
2727
            ),
2728
            new Field(
2729
                array(
2730
                    'id' => 0,
2731
                    'value' => true,
2732
                    'languageCode' => 'eng-US',
2733
                    'fieldDefIdentifier' => 'name',
2734
                    'fieldTypeIdentifier' => 'ezstring',
2735
                )
2736
            ),
2737
        );
2738
2739
        $this->assertEquals($expected, $actual);
2740
    }
2741
2742
    /**
2743
     * Test for the loadContentByContentInfo() method.
2744
     *
2745
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2746
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2747
     */
2748 View Code Duplication
    public function testLoadContentByContentInfoWithVersionNumberParameter()
2749
    {
2750
        $repository = $this->getRepository();
2751
2752
        $contentService = $repository->getContentService();
2753
2754
        /* BEGIN: Use Case */
2755
        $publishedContent = $this->createContentVersion1();
2756
2757
        $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...
2758
2759
        // This content instance is identical to $draftContent
2760
        $draftContentReloaded = $contentService->loadContentByContentInfo(
2761
            $publishedContent->contentInfo,
2762
            null,
2763
            2
2764
        );
2765
        /* END: Use Case */
2766
2767
        $this->assertEquals(
2768
            2,
2769
            $draftContentReloaded->getVersionInfo()->versionNo
2770
        );
2771
2772
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2773
        $this->assertEquals(
2774
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2775
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2776
        );
2777
    }
2778
2779
    /**
2780
     * Test for the loadContentByContentInfo() method.
2781
     *
2782
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2783
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2784
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithVersionNumberParameter
2785
     */
2786 View Code Duplication
    public function testLoadContentByContentInfoThrowsNotFoundExceptionWithVersionNumberParameter()
2787
    {
2788
        $repository = $this->getRepository();
2789
2790
        $contentService = $repository->getContentService();
2791
2792
        /* BEGIN: Use Case */
2793
        $content = $this->createContentVersion1();
2794
2795
        // This call will fail with a "NotFoundException", because no content
2796
        // with versionNo = 2 exists.
2797
        $contentService->loadContentByContentInfo($content->contentInfo, null, 2);
2798
        /* END: Use Case */
2799
    }
2800
2801
    /**
2802
     * Test for the loadContent() method.
2803
     *
2804
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages)
2805
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2806
     */
2807 View Code Duplication
    public function testLoadContentWithSecondParameter()
2808
    {
2809
        $repository = $this->getRepository();
2810
2811
        $contentService = $repository->getContentService();
2812
2813
        /* BEGIN: Use Case */
2814
        $draft = $this->createMultipleLanguageDraftVersion1();
2815
2816
        // This draft contains those fields localized with "eng-GB"
2817
        $draftLocalized = $contentService->loadContent($draft->id, array('eng-GB'), null, false);
2818
        /* END: Use Case */
2819
2820
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2821
2822
        return $draft;
2823
    }
2824
2825
    /**
2826
     * Test for the loadContent() method using undefined translation.
2827
     *
2828
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithSecondParameter
2829
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2830
     *
2831
     * @param \eZ\Publish\API\Repository\Values\Content\Content $contentDraft
2832
     */
2833
    public function testLoadContentWithSecondParameterThrowsNotFoundException(Content $contentDraft)
2834
    {
2835
        $repository = $this->getRepository();
2836
2837
        $contentService = $repository->getContentService();
2838
2839
        $contentService->loadContent($contentDraft->id, array('ger-DE'), null, false);
2840
    }
2841
2842
    /**
2843
     * Test for the loadContent() method.
2844
     *
2845
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2846
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2847
     */
2848 View Code Duplication
    public function testLoadContentWithThirdParameter()
2849
    {
2850
        $repository = $this->getRepository();
2851
2852
        $contentService = $repository->getContentService();
2853
2854
        /* BEGIN: Use Case */
2855
        $publishedContent = $this->createContentVersion1();
2856
2857
        $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...
2858
2859
        // This content instance is identical to $draftContent
2860
        $draftContentReloaded = $contentService->loadContent($publishedContent->id, null, 2);
2861
        /* END: Use Case */
2862
2863
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2864
2865
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2866
        $this->assertEquals(
2867
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2868
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2869
        );
2870
    }
2871
2872
    /**
2873
     * Test for the loadContent() method.
2874
     *
2875
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2876
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2877
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithThirdParameter
2878
     */
2879 View Code Duplication
    public function testLoadContentThrowsNotFoundExceptionWithThirdParameter()
2880
    {
2881
        $repository = $this->getRepository();
2882
2883
        $contentService = $repository->getContentService();
2884
2885
        /* BEGIN: Use Case */
2886
        $content = $this->createContentVersion1();
2887
2888
        // This call will fail with a "NotFoundException", because for this
2889
        // content object no versionNo=2 exists.
2890
        $contentService->loadContent($content->id, null, 2);
2891
        /* END: Use Case */
2892
    }
2893
2894
    /**
2895
     * Test for the loadContentByRemoteId() method.
2896
     *
2897
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages)
2898
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2899
     */
2900 View Code Duplication
    public function testLoadContentByRemoteIdWithSecondParameter()
2901
    {
2902
        $repository = $this->getRepository();
2903
2904
        $contentService = $repository->getContentService();
2905
2906
        /* BEGIN: Use Case */
2907
        $draft = $this->createMultipleLanguageDraftVersion1();
2908
2909
        $contentService->publishVersion($draft->versionInfo);
2910
2911
        // This draft contains those fields localized with "eng-GB"
2912
        $draftLocalized = $contentService->loadContentByRemoteId(
2913
            $draft->contentInfo->remoteId,
2914
            array('eng-GB'),
2915
            null,
2916
            false
2917
        );
2918
        /* END: Use Case */
2919
2920
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2921
    }
2922
2923
    /**
2924
     * Test for the loadContentByRemoteId() method.
2925
     *
2926
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2927
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2928
     */
2929 View Code Duplication
    public function testLoadContentByRemoteIdWithThirdParameter()
2930
    {
2931
        $repository = $this->getRepository();
2932
2933
        $contentService = $repository->getContentService();
2934
2935
        /* BEGIN: Use Case */
2936
        $publishedContent = $this->createContentVersion1();
2937
2938
        $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...
2939
2940
        // This content instance is identical to $draftContent
2941
        $draftContentReloaded = $contentService->loadContentByRemoteId(
2942
            $publishedContent->contentInfo->remoteId,
2943
            null,
2944
            2
2945
        );
2946
        /* END: Use Case */
2947
2948
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2949
2950
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2951
        $this->assertEquals(
2952
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2953
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2954
        );
2955
    }
2956
2957
    /**
2958
     * Test for the loadContentByRemoteId() method.
2959
     *
2960
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2961
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2962
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithThirdParameter
2963
     */
2964
    public function testLoadContentByRemoteIdThrowsNotFoundExceptionWithThirdParameter()
2965
    {
2966
        $repository = $this->getRepository();
2967
2968
        $contentService = $repository->getContentService();
2969
2970
        /* BEGIN: Use Case */
2971
        $content = $this->createContentVersion1();
2972
2973
        // This call will fail with a "NotFoundException", because for this
2974
        // content object no versionNo=2 exists.
2975
        $contentService->loadContentByRemoteId(
2976
            $content->contentInfo->remoteId,
2977
            null,
2978
            2
2979
        );
2980
        /* END: Use Case */
2981
    }
2982
2983
    /**
2984
     * Test that retrieval of translated name field respects prioritized language list.
2985
     *
2986
     * @dataProvider getPrioritizedLanguageList
2987
     * @param string[]|null $languageCodes
2988
     */
2989
    public function testLoadContentWithPrioritizedLanguagesList($languageCodes)
2990
    {
2991
        $repository = $this->getRepository();
2992
2993
        $contentService = $repository->getContentService();
2994
2995
        $content = $this->createContentVersion2();
2996
2997
        $content = $contentService->loadContent($content->id, $languageCodes);
2998
2999
        $expectedName = $content->getVersionInfo()->getName(
3000
            isset($languageCodes[0]) ? $languageCodes[0] : null
3001
        );
3002
        $nameValue = $content->getFieldValue('name');
3003
        /** @var \eZ\Publish\Core\FieldType\TextLine\Value $nameValue */
3004
        self::assertEquals($expectedName, $nameValue->text);
3005
        self::assertEquals($expectedName, $content->getVersionInfo()->getName());
3006
        // Also check value on shortcut method on content
3007
        self::assertEquals($expectedName, $content->getName());
3008
    }
3009
3010
    /**
3011
     * @return array
3012
     */
3013
    public function getPrioritizedLanguageList()
3014
    {
3015
        return [
3016
            [['eng-US']],
3017
            [['eng-GB']],
3018
            [['eng-GB', 'eng-US']],
3019
            [['eng-US', 'eng-GB']],
3020
        ];
3021
    }
3022
3023
    /**
3024
     * Test for the deleteVersion() method.
3025
     *
3026
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
3027
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3028
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3029
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3030
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
3031
     */
3032
    public function testDeleteVersion()
3033
    {
3034
        $repository = $this->getRepository();
3035
3036
        $contentService = $repository->getContentService();
3037
3038
        /* BEGIN: Use Case */
3039
        $content = $this->createContentVersion1();
3040
3041
        // Create new draft, because published or last version of the Content can't be deleted
3042
        $draft = $contentService->createContentDraft(
3043
            $content->getVersionInfo()->getContentInfo()
3044
        );
3045
3046
        // Delete the previously created draft
3047
        $contentService->deleteVersion($draft->getVersionInfo());
3048
        /* END: Use Case */
3049
3050
        $versions = $contentService->loadVersions($content->getVersionInfo()->getContentInfo());
3051
3052
        $this->assertCount(1, $versions);
3053
        $this->assertEquals(
3054
            $content->getVersionInfo()->id,
3055
            $versions[0]->id
3056
        );
3057
    }
3058
3059
    /**
3060
     * Test for the deleteVersion() method.
3061
     *
3062
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
3063
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3064
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3065
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3066
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3067
     */
3068
    public function testDeleteVersionThrowsBadStateExceptionOnPublishedVersion()
3069
    {
3070
        $repository = $this->getRepository();
3071
3072
        $contentService = $repository->getContentService();
3073
3074
        /* BEGIN: Use Case */
3075
        $content = $this->createContentVersion1();
3076
3077
        // This call will fail with a "BadStateException", because the content
3078
        // version is currently published.
3079
        $contentService->deleteVersion($content->getVersionInfo());
3080
        /* END: Use Case */
3081
    }
3082
3083
    /**
3084
     * Test for the deleteVersion() method.
3085
     *
3086
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
3087
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3088
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3089
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3090
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3091
     */
3092 View Code Duplication
    public function testDeleteVersionThrowsBadStateExceptionOnLastVersion()
3093
    {
3094
        $repository = $this->getRepository();
3095
3096
        $contentService = $repository->getContentService();
3097
3098
        /* BEGIN: Use Case */
3099
        $draft = $this->createContentDraftVersion1();
3100
3101
        // This call will fail with a "BadStateException", because the Content
3102
        // version is the last version of the Content.
3103
        $contentService->deleteVersion($draft->getVersionInfo());
3104
        /* END: Use Case */
3105
    }
3106
3107
    /**
3108
     * Test for the loadVersions() method.
3109
     *
3110
     * @see \eZ\Publish\API\Repository\ContentService::loadVersions()
3111
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3112
     *
3113
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[]
3114
     */
3115
    public function testLoadVersions()
3116
    {
3117
        $repository = $this->getRepository();
3118
3119
        $contentService = $repository->getContentService();
3120
3121
        /* BEGIN: Use Case */
3122
        $contentVersion2 = $this->createContentVersion2();
3123
3124
        // Load versions of this ContentInfo instance
3125
        $versions = $contentService->loadVersions($contentVersion2->contentInfo);
3126
        /* END: Use Case */
3127
3128
        $expectedVersionsOrder = [
3129
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1),
3130
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 2),
3131
        ];
3132
3133
        $this->assertEquals($expectedVersionsOrder, $versions);
3134
3135
        return $versions;
3136
    }
3137
3138
    /**
3139
     * Test for the loadVersions() method.
3140
     *
3141
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersions
3142
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersions
3143
     *
3144
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo[] $versions
3145
     */
3146
    public function testLoadVersionsSetsExpectedVersionInfo(array $versions)
3147
    {
3148
        $this->assertCount(2, $versions);
3149
3150
        $expectedVersions = [
3151
            [
3152
                'versionNo' => 1,
3153
                'creatorId' => 14,
3154
                'status' => VersionInfo::STATUS_ARCHIVED,
3155
                'initialLanguageCode' => 'eng-US',
3156
                'languageCodes' => ['eng-US'],
3157
            ],
3158
            [
3159
                'versionNo' => 2,
3160
                'creatorId' => 10,
3161
                'status' => VersionInfo::STATUS_PUBLISHED,
3162
                'initialLanguageCode' => 'eng-US',
3163
                'languageCodes' => ['eng-US', 'eng-GB'],
3164
            ],
3165
        ];
3166
3167
        $this->assertPropertiesCorrect($expectedVersions[0], $versions[0]);
3168
        $this->assertPropertiesCorrect($expectedVersions[1], $versions[1]);
3169
        $this->assertEquals(
3170
            $versions[0]->creationDate->getTimestamp(),
3171
            $versions[1]->creationDate->getTimestamp(),
3172
            'Creation time did not match within delta of 2 seconds',
3173
            2
3174
        );
3175
        $this->assertEquals(
3176
            $versions[0]->modificationDate->getTimestamp(),
3177
            $versions[1]->modificationDate->getTimestamp(),
3178
            'Creation time did not match within delta of 2 seconds',
3179
            2
3180
        );
3181
        $this->assertTrue($versions[0]->isArchived());
3182
        $this->assertFalse($versions[0]->isDraft());
3183
        $this->assertFalse($versions[0]->isPublished());
3184
3185
        $this->assertTrue($versions[1]->isPublished());
3186
        $this->assertFalse($versions[1]->isDraft());
3187
        $this->assertFalse($versions[1]->isArchived());
3188
    }
3189
3190
    /**
3191
     * Test for the copyContent() method.
3192
     *
3193
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
3194
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3195
     * @group field-type
3196
     */
3197 View Code Duplication
    public function testCopyContent()
3198
    {
3199
        $parentLocationId = $this->generateId('location', 56);
3200
3201
        $repository = $this->getRepository();
3202
3203
        $contentService = $repository->getContentService();
3204
        $locationService = $repository->getLocationService();
3205
3206
        /* BEGIN: Use Case */
3207
        $contentVersion2 = $this->createMultipleLanguageContentVersion2();
3208
3209
        // Configure new target location
3210
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
3211
3212
        $targetLocationCreate->priority = 42;
3213
        $targetLocationCreate->hidden = true;
3214
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3215
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3216
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3217
3218
        // Copy content with all versions and drafts
3219
        $contentCopied = $contentService->copyContent(
3220
            $contentVersion2->contentInfo,
3221
            $targetLocationCreate
3222
        );
3223
        /* END: Use Case */
3224
3225
        $this->assertInstanceOf(
3226
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
3227
            $contentCopied
3228
        );
3229
3230
        $this->assertNotEquals(
3231
            $contentVersion2->contentInfo->remoteId,
3232
            $contentCopied->contentInfo->remoteId
3233
        );
3234
3235
        $this->assertNotEquals(
3236
            $contentVersion2->id,
3237
            $contentCopied->id
3238
        );
3239
3240
        $this->assertEquals(
3241
            2,
3242
            count($contentService->loadVersions($contentCopied->contentInfo))
3243
        );
3244
3245
        $this->assertEquals(2, $contentCopied->getVersionInfo()->versionNo);
3246
3247
        $this->assertAllFieldsEquals($contentCopied->getFields());
3248
3249
        $this->assertDefaultContentStates($contentCopied->contentInfo);
3250
3251
        $this->assertNotNull(
3252
            $contentCopied->contentInfo->mainLocationId,
3253
            'Expected main location to be set given we provided a LocationCreateStruct'
3254
        );
3255
    }
3256
3257
    /**
3258
     * Test for the copyContent() method.
3259
     *
3260
     * @see \eZ\Publish\API\Repository\ContentService::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo)
3261
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
3262
     *
3263
     * @todo Fix to more descriptive name
3264
     */
3265 View Code Duplication
    public function testCopyContentWithThirdParameter()
3266
    {
3267
        $parentLocationId = $this->generateId('location', 56);
3268
3269
        $repository = $this->getRepository();
3270
3271
        $contentService = $repository->getContentService();
3272
        $locationService = $repository->getLocationService();
3273
3274
        /* BEGIN: Use Case */
3275
        $contentVersion2 = $this->createContentVersion2();
3276
3277
        // Configure new target location
3278
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
3279
3280
        $targetLocationCreate->priority = 42;
3281
        $targetLocationCreate->hidden = true;
3282
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3283
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3284
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3285
3286
        // Copy only the initial version
3287
        $contentCopied = $contentService->copyContent(
3288
            $contentVersion2->contentInfo,
3289
            $targetLocationCreate,
3290
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1)
3291
        );
3292
        /* END: Use Case */
3293
3294
        $this->assertInstanceOf(
3295
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
3296
            $contentCopied
3297
        );
3298
3299
        $this->assertNotEquals(
3300
            $contentVersion2->contentInfo->remoteId,
3301
            $contentCopied->contentInfo->remoteId
3302
        );
3303
3304
        $this->assertNotEquals(
3305
            $contentVersion2->id,
3306
            $contentCopied->id
3307
        );
3308
3309
        $this->assertEquals(
3310
            1,
3311
            count($contentService->loadVersions($contentCopied->contentInfo))
3312
        );
3313
3314
        $this->assertEquals(1, $contentCopied->getVersionInfo()->versionNo);
3315
3316
        $this->assertNotNull(
3317
            $contentCopied->contentInfo->mainLocationId,
3318
            'Expected main location to be set given we provided a LocationCreateStruct'
3319
        );
3320
    }
3321
3322
    /**
3323
     * Test for the addRelation() method.
3324
     *
3325
     * @return \eZ\Publish\API\Repository\Values\Content\Content
3326
     *
3327
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3328
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3329
     */
3330
    public function testAddRelation()
3331
    {
3332
        $repository = $this->getRepository();
3333
3334
        $contentService = $repository->getContentService();
3335
3336
        /* BEGIN: Use Case */
3337
        // RemoteId of the "Media" content of an eZ Publish demo installation
3338
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3339
3340
        $draft = $this->createContentDraftVersion1();
3341
3342
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3343
3344
        // Create relation between new content object and "Media" page
3345
        $relation = $contentService->addRelation(
3346
            $draft->getVersionInfo(),
3347
            $media
3348
        );
3349
        /* END: Use Case */
3350
3351
        $this->assertInstanceOf(
3352
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Relation',
3353
            $relation
3354
        );
3355
3356
        return $contentService->loadRelations($draft->getVersionInfo());
3357
    }
3358
3359
    /**
3360
     * Test for the addRelation() method.
3361
     *
3362
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3363
     *
3364
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3365
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3366
     */
3367
    public function testAddRelationAddsRelationToContent($relations)
3368
    {
3369
        $this->assertEquals(
3370
            1,
3371
            count($relations)
3372
        );
3373
    }
3374
3375
    /**
3376
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3377
     */
3378
    protected function assertExpectedRelations($relations)
3379
    {
3380
        $this->assertEquals(
3381
            array(
3382
                'type' => Relation::COMMON,
3383
                'sourceFieldDefinitionIdentifier' => null,
3384
                'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3385
                'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3386
            ),
3387
            array(
3388
                'type' => $relations[0]->type,
3389
                'sourceFieldDefinitionIdentifier' => $relations[0]->sourceFieldDefinitionIdentifier,
3390
                'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3391
                'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3392
            )
3393
        );
3394
    }
3395
3396
    /**
3397
     * Test for the addRelation() method.
3398
     *
3399
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3400
     *
3401
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3402
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3403
     */
3404
    public function testAddRelationSetsExpectedRelations($relations)
3405
    {
3406
        $this->assertExpectedRelations($relations);
3407
    }
3408
3409
    /**
3410
     * Test for the createContentDraft() method.
3411
     *
3412
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3413
     *
3414
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
3415
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelationSetsExpectedRelations
3416
     */
3417 View Code Duplication
    public function testCreateContentDraftWithRelations()
3418
    {
3419
        $repository = $this->getRepository();
3420
3421
        $contentService = $repository->getContentService();
3422
3423
        // RemoteId of the "Media" content of an eZ Publish demo installation
3424
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3425
        $draft = $this->createContentDraftVersion1();
3426
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3427
3428
        // Create relation between new content object and "Media" page
3429
        $contentService->addRelation(
3430
            $draft->getVersionInfo(),
3431
            $media
3432
        );
3433
3434
        $content = $contentService->publishVersion($draft->versionInfo);
3435
        $newDraft = $contentService->createContentDraft($content->contentInfo);
3436
3437
        return $contentService->loadRelations($newDraft->getVersionInfo());
3438
    }
3439
3440
    /**
3441
     * Test for the createContentDraft() method.
3442
     *
3443
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3444
     *
3445
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3446
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelations
3447
     */
3448
    public function testCreateContentDraftWithRelationsCreatesRelations($relations)
3449
    {
3450
        $this->assertEquals(
3451
            1,
3452
            count($relations)
3453
        );
3454
3455
        return $relations;
3456
    }
3457
3458
    /**
3459
     * Test for the createContentDraft() method.
3460
     *
3461
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3462
     *
3463
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelationsCreatesRelations
3464
     */
3465
    public function testCreateContentDraftWithRelationsCreatesExpectedRelations($relations)
3466
    {
3467
        $this->assertExpectedRelations($relations);
3468
    }
3469
3470
    /**
3471
     * Test for the addRelation() method.
3472
     *
3473
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3474
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3475
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3476
     */
3477 View Code Duplication
    public function testAddRelationThrowsBadStateException()
3478
    {
3479
        $repository = $this->getRepository();
3480
3481
        $contentService = $repository->getContentService();
3482
3483
        /* BEGIN: Use Case */
3484
        // RemoteId of the "Media" page of an eZ Publish demo installation
3485
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3486
3487
        $content = $this->createContentVersion1();
3488
3489
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3490
3491
        // This call will fail with a "BadStateException", because content is
3492
        // published and not a draft.
3493
        $contentService->addRelation(
3494
            $content->getVersionInfo(),
3495
            $media
3496
        );
3497
        /* END: Use Case */
3498
    }
3499
3500
    /**
3501
     * Test for the loadRelations() method.
3502
     *
3503
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3504
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3505
     */
3506
    public function testLoadRelations()
3507
    {
3508
        $repository = $this->getRepository();
3509
3510
        $contentService = $repository->getContentService();
3511
3512
        /* BEGIN: Use Case */
3513
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3514
        // of a eZ Publish demo installation.
3515
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3516
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3517
3518
        $draft = $this->createContentDraftVersion1();
3519
3520
        // Load other content objects
3521
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3522
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3523
3524
        // Create relation between new content object and "Media" page
3525
        $contentService->addRelation(
3526
            $draft->getVersionInfo(),
3527
            $media
3528
        );
3529
3530
        // Create another relation with the "Demo Design" page
3531
        $contentService->addRelation(
3532
            $draft->getVersionInfo(),
3533
            $demoDesign
3534
        );
3535
3536
        // Load all relations
3537
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3538
        /* END: Use Case */
3539
3540
        usort(
3541
            $relations,
3542
            function ($rel1, $rel2) {
3543
                return strcasecmp(
3544
                    $rel2->getDestinationContentInfo()->remoteId,
3545
                    $rel1->getDestinationContentInfo()->remoteId
3546
                );
3547
            }
3548
        );
3549
3550
        $this->assertEquals(
3551
            array(
3552
                array(
3553
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3554
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3555
                ),
3556
                array(
3557
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3558
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3559
                ),
3560
            ),
3561
            array(
3562
                array(
3563
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3564
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3565
                ),
3566
                array(
3567
                    'sourceContentInfo' => $relations[1]->sourceContentInfo->remoteId,
3568
                    'destinationContentInfo' => $relations[1]->destinationContentInfo->remoteId,
3569
                ),
3570
            )
3571
        );
3572
    }
3573
3574
    /**
3575
     * Test for the loadRelations() method.
3576
     *
3577
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3578
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3579
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3580
     */
3581
    public function testLoadRelationsSkipsArchivedContent()
3582
    {
3583
        $repository = $this->getRepository();
3584
3585
        $contentService = $repository->getContentService();
3586
3587
        /* BEGIN: Use Case */
3588
        $trashService = $repository->getTrashService();
3589
        $locationService = $repository->getLocationService();
3590
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3591
        // of a eZ Publish demo installation.
3592
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3593
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3594
3595
        $draft = $this->createContentDraftVersion1();
3596
3597
        // Load other content objects
3598
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3599
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3600
3601
        // Create relation between new content object and "Media" page
3602
        $contentService->addRelation(
3603
            $draft->getVersionInfo(),
3604
            $media
3605
        );
3606
3607
        // Create another relation with the "Demo Design" page
3608
        $contentService->addRelation(
3609
            $draft->getVersionInfo(),
3610
            $demoDesign
3611
        );
3612
3613
        $demoDesignLocation = $locationService->loadLocation($demoDesign->mainLocationId);
3614
3615
        // Trashing Content's last Location will change its status to archived,
3616
        // in this case relation towards it will not be loaded.
3617
        $trashService->trash($demoDesignLocation);
3618
3619
        // Load all relations
3620
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3621
        /* END: Use Case */
3622
3623
        $this->assertCount(1, $relations);
3624
        $this->assertEquals(
3625
            array(
3626
                array(
3627
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3628
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3629
                ),
3630
            ),
3631
            array(
3632
                array(
3633
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3634
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3635
                ),
3636
            )
3637
        );
3638
    }
3639
3640
    /**
3641
     * Test for the loadRelations() method.
3642
     *
3643
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3644
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3645
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3646
     */
3647
    public function testLoadRelationsSkipsDraftContent()
3648
    {
3649
        $repository = $this->getRepository();
3650
3651
        $contentService = $repository->getContentService();
3652
3653
        /* BEGIN: Use Case */
3654
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3655
        // of a eZ Publish demo installation.
3656
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3657
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3658
3659
        $draft = $this->createContentDraftVersion1();
3660
3661
        // Load other content objects
3662
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3663
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3664
3665
        // Create draft of "Media" page
3666
        $mediaDraft = $contentService->createContentDraft($media->contentInfo);
3667
3668
        // Create relation between "Media" page and new content object draft.
3669
        // This relation will not be loaded before the draft is published.
3670
        $contentService->addRelation(
3671
            $mediaDraft->getVersionInfo(),
3672
            $draft->getVersionInfo()->getContentInfo()
3673
        );
3674
3675
        // Create another relation with the "Demo Design" page
3676
        $contentService->addRelation(
3677
            $mediaDraft->getVersionInfo(),
3678
            $demoDesign
3679
        );
3680
3681
        // Load all relations
3682
        $relations = $contentService->loadRelations($mediaDraft->getVersionInfo());
3683
        /* END: Use Case */
3684
3685
        $this->assertCount(1, $relations);
3686
        $this->assertEquals(
3687
            array(
3688
                array(
3689
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3690
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3691
                ),
3692
            ),
3693
            array(
3694
                array(
3695
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3696
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3697
                ),
3698
            )
3699
        );
3700
    }
3701
3702
    /**
3703
     * Test for the loadReverseRelations() method.
3704
     *
3705
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3706
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3707
     */
3708
    public function testLoadReverseRelations()
3709
    {
3710
        $repository = $this->getRepository();
3711
3712
        $contentService = $repository->getContentService();
3713
3714
        /* BEGIN: Use Case */
3715
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3716
        // of a eZ Publish demo installation.
3717
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3718
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3719
3720
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3721
        $contentInfo = $versionInfo->getContentInfo();
3722
3723
        // Create some drafts
3724
        $mediaDraft = $contentService->createContentDraft(
3725
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3726
        );
3727
        $demoDesignDraft = $contentService->createContentDraft(
3728
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3729
        );
3730
3731
        // Create relation between new content object and "Media" page
3732
        $relation1 = $contentService->addRelation(
3733
            $mediaDraft->getVersionInfo(),
3734
            $contentInfo
3735
        );
3736
3737
        // Create another relation with the "Demo Design" page
3738
        $relation2 = $contentService->addRelation(
3739
            $demoDesignDraft->getVersionInfo(),
3740
            $contentInfo
3741
        );
3742
3743
        // Publish drafts, so relations become active
3744
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3745
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3746
3747
        // Load all relations
3748
        $relations = $contentService->loadRelations($versionInfo);
3749
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3750
        /* END: Use Case */
3751
3752
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3753
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3754
3755
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3756
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3757
3758
        $this->assertEquals(0, count($relations));
3759
        $this->assertEquals(2, count($reverseRelations));
3760
3761
        usort(
3762
            $reverseRelations,
3763
            function ($rel1, $rel2) {
3764
                return strcasecmp(
3765
                    $rel2->getSourceContentInfo()->remoteId,
3766
                    $rel1->getSourceContentInfo()->remoteId
3767
                );
3768
            }
3769
        );
3770
3771
        $this->assertEquals(
3772
            array(
3773
                array(
3774
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3775
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3776
                ),
3777
                array(
3778
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3779
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3780
                ),
3781
            ),
3782
            array(
3783
                array(
3784
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3785
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3786
                ),
3787
                array(
3788
                    'sourceContentInfo' => $reverseRelations[1]->sourceContentInfo->remoteId,
3789
                    'destinationContentInfo' => $reverseRelations[1]->destinationContentInfo->remoteId,
3790
                ),
3791
            )
3792
        );
3793
    }
3794
3795
    /**
3796
     * Test for the loadReverseRelations() method.
3797
     *
3798
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3799
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3800
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3801
     */
3802
    public function testLoadReverseRelationsSkipsArchivedContent()
3803
    {
3804
        $repository = $this->getRepository();
3805
3806
        $contentService = $repository->getContentService();
3807
3808
        /* BEGIN: Use Case */
3809
        $trashService = $repository->getTrashService();
3810
        $locationService = $repository->getLocationService();
3811
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3812
        // of a eZ Publish demo installation.
3813
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3814
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3815
3816
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3817
        $contentInfo = $versionInfo->getContentInfo();
3818
3819
        // Create some drafts
3820
        $mediaDraft = $contentService->createContentDraft(
3821
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3822
        );
3823
        $demoDesignDraft = $contentService->createContentDraft(
3824
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3825
        );
3826
3827
        // Create relation between new content object and "Media" page
3828
        $relation1 = $contentService->addRelation(
3829
            $mediaDraft->getVersionInfo(),
3830
            $contentInfo
3831
        );
3832
3833
        // Create another relation with the "Demo Design" page
3834
        $relation2 = $contentService->addRelation(
3835
            $demoDesignDraft->getVersionInfo(),
3836
            $contentInfo
3837
        );
3838
3839
        // Publish drafts, so relations become active
3840
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3841
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3842
3843
        $demoDesignLocation = $locationService->loadLocation($demoDesignDraft->contentInfo->mainLocationId);
3844
3845
        // Trashing Content's last Location will change its status to archived,
3846
        // in this case relation from it will not be loaded.
3847
        $trashService->trash($demoDesignLocation);
3848
3849
        // Load all relations
3850
        $relations = $contentService->loadRelations($versionInfo);
3851
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3852
        /* END: Use Case */
3853
3854
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3855
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3856
3857
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3858
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3859
3860
        $this->assertEquals(0, count($relations));
3861
        $this->assertEquals(1, count($reverseRelations));
3862
3863
        $this->assertEquals(
3864
            array(
3865
                array(
3866
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3867
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3868
                ),
3869
            ),
3870
            array(
3871
                array(
3872
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3873
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3874
                ),
3875
            )
3876
        );
3877
    }
3878
3879
    /**
3880
     * Test for the loadReverseRelations() method.
3881
     *
3882
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3883
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3884
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3885
     */
3886
    public function testLoadReverseRelationsSkipsDraftContent()
3887
    {
3888
        $repository = $this->getRepository();
3889
3890
        $contentService = $repository->getContentService();
3891
3892
        /* BEGIN: Use Case */
3893
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3894
        // of a eZ Publish demo installation.
3895
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3896
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3897
3898
        // Load "Media" page Content
3899
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3900
3901
        // Create some drafts
3902
        $newDraftVersionInfo = $this->createContentDraftVersion1()->getVersionInfo();
3903
        $demoDesignDraft = $contentService->createContentDraft(
3904
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3905
        );
3906
3907
        // Create relation between "Media" page and new content object
3908
        $relation1 = $contentService->addRelation(
3909
            $newDraftVersionInfo,
3910
            $media->contentInfo
3911
        );
3912
3913
        // Create another relation with the "Demo Design" page
3914
        $relation2 = $contentService->addRelation(
3915
            $demoDesignDraft->getVersionInfo(),
3916
            $media->contentInfo
3917
        );
3918
3919
        // Publish drafts, so relations become active
3920
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3921
        // We will not publish new Content draft, therefore relation from it
3922
        // will not be loaded as reverse relation for "Media" page
3923
        //$contentService->publishVersion( $newDraftVersionInfo );
3924
3925
        // Load all relations
3926
        $relations = $contentService->loadRelations($media->versionInfo);
3927
        $reverseRelations = $contentService->loadReverseRelations($media->contentInfo);
3928
        /* END: Use Case */
3929
3930
        $this->assertEquals($media->contentInfo->id, $relation1->getDestinationContentInfo()->id);
3931
        $this->assertEquals($newDraftVersionInfo->contentInfo->id, $relation1->getSourceContentInfo()->id);
3932
3933
        $this->assertEquals($media->contentInfo->id, $relation2->getDestinationContentInfo()->id);
3934
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3935
3936
        $this->assertEquals(0, count($relations));
3937
        $this->assertEquals(1, count($reverseRelations));
3938
3939
        $this->assertEquals(
3940
            array(
3941
                array(
3942
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3943
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3944
                ),
3945
            ),
3946
            array(
3947
                array(
3948
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3949
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3950
                ),
3951
            )
3952
        );
3953
    }
3954
3955
    /**
3956
     * Test for the deleteRelation() method.
3957
     *
3958
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3959
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3960
     */
3961
    public function testDeleteRelation()
3962
    {
3963
        $repository = $this->getRepository();
3964
3965
        $contentService = $repository->getContentService();
3966
3967
        /* BEGIN: Use Case */
3968
        // Remote ids of the "Media" and the "Demo Design" page of a eZ Publish
3969
        // demo installation.
3970
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3971
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3972
3973
        $draft = $this->createContentDraftVersion1();
3974
3975
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3976
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3977
3978
        // Establish some relations
3979
        $contentService->addRelation($draft->getVersionInfo(), $media);
3980
        $contentService->addRelation($draft->getVersionInfo(), $demoDesign);
3981
3982
        // Delete one of the currently created relations
3983
        $contentService->deleteRelation($draft->getVersionInfo(), $media);
3984
3985
        // The relations array now contains only one element
3986
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3987
        /* END: Use Case */
3988
3989
        $this->assertEquals(1, count($relations));
3990
    }
3991
3992
    /**
3993
     * Test for the deleteRelation() method.
3994
     *
3995
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3996
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3997
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3998
     */
3999 View Code Duplication
    public function testDeleteRelationThrowsBadStateException()
4000
    {
4001
        $repository = $this->getRepository();
4002
4003
        $contentService = $repository->getContentService();
4004
4005
        /* BEGIN: Use Case */
4006
        // RemoteId of the "Media" page of an eZ Publish demo installation
4007
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
4008
4009
        $content = $this->createContentVersion1();
4010
4011
        // Load the destination object
4012
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
4013
4014
        // Create a new draft
4015
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
4016
4017
        // Add a relation
4018
        $contentService->addRelation($draftVersion2->getVersionInfo(), $media);
4019
4020
        // Publish new version
4021
        $contentVersion2 = $contentService->publishVersion(
4022
            $draftVersion2->getVersionInfo()
4023
        );
4024
4025
        // This call will fail with a "BadStateException", because content is
4026
        // published and not a draft.
4027
        $contentService->deleteRelation(
4028
            $contentVersion2->getVersionInfo(),
4029
            $media
4030
        );
4031
        /* END: Use Case */
4032
    }
4033
4034
    /**
4035
     * Test for the deleteRelation() method.
4036
     *
4037
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
4038
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
4039
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
4040
     */
4041 View Code Duplication
    public function testDeleteRelationThrowsInvalidArgumentException()
4042
    {
4043
        $repository = $this->getRepository();
4044
4045
        $contentService = $repository->getContentService();
4046
4047
        /* BEGIN: Use Case */
4048
        // RemoteId of the "Media" page of an eZ Publish demo installation
4049
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
4050
4051
        $draft = $this->createContentDraftVersion1();
4052
4053
        // Load the destination object
4054
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
4055
4056
        // This call will fail with a "InvalidArgumentException", because no
4057
        // relation exists between $draft and $media.
4058
        $contentService->deleteRelation(
4059
            $draft->getVersionInfo(),
4060
            $media
4061
        );
4062
        /* END: Use Case */
4063
    }
4064
4065
    /**
4066
     * Test for the createContent() method.
4067
     *
4068
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
4069
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4070
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4071
     */
4072
    public function testCreateContentInTransactionWithRollback()
4073
    {
4074
        if ($this->isVersion4()) {
4075
            $this->markTestSkipped('This test requires eZ Publish 5');
4076
        }
4077
4078
        $repository = $this->getRepository();
4079
4080
        /* BEGIN: Use Case */
4081
        $contentTypeService = $repository->getContentTypeService();
4082
        $contentService = $repository->getContentService();
4083
4084
        // Start a transaction
4085
        $repository->beginTransaction();
4086
4087
        try {
4088
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
4089
4090
            // Get a content create struct and set mandatory properties
4091
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
4092
            $contentCreate->setField('name', 'Sindelfingen forum');
4093
4094
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
4095
            $contentCreate->alwaysAvailable = true;
4096
4097
            // Create a new content object
4098
            $contentId = $contentService->createContent($contentCreate)->id;
4099
        } catch (Exception $e) {
4100
            // Cleanup hanging transaction on error
4101
            $repository->rollback();
4102
            throw $e;
4103
        }
4104
4105
        // Rollback all changes
4106
        $repository->rollback();
4107
4108
        try {
4109
            // This call will fail with a "NotFoundException"
4110
            $contentService->loadContent($contentId);
4111
        } catch (NotFoundException $e) {
4112
            // This is expected
4113
            return;
4114
        }
4115
        /* END: Use Case */
4116
4117
        $this->fail('Content object still exists after rollback.');
4118
    }
4119
4120
    /**
4121
     * Test for the createContent() method.
4122
     *
4123
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
4124
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4125
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4126
     */
4127
    public function testCreateContentInTransactionWithCommit()
4128
    {
4129
        if ($this->isVersion4()) {
4130
            $this->markTestSkipped('This test requires eZ Publish 5');
4131
        }
4132
4133
        $repository = $this->getRepository();
4134
4135
        /* BEGIN: Use Case */
4136
        $contentTypeService = $repository->getContentTypeService();
4137
        $contentService = $repository->getContentService();
4138
4139
        // Start a transaction
4140
        $repository->beginTransaction();
4141
4142
        try {
4143
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
4144
4145
            // Get a content create struct and set mandatory properties
4146
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
4147
            $contentCreate->setField('name', 'Sindelfingen forum');
4148
4149
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
4150
            $contentCreate->alwaysAvailable = true;
4151
4152
            // Create a new content object
4153
            $contentId = $contentService->createContent($contentCreate)->id;
4154
4155
            // Commit changes
4156
            $repository->commit();
4157
        } catch (Exception $e) {
4158
            // Cleanup hanging transaction on error
4159
            $repository->rollback();
4160
            throw $e;
4161
        }
4162
4163
        // Load the new content object
4164
        $content = $contentService->loadContent($contentId);
4165
        /* END: Use Case */
4166
4167
        $this->assertEquals($contentId, $content->id);
4168
    }
4169
4170
    /**
4171
     * Test for the createContent() method.
4172
     *
4173
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
4174
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
4175
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
4176
     */
4177
    public function testCreateContentWithLocationCreateParameterInTransactionWithRollback()
4178
    {
4179
        $repository = $this->getRepository();
4180
4181
        $contentService = $repository->getContentService();
4182
4183
        /* BEGIN: Use Case */
4184
        // Start a transaction
4185
        $repository->beginTransaction();
4186
4187
        try {
4188
            $draft = $this->createContentDraftVersion1();
4189
        } catch (Exception $e) {
4190
            // Cleanup hanging transaction on error
4191
            $repository->rollback();
4192
            throw $e;
4193
        }
4194
4195
        $contentId = $draft->id;
4196
4197
        // Roleback the transaction
4198
        $repository->rollback();
4199
4200
        try {
4201
            // This call will fail with a "NotFoundException"
4202
            $contentService->loadContent($contentId);
4203
        } catch (NotFoundException $e) {
4204
            return;
4205
        }
4206
        /* END: Use Case */
4207
4208
        $this->fail('Can still load content object after rollback.');
4209
    }
4210
4211
    /**
4212
     * Test for the createContent() method.
4213
     *
4214
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
4215
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
4216
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
4217
     */
4218 View Code Duplication
    public function testCreateContentWithLocationCreateParameterInTransactionWithCommit()
4219
    {
4220
        $repository = $this->getRepository();
4221
4222
        $contentService = $repository->getContentService();
4223
4224
        /* BEGIN: Use Case */
4225
        // Start a transaction
4226
        $repository->beginTransaction();
4227
4228
        try {
4229
            $draft = $this->createContentDraftVersion1();
4230
4231
            $contentId = $draft->id;
4232
4233
            // Roleback the transaction
4234
            $repository->commit();
4235
        } catch (Exception $e) {
4236
            // Cleanup hanging transaction on error
4237
            $repository->rollback();
4238
            throw $e;
4239
        }
4240
4241
        // Load the new content object
4242
        $content = $contentService->loadContent($contentId);
4243
        /* END: Use Case */
4244
4245
        $this->assertEquals($contentId, $content->id);
4246
    }
4247
4248
    /**
4249
     * Test for the createContentDraft() method.
4250
     *
4251
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4252
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4253
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4254
     */
4255
    public function testCreateContentDraftInTransactionWithRollback()
4256
    {
4257
        $repository = $this->getRepository();
4258
4259
        $contentId = $this->generateId('object', 12);
4260
        /* BEGIN: Use Case */
4261
        // $contentId is the ID of the "Administrator users" user group
4262
4263
        // Get the content service
4264
        $contentService = $repository->getContentService();
4265
4266
        // Load the user group content object
4267
        $content = $contentService->loadContent($contentId);
4268
4269
        // Start a new transaction
4270
        $repository->beginTransaction();
4271
4272
        try {
4273
            // Create a new draft
4274
            $drafted = $contentService->createContentDraft($content->contentInfo);
4275
4276
            // Store version number for later reuse
4277
            $versionNo = $drafted->versionInfo->versionNo;
4278
        } catch (Exception $e) {
4279
            // Cleanup hanging transaction on error
4280
            $repository->rollback();
4281
            throw $e;
4282
        }
4283
4284
        // Rollback
4285
        $repository->rollback();
4286
4287
        try {
4288
            // This call will fail with a "NotFoundException"
4289
            $contentService->loadContent($contentId, null, $versionNo);
4290
        } catch (NotFoundException $e) {
4291
            return;
4292
        }
4293
        /* END: Use Case */
4294
4295
        $this->fail('Can still load content draft after rollback');
4296
    }
4297
4298
    /**
4299
     * Test for the createContentDraft() method.
4300
     *
4301
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4302
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4303
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4304
     */
4305 View Code Duplication
    public function testCreateContentDraftInTransactionWithCommit()
4306
    {
4307
        $repository = $this->getRepository();
4308
4309
        $contentId = $this->generateId('object', 12);
4310
        /* BEGIN: Use Case */
4311
        // $contentId is the ID of the "Administrator users" user group
4312
4313
        // Get the content service
4314
        $contentService = $repository->getContentService();
4315
4316
        // Load the user group content object
4317
        $content = $contentService->loadContent($contentId);
4318
4319
        // Start a new transaction
4320
        $repository->beginTransaction();
4321
4322
        try {
4323
            // Create a new draft
4324
            $drafted = $contentService->createContentDraft($content->contentInfo);
4325
4326
            // Store version number for later reuse
4327
            $versionNo = $drafted->versionInfo->versionNo;
4328
4329
            // Commit all changes
4330
            $repository->commit();
4331
        } catch (Exception $e) {
4332
            // Cleanup hanging transaction on error
4333
            $repository->rollback();
4334
            throw $e;
4335
        }
4336
4337
        $content = $contentService->loadContent($contentId, null, $versionNo);
4338
        /* END: Use Case */
4339
4340
        $this->assertEquals(
4341
            $versionNo,
4342
            $content->getVersionInfo()->versionNo
4343
        );
4344
    }
4345
4346
    /**
4347
     * Test for the publishVersion() method.
4348
     *
4349
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4350
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4351
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4352
     */
4353 View Code Duplication
    public function testPublishVersionInTransactionWithRollback()
4354
    {
4355
        $repository = $this->getRepository();
4356
4357
        $contentId = $this->generateId('object', 12);
4358
        /* BEGIN: Use Case */
4359
        // $contentId is the ID of the "Administrator users" user group
4360
4361
        // Get the content service
4362
        $contentService = $repository->getContentService();
4363
4364
        // Load the user group content object
4365
        $content = $contentService->loadContent($contentId);
4366
4367
        // Start a new transaction
4368
        $repository->beginTransaction();
4369
4370
        try {
4371
            $draftVersion = $contentService->createContentDraft($content->contentInfo)->getVersionInfo();
4372
4373
            // Publish a new version
4374
            $content = $contentService->publishVersion($draftVersion);
4375
4376
            // Store version number for later reuse
4377
            $versionNo = $content->versionInfo->versionNo;
4378
        } catch (Exception $e) {
4379
            // Cleanup hanging transaction on error
4380
            $repository->rollback();
4381
            throw $e;
4382
        }
4383
4384
        // Rollback
4385
        $repository->rollback();
4386
4387
        try {
4388
            // This call will fail with a "NotFoundException"
4389
            $contentService->loadContent($contentId, null, $versionNo);
4390
        } catch (NotFoundException $e) {
4391
            return;
4392
        }
4393
        /* END: Use Case */
4394
4395
        $this->fail('Can still load content draft after rollback');
4396
    }
4397
4398
    /**
4399
     * Test for the publishVersion() method.
4400
     *
4401
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4402
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4403
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
4404
     */
4405 View Code Duplication
    public function testPublishVersionInTransactionWithCommit()
4406
    {
4407
        $repository = $this->getRepository();
4408
4409
        /* BEGIN: Use Case */
4410
        // ID of the "Administrator users" user group
4411
        $contentId = 12;
4412
4413
        // Get the content service
4414
        $contentService = $repository->getContentService();
4415
4416
        // Load the user group content object
4417
        $template = $contentService->loadContent($contentId);
4418
4419
        // Start a new transaction
4420
        $repository->beginTransaction();
4421
4422
        try {
4423
            // Publish a new version
4424
            $content = $contentService->publishVersion(
4425
                $contentService->createContentDraft($template->contentInfo)->getVersionInfo()
4426
            );
4427
4428
            // Store version number for later reuse
4429
            $versionNo = $content->versionInfo->versionNo;
4430
4431
            // Commit all changes
4432
            $repository->commit();
4433
        } catch (Exception $e) {
4434
            // Cleanup hanging transaction on error
4435
            $repository->rollback();
4436
            throw $e;
4437
        }
4438
4439
        // Load current version info
4440
        $versionInfo = $contentService->loadVersionInfo($content->contentInfo);
4441
        /* END: Use Case */
4442
4443
        $this->assertEquals($versionNo, $versionInfo->versionNo);
4444
    }
4445
4446
    /**
4447
     * Test for the updateContent() method.
4448
     *
4449
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4450
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4451
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4452
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4453
     */
4454 View Code Duplication
    public function testUpdateContentInTransactionWithRollback()
4455
    {
4456
        $repository = $this->getRepository();
4457
4458
        $contentId = $this->generateId('object', 12);
4459
        /* BEGIN: Use Case */
4460
        // $contentId is the ID of the "Administrator users" user group
4461
4462
        // Load content service
4463
        $contentService = $repository->getContentService();
4464
4465
        // Create a new user group draft
4466
        $draft = $contentService->createContentDraft(
4467
            $contentService->loadContentInfo($contentId)
4468
        );
4469
4470
        // Get an update struct and change the group name
4471
        $contentUpdate = $contentService->newContentUpdateStruct();
4472
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4473
4474
        // Start a transaction
4475
        $repository->beginTransaction();
4476
4477
        try {
4478
            // Update the group name
4479
            $draft = $contentService->updateContent(
4480
                $draft->getVersionInfo(),
4481
                $contentUpdate
4482
            );
4483
4484
            // Publish updated version
4485
            $contentService->publishVersion($draft->getVersionInfo());
4486
        } catch (Exception $e) {
4487
            // Cleanup hanging transaction on error
4488
            $repository->rollback();
4489
            throw $e;
4490
        }
4491
4492
        // Rollback all changes.
4493
        $repository->rollback();
4494
4495
        // Name will still be "Administrator users"
4496
        $name = $contentService->loadContent($contentId)->getFieldValue('name');
4497
        /* END: Use Case */
4498
4499
        $this->assertEquals('Administrator users', $name);
4500
    }
4501
4502
    /**
4503
     * Test for the updateContent() method.
4504
     *
4505
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4506
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4507
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4508
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4509
     */
4510 View Code Duplication
    public function testUpdateContentInTransactionWithCommit()
4511
    {
4512
        $repository = $this->getRepository();
4513
4514
        $contentId = $this->generateId('object', 12);
4515
        /* BEGIN: Use Case */
4516
        // $contentId is the ID of the "Administrator users" user group
4517
4518
        // Load content service
4519
        $contentService = $repository->getContentService();
4520
4521
        // Create a new user group draft
4522
        $draft = $contentService->createContentDraft(
4523
            $contentService->loadContentInfo($contentId)
4524
        );
4525
4526
        // Get an update struct and change the group name
4527
        $contentUpdate = $contentService->newContentUpdateStruct();
4528
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4529
4530
        // Start a transaction
4531
        $repository->beginTransaction();
4532
4533
        try {
4534
            // Update the group name
4535
            $draft = $contentService->updateContent(
4536
                $draft->getVersionInfo(),
4537
                $contentUpdate
4538
            );
4539
4540
            // Publish updated version
4541
            $contentService->publishVersion($draft->getVersionInfo());
4542
4543
            // Commit all changes.
4544
            $repository->commit();
4545
        } catch (Exception $e) {
4546
            // Cleanup hanging transaction on error
4547
            $repository->rollback();
4548
            throw $e;
4549
        }
4550
4551
        // Name is now "Administrators"
4552
        $name = $contentService->loadContent($contentId)->getFieldValue('name', 'eng-US');
4553
        /* END: Use Case */
4554
4555
        $this->assertEquals('Administrators', $name);
4556
    }
4557
4558
    /**
4559
     * Test for the updateContentMetadata() method.
4560
     *
4561
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4562
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4563
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4564
     */
4565 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithRollback()
4566
    {
4567
        $repository = $this->getRepository();
4568
4569
        $contentId = $this->generateId('object', 12);
4570
        /* BEGIN: Use Case */
4571
        // $contentId is the ID of the "Administrator users" user group
4572
4573
        // Get the content service
4574
        $contentService = $repository->getContentService();
4575
4576
        // Load a ContentInfo object
4577
        $contentInfo = $contentService->loadContentInfo($contentId);
4578
4579
        // Store remoteId for later testing
4580
        $remoteId = $contentInfo->remoteId;
4581
4582
        // Start a transaction
4583
        $repository->beginTransaction();
4584
4585
        try {
4586
            // Get metadata update struct and change remoteId
4587
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4588
            $metadataUpdate->remoteId = md5(microtime(true));
4589
4590
            // Update the metadata of the published content object
4591
            $contentService->updateContentMetadata(
4592
                $contentInfo,
4593
                $metadataUpdate
4594
            );
4595
        } catch (Exception $e) {
4596
            // Cleanup hanging transaction on error
4597
            $repository->rollback();
4598
            throw $e;
4599
        }
4600
4601
        // Rollback all changes.
4602
        $repository->rollback();
4603
4604
        // Load current remoteId
4605
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4606
        /* END: Use Case */
4607
4608
        $this->assertEquals($remoteId, $remoteIdReloaded);
4609
    }
4610
4611
    /**
4612
     * Test for the updateContentMetadata() method.
4613
     *
4614
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4615
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4616
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4617
     */
4618 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithCommit()
4619
    {
4620
        $repository = $this->getRepository();
4621
4622
        $contentId = $this->generateId('object', 12);
4623
        /* BEGIN: Use Case */
4624
        // $contentId is the ID of the "Administrator users" user group
4625
4626
        // Get the content service
4627
        $contentService = $repository->getContentService();
4628
4629
        // Load a ContentInfo object
4630
        $contentInfo = $contentService->loadContentInfo($contentId);
4631
4632
        // Store remoteId for later testing
4633
        $remoteId = $contentInfo->remoteId;
4634
4635
        // Start a transaction
4636
        $repository->beginTransaction();
4637
4638
        try {
4639
            // Get metadata update struct and change remoteId
4640
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4641
            $metadataUpdate->remoteId = md5(microtime(true));
4642
4643
            // Update the metadata of the published content object
4644
            $contentService->updateContentMetadata(
4645
                $contentInfo,
4646
                $metadataUpdate
4647
            );
4648
4649
            // Commit all changes.
4650
            $repository->commit();
4651
        } catch (Exception $e) {
4652
            // Cleanup hanging transaction on error
4653
            $repository->rollback();
4654
            throw $e;
4655
        }
4656
4657
        // Load current remoteId
4658
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4659
        /* END: Use Case */
4660
4661
        $this->assertNotEquals($remoteId, $remoteIdReloaded);
4662
    }
4663
4664
    /**
4665
     * Test for the deleteVersion() method.
4666
     *
4667
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4668
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4669
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4670
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4671
     */
4672
    public function testDeleteVersionInTransactionWithRollback()
4673
    {
4674
        $repository = $this->getRepository();
4675
4676
        $contentId = $this->generateId('object', 12);
4677
        /* BEGIN: Use Case */
4678
        // $contentId is the ID of the "Administrator users" user group
4679
4680
        // Get the content service
4681
        $contentService = $repository->getContentService();
4682
4683
        // Start a new transaction
4684
        $repository->beginTransaction();
4685
4686
        try {
4687
            // Create a new draft
4688
            $draft = $contentService->createContentDraft(
4689
                $contentService->loadContentInfo($contentId)
4690
            );
4691
4692
            $contentService->deleteVersion($draft->getVersionInfo());
4693
        } catch (Exception $e) {
4694
            // Cleanup hanging transaction on error
4695
            $repository->rollback();
4696
            throw $e;
4697
        }
4698
4699
        // Rollback all changes.
4700
        $repository->rollback();
4701
4702
        // This array will be empty
4703
        $drafts = $contentService->loadContentDrafts();
4704
        /* END: Use Case */
4705
4706
        $this->assertSame(array(), $drafts);
4707
    }
4708
4709
    /**
4710
     * Test for the deleteVersion() method.
4711
     *
4712
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4713
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4714
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4715
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4716
     */
4717
    public function testDeleteVersionInTransactionWithCommit()
4718
    {
4719
        $repository = $this->getRepository();
4720
4721
        $contentId = $this->generateId('object', 12);
4722
        /* BEGIN: Use Case */
4723
        // $contentId is the ID of the "Administrator users" user group
4724
4725
        // Get the content service
4726
        $contentService = $repository->getContentService();
4727
4728
        // Start a new transaction
4729
        $repository->beginTransaction();
4730
4731
        try {
4732
            // Create a new draft
4733
            $draft = $contentService->createContentDraft(
4734
                $contentService->loadContentInfo($contentId)
4735
            );
4736
4737
            $contentService->deleteVersion($draft->getVersionInfo());
4738
4739
            // Commit all changes.
4740
            $repository->commit();
4741
        } catch (Exception $e) {
4742
            // Cleanup hanging transaction on error
4743
            $repository->rollback();
4744
            throw $e;
4745
        }
4746
4747
        // This array will contain no element
4748
        $drafts = $contentService->loadContentDrafts();
4749
        /* END: Use Case */
4750
4751
        $this->assertSame(array(), $drafts);
4752
    }
4753
4754
    /**
4755
     * Test for the deleteContent() method.
4756
     *
4757
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4758
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4759
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4760
     */
4761
    public function testDeleteContentInTransactionWithRollback()
4762
    {
4763
        $repository = $this->getRepository();
4764
4765
        $contentId = $this->generateId('object', 11);
4766
        /* BEGIN: Use Case */
4767
        // $contentId is the ID of the "Members" user group in an eZ Publish
4768
        // demo installation
4769
4770
        // Get content service
4771
        $contentService = $repository->getContentService();
4772
4773
        // Load a ContentInfo instance
4774
        $contentInfo = $contentService->loadContentInfo($contentId);
4775
4776
        // Start a new transaction
4777
        $repository->beginTransaction();
4778
4779
        try {
4780
            // Delete content object
4781
            $contentService->deleteContent($contentInfo);
4782
        } catch (Exception $e) {
4783
            // Cleanup hanging transaction on error
4784
            $repository->rollback();
4785
            throw $e;
4786
        }
4787
4788
        // Rollback all changes
4789
        $repository->rollback();
4790
4791
        // This call will return the original content object
4792
        $contentInfo = $contentService->loadContentInfo($contentId);
4793
        /* END: Use Case */
4794
4795
        $this->assertEquals($contentId, $contentInfo->id);
4796
    }
4797
4798
    /**
4799
     * Test for the deleteContent() method.
4800
     *
4801
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4802
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4803
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4804
     */
4805
    public function testDeleteContentInTransactionWithCommit()
4806
    {
4807
        $repository = $this->getRepository();
4808
4809
        $contentId = $this->generateId('object', 11);
4810
        /* BEGIN: Use Case */
4811
        // $contentId is the ID of the "Members" user group in an eZ Publish
4812
        // demo installation
4813
4814
        // Get content service
4815
        $contentService = $repository->getContentService();
4816
4817
        // Load a ContentInfo instance
4818
        $contentInfo = $contentService->loadContentInfo($contentId);
4819
4820
        // Start a new transaction
4821
        $repository->beginTransaction();
4822
4823
        try {
4824
            // Delete content object
4825
            $contentService->deleteContent($contentInfo);
4826
4827
            // Commit all changes
4828
            $repository->commit();
4829
        } catch (Exception $e) {
4830
            // Cleanup hanging transaction on error
4831
            $repository->rollback();
4832
            throw $e;
4833
        }
4834
4835
        // Deleted content info is not found anymore
4836
        try {
4837
            $contentService->loadContentInfo($contentId);
4838
        } catch (NotFoundException $e) {
4839
            return;
4840
        }
4841
        /* END: Use Case */
4842
4843
        $this->fail('Can still load ContentInfo after commit.');
4844
    }
4845
4846
    /**
4847
     * Test for the copyContent() method.
4848
     *
4849
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4850
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4851
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4852
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4853
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4854
     */
4855 View Code Duplication
    public function testCopyContentInTransactionWithRollback()
4856
    {
4857
        $repository = $this->getRepository();
4858
4859
        $contentId = $this->generateId('object', 11);
4860
        $locationId = $this->generateId('location', 13);
4861
        /* BEGIN: Use Case */
4862
        // $contentId is the ID of the "Members" user group in an eZ Publish
4863
        // demo installation
4864
4865
        // $locationId is the ID of the "Administrator users" group location
4866
4867
        // Get services
4868
        $contentService = $repository->getContentService();
4869
        $locationService = $repository->getLocationService();
4870
4871
        // Load content object to copy
4872
        $content = $contentService->loadContent($contentId);
4873
4874
        // Create new target location
4875
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4876
4877
        // Start a new transaction
4878
        $repository->beginTransaction();
4879
4880
        try {
4881
            // Copy content with all versions and drafts
4882
            $contentService->copyContent(
4883
                $content->contentInfo,
4884
                $locationCreate
4885
            );
4886
        } catch (Exception $e) {
4887
            // Cleanup hanging transaction on error
4888
            $repository->rollback();
4889
            throw $e;
4890
        }
4891
4892
        // Rollback all changes
4893
        $repository->rollback();
4894
4895
        $this->refreshSearch($repository);
4896
4897
        // This array will only contain a single admin user object
4898
        $locations = $locationService->loadLocationChildren(
4899
            $locationService->loadLocation($locationId)
4900
        )->locations;
4901
        /* END: Use Case */
4902
4903
        $this->assertEquals(1, count($locations));
4904
    }
4905
4906
    /**
4907
     * Test for the copyContent() method.
4908
     *
4909
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4910
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4911
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4912
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4913
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4914
     */
4915 View Code Duplication
    public function testCopyContentInTransactionWithCommit()
4916
    {
4917
        $repository = $this->getRepository();
4918
4919
        $contentId = $this->generateId('object', 11);
4920
        $locationId = $this->generateId('location', 13);
4921
        /* BEGIN: Use Case */
4922
        // $contentId is the ID of the "Members" user group in an eZ Publish
4923
        // demo installation
4924
4925
        // $locationId is the ID of the "Administrator users" group location
4926
4927
        // Get services
4928
        $contentService = $repository->getContentService();
4929
        $locationService = $repository->getLocationService();
4930
4931
        // Load content object to copy
4932
        $content = $contentService->loadContent($contentId);
4933
4934
        // Create new target location
4935
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4936
4937
        // Start a new transaction
4938
        $repository->beginTransaction();
4939
4940
        try {
4941
            // Copy content with all versions and drafts
4942
            $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...
4943
                $content->contentInfo,
4944
                $locationCreate
4945
            );
4946
4947
            // Commit all changes
4948
            $repository->commit();
4949
        } catch (Exception $e) {
4950
            // Cleanup hanging transaction on error
4951
            $repository->rollback();
4952
            throw $e;
4953
        }
4954
4955
        $this->refreshSearch($repository);
4956
4957
        // This will contain the admin user and the new child location
4958
        $locations = $locationService->loadLocationChildren(
4959
            $locationService->loadLocation($locationId)
4960
        )->locations;
4961
        /* END: Use Case */
4962
4963
        $this->assertEquals(2, count($locations));
4964
    }
4965
4966
    public function testURLAliasesCreatedForNewContent()
4967
    {
4968
        $repository = $this->getRepository();
4969
4970
        $contentService = $repository->getContentService();
4971
        $locationService = $repository->getLocationService();
4972
        $urlAliasService = $repository->getURLAliasService();
4973
4974
        /* BEGIN: Use Case */
4975
        $draft = $this->createContentDraftVersion1();
4976
4977
        // Automatically creates a new URLAlias for the content
4978
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
4979
        /* END: Use Case */
4980
4981
        $location = $locationService->loadLocation(
4982
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4983
        );
4984
4985
        $aliases = $urlAliasService->listLocationAliases($location, false);
4986
4987
        $this->assertAliasesCorrect(
4988
            array(
4989
                '/Design/Plain-site/An-awesome-forum' => array(
4990
                    'type' => URLAlias::LOCATION,
4991
                    'destination' => $location->id,
4992
                    'path' => '/Design/Plain-site/An-awesome-forum',
4993
                    'languageCodes' => array('eng-US'),
4994
                    'isHistory' => false,
4995
                    'isCustom' => false,
4996
                    'forward' => false,
4997
                ),
4998
            ),
4999
            $aliases
5000
        );
5001
    }
5002
5003
    public function testURLAliasesCreatedForUpdatedContent()
5004
    {
5005
        $repository = $this->getRepository();
5006
5007
        $contentService = $repository->getContentService();
5008
        $locationService = $repository->getLocationService();
5009
        $urlAliasService = $repository->getURLAliasService();
5010
5011
        /* BEGIN: Use Case */
5012
        $draft = $this->createUpdatedDraftVersion2();
5013
5014
        $location = $locationService->loadLocation(
5015
            $draft->getVersionInfo()->getContentInfo()->mainLocationId
5016
        );
5017
5018
        // Load and assert URL aliases before publishing updated Content, so that
5019
        // SPI cache is warmed up and cache invalidation is also tested.
5020
        $aliases = $urlAliasService->listLocationAliases($location, false);
5021
5022
        $this->assertAliasesCorrect(
5023
            array(
5024
                '/Design/Plain-site/An-awesome-forum' => array(
5025
                    'type' => URLAlias::LOCATION,
5026
                    'destination' => $location->id,
5027
                    'path' => '/Design/Plain-site/An-awesome-forum',
5028
                    'languageCodes' => array('eng-US'),
5029
                    'alwaysAvailable' => true,
5030
                    'isHistory' => false,
5031
                    'isCustom' => false,
5032
                    'forward' => false,
5033
                ),
5034
            ),
5035
            $aliases
5036
        );
5037
5038
        // Automatically marks old aliases for the content as history
5039
        // and creates new aliases, based on the changes
5040
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
5041
        /* END: Use Case */
5042
5043
        $location = $locationService->loadLocation(
5044
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
5045
        );
5046
5047
        $aliases = $urlAliasService->listLocationAliases($location, false);
5048
5049
        $this->assertAliasesCorrect(
5050
            array(
5051
                '/Design/Plain-site/An-awesome-forum2' => array(
5052
                    'type' => URLAlias::LOCATION,
5053
                    'destination' => $location->id,
5054
                    'path' => '/Design/Plain-site/An-awesome-forum2',
5055
                    'languageCodes' => array('eng-US'),
5056
                    'alwaysAvailable' => true,
5057
                    'isHistory' => false,
5058
                    'isCustom' => false,
5059
                    'forward' => false,
5060
                ),
5061
                '/Design/Plain-site/An-awesome-forum23' => array(
5062
                    'type' => URLAlias::LOCATION,
5063
                    'destination' => $location->id,
5064
                    'path' => '/Design/Plain-site/An-awesome-forum23',
5065
                    'languageCodes' => array('eng-GB'),
5066
                    'alwaysAvailable' => true,
5067
                    'isHistory' => false,
5068
                    'isCustom' => false,
5069
                    'forward' => false,
5070
                ),
5071
            ),
5072
            $aliases
5073
        );
5074
    }
5075
5076
    public function testCustomURLAliasesNotHistorizedOnUpdatedContent()
5077
    {
5078
        $repository = $this->getRepository();
5079
5080
        $contentService = $repository->getContentService();
5081
5082
        /* BEGIN: Use Case */
5083
        $urlAliasService = $repository->getURLAliasService();
5084
        $locationService = $repository->getLocationService();
5085
5086
        $content = $this->createContentVersion1();
5087
5088
        // Create a custom URL alias
5089
        $urlAliasService->createUrlAlias(
5090
            $locationService->loadLocation(
5091
                $content->getVersionInfo()->getContentInfo()->mainLocationId
5092
            ),
5093
            '/my/fancy/story-about-ez-publish',
5094
            'eng-US'
5095
        );
5096
5097
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
5098
5099
        $contentUpdate = $contentService->newContentUpdateStruct();
5100
        $contentUpdate->initialLanguageCode = 'eng-US';
5101
        $contentUpdate->setField('name', 'Amazing Bielefeld forum');
5102
5103
        $draftVersion2 = $contentService->updateContent(
5104
            $draftVersion2->getVersionInfo(),
5105
            $contentUpdate
5106
        );
5107
5108
        // Only marks auto-generated aliases as history
5109
        // the custom one is left untouched
5110
        $liveContent = $contentService->publishVersion($draftVersion2->getVersionInfo());
5111
        /* END: Use Case */
5112
5113
        $location = $locationService->loadLocation(
5114
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
5115
        );
5116
5117
        $aliases = $urlAliasService->listLocationAliases($location);
5118
5119
        $this->assertAliasesCorrect(
5120
            array(
5121
                '/my/fancy/story-about-ez-publish' => array(
5122
                    'type' => URLAlias::LOCATION,
5123
                    'destination' => $location->id,
5124
                    'path' => '/my/fancy/story-about-ez-publish',
5125
                    'languageCodes' => array('eng-US'),
5126
                    'isHistory' => false,
5127
                    'isCustom' => true,
5128
                    'forward' => false,
5129
                    'alwaysAvailable' => false,
5130
                ),
5131
            ),
5132
            $aliases
5133
        );
5134
    }
5135
5136
    /**
5137
     * Test to ensure that old versions are not affected by updates to newer
5138
     * drafts.
5139
     */
5140
    public function testUpdatingDraftDoesNotUpdateOldVersions()
5141
    {
5142
        $repository = $this->getRepository();
5143
5144
        $contentService = $repository->getContentService();
5145
5146
        $contentVersion2 = $this->createContentVersion2();
5147
5148
        $loadedContent1 = $contentService->loadContent($contentVersion2->id, null, 1);
5149
        $loadedContent2 = $contentService->loadContent($contentVersion2->id, null, 2);
5150
5151
        $this->assertNotEquals(
5152
            $loadedContent1->getFieldValue('name', 'eng-US'),
5153
            $loadedContent2->getFieldValue('name', 'eng-US')
5154
        );
5155
    }
5156
5157
    /**
5158
     * Test scenario with writer and publisher users.
5159
     * Writer can only create content. Publisher can publish this content.
5160
     */
5161
    public function testPublishWorkflow()
5162
    {
5163
        $repository = $this->getRepository();
5164
        $contentService = $repository->getContentService();
5165
5166
        $this->createRoleWithPolicies('Publisher', [
5167
            ['module' => 'content', 'function' => 'read'],
5168
            ['module' => 'content', 'function' => 'create'],
5169
            ['module' => 'content', 'function' => 'publish'],
5170
        ]);
5171
5172
        $this->createRoleWithPolicies('Writer', [
5173
            ['module' => 'content', 'function' => 'read'],
5174
            ['module' => 'content', 'function' => 'create'],
5175
        ]);
5176
5177
        $writerUser = $this->createCustomUserWithLogin(
5178
            'writer',
5179
            '[email protected]',
5180
            'Writers',
5181
            'Writer'
5182
        );
5183
5184
        $publisherUser = $this->createCustomUserWithLogin(
5185
            'publisher',
5186
            '[email protected]',
5187
            'Publishers',
5188
            'Publisher'
5189
        );
5190
5191
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5192
        $draft = $this->createContentDraftVersion1();
5193
5194
        $repository->getPermissionResolver()->setCurrentUserReference($publisherUser);
5195
        $content = $contentService->publishVersion($draft->versionInfo);
5196
5197
        $contentService->loadContent($content->id);
5198
    }
5199
5200
    /**
5201
     * Test publish / content policy is required to be able to publish content.
5202
     *
5203
     * @expectedException \eZ\Publish\Core\Base\Exceptions\UnauthorizedException
5204
     * @expectedExceptionMessageRegExp /User does not have access to 'publish' 'content'/
5205
     */
5206
    public function testPublishContentWithoutPublishPolicyThrowsException()
5207
    {
5208
        $repository = $this->getRepository();
5209
5210
        $this->createRoleWithPolicies('Writer', [
5211
            ['module' => 'content', 'function' => 'read'],
5212
            ['module' => 'content', 'function' => 'create'],
5213
            ['module' => 'content', 'function' => 'edit'],
5214
        ]);
5215
        $writerUser = $this->createCustomUserWithLogin(
5216
            'writer',
5217
            '[email protected]',
5218
            'Writers',
5219
            'Writer'
5220
        );
5221
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5222
5223
        $this->createContentVersion1();
5224
    }
5225
5226
    /**
5227
     * Test removal of the specific translation from all the Versions of a Content Object.
5228
     *
5229
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5230
     */
5231 View Code Duplication
    public function testDeleteTranslation()
5232
    {
5233
        $repository = $this->getRepository();
5234
        $contentService = $repository->getContentService();
5235
        $content = $this->createContentVersion2();
5236
5237
        // create multiple versions to exceed archive limit
5238
        for ($i = 0; $i < 5; ++$i) {
5239
            $contentDraft = $contentService->createContentDraft($content->contentInfo);
5240
            $contentUpdateStruct = $contentService->newContentUpdateStruct();
5241
            $contentDraft = $contentService->updateContent(
5242
                $contentDraft->versionInfo,
5243
                $contentUpdateStruct
5244
            );
5245
            $contentService->publishVersion($contentDraft->versionInfo);
5246
        }
5247
5248
        $contentService->deleteTranslation($content->contentInfo, 'eng-GB');
5249
5250
        $this->assertTranslationDoesNotExist('eng-GB', $content->id);
5251
    }
5252
5253
    /**
5254
     * Test deleting a Translation which is initial for some Version, updates initialLanguageCode
5255
     * with mainLanguageCode (assuming they are different).
5256
     */
5257
    public function testDeleteTranslationUpdatesInitialLanguageCodeVersion()
5258
    {
5259
        $repository = $this->getRepository();
5260
        $contentService = $repository->getContentService();
5261
5262
        $content = $this->createContentVersion2();
5263
        // create another, copied, version
5264
        $contentDraft = $contentService->updateContent(
5265
            $contentService->createContentDraft($content->contentInfo)->versionInfo,
5266
            $contentService->newContentUpdateStruct()
5267
        );
5268
        $publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
5269
5270
        // remove first version with only one translation as it is not the subject of this test
5271
        $contentService->deleteVersion(
5272
            $contentService->loadVersionInfo($publishedContent->contentInfo, 1)
5273
        );
5274
5275
        // sanity check
5276
        self::assertEquals('eng-US', $content->contentInfo->mainLanguageCode);
5277
        self::assertEquals('eng-US', $content->versionInfo->initialLanguageCode);
5278
5279
        // update mainLanguageCode so it is different than initialLanguageCode for Version
5280
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
5281
        $contentMetadataUpdateStruct->mainLanguageCode = 'eng-GB';
5282
        $content = $contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
5283
5284
        $contentService->deleteTranslation($content->contentInfo, 'eng-US');
5285
5286
        $this->assertTranslationDoesNotExist('eng-US', $content->id);
5287
    }
5288
5289
    /**
5290
     * Test removal of the specific translation properly updates languages of the URL alias.
5291
     *
5292
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5293
     */
5294
    public function testDeleteTranslationUpdatesUrlAlias()
5295
    {
5296
        $repository = $this->getRepository();
5297
        $contentService = $repository->getContentService();
5298
        $locationService = $repository->getLocationService();
5299
        $urlAliasService = $repository->getURLAliasService();
5300
5301
        $content = $this->createContentVersion2();
5302
        $mainLocation = $locationService->loadLocation($content->contentInfo->mainLocationId);
5303
5304
        // create custom URL alias for Content main Location
5305
        $urlAliasService->createUrlAlias($mainLocation, '/my-custom-url', 'eng-GB');
5306
5307
        // create secondary Location for Content
5308
        $secondaryLocation = $locationService->createLocation(
5309
            $content->contentInfo,
5310
            $locationService->newLocationCreateStruct(2)
5311
        );
5312
5313
        // create custom URL alias for Content secondary Location
5314
        $urlAliasService->createUrlAlias($secondaryLocation, '/my-secondary-url', 'eng-GB');
5315
5316
        // delete Translation
5317
        $contentService->deleteTranslation($content->contentInfo, 'eng-GB');
5318
5319
        foreach ([$mainLocation, $secondaryLocation] as $location) {
5320
            // check auto-generated URL aliases
5321
            foreach ($urlAliasService->listLocationAliases($location, false) as $alias) {
5322
                self::assertNotContains('eng-GB', $alias->languageCodes);
5323
            }
5324
5325
            // check custom URL aliases
5326
            foreach ($urlAliasService->listLocationAliases($location) as $alias) {
5327
                self::assertNotContains('eng-GB', $alias->languageCodes);
5328
            }
5329
        }
5330
    }
5331
5332
    /**
5333
     * Test removal of a main translation throws BadStateException.
5334
     *
5335
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5336
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5337
     * @expectedExceptionMessage Specified translation is the main translation of the Content Object
5338
     */
5339
    public function testDeleteTranslationMainLanguageThrowsBadStateException()
5340
    {
5341
        $repository = $this->getRepository();
5342
        $contentService = $repository->getContentService();
5343
        $content = $this->createContentVersion2();
5344
5345
        // delete first version which has only one translation
5346
        $contentService->deleteVersion($contentService->loadVersionInfo($content->contentInfo, 1));
5347
5348
        // try to delete main translation
5349
        $contentService->deleteTranslation($content->contentInfo, $content->contentInfo->mainLanguageCode);
5350
    }
5351
5352
    /**
5353
     * Test removal of a Translation is possible when some archived Versions have only this Translation.
5354
     *
5355
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5356
     */
5357
    public function testDeleteTranslationDeletesSingleTranslationVersions()
5358
    {
5359
        $repository = $this->getRepository();
5360
        $contentService = $repository->getContentService();
5361
        // content created by the createContentVersion1 method has eng-US translation only.
5362
        $content = $this->createContentVersion1();
5363
5364
        // create new version and add eng-GB translation
5365
        $contentDraft = $contentService->createContentDraft($content->contentInfo);
5366
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
5367
        $contentUpdateStruct->setField('name', 'Awesome Board', 'eng-GB');
5368
        $contentDraft = $contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
5369
        $publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
5370
5371
        // update mainLanguageCode to avoid exception related to that
5372
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
5373
        $contentMetadataUpdateStruct->mainLanguageCode = 'eng-GB';
5374
5375
        $content = $contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
5376
5377
        $contentService->deleteTranslation($content->contentInfo, 'eng-US');
5378
5379
        $this->assertTranslationDoesNotExist('eng-US', $content->id);
5380
    }
5381
5382
    /**
5383
     * Test removal of the translation by the user who is not allowed to delete a content
5384
     * throws UnauthorizedException.
5385
     *
5386
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5387
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5388
     * @expectedExceptionMessage User does not have access to 'remove' 'content'
5389
     */
5390
    public function testDeleteTranslationThrowsUnauthorizedException()
5391
    {
5392
        $repository = $this->getRepository();
5393
        $contentService = $repository->getContentService();
5394
5395
        $content = $this->createContentVersion2();
5396
5397
        // create user that can read/create/edit but cannot delete content
5398
        $this->createRoleWithPolicies('Writer', [
5399
            ['module' => 'content', 'function' => 'read'],
5400
            ['module' => 'content', 'function' => 'versionread'],
5401
            ['module' => 'content', 'function' => 'create'],
5402
            ['module' => 'content', 'function' => 'edit'],
5403
        ]);
5404
        $writerUser = $this->createCustomUserWithLogin(
5405
            'writer',
5406
            '[email protected]',
5407
            'Writers',
5408
            'Writer'
5409
        );
5410
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5411
        $contentService->deleteTranslation($content->contentInfo, 'eng-GB');
5412
    }
5413
5414
    /**
5415
     * Test removal of a non-existent translation throws InvalidArgumentException.
5416
     *
5417
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslation
5418
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
5419
     * @expectedExceptionMessage Argument '$languageCode' is invalid: ger-DE does not exist in the Content item
5420
     */
5421
    public function testDeleteTranslationThrowsInvalidArgumentException()
5422
    {
5423
        $repository = $this->getRepository();
5424
        $contentService = $repository->getContentService();
5425
        // content created by the createContentVersion1 method has eng-US translation only.
5426
        $content = $this->createContentVersion1();
5427
        $contentService->deleteTranslation($content->contentInfo, 'ger-DE');
5428
    }
5429
5430
    /**
5431
     * Test deleting a Translation from Draft.
5432
     *
5433
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5434
     */
5435
    public function testDeleteTranslationFromDraft()
5436
    {
5437
        $repository = $this->getRepository();
5438
        $contentService = $repository->getContentService();
5439
5440
        $languageCode = 'eng-GB';
5441
        $content = $this->createMultipleLanguageContentVersion2();
5442
        $draft = $contentService->createContentDraft($content->contentInfo);
5443
        $draft = $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5444
        $content = $contentService->publishVersion($draft->versionInfo);
5445
5446
        $loadedContent = $contentService->loadContent($content->id);
5447
        self::assertNotContains($languageCode, $loadedContent->versionInfo->languageCodes);
5448
        self::assertEmpty($loadedContent->getFieldsByLanguage($languageCode));
5449
    }
5450
5451
    /**
5452
     * Get values for multilingual field.
5453
     *
5454
     * @return array
5455
     */
5456
    public function providerForDeleteTranslationFromDraftRemovesUrlAliasOnPublishing()
5457
    {
5458
        return [
5459
            [
5460
                ['eng-US' => 'US Name', 'eng-GB' => 'GB Name'],
5461
            ],
5462
            [
5463
                ['eng-US' => 'Same Name', 'eng-GB' => 'Same Name'],
5464
            ],
5465
        ];
5466
    }
5467
5468
    /**
5469
     * Test deleting a Translation from Draft removes previously stored URL aliases for published Content.
5470
     *
5471
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5472
     *
5473
     * @dataProvider providerForDeleteTranslationFromDraftRemovesUrlAliasOnPublishing
5474
     *
5475
     * @param string[] $fieldValues translated field values
5476
     *
5477
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
5478
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
5479
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
5480
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5481
     */
5482
    public function testDeleteTranslationFromDraftRemovesUrlAliasOnPublishing(array $fieldValues)
5483
    {
5484
        $repository = $this->getRepository();
5485
        $contentService = $repository->getContentService();
5486
        $locationService = $repository->getLocationService();
5487
        $urlAliasService = $repository->getURLAliasService();
5488
5489
        // set language code to be removed
5490
        $languageCode = 'eng-GB';
5491
        $draft = $this->createMultilingualContentDraft(
5492
            'folder',
5493
            2,
5494
            'eng-US',
5495
            [
5496
                'name' => [
5497
                    'eng-GB' => $fieldValues['eng-GB'],
5498
                    'eng-US' => $fieldValues['eng-US'],
5499
                ],
5500
            ]
5501
        );
5502
        $content = $contentService->publishVersion($draft->versionInfo);
5503
5504
        // create secondary location
5505
        $locationService->createLocation(
5506
            $content->contentInfo,
5507
            $locationService->newLocationCreateStruct(5)
5508
        );
5509
5510
        // sanity check
5511
        $locations = $locationService->loadLocations($content->contentInfo);
5512
        self::assertCount(2, $locations, 'Sanity check: Expected to find 2 Locations');
5513
        foreach ($locations as $location) {
5514
            $urlAliasService->createUrlAlias($location, '/us-custom_' . $location->id, 'eng-US');
5515
            $urlAliasService->createUrlAlias($location, '/gb-custom_' . $location->id, 'eng-GB');
5516
5517
            // check default URL aliases
5518
            $aliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
5519
            self::assertNotEmpty($aliases, 'Sanity check: URL alias for the translation does not exist');
5520
5521
            // check custom URL aliases
5522
            $aliases = $urlAliasService->listLocationAliases($location, true, $languageCode);
5523
            self::assertNotEmpty($aliases, 'Sanity check: Custom URL alias for the translation does not exist');
5524
        }
5525
5526
        // delete translation and publish new version
5527
        $draft = $contentService->createContentDraft($content->contentInfo);
5528
        $draft = $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5529
        $contentService->publishVersion($draft->versionInfo);
5530
5531
        // check that aliases does not exist
5532
        foreach ($locations as $location) {
5533
            // check default URL aliases
5534
            $aliases = $urlAliasService->listLocationAliases($location, false, $languageCode);
5535
            self::assertEmpty($aliases, 'URL alias for the deleted translation still exists');
5536
5537
            // check custom URL aliases
5538
            $aliases = $urlAliasService->listLocationAliases($location, true, $languageCode);
5539
            self::assertEmpty($aliases, 'Custom URL alias for the deleted translation still exists');
5540
        }
5541
    }
5542
5543
    /**
5544
     * Test that URL aliases for deleted Translations are properly archived.
5545
     */
5546
    public function testDeleteTranslationFromDraftArchivesUrlAliasOnPublishing()
5547
    {
5548
        $repository = $this->getRepository();
5549
        $contentService = $repository->getContentService();
5550
        $urlAliasService = $repository->getURLAliasService();
5551
5552
        $content = $contentService->publishVersion(
5553
            $this->createMultilingualContentDraft(
5554
                'folder',
5555
                2,
5556
                'eng-US',
5557
                [
5558
                    'name' => [
5559
                        'eng-GB' => 'BritishEnglishContent',
5560
                        'eng-US' => 'AmericanEnglishContent',
5561
                    ],
5562
                ]
5563
            )->versionInfo
5564
        );
5565
5566
        $unrelatedContent = $contentService->publishVersion(
5567
            $this->createMultilingualContentDraft(
5568
                'folder',
5569
                2,
5570
                'eng-US',
5571
                [
5572
                    'name' => [
5573
                        'eng-GB' => 'AnotherBritishContent',
5574
                        'eng-US' => 'AnotherAmericanContent',
5575
                    ],
5576
                ]
5577
            )->versionInfo
5578
        );
5579
5580
        $urlAlias = $urlAliasService->lookup('/BritishEnglishContent');
5581
        self::assertFalse($urlAlias->isHistory);
5582
        self::assertEquals($urlAlias->path, '/BritishEnglishContent');
5583
        self::assertEquals($urlAlias->destination, $content->contentInfo->mainLocationId);
5584
5585
        $draft = $contentService->deleteTranslationFromDraft(
5586
            $contentService->createContentDraft($content->contentInfo)->versionInfo,
5587
            'eng-GB'
5588
        );
5589
        $content = $contentService->publishVersion($draft->versionInfo);
5590
5591
        $urlAlias = $urlAliasService->lookup('/BritishEnglishContent');
5592
        self::assertTrue($urlAlias->isHistory);
5593
        self::assertEquals($urlAlias->path, '/BritishEnglishContent');
5594
        self::assertEquals($urlAlias->destination, $content->contentInfo->mainLocationId);
5595
5596
        $unrelatedUrlAlias = $urlAliasService->lookup('/AnotherBritishContent');
5597
        self::assertFalse($unrelatedUrlAlias->isHistory);
5598
        self::assertEquals($unrelatedUrlAlias->path, '/AnotherBritishContent');
5599
        self::assertEquals($unrelatedUrlAlias->destination, $unrelatedContent->contentInfo->mainLocationId);
5600
    }
5601
5602
    /**
5603
     * Test deleting a Translation from Draft which has single Translation throws BadStateException.
5604
     *
5605
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5606
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5607
     * @expectedExceptionMessage Specified Translation is the only one Content Object Version has
5608
     */
5609
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnSingleTranslation()
5610
    {
5611
        $repository = $this->getRepository();
5612
        $contentService = $repository->getContentService();
5613
5614
        // create Content with single Translation
5615
        $publishedContent = $contentService->publishVersion(
5616
            $this->createContentDraft(
5617
                'forum',
5618
                2,
5619
                ['name' => 'Eng-US Version name']
5620
            )->versionInfo
5621
        );
5622
5623
        // update mainLanguageCode to avoid exception related to trying to delete main Translation
5624
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
5625
        $contentMetadataUpdateStruct->mainLanguageCode = 'eng-GB';
5626
        $publishedContent = $contentService->updateContentMetadata(
5627
            $publishedContent->contentInfo,
5628
            $contentMetadataUpdateStruct
5629
        );
5630
5631
        // create single Translation Version from the first one
5632
        $draft = $contentService->createContentDraft(
5633
            $publishedContent->contentInfo,
5634
            $publishedContent->versionInfo
5635
        );
5636
5637
        // attempt to delete Translation
5638
        $contentService->deleteTranslationFromDraft($draft->versionInfo, 'eng-US');
5639
    }
5640
5641
    /**
5642
     * Test deleting the Main Translation from Draft throws BadStateException.
5643
     *
5644
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5645
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5646
     * @expectedExceptionMessage Specified Translation is the main Translation of the Content Object
5647
     */
5648
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnMainTranslation()
5649
    {
5650
        $repository = $this->getRepository();
5651
        $contentService = $repository->getContentService();
5652
5653
        $mainLanguageCode = 'eng-US';
5654
        $draft = $this->createMultilingualContentDraft(
5655
            'forum',
5656
            2,
5657
            $mainLanguageCode,
5658
            [
5659
                'name' => [
5660
                    'eng-US' => 'An awesome eng-US forum',
5661
                    'eng-GB' => 'An awesome eng-GB forum',
5662
                ],
5663
            ]
5664
        );
5665
        $contentService->deleteTranslationFromDraft($draft->versionInfo, $mainLanguageCode);
5666
    }
5667
5668
    /**
5669
     * Test deleting the Translation from Published Version throws BadStateException.
5670
     *
5671
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5672
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
5673
     * @expectedExceptionMessage Version is not a draft
5674
     */
5675 View Code Duplication
    public function testDeleteTranslationFromDraftThrowsBadStateExceptionOnPublishedVersion()
5676
    {
5677
        $repository = $this->getRepository();
5678
        $contentService = $repository->getContentService();
5679
5680
        $languageCode = 'eng-US';
5681
        $content = $this->createMultipleLanguageContentVersion2();
5682
        $draft = $contentService->createContentDraft($content->contentInfo);
5683
        $publishedContent = $contentService->publishVersion($draft->versionInfo);
5684
        $contentService->deleteTranslationFromDraft($publishedContent->versionInfo, $languageCode);
5685
    }
5686
5687
    /**
5688
     * Test deleting a Translation from Draft throws UnauthorizedException if user cannot edit Content.
5689
     *
5690
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5691
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
5692
     * @expectedExceptionMessage User does not have access to 'edit' 'content'
5693
     */
5694
    public function testDeleteTranslationFromDraftThrowsUnauthorizedException()
5695
    {
5696
        $repository = $this->getRepository();
5697
        $contentService = $repository->getContentService();
5698
5699
        $languageCode = 'eng-GB';
5700
        $content = $this->createMultipleLanguageContentVersion2();
5701
        $draft = $contentService->createContentDraft($content->contentInfo);
5702
5703
        // create user that can read/create/delete but cannot edit or content
5704
        $this->createRoleWithPolicies('Writer', [
5705
            ['module' => 'content', 'function' => 'read'],
5706
            ['module' => 'content', 'function' => 'versionread'],
5707
            ['module' => 'content', 'function' => 'create'],
5708
            ['module' => 'content', 'function' => 'delete'],
5709
        ]);
5710
        $writerUser = $this->createCustomUserWithLogin(
5711
            'user',
5712
            '[email protected]',
5713
            'Writers',
5714
            'Writer'
5715
        );
5716
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5717
5718
        $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5719
    }
5720
5721
    /**
5722
     * Test deleting a non-existent Translation from Draft throws InvalidArgumentException.
5723
     *
5724
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteTranslationFromDraft
5725
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
5726
     * @expectedExceptionMessageRegExp /The Version \(ContentId=\d+, VersionNo=\d+\) is not translated into ger-DE/
5727
     */
5728
    public function testDeleteTranslationFromDraftThrowsInvalidArgumentException()
5729
    {
5730
        $repository = $this->getRepository();
5731
        $contentService = $repository->getContentService();
5732
5733
        $languageCode = 'ger-DE';
5734
        $content = $this->createMultipleLanguageContentVersion2();
5735
        $draft = $contentService->createContentDraft($content->contentInfo);
5736
        $contentService->deleteTranslationFromDraft($draft->versionInfo, $languageCode);
5737
    }
5738
5739
    /**
5740
     * Asserts that all aliases defined in $expectedAliasProperties with the
5741
     * given properties are available in $actualAliases and not more.
5742
     *
5743
     * @param array $expectedAliasProperties
5744
     * @param array $actualAliases
5745
     */
5746
    private function assertAliasesCorrect(array $expectedAliasProperties, array $actualAliases)
5747
    {
5748
        foreach ($actualAliases as $actualAlias) {
5749
            if (!isset($expectedAliasProperties[$actualAlias->path])) {
5750
                $this->fail(
5751
                    sprintf(
5752
                        'Alias with path "%s" in languages "%s" not expected.',
5753
                        $actualAlias->path,
5754
                        implode(', ', $actualAlias->languageCodes)
5755
                    )
5756
                );
5757
            }
5758
5759
            foreach ($expectedAliasProperties[$actualAlias->path] as $propertyName => $propertyValue) {
5760
                $this->assertEquals(
5761
                    $propertyValue,
5762
                    $actualAlias->$propertyName,
5763
                    sprintf(
5764
                        'Property $%s incorrect on alias with path "%s" in languages "%s".',
5765
                        $propertyName,
5766
                        $actualAlias->path,
5767
                        implode(', ', $actualAlias->languageCodes)
5768
                    )
5769
                );
5770
            }
5771
5772
            unset($expectedAliasProperties[$actualAlias->path]);
5773
        }
5774
5775
        if (!empty($expectedAliasProperties)) {
5776
            $this->fail(
5777
                sprintf(
5778
                    'Missing expected aliases with paths "%s".',
5779
                    implode('", "', array_keys($expectedAliasProperties))
5780
                )
5781
            );
5782
        }
5783
    }
5784
5785
    /**
5786
     * Asserts that the given fields are equal to the default fields fixture.
5787
     *
5788
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5789
     */
5790
    private function assertAllFieldsEquals(array $fields)
5791
    {
5792
        $actual = $this->normalizeFields($fields);
5793
        $expected = $this->normalizeFields($this->createFieldsFixture());
5794
5795
        $this->assertEquals($expected, $actual);
5796
    }
5797
5798
    /**
5799
     * Asserts that the given fields are equal to a language filtered set of the
5800
     * default fields fixture.
5801
     *
5802
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5803
     * @param string $languageCode
5804
     */
5805
    private function assertLocaleFieldsEquals(array $fields, $languageCode)
5806
    {
5807
        $actual = $this->normalizeFields($fields);
5808
5809
        $expected = array();
5810
        foreach ($this->normalizeFields($this->createFieldsFixture()) as $field) {
5811
            if ($field->languageCode !== $languageCode) {
5812
                continue;
5813
            }
5814
            $expected[] = $field;
5815
        }
5816
5817
        $this->assertEquals($expected, $actual);
5818
    }
5819
5820
    /**
5821
     * This method normalizes a set of fields and returns a normalized set.
5822
     *
5823
     * Normalization means it resets the storage specific field id to zero and
5824
     * it sorts the field by their identifier and their language code. In
5825
     * addition, the field value is removed, since this one depends on the
5826
     * specific FieldType, which is tested in a dedicated integration test.
5827
     *
5828
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5829
     *
5830
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5831
     */
5832
    private function normalizeFields(array $fields)
5833
    {
5834
        $normalized = array();
5835 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...
5836
            $normalized[] = new Field(
5837
                array(
5838
                    'id' => 0,
5839
                    'value' => ($field->value !== null ? true : null),
5840
                    'languageCode' => $field->languageCode,
5841
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
5842
                    'fieldTypeIdentifier' => $field->fieldTypeIdentifier,
5843
                )
5844
            );
5845
        }
5846
        usort(
5847
            $normalized,
5848 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...
5849
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
5850
                    return strcasecmp($field1->languageCode, $field2->languageCode);
5851
                }
5852
5853
                return $return;
5854
            }
5855
        );
5856
5857
        return $normalized;
5858
    }
5859
5860
    /**
5861
     * Returns a filtered set of the default fields fixture.
5862
     *
5863
     * @param string $languageCode
5864
     *
5865
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5866
     */
5867
    private function createLocaleFieldsFixture($languageCode)
5868
    {
5869
        $fields = array();
5870
        foreach ($this->createFieldsFixture() as $field) {
5871
            if (null === $field->languageCode || $languageCode === $field->languageCode) {
5872
                $fields[] = $field;
5873
            }
5874
        }
5875
5876
        return $fields;
5877
    }
5878
5879
    /**
5880
     * Asserts that given Content has default ContentStates.
5881
     *
5882
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
5883
     */
5884 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
5885
    {
5886
        $repository = $this->getRepository();
5887
        $objectStateService = $repository->getObjectStateService();
5888
5889
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
5890
5891
        foreach ($objectStateGroups as $objectStateGroup) {
5892
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
5893
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
5894
                // Only check the first object state which is the default one.
5895
                $this->assertEquals(
5896
                    $objectState,
5897
                    $contentState
5898
                );
5899
                break;
5900
            }
5901
        }
5902
    }
5903
5904
    /**
5905
     * Assert that given Content has no references to a translation specified by the $languageCode.
5906
     *
5907
     * @param string $languageCode
5908
     * @param int $contentId
5909
     */
5910
    private function assertTranslationDoesNotExist($languageCode, $contentId)
5911
    {
5912
        $repository = $this->getRepository();
5913
        $contentService = $repository->getContentService();
5914
5915
        $content = $contentService->loadContent($contentId);
5916
5917
        foreach ($content->fields as $fieldIdentifier => $field) {
5918
            /** @var array $field */
5919
            self::assertArrayNotHasKey($languageCode, $field);
5920
            self::assertNotEquals($languageCode, $content->contentInfo->mainLanguageCode);
5921
            self::assertArrayNotHasKey($languageCode, $content->versionInfo->getNames());
5922
            self::assertNotEquals($languageCode, $content->versionInfo->initialLanguageCode);
5923
            self::assertNotContains($languageCode, $content->versionInfo->languageCodes);
5924
        }
5925
        foreach ($contentService->loadVersions($content->contentInfo) as $versionInfo) {
5926
            self::assertArrayNotHasKey($languageCode, $versionInfo->getNames());
5927
            self::assertNotEquals($languageCode, $versionInfo->contentInfo->mainLanguageCode);
5928
            self::assertNotEquals($languageCode, $versionInfo->initialLanguageCode);
5929
            self::assertNotContains($languageCode, $versionInfo->languageCodes);
5930
        }
5931
    }
5932
5933
    /**
5934
     * Returns the default fixture of fields used in most tests.
5935
     *
5936
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5937
     */
5938
    private function createFieldsFixture()
5939
    {
5940
        return array(
5941
            new Field(
5942
                array(
5943
                    'id' => 0,
5944
                    'value' => 'Foo',
5945
                    'languageCode' => 'eng-US',
5946
                    'fieldDefIdentifier' => 'description',
5947
                    'fieldTypeIdentifier' => 'ezrichtext',
5948
                )
5949
            ),
5950
            new Field(
5951
                array(
5952
                    'id' => 0,
5953
                    'value' => 'Bar',
5954
                    'languageCode' => 'eng-GB',
5955
                    'fieldDefIdentifier' => 'description',
5956
                    'fieldTypeIdentifier' => 'ezrichtext',
5957
                )
5958
            ),
5959
            new Field(
5960
                array(
5961
                    'id' => 0,
5962
                    'value' => 'An awesome multi-lang forum²',
5963
                    'languageCode' => 'eng-US',
5964
                    'fieldDefIdentifier' => 'name',
5965
                    'fieldTypeIdentifier' => 'ezstring',
5966
                )
5967
            ),
5968
            new Field(
5969
                array(
5970
                    'id' => 0,
5971
                    'value' => 'An awesome multi-lang forum²³',
5972
                    'languageCode' => 'eng-GB',
5973
                    'fieldDefIdentifier' => 'name',
5974
                    'fieldTypeIdentifier' => 'ezstring',
5975
                )
5976
            ),
5977
        );
5978
    }
5979
5980
    /**
5981
     * Gets expected property values for the "Media" ContentInfo ValueObject.
5982
     *
5983
     * @return array
5984
     */
5985 View Code Duplication
    private function getExpectedMediaContentInfoProperties()
5986
    {
5987
        return [
5988
            'id' => 41,
5989
            'contentTypeId' => 1,
5990
            'name' => 'Media',
5991
            'sectionId' => 3,
5992
            'currentVersionNo' => 1,
5993
            'published' => true,
5994
            'ownerId' => 14,
5995
            'modificationDate' => $this->createDateTime(1060695457),
5996
            'publishedDate' => $this->createDateTime(1060695457),
5997
            'alwaysAvailable' => 1,
5998
            'remoteId' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
5999
            'mainLanguageCode' => 'eng-US',
6000
            'mainLocationId' => 43,
6001
        ];
6002
    }
6003
}
6004