Completed
Push — 7.0 ( 07af66...3d7579 )
by André
29:14
created

testRemoveTranslationUpdatesUrlAlias()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 5
nop 0
dl 0
loc 37
rs 8.5806
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
                )
1499
            ),
1500
            new Field(
1501
                array(
1502
                    'id' => 0,
1503
                    'value' => true,
1504
                    'languageCode' => 'eng-US',
1505
                    'fieldDefIdentifier' => 'description',
1506
                )
1507
            ),
1508
            new Field(
1509
                array(
1510
                    'id' => 0,
1511
                    'value' => true,
1512
                    'languageCode' => 'eng-GB',
1513
                    'fieldDefIdentifier' => 'name',
1514
                )
1515
            ),
1516
            new Field(
1517
                array(
1518
                    'id' => 0,
1519
                    'value' => true,
1520
                    'languageCode' => 'eng-US',
1521
                    'fieldDefIdentifier' => 'name',
1522
                )
1523
            ),
1524
        );
1525
1526
        $this->assertEquals($expected, $actual);
1527
    }
1528
1529
    /**
1530
     * Test for the updateContent() method.
1531
     *
1532
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1533
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1534
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1535
     */
1536
    public function testUpdateContentThrowsBadStateException()
1537
    {
1538
        $repository = $this->getRepository();
1539
1540
        $contentService = $repository->getContentService();
1541
1542
        /* BEGIN: Use Case */
1543
        $content = $this->createContentVersion1();
1544
1545
        // Now create an update struct and modify some fields
1546
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1547
        $contentUpdateStruct->setField('title', 'An awesome² story about ezp.');
1548
        $contentUpdateStruct->setField('title', 'An awesome²³ story about ezp.', 'eng-GB');
1549
1550
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1551
1552
        // This call will fail with a "BadStateException", because $publishedContent
1553
        // is not a draft.
1554
        $contentService->updateContent(
1555
            $content->getVersionInfo(),
1556
            $contentUpdateStruct
1557
        );
1558
        /* END: Use Case */
1559
    }
1560
1561
    /**
1562
     * Test for the updateContent() method.
1563
     *
1564
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1565
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1566
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1567
     */
1568 View Code Duplication
    public function testUpdateContentThrowsInvalidArgumentExceptionWhenFieldTypeDoesNotAccept()
1569
    {
1570
        $repository = $this->getRepository();
1571
1572
        $contentService = $repository->getContentService();
1573
1574
        /* BEGIN: Use Case */
1575
        $draft = $this->createContentDraftVersion1();
1576
1577
        // Now create an update struct and modify some fields
1578
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1579
        // The name field does not accept a stdClass object as its input
1580
        $contentUpdateStruct->setField('name', new \stdClass(), 'eng-US');
1581
1582
        // Throws an InvalidArgumentException, since the value for field "name"
1583
        // is not accepted
1584
        $contentService->updateContent(
1585
            $draft->getVersionInfo(),
1586
            $contentUpdateStruct
1587
        );
1588
        /* END: Use Case */
1589
    }
1590
1591
    /**
1592
     * Test for the updateContent() method.
1593
     *
1594
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1595
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1596
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1597
     */
1598 View Code Duplication
    public function testUpdateContentWhenMandatoryFieldIsEmpty()
1599
    {
1600
        $repository = $this->getRepository();
1601
1602
        $contentService = $repository->getContentService();
1603
1604
        /* BEGIN: Use Case */
1605
        $draft = $this->createContentDraftVersion1();
1606
1607
        // Now create an update struct and set a mandatory field to null
1608
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1609
        $contentUpdateStruct->setField('name', null);
1610
1611
        // Don't set this, then the above call without languageCode will fail
1612
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1613
1614
        // This call will fail with a "ContentFieldValidationException", because the
1615
        // mandatory "name" field is empty.
1616
        $contentService->updateContent(
1617
            $draft->getVersionInfo(),
1618
            $contentUpdateStruct
1619
        );
1620
        /* END: Use Case */
1621
    }
1622
1623
    /**
1624
     * Test for the updateContent() method.
1625
     *
1626
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1627
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1628
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1629
     */
1630
    public function testUpdateContentThrowsContentFieldValidationException()
