Completed
Push — 6.13 ( a3b33c...29a2d0 )
by
unknown
65:00 queued 27:58
created

testLoadContentInfoSetsExpectedContentInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading history...
2366
2367
        // Will return the VersionInfo of the $draftContent
2368
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2369
        /* END: Use Case */
2370
2371
        $this->assertEquals(2, $versionInfo->versionNo);
2372
2373
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2374
        $this->assertEquals(
2375
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2376
            $versionInfo->getContentInfo()->mainLocationId
2377
        );
2378
    }
2379
2380
    /**
2381
     * Test for the loadVersionInfo() method.
2382
     *
2383
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2384
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2385
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2386
     */
2387
    public function testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter()
2388
    {
2389
        $repository = $this->getRepository();
2390
2391
        $contentService = $repository->getContentService();
2392
2393
        /* BEGIN: Use Case */
2394
        $draft = $this->createContentDraftVersion1();
2395
2396
        // This call will fail with a "NotFoundException", because not versionNo
2397
        // 2 exists for this content object.
2398
        $contentService->loadVersionInfo($draft->contentInfo, 2);
2399
        /* END: Use Case */
2400
    }
2401
2402
    /**
2403
     * Test for the loadVersionInfoById() method.
2404
     *
2405
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2406
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2407
     */
2408
    public function testLoadVersionInfoByIdWithSecondParameter()
2409
    {
2410
        $repository = $this->getRepository();
2411
2412
        $contentService = $repository->getContentService();
2413
2414
        /* BEGIN: Use Case */
2415
        $publishedContent = $this->createContentVersion1();
2416
2417
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
2418
2419
        // Will return the VersionInfo of the $draftContent
2420
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2421
        /* END: Use Case */
2422
2423
        $this->assertEquals(2, $versionInfo->versionNo);
2424
2425
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2426
        $this->assertEquals(
2427
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2428
            $versionInfo->getContentInfo()->mainLocationId
2429
        );
2430
2431
        return [
2432
            'versionInfo' => $versionInfo,
2433
            'draftContent' => $draftContent,
2434
        ];
2435
    }
2436
2437
    /**
2438
     * Test for the returned value of the loadVersionInfoById() method.
2439
     *
2440
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter
2441
     * @covers \eZ\Publish\API\Repository\ContentService::loadVersionInfoById
2442
     *
2443
     * @param array $data
2444
     */
2445
    public function testLoadVersionInfoByIdWithSecondParameterSetsExpectedVersionInfo(array $data)
2446
    {
2447
        /** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo */
2448
        $versionInfo = $data['versionInfo'];
2449
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $draftContent */
2450
        $draftContent = $data['draftContent'];
2451
2452
        $this->assertPropertiesCorrect(
2453
            [
2454
                'names' => [
2455
                    'eng-US' => 'An awesome forum',
2456
                ],
2457
                'contentInfo' => new ContentInfo([
2458
                    'id' => $draftContent->contentInfo->id,
2459
                    'contentTypeId' => 28,
2460
                    'name' => 'An awesome forum',
2461
                    'sectionId' => 1,
2462
                    'currentVersionNo' => 1,
2463
                    'published' => true,
2464
                    'ownerId' => 14,
2465
                    // this Content Object is created at the test runtime
2466
                    'modificationDate' => $versionInfo->contentInfo->modificationDate,
2467
                    'publishedDate' => $versionInfo->contentInfo->publishedDate,
2468
                    'alwaysAvailable' => 1,
2469
                    'remoteId' => 'abcdef0123456789abcdef0123456789',
2470
                    'mainLanguageCode' => 'eng-US',
2471
                    'mainLocationId' => $draftContent->contentInfo->mainLocationId,
2472
                    'status' => ContentInfo::STATUS_PUBLISHED,
2473
                ]),
2474
                'id' => $draftContent->versionInfo->id,
2475
                'versionNo' => 2,
2476
                'creatorId' => 14,
2477
                'status' => 0,
2478
                'initialLanguageCode' => 'eng-US',
2479
                'languageCodes' => [
2480
                    'eng-US',
2481
                ],
2482
            ],
2483
            $versionInfo
2484
        );
2485
    }
2486
2487
    /**
2488
     * Test for the loadVersionInfoById() method.
2489
     *
2490
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2491
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2492
     */
