Completed
Push — 6.7 ( fb8957...f97c26 )
by André
18:17
created

testUpdateContentMetadataCheckWithinTransaction()   A

Complexity

Conditions 2
Paths 7

Size

Total Lines 38

Duplication

Lines 38
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 7
nop 0
dl 38
loc 38
rs 9.312
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\Exceptions\UnauthorizedException;
13
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
14
use eZ\Publish\API\Repository\Values\Content\Field;
15
use eZ\Publish\API\Repository\Values\Content\Location;
16
use eZ\Publish\API\Repository\Values\Content\URLAlias;
17
use eZ\Publish\API\Repository\Values\Content\Relation;
18
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
19
use eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation;
20
use eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation;
21
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation;
22
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
23
use Exception;
24
25
/**
26
 * Test case for operations in the ContentService using in memory storage.
27
 *
28
 * @see eZ\Publish\API\Repository\ContentService
29
 * @group content
30
 */
31
class ContentServiceTest extends BaseContentServiceTest
32
{
33
    /**
34
     * Test for the newContentCreateStruct() method.
35
     *
36
     * @see \eZ\Publish\API\Repository\ContentService::newContentCreateStruct()
37
     * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testLoadContentTypeByIdentifier
38
     * @group user
39
     * @group field-type
40
     */
41
    public function testNewContentCreateStruct()
42
    {
43
        $repository = $this->getRepository();
44
45
        /* BEGIN: Use Case */
46
        // Create a content type
47
        $contentTypeService = $repository->getContentTypeService();
48
49
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
50
51
        $contentService = $repository->getContentService();
52
53
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
54
        /* END: Use Case */
55
56
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentCreateStruct', $contentCreate);
57
    }
58
59
    /**
60
     * Test for the createContent() method.
61
     *
62
     * @return \eZ\Publish\API\Repository\Values\Content\Content
63
     *
64
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
65
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentCreateStruct
66
     * @group user
67
     * @group field-type
68
     */
69
    public function testCreateContent()
70
    {
71
        if ($this->isVersion4()) {
72
            $this->markTestSkipped('This test requires eZ Publish 5');
73
        }
74
75
        $repository = $this->getRepository();
76
77
        /* BEGIN: Use Case */
78
        $contentTypeService = $repository->getContentTypeService();
79
80
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
81
82
        $contentService = $repository->getContentService();
83
84
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
85
        $contentCreate->setField('name', 'My awesome forum');
86
87
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
88
        $contentCreate->alwaysAvailable = true;
89
90
        $content = $contentService->createContent($contentCreate);
91
        /* END: Use Case */
92
93
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content', $content);
94
95
        return $content;
96
    }
97
98
    /**
99
     * Test for the createContent() method.
100
     *
101
     * Tests made for issue #EZP-20955 where Anonymous user is granted access to create content
102
     * and should have access to do that.
103
     *
104
     * @return \eZ\Publish\API\Repository\Values\Content\Content
105
     *
106
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
107
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentCreateStruct
108
     * @group user
109
     * @group field-type
110
     */
111
    public function testCreateContentAndPublishWithPrivilegedAnonymousUser()
112
    {
113
        if ($this->isVersion4()) {
114
            $this->markTestSkipped('This test requires eZ Publish 5');
115
        }
116
117
        $anonymousUserId = $this->generateId('user', 10);
118
119
        $repository = $this->getRepository();
120
        $contentService = $repository->getContentService();
121
        $contentTypeService = $repository->getContentTypeService();
122
        $locationService = $repository->getLocationService();
123
        $roleService = $repository->getRoleService();
124
125
        // Give Anonymous user role additional rights
126
        $role = $roleService->loadRoleByIdentifier('Anonymous');
127
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'create');
128
        $policyCreateStruct->addLimitation(new SectionLimitation(['limitationValues' => [1]]));
129
        $policyCreateStruct->addLimitation(new LocationLimitation(['limitationValues' => [2]]));
130
        $policyCreateStruct->addLimitation(new ContentTypeLimitation(['limitationValues' => [1]]));
131
        $roleService->addPolicy($role, $policyCreateStruct);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\RoleService::addPolicy() has been deprecated with message: since 6.0, use {@see addPolicyByRoleDraft}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
132
133
        // Set Anonymous user as current
134
        $repository->setCurrentUser($repository->getUserService()->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
135
136
        // Create a new content object:
137
        $contentCreate = $contentService->newContentCreateStruct(
138
            $contentTypeService->loadContentTypeByIdentifier('folder'),
139
            'eng-GB'
140
        );
141
142
        $contentCreate->setField('name', 'Folder 1');
143
144
        $content = $contentService->createContent(
145
            $contentCreate,
146
            [$locationService->newLocationCreateStruct(2)]
147
        );
148
149
        $contentService->publishVersion(
150
            $content->getVersionInfo()
151
        );
152
    }
153
154
    /**
155
     * Test for the createContent() method.
156
     *
157
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
158
     *
159
     * @return \eZ\Publish\API\Repository\Values\Content\Content
160
     *
161
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
162
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
163
     */
164
    public function testCreateContentSetsContentInfo($content)
165
    {
166
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $content->contentInfo);
167
168
        return $content;
169
    }
