Completed
Push — master ( 7409fa...631718 )
by
unknown
116:54 queued 93:07
created

ContentServiceTest::testChangeContentName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 24
rs 9.536
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\LocationService;
12
use eZ\Publish\API\Repository\Values\Content\Content;
13
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
14
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
15
use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct;
16
use eZ\Publish\API\Repository\Values\Content\Field;
17
use eZ\Publish\API\Repository\Values\Content\Location;
18
use eZ\Publish\API\Repository\Values\Content\URLAlias;
19
use eZ\Publish\API\Repository\Values\Content\Relation;
20
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
21
use eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation;
22
use eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation;
23
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation;
24
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
25
use DOMDocument;
26
use Exception;
27
use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct;
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(array('limitationValues' => array(1))));
134
        $policyCreateStruct->addLimitation(new LocationLimitation(array('limitationValues' => array(2))));
135
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(array('limitationValues' => array(1))));
136
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
137
138
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'publish');
139
        $policyCreateStruct->addLimitation(new SectionLimitation(array('limitationValues' => array(1))));
140
        $policyCreateStruct->addLimitation(new LocationLimitation(array('limitationValues' => array(2))));
141
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(array('limitationValues' => array(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
            array($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
            array(
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
            array(
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
            array(
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
            array(
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
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
272
     *
273
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
274
     * @depends testCreateContent
275
     */
276
    public function testCreateContentSetsExpectedContentType($content)
277
    {
278
        $contentType = $content->getContentType();
279
280
        $this->assertEquals(
281
            [
282
                $contentType->id,
283
                // Won't match as it's set to true in createContentDraftVersion1()
284
                //$contentType->defaultAlwaysAvailable,
285
                //$contentType->defaultSortField,
286
                //$contentType->defaultSortOrder,
287
            ],
288
            [
289
                $content->contentInfo->contentTypeId,
290
                //$content->contentInfo->alwaysAvailable,
291
                //$location->sortField,
292
                //$location->sortOrder,
293
            ]
294
        );
295
    }
296
297
    /**
298
     * Test for the createContent() method.
299
     *
300
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
301
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
302
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
303
     */
304
    public function testCreateContentThrowsInvalidArgumentException()
305
    {
306
        if ($this->isVersion4()) {
307
            $this->markTestSkipped('This test requires eZ Publish 5');
308
        }
309
310
        $repository = $this->getRepository();
311
312
        /* BEGIN: Use Case */
313
        $contentTypeService = $repository->getContentTypeService();
314
        $contentService = $repository->getContentService();
315
316
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
317
318
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
319
        $contentCreate1->setField('name', 'An awesome Sidelfingen forum');
320
321
        $contentCreate1->remoteId = 'abcdef0123456789abcdef0123456789';
322
        $contentCreate1->alwaysAvailable = true;
323
324
        $draft = $contentService->createContent($contentCreate1);
325
        $contentService->publishVersion($draft->versionInfo);
326
327
        $contentCreate2 = $contentService->newContentCreateStruct($contentType, 'eng-GB');
328
        $contentCreate2->setField('name', 'An awesome Bielefeld forum');
329
330
        $contentCreate2->remoteId = 'abcdef0123456789abcdef0123456789';
331
        $contentCreate2->alwaysAvailable = false;
332
333
        // This call will fail with an "InvalidArgumentException", because the
334
        // remoteId is already in use.
335
        $contentService->createContent($contentCreate2);
336
        /* END: Use Case */
337
    }
338
339
    /**
340
     * Test for the createContent() method.
341
     *
342
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
343
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
344
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
345
     */
346 View Code Duplication
    public function testCreateContentThrowsInvalidArgumentExceptionOnFieldTypeNotAccept()
347
    {
348
        $repository = $this->getRepository();
349
350
        /* BEGIN: Use Case */
351
        $contentTypeService = $repository->getContentTypeService();
352
        $contentService = $repository->getContentService();
353
354
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
355
356
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
357
        // The name field does only accept strings and null as its values
358
        $contentCreate->setField('name', new \stdClass());
359
360
        // Throws InvalidArgumentException since the name field is filled
361
        // improperly
362
        $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...
363
        /* END: Use Case */
364
    }
365
366
    /**
367
     * Test for the createContent() method.
368
     *
369
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
370
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
371
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
372
     */
373
    public function testCreateContentThrowsContentFieldValidationException()
374
    {
375
        $repository = $this->getRepository();
376
377
        /* BEGIN: Use Case */
378
        $contentTypeService = $repository->getContentTypeService();
379
        $contentService = $repository->getContentService();
380
381
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
382
383
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
384
        $contentCreate1->setField('name', 'An awesome Sidelfingen folder');
385
        // Violates string length constraint
386
        $contentCreate1->setField('short_name', str_repeat('a', 200));
387
388
        // Throws ContentFieldValidationException, since short_name does not pass
389
        // validation of the string length validator
390
        $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...
391
        /* END: Use Case */
392
    }
393
394
    /**
395
     * Test for the createContent() method.
396
     *
397
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
398
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
399
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
400
     */
401 View Code Duplication
    public function testCreateContentRequiredFieldMissing()
402
    {
403
        $repository = $this->getRepository();
404
405
        /* BEGIN: Use Case */
406
        $contentTypeService = $repository->getContentTypeService();
407
        $contentService = $repository->getContentService();
408
409
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
410
411
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
412
        // Required field "name" is not set
413
414
        // Throws a ContentFieldValidationException, since a required field is
415
        // missing
416
        $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...
417
        /* END: Use Case */
418
    }
419
420
    /**
421
     * Test for the createContent() method.
422
     *
423
     * NOTE: We have bidirectional dependencies between the ContentService and
424
     * the LocationService, so that we cannot use PHPUnit's test dependencies
425
     * here.
426
     *
427
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
428
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
429
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationByRemoteId
430
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
431
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
432
     * @group user
433
     */
434
    public function testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately()
435
    {
436
        $repository = $this->getRepository();
437
438
        $locationService = $repository->getLocationService();
439
440
        /* BEGIN: Use Case */
441
        $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...
442
443
        // The location will not have been created, yet, so this throws an
444
        // exception
445
        $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...
446
            '0123456789abcdef0123456789abcdef'
447
        );
448
        /* END: Use Case */
449
    }
450
451
    /**
452
     * Test for the createContent() method.
453
     *
454
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
455
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
456
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
457
     */
458
    public function testCreateContentThrowsInvalidArgumentExceptionWithLocationCreateParameter()
459
    {
460
        $repository = $this->getRepository();
461
462
        $parentLocationId = $this->generateId('location', 56);
463
        /* BEGIN: Use Case */
464
        // $parentLocationId is a valid location ID
465
466
        $contentService = $repository->getContentService();
467
        $contentTypeService = $repository->getContentTypeService();
468
        $locationService = $repository->getLocationService();
469
470
        // Load content type
471
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
472
473
        // Configure new locations
474
        $locationCreate1 = $locationService->newLocationCreateStruct($parentLocationId);
475
476
        $locationCreate1->priority = 23;
477
        $locationCreate1->hidden = true;
478
        $locationCreate1->remoteId = '0123456789abcdef0123456789aaaaaa';
479
        $locationCreate1->sortField = Location::SORT_FIELD_NODE_ID;
480
        $locationCreate1->sortOrder = Location::SORT_ORDER_DESC;
481
482
        $locationCreate2 = $locationService->newLocationCreateStruct($parentLocationId);
483
484
        $locationCreate2->priority = 42;
485
        $locationCreate2->hidden = true;
486
        $locationCreate2->remoteId = '0123456789abcdef0123456789bbbbbb';
487
        $locationCreate2->sortField = Location::SORT_FIELD_NODE_ID;
488
        $locationCreate2->sortOrder = Location::SORT_ORDER_DESC;
489
490
        // Configure new content object
491
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
492
493
        $contentCreate->setField('name', 'A awesome Sindelfingen forum');
494
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
495
        $contentCreate->alwaysAvailable = true;
496
497
        // Create new content object under the specified location
498
        $draft = $contentService->createContent(
499
            $contentCreate,
500
            array($locationCreate1)
501
        );
502
        $contentService->publishVersion($draft->versionInfo);
503
504
        // This call will fail with an "InvalidArgumentException", because the
505
        // Content remoteId already exists,
506
        $contentService->createContent(
507
            $contentCreate,
508
            array($locationCreate2)
509
        );
510
        /* END: Use Case */
511
    }
512
513
    /**
514
     * Test for the loadContentInfo() method.
515
     *
516
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
517
     * @group user
518
     */
519 View Code Duplication
    public function testLoadContentInfo()
520
    {
521
        $repository = $this->getRepository();
522
523
        $mediaFolderId = $this->generateId('object', 41);
524
        /* BEGIN: Use Case */
525
        $contentService = $repository->getContentService();
526
527
        // Load the ContentInfo for "Media" folder
528
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
529
        /* END: Use Case */
530
531
        $this->assertInstanceOf(
532
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
533
            $contentInfo
534
        );
535
536
        return $contentInfo;
537
    }
538
539
    /**
540
     * Test for the returned value of the loadContentInfo() method.
541
     *
542
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
543
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfo
544
     *
545
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
546
     */
547
    public function testLoadContentInfoSetsExpectedContentInfo(ContentInfo $contentInfo)
548
    {
549
        $this->assertPropertiesCorrectUnsorted(
550
            $this->getExpectedMediaContentInfoProperties(),
551
            $contentInfo
552
        );
553
    }
554
555
    /**
556
     * Test for the loadContentInfo() method.
557
     *
558
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
559
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
560
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
561
     */
562 View Code Duplication
    public function testLoadContentInfoThrowsNotFoundException()
563
    {
564
        $repository = $this->getRepository();
565
566
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
567
        /* BEGIN: Use Case */
568
        $contentService = $repository->getContentService();
569
570
        // This call will fail with a NotFoundException
571
        $contentService->loadContentInfo($nonExistentContentId);
572
        /* END: Use Case */
573
    }
574
575
    /**
576
     * Test for the loadContentInfoList() method.
577
     *
578
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoList()
579
     */
580
    public function testLoadContentInfoList()
581
    {
582
        $repository = $this->getRepository();
583
584
        $mediaFolderId = $this->generateId('object', 41);
585
        $contentService = $repository->getContentService();
586
        $list = $contentService->loadContentInfoList([$mediaFolderId]);
587
588
        $this->assertCount(1, $list);
589
        $this->assertEquals([$mediaFolderId], array_keys($list), 'Array key was not content id');
590
        $this->assertInstanceOf(
591
            ContentInfo::class,
592
            $list[$mediaFolderId]
593
        );
594
    }
595
596
    /**
597
     * Test for the loadContentInfoList() method.
598
     *
599
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoList()
600
     * @depends testLoadContentInfoList
601
     */
602 View Code Duplication
    public function testLoadContentInfoListSkipsNotFoundItems()
603
    {
604
        $repository = $this->getRepository();
605
606
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
607
        $contentService = $repository->getContentService();
608
        $list = $contentService->loadContentInfoList([$nonExistentContentId]);
609
610
        $this->assertCount(0, $list);
611
    }
612
613
    /**
614
     * Test for the loadContentInfoByRemoteId() method.
615
     *
616
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
617
     */
618
    public function testLoadContentInfoByRemoteId()
619
    {
620
        $repository = $this->getRepository();
621
622
        /* BEGIN: Use Case */
623
        $contentService = $repository->getContentService();
624
625
        // Load the ContentInfo for "Media" folder
626
        $contentInfo = $contentService->loadContentInfoByRemoteId('faaeb9be3bd98ed09f606fc16d144eca');
627
        /* END: Use Case */
628
629
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $contentInfo);
630
631
        return $contentInfo;
632
    }
633
634
    /**
635
     * Test for the returned value of the loadContentInfoByRemoteId() method.
636
     *
637
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
638
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId
639
     *
640
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
641
     */
642 View Code Duplication
    public function testLoadContentInfoByRemoteIdSetsExpectedContentInfo(ContentInfo $contentInfo)
643
    {
644
        $this->assertPropertiesCorrectUnsorted(
645
            [
646
                'id' => 10,
647
                'contentTypeId' => 4,
648
                'name' => 'Anonymous User',
649
                'sectionId' => 2,
650
                'currentVersionNo' => 2,
651
                'published' => true,
652
                'ownerId' => 14,
653
                'modificationDate' => $this->createDateTime(1072180405),
654
                'publishedDate' => $this->createDateTime(1033920665),
655
                'alwaysAvailable' => 1,
656
                'remoteId' => 'faaeb9be3bd98ed09f606fc16d144eca',
657
                'mainLanguageCode' => 'eng-US',
658
                'mainLocationId' => 45,
659
            ],
660
            $contentInfo
661
        );
662
    }
663
664
    /**
665
     * Test for the loadContentInfoByRemoteId() method.
666
     *
667
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
668
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
669
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
670
     */
671
    public function testLoadContentInfoByRemoteIdThrowsNotFoundException()
672
    {
673
        $repository = $this->getRepository();
674
675
        /* BEGIN: Use Case */
676
        $contentService = $repository->getContentService();
677
678
        // This call will fail with a NotFoundException
679
        $contentService->loadContentInfoByRemoteId('abcdefghijklmnopqrstuvwxyz0123456789');
680
        /* END: Use Case */
681
    }
682
683
    /**
684
     * Test for the loadVersionInfo() method.
685
     *
686
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
687
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
688
     * @group user
689
     */
690 View Code Duplication
    public function testLoadVersionInfo()
691
    {
692
        $repository = $this->getRepository();
693
694
        $mediaFolderId = $this->generateId('object', 41);
695
        /* BEGIN: Use Case */
696
        // $mediaFolderId contains the ID of the "Media" folder
697
698
        $contentService = $repository->getContentService();
699
700
        // Load the ContentInfo for "Media" folder
701
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
702
703
        // Now load the current version info of the "Media" folder
704
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
705
        /* END: Use Case */
706
707
        $this->assertInstanceOf(
708
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
709
            $versionInfo
710
        );
711
    }
712
713
    /**
714
     * Test for the loadVersionInfoById() method.
715
     *
716
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
717
     */
718 View Code Duplication
    public function testLoadVersionInfoById()
719
    {
720
        $repository = $this->getRepository();
721
722
        $mediaFolderId = $this->generateId('object', 41);
723
        /* BEGIN: Use Case */
724
        // $mediaFolderId contains the ID of the "Media" folder
725
726
        $contentService = $repository->getContentService();
727
728
        // Load the VersionInfo for "Media" folder
729
        $versionInfo = $contentService->loadVersionInfoById($mediaFolderId);
730
        /* END: Use Case */
731
732
        $this->assertInstanceOf(
733
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
734
            $versionInfo
735
        );
736
737
        return $versionInfo;
738
    }
739
740
    /**
741
     * Test for the returned value of the loadVersionInfoById() method.
742
     *
743
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
744
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
745
     *
746
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
747
     */
748
    public function testLoadVersionInfoByIdSetsExpectedVersionInfo(VersionInfo $versionInfo)
749
    {
750
        $this->assertPropertiesCorrect(
751
            [
752
                'names' => [
753
                    'eng-US' => 'Media',
754
                ],
755
                'contentInfo' => new ContentInfo($this->getExpectedMediaContentInfoProperties()),
756
                'id' => 472,
757
                'versionNo' => 1,
758
                'modificationDate' => $this->createDateTime(1060695457),
759
                'creatorId' => 14,
760
                'creationDate' => $this->createDateTime(1060695450),
761
                'status' => VersionInfo::STATUS_PUBLISHED,
762
                'initialLanguageCode' => 'eng-US',
763
                'languageCodes' => [
764
                    'eng-US',
765
                ],
766
            ],
767
            $versionInfo
768
        );
769
        $this->assertTrue($versionInfo->isPublished());
770
        $this->assertFalse($versionInfo->isDraft());
771
        $this->assertFalse($versionInfo->isArchived());
772
    }
773
774
    /**
775
     * Test for the loadVersionInfoById() method.
776
     *
777
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
778
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
779
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
780
     */
781 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundException()
782
    {
783
        $repository = $this->getRepository();
784
785
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
786
        /* BEGIN: Use Case */
787
        $contentService = $repository->getContentService();
788
789
        // This call will fail with a "NotFoundException"
790
        $contentService->loadVersionInfoById($nonExistentContentId);
791
        /* END: Use Case */
792
    }
793
794
    /**
795
     * Test for the loadContentByContentInfo() method.
796
     *
797
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
798
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
799
     */
800 View Code Duplication
    public function testLoadContentByContentInfo()
801
    {
802
        $repository = $this->getRepository();
803
804
        $mediaFolderId = $this->generateId('object', 41);
805
        /* BEGIN: Use Case */
806
        // $mediaFolderId contains the ID of the "Media" folder
807
808
        $contentService = $repository->getContentService();
809
810
        // Load the ContentInfo for "Media" folder
811
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
812
813
        // Now load the current content version for the info instance
814
        $content = $contentService->loadContentByContentInfo($contentInfo);
815
        /* END: Use Case */
816
817
        $this->assertInstanceOf(
818
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
819
            $content
820
        );
821
    }
822
823
    /**
824
     * Test for the loadContentByVersionInfo() method.
825
     *
826
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
827
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
828
     */
829
    public function testLoadContentByVersionInfo()
830
    {
831
        $repository = $this->getRepository();
832
833
        $mediaFolderId = $this->generateId('object', 41);
834
        /* BEGIN: Use Case */
835
        // $mediaFolderId contains the ID of the "Media" folder
836
837
        $contentService = $repository->getContentService();
838
839
        // Load the ContentInfo for "Media" folder
840
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
841
842
        // Load the current VersionInfo
843
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
844
845
        // Now load the current content version for the info instance
846
        $content = $contentService->loadContentByVersionInfo($versionInfo);
847
        /* END: Use Case */
848
849
        $this->assertInstanceOf(
850
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
851
            $content
852
        );
853
    }
854
855
    /**
856
     * Test for the loadContent() method.
857
     *
858
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
859
     * @group user
860
     * @group field-type
861
     */
862 View Code Duplication
    public function testLoadContent()
863
    {
864
        $repository = $this->getRepository();
865
866
        $mediaFolderId = $this->generateId('object', 41);
867
        /* BEGIN: Use Case */
868
        // $mediaFolderId contains the ID of the "Media" folder
869
870
        $contentService = $repository->getContentService();
871
872
        // Load the Content for "Media" folder, any language and current version
873
        $content = $contentService->loadContent($mediaFolderId);
874
        /* END: Use Case */
875
876
        $this->assertInstanceOf(
877
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
878
            $content
879
        );
880
    }
881
882
    /**
883
     * Test for the loadContent() method.
884
     *
885
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
886
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
887
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
888
     */
889 View Code Duplication
    public function testLoadContentThrowsNotFoundException()
890
    {
891
        $repository = $this->getRepository();
892
893
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
894
        /* BEGIN: Use Case */
895
        $contentService = $repository->getContentService();
896
897
        // This call will fail with a "NotFoundException"
898
        $contentService->loadContent($nonExistentContentId);
899
        /* END: Use Case */
900
    }
901
902
    /**
903
     * Data provider for testLoadContentByRemoteId().
904
     *
905
     * @return array
906
     */
907
    public function contentRemoteIdVersionLanguageProvider()
908
    {
909
        return [
910
            ['f5c88a2209584891056f987fd965b0ba', null, null],
911
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], null],
912
            ['f5c88a2209584891056f987fd965b0ba', null, 1],
913
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], 1],
914
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, null],
915
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], null],
916
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, 1],
917
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], 1],
918
        ];
