Completed
Push — ezp-26764_fielddef_settings_61... ( f2dafb )
by
unknown
15:57
created

testLoadVersionsSetsExpectedVersionInfo()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

Loading history...
253
                'initialLanguageCode' => 'eng-US',
254
            ),
255
            array(
256
                'status' => $content->getVersionInfo()->status,
257
                'versionNo' => $content->getVersionInfo()->versionNo,
258
                'creatorId' => $content->getVersionInfo()->creatorId,
259
                'initialLanguageCode' => $content->getVersionInfo()->initialLanguageCode,
260
            )
261
        );
262
    }
263
264
    /**
265
     * Test for the createContent() method.
266
     *
267
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
268
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
269
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
270
     */
271
    public function testCreateContentThrowsInvalidArgumentException()
272
    {
273
        if ($this->isVersion4()) {
274
            $this->markTestSkipped('This test requires eZ Publish 5');
275
        }
276
277
        $repository = $this->getRepository();
278
279
        /* BEGIN: Use Case */
280
        $contentTypeService = $repository->getContentTypeService();
281
        $contentService = $repository->getContentService();
282
283
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
284
285
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
286
        $contentCreate1->setField('name', 'An awesome Sidelfingen forum');
287
288
        $contentCreate1->remoteId = 'abcdef0123456789abcdef0123456789';
289
        $contentCreate1->alwaysAvailable = true;
290
291
        $draft = $contentService->createContent($contentCreate1);
292
        $contentService->publishVersion($draft->versionInfo);
293
294
        $contentCreate2 = $contentService->newContentCreateStruct($contentType, 'eng-GB');
295
        $contentCreate2->setField('name', 'An awesome Bielefeld forum');
296
297
        $contentCreate2->remoteId = 'abcdef0123456789abcdef0123456789';
298
        $contentCreate2->alwaysAvailable = false;
299
300
        // This call will fail with an "InvalidArgumentException", because the
301
        // remoteId is already in use.
302
        $contentService->createContent($contentCreate2);
303
        /* END: Use Case */
304
    }
305
306
    /**
307
     * Test for the createContent() method.
308
     *
309
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
310
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
311
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
312
     */
313 View Code Duplication
    public function testCreateContentThrowsInvalidArgumentExceptionOnFieldTypeNotAccept()
314
    {
315
        $repository = $this->getRepository();
316
317
        /* BEGIN: Use Case */
318
        $contentTypeService = $repository->getContentTypeService();
319
        $contentService = $repository->getContentService();
320
321
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
322
323
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
324
        // The name field does only accept strings and null as its values
325
        $contentCreate->setField('name', new \stdClass());
326
327
        // Throws InvalidArgumentException since the name field is filled
328
        // improperly
329
        $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...
330
        /* END: Use Case */
331
    }
332
333
    /**
334
     * Test for the createContent() method.
335
     *
336
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
337
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
338
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
339
     */
340
    public function testCreateContentThrowsContentFieldValidationException()
341
    {
342
        $repository = $this->getRepository();
343
344
        /* BEGIN: Use Case */
345
        $contentTypeService = $repository->getContentTypeService();
346
        $contentService = $repository->getContentService();
347
348
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
349
350
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
351
        $contentCreate1->setField('name', 'An awesome Sidelfingen folder');
352
        // Violates string length constraint
353
        $contentCreate1->setField('short_name', str_repeat('a', 200));
354
355
        // Throws ContentFieldValidationException, since short_name does not pass
356
        // validation of the string length validator
357
        $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...
358
        /* END: Use Case */
359
    }
360
361
    /**
362
     * Test for the createContent() method.
363
     *
364
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
365
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
366
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
367
     */
368 View Code Duplication
    public function testCreateContentRequiredFieldMissing()
