Completed
Push — master ( ac8ea3...0c3653 )
by André
17:37
created

testCreateContentDraftSetsVersionInfo()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 1
dl 0
loc 24
rs 8.9713
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
    public function testLoadVersionInfo()
623
    {
624
        $repository = $this->getRepository();
625
626
        $mediaFolderId = $this->generateId('object', 41);
627
        /* BEGIN: Use Case */
628
        // $mediaFolderId contains the ID of the "Media" folder
629
630
        $contentService = $repository->getContentService();
631
632
        // Load the ContentInfo for "Media" folder
633
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
634
635
        // Now load the current version info of the "Media" folder
636
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
637
        /* END: Use Case */
638
639
        $this->assertInstanceOf(
640
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
641
            $versionInfo
642
        );
643
    }
644
645
    /**
646
     * Test for the loadVersionInfoById() method.
647
     *
648
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
649
     */
650 View Code Duplication
    public function testLoadVersionInfoById()
651
    {
652
        $repository = $this->getRepository();
653
654
        $mediaFolderId = $this->generateId('object', 41);
655
        /* BEGIN: Use Case */
656
        // $mediaFolderId contains the ID of the "Media" folder
657
658
        $contentService = $repository->getContentService();
659
660
        // Load the VersionInfo for "Media" folder
661
        $versionInfo = $contentService->loadVersionInfoById($mediaFolderId);
662
        /* END: Use Case */
663
664
        $this->assertInstanceOf(
665
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
666
            $versionInfo
667
        );
668
669
        return $versionInfo;
670
    }
671
672
    /**
673
     * Test for the returned value of the loadVersionInfoById() method.
674
     *
675
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
676
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
677
     *
678
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
679
     */
680
    public function testLoadVersionInfoByIdSetsExpectedVersionInfo(VersionInfo $versionInfo)
681
    {
682
        $this->assertPropertiesCorrect(
683
            [
684
                'names' => [
685
                    'eng-US' => 'Media',
686
                ],
687
                'contentInfo' => new ContentInfo($this->getExpectedMediaContentInfoProperties()),
688
                'id' => 472,
689
                'versionNo' => 1,
690
                'modificationDate' => $this->createDateTime(1060695457),
691
                'creatorId' => 14,
692
                'creationDate' => $this->createDateTime(1060695450),
693
                'status' => VersionInfo::STATUS_PUBLISHED,
694
                'initialLanguageCode' => 'eng-US',
695
                'languageCodes' => [
696
                    'eng-US',
697
                ],
698
            ],
699
            $versionInfo
700
        );
701
        $this->assertTrue($versionInfo->isPublished());
702
        $this->assertFalse($versionInfo->isDraft());
703
        $this->assertFalse($versionInfo->isArchived());
704
    }
705
706
    /**
707
     * Test for the loadVersionInfoById() method.
708
     *
709
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
710
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
711
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
712
     */
713 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundException()
714
    {
715
        $repository = $this->getRepository();
716
717
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
718
        /* BEGIN: Use Case */
719
        $contentService = $repository->getContentService();
720
721
        // This call will fail with a "NotFoundException"
722
        $contentService->loadVersionInfoById($nonExistentContentId);
723
        /* END: Use Case */
724
    }
725
726
    /**
727
     * Test for the loadContentByContentInfo() method.
728
     *
729
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
730
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
731
     */
732
    public function testLoadContentByContentInfo()
733
    {
734
        $repository = $this->getRepository();
735
736
        $mediaFolderId = $this->generateId('object', 41);
737
        /* BEGIN: Use Case */
738
        // $mediaFolderId contains the ID of the "Media" folder
739
740
        $contentService = $repository->getContentService();
741
742
        // Load the ContentInfo for "Media" folder
743
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
744
745
        // Now load the current content version for the info instance
746
        $content = $contentService->loadContentByContentInfo($contentInfo);
747
        /* END: Use Case */
748
749
        $this->assertInstanceOf(
750
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
751
            $content
752
        );
753
    }
754
755
    /**
756
     * Test for the loadContentByVersionInfo() method.
757
     *
758
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
759
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
760
     */
761
    public function testLoadContentByVersionInfo()
762
    {
763
        $repository = $this->getRepository();
764
765
        $mediaFolderId = $this->generateId('object', 41);
766
        /* BEGIN: Use Case */
767
        // $mediaFolderId contains the ID of the "Media" folder
768
769
        $contentService = $repository->getContentService();
770
771
        // Load the ContentInfo for "Media" folder
772
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
773
774
        // Load the current VersionInfo
775
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
776
777
        // Now load the current content version for the info instance
778
        $content = $contentService->loadContentByVersionInfo($versionInfo);
779
        /* END: Use Case */
780
781
        $this->assertInstanceOf(
782
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
783
            $content
784
        );
785
    }
786
787
    /**
788
     * Test for the loadContent() method.
789
     *
790
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
791
     * @group user
792
     * @group field-type
793
     */
794 View Code Duplication
    public function testLoadContent()
795
    {
796
        $repository = $this->getRepository();
797
798
        $mediaFolderId = $this->generateId('object', 41);
799
        /* BEGIN: Use Case */
800
        // $mediaFolderId contains the ID of the "Media" folder
801
802
        $contentService = $repository->getContentService();
803
804
        // Load the Content for "Media" folder, any language and current version
805
        $content = $contentService->loadContent($mediaFolderId);
806
        /* END: Use Case */
807
808
        $this->assertInstanceOf(
809
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
810
            $content
811
        );
812
    }
813
814
    /**
815
     * Test for the loadContent() method.
816
     *
817
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
818
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
819
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
820
     */
821 View Code Duplication
    public function testLoadContentThrowsNotFoundException()
822
    {
823
        $repository = $this->getRepository();
824
825
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
826
        /* BEGIN: Use Case */
827
        $contentService = $repository->getContentService();
828
829
        // This call will fail with a "NotFoundException"
830
        $contentService->loadContent($nonExistentContentId);
831
        /* END: Use Case */
832
    }
833
834
    /**
835
     * Data provider for testLoadContentByRemoteId().
836
     *
837
     * @return array
838
     */
839
    public function contentRemoteIdVersionLanguageProvider()
840
    {
841
        return [
842
            ['f5c88a2209584891056f987fd965b0ba', null, null],
843
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], null],
844
            ['f5c88a2209584891056f987fd965b0ba', null, 1],
845
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], 1],
846
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, null],
847
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], null],
848
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, 1],
849
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], 1],
850
        ];
851
    }
852
853
    /**
854
     * Test for the loadContentByRemoteId() method.
855
     *
856
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId
857
     * @dataProvider contentRemoteIdVersionLanguageProvider
858
     *
859
     * @param string $remoteId
860
     * @param array|null $languages
861
     * @param int $versionNo
862
     */
863
    public function testLoadContentByRemoteId($remoteId, $languages, $versionNo)
864
    {
865
        $repository = $this->getRepository();
866
867
        $contentService = $repository->getContentService();
868
869
        $content = $contentService->loadContentByRemoteId($remoteId, $languages, $versionNo);
870
871
        $this->assertInstanceOf(
872
            Content::class,
873
            $content
874
        );
875
876
        $this->assertEquals($remoteId, $content->contentInfo->remoteId);
877
        if ($languages !== null) {
878
            $this->assertEquals($languages, $content->getVersionInfo()->languageCodes);
879
        }
880
        $this->assertEquals($versionNo ?: 1, $content->getVersionInfo()->versionNo);
881
    }
882
883
    /**
884
     * Test for the loadContentByRemoteId() method.
885
     *
886
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
887
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
888
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
889
     */
890
    public function testLoadContentByRemoteIdThrowsNotFoundException()
891
    {
892
        $repository = $this->getRepository();
893
894
        /* BEGIN: Use Case */
895
        $contentService = $repository->getContentService();
896
897
        // This call will fail with a "NotFoundException", because no content
898
        // object exists for the given remoteId
899
        $contentService->loadContentByRemoteId('a1b1c1d1e1f1a2b2c2d2e2f2a3b3c3d3');
900
        /* END: Use Case */
901
    }
902
903
    /**
904
     * Test for the publishVersion() method.
905
     *
906
     * @return \eZ\Publish\API\Repository\Values\Content\Content
907
     *
908
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
909
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
910
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
911
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
912
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
913
     * @group user
914
     * @group field-type
915
     */
916
    public function testPublishVersion()
917
    {
918
        $time = time();
919
        /* BEGIN: Use Case */
920
        $content = $this->createContentVersion1();
921
        /* END: Use Case */
922
923
        $this->assertInstanceOf(Content::class, $content);
924
        $this->assertTrue($content->contentInfo->published);
925
        $this->assertEquals(VersionInfo::STATUS_PUBLISHED, $content->versionInfo->status);
926
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->publishedDate->getTimestamp());
927
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->modificationDate->getTimestamp());
928
        $this->assertTrue($content->versionInfo->isPublished());
929
        $this->assertFalse($content->versionInfo->isDraft());
930
        $this->assertFalse($content->versionInfo->isArchived());
931
932
        return $content;
933
    }
934
935
    /**
936
     * Test for the publishVersion() method.
937
     *
938
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
939
     *
940
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
941
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
942
     */
943
    public function testPublishVersionSetsExpectedContentInfo($content)