1631
    {
1632
        $repository = $this->getRepository();
1633
1634
        /* BEGIN: Use Case */
1635
        $contentTypeService = $repository->getContentTypeService();
1636
        $contentService = $repository->getContentService();
1637
1638
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1639
1640
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1641
        $contentCreate->setField('name', 'An awesome Sidelfingen folder');
1642
1643
        $draft = $contentService->createContent($contentCreate);
1644
1645
        $contentUpdate = $contentService->newContentUpdateStruct();
1646
        // Violates string length constraint
1647
        $contentUpdate->setField('short_name', str_repeat('a', 200), 'eng-US');
1648
1649
        // Throws ContentFieldValidationException because the string length
1650
        // validation of the field "short_name" fails
1651
        $contentService->updateContent($draft->getVersionInfo(), $contentUpdate);
1652
        /* END: Use Case */
1653
    }
1654
1655
    /**
1656
     * Test for the updateContent() method.
1657
     *
1658
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1659
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1660
     */
1661
    public function testUpdateContentWithNotUpdatingMandatoryField()
1662
    {
1663
        $repository = $this->getRepository();
1664
1665
        $contentService = $repository->getContentService();
1666
1667
        /* BEGIN: Use Case */
1668
        $draft = $this->createContentDraftVersion1();
1669
1670
        // Now create an update struct which does not overwrite mandatory
1671
        // fields
1672
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1673
        $contentUpdateStruct->setField(
1674
            'description',
1675
            '<?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"/>'
1676
        );
1677
1678
        // Don't set this, then the above call without languageCode will fail
1679
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1680
1681
        // This will only update the "description" field in the "eng-US"
1682
        // language
1683
        $updatedDraft = $contentService->updateContent(
1684
            $draft->getVersionInfo(),
1685
            $contentUpdateStruct
1686
        );
1687
        /* END: Use Case */
1688
1689
        foreach ($updatedDraft->getFields() as $field) {
1690
            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...
1691
                // Found field
1692
                return;
1693
            }
1694
        }
1695
        $this->fail(
1696
            'Field with identifier "name" in language "eng-US" could not be found or has empty value.'
1697
        );
1698
    }
1699
1700
    /**
1701
     * Test for the createContentDraft() method.
1702
     *
1703
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
1704
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1705
     */
1706
    public function testCreateContentDraftWithSecondParameter()
1707
    {
1708
        $repository = $this->getRepository();
1709
1710
        $contentService = $repository->getContentService();
1711
1712
        /* BEGIN: Use Case */
1713
        $contentVersion2 = $this->createContentVersion2();
1714
1715
        // Now we create a new draft from the initial version
1716
        $draftedContentReloaded = $contentService->createContentDraft(
1717
            $contentVersion2->contentInfo,
1718
            $contentVersion2->getVersionInfo()
1719
        );
1720
        /* END: Use Case */
1721
1722
        $this->assertEquals(3, $draftedContentReloaded->getVersionInfo()->versionNo);
1723
    }
1724
1725
    /**
1726
     * Test for the createContentDraft() method with third parameter.
1727
     *
1728
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1729
     */
1730 View Code Duplication
    public function testCreateContentDraftWithThirdParameter()
1731
    {
1732
        $repository = $this->getRepository();
1733
1734
        $contentService = $repository->getContentService();
1735
1736
        $content = $contentService->loadContent(4);
1737
        $user = $this->createUserVersion1();
1738
1739
        $draftContent = $contentService->createContentDraft(
1740
            $content->contentInfo,
1741
            $content->getVersionInfo(),
1742
            $user
1743
        );
1744
1745
        $this->assertInstanceOf(
1746
            Content::class,
1747
            $draftContent
1748
        );
1749
    }
1750
1751
    /**
1752
     * Test for the publishVersion() method.
1753
     *
1754
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1755
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1756
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1757
     */
1758 View Code Duplication
    public function testPublishVersionFromContentDraft()
1759
    {
1760
        $repository = $this->getRepository();
1761
1762
        $contentService = $repository->getContentService();
1763
1764
        /* BEGIN: Use Case */
1765
        $contentVersion2 = $this->createContentVersion2();
1766
        /* END: Use Case */
1767
1768
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo);
1769
1770
        $this->assertEquals(
1771
            array(
1772
                'status' => VersionInfo::STATUS_PUBLISHED,
1773
                'versionNo' => 2,
1774
            ),
1775
            array(
1776
                'status' => $versionInfo->status,
1777
                'versionNo' => $versionInfo->versionNo,
1778
            )
1779
        );
1780
        $this->assertTrue($versionInfo->isPublished());
1781
        $this->assertFalse($versionInfo->isDraft());
1782
        $this->assertFalse($versionInfo->isArchived());
1783
    }
1784
1785
    /**
1786
     * Test for the publishVersion() method.
1787
     *
1788
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1789
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1790
     */
1791 View Code Duplication
    public function testPublishVersionFromContentDraftArchivesOldVersion()