369
    {
370
        $repository = $this->getRepository();
371
372
        /* BEGIN: Use Case */
373
        $contentTypeService = $repository->getContentTypeService();
374
        $contentService = $repository->getContentService();
375
376
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
377
378
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
379
        // Required field "name" is not set
380
381
        // Throws a ContentFieldValidationException, since a required field is
382
        // missing
383
        $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...
384
        /* END: Use Case */
385
    }
386
387
    /**
388
     * Test for the createContent() method.
389
     *
390
     * NOTE: We have bidirectional dependencies between the ContentService and
391
     * the LocationService, so that we cannot use PHPUnit's test dependencies
392
     * here.
393
     *
394
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
395
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
396
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationByRemoteId
397
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
398
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
399
     * @group user
400
     */
401
    public function testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately()
402
    {
403
        $repository = $this->getRepository();
404
405
        $locationService = $repository->getLocationService();
406
407
        /* BEGIN: Use Case */
408
        $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...
409
410
        // The location will not have been created, yet, so this throws an
411
        // exception
412
        $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...
413
            '0123456789abcdef0123456789abcdef'
414
        );
415
        /* END: Use Case */
416
    }
417
418
    /**
419
     * Test for the createContent() method.
420
     *
421
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
422
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
423
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
424
     */
425
    public function testCreateContentThrowsInvalidArgumentExceptionWithLocationCreateParameter()
426
    {
427
        $repository = $this->getRepository();
428
429
        $parentLocationId = $this->generateId('location', 56);
430
        /* BEGIN: Use Case */
431
        // $parentLocationId is a valid location ID
432
433
        $contentService = $repository->getContentService();
434
        $contentTypeService = $repository->getContentTypeService();
435
        $locationService = $repository->getLocationService();
436
437
        // Load content type
438
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
439
440
        // Configure new locations
441
        $locationCreate1 = $locationService->newLocationCreateStruct($parentLocationId);
442
443
        $locationCreate1->priority = 23;
444
        $locationCreate1->hidden = true;
445
        $locationCreate1->remoteId = '0123456789abcdef0123456789aaaaaa';
446
        $locationCreate1->sortField = Location::SORT_FIELD_NODE_ID;
447
        $locationCreate1->sortOrder = Location::SORT_ORDER_DESC;
448
449
        $locationCreate2 = $locationService->newLocationCreateStruct($parentLocationId);
450
451
        $locationCreate2->priority = 42;
452
        $locationCreate2->hidden = true;
453
        $locationCreate2->remoteId = '0123456789abcdef0123456789bbbbbb';
454
        $locationCreate2->sortField = Location::SORT_FIELD_NODE_ID;
455
        $locationCreate2->sortOrder = Location::SORT_ORDER_DESC;
456
457
        // Configure new content object
458
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
459
460
        $contentCreate->setField('name', 'A awesome Sindelfingen forum');
461
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
462
        $contentCreate->alwaysAvailable = true;
463
464
        // Create new content object under the specified location
465
        $draft = $contentService->createContent(
466
            $contentCreate,
467
            array($locationCreate1)
468
        );
469
        $contentService->publishVersion($draft->versionInfo);
470
471
        // This call will fail with an "InvalidArgumentException", because the
472
        // Content remoteId already exists,
473
        $contentService->createContent(
474
            $contentCreate,
475
            array($locationCreate2)
476
        );
477
        /* END: Use Case */
478
    }
479
480
    /**
481
     * Test for the loadContentInfo() method.
482
     *
483
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
484
     * @group user
485
     */
486 View Code Duplication
    public function testLoadContentInfo()
487
    {
488
        $repository = $this->getRepository();
489
490
        $mediaFolderId = $this->generateId('object', 41);
491
        /* BEGIN: Use Case */
492
        $contentService = $repository->getContentService();
493
494
        // Load the ContentInfo for "Media" folder
495
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
496
        /* END: Use Case */
497
498
        $this->assertInstanceOf(
499
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
500
            $contentInfo
501
        );
502
503
        return $contentInfo;
504
    }