944
    {
945
        $this->assertEquals(
946
            array(
947
                $content->id,
948
                true,
949
                1,
950
                'abcdef0123456789abcdef0123456789',
951
                'eng-US',
952
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading history...
1292
    {
1293
        $repository = $this->getRepository();
1294
1295
        $contentService = $repository->getContentService();
1296
1297
        /* BEGIN: Use Case */
1298
        $content = $this->createContentVersion1();
1299
1300
        // Now we create a new draft from the published content
1301
        $contentService->createContentDraft($content->contentInfo);
1302
1303
        // This call will still load the published version
1304
        $versionInfoPublished = $contentService->loadVersionInfo($content->contentInfo);
1305
        /* END: Use Case */
1306
1307
        $this->assertEquals(1, $versionInfoPublished->versionNo);
1308
    }
1309
1310
    /**
1311
     * Test for the createContentDraft() method.
1312
     *
1313
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1314
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
1315
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1316
     */
1317
    public function testCreateContentDraftLoadContentStillLoadsPublishedVersion()
1318
    {
1319
        $repository = $this->getRepository();
1320
1321
        $contentService = $repository->getContentService();
1322
1323
        /* BEGIN: Use Case */
1324
        $content = $this->createContentVersion1();
1325
1326
        // Now we create a new draft from the published content
1327
        $contentService->createContentDraft($content->contentInfo);
1328
1329
        // This call will still load the published content version
1330
        $contentPublished = $contentService->loadContent($content->id);
1331
        /* END: Use Case */
1332
1333
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1334
    }
1335
1336
    /**
1337
     * Test for the createContentDraft() method.
1338
     *
1339
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1340
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
1341
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1342
     */
1343
    public function testCreateContentDraftLoadContentByRemoteIdStillLoadsPublishedVersion()
1344
    {
1345
        $repository = $this->getRepository();
1346
1347
        $contentService = $repository->getContentService();
1348
1349
        /* BEGIN: Use Case */
1350
        $content = $this->createContentVersion1();
1351
1352
        // Now we create a new draft from the published content
1353
        $contentService->createContentDraft($content->contentInfo);
1354
1355
        // This call will still load the published content version
1356
        $contentPublished = $contentService->loadContentByRemoteId('abcdef0123456789abcdef0123456789');
1357
        /* END: Use Case */
1358
1359
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1360
    }
1361
1362
    /**
1363
     * Test for the createContentDraft() method.
1364
     *
1365
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1366
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
1367
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1368
     */
1369
    public function testCreateContentDraftLoadContentByContentInfoStillLoadsPublishedVersion()
1370
    {
1371
        $repository = $this->getRepository();
1372
1373
        $contentService = $repository->getContentService();
1374
1375
        /* BEGIN: Use Case */
1376
        $content = $this->createContentVersion1();
1377
1378
        // Now we create a new draft from the published content
1379
        $contentService->createContentDraft($content->contentInfo);
1380
1381
        // This call will still load the published content version
1382
        $contentPublished = $contentService->loadContentByContentInfo($content->contentInfo);
1383
        /* END: Use Case */
1384
1385
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1386
    }
1387
1388
    /**
1389
     * Test for the newContentUpdateStruct() method.
1390
     *
1391
     * @covers \eZ\Publish\API\Repository\ContentService::newContentUpdateStruct
1392
     * @group user
1393
     */
1394
    public function testNewContentUpdateStruct()
1395
    {
1396
        $repository = $this->getRepository();
1397
1398
        /* BEGIN: Use Case */
1399
        $contentService = $repository->getContentService();
1400
1401
        $updateStruct = $contentService->newContentUpdateStruct();
1402
        /* END: Use Case */
1403
1404
        $this->assertInstanceOf(
1405
            ContentUpdateStruct::class,
1406
            $updateStruct
1407
        );
1408
1409
        $this->assertPropertiesCorrect(
1410
            [
1411
                'initialLanguageCode' => null,
1412
                'fields' => [],
1413
            ],
1414
            $updateStruct
1415
        );
1416
    }
1417
1418
    /**
1419
     * Test for the updateContent() method.
1420
     *
1421
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1422
     *
1423
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1424
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1425
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1426
     * @group user
1427
     * @group field-type
1428
     */
1429
    public function testUpdateContent()
1430
    {
1431
        /* BEGIN: Use Case */
1432
        $draftVersion2 = $this->createUpdatedDraftVersion2();
1433
        /* END: Use Case */
1434
1435
        $this->assertInstanceOf(
1436
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1437
            $draftVersion2
1438
        );
1439
1440
        $this->assertEquals(
1441
            $this->generateId('user', 10),
1442
            $draftVersion2->versionInfo->creatorId,
1443
            'creatorId is not properly set on new Version'
1444
        );
1445
1446
        return $draftVersion2;
1447
    }
1448
1449
    /**
1450
     * Test for the updateContent_WithDifferentUser() method.
1451
     *
1452
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1453
     *
1454
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1455
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1456
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1457
     * @group user
1458
     * @group field-type
1459
     */
1460
    public function testUpdateContentWithDifferentUser()
1461
    {
1462
        /* BEGIN: Use Case */
1463
        $arrayWithDraftVersion2 = $this->createUpdatedDraftVersion2NotAdmin();
1464
        /* END: Use Case */
1465
1466
        $this->assertInstanceOf(
1467
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1468
            $arrayWithDraftVersion2[0]
1469
        );
1470
1471
        $this->assertEquals(
1472
            $this->generateId('user', $arrayWithDraftVersion2[1]),
1473
            $arrayWithDraftVersion2[0]->versionInfo->creatorId,
1474
            'creatorId is not properly set on new Version'
1475
        );
1476
1477
        return $arrayWithDraftVersion2[0];
1478
    }
1479
1480
    /**
1481
     * Test for the updateContent() method.
1482
     *
1483
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1484
     *
1485
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1486
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1487
     */
1488
    public function testUpdateContentSetsExpectedFields($content)
1489
    {
1490
        $actual = $this->normalizeFields($content->getFields());
1491
1492
        $expected = array(
1493
            new Field(
1494
                array(
1495
                    'id' => 0,
1496
                    'value' => true,
1497
                    'languageCode' => 'eng-GB',
1498
                    'fieldDefIdentifier' => 'description',
1499
                )
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
    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
    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
        $name = $content->getVersionInfo()->getName();
2936
        /** @var \eZ\Publish\Core\FieldType\TextLine\Value $nameValue */
2937
        self::assertEquals($expectedName, $nameValue->text);
2938
        self::assertEquals($expectedName, $name);
2939
    }
2940
2941
    /**
2942
     * @return array
2943
     */
2944
    public function getPrioritizedLanguageList()
2945
    {
2946
        return [
2947
            [['eng-US']],
2948
            [['eng-GB']],
2949
            [['eng-GB', 'eng-US']],
2950
            [['eng-US', 'eng-GB']],
2951
        ];
2952
    }
2953
2954
    /**
2955
     * Test for the deleteVersion() method.
2956
     *
2957
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2958
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2959
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2960
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2961
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2962
     */
2963
    public function testDeleteVersion()
2964
    {
2965
        $repository = $this->getRepository();
2966
2967
        $contentService = $repository->getContentService();
2968
2969
        /* BEGIN: Use Case */
2970
        $content = $this->createContentVersion1();
2971
2972
        // Create new draft, because published or last version of the Content can't be deleted
2973
        $draft = $contentService->createContentDraft(
2974
            $content->getVersionInfo()->getContentInfo()
2975
        );
2976
2977
        // Delete the previously created draft
2978
        $contentService->deleteVersion($draft->getVersionInfo());
2979
        /* END: Use Case */
2980
2981
        $versions = $contentService->loadVersions($content->getVersionInfo()->getContentInfo());
2982
2983
        $this->assertCount(1, $versions);
2984
        $this->assertEquals(
2985
            $content->getVersionInfo()->id,
2986
            $versions[0]->id
2987
        );
2988
    }
2989
2990
    /**
2991
     * Test for the deleteVersion() method.
2992
     *
2993
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2994
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
2995
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2996
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2997
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2998
     */
2999
    public function testDeleteVersionThrowsBadStateExceptionOnPublishedVersion()
3000
    {
3001
        $repository = $this->getRepository();
3002
3003
        $contentService = $repository->getContentService();
3004
3005
        /* BEGIN: Use Case */
3006
        $content = $this->createContentVersion1();
3007
3008
        // This call will fail with a "BadStateException", because the content
3009
        // version is currently published.
3010
        $contentService->deleteVersion($content->getVersionInfo());
3011
        /* END: Use Case */
3012
    }
3013
3014
    /**
3015
     * Test for the deleteVersion() method.
3016
     *
3017
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
3018
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3019
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3020
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3021
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3022
     */
3023 View Code Duplication
    public function testDeleteVersionThrowsBadStateExceptionOnLastVersion()
3024
    {
3025
        $repository = $this->getRepository();
3026
3027
        $contentService = $repository->getContentService();
3028
3029
        /* BEGIN: Use Case */
3030
        $draft = $this->createContentDraftVersion1();
3031
3032
        // This call will fail with a "BadStateException", because the Content
3033
        // version is the last version of the Content.
3034
        $contentService->deleteVersion($draft->getVersionInfo());
3035
        /* END: Use Case */
3036
    }
3037
3038
    /**
3039
     * Test for the loadVersions() method.
3040
     *
3041
     * @see \eZ\Publish\API\Repository\ContentService::loadVersions()
3042
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3043
     *
3044
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[]
3045
     */
3046
    public function testLoadVersions()
3047
    {
3048
        $repository = $this->getRepository();
3049
3050
        $contentService = $repository->getContentService();
3051
3052
        /* BEGIN: Use Case */
3053
        $contentVersion2 = $this->createContentVersion2();
3054
3055
        // Load versions of this ContentInfo instance
3056
        $versions = $contentService->loadVersions($contentVersion2->contentInfo);
3057
        /* END: Use Case */
3058
3059
        $expectedVersionsOrder = [
3060
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1),
3061
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 2),
3062
        ];
3063
3064
        $this->assertEquals($expectedVersionsOrder, $versions);
3065
3066
        return $versions;
3067
    }
3068
3069
    /**
3070
     * Test for the loadVersions() method.
3071
     *
3072
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersions
3073
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersions
3074
     *
3075
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo[] $versions
3076
     */
3077
    public function testLoadVersionsSetsExpectedVersionInfo(array $versions)
3078
    {
3079
        $this->assertCount(2, $versions);
3080
3081
        $expectedVersions = [
3082
            [
3083
                'versionNo' => 1,
3084
                'creatorId' => 14,
3085
                'status' => VersionInfo::STATUS_ARCHIVED,
3086
                'initialLanguageCode' => 'eng-US',
3087
                'languageCodes' => ['eng-US'],
3088
            ],
3089
            [
3090
                'versionNo' => 2,
3091
                'creatorId' => 10,
3092
                'status' => VersionInfo::STATUS_PUBLISHED,
3093
                'initialLanguageCode' => 'eng-US',
3094
                'languageCodes' => ['eng-US', 'eng-GB'],
3095
            ],
3096
        ];
3097
3098
        $this->assertPropertiesCorrect($expectedVersions[0], $versions[0]);
3099
        $this->assertPropertiesCorrect($expectedVersions[1], $versions[1]);
3100
        $this->assertEquals(
3101
            $versions[0]->creationDate->getTimestamp(),
3102
            $versions[1]->creationDate->getTimestamp(),
3103
            'Creation time did not match within delta of 2 seconds',
3104
            2
3105
        );
3106
        $this->assertEquals(
3107
            $versions[0]->modificationDate->getTimestamp(),
3108
            $versions[1]->modificationDate->getTimestamp(),
3109
            'Creation time did not match within delta of 2 seconds',
3110
            2
3111
        );
3112
        $this->assertTrue($versions[0]->isArchived());
3113
        $this->assertFalse($versions[0]->isDraft());
3114
        $this->assertFalse($versions[0]->isPublished());
3115
3116
        $this->assertTrue($versions[1]->isPublished());
3117
        $this->assertFalse($versions[1]->isDraft());
3118
        $this->assertFalse($versions[1]->isArchived());
3119
    }
3120
3121
    /**
3122
     * Test for the copyContent() method.
3123
     *
3124
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
3125
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3126
     * @group field-type
3127
     */
3128 View Code Duplication
    public function testCopyContent()
3129
    {
3130
        $parentLocationId = $this->generateId('location', 56);
3131
3132
        $repository = $this->getRepository();
3133
3134
        $contentService = $repository->getContentService();
3135
        $locationService = $repository->getLocationService();
3136
3137
        /* BEGIN: Use Case */
3138
        $contentVersion2 = $this->createMultipleLanguageContentVersion2();
3139
3140
        // Configure new target location
3141
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
3142
3143
        $targetLocationCreate->priority = 42;
3144
        $targetLocationCreate->hidden = true;
3145
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3146
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3147
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3148
3149
        // Copy content with all versions and drafts
3150
        $contentCopied = $contentService->copyContent(
3151
            $contentVersion2->contentInfo,
3152
            $targetLocationCreate
3153
        );
3154
        /* END: Use Case */
3155
3156
        $this->assertInstanceOf(
3157
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
3158
            $contentCopied
3159
        );
3160
3161
        $this->assertNotEquals(
3162
            $contentVersion2->contentInfo->remoteId,
3163
            $contentCopied->contentInfo->remoteId
3164
        );
3165
3166
        $this->assertNotEquals(
3167
            $contentVersion2->id,
3168
            $contentCopied->id
3169
        );
3170
3171
        $this->assertEquals(
3172
            2,
3173
            count($contentService->loadVersions($contentCopied->contentInfo))
3174
        );
3175
3176
        $this->assertEquals(2, $contentCopied->getVersionInfo()->versionNo);
3177
3178
        $this->assertAllFieldsEquals($contentCopied->getFields());
3179
3180
        $this->assertDefaultContentStates($contentCopied->contentInfo);
3181
3182
        $this->assertNotNull(
3183
            $contentCopied->contentInfo->mainLocationId,
3184
            'Expected main location to be set given we provided a LocationCreateStruct'
3185
        );
3186
    }
3187
3188
    /**
3189
     * Test for the copyContent() method.
3190
     *
3191
     * @see \eZ\Publish\API\Repository\ContentService::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo)
3192
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
3193
     *
3194
     * @todo Fix to more descriptive name
3195
     */
3196 View Code Duplication
    public function testCopyContentWithThirdParameter()
3197
    {
3198
        $parentLocationId = $this->generateId('location', 56);
3199
3200
        $repository = $this->getRepository();
3201
3202
        $contentService = $repository->getContentService();
3203
        $locationService = $repository->getLocationService();
3204
3205
        /* BEGIN: Use Case */
3206
        $contentVersion2 = $this->createContentVersion2();
3207
3208
        // Configure new target location
3209
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
3210
3211
        $targetLocationCreate->priority = 42;
3212
        $targetLocationCreate->hidden = true;
3213
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
3214
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
3215
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
3216
3217
        // Copy only the initial version
3218
        $contentCopied = $contentService->copyContent(
3219
            $contentVersion2->contentInfo,
3220
            $targetLocationCreate,
3221
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1)
3222
        );
3223
        /* END: Use Case */
3224
3225
        $this->assertInstanceOf(
3226
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
3227
            $contentCopied
3228
        );
3229
3230
        $this->assertNotEquals(
3231
            $contentVersion2->contentInfo->remoteId,
3232
            $contentCopied->contentInfo->remoteId
3233
        );
3234
3235
        $this->assertNotEquals(
3236
            $contentVersion2->id,
3237
            $contentCopied->id
3238
        );
3239
3240
        $this->assertEquals(
3241
            1,
3242
            count($contentService->loadVersions($contentCopied->contentInfo))
3243
        );
3244
3245
        $this->assertEquals(1, $contentCopied->getVersionInfo()->versionNo);
3246
3247
        $this->assertNotNull(
3248
            $contentCopied->contentInfo->mainLocationId,
3249
            'Expected main location to be set given we provided a LocationCreateStruct'
3250
        );
3251
    }
3252
3253
    /**
3254
     * Test for the addRelation() method.
3255
     *
3256
     * @return \eZ\Publish\API\Repository\Values\Content\Content
3257
     *
3258
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3259
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
3260
     */
3261
    public function testAddRelation()
3262
    {
3263
        $repository = $this->getRepository();
3264
3265
        $contentService = $repository->getContentService();
3266
3267
        /* BEGIN: Use Case */
3268
        // RemoteId of the "Media" content of an eZ Publish demo installation
3269
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3270
3271
        $draft = $this->createContentDraftVersion1();
3272
3273
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3274
3275
        // Create relation between new content object and "Media" page
3276
        $relation = $contentService->addRelation(
3277
            $draft->getVersionInfo(),
3278
            $media
3279
        );
3280
        /* END: Use Case */
3281
3282
        $this->assertInstanceOf(
3283
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Relation',
3284
            $relation
3285
        );
3286
3287
        return $contentService->loadRelations($draft->getVersionInfo());
3288
    }
3289
3290
    /**
3291
     * Test for the addRelation() method.
3292
     *
3293
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3294
     *
3295
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3296
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3297
     */
3298
    public function testAddRelationAddsRelationToContent($relations)
3299
    {
3300
        $this->assertEquals(
3301
            1,
3302
            count($relations)
3303
        );
3304
    }
3305
3306
    /**
3307
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3308
     */
3309
    protected function assertExpectedRelations($relations)
3310
    {
3311
        $this->assertEquals(
3312
            array(
3313
                'type' => Relation::COMMON,
3314
                'sourceFieldDefinitionIdentifier' => null,
3315
                'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3316
                'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3317
            ),
3318
            array(
3319
                'type' => $relations[0]->type,
3320
                'sourceFieldDefinitionIdentifier' => $relations[0]->sourceFieldDefinitionIdentifier,
3321
                'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3322
                'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3323
            )
3324
        );
3325
    }
3326
3327
    /**
3328
     * Test for the addRelation() method.
3329
     *
3330
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3331
     *
3332
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3333
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3334
     */
3335
    public function testAddRelationSetsExpectedRelations($relations)
3336
    {
3337
        $this->assertExpectedRelations($relations);
3338
    }
3339
3340
    /**
3341
     * Test for the createContentDraft() method.
3342
     *
3343
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3344
     *
3345
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
3346
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelationSetsExpectedRelations
3347
     */
3348 View Code Duplication
    public function testCreateContentDraftWithRelations()
3349
    {
3350
        $repository = $this->getRepository();
3351
3352
        $contentService = $repository->getContentService();
3353
3354
        // RemoteId of the "Media" content of an eZ Publish demo installation
3355
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3356
        $draft = $this->createContentDraftVersion1();
3357
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3358
3359
        // Create relation between new content object and "Media" page
3360
        $contentService->addRelation(
3361
            $draft->getVersionInfo(),
3362
            $media
3363
        );
3364
3365
        $content = $contentService->publishVersion($draft->versionInfo);
3366
        $newDraft = $contentService->createContentDraft($content->contentInfo);
3367
3368
        return $contentService->loadRelations($newDraft->getVersionInfo());
3369
    }
3370
3371
    /**
3372
     * Test for the createContentDraft() method.
3373
     *
3374
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3375
     *
3376
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
3377
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelations
3378
     */
3379
    public function testCreateContentDraftWithRelationsCreatesRelations($relations)
3380
    {
3381
        $this->assertEquals(
3382
            1,
3383
            count($relations)
3384
        );
3385
3386
        return $relations;
3387
    }
3388
3389
    /**
3390
     * Test for the createContentDraft() method.
3391
     *
3392
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3393
     *
3394
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelationsCreatesRelations
3395
     */
3396
    public function testCreateContentDraftWithRelationsCreatesExpectedRelations($relations)
3397
    {
3398
        $this->assertExpectedRelations($relations);
3399
    }
3400
3401
    /**
3402
     * Test for the addRelation() method.
3403
     *
3404
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3405
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3406
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3407
     */
3408 View Code Duplication
    public function testAddRelationThrowsBadStateException()
3409
    {
3410
        $repository = $this->getRepository();
3411
3412
        $contentService = $repository->getContentService();
3413
3414
        /* BEGIN: Use Case */
3415
        // RemoteId of the "Media" page of an eZ Publish demo installation
3416
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3417
3418
        $content = $this->createContentVersion1();
3419
3420
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3421
3422
        // This call will fail with a "BadStateException", because content is
3423
        // published and not a draft.
3424
        $contentService->addRelation(
3425
            $content->getVersionInfo(),
3426
            $media
3427
        );
3428
        /* END: Use Case */
3429
    }
3430
3431
    /**
3432
     * Test for the loadRelations() method.
3433
     *
3434
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3435
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3436
     */
3437
    public function testLoadRelations()
3438
    {
3439
        $repository = $this->getRepository();
3440
3441
        $contentService = $repository->getContentService();
3442
3443
        /* BEGIN: Use Case */
3444
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3445
        // of a eZ Publish demo installation.
3446
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3447
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3448
3449
        $draft = $this->createContentDraftVersion1();
3450
3451
        // Load other content objects
3452
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3453
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3454
3455
        // Create relation between new content object and "Media" page
3456
        $contentService->addRelation(
3457
            $draft->getVersionInfo(),
3458
            $media
3459
        );
3460
3461
        // Create another relation with the "Demo Design" page
3462
        $contentService->addRelation(
3463
            $draft->getVersionInfo(),
3464
            $demoDesign
3465
        );
3466
3467
        // Load all relations
3468
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3469
        /* END: Use Case */
3470
3471
        usort(
3472
            $relations,
3473
            function ($rel1, $rel2) {
3474
                return strcasecmp(
3475
                    $rel2->getDestinationContentInfo()->remoteId,
3476
                    $rel1->getDestinationContentInfo()->remoteId
3477
                );
3478
            }
3479
        );
3480
3481
        $this->assertEquals(
3482
            array(
3483
                array(
3484
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3485
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3486
                ),
3487
                array(
3488
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3489
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3490
                ),
3491
            ),
3492
            array(
3493
                array(
3494
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3495
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3496
                ),
3497
                array(
3498
                    'sourceContentInfo' => $relations[1]->sourceContentInfo->remoteId,
3499
                    'destinationContentInfo' => $relations[1]->destinationContentInfo->remoteId,
3500
                ),
3501
            )
3502
        );
3503
    }
3504
3505
    /**
3506
     * Test for the loadRelations() method.
3507
     *
3508
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3509
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3510
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3511
     */
3512
    public function testLoadRelationsSkipsArchivedContent()
3513
    {
3514
        $repository = $this->getRepository();
3515
3516
        $contentService = $repository->getContentService();
3517
3518
        /* BEGIN: Use Case */
3519
        $trashService = $repository->getTrashService();
3520
        $locationService = $repository->getLocationService();
3521
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3522
        // of a eZ Publish demo installation.
3523
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3524
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3525
3526
        $draft = $this->createContentDraftVersion1();
3527
3528
        // Load other content objects
3529
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3530
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3531
3532
        // Create relation between new content object and "Media" page
3533
        $contentService->addRelation(
3534
            $draft->getVersionInfo(),
3535
            $media
3536
        );
3537
3538
        // Create another relation with the "Demo Design" page
3539
        $contentService->addRelation(
3540
            $draft->getVersionInfo(),
3541
            $demoDesign
3542
        );
3543
3544
        $demoDesignLocation = $locationService->loadLocation($demoDesign->mainLocationId);
3545
3546
        // Trashing Content's last Location will change its status to archived,
3547
        // in this case relation towards it will not be loaded.
3548
        $trashService->trash($demoDesignLocation);
3549
3550
        // Load all relations
3551
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3552
        /* END: Use Case */
3553
3554
        $this->assertCount(1, $relations);
3555
        $this->assertEquals(
3556
            array(
3557
                array(
3558
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3559
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3560
                ),
3561
            ),
3562
            array(
3563
                array(
3564
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3565
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3566
                ),
3567
            )
3568
        );
3569
    }
3570
3571
    /**
3572
     * Test for the loadRelations() method.
3573
     *
3574
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3575
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3576
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3577
     */
3578
    public function testLoadRelationsSkipsDraftContent()
3579
    {
3580
        $repository = $this->getRepository();
3581
3582
        $contentService = $repository->getContentService();
3583
3584
        /* BEGIN: Use Case */
3585
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3586
        // of a eZ Publish demo installation.
3587
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3588
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3589
3590
        $draft = $this->createContentDraftVersion1();
3591
3592
        // Load other content objects
3593
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3594
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3595
3596
        // Create draft of "Media" page
3597
        $mediaDraft = $contentService->createContentDraft($media->contentInfo);
3598
3599
        // Create relation between "Media" page and new content object draft.
3600
        // This relation will not be loaded before the draft is published.
3601
        $contentService->addRelation(
3602
            $mediaDraft->getVersionInfo(),
3603
            $draft->getVersionInfo()->getContentInfo()
3604
        );
3605
3606
        // Create another relation with the "Demo Design" page
3607
        $contentService->addRelation(
3608
            $mediaDraft->getVersionInfo(),
3609
            $demoDesign
3610
        );
3611
3612
        // Load all relations
3613
        $relations = $contentService->loadRelations($mediaDraft->getVersionInfo());
3614
        /* END: Use Case */
3615
3616
        $this->assertCount(1, $relations);
3617
        $this->assertEquals(
3618
            array(
3619
                array(
3620
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3621
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3622
                ),
3623
            ),
3624
            array(
3625
                array(
3626
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3627
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3628
                ),
3629
            )
3630
        );
3631
    }
3632
3633
    /**
3634
     * Test for the loadReverseRelations() method.
3635
     *
3636
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3637
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3638
     */
3639
    public function testLoadReverseRelations()
3640
    {
3641
        $repository = $this->getRepository();
3642
3643
        $contentService = $repository->getContentService();
3644
3645
        /* BEGIN: Use Case */
3646
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3647
        // of a eZ Publish demo installation.
3648
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3649
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3650
3651
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3652
        $contentInfo = $versionInfo->getContentInfo();
3653
3654
        // Create some drafts
3655
        $mediaDraft = $contentService->createContentDraft(
3656
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3657
        );
3658
        $demoDesignDraft = $contentService->createContentDraft(
3659
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3660
        );
3661
3662
        // Create relation between new content object and "Media" page
3663
        $relation1 = $contentService->addRelation(
3664
            $mediaDraft->getVersionInfo(),
3665
            $contentInfo
3666
        );
3667
3668
        // Create another relation with the "Demo Design" page
3669
        $relation2 = $contentService->addRelation(
3670
            $demoDesignDraft->getVersionInfo(),
3671
            $contentInfo
3672
        );
3673
3674
        // Publish drafts, so relations become active
3675
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3676
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3677
3678
        // Load all relations
3679
        $relations = $contentService->loadRelations($versionInfo);
3680
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3681
        /* END: Use Case */
3682
3683
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3684
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3685
3686
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3687
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3688
3689
        $this->assertEquals(0, count($relations));
3690
        $this->assertEquals(2, count($reverseRelations));
3691
3692
        usort(
3693
            $reverseRelations,
3694
            function ($rel1, $rel2) {
3695
                return strcasecmp(
3696
                    $rel2->getSourceContentInfo()->remoteId,
3697
                    $rel1->getSourceContentInfo()->remoteId
3698
                );
3699
            }
3700
        );
3701
3702
        $this->assertEquals(
3703
            array(
3704
                array(
3705
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3706
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3707
                ),
3708
                array(
3709
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3710
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3711
                ),
3712
            ),
3713
            array(
3714
                array(
3715
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3716
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3717
                ),
3718
                array(
3719
                    'sourceContentInfo' => $reverseRelations[1]->sourceContentInfo->remoteId,
3720
                    'destinationContentInfo' => $reverseRelations[1]->destinationContentInfo->remoteId,
3721
                ),
3722
            )
3723
        );
3724
    }
3725
3726
    /**
3727
     * Test for the loadReverseRelations() method.
3728
     *
3729
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3730
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3731
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3732
     */
3733
    public function testLoadReverseRelationsSkipsArchivedContent()
3734
    {
3735
        $repository = $this->getRepository();
3736
3737
        $contentService = $repository->getContentService();
3738
3739
        /* BEGIN: Use Case */
3740
        $trashService = $repository->getTrashService();
3741
        $locationService = $repository->getLocationService();
3742
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3743
        // of a eZ Publish demo installation.
3744
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3745
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3746
3747
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3748
        $contentInfo = $versionInfo->getContentInfo();
3749
3750
        // Create some drafts
3751
        $mediaDraft = $contentService->createContentDraft(
3752
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3753
        );
3754
        $demoDesignDraft = $contentService->createContentDraft(
3755
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3756
        );
3757
3758
        // Create relation between new content object and "Media" page
3759
        $relation1 = $contentService->addRelation(
3760
            $mediaDraft->getVersionInfo(),
3761
            $contentInfo
3762
        );
3763
3764
        // Create another relation with the "Demo Design" page
3765
        $relation2 = $contentService->addRelation(
3766
            $demoDesignDraft->getVersionInfo(),
3767
            $contentInfo
3768
        );
3769
3770
        // Publish drafts, so relations become active
3771
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3772
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3773
3774
        $demoDesignLocation = $locationService->loadLocation($demoDesignDraft->contentInfo->mainLocationId);
3775
3776
        // Trashing Content's last Location will change its status to archived,
3777
        // in this case relation from it will not be loaded.
3778
        $trashService->trash($demoDesignLocation);
3779
3780
        // Load all relations
3781
        $relations = $contentService->loadRelations($versionInfo);
3782
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3783
        /* END: Use Case */
3784
3785
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3786
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3787
3788
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3789
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3790
3791
        $this->assertEquals(0, count($relations));
3792
        $this->assertEquals(1, count($reverseRelations));
3793
3794
        $this->assertEquals(
3795
            array(
3796
                array(
3797
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3798
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3799
                ),
3800
            ),
3801
            array(
3802
                array(
3803
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3804
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3805
                ),
3806
            )
3807
        );
3808
    }
3809
3810
    /**
3811
     * Test for the loadReverseRelations() method.
3812
     *
3813
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3814
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3815
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3816
     */
3817
    public function testLoadReverseRelationsSkipsDraftContent()
3818
    {
3819
        $repository = $this->getRepository();
3820
3821
        $contentService = $repository->getContentService();
3822
3823
        /* BEGIN: Use Case */
3824
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3825
        // of a eZ Publish demo installation.
3826
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3827
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3828
3829
        // Load "Media" page Content
3830
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3831
3832
        // Create some drafts
3833
        $newDraftVersionInfo = $this->createContentDraftVersion1()->getVersionInfo();
3834
        $demoDesignDraft = $contentService->createContentDraft(
3835
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3836
        );
3837
3838
        // Create relation between "Media" page and new content object
3839
        $relation1 = $contentService->addRelation(
3840
            $newDraftVersionInfo,
3841
            $media->contentInfo
3842
        );
3843
3844
        // Create another relation with the "Demo Design" page
3845
        $relation2 = $contentService->addRelation(
3846
            $demoDesignDraft->getVersionInfo(),
3847
            $media->contentInfo
3848
        );
3849
3850
        // Publish drafts, so relations become active
3851
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3852
        // We will not publish new Content draft, therefore relation from it
3853
        // will not be loaded as reverse relation for "Media" page
3854
        //$contentService->publishVersion( $newDraftVersionInfo );
3855
3856
        // Load all relations
3857
        $relations = $contentService->loadRelations($media->versionInfo);
3858
        $reverseRelations = $contentService->loadReverseRelations($media->contentInfo);
3859
        /* END: Use Case */
3860
3861
        $this->assertEquals($media->contentInfo->id, $relation1->getDestinationContentInfo()->id);
3862
        $this->assertEquals($newDraftVersionInfo->contentInfo->id, $relation1->getSourceContentInfo()->id);
3863
3864
        $this->assertEquals($media->contentInfo->id, $relation2->getDestinationContentInfo()->id);
3865
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3866
3867
        $this->assertEquals(0, count($relations));
3868
        $this->assertEquals(1, count($reverseRelations));
3869
3870
        $this->assertEquals(
3871
            array(
3872
                array(
3873
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3874
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3875
                ),
3876
            ),
3877
            array(
3878
                array(
3879
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3880
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3881
                ),
3882
            )
3883
        );
3884
    }
3885
3886
    /**
3887
     * Test for the deleteRelation() method.
3888
     *
3889
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3890
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3891
     */
3892
    public function testDeleteRelation()
3893
    {
3894
        $repository = $this->getRepository();
3895
3896
        $contentService = $repository->getContentService();
3897
3898
        /* BEGIN: Use Case */
3899
        // Remote ids of the "Media" and the "Demo Design" page of a eZ Publish
3900
        // demo installation.
3901
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3902
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3903
3904
        $draft = $this->createContentDraftVersion1();
3905
3906
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3907
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3908
3909
        // Establish some relations
3910
        $contentService->addRelation($draft->getVersionInfo(), $media);
3911
        $contentService->addRelation($draft->getVersionInfo(), $demoDesign);
3912
3913
        // Delete one of the currently created relations
3914
        $contentService->deleteRelation($draft->getVersionInfo(), $media);
3915
3916
        // The relations array now contains only one element
3917
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3918
        /* END: Use Case */
3919
3920
        $this->assertEquals(1, count($relations));
3921
    }
3922
3923
    /**
3924
     * Test for the deleteRelation() method.
3925
     *
3926
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3927
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3928
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3929
     */
3930 View Code Duplication
    public function testDeleteRelationThrowsBadStateException()
3931
    {
3932
        $repository = $this->getRepository();
3933
3934
        $contentService = $repository->getContentService();
3935
3936
        /* BEGIN: Use Case */
3937
        // RemoteId of the "Media" page of an eZ Publish demo installation
3938
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3939
3940
        $content = $this->createContentVersion1();
3941
3942
        // Load the destination object
3943
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3944
3945
        // Create a new draft
3946
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
3947
3948
        // Add a relation
3949
        $contentService->addRelation($draftVersion2->getVersionInfo(), $media);
3950
3951
        // Publish new version
3952
        $contentVersion2 = $contentService->publishVersion(
3953
            $draftVersion2->getVersionInfo()
3954
        );
3955
3956
        // This call will fail with a "BadStateException", because content is
3957
        // published and not a draft.
3958
        $contentService->deleteRelation(
3959
            $contentVersion2->getVersionInfo(),
3960
            $media
3961
        );
3962
        /* END: Use Case */
3963
    }
3964
3965
    /**
3966
     * Test for the deleteRelation() method.
3967
     *
3968
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3969
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
3970
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3971
     */
3972 View Code Duplication
    public function testDeleteRelationThrowsInvalidArgumentException()
3973
    {
3974
        $repository = $this->getRepository();
3975
3976
        $contentService = $repository->getContentService();
3977
3978
        /* BEGIN: Use Case */
3979
        // RemoteId of the "Media" page of an eZ Publish demo installation
3980
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3981
3982
        $draft = $this->createContentDraftVersion1();
3983
3984
        // Load the destination object
3985
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3986
3987
        // This call will fail with a "InvalidArgumentException", because no
3988
        // relation exists between $draft and $media.
3989
        $contentService->deleteRelation(
3990
            $draft->getVersionInfo(),
3991
            $media
3992
        );
3993
        /* END: Use Case */
3994
    }
3995
3996
    /**
3997
     * Test for the createContent() method.
3998
     *
3999
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
4000
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4001
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4002
     */
4003
    public function testCreateContentInTransactionWithRollback()
4004
    {
4005
        if ($this->isVersion4()) {
4006
            $this->markTestSkipped('This test requires eZ Publish 5');
4007
        }
4008
4009
        $repository = $this->getRepository();
4010
4011
        /* BEGIN: Use Case */
4012
        $contentTypeService = $repository->getContentTypeService();
4013
        $contentService = $repository->getContentService();
4014
4015
        // Start a transaction
4016
        $repository->beginTransaction();
4017
4018
        try {
4019
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
4020
4021
            // Get a content create struct and set mandatory properties
4022
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
4023
            $contentCreate->setField('name', 'Sindelfingen forum');
4024
4025
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
4026
            $contentCreate->alwaysAvailable = true;
4027
4028
            // Create a new content object
4029
            $contentId = $contentService->createContent($contentCreate)->id;
4030
        } catch (Exception $e) {
4031
            // Cleanup hanging transaction on error
4032
            $repository->rollback();
4033
            throw $e;
4034
        }
4035
4036
        // Rollback all changes
4037
        $repository->rollback();
4038
4039
        try {
4040
            // This call will fail with a "NotFoundException"
4041
            $contentService->loadContent($contentId);
4042
        } catch (NotFoundException $e) {
4043
            // This is expected
4044
            return;
4045
        }
4046
        /* END: Use Case */
4047
4048
        $this->fail('Content object still exists after rollback.');
4049
    }
4050
4051
    /**
4052
     * Test for the createContent() method.
4053
     *
4054
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
4055
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4056
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4057
     */
4058
    public function testCreateContentInTransactionWithCommit()
4059
    {
4060
        if ($this->isVersion4()) {
4061
            $this->markTestSkipped('This test requires eZ Publish 5');
4062
        }
4063
4064
        $repository = $this->getRepository();
4065
4066
        /* BEGIN: Use Case */
4067
        $contentTypeService = $repository->getContentTypeService();
4068
        $contentService = $repository->getContentService();
4069
4070
        // Start a transaction
4071
        $repository->beginTransaction();
4072
4073
        try {
4074
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
4075
4076
            // Get a content create struct and set mandatory properties
4077
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
4078
            $contentCreate->setField('name', 'Sindelfingen forum');
4079
4080
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
4081
            $contentCreate->alwaysAvailable = true;
4082
4083
            // Create a new content object
4084
            $contentId = $contentService->createContent($contentCreate)->id;
4085
4086
            // Commit changes
4087
            $repository->commit();
4088
        } catch (Exception $e) {
4089
            // Cleanup hanging transaction on error
4090
            $repository->rollback();
4091
            throw $e;
4092
        }
4093
4094
        // Load the new content object
4095
        $content = $contentService->loadContent($contentId);
4096
        /* END: Use Case */
4097
4098
        $this->assertEquals($contentId, $content->id);
4099
    }
4100
4101
    /**
4102
     * Test for the createContent() method.
4103
     *
4104
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
4105
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
4106
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
4107
     */
4108
    public function testCreateContentWithLocationCreateParameterInTransactionWithRollback()
4109
    {
4110
        $repository = $this->getRepository();
4111
4112
        $contentService = $repository->getContentService();
4113
4114
        /* BEGIN: Use Case */
4115
        // Start a transaction
4116
        $repository->beginTransaction();
4117
4118
        try {
4119
            $draft = $this->createContentDraftVersion1();
4120
        } catch (Exception $e) {
4121
            // Cleanup hanging transaction on error
4122
            $repository->rollback();
4123
            throw $e;
4124
        }
4125
4126
        $contentId = $draft->id;
4127
4128
        // Roleback the transaction
4129
        $repository->rollback();
4130
4131
        try {
4132
            // This call will fail with a "NotFoundException"
4133
            $contentService->loadContent($contentId);
4134
        } catch (NotFoundException $e) {
4135
            return;
4136
        }
4137
        /* END: Use Case */
4138
4139
        $this->fail('Can still load content object after rollback.');
4140
    }
4141
4142
    /**
4143
     * Test for the createContent() method.
4144
     *
4145
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
4146
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
4147
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
4148
     */
4149 View Code Duplication
    public function testCreateContentWithLocationCreateParameterInTransactionWithCommit()
4150
    {
4151
        $repository = $this->getRepository();
4152
4153
        $contentService = $repository->getContentService();
4154
4155
        /* BEGIN: Use Case */
4156
        // Start a transaction
4157
        $repository->beginTransaction();
4158
4159
        try {
4160
            $draft = $this->createContentDraftVersion1();
4161
4162
            $contentId = $draft->id;
4163
4164
            // Roleback the transaction
4165
            $repository->commit();
4166
        } catch (Exception $e) {
4167
            // Cleanup hanging transaction on error
4168
            $repository->rollback();
4169
            throw $e;
4170
        }
4171
4172
        // Load the new content object
4173
        $content = $contentService->loadContent($contentId);
4174
        /* END: Use Case */
4175
4176
        $this->assertEquals($contentId, $content->id);
4177
    }
4178
4179
    /**
4180
     * Test for the createContentDraft() method.
4181
     *
4182
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4183
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4184
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4185
     */
4186
    public function testCreateContentDraftInTransactionWithRollback()
4187
    {
4188
        $repository = $this->getRepository();
4189
4190
        $contentId = $this->generateId('object', 12);
4191
        /* BEGIN: Use Case */
4192
        // $contentId is the ID of the "Administrator users" user group
4193
4194
        // Get the content service
4195
        $contentService = $repository->getContentService();
4196
4197
        // Load the user group content object
4198
        $content = $contentService->loadContent($contentId);
4199
4200
        // Start a new transaction
4201
        $repository->beginTransaction();
4202
4203
        try {
4204
            // Create a new draft
4205
            $drafted = $contentService->createContentDraft($content->contentInfo);
4206
4207
            // Store version number for later reuse
4208
            $versionNo = $drafted->versionInfo->versionNo;
4209
        } catch (Exception $e) {
4210
            // Cleanup hanging transaction on error
4211
            $repository->rollback();
4212
            throw $e;
4213
        }
4214
4215
        // Rollback
4216
        $repository->rollback();
4217
4218
        try {
4219
            // This call will fail with a "NotFoundException"
4220
            $contentService->loadContent($contentId, null, $versionNo);
4221
        } catch (NotFoundException $e) {
4222
            return;
4223
        }
4224
        /* END: Use Case */
4225
4226
        $this->fail('Can still load content draft after rollback');
4227
    }
4228
4229
    /**
4230
     * Test for the createContentDraft() method.
4231
     *
4232
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
4233
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
4234
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4235
     */
4236 View Code Duplication
    public function testCreateContentDraftInTransactionWithCommit()
4237
    {
4238
        $repository = $this->getRepository();
4239
4240
        $contentId = $this->generateId('object', 12);
4241
        /* BEGIN: Use Case */
4242
        // $contentId is the ID of the "Administrator users" user group
4243
4244
        // Get the content service
4245
        $contentService = $repository->getContentService();
4246
4247
        // Load the user group content object
4248
        $content = $contentService->loadContent($contentId);
4249
4250
        // Start a new transaction
4251
        $repository->beginTransaction();
4252
4253
        try {
4254
            // Create a new draft
4255
            $drafted = $contentService->createContentDraft($content->contentInfo);
4256
4257
            // Store version number for later reuse
4258
            $versionNo = $drafted->versionInfo->versionNo;
4259
4260
            // Commit all changes
4261
            $repository->commit();
4262
        } catch (Exception $e) {
4263
            // Cleanup hanging transaction on error
4264
            $repository->rollback();
4265
            throw $e;
4266
        }
4267
4268
        $content = $contentService->loadContent($contentId, null, $versionNo);
4269
        /* END: Use Case */
4270
4271
        $this->assertEquals(
4272
            $versionNo,
4273
            $content->getVersionInfo()->versionNo
4274
        );
4275
    }
4276
4277
    /**
4278
     * Test for the publishVersion() method.
4279
     *
4280
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4281
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4282
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4283
     */
4284 View Code Duplication
    public function testPublishVersionInTransactionWithRollback()
4285
    {
4286
        $repository = $this->getRepository();
4287
4288
        $contentId = $this->generateId('object', 12);
4289
        /* BEGIN: Use Case */
4290
        // $contentId is the ID of the "Administrator users" user group
4291
4292
        // Get the content service
4293
        $contentService = $repository->getContentService();
4294
4295
        // Load the user group content object
4296
        $content = $contentService->loadContent($contentId);
4297
4298
        // Start a new transaction
4299
        $repository->beginTransaction();
4300
4301
        try {
4302
            $draftVersion = $contentService->createContentDraft($content->contentInfo)->getVersionInfo();
4303
4304
            // Publish a new version
4305
            $content = $contentService->publishVersion($draftVersion);
4306
4307
            // Store version number for later reuse
4308
            $versionNo = $content->versionInfo->versionNo;
4309
        } catch (Exception $e) {
4310
            // Cleanup hanging transaction on error
4311
            $repository->rollback();
4312
            throw $e;
4313
        }
4314
4315
        // Rollback
4316
        $repository->rollback();
4317
4318
        try {
4319
            // This call will fail with a "NotFoundException"
4320
            $contentService->loadContent($contentId, null, $versionNo);
4321
        } catch (NotFoundException $e) {
4322
            return;
4323
        }
4324
        /* END: Use Case */
4325
4326
        $this->fail('Can still load content draft after rollback');
4327
    }
4328
4329
    /**
4330
     * Test for the publishVersion() method.
4331
     *
4332
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
4333
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
4334
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
4335
     */
4336 View Code Duplication
    public function testPublishVersionInTransactionWithCommit()
4337
    {
4338
        $repository = $this->getRepository();
4339
4340
        /* BEGIN: Use Case */
4341
        // ID of the "Administrator users" user group
4342
        $contentId = 12;
4343
4344
        // Get the content service
4345
        $contentService = $repository->getContentService();
4346
4347
        // Load the user group content object
4348
        $template = $contentService->loadContent($contentId);
4349
4350
        // Start a new transaction
4351
        $repository->beginTransaction();
4352
4353
        try {
4354
            // Publish a new version
4355
            $content = $contentService->publishVersion(
4356
                $contentService->createContentDraft($template->contentInfo)->getVersionInfo()
4357
            );
4358
4359
            // Store version number for later reuse
4360
            $versionNo = $content->versionInfo->versionNo;
4361
4362
            // Commit all changes
4363
            $repository->commit();
4364
        } catch (Exception $e) {
4365
            // Cleanup hanging transaction on error
4366
            $repository->rollback();
4367
            throw $e;
4368
        }
4369
4370
        // Load current version info
4371
        $versionInfo = $contentService->loadVersionInfo($content->contentInfo);
4372
        /* END: Use Case */
4373
4374
        $this->assertEquals($versionNo, $versionInfo->versionNo);
4375
    }
4376
4377
    /**
4378
     * Test for the updateContent() method.
4379
     *
4380
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4381
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4382
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4383
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4384
     */
4385 View Code Duplication
    public function testUpdateContentInTransactionWithRollback()
4386
    {
4387
        $repository = $this->getRepository();
4388
4389
        $contentId = $this->generateId('object', 12);
4390
        /* BEGIN: Use Case */
4391
        // $contentId is the ID of the "Administrator users" user group
4392
4393
        // Load content service
4394
        $contentService = $repository->getContentService();
4395
4396
        // Create a new user group draft
4397
        $draft = $contentService->createContentDraft(
4398
            $contentService->loadContentInfo($contentId)
4399
        );
4400
4401
        // Get an update struct and change the group name
4402
        $contentUpdate = $contentService->newContentUpdateStruct();
4403
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4404
4405
        // Start a transaction
4406
        $repository->beginTransaction();
4407
4408
        try {
4409
            // Update the group name
4410
            $draft = $contentService->updateContent(
4411
                $draft->getVersionInfo(),
4412
                $contentUpdate
4413
            );
4414
4415
            // Publish updated version
4416
            $contentService->publishVersion($draft->getVersionInfo());
4417
        } catch (Exception $e) {
4418
            // Cleanup hanging transaction on error
4419
            $repository->rollback();
4420
            throw $e;
4421
        }
4422
4423
        // Rollback all changes.
4424
        $repository->rollback();
4425
4426
        // Name will still be "Administrator users"
4427
        $name = $contentService->loadContent($contentId)->getFieldValue('name');
4428
        /* END: Use Case */
4429
4430
        $this->assertEquals('Administrator users', $name);
4431
    }
4432
4433
    /**
4434
     * Test for the updateContent() method.
4435
     *
4436
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4437
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4438
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4439
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4440
     */
4441 View Code Duplication
    public function testUpdateContentInTransactionWithCommit()
4442
    {
4443
        $repository = $this->getRepository();
4444
4445
        $contentId = $this->generateId('object', 12);
4446
        /* BEGIN: Use Case */
4447
        // $contentId is the ID of the "Administrator users" user group
4448
4449
        // Load content service
4450
        $contentService = $repository->getContentService();
4451
4452
        // Create a new user group draft
4453
        $draft = $contentService->createContentDraft(
4454
            $contentService->loadContentInfo($contentId)
4455
        );
4456
4457
        // Get an update struct and change the group name
4458
        $contentUpdate = $contentService->newContentUpdateStruct();
4459
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4460
4461
        // Start a transaction
4462
        $repository->beginTransaction();
4463
4464
        try {
4465
            // Update the group name
4466
            $draft = $contentService->updateContent(
4467
                $draft->getVersionInfo(),
4468
                $contentUpdate
4469
            );
4470
4471
            // Publish updated version
4472
            $contentService->publishVersion($draft->getVersionInfo());
4473
4474
            // Commit all changes.
4475
            $repository->commit();
4476
        } catch (Exception $e) {
4477
            // Cleanup hanging transaction on error
4478
            $repository->rollback();
4479
            throw $e;
4480
        }
4481
4482
        // Name is now "Administrators"
4483
        $name = $contentService->loadContent($contentId)->getFieldValue('name', 'eng-US');
4484
        /* END: Use Case */
4485
4486
        $this->assertEquals('Administrators', $name);
4487
    }
4488
4489
    /**
4490
     * Test for the updateContentMetadata() method.
4491
     *
4492
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4493
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4494
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4495
     */
4496 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithRollback()
4497
    {
4498
        $repository = $this->getRepository();
4499
4500
        $contentId = $this->generateId('object', 12);
4501
        /* BEGIN: Use Case */
4502
        // $contentId is the ID of the "Administrator users" user group
4503
4504
        // Get the content service
4505
        $contentService = $repository->getContentService();
4506
4507
        // Load a ContentInfo object
4508
        $contentInfo = $contentService->loadContentInfo($contentId);
4509
4510
        // Store remoteId for later testing
4511
        $remoteId = $contentInfo->remoteId;
4512
4513
        // Start a transaction
4514
        $repository->beginTransaction();
4515
4516
        try {
4517
            // Get metadata update struct and change remoteId
4518
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4519
            $metadataUpdate->remoteId = md5(microtime(true));
4520
4521
            // Update the metadata of the published content object
4522
            $contentService->updateContentMetadata(
4523
                $contentInfo,
4524
                $metadataUpdate
4525
            );
4526
        } catch (Exception $e) {
4527
            // Cleanup hanging transaction on error
4528
            $repository->rollback();
4529
            throw $e;
4530
        }
4531
4532
        // Rollback all changes.
4533
        $repository->rollback();
4534
4535
        // Load current remoteId
4536
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4537
        /* END: Use Case */
4538
4539
        $this->assertEquals($remoteId, $remoteIdReloaded);
4540
    }
4541
4542
    /**
4543
     * Test for the updateContentMetadata() method.
4544
     *
4545
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4546
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4547
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4548
     */
4549 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithCommit()
4550
    {
4551
        $repository = $this->getRepository();
4552
4553
        $contentId = $this->generateId('object', 12);
4554
        /* BEGIN: Use Case */
4555
        // $contentId is the ID of the "Administrator users" user group
4556
4557
        // Get the content service
4558
        $contentService = $repository->getContentService();
4559
4560
        // Load a ContentInfo object
4561
        $contentInfo = $contentService->loadContentInfo($contentId);
4562
4563
        // Store remoteId for later testing
4564
        $remoteId = $contentInfo->remoteId;
4565
4566
        // Start a transaction
4567
        $repository->beginTransaction();
4568
4569
        try {
4570
            // Get metadata update struct and change remoteId
4571
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4572
            $metadataUpdate->remoteId = md5(microtime(true));
4573
4574
            // Update the metadata of the published content object
4575
            $contentService->updateContentMetadata(
4576
                $contentInfo,
4577
                $metadataUpdate
4578
            );
4579
4580
            // Commit all changes.
4581
            $repository->commit();
4582
        } catch (Exception $e) {
4583
            // Cleanup hanging transaction on error
4584
            $repository->rollback();
4585
            throw $e;
4586
        }
4587
4588
        // Load current remoteId
4589
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4590
        /* END: Use Case */
4591
4592
        $this->assertNotEquals($remoteId, $remoteIdReloaded);
4593
    }
4594
4595
    /**
4596
     * Test for the deleteVersion() method.
4597
     *
4598
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4599
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4600
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4601
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4602
     */
4603
    public function testDeleteVersionInTransactionWithRollback()
4604
    {
4605
        $repository = $this->getRepository();
4606
4607
        $contentId = $this->generateId('object', 12);
4608
        /* BEGIN: Use Case */
4609
        // $contentId is the ID of the "Administrator users" user group
4610
4611
        // Get the content service
4612
        $contentService = $repository->getContentService();
4613
4614
        // Start a new transaction
4615
        $repository->beginTransaction();
4616
4617
        try {
4618
            // Create a new draft
4619
            $draft = $contentService->createContentDraft(
4620
                $contentService->loadContentInfo($contentId)
4621
            );
4622
4623
            $contentService->deleteVersion($draft->getVersionInfo());
4624
        } catch (Exception $e) {
4625
            // Cleanup hanging transaction on error
4626
            $repository->rollback();
4627
            throw $e;
4628
        }
4629
4630
        // Rollback all changes.
4631
        $repository->rollback();
4632
4633
        // This array will be empty
4634
        $drafts = $contentService->loadContentDrafts();
4635
        /* END: Use Case */
4636
4637
        $this->assertSame(array(), $drafts);
4638
    }
4639
4640
    /**
4641
     * Test for the deleteVersion() method.
4642
     *
4643
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4644
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4645
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4646
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4647
     */
4648
    public function testDeleteVersionInTransactionWithCommit()
4649
    {
4650
        $repository = $this->getRepository();
4651
4652
        $contentId = $this->generateId('object', 12);
4653
        /* BEGIN: Use Case */
4654
        // $contentId is the ID of the "Administrator users" user group
4655
4656
        // Get the content service
4657
        $contentService = $repository->getContentService();
4658
4659
        // Start a new transaction
4660
        $repository->beginTransaction();
4661
4662
        try {
4663
            // Create a new draft
4664
            $draft = $contentService->createContentDraft(
4665
                $contentService->loadContentInfo($contentId)
4666
            );
4667
4668
            $contentService->deleteVersion($draft->getVersionInfo());
4669
4670
            // Commit all changes.
4671
            $repository->commit();
4672
        } catch (Exception $e) {
4673
            // Cleanup hanging transaction on error
4674
            $repository->rollback();
4675
            throw $e;
4676
        }
4677
4678
        // This array will contain no element
4679
        $drafts = $contentService->loadContentDrafts();
4680
        /* END: Use Case */
4681
4682
        $this->assertSame(array(), $drafts);
4683
    }
4684
4685
    /**
4686
     * Test for the deleteContent() method.
4687
     *
4688
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4689
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4690
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4691
     */
4692
    public function testDeleteContentInTransactionWithRollback()
4693
    {
4694
        $repository = $this->getRepository();
4695
4696
        $contentId = $this->generateId('object', 11);
4697
        /* BEGIN: Use Case */
4698
        // $contentId is the ID of the "Members" user group in an eZ Publish
4699
        // demo installation
4700
4701
        // Get content service
4702
        $contentService = $repository->getContentService();
4703
4704
        // Load a ContentInfo instance
4705
        $contentInfo = $contentService->loadContentInfo($contentId);
4706
4707
        // Start a new transaction
4708
        $repository->beginTransaction();
4709
4710
        try {
4711
            // Delete content object
4712
            $contentService->deleteContent($contentInfo);
4713
        } catch (Exception $e) {
4714
            // Cleanup hanging transaction on error
4715
            $repository->rollback();
4716
            throw $e;
4717
        }
4718
4719
        // Rollback all changes
4720
        $repository->rollback();
4721
4722
        // This call will return the original content object
4723
        $contentInfo = $contentService->loadContentInfo($contentId);
4724
        /* END: Use Case */
4725
4726
        $this->assertEquals($contentId, $contentInfo->id);
4727
    }
4728
4729
    /**
4730
     * Test for the deleteContent() method.
4731
     *
4732
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4733
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4734
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4735
     */
4736
    public function testDeleteContentInTransactionWithCommit()
4737
    {
4738
        $repository = $this->getRepository();
4739
4740
        $contentId = $this->generateId('object', 11);
4741
        /* BEGIN: Use Case */
4742
        // $contentId is the ID of the "Members" user group in an eZ Publish
4743
        // demo installation
4744
4745
        // Get content service
4746
        $contentService = $repository->getContentService();
4747
4748
        // Load a ContentInfo instance
4749
        $contentInfo = $contentService->loadContentInfo($contentId);
4750
4751
        // Start a new transaction
4752
        $repository->beginTransaction();
4753
4754
        try {
4755
            // Delete content object
4756
            $contentService->deleteContent($contentInfo);
4757
4758
            // Commit all changes
4759
            $repository->commit();
4760
        } catch (Exception $e) {
4761
            // Cleanup hanging transaction on error
4762
            $repository->rollback();
4763
            throw $e;
4764
        }
4765
4766
        // Deleted content info is not found anymore
4767
        try {
4768
            $contentService->loadContentInfo($contentId);
4769
        } catch (NotFoundException $e) {
4770
            return;
4771
        }
4772
        /* END: Use Case */
4773
4774
        $this->fail('Can still load ContentInfo after commit.');
4775
    }
4776
4777
    /**
4778
     * Test for the copyContent() method.
4779
     *
4780
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4781
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4782
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4783
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4784
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4785
     */
4786 View Code Duplication
    public function testCopyContentInTransactionWithRollback()
4787
    {
4788
        $repository = $this->getRepository();
4789
4790
        $contentId = $this->generateId('object', 11);
4791
        $locationId = $this->generateId('location', 13);
4792
        /* BEGIN: Use Case */
4793
        // $contentId is the ID of the "Members" user group in an eZ Publish
4794
        // demo installation
4795
4796
        // $locationId is the ID of the "Administrator users" group location
4797
4798
        // Get services
4799
        $contentService = $repository->getContentService();
4800
        $locationService = $repository->getLocationService();
4801
4802
        // Load content object to copy
4803
        $content = $contentService->loadContent($contentId);
4804
4805
        // Create new target location
4806
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4807
4808
        // Start a new transaction
4809
        $repository->beginTransaction();
4810
4811
        try {
4812
            // Copy content with all versions and drafts
4813
            $contentService->copyContent(
4814
                $content->contentInfo,
4815
                $locationCreate
4816
            );
4817
        } catch (Exception $e) {
4818
            // Cleanup hanging transaction on error
4819
            $repository->rollback();
4820
            throw $e;
4821
        }
4822
4823
        // Rollback all changes
4824
        $repository->rollback();
4825
4826
        $this->refreshSearch($repository);
4827
4828
        // This array will only contain a single admin user object
4829
        $locations = $locationService->loadLocationChildren(
4830
            $locationService->loadLocation($locationId)
4831
        )->locations;
4832
        /* END: Use Case */
4833
4834
        $this->assertEquals(1, count($locations));
4835
    }
4836
4837
    /**
4838
     * Test for the copyContent() method.
4839
     *
4840
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4841
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4842
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4843
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4844
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4845
     */
4846 View Code Duplication
    public function testCopyContentInTransactionWithCommit()
4847
    {
4848
        $repository = $this->getRepository();
4849
4850
        $contentId = $this->generateId('object', 11);
4851
        $locationId = $this->generateId('location', 13);
4852
        /* BEGIN: Use Case */
4853
        // $contentId is the ID of the "Members" user group in an eZ Publish
4854
        // demo installation
4855
4856
        // $locationId is the ID of the "Administrator users" group location
4857
4858
        // Get services
4859
        $contentService = $repository->getContentService();
4860
        $locationService = $repository->getLocationService();
4861
4862
        // Load content object to copy
4863
        $content = $contentService->loadContent($contentId);
4864
4865
        // Create new target location
4866
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4867
4868
        // Start a new transaction
4869
        $repository->beginTransaction();
4870
4871
        try {
4872
            // Copy content with all versions and drafts
4873
            $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...
4874
                $content->contentInfo,
4875
                $locationCreate
4876
            );
4877
4878
            // Commit all changes
4879
            $repository->commit();
4880
        } catch (Exception $e) {
4881
            // Cleanup hanging transaction on error
4882
            $repository->rollback();
4883
            throw $e;
4884
        }
4885
4886
        $this->refreshSearch($repository);
4887
4888
        // This will contain the admin user and the new child location
4889
        $locations = $locationService->loadLocationChildren(
4890
            $locationService->loadLocation($locationId)
4891
        )->locations;
4892
        /* END: Use Case */
4893
4894
        $this->assertEquals(2, count($locations));
4895
    }
4896
4897
    public function testURLAliasesCreatedForNewContent()
4898
    {
4899
        $repository = $this->getRepository();
4900
4901
        $contentService = $repository->getContentService();
4902
        $locationService = $repository->getLocationService();
4903
        $urlAliasService = $repository->getURLAliasService();
4904
4905
        /* BEGIN: Use Case */
4906
        $draft = $this->createContentDraftVersion1();
4907
4908
        // Automatically creates a new URLAlias for the content
4909
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
4910
        /* END: Use Case */
4911
4912
        $location = $locationService->loadLocation(
4913
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4914
        );
4915
4916
        $aliases = $urlAliasService->listLocationAliases($location, false);
4917
4918
        $this->assertAliasesCorrect(
4919
            array(
4920
                '/Design/Plain-site/An-awesome-forum' => array(
4921
                    'type' => URLAlias::LOCATION,
4922
                    'destination' => $location->id,
4923
                    'path' => '/Design/Plain-site/An-awesome-forum',
4924
                    'languageCodes' => array('eng-US'),
4925
                    'isHistory' => false,
4926
                    'isCustom' => false,
4927
                    'forward' => false,
4928
                ),
4929
            ),
4930
            $aliases
4931
        );
4932
    }
4933
4934
    public function testURLAliasesCreatedForUpdatedContent()
4935
    {
4936
        $repository = $this->getRepository();
4937
4938
        $contentService = $repository->getContentService();
4939
        $locationService = $repository->getLocationService();
4940
        $urlAliasService = $repository->getURLAliasService();
4941
4942
        /* BEGIN: Use Case */
4943
        $draft = $this->createUpdatedDraftVersion2();
4944
4945
        $location = $locationService->loadLocation(
4946
            $draft->getVersionInfo()->getContentInfo()->mainLocationId
4947
        );
4948
4949
        // Load and assert URL aliases before publishing updated Content, so that
4950
        // SPI cache is warmed up and cache invalidation is also tested.
4951
        $aliases = $urlAliasService->listLocationAliases($location, false);
4952
4953
        $this->assertAliasesCorrect(
4954
            array(
4955
                '/Design/Plain-site/An-awesome-forum' => array(
4956
                    'type' => URLAlias::LOCATION,
4957
                    'destination' => $location->id,
4958
                    'path' => '/Design/Plain-site/An-awesome-forum',
4959
                    'languageCodes' => array('eng-US'),
4960
                    'alwaysAvailable' => true,
4961
                    'isHistory' => false,
4962
                    'isCustom' => false,
4963
                    'forward' => false,
4964
                ),
4965
            ),
4966
            $aliases
4967
        );
4968
4969
        // Automatically marks old aliases for the content as history
4970
        // and creates new aliases, based on the changes
4971
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
4972
        /* END: Use Case */
4973
4974
        $location = $locationService->loadLocation(
4975
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4976
        );
4977
4978
        $aliases = $urlAliasService->listLocationAliases($location, false);
4979
4980
        $this->assertAliasesCorrect(
4981
            array(
4982
                '/Design/Plain-site/An-awesome-forum2' => array(
4983
                    'type' => URLAlias::LOCATION,
4984
                    'destination' => $location->id,
4985
                    'path' => '/Design/Plain-site/An-awesome-forum2',
4986
                    'languageCodes' => array('eng-US'),
4987
                    'alwaysAvailable' => true,
4988
                    'isHistory' => false,
4989
                    'isCustom' => false,
4990
                    'forward' => false,
4991
                ),
4992
                '/Design/Plain-site/An-awesome-forum23' => array(
4993
                    'type' => URLAlias::LOCATION,
4994
                    'destination' => $location->id,
4995
                    'path' => '/Design/Plain-site/An-awesome-forum23',
4996
                    'languageCodes' => array('eng-GB'),
4997
                    'alwaysAvailable' => true,
4998
                    'isHistory' => false,
4999
                    'isCustom' => false,
5000
                    'forward' => false,
5001
                ),
5002
            ),
5003
            $aliases
5004
        );
5005
    }
5006
5007
    public function testCustomURLAliasesNotHistorizedOnUpdatedContent()
5008
    {
5009
        $repository = $this->getRepository();
5010
5011
        $contentService = $repository->getContentService();
5012
5013
        /* BEGIN: Use Case */
5014
        $urlAliasService = $repository->getURLAliasService();
5015
        $locationService = $repository->getLocationService();
5016
5017
        $content = $this->createContentVersion1();
5018
5019
        // Create a custom URL alias
5020
        $urlAliasService->createUrlAlias(
5021
            $locationService->loadLocation(
5022
                $content->getVersionInfo()->getContentInfo()->mainLocationId
5023
            ),
5024
            '/my/fancy/story-about-ez-publish',
5025
            'eng-US'
5026
        );
5027
5028
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
5029
5030
        $contentUpdate = $contentService->newContentUpdateStruct();
5031
        $contentUpdate->initialLanguageCode = 'eng-US';
5032
        $contentUpdate->setField('name', 'Amazing Bielefeld forum');
5033
5034
        $draftVersion2 = $contentService->updateContent(
5035
            $draftVersion2->getVersionInfo(),
5036
            $contentUpdate
5037
        );
5038
5039
        // Only marks auto-generated aliases as history
5040
        // the custom one is left untouched
5041
        $liveContent = $contentService->publishVersion($draftVersion2->getVersionInfo());
5042
        /* END: Use Case */
5043
5044
        $location = $locationService->loadLocation(
5045
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
5046
        );
5047
5048
        $aliases = $urlAliasService->listLocationAliases($location);
5049
5050
        $this->assertAliasesCorrect(
5051
            array(
5052
                '/my/fancy/story-about-ez-publish' => array(
5053
                    'type' => URLAlias::LOCATION,
5054
                    'destination' => $location->id,
5055
                    'path' => '/my/fancy/story-about-ez-publish',
5056
                    'languageCodes' => array('eng-US'),
5057
                    'isHistory' => false,
5058
                    'isCustom' => true,
5059
                    'forward' => false,
5060
                    'alwaysAvailable' => false,
5061
                ),
5062
            ),
5063
            $aliases
5064
        );
5065
    }
5066
5067
    /**
5068
     * Test to ensure that old versions are not affected by updates to newer
5069
     * drafts.
5070
     */
5071
    public function testUpdatingDraftDoesNotUpdateOldVersions()
5072
    {
5073
        $repository = $this->getRepository();
5074
5075
        $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...
5076
5077
        $contentService = $repository->getContentService();
5078
5079
        $contentVersion2 = $this->createContentVersion2();
5080
5081
        $loadedContent1 = $contentService->loadContent($contentVersion2->id, null, 1);
5082
        $loadedContent2 = $contentService->loadContent($contentVersion2->id, null, 2);
5083
5084
        $this->assertNotEquals(
5085
            $loadedContent1->getFieldValue('name', 'eng-US'),
5086
            $loadedContent2->getFieldValue('name', 'eng-US')
5087
        );
5088
    }
5089
5090
    /**
5091
     * Test scenario with writer and publisher users.
5092
     * Writer can only create content. Publisher can publish this content.
5093
     */
5094
    public function testPublishWorkflow()
5095
    {
5096
        $repository = $this->getRepository();
5097
        $contentService = $repository->getContentService();
5098
5099
        $this->createRoleWithPolicies('Publisher', [
5100
            ['content', 'read'],
5101
            ['content', 'create'],
5102
            ['content', 'publish'],
5103
        ]);
5104
5105
        $this->createRoleWithPolicies('Writer', [
5106
            ['content', 'read'],
5107
            ['content', 'create'],
5108
        ]);
5109
5110
        $writerUser = $this->createCustomUserWithLogin(
5111
            'writer',
5112
            '[email protected]',
5113
            'Writers',
5114
            'Writer'
5115
        );
5116
5117
        $publisherUser = $this->createCustomUserWithLogin(
5118
            'publisher',
5119
            '[email protected]',
5120
            'Publishers',
5121
            'Publisher'
5122
        );
5123
5124
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5125
        $draft = $this->createContentDraftVersion1();
5126
5127
        $repository->getPermissionResolver()->setCurrentUserReference($publisherUser);
5128
        $content = $contentService->publishVersion($draft->versionInfo);
5129
5130
        $contentService->loadContent($content->id);
5131
    }
5132
5133
    /**
5134
     * Test publish / content policy is required to be able to publish content.
5135
     *
5136
     * @expectedException \eZ\Publish\Core\Base\Exceptions\UnauthorizedException
5137
     * @expectedExceptionMessageRegExp /User does not have access to 'publish' 'content'/
5138
     */
5139
    public function testPublishContentWithoutPublishPolicyThrowsException()
5140
    {
5141
        $repository = $this->getRepository();
5142
5143
        $this->createRoleWithPolicies('Writer', [
5144
            ['content', 'read'],
5145
            ['content', 'create'],
5146
            ['content', 'edit'],
5147
        ]);
5148
        $writerUser = $this->createCustomUserWithLogin(
5149
            'writer',
5150
            '[email protected]',
5151
            'Writers',
5152
            'Writer'
5153
        );
5154
        $repository->getPermissionResolver()->setCurrentUserReference($writerUser);
5155
5156
        $this->createContentVersion1();
5157
    }
5158
5159
    /**
5160
     * Test for the newTranslationInfo() method.
5161
     *
5162
     * @covers \eZ\Publish\Core\Repository\ContentService::newTranslationInfo
5163
     */
5164
    public function testNewTranslationInfo()
5165
    {
5166
        $repository = $this->getRepository();
5167
        $contentService = $repository->getContentService();
5168
5169
        $translationInfo = $contentService->newTranslationInfo();
5170
5171
        $this->assertInstanceOf(
5172
            TranslationInfo::class,
5173
            $translationInfo
5174
        );
5175
5176
        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...
5177
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
5178
        }
5179
    }
