Completed
Push — 6.7 ( 5d0642...0d9bf8 )
by André
113:22 queued 94:56
created

testUpdateContentValidatorIgnoresRequiredFieldsOfNotUpdatedLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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