Completed
Push — master ( 915f3b...c955a4 )
by
unknown
123:31 queued 89:18
created

ContentServiceTest::testNewContentUpdateStruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

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