5180
5181
    /**
5182
     * Simplify creating custom role with limited set of policies.
5183
     *
5184
     * @param $roleName
5185
     * @param array $policies e.g. [ ['content', 'create'], ['content', 'edit'], ]
5186
     */
5187
    private function createRoleWithPolicies($roleName, array $policies)
5188
    {
5189
        $repository = $this->getRepository();
5190
        $roleService = $repository->getRoleService();
5191
5192
        $roleCreateStruct = $roleService->newRoleCreateStruct($roleName);
5193
        foreach ($policies as $policy) {
5194
            $policyCreateStruct = $roleService->newPolicyCreateStruct($policy[0], $policy[1]);
5195
            $roleCreateStruct->addPolicy($policyCreateStruct);
5196
        }
5197
5198
        $roleDraft = $roleService->createRole($roleCreateStruct);
5199
        $roleService->publishRoleDraft($roleDraft);
5200
    }
5201
5202
    /**
5203
     * Asserts that all aliases defined in $expectedAliasProperties with the
5204
     * given properties are available in $actualAliases and not more.
5205
     *
5206
     * @param array $expectedAliasProperties
5207
     * @param array $actualAliases
5208
     */
5209
    private function assertAliasesCorrect(array $expectedAliasProperties, array $actualAliases)