919
    }
920
921
    /**
922
     * Test for the loadContentByRemoteId() method.
923
     *
924
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId
925
     * @dataProvider contentRemoteIdVersionLanguageProvider
926
     *
927
     * @param string $remoteId
928
     * @param array|null $languages
929
     * @param int $versionNo
930
     */
931
    public function testLoadContentByRemoteId($remoteId, $languages, $versionNo)
932
    {
933
        $repository = $this->getRepository();
934
935
        $contentService = $repository->getContentService();
936
937
        $content = $contentService->loadContentByRemoteId($remoteId, $languages, $versionNo);
938
939
        $this->assertInstanceOf(
940
            Content::class,
941
            $content
942
        );
943
944
        $this->assertEquals($remoteId, $content->contentInfo->remoteId);
945
        if ($languages !== null) {
946
            $this->assertEquals($languages, $content->getVersionInfo()->languageCodes);
947
        }
948
        $this->assertEquals($versionNo ?: 1, $content->getVersionInfo()->versionNo);
949
    }
950
951
    /**
952
     * Test for the loadContentByRemoteId() method.
953
     *
954
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
955
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
956
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
957
     */
958
    public function testLoadContentByRemoteIdThrowsNotFoundException()
959
    {
960
        $repository = $this->getRepository();
961
962
        /* BEGIN: Use Case */
963
        $contentService = $repository->getContentService();
964
965
        // This call will fail with a "NotFoundException", because no content
966
        // object exists for the given remoteId
967
        $contentService->loadContentByRemoteId('a1b1c1d1e1f1a2b2c2d2e2f2a3b3c3d3');
968
        /* END: Use Case */
969
    }
970
971
    /**
972
     * Test for the publishVersion() method.
973
     *
974
     * @return \eZ\Publish\API\Repository\Values\Content\Content
975
     *
976
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
977
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
978
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
979
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
980
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
981
     * @group user
982
     * @group field-type
983
     */
984
    public function testPublishVersion()
985
    {
986
        $time = time();
987
        /* BEGIN: Use Case */
988
        $content = $this->createContentVersion1();
989
        /* END: Use Case */
990
991
        $this->assertInstanceOf(Content::class, $content);
992
        $this->assertTrue($content->contentInfo->published);
993
        $this->assertEquals(VersionInfo::STATUS_PUBLISHED, $content->versionInfo->status);
994
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->publishedDate->getTimestamp());
995
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->modificationDate->getTimestamp());
996
        $this->assertTrue($content->versionInfo->isPublished());
997
        $this->assertFalse($content->versionInfo->isDraft());
998
        $this->assertFalse($content->versionInfo->isArchived());
999
1000
        return $content;
1001
    }
1002
1003
    /**
1004
     * Test for the publishVersion() method.
1005
     *
1006
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1007
     *
1008
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1009
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1010
     */
1011
    public function testPublishVersionSetsExpectedContentInfo($content)
1012
    {
1013
        $this->assertEquals(
1014
            array(
1015
                $content->id,
1016
                true,
1017
                1,
1018
                'abcdef0123456789abcdef0123456789',
1019
                'eng-US',
1020
                $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...
1021
                true,
1022
            ),
1023
            array(
1024
                $content->contentInfo->id,
1025
                $content->contentInfo->alwaysAvailable,
1026
                $content->contentInfo->currentVersionNo,
1027
                $content->contentInfo->remoteId,
1028
                $content->contentInfo->mainLanguageCode,
1029
                $content->contentInfo->ownerId,
1030
                $content->contentInfo->published,
1031
            )
1032
        );
1033
1034
        $this->assertNotNull($content->contentInfo->mainLocationId);
1035
        $date = new \DateTime('1984/01/01');
1036
        $this->assertGreaterThan(
1037
            $date->getTimestamp(),
1038
            $content->contentInfo->publishedDate->getTimestamp()
1039
        );
1040
    }
1041
1042
    /**
1043
     * Test for the publishVersion() method.
1044
     *
1045
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1046
     *
1047
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1048
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1049
     */
1050
    public function testPublishVersionSetsExpectedVersionInfo($content)