1792
    {
1793
        $repository = $this->getRepository();
1794
1795
        $contentService = $repository->getContentService();
1796
1797
        /* BEGIN: Use Case */
1798
        $contentVersion2 = $this->createContentVersion2();
1799
        /* END: Use Case */
1800
1801
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo, 1);
1802
1803
        $this->assertEquals(
1804
            array(
1805
                'status' => VersionInfo::STATUS_ARCHIVED,
1806
                'versionNo' => 1,
1807
            ),
1808
            array(
1809
                'status' => $versionInfo->status,
1810
                'versionNo' => $versionInfo->versionNo,
1811
            )
1812
        );
1813
        $this->assertTrue($versionInfo->isArchived());
1814
        $this->assertFalse($versionInfo->isDraft());
1815
        $this->assertFalse($versionInfo->isPublished());
1816
    }
1817
1818
    /**
1819
     * Test for the publishVersion() method.
1820
     *
1821
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1822
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1823
     */
1824
    public function testPublishVersionFromContentDraftUpdatesContentInfoCurrentVersion()
1825
    {
1826
        /* BEGIN: Use Case */
1827
        $contentVersion2 = $this->createContentVersion2();
1828
        /* END: Use Case */
1829
1830
        $this->assertEquals(2, $contentVersion2->contentInfo->currentVersionNo);
1831
    }
1832
1833
    /**
1834
     * Test for the publishVersion() method.
1835
     *
1836
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1837
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1838
     */
1839
    public function testPublishVersionFromOldContentDraftArchivesNewerVersionNo()
1840
    {
1841
        $repository = $this->getRepository();
1842
1843
        $contentService = $repository->getContentService();
1844
1845
        /* BEGIN: Use Case */
1846
        $content = $this->createContentVersion1();
1847
1848
        // Create a new draft with versionNo = 2
1849
        $draftedContentVersion2 = $contentService->createContentDraft($content->contentInfo);
1850
1851
        // Create another new draft with versionNo = 3
1852
        $draftedContentVersion3 = $contentService->createContentDraft($content->contentInfo);
1853
1854
        // Publish draft with versionNo = 3
1855
        $contentService->publishVersion($draftedContentVersion3->getVersionInfo());
1856
1857
        // Publish the first draft with versionNo = 2
1858
        // currentVersionNo is now 2, versionNo 3 will be archived
1859
        $publishedDraft = $contentService->publishVersion($draftedContentVersion2->getVersionInfo());
1860
        /* END: Use Case */
1861
1862
        $this->assertEquals(2, $publishedDraft->contentInfo->currentVersionNo);
1863
    }
1864
1865
    /**
1866
     * Test for the publishVersion() method, and that it creates limited archives.
1867
     *
1868
     * @todo Adapt this when per content type archive limited is added on repository Content Type model.
1869
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1870
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1871
     */
1872
    public function testPublishVersionNotCreatingUnlimitedArchives()
1873
    {
1874
        $repository = $this->getRepository();
1875
1876
        $contentService = $repository->getContentService();
1877
1878
        $content = $this->createContentVersion1();
1879
1880
        // load first to make sure list gets updated also (cache)
1881
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
1882
        $this->assertEquals(1, count($versionInfoList));
1883
        $this->assertEquals(1, $versionInfoList[0]->versionNo);
1884
1885
        // Create a new draft with versionNo = 2
1886
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1887
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1888
1889
        // Create a new draft with versionNo = 3
1890
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1891
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1892
1893
        // Create a new draft with versionNo = 4
1894
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1895
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1896
1897
        // Create a new draft with versionNo = 5
1898
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1899
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1900
1901
        // Create a new draft with versionNo = 6
1902
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1903
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1904
1905
        // Create a new draft with versionNo = 7
1906
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1907
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1908
1909
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
1910
1911
        $this->assertEquals(6, count($versionInfoList));
1912
        $this->assertEquals(2, $versionInfoList[0]->versionNo);
1913
        $this->assertEquals(7, $versionInfoList[5]->versionNo);
1914
1915
        $this->assertEquals(
1916
            [
1917
                VersionInfo::STATUS_ARCHIVED,
1918
                VersionInfo::STATUS_ARCHIVED,
1919
                VersionInfo::STATUS_ARCHIVED,
1920
                VersionInfo::STATUS_ARCHIVED,
1921
                VersionInfo::STATUS_ARCHIVED,
1922
                VersionInfo::STATUS_PUBLISHED,
1923
            ],
1924
            [
1925
                $versionInfoList[0]->status,
1926
                $versionInfoList[1]->status,
1927
                $versionInfoList[2]->status,
1928
                $versionInfoList[3]->status,
1929
                $versionInfoList[4]->status,
1930
                $versionInfoList[5]->status,
1931
            ]
1932
        );
1933
    }