5210
    {
5211
        foreach ($actualAliases as $actualAlias) {
5212
            if (!isset($expectedAliasProperties[$actualAlias->path])) {
5213
                $this->fail(
5214
                    sprintf(
5215
                        'Alias with path "%s" in languages "%s" not expected.',
5216
                        $actualAlias->path,
5217
                        implode(', ', $actualAlias->languageCodes)
5218
                    )
5219
                );
5220
            }
5221
5222
            foreach ($expectedAliasProperties[$actualAlias->path] as $propertyName => $propertyValue) {
5223
                $this->assertEquals(
5224
                    $propertyValue,
5225
                    $actualAlias->$propertyName,
5226
                    sprintf(
5227
                        'Property $%s incorrect on alias with path "%s" in languages "%s".',
5228
                        $propertyName,
5229
                        $actualAlias->path,
5230
                        implode(', ', $actualAlias->languageCodes)
5231
                    )
5232
                );
5233
            }
5234
5235
            unset($expectedAliasProperties[$actualAlias->path]);
5236
        }
5237
5238
        if (!empty($expectedAliasProperties)) {
5239
            $this->fail(
5240
                sprintf(
5241
                    'Missing expected aliases with paths "%s".',
5242
                    implode('", "', array_keys($expectedAliasProperties))
5243
                )
5244
            );
5245
        }
5246
    }