1051
    {
1052
        $this->assertEquals(
1053
            array(
1054
                $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...
1055
                'eng-US',
1056
                VersionInfo::STATUS_PUBLISHED,
1057
                1,
1058
            ),
1059
            array(
1060
                $content->getVersionInfo()->creatorId,
1061
                $content->getVersionInfo()->initialLanguageCode,
1062
                $content->getVersionInfo()->status,
1063
                $content->getVersionInfo()->versionNo,
1064
            )
1065
        );
1066
1067
        $date = new \DateTime('1984/01/01');
1068
        $this->assertGreaterThan(
1069
            $date->getTimestamp(),
1070
            $content->getVersionInfo()->modificationDate->getTimestamp()
1071
        );
1072
1073
        $this->assertNotNull($content->getVersionInfo()->modificationDate);
1074
        $this->assertTrue($content->getVersionInfo()->isPublished());
1075
        $this->assertFalse($content->getVersionInfo()->isDraft());
1076
        $this->assertFalse($content->getVersionInfo()->isArchived());
1077
    }
1078
1079
    /**
1080
     * Test for the publishVersion() method.
1081
     *
1082
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1083
     *
1084
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1085
     * @depends testPublishVersion
1086
     */
1087
    public function testPublishVersionSetsExpectedContentType($content)
1088
    {
1089
        $contentType = $content->getContentType();
1090
1091
        $this->assertEquals(
1092
            [
1093
                $contentType->id,
1094
                // won't be a match as it's set to true in createContentDraftVersion1()
1095
                //$contentType->defaultAlwaysAvailable,
1096
                //$contentType->defaultSortField,
1097
                //$contentType->defaultSortOrder,
1098
            ],
1099
            [
1100
                $content->contentInfo->contentTypeId,
1101
                //$content->contentInfo->alwaysAvailable,
1102
                //$location->sortField,
1103
                //$location->sortOrder,
1104
            ]
1105
        );
1106
    }
1107
1108
    /**
1109
     * Test for the publishVersion() method.
1110
     *
1111
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1112
     *
1113
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1114
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
1115
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1116
     */
1117
    public function testPublishVersionCreatesLocationsDefinedOnCreate()
1118
    {
1119
        $repository = $this->getRepository();
1120
1121
        /* BEGIN: Use Case */
1122
        $content = $this->createContentVersion1();
1123
        /* END: Use Case */
1124
1125
        $locationService = $repository->getLocationService();
1126
        $location = $locationService->loadLocationByRemoteId(
1127
            '0123456789abcdef0123456789abcdef'
1128
        );
1129
1130
        $this->assertEquals(
1131
            $location->getContentInfo(),
1132
            $content->getVersionInfo()->getContentInfo()
1133
        );
1134
1135
        return array($content, $location);
1136
    }
1137
1138
    /**
1139
     * Test for the publishVersion() method.
1140
     *
1141
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1142
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionCreatesLocationsDefinedOnCreate
1143
     */
1144
    public function testCreateContentWithLocationCreateParameterCreatesExpectedLocation(array $testData)
1145
    {
1146
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
1147
        /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
1148
        list($content, $location) = $testData;
1149
1150
        $parentLocationId = $this->generateId('location', 56);
1151
        $parentLocation = $this->getRepository()->getLocationService()->loadLocation($parentLocationId);
1152
        $mainLocationId = $content->getVersionInfo()->getContentInfo()->mainLocationId;
1153
1154
        $this->assertPropertiesCorrect(
1155
            array(
1156
                'id' => $mainLocationId,
1157
                'priority' => 23,
1158
                'hidden' => true,
1159
                'invisible' => true,
1160
                'remoteId' => '0123456789abcdef0123456789abcdef',
1161
                'parentLocationId' => $parentLocationId,
1162
                'pathString' => $parentLocation->pathString . $mainLocationId . '/',
1163
                'depth' => $parentLocation->depth + 1,
1164
                'sortField' => Location::SORT_FIELD_NODE_ID,
1165
                'sortOrder' => Location::SORT_ORDER_DESC,
1166
            ),
1167
            $location
1168
        );
1169
    }
1170
1171
    /**
1172
     * Test for the publishVersion() method.
1173
     *
1174
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1175
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1176
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1177
     */
1178 View Code Duplication
    public function testPublishVersionThrowsBadStateException()
1179
    {
1180
        $repository = $this->getRepository();
1181
1182
        $contentService = $repository->getContentService();
1183
1184
        /* BEGIN: Use Case */
1185
        $draft = $this->createContentDraftVersion1();
1186
1187
        // Publish the content draft
1188
        $contentService->publishVersion($draft->getVersionInfo());
1189
1190
        // This call will fail with a "BadStateException", because the version
1191
        // is already published.
1192
        $contentService->publishVersion($draft->getVersionInfo());
1193
        /* END: Use Case */
1194
    }
1195
1196
    /**
1197
     * Test that publishVersion() does not affect publishedDate (assuming previous version exists).
1198
     *
1199
     * @covers \eZ\Publish\API\Repository\ContentService::publishVersion
1200
     */
1201
    public function testPublishVersionDoesNotChangePublishedDate()
1202
    {
1203
        $repository = $this->getRepository();
1204
1205
        $contentService = $repository->getContentService();
1206
1207
        $publishedContent = $this->createContentVersion1();
1208
1209
        // force timestamps to differ
1210
        sleep(1);
1211
1212
        $contentDraft = $contentService->createContentDraft($publishedContent->contentInfo);
1213
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1214
        $contentUpdateStruct->setField('name', 'New name');
1215
        $contentDraft = $contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
1216
        $republishedContent = $contentService->publishVersion($contentDraft->versionInfo);
1217
1218
        $this->assertEquals(
1219
            $publishedContent->contentInfo->publishedDate->getTimestamp(),
1220
            $republishedContent->contentInfo->publishedDate->getTimestamp()
1221
        );
1222
        $this->assertGreaterThan(
1223
            $publishedContent->contentInfo->modificationDate->getTimestamp(),
1224
            $republishedContent->contentInfo->modificationDate->getTimestamp()
1225
        );
1226
    }
1227
1228
    /**
1229
     * Test for the createContentDraft() method.
1230
     *
1231
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1232
     *
1233
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1234
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1235
     * @group user
1236
     */
1237
    public function testCreateContentDraft()
1238
    {
1239
        $repository = $this->getRepository();
1240
1241
        $contentService = $repository->getContentService();
1242
1243
        /* BEGIN: Use Case */
1244
        $content = $this->createContentVersion1();
1245
1246
        // Now we create a new draft from the published content
1247
        $draftedContent = $contentService->createContentDraft($content->contentInfo);
1248
        /* END: Use Case */
1249
1250
        $this->assertInstanceOf(
1251
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1252
            $draftedContent
1253
        );
1254
1255
        return $draftedContent;
1256
    }
1257
1258
    /**
1259
     * Test for the createContentDraft() method.
1260
     *
1261
     * Test that editor has access to edit own draft.
1262
     * Note: Editors have access to version_read, which is needed to load content drafts.
1263
     *
1264
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1265
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1266
     * @group user
1267
     */
1268 View Code Duplication
    public function testCreateContentDraftAndLoadAccess()
1269
    {
1270
        $repository = $this->getRepository();
1271
1272
        /* BEGIN: Use Case */
1273
        $user = $this->createUserVersion1();
1274
1275
        // Set new editor as user
1276
        $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...
1277
1278
        // Create draft
1279
        $draft = $this->createContentDraftVersion1(2, 'folder');
1280
1281
        // Try to load the draft
1282
        $contentService = $repository->getContentService();
1283
        $loadedDraft = $contentService->loadContent($draft->id);
1284
1285
        /* END: Use Case */
1286
1287
        $this->assertEquals($draft->id, $loadedDraft->id);
1288
    }
1289
1290
    /**
1291
     * Test for the createContentDraft() method.
1292
     *
1293
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1294
     *
1295
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1296
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1297
     */
1298
    public function testCreateContentDraftSetsExpectedProperties($draft)
1299
    {
1300
        $this->assertEquals(
1301
            array(
1302
                'fieldCount' => 2,
1303
                'relationCount' => 0,
1304
            ),
1305
            array(
1306
                'fieldCount' => count($draft->getFields()),
1307
                'relationCount' => count($this->getRepository()->getContentService()->loadRelations($draft->getVersionInfo())),
1308
            )
1309
        );
1310
    }
1311
1312
    /**
1313
     * Test for the createContentDraft() method.
1314
     *
1315
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1316
     *
1317
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1318
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1319
     */
1320
    public function testCreateContentDraftSetsContentInfo($draft)
1321
    {
1322
        $contentInfo = $draft->contentInfo;
1323
1324
        $this->assertEquals(
1325
            array(
1326
                $draft->id,
1327
                true,
1328
                1,
1329
                'eng-US',
1330
                $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...
1331
                'abcdef0123456789abcdef0123456789',
1332
                1,
1333
            ),
1334
            array(
1335
                $contentInfo->id,
1336
                $contentInfo->alwaysAvailable,
1337
                $contentInfo->currentVersionNo,
1338
                $contentInfo->mainLanguageCode,
1339
                $contentInfo->ownerId,
1340
                $contentInfo->remoteId,
1341
                $contentInfo->sectionId,
1342
            )
1343
        );
1344
    }
1345
1346
    /**
1347
     * Test for the createContentDraft() method.
1348
     *
1349
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1350
     *
1351
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1352
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1353
     */
1354
    public function testCreateContentDraftSetsVersionInfo($draft)
1355
    {
1356
        $versionInfo = $draft->getVersionInfo();
1357
1358
        $this->assertEquals(
1359
            array(
1360
                '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...
1361
                'initialLanguageCode' => 'eng-US',
1362
                'languageCodes' => array(0 => 'eng-US'),
1363
                'status' => VersionInfo::STATUS_DRAFT,
1364
                'versionNo' => 2,
1365
            ),
1366
            array(
1367
                'creatorId' => $versionInfo->creatorId,
1368
                'initialLanguageCode' => $versionInfo->initialLanguageCode,
1369
                'languageCodes' => $versionInfo->languageCodes,
1370
                'status' => $versionInfo->status,
1371
                'versionNo' => $versionInfo->versionNo,
1372
            )
1373
        );
1374
        $this->assertTrue($versionInfo->isDraft());
1375
        $this->assertFalse($versionInfo->isPublished());
1376
        $this->assertFalse($versionInfo->isArchived());
1377
    }
1378
1379
    /**
1380
     * Test for the createContentDraft() method.
1381
     *
1382
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1383
     *
1384
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1385
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1386
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
1387
     */
1388 View Code Duplication
    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...
1389
    {
1390
        $repository = $this->getRepository();
1391
1392
        $contentService = $repository->getContentService();
1393
1394
        /* BEGIN: Use Case */
1395
        $content = $this->createContentVersion1();
1396
1397
        // Now we create a new draft from the published content
1398
        $contentService->createContentDraft($content->contentInfo);
1399
1400
        // This call will still load the published version
1401
        $versionInfoPublished = $contentService->loadVersionInfo($content->contentInfo);
1402
        /* END: Use Case */
1403
1404
        $this->assertEquals(1, $versionInfoPublished->versionNo);
1405
    }
1406
1407
    /**
1408
     * Test for the createContentDraft() method.
1409
     *
1410
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1411
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
1412
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1413
     */
1414 View Code Duplication
    public function testCreateContentDraftLoadContentStillLoadsPublishedVersion()
1415
    {
1416
        $repository = $this->getRepository();
1417
1418
        $contentService = $repository->getContentService();
1419
1420
        /* BEGIN: Use Case */
1421
        $content = $this->createContentVersion1();
1422
1423
        // Now we create a new draft from the published content
1424
        $contentService->createContentDraft($content->contentInfo);
1425
1426
        // This call will still load the published content version
1427
        $contentPublished = $contentService->loadContent($content->id);
1428
        /* END: Use Case */
1429
1430
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1431
    }
1432
1433
    /**
1434
     * Test for the createContentDraft() method.
1435
     *
1436
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1437
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
1438
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1439
     */
1440 View Code Duplication
    public function testCreateContentDraftLoadContentByRemoteIdStillLoadsPublishedVersion()
1441
    {
1442
        $repository = $this->getRepository();
1443
1444
        $contentService = $repository->getContentService();
1445
1446
        /* BEGIN: Use Case */
1447
        $content = $this->createContentVersion1();
1448
1449
        // Now we create a new draft from the published content
1450
        $contentService->createContentDraft($content->contentInfo);
1451
1452
        // This call will still load the published content version
1453
        $contentPublished = $contentService->loadContentByRemoteId('abcdef0123456789abcdef0123456789');
1454
        /* END: Use Case */
1455
1456
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1457
    }
