Completed
Push — master ( 779935...ba0bf5 )
by André
58:31 queued 38:49
created

testDeleteContentWithEmptyBinaryField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Importance

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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