Completed
Push — EZP-23281 ( d976e8...bb7535 )
by André
14:16
created

testPublishVersionNotCreatingUnlimitedArchives()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 57
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 33
nc 1
nop 0
dl 0
loc 57
rs 9.6818
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 publishVersion() method, and that it creates limited archives.
1661
     *
1662
     * @todo Adapt this when per content type archvie limited is added on repository Content Type model.
1663
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1664
     * @depe nds \eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1665
     */
1666
    public function testPublishVersionNotCreatingUnlimitedArchives()
1667
    {
1668
        $repository = $this->getRepository();
1669
1670
        $contentService = $repository->getContentService();
1671
1672
        $content = $this->createContentVersion1();
1673
1674
        // Create a new draft with versionNo = 2
1675
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1676
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1677
1678
        // Create a new draft with versionNo = 3
1679
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1680
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1681
1682
        // Create a new draft with versionNo = 4
1683
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1684
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1685
1686
        // Create a new draft with versionNo = 5
1687
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1688
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1689
1690
        // Create a new draft with versionNo = 6
1691
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1692
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1693
1694
        // Create a new draft with versionNo = 7
1695
        $draftedContentVersion = $contentService->createContentDraft($content->contentInfo);
1696
        $contentService->publishVersion($draftedContentVersion->getVersionInfo());
1697
1698
        $versionInfoList = $contentService->loadVersions($content->contentInfo);
1699
1700
        $this->assertEquals(6, count($versionInfoList));
1701
        $this->assertEquals(2, $versionInfoList[0]->versionNo);
1702
        $this->assertEquals(7, $versionInfoList[5]->versionNo);
1703
1704
        $this->assertEquals(
1705
            [
1706
                VersionInfo::STATUS_ARCHIVED,
1707
                VersionInfo::STATUS_ARCHIVED,
1708
                VersionInfo::STATUS_ARCHIVED,
1709
                VersionInfo::STATUS_ARCHIVED,
1710
                VersionInfo::STATUS_ARCHIVED,
1711
                VersionInfo::STATUS_PUBLISHED,
1712
            ],
1713
            [
1714
                $versionInfoList[0]->status,
1715
                $versionInfoList[1]->status,
1716
                $versionInfoList[2]->status,
1717
                $versionInfoList[3]->status,
1718
                $versionInfoList[4]->status,
1719
                $versionInfoList[5]->status,
1720
            ]
1721
        );
1722
    }
1723
1724
    /**
1725
     * Test for the newContentMetadataUpdateStruct() method.
1726
     *
1727
     * @see \eZ\Publish\API\Repository\ContentService::newContentMetadataUpdateStruct()
1728
     * @group user
1729
     */
1730
    public function testNewContentMetadataUpdateStruct()
1731
    {
1732
        $repository = $this->getRepository();
1733
1734
        /* BEGIN: Use Case */
1735
        $contentService = $repository->getContentService();
1736
1737
        // Creates a new metadata update struct
1738
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1739
1740
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1741
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1742
        $metadataUpdate->alwaysAvailable = false;
1743
        /* END: Use Case */
1744
1745
        $this->assertInstanceOf(
1746
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentMetadataUpdateStruct',
1747
            $metadataUpdate
1748
        );
1749
    }
1750
1751
    /**
1752
     * Test for the updateContentMetadata() method.
1753
     *
1754
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1755
     *
1756
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1757
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1758
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentMetadataUpdateStruct
1759
     * @group user
1760
     */
1761
    public function testUpdateContentMetadata()
1762
    {
1763
        $repository = $this->getRepository();
1764
1765
        $contentService = $repository->getContentService();
1766
1767
        /* BEGIN: Use Case */
1768
        $content = $this->createContentVersion1();
1769
1770
        // Creates a metadata update struct
1771
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1772
1773
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1774
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1775
        $metadataUpdate->alwaysAvailable = false;
1776
        $metadataUpdate->publishedDate = $this->createDateTime(441759600); // 1984/01/01
1777
        $metadataUpdate->modificationDate = $this->createDateTime(441759600); // 1984/01/01
1778
1779
        // Update the metadata of the published content object
1780
        $content = $contentService->updateContentMetadata(
1781
            $content->contentInfo,
1782
            $metadataUpdate
1783
        );
1784
        /* END: Use Case */
1785
1786
        $this->assertInstanceOf(
1787
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1788
            $content
1789
        );
1790
1791
        return $content;
1792
    }
1793
1794
    /**
1795
     * Test for the updateContentMetadata() method.
1796
     *
1797
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1798
     *
1799
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1800
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
1801
     */
1802
    public function testUpdateContentMetadataSetsExpectedProperties($content)
1803
    {
1804
        $contentInfo = $content->contentInfo;
1805
1806
        $this->assertEquals(
1807
            array(
1808
                'remoteId' => 'aaaabbbbccccddddeeeeffff11112222',
1809
                'sectionId' => $this->generateId('section', 1),
1810
                'alwaysAvailable' => false,
1811
                'currentVersionNo' => 1,
1812
                'mainLanguageCode' => 'eng-GB',
1813
                'modificationDate' => $this->createDateTime(441759600),
1814
                '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...
1815
                'published' => true,
1816
                'publishedDate' => $this->createDateTime(441759600),
1817
            ),
1818
            array(
1819
                'remoteId' => $contentInfo->remoteId,
1820
                'sectionId' => $contentInfo->sectionId,
1821
                'alwaysAvailable' => $contentInfo->alwaysAvailable,
1822
                'currentVersionNo' => $contentInfo->currentVersionNo,
1823
                'mainLanguageCode' => $contentInfo->mainLanguageCode,
1824
                'modificationDate' => $contentInfo->modificationDate,
1825
                'ownerId' => $contentInfo->ownerId,
1826
                'published' => $contentInfo->published,
1827
                'publishedDate' => $contentInfo->publishedDate,
1828
            )
1829
        );
1830
    }
1831
1832
    /**
1833
     * Test for the updateContentMetadata() method.
1834
     *
1835
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1836
     *
1837
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1838
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
1839
     */
1840
    public function testUpdateContentMetadataNotUpdatesContentVersion($content)
1841
    {
1842
        $this->assertEquals(1, $content->getVersionInfo()->versionNo);
1843
    }
1844
1845
    /**
1846
     * Test for the updateContentMetadata() method.
1847
     *
1848
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1849
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1850
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
1851
     */
1852
    public function testUpdateContentMetadataThrowsInvalidArgumentException()
1853
    {
1854
        $repository = $this->getRepository();
1855
1856
        $contentService = $repository->getContentService();
1857
1858
        /* BEGIN: Use Case */
1859
        // RemoteId of the "Media" page of an eZ Publish demo installation
1860
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1861
1862
        $content = $this->createContentVersion1();
1863
1864
        // Creates a metadata update struct
1865
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1866
        $metadataUpdate->remoteId = $mediaRemoteId;
1867
1868
        // This call will fail with an "InvalidArgumentException", because the
1869
        // specified remoteId is already used by the "Media" page.
1870
        $contentService->updateContentMetadata(
1871
            $content->contentInfo,
1872
            $metadataUpdate
1873
        );
1874
        /* END: Use Case */
1875
    }
1876
1877
    /**
1878
     * Test for the deleteContent() method.
1879
     *
1880
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
1881
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1882
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1883
     */
1884 View Code Duplication
    public function testDeleteContent()
1885
    {
1886
        $repository = $this->getRepository();
1887
1888
        $contentService = $repository->getContentService();
1889
        $locationService = $repository->getLocationService();
1890
1891
        /* BEGIN: Use Case */
1892
        $contentVersion2 = $this->createContentVersion2();
1893
1894
        // Load the locations for this content object
1895
        $locations = $locationService->loadLocations($contentVersion2->contentInfo);
1896
1897
        // This will delete the content, all versions and the associated locations
1898
        $contentService->deleteContent($contentVersion2->contentInfo);
1899
        /* END: Use Case */
1900
1901
        foreach ($locations as $location) {
1902
            $locationService->loadLocation($location->id);
1903
        }
1904
    }
1905
1906
    /**
1907
     * Test for the deleteContent() method.
1908
     *
1909
     * Test for issue EZP-21057:
1910
     * "contentService: Unable to delete a content with an empty file attribute"
1911
     *
1912
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
1913
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1914
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1915
     */
1916 View Code Duplication
    public function testDeleteContentWithEmptyBinaryField()
1917
    {
1918
        $repository = $this->getRepository();
1919
1920
        $contentService = $repository->getContentService();
1921
        $locationService = $repository->getLocationService();
1922
1923
        /* BEGIN: Use Case */
1924
        $contentVersion = $this->createContentVersion1EmptyBinaryField();
1925
1926
        // Load the locations for this content object
1927
        $locations = $locationService->loadLocations($contentVersion->contentInfo);
1928
1929
        // This will delete the content, all versions and the associated locations
1930
        $contentService->deleteContent($contentVersion->contentInfo);
1931
        /* END: Use Case */
1932
1933
        foreach ($locations as $location) {
1934
            $locationService->loadLocation($location->id);
1935
        }
1936
    }
1937
1938
    /**
1939
     * Test for the loadContentDrafts() method.
1940
     *
1941
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
1942
     */
1943
    public function testLoadContentDraftsReturnsEmptyArrayByDefault()
1944
    {
1945
        $repository = $this->getRepository();
1946
1947
        /* BEGIN: Use Case */
1948
        $contentService = $repository->getContentService();
1949
1950
        $contentDrafts = $contentService->loadContentDrafts();
1951
        /* END: Use Case */
1952
1953
        $this->assertSame(array(), $contentDrafts);
1954
    }
1955
1956
    /**
1957
     * Test for the loadContentDrafts() method.
1958
     *
1959
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
1960
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1961
     */
1962
    public function testLoadContentDrafts()
1963
    {
1964
        $repository = $this->getRepository();
1965
1966
        /* BEGIN: Use Case */
1967
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
1968
        // of a eZ Publish demo installation.
1969
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1970
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
1971
1972
        $contentService = $repository->getContentService();
1973
1974
        // "Media" content object
1975
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
1976
1977
        // "eZ Publish Demo Design ..." content object
1978
        $demoDesignContentInfo = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
1979
1980
        // Create some drafts
1981
        $contentService->createContentDraft($mediaContentInfo);
1982
        $contentService->createContentDraft($demoDesignContentInfo);
1983
1984
        // Now $contentDrafts should contain two drafted versions
1985
        $draftedVersions = $contentService->loadContentDrafts();
1986
        /* END: Use Case */
1987
1988
        $actual = array(
1989
            $draftedVersions[0]->status,
1990
            $draftedVersions[0]->getContentInfo()->remoteId,
1991
            $draftedVersions[1]->status,
1992
            $draftedVersions[1]->getContentInfo()->remoteId,
1993
        );
1994
        sort($actual, SORT_STRING);
1995
1996
        $this->assertEquals(
1997
            array(
1998
                VersionInfo::STATUS_DRAFT,
1999
                VersionInfo::STATUS_DRAFT,
2000
                $demoDesignRemoteId,
2001
                $mediaRemoteId,
2002
            ),
2003
            $actual
2004
        );
2005
    }
2006
2007
    /**
2008
     * Test for the loadContentDrafts() method.
2009
     *
2010
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
2011
     */
2012
    public function testLoadContentDraftsWithFirstParameter()