1934
1935
    /**
1936
     * Test for the newContentMetadataUpdateStruct() method.
1937
     *
1938
     * @covers \eZ\Publish\API\Repository\ContentService::newContentMetadataUpdateStruct
1939
     * @group user
1940
     */
1941
    public function testNewContentMetadataUpdateStruct()
1942
    {
1943
        $repository = $this->getRepository();
1944
1945
        /* BEGIN: Use Case */
1946
        $contentService = $repository->getContentService();
1947
1948
        // Creates a new metadata update struct
1949
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1950
1951
        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...
1952
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
1953
        }
1954
1955
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1956
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1957
        $metadataUpdate->alwaysAvailable = false;
1958
        /* END: Use Case */
1959
1960
        $this->assertInstanceOf(
1961
            ContentMetadataUpdateStruct::class,
1962
            $metadataUpdate
1963
        );
1964
    }
1965
1966
    /**
1967
     * Test for the updateContentMetadata() method.
1968
     *
1969
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1970
     *
1971
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1972
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1973
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentMetadataUpdateStruct
1974
     * @group user
1975
     */
1976
    public function testUpdateContentMetadata()
1977
    {
1978
        $repository = $this->getRepository();
1979
1980
        $contentService = $repository->getContentService();
1981
1982
        /* BEGIN: Use Case */
1983
        $content = $this->createContentVersion1();
1984
1985
        // Creates a metadata update struct
1986
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1987
1988
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1989
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1990
        $metadataUpdate->alwaysAvailable = false;
1991
        $metadataUpdate->publishedDate = $this->createDateTime(441759600); // 1984/01/01
1992
        $metadataUpdate->modificationDate = $this->createDateTime(441759600); // 1984/01/01
1993
1994
        // Update the metadata of the published content object
1995
        $content = $contentService->updateContentMetadata(
1996
            $content->contentInfo,
1997
            $metadataUpdate
1998
        );
1999
        /* END: Use Case */
2000
2001
        $this->assertInstanceOf(
2002
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
2003
            $content
2004
        );
2005
2006
        return $content;
2007
    }
2008
2009
    /**
2010
     * Test for the updateContentMetadata() method.
2011
     *
2012
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2013
     *
2014
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2015
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2016
     */
2017
    public function testUpdateContentMetadataSetsExpectedProperties($content)
2018
    {
2019
        $contentInfo = $content->contentInfo;
2020
2021
        $this->assertEquals(
2022
            array(
2023
                'remoteId' => 'aaaabbbbccccddddeeeeffff11112222',
2024
                'sectionId' => $this->generateId('section', 1),
2025
                'alwaysAvailable' => false,
2026
                'currentVersionNo' => 1,
2027
                'mainLanguageCode' => 'eng-GB',
2028
                'modificationDate' => $this->createDateTime(441759600),
2029
                '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...
2030
                'published' => true,
2031
                'publishedDate' => $this->createDateTime(441759600),
2032
            ),
2033
            array(
2034
                'remoteId' => $contentInfo->remoteId,
2035
                'sectionId' => $contentInfo->sectionId,
2036
                'alwaysAvailable' => $contentInfo->alwaysAvailable,
2037
                'currentVersionNo' => $contentInfo->currentVersionNo,
2038
                'mainLanguageCode' => $contentInfo->mainLanguageCode,
2039
                'modificationDate' => $contentInfo->modificationDate,
2040
                'ownerId' => $contentInfo->ownerId,
2041
                'published' => $contentInfo->published,
2042
                'publishedDate' => $contentInfo->publishedDate,
2043
            )
2044
        );
2045
    }
2046
2047
    /**
2048
     * Test for the updateContentMetadata() method.
2049
     *
2050
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2051
     *
2052
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2053
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2054
     */
2055
    public function testUpdateContentMetadataNotUpdatesContentVersion($content)
2056
    {
2057
        $this->assertEquals(1, $content->getVersionInfo()->versionNo);
2058
    }
2059
2060
    /**
2061
     * Test for the updateContentMetadata() method.
2062
     *
2063
     * @covers \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2064
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2065
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2066
     */
2067
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnDuplicateRemoteId()
2068
    {
2069
        $repository = $this->getRepository();
2070
2071
        $contentService = $repository->getContentService();
2072
2073
        /* BEGIN: Use Case */
2074
        // RemoteId of the "Media" page of an eZ Publish demo installation
2075
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2076
2077
        $content = $this->createContentVersion1();
2078
2079
        // Creates a metadata update struct
2080
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2081
        $metadataUpdate->remoteId = $mediaRemoteId;
2082
2083
        // This call will fail with an "InvalidArgumentException", because the
2084
        // specified remoteId is already used by the "Media" page.
2085
        $contentService->updateContentMetadata(
2086
            $content->contentInfo,
2087
            $metadataUpdate
2088
        );
2089
        /* END: Use Case */
2090
    }