1458
1459
    /**
1460
     * Test for the createContentDraft() method.
1461
     *
1462
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1463
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
1464
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1465
     */
1466 View Code Duplication
    public function testCreateContentDraftLoadContentByContentInfoStillLoadsPublishedVersion()
1467
    {
1468
        $repository = $this->getRepository();
1469
1470
        $contentService = $repository->getContentService();
1471
1472
        /* BEGIN: Use Case */
1473
        $content = $this->createContentVersion1();
1474
1475
        // Now we create a new draft from the published content
1476
        $contentService->createContentDraft($content->contentInfo);
1477
1478
        // This call will still load the published content version
1479
        $contentPublished = $contentService->loadContentByContentInfo($content->contentInfo);
1480
        /* END: Use Case */
1481
1482
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1483
    }
1484
1485
    /**
1486
     * Test for the newContentUpdateStruct() method.
1487
     *
1488
     * @covers \eZ\Publish\API\Repository\ContentService::newContentUpdateStruct
1489
     * @group user
1490
     */
1491
    public function testNewContentUpdateStruct()
1492
    {
1493
        $repository = $this->getRepository();
1494
1495
        /* BEGIN: Use Case */
1496
        $contentService = $repository->getContentService();
1497
1498
        $updateStruct = $contentService->newContentUpdateStruct();
1499
        /* END: Use Case */
1500
1501
        $this->assertInstanceOf(
1502
            ContentUpdateStruct::class,
1503
            $updateStruct
1504
        );
1505
1506
        $this->assertPropertiesCorrect(
1507
            [
1508
                'initialLanguageCode' => null,
1509
                'fields' => [],
1510
            ],
1511
            $updateStruct
1512
        );
1513
    }
1514
1515
    /**
1516
     * Test for the updateContent() method.
1517
     *
1518
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1519
     *
1520
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1521
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1522
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1523
     * @group user
1524
     * @group field-type
1525
     */
1526
    public function testUpdateContent()
1527
    {
1528
        /* BEGIN: Use Case */
1529
        $draftVersion2 = $this->createUpdatedDraftVersion2();
1530
        /* END: Use Case */
1531
1532
        $this->assertInstanceOf(
1533
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1534
            $draftVersion2
1535
        );
1536
1537
        $this->assertEquals(
1538
            $this->generateId('user', 10),
1539
            $draftVersion2->versionInfo->creatorId,
1540
            'creatorId is not properly set on new Version'
1541
        );
1542
1543
        return $draftVersion2;
1544
    }
1545
1546
    /**
1547
     * Test for the updateContent_WithDifferentUser() method.
1548
     *
1549
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1550
     *
1551
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1552
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1553
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1554
     * @group user
1555
     * @group field-type
1556
     */
1557
    public function testUpdateContentWithDifferentUser()
1558
    {
1559
        /* BEGIN: Use Case */
1560
        $arrayWithDraftVersion2 = $this->createUpdatedDraftVersion2NotAdmin();
1561
        /* END: Use Case */
1562
1563
        $this->assertInstanceOf(
1564
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1565
            $arrayWithDraftVersion2[0]
1566
        );
1567
1568
        $this->assertEquals(
1569
            $this->generateId('user', $arrayWithDraftVersion2[1]),
1570
            $arrayWithDraftVersion2[0]->versionInfo->creatorId,
1571
            'creatorId is not properly set on new Version'
1572
        );
1573
1574
        return $arrayWithDraftVersion2[0];
1575
    }
1576
1577
    /**
1578
     * Test for the updateContent() method.
1579
     *
1580
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1581
     *
1582
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1583
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1584
     */
1585
    public function testUpdateContentSetsExpectedFields($content)
1586
    {
1587
        $actual = $this->normalizeFields($content->getFields());
1588
1589
        $expected = array(
1590
            new Field(
1591
                array(
1592
                    'id' => 0,
1593
                    'value' => true,
1594
                    'languageCode' => 'eng-GB',
1595
                    'fieldDefIdentifier' => 'description',
1596
                    'fieldTypeIdentifier' => 'ezrichtext',
1597
                )
1598
            ),
1599
            new Field(
1600
                array(
1601
                    'id' => 0,
1602
                    'value' => true,
1603
                    'languageCode' => 'eng-US',
1604
                    'fieldDefIdentifier' => 'description',
1605
                    'fieldTypeIdentifier' => 'ezrichtext',
1606
                )
1607
            ),
1608
            new Field(
1609
                array(
1610
                    'id' => 0,
1611
                    'value' => true,
1612
                    'languageCode' => 'eng-GB',
1613
                    'fieldDefIdentifier' => 'name',
1614
                    'fieldTypeIdentifier' => 'ezstring',
1615
                )
1616
            ),
1617
            new Field(
1618
                array(
1619
                    'id' => 0,
1620
                    'value' => true,
1621
                    'languageCode' => 'eng-US',
1622
                    'fieldDefIdentifier' => 'name',
1623
                    'fieldTypeIdentifier' => 'ezstring',
1624
                )
1625
            ),
1626
        );
1627
1628
        $this->assertEquals($expected, $actual);
1629
    }
1630
1631
    /**
1632
     * Test for the updateContent() method.
1633
     *
1634
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1635
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1636
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1637
     */
1638
    public function testUpdateContentThrowsBadStateException()
1639
    {
1640
        $repository = $this->getRepository();
1641
1642
        $contentService = $repository->getContentService();
1643
1644
        /* BEGIN: Use Case */
1645
        $content = $this->createContentVersion1();
1646
1647
        // Now create an update struct and modify some fields
1648
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1649
        $contentUpdateStruct->setField('title', 'An awesome² story about ezp.');
1650
        $contentUpdateStruct->setField('title', 'An awesome²³ story about ezp.', 'eng-GB');
1651
1652
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1653
1654
        // This call will fail with a "BadStateException", because $publishedContent
1655
        // is not a draft.
1656
        $contentService->updateContent(
1657
            $content->getVersionInfo(),
1658
            $contentUpdateStruct
1659
        );
1660
        /* END: Use Case */
1661
    }
1662
1663
    /**
1664
     * Test for the updateContent() method.
1665
     *
1666
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1667
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1668
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1669
     */
1670 View Code Duplication
    public function testUpdateContentThrowsInvalidArgumentExceptionWhenFieldTypeDoesNotAccept()
1671
    {
1672
        $repository = $this->getRepository();
1673
1674
        $contentService = $repository->getContentService();
1675
1676
        /* BEGIN: Use Case */
1677
        $draft = $this->createContentDraftVersion1();
1678
1679
        // Now create an update struct and modify some fields
1680
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1681
        // The name field does not accept a stdClass object as its input
1682
        $contentUpdateStruct->setField('name', new \stdClass(), 'eng-US');
1683
1684
        // Throws an InvalidArgumentException, since the value for field "name"
1685
        // is not accepted
1686
        $contentService->updateContent(
1687
            $draft->getVersionInfo(),
1688
            $contentUpdateStruct
1689
        );
1690
        /* END: Use Case */
1691
    }
1692
1693
    /**
1694
     * Test for the updateContent() method.
1695
     *
1696
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1697
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1698
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1699
     */
1700 View Code Duplication
    public function testUpdateContentWhenMandatoryFieldIsEmpty()
1701
    {
1702
        $repository = $this->getRepository();
1703
1704
        $contentService = $repository->getContentService();
1705
1706
        /* BEGIN: Use Case */
1707
        $draft = $this->createContentDraftVersion1();
1708
1709
        // Now create an update struct and set a mandatory field to null
1710
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1711
        $contentUpdateStruct->setField('name', null);
1712
1713
        // Don't set this, then the above call without languageCode will fail
1714
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1715
1716
        // This call will fail with a "ContentFieldValidationException", because the
1717
        // mandatory "name" field is empty.
1718
        $contentService->updateContent(
1719
            $draft->getVersionInfo(),
1720
            $contentUpdateStruct
1721
        );
1722
        /* END: Use Case */
1723
    }
1724
1725
    /**
1726
     * Test for the updateContent() method.
1727
     *
1728
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1729
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1730
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1731
     */
1732
    public function testUpdateContentThrowsContentFieldValidationException()
1733
    {
1734
        $repository = $this->getRepository();
1735
1736
        /* BEGIN: Use Case */
1737
        $contentTypeService = $repository->getContentTypeService();
1738
        $contentService = $repository->getContentService();
1739
1740
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1741
1742
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1743
        $contentCreate->setField('name', 'An awesome Sidelfingen folder');
1744
1745
        $draft = $contentService->createContent($contentCreate);
1746
1747
        $contentUpdate = $contentService->newContentUpdateStruct();
1748
        // Violates string length constraint
1749
        $contentUpdate->setField('short_name', str_repeat('a', 200), 'eng-US');
1750
1751
        // Throws ContentFieldValidationException because the string length
1752
        // validation of the field "short_name" fails
1753
        $contentService->updateContent($draft->getVersionInfo(), $contentUpdate);
1754
        /* END: Use Case */
1755
    }
1756
1757
    /**
1758
     * Test for the updateContent() method.
1759
     *
1760
     * @covers \eZ\Publish\API\Repository\ContentService::updateContent()
1761
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1762
     */
1763
    public function testUpdateContentValidatorIgnoresRequiredFieldsOfNotUpdatedLanguages()
1764
    {
1765
        $repository = $this->getRepository();
1766
        /* BEGIN: Use Case */
1767
        $contentTypeService = $repository->getContentTypeService();
1768
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1769
1770
        // Create multilangual content
1771
        $contentService = $repository->getContentService();
1772
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1773
        $contentCreate->setField('name', 'An awesome Sidelfingen folder', 'eng-US');
1774
        $contentCreate->setField('name', 'An awesome Sidelfingen folder', 'eng-GB');
1775
1776
        $contentDraft = $contentService->createContent($contentCreate);
1777
1778
        // 2. Update content type definition
1779
        $contentTypeDraft = $contentTypeService->createContentTypeDraft($contentType);
1780
1781
        $fieldDefinition = $contentType->getFieldDefinition('description');
1782
        $fieldDefinitionUpdate = $contentTypeService->newFieldDefinitionUpdateStruct();
1783
        $fieldDefinitionUpdate->identifier = 'description';
1784
        $fieldDefinitionUpdate->isRequired = true;
1785
1786
        $contentTypeService->updateFieldDefinition(
1787
            $contentTypeDraft,
1788
            $fieldDefinition,
1789
            $fieldDefinitionUpdate
1790
        );
1791
        $contentTypeService->publishContentTypeDraft($contentTypeDraft);
1792
1793
        // 3. Update only eng-US translation
1794
        $description = new DOMDocument();
1795
        $description->loadXML(<<<XML
1796
<?xml version="1.0" encoding="UTF-8"?>
1797
<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">
1798
    <para>Lorem ipsum dolor</para>
1799
</section>
1800
XML
1801
        );
1802
1803
        $contentUpdate = $contentService->newContentUpdateStruct();
1804
        $contentUpdate->setField('name', 'An awesome Sidelfingen folder (updated)', 'eng-US');
1805
        $contentUpdate->setField('description', $description);
1806
1807
        $contentService->updateContent($contentDraft->getVersionInfo(), $contentUpdate);
1808
        /* END: Use Case */
1809
    }
1810
1811
    /**
1812
     * Test for the updateContent() method.
1813
     *
1814
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1815
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1816
     */
1817
    public function testUpdateContentWithNotUpdatingMandatoryField()