170
171
    /**
172
     * Test for the createContent() method.
173
     *
174
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
175
     *
176
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
177
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsContentInfo
178
     */
179
    public function testCreateContentSetsExpectedContentInfo($content)
180
    {
181
        $this->assertEquals(
182
            [
183
                $content->id,
184
                28, // id of content type "forum"
185
                true,
186
                1,
187
                'abcdef0123456789abcdef0123456789',
188
                'eng-US',
189
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
190
                false,
191
                null,
192
                // Main Location id for unpublished Content should be null
193
                null,
194
            ],
195
            [
196
                $content->contentInfo->id,
197
                $content->contentInfo->contentTypeId,
198
                $content->contentInfo->alwaysAvailable,
199
                $content->contentInfo->currentVersionNo,
200
                $content->contentInfo->remoteId,
201
                $content->contentInfo->mainLanguageCode,
202
                $content->contentInfo->ownerId,
203
                $content->contentInfo->published,
204
                $content->contentInfo->publishedDate,
205
                $content->contentInfo->mainLocationId,
206
            ]
207
        );
208
    }
209
210
    /**
211
     * Test for the createContent() method.
212
     *
213
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
214
     *
215
     * @return \eZ\Publish\API\Repository\Values\Content\Content
216
     *
217
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
218
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
219
     */
220
    public function testCreateContentSetsVersionInfo($content)
221
    {
222
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo', $content->getVersionInfo());
223
224
        return $content;
225
    }
226
227
    /**
228
     * Test for the createContent() method.
229
     *
230
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
231
     *
232
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
233
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsVersionInfo
234
     */
235
    public function testCreateContentSetsExpectedVersionInfo($content)
236
    {
237
        $this->assertEquals(
238
            [
239
                'status' => VersionInfo::STATUS_DRAFT,
240
                'versionNo' => 1,
241
                'creatorId' => $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
242
                'initialLanguageCode' => 'eng-US',
243
            ],
244
            [
245
                'status' => $content->getVersionInfo()->status,
246
                'versionNo' => $content->getVersionInfo()->versionNo,
247
                'creatorId' => $content->getVersionInfo()->creatorId,
248
                'initialLanguageCode' => $content->getVersionInfo()->initialLanguageCode,
249
            ]
250
        );
251
    }
252
253
    /**
254
     * Test for the createContent() method.
255
     *
256
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
257
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
258
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
259
     */
260
    public function testCreateContentThrowsInvalidArgumentException()