505
506
    /**
507
     * Test for the returned value of the loadContentInfo() method.
508
     *
509
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
510
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfo
511
     *
512
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
513
     */
514
    public function testLoadContentInfoSetsExpectedContentInfo(ContentInfo $contentInfo)
515
    {
516
        $this->assertPropertiesCorrectUnsorted(
517
            $this->getExpectedMediaContentInfoProperties(),
518
            $contentInfo
519
        );
520
    }
521
522
    /**
523
     * Test for the loadContentInfo() method.
524
     *
525
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
526
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
527
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
528
     */
529 View Code Duplication
    public function testLoadContentInfoThrowsNotFoundException()
530
    {
531
        $repository = $this->getRepository();
532
533
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
534
        /* BEGIN: Use Case */
535
        $contentService = $repository->getContentService();
536
537
        // This call will fail with a NotFoundException
538
        $contentService->loadContentInfo($nonExistentContentId);
539
        /* END: Use Case */
540
    }
541
542
    /**
543
     * Test for the loadContentInfoByRemoteId() method.
544
     *
545
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
546
     */
547
    public function testLoadContentInfoByRemoteId()
548
    {
549
        $repository = $this->getRepository();
550
551
        /* BEGIN: Use Case */
552
        $contentService = $repository->getContentService();
553
554
        // Load the ContentInfo for "Media" folder
555
        $contentInfo = $contentService->loadContentInfoByRemoteId('faaeb9be3bd98ed09f606fc16d144eca');
556
        /* END: Use Case */
557
558
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $contentInfo);
559
560
        return $contentInfo;
561
    }
562
563
    /**
564
     * Test for the returned value of the loadContentInfoByRemoteId() method.
565
     *
566
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
567
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId
568
     *
569
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
570
     */
571 View Code Duplication
    public function testLoadContentInfoByRemoteIdSetsExpectedContentInfo(ContentInfo $contentInfo)
572
    {
573
        $this->assertPropertiesCorrectUnsorted(
574
            [
575
                'id' => 10,
576
                'contentTypeId' => 4,
577
                'name' => 'Anonymous User',
578
                'sectionId' => 2,
579
                'currentVersionNo' => 2,
580
                'published' => true,
581
                'ownerId' => 14,
582
                'modificationDate' => $this->createDateTime(1072180405),
583
                'publishedDate' => $this->createDateTime(1033920665),
584
                'alwaysAvailable' => 1,
585
                'remoteId' => 'faaeb9be3bd98ed09f606fc16d144eca',
586
                'mainLanguageCode' => 'eng-US',
587
                'mainLocationId' => 45,
588
            ],
589
            $contentInfo
590
        );
591
    }
592
593
    /**
594
     * Test for the loadContentInfoByRemoteId() method.
595
     *
596
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
597
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
598
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
599
     */
600
    public function testLoadContentInfoByRemoteIdThrowsNotFoundException()
601
    {
602
        $repository = $this->getRepository();
603
604
        /* BEGIN: Use Case */
605
        $contentService = $repository->getContentService();
606
607
        // This call will fail with a NotFoundException
608
        $contentService->loadContentInfoByRemoteId('abcdefghijklmnopqrstuvwxyz0123456789');
609
        /* END: Use Case */
610
    }
611
612
    /**
613
     * Test for the loadVersionInfo() method.
614
     *
615
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
616
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
617
     * @group user
618
     */
619 View Code Duplication
    public function testLoadVersionInfo()
620
    {
621
        $repository = $this->getRepository();
622
623
        $mediaFolderId = $this->generateId('object', 41);
624
        /* BEGIN: Use Case */
625
        // $mediaFolderId contains the ID of the "Media" folder
626
627
        $contentService = $repository->getContentService();
628
629
        // Load the ContentInfo for "Media" folder
630
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
631
632
        // Now load the current version info of the "Media" folder
633
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
634
        /* END: Use Case */
635
636
        $this->assertInstanceOf(
637
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
638
            $versionInfo
639
        );
640
    }