2013
    {
2014
        $repository = $this->getRepository();
2015
2016
        /* BEGIN: Use Case */
2017
        $user = $this->createUserVersion1();
2018
2019
        // Get current user
2020
        $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...
2021
2022
        // Set new editor as user
2023
        $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...
2024
2025
        // Remote id of the "Media" content object in an eZ Publish demo installation.
2026
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2027
2028
        $contentService = $repository->getContentService();
2029
2030
        // "Media" content object
2031
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2032
2033
        // Create a content draft
2034
        $contentService->createContentDraft($mediaContentInfo);
2035
2036
        // Reset to previous current user
2037
        $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...
2038
2039
        // Now $contentDrafts for the previous current user and the new user
2040
        $newCurrentUserDrafts = $contentService->loadContentDrafts($user);
2041
        $oldCurrentUserDrafts = $contentService->loadContentDrafts($oldCurrentUser);
2042
        /* END: Use Case */
2043
2044
        $this->assertSame(array(), $oldCurrentUserDrafts);
2045
2046
        $this->assertEquals(
2047
            array(
2048
                VersionInfo::STATUS_DRAFT,
2049
                $mediaRemoteId,
2050
            ),
2051
            array(
2052
                $newCurrentUserDrafts[0]->status,
2053
                $newCurrentUserDrafts[0]->getContentInfo()->remoteId,
2054
            )
2055
        );
2056
    }
2057
2058
    /**
2059
     * Test for the loadVersionInfo() method.
2060
     *
2061
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2062
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2063
     */
2064 View Code Duplication
    public function testLoadVersionInfoWithSecondParameter()
2065
    {
2066
        $repository = $this->getRepository();
2067
2068
        $contentService = $repository->getContentService();
2069
2070
        /* BEGIN: Use Case */
2071
        $publishedContent = $this->createContentVersion1();
2072
2073
        $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...
2074
2075
        // Will return the VersionInfo of the $draftContent
2076
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2077
        /* END: Use Case */
2078
2079
        $this->assertEquals(2, $versionInfo->versionNo);
2080
2081
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2082
        $this->assertEquals(
2083
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2084
            $versionInfo->getContentInfo()->mainLocationId
2085
        );
2086
    }
2087
2088
    /**
2089
     * Test for the loadVersionInfo() method.
2090
     *
2091
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2092
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2093
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2094
     */
2095
    public function testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter()
2096
    {
2097
        $repository = $this->getRepository();
2098
2099
        $contentService = $repository->getContentService();
2100
2101
        /* BEGIN: Use Case */
2102
        $draft = $this->createContentDraftVersion1();
2103
2104
        // This call will fail with a "NotFoundException", because not versionNo
2105
        // 2 exists for this content object.
2106
        $contentService->loadVersionInfo($draft->contentInfo, 2);
2107
        /* END: Use Case */
2108
    }
2109
2110
    /**
2111
     * Test for the loadVersionInfoById() method.
2112
     *
2113
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2114
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2115
     */
2116 View Code Duplication
    public function testLoadVersionInfoByIdWithSecondParameter()
2117
    {
2118
        $repository = $this->getRepository();
2119
2120
        $contentService = $repository->getContentService();
2121
2122
        /* BEGIN: Use Case */
2123
        $publishedContent = $this->createContentVersion1();
2124
2125
        $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...
2126
2127
        // Will return the VersionInfo of the $draftContent
2128
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2129
        /* END: Use Case */
2130
2131
        $this->assertEquals(2, $versionInfo->versionNo);
2132
2133
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2134
        $this->assertEquals(
2135
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2136
            $versionInfo->getContentInfo()->mainLocationId
2137
        );
2138
    }
2139
2140
    /**
2141
     * Test for the loadVersionInfoById() method.
2142
     *
2143
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2144
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2145
     */
2146 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter()
2147
    {
2148
        $repository = $this->getRepository();
2149
2150
        $contentService = $repository->getContentService();
2151
2152
        /* BEGIN: Use Case */
2153
        $content = $this->createContentVersion1();
2154
2155
        // This call will fail with a "NotFoundException", because not versionNo
2156
        // 2 exists for this content object.
2157
        $contentService->loadVersionInfoById($content->id, 2);
2158
        /* END: Use Case */
2159
    }
2160
2161
    /**
2162
     * Test for the loadContentByVersionInfo() method.
2163
     *
2164
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
2165
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2166
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
2167
     */
2168
    public function testLoadContentByVersionInfoWithSecondParameter()
2169
    {
2170
        $repository = $this->getRepository();
2171
2172
        $sectionId = $this->generateId('section', 1);
2173
        /* BEGIN: Use Case */
2174
        $contentTypeService = $repository->getContentTypeService();
2175
2176
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2177
2178
        $contentService = $repository->getContentService();
2179
2180
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2181
2182
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2183
2184
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2185
2186
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2187
        // $sectionId contains the ID of section 1
2188
        $contentCreateStruct->sectionId = $sectionId;
2189
        $contentCreateStruct->alwaysAvailable = true;
2190
2191
        // Create a new content draft
2192
        $content = $contentService->createContent($contentCreateStruct);
2193
2194
        // Now publish this draft
2195
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2196
2197
        // Will return a content instance with fields in "eng-US"
2198
        $reloadedContent = $contentService->loadContentByVersionInfo(
2199
            $publishedContent->getVersionInfo(),
2200
            array(
2201
                'eng-GB',
2202
            ),
2203
            false
2204
        );
2205
        /* END: Use Case */
2206
2207
        $actual = array();
2208 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...
2209
            $actual[] = new Field(
2210
                array(
2211
                    'id' => 0,
2212
                    '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...
2213
                    'languageCode' => $field->languageCode,
2214
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
2215
                )
2216
            );
2217
        }
2218
        usort(
2219
            $actual,
2220 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...
2221
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
2222
                    return strcasecmp($field1->languageCode, $field2->languageCode);
2223
                }
2224
2225
                return $return;
2226
            }
2227
        );
2228
2229
        $expected = array(
2230
            new Field(
2231
                array(
2232
                    'id' => 0,
2233
                    'value' => true,
2234
                    'languageCode' => 'eng-GB',
2235
                    'fieldDefIdentifier' => 'description',
2236
                )
2237
            ),
2238
            new Field(
2239
                array(
2240
                    'id' => 0,
2241
                    'value' => true,
2242
                    'languageCode' => 'eng-GB',
2243
                    'fieldDefIdentifier' => 'name',
2244
                )
2245
            ),
2246
        );
2247
2248
        $this->assertEquals($expected, $actual);
2249
    }
2250
2251
    /**
2252
     * Test for the loadContentByContentInfo() method.
2253
     *
2254
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages)
2255
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2256
     */
2257
    public function testLoadContentByContentInfoWithLanguageParameters()
2258
    {
2259
        $repository = $this->getRepository();
2260
2261
        $sectionId = $this->generateId('section', 1);
2262
        /* BEGIN: Use Case */
2263
        $contentTypeService = $repository->getContentTypeService();
2264
2265
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2266
2267
        $contentService = $repository->getContentService();
2268
2269
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2270
2271
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2272
2273
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2274
2275
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2276
        // $sectionId contains the ID of section 1
2277
        $contentCreateStruct->sectionId = $sectionId;
2278
        $contentCreateStruct->alwaysAvailable = true;
2279
2280
        // Create a new content draft
2281
        $content = $contentService->createContent($contentCreateStruct);
2282
2283
        // Now publish this draft
2284
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2285
2286
        // Will return a content instance with fields in "eng-US"
2287
        $reloadedContent = $contentService->loadContentByContentInfo(
2288
            $publishedContent->contentInfo,
2289
            array(
2290
                'eng-US',
2291
            ),
2292
            null,
2293
            false
2294
        );
2295
        /* END: Use Case */
2296
2297
        $actual = $this->normalizeFields($reloadedContent->getFields());
2298
2299
        $expected = array(
2300
            new Field(
2301
                array(
2302
                    'id' => 0,
2303
                    'value' => true,
2304
                    'languageCode' => 'eng-US',
2305
                    'fieldDefIdentifier' => 'description',
2306
                )
2307
            ),
2308
            new Field(
2309
                array(
2310
                    'id' => 0,
2311
                    'value' => true,
2312
                    'languageCode' => 'eng-US',
2313
                    'fieldDefIdentifier' => 'name',
2314
                )
2315
            ),
2316
        );
2317
2318
        $this->assertEquals($expected, $actual);
2319
2320
        // Will return a content instance with fields in "eng-GB" (versions prior to 6.0.0-beta9 returned "eng-US" also)
2321
        $reloadedContent = $contentService->loadContentByContentInfo(
2322
            $publishedContent->contentInfo,
2323
            array(
2324
                'eng-GB',
2325
            ),
2326
            null,
2327
            true
2328
        );
2329
2330
        $actual = $this->normalizeFields($reloadedContent->getFields());
2331
2332
        $expected = array(
2333
            new Field(
2334
                array(
2335
                    'id' => 0,
2336
                    'value' => true,
2337
                    'languageCode' => 'eng-GB',
2338
                    'fieldDefIdentifier' => 'description',
2339
                )
2340
            ),
2341
            new Field(
2342
                array(
2343
                    'id' => 0,
2344
                    'value' => true,
2345
                    'languageCode' => 'eng-GB',
2346
                    'fieldDefIdentifier' => 'name',
2347
                )
2348
            ),
2349
        );
2350
2351
        $this->assertEquals($expected, $actual);
2352
2353
        // Will return a content instance with fields in main language "eng-US", as "fre-FR" does not exists
2354
        $reloadedContent = $contentService->loadContentByContentInfo(
2355
            $publishedContent->contentInfo,
2356
            array(
2357
                'fre-FR',
2358
            ),
2359
            null,
2360
            true
2361
        );
2362
2363
        $actual = $this->normalizeFields($reloadedContent->getFields());
2364
2365
        $expected = array(
2366
            new Field(
2367
                array(
2368
                    'id' => 0,
2369
                    'value' => true,
2370
                    'languageCode' => 'eng-US',
2371
                    'fieldDefIdentifier' => 'description',
2372
                )
2373
            ),
2374
            new Field(
2375
                array(
2376
                    'id' => 0,
2377
                    'value' => true,
2378
                    'languageCode' => 'eng-US',
2379
                    'fieldDefIdentifier' => 'name',
2380
                )
2381
            ),
2382
        );
2383
2384
        $this->assertEquals($expected, $actual);
2385
    }
2386
2387
    /**
2388
     * Test for the loadContentByContentInfo() method.
2389
     *
2390
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2391
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2392
     */
2393 View Code Duplication
    public function testLoadContentByContentInfoWithVersionNumberParameter()
2394
    {
2395
        $repository = $this->getRepository();
2396
2397
        $contentService = $repository->getContentService();
2398
2399
        /* BEGIN: Use Case */
2400
        $publishedContent = $this->createContentVersion1();
2401
2402
        $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...
2403
2404
        // This content instance is identical to $draftContent
2405
        $draftContentReloaded = $contentService->loadContentByContentInfo(
2406
            $publishedContent->contentInfo,
2407
            null,
2408
            2
2409
        );
2410
        /* END: Use Case */
2411
2412
        $this->assertEquals(
2413
            2,
2414
            $draftContentReloaded->getVersionInfo()->versionNo
2415
        );
2416
2417
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2418
        $this->assertEquals(
2419
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2420
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2421
        );
2422
    }
2423
2424
    /**
2425
     * Test for the loadContentByContentInfo() method.
2426
     *
2427
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2428
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2429
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithVersionNumberParameter
2430
     */
2431 View Code Duplication
    public function testLoadContentByContentInfoThrowsNotFoundExceptionWithVersionNumberParameter()
2432
    {
2433
        $repository = $this->getRepository();
2434
2435
        $contentService = $repository->getContentService();
2436
2437
        /* BEGIN: Use Case */
2438
        $content = $this->createContentVersion1();
2439
2440
        // This call will fail with a "NotFoundException", because no content
2441
        // with versionNo = 2 exists.
2442
        $contentService->loadContentByContentInfo($content->contentInfo, null, 2);
2443
        /* END: Use Case */
2444
    }
2445
2446
    /**
2447
     * Test for the loadContent() method.
2448
     *
2449
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages)
2450
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2451
     */