261
    {
262
        if ($this->isVersion4()) {
263
            $this->markTestSkipped('This test requires eZ Publish 5');
264
        }
265
266
        $repository = $this->getRepository();
267
268
        /* BEGIN: Use Case */
269
        $contentTypeService = $repository->getContentTypeService();
270
        $contentService = $repository->getContentService();
271
272
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
273
274
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
275
        $contentCreate1->setField('name', 'An awesome Sidelfingen forum');
276
277
        $contentCreate1->remoteId = 'abcdef0123456789abcdef0123456789';
278
        $contentCreate1->alwaysAvailable = true;
279
280
        $draft = $contentService->createContent($contentCreate1);
281
        $contentService->publishVersion($draft->versionInfo);
282
283
        $contentCreate2 = $contentService->newContentCreateStruct($contentType, 'eng-GB');
284
        $contentCreate2->setField('name', 'An awesome Bielefeld forum');
285
286
        $contentCreate2->remoteId = 'abcdef0123456789abcdef0123456789';
287
        $contentCreate2->alwaysAvailable = false;
288
289
        // This call will fail with an "InvalidArgumentException", because the
290
        // remoteId is already in use.
291
        $contentService->createContent($contentCreate2);
292
        /* END: Use Case */
293
    }
294
295
    /**
296
     * Test for the createContent() method.
297
     *
298
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
299
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
300
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
301
     */
302 View Code Duplication
    public function testCreateContentThrowsInvalidArgumentExceptionOnFieldTypeNotAccept()
303
    {
304
        $repository = $this->getRepository();
305
306
        /* BEGIN: Use Case */
307
        $contentTypeService = $repository->getContentTypeService();
308
        $contentService = $repository->getContentService();
309
310
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
311
312
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
313
        // The name field does only accept strings and null as its values
314
        $contentCreate->setField('name', new \stdClass());
315
316
        // Throws InvalidArgumentException since the name field is filled
317
        // improperly
318
        $draft = $contentService->createContent($contentCreate);
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
319
        /* END: Use Case */
320
    }
321
322
    /**
323
     * Test for the createContent() method.
324
     *
325
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
326
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
327
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
328
     */
329
    public function testCreateContentThrowsContentFieldValidationException()
330
    {
331
        $repository = $this->getRepository();
332
333
        /* BEGIN: Use Case */
334
        $contentTypeService = $repository->getContentTypeService();
335
        $contentService = $repository->getContentService();
336
337
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
338
339
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
340
        $contentCreate1->setField('name', 'An awesome Sidelfingen folder');
341
        // Violates string length constraint
342
        $contentCreate1->setField('short_name', str_repeat('a', 200));
343
344
        // Throws ContentFieldValidationException, since short_name does not pass
345
        // validation of the string length validator
346
        $draft = $contentService->createContent($contentCreate1);
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
347
        /* END: Use Case */
348
    }
349
350
    /**
351
     * Test for the createContent() method.
352
     *
353
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
354
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
355
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
356
     */
357 View Code Duplication
    public function testCreateContentRequiredFieldMissing()
358
    {
359
        $repository = $this->getRepository();
360
361
        /* BEGIN: Use Case */
362
        $contentTypeService = $repository->getContentTypeService();
363
        $contentService = $repository->getContentService();
364
365
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
366
367
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
368
        // Required field "name" is not set
369
370
        // Throws a ContentFieldValidationException, since a required field is
371
        // missing
372
        $draft = $contentService->createContent($contentCreate1);
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
373
        /* END: Use Case */
374
    }
375
376
    /**
377
     * Test for the createContent() method.
378
     *
379
     * NOTE: We have bidirectional dependencies between the ContentService and
380
     * the LocationService, so that we cannot use PHPUnit's test dependencies
381
     * here.
382
     *
383
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
384
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
385
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationByRemoteId
386
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
387
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
388
     * @group user
389
     */
390
    public function testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately()
391
    {
392
        $repository = $this->getRepository();
393
394
        $locationService = $repository->getLocationService();
395
396
        /* BEGIN: Use Case */
397
        $draft = $this->createContentDraftVersion1();
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
398
399
        // The location will not have been created, yet, so this throws an
400
        // exception
401
        $location = $locationService->loadLocationByRemoteId(
0 ignored issues
show
Unused Code introduced by
$location is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
402
            '0123456789abcdef0123456789abcdef'
403
        );
404
        /* END: Use Case */
405
    }
406
407
    /**
408
     * Test for the createContent() method.
409
     *
410
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
411
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
412
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
413
     */
