Completed
Push — 6.13 ( d52368...f5fae0 )
by André
20:11
created

stVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

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