2493 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter()
2494
    {
2495
        $repository = $this->getRepository();
2496
2497
        $contentService = $repository->getContentService();
2498
2499
        /* BEGIN: Use Case */
2500
        $content = $this->createContentVersion1();
2501
2502
        // This call will fail with a "NotFoundException", because not versionNo
2503
        // 2 exists for this content object.
2504
        $contentService->loadVersionInfoById($content->id, 2);
2505
        /* END: Use Case */
2506
    }
2507
2508
    /**
2509
     * Test for the loadContentByVersionInfo() method.
2510
     *
2511
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
2512
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2513
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
2514
     */
2515
    public function testLoadContentByVersionInfoWithSecondParameter()
2516
    {
2517
        $repository = $this->getRepository();
2518
2519
        $sectionId = $this->generateId('section', 1);
2520
        /* BEGIN: Use Case */
2521
        $contentTypeService = $repository->getContentTypeService();
2522
2523
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2524
2525
        $contentService = $repository->getContentService();
2526
2527
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2528
2529
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2530
2531
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2532
2533
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2534
        // $sectionId contains the ID of section 1
2535
        $contentCreateStruct->sectionId = $sectionId;
2536
        $contentCreateStruct->alwaysAvailable = true;
2537
2538
        // Create a new content draft
2539
        $content = $contentService->createContent($contentCreateStruct);
2540
2541
        // Now publish this draft
2542
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2543
2544
        // Will return a content instance with fields in "eng-US"
2545
        $reloadedContent = $contentService->loadContentByVersionInfo(
2546
            $publishedContent->getVersionInfo(),
2547
            array(
2548
                'eng-GB',
2549
            ),
2550
            false
2551
        );
2552
        /* END: Use Case */
2553
2554
        $actual = array();
2555 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...
2556
            $actual[] = new Field(
2557
                array(
2558
                    'id' => 0,
2559
                    'value' => ($field->value !== null ? true : null), // Actual value tested by FieldType integration tests
2560
                    'languageCode' => $field->languageCode,
2561
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
2562
                )
2563
            );
2564
        }
2565
        usort(
2566
            $actual,
2567 View Code Duplication
            function ($field1, $field2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2568
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
2569
                    return strcasecmp($field1->languageCode, $field2->languageCode);
2570
                }
2571
2572
                return $return;
2573
            }
2574
        );
2575
2576
        $expected = array(
2577
            new Field(
2578
                array(
2579
                    'id' => 0,
2580
                    'value' => true,
2581
                    'languageCode' => 'eng-GB',
2582
                    'fieldDefIdentifier' => 'description',
2583
                )
2584
            ),
2585
            new Field(
2586
                array(
2587
                    'id' => 0,
2588
                    'value' => true,
2589
                    'languageCode' => 'eng-GB',
2590
                    'fieldDefIdentifier' => 'name',
2591
                )
2592
            ),
2593
        );
2594
2595
        $this->assertEquals($expected, $actual);
2596
    }
2597
2598
    /**
2599
     * Test for the loadContentByContentInfo() method.
2600
     *
2601
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages)
2602
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2603
     */
2604
    public function testLoadContentByContentInfoWithLanguageParameters()