641
642
    /**
643
     * Test for the loadVersionInfoById() method.
644
     *
645
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
646
     */
647 View Code Duplication
    public function testLoadVersionInfoById()
648
    {
649
        $repository = $this->getRepository();
650
651
        $mediaFolderId = $this->generateId('object', 41);
652
        /* BEGIN: Use Case */
653
        // $mediaFolderId contains the ID of the "Media" folder
654
655
        $contentService = $repository->getContentService();
656
657
        // Load the VersionInfo for "Media" folder
658
        $versionInfo = $contentService->loadVersionInfoById($mediaFolderId);
659
        /* END: Use Case */
660
661
        $this->assertInstanceOf(
662
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
663
            $versionInfo
664
        );
665
666
        return $versionInfo;
667
    }
668
669
    /**
670
     * Test for the returned value of the loadVersionInfoById() method.
671
     *
672
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
673
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
674
     *
675
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
676
     */
677
    public function testLoadVersionInfoByIdSetsExpectedVersionInfo(VersionInfo $versionInfo)
678
    {
679
        $this->assertPropertiesCorrect(
680
            [
681
                'names' => [
682
                    'eng-US' => 'Media',
683
                ],
684
                'contentInfo' => new ContentInfo($this->getExpectedMediaContentInfoProperties()),
685
                'id' => 472,
686
                'versionNo' => 1,
687
                'modificationDate' => $this->createDateTime(1060695457),
688
                'creatorId' => 14,
689
                'creationDate' => $this->createDateTime(1060695450),
690
                'status' => 1,
691
                'initialLanguageCode' => 'eng-US',
692
                'languageCodes' => [
693
                    'eng-US',
694
                ],
695
            ],
696
            $versionInfo
697
        );
698
    }
699
700
    /**
701
     * Test for the loadVersionInfoById() method.
702
     *
703
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
704
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
705
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
706
     */
707 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundException()
708
    {
709
        $repository = $this->getRepository();
710
711
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
712
        /* BEGIN: Use Case */
713
        $contentService = $repository->getContentService();
714
715
        // This call will fail with a "NotFoundException"
716
        $contentService->loadVersionInfoById($nonExistentContentId);
717
        /* END: Use Case */
718
    }
719
720
    /**
721
     * Test for the loadContentByContentInfo() method.
722
     *
723
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
724
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
725
     */
726 View Code Duplication
    public function testLoadContentByContentInfo()
727
    {
728
        $repository = $this->getRepository();
729
730
        $mediaFolderId = $this->generateId('object', 41);
731
        /* BEGIN: Use Case */
732
        // $mediaFolderId contains the ID of the "Media" folder
733
734
        $contentService = $repository->getContentService();
735
736
        // Load the ContentInfo for "Media" folder
737
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
738
739
        // Now load the current content version for the info instance
740
        $content = $contentService->loadContentByContentInfo($contentInfo);
741
        /* END: Use Case */
742
743
        $this->assertInstanceOf(
744
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
745
            $content
746
        );
747
    }
748
749
    /**
750
     * Test for the loadContentByVersionInfo() method.
751
     *
752
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
753
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
754
     */
755
    public function testLoadContentByVersionInfo()
756
    {
757
        $repository = $this->getRepository();
758
759
        $mediaFolderId = $this->generateId('object', 41);
760
        /* BEGIN: Use Case */
761
        // $mediaFolderId contains the ID of the "Media" folder
762
763
        $contentService = $repository->getContentService();
764
765
        // Load the ContentInfo for "Media" folder
766
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
767
768
        // Load the current VersionInfo
769
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
770
771
        // Now load the current content version for the info instance
772
        $content = $contentService->loadContentByVersionInfo($versionInfo);
773
        /* END: Use Case */
774
775
        $this->assertInstanceOf(
776
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
777
            $content
778
        );
779
    }
