Completed
Push — fix_travis_behat ( 061bab...ab732a )
by
unknown
30:38 queued 14:06
created

testLoadContentDraftsWithFirstParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

Loading history...
253
                'initialLanguageCode' => 'eng-US',
254
            ),
255
            array(
256
                'status' => $content->getVersionInfo()->status,
257
                'versionNo' => $content->getVersionInfo()->versionNo,
258
                'creatorId' => $content->getVersionInfo()->creatorId,
259
                'initialLanguageCode' => $content->getVersionInfo()->initialLanguageCode,
260
            )
261
        );
262
        $this->assertTrue($content->getVersionInfo()->isDraft());
263
        $this->assertFalse($content->getVersionInfo()->isPublished());
264
        $this->assertFalse($content->getVersionInfo()->isArchived());
265
    }
266
267
    /**
268
     * Test for the createContent() method.
269
     *
270
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
271
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
272
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
273
     */
274
    public function testCreateContentThrowsInvalidArgumentException()
275
    {
276
        if ($this->isVersion4()) {
277
            $this->markTestSkipped('This test requires eZ Publish 5');
278
        }
279
280
        $repository = $this->getRepository();
281
282
        /* BEGIN: Use Case */
283
        $contentTypeService = $repository->getContentTypeService();
284
        $contentService = $repository->getContentService();
285
286
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
287
288
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
289
        $contentCreate1->setField('name', 'An awesome Sidelfingen forum');
290
291
        $contentCreate1->remoteId = 'abcdef0123456789abcdef0123456789';
292
        $contentCreate1->alwaysAvailable = true;
293
294
        $draft = $contentService->createContent($contentCreate1);
295
        $contentService->publishVersion($draft->versionInfo);
296
297
        $contentCreate2 = $contentService->newContentCreateStruct($contentType, 'eng-GB');
298
        $contentCreate2->setField('name', 'An awesome Bielefeld forum');
299
300
        $contentCreate2->remoteId = 'abcdef0123456789abcdef0123456789';
301
        $contentCreate2->alwaysAvailable = false;
302
303
        // This call will fail with an "InvalidArgumentException", because the
304
        // remoteId is already in use.
305
        $contentService->createContent($contentCreate2);
306
        /* END: Use Case */
307
    }
308
309
    /**
310
     * Test for the createContent() method.
311
     *
312
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
313
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
314
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
315
     */
316 View Code Duplication
    public function testCreateContentThrowsInvalidArgumentExceptionOnFieldTypeNotAccept()
317
    {
318
        $repository = $this->getRepository();
319
320
        /* BEGIN: Use Case */
321
        $contentTypeService = $repository->getContentTypeService();
322
        $contentService = $repository->getContentService();
323
324
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
325
326
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
327
        // The name field does only accept strings and null as its values
328
        $contentCreate->setField('name', new \stdClass());
329
330
        // Throws InvalidArgumentException since the name field is filled
331
        // improperly
332
        $draft = $contentService->createContent($contentCreate);
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
333
        /* END: Use Case */
334
    }
335
336
    /**
337
     * Test for the createContent() method.
338
     *
339
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
340
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
341
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
342
     */
343
    public function testCreateContentThrowsContentFieldValidationException()
344
    {
345
        $repository = $this->getRepository();
346
347
        /* BEGIN: Use Case */
348
        $contentTypeService = $repository->getContentTypeService();
349
        $contentService = $repository->getContentService();
350
351
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
352
353
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
354
        $contentCreate1->setField('name', 'An awesome Sidelfingen folder');
355
        // Violates string length constraint
356
        $contentCreate1->setField('short_name', str_repeat('a', 200));
357
358
        // Throws ContentFieldValidationException, since short_name does not pass
359
        // validation of the string length validator
360
        $draft = $contentService->createContent($contentCreate1);
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
361
        /* END: Use Case */
362
    }
363
364
    /**
365
     * Test for the createContent() method.
366
     *
367
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
368
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
369
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
370
     */
371 View Code Duplication
    public function testCreateContentRequiredFieldMissing()
372
    {
373
        $repository = $this->getRepository();
374
375
        /* BEGIN: Use Case */
376
        $contentTypeService = $repository->getContentTypeService();
377
        $contentService = $repository->getContentService();
378
379
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
380
381
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
382
        // Required field "name" is not set
383
384
        // Throws a ContentFieldValidationException, since a required field is
385
        // missing
386
        $draft = $contentService->createContent($contentCreate1);
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
387
        /* END: Use Case */
388
    }
389
390
    /**
391
     * Test for the createContent() method.
392
     *
393
     * NOTE: We have bidirectional dependencies between the ContentService and
394
     * the LocationService, so that we cannot use PHPUnit's test dependencies
395
     * here.
396
     *
397
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
398
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
399
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationByRemoteId
400
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
401
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
402
     * @group user
403
     */
404
    public function testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately()
405
    {
406
        $repository = $this->getRepository();
407
408
        $locationService = $repository->getLocationService();
409
410
        /* BEGIN: Use Case */
411
        $draft = $this->createContentDraftVersion1();
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
412
413
        // The location will not have been created, yet, so this throws an
414
        // exception
415
        $location = $locationService->loadLocationByRemoteId(
0 ignored issues
show
Unused Code introduced by
$location is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
416
            '0123456789abcdef0123456789abcdef'
417
        );
418
        /* END: Use Case */
419
    }
420
421
    /**
422
     * Test for the createContent() method.
423
     *
424
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
425
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
426
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
427
     */
428
    public function testCreateContentThrowsInvalidArgumentExceptionWithLocationCreateParameter()
429
    {
430
        $repository = $this->getRepository();
431
432
        $parentLocationId = $this->generateId('location', 56);
433
        /* BEGIN: Use Case */
434
        // $parentLocationId is a valid location ID
435
436
        $contentService = $repository->getContentService();
437
        $contentTypeService = $repository->getContentTypeService();
438
        $locationService = $repository->getLocationService();
439
440
        // Load content type
441
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
442
443
        // Configure new locations
444
        $locationCreate1 = $locationService->newLocationCreateStruct($parentLocationId);
445
446
        $locationCreate1->priority = 23;
447
        $locationCreate1->hidden = true;
448
        $locationCreate1->remoteId = '0123456789abcdef0123456789aaaaaa';
449
        $locationCreate1->sortField = Location::SORT_FIELD_NODE_ID;
450
        $locationCreate1->sortOrder = Location::SORT_ORDER_DESC;
451
452
        $locationCreate2 = $locationService->newLocationCreateStruct($parentLocationId);
453
454
        $locationCreate2->priority = 42;
455
        $locationCreate2->hidden = true;
456
        $locationCreate2->remoteId = '0123456789abcdef0123456789bbbbbb';
457
        $locationCreate2->sortField = Location::SORT_FIELD_NODE_ID;
458
        $locationCreate2->sortOrder = Location::SORT_ORDER_DESC;
459
460
        // Configure new content object
461
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
462
463
        $contentCreate->setField('name', 'A awesome Sindelfingen forum');
464
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
465
        $contentCreate->alwaysAvailable = true;
466
467
        // Create new content object under the specified location
468
        $draft = $contentService->createContent(
469
            $contentCreate,
470
            array($locationCreate1)
471
        );
472
        $contentService->publishVersion($draft->versionInfo);
473
474
        // This call will fail with an "InvalidArgumentException", because the
475
        // Content remoteId already exists,
476
        $contentService->createContent(
477
            $contentCreate,
478
            array($locationCreate2)
479
        );
480
        /* END: Use Case */
481
    }
482
483
    /**
484
     * Test for the loadContentInfo() method.
485
     *
486
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
487
     * @group user
488
     */
489 View Code Duplication
    public function testLoadContentInfo()
490
    {
491
        $repository = $this->getRepository();
492
493
        $mediaFolderId = $this->generateId('object', 41);
494
        /* BEGIN: Use Case */
495
        $contentService = $repository->getContentService();
496
497
        // Load the ContentInfo for "Media" folder
498
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
499
        /* END: Use Case */
500
501
        $this->assertInstanceOf(
502
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
503
            $contentInfo
504
        );
505
506
        return $contentInfo;
507
    }
508
509
    /**
510
     * Test for the returned value of the loadContentInfo() method.
511
     *
512
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
513
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfo
514
     *
515
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
516
     */
517
    public function testLoadContentInfoSetsExpectedContentInfo(ContentInfo $contentInfo)
518
    {
519
        $this->assertPropertiesCorrectUnsorted(
520
            $this->getExpectedMediaContentInfoProperties(),
521
            $contentInfo
522
        );
523
    }
524
525
    /**
526
     * Test for the loadContentInfo() method.
527
     *
528
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
529
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
530
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
531
     */
532 View Code Duplication
    public function testLoadContentInfoThrowsNotFoundException()
533
    {
534
        $repository = $this->getRepository();
535
536
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
537
        /* BEGIN: Use Case */
538
        $contentService = $repository->getContentService();
539
540
        // This call will fail with a NotFoundException
541
        $contentService->loadContentInfo($nonExistentContentId);
542
        /* END: Use Case */
543
    }
544
545
    /**
546
     * Test for the loadContentInfoByRemoteId() method.
547
     *
548
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
549
     */
550
    public function testLoadContentInfoByRemoteId()
551
    {
552
        $repository = $this->getRepository();
553
554
        /* BEGIN: Use Case */
555
        $contentService = $repository->getContentService();
556
557
        // Load the ContentInfo for "Media" folder
558
        $contentInfo = $contentService->loadContentInfoByRemoteId('faaeb9be3bd98ed09f606fc16d144eca');
559
        /* END: Use Case */
560
561
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $contentInfo);
562
563
        return $contentInfo;
564
    }
565
566
    /**
567
     * Test for the returned value of the loadContentInfoByRemoteId() method.
568
     *
569
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
570
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId
571
     *
572
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
573
     */
574 View Code Duplication
    public function testLoadContentInfoByRemoteIdSetsExpectedContentInfo(ContentInfo $contentInfo)
575
    {
576
        $this->assertPropertiesCorrectUnsorted(
577
            [
578
                'id' => 10,
579
                'contentTypeId' => 4,
580
                'name' => 'Anonymous User',
581
                'sectionId' => 2,
582
                'currentVersionNo' => 2,
583
                'published' => true,
584
                'ownerId' => 14,
585
                'modificationDate' => $this->createDateTime(1072180405),
586
                'publishedDate' => $this->createDateTime(1033920665),
587
                'alwaysAvailable' => 1,
588
                'remoteId' => 'faaeb9be3bd98ed09f606fc16d144eca',
589
                'mainLanguageCode' => 'eng-US',
590
                'mainLocationId' => 45,
591
            ],
592
            $contentInfo
593
        );
594
    }
595
596
    /**
597
     * Test for the loadContentInfoByRemoteId() method.
598
     *
599
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
600
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
601
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
602
     */
603
    public function testLoadContentInfoByRemoteIdThrowsNotFoundException()
604
    {
605
        $repository = $this->getRepository();
606
607
        /* BEGIN: Use Case */
608
        $contentService = $repository->getContentService();
609
610
        // This call will fail with a NotFoundException
611
        $contentService->loadContentInfoByRemoteId('abcdefghijklmnopqrstuvwxyz0123456789');
612
        /* END: Use Case */
613
    }
614
615
    /**
616
     * Test for the loadVersionInfo() method.
617
     *
618
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
619
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
620
     * @group user
621
     */
622 View Code Duplication
    public function testLoadVersionInfo()
623
    {
624
        $repository = $this->getRepository();
625
626
        $mediaFolderId = $this->generateId('object', 41);
627
        /* BEGIN: Use Case */
628
        // $mediaFolderId contains the ID of the "Media" folder
629
630
        $contentService = $repository->getContentService();
631
632
        // Load the ContentInfo for "Media" folder
633
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
634
635
        // Now load the current version info of the "Media" folder
636
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
637
        /* END: Use Case */
638
639
        $this->assertInstanceOf(
640
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
641
            $versionInfo
642
        );
643
    }
644
645
    /**
646
     * Test for the loadVersionInfoById() method.
647
     *
648
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
649
     */
650 View Code Duplication
    public function testLoadVersionInfoById()
651
    {
652
        $repository = $this->getRepository();
653
654
        $mediaFolderId = $this->generateId('object', 41);
655
        /* BEGIN: Use Case */
656
        // $mediaFolderId contains the ID of the "Media" folder
657
658
        $contentService = $repository->getContentService();
659
660
        // Load the VersionInfo for "Media" folder
661
        $versionInfo = $contentService->loadVersionInfoById($mediaFolderId);
662
        /* END: Use Case */
663
664
        $this->assertInstanceOf(
665
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
666
            $versionInfo
667
        );
668
669
        return $versionInfo;
670
    }
671
672
    /**
673
     * Test for the returned value of the loadVersionInfoById() method.
674
     *
675
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
676
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
677
     *
678
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
679
     */
680
    public function testLoadVersionInfoByIdSetsExpectedVersionInfo(VersionInfo $versionInfo)
681
    {
682
        $this->assertPropertiesCorrect(
683
            [
684
                'names' => [
685
                    'eng-US' => 'Media',
686
                ],
687
                'contentInfo' => new ContentInfo($this->getExpectedMediaContentInfoProperties()),
688
                'id' => 472,
689
                'versionNo' => 1,
690
                'modificationDate' => $this->createDateTime(1060695457),
691
                'creatorId' => 14,
692
                'creationDate' => $this->createDateTime(1060695450),
693
                'status' => VersionInfo::STATUS_PUBLISHED,
694
                'initialLanguageCode' => 'eng-US',
695
                'languageCodes' => [
696
                    'eng-US',
697
                ],
698
            ],
699
            $versionInfo
700
        );
701
        $this->assertTrue($versionInfo->isPublished());
702
        $this->assertFalse($versionInfo->isDraft());
703
        $this->assertFalse($versionInfo->isArchived());
704
    }
705
706
    /**
707
     * Test for the loadVersionInfoById() method.
708
     *
709
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
710
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
711
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
712
     */
713 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundException()
714
    {
715
        $repository = $this->getRepository();
716
717
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
718
        /* BEGIN: Use Case */
719
        $contentService = $repository->getContentService();
720
721
        // This call will fail with a "NotFoundException"
722
        $contentService->loadVersionInfoById($nonExistentContentId);
723
        /* END: Use Case */
724
    }
725
726
    /**
727
     * Test for the loadContentByContentInfo() method.
728
     *
729
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
730
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
731
     */
732 View Code Duplication
    public function testLoadContentByContentInfo()
733
    {
734
        $repository = $this->getRepository();
735
736
        $mediaFolderId = $this->generateId('object', 41);
737
        /* BEGIN: Use Case */
738
        // $mediaFolderId contains the ID of the "Media" folder
739
740
        $contentService = $repository->getContentService();
741
742
        // Load the ContentInfo for "Media" folder
743
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
744
745
        // Now load the current content version for the info instance
746
        $content = $contentService->loadContentByContentInfo($contentInfo);
747
        /* END: Use Case */
748
749
        $this->assertInstanceOf(
750
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
751
            $content
752
        );
753
    }
754
755
    /**
756
     * Test for the loadContentByVersionInfo() method.
757
     *
758
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
759
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
760
     */
761
    public function testLoadContentByVersionInfo()
762
    {
763
        $repository = $this->getRepository();
764
765
        $mediaFolderId = $this->generateId('object', 41);
766
        /* BEGIN: Use Case */
767
        // $mediaFolderId contains the ID of the "Media" folder
768
769
        $contentService = $repository->getContentService();
770
771
        // Load the ContentInfo for "Media" folder
772
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
773
774
        // Load the current VersionInfo
775
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
776
777
        // Now load the current content version for the info instance
778
        $content = $contentService->loadContentByVersionInfo($versionInfo);
779
        /* END: Use Case */
780
781
        $this->assertInstanceOf(
782
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
783
            $content
784
        );
785
    }
786
787
    /**
788
     * Test for the loadContent() method.
789
     *
790
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
791
     * @group user
792
     * @group field-type
793
     */
794 View Code Duplication
    public function testLoadContent()
795
    {
796
        $repository = $this->getRepository();
797
798
        $mediaFolderId = $this->generateId('object', 41);
799
        /* BEGIN: Use Case */
800
        // $mediaFolderId contains the ID of the "Media" folder
801
802
        $contentService = $repository->getContentService();
803
804
        // Load the Content for "Media" folder, any language and current version
805
        $content = $contentService->loadContent($mediaFolderId);
806
        /* END: Use Case */
807
808
        $this->assertInstanceOf(
809
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
810
            $content
811
        );
812
    }
813
814
    /**
815
     * Test for the loadContent() method.
816
     *
817
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
818
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
819
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
820
     */
821 View Code Duplication
    public function testLoadContentThrowsNotFoundException()
822
    {
823
        $repository = $this->getRepository();
824
825
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
826
        /* BEGIN: Use Case */
827
        $contentService = $repository->getContentService();
828
829
        // This call will fail with a "NotFoundException"
830
        $contentService->loadContent($nonExistentContentId);
831
        /* END: Use Case */
832
    }
833
834
    /**
835
     * Data provider for testLoadContentByRemoteId().
836
     *
837
     * @return array
838
     */
839
    public function contentRemoteIdVersionLanguageProvider()
840
    {
841
        return [
842
            ['f5c88a2209584891056f987fd965b0ba', null, null],
843
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], null],
844
            ['f5c88a2209584891056f987fd965b0ba', null, 1],
845
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], 1],
846
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, null],
847
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], null],
848
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, 1],
849
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], 1],
850
        ];
851
    }
852
853
    /**
854
     * Test for the loadContentByRemoteId() method.
855
     *
856
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId
857
     * @dataProvider contentRemoteIdVersionLanguageProvider
858
     *
859
     * @param string $remoteId
860
     * @param array|null $languages
861
     * @param int $versionNo
862
     */
863
    public function testLoadContentByRemoteId($remoteId, $languages, $versionNo)
864
    {
865
        $repository = $this->getRepository();
866
867
        $contentService = $repository->getContentService();
868
869
        $content = $contentService->loadContentByRemoteId($remoteId, $languages, $versionNo);
870
871
        $this->assertInstanceOf(
872
            Content::class,
873
            $content
874
        );
875
876
        $this->assertEquals($remoteId, $content->contentInfo->remoteId);
877
        if ($languages !== null) {
878
            $this->assertEquals($languages, $content->getVersionInfo()->languageCodes);
879
        }
880
        $this->assertEquals($versionNo ?: 1, $content->getVersionInfo()->versionNo);
881
    }
882
883
    /**
884
     * Test for the loadContentByRemoteId() method.
885
     *
886
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
887
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
888
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
889
     */
890
    public function testLoadContentByRemoteIdThrowsNotFoundException()
891
    {
892
        $repository = $this->getRepository();
893
894
        /* BEGIN: Use Case */
895
        $contentService = $repository->getContentService();
896
897
        // This call will fail with a "NotFoundException", because no content
898
        // object exists for the given remoteId
899
        $contentService->loadContentByRemoteId('a1b1c1d1e1f1a2b2c2d2e2f2a3b3c3d3');
900
        /* END: Use Case */
901
    }