2091
2092
    /**
2093
     * Test for the updateContentMetadata() method.
2094
     *
2095
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContentMetadata
2096
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2097
     */
2098
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnNoMetadataPropertiesSet()
2099
    {
2100
        $repository = $this->getRepository();
2101
2102
        $contentService = $repository->getContentService();
2103
2104
        $contentInfo = $contentService->loadContentInfo(4);
2105
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
2106
2107
        // Throws an exception because no properties are set in $contentMetadataUpdateStruct
2108
        $contentService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
2109
    }
2110
2111
    /**
2112
     * Test for the deleteContent() method.
2113
     *
2114
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2115
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2116
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2117
     */
2118 View Code Duplication
    public function testDeleteContent()
2119
    {
2120
        $repository = $this->getRepository();
2121
2122
        $contentService = $repository->getContentService();
2123
        $locationService = $repository->getLocationService();
2124
2125
        /* BEGIN: Use Case */
2126
        $contentVersion2 = $this->createContentVersion2();
2127
2128
        // Load the locations for this content object
2129
        $locations = $locationService->loadLocations($contentVersion2->contentInfo);
2130
2131
        // This will delete the content, all versions and the associated locations
2132
        $contentService->deleteContent($contentVersion2->contentInfo);
2133
        /* END: Use Case */
2134
2135
        foreach ($locations as $location) {
2136
            $locationService->loadLocation($location->id);
2137
        }
2138
    }
2139
2140
    /**
2141
     * Test for the deleteContent() method.
2142
     *
2143
     * Test for issue EZP-21057:
2144
     * "contentService: Unable to delete a content with an empty file attribute"
2145
     *
2146
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2147
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2148
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2149
     */
2150 View Code Duplication
    public function testDeleteContentWithEmptyBinaryField()
2151
    {
2152
        $repository = $this->getRepository();
2153
2154
        $contentService = $repository->getContentService();
2155
        $locationService = $repository->getLocationService();
2156
2157
        /* BEGIN: Use Case */
2158
        $contentVersion = $this->createContentVersion1EmptyBinaryField();
2159
2160
        // Load the locations for this content object
2161
        $locations = $locationService->loadLocations($contentVersion->contentInfo);
2162
2163
        // This will delete the content, all versions and the associated locations
2164
        $contentService->deleteContent($contentVersion->contentInfo);
2165
        /* END: Use Case */
2166
2167
        foreach ($locations as $location) {
2168
            $locationService->loadLocation($location->id);
2169
        }
2170
    }
2171
2172
    /**
2173
     * Test for the loadContentDrafts() method.
2174
     *
2175
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2176
     */
2177
    public function testLoadContentDraftsReturnsEmptyArrayByDefault()
2178
    {
2179
        $repository = $this->getRepository();
2180
2181
        /* BEGIN: Use Case */
2182
        $contentService = $repository->getContentService();
2183
2184
        $contentDrafts = $contentService->loadContentDrafts();
2185
        /* END: Use Case */
2186
2187
        $this->assertSame(array(), $contentDrafts);
2188
    }
2189
2190
    /**
2191
     * Test for the loadContentDrafts() method.
2192
     *
2193
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2194
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2195
     */
2196
    public function testLoadContentDrafts()
2197
    {
2198
        $repository = $this->getRepository();
2199
2200
        /* BEGIN: Use Case */
2201
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
2202
        // of a eZ Publish demo installation.
2203
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2204
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
2205
2206
        $contentService = $repository->getContentService();
2207
2208
        // "Media" content object
2209
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2210
2211
        // "eZ Publish Demo Design ..." content object
2212
        $demoDesignContentInfo = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
2213
2214
        // Create some drafts
2215
        $contentService->createContentDraft($mediaContentInfo);
2216
        $contentService->createContentDraft($demoDesignContentInfo);
2217
2218
        // Now $contentDrafts should contain two drafted versions
2219
        $draftedVersions = $contentService->loadContentDrafts();
2220
        /* END: Use Case */
2221
2222
        $actual = array(
2223
            $draftedVersions[0]->status,
2224
            $draftedVersions[0]->getContentInfo()->remoteId,
2225
            $draftedVersions[1]->status,
2226
            $draftedVersions[1]->getContentInfo()->remoteId,
2227
        );
2228
        sort($actual, SORT_STRING);
2229
2230
        $this->assertEquals(
2231
            array(
2232
                VersionInfo::STATUS_DRAFT,
2233
                VersionInfo::STATUS_DRAFT,
2234
                $demoDesignRemoteId,
2235
                $mediaRemoteId,
2236
            ),
2237
            $actual
2238
        );
2239
    }