414
    public function testCreateContentThrowsInvalidArgumentExceptionWithLocationCreateParameter()
415
    {
416
        $repository = $this->getRepository();
417
418
        $parentLocationId = $this->generateId('location', 56);
419
        /* BEGIN: Use Case */
420
        // $parentLocationId is a valid location ID
421
422
        $contentService = $repository->getContentService();
423
        $contentTypeService = $repository->getContentTypeService();
424
        $locationService = $repository->getLocationService();
425
426
        // Load content type
427
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
428
429
        // Configure new locations
430
        $locationCreate1 = $locationService->newLocationCreateStruct($parentLocationId);
431
432
        $locationCreate1->priority = 23;
433
        $locationCreate1->hidden = true;
434
        $locationCreate1->remoteId = '0123456789abcdef0123456789aaaaaa';
435
        $locationCreate1->sortField = Location::SORT_FIELD_NODE_ID;
436
        $locationCreate1->sortOrder = Location::SORT_ORDER_DESC;
437
438
        $locationCreate2 = $locationService->newLocationCreateStruct($parentLocationId);
439
440
        $locationCreate2->priority = 42;
441
        $locationCreate2->hidden = true;
442
        $locationCreate2->remoteId = '0123456789abcdef0123456789bbbbbb';
443
        $locationCreate2->sortField = Location::SORT_FIELD_NODE_ID;
444
        $locationCreate2->sortOrder = Location::SORT_ORDER_DESC;
445
446
        // Configure new content object
447
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
448
449
        $contentCreate->setField('name', 'A awesome Sindelfingen forum');
450
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
451
        $contentCreate->alwaysAvailable = true;
452
453
        // Create new content object under the specified location
454
        $draft = $contentService->createContent(
455
            $contentCreate,
456
            [$locationCreate1]
457
        );
458
        $contentService->publishVersion($draft->versionInfo);
459
460
        // This call will fail with an "InvalidArgumentException", because the
461
        // Content remoteId already exists,
462
        $contentService->createContent(
463
            $contentCreate,
464
            [$locationCreate2]
465
        );
466
        /* END: Use Case */
467
    }
468
469
    /**
470
     * Test for the loadContentInfo() method.
471
     *
472
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
473
     * @group user
474
     */
475 View Code Duplication
    public function testLoadContentInfo()
476
    {
477
        $repository = $this->getRepository();
478
479
        $mediaFolderId = $this->generateId('object', 41);
480
        /* BEGIN: Use Case */
481
        $contentService = $repository->getContentService();
482
483
        // Load the ContentInfo for "Media" folder
484
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
485
        /* END: Use Case */
486
487
        $this->assertInstanceOf(
488
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
489
            $contentInfo
490
        );
491
    }
492
493
    /**
494
     * Test for the loadContentInfo() method.
495
     *
496
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
497
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
498
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
499
     */
500 View Code Duplication
    public function testLoadContentInfoThrowsNotFoundException()
501
    {
502
        $repository = $this->getRepository();
503
504
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
505
        /* BEGIN: Use Case */
506
        $contentService = $repository->getContentService();
507
508
        // This call will fail with a NotFoundException
509
        $contentService->loadContentInfo($nonExistentContentId);
510
        /* END: Use Case */
511
    }
512
513
    /**
514
     * Test for the loadContentInfoByRemoteId() method.
515
     *
516
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
517
     */
518
    public function testLoadContentInfoByRemoteId()
519
    {
520
        $repository = $this->getRepository();
521
522
        /* BEGIN: Use Case */
523
        $contentService = $repository->getContentService();
524
525
        // Load the ContentInfo for "Media" folder
526
        $contentInfo = $contentService->loadContentInfoByRemoteId('faaeb9be3bd98ed09f606fc16d144eca');
527
        /* END: Use Case */
528
529
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $contentInfo);
530
    }
531
532
    /**
533
     * Test for the loadContentInfoByRemoteId() method.
534
     *
535
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
536
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
537
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
538
     */
539
    public function testLoadContentInfoByRemoteIdThrowsNotFoundException()
