Completed
Push — master ( b83501...acd6b4 )
by André
31:22 queued 12:05
created

ContentServiceTest::createRoleWithPolicies()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 0
loc 14
rs 9.4285
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\Values\Content\ContentInfo;
13
use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct;
14
use eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct;
15
use eZ\Publish\API\Repository\Values\Content\Field;
16
use eZ\Publish\API\Repository\Values\Content\Location;
17
use eZ\Publish\API\Repository\Values\Content\URLAlias;
18
use eZ\Publish\API\Repository\Values\Content\Relation;
19
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
20
use eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation;
21
use eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation;
22
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation;
23
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
24
use Exception;
25
26
/**
27
 * Test case for operations in the ContentService using in memory storage.
28
 *
29
 * @see eZ\Publish\API\Repository\ContentService
30
 * @group content
31
 */
32
class ContentServiceTest extends BaseContentServiceTest
33
{
34
    /**
35
     * Test for the newContentCreateStruct() method.
36
     *
37
     * @see \eZ\Publish\API\Repository\ContentService::newContentCreateStruct()
38
     * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testLoadContentTypeByIdentifier
39
     * @group user
40
     * @group field-type
41
     */
42
    public function testNewContentCreateStruct()
43
    {
44
        $repository = $this->getRepository();
45
46
        /* BEGIN: Use Case */
47
        // Create a content type
48
        $contentTypeService = $repository->getContentTypeService();
49
50
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
51
52
        $contentService = $repository->getContentService();
53
54
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
55
        /* END: Use Case */
56
57
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentCreateStruct', $contentCreate);
58
    }
59
60
    /**
61
     * Test for the createContent() method.
62
     *
63
     * @return \eZ\Publish\API\Repository\Values\Content\Content
64
     *
65
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
66
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentCreateStruct
67
     * @group user
68
     * @group field-type
69
     */
70
    public function testCreateContent()
71
    {
72
        if ($this->isVersion4()) {
73
            $this->markTestSkipped('This test requires eZ Publish 5');
74
        }
75
76
        $repository = $this->getRepository();
77
78
        /* BEGIN: Use Case */
79
        $contentTypeService = $repository->getContentTypeService();
80
81
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
82
83
        $contentService = $repository->getContentService();
84
85
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
86
        $contentCreate->setField('name', 'My awesome forum');
87
88
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
89
        $contentCreate->alwaysAvailable = true;
90
91
        $content = $contentService->createContent($contentCreate);
92
        /* END: Use Case */
93
94
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content', $content);
95
96
        return $content;
97
    }
98
99
    /**
100
     * Test for the createContent() method.
101
     *
102
     * Tests made for issue #EZP-20955 where Anonymous user is granted access to create content
103
     * and should have access to do that.
104
     *
105
     * @return \eZ\Publish\API\Repository\Values\Content\Content
106
     *
107
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
108
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentCreateStruct
109
     * @group user
110
     * @group field-type
111
     */
112
    public function testCreateContentAndPublishWithPrivilegedAnonymousUser()
113
    {
114
        if ($this->isVersion4()) {
115
            $this->markTestSkipped('This test requires eZ Publish 5');
116
        }
117
118
        $anonymousUserId = $this->generateId('user', 10);
119
120
        $repository = $this->getRepository();
121
        $contentService = $repository->getContentService();
122
        $contentTypeService = $repository->getContentTypeService();
123
        $locationService = $repository->getLocationService();
124
        $roleService = $repository->getRoleService();
125
126
        // Give Anonymous user role additional rights
127
        $role = $roleService->loadRoleByIdentifier('Anonymous');
128
        $roleDraft = $roleService->createRoleDraft($role);
129
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'create');
130
        $policyCreateStruct->addLimitation(new SectionLimitation(array('limitationValues' => array(1))));
131
        $policyCreateStruct->addLimitation(new LocationLimitation(array('limitationValues' => array(2))));
132
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(array('limitationValues' => array(1))));
133
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
134
135
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'publish');
136
        $policyCreateStruct->addLimitation(new SectionLimitation(array('limitationValues' => array(1))));
137
        $policyCreateStruct->addLimitation(new LocationLimitation(array('limitationValues' => array(2))));
138
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(array('limitationValues' => array(1))));
139
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
140
        $roleService->publishRoleDraft($roleDraft);
141
142
        // Set Anonymous user as current
143
        $repository->getPermissionResolver()->setCurrentUserReference($repository->getUserService()->loadUser($anonymousUserId));
144
145
        // Create a new content object:
146
        $contentCreate = $contentService->newContentCreateStruct(
147
            $contentTypeService->loadContentTypeByIdentifier('folder'),
148
            'eng-GB'
149
        );
150
151
        $contentCreate->setField('name', 'Folder 1');
152
153
        $content = $contentService->createContent(
154
            $contentCreate,
155
            array($locationService->newLocationCreateStruct(2))
156
        );
157
158
        $contentService->publishVersion(
159
            $content->getVersionInfo()
160
        );
161
    }
162
163
    /**
164
     * Test for the createContent() method.
165
     *
166
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
167
     *
168
     * @return \eZ\Publish\API\Repository\Values\Content\Content
169
     *
170
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
171
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
172
     */
173
    public function testCreateContentSetsContentInfo($content)
174
    {
175
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $content->contentInfo);
176
177
        return $content;
178
    }
179
180
    /**
181
     * Test for the createContent() method.
182
     *
183
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
184
     *
185
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
186
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsContentInfo
187
     */
188
    public function testCreateContentSetsExpectedContentInfo($content)
189
    {
190
        $this->assertEquals(
191
            array(
192
                $content->id,
193
                28, // id of content type "forum"
194
                true,
195
                1,
196
                'abcdef0123456789abcdef0123456789',
197
                'eng-US',
198
                $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...
199
                false,
200
                null,
201
                // Main Location id for unpublished Content should be null
202
                null,
203
            ),
204
            array(
205
                $content->contentInfo->id,
206
                $content->contentInfo->contentTypeId,
207
                $content->contentInfo->alwaysAvailable,
208
                $content->contentInfo->currentVersionNo,
209
                $content->contentInfo->remoteId,
210
                $content->contentInfo->mainLanguageCode,
211
                $content->contentInfo->ownerId,
212
                $content->contentInfo->published,
213
                $content->contentInfo->publishedDate,
214
                $content->contentInfo->mainLocationId,
215
            )
216
        );
217
    }
218
219
    /**
220
     * Test for the createContent() method.
221
     *
222
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
223
     *
224
     * @return \eZ\Publish\API\Repository\Values\Content\Content
225
     *
226
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
227
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
228
     */
229
    public function testCreateContentSetsVersionInfo($content)
230
    {
231
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo', $content->getVersionInfo());
232
233
        return $content;
234
    }
235
236
    /**
237
     * Test for the createContent() method.
238
     *
239
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
240
     *
241
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
242
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsVersionInfo
243
     */
244
    public function testCreateContentSetsExpectedVersionInfo($content)
245
    {
246
        $this->assertEquals(
247
            array(
248
                'status' => VersionInfo::STATUS_DRAFT,
249
                'versionNo' => 1,
250
                '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...
251
                'initialLanguageCode' => 'eng-US',
252
            ),
253
            array(
254
                'status' => $content->getVersionInfo()->status,
255
                'versionNo' => $content->getVersionInfo()->versionNo,
256
                'creatorId' => $content->getVersionInfo()->creatorId,
257
                'initialLanguageCode' => $content->getVersionInfo()->initialLanguageCode,
258
            )
259
        );
260
        $this->assertTrue($content->getVersionInfo()->isDraft());
261
        $this->assertFalse($content->getVersionInfo()->isPublished());
262
        $this->assertFalse($content->getVersionInfo()->isArchived());
263
    }
264
265
    /**
266
     * Test for the createContent() method.
267
     *
268
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
269
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
270
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
271
     */
272
    public function testCreateContentThrowsInvalidArgumentException()
273
    {
274
        if ($this->isVersion4()) {
275
            $this->markTestSkipped('This test requires eZ Publish 5');
276
        }
277
278
        $repository = $this->getRepository();
279
280
        /* BEGIN: Use Case */
281
        $contentTypeService = $repository->getContentTypeService();
282
        $contentService = $repository->getContentService();
283
284
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
285
286
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
287
        $contentCreate1->setField('name', 'An awesome Sidelfingen forum');
288
289
        $contentCreate1->remoteId = 'abcdef0123456789abcdef0123456789';
290
        $contentCreate1->alwaysAvailable = true;
291
292
        $draft = $contentService->createContent($contentCreate1);
293
        $contentService->publishVersion($draft->versionInfo);
294
295
        $contentCreate2 = $contentService->newContentCreateStruct($contentType, 'eng-GB');
296
        $contentCreate2->setField('name', 'An awesome Bielefeld forum');
297
298
        $contentCreate2->remoteId = 'abcdef0123456789abcdef0123456789';
299
        $contentCreate2->alwaysAvailable = false;
300
301
        // This call will fail with an "InvalidArgumentException", because the
302
        // remoteId is already in use.
303
        $contentService->createContent($contentCreate2);
304
        /* END: Use Case */
305
    }
306
307
    /**
308
     * Test for the createContent() method.
309
     *
310
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
311
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
312
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
313
     */
314 View Code Duplication
    public function testCreateContentThrowsInvalidArgumentExceptionOnFieldTypeNotAccept()
315
    {
316
        $repository = $this->getRepository();
317
318
        /* BEGIN: Use Case */
319
        $contentTypeService = $repository->getContentTypeService();
320
        $contentService = $repository->getContentService();
321
322
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
323
324
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
325
        // The name field does only accept strings and null as its values
326
        $contentCreate->setField('name', new \stdClass());
327
328
        // Throws InvalidArgumentException since the name field is filled
329
        // improperly
330
        $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...
331
        /* END: Use Case */
332
    }
333
334
    /**
335
     * Test for the createContent() method.
336
     *
337
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
338
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
339
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
340
     */
341
    public function testCreateContentThrowsContentFieldValidationException()