902
903
    /**
904
     * Test for the publishVersion() method.
905
     *
906
     * @return \eZ\Publish\API\Repository\Values\Content\Content
907
     *
908
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
909
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
910
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
911
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
912
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
913
     * @group user
914
     * @group field-type
915
     */
916
    public function testPublishVersion()
917
    {
918
        $time = time();
919
        /* BEGIN: Use Case */
920
        $content = $this->createContentVersion1();
921
        /* END: Use Case */
922
923
        $this->assertInstanceOf(Content::class, $content);
924
        $this->assertTrue($content->contentInfo->published);
925
        $this->assertEquals(VersionInfo::STATUS_PUBLISHED, $content->versionInfo->status);
926
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->publishedDate->getTimestamp());
927
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->modificationDate->getTimestamp());
928
        $this->assertTrue($content->versionInfo->isPublished());
929
        $this->assertFalse($content->versionInfo->isDraft());
930
        $this->assertFalse($content->versionInfo->isArchived());
931
932
        return $content;
933
    }
934
935
    /**
936
     * Test for the publishVersion() method.
937
     *
938
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
939
     *
940
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
941
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
942
     */
943
    public function testPublishVersionSetsExpectedContentInfo($content)
944
    {
945
        $this->assertEquals(
946
            array(
947
                $content->id,
948
                true,
949
                1,
950
                'abcdef0123456789abcdef0123456789',
951
                'eng-US',
952
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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

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

Loading history...
953
                true,
954
            ),
955
            array(
956
                $content->contentInfo->id,
957
                $content->contentInfo->alwaysAvailable,
958
                $content->contentInfo->currentVersionNo,
959
                $content->contentInfo->remoteId,
960
                $content->contentInfo->mainLanguageCode,
961
                $content->contentInfo->ownerId,
962
                $content->contentInfo->published,
963
            )
964
        );
965
966
        $this->assertNotNull($content->contentInfo->mainLocationId);
967
        $date = new \DateTime('1984/01/01');
968
        $this->assertGreaterThan(
969
            $date->getTimestamp(),
970
            $content->contentInfo->publishedDate->getTimestamp()
971
        );
972
    }
973
974
    /**
975
     * Test for the publishVersion() method.
976
     *
977
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
978
     *
979
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
980
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
981
     */
982
    public function testPublishVersionSetsExpectedVersionInfo($content)
983
    {
984
        $this->assertEquals(
985
            array(
986
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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

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

Loading history...
987
                'eng-US',
988
                VersionInfo::STATUS_PUBLISHED,
989
                1,
990
            ),
991
            array(
992
                $content->getVersionInfo()->creatorId,
993
                $content->getVersionInfo()->initialLanguageCode,
994
                $content->getVersionInfo()->status,
995
                $content->getVersionInfo()->versionNo,
996
            )
997
        );
998
999
        $date = new \DateTime('1984/01/01');
1000
        $this->assertGreaterThan(
1001
            $date->getTimestamp(),
1002
            $content->getVersionInfo()->modificationDate->getTimestamp()
1003
        );
1004
1005
        $this->assertNotNull($content->getVersionInfo()->modificationDate);
1006
        $this->assertTrue($content->getVersionInfo()->isPublished());
1007
        $this->assertFalse($content->getVersionInfo()->isDraft());
1008
        $this->assertFalse($content->getVersionInfo()->isArchived());
1009
    }
1010
1011
    /**
1012
     * Test for the publishVersion() method.
1013
     *
1014
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1015
     *
1016
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1017
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
1018
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1019
     */
1020
    public function testPublishVersionCreatesLocationsDefinedOnCreate()
1021
    {
1022
        $repository = $this->getRepository();
1023
1024
        /* BEGIN: Use Case */
1025
        $content = $this->createContentVersion1();
1026
        /* END: Use Case */
1027
1028
        $locationService = $repository->getLocationService();
1029
        $location = $locationService->loadLocationByRemoteId(
1030
            '0123456789abcdef0123456789abcdef'
1031
        );
1032
1033
        $this->assertEquals(
1034
            $location->getContentInfo(),
1035
            $content->getVersionInfo()->getContentInfo()
1036
        );
1037
1038
        return array($content, $location);
1039
    }
1040
1041
    /**
1042
     * Test for the publishVersion() method.
1043
     *
1044
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1045
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionCreatesLocationsDefinedOnCreate
1046
     */
1047
    public function testCreateContentWithLocationCreateParameterCreatesExpectedLocation(array $testData)
1048
    {
1049
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
1050
        /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
1051
        list($content, $location) = $testData;
1052
1053
        $parentLocationId = $this->generateId('location', 56);
1054
        $parentLocation = $this->getRepository()->getLocationService()->loadLocation($parentLocationId);
1055
        $mainLocationId = $content->getVersionInfo()->getContentInfo()->mainLocationId;
1056
1057
        $this->assertPropertiesCorrect(
1058
            array(
1059
                'id' => $mainLocationId,
1060
                'priority' => 23,
1061
                'hidden' => true,
1062
                'invisible' => true,
1063
                'remoteId' => '0123456789abcdef0123456789abcdef',
1064
                'parentLocationId' => $parentLocationId,
1065
                'pathString' => $parentLocation->pathString . $mainLocationId . '/',
1066
                'depth' => $parentLocation->depth + 1,
1067
                'sortField' => Location::SORT_FIELD_NODE_ID,
1068
                'sortOrder' => Location::SORT_ORDER_DESC,
1069
            ),
1070
            $location
1071
        );
1072
    }
1073
1074
    /**
1075
     * Test for the publishVersion() method.
1076
     *
1077
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1078
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1079
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1080
     */
1081 View Code Duplication
    public function testPublishVersionThrowsBadStateException()
1082
    {
1083
        $repository = $this->getRepository();
1084
1085
        $contentService = $repository->getContentService();
1086
1087
        /* BEGIN: Use Case */
1088
        $draft = $this->createContentDraftVersion1();
1089
1090
        // Publish the content draft
1091
        $contentService->publishVersion($draft->getVersionInfo());
1092
1093
        // This call will fail with a "BadStateException", because the version
1094
        // is already published.
1095
        $contentService->publishVersion($draft->getVersionInfo());
1096
        /* END: Use Case */
1097
    }
1098
1099
    /**
1100
     * Test that publishVersion() does not affect publishedDate (assuming previous version exists).
1101
     *
1102
     * @covers \eZ\Publish\API\Repository\ContentService::publishVersion
1103
     */
1104
    public function testPublishVersionDoesNotChangePublishedDate()
1105
    {
1106
        $repository = $this->getRepository();
1107
1108
        $contentService = $repository->getContentService();
1109
1110
        $publishedContent = $this->createContentVersion1();
1111
1112
        // force timestamps to differ
1113
        sleep(1);
1114
1115
        $contentDraft = $contentService->createContentDraft($publishedContent->contentInfo);
1116
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1117
        $contentUpdateStruct->setField('name', 'New name');
1118
        $contentDraft = $contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
1119
        $republishedContent = $contentService->publishVersion($contentDraft->versionInfo);
1120
1121
        $this->assertEquals(
1122
            $publishedContent->contentInfo->publishedDate->getTimestamp(),
1123
            $republishedContent->contentInfo->publishedDate->getTimestamp()
1124
        );
1125
        $this->assertGreaterThan(
1126
            $publishedContent->contentInfo->modificationDate->getTimestamp(),
1127
            $republishedContent->contentInfo->modificationDate->getTimestamp()
1128
        );
1129
    }
1130
1131
    /**
1132
     * Test for the createContentDraft() method.
1133
     *
1134
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1135
     *
1136
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1137
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1138
     * @group user
1139
     */
1140
    public function testCreateContentDraft()
1141
    {
1142
        $repository = $this->getRepository();
1143
1144
        $contentService = $repository->getContentService();
1145
1146
        /* BEGIN: Use Case */
1147
        $content = $this->createContentVersion1();
1148
1149
        // Now we create a new draft from the published content
1150
        $draftedContent = $contentService->createContentDraft($content->contentInfo);
1151
        /* END: Use Case */
1152
1153
        $this->assertInstanceOf(
1154
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1155
            $draftedContent
1156
        );
1157
1158
        return $draftedContent;
1159
    }
1160
1161
    /**
1162
     * Test for the createContentDraft() method.
1163
     *
1164
     * Test that editor has access to edit own draft.
1165
     * Note: Editors have access to version_read, which is needed to load content drafts.
1166
     *
1167
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1168
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1169
     * @group user
1170
     */
1171 View Code Duplication
    public function testCreateContentDraftAndLoadAccess()
1172
    {
1173
        $repository = $this->getRepository();
1174
1175
        /* BEGIN: Use Case */
1176
        $user = $this->createUserVersion1();
1177
1178
        // Set new editor as user
1179
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
1180
1181
        // Create draft
1182
        $draft = $this->createContentDraftVersion1(2, 'folder');
1183
1184
        // Try to load the draft
1185
        $contentService = $repository->getContentService();
1186
        $loadedDraft = $contentService->loadContent($draft->id);
1187
1188
        /* END: Use Case */
1189
1190
        $this->assertEquals($draft->id, $loadedDraft->id);
1191
    }
1192
1193
    /**
1194
     * Test for the createContentDraft() method.
1195
     *
1196
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1197
     *
1198
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1199
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1200
     */
1201
    public function testCreateContentDraftSetsExpectedProperties($draft)
1202
    {
1203
        $this->assertEquals(
1204
            array(
1205
                'fieldCount' => 2,
1206
                'relationCount' => 0,
1207
            ),
1208
            array(
1209
                'fieldCount' => count($draft->getFields()),
1210
                'relationCount' => count($this->getRepository()->getContentService()->loadRelations($draft->getVersionInfo())),
1211
            )
1212
        );
1213
    }
1214
1215
    /**
1216
     * Test for the createContentDraft() method.
1217
     *
1218
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1219
     *
1220
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1221
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1222
     */
1223
    public function testCreateContentDraftSetsContentInfo($draft)
1224
    {
1225
        $contentInfo = $draft->contentInfo;
1226
1227
        $this->assertEquals(
1228
            array(
1229
                $draft->id,
1230
                true,
1231
                1,
1232
                'eng-US',
1233
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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

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

Loading history...
1234
                'abcdef0123456789abcdef0123456789',
1235
                1,
1236
            ),
1237
            array(
1238
                $contentInfo->id,
1239
                $contentInfo->alwaysAvailable,
1240
                $contentInfo->currentVersionNo,
1241
                $contentInfo->mainLanguageCode,
1242
                $contentInfo->ownerId,
1243
                $contentInfo->remoteId,
1244
                $contentInfo->sectionId,
1245
            )
1246
        );
1247
    }
1248
1249
    /**
1250
     * Test for the createContentDraft() method.
1251
     *
1252
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1253
     *
1254
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1255
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1256
     */
1257
    public function testCreateContentDraftSetsVersionInfo($draft)
1258
    {
1259
        $versionInfo = $draft->getVersionInfo();
1260
1261
        $this->assertEquals(
1262
            array(
1263
                'creatorId' => $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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

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

Loading history...
1264
                'initialLanguageCode' => 'eng-US',
1265
                'languageCodes' => array(0 => 'eng-US'),
1266
                'status' => VersionInfo::STATUS_DRAFT,
1267
                'versionNo' => 2,
1268
            ),
1269
            array(
1270
                'creatorId' => $versionInfo->creatorId,
1271
                'initialLanguageCode' => $versionInfo->initialLanguageCode,
1272
                'languageCodes' => $versionInfo->languageCodes,
1273
                'status' => $versionInfo->status,
1274
                'versionNo' => $versionInfo->versionNo,
1275
            )
1276
        );
1277
        $this->assertTrue($versionInfo->isDraft());
1278
        $this->assertFalse($versionInfo->isPublished());
1279
        $this->assertFalse($versionInfo->isArchived());
1280
    }
1281
1282
    /**
1283
     * Test for the createContentDraft() method.
1284
     *
1285
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1286
     *
1287
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1288
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1289
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
1290
     */
1291 View Code Duplication
    public function testCreateContentDraftLoadVersionInfoStillLoadsPublishedVersion($draft)
0 ignored issues
show
Unused Code introduced by
The parameter $draft is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1292
    {
1293
        $repository = $this->getRepository();
1294
1295
        $contentService = $repository->getContentService();
1296
1297
        /* BEGIN: Use Case */
1298
        $content = $this->createContentVersion1();
1299
1300
        // Now we create a new draft from the published content
1301
        $contentService->createContentDraft($content->contentInfo);
1302
1303
        // This call will still load the published version
1304
        $versionInfoPublished = $contentService->loadVersionInfo($content->contentInfo);
1305
        /* END: Use Case */
1306
1307
        $this->assertEquals(1, $versionInfoPublished->versionNo);
1308
    }
1309
1310
    /**
1311
     * Test for the createContentDraft() method.
1312
     *
1313
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1314
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
1315
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1316
     */
1317 View Code Duplication
    public function testCreateContentDraftLoadContentStillLoadsPublishedVersion()
1318
    {
1319
        $repository = $this->getRepository();
1320
1321
        $contentService = $repository->getContentService();
1322
1323
        /* BEGIN: Use Case */
1324
        $content = $this->createContentVersion1();
1325
1326
        // Now we create a new draft from the published content
1327
        $contentService->createContentDraft($content->contentInfo);
1328
1329
        // This call will still load the published content version
1330
        $contentPublished = $contentService->loadContent($content->id);
1331
        /* END: Use Case */
1332
1333
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1334
    }
1335
1336
    /**
1337
     * Test for the createContentDraft() method.
1338
     *
1339
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1340
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
1341
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1342
     */
1343 View Code Duplication
    public function testCreateContentDraftLoadContentByRemoteIdStillLoadsPublishedVersion()
1344
    {
1345
        $repository = $this->getRepository();
1346
1347
        $contentService = $repository->getContentService();
1348
1349
        /* BEGIN: Use Case */
1350
        $content = $this->createContentVersion1();
1351
1352
        // Now we create a new draft from the published content
1353
        $contentService->createContentDraft($content->contentInfo);
1354
1355
        // This call will still load the published content version
1356
        $contentPublished = $contentService->loadContentByRemoteId('abcdef0123456789abcdef0123456789');
1357
        /* END: Use Case */
1358
1359
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1360
    }
1361
1362
    /**
1363
     * Test for the createContentDraft() method.
1364
     *
1365
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1366
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
1367
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1368
     */
1369 View Code Duplication
    public function testCreateContentDraftLoadContentByContentInfoStillLoadsPublishedVersion()
1370
    {
1371
        $repository = $this->getRepository();
1372
1373
        $contentService = $repository->getContentService();
1374
1375
        /* BEGIN: Use Case */
1376
        $content = $this->createContentVersion1();
1377
1378
        // Now we create a new draft from the published content
1379
        $contentService->createContentDraft($content->contentInfo);
1380
1381
        // This call will still load the published content version
1382
        $contentPublished = $contentService->loadContentByContentInfo($content->contentInfo);
1383
        /* END: Use Case */
1384
1385
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1386
    }
1387
1388
    /**
1389
     * Test for the newContentUpdateStruct() method.
1390
     *
1391
     * @covers \eZ\Publish\API\Repository\ContentService::newContentUpdateStruct
1392
     * @group user
1393
     */
1394
    public function testNewContentUpdateStruct()
1395
    {
1396
        $repository = $this->getRepository();
1397
1398
        /* BEGIN: Use Case */
1399
        $contentService = $repository->getContentService();
1400
1401
        $updateStruct = $contentService->newContentUpdateStruct();
1402
        /* END: Use Case */
1403
1404
        $this->assertInstanceOf(
1405
            ContentUpdateStruct::class,
1406
            $updateStruct
1407
        );
1408
1409
        $this->assertPropertiesCorrect(
1410
            [
1411
                'initialLanguageCode' => null,
1412
                'fields' => [],
1413
            ],
1414
            $updateStruct
1415
        );
1416
    }
1417
1418
    /**
1419
     * Test for the updateContent() method.
1420
     *
1421
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1422
     *
1423
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1424
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1425
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1426
     * @group user
1427
     * @group field-type
1428
     */
1429
    public function testUpdateContent()
1430
    {
1431
        /* BEGIN: Use Case */
1432
        $draftVersion2 = $this->createUpdatedDraftVersion2();
1433
        /* END: Use Case */
1434
1435
        $this->assertInstanceOf(
1436
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1437
            $draftVersion2
1438
        );
1439
1440
        $this->assertEquals(
1441
            $this->generateId('user', 10),
1442
            $draftVersion2->versionInfo->creatorId,
1443
            'creatorId is not properly set on new Version'
1444
        );
1445
1446
        return $draftVersion2;
1447
    }
1448
1449
    /**
1450
     * Test for the updateContent_WithDifferentUser() method.
1451
     *
1452
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1453
     *
1454
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1455
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1456
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1457
     * @group user
1458
     * @group field-type
1459
     */
1460
    public function testUpdateContentWithDifferentUser()
1461
    {
1462
        /* BEGIN: Use Case */
1463
        $arrayWithDraftVersion2 = $this->createUpdatedDraftVersion2NotAdmin();
1464
        /* END: Use Case */
1465
1466
        $this->assertInstanceOf(
1467
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1468
            $arrayWithDraftVersion2[0]
1469
        );
1470
1471
        $this->assertEquals(
1472
            $this->generateId('user', $arrayWithDraftVersion2[1]),
1473
            $arrayWithDraftVersion2[0]->versionInfo->creatorId,
1474
            'creatorId is not properly set on new Version'
1475
        );
1476
1477
        return $arrayWithDraftVersion2[0];
1478
    }
1479
1480
    /**
1481
     * Test for the updateContent() method.
1482
     *
1483
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1484
     *
1485
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1486
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1487
     */
1488
    public function testUpdateContentSetsExpectedFields($content)
1489
    {
1490
        $actual = $this->normalizeFields($content->getFields());
1491
1492
        $expected = array(
1493
            new Field(
1494
                array(
1495
                    'id' => 0,
1496
                    'value' => true,
1497
                    'languageCode' => 'eng-GB',
1498
                    'fieldDefIdentifier' => 'description',
1499
                )
1500
            ),
1501
            new Field(
1502
                array(
1503
                    'id' => 0,
1504
                    'value' => true,
1505
                    'languageCode' => 'eng-US',
1506
                    'fieldDefIdentifier' => 'description',
1507
                )
1508
            ),
1509
            new Field(
1510
                array(
1511
                    'id' => 0,
1512
                    'value' => true,
1513
                    'languageCode' => 'eng-GB',
1514
                    'fieldDefIdentifier' => 'name',
1515
                )
1516
            ),
1517
            new Field(
1518
                array(
1519
                    'id' => 0,
1520
                    'value' => true,
1521
                    'languageCode' => 'eng-US',
1522
                    'fieldDefIdentifier' => 'name',
1523
                )
1524
            ),
1525
        );
1526
1527
        $this->assertEquals($expected, $actual);
1528
    }
1529
1530
    /**
1531
     * Test for the updateContent() method.
1532
     *
1533
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1534
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1535
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1536
     */