780
781
    /**
782
     * Test for the loadContent() method.
783
     *
784
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
785
     * @group user
786
     * @group field-type
787
     */
788 View Code Duplication
    public function testLoadContent()
789
    {
790
        $repository = $this->getRepository();
791
792
        $mediaFolderId = $this->generateId('object', 41);
793
        /* BEGIN: Use Case */
794
        // $mediaFolderId contains the ID of the "Media" folder
795
796
        $contentService = $repository->getContentService();
797
798
        // Load the Content for "Media" folder, any language and current version
799
        $content = $contentService->loadContent($mediaFolderId);
800
        /* END: Use Case */
801
802
        $this->assertInstanceOf(
803
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
804
            $content
805
        );
806
    }
807
808
    /**
809
     * Test for the loadContent() method.
810
     *
811
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
812
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
813
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
814
     */
815 View Code Duplication
    public function testLoadContentThrowsNotFoundException()
816
    {
817
        $repository = $this->getRepository();
818
819
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
820
        /* BEGIN: Use Case */
821
        $contentService = $repository->getContentService();
822
823
        // This call will fail with a "NotFoundException"
824
        $contentService->loadContent($nonExistentContentId);
825
        /* END: Use Case */
826
    }
827
828
    /**
829
     * Data provider for testLoadContentByRemoteId().
830
     *
831
     * @return array
832
     */
833
    public function contentRemoteIdVersionLanguageProvider()
834
    {
835
        return [
836
            ['f5c88a2209584891056f987fd965b0ba', null, null],
837
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], null],
838
            ['f5c88a2209584891056f987fd965b0ba', null, 1],
839
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], 1],
840
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, null],
841
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], null],
842
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', null, 1],
843
            ['a6e35cbcb7cd6ae4b691f3eee30cd262', ['eng-US'], 1],
844
        ];
845
    }
846
847
    /**
848
     * Test for the loadContentByRemoteId() method.
849
     *
850
     * @covers \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId
851
     * @dataProvider contentRemoteIdVersionLanguageProvider
852
     *
853
     * @param string $remoteId
854
     * @param array|null $languages
855
     * @param int $versionNo
856
     */
857
    public function testLoadContentByRemoteId($remoteId, $languages, $versionNo)
858
    {
859
        $repository = $this->getRepository();
860
861
        $contentService = $repository->getContentService();
862
863
        $content = $contentService->loadContentByRemoteId($remoteId, $languages, $versionNo);
864
865
        $this->assertInstanceOf(
866
            Content::class,
867
            $content
868
        );
869
870
        $this->assertEquals($remoteId, $content->contentInfo->remoteId);
871
        if ($languages !== null) {
872
            $this->assertEquals($languages, $content->getVersionInfo()->languageCodes);
873
        }
874
        $this->assertEquals($versionNo ?: 1, $content->getVersionInfo()->versionNo);
875
    }
876
877
    /**
878
     * Test for the loadContentByRemoteId() method.
879
     *
880
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
881
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
882
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
883
     */
884
    public function testLoadContentByRemoteIdThrowsNotFoundException()
885
    {
886
        $repository = $this->getRepository();
887
888
        /* BEGIN: Use Case */
889
        $contentService = $repository->getContentService();
890
891
        // This call will fail with a "NotFoundException", because no content
892
        // object exists for the given remoteId
893
        $contentService->loadContentByRemoteId('a1b1c1d1e1f1a2b2c2d2e2f2a3b3c3d3');
894
        /* END: Use Case */
895
    }
896
897
    /**
898
     * Test for the publishVersion() method.
899
     *
900
     * @return \eZ\Publish\API\Repository\Values\Content\Content
901
     *
902
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
903
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
904
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
905
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
906
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
907
     * @group user
908
     * @group field-type
909
     */