342
    {
343
        $repository = $this->getRepository();
344
345
        /* BEGIN: Use Case */
346
        $contentTypeService = $repository->getContentTypeService();
347
        $contentService = $repository->getContentService();
348
349
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
350
351
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
352
        $contentCreate1->setField('name', 'An awesome Sidelfingen folder');
353
        // Violates string length constraint
354
        $contentCreate1->setField('short_name', str_repeat('a', 200));
355
356
        // Throws ContentFieldValidationException, since short_name does not pass
357
        // validation of the string length validator
358
        $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...
359
        /* END: Use Case */
360
    }
361
362
    /**
363
     * Test for the createContent() method.
364
     *
365
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
366
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
367
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
368
     */
369 View Code Duplication
    public function testCreateContentRequiredFieldMissing()
370
    {
371
        $repository = $this->getRepository();
372
373
        /* BEGIN: Use Case */
374
        $contentTypeService = $repository->getContentTypeService();
375
        $contentService = $repository->getContentService();
376
377
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
378
379
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
380
        // Required field "name" is not set
381
382
        // Throws a ContentFieldValidationException, since a required field is
383
        // missing
384
        $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...
385
        /* END: Use Case */
386
    }
387
388
    /**
389
     * Test for the createContent() method.
390
     *
391
     * NOTE: We have bidirectional dependencies between the ContentService and
392
     * the LocationService, so that we cannot use PHPUnit's test dependencies
393
     * here.
394
     *
395
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
396
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
397
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationByRemoteId
398
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
399
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
400
     * @group user
401
     */
402
    public function testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately()
403
    {
404
        $repository = $this->getRepository();
405
406
        $locationService = $repository->getLocationService();
407
408
        /* BEGIN: Use Case */
409
        $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...
410
411
        // The location will not have been created, yet, so this throws an
412
        // exception
413
        $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...
414
            '0123456789abcdef0123456789abcdef'
415
        );
416
        /* END: Use Case */
417
    }
418
419
    /**
420
     * Test for the createContent() method.
421
     *
422
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
423
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
424
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
425
     */
426
    public function testCreateContentThrowsInvalidArgumentExceptionWithLocationCreateParameter()
427
    {
428
        $repository = $this->getRepository();
429
430
        $parentLocationId = $this->generateId('location', 56);
431
        /* BEGIN: Use Case */
432
        // $parentLocationId is a valid location ID
433
434
        $contentService = $repository->getContentService();
435
        $contentTypeService = $repository->getContentTypeService();
436
        $locationService = $repository->getLocationService();
437
438
        // Load content type
439
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
440
441
        // Configure new locations
442
        $locationCreate1 = $locationService->newLocationCreateStruct($parentLocationId);
443
444
        $locationCreate1->priority = 23;
445
        $locationCreate1->hidden = true;
446
        $locationCreate1->remoteId = '0123456789abcdef0123456789aaaaaa';
447
        $locationCreate1->sortField = Location::SORT_FIELD_NODE_ID;
448
        $locationCreate1->sortOrder = Location::SORT_ORDER_DESC;
449
450
        $locationCreate2 = $locationService->newLocationCreateStruct($parentLocationId);
451
452
        $locationCreate2->priority = 42;
453
        $locationCreate2->hidden = true;
454
        $locationCreate2->remoteId = '0123456789abcdef0123456789bbbbbb';
455
        $locationCreate2->sortField = Location::SORT_FIELD_NODE_ID;
456
        $locationCreate2->sortOrder = Location::SORT_ORDER_DESC;
457
458
        // Configure new content object
459
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
460
461
        $contentCreate->setField('name', 'A awesome Sindelfingen forum');
462
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
463
        $contentCreate->alwaysAvailable = true;
464
465
        // Create new content object under the specified location
466
        $draft = $contentService->createContent(
467
            $contentCreate,
468
            array($locationCreate1)
469
        );
470
        $contentService->publishVersion($draft->versionInfo);
471
472
        // This call will fail with an "InvalidArgumentException", because the
473
        // Content remoteId already exists,
474
        $contentService->createContent(
475
            $contentCreate,
476
            array($locationCreate2)
477
        );
478
        /* END: Use Case */
479
    }
480
481
    /**
482
     * Test for the loadContentInfo() method.
483
     *
484
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
485
     * @group user
486
     */
487 View Code Duplication
    public function testLoadContentInfo()
488
    {
489
        $repository = $this->getRepository();
490
491
        $mediaFolderId = $this->generateId('object', 41);
492
        /* BEGIN: Use Case */
493
        $contentService = $repository->getContentService();
494
495
        // Load the ContentInfo for "Media" folder
496
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
497
        /* END: Use Case */
498
499
        $this->assertInstanceOf(
500
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
501
            $contentInfo
502
        );
503
504
        return $contentInfo;
505
    }
506
507
    /**
508
     * Test for the returned value of the loadContentInfo() method.
509
     *
510
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
511
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfo
512
     *
513
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
514
     */
515
    public function testLoadContentInfoSetsExpectedContentInfo(ContentInfo $contentInfo)
516
    {
517
        $this->assertPropertiesCorrectUnsorted(
518
            $this->getExpectedMediaContentInfoProperties(),
519
            $contentInfo
520
        );
521
    }
522
523
    /**
524
     * Test for the loadContentInfo() method.
525
     *
526
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
527
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
528
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
529
     */
530 View Code Duplication
    public function testLoadContentInfoThrowsNotFoundException()
531
    {
532
        $repository = $this->getRepository();
533
534
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
535
        /* BEGIN: Use Case */
536
        $contentService = $repository->getContentService();
537
538
        // This call will fail with a NotFoundException
539
        $contentService->loadContentInfo($nonExistentContentId);
540
        /* END: Use Case */
541
    }
542
543
    /**
544
     * Test for the loadContentInfoByRemoteId() method.
545
     *
546
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
547
     */
548
    public function testLoadContentInfoByRemoteId()
549
    {
550
        $repository = $this->getRepository();
551
552
        /* BEGIN: Use Case */
553
        $contentService = $repository->getContentService();
554
555
        // Load the ContentInfo for "Media" folder
556
        $contentInfo = $contentService->loadContentInfoByRemoteId('faaeb9be3bd98ed09f606fc16d144eca');
557
        /* END: Use Case */
558
559
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $contentInfo);
560
561
        return $contentInfo;
562
    }
563
564
    /**
565
     * Test for the returned value of the loadContentInfoByRemoteId() method.
566
     *
567
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
568
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId
569
     *
570
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
571
     */
572 View Code Duplication
    public function testLoadContentInfoByRemoteIdSetsExpectedContentInfo(ContentInfo $contentInfo)
573
    {
574
        $this->assertPropertiesCorrectUnsorted(
575
            [
576
                'id' => 10,
577
                'contentTypeId' => 4,
578
                'name' => 'Anonymous User',
579
                'sectionId' => 2,
580
                'currentVersionNo' => 2,
581
                'published' => true,
582
                'ownerId' => 14,
583
                'modificationDate' => $this->createDateTime(1072180405),
584
                'publishedDate' => $this->createDateTime(1033920665),
585
                'alwaysAvailable' => 1,
586
                'remoteId' => 'faaeb9be3bd98ed09f606fc16d144eca',
587
                'mainLanguageCode' => 'eng-US',
588
                'mainLocationId' => 45,
589
            ],
590
            $contentInfo
591
        );
592
    }
593
594
    /**
595
     * Test for the loadContentInfoByRemoteId() method.
596
     *
597
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
598
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
599
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
600
     */
601
    public function testLoadContentInfoByRemoteIdThrowsNotFoundException()
602
    {
603
        $repository = $this->getRepository();
604
605
        /* BEGIN: Use Case */
606
        $contentService = $repository->getContentService();
607
608
        // This call will fail with a NotFoundException
609
        $contentService->loadContentInfoByRemoteId('abcdefghijklmnopqrstuvwxyz0123456789');
610
        /* END: Use Case */
611
    }
612
613
    /**
614
     * Test for the loadVersionInfo() method.
615
     *
616
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
617
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
618
     * @group user
619
     */
620
    public function testLoadVersionInfo()
621
    {
622
        $repository = $this->getRepository();
623
624
        $mediaFolderId = $this->generateId('object', 41);
625
        /* BEGIN: Use Case */
626
        // $mediaFolderId contains the ID of the "Media" folder
627
628
        $contentService = $repository->getContentService();
629
630
        // Load the ContentInfo for "Media" folder
631
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
632
633
        // Now load the current version info of the "Media" folder
634
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
635
        /* END: Use Case */
636
637
        $this->assertInstanceOf(
638
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
639
            $versionInfo
640
        );
641
    }
642
643
    /**
644
     * Test for the loadVersionInfoById() method.
645
     *
646
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
647
     */
648 View Code Duplication
    public function testLoadVersionInfoById()
649
    {
650
        $repository = $this->getRepository();
651
652
        $mediaFolderId = $this->generateId('object', 41);
653
        /* BEGIN: Use Case */
654
        // $mediaFolderId contains the ID of the "Media" folder
655
656
        $contentService = $repository->getContentService();
657
658
        // Load the VersionInfo for "Media" folder
659
        $versionInfo = $contentService->loadVersionInfoById($mediaFolderId);
660
        /* END: Use Case */
661
662
        $this->assertInstanceOf(
663
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
664
            $versionInfo
665
        );
666
667
        return $versionInfo;
668
    }
669
670
    /**
671
     * Test for the returned value of the loadVersionInfoById() method.
672
     *
673
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
674
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
675
     *
676
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
677
     */
678
    public function testLoadVersionInfoByIdSetsExpectedVersionInfo(VersionInfo $versionInfo)
679
    {
680
        $this->assertPropertiesCorrect(
681
            [
682
                'names' => [
683
                    'eng-US' => 'Media',
684
                ],
685
                'contentInfo' => new ContentInfo($this->getExpectedMediaContentInfoProperties()),
686
                'id' => 472,
687
                'versionNo' => 1,
688
                'modificationDate' => $this->createDateTime(1060695457),
689
                'creatorId' => 14,
690
                'creationDate' => $this->createDateTime(1060695450),
691
                'status' => VersionInfo::STATUS_PUBLISHED,
692
                'initialLanguageCode' => 'eng-US',
693
                'languageCodes' => [
694
                    'eng-US',
695
                ],
696
            ],
697
            $versionInfo
698
        );
699
        $this->assertTrue($versionInfo->isPublished());
700
        $this->assertFalse($versionInfo->isDraft());
701
        $this->assertFalse($versionInfo->isArchived());
702
    }