1537 View Code Duplication
    public function testUpdateContentThrowsBadStateException()
1538
    {
1539
        $repository = $this->getRepository();
1540
1541
        $contentService = $repository->getContentService();
1542
1543
        /* BEGIN: Use Case */
1544
        $content = $this->createContentVersion1();
1545
1546
        // Now create an update struct and modify some fields
1547
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1548
        $contentUpdateStruct->setField('title', 'An awesome² story about ezp.');
1549
        $contentUpdateStruct->setField('title', 'An awesome²³ story about ezp.', 'eng-GB');
1550
1551
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1552
1553
        // This call will fail with a "BadStateException", because $publishedContent
1554
        // is not a draft.
1555
        $contentService->updateContent(
1556
            $content->getVersionInfo(),
1557
            $contentUpdateStruct
1558
        );
1559
        /* END: Use Case */
1560
    }
1561
1562
    /**
1563
     * Test for the updateContent() method.
1564
     *
1565
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1566
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1567
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1568
     */
1569 View Code Duplication
    public function testUpdateContentThrowsInvalidArgumentExceptionWhenFieldTypeDoesNotAccept()
1570
    {
1571
        $repository = $this->getRepository();
1572
1573
        $contentService = $repository->getContentService();
1574
1575
        /* BEGIN: Use Case */
1576
        $draft = $this->createContentDraftVersion1();
1577
1578
        // Now create an update struct and modify some fields
1579
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1580
        // The name field does not accept a stdClass object as its input
1581
        $contentUpdateStruct->setField('name', new \stdClass(), 'eng-US');
1582
1583
        // Throws an InvalidArgumentException, since the value for field "name"
1584
        // is not accepted
1585
        $contentService->updateContent(
1586
            $draft->getVersionInfo(),
1587
            $contentUpdateStruct
1588
        );
1589
        /* END: Use Case */
1590
    }
1591
1592
    /**
1593
     * Test for the updateContent() method.
1594
     *
1595
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1596
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1597
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1598
     */
1599 View Code Duplication
    public function testUpdateContentWhenMandatoryFieldIsEmpty()
1600
    {
1601
        $repository = $this->getRepository();
1602
1603
        $contentService = $repository->getContentService();
1604
1605
        /* BEGIN: Use Case */
1606
        $draft = $this->createContentDraftVersion1();
1607
1608
        // Now create an update struct and set a mandatory field to null
1609
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1610
        $contentUpdateStruct->setField('name', null);
1611
1612
        // Don't set this, then the above call without languageCode will fail
1613
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1614
1615
        // This call will fail with a "ContentFieldValidationException", because the
1616
        // mandatory "name" field is empty.
1617
        $contentService->updateContent(
1618
            $draft->getVersionInfo(),
1619
            $contentUpdateStruct
1620
        );
1621
        /* END: Use Case */
1622
    }
1623
1624
    /**
1625
     * Test for the updateContent() method.
1626
     *
1627
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1628
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1629
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1630
     */
1631
    public function testUpdateContentThrowsContentFieldValidationException()
1632
    {
1633
        $repository = $this->getRepository();
1634
1635
        /* BEGIN: Use Case */
1636
        $contentTypeService = $repository->getContentTypeService();
1637
        $contentService = $repository->getContentService();
1638
1639
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1640
1641
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1642
        $contentCreate->setField('name', 'An awesome Sidelfingen folder');
1643
1644
        $draft = $contentService->createContent($contentCreate);
1645
1646
        $contentUpdate = $contentService->newContentUpdateStruct();
1647
        // Violates string length constraint
1648
        $contentUpdate->setField('short_name', str_repeat('a', 200), 'eng-US');
1649
1650
        // Throws ContentFieldValidationException because the string length
1651
        // validation of the field "short_name" fails
1652
        $contentService->updateContent($draft->getVersionInfo(), $contentUpdate);
1653
        /* END: Use Case */
1654
    }
1655
1656
    /**
1657
     * Test for the updateContent() method.
1658
     *
1659
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1660
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1661
     */
1662
    public function testUpdateContentWithNotUpdatingMandatoryField()
1663
    {
1664
        $repository = $this->getRepository();
1665
1666
        $contentService = $repository->getContentService();
1667
1668
        /* BEGIN: Use Case */
1669
        $draft = $this->createContentDraftVersion1();
1670
1671
        // Now create an update struct which does not overwrite mandatory
1672
        // fields
1673
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1674
        $contentUpdateStruct->setField(
1675
            'description',
1676
            '<?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"/>'
1677
        );
1678
1679
        // Don't set this, then the above call without languageCode will fail
1680
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1681
1682
        // This will only update the "description" field in the "eng-US"
1683
        // language
1684
        $updatedDraft = $contentService->updateContent(
1685
            $draft->getVersionInfo(),
1686
            $contentUpdateStruct
1687
        );
1688
        /* END: Use Case */
1689
1690
        foreach ($updatedDraft->getFields() as $field) {
1691
            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...
1692
                // Found field
1693
                return;
1694
            }
1695
        }
1696
        $this->fail(
1697
            'Field with identifier "name" in language "eng-US" could not be found or has empty value.'
1698
        );
1699
    }
1700
1701
    /**
1702
     * Test for the createContentDraft() method.
1703
     *
1704
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
1705
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1706
     */
1707 View Code Duplication
    public function testCreateContentDraftWithSecondParameter()
1708
    {
1709
        $repository = $this->getRepository();
1710
1711
        $contentService = $repository->getContentService();
1712
1713
        /* BEGIN: Use Case */
1714
        $contentVersion2 = $this->createContentVersion2();
1715
1716
        // Now we create a new draft from the initial version
1717
        $draftedContentReloaded = $contentService->createContentDraft(
1718
            $contentVersion2->contentInfo,
1719
            $contentVersion2->getVersionInfo()
1720
        );
1721
        /* END: Use Case */
1722
1723
        $this->assertEquals(3, $draftedContentReloaded->getVersionInfo()->versionNo);
1724
    }
1725
1726
    /**
1727
     * Test for the createContentDraft() method with third parameter.
1728
     *
1729
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1730
     */
1731 View Code Duplication
    public function testCreateContentDraftWithThirdParameter()
1732
    {
1733
        $repository = $this->getRepository();
1734
1735
        $contentService = $repository->getContentService();
1736
1737
        $content = $contentService->loadContent(4);
1738
        $user = $this->createUserVersion1();
1739
1740
        $draftContent = $contentService->createContentDraft(
1741
            $content->contentInfo,
1742
            $content->getVersionInfo(),
1743
            $user
1744
        );
1745
1746
        $this->assertInstanceOf(
1747
            Content::class,
1748
            $draftContent
1749
        );
1750
    }
1751
1752
    /**
1753
     * Test for the publishVersion() method.
1754
     *
1755
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1756
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1757
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1758
     */
1759 View Code Duplication
    public function testPublishVersionFromContentDraft()
1760
    {
1761
        $repository = $this->getRepository();
1762
1763
        $contentService = $repository->getContentService();
1764
1765
        /* BEGIN: Use Case */
1766
        $contentVersion2 = $this->createContentVersion2();
1767
        /* END: Use Case */
1768
1769
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo);
1770
1771
        $this->assertEquals(
1772
            array(
1773
                'status' => VersionInfo::STATUS_PUBLISHED,
1774
                'versionNo' => 2,
1775
            ),
1776
            array(
1777
                'status' => $versionInfo->status,
1778
                'versionNo' => $versionInfo->versionNo,
1779
            )
1780
        );
1781
        $this->assertTrue($versionInfo->isPublished());
1782
        $this->assertFalse($versionInfo->isDraft());
1783
        $this->assertFalse($versionInfo->isArchived());
1784
    }
1785
1786
    /**
1787
     * Test for the publishVersion() method.
1788
     *
1789
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1790
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1791
     */
1792 View Code Duplication
    public function testPublishVersionFromContentDraftArchivesOldVersion()
1793
    {
1794
        $repository = $this->getRepository();
1795
1796
        $contentService = $repository->getContentService();
1797
1798
        /* BEGIN: Use Case */
1799
        $contentVersion2 = $this->createContentVersion2();
1800
        /* END: Use Case */
1801
1802
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo, 1);
1803
1804
        $this->assertEquals(
1805
            array(
1806
                'status' => VersionInfo::STATUS_ARCHIVED,
1807
                'versionNo' => 1,
1808
            ),
1809
            array(
1810
                'status' => $versionInfo->status,
1811
                'versionNo' => $versionInfo->versionNo,
1812
            )
1813
        );
1814
        $this->assertTrue($versionInfo->isArchived());
1815
        $this->assertFalse($versionInfo->isDraft());
1816
        $this->assertFalse($versionInfo->isPublished());
1817
    }
1818
1819
    /**
1820
     * Test for the publishVersion() method.
1821
     *
1822
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1823
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1824
     */
1825
    public function testPublishVersionFromContentDraftUpdatesContentInfoCurrentVersion()
1826
    {
1827
        /* BEGIN: Use Case */
1828
        $contentVersion2 = $this->createContentVersion2();
1829
        /* END: Use Case */
1830
1831
        $this->assertEquals(2, $contentVersion2->contentInfo->currentVersionNo);
1832
    }
1833
1834
    /**
1835
     * Test for the publishVersion() method.
1836
     *
1837
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1838
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1839
     */
1840
    public function testPublishVersionFromOldContentDraftArchivesNewerVersionNo()
1841
    {
1842
        $repository = $this->getRepository();
1843
1844
        $contentService = $repository->getContentService();
1845
1846
        /* BEGIN: Use Case */
1847
        $content = $this->createContentVersion1();
1848
1849
        // Create a new draft with versionNo = 2
1850
        $draftedContentVersion2 = $contentService->createContentDraft($content->contentInfo);
1851
1852
        // Create another new draft with versionNo = 3
1853
        $draftedContentVersion3 = $contentService->createContentDraft($content->contentInfo);
1854
1855
        // Publish draft with versionNo = 3
1856
        $contentService->publishVersion($draftedContentVersion3->getVersionInfo());
1857
1858
        // Publish the first draft with versionNo = 2
1859
        // currentVersionNo is now 2, versionNo 3 will be archived
1860
        $publishedDraft = $contentService->publishVersion($draftedContentVersion2->getVersionInfo());
1861
        /* END: Use Case */
1862
1863
        $this->assertEquals(2, $publishedDraft->contentInfo->currentVersionNo);
1864
    }
1865
1866
    /**
1867
     * Test for the publishVersion() method, and that it creates limited archives.
1868
     *
1869
     * @todo Adapt this when per content type archive limited is added on repository Content Type model.
1870
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1871
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1872
     */
1873
    public function testPublishVersionNotCreatingUnlimitedArchives()
1874
    {
1875
        $repository = $this->getRepository();
1876
1877
        $contentService = $repository->getContentService();
1878
1879
        $content = $this->createContentVersion1();
1880
1881
        // Create a new draft with versionNo = 2
1882
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1883
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1884
1885
        // Create a new draft with versionNo = 3
1886
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1887
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1888
1889
        // Create a new draft with versionNo = 4
1890
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1891
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1892
1893
        // Create a new draft with versionNo = 5
1894
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1895
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1896
1897
        // Create a new draft with versionNo = 6
1898
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1899
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1900
1901
        // Create a new draft with versionNo = 7
1902
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1903
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1904
1905
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
1906
1907
        $this->assertEquals(6, count($versionInfoList));
1908
        $this->assertEquals(2, $versionInfoList[0]->versionNo);
1909
        $this->assertEquals(7, $versionInfoList[5]->versionNo);
1910
1911
        $this->assertEquals(
1912
            [
1913
                VersionInfo::STATUS_ARCHIVED,
1914
                VersionInfo::STATUS_ARCHIVED,
1915
                VersionInfo::STATUS_ARCHIVED,
1916
                VersionInfo::STATUS_ARCHIVED,
1917
                VersionInfo::STATUS_ARCHIVED,
1918
                VersionInfo::STATUS_PUBLISHED,
1919
            ],
1920
            [
1921
                $versionInfoList[0]->status,
1922
                $versionInfoList[1]->status,
1923
                $versionInfoList[2]->status,
1924
                $versionInfoList[3]->status,
1925
                $versionInfoList[4]->status,
1926
                $versionInfoList[5]->status,
1927
            ]
1928
        );
1929
    }
1930
1931
    /**
1932
     * Test for the newContentMetadataUpdateStruct() method.
1933
     *
1934
     * @covers \eZ\Publish\API\Repository\ContentService::newContentMetadataUpdateStruct
1935
     * @group user
1936
     */
1937
    public function testNewContentMetadataUpdateStruct()
1938
    {
1939
        $repository = $this->getRepository();
1940
1941
        /* BEGIN: Use Case */
1942
        $contentService = $repository->getContentService();
1943
1944
        // Creates a new metadata update struct
1945
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1946
1947
        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...
1948
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
1949
        }
1950
1951
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1952
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1953
        $metadataUpdate->alwaysAvailable = false;
1954
        /* END: Use Case */
1955
1956
        $this->assertInstanceOf(
1957
            ContentMetadataUpdateStruct::class,
1958
            $metadataUpdate
1959
        );
1960
    }
1961
1962
    /**
1963
     * Test for the updateContentMetadata() method.
1964
     *
1965
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1966
     *
1967
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1968
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1969
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentMetadataUpdateStruct
1970
     * @group user
1971
     */
1972
    public function testUpdateContentMetadata()
1973
    {
1974
        $repository = $this->getRepository();
1975
1976
        $contentService = $repository->getContentService();
1977
1978
        /* BEGIN: Use Case */
1979
        $content = $this->createContentVersion1();
1980
1981
        // Creates a metadata update struct
1982
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1983
1984
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1985
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1986
        $metadataUpdate->alwaysAvailable = false;
1987
        $metadataUpdate->publishedDate = $this->createDateTime(441759600); // 1984/01/01
1988
        $metadataUpdate->modificationDate = $this->createDateTime(441759600); // 1984/01/01
1989
1990
        // Update the metadata of the published content object
1991
        $content = $contentService->updateContentMetadata(
1992
            $content->contentInfo,
1993
            $metadataUpdate
1994
        );
1995
        /* END: Use Case */
1996
1997
        $this->assertInstanceOf(
1998
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1999
            $content
2000
        );
2001
2002
        return $content;
2003
    }
2004
2005
    /**
2006
     * Test for the updateContentMetadata() method.
2007
     *
2008
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2009
     *
2010
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2011
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2012
     */
2013
    public function testUpdateContentMetadataSetsExpectedProperties($content)
2014
    {
2015
        $contentInfo = $content->contentInfo;
2016
2017
        $this->assertEquals(
2018
            array(
2019
                'remoteId' => 'aaaabbbbccccddddeeeeffff11112222',
2020
                'sectionId' => $this->generateId('section', 1),
2021
                'alwaysAvailable' => false,
2022
                'currentVersionNo' => 1,
2023
                'mainLanguageCode' => 'eng-GB',
2024
                'modificationDate' => $this->createDateTime(441759600),
2025
                '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...
2026
                'published' => true,
2027
                'publishedDate' => $this->createDateTime(441759600),
2028
            ),
2029
            array(
2030
                'remoteId' => $contentInfo->remoteId,
2031
                'sectionId' => $contentInfo->sectionId,
2032
                'alwaysAvailable' => $contentInfo->alwaysAvailable,
2033
                'currentVersionNo' => $contentInfo->currentVersionNo,
2034
                'mainLanguageCode' => $contentInfo->mainLanguageCode,
2035
                'modificationDate' => $contentInfo->modificationDate,
2036
                'ownerId' => $contentInfo->ownerId,
2037
                'published' => $contentInfo->published,
2038
                'publishedDate' => $contentInfo->publishedDate,
2039
            )
2040
        );
2041
    }
2042
2043
    /**
2044
     * Test for the updateContentMetadata() method.
2045
     *
2046
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
2047
     *
2048
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2049
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2050
     */
2051
    public function testUpdateContentMetadataNotUpdatesContentVersion($content)
2052
    {
2053
        $this->assertEquals(1, $content->getVersionInfo()->versionNo);
2054
    }
2055
2056
    /**
2057
     * Test for the updateContentMetadata() method.
2058
     *
2059
     * @covers \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
2060
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2061
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
2062
     */
2063
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnDuplicateRemoteId()
2064
    {
2065
        $repository = $this->getRepository();
2066
2067
        $contentService = $repository->getContentService();
2068
2069
        /* BEGIN: Use Case */
2070
        // RemoteId of the "Media" page of an eZ Publish demo installation
2071
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2072
2073
        $content = $this->createContentVersion1();
2074
2075
        // Creates a metadata update struct
2076
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
2077
        $metadataUpdate->remoteId = $mediaRemoteId;
2078
2079
        // This call will fail with an "InvalidArgumentException", because the
2080
        // specified remoteId is already used by the "Media" page.
2081
        $contentService->updateContentMetadata(
2082
            $content->contentInfo,
2083
            $metadataUpdate
2084
        );
2085
        /* END: Use Case */
2086
    }
2087
2088
    /**
2089
     * Test for the updateContentMetadata() method.
2090
     *
2091
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContentMetadata
2092
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2093
     */
2094
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionOnNoMetadataPropertiesSet()
2095
    {
2096
        $repository = $this->getRepository();
2097
2098
        $contentService = $repository->getContentService();
2099
2100
        $contentInfo = $contentService->loadContentInfo(4);
2101
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
2102
2103
        // Throws an exception because no properties are set in $contentMetadataUpdateStruct
2104
        $contentService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
2105
    }
2106
2107
    /**
2108
     * Test for the deleteContent() method.
2109
     *
2110
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2111
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2112
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2113
     */
2114 View Code Duplication
    public function testDeleteContent()
2115
    {
2116
        $repository = $this->getRepository();
2117
2118
        $contentService = $repository->getContentService();
2119
        $locationService = $repository->getLocationService();
2120
2121
        /* BEGIN: Use Case */
2122
        $contentVersion2 = $this->createContentVersion2();
2123
2124
        // Load the locations for this content object
2125
        $locations = $locationService->loadLocations($contentVersion2->contentInfo);
2126
2127
        // This will delete the content, all versions and the associated locations
2128
        $contentService->deleteContent($contentVersion2->contentInfo);
2129
        /* END: Use Case */
2130
2131
        foreach ($locations as $location) {
2132
            $locationService->loadLocation($location->id);
2133
        }
2134
    }
2135
2136
    /**
2137
     * Test for the deleteContent() method.
2138
     *
2139
     * Test for issue EZP-21057:
2140
     * "contentService: Unable to delete a content with an empty file attribute"
2141
     *
2142
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
2143
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2144
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2145
     */
2146 View Code Duplication
    public function testDeleteContentWithEmptyBinaryField()