5247
5248
    /**
5249
     * Asserts that the given fields are equal to the default fields fixture.
5250
     *
5251
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5252
     */
5253
    private function assertAllFieldsEquals(array $fields)
5254
    {
5255
        $actual = $this->normalizeFields($fields);
5256
        $expected = $this->normalizeFields($this->createFieldsFixture());
5257
5258
        $this->assertEquals($expected, $actual);
5259
    }
5260
5261
    /**
5262
     * Asserts that the given fields are equal to a language filtered set of the
5263
     * default fields fixture.
5264
     *
5265
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5266
     * @param string $languageCode
5267
     */
5268
    private function assertLocaleFieldsEquals(array $fields, $languageCode)
5269
    {
5270
        $actual = $this->normalizeFields($fields);
5271
5272
        $expected = array();
5273
        foreach ($this->normalizeFields($this->createFieldsFixture()) as $field) {
5274
            if ($field->languageCode !== $languageCode) {
5275
                continue;
5276
            }
5277
            $expected[] = $field;
5278
        }
5279
5280
        $this->assertEquals($expected, $actual);
5281
    }
5282
5283
    /**
5284
     * This method normalizes a set of fields and returns a normalized set.
5285
     *
5286
     * Normalization means it resets the storage specific field id to zero and
5287
     * it sorts the field by their identifier and their language code. In
5288
     * addition, the field value is removed, since this one depends on the
5289
     * specific FieldType, which is tested in a dedicated integration test.
5290
     *
5291
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
5292
     *
5293
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5294
     */