703
704
    /**
705
     * Test for the loadVersionInfoById() method.
706
     *
707
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
708
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
709
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
710
     */
711 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundException()
712
    {
713
        $repository = $this->getRepository();
714
715
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
716
        /* BEGIN: Use Case */
717
        $contentService = $repository->getContentService();
718
719
        // This call will fail with a "NotFoundException"
720
        $contentService->loadVersionInfoById($nonExistentContentId);
721
        /* END: Use Case */
722
    }
723
724
    /**
725
     * Test for the loadContentByContentInfo() method.
726
     *
727
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
728
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
729
     */
730
    public function testLoadContentByContentInfo()
731
    {
732
        $repository = $this->getRepository();
733
734
        $mediaFolderId = $this->generateId('object', 41);
735
        /* BEGIN: Use Case */
736
        // $mediaFolderId contains the ID of the "Media" folder
737
738
        $contentService = $repository->getContentService();
739
740
        // Load the ContentInfo for "Media" folder
741
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
742
743
        // Now load the current content version for the info instance
744
        $content = $contentService->loadContentByContentInfo($contentInfo);
745
        /* END: Use Case */
746
747
        $this->assertInstanceOf(
748
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
749
            $content
750
        );
751
    }
752
753
    /**
754
     * Test for the loadContentByVersionInfo() method.
755
     *
756
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
757
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
758
     */
759
    public function testLoadContentByVersionInfo()
760
    {
761
        $repository = $this->getRepository();
762
763
        $mediaFolderId = $this->generateId('object', 41);
764
        /* BEGIN: Use Case */
765
        // $mediaFolderId contains the ID of the "Media" folder
766
767
        $contentService = $repository->getContentService();
768
769
        // Load the ContentInfo for "Media" folder
770
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
771
772
        // Load the current VersionInfo
773
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
774
775
        // Now load the current content version for the info instance
776
        $content = $contentService->loadContentByVersionInfo($versionInfo);
777
        /* END: Use Case */
778
779
        $this->assertInstanceOf(
780
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
781
            $content
782
        );
783
    }
784
785
    /**
786
     * Test for the loadContent() method.
787
     *
788
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
789
     * @group user
790
     * @group field-type
791
     */
792 View Code Duplication
    public function testLoadContent()
793
    {
794
        $repository = $this->getRepository();
795
796
        $mediaFolderId = $this->generateId('object', 41);
797
        /* BEGIN: Use Case */
798
        // $mediaFolderId contains the ID of the "Media" folder
799
800
        $contentService = $repository->getContentService();
801
802
        // Load the Content for "Media" folder, any language and current version
803
        $content = $contentService->loadContent($mediaFolderId);
804
        /* END: Use Case */
805
806
        $this->assertInstanceOf(
807
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
808
            $content
809
        );
810
    }
811
812
    /**
813
     * Test for the loadContent() method.
814
     *
815
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
816
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
817
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
818
     */
819 View Code Duplication
    public function testLoadContentThrowsNotFoundException()
820
    {
821
        $repository = $this->getRepository();
822
823
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
824
        /* BEGIN: Use Case */
825
        $contentService = $repository->getContentService();
826
827
        // This call will fail with a "NotFoundException"
828
        $contentService->loadContent($nonExistentContentId);
829
        /* END: Use Case */
830
    }
831
832
    /**
833
     * Data provider for testLoadContentByRemoteId().
834
     *
835
     * @return array
836
     */
837
    public function contentRemoteIdVersionLanguageProvider()
838
    {
839
        return [
840
            ['f5c88a2209584891056f987fd965b0ba', null, null],
841
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], null],
842
            ['f5c88a2209584891056f987fd965b0ba', null, 1],
843
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], 1],
844
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, null],
845
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], null],
846
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, 1],
847
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], 1],
848
        ];
849
    }
850
851
    /**
852
     * Test for the loadContentByRemoteId() method.
853
     *
854
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId
855
     * @dataProvider contentRemoteIdVersionLanguageProvider
856
     *
857
     * @param string $remoteId
858
     * @param array|null $languages
859
     * @param int $versionNo
860
     */
861
    public function testLoadContentByRemoteId($remoteId, $languages, $versionNo)
862
    {
863
        $repository = $this->getRepository();
864
865
        $contentService = $repository->getContentService();
866
867
        $content = $contentService->loadContentByRemoteId($remoteId, $languages, $versionNo);
868
869
        $this->assertInstanceOf(
870
            Content::class,
871
            $content
872
        );
873
874
        $this->assertEquals($remoteId, $content->contentInfo->remoteId);
875
        if ($languages !== null) {
876
            $this->assertEquals($languages, $content->getVersionInfo()->languageCodes);
877
        }
878
        $this->assertEquals($versionNo ?: 1, $content->getVersionInfo()->versionNo);
879
    }
880
881
    /**
882
     * Test for the loadContentByRemoteId() method.
883
     *
884
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
885
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
886
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
887
     */
888
    public function testLoadContentByRemoteIdThrowsNotFoundException()
889
    {
890
        $repository = $this->getRepository();
891
892
        /* BEGIN: Use Case */
893
        $contentService = $repository->getContentService();
894
895
        // This call will fail with a "NotFoundException", because no content
896
        // object exists for the given remoteId
897
        $contentService->loadContentByRemoteId('a1b1c1d1e1f1a2b2c2d2e2f2a3b3c3d3');
898
        /* END: Use Case */
899
    }
900
901
    /**
902
     * Test for the publishVersion() method.
903
     *
904
     * @return \eZ\Publish\API\Repository\Values\Content\Content
905
     *
906
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
907
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
908
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
909
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
910
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
911
     * @group user
912
     * @group field-type
913
     */
914
    public function testPublishVersion()
915
    {
916
        $time = time();
917
        /* BEGIN: Use Case */
918
        $content = $this->createContentVersion1();
919
        /* END: Use Case */
920
921
        $this->assertInstanceOf(Content::class, $content);
922
        $this->assertTrue($content->contentInfo->published);
923
        $this->assertEquals(VersionInfo::STATUS_PUBLISHED, $content->versionInfo->status);
924
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->publishedDate->getTimestamp());
925
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->modificationDate->getTimestamp());
926
        $this->assertTrue($content->versionInfo->isPublished());
927
        $this->assertFalse($content->versionInfo->isDraft());
928
        $this->assertFalse($content->versionInfo->isArchived());
929
930
        return $content;
931
    }
932
933
    /**
934
     * Test for the publishVersion() method.
935
     *
936
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
937
     *
938
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
939
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
940
     */
941
    public function testPublishVersionSetsExpectedContentInfo($content)
942
    {
943
        $this->assertEquals(
944
            array(
945
                $content->id,
946
                true,
947
                1,
948
                'abcdef0123456789abcdef0123456789',
949
                'eng-US',
950
                $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...
951
                true,
952
            ),
953
            array(
954
                $content->contentInfo->id,
955
                $content->contentInfo->alwaysAvailable,
956
                $content->contentInfo->currentVersionNo,
957
                $content->contentInfo->remoteId,
958
                $content->contentInfo->mainLanguageCode,
959
                $content->contentInfo->ownerId,
960
                $content->contentInfo->published,
961
            )
962
        );
963
964
        $this->assertNotNull($content->contentInfo->mainLocationId);
965
        $date = new \DateTime('1984/01/01');
966
        $this->assertGreaterThan(
967
            $date->getTimestamp(),
968
            $content->contentInfo->publishedDate->getTimestamp()
969
        );
970
    }
971
972
    /**
973
     * Test for the publishVersion() method.
974
     *
975
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
976
     *
977
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
978
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
979
     */
980
    public function testPublishVersionSetsExpectedVersionInfo($content)
981
    {
982
        $this->assertEquals(
983
            array(
984
                $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...
985
                'eng-US',
986
                VersionInfo::STATUS_PUBLISHED,
987
                1,
988
            ),
989
            array(
990
                $content->getVersionInfo()->creatorId,
991
                $content->getVersionInfo()->initialLanguageCode,
992
                $content->getVersionInfo()->status,
993
                $content->getVersionInfo()->versionNo,
994
            )
995
        );
996
997
        $date = new \DateTime('1984/01/01');
998
        $this->assertGreaterThan(
999
            $date->getTimestamp(),
1000
            $content->getVersionInfo()->modificationDate->getTimestamp()
1001
        );
1002
1003
        $this->assertNotNull($content->getVersionInfo()->modificationDate);
1004
        $this->assertTrue($content->getVersionInfo()->isPublished());
1005
        $this->assertFalse($content->getVersionInfo()->isDraft());
1006
        $this->assertFalse($content->getVersionInfo()->isArchived());
1007
    }
1008
1009
    /**
1010
     * Test for the publishVersion() method.
1011
     *
1012
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1013
     *
1014
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1015
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
1016
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1017
     */
1018
    public function testPublishVersionCreatesLocationsDefinedOnCreate()
1019
    {
1020
        $repository = $this->getRepository();
1021
1022
        /* BEGIN: Use Case */
1023
        $content = $this->createContentVersion1();
1024
        /* END: Use Case */
1025
1026
        $locationService = $repository->getLocationService();
1027
        $location = $locationService->loadLocationByRemoteId(
1028
            '0123456789abcdef0123456789abcdef'
1029
        );
1030
1031
        $this->assertEquals(
1032
            $location->getContentInfo(),
1033
            $content->getVersionInfo()->getContentInfo()
1034
        );
1035
1036
        return array($content, $location);
1037
    }
1038
1039
    /**
1040
     * Test for the publishVersion() method.
1041
     *
1042
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1043
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionCreatesLocationsDefinedOnCreate
1044
     */
1045
    public function testCreateContentWithLocationCreateParameterCreatesExpectedLocation(array $testData)