1818
    {
1819
        $repository = $this->getRepository();
1820
1821
        $contentService = $repository->getContentService();
1822
1823
        /* BEGIN: Use Case */
1824
        $draft = $this->createContentDraftVersion1();
1825
1826
        // Now create an update struct which does not overwrite mandatory
1827
        // fields
1828
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1829
        $contentUpdateStruct->setField(
1830
            'description',
1831
            '<?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"/>'
1832
        );
1833
1834
        // Don't set this, then the above call without languageCode will fail
1835
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1836
1837
        // This will only update the "description" field in the "eng-US"
1838
        // language
1839
        $updatedDraft = $contentService->updateContent(
1840
            $draft->getVersionInfo(),
1841
            $contentUpdateStruct
1842
        );
1843
        /* END: Use Case */
1844
1845
        foreach ($updatedDraft->getFields() as $field) {
1846
            if ($field->languageCode === 'eng-US' && $field->fieldDefIdentifier === 'name' && $field->value !== null) {
1847
                // Found field
1848
                return;
1849
            }
1850
        }
1851
        $this->fail(
1852
            'Field with identifier "name" in language "eng-US" could not be found or has empty value.'
1853
        );
1854
    }
1855
1856
    /**
1857
     * Test for the createContentDraft() method.
1858
     *
1859
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
1860
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1861
     */
1862 View Code Duplication
    public function testCreateContentDraftWithSecondParameter()
1863
    {
1864
        $repository = $this->getRepository();
1865
1866
        $contentService = $repository->getContentService();
1867
1868
        /* BEGIN: Use Case */
1869
        $contentVersion2 = $this->createContentVersion2();
1870
1871
        // Now we create a new draft from the initial version
1872
        $draftedContentReloaded = $contentService->createContentDraft(
1873
            $contentVersion2->contentInfo,
1874
            $contentVersion2->getVersionInfo()
1875
        );
1876
        /* END: Use Case */
1877
1878
        $this->assertEquals(3, $draftedContentReloaded->getVersionInfo()->versionNo);
1879
    }
1880
1881
    /**
1882
     * Test for the createContentDraft() method with third parameter.
1883
     *
1884
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1885
     */
1886 View Code Duplication
    public function testCreateContentDraftWithThirdParameter()
1887
    {
1888
        $repository = $this->getRepository();
1889
1890
        $contentService = $repository->getContentService();
1891
1892
        $content = $contentService->loadContent(4);
1893
        $user = $this->createUserVersion1();
1894
1895
        $draftContent = $contentService->createContentDraft(
1896
            $content->contentInfo,
1897
            $content->getVersionInfo(),
1898
            $user
1899
        );
1900
1901
        $this->assertInstanceOf(
1902
            Content::class,
1903
            $draftContent
1904
        );
1905
    }
1906
1907
    /**
1908
     * Test for the publishVersion() method.
1909
     *
1910
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1911
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1912
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1913
     */
1914 View Code Duplication
    public function testPublishVersionFromContentDraft()
1915
    {
1916
        $repository = $this->getRepository();
1917
1918
        $contentService = $repository->getContentService();
1919
1920
        /* BEGIN: Use Case */
1921
        $contentVersion2 = $this->createContentVersion2();
1922
        /* END: Use Case */
1923
1924
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo);
1925
1926
        $this->assertEquals(
1927
            array(
1928
                'status' => VersionInfo::STATUS_PUBLISHED,
1929
                'versionNo' => 2,
1930
            ),
1931
            array(
1932
                'status' => $versionInfo->status,
1933
                'versionNo' => $versionInfo->versionNo,
1934
            )
1935
        );
1936
        $this->assertTrue($versionInfo->isPublished());
1937
        $this->assertFalse($versionInfo->isDraft());
1938
        $this->assertFalse($versionInfo->isArchived());
1939
    }
1940
1941
    /**
1942
     * Test for the publishVersion() method.
1943
     *
1944
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1945
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1946
     */
1947 View Code Duplication
    public function testPublishVersionFromContentDraftArchivesOldVersion()
1948
    {
1949
        $repository = $this->getRepository();
1950
1951
        $contentService = $repository->getContentService();
1952
1953
        /* BEGIN: Use Case */
1954
        $contentVersion2 = $this->createContentVersion2();
1955
        /* END: Use Case */
1956
1957
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo, 1);
1958
1959
        $this->assertEquals(
1960
            array(
1961
                'status' => VersionInfo::STATUS_ARCHIVED,
1962
                'versionNo' => 1,
1963
            ),
1964
            array(
1965
                'status' => $versionInfo->status,
1966
                'versionNo' => $versionInfo->versionNo,
1967
            )
1968
        );
1969
        $this->assertTrue($versionInfo->isArchived());
1970
        $this->assertFalse($versionInfo->isDraft());
1971
        $this->assertFalse($versionInfo->isPublished());
1972
    }
1973
1974
    /**
1975
     * Test for the publishVersion() method.
1976
     *
1977
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1978
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1979
     */
1980
    public function testPublishVersionFromContentDraftUpdatesContentInfoCurrentVersion()
1981
    {
1982
        /* BEGIN: Use Case */
1983
        $contentVersion2 = $this->createContentVersion2();
1984
        /* END: Use Case */
1985
1986
        $this->assertEquals(2, $contentVersion2->contentInfo->currentVersionNo);
1987
    }
1988
1989
    /**
1990
     * Test for the publishVersion() method.
1991
     *
1992
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1993
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1994
     */
1995 View Code Duplication
    public function testPublishVersionFromOldContentDraftArchivesNewerVersionNo()
1996
    {
1997
        $repository = $this->getRepository();
1998
1999
        $contentService = $repository->getContentService();
2000
2001
        /* BEGIN: Use Case */
2002
        $content = $this->createContentVersion1();
2003
2004
        // Create a new draft with versionNo = 2
2005
        $draftedContentVersion2 = $contentService->createContentDraft($content->contentInfo);
2006
2007
        // Create another new draft with versionNo = 3
2008
        $draftedContentVersion3 = $contentService->createContentDraft($content->contentInfo);
2009
2010
        // Publish draft with versionNo = 3
2011
        $contentService->publishVersion($draftedContentVersion3->getVersionInfo());
2012
2013
        // Publish the first draft with versionNo = 2
2014
        // currentVersionNo is now 2, versionNo 3 will be archived
2015
        $publishedDraft = $contentService->publishVersion($draftedContentVersion2->getVersionInfo());
2016
        /* END: Use Case */
2017
2018
        $this->assertEquals(2, $publishedDraft->contentInfo->currentVersionNo);
2019
    }
2020
2021
    /**
2022
     * Test for the publishVersion() method, and that it creates limited archives.
2023
     *
2024
     * @todo Adapt this when per content type archive limited is added on repository Content Type model.
2025
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
2026
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2027
     */
2028
    public function testPublishVersionNotCreatingUnlimitedArchives()
2029
    {
2030
        $repository = $this->getRepository();
2031
2032
        $contentService = $repository->getContentService();
2033
2034
        $content = $this->createContentVersion1();
2035
2036
        // load first to make sure list gets updated also (cache)
2037
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
2038
        $this->assertEquals(1, count($versionInfoList));
2039
        $this->assertEquals(1, $versionInfoList[0]->versionNo);
2040
2041
        // Create a new draft with versionNo = 2
2042
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
2043
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
2044
2045
        // Create a new draft with versionNo = 3
2046
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
2047
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
2048
2049
        // Create a new draft with versionNo = 4
2050
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
2051
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
2052
2053
        // Create a new draft with versionNo = 5
2054
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
2055
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
2056
2057
        // Create a new draft with versionNo = 6
2058
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
2059
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
2060
2061
        // Create a new draft with versionNo = 7
2062
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
2063
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
2064
2065
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
2066
2067
        $this->assertEquals(6, count($versionInfoList));
2068
        $this->assertEquals(2, $versionInfoList[0]->versionNo);
2069
        $this->assertEquals(7, $versionInfoList[5]->versionNo);
2070
2071
        $this->assertEquals(
2072
            [
2073
                VersionInfo::STATUS_ARCHIVED,
2074
                VersionInfo::STATUS_ARCHIVED,
2075
                VersionInfo::STATUS_ARCHIVED,
2076
                VersionInfo::STATUS_ARCHIVED,
2077
                VersionInfo::STATUS_ARCHIVED,
2078
                VersionInfo::STATUS_PUBLISHED,
2079
            ],
2080
            [
2081
                $versionInfoList[0]->status,
2082
                $versionInfoList[1]->status,
2083
                $versionInfoList[2]->status,
2084
                $versionInfoList[3]->status,
2085
                $versionInfoList[4]->status,
2086
                $versionInfoList[5]->status,
2087
            ]
2088
        );
2089
    }
2090
2091
    /**
2092
     * Test for the newContentMetadataUpdateStruct() method.
2093
     *
2094
     * @covers \eZ\Publish\API\Repository\ContentService::newContentMetadataUpdateStruct
2095
     * @group user
2096
     */
2097
    public function testNewContentMetadataUpdateStruct()
2098
    {
2099
        $repository = $this->getRepository();
2100
2101
        /* BEGIN: Use Case */
2102
        $contentService = $repository->getContentService();
2103
2104
        // Creates a new metadata update struct
2105
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2106
2107
        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...
2108
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
2109
        }
2110
2111
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
2112
        $metadataUpdate->mainLanguageCode = 'eng-GB';
2113
        $metadataUpdate->alwaysAvailable = false;
2114
        /* END: Use Case */
2115
2116
        $this->assertInstanceOf(
2117
            ContentMetadataUpdateStruct::class,
2118
            $metadataUpdate
2119
        );
2120
    }
2121
2122
    /**
2123
     * Test for the updateContentMetadata() method.
2124
     *
2125
     * @return \eZ\Publish\API\Repository\Values\Content\Content
2126
     *
2127
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2128
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2129
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentMetadataUpdateStruct
2130
     * @group user
2131
     */
2132
    public function testUpdateContentMetadata()
2133
    {
2134
        $repository = $this->getRepository();
2135
2136
        $contentService = $repository->getContentService();
2137
2138
        /* BEGIN: Use Case */
2139
        $content = $this->createContentVersion1();
2140
2141
        // Creates a metadata update struct
2142
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2143
2144
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
2145
        $metadataUpdate->mainLanguageCode = 'eng-GB';
2146
        $metadataUpdate->alwaysAvailable = false;
2147
        $metadataUpdate->publishedDate = $this->createDateTime(441759600); // 1984/01/01
2148
        $metadataUpdate->modificationDate = $this->createDateTime(441759600); // 1984/01/01
2149
2150
        // Update the metadata of the published content object
2151
        $content = $contentService->updateContentMetadata(
2152
            $content->contentInfo,
2153
            $metadataUpdate
2154
        );
2155
        /* END: Use Case */
2156
2157
        $this->assertInstanceOf(
2158
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
2159
            $content
2160
        );
2161
2162
        return $content;
2163
    }
2164
2165
    /**
2166
     * Test for the updateContentMetadata() method.
2167
     *
2168
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2169
     *
2170
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2171
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2172
     */
2173
    public function testUpdateContentMetadataSetsExpectedProperties($content)
2174
    {
2175
        $contentInfo = $content->contentInfo;
2176
2177
        $this->assertEquals(
2178
            array(
2179
                'remoteId' => 'aaaabbbbccccddddeeeeffff11112222',
2180
                'sectionId' => $this->generateId('section', 1),
2181
                'alwaysAvailable' => false,
2182
                'currentVersionNo' => 1,
2183
                'mainLanguageCode' => 'eng-GB',
2184
                'modificationDate' => $this->createDateTime(441759600),
2185
                '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...
2186
                'published' => true,
2187
                'publishedDate' => $this->createDateTime(441759600),
2188
            ),
2189
            array(
2190
                'remoteId' => $contentInfo->remoteId,
2191
                'sectionId' => $contentInfo->sectionId,
2192
                'alwaysAvailable' => $contentInfo->alwaysAvailable,
2193
                'currentVersionNo' => $contentInfo->currentVersionNo,
2194
                'mainLanguageCode' => $contentInfo->mainLanguageCode,
2195
                'modificationDate' => $contentInfo->modificationDate,
2196
                'ownerId' => $contentInfo->ownerId,
2197
                'published' => $contentInfo->published,
2198
                'publishedDate' => $contentInfo->publishedDate,
2199
            )
2200
        );
2201
    }
2202
2203
    /**
2204
     * Test for the updateContentMetadata() method.
2205
     *
2206
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2207
     *
2208
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2209
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2210
     */
2211
    public function testUpdateContentMetadataNotUpdatesContentVersion($content)