910
    public function testPublishVersion()
911
    {
912
        $time = time();
913
        /* BEGIN: Use Case */
914
        $content = $this->createContentVersion1();
915
        /* END: Use Case */
916
917
        $this->assertInstanceOf(Content::class, $content);
918
        $this->assertTrue($content->contentInfo->published);
919
        $this->assertEquals(VersionInfo::STATUS_PUBLISHED, $content->versionInfo->status);
920
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->publishedDate->getTimestamp());
921
        $this->assertGreaterThanOrEqual($time, $content->contentInfo->modificationDate->getTimestamp());
922
923
        return $content;
924
    }
925
926
    /**
927
     * Test for the publishVersion() method.
928
     *
929
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
930
     *
931
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
932
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
933
     */
934
    public function testPublishVersionSetsExpectedContentInfo($content)
935
    {
936
        $this->assertEquals(
937
            array(
938
                $content->id,
939
                true,
940
                1,
941
                'abcdef0123456789abcdef0123456789',
942
                'eng-US',
943
                $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...
944
                true,
945
            ),
946
            array(
947
                $content->contentInfo->id,
948
                $content->contentInfo->alwaysAvailable,
949
                $content->contentInfo->currentVersionNo,
950
                $content->contentInfo->remoteId,
951
                $content->contentInfo->mainLanguageCode,
952
                $content->contentInfo->ownerId,
953
                $content->contentInfo->published,
954
            )
955
        );
956
957
        $this->assertNotNull($content->contentInfo->mainLocationId);
958
        $date = new \DateTime('1984/01/01');
959
        $this->assertGreaterThan(
960
            $date->getTimestamp(),
961
            $content->contentInfo->publishedDate->getTimestamp()
962
        );
963
    }
964
965
    /**
966
     * Test for the publishVersion() method.
967
     *
968
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
969
     *
970
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
971
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
972
     */
973
    public function testPublishVersionSetsExpectedVersionInfo($content)
974
    {
975
        $this->assertEquals(
976
            array(
977
                $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...
978
                'eng-US',
979
                VersionInfo::STATUS_PUBLISHED,
980
                1,
981
            ),
982
            array(
983
                $content->getVersionInfo()->creatorId,
984
                $content->getVersionInfo()->initialLanguageCode,
985
                $content->getVersionInfo()->status,
986
                $content->getVersionInfo()->versionNo,
987
            )
988
        );
989
990
        $date = new \DateTime('1984/01/01');
991
        $this->assertGreaterThan(
992
            $date->getTimestamp(),
993
            $content->getVersionInfo()->modificationDate->getTimestamp()
994
        );
995
996
        $this->assertNotNull($content->getVersionInfo()->modificationDate);
997
    }
998
999
    /**
1000
     * Test for the publishVersion() method.
1001
     *
1002
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1003
     *
1004
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1005
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
1006
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1007
     */
1008
    public function testPublishVersionCreatesLocationsDefinedOnCreate()
1009
    {
1010
        $repository = $this->getRepository();
1011
1012
        /* BEGIN: Use Case */
1013
        $content = $this->createContentVersion1();
1014
        /* END: Use Case */
1015
1016
        $locationService = $repository->getLocationService();
1017
        $location = $locationService->loadLocationByRemoteId(
1018
            '0123456789abcdef0123456789abcdef'
1019
        );
1020
1021
        $this->assertEquals(
1022
            $location->getContentInfo(),
1023
            $content->getVersionInfo()->getContentInfo()
1024
        );
1025
1026
        return array($content, $location);
1027
    }
1028
1029
    /**
1030
     * Test for the publishVersion() method.
1031
     *
1032
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1033
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionCreatesLocationsDefinedOnCreate
1034
     */
1035
    public function testCreateContentWithLocationCreateParameterCreatesExpectedLocation(array $testData)