1046
    {
1047
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
1048
        /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
1049
        list($content, $location) = $testData;
1050
1051
        $parentLocationId = $this->generateId('location', 56);
1052
        $parentLocation = $this->getRepository()->getLocationService()->loadLocation($parentLocationId);
1053
        $mainLocationId = $content->getVersionInfo()->getContentInfo()->mainLocationId;
1054
1055
        $this->assertPropertiesCorrect(
1056
            array(
1057
                'id' => $mainLocationId,
1058
                'priority' => 23,
1059
                'hidden' => true,
1060
                'invisible' => true,
1061
                'remoteId' => '0123456789abcdef0123456789abcdef',
1062
                'parentLocationId' => $parentLocationId,
1063
                'pathString' => $parentLocation->pathString . $mainLocationId . '/',
1064
                'depth' => $parentLocation->depth + 1,
1065
                'sortField' => Location::SORT_FIELD_NODE_ID,
1066
                'sortOrder' => Location::SORT_ORDER_DESC,
1067
            ),
1068
            $location
1069
        );
1070
    }
1071
1072
    /**
1073
     * Test for the publishVersion() method.
1074
     *
1075
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1076
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1077
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1078
     */
1079 View Code Duplication
    public function testPublishVersionThrowsBadStateException()
1080
    {
1081
        $repository = $this->getRepository();
1082
1083
        $contentService = $repository->getContentService();
1084
1085
        /* BEGIN: Use Case */
1086
        $draft = $this->createContentDraftVersion1();
1087
1088
        // Publish the content draft
1089
        $contentService->publishVersion($draft->getVersionInfo());
1090
1091
        // This call will fail with a "BadStateException", because the version
1092
        // is already published.
1093
        $contentService->publishVersion($draft->getVersionInfo());
1094
        /* END: Use Case */
1095
    }
1096
1097
    /**
1098
     * Test that publishVersion() does not affect publishedDate (assuming previous version exists).
1099
     *
1100
     * @covers \eZ\Publish\API\Repository\ContentService::publishVersion
1101
     */
1102
    public function testPublishVersionDoesNotChangePublishedDate()
1103
    {
1104
        $repository = $this->getRepository();
1105
1106
        $contentService = $repository->getContentService();
1107
1108
        $publishedContent = $this->createContentVersion1();
1109
1110
        // force timestamps to differ
1111
        sleep(1);
1112
1113
        $contentDraft = $contentService->createContentDraft($publishedContent->contentInfo);
1114
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1115
        $contentUpdateStruct->setField('name', 'New name');
1116
        $contentDraft = $contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
1117
        $republishedContent = $contentService->publishVersion($contentDraft->versionInfo);
1118
1119
        $this->assertEquals(
1120
            $publishedContent->contentInfo->publishedDate->getTimestamp(),
1121
            $republishedContent->contentInfo->publishedDate->getTimestamp()
1122
        );
1123
        $this->assertGreaterThan(
1124
            $publishedContent->contentInfo->modificationDate->getTimestamp(),
1125
            $republishedContent->contentInfo->modificationDate->getTimestamp()
1126
        );
1127
    }
1128
1129
    /**
1130
     * Test for the createContentDraft() method.
1131
     *
1132
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1133
     *
1134
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1135
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1136
     * @group user
1137
     */
1138
    public function testCreateContentDraft()
1139
    {
1140
        $repository = $this->getRepository();
1141
1142
        $contentService = $repository->getContentService();
1143
1144
        /* BEGIN: Use Case */
1145
        $content = $this->createContentVersion1();
1146
1147
        // Now we create a new draft from the published content
1148
        $draftedContent = $contentService->createContentDraft($content->contentInfo);
1149
        /* END: Use Case */
1150
1151
        $this->assertInstanceOf(
1152
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1153
            $draftedContent
1154
        );
1155
1156
        return $draftedContent;
1157
    }
1158
1159
    /**
1160
     * Test for the createContentDraft() method.
1161
     *
1162
     * Test that editor has access to edit own draft.
1163
     * Note: Editors have access to version_read, which is needed to load content drafts.
1164
     *
1165
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1166
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1167
     * @group user
1168
     */
1169 View Code Duplication
    public function testCreateContentDraftAndLoadAccess()
1170
    {
1171
        $repository = $this->getRepository();
1172
1173
        /* BEGIN: Use Case */
1174
        $user = $this->createUserVersion1();
1175
1176
        // Set new editor as user
1177
        $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...
1178
1179
        // Create draft
1180
        $draft = $this->createContentDraftVersion1(2, 'folder');
1181
1182
        // Try to load the draft
1183
        $contentService = $repository->getContentService();
1184
        $loadedDraft = $contentService->loadContent($draft->id);
1185
1186
        /* END: Use Case */
1187
1188
        $this->assertEquals($draft->id, $loadedDraft->id);
1189
    }
1190
1191
    /**
1192
     * Test for the createContentDraft() method.
1193
     *
1194
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1195
     *
1196
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1197
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1198
     */
1199
    public function testCreateContentDraftSetsExpectedProperties($draft)
1200
    {
1201
        $this->assertEquals(
1202
            array(
1203
                'fieldCount' => 2,
1204
                'relationCount' => 0,
1205
            ),
1206
            array(
1207
                'fieldCount' => count($draft->getFields()),
1208
                'relationCount' => count($this->getRepository()->getContentService()->loadRelations($draft->getVersionInfo())),
1209
            )
1210
        );
1211
    }
1212
1213
    /**
1214
     * Test for the createContentDraft() method.
1215
     *
1216
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1217
     *
1218
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1219
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1220
     */
1221
    public function testCreateContentDraftSetsContentInfo($draft)
1222
    {
1223
        $contentInfo = $draft->contentInfo;
1224
1225
        $this->assertEquals(
1226
            array(
1227
                $draft->id,
1228
                true,
1229
                1,
1230
                'eng-US',
1231
                $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...
1232
                'abcdef0123456789abcdef0123456789',
1233
                1,
1234
            ),
1235
            array(
1236
                $contentInfo->id,
1237
                $contentInfo->alwaysAvailable,
1238
                $contentInfo->currentVersionNo,
1239
                $contentInfo->mainLanguageCode,
1240
                $contentInfo->ownerId,
1241
                $contentInfo->remoteId,
1242
                $contentInfo->sectionId,
1243
            )
1244
        );
1245
    }
1246
1247
    /**
1248
     * Test for the createContentDraft() method.
1249
     *
1250
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1251
     *
1252
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1253
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1254
     */
1255
    public function testCreateContentDraftSetsVersionInfo($draft)
1256
    {
1257
        $versionInfo = $draft->getVersionInfo();
1258
1259
        $this->assertEquals(
1260
            array(
1261
                '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...
1262
                'initialLanguageCode' => 'eng-US',
1263
                'languageCodes' => array(0 => 'eng-US'),
1264
                'status' => VersionInfo::STATUS_DRAFT,
1265
                'versionNo' => 2,
1266
            ),
1267
            array(
1268
                'creatorId' => $versionInfo->creatorId,
1269
                'initialLanguageCode' => $versionInfo->initialLanguageCode,
1270
                'languageCodes' => $versionInfo->languageCodes,
1271
                'status' => $versionInfo->status,
1272
                'versionNo' => $versionInfo->versionNo,
1273
            )
1274
        );
1275
        $this->assertTrue($versionInfo->isDraft());
1276
        $this->assertFalse($versionInfo->isPublished());
1277
        $this->assertFalse($versionInfo->isArchived());
1278
    }
1279
1280
    /**
1281
     * Test for the createContentDraft() method.
1282
     *
1283
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1284
     *
1285
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1286
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1287
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
1288
     */
1289
    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...
1290
    {
1291
        $repository = $this->getRepository();
1292
1293
        $contentService = $repository->getContentService();
1294
1295
        /* BEGIN: Use Case */
1296
        $content = $this->createContentVersion1();
1297
1298
        // Now we create a new draft from the published content
1299
        $contentService->createContentDraft($content->contentInfo);
1300
1301
        // This call will still load the published version
1302
        $versionInfoPublished = $contentService->loadVersionInfo($content->contentInfo);
1303
        /* END: Use Case */
1304
1305
        $this->assertEquals(1, $versionInfoPublished->versionNo);
1306
    }
1307
1308
    /**
1309
     * Test for the createContentDraft() method.
1310
     *
1311
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1312
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
1313
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1314
     */
1315
    public function testCreateContentDraftLoadContentStillLoadsPublishedVersion()
1316
    {
1317
        $repository = $this->getRepository();
1318
1319
        $contentService = $repository->getContentService();
1320
1321
        /* BEGIN: Use Case */
1322
        $content = $this->createContentVersion1();
1323
1324
        // Now we create a new draft from the published content
1325
        $contentService->createContentDraft($content->contentInfo);
1326
1327
        // This call will still load the published content version
1328
        $contentPublished = $contentService->loadContent($content->id);
1329
        /* END: Use Case */
1330
1331
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1332
    }
1333
1334
    /**
1335
     * Test for the createContentDraft() method.
1336
     *
1337
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1338
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
1339
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1340
     */
1341
    public function testCreateContentDraftLoadContentByRemoteIdStillLoadsPublishedVersion()
1342
    {
1343
        $repository = $this->getRepository();
1344
1345
        $contentService = $repository->getContentService();
1346
1347
        /* BEGIN: Use Case */
1348
        $content = $this->createContentVersion1();
1349
1350
        // Now we create a new draft from the published content
1351
        $contentService->createContentDraft($content->contentInfo);
1352
1353
        // This call will still load the published content version
1354
        $contentPublished = $contentService->loadContentByRemoteId('abcdef0123456789abcdef0123456789');
1355
        /* END: Use Case */
1356
1357
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1358
    }
1359
1360
    /**
1361
     * Test for the createContentDraft() method.
1362
     *
1363
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1364
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
1365
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1366
     */
1367
    public function testCreateContentDraftLoadContentByContentInfoStillLoadsPublishedVersion()
