Completed
Push — ezp26914-imagine_resolve_excep... ( b3a31a...daccc4 )
by
unknown
16:32
created

testCreateContentThrowsInvalidArgumentExceptionWithLocationCreateParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 30
nc 1
nop 0
dl 0
loc 54
rs 9.6716
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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

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

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

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

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

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

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