2452
    public function testLoadContentWithSecondParameter()
2453
    {
2454
        $repository = $this->getRepository();
2455
2456
        $contentService = $repository->getContentService();
2457
2458
        /* BEGIN: Use Case */
2459
        $draft = $this->createMultipleLanguageDraftVersion1();
2460
2461
        // This draft contains those fields localized with "eng-GB"
2462
        $draftLocalized = $contentService->loadContent($draft->id, array('eng-GB'), null, false);
2463
        /* END: Use Case */
2464
2465
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2466
    }
2467
2468
    /**
2469
     * Test for the loadContent() method.
2470
     *
2471
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2472
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2473
     */
2474 View Code Duplication
    public function testLoadContentWithThirdParameter()
2475
    {
2476
        $repository = $this->getRepository();
2477
2478
        $contentService = $repository->getContentService();
2479
2480
        /* BEGIN: Use Case */
2481
        $publishedContent = $this->createContentVersion1();
2482
2483
        $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...
2484
2485
        // This content instance is identical to $draftContent
2486
        $draftContentReloaded = $contentService->loadContent($publishedContent->id, null, 2);
2487
        /* END: Use Case */
2488
2489
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2490
2491
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2492
        $this->assertEquals(
2493
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2494
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2495
        );
2496
    }
2497
2498
    /**
2499
     * Test for the loadContent() method.
2500
     *
2501
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2502
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2503
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithThirdParameter
2504
     */
2505 View Code Duplication
    public function testLoadContentThrowsNotFoundExceptionWithThirdParameter()
2506
    {
2507
        $repository = $this->getRepository();
2508
2509
        $contentService = $repository->getContentService();
2510
2511
        /* BEGIN: Use Case */
2512
        $content = $this->createContentVersion1();
2513
2514
        // This call will fail with a "NotFoundException", because for this
2515
        // content object no versionNo=2 exists.
2516
        $contentService->loadContent($content->id, null, 2);
2517
        /* END: Use Case */
2518
    }
2519
2520
    /**
2521
     * Test for the loadContentByRemoteId() method.
2522
     *
2523
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages)
2524
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2525
     */
2526
    public function testLoadContentByRemoteIdWithSecondParameter()
2527
    {
2528
        $repository = $this->getRepository();
2529
2530
        $contentService = $repository->getContentService();
2531
2532
        /* BEGIN: Use Case */
2533
        $draft = $this->createMultipleLanguageDraftVersion1();
2534
2535
        $contentService->publishVersion($draft->versionInfo);
2536
2537
        // This draft contains those fields localized with "eng-GB"
2538
        $draftLocalized = $contentService->loadContentByRemoteId(
2539
            $draft->contentInfo->remoteId,
2540
            array('eng-GB'),
2541
            null,
2542
            false
2543
        );
2544
        /* END: Use Case */
2545
2546
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2547
    }
2548
2549
    /**
2550
     * Test for the loadContentByRemoteId() method.
2551
     *
2552
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2553
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2554
     */
2555 View Code Duplication
    public function testLoadContentByRemoteIdWithThirdParameter()
2556
    {
2557
        $repository = $this->getRepository();
2558
2559
        $contentService = $repository->getContentService();
2560
2561
        /* BEGIN: Use Case */
2562
        $publishedContent = $this->createContentVersion1();
2563
2564
        $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...
2565
2566
        // This content instance is identical to $draftContent
2567
        $draftContentReloaded = $contentService->loadContentByRemoteId(
2568
            $publishedContent->contentInfo->remoteId,
2569
            null,
2570
            2
2571
        );
2572
        /* END: Use Case */
2573
2574
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2575
2576
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2577
        $this->assertEquals(
2578
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2579
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2580
        );
2581
    }
2582
2583
    /**
2584
     * Test for the loadContentByRemoteId() method.
2585
     *
2586
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2587
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2588
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithThirdParameter
2589
     */
2590
    public function testLoadContentByRemoteIdThrowsNotFoundExceptionWithThirdParameter()
2591
    {
2592
        $repository = $this->getRepository();
2593
2594
        $contentService = $repository->getContentService();
2595
2596
        /* BEGIN: Use Case */
2597
        $content = $this->createContentVersion1();
2598
2599
        // This call will fail with a "NotFoundException", because for this
2600
        // content object no versionNo=2 exists.
2601
        $contentService->loadContentByRemoteId(
2602
            $content->contentInfo->remoteId,
2603
            null,
2604
            2
2605
        );
2606
        /* END: Use Case */
2607
    }
2608
2609
    /**
2610
     * Test for the deleteVersion() method.
2611
     *
2612
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2613
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2614
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2615
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2616
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2617
     */
2618
    public function testDeleteVersion()
2619
    {
2620
        $repository = $this->getRepository();
2621
2622
        $contentService = $repository->getContentService();
2623
2624
        /* BEGIN: Use Case */
2625
        $content = $this->createContentVersion1();
2626
2627
        // Create new draft, because published or last version of the Content can't be deleted
2628
        $draft = $contentService->createContentDraft(
2629
            $content->getVersionInfo()->getContentInfo()
2630
        );
2631
2632
        // Delete the previously created draft
2633
        $contentService->deleteVersion($draft->getVersionInfo());
2634
        /* END: Use Case */
2635
2636
        $versions = $contentService->loadVersions($content->getVersionInfo()->getContentInfo());
2637
2638
        $this->assertCount(1, $versions);
2639
        $this->assertEquals(
2640
            $content->getVersionInfo()->id,
2641
            $versions[0]->id
2642
        );
2643
    }
2644
2645
    /**
2646
     * Test for the deleteVersion() method.
2647
     *
2648
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2649
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
2650
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2651
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2652
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2653
     */
2654
    public function testDeleteVersionThrowsBadStateExceptionOnPublishedVersion()
2655
    {
2656
        $repository = $this->getRepository();
2657
2658
        $contentService = $repository->getContentService();
2659
2660
        /* BEGIN: Use Case */
2661
        $content = $this->createContentVersion1();
2662
2663
        // This call will fail with a "BadStateException", because the content
2664
        // version is currently published.
2665
        $contentService->deleteVersion($content->getVersionInfo());
2666
        /* END: Use Case */
2667
    }
2668
2669
    /**
2670
     * Test for the deleteVersion() method.
2671
     *
2672
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2673
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
2674
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2675
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2676
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2677
     */
2678 View Code Duplication
    public function testDeleteVersionThrowsBadStateExceptionOnLastVersion()
2679
    {
2680
        $repository = $this->getRepository();
2681
2682
        $contentService = $repository->getContentService();
2683
2684
        /* BEGIN: Use Case */
2685
        $draft = $this->createContentDraftVersion1();
2686
2687
        // This call will fail with a "BadStateException", because the Content
2688
        // version is the last version of the Content.
2689
        $contentService->deleteVersion($draft->getVersionInfo());
2690
        /* END: Use Case */
2691
    }
2692
2693
    /**
2694
     * Test for the loadVersions() method.
2695
     *
2696
     * @see \eZ\Publish\API\Repository\ContentService::loadVersions()
2697
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2698
     */
2699
    public function testLoadVersions()
2700
    {
2701
        $repository = $this->getRepository();
2702
2703
        $contentService = $repository->getContentService();
2704
2705
        /* BEGIN: Use Case */
2706
        $contentVersion2 = $this->createContentVersion2();
2707
2708
        // Load versions of this ContentInfo instance
2709
        $versions = $contentService->loadVersions($contentVersion2->contentInfo);
2710
        /* END: Use Case */
2711
2712
        $expectedVersionIds = array(
2713
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1)->id => true,
2714
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 2)->id => true,
2715
        );
2716
2717
        foreach ($versions as $actualVersion) {
2718
            if (!isset($expectedVersionIds[$actualVersion->id])) {
2719
                $this->fail("Unexpected version with ID '{$actualVersion->id}' loaded.");
2720
            }
2721
            unset($expectedVersionIds[$actualVersion->id]);
2722
        }
2723
2724
        if (!empty($expectedVersionIds)) {
2725
            $this->fail(
2726
                sprintf(
2727
                    "Expected versions not loaded: '%s'",
2728
                    implode("', '", $expectedVersionIds)
2729
                )
2730
            );
2731
        }
2732
    }
2733
2734
    /**
2735
     * Test for the copyContent() method.
2736
     *
2737
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
2738
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2739
     * @group field-type
2740
     */
2741 View Code Duplication
    public function testCopyContent()
2742
    {
2743
        $parentLocationId = $this->generateId('location', 56);
2744
2745
        $repository = $this->getRepository();
2746
2747
        $contentService = $repository->getContentService();
2748
        $locationService = $repository->getLocationService();
2749
2750
        /* BEGIN: Use Case */
2751
        $contentVersion2 = $this->createMultipleLanguageContentVersion2();
2752
2753
        // Configure new target location
2754
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
2755
2756
        $targetLocationCreate->priority = 42;
2757
        $targetLocationCreate->hidden = true;
2758
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
2759
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
2760
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
2761
2762
        // Copy content with all versions and drafts
2763
        $contentCopied = $contentService->copyContent(
2764
            $contentVersion2->contentInfo,
2765
            $targetLocationCreate
2766
        );
2767
        /* END: Use Case */
2768
2769
        $this->assertInstanceOf(
2770
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
2771
            $contentCopied
2772
        );
2773
2774
        $this->assertNotEquals(
2775
            $contentVersion2->contentInfo->remoteId,
2776
            $contentCopied->contentInfo->remoteId
2777
        );
2778
2779
        $this->assertNotEquals(
2780
            $contentVersion2->id,
2781
            $contentCopied->id
2782
        );
2783
2784
        $this->assertEquals(
2785
            2,
2786
            count($contentService->loadVersions($contentCopied->contentInfo))
2787
        );
2788
2789
        $this->assertEquals(2, $contentCopied->getVersionInfo()->versionNo);
2790
2791
        $this->assertAllFieldsEquals($contentCopied->getFields());
2792
2793
        $this->assertDefaultContentStates($contentCopied->contentInfo);
2794
2795
        $this->assertNotNull(
2796
            $contentCopied->contentInfo->mainLocationId,
2797
            'Expected main location to be set given we provided a LocationCreateStruct'
2798
        );
2799
    }
2800
2801
    /**
2802
     * Test for the copyContent() method.
2803
     *
2804
     * @see \eZ\Publish\API\Repository\ContentService::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo)
2805
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
2806
     *
2807
     * @todo Fix to more descriptive name
2808
     */
2809 View Code Duplication
    public function testCopyContentWithThirdParameter()
2810
    {
2811
        $parentLocationId = $this->generateId('location', 56);
2812
2813
        $repository = $this->getRepository();
2814
2815
        $contentService = $repository->getContentService();
2816
        $locationService = $repository->getLocationService();
2817
2818
        /* BEGIN: Use Case */
2819
        $contentVersion2 = $this->createContentVersion2();
2820
2821
        // Configure new target location
2822
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
2823
2824
        $targetLocationCreate->priority = 42;
2825
        $targetLocationCreate->hidden = true;
2826
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
2827
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
2828
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
2829
2830
        // Copy only the initial version
2831
        $contentCopied = $contentService->copyContent(
2832
            $contentVersion2->contentInfo,
2833
            $targetLocationCreate,
2834
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1)
2835
        );
2836
        /* END: Use Case */
2837
2838
        $this->assertInstanceOf(
2839
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
2840
            $contentCopied
2841
        );
2842
2843
        $this->assertNotEquals(
2844
            $contentVersion2->contentInfo->remoteId,
2845
            $contentCopied->contentInfo->remoteId
2846
        );
2847
2848
        $this->assertNotEquals(
2849
            $contentVersion2->id,
2850
            $contentCopied->id
2851
        );
2852
2853
        $this->assertEquals(
2854
            1,
2855
            count($contentService->loadVersions($contentCopied->contentInfo))
2856
        );