1368
    {
1369
        $repository = $this->getRepository();
1370
1371
        $contentService = $repository->getContentService();
1372
1373
        /* BEGIN: Use Case */
1374
        $content = $this->createContentVersion1();
1375
1376
        // Now we create a new draft from the published content
1377
        $contentService->createContentDraft($content->contentInfo);
1378
1379
        // This call will still load the published content version
1380
        $contentPublished = $contentService->loadContentByContentInfo($content->contentInfo);
1381
        /* END: Use Case */
1382
1383
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1384
    }
1385
1386
    /**
1387
     * Test for the newContentUpdateStruct() method.
1388
     *
1389
     * @covers \eZ\Publish\API\Repository\ContentService::newContentUpdateStruct
1390
     * @group user
1391
     */
1392
    public function testNewContentUpdateStruct()
1393
    {
1394
        $repository = $this->getRepository();
1395
1396
        /* BEGIN: Use Case */
1397
        $contentService = $repository->getContentService();
1398
1399
        $updateStruct = $contentService->newContentUpdateStruct();
1400
        /* END: Use Case */
1401
1402
        $this->assertInstanceOf(
1403
            ContentUpdateStruct::class,
1404
            $updateStruct
1405
        );
1406
1407
        $this->assertPropertiesCorrect(
1408
            [
1409
                'initialLanguageCode' => null,
1410
                'fields' => [],
1411
            ],
1412
            $updateStruct
1413
        );
1414
    }
1415
1416
    /**
1417
     * Test for the updateContent() method.
1418
     *
1419
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1420
     *
1421
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1422
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1423
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1424
     * @group user
1425
     * @group field-type
1426
     */
1427
    public function testUpdateContent()
1428
    {
1429
        /* BEGIN: Use Case */
1430
        $draftVersion2 = $this->createUpdatedDraftVersion2();
1431
        /* END: Use Case */
1432
1433
        $this->assertInstanceOf(
1434
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1435
            $draftVersion2
1436
        );
1437
1438
        $this->assertEquals(
1439
            $this->generateId('user', 10),
1440
            $draftVersion2->versionInfo->creatorId,
1441
            'creatorId is not properly set on new Version'
1442
        );
1443
1444
        return $draftVersion2;
1445
    }
1446
1447
    /**
1448
     * Test for the updateContent_WithDifferentUser() method.
1449
     *
1450
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1451
     *
1452
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1453
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1454
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1455
     * @group user
1456
     * @group field-type
1457
     */
1458
    public function testUpdateContentWithDifferentUser()
1459
    {
1460
        /* BEGIN: Use Case */
1461
        $arrayWithDraftVersion2 = $this->createUpdatedDraftVersion2NotAdmin();
1462
        /* END: Use Case */
1463
1464
        $this->assertInstanceOf(
1465
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1466
            $arrayWithDraftVersion2[0]
1467
        );
1468
1469
        $this->assertEquals(
1470
            $this->generateId('user', $arrayWithDraftVersion2[1]),
1471
            $arrayWithDraftVersion2[0]->versionInfo->creatorId,
1472
            'creatorId is not properly set on new Version'
1473
        );
1474
1475
        return $arrayWithDraftVersion2[0];
1476
    }
1477
1478
    /**
1479
     * Test for the updateContent() method.
1480
     *
1481
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1482
     *
1483
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1484
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1485
     */
1486
    public function testUpdateContentSetsExpectedFields($content)
1487
    {
1488
        $actual = $this->normalizeFields($content->getFields());
1489
1490
        $expected = array(
1491
            new Field(
1492
                array(
1493
                    'id' => 0,
1494
                    'value' => true,
1495
                    'languageCode' => 'eng-GB',
1496
                    'fieldDefIdentifier' => 'description',
1497
                    'fieldTypeIdentifier' => 'ezrichtext',
1498
                )
1499
            ),
1500
            new Field(
1501
                array(
1502
                    'id' => 0,
1503
                    'value' => true,
1504
                    'languageCode' => 'eng-US',
1505
                    'fieldDefIdentifier' => 'description',
1506
                    'fieldTypeIdentifier' => 'ezrichtext',
1507
                )
1508
            ),
1509
            new Field(
1510
                array(
1511
                    'id' => 0,
1512
                    'value' => true,
1513
                    'languageCode' => 'eng-GB',
1514
                    'fieldDefIdentifier' => 'name',
1515
                    'fieldTypeIdentifier' => 'ezstring',
1516
                )
1517
            ),
1518
            new Field(
1519
                array(
1520
                    'id' => 0,
1521
                    'value' => true,
1522
                    'languageCode' => 'eng-US',
1523
                    'fieldDefIdentifier' => 'name',
1524
                    'fieldTypeIdentifier' => 'ezstring',
1525
                )
1526
            ),
1527
        );
1528
1529
        $this->assertEquals($expected, $actual);
1530
    }
1531
1532
    /**
1533
     * Test for the updateContent() method.
1534
     *
1535
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1536
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1537
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1538
     */
1539
    public function testUpdateContentThrowsBadStateException()
1540
    {
1541
        $repository = $this->getRepository();
1542
1543
        $contentService = $repository->getContentService();
1544
1545
        /* BEGIN: Use Case */
1546
        $content = $this->createContentVersion1();
1547
1548
        // Now create an update struct and modify some fields
1549
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1550
        $contentUpdateStruct->setField('title', 'An awesome² story about ezp.');
1551
        $contentUpdateStruct->setField('title', 'An awesome²³ story about ezp.', 'eng-GB');
1552
1553
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1554
1555
        // This call will fail with a "BadStateException", because $publishedContent
1556
        // is not a draft.
1557
        $contentService->updateContent(
1558
            $content->getVersionInfo(),
1559
            $contentUpdateStruct
1560
        );
1561
        /* END: Use Case */
1562
    }
1563
1564
    /**
1565
     * Test for the updateContent() method.
1566
     *
1567
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1568
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1569
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1570
     */
1571 View Code Duplication
    public function testUpdateContentThrowsInvalidArgumentExceptionWhenFieldTypeDoesNotAccept()
1572
    {
1573
        $repository = $this->getRepository();
1574
1575
        $contentService = $repository->getContentService();
1576
1577
        /* BEGIN: Use Case */
1578
        $draft = $this->createContentDraftVersion1();
1579
1580
        // Now create an update struct and modify some fields
1581
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1582
        // The name field does not accept a stdClass object as its input
1583
        $contentUpdateStruct->setField('name', new \stdClass(), 'eng-US');
1584
1585
        // Throws an InvalidArgumentException, since the value for field "name"
1586
        // is not accepted
1587
        $contentService->updateContent(
1588
            $draft->getVersionInfo(),
1589
            $contentUpdateStruct
1590
        );
1591
        /* END: Use Case */
1592
    }
1593
1594
    /**
1595
     * Test for the updateContent() method.
1596
     *
1597
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1598
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1599
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1600
     */
1601 View Code Duplication
    public function testUpdateContentWhenMandatoryFieldIsEmpty()
1602
    {
1603
        $repository = $this->getRepository();
1604
1605
        $contentService = $repository->getContentService();
1606
1607
        /* BEGIN: Use Case */
1608
        $draft = $this->createContentDraftVersion1();
1609
1610
        // Now create an update struct and set a mandatory field to null
1611
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1612
        $contentUpdateStruct->setField('name', null);
1613
1614
        // Don't set this, then the above call without languageCode will fail
1615
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1616
1617
        // This call will fail with a "ContentFieldValidationException", because the
1618
        // mandatory "name" field is empty.
1619
        $contentService->updateContent(
1620
            $draft->getVersionInfo(),
1621
            $contentUpdateStruct
1622
        );
1623
        /* END: Use Case */
1624
    }
1625
1626
    /**
1627
     * Test for the updateContent() method.
1628
     *
1629
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1630
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1631
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1632
     */
1633
    public function testUpdateContentThrowsContentFieldValidationException()
1634
    {
1635
        $repository = $this->getRepository();
1636
1637
        /* BEGIN: Use Case */
1638
        $contentTypeService = $repository->getContentTypeService();
1639
        $contentService = $repository->getContentService();
1640
1641
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1642
1643
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1644
        $contentCreate->setField('name', 'An awesome Sidelfingen folder');
1645
1646
        $draft = $contentService->createContent($contentCreate);
1647
1648
        $contentUpdate = $contentService->newContentUpdateStruct();
1649
        // Violates string length constraint
1650
        $contentUpdate->setField('short_name', str_repeat('a', 200), 'eng-US');
1651
1652
        // Throws ContentFieldValidationException because the string length
1653
        // validation of the field "short_name" fails
1654
        $contentService->updateContent($draft->getVersionInfo(), $contentUpdate);
1655
        /* END: Use Case */
1656
    }
1657
1658
    /**
1659
     * Test for the updateContent() method.
1660
     *
1661
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1662
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1663
     */
1664
    public function testUpdateContentWithNotUpdatingMandatoryField()
1665
    {
1666
        $repository = $this->getRepository();
1667
1668
        $contentService = $repository->getContentService();
1669
1670
        /* BEGIN: Use Case */
1671
        $draft = $this->createContentDraftVersion1();
1672
1673
        // Now create an update struct which does not overwrite mandatory
1674
        // fields
1675
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1676
        $contentUpdateStruct->setField(
1677
            'description',
1678
            '<?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"/>'
1679
        );
1680
1681
        // Don't set this, then the above call without languageCode will fail
1682
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1683
1684
        // This will only update the "description" field in the "eng-US"
1685
        // language
1686
        $updatedDraft = $contentService->updateContent(
1687
            $draft->getVersionInfo(),
1688
            $contentUpdateStruct
1689
        );
1690
        /* END: Use Case */
1691
1692
        foreach ($updatedDraft->getFields() as $field) {
1693
            if ($field->languageCode === 'eng-US' && $field->fieldDefIdentifier === 'name' && $field->value !== null) {
0 ignored issues
show
Documentation introduced by
The property $value is declared protected in eZ\Publish\API\Repository\Values\Content\Field. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1694
                // Found field
1695
                return;
1696
            }
1697
        }
1698
        $this->fail(
1699
            'Field with identifier "name" in language "eng-US" could not be found or has empty value.'
1700
        );
1701
    }
1702
1703
    /**
1704
     * Test for the createContentDraft() method.
1705
     *
1706
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
1707
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1708
     */
1709
    public function testCreateContentDraftWithSecondParameter()