540
    {
541
        $repository = $this->getRepository();
542
543
        /* BEGIN: Use Case */
544
        $contentService = $repository->getContentService();
545
546
        // This call will fail with a NotFoundException
547
        $contentService->loadContentInfoByRemoteId('abcdefghijklmnopqrstuvwxyz0123456789');
548
        /* END: Use Case */
549
    }
550
551
    /**
552
     * Test for the loadVersionInfo() method.
553
     *
554
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
555
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
556
     * @group user
557
     */
558
    public function testLoadVersionInfo()
559
    {
560
        $repository = $this->getRepository();
561
562
        $mediaFolderId = $this->generateId('object', 41);
563
        /* BEGIN: Use Case */
564
        // $mediaFolderId contains the ID of the "Media" folder
565
566
        $contentService = $repository->getContentService();
567
568
        // Load the ContentInfo for "Media" folder
569
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
570
571
        // Now load the current version info of the "Media" folder
572
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
573
        /* END: Use Case */
574
575
        $this->assertInstanceOf(
576
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
577
            $versionInfo
578
        );
579
    }
580
581
    /**
582
     * Test for the loadVersionInfoById() method.
583
     *
584
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
585
     */
586 View Code Duplication
    public function testLoadVersionInfoById()
587
    {
588
        $repository = $this->getRepository();
589
590
        $mediaFolderId = $this->generateId('object', 41);
591
        /* BEGIN: Use Case */
592
        // $mediaFolderId contains the ID of the "Media" folder
593
594
        $contentService = $repository->getContentService();
595
596
        // Load the VersionInfo for "Media" folder
597
        $versionInfo = $contentService->loadVersionInfoById($mediaFolderId);
598
        /* END: Use Case */
599
600
        $this->assertInstanceOf(
601
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
602
            $versionInfo
603
        );
604
    }
605
606
    /**
607
     * Test for the loadVersionInfoById() method.
608
     *
609
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
610
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
611
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
612
     */
613 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundException()
614
    {
615
        $repository = $this->getRepository();
616
617
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
618
        /* BEGIN: Use Case */
619
        $contentService = $repository->getContentService();
620
621
        // This call will fail with a "NotFoundException"
622
        $contentService->loadVersionInfoById($nonExistentContentId);
623
        /* END: Use Case */
624
    }
625
626
    /**
627
     * Test for the loadContentByContentInfo() method.
628
     *
629
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
630
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
631
     */
632
    public function testLoadContentByContentInfo()
633
    {
634
        $repository = $this->getRepository();
635
636
        $mediaFolderId = $this->generateId('object', 41);
637
        /* BEGIN: Use Case */
638
        // $mediaFolderId contains the ID of the "Media" folder
639
640
        $contentService = $repository->getContentService();
641
642
        // Load the ContentInfo for "Media" folder
643
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
644
645
        // Now load the current content version for the info instance
646
        $content = $contentService->loadContentByContentInfo($contentInfo);
647
        /* END: Use Case */
648
649
        $this->assertInstanceOf(
650
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
651
            $content
652
        );
653
    }
654
655
    /**
656
     * Test for the loadContentByVersionInfo() method.
657
     *
658
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
659
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
660
     */
661
    public function testLoadContentByVersionInfo()
662
    {
663
        $repository = $this->getRepository();
664
665
        $mediaFolderId = $this->generateId('object', 41);
666
        /* BEGIN: Use Case */
667
        // $mediaFolderId contains the ID of the "Media" folder
668
669
        $contentService = $repository->getContentService();
670
671
        // Load the ContentInfo for "Media" folder
672
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
673
674
        // Load the current VersionInfo
675
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
676
677
        // Now load the current content version for the info instance
678
        $content = $contentService->loadContentByVersionInfo($versionInfo);
679
        /* END: Use Case */
680
681
        $this->assertInstanceOf(
682
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
683
            $content
684
        );
685
    }
686
687
    /**
688
     * Test for the loadContent() method.
689
     *
690
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
691
     * @group user
692
     * @group field-type
693
     */
694 View Code Duplication
    public function testLoadContent()