2857
2858
        $this->assertEquals(1, $contentCopied->getVersionInfo()->versionNo);
2859
2860
        $this->assertNotNull(
2861
            $contentCopied->contentInfo->mainLocationId,
2862
            'Expected main location to be set given we provided a LocationCreateStruct'
2863
        );
2864
    }
2865
2866
    /**
2867
     * Test for the addRelation() method.
2868
     *
2869
     * @return \eZ\Publish\API\Repository\Values\Content\Content
2870
     *
2871
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
2872
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2873
     */
2874
    public function testAddRelation()
2875
    {
2876
        $repository = $this->getRepository();
2877
2878
        $contentService = $repository->getContentService();
2879
2880
        /* BEGIN: Use Case */
2881
        // RemoteId of the "Media" content of an eZ Publish demo installation
2882
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2883
2884
        $draft = $this->createContentDraftVersion1();
2885
2886
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2887
2888
        // Create relation between new content object and "Media" page
2889
        $relation = $contentService->addRelation(
2890
            $draft->getVersionInfo(),
2891
            $media
2892
        );
2893
        /* END: Use Case */
2894
2895
        $this->assertInstanceOf(
2896
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Relation',
2897
            $relation
2898
        );
2899
2900
        return $contentService->loadRelations($draft->getVersionInfo());
2901
    }
2902
2903
    /**
2904
     * Test for the addRelation() method.
2905
     *
2906
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
2907
     *
2908
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
2909
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
2910
     */
2911
    public function testAddRelationAddsRelationToContent($relations)
2912
    {
2913
        $this->assertEquals(
2914
            1,
2915
            count($relations)
2916
        );
2917
    }
2918
2919
    /**
2920
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
2921
     */
2922
    protected function assertExpectedRelations($relations)
2923
    {
2924
        $this->assertEquals(
2925
            array(
2926
                'type' => Relation::COMMON,
2927
                'sourceFieldDefinitionIdentifier' => null,
2928
                'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
2929
                'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
2930
            ),
2931
            array(
2932
                'type' => $relations[0]->type,
2933
                'sourceFieldDefinitionIdentifier' => $relations[0]->sourceFieldDefinitionIdentifier,
2934
                'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
2935
                'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
2936
            )
2937
        );
2938
    }
2939
2940
    /**
2941
     * Test for the addRelation() method.
2942
     *
2943
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
2944
     *
2945
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
2946
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
2947
     */
2948
    public function testAddRelationSetsExpectedRelations($relations)
2949
    {
2950
        $this->assertExpectedRelations($relations);
2951
    }
2952
2953
    /**
2954
     * Test for the createContentDraft() method.
2955
     *
2956
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
2957
     *
2958
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
2959
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelationSetsExpectedRelations
2960
     */
2961 View Code Duplication
    public function testCreateContentDraftWithRelations()
2962
    {
2963
        $repository = $this->getRepository();
2964
2965
        $contentService = $repository->getContentService();
2966
2967
        // RemoteId of the "Media" content of an eZ Publish demo installation
2968
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2969
        $draft = $this->createContentDraftVersion1();
2970
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2971
2972
        // Create relation between new content object and "Media" page
2973
        $contentService->addRelation(
2974
            $draft->getVersionInfo(),
2975
            $media
2976
        );
2977
2978
        $content = $contentService->publishVersion($draft->versionInfo);
2979
        $newDraft = $contentService->createContentDraft($content->contentInfo);
2980
2981
        return $contentService->loadRelations($newDraft->getVersionInfo());
2982
    }
2983
2984
    /**
2985
     * Test for the createContentDraft() method.
2986
     *
2987
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
2988
     *
2989
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
2990
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelations
2991
     */
2992
    public function testCreateContentDraftWithRelationsCreatesRelations($relations)
2993
    {
2994
        $this->assertEquals(
2995
            1,
2996
            count($relations)
2997
        );
2998
2999
        return $relations;
3000
    }
3001
3002
    /**
3003
     * Test for the createContentDraft() method.
3004
     *
3005
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
3006
     *
3007
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelationsCreatesRelations
3008
     */
3009
    public function testCreateContentDraftWithRelationsCreatesExpectedRelations($relations)
3010
    {
3011
        $this->assertExpectedRelations($relations);
3012
    }
3013
3014
    /**
3015
     * Test for the addRelation() method.
3016
     *
3017
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
3018
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3019
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3020
     */
3021 View Code Duplication
    public function testAddRelationThrowsBadStateException()
3022
    {
3023
        $repository = $this->getRepository();
3024
3025
        $contentService = $repository->getContentService();
3026
3027
        /* BEGIN: Use Case */
3028
        // RemoteId of the "Media" page of an eZ Publish demo installation
3029
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3030
3031
        $content = $this->createContentVersion1();
3032
3033
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3034
3035
        // This call will fail with a "BadStateException", because content is
3036
        // published and not a draft.
3037
        $contentService->addRelation(
3038
            $content->getVersionInfo(),
3039
            $media
3040
        );
3041
        /* END: Use Case */
3042
    }
3043
3044
    /**
3045
     * Test for the loadRelations() method.
3046
     *
3047
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3048
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3049
     */
3050
    public function testLoadRelations()
3051
    {
3052
        $repository = $this->getRepository();
3053
3054
        $contentService = $repository->getContentService();
3055
3056
        /* BEGIN: Use Case */
3057
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3058
        // of a eZ Publish demo installation.
3059
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3060
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3061
3062
        $draft = $this->createContentDraftVersion1();
3063
3064
        // Load other content objects
3065
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3066
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3067
3068
        // Create relation between new content object and "Media" page
3069
        $contentService->addRelation(
3070
            $draft->getVersionInfo(),
3071
            $media
3072
        );
3073
3074
        // Create another relation with the "Demo Design" page
3075
        $contentService->addRelation(
3076
            $draft->getVersionInfo(),
3077
            $demoDesign
3078
        );
3079
3080
        // Load all relations
3081
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3082
        /* END: Use Case */
3083
3084
        usort(
3085
            $relations,
3086
            function ($rel1, $rel2) {
3087
                return strcasecmp(
3088
                    $rel2->getDestinationContentInfo()->remoteId,
3089
                    $rel1->getDestinationContentInfo()->remoteId
3090
                );
3091
            }
3092
        );
3093
3094
        $this->assertEquals(
3095
            array(
3096
                array(
3097
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3098
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3099
                ),
3100
                array(
3101
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3102
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3103
                ),
3104
            ),
3105
            array(
3106
                array(
3107
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3108
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3109
                ),
3110
                array(
3111
                    'sourceContentInfo' => $relations[1]->sourceContentInfo->remoteId,
3112
                    'destinationContentInfo' => $relations[1]->destinationContentInfo->remoteId,
3113
                ),
3114
            )
3115
        );
3116
    }
3117
3118
    /**
3119
     * Test for the loadRelations() method.
3120
     *
3121
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3122
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3123
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3124
     */
3125
    public function testLoadRelationsSkipsArchivedContent()
3126
    {
3127
        $repository = $this->getRepository();
3128
3129
        $contentService = $repository->getContentService();
3130
3131
        /* BEGIN: Use Case */
3132
        $trashService = $repository->getTrashService();
3133
        $locationService = $repository->getLocationService();
3134
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3135
        // of a eZ Publish demo installation.
3136
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3137
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3138
3139
        $draft = $this->createContentDraftVersion1();
3140
3141
        // Load other content objects
3142
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3143
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3144
3145
        // Create relation between new content object and "Media" page
3146
        $contentService->addRelation(
3147
            $draft->getVersionInfo(),
3148
            $media
3149
        );
3150
3151
        // Create another relation with the "Demo Design" page
3152
        $contentService->addRelation(
3153
            $draft->getVersionInfo(),
3154
            $demoDesign
3155
        );
3156
3157
        $demoDesignLocation = $locationService->loadLocation($demoDesign->mainLocationId);
3158
3159
        // Trashing Content's last Location will change its status to archived,
3160
        // in this case relation towards it will not be loaded.
3161
        $trashService->trash($demoDesignLocation);
3162
3163
        // Load all relations
3164
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3165
        /* END: Use Case */
3166
3167
        $this->assertCount(1, $relations);
3168
        $this->assertEquals(
3169
            array(
3170
                array(
3171
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3172
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3173
                ),
3174
            ),
3175
            array(
3176
                array(
3177
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3178
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3179
                ),
3180
            )
3181
        );
3182
    }
3183
3184
    /**
3185
     * Test for the loadRelations() method.
3186
     *
3187
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3188
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3189
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3190
     */
3191
    public function testLoadRelationsSkipsDraftContent()
3192
    {
3193
        $repository = $this->getRepository();
3194
3195
        $contentService = $repository->getContentService();
3196
3197
        /* BEGIN: Use Case */
3198
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3199
        // of a eZ Publish demo installation.
3200
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3201
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3202
3203
        $draft = $this->createContentDraftVersion1();
3204
3205
        // Load other content objects
3206
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3207
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3208
3209
        // Create draft of "Media" page
3210
        $mediaDraft = $contentService->createContentDraft($media->contentInfo);
3211
3212
        // Create relation between "Media" page and new content object draft.
3213
        // This relation will not be loaded before the draft is published.
3214
        $contentService->addRelation(
3215
            $mediaDraft->getVersionInfo(),
3216
            $draft->getVersionInfo()->getContentInfo()
3217
        );
3218
3219
        // Create another relation with the "Demo Design" page
3220
        $contentService->addRelation(
3221
            $mediaDraft->getVersionInfo(),
3222
            $demoDesign
3223
        );
3224
3225
        // Load all relations
3226
        $relations = $contentService->loadRelations($mediaDraft->getVersionInfo());
3227
        /* END: Use Case */
3228
3229
        $this->assertCount(1, $relations);
3230
        $this->assertEquals(
3231
            array(
3232
                array(
3233
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3234
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3235
                ),
3236
            ),
3237
            array(
3238
                array(
3239
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3240
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3241
                ),
3242
            )
3243
        );
3244
    }
3245
3246
    /**
3247
     * Test for the loadReverseRelations() method.
3248
     *
3249
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3250
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3251
     */
3252
    public function testLoadReverseRelations()
3253
    {
3254
        $repository = $this->getRepository();
3255
3256
        $contentService = $repository->getContentService();
3257
3258
        /* BEGIN: Use Case */
3259
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3260
        // of a eZ Publish demo installation.
3261
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3262
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3263
3264
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3265
        $contentInfo = $versionInfo->getContentInfo();
3266
3267
        // Create some drafts
3268
        $mediaDraft = $contentService->createContentDraft(
3269
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3270
        );
3271
        $demoDesignDraft = $contentService->createContentDraft(
3272
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3273
        );
3274
3275
        // Create relation between new content object and "Media" page
3276
        $relation1 = $contentService->addRelation(
3277
            $mediaDraft->getVersionInfo(),
3278
            $contentInfo
3279
        );
3280
3281
        // Create another relation with the "Demo Design" page
3282
        $relation2 = $contentService->addRelation(
3283
            $demoDesignDraft->getVersionInfo(),
3284
            $contentInfo
3285
        );
3286
3287
        // Publish drafts, so relations become active
3288
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3289
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3290
3291
        // Load all relations
3292
        $relations = $contentService->loadRelations($versionInfo);
3293
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3294
        /* END: Use Case */
3295
3296
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3297
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3298
3299
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3300
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3301
3302
        $this->assertEquals(0, count($relations));
3303
        $this->assertEquals(2, count($reverseRelations));
3304
3305
        usort(
3306
            $reverseRelations,
3307
            function ($rel1, $rel2) {
3308
                return strcasecmp(
3309
                    $rel2->getSourceContentInfo()->remoteId,
3310
                    $rel1->getSourceContentInfo()->remoteId
3311
                );
3312
            }
3313
        );
3314
3315
        $this->assertEquals(
3316
            array(
3317
                array(
3318
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3319
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3320
                ),
3321
                array(
3322
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3323
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3324
                ),
3325
            ),
3326
            array(
3327
                array(
3328
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3329
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3330
                ),
3331
                array(
3332
                    'sourceContentInfo' => $reverseRelations[1]->sourceContentInfo->remoteId,
3333
                    'destinationContentInfo' => $reverseRelations[1]->destinationContentInfo->remoteId,
3334
                ),
3335
            )
3336
        );
3337
    }