2147
    {
2148
        $repository = $this->getRepository();
2149
2150
        $contentService = $repository->getContentService();
2151
        $locationService = $repository->getLocationService();
2152
2153
        /* BEGIN: Use Case */
2154
        $contentVersion = $this->createContentVersion1EmptyBinaryField();
2155
2156
        // Load the locations for this content object
2157
        $locations = $locationService->loadLocations($contentVersion->contentInfo);
2158
2159
        // This will delete the content, all versions and the associated locations
2160
        $contentService->deleteContent($contentVersion->contentInfo);
2161
        /* END: Use Case */
2162
2163
        foreach ($locations as $location) {
2164
            $locationService->loadLocation($location->id);
2165
        }
2166
    }
2167
2168
    /**
2169
     * Test for the loadContentDrafts() method.
2170
     *
2171
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2172
     */
2173
    public function testLoadContentDraftsReturnsEmptyArrayByDefault()
2174
    {
2175
        $repository = $this->getRepository();
2176
2177
        /* BEGIN: Use Case */
2178
        $contentService = $repository->getContentService();
2179
2180
        $contentDrafts = $contentService->loadContentDrafts();
2181
        /* END: Use Case */
2182
2183
        $this->assertSame(array(), $contentDrafts);
2184
    }
2185
2186
    /**
2187
     * Test for the loadContentDrafts() method.
2188
     *
2189
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
2190
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2191
     */
2192
    public function testLoadContentDrafts()
2193
    {
2194
        $repository = $this->getRepository();
2195
2196
        /* BEGIN: Use Case */
2197
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
2198
        // of a eZ Publish demo installation.
2199
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2200
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
2201
2202
        $contentService = $repository->getContentService();
2203
2204
        // "Media" content object
2205
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2206
2207
        // "eZ Publish Demo Design ..." content object
2208
        $demoDesignContentInfo = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
2209
2210
        // Create some drafts
2211
        $contentService->createContentDraft($mediaContentInfo);
2212
        $contentService->createContentDraft($demoDesignContentInfo);
2213
2214
        // Now $contentDrafts should contain two drafted versions
2215
        $draftedVersions = $contentService->loadContentDrafts();
2216
        /* END: Use Case */
2217
2218
        $actual = array(
2219
            $draftedVersions[0]->status,
2220
            $draftedVersions[0]->getContentInfo()->remoteId,
2221
            $draftedVersions[1]->status,
2222
            $draftedVersions[1]->getContentInfo()->remoteId,
2223
        );
2224
        sort($actual, SORT_STRING);
2225
2226
        $this->assertEquals(
2227
            array(
2228
                VersionInfo::STATUS_DRAFT,
2229
                VersionInfo::STATUS_DRAFT,
2230
                $demoDesignRemoteId,
2231
                $mediaRemoteId,
2232
            ),
2233
            $actual
2234
        );
2235
    }
2236
2237
    /**
2238
     * Test for the loadContentDrafts() method.
2239
     *
2240
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
2241
     */
2242
    public function testLoadContentDraftsWithFirstParameter()
2243
    {
2244
        $repository = $this->getRepository();
2245
2246
        /* BEGIN: Use Case */
2247
        $user = $this->createUserVersion1();
2248
2249
        // Get current user
2250
        $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...
2251
2252
        // Set new editor as user
2253
        $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...
2254
2255
        // Remote id of the "Media" content object in an eZ Publish demo installation.
2256
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2257
2258
        $contentService = $repository->getContentService();
2259
2260
        // "Media" content object
2261
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2262
2263
        // Create a content draft
2264
        $contentService->createContentDraft($mediaContentInfo);
2265
2266
        // Reset to previous current user
2267
        $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...
2268
2269
        // Now $contentDrafts for the previous current user and the new user
2270
        $newCurrentUserDrafts = $contentService->loadContentDrafts($user);
2271
        $oldCurrentUserDrafts = $contentService->loadContentDrafts($oldCurrentUser);
2272
        /* END: Use Case */
2273
2274
        $this->assertSame(array(), $oldCurrentUserDrafts);
2275
2276
        $this->assertEquals(
2277
            array(
2278
                VersionInfo::STATUS_DRAFT,
2279
                $mediaRemoteId,
2280
            ),
2281
            array(
2282
                $newCurrentUserDrafts[0]->status,
2283
                $newCurrentUserDrafts[0]->getContentInfo()->remoteId,
2284
            )
2285
        );
2286
        $this->assertTrue($newCurrentUserDrafts[0]->isDraft());
2287
        $this->assertFalse($newCurrentUserDrafts[0]->isArchived());
2288
        $this->assertFalse($newCurrentUserDrafts[0]->isPublished());
2289
    }
2290
2291
    /**
2292
     * Test for the loadVersionInfo() method.
2293
     *
2294
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2295
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2296
     */
2297
    public function testLoadVersionInfoWithSecondParameter()
2298
    {
2299
        $repository = $this->getRepository();
2300
2301
        $contentService = $repository->getContentService();
2302
2303
        /* BEGIN: Use Case */
2304
        $publishedContent = $this->createContentVersion1();
2305
2306
        $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...
2307
2308
        // Will return the VersionInfo of the $draftContent
2309
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2310
        /* END: Use Case */
2311
2312
        $this->assertEquals(2, $versionInfo->versionNo);
2313
2314
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2315
        $this->assertEquals(
2316
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2317
            $versionInfo->getContentInfo()->mainLocationId
2318
        );
2319
    }
2320
2321
    /**
2322
     * Test for the loadVersionInfo() method.
2323
     *
2324
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2325
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2326
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2327
     */
2328
    public function testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter()
2329
    {
2330
        $repository = $this->getRepository();
2331
2332
        $contentService = $repository->getContentService();
2333
2334
        /* BEGIN: Use Case */
2335
        $draft = $this->createContentDraftVersion1();
2336
2337
        // This call will fail with a "NotFoundException", because not versionNo
2338
        // 2 exists for this content object.
2339
        $contentService->loadVersionInfo($draft->contentInfo, 2);
2340
        /* END: Use Case */
2341
    }
2342
2343
    /**
2344
     * Test for the loadVersionInfoById() method.
2345
     *
2346
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2347
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2348
     */
2349
    public function testLoadVersionInfoByIdWithSecondParameter()
2350
    {
2351
        $repository = $this->getRepository();
2352
2353
        $contentService = $repository->getContentService();
2354
2355
        /* BEGIN: Use Case */
2356
        $publishedContent = $this->createContentVersion1();
2357
2358
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
2359
2360
        // Will return the VersionInfo of the $draftContent
2361
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2362
        /* END: Use Case */
2363
2364
        $this->assertEquals(2, $versionInfo->versionNo);
2365
2366
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2367
        $this->assertEquals(
2368
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2369
            $versionInfo->getContentInfo()->mainLocationId
2370
        );
2371
2372
        return [
2373
            'versionInfo' => $versionInfo,
2374
            'draftContent' => $draftContent,
2375
        ];
2376
    }
2377
2378
    /**
2379
     * Test for the returned value of the loadVersionInfoById() method.
2380
     *
2381
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter
2382
     * @covers \eZ\Publish\API\Repository\ContentService::loadVersionInfoById
2383
     *
2384
     * @param array $data
2385
     */
2386
    public function testLoadVersionInfoByIdWithSecondParameterSetsExpectedVersionInfo(array $data)
2387
    {
2388
        /** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo */
2389
        $versionInfo = $data['versionInfo'];
2390
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $draftContent */
2391
        $draftContent = $data['draftContent'];
2392
2393
        $this->assertPropertiesCorrect(
2394
            [
2395
                'names' => [
2396
                    'eng-US' => 'An awesome forum',
2397
                ],
2398
                'contentInfo' => new ContentInfo([
2399
                    'id' => $draftContent->contentInfo->id,
2400
                    'contentTypeId' => 28,
2401
                    'name' => 'An awesome forum',
2402
                    'sectionId' => 1,
2403
                    'currentVersionNo' => 1,
2404
                    'published' => true,
2405
                    'ownerId' => 14,
2406
                    // this Content Object is created at the test runtime
2407
                    'modificationDate' => $versionInfo->contentInfo->modificationDate,
2408
                    'publishedDate' => $versionInfo->contentInfo->publishedDate,
2409
                    'alwaysAvailable' => 1,
2410
                    'remoteId' => 'abcdef0123456789abcdef0123456789',
2411
                    'mainLanguageCode' => 'eng-US',
2412
                    'mainLocationId' => $draftContent->contentInfo->mainLocationId,
2413
                ]),
2414
                'id' => $draftContent->versionInfo->id,
2415
                'versionNo' => 2,
2416
                'creatorId' => 14,
2417
                'status' => 0,
2418
                'initialLanguageCode' => 'eng-US',
2419
                'languageCodes' => [
2420
                    'eng-US',
2421
                ],
2422
            ],
2423
            $versionInfo
2424
        );
2425
    }
2426
2427
    /**
2428
     * Test for the loadVersionInfoById() method.
2429
     *
2430
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2431
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2432
     */
2433 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter()
2434
    {
2435
        $repository = $this->getRepository();
2436
2437
        $contentService = $repository->getContentService();
2438
2439
        /* BEGIN: Use Case */
2440
        $content = $this->createContentVersion1();
2441
2442
        // This call will fail with a "NotFoundException", because not versionNo
2443
        // 2 exists for this content object.
2444
        $contentService->loadVersionInfoById($content->id, 2);
2445
        /* END: Use Case */
2446
    }
2447
2448
    /**
2449
     * Test for the loadContentByVersionInfo() method.
2450
     *
2451
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
2452
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2453
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
2454
     */
2455
    public function testLoadContentByVersionInfoWithSecondParameter()
2456
    {
2457
        $repository = $this->getRepository();
2458
2459
        $sectionId = $this->generateId('section', 1);
2460
        /* BEGIN: Use Case */
2461
        $contentTypeService = $repository->getContentTypeService();
2462
2463
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2464
2465
        $contentService = $repository->getContentService();
2466
2467
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2468
2469
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2470
2471
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2472
2473
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2474
        // $sectionId contains the ID of section 1
2475
        $contentCreateStruct->sectionId = $sectionId;
2476
        $contentCreateStruct->alwaysAvailable = true;
2477
2478
        // Create a new content draft
2479
        $content = $contentService->createContent($contentCreateStruct);
2480
2481
        // Now publish this draft
2482
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2483
2484
        // Will return a content instance with fields in "eng-US"
2485
        $reloadedContent = $contentService->loadContentByVersionInfo(
2486
            $publishedContent->getVersionInfo(),
2487
            array(
2488
                'eng-GB',
2489
            ),
2490
            false
2491
        );
2492
        /* END: Use Case */
2493
2494
        $actual = array();
2495 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...
2496
            $actual[] = new Field(
2497
                array(
2498
                    'id' => 0,
2499
                    '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...
2500
                    'languageCode' => $field->languageCode,
2501
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
2502
                )
2503
            );
2504
        }
2505
        usort(
2506
            $actual,
2507 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...
2508
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
2509
                    return strcasecmp($field1->languageCode, $field2->languageCode);
2510
                }
2511
2512
                return $return;
2513
            }
2514
        );
2515
2516
        $expected = array(
2517
            new Field(
2518
                array(
2519
                    'id' => 0,
2520
                    'value' => true,
2521
                    'languageCode' => 'eng-GB',
2522
                    'fieldDefIdentifier' => 'description',
2523
                )
2524
            ),
2525
            new Field(
2526
                array(
2527
                    'id' => 0,
2528
                    'value' => true,
2529
                    'languageCode' => 'eng-GB',
2530
                    'fieldDefIdentifier' => 'name',
2531
                )
2532
            ),
2533
        );
2534
2535
        $this->assertEquals($expected, $actual);
2536
    }
2537
2538
    /**
2539
     * Test for the loadContentByContentInfo() method.
2540
     *
2541
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages)
2542
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2543
     */
2544
    public function testLoadContentByContentInfoWithLanguageParameters()
2545
    {
2546
        $repository = $this->getRepository();
2547
2548
        $sectionId = $this->generateId('section', 1);
2549
        /* BEGIN: Use Case */
2550
        $contentTypeService = $repository->getContentTypeService();
2551
2552
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2553
2554
        $contentService = $repository->getContentService();
2555
2556
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2557
2558
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2559
2560
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2561
2562
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2563
        // $sectionId contains the ID of section 1
2564
        $contentCreateStruct->sectionId = $sectionId;
2565
        $contentCreateStruct->alwaysAvailable = true;
2566
2567
        // Create a new content draft
2568
        $content = $contentService->createContent($contentCreateStruct);
2569
2570
        // Now publish this draft
2571
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2572
2573
        // Will return a content instance with fields in "eng-US"
2574
        $reloadedContent = $contentService->loadContentByContentInfo(
2575
            $publishedContent->contentInfo,
2576
            array(
2577
                'eng-US',
2578
            ),
2579
            null,
2580
            false
2581
        );
2582
        /* END: Use Case */
2583
2584
        $actual = $this->normalizeFields($reloadedContent->getFields());
2585
2586
        $expected = array(
2587
            new Field(
2588
                array(
2589
                    'id' => 0,
2590
                    'value' => true,
2591
                    'languageCode' => 'eng-US',
2592
                    'fieldDefIdentifier' => 'description',
2593
                )
2594
            ),
2595
            new Field(
2596
                array(
2597
                    'id' => 0,
2598
                    'value' => true,
2599
                    'languageCode' => 'eng-US',
2600
                    'fieldDefIdentifier' => 'name',
2601
                )
2602
            ),
2603
        );
2604
2605
        $this->assertEquals($expected, $actual);
2606
2607
        // Will return a content instance with fields in "eng-GB" (versions prior to 6.0.0-beta9 returned "eng-US" also)
2608
        $reloadedContent = $contentService->loadContentByContentInfo(
2609
            $publishedContent->contentInfo,
2610
            array(
2611
                'eng-GB',
2612
            ),
2613
            null,
2614
            true
2615
        );
2616
2617
        $actual = $this->normalizeFields($reloadedContent->getFields());
2618
2619
        $expected = array(
2620
            new Field(
2621
                array(
2622
                    'id' => 0,
2623
                    'value' => true,
2624
                    'languageCode' => 'eng-GB',
2625
                    'fieldDefIdentifier' => 'description',
2626
                )
2627
            ),
2628
            new Field(
2629
                array(
2630
                    'id' => 0,
2631
                    'value' => true,
2632
                    'languageCode' => 'eng-GB',
2633
                    'fieldDefIdentifier' => 'name',
2634
                )
2635
            ),
2636
        );
2637
2638
        $this->assertEquals($expected, $actual);
2639
2640
        // Will return a content instance with fields in main language "eng-US", as "fre-FR" does not exists
2641
        $reloadedContent = $contentService->loadContentByContentInfo(
2642
            $publishedContent->contentInfo,
2643
            array(
2644
                'fre-FR',
2645
            ),
2646
            null,
2647
            true
2648
        );
2649
2650
        $actual = $this->normalizeFields($reloadedContent->getFields());
2651
2652
        $expected = array(
2653
            new Field(
2654
                array(
2655
                    'id' => 0,
2656
                    'value' => true,
2657
                    'languageCode' => 'eng-US',
2658
                    'fieldDefIdentifier' => 'description',
2659
                )
2660
            ),
2661
            new Field(
2662
                array(
2663
                    'id' => 0,
2664
                    'value' => true,
2665
                    'languageCode' => 'eng-US',
2666
                    'fieldDefIdentifier' => 'name',
2667
                )
2668
            ),
2669
        );
2670
2671
        $this->assertEquals($expected, $actual);
2672
    }
2673
2674
    /**
2675
     * Test for the loadContentByContentInfo() method.
2676
     *
2677
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2678
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2679
     */
2680 View Code Duplication
    public function testLoadContentByContentInfoWithVersionNumberParameter()
2681
    {
2682
        $repository = $this->getRepository();
2683
2684
        $contentService = $repository->getContentService();
2685
2686
        /* BEGIN: Use Case */
2687
        $publishedContent = $this->createContentVersion1();
2688
2689
        $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...
2690
2691
        // This content instance is identical to $draftContent
2692
        $draftContentReloaded = $contentService->loadContentByContentInfo(
2693
            $publishedContent->contentInfo,
2694
            null,
2695
            2
2696
        );
2697
        /* END: Use Case */
2698
2699
        $this->assertEquals(
2700
            2,
2701
            $draftContentReloaded->getVersionInfo()->versionNo
2702
        );
2703
2704
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2705
        $this->assertEquals(
2706
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2707
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2708
        );
2709
    }
2710
2711
    /**
2712
     * Test for the loadContentByContentInfo() method.
2713
     *
2714
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2715
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2716
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithVersionNumberParameter
2717
     */
2718 View Code Duplication
    public function testLoadContentByContentInfoThrowsNotFoundExceptionWithVersionNumberParameter()
2719
    {
2720
        $repository = $this->getRepository();
2721
2722
        $contentService = $repository->getContentService();
2723
2724
        /* BEGIN: Use Case */
2725
        $content = $this->createContentVersion1();
2726
2727
        // This call will fail with a "NotFoundException", because no content
2728
        // with versionNo = 2 exists.
2729
        $contentService->loadContentByContentInfo($content->contentInfo, null, 2);
2730
        /* END: Use Case */
2731
    }
2732
2733
    /**
2734
     * Test for the loadContent() method.
2735
     *
2736
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages)
2737
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2738
     */
2739 View Code Duplication
    public function testLoadContentWithSecondParameter()
2740
    {
2741
        $repository = $this->getRepository();
2742
2743
        $contentService = $repository->getContentService();
2744
2745
        /* BEGIN: Use Case */
2746
        $draft = $this->createMultipleLanguageDraftVersion1();
2747
2748
        // This draft contains those fields localized with "eng-GB"
2749
        $draftLocalized = $contentService->loadContent($draft->id, array('eng-GB'), null, false);
2750
        /* END: Use Case */
2751
2752
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2753
2754
        return $draft;
2755
    }
2756
2757
    /**
2758
     * Test for the loadContent() method using undefined translation.
2759
     *
2760
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithSecondParameter
2761
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2762
     *
2763
     * @param \eZ\Publish\API\Repository\Values\Content\Content $contentDraft
2764
     */
2765
    public function testLoadContentWithSecondParameterThrowsNotFoundException(Content $contentDraft)
2766
    {
2767
        $repository = $this->getRepository();
2768
2769
        $contentService = $repository->getContentService();
2770
2771
        $contentService->loadContent($contentDraft->id, array('ger-DE'), null, false);
2772
    }
2773
2774
    /**
2775
     * Test for the loadContent() method.
2776
     *
2777
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2778
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2779
     */
2780 View Code Duplication
    public function testLoadContentWithThirdParameter()
2781
    {
2782
        $repository = $this->getRepository();
2783
2784
        $contentService = $repository->getContentService();
2785
2786
        /* BEGIN: Use Case */
2787
        $publishedContent = $this->createContentVersion1();
2788
2789
        $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...
2790
2791
        // This content instance is identical to $draftContent
2792
        $draftContentReloaded = $contentService->loadContent($publishedContent->id, null, 2);
2793
        /* END: Use Case */
2794
2795
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2796
2797
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2798
        $this->assertEquals(
2799
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2800
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2801
        );
2802
    }
2803
2804
    /**
2805
     * Test for the loadContent() method.
2806
     *
2807
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2808
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2809
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithThirdParameter
2810
     */