1036
    {
1037
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
1038
        /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
1039
        list($content, $location) = $testData;
1040
1041
        $parentLocationId = $this->generateId('location', 56);
1042
        $parentLocation = $this->getRepository()->getLocationService()->loadLocation($parentLocationId);
1043
        $mainLocationId = $content->getVersionInfo()->getContentInfo()->mainLocationId;
1044
1045
        $this->assertPropertiesCorrect(
1046
            array(
1047
                'id' => $mainLocationId,
1048
                'priority' => 23,
1049
                'hidden' => true,
1050
                'invisible' => true,
1051
                'remoteId' => '0123456789abcdef0123456789abcdef',
1052
                'parentLocationId' => $parentLocationId,
1053
                'pathString' => $parentLocation->pathString . $mainLocationId . '/',
1054
                'depth' => $parentLocation->depth + 1,
1055
                'sortField' => Location::SORT_FIELD_NODE_ID,
1056
                'sortOrder' => Location::SORT_ORDER_DESC,
1057
            ),
1058
            $location
1059
        );
1060
    }
1061
1062
    /**
1063
     * Test for the publishVersion() method.
1064
     *
1065
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1066
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1067
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1068
     */
1069 View Code Duplication
    public function testPublishVersionThrowsBadStateException()
1070
    {
1071
        $repository = $this->getRepository();
1072
1073
        $contentService = $repository->getContentService();
1074
1075
        /* BEGIN: Use Case */
1076
        $draft = $this->createContentDraftVersion1();
1077
1078
        // Publish the content draft
1079
        $contentService->publishVersion($draft->getVersionInfo());
1080
1081
        // This call will fail with a "BadStateException", because the version
1082
        // is already published.
1083
        $contentService->publishVersion($draft->getVersionInfo());
1084
        /* END: Use Case */
1085
    }
1086
1087
    /**
1088
     * Test that publishVersion() does not affect publishedDate (assuming previous version exists).
1089
     *
1090
     * @covers \eZ\Publish\API\Repository\ContentService::publishVersion
1091
     */
1092
    public function testPublishVersionDoesNotChangePublishedDate()
1093
    {
1094
        $repository = $this->getRepository();
1095
1096
        $contentService = $repository->getContentService();
1097
1098
        $publishedContent = $this->createContentVersion1();
1099
1100
        // force timestamps to differ
1101
        sleep(1);
1102
1103
        $contentDraft = $contentService->createContentDraft($publishedContent->contentInfo);
1104
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1105
        $contentUpdateStruct->setField('name', 'New name');
1106
        $contentDraft = $contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
1107
        $republishedContent = $contentService->publishVersion($contentDraft->versionInfo);
1108
1109
        $this->assertEquals(
1110
            $publishedContent->contentInfo->publishedDate->getTimestamp(),
1111
            $republishedContent->contentInfo->publishedDate->getTimestamp()
1112
        );
1113
        $this->assertGreaterThan(
1114
            $publishedContent->contentInfo->modificationDate->getTimestamp(),
1115
            $republishedContent->contentInfo->modificationDate->getTimestamp()
1116
        );
1117
    }
1118
1119
    /**
1120
     * Test for the createContentDraft() method.
1121
     *
1122
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1123
     *
1124
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1125
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1126
     * @group user
1127
     */
1128
    public function testCreateContentDraft()
1129
    {
1130
        $repository = $this->getRepository();
1131
1132
        $contentService = $repository->getContentService();
1133
1134
        /* BEGIN: Use Case */
1135
        $content = $this->createContentVersion1();
1136
1137
        // Now we create a new draft from the published content
1138
        $draftedContent = $contentService->createContentDraft($content->contentInfo);
1139
        /* END: Use Case */
1140
1141
        $this->assertInstanceOf(
1142
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1143
            $draftedContent
1144
        );
1145
1146
        return $draftedContent;
1147
    }