2212
    {
2213
        $this->assertEquals(1, $content->getVersionInfo()->versionNo);
2214
    }
2215
2216
    /**
2217
     * Test for the updateContentMetadata() method.
2218
     *
2219
     * @covers \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2220
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2221
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2222
     */
2223
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnDuplicateRemoteId()
2224
    {
2225
        $repository = $this->getRepository();
2226
2227
        $contentService = $repository->getContentService();
2228
2229
        /* BEGIN: Use Case */
2230
        // RemoteId of the "Media" page of an eZ Publish demo installation
2231
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2232
2233
        $content = $this->createContentVersion1();
2234
2235
        // Creates a metadata update struct
2236
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2237
        $metadataUpdate->remoteId = $mediaRemoteId;
2238
2239
        // This call will fail with an "InvalidArgumentException", because the
2240
        // specified remoteId is already used by the "Media" page.
2241
        $contentService->updateContentMetadata(
2242
            $content->contentInfo,
2243
            $metadataUpdate
2244
        );
2245
        /* END: Use Case */
2246
    }
2247
2248
    /**
2249
     * Test for the updateContentMetadata() method.
2250
     *
2251
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContentMetadata
2252
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2253
     */
2254
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnNoMetadataPropertiesSet()
2255
    {
2256
        $repository = $this->getRepository();
2257
2258
        $contentService = $repository->getContentService();
2259
2260
        $contentInfo = $contentService->loadContentInfo(4);
2261
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
2262
2263
        // Throws an exception because no properties are set in $contentMetadataUpdateStruct
2264
        $contentService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
2265
    }
2266
2267
    /**
2268
     * Test for the deleteContent() method.
2269
     *
2270
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2271
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2272
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2273
     */
2274 View Code Duplication
    public function testDeleteContent()
2275
    {
2276
        $repository = $this->getRepository();
2277
2278
        $contentService = $repository->getContentService();
2279
        $locationService = $repository->getLocationService();
2280
2281
        /* BEGIN: Use Case */
2282
        $contentVersion2 = $this->createContentVersion2();
2283
2284
        // Load the locations for this content object
2285
        $locations = $locationService->loadLocations($contentVersion2->contentInfo);
2286
2287
        // This will delete the content, all versions and the associated locations
2288
        $contentService->deleteContent($contentVersion2->contentInfo);
2289
        /* END: Use Case */
2290
2291
        foreach ($locations as $location) {
2292
            $locationService->loadLocation($location->id);
2293
        }
2294
    }
2295
2296
    /**
2297
     * Test for the deleteContent() method.
2298
     *
2299
     * Test for issue EZP-21057:
2300
     * "contentService: Unable to delete a content with an empty file attribute"
2301
     *
2302
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2303
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2304
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2305
     */
2306 View Code Duplication
    public function testDeleteContentWithEmptyBinaryField()
2307
    {
2308
        $repository = $this->getRepository();
2309
2310
        $contentService = $repository->getContentService();
2311
        $locationService = $repository->getLocationService();
2312
2313
        /* BEGIN: Use Case */
2314
        $contentVersion = $this->createContentVersion1EmptyBinaryField();
2315
2316
        // Load the locations for this content object
2317
        $locations = $locationService->loadLocations($contentVersion->contentInfo);
2318
2319
        // This will delete the content, all versions and the associated locations
2320
        $contentService->deleteContent($contentVersion->contentInfo);
2321
        /* END: Use Case */
2322
2323
        foreach ($locations as $location) {
2324
            $locationService->loadLocation($location->id);
2325
        }
2326
    }
2327
2328
    /**
2329
     * Test for the loadContentDrafts() method.
2330
     *
2331
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2332
     */
2333
    public function testLoadContentDraftsReturnsEmptyArrayByDefault()
2334
    {
2335
        $repository = $this->getRepository();
2336
2337
        /* BEGIN: Use Case */
2338
        $contentService = $repository->getContentService();
2339
2340
        $contentDrafts = $contentService->loadContentDrafts();
2341
        /* END: Use Case */
2342
2343
        $this->assertSame(array(), $contentDrafts);
2344
    }
2345
2346
    /**
2347
     * Test for the loadContentDrafts() method.
2348
     *
2349
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2350
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2351
     */
2352
    public function testLoadContentDrafts()
2353
    {
2354
        $repository = $this->getRepository();
2355
2356
        /* BEGIN: Use Case */
2357
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
2358
        // of a eZ Publish demo installation.
2359
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2360
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
2361
2362
        $contentService = $repository->getContentService();
2363
2364
        // "Media" content object
2365
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2366
2367
        // "eZ Publish Demo Design ..." content object
2368
        $demoDesignContentInfo = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
2369
2370
        // Create some drafts
2371
        $contentService->createContentDraft($mediaContentInfo);
2372
        $contentService->createContentDraft($demoDesignContentInfo);
2373
2374
        // Now $contentDrafts should contain two drafted versions
2375
        $draftedVersions = $contentService->loadContentDrafts();
2376
        /* END: Use Case */
2377
2378
        $actual = array(
2379
            $draftedVersions[0]->status,
2380
            $draftedVersions[0]->getContentInfo()->remoteId,
2381
            $draftedVersions[1]->status,
2382
            $draftedVersions[1]->getContentInfo()->remoteId,
2383
        );
2384
        sort($actual, SORT_STRING);
2385
2386
        $this->assertEquals(
2387
            array(
2388
                VersionInfo::STATUS_DRAFT,
2389
                VersionInfo::STATUS_DRAFT,
2390
                $demoDesignRemoteId,
2391
                $mediaRemoteId,
2392
            ),
2393
            $actual
2394
        );
2395
    }
2396
2397
    /**
2398
     * Test for the loadContentDrafts() method.
2399
     *
2400
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
2401
     */
2402
    public function testLoadContentDraftsWithFirstParameter()
2403
    {
2404
        $repository = $this->getRepository();
2405
2406
        /* BEGIN: Use Case */
2407
        $user = $this->createUserVersion1();
2408
2409
        // Get current user
2410
        $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...
2411
2412
        // Set new editor as user
2413
        $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...
2414
2415
        // Remote id of the "Media" content object in an eZ Publish demo installation.
2416
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2417
2418
        $contentService = $repository->getContentService();
2419
2420
        // "Media" content object
2421
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2422
2423
        // Create a content draft
2424
        $contentService->createContentDraft($mediaContentInfo);
2425
2426
        // Reset to previous current user
2427
        $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...
2428
2429
        // Now $contentDrafts for the previous current user and the new user
2430
        $newCurrentUserDrafts = $contentService->loadContentDrafts($user);
2431
        $oldCurrentUserDrafts = $contentService->loadContentDrafts($oldCurrentUser);
2432
        /* END: Use Case */
2433
2434
        $this->assertSame(array(), $oldCurrentUserDrafts);
2435
2436
        $this->assertEquals(
2437
            array(
2438
                VersionInfo::STATUS_DRAFT,
2439
                $mediaRemoteId,
2440
            ),
2441
            array(
2442
                $newCurrentUserDrafts[0]->status,
2443
                $newCurrentUserDrafts[0]->getContentInfo()->remoteId,
2444
            )
2445
        );
2446
        $this->assertTrue($newCurrentUserDrafts[0]->isDraft());
2447
        $this->assertFalse($newCurrentUserDrafts[0]->isArchived());
2448
        $this->assertFalse($newCurrentUserDrafts[0]->isPublished());
2449
    }
2450
2451
    /**
2452
     * Test for the loadVersionInfo() method.
2453
     *
2454
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2455
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2456
     */
2457
    public function testLoadVersionInfoWithSecondParameter()
2458
    {
2459
        $repository = $this->getRepository();
2460
2461
        $contentService = $repository->getContentService();
2462
2463
        /* BEGIN: Use Case */
2464
        $publishedContent = $this->createContentVersion1();
2465
2466
        $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...
2467
2468
        // Will return the VersionInfo of the $draftContent
2469
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2470
        /* END: Use Case */
2471
2472
        $this->assertEquals(2, $versionInfo->versionNo);
2473
2474
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2475
        $this->assertEquals(
2476
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2477
            $versionInfo->getContentInfo()->mainLocationId
2478
        );
2479
    }
2480
2481
    /**
2482
     * Test for the loadVersionInfo() method.
2483
     *
2484
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2485
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2486
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2487
     */
2488
    public function testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter()
2489
    {
2490
        $repository = $this->getRepository();
2491
2492
        $contentService = $repository->getContentService();
2493
2494
        /* BEGIN: Use Case */
2495
        $draft = $this->createContentDraftVersion1();
2496
2497
        // This call will fail with a "NotFoundException", because not versionNo
2498
        // 2 exists for this content object.
2499
        $contentService->loadVersionInfo($draft->contentInfo, 2);
2500
        /* END: Use Case */
2501
    }
2502
2503
    /**
2504
     * Test for the loadVersionInfoById() method.
2505
     *
2506
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2507
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2508
     */
2509
    public function testLoadVersionInfoByIdWithSecondParameter()
2510
    {
2511
        $repository = $this->getRepository();
2512
2513
        $contentService = $repository->getContentService();
2514
2515
        /* BEGIN: Use Case */
2516
        $publishedContent = $this->createContentVersion1();
2517
2518
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
2519
2520
        // Will return the VersionInfo of the $draftContent
2521
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2522
        /* END: Use Case */
2523
2524
        $this->assertEquals(2, $versionInfo->versionNo);
2525
2526
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2527
        $this->assertEquals(
2528
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2529
            $versionInfo->getContentInfo()->mainLocationId
2530
        );
2531
2532
        return [
2533
            'versionInfo' => $versionInfo,
2534
            'draftContent' => $draftContent,
2535
        ];
2536
    }
2537
2538
    /**
2539
     * Test for the returned value of the loadVersionInfoById() method.
2540
     *
2541
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter
2542
     * @covers \eZ\Publish\API\Repository\ContentService::loadVersionInfoById
2543
     *
2544
     * @param array $data
2545
     */
2546
    public function testLoadVersionInfoByIdWithSecondParameterSetsExpectedVersionInfo(array $data)
2547
    {
2548
        /** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo */
2549
        $versionInfo = $data['versionInfo'];
2550
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $draftContent */
2551
        $draftContent = $data['draftContent'];
2552
2553
        $this->assertPropertiesCorrect(
2554
            [
2555
                'names' => [
2556
                    'eng-US' => 'An awesome forum',
2557
                ],
2558
                'contentInfo' => new ContentInfo([
2559
                    'id' => $draftContent->contentInfo->id,
2560
                    'contentTypeId' => 28,
2561
                    'name' => 'An awesome forum',
2562
                    'sectionId' => 1,
2563
                    'currentVersionNo' => 1,
2564
                    'published' => true,
2565
                    'ownerId' => 14,
2566
                    // this Content Object is created at the test runtime
2567
                    'modificationDate' => $versionInfo->contentInfo->modificationDate,
2568
                    'publishedDate' => $versionInfo->contentInfo->publishedDate,
2569
                    'alwaysAvailable' => 1,
2570
                    'remoteId' => 'abcdef0123456789abcdef0123456789',
2571
                    'mainLanguageCode' => 'eng-US',
2572
                    'mainLocationId' => $draftContent->contentInfo->mainLocationId,
2573
                    'status' => ContentInfo::STATUS_PUBLISHED,
2574
                ]),
2575
                'id' => $draftContent->versionInfo->id,
2576
                'versionNo' => 2,
2577
                'creatorId' => 14,
2578
                'status' => 0,
2579
                'initialLanguageCode' => 'eng-US',
2580
                'languageCodes' => [
2581
                    'eng-US',
2582
                ],
2583
            ],
2584
            $versionInfo
2585
        );
2586
    }
2587
2588
    /**
2589
     * Test for the loadVersionInfoById() method.
2590
     *
2591
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2592
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2593
     */
2594 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter()
2595
    {
2596
        $repository = $this->getRepository();
2597
2598
        $contentService = $repository->getContentService();
2599
2600
        /* BEGIN: Use Case */
2601
        $content = $this->createContentVersion1();
2602
2603
        // This call will fail with a "NotFoundException", because not versionNo
2604
        // 2 exists for this content object.
2605
        $contentService->loadVersionInfoById($content->id, 2);
2606
        /* END: Use Case */
2607
    }