3338
3339
    /**
3340
     * Test for the loadReverseRelations() method.
3341
     *
3342
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3343
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3344
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3345
     */
3346
    public function testLoadReverseRelationsSkipsArchivedContent()
3347
    {
3348
        $repository = $this->getRepository();
3349
3350
        $contentService = $repository->getContentService();
3351
3352
        /* BEGIN: Use Case */
3353
        $trashService = $repository->getTrashService();
3354
        $locationService = $repository->getLocationService();
3355
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3356
        // of a eZ Publish demo installation.
3357
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3358
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3359
3360
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3361
        $contentInfo = $versionInfo->getContentInfo();
3362
3363
        // Create some drafts
3364
        $mediaDraft = $contentService->createContentDraft(
3365
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3366
        );
3367
        $demoDesignDraft = $contentService->createContentDraft(
3368
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3369
        );
3370
3371
        // Create relation between new content object and "Media" page
3372
        $relation1 = $contentService->addRelation(
3373
            $mediaDraft->getVersionInfo(),
3374
            $contentInfo
3375
        );
3376
3377
        // Create another relation with the "Demo Design" page
3378
        $relation2 = $contentService->addRelation(
3379
            $demoDesignDraft->getVersionInfo(),
3380
            $contentInfo
3381
        );
3382
3383
        // Publish drafts, so relations become active
3384
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3385
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3386
3387
        $demoDesignLocation = $locationService->loadLocation($demoDesignDraft->contentInfo->mainLocationId);
3388
3389
        // Trashing Content's last Location will change its status to archived,
3390
        // in this case relation from it will not be loaded.
3391
        $trashService->trash($demoDesignLocation);
3392
3393
        // Load all relations
3394
        $relations = $contentService->loadRelations($versionInfo);
3395
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3396
        /* END: Use Case */
3397
3398
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3399
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3400
3401
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3402
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3403
3404
        $this->assertEquals(0, count($relations));
3405
        $this->assertEquals(1, count($reverseRelations));
3406
3407
        $this->assertEquals(
3408
            array(
3409
                array(
3410
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3411
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3412
                ),
3413
            ),
3414
            array(
3415
                array(
3416
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3417
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3418
                ),
3419
            )
3420
        );
3421
    }
3422
3423
    /**
3424
     * Test for the loadReverseRelations() method.
3425
     *
3426
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3427
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3428
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3429
     */
3430
    public function testLoadReverseRelationsSkipsDraftContent()
3431
    {
3432
        $repository = $this->getRepository();
3433
3434
        $contentService = $repository->getContentService();
3435
3436
        /* BEGIN: Use Case */
3437
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3438
        // of a eZ Publish demo installation.
3439
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3440
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3441
3442
        // Load "Media" page Content
3443
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3444
3445
        // Create some drafts
3446
        $newDraftVersionInfo = $this->createContentDraftVersion1()->getVersionInfo();
3447
        $demoDesignDraft = $contentService->createContentDraft(
3448
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3449
        );
3450
3451
        // Create relation between "Media" page and new content object
3452
        $relation1 = $contentService->addRelation(
3453
            $newDraftVersionInfo,
3454
            $media->contentInfo
3455
        );
3456
3457
        // Create another relation with the "Demo Design" page
3458
        $relation2 = $contentService->addRelation(
3459
            $demoDesignDraft->getVersionInfo(),
3460
            $media->contentInfo
3461
        );
3462
3463
        // Publish drafts, so relations become active
3464
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3465
        // We will not publish new Content draft, therefore relation from it
3466
        // will not be loaded as reverse relation for "Media" page
3467
        //$contentService->publishVersion( $newDraftVersionInfo );
3468
3469
        // Load all relations
3470
        $relations = $contentService->loadRelations($media->versionInfo);
3471
        $reverseRelations = $contentService->loadReverseRelations($media->contentInfo);
3472
        /* END: Use Case */
3473
3474
        $this->assertEquals($media->contentInfo->id, $relation1->getDestinationContentInfo()->id);
3475
        $this->assertEquals($newDraftVersionInfo->contentInfo->id, $relation1->getSourceContentInfo()->id);
3476
3477
        $this->assertEquals($media->contentInfo->id, $relation2->getDestinationContentInfo()->id);
3478
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3479
3480
        $this->assertEquals(0, count($relations));
3481
        $this->assertEquals(1, count($reverseRelations));
3482
3483
        $this->assertEquals(
3484
            array(
3485
                array(
3486
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3487
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3488
                ),
3489
            ),
3490
            array(
3491
                array(
3492
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3493
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3494
                ),
3495
            )
3496
        );
3497
    }
3498
3499
    /**
3500
     * Test for the deleteRelation() method.
3501
     *
3502
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3503
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3504
     */
3505
    public function testDeleteRelation()
3506
    {
3507
        $repository = $this->getRepository();
3508
3509
        $contentService = $repository->getContentService();
3510
3511
        /* BEGIN: Use Case */
3512
        // Remote ids of the "Media" and the "Demo Design" page of a eZ Publish
3513
        // demo installation.
3514
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3515
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3516
3517
        $draft = $this->createContentDraftVersion1();
3518
3519
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3520
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3521
3522
        // Establish some relations
3523
        $contentService->addRelation($draft->getVersionInfo(), $media);
3524
        $contentService->addRelation($draft->getVersionInfo(), $demoDesign);
3525
3526
        // Delete one of the currently created relations
3527
        $contentService->deleteRelation($draft->getVersionInfo(), $media);
3528
3529
        // The relations array now contains only one element
3530
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3531
        /* END: Use Case */
3532
3533
        $this->assertEquals(1, count($relations));
3534
    }
3535
3536
    /**
3537
     * Test for the deleteRelation() method.
3538
     *
3539
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3540
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3541
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3542
     */
3543 View Code Duplication
    public function testDeleteRelationThrowsBadStateException()
3544
    {
3545
        $repository = $this->getRepository();
3546
3547
        $contentService = $repository->getContentService();
3548
3549
        /* BEGIN: Use Case */
3550
        // RemoteId of the "Media" page of an eZ Publish demo installation
3551
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3552
3553
        $content = $this->createContentVersion1();
3554
3555
        // Load the destination object
3556
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3557
3558
        // Create a new draft
3559
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
3560
3561
        // Add a relation
3562
        $contentService->addRelation($draftVersion2->getVersionInfo(), $media);
3563
3564
        // Publish new version
3565
        $contentVersion2 = $contentService->publishVersion(
3566
            $draftVersion2->getVersionInfo()
3567
        );
3568
3569
        // This call will fail with a "BadStateException", because content is
3570
        // published and not a draft.
3571
        $contentService->deleteRelation(
3572
            $contentVersion2->getVersionInfo(),
3573
            $media
3574
        );
3575
        /* END: Use Case */
3576
    }
3577
3578
    /**
3579
     * Test for the deleteRelation() method.
3580
     *
3581
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3582
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
3583
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3584
     */
3585 View Code Duplication
    public function testDeleteRelationThrowsInvalidArgumentException()
3586
    {
3587
        $repository = $this->getRepository();
3588
3589
        $contentService = $repository->getContentService();
3590
3591
        /* BEGIN: Use Case */
3592
        // RemoteId of the "Media" page of an eZ Publish demo installation
3593
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3594
3595
        $draft = $this->createContentDraftVersion1();
3596
3597
        // Load the destination object
3598
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3599
3600
        // This call will fail with a "InvalidArgumentException", because no
3601
        // relation exists between $draft and $media.
3602
        $contentService->deleteRelation(
3603
            $draft->getVersionInfo(),
3604
            $media
3605
        );
3606
        /* END: Use Case */
3607
    }
3608
3609
    /**
3610
     * Test for the createContent() method.
3611
     *
3612
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
3613
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3614
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3615
     */
3616
    public function testCreateContentInTransactionWithRollback()
3617
    {
3618
        if ($this->isVersion4()) {
3619
            $this->markTestSkipped('This test requires eZ Publish 5');
3620
        }
3621
3622
        $repository = $this->getRepository();
3623
3624
        /* BEGIN: Use Case */
3625
        $contentTypeService = $repository->getContentTypeService();
3626
        $contentService = $repository->getContentService();
3627
3628
        // Start a transaction
3629
        $repository->beginTransaction();
3630
3631
        try {
3632
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
3633
3634
            // Get a content create struct and set mandatory properties
3635
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
3636
            $contentCreate->setField('name', 'Sindelfingen forum');
3637
3638
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
3639
            $contentCreate->alwaysAvailable = true;
3640
3641
            // Create a new content object
3642
            $contentId = $contentService->createContent($contentCreate)->id;
3643
        } catch (Exception $e) {
3644
            // Cleanup hanging transaction on error
3645
            $repository->rollback();
3646
            throw $e;
3647
        }
3648
3649
        // Rollback all changes
3650
        $repository->rollback();
3651
3652
        try {
3653
            // This call will fail with a "NotFoundException"
3654
            $contentService->loadContent($contentId);
3655
        } catch (NotFoundException $e) {
3656
            // This is expected
3657
            return;
3658
        }
3659
        /* END: Use Case */
3660
3661
        $this->fail('Content object still exists after rollback.');
3662
    }
3663
3664
    /**
3665
     * Test for the createContent() method.
3666
     *
3667
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
3668
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3669
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3670
     */
3671
    public function testCreateContentInTransactionWithCommit()
3672
    {
3673
        if ($this->isVersion4()) {
3674
            $this->markTestSkipped('This test requires eZ Publish 5');
3675
        }
3676
3677
        $repository = $this->getRepository();
3678
3679
        /* BEGIN: Use Case */
3680
        $contentTypeService = $repository->getContentTypeService();
3681
        $contentService = $repository->getContentService();
3682
3683
        // Start a transaction
3684
        $repository->beginTransaction();
3685
3686
        try {
3687
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
3688
3689
            // Get a content create struct and set mandatory properties
3690
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
3691
            $contentCreate->setField('name', 'Sindelfingen forum');
3692
3693
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
3694
            $contentCreate->alwaysAvailable = true;
3695
3696
            // Create a new content object
3697
            $contentId = $contentService->createContent($contentCreate)->id;
3698
3699
            // Commit changes
3700
            $repository->commit();
3701
        } catch (Exception $e) {
3702
            // Cleanup hanging transaction on error
3703
            $repository->rollback();
3704
            throw $e;
3705
        }
3706
3707
        // Load the new content object
3708
        $content = $contentService->loadContent($contentId);
3709
        /* END: Use Case */
3710
3711
        $this->assertEquals($contentId, $content->id);
3712
    }
3713
3714
    /**
3715
     * Test for the createContent() method.
3716
     *
3717
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
3718
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
3719
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
3720
     */
3721
    public function testCreateContentWithLocationCreateParameterInTransactionWithRollback()
3722
    {
3723
        $repository = $this->getRepository();
3724
3725
        $contentService = $repository->getContentService();
3726
3727
        /* BEGIN: Use Case */
3728
        // Start a transaction
3729
        $repository->beginTransaction();
3730
3731
        try {
3732
            $draft = $this->createContentDraftVersion1();
3733
        } catch (Exception $e) {
3734
            // Cleanup hanging transaction on error
3735
            $repository->rollback();
3736
            throw $e;
3737
        }
3738
3739
        $contentId = $draft->id;
3740
3741
        // Roleback the transaction
3742
        $repository->rollback();
3743
3744
        try {
3745
            // This call will fail with a "NotFoundException"
3746
            $contentService->loadContent($contentId);
3747
        } catch (NotFoundException $e) {
3748
            return;
3749
        }
3750
        /* END: Use Case */
3751
3752
        $this->fail('Can still load content object after rollback.');
3753
    }
