Completed
Push — version_list_ordering ( 7460a7...4d573c )
by André
74:04 queued 25:57
created

testLoadContentByContentInfoWithLanguageParameters()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 129
Code Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 77
nc 1
nop 0
dl 0
loc 129
rs 8.2857
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

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