2811 View Code Duplication
    public function testLoadContentThrowsNotFoundExceptionWithThirdParameter()
2812
    {
2813
        $repository = $this->getRepository();
2814
2815
        $contentService = $repository->getContentService();
2816
2817
        /* BEGIN: Use Case */
2818
        $content = $this->createContentVersion1();
2819
2820
        // This call will fail with a "NotFoundException", because for this
2821
        // content object no versionNo=2 exists.
2822
        $contentService->loadContent($content->id, null, 2);
2823
        /* END: Use Case */
2824
    }
2825
2826
    /**
2827
     * Test for the loadContentByRemoteId() method.
2828
     *
2829
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages)
2830
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2831
     */
2832 View Code Duplication
    public function testLoadContentByRemoteIdWithSecondParameter()
2833
    {
2834
        $repository = $this->getRepository();
2835
2836
        $contentService = $repository->getContentService();
2837
2838
        /* BEGIN: Use Case */
2839
        $draft = $this->createMultipleLanguageDraftVersion1();
2840
2841
        $contentService->publishVersion($draft->versionInfo);
2842
2843
        // This draft contains those fields localized with "eng-GB"
2844
        $draftLocalized = $contentService->loadContentByRemoteId(
2845
            $draft->contentInfo->remoteId,
2846
            array('eng-GB'),
2847
            null,
2848
            false
2849
        );
2850
        /* END: Use Case */
2851
2852
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2853
    }
2854
2855
    /**
2856
     * Test for the loadContentByRemoteId() method.
2857
     *
2858
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2859
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2860
     */
2861 View Code Duplication
    public function testLoadContentByRemoteIdWithThirdParameter()
2862
    {
2863
        $repository = $this->getRepository();
2864
2865
        $contentService = $repository->getContentService();
2866
2867
        /* BEGIN: Use Case */
2868
        $publishedContent = $this->createContentVersion1();
2869
2870
        $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...
2871
2872
        // This content instance is identical to $draftContent
2873
        $draftContentReloaded = $contentService->loadContentByRemoteId(
2874
            $publishedContent->contentInfo->remoteId,
2875
            null,
2876
            2
2877
        );
2878
        /* END: Use Case */
2879
2880
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2881
2882
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2883
        $this->assertEquals(
2884
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2885
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2886
        );
2887
    }
2888
2889
    /**
2890
     * Test for the loadContentByRemoteId() method.
2891
     *
2892
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2893
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2894
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithThirdParameter
2895
     */
2896
    public function testLoadContentByRemoteIdThrowsNotFoundExceptionWithThirdParameter()
2897
    {
2898
        $repository = $this->getRepository();
2899
2900
        $contentService = $repository->getContentService();
2901
2902
        /* BEGIN: Use Case */
2903
        $content = $this->createContentVersion1();
2904
2905
        // This call will fail with a "NotFoundException", because for this
2906
        // content object no versionNo=2 exists.
2907
        $contentService->loadContentByRemoteId(
2908
            $content->contentInfo->remoteId,
2909
            null,
2910
            2
2911
        );
2912
        /* END: Use Case */
2913
    }
2914
2915
    /**
2916
     * Test that retrieval of translated name field respects prioritized language list.
2917
     *
2918
     * @dataProvider getPrioritizedLanguageList
2919
     * @param string[]|null $languageCodes
2920
     */
2921
    public function testLoadContentWithPrioritizedLanguagesList($languageCodes)
2922
    {
2923
        $repository = $this->getRepository();
2924
2925
        $contentService = $repository->getContentService();
2926
2927
        $content = $this->createContentVersion2();
2928
2929
        $content = $contentService->loadContent($content->id, $languageCodes);
2930
2931
        $expectedName = $content->getVersionInfo()->getName(
2932
            isset($languageCodes[0]) ? $languageCodes[0] : null
2933
        );
2934
        $nameValue = $content->getFieldValue('name');
2935
        /** @var \eZ\Publish\Core\FieldType\TextLine\Value $nameValue */
2936
        self::assertEquals($expectedName, $nameValue->text);
2937
        self::assertEquals($expectedName, $content->getVersionInfo()->getName());
2938
        // Also check value on shortcut method on content
2939
        self::assertEquals($expectedName, $content->getName());
2940
    }
2941
2942
    /**
2943
     * @return array
2944
     */
2945
    public function getPrioritizedLanguageList()
2946
    {
2947
        return [
2948
            [['eng-US']],
2949
            [['eng-GB']],
2950
            [['eng-GB', 'eng-US']],
2951
            [['eng-US', 'eng-GB']],
2952
        ];
2953
    }
2954
2955
    /**
2956
     * Test for the deleteVersion() method.
2957
     *
2958
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2959
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2960
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2961
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2962
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2963
     */
2964
    public function testDeleteVersion()
2965
    {
2966
        $repository = $this->getRepository();
2967
2968
        $contentService = $repository->getContentService();
2969
2970
        /* BEGIN: Use Case */
2971
        $content = $this->createContentVersion1();
2972
2973
        // Create new draft, because published or last version of the Content can't be deleted
2974
        $draft = $contentService->createContentDraft(
2975
            $content->getVersionInfo()->getContentInfo()
2976
        );
2977
2978
        // Delete the previously created draft
2979
        $contentService->deleteVersion($draft->getVersionInfo());
2980
        /* END: Use Case */
2981
2982
        $versions = $contentService->loadVersions($content->getVersionInfo()->getContentInfo());
2983
2984
        $this->assertCount(1, $versions);
2985
        $this->assertEquals(
2986
            $content->getVersionInfo()->id,
2987
            $versions[0]->id
2988
        );
2989
    }
2990
2991
    /**
2992
     * Test for the deleteVersion() method.
2993
     *
2994
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2995
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
2996
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2997
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2998
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2999
     */
3000
    public function testDeleteVersionThrowsBadStateExceptionOnPublishedVersion()
3001
    {
3002
        $repository = $this->getRepository();
3003
3004
        $contentService = $repository->getContentService();
3005
3006
        /* BEGIN: Use Case */
3007
        $content = $this->createContentVersion1();
3008
3009
        // This call will fail with a "BadStateException", because the content
3010
        // version is currently published.
3011
        $contentService->deleteVersion($content->getVersionInfo());
3012
        /* END: Use Case */
3013
    }
3014
3015
    /**
3016
     * Test for the deleteVersion() method.
3017
     *
3018
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
3019
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3020
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3021
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3022
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3023
     */
3024 View Code Duplication
    public function testDeleteVersionThrowsBadStateExceptionOnLastVersion()
3025
    {
3026
        $repository = $this->getRepository();
3027
3028
        $contentService = $repository->getContentService();
3029
3030
        /* BEGIN: Use Case */
3031
        $draft = $this->createContentDraftVersion1();
3032
3033
        // This call will fail with a "BadStateException", because the Content
3034
        // version is the last version of the Content.
3035
        $contentService->deleteVersion($draft->getVersionInfo());
3036
        /* END: Use Case */
3037
    }
3038
3039
    /**
3040
     * Test for the loadVersions() method.
3041
     *
3042
     * @see \eZ\Publish\API\Repository\ContentService::loadVersions()
3043
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3044
     *
3045
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[]
3046
     */
3047
    public function testLoadVersions()
3048
    {
3049
        $repository = $this->getRepository();
3050
3051
        $contentService = $repository->getContentService();
3052
3053
        /* BEGIN: Use Case */
3054
        $contentVersion2 = $this->createContentVersion2();
3055
3056
        // Load versions of this ContentInfo instance
3057
        $versions = $contentService->loadVersions($contentVersion2->contentInfo);
3058
        /* END: Use Case */
3059
3060
        $expectedVersionsOrder = [
3061
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1),
3062
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 2),
3063
        ];
3064
3065
        $this->assertEquals($expectedVersionsOrder, $versions);
3066
3067
        return $versions;
3068
    }
3069
3070
    /**
3071
     * Test for the loadVersions() method.
3072
     *
3073
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersions
3074
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersions
3075
     *
3076
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo[] $versions
3077
     */
3078
    public function testLoadVersionsSetsExpectedVersionInfo(array $versions)
3079
    {
3080
        $this->assertCount(2, $versions);
3081
3082
        $expectedVersions = [
3083
            [
3084
                'versionNo' => 1,
3085
                'creatorId' => 14,
3086
                'status' => VersionInfo::STATUS_ARCHIVED,
3087
                'initialLanguageCode' => 'eng-US',
3088
                'languageCodes' => ['eng-US'],
3089
            ],
3090
            [
3091
                'versionNo' => 2,
3092
                'creatorId' => 10,
3093
                'status' => VersionInfo::STATUS_PUBLISHED,
3094
                'initialLanguageCode' => 'eng-US',
3095
                'languageCodes' => ['eng-US', 'eng-GB'],
3096
            ],
3097
        ];
3098
3099
        $this->assertPropertiesCorrect($expectedVersions[0], $versions[0]);
3100
        $this->assertPropertiesCorrect($expectedVersions[1], $versions[1]);
3101
        $this->assertEquals(
3102
            $versions[0]->creationDate->getTimestamp(),
3103
            $versions[1]->creationDate->getTimestamp(),
3104
            'Creation time did not match within delta of 2 seconds',
3105
            2
3106
        );
3107
        $this->assertEquals(
3108
            $versions[0]->modificationDate->getTimestamp(),
3109
            $versions[1]->modificationDate->getTimestamp(),
3110
            'Creation time did not match within delta of 2 seconds',
3111
            2
3112
        );
3113
        $this->assertTrue($versions[0]->isArchived());
3114
        $this->assertFalse($versions[0]->isDraft());
3115
        $this->assertFalse($versions[0]->isPublished());
3116
3117
        $this->assertTrue($versions[1]->isPublished());
3118
        $this->assertFalse($versions[1]->isDraft());
3119
        $this->assertFalse($versions[1]->isArchived());
3120
    }
3121
3122
    /**
3123
     * Test for the copyContent() method.
3124
     *
3125
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
3126
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3127
     * @group field-type
3128
     */
3129 View Code Duplication
    public function testCopyContent()
3130
    {
3131
        $parentLocationId = $this->generateId('location', 56);
3132
3133
        $repository = $this->getRepository();
3134
3135
        $contentService = $repository->getContentService();
3136
        $locationService = $repository->getLocationService();
3137
3138
        /* BEGIN: Use Case */
3139
        $contentVersion2 = $this->createMultipleLanguageContentVersion2();
3140
3141
        // Configure new target location
3142
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
3143
3144
        $targetLocationCreate->priority = 42;
3145
        $targetLocationCreate->hidden = true;
3146
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3147
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3148
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3149
3150
        // Copy content with all versions and drafts
3151
        $contentCopied = $contentService->copyContent(
3152
            $contentVersion2->contentInfo,
3153
            $targetLocationCreate
3154
        );
3155
        /* END: Use Case */
3156
3157
        $this->assertInstanceOf(
3158
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
3159
            $contentCopied
3160
        );
3161
3162
        $this->assertNotEquals(
3163
            $contentVersion2->contentInfo->remoteId,
3164
            $contentCopied->contentInfo->remoteId
3165
        );
3166
3167
        $this->assertNotEquals(
3168
            $contentVersion2->id,
3169
            $contentCopied->id
3170
        );
3171
3172
        $this->assertEquals(
3173
            2,
3174
            count($contentService->loadVersions($contentCopied->contentInfo))
3175
        );
3176
3177
        $this->assertEquals(2, $contentCopied->getVersionInfo()->versionNo);
3178
3179
        $this->assertAllFieldsEquals($contentCopied->getFields());
3180
3181
        $this->assertDefaultContentStates($contentCopied->contentInfo);
3182
3183
        $this->assertNotNull(
3184
            $contentCopied->contentInfo->mainLocationId,
3185
            'Expected main location to be set given we provided a LocationCreateStruct'
3186
        );
3187
    }
3188
3189
    /**
3190
     * Test for the copyContent() method.
3191
     *
3192
     * @see \eZ\Publish\API\Repository\ContentService::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo)
3193
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
3194
     *
3195
     * @todo Fix to more descriptive name
3196
     */
3197 View Code Duplication
    public function testCopyContentWithThirdParameter()
3198
    {
3199
        $parentLocationId = $this->generateId('location', 56);
3200
3201
        $repository = $this->getRepository();
3202
3203
        $contentService = $repository->getContentService();
3204
        $locationService = $repository->getLocationService();
3205
3206
        /* BEGIN: Use Case */
3207
        $contentVersion2 = $this->createContentVersion2();
3208
3209
        // Configure new target location
3210
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
3211
3212
        $targetLocationCreate->priority = 42;
3213
        $targetLocationCreate->hidden = true;
3214
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3215
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3216
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3217
3218
        // Copy only the initial version
3219
        $contentCopied = $contentService->copyContent(
3220
            $contentVersion2->contentInfo,
3221
            $targetLocationCreate,
3222
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1)
3223
        );
3224
        /* END: Use Case */
3225
3226
        $this->assertInstanceOf(
3227
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
3228
            $contentCopied
3229
        );
3230
3231
        $this->assertNotEquals(
3232
            $contentVersion2->contentInfo->remoteId,
3233
            $contentCopied->contentInfo->remoteId
3234
        );
3235
3236
        $this->assertNotEquals(
3237
            $contentVersion2->id,
3238
            $contentCopied->id
3239
        );
3240
3241
        $this->assertEquals(
3242
            1,
3243
            count($contentService->loadVersions($contentCopied->contentInfo))
3244
        );
3245
3246
        $this->assertEquals(1, $contentCopied->getVersionInfo()->versionNo);
3247
3248
        $this->assertNotNull(
3249
            $contentCopied->contentInfo->mainLocationId,
3250
            'Expected main location to be set given we provided a LocationCreateStruct'
3251
        );
3252
    }
3253
3254
    /**
3255
     * Test for the addRelation() method.
3256
     *
3257
     * @return \eZ\Publish\API\Repository\Values\Content\Content
3258
     *
3259
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3260
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3261
     */
3262
    public function testAddRelation()
3263
    {
3264
        $repository = $this->getRepository();
3265
3266
        $contentService = $repository->getContentService();
3267
3268
        /* BEGIN: Use Case */
3269
        // RemoteId of the "Media" content of an eZ Publish demo installation
3270
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3271
3272
        $draft = $this->createContentDraftVersion1();
3273
3274
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3275
3276
        // Create relation between new content object and "Media" page
3277
        $relation = $contentService->addRelation(
3278
            $draft->getVersionInfo(),
3279
            $media
3280
        );
3281
        /* END: Use Case */
3282
3283
        $this->assertInstanceOf(
3284
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Relation',
3285
            $relation
3286
        );
3287
3288
        return $contentService->loadRelations($draft->getVersionInfo());
3289
    }
3290
3291
    /**
3292
     * Test for the addRelation() method.
3293
     *
3294
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3295
     *
3296
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3297
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3298
     */
3299
    public function testAddRelationAddsRelationToContent($relations)
3300
    {
3301
        $this->assertEquals(
3302
            1,
3303
            count($relations)
3304
        );
3305
    }
3306
3307
    /**
3308
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3309
     */
3310
    protected function assertExpectedRelations($relations)
3311
    {
3312
        $this->assertEquals(
3313
            array(
3314
                'type' => Relation::COMMON,
3315
                'sourceFieldDefinitionIdentifier' => null,
3316
                'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3317
                'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3318
            ),
3319
            array(
3320
                'type' => $relations[0]->type,
3321
                'sourceFieldDefinitionIdentifier' => $relations[0]->sourceFieldDefinitionIdentifier,
3322
                'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3323
                'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3324
            )
3325
        );
3326
    }
3327
3328
    /**
3329
     * Test for the addRelation() method.
3330
     *
3331
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3332
     *
3333
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3334
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3335
     */
3336
    public function testAddRelationSetsExpectedRelations($relations)
3337
    {
3338
        $this->assertExpectedRelations($relations);
3339
    }
3340
3341
    /**
3342
     * Test for the createContentDraft() method.
3343
     *
3344
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3345
     *
3346
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
3347
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelationSetsExpectedRelations
3348
     */
3349 View Code Duplication
    public function testCreateContentDraftWithRelations()
3350
    {
3351
        $repository = $this->getRepository();
3352
3353
        $contentService = $repository->getContentService();
3354
3355
        // RemoteId of the "Media" content of an eZ Publish demo installation
3356
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3357
        $draft = $this->createContentDraftVersion1();
3358
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3359
3360
        // Create relation between new content object and "Media" page
3361
        $contentService->addRelation(
3362
            $draft->getVersionInfo(),
3363
            $media
3364
        );
3365
3366
        $content = $contentService->publishVersion($draft->versionInfo);
3367
        $newDraft = $contentService->createContentDraft($content->contentInfo);
3368
3369
        return $contentService->loadRelations($newDraft->getVersionInfo());
3370
    }
3371
3372
    /**
3373
     * Test for the createContentDraft() method.
3374
     *
3375
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3376
     *
3377
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3378
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelations
3379
     */
3380
    public function testCreateContentDraftWithRelationsCreatesRelations($relations)
3381
    {
3382
        $this->assertEquals(
3383
            1,
3384
            count($relations)
3385
        );
3386
3387
        return $relations;
3388
    }
3389
3390
    /**
3391
     * Test for the createContentDraft() method.
3392
     *
3393
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3394
     *
3395
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelationsCreatesRelations
3396
     */
3397
    public function testCreateContentDraftWithRelationsCreatesExpectedRelations($relations)
3398
    {
3399
        $this->assertExpectedRelations($relations);
3400
    }
3401
3402
    /**
3403
     * Test for the addRelation() method.
3404
     *
3405
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3406
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3407
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3408
     */
3409 View Code Duplication
    public function testAddRelationThrowsBadStateException()
3410
    {
3411
        $repository = $this->getRepository();
3412
3413
        $contentService = $repository->getContentService();
3414
3415
        /* BEGIN: Use Case */
3416
        // RemoteId of the "Media" page of an eZ Publish demo installation
3417
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3418
3419
        $content = $this->createContentVersion1();
3420
3421
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3422
3423
        // This call will fail with a "BadStateException", because content is
3424
        // published and not a draft.
3425
        $contentService->addRelation(
3426
            $content->getVersionInfo(),
3427
            $media
3428
        );
3429
        /* END: Use Case */
3430
    }
3431
3432
    /**
3433
     * Test for the loadRelations() method.
3434
     *
3435
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3436
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3437
     */
3438
    public function testLoadRelations()
