Completed
Push — master ( 3650d6...6d4b31 )
by André
20:00
created

ContentServiceTest::testPublishWorkflow()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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