695
    {
696
        $repository = $this->getRepository();
697
698
        $mediaFolderId = $this->generateId('object', 41);
699
        /* BEGIN: Use Case */
700
        // $mediaFolderId contains the ID of the "Media" folder
701
702
        $contentService = $repository->getContentService();
703
704
        // Load the Content for "Media" folder, any language and current version
705
        $content = $contentService->loadContent($mediaFolderId);
706
        /* END: Use Case */
707
708
        $this->assertInstanceOf(
709
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
710
            $content
711
        );
712
    }
713
714
    /**
715
     * Test for the loadContent() method.
716
     *
717
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
718
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
719
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
720
     */
721 View Code Duplication
    public function testLoadContentThrowsNotFoundException()
722
    {
723
        $repository = $this->getRepository();
724
725
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
726
        /* BEGIN: Use Case */
727
        $contentService = $repository->getContentService();
728
729
        // This call will fail with a "NotFoundException"
730
        $contentService->loadContent($nonExistentContentId);
731
        /* END: Use Case */
732
    }
733
734
    /**
735
     * Test for the loadContentByRemoteId() method.
736
     *
737
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
738
     */
739
    public function testLoadContentByRemoteId()
740
    {
741
        $repository = $this->getRepository();
742
743
        /* BEGIN: Use Case */
744
        // Remote id of the "Media" folder
745
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
746
747
        $contentService = $repository->getContentService();
748
749
        // Load the Content for "Media" folder
750
        $content = $contentService->loadContentByRemoteId($mediaRemoteId);
751
        /* END: Use Case */
752
753
        $this->assertInstanceOf(
754
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
755
            $content
756
        );
757
    }
758
759
    /**
760
     * Test for the loadContentByRemoteId() method.
761
     *
762
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
763
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
764
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
765
     */
766
    public function testLoadContentByRemoteIdThrowsNotFoundException()
767
    {
768
        $repository = $this->getRepository();
769
770
        /* BEGIN: Use Case */
771
        $contentService = $repository->getContentService();
772
773
        // This call will fail with a "NotFoundException", because no content
774
        // object exists for the given remoteId
775
        $contentService->loadContentByRemoteId('a1b1c1d1e1f1a2b2c2d2e2f2a3b3c3d3');
776
        /* END: Use Case */
777
    }
778
779
    /**
780
     * Test for the publishVersion() method.
781
     *
782
     * @return \eZ\Publish\API\Repository\Values\Content\Content
783
     *
784
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
785
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
786
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
787
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
788
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
789
     * @group user
790
     * @group field-type
791
     */
792
    public function testPublishVersion()
793
    {
794
        /* BEGIN: Use Case */
795
        $content = $this->createContentVersion1();
796
        /* END: Use Case */
797
798
        $this->assertInstanceOf(
799
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
800
            $content
801
        );
802
803
        return $content;
804
    }
805
806
    /**
807
     * Test for the publishVersion() method.
808
     *
809
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
810
     *
811
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
812
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
813
     */
814
    public function testPublishVersionSetsExpectedContentInfo($content)
815
    {
816
        $this->assertEquals(
817
            [
818
                $content->id,
819
                true,
820
                1,
821
                'abcdef0123456789abcdef0123456789',
822
                'eng-US',
823
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
824
                true,
825
            ],
826
            [
827
                $content->contentInfo->id,
828
                $content->contentInfo->alwaysAvailable,
829
                $content->contentInfo->currentVersionNo,
830
                $content->contentInfo->remoteId,
831
                $content->contentInfo->mainLanguageCode,
832
                $content->contentInfo->ownerId,
833
                $content->contentInfo->published,
834
            ]
835
        );
836
837
        $this->assertNotNull($content->contentInfo->mainLocationId);
838
        $date = new \DateTime('1984/01/01');
839
        $this->assertGreaterThan(
840
            $date->getTimestamp(),
841
            $content->contentInfo->publishedDate->getTimestamp()
842
        );
843
    }
844
845
    /**
846
     * Test for the publishVersion() method.
847
     *
848
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
849
     *
850
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
851
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
852
     */
853
    public function testPublishVersionSetsExpectedVersionInfo($content)