3439
    {
3440
        $repository = $this->getRepository();
3441
3442
        $contentService = $repository->getContentService();
3443
3444
        /* BEGIN: Use Case */
3445
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3446
        // of a eZ Publish demo installation.
3447
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3448
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3449
3450
        $draft = $this->createContentDraftVersion1();
3451
3452
        // Load other content objects
3453
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3454
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3455
3456
        // Create relation between new content object and "Media" page
3457
        $contentService->addRelation(
3458
            $draft->getVersionInfo(),
3459
            $media
3460
        );
3461
3462
        // Create another relation with the "Demo Design" page
3463
        $contentService->addRelation(
3464
            $draft->getVersionInfo(),
3465
            $demoDesign
3466
        );
3467
3468
        // Load all relations
3469
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3470
        /* END: Use Case */
3471
3472
        usort(
3473
            $relations,
3474
            function ($rel1, $rel2) {
3475
                return strcasecmp(
3476
                    $rel2->getDestinationContentInfo()->remoteId,
3477
                    $rel1->getDestinationContentInfo()->remoteId
3478
                );
3479
            }
3480
        );
3481
3482
        $this->assertEquals(
3483
            array(
3484
                array(
3485
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3486
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3487
                ),
3488
                array(
3489
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3490
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3491
                ),
3492
            ),
3493
            array(
3494
                array(
3495
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3496
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3497
                ),
3498
                array(
3499
                    'sourceContentInfo' => $relations[1]->sourceContentInfo->remoteId,
3500
                    'destinationContentInfo' => $relations[1]->destinationContentInfo->remoteId,
3501
                ),
3502
            )
3503
        );
3504
    }
3505
3506
    /**
3507
     * Test for the loadRelations() method.
3508
     *
3509
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3510
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3511
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3512
     */
3513
    public function testLoadRelationsSkipsArchivedContent()
3514
    {
3515
        $repository = $this->getRepository();
3516
3517
        $contentService = $repository->getContentService();
3518
3519
        /* BEGIN: Use Case */
3520
        $trashService = $repository->getTrashService();
3521
        $locationService = $repository->getLocationService();
3522
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3523
        // of a eZ Publish demo installation.
3524
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3525
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3526
3527
        $draft = $this->createContentDraftVersion1();
3528
3529
        // Load other content objects
3530
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3531
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3532
3533
        // Create relation between new content object and "Media" page
3534
        $contentService->addRelation(
3535
            $draft->getVersionInfo(),
3536
            $media
3537
        );
3538
3539
        // Create another relation with the "Demo Design" page
3540
        $contentService->addRelation(
3541
            $draft->getVersionInfo(),
3542
            $demoDesign
3543
        );
3544
3545
        $demoDesignLocation = $locationService->loadLocation($demoDesign->mainLocationId);
3546
3547
        // Trashing Content's last Location will change its status to archived,
3548
        // in this case relation towards it will not be loaded.
3549
        $trashService->trash($demoDesignLocation);
3550
3551
        // Load all relations
3552
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3553
        /* END: Use Case */
3554
3555
        $this->assertCount(1, $relations);
3556
        $this->assertEquals(
3557
            array(
3558
                array(
3559
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3560
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3561
                ),
3562
            ),
3563
            array(
3564
                array(
3565
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3566
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3567
                ),
3568
            )
3569
        );
3570
    }
3571
3572
    /**
3573
     * Test for the loadRelations() method.
3574
     *
3575
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3576
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3577
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3578
     */
3579
    public function testLoadRelationsSkipsDraftContent()
3580
    {
3581
        $repository = $this->getRepository();
3582
3583
        $contentService = $repository->getContentService();
3584
3585
        /* BEGIN: Use Case */
3586
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3587
        // of a eZ Publish demo installation.
3588
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3589
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3590
3591
        $draft = $this->createContentDraftVersion1();
3592
3593
        // Load other content objects
3594
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3595
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3596
3597
        // Create draft of "Media" page
3598
        $mediaDraft = $contentService->createContentDraft($media->contentInfo);
3599
3600
        // Create relation between "Media" page and new content object draft.
3601
        // This relation will not be loaded before the draft is published.
3602
        $contentService->addRelation(
3603
            $mediaDraft->getVersionInfo(),
3604
            $draft->getVersionInfo()->getContentInfo()
3605
        );
3606
3607
        // Create another relation with the "Demo Design" page
3608
        $contentService->addRelation(
3609
            $mediaDraft->getVersionInfo(),
3610
            $demoDesign
3611
        );
3612
3613
        // Load all relations
3614
        $relations = $contentService->loadRelations($mediaDraft->getVersionInfo());
3615
        /* END: Use Case */
3616
3617
        $this->assertCount(1, $relations);
3618
        $this->assertEquals(
3619
            array(
3620
                array(
3621
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3622
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3623
                ),
3624
            ),
3625
            array(
3626
                array(
3627
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3628
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3629
                ),
3630
            )
3631
        );
3632
    }
3633
3634
    /**
3635
     * Test for the loadReverseRelations() method.
3636
     *
3637
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3638
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3639
     */
3640
    public function testLoadReverseRelations()
3641
    {
3642
        $repository = $this->getRepository();
3643
3644
        $contentService = $repository->getContentService();
3645
3646
        /* BEGIN: Use Case */
3647
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3648
        // of a eZ Publish demo installation.
3649
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3650
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3651
3652
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3653
        $contentInfo = $versionInfo->getContentInfo();
3654
3655
        // Create some drafts
3656
        $mediaDraft = $contentService->createContentDraft(
3657
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3658
        );
3659
        $demoDesignDraft = $contentService->createContentDraft(
3660
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3661
        );
3662
3663
        // Create relation between new content object and "Media" page
3664
        $relation1 = $contentService->addRelation(
3665
            $mediaDraft->getVersionInfo(),
3666
            $contentInfo
3667
        );
3668
3669
        // Create another relation with the "Demo Design" page
3670
        $relation2 = $contentService->addRelation(
3671
            $demoDesignDraft->getVersionInfo(),
3672
            $contentInfo
3673
        );
3674
3675
        // Publish drafts, so relations become active
3676
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3677
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3678
3679
        // Load all relations
3680
        $relations = $contentService->loadRelations($versionInfo);
3681
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3682
        /* END: Use Case */
3683
3684
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3685
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3686
3687
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3688
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3689
3690
        $this->assertEquals(0, count($relations));
3691
        $this->assertEquals(2, count($reverseRelations));
3692
3693
        usort(
3694
            $reverseRelations,
3695
            function ($rel1, $rel2) {
3696
                return strcasecmp(
3697
                    $rel2->getSourceContentInfo()->remoteId,
3698
                    $rel1->getSourceContentInfo()->remoteId
3699
                );
3700
            }
3701
        );
3702
3703
        $this->assertEquals(
3704
            array(
3705
                array(
3706
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3707
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3708
                ),
3709
                array(
3710
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3711
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3712
                ),
3713
            ),
3714
            array(
3715
                array(
3716
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3717
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3718
                ),
3719
                array(
3720
                    'sourceContentInfo' => $reverseRelations[1]->sourceContentInfo->remoteId,
3721
                    'destinationContentInfo' => $reverseRelations[1]->destinationContentInfo->remoteId,
3722
                ),
3723
            )
3724
        );
3725
    }
3726
3727
    /**
3728
     * Test for the loadReverseRelations() method.
3729
     *
3730
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3731
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3732
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3733
     */
3734
    public function testLoadReverseRelationsSkipsArchivedContent()
3735
    {
3736
        $repository = $this->getRepository();
3737
3738
        $contentService = $repository->getContentService();
3739
3740
        /* BEGIN: Use Case */
3741
        $trashService = $repository->getTrashService();
3742
        $locationService = $repository->getLocationService();
3743
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3744
        // of a eZ Publish demo installation.
3745
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3746
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3747
3748
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3749
        $contentInfo = $versionInfo->getContentInfo();
3750
3751
        // Create some drafts
3752
        $mediaDraft = $contentService->createContentDraft(
3753
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3754
        );
3755
        $demoDesignDraft = $contentService->createContentDraft(
3756
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3757
        );
3758
3759
        // Create relation between new content object and "Media" page
3760
        $relation1 = $contentService->addRelation(
3761
            $mediaDraft->getVersionInfo(),
3762
            $contentInfo
3763
        );
3764
3765
        // Create another relation with the "Demo Design" page
3766
        $relation2 = $contentService->addRelation(
3767
            $demoDesignDraft->getVersionInfo(),
3768
            $contentInfo
3769
        );
3770
3771
        // Publish drafts, so relations become active
3772
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3773
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3774
3775
        $demoDesignLocation = $locationService->loadLocation($demoDesignDraft->contentInfo->mainLocationId);
3776
3777
        // Trashing Content's last Location will change its status to archived,
3778
        // in this case relation from it will not be loaded.
3779
        $trashService->trash($demoDesignLocation);
3780
3781
        // Load all relations
3782
        $relations = $contentService->loadRelations($versionInfo);
3783
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3784
        /* END: Use Case */
3785
3786
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3787
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3788
3789
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3790
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3791
3792
        $this->assertEquals(0, count($relations));
3793
        $this->assertEquals(1, count($reverseRelations));
3794
3795
        $this->assertEquals(
3796
            array(
3797
                array(
3798
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3799
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3800
                ),
3801
            ),
3802
            array(
3803
                array(
3804
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3805
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3806
                ),
3807
            )
3808
        );
3809
    }
3810
3811
    /**
3812
     * Test for the loadReverseRelations() method.
3813
     *
3814
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3815
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3816
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3817
     */
3818
    public function testLoadReverseRelationsSkipsDraftContent()
3819
    {
3820
        $repository = $this->getRepository();
3821
3822
        $contentService = $repository->getContentService();
3823
3824
        /* BEGIN: Use Case */
3825
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3826
        // of a eZ Publish demo installation.
3827
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3828
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3829
3830
        // Load "Media" page Content
3831
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3832
3833
        // Create some drafts
3834
        $newDraftVersionInfo = $this->createContentDraftVersion1()->getVersionInfo();
3835
        $demoDesignDraft = $contentService->createContentDraft(
3836
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3837
        );
3838
3839
        // Create relation between "Media" page and new content object
3840
        $relation1 = $contentService->addRelation(
3841
            $newDraftVersionInfo,
3842
            $media->contentInfo
3843
        );
3844
3845
        // Create another relation with the "Demo Design" page
3846
        $relation2 = $contentService->addRelation(
3847
            $demoDesignDraft->getVersionInfo(),
3848
            $media->contentInfo
3849
        );
3850
3851
        // Publish drafts, so relations become active
3852
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3853
        // We will not publish new Content draft, therefore relation from it
3854
        // will not be loaded as reverse relation for "Media" page
3855
        //$contentService->publishVersion( $newDraftVersionInfo );
3856
3857
        // Load all relations
3858
        $relations = $contentService->loadRelations($media->versionInfo);
3859
        $reverseRelations = $contentService->loadReverseRelations($media->contentInfo);
3860
        /* END: Use Case */
3861
3862
        $this->assertEquals($media->contentInfo->id, $relation1->getDestinationContentInfo()->id);
3863
        $this->assertEquals($newDraftVersionInfo->contentInfo->id, $relation1->getSourceContentInfo()->id);
3864
3865
        $this->assertEquals($media->contentInfo->id, $relation2->getDestinationContentInfo()->id);
3866
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3867
3868
        $this->assertEquals(0, count($relations));
3869
        $this->assertEquals(1, count($reverseRelations));
3870
3871
        $this->assertEquals(
3872
            array(
3873
                array(
3874
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3875
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3876
                ),
3877
            ),
3878
            array(
3879
                array(
3880
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3881
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3882
                ),
3883
            )
3884
        );
3885
    }
3886
3887
    /**
3888
     * Test for the deleteRelation() method.
3889
     *
3890
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3891
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3892
     */
3893
    public function testDeleteRelation()
3894
    {
3895
        $repository = $this->getRepository();
3896
3897
        $contentService = $repository->getContentService();
3898
3899
        /* BEGIN: Use Case */
3900
        // Remote ids of the "Media" and the "Demo Design" page of a eZ Publish
3901
        // demo installation.
3902
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3903
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3904
3905
        $draft = $this->createContentDraftVersion1();
3906
3907
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3908
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3909
3910
        // Establish some relations
3911
        $contentService->addRelation($draft->getVersionInfo(), $media);
3912
        $contentService->addRelation($draft->getVersionInfo(), $demoDesign);
3913
3914
        // Delete one of the currently created relations
3915
        $contentService->deleteRelation($draft->getVersionInfo(), $media);
3916
3917
        // The relations array now contains only one element
3918
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3919
        /* END: Use Case */
3920
3921
        $this->assertEquals(1, count($relations));
3922
    }
3923
3924
    /**
3925
     * Test for the deleteRelation() method.
3926
     *
3927
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3928
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3929
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3930
     */
3931 View Code Duplication
    public function testDeleteRelationThrowsBadStateException()
3932
    {
3933
        $repository = $this->getRepository();
3934
3935
        $contentService = $repository->getContentService();
3936
3937
        /* BEGIN: Use Case */
3938
        // RemoteId of the "Media" page of an eZ Publish demo installation
3939
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3940
3941
        $content = $this->createContentVersion1();
3942
3943
        // Load the destination object
3944
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3945
3946
        // Create a new draft
3947
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
3948
3949
        // Add a relation
3950
        $contentService->addRelation($draftVersion2->getVersionInfo(), $media);
3951
3952
        // Publish new version
3953
        $contentVersion2 = $contentService->publishVersion(
3954
            $draftVersion2->getVersionInfo()
3955
        );
3956
3957
        // This call will fail with a "BadStateException", because content is
3958
        // published and not a draft.
3959
        $contentService->deleteRelation(
3960
            $contentVersion2->getVersionInfo(),
3961
            $media
3962
        );
3963
        /* END: Use Case */
3964
    }
3965
3966
    /**
3967
     * Test for the deleteRelation() method.
3968
     *
3969
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3970
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
3971
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3972
     */
3973 View Code Duplication
    public function testDeleteRelationThrowsInvalidArgumentException()
3974
    {
3975
        $repository = $this->getRepository();
3976
3977
        $contentService = $repository->getContentService();
3978
3979
        /* BEGIN: Use Case */
3980
        // RemoteId of the "Media" page of an eZ Publish demo installation
3981
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3982
3983
        $draft = $this->createContentDraftVersion1();
3984
3985
        // Load the destination object
3986
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3987
3988
        // This call will fail with a "InvalidArgumentException", because no
3989
        // relation exists between $draft and $media.
3990
        $contentService->deleteRelation(
3991
            $draft->getVersionInfo(),
3992
            $media
3993
        );
3994
        /* END: Use Case */
3995
    }
3996
3997
    /**
3998
     * Test for the createContent() method.
3999
     *
4000
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
4001
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4002
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4003
     */
4004
    public function testCreateContentInTransactionWithRollback()
4005
    {
4006
        if ($this->isVersion4()) {
4007
            $this->markTestSkipped('This test requires eZ Publish 5');
4008
        }
4009
4010
        $repository = $this->getRepository();
4011
4012
        /* BEGIN: Use Case */
4013
        $contentTypeService = $repository->getContentTypeService();
4014
        $contentService = $repository->getContentService();
4015
4016
        // Start a transaction
4017
        $repository->beginTransaction();
4018
4019
        try {
4020
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
4021
4022
            // Get a content create struct and set mandatory properties
4023
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
4024
            $contentCreate->setField('name', 'Sindelfingen forum');
4025
4026
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
4027
            $contentCreate->alwaysAvailable = true;
4028
4029
            // Create a new content object
4030
            $contentId = $contentService->createContent($contentCreate)->id;
4031
        } catch (Exception $e) {
4032
            // Cleanup hanging transaction on error
4033
            $repository->rollback();
4034
            throw $e;
4035
        }
4036
4037
        // Rollback all changes
4038
        $repository->rollback();
4039
4040
        try {
4041
            // This call will fail with a "NotFoundException"
4042
            $contentService->loadContent($contentId);
4043
        } catch (NotFoundException $e) {
4044
            // This is expected
4045
            return;
4046
        }
4047
        /* END: Use Case */
4048
4049
        $this->fail('Content object still exists after rollback.');
4050
    }
4051
4052
    /**
4053
     * Test for the createContent() method.
4054
     *
4055
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
4056
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4057
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4058
     */
4059
    public function testCreateContentInTransactionWithCommit()
4060
    {
4061
        if ($this->isVersion4()) {
4062
            $this->markTestSkipped('This test requires eZ Publish 5');
4063
        }
4064
4065
        $repository = $this->getRepository();
4066
4067
        /* BEGIN: Use Case */
4068
        $contentTypeService = $repository->getContentTypeService();
4069
        $contentService = $repository->getContentService();
4070
4071
        // Start a transaction
4072
        $repository->beginTransaction();
4073
4074
        try {
4075
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
4076
4077
            // Get a content create struct and set mandatory properties
4078
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
4079
            $contentCreate->setField('name', 'Sindelfingen forum');
4080
4081
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
4082
            $contentCreate->alwaysAvailable = true;
4083
4084
            // Create a new content object
4085
            $contentId = $contentService->createContent($contentCreate)->id;
4086
4087
            // Commit changes
4088
            $repository->commit();
4089
        } catch (Exception $e) {
4090
            // Cleanup hanging transaction on error
4091
            $repository->rollback();
4092
            throw $e;
4093
        }
4094
4095
        // Load the new content object
4096
        $content = $contentService->loadContent($contentId);
4097
        /* END: Use Case */
4098
4099
        $this->assertEquals($contentId, $content->id);
4100
    }
4101
4102
    /**
4103
     * Test for the createContent() method.
4104
     *
4105
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
4106
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
4107
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
4108
     */
4109
    public function testCreateContentWithLocationCreateParameterInTransactionWithRollback()
4110
    {
4111
        $repository = $this->getRepository();
4112
4113
        $contentService = $repository->getContentService();
4114
4115
        /* BEGIN: Use Case */
4116
        // Start a transaction
4117
        $repository->beginTransaction();
4118
4119
        try {
4120
            $draft = $this->createContentDraftVersion1();
4121
        } catch (Exception $e) {
4122
            // Cleanup hanging transaction on error
4123
            $repository->rollback();
4124
            throw $e;
4125
        }
4126
4127
        $contentId = $draft->id;
4128
4129
        // Roleback the transaction
4130
        $repository->rollback();
4131
4132
        try {
4133
            // This call will fail with a "NotFoundException"
4134
            $contentService->loadContent($contentId);
4135
        } catch (NotFoundException $e) {
4136
            return;
4137
        }
4138
        /* END: Use Case */
4139
4140
        $this->fail('Can still load content object after rollback.');
4141
    }
4142
4143
    /**
4144
     * Test for the createContent() method.
4145
     *
4146
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
4147
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
4148
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
4149
     */
4150 View Code Duplication
    public function testCreateContentWithLocationCreateParameterInTransactionWithCommit()
4151
    {
4152
        $repository = $this->getRepository();
4153
4154
        $contentService = $repository->getContentService();
4155
4156
        /* BEGIN: Use Case */
4157
        // Start a transaction
4158
        $repository->beginTransaction();
4159
4160
        try {
4161
            $draft = $this->createContentDraftVersion1();
4162
4163
            $contentId = $draft->id;
4164
4165
            // Roleback the transaction
4166
            $repository->commit();
4167
        } catch (Exception $e) {
4168
            // Cleanup hanging transaction on error
4169
            $repository->rollback();
4170
            throw $e;
4171
        }
4172
4173
        // Load the new content object
4174
        $content = $contentService->loadContent($contentId);
4175
        /* END: Use Case */
4176
4177
        $this->assertEquals($contentId, $content->id);
4178
    }