2240
2241
    /**
2242
     * Test for the loadContentDrafts() method.
2243
     *
2244
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
2245
     */
2246
    public function testLoadContentDraftsWithFirstParameter()
2247
    {
2248
        $repository = $this->getRepository();
2249
2250
        /* BEGIN: Use Case */
2251
        $user = $this->createUserVersion1();
2252
2253
        // Get current user
2254
        $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...
2255
2256
        // Set new editor as user
2257
        $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...
2258
2259
        // Remote id of the "Media" content object in an eZ Publish demo installation.
2260
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2261
2262
        $contentService = $repository->getContentService();
2263
2264
        // "Media" content object
2265
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2266
2267
        // Create a content draft
2268
        $contentService->createContentDraft($mediaContentInfo);
2269
2270
        // Reset to previous current user
2271
        $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...
2272
2273
        // Now $contentDrafts for the previous current user and the new user
2274
        $newCurrentUserDrafts = $contentService->loadContentDrafts($user);
2275
        $oldCurrentUserDrafts = $contentService->loadContentDrafts($oldCurrentUser);
2276
        /* END: Use Case */
2277
2278
        $this->assertSame(array(), $oldCurrentUserDrafts);
2279
2280
        $this->assertEquals(
2281
            array(
2282
                VersionInfo::STATUS_DRAFT,
2283
                $mediaRemoteId,
2284
            ),
2285
            array(
2286
                $newCurrentUserDrafts[0]->status,
2287
                $newCurrentUserDrafts[0]->getContentInfo()->remoteId,
2288
            )
2289
        );
2290
        $this->assertTrue($newCurrentUserDrafts[0]->isDraft());
2291
        $this->assertFalse($newCurrentUserDrafts[0]->isArchived());
2292
        $this->assertFalse($newCurrentUserDrafts[0]->isPublished());
2293
    }
2294
2295
    /**
2296
     * Test for the loadVersionInfo() method.
2297
     *
2298
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2299
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2300
     */
2301
    public function testLoadVersionInfoWithSecondParameter()
2302
    {
2303
        $repository = $this->getRepository();
2304
2305
        $contentService = $repository->getContentService();
2306
2307
        /* BEGIN: Use Case */
2308
        $publishedContent = $this->createContentVersion1();
2309
2310
        $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...
2311
2312
        // Will return the VersionInfo of the $draftContent
2313
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2314
        /* END: Use Case */
2315
2316
        $this->assertEquals(2, $versionInfo->versionNo);
2317
2318
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2319
        $this->assertEquals(
2320
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2321
            $versionInfo->getContentInfo()->mainLocationId
2322
        );
2323
    }
2324
2325
    /**
2326
     * Test for the loadVersionInfo() method.
2327
     *
2328
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2329
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2330
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2331
     */
2332
    public function testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter()
2333
    {
2334
        $repository = $this->getRepository();
2335
2336
        $contentService = $repository->getContentService();
2337
2338
        /* BEGIN: Use Case */
2339
        $draft = $this->createContentDraftVersion1();
2340
2341
        // This call will fail with a "NotFoundException", because not versionNo
2342
        // 2 exists for this content object.
2343
        $contentService->loadVersionInfo($draft->contentInfo, 2);
2344
        /* END: Use Case */
2345
    }
2346
2347
    /**
2348
     * Test for the loadVersionInfoById() method.
2349
     *
2350
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2351
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2352
     */
2353
    public function testLoadVersionInfoByIdWithSecondParameter()
2354
    {
2355
        $repository = $this->getRepository();
2356
2357
        $contentService = $repository->getContentService();
2358
2359
        /* BEGIN: Use Case */
2360
        $publishedContent = $this->createContentVersion1();
2361
2362
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
2363
2364
        // Will return the VersionInfo of the $draftContent
2365
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2366
        /* END: Use Case */
2367
2368
        $this->assertEquals(2, $versionInfo->versionNo);
2369
2370
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2371
        $this->assertEquals(
2372
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2373
            $versionInfo->getContentInfo()->mainLocationId
2374
        );
2375
2376
        return [
2377
            'versionInfo' => $versionInfo,
2378
            'draftContent' => $draftContent,
2379
        ];
2380
    }