854
    {
855
        $this->assertEquals(
856
            [
857
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
858
                'eng-US',
859
                VersionInfo::STATUS_PUBLISHED,
860
                1,
861
            ],
862
            [
863
                $content->getVersionInfo()->creatorId,
864
                $content->getVersionInfo()->initialLanguageCode,
865
                $content->getVersionInfo()->status,
866
                $content->getVersionInfo()->versionNo,
867
            ]
868
        );
869
870
        $date = new \DateTime('1984/01/01');
871
        $this->assertGreaterThan(
872
            $date->getTimestamp(),
873
            $content->getVersionInfo()->modificationDate->getTimestamp()
874
        );
875
876
        $this->assertNotNull($content->getVersionInfo()->modificationDate);
877
    }
878
879
    /**
880
     * Test for the publishVersion() method.
881
     *
882
     * @return \eZ\Publish\API\Repository\Values\Content\Content
883
     *
884
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
885
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
886
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
887
     */
888
    public function testPublishVersionCreatesLocationsDefinedOnCreate()
889
    {
890
        $repository = $this->getRepository();
891
892
        /* BEGIN: Use Case */
893
        $content = $this->createContentVersion1();
894
        /* END: Use Case */
895
896
        $locationService = $repository->getLocationService();
897
        $location = $locationService->loadLocationByRemoteId(
898
            '0123456789abcdef0123456789abcdef'
899
        );
900
901
        $this->assertEquals(
902
            $location->getContentInfo(),
903
            $content->getVersionInfo()->getContentInfo()
904
        );
905
906
        return [$content, $location];
907
    }
908
909
    /**
910
     * Test for the publishVersion() method.
911
     *
912
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
913
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionCreatesLocationsDefinedOnCreate
914
     */
915
    public function testCreateContentWithLocationCreateParameterCreatesExpectedLocation(array $testData)
916
    {
917
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
918
        /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
919
        list($content, $location) = $testData;
920
921
        $parentLocationId = $this->generateId('location', 56);
922
        $parentLocation = $this->getRepository()->getLocationService()->loadLocation($parentLocationId);
923
        $mainLocationId = $content->getVersionInfo()->getContentInfo()->mainLocationId;
924
925
        $this->assertPropertiesCorrect(
926
            [
927
                'id' => $mainLocationId,
928
                'priority' => 23,
929
                'hidden' => true,
930
                'invisible' => true,
931
                'remoteId' => '0123456789abcdef0123456789abcdef',
932
                'parentLocationId' => $parentLocationId,
933
                'pathString' => $parentLocation->pathString . $mainLocationId . '/',
934
                'depth' => $parentLocation->depth + 1,
935
                'sortField' => Location::SORT_FIELD_NODE_ID,
936
                'sortOrder' => Location::SORT_ORDER_DESC,
937
            ],
938
            $location
939
        );
940
    }
941
942
    /**
943
     * Test for the publishVersion() method.
944
     *
945
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
946
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
947
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
948
     */
949 View Code Duplication
    public function testPublishVersionThrowsBadStateException()
950
    {
951
        $repository = $this->getRepository();
952
953
        $contentService = $repository->getContentService();
954
955
        /* BEGIN: Use Case */
956
        $draft = $this->createContentDraftVersion1();
957
958
        // Publish the content draft
959
        $contentService->publishVersion($draft->getVersionInfo());
960
961
        // This call will fail with a "BadStateException", because the version
962
        // is already published.
963
        $contentService->publishVersion($draft->getVersionInfo());
964
        /* END: Use Case */
965
    }
966
967
    /**
968
     * Test for the createContentDraft() method.
969
     *
970
     * @return \eZ\Publish\API\Repository\Values\Content\Content
971
     *
972
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
973
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
974
     * @group user
975
     */
976
    public function testCreateContentDraft()
977
    {
978
        $repository = $this->getRepository();
979
980
        $contentService = $repository->getContentService();
981
982
        /* BEGIN: Use Case */
983
        $content = $this->createContentVersion1();
984
985
        // Now we create a new draft from the published content
986
        $draftedContent = $contentService->createContentDraft($content->contentInfo);
987
        /* END: Use Case */
988
989
        $this->assertInstanceOf(
990
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
991
            $draftedContent
992
        );
993
994
        return $draftedContent;
995
    }