1710
    {
1711
        $repository = $this->getRepository();
1712
1713
        $contentService = $repository->getContentService();
1714
1715
        /* BEGIN: Use Case */
1716
        $contentVersion2 = $this->createContentVersion2();
1717
1718
        // Now we create a new draft from the initial version
1719
        $draftedContentReloaded = $contentService->createContentDraft(
1720
            $contentVersion2->contentInfo,
1721
            $contentVersion2->getVersionInfo()
1722
        );
1723
        /* END: Use Case */
1724
1725
        $this->assertEquals(3, $draftedContentReloaded->getVersionInfo()->versionNo);
1726
    }
1727
1728
    /**
1729
     * Test for the createContentDraft() method with third parameter.
1730
     *
1731
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1732
     */
1733 View Code Duplication
    public function testCreateContentDraftWithThirdParameter()
1734
    {
1735
        $repository = $this->getRepository();
1736
1737
        $contentService = $repository->getContentService();
1738
1739
        $content = $contentService->loadContent(4);
1740
        $user = $this->createUserVersion1();
1741
1742
        $draftContent = $contentService->createContentDraft(
1743
            $content->contentInfo,
1744
            $content->getVersionInfo(),
1745
            $user
1746
        );
1747
1748
        $this->assertInstanceOf(
1749
            Content::class,
1750
            $draftContent
1751
        );
1752
    }
1753
1754
    /**
1755
     * Test for the publishVersion() method.
1756
     *
1757
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1758
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1759
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1760
     */
1761 View Code Duplication
    public function testPublishVersionFromContentDraft()
1762
    {
1763
        $repository = $this->getRepository();
1764
1765
        $contentService = $repository->getContentService();
1766
1767
        /* BEGIN: Use Case */
1768
        $contentVersion2 = $this->createContentVersion2();
1769
        /* END: Use Case */
1770
1771
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo);
1772
1773
        $this->assertEquals(
1774
            array(
1775
                'status' => VersionInfo::STATUS_PUBLISHED,
1776
                'versionNo' => 2,
1777
            ),
1778
            array(
1779
                'status' => $versionInfo->status,
1780
                'versionNo' => $versionInfo->versionNo,
1781
            )
1782
        );
1783
        $this->assertTrue($versionInfo->isPublished());
1784
        $this->assertFalse($versionInfo->isDraft());
1785
        $this->assertFalse($versionInfo->isArchived());
1786
    }
1787
1788
    /**
1789
     * Test for the publishVersion() method.
1790
     *
1791
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1792
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1793
     */
1794 View Code Duplication
    public function testPublishVersionFromContentDraftArchivesOldVersion()
1795
    {
1796
        $repository = $this->getRepository();
1797
1798
        $contentService = $repository->getContentService();
1799
1800
        /* BEGIN: Use Case */
1801
        $contentVersion2 = $this->createContentVersion2();
1802
        /* END: Use Case */
1803
1804
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo, 1);
1805
1806
        $this->assertEquals(
1807
            array(
1808
                'status' => VersionInfo::STATUS_ARCHIVED,
1809
                'versionNo' => 1,
1810
            ),
1811
            array(
1812
                'status' => $versionInfo->status,
1813
                'versionNo' => $versionInfo->versionNo,
1814
            )
1815
        );
1816
        $this->assertTrue($versionInfo->isArchived());
1817
        $this->assertFalse($versionInfo->isDraft());
1818
        $this->assertFalse($versionInfo->isPublished());
1819
    }
1820
1821
    /**
1822
     * Test for the publishVersion() method.
1823
     *
1824
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1825
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1826
     */
1827
    public function testPublishVersionFromContentDraftUpdatesContentInfoCurrentVersion()
1828
    {
1829
        /* BEGIN: Use Case */
1830
        $contentVersion2 = $this->createContentVersion2();
1831
        /* END: Use Case */
1832
1833
        $this->assertEquals(2, $contentVersion2->contentInfo->currentVersionNo);
1834
    }
1835
1836
    /**
1837
     * Test for the publishVersion() method.
1838
     *
1839
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1840
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1841
     */
1842
    public function testPublishVersionFromOldContentDraftArchivesNewerVersionNo()
1843
    {
1844
        $repository = $this->getRepository();
1845
1846
        $contentService = $repository->getContentService();
1847
1848
        /* BEGIN: Use Case */
1849
        $content = $this->createContentVersion1();
1850
1851
        // Create a new draft with versionNo = 2
1852
        $draftedContentVersion2 = $contentService->createContentDraft($content->contentInfo);
1853
1854
        // Create another new draft with versionNo = 3
1855
        $draftedContentVersion3 = $contentService->createContentDraft($content->contentInfo);
1856
1857
        // Publish draft with versionNo = 3
1858
        $contentService->publishVersion($draftedContentVersion3->getVersionInfo());
1859
1860
        // Publish the first draft with versionNo = 2
1861
        // currentVersionNo is now 2, versionNo 3 will be archived
1862
        $publishedDraft = $contentService->publishVersion($draftedContentVersion2->getVersionInfo());
1863
        /* END: Use Case */
1864
1865
        $this->assertEquals(2, $publishedDraft->contentInfo->currentVersionNo);
1866
    }
1867
1868
    /**
1869
     * Test for the publishVersion() method, and that it creates limited archives.
1870
     *
1871
     * @todo Adapt this when per content type archive limited is added on repository Content Type model.
1872
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1873
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1874
     */
1875
    public function testPublishVersionNotCreatingUnlimitedArchives()
1876
    {
1877
        $repository = $this->getRepository();
1878
1879
        $contentService = $repository->getContentService();
1880
1881
        $content = $this->createContentVersion1();
1882
1883
        // load first to make sure list gets updated also (cache)
1884
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
1885
        $this->assertEquals(1, count($versionInfoList));
1886
        $this->assertEquals(1, $versionInfoList[0]->versionNo);
1887
1888
        // Create a new draft with versionNo = 2
1889
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1890
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1891
1892
        // Create a new draft with versionNo = 3
1893
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1894
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1895
1896
        // Create a new draft with versionNo = 4
1897
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1898
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1899
1900
        // Create a new draft with versionNo = 5
1901
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1902
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1903
1904
        // Create a new draft with versionNo = 6
1905
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1906
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1907
1908
        // Create a new draft with versionNo = 7
1909
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1910
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1911
1912
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
1913
1914
        $this->assertEquals(6, count($versionInfoList));
1915
        $this->assertEquals(2, $versionInfoList[0]->versionNo);
1916
        $this->assertEquals(7, $versionInfoList[5]->versionNo);
1917
1918
        $this->assertEquals(
1919
            [
1920
                VersionInfo::STATUS_ARCHIVED,
1921
                VersionInfo::STATUS_ARCHIVED,
1922
                VersionInfo::STATUS_ARCHIVED,
1923
                VersionInfo::STATUS_ARCHIVED,
1924
                VersionInfo::STATUS_ARCHIVED,
1925
                VersionInfo::STATUS_PUBLISHED,
1926
            ],
1927
            [
1928
                $versionInfoList[0]->status,
1929
                $versionInfoList[1]->status,
1930
                $versionInfoList[2]->status,
1931
                $versionInfoList[3]->status,
1932
                $versionInfoList[4]->status,
1933
                $versionInfoList[5]->status,
1934
            ]
1935
        );
1936
    }
1937
1938
    /**
1939
     * Test for the newContentMetadataUpdateStruct() method.
1940
     *
1941
     * @covers \eZ\Publish\API\Repository\ContentService::newContentMetadataUpdateStruct
1942
     * @group user
1943
     */
1944
    public function testNewContentMetadataUpdateStruct()
1945
    {
1946
        $repository = $this->getRepository();
1947
1948
        /* BEGIN: Use Case */
1949
        $contentService = $repository->getContentService();
1950
1951
        // Creates a new metadata update struct
1952
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1953
1954
        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...
1955
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
1956
        }
1957
1958
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1959
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1960
        $metadataUpdate->alwaysAvailable = false;
1961
        /* END: Use Case */
1962
1963
        $this->assertInstanceOf(
1964
            ContentMetadataUpdateStruct::class,
1965
            $metadataUpdate
1966
        );
1967
    }
1968
1969
    /**
1970
     * Test for the updateContentMetadata() method.
1971
     *
1972
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1973
     *
1974
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1975
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1976
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentMetadataUpdateStruct
1977
     * @group user
1978
     */
1979
    public function testUpdateContentMetadata()
1980
    {
1981
        $repository = $this->getRepository();
1982
1983
        $contentService = $repository->getContentService();
1984
1985
        /* BEGIN: Use Case */
1986
        $content = $this->createContentVersion1();
1987
1988
        // Creates a metadata update struct
1989
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1990
1991
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1992
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1993
        $metadataUpdate->alwaysAvailable = false;
1994
        $metadataUpdate->publishedDate = $this->createDateTime(441759600); // 1984/01/01
1995
        $metadataUpdate->modificationDate = $this->createDateTime(441759600); // 1984/01/01
1996
1997
        // Update the metadata of the published content object
1998
        $content = $contentService->updateContentMetadata(
1999
            $content->contentInfo,
2000
            $metadataUpdate
2001
        );
2002
        /* END: Use Case */
2003
2004
        $this->assertInstanceOf(
2005
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
2006
            $content
2007
        );
2008
2009
        return $content;
2010
    }
2011
2012
    /**
2013
     * Test for the updateContentMetadata() method.
2014
     *
2015
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2016
     *
2017
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2018
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2019
     */
2020
    public function testUpdateContentMetadataSetsExpectedProperties($content)
