Completed
Push — master ( 254dd2...f64074 )
by André
35:14 queued 21:38
created

testDeleteTranslationLastLanguageThrowsBadStateException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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