1148
1149
    /**
1150
     * Test for the createContentDraft() method.
1151
     *
1152
     * Test that editor has access to edit own draft.
1153
     * Note: Editors have access to version_read, which is needed to load content drafts.
1154
     *
1155
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1156
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1157
     * @group user
1158
     */
1159 View Code Duplication
    public function testCreateContentDraftAndLoadAccess()
1160
    {
1161
        $repository = $this->getRepository();
1162
1163
        /* BEGIN: Use Case */
1164
        $user = $this->createUserVersion1();
1165
1166
        // Set new editor as user
1167
        $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...
1168
1169
        // Create draft
1170
        $draft = $this->createContentDraftVersion1(2, 'folder');
1171
1172
        // Try to load the draft
1173
        $contentService = $repository->getContentService();
1174
        $loadedDraft = $contentService->loadContent($draft->id);
1175
1176
        /* END: Use Case */
1177
1178
        $this->assertEquals($draft->id, $loadedDraft->id);
1179
    }
1180
1181
    /**
1182
     * Test for the createContentDraft() method.
1183
     *
1184
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1185
     *
1186
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1187
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1188
     */
1189
    public function testCreateContentDraftSetsExpectedProperties($draft)
1190
    {
1191
        $this->assertEquals(
1192
            array(
1193
                'fieldCount' => 2,
1194
                'relationCount' => 0,
1195
            ),
1196
            array(
1197
                'fieldCount' => count($draft->getFields()),
1198
                'relationCount' => count($this->getRepository()->getContentService()->loadRelations($draft->getVersionInfo())),
1199
            )
1200
        );
1201
    }
1202
1203
    /**
1204
     * Test for the createContentDraft() method.
1205
     *
1206
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1207
     *
1208
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1209
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1210
     */
1211
    public function testCreateContentDraftSetsContentInfo($draft)
1212
    {
1213
        $contentInfo = $draft->contentInfo;
1214
1215
        $this->assertEquals(
1216
            array(
1217
                $draft->id,
1218
                true,
1219
                1,
1220
                'eng-US',
1221
                $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...
1222
                'abcdef0123456789abcdef0123456789',
1223
                1,
1224
            ),
1225
            array(
1226
                $contentInfo->id,
1227
                $contentInfo->alwaysAvailable,
1228
                $contentInfo->currentVersionNo,
1229
                $contentInfo->mainLanguageCode,
1230
                $contentInfo->ownerId,
1231
                $contentInfo->remoteId,
1232
                $contentInfo->sectionId,
1233
            )
1234
        );
1235
    }
1236
1237
    /**
1238
     * Test for the createContentDraft() method.
1239
     *
1240
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1241
     *
1242
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1243
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1244
     */
1245
    public function testCreateContentDraftSetsVersionInfo($draft)
1246
    {
1247
        $versionInfo = $draft->getVersionInfo();
1248
1249
        $this->assertEquals(
1250
            array(
1251
                '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...
1252
                'initialLanguageCode' => 'eng-US',
1253
                'languageCodes' => array(0 => 'eng-US'),
1254
                'status' => VersionInfo::STATUS_DRAFT,
1255
                'versionNo' => 2,
1256
            ),
1257
            array(
1258
                'creatorId' => $versionInfo->creatorId,
1259
                'initialLanguageCode' => $versionInfo->initialLanguageCode,
1260
                'languageCodes' => $versionInfo->languageCodes,
1261
                'status' => $versionInfo->status,
1262
                'versionNo' => $versionInfo->versionNo,
1263
            )
1264
        );
1265
    }
1266
1267
    /**
1268
     * Test for the createContentDraft() method.
1269
     *
1270
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1271
     *
1272
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1273
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1274
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
1275
     */
1276 View Code Duplication
    public function testCreateContentDraftLoadVersionInfoStillLoadsPublishedVersion($draft)
0 ignored issues
show
Unused Code introduced by
The parameter $draft is not used and could be removed.

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

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