5295
    private function normalizeFields(array $fields)
5296
    {
5297
        $normalized = array();
5298 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...
5299
            $normalized[] = new Field(
5300
                array(
5301
                    'id' => 0,
5302
                    '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...
5303
                    'languageCode' => $field->languageCode,
5304
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
5305
                )
5306
            );
5307
        }
5308
        usort(
5309
            $normalized,
5310 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...
5311
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
5312
                    return strcasecmp($field1->languageCode, $field2->languageCode);
5313
                }
5314
5315
                return $return;
5316
            }
5317
        );
5318
5319
        return $normalized;
5320
    }
5321
5322
    /**
5323
     * Returns a filtered set of the default fields fixture.
5324
     *
5325
     * @param string $languageCode
5326
     *
5327
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5328
     */
5329
    private function createLocaleFieldsFixture($languageCode)
5330
    {
5331
        $fields = array();
5332
        foreach ($this->createFieldsFixture() as $field) {
5333
            if (null === $field->languageCode || $languageCode === $field->languageCode) {
5334
                $fields[] = $field;
5335
            }
5336
        }
5337
5338
        return $fields;
5339
    }
5340
5341
    /**
5342
     * Asserts that given Content has default ContentStates.
5343
     *
5344
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
5345
     */
5346 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
5347
    {
5348
        $repository = $this->getRepository();
5349
        $objectStateService = $repository->getObjectStateService();
5350
5351
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
5352
5353
        foreach ($objectStateGroups as $objectStateGroup) {
5354
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
5355
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
5356
                // Only check the first object state which is the default one.
5357
                $this->assertEquals(
5358
                    $objectState,
5359
                    $contentState
5360
                );
5361
                break;
5362
            }
5363
        }