996
997
    /**
998
     * Test for the createContentDraft() method.
999
     *
1000
     * Test that editor has access to edit own draft.
1001
     * Note: Editors have access to version_read, which is needed to load content drafts.
1002
     *
1003
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1004
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1005
     * @group user
1006
     */
1007
    public function testCreateContentDraftAndLoadAccess()
1008
    {
1009
        $repository = $this->getRepository();
1010
1011
        /* BEGIN: Use Case */
1012
        $user = $this->createUserVersion1();
1013
1014
        // Set new editor as user
1015
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1016
1017
        // Create draft
1018
        $draft = $this->createContentDraftVersion1(2, 'folder');
1019
1020
        // Try to load the draft
1021
        $contentService = $repository->getContentService();
1022
        $loadedDraft = $contentService->loadContent($draft->id);
1023
1024
        /* END: Use Case */
1025
1026
        $this->assertEquals($draft->id, $loadedDraft->id);
1027
    }
1028
1029
    /**
1030
     * Test for the createContentDraft() method.
1031
     *
1032
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1033
     *
1034
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1035
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1036
     */
1037
    public function testCreateContentDraftSetsExpectedProperties($draft)
1038
    {
1039
        $this->assertEquals(
1040
            [
1041
                'fieldCount' => 2,
1042
                'relationCount' => 0,
1043
            ],
1044
            [
1045
                'fieldCount' => count($draft->getFields()),
1046
                'relationCount' => count($this->getRepository()->getContentService()->loadRelations($draft->getVersionInfo())),
1047
            ]
1048
        );
1049
    }
1050
1051
    /**
1052
     * Test for the createContentDraft() method.
1053
     *
1054
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1055
     *
1056
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1057
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1058
     */
1059
    public function testCreateContentDraftSetsContentInfo($draft)
1060
    {
1061
        $contentInfo = $draft->contentInfo;
1062
1063
        $this->assertEquals(
1064
            [
1065
                $draft->id,
1066
                true,
1067
                1,
1068
                'eng-US',
1069
                $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1070
                'abcdef0123456789abcdef0123456789',
1071
                1,
1072
            ],
1073
            [
1074
                $contentInfo->id,
1075
                $contentInfo->alwaysAvailable,
1076
                $contentInfo->currentVersionNo,
1077
                $contentInfo->mainLanguageCode,
1078
                $contentInfo->ownerId,
1079
                $contentInfo->remoteId,
1080
                $contentInfo->sectionId,
1081
            ]
1082
        );
1083
    }
1084
1085
    /**
1086
     * Test for the createContentDraft() method.
1087
     *
1088
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1089
     *
1090
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1091
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1092
     */
1093
    public function testCreateContentDraftSetsVersionInfo($draft)
1094
    {
1095
        $versionInfo = $draft->getVersionInfo();
1096
1097
        $this->assertEquals(
1098
            [
1099
                'creatorId' => $this->getRepository()->getCurrentUser()->id,
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1100
                'initialLanguageCode' => 'eng-US',
1101
                'languageCodes' => [0 => 'eng-US'],
1102
                'status' => VersionInfo::STATUS_DRAFT,
1103
                'versionNo' => 2,
1104
            ],
1105
            [
1106
                'creatorId' => $versionInfo->creatorId,
1107
                'initialLanguageCode' => $versionInfo->initialLanguageCode,
1108
                'languageCodes' => $versionInfo->languageCodes,
1109
                'status' => $versionInfo->status,
1110
                'versionNo' => $versionInfo->versionNo,
1111
            ]
1112
        );
1113
    }
1114
1115
    /**
1116
     * Test for the createContentDraft() method.
1117
     *
1118
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1119
     *
1120
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1121
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1122
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
1123
     */
1124
    public function testCreateContentDraftLoadVersionInfoStillLoadsPublishedVersion($draft)
0 ignored issues
show
Unused Code introduced by
The parameter $draft is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

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