3754
3755
    /**
3756
     * Test for the createContent() method.
3757
     *
3758
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
3759
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
3760
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
3761
     */
3762 View Code Duplication
    public function testCreateContentWithLocationCreateParameterInTransactionWithCommit()
3763
    {
3764
        $repository = $this->getRepository();
3765
3766
        $contentService = $repository->getContentService();
3767
3768
        /* BEGIN: Use Case */
3769
        // Start a transaction
3770
        $repository->beginTransaction();
3771
3772
        try {
3773
            $draft = $this->createContentDraftVersion1();
3774
3775
            $contentId = $draft->id;
3776
3777
            // Roleback the transaction
3778
            $repository->commit();
3779
        } catch (Exception $e) {
3780
            // Cleanup hanging transaction on error
3781
            $repository->rollback();
3782
            throw $e;
3783
        }
3784
3785
        // Load the new content object
3786
        $content = $contentService->loadContent($contentId);
3787
        /* END: Use Case */
3788
3789
        $this->assertEquals($contentId, $content->id);
3790
    }
3791
3792
    /**
3793
     * Test for the createContentDraft() method.
3794
     *
3795
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
3796
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
3797
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3798
     */
3799
    public function testCreateContentDraftInTransactionWithRollback()
3800
    {
3801
        $repository = $this->getRepository();
3802
3803
        $contentId = $this->generateId('object', 12);
3804
        /* BEGIN: Use Case */
3805
        // $contentId is the ID of the "Administrator users" user group
3806
3807
        // Get the content service
3808
        $contentService = $repository->getContentService();
3809
3810
        // Load the user group content object
3811
        $content = $contentService->loadContent($contentId);
3812
3813
        // Start a new transaction
3814
        $repository->beginTransaction();
3815
3816
        try {
3817
            // Create a new draft
3818
            $drafted = $contentService->createContentDraft($content->contentInfo);
3819
3820
            // Store version number for later reuse
3821
            $versionNo = $drafted->versionInfo->versionNo;
3822
        } catch (Exception $e) {
3823
            // Cleanup hanging transaction on error
3824
            $repository->rollback();
3825
            throw $e;
3826
        }
3827
3828
        // Rollback
3829
        $repository->rollback();
3830
3831
        try {
3832
            // This call will fail with a "NotFoundException"
3833
            $contentService->loadContent($contentId, null, $versionNo);
3834
        } catch (NotFoundException $e) {
3835
            return;
3836
        }
3837
        /* END: Use Case */
3838
3839
        $this->fail('Can still load content draft after rollback');
3840
    }
3841
3842
    /**
3843
     * Test for the createContentDraft() method.
3844
     *
3845
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
3846
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
3847
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3848
     */
3849 View Code Duplication
    public function testCreateContentDraftInTransactionWithCommit()
3850
    {
3851
        $repository = $this->getRepository();
3852
3853
        $contentId = $this->generateId('object', 12);
3854
        /* BEGIN: Use Case */
3855
        // $contentId is the ID of the "Administrator users" user group
3856
3857
        // Get the content service
3858
        $contentService = $repository->getContentService();
3859
3860
        // Load the user group content object
3861
        $content = $contentService->loadContent($contentId);
3862
3863
        // Start a new transaction
3864
        $repository->beginTransaction();
3865
3866
        try {
3867
            // Create a new draft
3868
            $drafted = $contentService->createContentDraft($content->contentInfo);
3869
3870
            // Store version number for later reuse
3871
            $versionNo = $drafted->versionInfo->versionNo;
3872
3873
            // Commit all changes
3874
            $repository->commit();
3875
        } catch (Exception $e) {
3876
            // Cleanup hanging transaction on error
3877
            $repository->rollback();
3878
            throw $e;
3879
        }
3880
3881
        $content = $contentService->loadContent($contentId, null, $versionNo);
3882
        /* END: Use Case */
3883
3884
        $this->assertEquals(
3885
            $versionNo,
3886
            $content->getVersionInfo()->versionNo
3887
        );
3888
    }
3889
3890
    /**
3891
     * Test for the publishVersion() method.
3892
     *
3893
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
3894
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3895
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3896
     */
3897 View Code Duplication
    public function testPublishVersionInTransactionWithRollback()
3898
    {
3899
        $repository = $this->getRepository();
3900
3901
        $contentId = $this->generateId('object', 12);
3902
        /* BEGIN: Use Case */
3903
        // $contentId is the ID of the "Administrator users" user group
3904
3905
        // Get the content service
3906
        $contentService = $repository->getContentService();
3907
3908
        // Load the user group content object
3909
        $content = $contentService->loadContent($contentId);
3910
3911
        // Start a new transaction
3912
        $repository->beginTransaction();
3913
3914
        try {
3915
            $draftVersion = $contentService->createContentDraft($content->contentInfo)->getVersionInfo();
3916
3917
            // Publish a new version
3918
            $content = $contentService->publishVersion($draftVersion);
3919
3920
            // Store version number for later reuse
3921
            $versionNo = $content->versionInfo->versionNo;
3922
        } catch (Exception $e) {
3923
            // Cleanup hanging transaction on error
3924
            $repository->rollback();
3925
            throw $e;
3926
        }
3927
3928
        // Rollback
3929
        $repository->rollback();
3930
3931
        try {
3932
            // This call will fail with a "NotFoundException"
3933
            $contentService->loadContent($contentId, null, $versionNo);
3934
        } catch (NotFoundException $e) {
3935
            return;
3936
        }
3937
        /* END: Use Case */
3938
3939
        $this->fail('Can still load content draft after rollback');
3940
    }
3941
3942
    /**
3943
     * Test for the publishVersion() method.
3944
     *
3945
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
3946
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3947
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
3948
     */
3949 View Code Duplication
    public function testPublishVersionInTransactionWithCommit()
3950
    {
3951
        $repository = $this->getRepository();
3952
3953
        /* BEGIN: Use Case */
3954
        // ID of the "Administrator users" user group
3955
        $contentId = 12;
3956
3957
        // Get the content service
3958
        $contentService = $repository->getContentService();
3959
3960
        // Load the user group content object
3961
        $template = $contentService->loadContent($contentId);
3962
3963
        // Start a new transaction
3964
        $repository->beginTransaction();
3965
3966
        try {
3967
            // Publish a new version
3968
            $content = $contentService->publishVersion(
3969
                $contentService->createContentDraft($template->contentInfo)->getVersionInfo()
3970
            );
3971
3972
            // Store version number for later reuse
3973
            $versionNo = $content->versionInfo->versionNo;
3974
3975
            // Commit all changes
3976
            $repository->commit();
3977
        } catch (Exception $e) {
3978
            // Cleanup hanging transaction on error
3979
            $repository->rollback();
3980
            throw $e;
3981
        }
3982
3983
        // Load current version info
3984
        $versionInfo = $contentService->loadVersionInfo($content->contentInfo);
3985
        /* END: Use Case */
3986
3987
        $this->assertEquals($versionNo, $versionInfo->versionNo);
3988
    }
3989
3990
    /**
3991
     * Test for the updateContent() method.
3992
     *
3993
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
3994
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
3995
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3996
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
3997
     */
3998 View Code Duplication
    public function testUpdateContentInTransactionWithRollback()
3999
    {
4000
        $repository = $this->getRepository();
4001
4002
        $contentId = $this->generateId('object', 12);
4003
        /* BEGIN: Use Case */
4004
        // $contentId is the ID of the "Administrator users" user group
4005
4006
        // Load content service
4007
        $contentService = $repository->getContentService();
4008
4009
        // Create a new user group draft
4010
        $draft = $contentService->createContentDraft(
4011
            $contentService->loadContentInfo($contentId)
4012
        );
4013
4014
        // Get an update struct and change the group name
4015
        $contentUpdate = $contentService->newContentUpdateStruct();
4016
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4017
4018
        // Start a transaction
4019
        $repository->beginTransaction();
4020
4021
        try {
4022
            // Update the group name
4023
            $draft = $contentService->updateContent(
4024
                $draft->getVersionInfo(),
4025
                $contentUpdate
4026
            );
4027
4028
            // Publish updated version
4029
            $contentService->publishVersion($draft->getVersionInfo());
4030
        } catch (Exception $e) {
4031
            // Cleanup hanging transaction on error
4032
            $repository->rollback();
4033
            throw $e;
4034
        }
4035
4036
        // Rollback all changes.
4037
        $repository->rollback();
4038
4039
        // Name will still be "Administrator users"
4040
        $name = $contentService->loadContent($contentId)->getFieldValue('name');
4041
        /* END: Use Case */
4042
4043
        $this->assertEquals('Administrator users', $name);
4044
    }
4045
4046
    /**
4047
     * Test for the updateContent() method.
4048
     *
4049
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
4050
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
4051
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
4052
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4053
     */
4054 View Code Duplication
    public function testUpdateContentInTransactionWithCommit()
4055
    {
4056
        $repository = $this->getRepository();
4057
4058
        $contentId = $this->generateId('object', 12);
4059
        /* BEGIN: Use Case */
4060
        // $contentId is the ID of the "Administrator users" user group
4061
4062
        // Load content service
4063
        $contentService = $repository->getContentService();
4064
4065
        // Create a new user group draft
4066
        $draft = $contentService->createContentDraft(
4067
            $contentService->loadContentInfo($contentId)
4068
        );
4069
4070
        // Get an update struct and change the group name
4071
        $contentUpdate = $contentService->newContentUpdateStruct();
4072
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
4073
4074
        // Start a transaction
4075
        $repository->beginTransaction();
4076
4077
        try {
4078
            // Update the group name
4079
            $draft = $contentService->updateContent(
4080
                $draft->getVersionInfo(),
4081
                $contentUpdate
4082
            );
4083
4084
            // Publish updated version
4085
            $contentService->publishVersion($draft->getVersionInfo());
4086
4087
            // Commit all changes.
4088
            $repository->commit();
4089
        } catch (Exception $e) {
4090
            // Cleanup hanging transaction on error
4091
            $repository->rollback();
4092
            throw $e;
4093
        }
4094
4095
        // Name is now "Administrators"
4096
        $name = $contentService->loadContent($contentId)->getFieldValue('name', 'eng-US');
4097
        /* END: Use Case */
4098
4099
        $this->assertEquals('Administrators', $name);
4100
    }
4101
4102
    /**
4103
     * Test for the updateContentMetadata() method.
4104
     *
4105
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4106
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4107
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4108
     */
4109 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithRollback()
4110
    {
4111
        $repository = $this->getRepository();
4112
4113
        $contentId = $this->generateId('object', 12);
4114
        /* BEGIN: Use Case */
4115
        // $contentId is the ID of the "Administrator users" user group
4116
4117
        // Get the content service
4118
        $contentService = $repository->getContentService();
4119
4120
        // Load a ContentInfo object
4121
        $contentInfo = $contentService->loadContentInfo($contentId);
4122
4123
        // Store remoteId for later testing
4124
        $remoteId = $contentInfo->remoteId;
4125
4126
        // Start a transaction
4127
        $repository->beginTransaction();
4128
4129
        try {
4130
            // Get metadata update struct and change remoteId
4131
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4132
            $metadataUpdate->remoteId = md5(microtime(true));
4133
4134
            // Update the metadata of the published content object
4135
            $contentService->updateContentMetadata(
4136
                $contentInfo,
4137
                $metadataUpdate
4138
            );
4139
        } catch (Exception $e) {
4140
            // Cleanup hanging transaction on error
4141
            $repository->rollback();
4142
            throw $e;
4143
        }
4144
4145
        // Rollback all changes.
4146
        $repository->rollback();
4147
4148
        // Load current remoteId
4149
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4150
        /* END: Use Case */
4151
4152
        $this->assertEquals($remoteId, $remoteIdReloaded);
4153
    }