5364
    }
5365
5366
    /**
5367
     * Returns the default fixture of fields used in most tests.
5368
     *
5369
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
5370
     */
5371
    private function createFieldsFixture()
5372
    {
5373
        return array(
5374
            new Field(
5375
                array(
5376
                    'id' => 0,
5377
                    'value' => 'Foo',
5378
                    'languageCode' => 'eng-US',
5379
                    'fieldDefIdentifier' => 'description',
5380
                )
5381
            ),
5382
            new Field(
5383
                array(
5384
                    'id' => 0,
5385
                    'value' => 'Bar',
5386
                    'languageCode' => 'eng-GB',
5387
                    'fieldDefIdentifier' => 'description',
5388
                )
5389
            ),
5390
            new Field(
5391
                array(
5392
                    'id' => 0,
5393
                    'value' => 'An awesome multi-lang forum²',
5394
                    'languageCode' => 'eng-US',
5395
                    'fieldDefIdentifier' => 'name',
5396
                )
5397
            ),
5398
            new Field(
5399
                array(
5400
                    'id' => 0,
5401
                    'value' => 'An awesome multi-lang forum²³',
5402
                    'languageCode' => 'eng-GB',
5403
                    'fieldDefIdentifier' => 'name',
5404
                )
5405
            ),
5406
        );
5407
    }
5408
5409
    /**
5410
     * Gets expected property values for the "Media" ContentInfo ValueObject.
5411
     *
5412
     * @return array
5413
     */
5414 View Code Duplication
    private function getExpectedMediaContentInfoProperties()
5415
    {
5416
        return [
5417
            'id' => 41,
5418
            'contentTypeId' => 1,
5419
            'name' => 'Media',
5420
            'sectionId' => 3,
5421
            'currentVersionNo' => 1,
5422
            'published' => true,
5423
            'ownerId' => 14,
5424
            'modificationDate' => $this->createDateTime(1060695457),
5425
            'publishedDate' => $this->createDateTime(1060695457),
5426
            'alwaysAvailable' => 1,
5427
            'remoteId' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
5428
            'mainLanguageCode' => 'eng-US',
5429
            'mainLocationId' => 43,
5430
        ];
5431
    }
5432
}
5433