4179
4180
    /**
4181
     * Test for the createContentDraft() method.
4182
     *
4183
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4184
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4185
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4186
     */
4187
    public function testCreateContentDraftInTransactionWithRollback()
4188
    {
4189
        $repository = $this->getRepository();
4190
4191
        $contentId = $this->generateId('object', 12);
4192
        /* BEGIN: Use Case */
4193
        // $contentId is the ID of the "Administrator users" user group
4194
4195
        // Get the content service
4196
        $contentService = $repository->getContentService();
4197
4198
        // Load the user group content object
4199
        $content = $contentService->loadContent($contentId);
4200
4201
        // Start a new transaction
4202
        $repository->beginTransaction();
4203
4204
        try {
4205
            // Create a new draft
4206
            $drafted = $contentService->createContentDraft($content->contentInfo);
4207
4208
            // Store version number for later reuse
4209
            $versionNo = $drafted->versionInfo->versionNo;
4210
        } catch (Exception $e) {
4211
            // Cleanup hanging transaction on error
4212
            $repository->rollback();
4213
            throw $e;
4214
        }
4215
4216
        // Rollback
4217
        $repository->rollback();
4218
4219
        try {
4220
            // This call will fail with a "NotFoundException"
4221
            $contentService->loadContent($contentId, null, $versionNo);
4222
        } catch (NotFoundException $e) {
4223
            return;
4224
        }
4225
        /* END: Use Case */
4226
4227
        $this->fail('Can still load content draft after rollback');
4228
    }
4229
4230
    /**
4231
     * Test for the createContentDraft() method.
4232
     *
4233
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4234
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4235
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4236
     */
4237 View Code Duplication
    public function testCreateContentDraftInTransactionWithCommit()
4238
    {
4239
        $repository = $this->getRepository();
4240
4241
        $contentId = $this->generateId('object', 12);
4242
        /* BEGIN: Use Case */
4243
        // $contentId is the ID of the "Administrator users" user group
4244
4245
        // Get the content service
4246
        $contentService = $repository->getContentService();
4247
4248
        // Load the user group content object
4249
        $content = $contentService->loadContent($contentId);
4250
4251
        // Start a new transaction
4252
        $repository->beginTransaction();
4253
4254
        try {
4255
            // Create a new draft
4256
            $drafted = $contentService->createContentDraft($content->contentInfo);
4257
4258
            // Store version number for later reuse
4259
            $versionNo = $drafted->versionInfo->versionNo;
4260
4261
            // Commit all changes
4262
            $repository->commit();
4263
        } catch (Exception $e) {
4264
            // Cleanup hanging transaction on error
4265
            $repository->rollback();
4266
            throw $e;
4267
        }
4268
4269
        $content = $contentService->loadContent($contentId, null, $versionNo);
4270
        /* END: Use Case */
4271
4272
        $this->assertEquals(
4273
            $versionNo,
4274
            $content->getVersionInfo()->versionNo
4275
        );
4276
    }
4277
4278
    /**
4279
     * Test for the publishVersion() method.
4280
     *
4281
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4282
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4283
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4284
     */
4285 View Code Duplication
    public function testPublishVersionInTransactionWithRollback()
4286
    {
4287
        $repository = $this->getRepository();
4288
4289
        $contentId = $this->generateId('object', 12);
4290
        /* BEGIN: Use Case */
4291
        // $contentId is the ID of the "Administrator users" user group
4292
4293
        // Get the content service
4294
        $contentService = $repository->getContentService();
4295
4296
        // Load the user group content object
4297
        $content = $contentService->loadContent($contentId);
4298
4299
        // Start a new transaction
4300
        $repository->beginTransaction();
4301
4302
        try {
4303
            $draftVersion = $contentService->createContentDraft($content->contentInfo)->getVersionInfo();
4304
4305
            // Publish a new version
4306
            $content = $contentService->publishVersion($draftVersion);
4307
4308
            // Store version number for later reuse
4309
            $versionNo = $content->versionInfo->versionNo;
4310
        } catch (Exception $e) {
4311
            // Cleanup hanging transaction on error
4312
            $repository->rollback();
4313
            throw $e;
4314
        }
4315
4316
        // Rollback
4317
        $repository->rollback();
4318
4319
        try {
4320
            // This call will fail with a "NotFoundException"
4321
            $contentService->loadContent($contentId, null, $versionNo);
4322
        } catch (NotFoundException $e) {
4323
            return;
4324
        }
4325
        /* END: Use Case */
4326
4327
        $this->fail('Can still load content draft after rollback');
4328
    }
4329
4330
    /**
4331
     * Test for the publishVersion() method.
4332
     *
4333
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4334
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4335
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
4336
     */
4337 View Code Duplication
    public function testPublishVersionInTransactionWithCommit()
4338
    {
4339
        $repository = $this->getRepository();
4340
4341
        /* BEGIN: Use Case */
4342
        // ID of the "Administrator users" user group
4343
        $contentId = 12;
4344
4345
        // Get the content service
4346
        $contentService = $repository->getContentService();
4347
4348
        // Load the user group content object
4349
        $template = $contentService->loadContent($contentId);
4350
4351
        // Start a new transaction
4352
        $repository->beginTransaction();
4353
4354
        try {
4355
            // Publish a new version
4356
            $content = $contentService->publishVersion(
4357
                $contentService->createContentDraft($template->contentInfo)->getVersionInfo()
4358
            );
4359
4360
            // Store version number for later reuse
4361
            $versionNo = $content->versionInfo->versionNo;
4362
4363
            // Commit all changes
4364
            $repository->commit();
4365
        } catch (Exception $e) {
4366
            // Cleanup hanging transaction on error
4367
            $repository->rollback();
4368
            throw $e;
4369
        }
4370
4371
        // Load current version info
4372
        $versionInfo = $contentService->loadVersionInfo($content->contentInfo);
4373
        /* END: Use Case */
4374
4375
        $this->assertEquals($versionNo, $versionInfo->versionNo);
4376
    }
4377
4378
    /**
4379
     * Test for the updateContent() method.
4380
     *
4381
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4382
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4383
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4384
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4385
     */
4386 View Code Duplication
    public function testUpdateContentInTransactionWithRollback()
4387
    {
4388
        $repository = $this->getRepository();
4389
4390
        $contentId = $this->generateId('object', 12);
4391
        /* BEGIN: Use Case */
4392
        // $contentId is the ID of the "Administrator users" user group
4393
4394
        // Load content service
4395
        $contentService = $repository->getContentService();
4396
4397
        // Create a new user group draft
4398
        $draft = $contentService->createContentDraft(
4399
            $contentService->loadContentInfo($contentId)
4400
        );
4401
4402
        // Get an update struct and change the group name
4403
        $contentUpdate = $contentService->newContentUpdateStruct();
4404
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4405
4406
        // Start a transaction
4407
        $repository->beginTransaction();
4408
4409
        try {
4410
            // Update the group name
4411
            $draft = $contentService->updateContent(
4412
                $draft->getVersionInfo(),
4413
                $contentUpdate
4414
            );
4415
4416
            // Publish updated version
4417
            $contentService->publishVersion($draft->getVersionInfo());
4418
        } catch (Exception $e) {
4419
            // Cleanup hanging transaction on error
4420
            $repository->rollback();
4421
            throw $e;
4422
        }
4423
4424
        // Rollback all changes.
4425
        $repository->rollback();
4426
4427
        // Name will still be "Administrator users"
4428
        $name = $contentService->loadContent($contentId)->getFieldValue('name');
4429
        /* END: Use Case */
4430
4431
        $this->assertEquals('Administrator users', $name);
4432
    }
4433
4434
    /**
4435
     * Test for the updateContent() method.
4436
     *
4437
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4438
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4439
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4440
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4441
     */
4442 View Code Duplication
    public function testUpdateContentInTransactionWithCommit()
4443
    {
4444
        $repository = $this->getRepository();
4445
4446
        $contentId = $this->generateId('object', 12);
4447
        /* BEGIN: Use Case */
4448
        // $contentId is the ID of the "Administrator users" user group
4449
4450
        // Load content service
4451
        $contentService = $repository->getContentService();
4452
4453
        // Create a new user group draft
4454
        $draft = $contentService->createContentDraft(
4455
            $contentService->loadContentInfo($contentId)
4456
        );
4457
4458
        // Get an update struct and change the group name
4459
        $contentUpdate = $contentService->newContentUpdateStruct();
4460
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4461
4462
        // Start a transaction
4463
        $repository->beginTransaction();
4464
4465
        try {
4466
            // Update the group name
4467
            $draft = $contentService->updateContent(
4468
                $draft->getVersionInfo(),
4469
                $contentUpdate
4470
            );
4471
4472
            // Publish updated version
4473
            $contentService->publishVersion($draft->getVersionInfo());
4474
4475
            // Commit all changes.
4476
            $repository->commit();
4477
        } catch (Exception $e) {
4478
            // Cleanup hanging transaction on error
4479
            $repository->rollback();
4480
            throw $e;
4481
        }
4482
4483
        // Name is now "Administrators"
4484
        $name = $contentService->loadContent($contentId)->getFieldValue('name', 'eng-US');
4485
        /* END: Use Case */
4486
4487
        $this->assertEquals('Administrators', $name);
4488
    }
4489
4490
    /**
4491
     * Test for the updateContentMetadata() method.
4492
     *
4493
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4494
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4495
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4496
     */
4497 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithRollback()
4498
    {
4499
        $repository = $this->getRepository();
4500
4501
        $contentId = $this->generateId('object', 12);
4502
        /* BEGIN: Use Case */
4503
        // $contentId is the ID of the "Administrator users" user group
4504
4505
        // Get the content service
4506
        $contentService = $repository->getContentService();
4507
4508
        // Load a ContentInfo object
4509
        $contentInfo = $contentService->loadContentInfo($contentId);
4510
4511
        // Store remoteId for later testing
4512
        $remoteId = $contentInfo->remoteId;
4513
4514
        // Start a transaction
4515
        $repository->beginTransaction();
4516
4517
        try {
4518
            // Get metadata update struct and change remoteId
4519
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4520
            $metadataUpdate->remoteId = md5(microtime(true));
4521
4522
            // Update the metadata of the published content object
4523
            $contentService->updateContentMetadata(
4524
                $contentInfo,
4525
                $metadataUpdate
4526
            );
4527
        } catch (Exception $e) {
4528
            // Cleanup hanging transaction on error
4529
            $repository->rollback();
4530
            throw $e;
4531
        }
4532
4533
        // Rollback all changes.
4534
        $repository->rollback();
4535
4536
        // Load current remoteId
4537
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4538
        /* END: Use Case */
4539
4540
        $this->assertEquals($remoteId, $remoteIdReloaded);
4541
    }
4542
4543
    /**
4544
     * Test for the updateContentMetadata() method.
4545
     *
4546
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4547
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4548
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4549
     */
4550 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithCommit()
4551
    {
4552
        $repository = $this->getRepository();
4553
4554
        $contentId = $this->generateId('object', 12);
4555
        /* BEGIN: Use Case */
4556
        // $contentId is the ID of the "Administrator users" user group
4557
4558
        // Get the content service
4559
        $contentService = $repository->getContentService();
4560
4561
        // Load a ContentInfo object
4562
        $contentInfo = $contentService->loadContentInfo($contentId);
4563
4564
        // Store remoteId for later testing
4565
        $remoteId = $contentInfo->remoteId;
4566
4567
        // Start a transaction
4568
        $repository->beginTransaction();
4569
4570
        try {
4571
            // Get metadata update struct and change remoteId
4572
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4573
            $metadataUpdate->remoteId = md5(microtime(true));
4574
4575
            // Update the metadata of the published content object
4576
            $contentService->updateContentMetadata(
4577
                $contentInfo,
4578
                $metadataUpdate
4579
            );
4580
4581
            // Commit all changes.
4582
            $repository->commit();
4583
        } catch (Exception $e) {
4584
            // Cleanup hanging transaction on error
4585
            $repository->rollback();
4586
            throw $e;
4587
        }
4588
4589
        // Load current remoteId
4590
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4591
        /* END: Use Case */
4592
4593
        $this->assertNotEquals($remoteId, $remoteIdReloaded);
4594
    }
4595
4596
    /**
4597
     * Test for the deleteVersion() method.
4598
     *
4599
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4600
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4601
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4602
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4603
     */
4604
    public function testDeleteVersionInTransactionWithRollback()
4605
    {
4606
        $repository = $this->getRepository();
4607
4608
        $contentId = $this->generateId('object', 12);
4609
        /* BEGIN: Use Case */
4610
        // $contentId is the ID of the "Administrator users" user group
4611
4612
        // Get the content service
4613
        $contentService = $repository->getContentService();
4614
4615
        // Start a new transaction
4616
        $repository->beginTransaction();
4617
4618
        try {
4619
            // Create a new draft
4620
            $draft = $contentService->createContentDraft(
4621
                $contentService->loadContentInfo($contentId)
4622
            );
4623
4624
            $contentService->deleteVersion($draft->getVersionInfo());
4625
        } catch (Exception $e) {
4626
            // Cleanup hanging transaction on error
4627
            $repository->rollback();
4628
            throw $e;
4629
        }
4630
4631
        // Rollback all changes.
4632
        $repository->rollback();
4633
4634
        // This array will be empty
4635
        $drafts = $contentService->loadContentDrafts();
4636
        /* END: Use Case */
4637
4638
        $this->assertSame(array(), $drafts);
4639
    }
4640
4641
    /**
4642
     * Test for the deleteVersion() method.
4643
     *
4644
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4645
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4646
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4647
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4648
     */
4649
    public function testDeleteVersionInTransactionWithCommit()
4650
    {
4651
        $repository = $this->getRepository();
4652
4653
        $contentId = $this->generateId('object', 12);
4654
        /* BEGIN: Use Case */
4655
        // $contentId is the ID of the "Administrator users" user group
4656
4657
        // Get the content service
4658
        $contentService = $repository->getContentService();
4659
4660
        // Start a new transaction
4661
        $repository->beginTransaction();
4662
4663
        try {
4664
            // Create a new draft
4665
            $draft = $contentService->createContentDraft(
4666
                $contentService->loadContentInfo($contentId)
4667
            );
4668
4669
            $contentService->deleteVersion($draft->getVersionInfo());
4670
4671
            // Commit all changes.
4672
            $repository->commit();
4673
        } catch (Exception $e) {
4674
            // Cleanup hanging transaction on error
4675
            $repository->rollback();
4676
            throw $e;
4677
        }
4678
4679
        // This array will contain no element
4680
        $drafts = $contentService->loadContentDrafts();
4681
        /* END: Use Case */
4682
4683
        $this->assertSame(array(), $drafts);
4684
    }
4685
4686
    /**
4687
     * Test for the deleteContent() method.
4688
     *
4689
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4690
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4691
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4692
     */
4693
    public function testDeleteContentInTransactionWithRollback()
4694
    {
4695
        $repository = $this->getRepository();
4696
4697
        $contentId = $this->generateId('object', 11);
4698
        /* BEGIN: Use Case */
4699
        // $contentId is the ID of the "Members" user group in an eZ Publish
4700
        // demo installation
4701
4702
        // Get content service
4703
        $contentService = $repository->getContentService();
4704
4705
        // Load a ContentInfo instance
4706
        $contentInfo = $contentService->loadContentInfo($contentId);
4707
4708
        // Start a new transaction
4709
        $repository->beginTransaction();
4710
4711
        try {
4712
            // Delete content object
4713
            $contentService->deleteContent($contentInfo);
4714
        } catch (Exception $e) {
4715
            // Cleanup hanging transaction on error
4716
            $repository->rollback();
4717
            throw $e;
4718
        }
4719
4720
        // Rollback all changes
4721
        $repository->rollback();
4722
4723
        // This call will return the original content object
4724
        $contentInfo = $contentService->loadContentInfo($contentId);
4725
        /* END: Use Case */
4726
4727
        $this->assertEquals($contentId, $contentInfo->id);
4728
    }
4729
4730
    /**
4731
     * Test for the deleteContent() method.
4732
     *
4733
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4734
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4735
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4736
     */
4737
    public function testDeleteContentInTransactionWithCommit()
4738
    {
4739
        $repository = $this->getRepository();
4740
4741
        $contentId = $this->generateId('object', 11);
4742
        /* BEGIN: Use Case */
4743
        // $contentId is the ID of the "Members" user group in an eZ Publish
4744
        // demo installation
4745
4746
        // Get content service
4747
        $contentService = $repository->getContentService();
4748
4749
        // Load a ContentInfo instance
4750
        $contentInfo = $contentService->loadContentInfo($contentId);
4751
4752
        // Start a new transaction
4753
        $repository->beginTransaction();
4754
4755
        try {
4756
            // Delete content object
4757
            $contentService->deleteContent($contentInfo);
4758
4759
            // Commit all changes
4760
            $repository->commit();
4761
        } catch (Exception $e) {
4762
            // Cleanup hanging transaction on error
4763
            $repository->rollback();
4764
            throw $e;
4765
        }
4766
4767
        // Deleted content info is not found anymore
4768
        try {
4769
            $contentService->loadContentInfo($contentId);
4770
        } catch (NotFoundException $e) {
4771
            return;
4772
        }
4773
        /* END: Use Case */
4774
4775
        $this->fail('Can still load ContentInfo after commit.');
4776
    }
4777
4778
    /**
4779
     * Test for the copyContent() method.
4780
     *
4781
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4782
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4783
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4784
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4785
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4786
     */
4787 View Code Duplication
    public function testCopyContentInTransactionWithRollback()
4788
    {
4789
        $repository = $this->getRepository();
4790
4791
        $contentId = $this->generateId('object', 11);
4792
        $locationId = $this->generateId('location', 13);
4793
        /* BEGIN: Use Case */
4794
        // $contentId is the ID of the "Members" user group in an eZ Publish
4795
        // demo installation
4796
4797
        // $locationId is the ID of the "Administrator users" group location
4798
4799
        // Get services
4800
        $contentService = $repository->getContentService();
4801
        $locationService = $repository->getLocationService();
4802
4803
        // Load content object to copy
4804
        $content = $contentService->loadContent($contentId);
4805
4806
        // Create new target location
4807
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4808
4809
        // Start a new transaction
4810
        $repository->beginTransaction();
4811
4812
        try {
4813
            // Copy content with all versions and drafts
4814
            $contentService->copyContent(
4815
                $content->contentInfo,
4816
                $locationCreate
4817
            );
4818
        } catch (Exception $e) {
4819
            // Cleanup hanging transaction on error
4820
            $repository->rollback();
4821
            throw $e;
4822
        }
4823
4824
        // Rollback all changes
4825
        $repository->rollback();
4826
4827
        $this->refreshSearch($repository);
4828
4829
        // This array will only contain a single admin user object
4830
        $locations = $locationService->loadLocationChildren(
4831
            $locationService->loadLocation($locationId)
4832
        )->locations;
4833
        /* END: Use Case */
4834
4835
        $this->assertEquals(1, count($locations));
4836
    }
4837
4838
    /**
4839
     * Test for the copyContent() method.
4840
     *
4841
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4842
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4843
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4844
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4845
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4846
     */