2605
    {
2606
        $repository = $this->getRepository();
2607
2608
        $sectionId = $this->generateId('section', 1);
2609
        /* BEGIN: Use Case */
2610
        $contentTypeService = $repository->getContentTypeService();
2611
2612
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2613
2614
        $contentService = $repository->getContentService();
2615
2616
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2617
2618
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2619
2620
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2621
2622
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2623
        // $sectionId contains the ID of section 1
2624
        $contentCreateStruct->sectionId = $sectionId;
2625
        $contentCreateStruct->alwaysAvailable = true;
2626
2627
        // Create a new content draft
2628
        $content = $contentService->createContent($contentCreateStruct);
2629
2630
        // Now publish this draft
2631
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2632
2633
        // Will return a content instance with fields in "eng-US"
2634
        $reloadedContent = $contentService->loadContentByContentInfo(
2635
            $publishedContent->contentInfo,
2636
            array(
2637
                'eng-US',
2638
            ),
2639
            null,
2640
            false
2641
        );
2642
        /* END: Use Case */
2643
2644
        $actual = $this->normalizeFields($reloadedContent->getFields());
2645
2646
        $expected = array(
2647
            new Field(
2648
                array(
2649
                    'id' => 0,
2650
                    'value' => true,
2651
                    'languageCode' => 'eng-US',
2652
                    'fieldDefIdentifier' => 'description',
2653
                    'fieldTypeIdentifier' => 'ezrichtext',
2654
                )
2655
            ),
2656
            new Field(
2657
                array(
2658
                    'id' => 0,
2659
                    'value' => true,
2660
                    'languageCode' => 'eng-US',
2661
                    'fieldDefIdentifier' => 'name',
2662
                    'fieldTypeIdentifier' => 'ezstring',
2663
                )
2664
            ),
2665
        );
2666
2667
        $this->assertEquals($expected, $actual);
2668
2669
        // Will return a content instance with fields in "eng-GB" (versions prior to 6.0.0-beta9 returned "eng-US" also)
2670
        $reloadedContent = $contentService->loadContentByContentInfo(
2671
            $publishedContent->contentInfo,
2672
            array(
2673
                'eng-GB',
2674
            ),
2675
            null,
2676
            true
2677
        );
2678
2679
        $actual = $this->normalizeFields($reloadedContent->getFields());
2680
2681
        $expected = array(
2682
            new Field(
2683
                array(
2684
                    'id' => 0,
2685
                    'value' => true,
2686
                    'languageCode' => 'eng-GB',
2687
                    'fieldDefIdentifier' => 'description',
2688
                    'fieldTypeIdentifier' => 'ezrichtext',
2689
                )
2690
            ),
2691
            new Field(
2692
                array(
2693
                    'id' => 0,
2694
                    'value' => true,
2695
                    'languageCode' => 'eng-GB',
2696
                    'fieldDefIdentifier' => 'name',
2697
                    'fieldTypeIdentifier' => 'ezstring',
2698
                )
2699
            ),
2700
        );
2701
2702
        $this->assertEquals($expected, $actual);
2703
2704
        // Will return a content instance with fields in main language "eng-US", as "fre-FR" does not exists
2705
        $reloadedContent = $contentService->loadContentByContentInfo(
2706
            $publishedContent->contentInfo,
2707
            array(
2708
                'fre-FR',
2709
            ),
2710
            null,
2711
            true
2712
        );
2713
2714
        $actual = $this->normalizeFields($reloadedContent->getFields());
2715
2716
        $expected = array(
2717
            new Field(
2718
                array(
2719
                    'id' => 0,
2720
                    'value' => true,
2721
                    'languageCode' => 'eng-US',
2722
                    'fieldDefIdentifier' => 'description',
2723
                    'fieldTypeIdentifier' => 'ezrichtext',
2724
                )
2725
            ),
2726
            new Field(
2727
                array(
2728
                    'id' => 0,
2729
                    'value' => true,
2730
                    'languageCode' => 'eng-US',
2731
                    'fieldDefIdentifier' => 'name',
2732
                    'fieldTypeIdentifier' => 'ezstring',
2733
                )
2734
            ),
2735
        );
2736
2737
        $this->assertEquals($expected, $actual);
2738
    }
2739
2740
    /**
2741
     * Test for the loadContentByContentInfo() method.
2742
     *
2743
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2744
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2745
     */
2746 View Code Duplication
    public function testLoadContentByContentInfoWithVersionNumberParameter()
2747
    {
2748
        $repository = $this->getRepository();
2749
2750
        $contentService = $repository->getContentService();
2751
2752
        /* BEGIN: Use Case */
2753
        $publishedContent = $this->createContentVersion1();
2754
2755
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
0 ignored issues
show
Unused Code introduced by
$draftContent is not used, you could remove the assignment.

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

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

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

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

Loading history...
2756
2757
        // This content instance is identical to $draftContent
2758
        $draftContentReloaded = $contentService->loadContentByContentInfo(
2759
            $publishedContent->contentInfo,
2760
            null,
2761
            2
2762
        );
2763
        /* END: Use Case */
2764
2765
        $this->assertEquals(
2766
            2,
2767
            $draftContentReloaded->getVersionInfo()->versionNo
2768
        );
2769
2770
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2771
        $this->assertEquals(
2772
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2773
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2774
        );
2775
    }
2776
2777
    /**
2778
     * Test for the loadContentByContentInfo() method.
2779
     *
2780
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2781
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2782
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithVersionNumberParameter
2783
     */
2784 View Code Duplication
    public function testLoadContentByContentInfoThrowsNotFoundExceptionWithVersionNumberParameter()
2785
    {
2786
        $repository = $this->getRepository();
2787
2788
        $contentService = $repository->getContentService();
2789
2790
        /* BEGIN: Use Case */
2791
        $content = $this->createContentVersion1();
2792
2793
        // This call will fail with a "NotFoundException", because no content
2794
        // with versionNo = 2 exists.
2795
        $contentService->loadContentByContentInfo($content->contentInfo, null, 2);
2796
        /* END: Use Case */
2797
    }