2381
2382
    /**
2383
     * Test for the returned value of the loadVersionInfoById() method.
2384
     *
2385
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter
2386
     * @covers \eZ\Publish\API\Repository\ContentService::loadVersionInfoById
2387
     *
2388
     * @param array $data
2389
     */
2390
    public function testLoadVersionInfoByIdWithSecondParameterSetsExpectedVersionInfo(array $data)
2391
    {
2392
        /** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo */
2393
        $versionInfo = $data['versionInfo'];
2394
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $draftContent */
2395
        $draftContent = $data['draftContent'];
2396
2397
        $this->assertPropertiesCorrect(
2398
            [
2399
                'names' => [
2400
                    'eng-US' => 'An awesome forum',
2401
                ],
2402
                'contentInfo' => new ContentInfo([
2403
                    'id' => $draftContent->contentInfo->id,
2404
                    'contentTypeId' => 28,
2405
                    'name' => 'An awesome forum',
2406
                    'sectionId' => 1,
2407
                    'currentVersionNo' => 1,
2408
                    'published' => true,
2409
                    'ownerId' => 14,
2410
                    // this Content Object is created at the test runtime
2411
                    'modificationDate' => $versionInfo->contentInfo->modificationDate,
2412
                    'publishedDate' => $versionInfo->contentInfo->publishedDate,
2413
                    'alwaysAvailable' => 1,
2414
                    'remoteId' => 'abcdef0123456789abcdef0123456789',
2415
                    'mainLanguageCode' => 'eng-US',
2416
                    'mainLocationId' => $draftContent->contentInfo->mainLocationId,
2417
                ]),
2418
                'id' => $draftContent->versionInfo->id,
2419
                'versionNo' => 2,
2420
                'creatorId' => 14,
2421
                'status' => 0,
2422
                'initialLanguageCode' => 'eng-US',
2423
                'languageCodes' => [
2424
                    'eng-US',
2425
                ],
2426
            ],
2427
            $versionInfo
2428
        );
2429
    }
2430
2431
    /**
2432
     * Test for the loadVersionInfoById() method.
2433
     *
2434
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2435
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2436
     */
2437 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter()
2438
    {
2439
        $repository = $this->getRepository();
2440
2441
        $contentService = $repository->getContentService();
2442
2443
        /* BEGIN: Use Case */
2444
        $content = $this->createContentVersion1();
2445
2446
        // This call will fail with a "NotFoundException", because not versionNo
2447
        // 2 exists for this content object.
2448
        $contentService->loadVersionInfoById($content->id, 2);
2449
        /* END: Use Case */
2450
    }
2451
2452
    /**
2453
     * Test for the loadContentByVersionInfo() method.
2454
     *
2455
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
2456
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2457
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
2458
     */
2459
    public function testLoadContentByVersionInfoWithSecondParameter()
2460
    {
2461
        $repository = $this->getRepository();
2462
2463
        $sectionId = $this->generateId('section', 1);
2464
        /* BEGIN: Use Case */
2465
        $contentTypeService = $repository->getContentTypeService();
2466
2467
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2468
2469
        $contentService = $repository->getContentService();
2470
2471
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2472
2473
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2474
2475
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2476
2477
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2478
        // $sectionId contains the ID of section 1
2479
        $contentCreateStruct->sectionId = $sectionId;
2480
        $contentCreateStruct->alwaysAvailable = true;
2481
2482
        // Create a new content draft
2483
        $content = $contentService->createContent($contentCreateStruct);
2484
2485
        // Now publish this draft
2486
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2487
2488
        // Will return a content instance with fields in "eng-US"
2489
        $reloadedContent = $contentService->loadContentByVersionInfo(
2490
            $publishedContent->getVersionInfo(),
2491
            array(
2492
                'eng-GB',
2493
            ),
2494
            false
2495
        );
2496
        /* END: Use Case */
2497
2498
        $actual = array();
2499 View Code Duplication
        foreach ($reloadedContent->getFields() as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

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

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

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

Loading history...
5515
            $normalized[] = new Field(
5516
                array(
5517
                    'id' => 0,
5518
                    '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...
5519
                    'languageCode' => $field->languageCode,
5520
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
5521
                )
5522
            );
5523
        }
5524
        usort(
5525
            $normalized,
5526 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...
5527
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
5528
                    return strcasecmp($field1->languageCode, $field2->languageCode);
5529
                }
5530
5531
                return $return;
5532
            }
5533
        );
5534
5535
        return $normalized;
5536
    }
5537
5538
    /**
5539
     * Returns a filtered set of the default fields fixture.
5540
     *
5541
     * @param string $languageCode
5542
     *
5543
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5544
     */