2021
    {
2022
        $contentInfo = $content->contentInfo;
2023
2024
        $this->assertEquals(
2025
            array(
2026
                'remoteId' => 'aaaabbbbccccddddeeeeffff11112222',
2027
                'sectionId' => $this->generateId('section', 1),
2028
                'alwaysAvailable' => false,
2029
                'currentVersionNo' => 1,
2030
                'mainLanguageCode' => 'eng-GB',
2031
                'modificationDate' => $this->createDateTime(441759600),
2032
                '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...
2033
                'published' => true,
2034
                'publishedDate' => $this->createDateTime(441759600),
2035
            ),
2036
            array(
2037
                'remoteId' => $contentInfo->remoteId,
2038
                'sectionId' => $contentInfo->sectionId,
2039
                'alwaysAvailable' => $contentInfo->alwaysAvailable,
2040
                'currentVersionNo' => $contentInfo->currentVersionNo,
2041
                'mainLanguageCode' => $contentInfo->mainLanguageCode,
2042
                'modificationDate' => $contentInfo->modificationDate,
2043
                'ownerId' => $contentInfo->ownerId,
2044
                'published' => $contentInfo->published,
2045
                'publishedDate' => $contentInfo->publishedDate,
2046
            )
2047
        );
2048
    }
2049
2050
    /**
2051
     * Test for the updateContentMetadata() method.
2052
     *
2053
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2054
     *
2055
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2056
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2057
     */
2058
    public function testUpdateContentMetadataNotUpdatesContentVersion($content)
2059
    {
2060
        $this->assertEquals(1, $content->getVersionInfo()->versionNo);
2061
    }
2062
2063
    /**
2064
     * Test for the updateContentMetadata() method.
2065
     *
2066
     * @covers \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2067
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2068
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2069
     */
2070
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnDuplicateRemoteId()
2071
    {
2072
        $repository = $this->getRepository();
2073
2074
        $contentService = $repository->getContentService();
2075
2076
        /* BEGIN: Use Case */
2077
        // RemoteId of the "Media" page of an eZ Publish demo installation
2078
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2079
2080
        $content = $this->createContentVersion1();
2081
2082
        // Creates a metadata update struct
2083
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2084
        $metadataUpdate->remoteId = $mediaRemoteId;
2085
2086
        // This call will fail with an "InvalidArgumentException", because the
2087
        // specified remoteId is already used by the "Media" page.
2088
        $contentService->updateContentMetadata(
2089
            $content->contentInfo,
2090
            $metadataUpdate
2091
        );
2092
        /* END: Use Case */
2093
    }
2094
2095
    /**
2096
     * Test for the updateContentMetadata() method.
2097
     *
2098
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContentMetadata
2099
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2100
     */
2101
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnNoMetadataPropertiesSet()
2102
    {
2103
        $repository = $this->getRepository();
2104
2105
        $contentService = $repository->getContentService();
2106
2107
        $contentInfo = $contentService->loadContentInfo(4);
2108
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
2109
2110
        // Throws an exception because no properties are set in $contentMetadataUpdateStruct
2111
        $contentService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
2112
    }
2113
2114
    /**
2115
     * Test for the deleteContent() method.
2116
     *
2117
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2118
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2119
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2120
     */
2121 View Code Duplication
    public function testDeleteContent()
2122
    {
2123
        $repository = $this->getRepository();
2124
2125
        $contentService = $repository->getContentService();
2126
        $locationService = $repository->getLocationService();
2127
2128
        /* BEGIN: Use Case */
2129
        $contentVersion2 = $this->createContentVersion2();
2130
2131
        // Load the locations for this content object
2132
        $locations = $locationService->loadLocations($contentVersion2->contentInfo);
2133
2134
        // This will delete the content, all versions and the associated locations
2135
        $contentService->deleteContent($contentVersion2->contentInfo);
2136
        /* END: Use Case */
2137
2138
        foreach ($locations as $location) {
2139
            $locationService->loadLocation($location->id);
2140
        }
2141
    }
2142
2143
    /**
2144
     * Test for the deleteContent() method.
2145
     *
2146
     * Test for issue EZP-21057:
2147
     * "contentService: Unable to delete a content with an empty file attribute"
2148
     *
2149
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2150
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2151
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2152
     */
2153 View Code Duplication
    public function testDeleteContentWithEmptyBinaryField()
2154
    {
2155
        $repository = $this->getRepository();
2156
2157
        $contentService = $repository->getContentService();
2158
        $locationService = $repository->getLocationService();
2159
2160
        /* BEGIN: Use Case */
2161
        $contentVersion = $this->createContentVersion1EmptyBinaryField();
2162
2163
        // Load the locations for this content object
2164
        $locations = $locationService->loadLocations($contentVersion->contentInfo);
2165
2166
        // This will delete the content, all versions and the associated locations
2167
        $contentService->deleteContent($contentVersion->contentInfo);
2168
        /* END: Use Case */
2169
2170
        foreach ($locations as $location) {
2171
            $locationService->loadLocation($location->id);
2172
        }
2173
    }
2174
2175
    /**
2176
     * Test for the loadContentDrafts() method.
2177
     *
2178
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2179
     */
2180
    public function testLoadContentDraftsReturnsEmptyArrayByDefault()
2181
    {
2182
        $repository = $this->getRepository();
2183
2184
        /* BEGIN: Use Case */
2185
        $contentService = $repository->getContentService();
2186
2187
        $contentDrafts = $contentService->loadContentDrafts();
2188
        /* END: Use Case */
2189
2190
        $this->assertSame(array(), $contentDrafts);
2191
    }
2192
2193
    /**
2194
     * Test for the loadContentDrafts() method.
2195
     *
2196
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2197
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2198
     */
2199
    public function testLoadContentDrafts()
2200
    {
2201
        $repository = $this->getRepository();
2202
2203
        /* BEGIN: Use Case */
2204
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
2205
        // of a eZ Publish demo installation.
2206
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2207
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
2208
2209
        $contentService = $repository->getContentService();
2210
2211
        // "Media" content object
2212
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2213
2214
        // "eZ Publish Demo Design ..." content object
2215
        $demoDesignContentInfo = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
2216
2217
        // Create some drafts
2218
        $contentService->createContentDraft($mediaContentInfo);
2219
        $contentService->createContentDraft($demoDesignContentInfo);
2220
2221
        // Now $contentDrafts should contain two drafted versions
2222
        $draftedVersions = $contentService->loadContentDrafts();
2223
        /* END: Use Case */
2224
2225
        $actual = array(
2226
            $draftedVersions[0]->status,
2227
            $draftedVersions[0]->getContentInfo()->remoteId,
2228
            $draftedVersions[1]->status,
2229
            $draftedVersions[1]->getContentInfo()->remoteId,
2230
        );
2231
        sort($actual, SORT_STRING);
2232
2233
        $this->assertEquals(
2234
            array(
2235
                VersionInfo::STATUS_DRAFT,
2236
                VersionInfo::STATUS_DRAFT,
2237
                $demoDesignRemoteId,
2238
                $mediaRemoteId,
2239
            ),
2240
            $actual
2241
        );
2242
    }
2243
2244
    /**
2245
     * Test for the loadContentDrafts() method.
2246
     *
2247
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
2248
     */
2249
    public function testLoadContentDraftsWithFirstParameter()
2250
    {
2251
        $repository = $this->getRepository();
2252
2253
        /* BEGIN: Use Case */
2254
        $user = $this->createUserVersion1();
2255
2256
        // Get current user
2257
        $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...
2258
2259
        // Set new editor as user
2260
        $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...
2261
2262
        // Remote id of the "Media" content object in an eZ Publish demo installation.
2263
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2264
2265
        $contentService = $repository->getContentService();
2266
2267
        // "Media" content object
2268
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2269
2270
        // Create a content draft
2271
        $contentService->createContentDraft($mediaContentInfo);
2272
2273
        // Reset to previous current user
2274
        $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...
2275
2276
        // Now $contentDrafts for the previous current user and the new user
2277
        $newCurrentUserDrafts = $contentService->loadContentDrafts($user);
2278
        $oldCurrentUserDrafts = $contentService->loadContentDrafts($oldCurrentUser);
2279
        /* END: Use Case */
2280
2281
        $this->assertSame(array(), $oldCurrentUserDrafts);
2282
2283
        $this->assertEquals(
2284
            array(
2285
                VersionInfo::STATUS_DRAFT,
2286
                $mediaRemoteId,
2287
            ),
2288
            array(
2289
                $newCurrentUserDrafts[0]->status,
2290
                $newCurrentUserDrafts[0]->getContentInfo()->remoteId,
2291
            )
2292
        );
2293
        $this->assertTrue($newCurrentUserDrafts[0]->isDraft());
2294
        $this->assertFalse($newCurrentUserDrafts[0]->isArchived());
2295
        $this->assertFalse($newCurrentUserDrafts[0]->isPublished());
2296
    }
2297
2298
    /**
2299
     * Test for the loadVersionInfo() method.
2300
     *
2301
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2302
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2303
     */
2304
    public function testLoadVersionInfoWithSecondParameter()
2305
    {
2306
        $repository = $this->getRepository();
2307
2308
        $contentService = $repository->getContentService();
2309
2310
        /* BEGIN: Use Case */
2311
        $publishedContent = $this->createContentVersion1();
2312
2313
        $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...
2314
2315
        // Will return the VersionInfo of the $draftContent
2316
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2317
        /* END: Use Case */
2318
2319
        $this->assertEquals(2, $versionInfo->versionNo);
2320
2321
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2322
        $this->assertEquals(
2323
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2324
            $versionInfo->getContentInfo()->mainLocationId
2325
        );
2326
    }
2327
2328
    /**
2329
     * Test for the loadVersionInfo() method.
2330
     *
2331
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2332
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2333
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2334
     */
2335
    public function testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter()
2336
    {
2337
        $repository = $this->getRepository();
2338
2339
        $contentService = $repository->getContentService();
2340
2341
        /* BEGIN: Use Case */
2342
        $draft = $this->createContentDraftVersion1();
2343
2344
        // This call will fail with a "NotFoundException", because not versionNo
2345
        // 2 exists for this content object.
2346
        $contentService->loadVersionInfo($draft->contentInfo, 2);
2347
        /* END: Use Case */
2348
    }
2349
2350
    /**
2351
     * Test for the loadVersionInfoById() method.
2352
     *
2353
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2354
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2355
     */
2356
    public function testLoadVersionInfoByIdWithSecondParameter()