2798
2799
    /**
2800
     * Test for the loadContent() method.
2801
     *
2802
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages)
2803
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2804
     */
2805 View Code Duplication
    public function testLoadContentWithSecondParameter()
2806
    {
2807
        $repository = $this->getRepository();
2808
2809
        $contentService = $repository->getContentService();
2810
2811
        /* BEGIN: Use Case */
2812
        $draft = $this->createMultipleLanguageDraftVersion1();
2813
2814
        // This draft contains those fields localized with "eng-GB"
2815
        $draftLocalized = $contentService->loadContent($draft->id, array('eng-GB'), null, false);
2816
        /* END: Use Case */
2817
2818
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2819
2820
        return $draft;
2821
    }
2822
2823
    /**
2824
     * Test for the loadContent() method using undefined translation.
2825
     *
2826
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithSecondParameter
2827
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2828
     *
2829
     * @param \eZ\Publish\API\Repository\Values\Content\Content $contentDraft
2830
     */
2831
    public function testLoadContentWithSecondParameterThrowsNotFoundException(Content $contentDraft)
2832
    {
2833
        $repository = $this->getRepository();
2834
2835
        $contentService = $repository->getContentService();
2836
2837
        $contentService->loadContent($contentDraft->id, array('ger-DE'), null, false);
2838
    }
2839
2840
    /**
2841
     * Test for the loadContent() method.
2842
     *
2843
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2844
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2845
     */
2846 View Code Duplication
    public function testLoadContentWithThirdParameter()
2847
    {
2848
        $repository = $this->getRepository();
2849
2850
        $contentService = $repository->getContentService();
2851
2852
        /* BEGIN: Use Case */
2853
        $publishedContent = $this->createContentVersion1();
2854
2855
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
0 ignored issues
show
Unused Code introduced by
$draftContent is not used, you could remove the assignment.

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

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

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

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

Loading history...
2856
2857
        // This content instance is identical to $draftContent
2858
        $draftContentReloaded = $contentService->loadContent($publishedContent->id, null, 2);
2859
        /* END: Use Case */
2860
2861
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2862
2863
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2864
        $this->assertEquals(
2865
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2866
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2867
        );
2868
    }
2869
2870
    /**
2871
     * Test for the loadContent() method.
2872
     *
2873
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2874
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2875
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithThirdParameter
2876
     */
2877 View Code Duplication
    public function testLoadContentThrowsNotFoundExceptionWithThirdParameter()
2878
    {
2879
        $repository = $this->getRepository();
2880
2881
        $contentService = $repository->getContentService();
2882
2883
        /* BEGIN: Use Case */
2884
        $content = $this->createContentVersion1();
2885
2886
        // This call will fail with a "NotFoundException", because for this
2887
        // content object no versionNo=2 exists.
2888
        $contentService->loadContent($content->id, null, 2);
2889
        /* END: Use Case */
2890
    }
2891
2892
    /**
2893
     * Test for the loadContentByRemoteId() method.
2894
     *
2895
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages)
2896
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2897
     */
2898 View Code Duplication
    public function testLoadContentByRemoteIdWithSecondParameter()
2899
    {
2900
        $repository = $this->getRepository();
2901
2902
        $contentService = $repository->getContentService();
2903
2904
        /* BEGIN: Use Case */
2905
        $draft = $this->createMultipleLanguageDraftVersion1();
2906
2907
        $contentService->publishVersion($draft->versionInfo);
2908
2909
        // This draft contains those fields localized with "eng-GB"
2910
        $draftLocalized = $contentService->loadContentByRemoteId(
2911
            $draft->contentInfo->remoteId,
2912
            array('eng-GB'),
2913
            null,
2914
            false
2915
        );
2916
        /* END: Use Case */
2917
2918
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2919
    }
2920
2921
    /**
2922
     * Test for the loadContentByRemoteId() method.
2923
     *
2924
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2925
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2926
     */
2927 View Code Duplication
    public function testLoadContentByRemoteIdWithThirdParameter()
2928
    {
2929
        $repository = $this->getRepository();
2930
2931
        $contentService = $repository->getContentService();
2932
2933
        /* BEGIN: Use Case */
2934
        $publishedContent = $this->createContentVersion1();
2935
2936
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
0 ignored issues
show
Unused Code introduced by
$draftContent is not used, you could remove the assignment.

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

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

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

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

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