4154
4155
    /**
4156
     * Test for the updateContentMetadata() method.
4157
     *
4158
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4159
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4160
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4161
     */
4162 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithCommit()
4163
    {
4164
        $repository = $this->getRepository();
4165
4166
        $contentId = $this->generateId('object', 12);
4167
        /* BEGIN: Use Case */
4168
        // $contentId is the ID of the "Administrator users" user group
4169
4170
        // Get the content service
4171
        $contentService = $repository->getContentService();
4172
4173
        // Load a ContentInfo object
4174
        $contentInfo = $contentService->loadContentInfo($contentId);
4175
4176
        // Store remoteId for later testing
4177
        $remoteId = $contentInfo->remoteId;
4178
4179
        // Start a transaction
4180
        $repository->beginTransaction();
4181
4182
        try {
4183
            // Get metadata update struct and change remoteId
4184
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4185
            $metadataUpdate->remoteId = md5(microtime(true));
4186
4187
            // Update the metadata of the published content object
4188
            $contentService->updateContentMetadata(
4189
                $contentInfo,
4190
                $metadataUpdate
4191
            );
4192
4193
            // Commit all changes.
4194
            $repository->commit();
4195
        } catch (Exception $e) {
4196
            // Cleanup hanging transaction on error
4197
            $repository->rollback();
4198
            throw $e;
4199
        }
4200
4201
        // Load current remoteId
4202
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4203
        /* END: Use Case */
4204
4205
        $this->assertNotEquals($remoteId, $remoteIdReloaded);
4206
    }
4207
4208
    /**
4209
     * Test for the deleteVersion() method.
4210
     *
4211
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4212
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4213
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4214
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4215
     */
4216
    public function testDeleteVersionInTransactionWithRollback()
4217
    {
4218
        $repository = $this->getRepository();
4219
4220
        $contentId = $this->generateId('object', 12);
4221
        /* BEGIN: Use Case */
4222
        // $contentId is the ID of the "Administrator users" user group
4223
4224
        // Get the content service
4225
        $contentService = $repository->getContentService();
4226
4227
        // Start a new transaction
4228
        $repository->beginTransaction();
4229
4230
        try {
4231
            // Create a new draft
4232
            $draft = $contentService->createContentDraft(
4233
                $contentService->loadContentInfo($contentId)
4234
            );
4235
4236
            $contentService->deleteVersion($draft->getVersionInfo());
4237
        } catch (Exception $e) {
4238
            // Cleanup hanging transaction on error
4239
            $repository->rollback();
4240
            throw $e;
4241
        }
4242
4243
        // Rollback all changes.
4244
        $repository->rollback();
4245
4246
        // This array will be empty
4247
        $drafts = $contentService->loadContentDrafts();
4248
        /* END: Use Case */
4249
4250
        $this->assertSame(array(), $drafts);
4251
    }
4252
4253
    /**
4254
     * Test for the deleteVersion() method.
4255
     *
4256
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4257
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4258
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4259
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4260
     */
4261
    public function testDeleteVersionInTransactionWithCommit()
4262
    {
4263
        $repository = $this->getRepository();
4264
4265
        $contentId = $this->generateId('object', 12);
4266
        /* BEGIN: Use Case */
4267
        // $contentId is the ID of the "Administrator users" user group
4268
4269
        // Get the content service
4270
        $contentService = $repository->getContentService();
4271
4272
        // Start a new transaction
4273
        $repository->beginTransaction();
4274
4275
        try {
4276
            // Create a new draft
4277
            $draft = $contentService->createContentDraft(
4278
                $contentService->loadContentInfo($contentId)
4279
            );
4280
4281
            $contentService->deleteVersion($draft->getVersionInfo());
4282
4283
            // Commit all changes.
4284
            $repository->commit();
4285
        } catch (Exception $e) {
4286
            // Cleanup hanging transaction on error
4287
            $repository->rollback();
4288
            throw $e;
4289
        }
4290
4291
        // This array will contain no element
4292
        $drafts = $contentService->loadContentDrafts();
4293
        /* END: Use Case */
4294
4295
        $this->assertSame(array(), $drafts);
4296
    }
4297
4298
    /**
4299
     * Test for the deleteContent() method.
4300
     *
4301
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4302
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4303
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4304
     */
4305
    public function testDeleteContentInTransactionWithRollback()
4306
    {
4307
        $repository = $this->getRepository();
4308
4309
        $contentId = $this->generateId('object', 11);
4310
        /* BEGIN: Use Case */
4311
        // $contentId is the ID of the "Members" user group in an eZ Publish
4312
        // demo installation
4313
4314
        // Get content service
4315
        $contentService = $repository->getContentService();
4316
4317
        // Load a ContentInfo instance
4318
        $contentInfo = $contentService->loadContentInfo($contentId);
4319
4320
        // Start a new transaction
4321
        $repository->beginTransaction();
4322
4323
        try {
4324
            // Delete content object
4325
            $contentService->deleteContent($contentInfo);
4326
        } catch (Exception $e) {
4327
            // Cleanup hanging transaction on error
4328
            $repository->rollback();
4329
            throw $e;
4330
        }
4331
4332
        // Rollback all changes
4333
        $repository->rollback();
4334
4335
        // This call will return the original content object
4336
        $contentInfo = $contentService->loadContentInfo($contentId);
4337
        /* END: Use Case */
4338
4339
        $this->assertEquals($contentId, $contentInfo->id);
4340
    }
4341
4342
    /**
4343
     * Test for the deleteContent() method.
4344
     *
4345
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4346
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4347
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4348
     */
4349
    public function testDeleteContentInTransactionWithCommit()
4350
    {
4351
        $repository = $this->getRepository();
4352
4353
        $contentId = $this->generateId('object', 11);
4354
        /* BEGIN: Use Case */
4355
        // $contentId is the ID of the "Members" user group in an eZ Publish
4356
        // demo installation
4357
4358
        // Get content service
4359
        $contentService = $repository->getContentService();
4360
4361
        // Load a ContentInfo instance
4362
        $contentInfo = $contentService->loadContentInfo($contentId);
4363
4364
        // Start a new transaction
4365
        $repository->beginTransaction();
4366
4367
        try {
4368
            // Delete content object
4369
            $contentService->deleteContent($contentInfo);
4370
4371
            // Commit all changes
4372
            $repository->commit();
4373
        } catch (Exception $e) {
4374
            // Cleanup hanging transaction on error
4375
            $repository->rollback();
4376
            throw $e;
4377
        }
4378
4379
        // Deleted content info is not found anymore
4380
        try {
4381
            $contentService->loadContentInfo($contentId);
4382
        } catch (NotFoundException $e) {
4383
            return;
4384
        }
4385
        /* END: Use Case */
4386
4387
        $this->fail('Can still load ContentInfo after commit.');
4388
    }
4389
4390
    /**
4391
     * Test for the copyContent() method.
4392
     *
4393
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4394
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4395
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4396
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4397
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4398
     */
4399 View Code Duplication
    public function testCopyContentInTransactionWithRollback()
4400
    {
4401
        $repository = $this->getRepository();
4402
4403
        $contentId = $this->generateId('object', 11);
4404
        $locationId = $this->generateId('location', 13);
4405
        /* BEGIN: Use Case */
4406
        // $contentId is the ID of the "Members" user group in an eZ Publish
4407
        // demo installation
4408
4409
        // $locationId is the ID of the "Administrator users" group location
4410
4411
        // Get services
4412
        $contentService = $repository->getContentService();
4413
        $locationService = $repository->getLocationService();
4414
4415
        // Load content object to copy
4416
        $content = $contentService->loadContent($contentId);
4417
4418
        // Create new target location
4419
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4420
4421
        // Start a new transaction
4422
        $repository->beginTransaction();
4423
4424
        try {
4425
            // Copy content with all versions and drafts
4426
            $contentService->copyContent(
4427
                $content->contentInfo,
4428
                $locationCreate
4429
            );
4430
        } catch (Exception $e) {
4431
            // Cleanup hanging transaction on error
4432
            $repository->rollback();
4433
            throw $e;
4434
        }
4435
4436
        // Rollback all changes
4437
        $repository->rollback();
4438
4439
        // This array will only contain a single admin user object
4440
        $locations = $locationService->loadLocationChildren(
4441
            $locationService->loadLocation($locationId)
4442
        )->locations;
4443
        /* END: Use Case */
4444
4445
        $this->assertEquals(1, count($locations));
4446
    }
4447
4448
    /**
4449
     * Test for the copyContent() method.
4450
     *
4451
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4452
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4453
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4454
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4455
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4456
     */
4457 View Code Duplication
    public function testCopyContentInTransactionWithCommit()
4458
    {
4459
        $repository = $this->getRepository();
4460
4461
        $contentId = $this->generateId('object', 11);
4462
        $locationId = $this->generateId('location', 13);
4463
        /* BEGIN: Use Case */
4464
        // $contentId is the ID of the "Members" user group in an eZ Publish
4465
        // demo installation
4466
4467
        // $locationId is the ID of the "Administrator users" group location
4468
4469
        // Get services
4470
        $contentService = $repository->getContentService();
4471
        $locationService = $repository->getLocationService();
4472
4473
        // Load content object to copy
4474
        $content = $contentService->loadContent($contentId);
4475
4476
        // Create new target location
4477
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4478
4479
        // Start a new transaction
4480
        $repository->beginTransaction();
4481
4482
        try {
4483
            // Copy content with all versions and drafts
4484
            $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...
4485
                $content->contentInfo,
4486
                $locationCreate
4487
            );
4488
4489
            // Commit all changes
4490
            $repository->commit();
4491
        } catch (Exception $e) {
4492
            // Cleanup hanging transaction on error
4493
            $repository->rollback();
4494
            throw $e;
4495
        }
4496
4497
        $this->refreshSearch($repository);
4498
4499
        // This will contain the admin user and the new child location
4500
        $locations = $locationService->loadLocationChildren(
4501
            $locationService->loadLocation($locationId)
4502
        )->locations;
4503
        /* END: Use Case */
4504
4505
        $this->assertEquals(2, count($locations));
4506
    }
4507
4508
    /**
4509
     */
4510
    public function testURLAliasesCreatedForNewContent()
4511
    {
4512
        $repository = $this->getRepository();
4513
4514
        $contentService = $repository->getContentService();
4515
        $locationService = $repository->getLocationService();
4516
        $urlAliasService = $repository->getURLAliasService();
4517
4518
        /* BEGIN: Use Case */
4519
        $draft = $this->createContentDraftVersion1();
4520
4521
        // Automatically creates a new URLAlias for the content
4522
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
4523
        /* END: Use Case */
4524
4525
        $location = $locationService->loadLocation(
4526
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4527
        );
4528
4529
        $aliases = $urlAliasService->listLocationAliases($location, false);
4530
4531
        $this->assertAliasesCorrect(
4532
            array(
4533
                '/Design/Plain-site/An-awesome-forum' => array(
4534
                    'type' => URLAlias::LOCATION,
4535
                    'destination' => $location->id,
4536
                    'path' => '/Design/Plain-site/An-awesome-forum',
4537
                    'languageCodes' => array('eng-US'),
4538
                    'isHistory' => false,
4539
                    'isCustom' => false,
4540
                    'forward' => false,
4541
                ),
4542
            ),
4543
            $aliases
4544
        );
4545
    }
4546
4547
    /**
4548
     */
4549
    public function testURLAliasesCreatedForUpdatedContent()