2608
2609
    /**
2610
     * Test for the loadContentByVersionInfo() method.
2611
     *
2612
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
2613
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2614
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
2615
     */
2616
    public function testLoadContentByVersionInfoWithSecondParameter()
2617
    {
2618
        $repository = $this->getRepository();
2619
2620
        $sectionId = $this->generateId('section', 1);
2621
        /* BEGIN: Use Case */
2622
        $contentTypeService = $repository->getContentTypeService();
2623
2624
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2625
2626
        $contentService = $repository->getContentService();
2627
2628
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2629
2630
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2631
2632
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2633
2634
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2635
        // $sectionId contains the ID of section 1
2636
        $contentCreateStruct->sectionId = $sectionId;
2637
        $contentCreateStruct->alwaysAvailable = true;
2638
2639
        // Create a new content draft
2640
        $content = $contentService->createContent($contentCreateStruct);
2641
2642
        // Now publish this draft
2643
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2644
2645
        // Will return a content instance with fields in "eng-US"
2646
        $reloadedContent = $contentService->loadContentByVersionInfo(
2647
            $publishedContent->getVersionInfo(),
2648
            array(
2649
                'eng-GB',
2650
            ),
2651
            false
2652
        );
2653
        /* END: Use Case */
2654
2655
        $actual = array();
2656 View Code Duplication
        foreach ($reloadedContent->getFields() as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
6102
            $normalized[] = new Field(
6103
                array(
6104
                    'id' => 0,
6105
                    'value' => ($field->value !== null ? true : null),
6106
                    'languageCode' => $field->languageCode,
6107
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
6108
                    'fieldTypeIdentifier' => $field->fieldTypeIdentifier,
6109
                )
6110
            );
6111
        }
6112
        usort(
6113
            $normalized,
6114 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...
6115
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
6116
                    return strcasecmp($field1->languageCode, $field2->languageCode);
6117
                }
6118
6119
                return $return;
6120
            }
6121
        );
6122
6123
        return $normalized;
6124
    }
6125
6126
    /**
6127
     * Returns a filtered set of the default fields fixture.
6128
     *
6129
     * @param string $languageCode
6130
     *
6131
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
6132
     */
6133
    private function createLocaleFieldsFixture($languageCode)
6134
    {
6135
        $fields = array();
6136
        foreach ($this->createFieldsFixture() as $field) {
6137
            if (null === $field->languageCode || $languageCode === $field->languageCode) {
6138
                $fields[] = $field;
6139
            }
6140
        }
6141
6142
        return $fields;
6143
    }
6144
6145
    /**
6146
     * Asserts that given Content has default ContentStates.
6147
     *
6148
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
6149
     */
6150 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
6151
    {
6152
        $repository = $this->getRepository();
6153
        $objectStateService = $repository->getObjectStateService();
6154
6155
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
6156
6157
        foreach ($objectStateGroups as $objectStateGroup) {
6158
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
6159
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
6160
                // Only check the first object state which is the default one.
6161
                $this->assertEquals(
6162
                    $objectState,
6163
                    $contentState
6164
                );
6165
                break;
6166
            }
6167
        }
6168
    }
6169
6170
    /**
6171
     * Assert that given Content has no references to a translation specified by the $languageCode.
6172
     *
6173
     * @param string $languageCode
6174
     * @param int $contentId
6175
     */
6176
    private function assertTranslationDoesNotExist($languageCode, $contentId)
6177
    {
6178
        $repository = $this->getRepository();
6179
        $contentService = $repository->getContentService();
6180
6181
        $content = $contentService->loadContent($contentId);
6182
6183
        foreach ($content->fields as $fieldIdentifier => $field) {
6184
            /** @var array $field */
6185
            self::assertArrayNotHasKey($languageCode, $field);
6186
            self::assertNotEquals($languageCode, $content->contentInfo->mainLanguageCode);
6187
            self::assertArrayNotHasKey($languageCode, $content->versionInfo->getNames());
6188
            self::assertNotEquals($languageCode, $content->versionInfo->initialLanguageCode);
6189
            self::assertNotContains($languageCode, $content->versionInfo->languageCodes);
6190
        }
6191
        foreach ($contentService->loadVersions($content->contentInfo) as $versionInfo) {
6192
            self::assertArrayNotHasKey($languageCode, $versionInfo->getNames());
6193
            self::assertNotEquals($languageCode, $versionInfo->contentInfo->mainLanguageCode);
6194
            self::assertNotEquals($languageCode, $versionInfo->initialLanguageCode);
6195
            self::assertNotContains($languageCode, $versionInfo->languageCodes);
6196
        }
6197
    }
6198
6199
    /**
6200
     * Returns the default fixture of fields used in most tests.
6201
     *
6202
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
6203
     */
6204
    private function createFieldsFixture()
6205
    {
6206
        return array(
6207
            new Field(
6208
                array(
6209
                    'id' => 0,
6210
                    'value' => 'Foo',
6211
                    'languageCode' => 'eng-US',
6212
                    'fieldDefIdentifier' => 'description',
6213
                    'fieldTypeIdentifier' => 'ezrichtext',
6214
                )
6215
            ),
6216
            new Field(
6217
                array(
6218
                    'id' => 0,
6219
                    'value' => 'Bar',
6220
                    'languageCode' => 'eng-GB',
6221
                    'fieldDefIdentifier' => 'description',
6222
                    'fieldTypeIdentifier' => 'ezrichtext',
6223
                )
6224
            ),
6225
            new Field(
6226
                array(
6227
                    'id' => 0,
6228
                    'value' => 'An awesome multi-lang forum²',
6229
                    'languageCode' => 'eng-US',
6230
                    'fieldDefIdentifier' => 'name',
6231
                    'fieldTypeIdentifier' => 'ezstring',
6232
                )
6233
            ),
6234
            new Field(
6235
                array(
6236
                    'id' => 0,
6237
                    'value' => 'An awesome multi-lang forum²³',
6238
                    'languageCode' => 'eng-GB',
6239
                    'fieldDefIdentifier' => 'name',
6240
                    'fieldTypeIdentifier' => 'ezstring',
6241
                )
6242
            ),
6243
        );
6244
    }
6245
6246
    /**
6247
     * Gets expected property values for the "Media" ContentInfo ValueObject.
6248
     *
6249
     * @return array
6250
     */
6251 View Code Duplication
    private function getExpectedMediaContentInfoProperties()
6252
    {
6253
        return [
6254
            'id' => 41,
6255
            'contentTypeId' => 1,
6256
            'name' => 'Media',
6257
            'sectionId' => 3,
6258
            'currentVersionNo' => 1,
6259
            'published' => true,
6260
            'ownerId' => 14,
6261
            'modificationDate' => $this->createDateTime(1060695457),
6262
            'publishedDate' => $this->createDateTime(1060695457),
6263
            'alwaysAvailable' => 1,
6264
            'remoteId' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
6265
            'mainLanguageCode' => 'eng-US',
6266
            'mainLocationId' => 43,
6267
            'status' => ContentInfo::STATUS_PUBLISHED,
6268
        ];
6269
    }
6270
6271
    /**
6272
     * @covers \eZ\Publish\API\Repository\ContentService::hideContent
6273
     *
6274
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
6275
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
6276
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
6277
     */
6278
    public function testHideContent(): void
6279
    {
6280
        $repository = $this->getRepository();
6281
        $contentTypeService = $repository->getContentTypeService();
6282
        $contentService = $repository->getContentService();
6283
        $locationService = $repository->getLocationService();
6284
6285
        $locationCreateStructs = array_map(
6286
            function (Location $parentLocation) use ($locationService) {
6287
                return $locationService->newLocationCreateStruct($parentLocation->id);
6288
            },
6289
            $this->createParentLocationsForHideReveal($locationService, 2)
6290
        );
6291
6292
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
6293
6294
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
6295
        $contentCreate->setField('name', 'Folder to hide');
6296
6297
        $content = $contentService->createContent(
6298
            $contentCreate,
6299
            $locationCreateStructs
6300
        );
6301
6302
        $publishedContent = $contentService->publishVersion($content->versionInfo);
6303
        $locations = $locationService->loadLocations($publishedContent->contentInfo);
6304
6305
        // Sanity check
6306
        $this->assertCount(3, $locations);
6307
        $this->assertCount(0, $this->filterHiddenLocations($locations));
6308
6309
        /* BEGIN: Use Case */
6310
        $contentService->hideContent($publishedContent->contentInfo);
6311
        /* END: Use Case */
6312
6313
        $locations = $locationService->loadLocations($publishedContent->contentInfo);
6314
        $this->assertCount(3, $locations);
6315
        $this->assertCount(3, $this->filterHiddenLocations($locations));
6316
    }
6317
6318
    /**
6319
     * @covers \eZ\Publish\API\Repository\ContentService::revealContent
6320
     *
6321
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
6322
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
6323
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
6324
     */
6325
    public function testRevealContent()
6326
    {
6327
        $repository = $this->getRepository();
6328
        $contentTypeService = $repository->getContentTypeService();
6329
        $contentService = $repository->getContentService();
6330
        $locationService = $repository->getLocationService();
6331
6332
        $locationCreateStructs = array_map(
6333
            function (Location $parentLocation) use ($locationService) {
6334
                return $locationService->newLocationCreateStruct($parentLocation->id);
6335
            },
6336
            $this->createParentLocationsForHideReveal($locationService, 2)
6337
        );
6338
6339
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
6340
6341
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
6342
        $contentCreate->setField('name', 'Folder to hide');
6343
6344
        $locationCreateStructs[0]->hidden = true;
6345
6346
        $content = $contentService->createContent(
6347
            $contentCreate,
6348
            $locationCreateStructs
6349
        );
6350
6351
        $publishedContent = $contentService->publishVersion($content->versionInfo);
6352
        $locations = $locationService->loadLocations($publishedContent->contentInfo);
6353
6354
        // Sanity check
6355
        $hiddenLocations = $this->filterHiddenLocations($locations);
6356
        $this->assertCount(3, $locations);
6357
        $this->assertCount(1, $hiddenLocations);
6358
6359
        // BEGIN: Use Case
6360
        $contentService->hideContent($publishedContent->contentInfo);
6361
        $this->assertCount(
6362
            3,
6363
            $this->filterHiddenLocations(
6364
                $locationService->loadLocations($publishedContent->contentInfo)
6365
            )
6366
        );
6367
6368
        $contentService->revealContent($publishedContent->contentInfo);
6369
        // END: Use Case
6370
6371
        $locations = $locationService->loadLocations($publishedContent->contentInfo);
6372
        $hiddenLocationsAfterReveal = $this->filterHiddenLocations($locations);
6373
        $this->assertCount(3, $locations);
6374
        $this->assertCount(1, $hiddenLocationsAfterReveal);
6375
        $this->assertEquals($hiddenLocations, $hiddenLocationsAfterReveal);
6376
    }
6377
6378
    /**
6379
     * @depends testRevealContent
6380
     */
6381
    public function testRevealContentWithHiddenParent()