4847 View Code Duplication
    public function testCopyContentInTransactionWithCommit()
4848
    {
4849
        $repository = $this->getRepository();
4850
4851
        $contentId = $this->generateId('object', 11);
4852
        $locationId = $this->generateId('location', 13);
4853
        /* BEGIN: Use Case */
4854
        // $contentId is the ID of the "Members" user group in an eZ Publish
4855
        // demo installation
4856
4857
        // $locationId is the ID of the "Administrator users" group location
4858
4859
        // Get services
4860
        $contentService = $repository->getContentService();
4861
        $locationService = $repository->getLocationService();
4862
4863
        // Load content object to copy
4864
        $content = $contentService->loadContent($contentId);
4865
4866
        // Create new target location
4867
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4868
4869
        // Start a new transaction
4870
        $repository->beginTransaction();
4871
4872
        try {
4873
            // Copy content with all versions and drafts
4874
            $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...
4875
                $content->contentInfo,
4876
                $locationCreate
4877
            );
4878
4879
            // Commit all changes
4880
            $repository->commit();
4881
        } catch (Exception $e) {
4882
            // Cleanup hanging transaction on error
4883
            $repository->rollback();
4884
            throw $e;
4885
        }
4886
4887
        $this->refreshSearch($repository);
4888
4889
        // This will contain the admin user and the new child location
4890
        $locations = $locationService->loadLocationChildren(
4891
            $locationService->loadLocation($locationId)
4892
        )->locations;
4893
        /* END: Use Case */
4894
4895
        $this->assertEquals(2, count($locations));
4896
    }
4897
4898
    public function testURLAliasesCreatedForNewContent()
4899
    {
4900
        $repository = $this->getRepository();
4901
4902
        $contentService = $repository->getContentService();
4903
        $locationService = $repository->getLocationService();
4904
        $urlAliasService = $repository->getURLAliasService();
4905
4906
        /* BEGIN: Use Case */
4907
        $draft = $this->createContentDraftVersion1();
4908
4909
        // Automatically creates a new URLAlias for the content
4910
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
4911
        /* END: Use Case */
4912
4913
        $location = $locationService->loadLocation(
4914
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4915
        );
4916
4917
        $aliases = $urlAliasService->listLocationAliases($location, false);
4918
4919
        $this->assertAliasesCorrect(
4920
            array(
4921
                '/Design/Plain-site/An-awesome-forum' => array(
4922
                    'type' => URLAlias::LOCATION,
4923
                    'destination' => $location->id,
4924
                    'path' => '/Design/Plain-site/An-awesome-forum',
4925
                    'languageCodes' => array('eng-US'),
4926
                    'isHistory' => false,
4927
                    'isCustom' => false,
4928
                    'forward' => false,
4929
                ),
4930
            ),
4931
            $aliases
4932
        );
4933
    }
4934
4935
    public function testURLAliasesCreatedForUpdatedContent()
4936
    {
4937
        $repository = $this->getRepository();
4938
4939
        $contentService = $repository->getContentService();
4940
        $locationService = $repository->getLocationService();
4941
        $urlAliasService = $repository->getURLAliasService();
4942
4943
        /* BEGIN: Use Case */
4944
        $draft = $this->createUpdatedDraftVersion2();
4945
4946
        $location = $locationService->loadLocation(
4947
            $draft->getVersionInfo()->getContentInfo()->mainLocationId
4948
        );
4949
4950
        // Load and assert URL aliases before publishing updated Content, so that
4951
        // SPI cache is warmed up and cache invalidation is also tested.
4952
        $aliases = $urlAliasService->listLocationAliases($location, false);
4953
4954
        $this->assertAliasesCorrect(
4955
            array(
4956
                '/Design/Plain-site/An-awesome-forum' => array(
4957
                    'type' => URLAlias::LOCATION,
4958
                    'destination' => $location->id,
4959
                    'path' => '/Design/Plain-site/An-awesome-forum',
4960
                    'languageCodes' => array('eng-US'),
4961
                    'alwaysAvailable' => true,
4962
                    'isHistory' => false,
4963
                    'isCustom' => false,
4964
                    'forward' => false,
4965
                ),
4966
            ),
4967
            $aliases
4968
        );
4969
4970
        // Automatically marks old aliases for the content as history
4971
        // and creates new aliases, based on the changes
4972
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
4973
        /* END: Use Case */
4974
4975
        $location = $locationService->loadLocation(
4976
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4977
        );
4978
4979
        $aliases = $urlAliasService->listLocationAliases($location, false);
4980
4981
        $this->assertAliasesCorrect(
4982
            array(
4983
                '/Design/Plain-site/An-awesome-forum2' => array(
4984
                    'type' => URLAlias::LOCATION,
4985
                    'destination' => $location->id,
4986
                    'path' => '/Design/Plain-site/An-awesome-forum2',
4987
                    'languageCodes' => array('eng-US'),
4988
                    'alwaysAvailable' => true,
4989
                    'isHistory' => false,
4990
                    'isCustom' => false,
4991
                    'forward' => false,
4992
                ),
4993
                '/Design/Plain-site/An-awesome-forum23' => array(
4994
                    'type' => URLAlias::LOCATION,
4995
                    'destination' => $location->id,
4996
                    'path' => '/Design/Plain-site/An-awesome-forum23',
4997
                    'languageCodes' => array('eng-GB'),
4998
                    'alwaysAvailable' => true,
4999
                    'isHistory' => false,
5000
                    'isCustom' => false,
5001
                    'forward' => false,
5002
                ),
5003
            ),
5004
            $aliases
5005
        );
5006
    }
5007
5008
    public function testCustomURLAliasesNotHistorizedOnUpdatedContent()
5009
    {
5010
        $repository = $this->getRepository();
5011
5012
        $contentService = $repository->getContentService();
5013
5014
        /* BEGIN: Use Case */
5015
        $urlAliasService = $repository->getURLAliasService();
5016
        $locationService = $repository->getLocationService();
5017
5018
        $content = $this->createContentVersion1();
5019
5020
        // Create a custom URL alias
5021
        $urlAliasService->createUrlAlias(
5022
            $locationService->loadLocation(
5023
                $content->getVersionInfo()->getContentInfo()->mainLocationId
5024
            ),
5025
            '/my/fancy/story-about-ez-publish',
5026
            'eng-US'
5027
        );
5028
5029
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
5030
5031
        $contentUpdate = $contentService->newContentUpdateStruct();
5032
        $contentUpdate->initialLanguageCode = 'eng-US';
5033
        $contentUpdate->setField('name', 'Amazing Bielefeld forum');
5034
5035
        $draftVersion2 = $contentService->updateContent(
5036
            $draftVersion2->getVersionInfo(),
5037
            $contentUpdate
5038
        );
5039
5040
        // Only marks auto-generated aliases as history
5041
        // the custom one is left untouched
5042
        $liveContent = $contentService->publishVersion($draftVersion2->getVersionInfo());
5043
        /* END: Use Case */
5044
5045
        $location = $locationService->loadLocation(
5046
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
5047
        );
5048
5049
        $aliases = $urlAliasService->listLocationAliases($location);
5050
5051
        $this->assertAliasesCorrect(
5052
            array(
5053
                '/my/fancy/story-about-ez-publish' => array(
5054
                    'type' => URLAlias::LOCATION,
5055
                    'destination' => $location->id,
5056
                    'path' => '/my/fancy/story-about-ez-publish',
5057
                    'languageCodes' => array('eng-US'),
5058
                    'isHistory' => false,
5059
                    'isCustom' => true,
5060
                    'forward' => false,
5061
                    'alwaysAvailable' => false,
5062
                ),
5063
            ),
5064
            $aliases
5065
        );
5066
    }
5067
5068
    /**
5069
     * Test to ensure that old versions are not affected by updates to newer
5070
     * drafts.
5071
     */
5072
    public function testUpdatingDraftDoesNotUpdateOldVersions()
5073
    {
5074
        $repository = $this->getRepository();
5075
5076
        $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...
5077
5078
        $contentService = $repository->getContentService();
5079
5080
        $contentVersion2 = $this->createContentVersion2();
5081
5082
        $loadedContent1 = $contentService->loadContent($contentVersion2->id, null, 1);
5083
        $loadedContent2 = $contentService->loadContent($contentVersion2->id, null, 2);
5084
5085
        $this->assertNotEquals(
5086
            $loadedContent1->getFieldValue('name', 'eng-US'),
5087
            $loadedContent2->getFieldValue('name', 'eng-US')
5088
        );
5089
    }
5090
5091
    /**
5092
     * Test scenario with writer and publisher users.
5093
     * Writer can only create content. Publisher can publish this content.
5094
     */
5095
    public function testPublishWorkflow()
5096
    {
5097
        $repository = $this->getRepository();
5098
        $contentService = $repository->getContentService();
5099
5100
        $this->createRoleWithPolicies('Publisher', [
5101
            ['content', 'read'],
5102
            ['content', 'create'],
5103
            ['content', 'publish'],
5104
        ]);
5105
5106
        $this->createRoleWithPolicies('Writer', [
5107
            ['content', 'read'],
5108
            ['content', 'create'],
5109
        ]);
5110
5111
        $writerUser = $this->createCustomUserWithLogin(
5112
            'writer',
5113
            '[email protected]',
5114
            'Writers',
5115
            'Writer'
5116
        );
5117
5118
        $publisherUser = $this->createCustomUserWithLogin(
5119
            'publisher',
5120
            '[email protected]',
5121
            'Publishers',
5122
            'Publisher'
5123
        );
5124
5125
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5126
        $draft = $this->createContentDraftVersion1();
5127
5128
        $repository->getPermissionResolver()->setCurrentUserReference($publisherUser);
5129
        $content = $contentService->publishVersion($draft->versionInfo);
5130
5131
        $contentService->loadContent($content->id);
5132
    }
5133
5134
    /**
5135
     * Test publish / content policy is required to be able to publish content.
5136
     *
5137
     * @expectedException \eZ\Publish\Core\Base\Exceptions\UnauthorizedException
5138
     * @expectedExceptionMessageRegExp /User does not have access to 'publish' 'content'/
5139
     */
5140
    public function testPublishContentWithoutPublishPolicyThrowsException()
5141
    {
5142
        $repository = $this->getRepository();
5143
5144
        $this->createRoleWithPolicies('Writer', [
5145
            ['content', 'read'],
5146
            ['content', 'create'],
5147
            ['content', 'edit'],
5148
        ]);
5149
        $writerUser = $this->createCustomUserWithLogin(
5150
            'writer',
5151
            '[email protected]',
5152
            'Writers',
5153
            'Writer'
5154
        );
5155
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5156
5157
        $this->createContentVersion1();
5158
    }
5159
5160
    /**
5161
     * Test for the newTranslationInfo() method.
5162
     *
5163
     * @covers \eZ\Publish\Core\Repository\ContentService::newTranslationInfo
5164
     */
5165
    public function testNewTranslationInfo()
5166
    {
5167
        $repository = $this->getRepository();
5168
        $contentService = $repository->getContentService();
5169
5170
        $translationInfo = $contentService->newTranslationInfo();
5171
5172
        $this->assertInstanceOf(
5173
            TranslationInfo::class,
5174
            $translationInfo
5175
        );
5176
5177
        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...
5178
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
5179
        }
5180
    }
5181
5182
    /**
5183
     * Simplify creating custom role with limited set of policies.
5184
     *
5185
     * @param $roleName
5186
     * @param array $policies e.g. [ ['content', 'create'], ['content', 'edit'], ]
5187
     */
5188
    private function createRoleWithPolicies($roleName, array $policies)
5189
    {
5190
        $repository = $this->getRepository();
5191
        $roleService = $repository->getRoleService();
5192
5193
        $roleCreateStruct = $roleService->newRoleCreateStruct($roleName);
5194
        foreach ($policies as $policy) {
5195
            $policyCreateStruct = $roleService->newPolicyCreateStruct($policy[0], $policy[1]);
5196
            $roleCreateStruct->addPolicy($policyCreateStruct);
5197
        }
5198
5199
        $roleDraft = $roleService->createRole($roleCreateStruct);
5200
        $roleService->publishRoleDraft($roleDraft);
5201
    }
5202
5203
    /**
5204
     * Asserts that all aliases defined in $expectedAliasProperties with the
5205
     * given properties are available in $actualAliases and not more.
5206
     *
5207
     * @param array $expectedAliasProperties
5208
     * @param array $actualAliases
5209
     */
5210
    private function assertAliasesCorrect(array $expectedAliasProperties, array $actualAliases)
5211
    {
5212
        foreach ($actualAliases as $actualAlias) {
5213
            if (!isset($expectedAliasProperties[$actualAlias->path])) {
5214
                $this->fail(
5215
                    sprintf(
5216
                        'Alias with path "%s" in languages "%s" not expected.',
5217
                        $actualAlias->path,
5218
                        implode(', ', $actualAlias->languageCodes)
5219
                    )
5220
                );
5221
            }
5222
5223
            foreach ($expectedAliasProperties[$actualAlias->path] as $propertyName => $propertyValue) {
5224
                $this->assertEquals(
5225
                    $propertyValue,
5226
                    $actualAlias->$propertyName,
5227
                    sprintf(
5228
                        'Property $%s incorrect on alias with path "%s" in languages "%s".',
5229
                        $propertyName,
5230
                        $actualAlias->path,
5231
                        implode(', ', $actualAlias->languageCodes)
5232
                    )
5233
                );
5234
            }
5235
5236
            unset($expectedAliasProperties[$actualAlias->path]);
5237
        }
5238
5239
        if (!empty($expectedAliasProperties)) {
5240
            $this->fail(
5241
                sprintf(
5242
                    'Missing expected aliases with paths "%s".',
5243
                    implode('", "', array_keys($expectedAliasProperties))
5244
                )
5245
            );
5246
        }
5247
    }
5248
5249
    /**
5250
     * Asserts that the given fields are equal to the default fields fixture.
5251
     *
5252
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5253
     */
5254
    private function assertAllFieldsEquals(array $fields)
5255
    {
5256
        $actual = $this->normalizeFields($fields);
5257
        $expected = $this->normalizeFields($this->createFieldsFixture());
5258
5259
        $this->assertEquals($expected, $actual);
5260
    }
5261
5262
    /**
5263
     * Asserts that the given fields are equal to a language filtered set of the
5264
     * default fields fixture.
5265
     *
5266
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5267
     * @param string $languageCode
5268
     */
5269
    private function assertLocaleFieldsEquals(array $fields, $languageCode)
5270
    {
5271
        $actual = $this->normalizeFields($fields);
5272
5273
        $expected = array();
5274
        foreach ($this->normalizeFields($this->createFieldsFixture()) as $field) {
5275
            if ($field->languageCode !== $languageCode) {
5276
                continue;
5277
            }
5278
            $expected[] = $field;
5279
        }
5280
5281
        $this->assertEquals($expected, $actual);
5282
    }
5283
5284
    /**
5285
     * This method normalizes a set of fields and returns a normalized set.
5286
     *
5287
     * Normalization means it resets the storage specific field id to zero and
5288
     * it sorts the field by their identifier and their language code. In
5289
     * addition, the field value is removed, since this one depends on the
5290
     * specific FieldType, which is tested in a dedicated integration test.
5291
     *
5292
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5293
     *
5294
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5295
     */
5296
    private function normalizeFields(array $fields)
5297
    {
5298
        $normalized = array();
5299 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...
5300
            $normalized[] = new Field(
5301
                array(
5302
                    'id' => 0,
5303
                    '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...
5304
                    'languageCode' => $field->languageCode,
5305
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
5306
                )
5307
            );
5308
        }
5309
        usort(
5310
            $normalized,
5311 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...
5312
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
5313
                    return strcasecmp($field1->languageCode, $field2->languageCode);
5314
                }
5315
5316
                return $return;
5317
            }
5318
        );
5319
5320
        return $normalized;
5321
    }
5322
5323
    /**
5324
     * Returns a filtered set of the default fields fixture.
5325
     *
5326
     * @param string $languageCode
5327
     *
5328
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5329
     */
5330
    private function createLocaleFieldsFixture($languageCode)
5331
    {
5332
        $fields = array();
5333
        foreach ($this->createFieldsFixture() as $field) {
5334
            if (null === $field->languageCode || $languageCode === $field->languageCode) {
5335
                $fields[] = $field;
5336
            }
5337
        }
5338
5339
        return $fields;
5340
    }
5341
5342
    /**
5343
     * Asserts that given Content has default ContentStates.
5344
     *
5345
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
5346
     */
5347 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
5348
    {
5349
        $repository = $this->getRepository();
5350
        $objectStateService = $repository->getObjectStateService();
5351
5352
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
5353
5354
        foreach ($objectStateGroups as $objectStateGroup) {
5355
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
5356
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
5357
                // Only check the first object state which is the default one.
5358
                $this->assertEquals(
5359
                    $objectState,
5360
                    $contentState
5361
                );
5362
                break;
5363
            }
5364
        }
5365
    }
5366
5367
    /**
5368
     * Returns the default fixture of fields used in most tests.
5369
     *
5370
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5371
     */
5372
    private function createFieldsFixture()
5373
    {
5374
        return array(
5375
            new Field(
5376
                array(
5377
                    'id' => 0,
5378
                    'value' => 'Foo',
5379
                    'languageCode' => 'eng-US',
5380
                    'fieldDefIdentifier' => 'description',
5381
                )
5382
            ),
5383
            new Field(
5384
                array(
5385
                    'id' => 0,
5386
                    'value' => 'Bar',
5387
                    'languageCode' => 'eng-GB',
5388
                    'fieldDefIdentifier' => 'description',
5389
                )
5390
            ),
5391
            new Field(
5392
                array(
5393
                    'id' => 0,
5394
                    'value' => 'An awesome multi-lang forum²',
5395
                    'languageCode' => 'eng-US',
5396
                    'fieldDefIdentifier' => 'name',
5397
                )
5398
            ),
5399
            new Field(
5400
                array(
5401
                    'id' => 0,
5402
                    'value' => 'An awesome multi-lang forum²³',
5403
                    'languageCode' => 'eng-GB',
5404
                    'fieldDefIdentifier' => 'name',
5405
                )
5406
            ),
5407
        );
5408
    }
5409
5410
    /**
5411
     * Gets expected property values for the "Media" ContentInfo ValueObject.
5412
     *
5413
     * @return array
5414
     */
5415 View Code Duplication
    private function getExpectedMediaContentInfoProperties()
5416
    {
5417
        return [
5418
            'id' => 41,
5419
            'contentTypeId' => 1,
5420
            'name' => 'Media',
5421
            'sectionId' => 3,
5422
            'currentVersionNo' => 1,
5423
            'published' => true,
5424
            'ownerId' => 14,
5425
            'modificationDate' => $this->createDateTime(1060695457),
5426
            'publishedDate' => $this->createDateTime(1060695457),
5427
            'alwaysAvailable' => 1,
5428
            'remoteId' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
5429
            'mainLanguageCode' => 'eng-US',
5430
            'mainLocationId' => 43,
5431
        ];
5432
    }
5433
}
5434