4550
    {
4551
        $repository = $this->getRepository();
4552
4553
        $contentService = $repository->getContentService();
4554
        $locationService = $repository->getLocationService();
4555
        $urlAliasService = $repository->getURLAliasService();
4556
4557
        /* BEGIN: Use Case */
4558
        $draft = $this->createUpdatedDraftVersion2();
4559
4560
        $location = $locationService->loadLocation(
4561
            $draft->getVersionInfo()->getContentInfo()->mainLocationId
4562
        );
4563
4564
        // Load and assert URL aliases before publishing updated Content, so that
4565
        // SPI cache is warmed up and cache invalidation is also tested.
4566
        $aliases = $urlAliasService->listLocationAliases($location, false);
4567
4568
        $this->assertAliasesCorrect(
4569
            array(
4570
                '/Design/Plain-site/An-awesome-forum' => array(
4571
                    'type' => URLAlias::LOCATION,
4572
                    'destination' => $location->id,
4573
                    'path' => '/Design/Plain-site/An-awesome-forum',
4574
                    'languageCodes' => array('eng-US'),
4575
                    'alwaysAvailable' => true,
4576
                    'isHistory' => false,
4577
                    'isCustom' => false,
4578
                    'forward' => false,
4579
                ),
4580
            ),
4581
            $aliases
4582
        );
4583
4584
        // Automatically marks old aliases for the content as history
4585
        // and creates new aliases, based on the changes
4586
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
4587
        /* END: Use Case */
4588
4589
        $location = $locationService->loadLocation(
4590
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4591
        );
4592
4593
        $aliases = $urlAliasService->listLocationAliases($location, false);
4594
4595
        $this->assertAliasesCorrect(
4596
            array(
4597
                '/Design/Plain-site/An-awesome-forum2' => array(
4598
                    'type' => URLAlias::LOCATION,
4599
                    'destination' => $location->id,
4600
                    'path' => '/Design/Plain-site/An-awesome-forum2',
4601
                    'languageCodes' => array('eng-US'),
4602
                    'alwaysAvailable' => true,
4603
                    'isHistory' => false,
4604
                    'isCustom' => false,
4605
                    'forward' => false,
4606
                ),
4607
                '/Design/Plain-site/An-awesome-forum23' => array(
4608
                    'type' => URLAlias::LOCATION,
4609
                    'destination' => $location->id,
4610
                    'path' => '/Design/Plain-site/An-awesome-forum23',
4611
                    'languageCodes' => array('eng-GB'),
4612
                    'alwaysAvailable' => true,
4613
                    'isHistory' => false,
4614
                    'isCustom' => false,
4615
                    'forward' => false,
4616
                ),
4617
            ),
4618
            $aliases
4619
        );
4620
    }
4621
4622
    /**
4623
     */
4624
    public function testCustomURLAliasesNotHistorizedOnUpdatedContent()
4625
    {
4626
        $repository = $this->getRepository();
4627
4628
        $contentService = $repository->getContentService();
4629
4630
        /* BEGIN: Use Case */
4631
        $urlAliasService = $repository->getURLAliasService();
4632
        $locationService = $repository->getLocationService();
4633
4634
        $content = $this->createContentVersion1();
4635
4636
        // Create a custom URL alias
4637
        $urlAliasService->createUrlAlias(
4638
            $locationService->loadLocation(
4639
                $content->getVersionInfo()->getContentInfo()->mainLocationId
4640
            ),
4641
            '/my/fancy/story-about-ez-publish',
4642
            'eng-US'
4643
        );
4644
4645
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
4646
4647
        $contentUpdate = $contentService->newContentUpdateStruct();
4648
        $contentUpdate->initialLanguageCode = 'eng-US';
4649
        $contentUpdate->setField('name', 'Amazing Bielefeld forum');
4650
4651
        $draftVersion2 = $contentService->updateContent(
4652
            $draftVersion2->getVersionInfo(),
4653
            $contentUpdate
4654
        );
4655
4656
        // Only marks auto-generated aliases as history
4657
        // the custom one is left untouched
4658
        $liveContent = $contentService->publishVersion($draftVersion2->getVersionInfo());
4659
        /* END: Use Case */
4660
4661
        $location = $locationService->loadLocation(
4662
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4663
        );
4664
4665
        $aliases = $urlAliasService->listLocationAliases($location);
4666
4667
        $this->assertAliasesCorrect(
4668
            array(
4669
                '/my/fancy/story-about-ez-publish' => array(
4670
                    'type' => URLAlias::LOCATION,
4671
                    'destination' => $location->id,
4672
                    'path' => '/my/fancy/story-about-ez-publish',
4673
                    'languageCodes' => array('eng-US'),
4674
                    'isHistory' => false,
4675
                    'isCustom' => true,
4676
                    'forward' => false,
4677
                    'alwaysAvailable' => false,
4678
                ),
4679
            ),
4680
            $aliases
4681
        );
4682
    }
4683
4684
    /**
4685
     * Test to ensure that old versions are not affected by updates to newer
4686
     * drafts.
4687
     */
4688
    public function testUpdatingDraftDoesNotUpdateOldVersions()
4689
    {
4690
        $repository = $this->getRepository();
4691
4692
        $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...
4693
4694
        $contentService = $repository->getContentService();
4695
4696
        $contentVersion2 = $this->createContentVersion2();
4697
4698
        $loadedContent1 = $contentService->loadContent($contentVersion2->id, null, 1);
4699
        $loadedContent2 = $contentService->loadContent($contentVersion2->id, null, 2);
4700
4701
        $this->assertNotEquals(
4702
            $loadedContent1->getFieldValue('name', 'eng-US'),
4703
            $loadedContent2->getFieldValue('name', 'eng-US')
4704
        );
4705
    }
4706
4707
    /**
4708
     * Asserts that all aliases defined in $expectedAliasProperties with the
4709
     * given properties are available in $actualAliases and not more.
4710
     *
4711
     * @param array $expectedAliasProperties
4712
     * @param array $actualAliases
4713
     */
4714
    private function assertAliasesCorrect(array $expectedAliasProperties, array $actualAliases)
4715
    {
4716
        foreach ($actualAliases as $actualAlias) {
4717
            if (!isset($expectedAliasProperties[$actualAlias->path])) {
4718
                $this->fail(
4719
                    sprintf(
4720
                        'Alias with path "%s" in languages "%s" not expected.',
4721
                        $actualAlias->path,
4722
                        implode(', ', $actualAlias->languageCodes)
4723
                    )
4724
                );
4725
            }
4726
4727
            foreach ($expectedAliasProperties[$actualAlias->path] as $propertyName => $propertyValue) {
4728
                $this->assertEquals(
4729
                    $propertyValue,
4730
                    $actualAlias->$propertyName,
4731
                    sprintf(
4732
                        'Property $%s incorrect on alias with path "%s" in languages "%s".',
4733
                        $propertyName,
4734
                        $actualAlias->path,
4735
                        implode(', ', $actualAlias->languageCodes)
4736
                    )
4737
                );
4738
            }
4739
4740
            unset($expectedAliasProperties[$actualAlias->path]);
4741
        }
4742
4743
        if (!empty($expectedAliasProperties)) {
4744
            $this->fail(
4745
                sprintf(
4746
                    'Missing expected aliases with paths "%s".',
4747
                    implode('", "', array_keys($expectedAliasProperties))
4748
                )
4749
            );
4750
        }
4751
    }
4752
4753
    /**
4754
     * Asserts that the given fields are equal to the default fields fixture.
4755
     *
4756
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
4757
     */
4758
    private function assertAllFieldsEquals(array $fields)
4759
    {
4760
        $actual = $this->normalizeFields($fields);
4761
        $expected = $this->normalizeFields($this->createFieldsFixture());
4762
4763
        $this->assertEquals($expected, $actual);
4764
    }
4765
4766
    /**
4767
     * Asserts that the given fields are equal to a language filtered set of the
4768
     * default fields fixture.
4769
     *
4770
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
4771
     * @param string $languageCode
4772
     */
4773
    private function assertLocaleFieldsEquals(array $fields, $languageCode)
4774
    {
4775
        $actual = $this->normalizeFields($fields);
4776
4777
        $expected = array();
4778
        foreach ($this->normalizeFields($this->createFieldsFixture()) as $field) {
4779
            if ($field->languageCode !== $languageCode) {
4780
                continue;
4781
            }
4782
            $expected[] = $field;
4783
        }
4784
4785
        $this->assertEquals($expected, $actual);
4786
    }
4787
4788
    /**
4789
     * This method normalizes a set of fields and returns a normalized set.
4790
     *
4791
     * Normalization means it resets the storage specific field id to zero and
4792
     * it sorts the field by their identifier and their language code. In
4793
     * addition, the field value is removed, since this one depends on the
4794
     * specific FieldType, which is tested in a dedicated integration test.
4795
     *
4796
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
4797
     *
4798
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
4799
     */
4800
    private function normalizeFields(array $fields)
4801
    {
4802
        $normalized = array();
4803 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...
4804
            $normalized[] = new Field(
4805
                array(
4806
                    'id' => 0,
4807
                    '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...
4808
                    'languageCode' => $field->languageCode,
4809
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
4810
                )
4811
            );
4812
        }
4813
        usort(
4814
            $normalized,
4815 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...
4816
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
4817
                    return strcasecmp($field1->languageCode, $field2->languageCode);
4818
                }
4819
4820
                return $return;
4821
            }
4822
        );
4823
4824
        return $normalized;
4825
    }
4826
4827
    /**
4828
     * Returns a filtered set of the default fields fixture.
4829
     *
4830
     * @param string $languageCode
4831
     *
4832
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
4833
     */
4834
    private function createLocaleFieldsFixture($languageCode)
4835
    {
4836
        $fields = array();
4837
        foreach ($this->createFieldsFixture() as $field) {
4838
            if (null === $field->languageCode || $languageCode === $field->languageCode) {
4839
                $fields[] = $field;
4840
            }
4841
        }
4842
4843
        return $fields;
4844
    }
4845
4846
    /**
4847
     * Asserts that given Content has default ContentStates.
4848
     *
4849
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
4850
     */
4851 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
4852
    {
4853
        $repository = $this->getRepository();
4854
        $objectStateService = $repository->getObjectStateService();
4855
4856
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
4857
4858
        foreach ($objectStateGroups as $objectStateGroup) {
4859
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
4860
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
4861
                // Only check the first object state which is the default one.
4862
                $this->assertEquals(
4863
                    $objectState,
4864
                    $contentState
4865
                );
4866
                break;
4867
            }
4868
        }
4869
    }
4870
4871
    /**
4872
     * Returns the default fixture of fields used in most tests.
4873
     *
4874
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
4875
     */
4876
    private function createFieldsFixture()
4877
    {
4878
        return array(
4879
            new Field(
4880
                array(
4881
                    'id' => 0,
4882
                    'value' => 'Foo',
4883
                    'languageCode' => 'eng-US',
4884
                    'fieldDefIdentifier' => 'description',
4885
                )
4886
            ),
4887
            new Field(
4888
                array(
4889
                    'id' => 0,
4890
                    'value' => 'Bar',
4891
                    'languageCode' => 'eng-GB',
4892
                    'fieldDefIdentifier' => 'description',
4893
                )
4894
            ),
4895
            new Field(
4896
                array(
4897
                    'id' => 0,
4898
                    'value' => 'An awesome multi-lang forum²',
4899
                    'languageCode' => 'eng-US',
4900
                    'fieldDefIdentifier' => 'name',
4901
                )
4902
            ),
4903
            new Field(
4904
                array(
4905
                    'id' => 0,
4906
                    'value' => 'An awesome multi-lang forum²³',
4907
                    'languageCode' => 'eng-GB',
4908
                    'fieldDefIdentifier' => 'name',
4909
                )
4910
            ),
4911
        );
4912
    }
4913
}
4914