5545
    private function createLocaleFieldsFixture($languageCode)
5546
    {
5547
        $fields = array();
5548
        foreach ($this->createFieldsFixture() as $field) {
5549
            if (null === $field->languageCode || $languageCode === $field->languageCode) {
5550
                $fields[] = $field;
5551
            }
5552
        }
5553
5554
        return $fields;
5555
    }
5556
5557
    /**
5558
     * Asserts that given Content has default ContentStates.
5559
     *
5560
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
5561
     */
5562 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
5563
    {
5564
        $repository = $this->getRepository();
5565
        $objectStateService = $repository->getObjectStateService();
5566
5567
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
5568
5569
        foreach ($objectStateGroups as $objectStateGroup) {
5570
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
5571
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
5572
                // Only check the first object state which is the default one.
5573
                $this->assertEquals(
5574
                    $objectState,
5575
                    $contentState
5576
                );
5577
                break;
5578
            }
5579
        }
5580
    }
5581
5582
    /**
5583
     * Assert that given Content has no references to a translation specified by the $languageCode.
5584
     *
5585
     * @param string $languageCode
5586
     * @param int $contentId
5587
     */
5588
    private function assertTranslationDoesNotExist($languageCode, $contentId)
5589
    {
5590
        $repository = $this->getRepository();
5591
        $contentService = $repository->getContentService();
5592
5593
        $content = $contentService->loadContent($contentId);
5594
5595
        foreach ($content->fields as $fieldIdentifier => $field) {
5596
            /** @var array $field */
5597
            self::assertArrayNotHasKey($languageCode, $field);
5598
            self::assertNotEquals($languageCode, $content->contentInfo->mainLanguageCode);
5599
            self::assertArrayNotHasKey($languageCode, $content->versionInfo->getNames());
5600
            self::assertNotEquals($languageCode, $content->versionInfo->initialLanguageCode);
5601
            self::assertNotContains($languageCode, $content->versionInfo->languageCodes);
5602
        }
5603
        foreach ($contentService->loadVersions($content->contentInfo) as $versionInfo) {
5604
            self::assertArrayNotHasKey($languageCode, $versionInfo->getNames());
5605
            self::assertNotEquals($languageCode, $versionInfo->contentInfo->mainLanguageCode);
5606
            self::assertNotEquals($languageCode, $versionInfo->initialLanguageCode);
5607
            self::assertNotContains($languageCode, $versionInfo->languageCodes);
5608
        }
5609
    }
5610
5611
    /**
5612
     * Returns the default fixture of fields used in most tests.
5613
     *
5614
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5615
     */
5616
    private function createFieldsFixture()
5617
    {
5618
        return array(
5619
            new Field(
5620
                array(
5621
                    'id' => 0,
5622
                    'value' => 'Foo',
5623
                    'languageCode' => 'eng-US',
5624
                    'fieldDefIdentifier' => 'description',
5625
                )
5626
            ),
5627
            new Field(
5628
                array(
5629
                    'id' => 0,
5630
                    'value' => 'Bar',
5631
                    'languageCode' => 'eng-GB',
5632
                    'fieldDefIdentifier' => 'description',
5633
                )
5634
            ),
5635
            new Field(
5636
                array(
5637
                    'id' => 0,
5638
                    'value' => 'An awesome multi-lang forum²',
5639
                    'languageCode' => 'eng-US',
5640
                    'fieldDefIdentifier' => 'name',
5641
                )
5642
            ),
5643
            new Field(
5644
                array(
5645
                    'id' => 0,
5646
                    'value' => 'An awesome multi-lang forum²³',
5647
                    'languageCode' => 'eng-GB',
5648
                    'fieldDefIdentifier' => 'name',
5649
                )
5650
            ),
5651
        );
5652
    }
5653
5654
    /**
5655
     * Gets expected property values for the "Media" ContentInfo ValueObject.
5656
     *
5657
     * @return array
5658
     */
5659 View Code Duplication
    private function getExpectedMediaContentInfoProperties()
5660
    {
5661
        return [
5662
            'id' => 41,
5663
            'contentTypeId' => 1,
5664
            'name' => 'Media',
5665
            'sectionId' => 3,
5666
            'currentVersionNo' => 1,
5667
            'published' => true,
5668
            'ownerId' => 14,
5669
            'modificationDate' => $this->createDateTime(1060695457),
5670
            'publishedDate' => $this->createDateTime(1060695457),
5671
            'alwaysAvailable' => 1,
5672
            'remoteId' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
5673
            'mainLanguageCode' => 'eng-US',
5674
            'mainLocationId' => 43,
5675
        ];
5676
    }
5677
}
5678