Completed
Push — master ( cc4152...a16046 )
by
unknown
44:04 queued 16:02
created

testLoadVersionsAfterDeletingTwoDrafts()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

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

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

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

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

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

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