Completed
Push — master ( 02e816...b5f511 )
by André
12:31
created

testCreateContentDraftAndLoadAccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 21
rs 9.3142
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
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'create');
126
        $policyCreateStruct->addLimitation(new SectionLimitation(array('limitationValues' => array(1))));
127
        $policyCreateStruct->addLimitation(new LocationLimitation(array('limitationValues' => array(2))));
128
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(array('limitationValues' => array(1))));
129
        $roleService->addPolicy($role, $policyCreateStruct);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\RoleService::addPolicy() has been deprecated with message: since 6.0, use {@see addPolicyByRoleDraft}

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