Completed
Push — 6.13 ( 09bc2f...2da551 )
by André
18:44 queued 11s
created

testUpdateContentMetadataCheckWithinTransaction()   A

Complexity

Conditions 2
Paths 7

Size

Total Lines 38

Duplication

Lines 38
Ratio 100 %

Importance

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