2357
    {
2358
        $repository = $this->getRepository();
2359
2360
        $contentService = $repository->getContentService();
2361
2362
        /* BEGIN: Use Case */
2363
        $publishedContent = $this->createContentVersion1();
2364
2365
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
2366
2367
        // Will return the VersionInfo of the $draftContent
2368
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2369
        /* END: Use Case */
2370
2371
        $this->assertEquals(2, $versionInfo->versionNo);
2372
2373
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2374
        $this->assertEquals(
2375
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2376
            $versionInfo->getContentInfo()->mainLocationId
2377
        );
2378
2379
        return [
2380
            'versionInfo' => $versionInfo,
2381
            'draftContent' => $draftContent,
2382
        ];
2383
    }
2384
2385
    /**
2386
     * Test for the returned value of the loadVersionInfoById() method.
2387
     *
2388
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter
2389
     * @covers \eZ\Publish\API\Repository\ContentService::loadVersionInfoById
2390
     *
2391
     * @param array $data
2392
     */
2393
    public function testLoadVersionInfoByIdWithSecondParameterSetsExpectedVersionInfo(array $data)
2394
    {
2395
        /** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo */
2396
        $versionInfo = $data['versionInfo'];
2397
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $draftContent */
2398
        $draftContent = $data['draftContent'];
2399
2400
        $this->assertPropertiesCorrect(
2401
            [
2402
                'names' => [
2403
                    'eng-US' => 'An awesome forum',
2404
                ],
2405
                'contentInfo' => new ContentInfo([
2406
                    'id' => $draftContent->contentInfo->id,
2407
                    'contentTypeId' => 28,
2408
                    'name' => 'An awesome forum',
2409
                    'sectionId' => 1,
2410
                    'currentVersionNo' => 1,
2411
                    'published' => true,
2412
                    'ownerId' => 14,
2413
                    // this Content Object is created at the test runtime
2414
                    'modificationDate' => $versionInfo->contentInfo->modificationDate,
2415
                    'publishedDate' => $versionInfo->contentInfo->publishedDate,
2416
                    'alwaysAvailable' => 1,
2417
                    'remoteId' => 'abcdef0123456789abcdef0123456789',
2418
                    'mainLanguageCode' => 'eng-US',
2419
                    'mainLocationId' => $draftContent->contentInfo->mainLocationId,
2420
                ]),
2421
                'id' => $draftContent->versionInfo->id,
2422
                'versionNo' => 2,
2423
                'creatorId' => 14,
2424
                'status' => 0,
2425
                'initialLanguageCode' => 'eng-US',
2426
                'languageCodes' => [
2427
                    'eng-US',
2428
                ],
2429
            ],
2430
            $versionInfo
2431
        );
2432
    }
2433
2434
    /**
2435
     * Test for the loadVersionInfoById() method.
2436
     *
2437
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2438
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2439
     */
2440 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter()
2441
    {
2442
        $repository = $this->getRepository();
2443
2444
        $contentService = $repository->getContentService();
2445
2446
        /* BEGIN: Use Case */
2447
        $content = $this->createContentVersion1();
2448
2449
        // This call will fail with a "NotFoundException", because not versionNo
2450
        // 2 exists for this content object.
2451
        $contentService->loadVersionInfoById($content->id, 2);
2452
        /* END: Use Case */
2453
    }
2454
2455
    /**
2456
     * Test for the loadContentByVersionInfo() method.
2457
     *
2458
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
2459
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2460
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
2461
     */
2462
    public function testLoadContentByVersionInfoWithSecondParameter()
2463
    {
2464
        $repository = $this->getRepository();
2465
2466
        $sectionId = $this->generateId('section', 1);
2467
        /* BEGIN: Use Case */
2468
        $contentTypeService = $repository->getContentTypeService();
2469
2470
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2471
2472
        $contentService = $repository->getContentService();
2473
2474
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2475
2476
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2477
2478
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2479
2480
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2481
        // $sectionId contains the ID of section 1
2482
        $contentCreateStruct->sectionId = $sectionId;
2483
        $contentCreateStruct->alwaysAvailable = true;
2484
2485
        // Create a new content draft
2486
        $content = $contentService->createContent($contentCreateStruct);
2487
2488
        // Now publish this draft
2489
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2490
2491
        // Will return a content instance with fields in "eng-US"
2492
        $reloadedContent = $contentService->loadContentByVersionInfo(
2493
            $publishedContent->getVersionInfo(),
2494
            array(
2495
                'eng-GB',
2496
            ),
2497
            false
2498
        );
2499
        /* END: Use Case */
2500
2501
        $actual = array();
2502 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...
2503
            $actual[] = new Field(
2504
                array(
2505
                    'id' => 0,
2506
                    'value' => ($field->value !== null ? true : null), // Actual value tested by FieldType integration tests
0 ignored issues
show
Documentation introduced by
The property $value is declared protected in eZ\Publish\API\Repository\Values\Content\Field. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

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

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
5721
                    'languageCode' => $field->languageCode,
5722
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
5723
                    'fieldTypeIdentifier' => $field->fieldTypeIdentifier,
5724
                )
5725
            );
5726
        }
5727
        usort(
5728
            $normalized,
5729 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...
5730
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
5731
                    return strcasecmp($field1->languageCode, $field2->languageCode);
5732
                }
5733
5734
                return $return;
5735
            }
5736
        );
5737
5738
        return $normalized;
5739
    }
5740
5741
    /**
5742
     * Returns a filtered set of the default fields fixture.
5743
     *
5744
     * @param string $languageCode
5745
     *
5746
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5747
     */
5748
    private function createLocaleFieldsFixture($languageCode)
5749
    {
5750
        $fields = array();
5751
        foreach ($this->createFieldsFixture() as $field) {
5752
            if (null === $field->languageCode || $languageCode === $field->languageCode) {
5753
                $fields[] = $field;
5754
            }
5755
        }
5756
5757
        return $fields;
5758
    }
5759
5760
    /**
5761
     * Asserts that given Content has default ContentStates.
5762
     *
5763
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
5764
     */
5765 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
5766
    {
5767
        $repository = $this->getRepository();
5768
        $objectStateService = $repository->getObjectStateService();
5769
5770
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
5771
5772
        foreach ($objectStateGroups as $objectStateGroup) {
5773
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
5774
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
5775
                // Only check the first object state which is the default one.
5776
                $this->assertEquals(
5777
                    $objectState,
5778
                    $contentState
5779
                );
5780
                break;
5781
            }
5782
        }
5783
    }
5784
5785
    /**
5786
     * Assert that given Content has no references to a translation specified by the $languageCode.
5787
     *
5788
     * @param string $languageCode
5789
     * @param int $contentId
5790
     */
5791
    private function assertTranslationDoesNotExist($languageCode, $contentId)
5792
    {
5793
        $repository = $this->getRepository();
5794
        $contentService = $repository->getContentService();
5795
5796
        $content = $contentService->loadContent($contentId);
5797
5798
        foreach ($content->fields as $fieldIdentifier => $field) {
5799
            /** @var array $field */
5800
            self::assertArrayNotHasKey($languageCode, $field);
5801
            self::assertNotEquals($languageCode, $content->contentInfo->mainLanguageCode);
5802
            self::assertArrayNotHasKey($languageCode, $content->versionInfo->getNames());
5803
            self::assertNotEquals($languageCode, $content->versionInfo->initialLanguageCode);
5804
            self::assertNotContains($languageCode, $content->versionInfo->languageCodes);
5805
        }
5806
        foreach ($contentService->loadVersions($content->contentInfo) as $versionInfo) {
5807
            self::assertArrayNotHasKey($languageCode, $versionInfo->getNames());
5808
            self::assertNotEquals($languageCode, $versionInfo->contentInfo->mainLanguageCode);
5809
            self::assertNotEquals($languageCode, $versionInfo->initialLanguageCode);
5810
            self::assertNotContains($languageCode, $versionInfo->languageCodes);
5811
        }
5812
    }
5813
5814
    /**
5815
     * Returns the default fixture of fields used in most tests.
5816
     *
5817
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5818
     */
5819
    private function createFieldsFixture()
5820
    {
5821
        return array(
5822
            new Field(
5823
                array(
5824
                    'id' => 0,
5825
                    'value' => 'Foo',
5826
                    'languageCode' => 'eng-US',
5827
                    'fieldDefIdentifier' => 'description',
5828
                    'fieldTypeIdentifier' => 'ezrichtext',
5829
                )
5830
            ),
5831
            new Field(
5832
                array(
5833
                    'id' => 0,
5834
                    'value' => 'Bar',
5835
                    'languageCode' => 'eng-GB',
5836
                    'fieldDefIdentifier' => 'description',
5837
                    'fieldTypeIdentifier' => 'ezrichtext',
5838
                )
5839
            ),
5840
            new Field(
5841
                array(
5842
                    'id' => 0,
5843
                    'value' => 'An awesome multi-lang forum²',
5844
                    'languageCode' => 'eng-US',
5845
                    'fieldDefIdentifier' => 'name',
5846
                    'fieldTypeIdentifier' => 'ezstring',
5847
                )
5848
            ),
5849
            new Field(
5850
                array(
5851
                    'id' => 0,
5852
                    'value' => 'An awesome multi-lang forum²³',
5853
                    'languageCode' => 'eng-GB',
5854
                    'fieldDefIdentifier' => 'name',
5855
                    'fieldTypeIdentifier' => 'ezstring',
5856
                )
5857
            ),
5858
        );
5859
    }
5860
5861
    /**
5862
     * Gets expected property values for the "Media" ContentInfo ValueObject.
5863
     *
5864
     * @return array
5865
     */
5866 View Code Duplication
    private function getExpectedMediaContentInfoProperties()
5867
    {
5868
        return [
5869
            'id' => 41,
5870
            'contentTypeId' => 1,
5871
            'name' => 'Media',
5872
            'sectionId' => 3,
5873
            'currentVersionNo' => 1,
5874
            'published' => true,
5875
            'ownerId' => 14,
5876
            'modificationDate' => $this->createDateTime(1060695457),
5877
            'publishedDate' => $this->createDateTime(1060695457),
5878
            'alwaysAvailable' => 1,
5879
            'remoteId' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
5880
            'mainLanguageCode' => 'eng-US',
5881
            'mainLocationId' => 43,
5882
        ];
5883
    }
5884
}
5885