6382
    {
6383
        $repository = $this->getRepository();
6384
6385
        $contentTypeService = $repository->getContentTypeService();
6386
        $contentService = $repository->getContentService();
6387
        $locationService = $repository->getLocationService();
6388
6389
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
6390
6391
        $contentNames = [
6392
            'Parent Content',
6393
            'Child (Nesting 1)',
6394
            'Child (Nesting 2)',
6395
            'Child (Nesting 3)',
6396
            'Child (Nesting 4)',
6397
        ];
6398
6399
        $parentLocation = $locationService->newLocationCreateStruct(
6400
            $this->generateId('location', 2)
6401
        );
6402
6403
        /** @var Content[] $contents */
6404
        $contents = [];
6405
6406 View Code Duplication
        foreach ($contentNames as $contentName) {
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...
6407
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
6408
            $contentCreate->setField('name', $contentName);
6409
6410
            $content = $contentService->createContent($contentCreate, [$parentLocation]);
6411
            $contents[] = $publishedContent = $contentService->publishVersion($content->versionInfo);
6412
6413
            $parentLocation = $locationService->newLocationCreateStruct(
6414
                $this->generateId('location', $publishedContent->contentInfo->mainLocationId)
6415
            );
6416
        }
6417
6418
        $contentService->hideContent($contents[0]->contentInfo);
6419
        $contentService->hideContent($contents[2]->contentInfo);
6420
        $contentService->revealContent($contents[2]->contentInfo);
6421
6422
        $parentContent = $contentService->loadContent($contents[0]->id);
6423
        $parentLocation = $locationService->loadLocation($parentContent->contentInfo->mainLocationId);
6424
        $parentSublocations = $locationService->loadLocationList([
6425
            $contents[1]->contentInfo->mainLocationId,
6426
            $contents[2]->contentInfo->mainLocationId,
6427
            $contents[3]->contentInfo->mainLocationId,
6428
            $contents[4]->contentInfo->mainLocationId,
6429
        ]);
6430
6431
        // Parent remains invisible
6432
        self::assertTrue($parentLocation->invisible);
6433
6434
        // All parent sublocations remain invisible as well
6435
        foreach ($parentSublocations as $parentSublocation) {
6436
            self::assertTrue($parentSublocation->invisible);
6437
        }
6438
    }
6439
6440
    /**
6441
     * @depends testRevealContent
6442
     */
6443
    public function testRevealContentWithHiddenChildren()
6444
    {
6445
        $repository = $this->getRepository();
6446
6447
        $contentTypeService = $repository->getContentTypeService();
6448
        $contentService = $repository->getContentService();
6449
        $locationService = $repository->getLocationService();
6450
6451
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
6452
6453
        $contentNames = [
6454
            'Parent Content',
6455
            'Child (Nesting 1)',
6456
            'Child (Nesting 2)',
6457
            'Child (Nesting 3)',
6458
            'Child (Nesting 4)',
6459
        ];
6460
6461
        $parentLocation = $locationService->newLocationCreateStruct(
6462
            $this->generateId('location', 2)
6463
        );
6464
6465
        /** @var Content[] $contents */
6466
        $contents = [];
6467
6468 View Code Duplication
        foreach ($contentNames as $contentName) {
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...
6469
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
6470
            $contentCreate->setField('name', $contentName);
6471
6472
            $content = $contentService->createContent($contentCreate, [$parentLocation]);
6473
            $contents[] = $publishedContent = $contentService->publishVersion($content->versionInfo);
6474
6475
            $parentLocation = $locationService->newLocationCreateStruct(
6476
                $this->generateId('location', $publishedContent->contentInfo->mainLocationId)
6477
            );
6478
        }
6479
6480
        $contentService->hideContent($contents[0]->contentInfo);
6481
        $contentService->hideContent($contents[2]->contentInfo);
6482
        $contentService->revealContent($contents[0]->contentInfo);
6483
6484
        $directChildContent = $contentService->loadContent($contents[1]->id);
6485
        $directChildLocation = $locationService->loadLocation($directChildContent->contentInfo->mainLocationId);
6486
6487
        $childContent = $contentService->loadContent($contents[2]->id);
6488
        $childLocation = $locationService->loadLocation($childContent->contentInfo->mainLocationId);
6489
        $childSublocations = $locationService->loadLocationList([
6490
            $contents[3]->contentInfo->mainLocationId,
6491
            $contents[4]->contentInfo->mainLocationId,
6492
        ]);
6493
6494
        // Direct child content is not hidden
6495
        self::assertFalse($directChildContent->contentInfo->isHidden);
6496
6497
        // Direct child content location is still invisible
6498
        self::assertFalse($directChildLocation->invisible);
6499
6500
        // Child content is still hidden
6501
        self::assertTrue($childContent->contentInfo->isHidden);
6502
6503
        // Child content location is still invisible
6504
        self::assertTrue($childLocation->invisible);
6505
6506
        // All childs sublocations remain invisible as well
6507
        foreach ($childSublocations as $childSublocation) {
6508
            self::assertTrue($childSublocation->invisible);
6509
        }
6510
    }
6511
6512
    public function testHideContentWithParentLocation()
6513
    {
6514
        $repository = $this->getRepository();
6515
        $contentTypeService = $repository->getContentTypeService();
6516
6517
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
6518
6519
        $contentService = $repository->getContentService();
6520
        $locationService = $repository->getLocationService();
6521
6522
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
6523
        $contentCreate->setField('name', 'Parent');
6524
6525
        $content = $contentService->createContent(
6526
            $contentCreate,
6527
            [
6528
                $locationService->newLocationCreateStruct(
6529
                    $this->generateId('location', 2)
6530
                ),
6531
            ]
6532
        );
6533
6534
        $publishedContent = $contentService->publishVersion($content->versionInfo);
6535
6536
        /* BEGIN: Use Case */
6537
        $contentService->hideContent($publishedContent->contentInfo);
6538
        /* END: Use Case */
6539
6540
        $locations = $locationService->loadLocations($publishedContent->contentInfo);
6541
6542
        $childContentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
6543
        $childContentCreate->setField('name', 'Child');
6544
6545
        $childContent = $contentService->createContent(
6546
            $childContentCreate,
6547
            [
6548
                $locationService->newLocationCreateStruct(
6549
                    $locations[0]->id
6550
                ),
6551
            ]
6552
        );
6553
6554
        $publishedChildContent = $contentService->publishVersion($childContent->versionInfo);
6555
6556
        $childLocations = $locationService->loadLocations($publishedChildContent->contentInfo);
6557
6558
        $this->assertTrue($locations[0]->hidden);
6559
        $this->assertTrue($locations[0]->invisible);
6560
6561
        $this->assertFalse($childLocations[0]->hidden);
6562
        $this->assertTrue($childLocations[0]->invisible);
6563
    }
6564
6565
    public function testChangeContentName()
6566
    {
6567
        $repository = $this->getRepository();
6568
6569
        $contentService = $repository->getContentService();
6570
        $contentDraft = $this->createContentDraft(
6571
            'folder',
6572
            $this->generateId('location', 2),
6573
            [
6574
                'name' => 'Marco',
6575
            ]
6576
        );
6577
6578
        $publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
6579
        $contentMetadataUpdateStruct = new ContentMetadataUpdateStruct([
6580
            'name' => 'Polo',
6581
        ]);
6582
        $contentService->updateContentMetadata($publishedContent->contentInfo, $contentMetadataUpdateStruct);
6583
6584
        $updatedContent = $contentService->loadContent($publishedContent->id);
6585
6586
        $this->assertEquals('Marco', $publishedContent->contentInfo->name);
6587
        $this->assertEquals('Polo', $updatedContent->contentInfo->name);
6588
    }
6589
6590
    public function testCopyTranslationsFromPublishedToDraft()
6591
    {
6592
        $repository = $this->getRepository();
6593
6594
        $contentService = $repository->getContentService();
6595
6596
        $contentDraft = $this->createContentDraft(
6597
            'folder',
6598
            $this->generateId('location', 2),
6599
            [
6600
                'name' => 'Folder US',
6601
            ]
6602
        );
6603
6604
        $publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
6605
6606
        $deDraft = $contentService->createContentDraft($publishedContent->contentInfo);
6607
6608
        $contentUpdateStruct = new ContentUpdateStruct([
6609
            'initialLanguageCode' => 'ger-DE',
6610
            'fields' => $contentDraft->getFields(),
6611
        ]);
6612
6613
        $contentUpdateStruct->setField('name', 'Folder GER', 'ger-DE');
6614
6615
        $deContent = $contentService->updateContent($deDraft->versionInfo, $contentUpdateStruct);
6616
6617
        $updatedContent = $contentService->loadContent($deContent->id, null, $deContent->versionInfo->versionNo);
6618
        $this->assertEquals(
6619
            [
6620
                'eng-US' => 'Folder US',
6621
                'ger-DE' => 'Folder GER',
6622
            ],
6623
            $updatedContent->fields['name']
6624
        );
6625
6626
        $gbDraft = $contentService->createContentDraft($publishedContent->contentInfo);
6627
6628
        $contentUpdateStruct = new ContentUpdateStruct([
6629
            'initialLanguageCode' => 'eng-GB',
6630
            'fields' => $contentDraft->getFields(),
6631
        ]);
6632
6633
        $contentUpdateStruct->setField('name', 'Folder GB', 'eng-GB');
6634
6635
        $gbContent = $contentService->updateContent($gbDraft->versionInfo, $contentUpdateStruct);
6636
        $contentService->publishVersion($gbDraft->versionInfo);
6637
        $updatedContent = $contentService->loadContent($gbContent->id, null, $gbContent->versionInfo->versionNo);
6638
        $this->assertEquals(
6639
            [
6640
                'eng-US' => 'Folder US',
6641
                'eng-GB' => 'Folder GB',
6642
            ],
6643
            $updatedContent->fields['name']
6644
        );
6645
6646
        $dePublished = $contentService->publishVersion($deDraft->versionInfo);
6647
        $this->assertEquals(
6648
            [
6649
                'eng-US' => 'Folder US',
6650
                'ger-DE' => 'Folder GER',
6651
                'eng-GB' => 'Folder GB',
6652
            ],
6653
            $dePublished->fields['name']
6654
        );
6655
    }
6656
6657
    /**
6658
     * Create structure of parent folders with Locations to be used for Content hide/reveal tests.
6659
     *
6660
     * @param \eZ\Publish\API\Repository\LocationService $locationService
6661
     * @param int $parentLocationId
6662
     *
6663
     * @return \eZ\Publish\API\Repository\Values\Content\Location[] A list of Locations aimed to be parents
6664
     *
6665
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
6666
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
6667
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
6668
     */
6669
    private function createParentLocationsForHideReveal(LocationService $locationService, int $parentLocationId): array
6670
    {
6671
        $parentFoldersLocationsIds = [
6672
            $this->createFolder(['eng-US' => 'P1'], $parentLocationId)->contentInfo->mainLocationId,
6673
            $this->createFolder(['eng-US' => 'P2'], $parentLocationId)->contentInfo->mainLocationId,
6674
            $this->createFolder(['eng-US' => 'P3'], $parentLocationId)->contentInfo->mainLocationId,
6675
        ];
6676
6677
        return array_values($locationService->loadLocationList($parentFoldersLocationsIds));
6678
    }
6679
6680
    /**
6681
     * Filter Locations list by hidden only.
6682
     *
6683
     * @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
6684
     *
6685
     * @return array
6686
     */
6687
    private function filterHiddenLocations(array $locations): array
6688
    {
6689
        return array_values(
6690
            array_filter(
6691
                $locations,
6692
                function (Location $location) {
6693
                    return $location->hidden;
6694
                }
6695
            )
6696
        );
6697
    }
6698
6699
    public function testPublishVersionWithSelectedLanguages()
6700
    {
6701
        $repository = $this->getRepository();
6702
6703
        $contentService = $repository->getContentService();
6704
6705
        $publishedContent = $this->createFolder(
6706
            [
6707
                'eng-US' => 'Published US',
6708
                'ger-DE' => 'Published DE',
6709
            ],
6710
            $this->generateId('location', 2)
6711
        );
6712
6713
        $draft = $contentService->createContentDraft($publishedContent->contentInfo);
6714
        $contentUpdateStruct = new ContentUpdateStruct([
6715
            'initialLanguageCode' => 'eng-US',
6716
        ]);
6717
        $contentUpdateStruct->setField('name', 'Draft 1 US', 'eng-US');
6718
        $contentUpdateStruct->setField('name', 'Draft 1 DE', 'ger-DE');
6719
6720
        $contentService->updateContent($draft->versionInfo, $contentUpdateStruct);
6721
6722
        $contentService->publishVersion($draft->versionInfo, ['ger-DE']);
6723
        $content = $contentService->loadContent($draft->contentInfo->id);
6724
        $this->assertEquals(
6725
            [
6726
                'eng-US' => 'Published US',
6727
                'ger-DE' => 'Draft 1 DE',
6728
            ],
6729
            $content->fields['name']
6730
        );
6731
    }
6732
}
6733