Completed
Push — master ( 9749fb...c9d304 )
by André
26:06 queued 10:47
created

ContentServiceTest::assertLocaleFieldsEquals()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading history...
1123
    {
1124
        $repository = $this->getRepository();
1125
1126
        $contentService = $repository->getContentService();
1127
1128
        /* BEGIN: Use Case */
1129
        $content = $this->createContentVersion1();
1130
1131
        // Now we create a new draft from the published content
1132
        $contentService->createContentDraft($content->contentInfo);
1133
1134
        // This call will still load the published version
1135
        $versionInfoPublished = $contentService->loadVersionInfo($content->contentInfo);
1136
        /* END: Use Case */
1137
1138
        $this->assertEquals(1, $versionInfoPublished->versionNo);
1139
    }
1140
1141
    /**
1142
     * Test for the createContentDraft() method.
1143
     *
1144
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1145
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
1146
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1147
     */
1148
    public function testCreateContentDraftLoadContentStillLoadsPublishedVersion()
1149
    {
1150
        $repository = $this->getRepository();
1151
1152
        $contentService = $repository->getContentService();
1153
1154
        /* BEGIN: Use Case */
1155
        $content = $this->createContentVersion1();
1156
1157
        // Now we create a new draft from the published content
1158
        $contentService->createContentDraft($content->contentInfo);
1159
1160
        // This call will still load the published content version
1161
        $contentPublished = $contentService->loadContent($content->id);
1162
        /* END: Use Case */
1163
1164
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1165
    }
1166
1167
    /**
1168
     * Test for the createContentDraft() method.
1169
     *
1170
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1171
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
1172
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1173
     */
1174
    public function testCreateContentDraftLoadContentByRemoteIdStillLoadsPublishedVersion()
1175
    {
1176
        $repository = $this->getRepository();
1177
1178
        $contentService = $repository->getContentService();
1179
1180
        /* BEGIN: Use Case */
1181
        $content = $this->createContentVersion1();
1182
1183
        // Now we create a new draft from the published content
1184
        $contentService->createContentDraft($content->contentInfo);
1185
1186
        // This call will still load the published content version
1187
        $contentPublished = $contentService->loadContentByRemoteId('abcdef0123456789abcdef0123456789');
1188
        /* END: Use Case */
1189
1190
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1191
    }
1192
1193
    /**
1194
     * Test for the createContentDraft() method.
1195
     *
1196
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1197
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
1198
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1199
     */
1200
    public function testCreateContentDraftLoadContentByContentInfoStillLoadsPublishedVersion()
1201
    {
1202
        $repository = $this->getRepository();
1203
1204
        $contentService = $repository->getContentService();
1205
1206
        /* BEGIN: Use Case */
1207
        $content = $this->createContentVersion1();
1208
1209
        // Now we create a new draft from the published content
1210
        $contentService->createContentDraft($content->contentInfo);
1211
1212
        // This call will still load the published content version
1213
        $contentPublished = $contentService->loadContentByContentInfo($content->contentInfo);
1214
        /* END: Use Case */
1215
1216
        $this->assertEquals(1, $contentPublished->getVersionInfo()->versionNo);
1217
    }
1218
1219
    /**
1220
     * Test for the newContentUpdateStruct() method.
1221
     *
1222
     * @see \eZ\Publish\API\Repository\ContentService::newContentUpdateStruct()
1223
     * @group user
1224
     */
1225
    public function testNewContentUpdateStruct()
1226
    {
1227
        $repository = $this->getRepository();
1228
1229
        /* BEGIN: Use Case */
1230
        $contentService = $repository->getContentService();
1231
1232
        $updateStruct = $contentService->newContentUpdateStruct();
1233
        /* END: Use Case */
1234
1235
        $this->assertInstanceOf(
1236
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentUpdateStruct',
1237
            $updateStruct
1238
        );
1239
    }
1240
1241
    /**
1242
     * Test for the updateContent() method.
1243
     *
1244
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1245
     *
1246
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1247
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1248
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1249
     * @group user
1250
     * @group field-type
1251
     */
1252
    public function testUpdateContent()
1253
    {
1254
        /* BEGIN: Use Case */
1255
        $draftVersion2 = $this->createUpdatedDraftVersion2();
1256
        /* END: Use Case */
1257
1258
        $this->assertInstanceOf(
1259
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1260
            $draftVersion2
1261
        );
1262
1263
        $this->assertEquals(
1264
            $this->generateId('user', 10),
1265
            $draftVersion2->versionInfo->creatorId,
1266
            'creatorId is not properly set on new Version'
1267
        );
1268
1269
        return $draftVersion2;
1270
    }
1271
1272
    /**
1273
     * Test for the updateContent_WithDifferentUser() method.
1274
     *
1275
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1276
     *
1277
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1278
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentUpdateStruct
1279
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1280
     * @group user
1281
     * @group field-type
1282
     */
1283
    public function testUpdateContentWithDifferentUser()
1284
    {
1285
        /* BEGIN: Use Case */
1286
        $arrayWithDraftVersion2 = $this->createUpdatedDraftVersion2NotAdmin();
1287
        /* END: Use Case */
1288
1289
        $this->assertInstanceOf(
1290
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1291
            $arrayWithDraftVersion2[0]
1292
        );
1293
1294
        $this->assertEquals(
1295
            $this->generateId('user', $arrayWithDraftVersion2[1]),
1296
            $arrayWithDraftVersion2[0]->versionInfo->creatorId,
1297
            'creatorId is not properly set on new Version'
1298
        );
1299
1300
        return $arrayWithDraftVersion2[0];
1301
    }
1302
1303
    /**
1304
     * Test for the updateContent() method.
1305
     *
1306
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1307
     *
1308
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1309
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1310
     */
1311
    public function testUpdateContentSetsExpectedFields($content)
1312
    {
1313
        $actual = $this->normalizeFields($content->getFields());
1314
1315
        $expected = array(
1316
            new Field(
1317
                array(
1318
                    'id' => 0,
1319
                    'value' => true,
1320
                    'languageCode' => 'eng-GB',
1321
                    'fieldDefIdentifier' => 'description',
1322
                )
1323
            ),
1324
            new Field(
1325
                array(
1326
                    'id' => 0,
1327
                    'value' => true,
1328
                    'languageCode' => 'eng-US',
1329
                    'fieldDefIdentifier' => 'description',
1330
                )
1331
            ),
1332
            new Field(
1333
                array(
1334
                    'id' => 0,
1335
                    'value' => true,
1336
                    'languageCode' => 'eng-GB',
1337
                    'fieldDefIdentifier' => 'name',
1338
                )
1339
            ),
1340
            new Field(
1341
                array(
1342
                    'id' => 0,
1343
                    'value' => true,
1344
                    'languageCode' => 'eng-US',
1345
                    'fieldDefIdentifier' => 'name',
1346
                )
1347
            ),
1348
        );
1349
1350
        $this->assertEquals($expected, $actual);
1351
    }
1352
1353
    /**
1354
     * Test for the updateContent() method.
1355
     *
1356
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1357
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1358
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1359
     */
1360 View Code Duplication
    public function testUpdateContentThrowsBadStateException()
1361
    {
1362
        $repository = $this->getRepository();
1363
1364
        $contentService = $repository->getContentService();
1365
1366
        /* BEGIN: Use Case */
1367
        $content = $this->createContentVersion1();
1368
1369
        // Now create an update struct and modify some fields
1370
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1371
        $contentUpdateStruct->setField('title', 'An awesome² story about ezp.');
1372
        $contentUpdateStruct->setField('title', 'An awesome²³ story about ezp.', 'eng-GB');
1373
1374
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1375
1376
        // This call will fail with a "BadStateException", because $publishedContent
1377
        // is not a draft.
1378
        $contentService->updateContent(
1379
            $content->getVersionInfo(),
1380
            $contentUpdateStruct
1381
        );
1382
        /* END: Use Case */
1383
    }
1384
1385
    /**
1386
     * Test for the updateContent() method.
1387
     *
1388
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1389
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1390
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1391
     */
1392 View Code Duplication
    public function testUpdateContentThrowsInvalidArgumentExceptionWhenFieldTypeDoesNotAccept()
1393
    {
1394
        $repository = $this->getRepository();
1395
1396
        $contentService = $repository->getContentService();
1397
1398
        /* BEGIN: Use Case */
1399
        $draft = $this->createContentDraftVersion1();
1400
1401
        // Now create an update struct and modify some fields
1402
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1403
        // The name field does not accept a stdClass object as its input
1404
        $contentUpdateStruct->setField('name', new \stdClass(), 'eng-US');
1405
1406
        // Throws an InvalidArgumentException, since the value for field "name"
1407
        // is not accepted
1408
        $contentService->updateContent(
1409
            $draft->getVersionInfo(),
1410
            $contentUpdateStruct
1411
        );
1412
        /* END: Use Case */
1413
    }
1414
1415
    /**
1416
     * Test for the updateContent() method.
1417
     *
1418
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1419
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1420
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1421
     */
1422 View Code Duplication
    public function testUpdateContentWhenMandatoryFieldIsEmpty()
1423
    {
1424
        $repository = $this->getRepository();
1425
1426
        $contentService = $repository->getContentService();
1427
1428
        /* BEGIN: Use Case */
1429
        $draft = $this->createContentDraftVersion1();
1430
1431
        // Now create an update struct and set a mandatory field to null
1432
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1433
        $contentUpdateStruct->setField('name', null);
1434
1435
        // Don't set this, then the above call without languageCode will fail
1436
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1437
1438
        // This call will fail with a "ContentFieldValidationException", because the
1439
        // mandatory "name" field is empty.
1440
        $contentService->updateContent(
1441
            $draft->getVersionInfo(),
1442
            $contentUpdateStruct
1443
        );
1444
        /* END: Use Case */
1445
    }
1446
1447
    /**
1448
     * Test for the updateContent() method.
1449
     *
1450
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1451
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1452
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1453
     */
1454
    public function testUpdateContentThrowsContentFieldValidationException()
1455
    {
1456
        $repository = $this->getRepository();
1457
1458
        /* BEGIN: Use Case */
1459
        $contentTypeService = $repository->getContentTypeService();
1460
        $contentService = $repository->getContentService();
1461
1462
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
1463
1464
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
1465
        $contentCreate->setField('name', 'An awesome Sidelfingen folder');
1466
1467
        $draft = $contentService->createContent($contentCreate);
1468
1469
        $contentUpdate = $contentService->newContentUpdateStruct();
1470
        // Violates string length constraint
1471
        $contentUpdate->setField('short_name', str_repeat('a', 200), 'eng-US');
1472
1473
        // Throws ContentFieldValidationException because the string length
1474
        // validation of the field "short_name" fails
1475
        $contentService->updateContent($draft->getVersionInfo(), $contentUpdate);
1476
        /* END: Use Case */
1477
    }
1478
1479
    /**
1480
     * Test for the updateContent() method.
1481
     *
1482
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1483
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1484
     */
1485
    public function testUpdateContentWithNotUpdatingMandatoryField()
1486
    {
1487
        $repository = $this->getRepository();
1488
1489
        $contentService = $repository->getContentService();
1490
1491
        /* BEGIN: Use Case */
1492
        $draft = $this->createContentDraftVersion1();
1493
1494
        // Now create an update struct which does not overwrite mandatory
1495
        // fields
1496
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1497
        $contentUpdateStruct->setField(
1498
            'description',
1499
            '<?xml version="1.0" encoding="UTF-8"?><section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0-variant ezpublish-1.0"/>'
1500
        );
1501
1502
        // Don't set this, then the above call without languageCode will fail
1503
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1504
1505
        // This will only update the "description" field in the "eng-US"
1506
        // language
1507
        $updatedDraft = $contentService->updateContent(
1508
            $draft->getVersionInfo(),
1509
            $contentUpdateStruct
1510
        );
1511
        /* END: Use Case */
1512
1513
        foreach ($updatedDraft->getFields() as $field) {
1514
            if ($field->languageCode === 'eng-US' && $field->fieldDefIdentifier === 'name' && $field->value !== null) {
0 ignored issues
show
Documentation introduced by
The property $value is declared protected in eZ\Publish\API\Repository\Values\Content\Field. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
1515
                // Found field
1516
                return;
1517
            }
1518
        }
1519
        $this->fail(
1520
            'Field with identifier "name" in language "eng-US" could not be found or has empty value.'
1521
        );
1522
    }
1523
1524
    /**
1525
     * Test for the createContentDraft() method.
1526
     *
1527
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
1528
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1529
     */
1530 View Code Duplication
    public function testCreateContentDraftWithSecondParameter()
1531
    {
1532
        $repository = $this->getRepository();
1533
1534
        $contentService = $repository->getContentService();
1535
1536
        /* BEGIN: Use Case */
1537
        $contentVersion2 = $this->createContentVersion2();
1538
1539
        // Now we create a new draft from the initial version
1540
        $draftedContentReloaded = $contentService->createContentDraft(
1541
            $contentVersion2->contentInfo,
1542
            $contentVersion2->getVersionInfo()
1543
        );
1544
        /* END: Use Case */
1545
1546
        $this->assertEquals(3, $draftedContentReloaded->getVersionInfo()->versionNo);
1547
    }
1548
1549
    /**
1550
     * Test for the publishVersion() method.
1551
     *
1552
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1553
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1554
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1555
     */
1556 View Code Duplication
    public function testPublishVersionFromContentDraft()
1557
    {
1558
        $repository = $this->getRepository();
1559
1560
        $contentService = $repository->getContentService();
1561
1562
        /* BEGIN: Use Case */
1563
        $contentVersion2 = $this->createContentVersion2();
1564
        /* END: Use Case */
1565
1566
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo);
1567
1568
        $this->assertEquals(
1569
            array(
1570
                'status' => VersionInfo::STATUS_PUBLISHED,
1571
                'versionNo' => 2,
1572
            ),
1573
            array(
1574
                'status' => $versionInfo->status,
1575
                'versionNo' => $versionInfo->versionNo,
1576
            )
1577
        );
1578
    }
1579
1580
    /**
1581
     * Test for the publishVersion() method.
1582
     *
1583
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1584
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1585
     */
1586 View Code Duplication
    public function testPublishVersionFromContentDraftArchivesOldVersion()
1587
    {
1588
        $repository = $this->getRepository();
1589
1590
        $contentService = $repository->getContentService();
1591
1592
        /* BEGIN: Use Case */
1593
        $contentVersion2 = $this->createContentVersion2();
1594
        /* END: Use Case */
1595
1596
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo, 1);
1597
1598
        $this->assertEquals(
1599
            array(
1600
                'status' => VersionInfo::STATUS_ARCHIVED,
1601
                'versionNo' => 1,
1602
            ),
1603
            array(
1604
                'status' => $versionInfo->status,
1605
                'versionNo' => $versionInfo->versionNo,
1606
            )
1607
        );
1608
    }
1609
1610
    /**
1611
     * Test for the publishVersion() method.
1612
     *
1613
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1614
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1615
     */
1616
    public function testPublishVersionFromContentDraftUpdatesContentInfoCurrentVersion()
1617
    {
1618
        /* BEGIN: Use Case */
1619
        $contentVersion2 = $this->createContentVersion2();
1620
        /* END: Use Case */
1621
1622
        $this->assertEquals(2, $contentVersion2->contentInfo->currentVersionNo);
1623
    }
1624
1625
    /**
1626
     * Test for the publishVersion() method.
1627
     *
1628
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1629
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1630
     */
1631
    public function testPublishVersionFromOldContentDraftArchivesNewerVersionNo()
1632
    {
1633
        $repository = $this->getRepository();
1634
1635
        $contentService = $repository->getContentService();
1636
1637
        /* BEGIN: Use Case */
1638
        $content = $this->createContentVersion1();
1639
1640
        // Create a new draft with versionNo = 2
1641
        $draftedContentVersion2 = $contentService->createContentDraft($content->contentInfo);
1642
1643
        // Create another new draft with versionNo = 3
1644
        $draftedContentVersion3 = $contentService->createContentDraft($content->contentInfo);
1645
1646
        // Publish draft with versionNo = 3
1647
        $contentService->publishVersion($draftedContentVersion3->getVersionInfo());
1648
1649
        // Publish the first draft with versionNo = 2
1650
        // currentVersionNo is now 2, versionNo 3 will be archived
1651
        $publishedDraft = $contentService->publishVersion($draftedContentVersion2->getVersionInfo());
1652
        /* END: Use Case */
1653
1654
        $this->assertEquals(2, $publishedDraft->contentInfo->currentVersionNo);
1655
    }
1656
1657
    /**
1658
     * Test for the newContentMetadataUpdateStruct() method.
1659
     *
1660
     * @see \eZ\Publish\API\Repository\ContentService::newContentMetadataUpdateStruct()
1661
     * @group user
1662
     */
1663
    public function testNewContentMetadataUpdateStruct()
1664
    {
1665
        $repository = $this->getRepository();
1666
1667
        /* BEGIN: Use Case */
1668
        $contentService = $repository->getContentService();
1669
1670
        // Creates a new metadata update struct
1671
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1672
1673
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1674
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1675
        $metadataUpdate->alwaysAvailable = false;
1676
        /* END: Use Case */
1677
1678
        $this->assertInstanceOf(
1679
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentMetadataUpdateStruct',
1680
            $metadataUpdate
1681
        );
1682
    }
1683
1684
    /**
1685
     * Test for the updateContentMetadata() method.
1686
     *
1687
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1688
     *
1689
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1690
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1691
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentMetadataUpdateStruct
1692
     * @group user
1693
     */
1694
    public function testUpdateContentMetadata()
1695
    {
1696
        $repository = $this->getRepository();
1697
1698
        $contentService = $repository->getContentService();
1699
1700
        /* BEGIN: Use Case */
1701
        $content = $this->createContentVersion1();
1702
1703
        // Creates a metadata update struct
1704
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1705
1706
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1707
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1708
        $metadataUpdate->alwaysAvailable = false;
1709
        $metadataUpdate->publishedDate = $this->createDateTime(441759600); // 1984/01/01
1710
        $metadataUpdate->modificationDate = $this->createDateTime(441759600); // 1984/01/01
1711
1712
        // Update the metadata of the published content object
1713
        $content = $contentService->updateContentMetadata(
1714
            $content->contentInfo,
1715
            $metadataUpdate
1716
        );
1717
        /* END: Use Case */
1718
1719
        $this->assertInstanceOf(
1720
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1721
            $content
1722
        );
1723
1724
        return $content;
1725
    }
1726
1727
    /**
1728
     * Test for the updateContentMetadata() method.
1729
     *
1730
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1731
     *
1732
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1733
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
1734
     */
1735
    public function testUpdateContentMetadataSetsExpectedProperties($content)
1736
    {
1737
        $contentInfo = $content->contentInfo;
1738
1739
        $this->assertEquals(
1740
            array(
1741
                'remoteId' => 'aaaabbbbccccddddeeeeffff11112222',
1742
                'sectionId' => $this->generateId('section', 1),
1743
                'alwaysAvailable' => false,
1744
                'currentVersionNo' => 1,
1745
                'mainLanguageCode' => 'eng-GB',
1746
                'modificationDate' => $this->createDateTime(441759600),
1747
                '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...
1748
                'published' => true,
1749
                'publishedDate' => $this->createDateTime(441759600),
1750
            ),
1751
            array(
1752
                'remoteId' => $contentInfo->remoteId,
1753
                'sectionId' => $contentInfo->sectionId,
1754
                'alwaysAvailable' => $contentInfo->alwaysAvailable,
1755
                'currentVersionNo' => $contentInfo->currentVersionNo,
1756
                'mainLanguageCode' => $contentInfo->mainLanguageCode,
1757
                'modificationDate' => $contentInfo->modificationDate,
1758
                'ownerId' => $contentInfo->ownerId,
1759
                'published' => $contentInfo->published,
1760
                'publishedDate' => $contentInfo->publishedDate,
1761
            )
1762
        );
1763
    }
1764
1765
    /**
1766
     * Test for the updateContentMetadata() method.
1767
     *
1768
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1769
     *
1770
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1771
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
1772
     */
1773
    public function testUpdateContentMetadataNotUpdatesContentVersion($content)
1774
    {
1775
        $this->assertEquals(1, $content->getVersionInfo()->versionNo);
1776
    }
1777
1778
    /**
1779
     * Test for the updateContentMetadata() method.
1780
     *
1781
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1782
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1783
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
1784
     */
1785
    public function testUpdateContentMetadataThrowsInvalidArgumentException()
1786
    {
1787
        $repository = $this->getRepository();
1788
1789
        $contentService = $repository->getContentService();
1790
1791
        /* BEGIN: Use Case */
1792
        // RemoteId of the "Media" page of an eZ Publish demo installation
1793
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1794
1795
        $content = $this->createContentVersion1();
1796
1797
        // Creates a metadata update struct
1798
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1799
        $metadataUpdate->remoteId = $mediaRemoteId;
1800
1801
        // This call will fail with an "InvalidArgumentException", because the
1802
        // specified remoteId is already used by the "Media" page.
1803
        $contentService->updateContentMetadata(
1804
            $content->contentInfo,
1805
            $metadataUpdate
1806
        );
1807
        /* END: Use Case */
1808
    }
1809
1810
    /**
1811
     * Test for the deleteContent() method.
1812
     *
1813
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
1814
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1815
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1816
     */
1817 View Code Duplication
    public function testDeleteContent()
1818
    {
1819
        $repository = $this->getRepository();
1820
1821
        $contentService = $repository->getContentService();
1822
        $locationService = $repository->getLocationService();
1823
1824
        /* BEGIN: Use Case */
1825
        $contentVersion2 = $this->createContentVersion2();
1826
1827
        // Load the locations for this content object
1828
        $locations = $locationService->loadLocations($contentVersion2->contentInfo);
1829
1830
        // This will delete the content, all versions and the associated locations
1831
        $contentService->deleteContent($contentVersion2->contentInfo);
1832
        /* END: Use Case */
1833
1834
        foreach ($locations as $location) {
1835
            $locationService->loadLocation($location->id);
1836
        }
1837
    }
1838
1839
    /**
1840
     * Test for the deleteContent() method.
1841
     *
1842
     * Test for issue EZP-21057:
1843
     * "contentService: Unable to delete a content with an empty file attribute"
1844
     *
1845
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
1846
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1847
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1848
     */
1849 View Code Duplication
    public function testDeleteContentWithEmptyBinaryField()
1850
    {
1851
        $repository = $this->getRepository();
1852
1853
        $contentService = $repository->getContentService();
1854
        $locationService = $repository->getLocationService();
1855
1856
        /* BEGIN: Use Case */
1857
        $contentVersion = $this->createContentVersion1EmptyBinaryField();
1858
1859
        // Load the locations for this content object
1860
        $locations = $locationService->loadLocations($contentVersion->contentInfo);
1861
1862
        // This will delete the content, all versions and the associated locations
1863
        $contentService->deleteContent($contentVersion->contentInfo);
1864
        /* END: Use Case */
1865
1866
        foreach ($locations as $location) {
1867
            $locationService->loadLocation($location->id);
1868
        }
1869
    }
1870
1871
    /**
1872
     * Test for the loadContentDrafts() method.
1873
     *
1874
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
1875
     */
1876
    public function testLoadContentDraftsReturnsEmptyArrayByDefault()
1877
    {
1878
        $repository = $this->getRepository();
1879
1880
        /* BEGIN: Use Case */
1881
        $contentService = $repository->getContentService();
1882
1883
        $contentDrafts = $contentService->loadContentDrafts();
1884
        /* END: Use Case */
1885
1886
        $this->assertSame(array(), $contentDrafts);
1887
    }
1888
1889
    /**
1890
     * Test for the loadContentDrafts() method.
1891
     *
1892
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
1893
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1894
     */
1895
    public function testLoadContentDrafts()
1896
    {
1897
        $repository = $this->getRepository();
1898
1899
        /* BEGIN: Use Case */
1900
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
1901
        // of a eZ Publish demo installation.
1902
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1903
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
1904
1905
        $contentService = $repository->getContentService();
1906
1907
        // "Media" content object
1908
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
1909
1910
        // "eZ Publish Demo Design ..." content object
1911
        $demoDesignContentInfo = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
1912
1913
        // Create some drafts
1914
        $contentService->createContentDraft($mediaContentInfo);
1915
        $contentService->createContentDraft($demoDesignContentInfo);
1916
1917
        // Now $contentDrafts should contain two drafted versions
1918
        $draftedVersions = $contentService->loadContentDrafts();
1919
        /* END: Use Case */
1920
1921
        $actual = array(
1922
            $draftedVersions[0]->status,
1923
            $draftedVersions[0]->getContentInfo()->remoteId,
1924
            $draftedVersions[1]->status,
1925
            $draftedVersions[1]->getContentInfo()->remoteId,
1926
        );
1927
        sort($actual, SORT_STRING);
1928
1929
        $this->assertEquals(
1930
            array(
1931
                VersionInfo::STATUS_DRAFT,
1932
                VersionInfo::STATUS_DRAFT,
1933
                $demoDesignRemoteId,
1934
                $mediaRemoteId,
1935
            ),
1936
            $actual
1937
        );
1938
    }
1939
1940
    /**
1941
     * Test for the loadContentDrafts() method.
1942
     *
1943
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
1944
     */
1945
    public function testLoadContentDraftsWithFirstParameter()
1946
    {
1947
        $repository = $this->getRepository();
1948
1949
        /* BEGIN: Use Case */
1950
        $user = $this->createUserVersion1();
1951
1952
        // Get current user
1953
        $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...
1954
1955
        // Set new editor as user
1956
        $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...
1957
1958
        // Remote id of the "Media" content object in an eZ Publish demo installation.
1959
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1960
1961
        $contentService = $repository->getContentService();
1962
1963
        // "Media" content object
1964
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
1965
1966
        // Create a content draft
1967
        $contentService->createContentDraft($mediaContentInfo);
1968
1969
        // Reset to previous current user
1970
        $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...
1971
1972
        // Now $contentDrafts for the previous current user and the new user
1973
        $newCurrentUserDrafts = $contentService->loadContentDrafts($user);
1974
        $oldCurrentUserDrafts = $contentService->loadContentDrafts($oldCurrentUser);
1975
        /* END: Use Case */
1976
1977
        $this->assertSame(array(), $oldCurrentUserDrafts);
1978
1979
        $this->assertEquals(
1980
            array(
1981
                VersionInfo::STATUS_DRAFT,
1982
                $mediaRemoteId,
1983
            ),
1984
            array(
1985
                $newCurrentUserDrafts[0]->status,
1986
                $newCurrentUserDrafts[0]->getContentInfo()->remoteId,
1987
            )
1988
        );
1989
    }
1990
1991
    /**
1992
     * Test for the loadVersionInfo() method.
1993
     *
1994
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
1995
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1996
     */
1997 View Code Duplication
    public function testLoadVersionInfoWithSecondParameter()
1998
    {
1999
        $repository = $this->getRepository();
2000
2001
        $contentService = $repository->getContentService();
2002
2003
        /* BEGIN: Use Case */
2004
        $publishedContent = $this->createContentVersion1();
2005
2006
        $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...
2007
2008
        // Will return the VersionInfo of the $draftContent
2009
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2010
        /* END: Use Case */
2011
2012
        $this->assertEquals(2, $versionInfo->versionNo);
2013
2014
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2015
        $this->assertEquals(
2016
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2017
            $versionInfo->getContentInfo()->mainLocationId
2018
        );
2019
    }
2020
2021
    /**
2022
     * Test for the loadVersionInfo() method.
2023
     *
2024
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2025
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2026
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2027
     */
2028
    public function testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter()
2029
    {
2030
        $repository = $this->getRepository();
2031
2032
        $contentService = $repository->getContentService();
2033
2034
        /* BEGIN: Use Case */
2035
        $draft = $this->createContentDraftVersion1();
2036
2037
        // This call will fail with a "NotFoundException", because not versionNo
2038
        // 2 exists for this content object.
2039
        $contentService->loadVersionInfo($draft->contentInfo, 2);
2040
        /* END: Use Case */
2041
    }
2042
2043
    /**
2044
     * Test for the loadVersionInfoById() method.
2045
     *
2046
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2047
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2048
     */
2049 View Code Duplication
    public function testLoadVersionInfoByIdWithSecondParameter()
2050
    {
2051
        $repository = $this->getRepository();
2052
2053
        $contentService = $repository->getContentService();
2054
2055
        /* BEGIN: Use Case */
2056
        $publishedContent = $this->createContentVersion1();
2057
2058
        $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...
2059
2060
        // Will return the VersionInfo of the $draftContent
2061
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2062
        /* END: Use Case */
2063
2064
        $this->assertEquals(2, $versionInfo->versionNo);
2065
2066
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2067
        $this->assertEquals(
2068
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2069
            $versionInfo->getContentInfo()->mainLocationId
2070
        );
2071
    }
2072
2073
    /**
2074
     * Test for the loadVersionInfoById() method.
2075
     *
2076
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2077
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2078
     */
2079 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter()
2080
    {
2081
        $repository = $this->getRepository();
2082
2083
        $contentService = $repository->getContentService();
2084
2085
        /* BEGIN: Use Case */
2086
        $content = $this->createContentVersion1();
2087
2088
        // This call will fail with a "NotFoundException", because not versionNo
2089
        // 2 exists for this content object.
2090
        $contentService->loadVersionInfoById($content->id, 2);
2091
        /* END: Use Case */
2092
    }
2093
2094
    /**
2095
     * Test for the loadContentByVersionInfo() method.
2096
     *
2097
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
2098
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2099
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
2100
     */
2101
    public function testLoadContentByVersionInfoWithSecondParameter()
2102
    {
2103
        $repository = $this->getRepository();
2104
2105
        $sectionId = $this->generateId('section', 1);
2106
        /* BEGIN: Use Case */
2107
        $contentTypeService = $repository->getContentTypeService();
2108
2109
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2110
2111
        $contentService = $repository->getContentService();
2112
2113
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2114
2115
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2116
2117
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2118
2119
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2120
        // $sectionId contains the ID of section 1
2121
        $contentCreateStruct->sectionId = $sectionId;
2122
        $contentCreateStruct->alwaysAvailable = true;
2123
2124
        // Create a new content draft
2125
        $content = $contentService->createContent($contentCreateStruct);
2126
2127
        // Now publish this draft
2128
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2129
2130
        // Will return a content instance with fields in "eng-US"
2131
        $reloadedContent = $contentService->loadContentByVersionInfo(
2132
            $publishedContent->getVersionInfo(),
2133
            array(
2134
                'eng-GB',
2135
            ),
2136
            false
2137
        );
2138
        /* END: Use Case */
2139
2140
        $actual = array();
2141 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...
2142
            $actual[] = new Field(
2143
                array(
2144
                    'id' => 0,
2145
                    'value' => ($field->value !== null ? true : null), // Actual value tested by FieldType integration tests
0 ignored issues
show
Documentation introduced by
The property $value is declared protected in eZ\Publish\API\Repository\Values\Content\Field. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
2146
                    'languageCode' => $field->languageCode,
2147
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
2148
                )
2149
            );
2150
        }
2151
        usort(
2152
            $actual,
2153 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...
2154
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
2155
                    return strcasecmp($field1->languageCode, $field2->languageCode);
2156
                }
2157
2158
                return $return;
2159
            }
2160
        );
2161
2162
        $expected = array(
2163
            new Field(
2164
                array(
2165
                    'id' => 0,
2166
                    'value' => true,
2167
                    'languageCode' => 'eng-GB',
2168
                    'fieldDefIdentifier' => 'description',
2169
                )
2170
            ),
2171
            new Field(
2172
                array(
2173
                    'id' => 0,
2174
                    'value' => true,
2175
                    'languageCode' => 'eng-GB',
2176
                    'fieldDefIdentifier' => 'name',
2177
                )
2178
            ),
2179
        );
2180
2181
        $this->assertEquals($expected, $actual);
2182
    }
2183
2184
    /**
2185
     * Test for the loadContentByContentInfo() method.
2186
     *
2187
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages)
2188
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2189
     */
2190
    public function testLoadContentByContentInfoWithLanguageParameters()
2191
    {
2192
        $repository = $this->getRepository();
2193
2194
        $sectionId = $this->generateId('section', 1);
2195
        /* BEGIN: Use Case */
2196
        $contentTypeService = $repository->getContentTypeService();
2197
2198
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2199
2200
        $contentService = $repository->getContentService();
2201
2202
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2203
2204
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2205
2206
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2207
2208
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2209
        // $sectionId contains the ID of section 1
2210
        $contentCreateStruct->sectionId = $sectionId;
2211
        $contentCreateStruct->alwaysAvailable = true;
2212
2213
        // Create a new content draft
2214
        $content = $contentService->createContent($contentCreateStruct);
2215
2216
        // Now publish this draft
2217
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2218
2219
        // Will return a content instance with fields in "eng-US"
2220
        $reloadedContent = $contentService->loadContentByContentInfo(
2221
            $publishedContent->contentInfo,
2222
            array(
2223
                'eng-US',
2224
            ),
2225
            null,
2226
            false
2227
        );
2228
        /* END: Use Case */
2229
2230
        $actual = $this->normalizeFields($reloadedContent->getFields());
2231
2232
        $expected = array(
2233
            new Field(
2234
                array(
2235
                    'id' => 0,
2236
                    'value' => true,
2237
                    'languageCode' => 'eng-US',
2238
                    'fieldDefIdentifier' => 'description',
2239
                )
2240
            ),
2241
            new Field(
2242
                array(
2243
                    'id' => 0,
2244
                    'value' => true,
2245
                    'languageCode' => 'eng-US',
2246
                    'fieldDefIdentifier' => 'name',
2247
                )
2248
            ),
2249
        );
2250
2251
        $this->assertEquals($expected, $actual);
2252
2253
        // Will return a content instance with fields in "eng-GB" (versions prior to 6.0.0-beta9 returned "eng-US" also)
2254
        $reloadedContent = $contentService->loadContentByContentInfo(
2255
            $publishedContent->contentInfo,
2256
            array(
2257
                'eng-GB',
2258
            ),
2259
            null,
2260
            true
2261
        );
2262
2263
        $actual = $this->normalizeFields($reloadedContent->getFields());
2264
2265
        $expected = array(
2266
            new Field(
2267
                array(
2268
                    'id' => 0,
2269
                    'value' => true,
2270
                    'languageCode' => 'eng-GB',
2271
                    'fieldDefIdentifier' => 'description',
2272
                )
2273
            ),
2274
            new Field(
2275
                array(
2276
                    'id' => 0,
2277
                    'value' => true,
2278
                    'languageCode' => 'eng-GB',
2279
                    'fieldDefIdentifier' => 'name',
2280
                )
2281
            ),
2282
        );
2283
2284
        $this->assertEquals($expected, $actual);
2285
2286
        // Will return a content instance with fields in main language "eng-US", as "fre-FR" does not exists
2287
        $reloadedContent = $contentService->loadContentByContentInfo(
2288
            $publishedContent->contentInfo,
2289
            array(
2290
                'fre-FR',
2291
            ),
2292
            null,
2293
            true
2294
        );
2295
2296
        $actual = $this->normalizeFields($reloadedContent->getFields());
2297
2298
        $expected = array(
2299
            new Field(
2300
                array(
2301
                    'id' => 0,
2302
                    'value' => true,
2303
                    'languageCode' => 'eng-US',
2304
                    'fieldDefIdentifier' => 'description',
2305
                )
2306
            ),
2307
            new Field(
2308
                array(
2309
                    'id' => 0,
2310
                    'value' => true,
2311
                    'languageCode' => 'eng-US',
2312
                    'fieldDefIdentifier' => 'name',
2313
                )
2314
            ),
2315
        );
2316
2317
        $this->assertEquals($expected, $actual);
2318
    }
2319
2320
    /**
2321
     * Test for the loadContentByContentInfo() method.
2322
     *
2323
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2324
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2325
     */
2326 View Code Duplication
    public function testLoadContentByContentInfoWithVersionNumberParameter()
2327
    {
2328
        $repository = $this->getRepository();
2329
2330
        $contentService = $repository->getContentService();
2331
2332
        /* BEGIN: Use Case */
2333
        $publishedContent = $this->createContentVersion1();
2334
2335
        $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...
2336
2337
        // This content instance is identical to $draftContent
2338
        $draftContentReloaded = $contentService->loadContentByContentInfo(
2339
            $publishedContent->contentInfo,
2340
            null,
2341
            2
2342
        );
2343
        /* END: Use Case */
2344
2345
        $this->assertEquals(
2346
            2,
2347
            $draftContentReloaded->getVersionInfo()->versionNo
2348
        );
2349
2350
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2351
        $this->assertEquals(
2352
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2353
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2354
        );
2355
    }
2356
2357
    /**
2358
     * Test for the loadContentByContentInfo() method.
2359
     *
2360
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2361
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2362
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithVersionNumberParameter
2363
     */
2364 View Code Duplication
    public function testLoadContentByContentInfoThrowsNotFoundExceptionWithVersionNumberParameter()
2365
    {
2366
        $repository = $this->getRepository();
2367
2368
        $contentService = $repository->getContentService();
2369
2370
        /* BEGIN: Use Case */
2371
        $content = $this->createContentVersion1();
2372
2373
        // This call will fail with a "NotFoundException", because no content
2374
        // with versionNo = 2 exists.
2375
        $contentService->loadContentByContentInfo($content->contentInfo, null, 2);
2376
        /* END: Use Case */
2377
    }
2378
2379
    /**
2380
     * Test for the loadContent() method.
2381
     *
2382
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages)
2383
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2384
     */
2385
    public function testLoadContentWithSecondParameter()
2386
    {
2387
        $repository = $this->getRepository();
2388
2389
        $contentService = $repository->getContentService();
2390
2391
        /* BEGIN: Use Case */
2392
        $draft = $this->createMultipleLanguageDraftVersion1();
2393
2394
        // This draft contains those fields localized with "eng-GB"
2395
        $draftLocalized = $contentService->loadContent($draft->id, array('eng-GB'), null, false);
2396
        /* END: Use Case */
2397
2398
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2399
    }
2400
2401
    /**
2402
     * Test for the loadContent() method.
2403
     *
2404
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2405
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2406
     */
2407 View Code Duplication
    public function testLoadContentWithThirdParameter()
2408
    {
2409
        $repository = $this->getRepository();
2410
2411
        $contentService = $repository->getContentService();
2412
2413
        /* BEGIN: Use Case */
2414
        $publishedContent = $this->createContentVersion1();
2415
2416
        $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...
2417
2418
        // This content instance is identical to $draftContent
2419
        $draftContentReloaded = $contentService->loadContent($publishedContent->id, null, 2);
2420
        /* END: Use Case */
2421
2422
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2423
2424
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2425
        $this->assertEquals(
2426
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2427
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2428
        );
2429
    }
2430
2431
    /**
2432
     * Test for the loadContent() method.
2433
     *
2434
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2435
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2436
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithThirdParameter
2437
     */
2438 View Code Duplication
    public function testLoadContentThrowsNotFoundExceptionWithThirdParameter()
2439
    {
2440
        $repository = $this->getRepository();
2441
2442
        $contentService = $repository->getContentService();
2443
2444
        /* BEGIN: Use Case */
2445
        $content = $this->createContentVersion1();
2446
2447
        // This call will fail with a "NotFoundException", because for this
2448
        // content object no versionNo=2 exists.
2449
        $contentService->loadContent($content->id, null, 2);
2450
        /* END: Use Case */
2451
    }
2452
2453
    /**
2454
     * Test for the loadContentByRemoteId() method.
2455
     *
2456
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages)
2457
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2458
     */
2459
    public function testLoadContentByRemoteIdWithSecondParameter()
2460
    {
2461
        $repository = $this->getRepository();
2462
2463
        $contentService = $repository->getContentService();
2464
2465
        /* BEGIN: Use Case */
2466
        $draft = $this->createMultipleLanguageDraftVersion1();
2467
2468
        $contentService->publishVersion($draft->versionInfo);
2469
2470
        // This draft contains those fields localized with "eng-GB"
2471
        $draftLocalized = $contentService->loadContentByRemoteId(
2472
            $draft->contentInfo->remoteId,
2473
            array('eng-GB'),
2474
            null,
2475
            false
2476
        );
2477
        /* END: Use Case */
2478
2479
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2480
    }
2481
2482
    /**
2483
     * Test for the loadContentByRemoteId() method.
2484
     *
2485
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2486
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2487
     */
2488 View Code Duplication
    public function testLoadContentByRemoteIdWithThirdParameter()
2489
    {
2490
        $repository = $this->getRepository();
2491
2492
        $contentService = $repository->getContentService();
2493
2494
        /* BEGIN: Use Case */
2495
        $publishedContent = $this->createContentVersion1();
2496
2497
        $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...
2498
2499
        // This content instance is identical to $draftContent
2500
        $draftContentReloaded = $contentService->loadContentByRemoteId(
2501
            $publishedContent->contentInfo->remoteId,
2502
            null,
2503
            2
2504
        );
2505
        /* END: Use Case */
2506
2507
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2508
2509
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2510
        $this->assertEquals(
2511
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2512
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2513
        );
2514
    }
2515
2516
    /**
2517
     * Test for the loadContentByRemoteId() method.
2518
     *
2519
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2520
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2521
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithThirdParameter
2522
     */
2523
    public function testLoadContentByRemoteIdThrowsNotFoundExceptionWithThirdParameter()
2524
    {
2525
        $repository = $this->getRepository();
2526
2527
        $contentService = $repository->getContentService();
2528
2529
        /* BEGIN: Use Case */
2530
        $content = $this->createContentVersion1();
2531
2532
        // This call will fail with a "NotFoundException", because for this
2533
        // content object no versionNo=2 exists.
2534
        $contentService->loadContentByRemoteId(
2535
            $content->contentInfo->remoteId,
2536
            null,
2537
            2
2538
        );
2539
        /* END: Use Case */
2540
    }
2541
2542
    /**
2543
     * Test for the deleteVersion() method.
2544
     *
2545
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2546
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2547
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2548
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2549
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
2550
     */
2551
    public function testDeleteVersion()
2552
    {
2553
        $repository = $this->getRepository();
2554
2555
        $contentService = $repository->getContentService();
2556
2557
        /* BEGIN: Use Case */
2558
        $content = $this->createContentVersion1();
2559
2560
        // Create new draft, because published or last version of the Content can't be deleted
2561
        $draft = $contentService->createContentDraft(
2562
            $content->getVersionInfo()->getContentInfo()
2563
        );
2564
2565
        // Delete the previously created draft
2566
        $contentService->deleteVersion($draft->getVersionInfo());
2567
        /* END: Use Case */
2568
2569
        $versions = $contentService->loadVersions($content->getVersionInfo()->getContentInfo());
2570
2571
        $this->assertCount(1, $versions);
2572
        $this->assertEquals(
2573
            $content->getVersionInfo()->id,
2574
            $versions[0]->id
2575
        );
2576
    }
2577
2578
    /**
2579
     * Test for the deleteVersion() method.
2580
     *
2581
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2582
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
2583
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2584
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2585
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2586
     */
2587
    public function testDeleteVersionThrowsBadStateExceptionOnPublishedVersion()
2588
    {
2589
        $repository = $this->getRepository();
2590
2591
        $contentService = $repository->getContentService();
2592
2593
        /* BEGIN: Use Case */
2594
        $content = $this->createContentVersion1();
2595
2596
        // This call will fail with a "BadStateException", because the content
2597
        // version is currently published.
2598
        $contentService->deleteVersion($content->getVersionInfo());
2599
        /* END: Use Case */
2600
    }
2601
2602
    /**
2603
     * Test for the deleteVersion() method.
2604
     *
2605
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
2606
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
2607
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
2608
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2609
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2610
     */
2611 View Code Duplication
    public function testDeleteVersionThrowsBadStateExceptionOnLastVersion()
2612
    {
2613
        $repository = $this->getRepository();
2614
2615
        $contentService = $repository->getContentService();
2616
2617
        /* BEGIN: Use Case */
2618
        $draft = $this->createContentDraftVersion1();
2619
2620
        // This call will fail with a "BadStateException", because the Content
2621
        // version is the last version of the Content.
2622
        $contentService->deleteVersion($draft->getVersionInfo());
2623
        /* END: Use Case */
2624
    }
2625
2626
    /**
2627
     * Test for the loadVersions() method.
2628
     *
2629
     * @see \eZ\Publish\API\Repository\ContentService::loadVersions()
2630
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
2631
     */
2632
    public function testLoadVersions()
2633
    {
2634
        $repository = $this->getRepository();
2635
2636
        $contentService = $repository->getContentService();
2637
2638
        /* BEGIN: Use Case */
2639
        $contentVersion2 = $this->createContentVersion2();
2640
2641
        // Load versions of this ContentInfo instance
2642
        $versions = $contentService->loadVersions($contentVersion2->contentInfo);
2643
        /* END: Use Case */
2644
2645
        $expectedVersionsOrder = [
2646
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1),
2647
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 2),
2648
        ];
2649
2650
        $this->assertEquals($expectedVersionsOrder, $versions);
2651
    }
2652
2653
    /**
2654
     * Test for the copyContent() method.
2655
     *
2656
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
2657
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2658
     * @group field-type
2659
     */
2660 View Code Duplication
    public function testCopyContent()
2661
    {
2662
        $parentLocationId = $this->generateId('location', 56);
2663
2664
        $repository = $this->getRepository();
2665
2666
        $contentService = $repository->getContentService();
2667
        $locationService = $repository->getLocationService();
2668
2669
        /* BEGIN: Use Case */
2670
        $contentVersion2 = $this->createMultipleLanguageContentVersion2();
2671
2672
        // Configure new target location
2673
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
2674
2675
        $targetLocationCreate->priority = 42;
2676
        $targetLocationCreate->hidden = true;
2677
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
2678
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
2679
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
2680
2681
        // Copy content with all versions and drafts
2682
        $contentCopied = $contentService->copyContent(
2683
            $contentVersion2->contentInfo,
2684
            $targetLocationCreate
2685
        );
2686
        /* END: Use Case */
2687
2688
        $this->assertInstanceOf(
2689
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
2690
            $contentCopied
2691
        );
2692
2693
        $this->assertNotEquals(
2694
            $contentVersion2->contentInfo->remoteId,
2695
            $contentCopied->contentInfo->remoteId
2696
        );
2697
2698
        $this->assertNotEquals(
2699
            $contentVersion2->id,
2700
            $contentCopied->id
2701
        );
2702
2703
        $this->assertEquals(
2704
            2,
2705
            count($contentService->loadVersions($contentCopied->contentInfo))
2706
        );
2707
2708
        $this->assertEquals(2, $contentCopied->getVersionInfo()->versionNo);
2709
2710
        $this->assertAllFieldsEquals($contentCopied->getFields());
2711
2712
        $this->assertDefaultContentStates($contentCopied->contentInfo);
2713
2714
        $this->assertNotNull(
2715
            $contentCopied->contentInfo->mainLocationId,
2716
            'Expected main location to be set given we provided a LocationCreateStruct'
2717
        );
2718
    }
2719
2720
    /**
2721
     * Test for the copyContent() method.
2722
     *
2723
     * @see \eZ\Publish\API\Repository\ContentService::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo)
2724
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
2725
     *
2726
     * @todo Fix to more descriptive name
2727
     */
2728 View Code Duplication
    public function testCopyContentWithThirdParameter()
2729
    {
2730
        $parentLocationId = $this->generateId('location', 56);
2731
2732
        $repository = $this->getRepository();
2733
2734
        $contentService = $repository->getContentService();
2735
        $locationService = $repository->getLocationService();
2736
2737
        /* BEGIN: Use Case */
2738
        $contentVersion2 = $this->createContentVersion2();
2739
2740
        // Configure new target location
2741
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
2742
2743
        $targetLocationCreate->priority = 42;
2744
        $targetLocationCreate->hidden = true;
2745
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
2746
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
2747
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
2748
2749
        // Copy only the initial version
2750
        $contentCopied = $contentService->copyContent(
2751
            $contentVersion2->contentInfo,
2752
            $targetLocationCreate,
2753
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1)
2754
        );
2755
        /* END: Use Case */
2756
2757
        $this->assertInstanceOf(
2758
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
2759
            $contentCopied
2760
        );
2761
2762
        $this->assertNotEquals(
2763
            $contentVersion2->contentInfo->remoteId,
2764
            $contentCopied->contentInfo->remoteId
2765
        );
2766
2767
        $this->assertNotEquals(
2768
            $contentVersion2->id,
2769
            $contentCopied->id
2770
        );
2771
2772
        $this->assertEquals(
2773
            1,
2774
            count($contentService->loadVersions($contentCopied->contentInfo))
2775
        );
2776
2777
        $this->assertEquals(1, $contentCopied->getVersionInfo()->versionNo);
2778
2779
        $this->assertNotNull(
2780
            $contentCopied->contentInfo->mainLocationId,
2781
            'Expected main location to be set given we provided a LocationCreateStruct'
2782
        );
2783
    }
2784
2785
    /**
2786
     * Test for the addRelation() method.
2787
     *
2788
     * @return \eZ\Publish\API\Repository\Values\Content\Content
2789
     *
2790
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
2791
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2792
     */
2793
    public function testAddRelation()
2794
    {
2795
        $repository = $this->getRepository();
2796
2797
        $contentService = $repository->getContentService();
2798
2799
        /* BEGIN: Use Case */
2800
        // RemoteId of the "Media" content of an eZ Publish demo installation
2801
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2802
2803
        $draft = $this->createContentDraftVersion1();
2804
2805
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2806
2807
        // Create relation between new content object and "Media" page
2808
        $relation = $contentService->addRelation(
2809
            $draft->getVersionInfo(),
2810
            $media
2811
        );
2812
        /* END: Use Case */
2813
2814
        $this->assertInstanceOf(
2815
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Relation',
2816
            $relation
2817
        );
2818
2819
        return $contentService->loadRelations($draft->getVersionInfo());
2820
    }
2821
2822
    /**
2823
     * Test for the addRelation() method.
2824
     *
2825
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
2826
     *
2827
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
2828
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
2829
     */
2830
    public function testAddRelationAddsRelationToContent($relations)
2831
    {
2832
        $this->assertEquals(
2833
            1,
2834
            count($relations)
2835
        );
2836
    }
2837
2838
    /**
2839
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
2840
     */
2841
    protected function assertExpectedRelations($relations)
2842
    {
2843
        $this->assertEquals(
2844
            array(
2845
                'type' => Relation::COMMON,
2846
                'sourceFieldDefinitionIdentifier' => null,
2847
                'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
2848
                'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
2849
            ),
2850
            array(
2851
                'type' => $relations[0]->type,
2852
                'sourceFieldDefinitionIdentifier' => $relations[0]->sourceFieldDefinitionIdentifier,
2853
                'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
2854
                'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
2855
            )
2856
        );
2857
    }
2858
2859
    /**
2860
     * Test for the addRelation() method.
2861
     *
2862
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
2863
     *
2864
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
2865
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
2866
     */
2867
    public function testAddRelationSetsExpectedRelations($relations)
2868
    {
2869
        $this->assertExpectedRelations($relations);
2870
    }
2871
2872
    /**
2873
     * Test for the createContentDraft() method.
2874
     *
2875
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
2876
     *
2877
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
2878
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelationSetsExpectedRelations
2879
     */
2880 View Code Duplication
    public function testCreateContentDraftWithRelations()
2881
    {
2882
        $repository = $this->getRepository();
2883
2884
        $contentService = $repository->getContentService();
2885
2886
        // RemoteId of the "Media" content of an eZ Publish demo installation
2887
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2888
        $draft = $this->createContentDraftVersion1();
2889
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2890
2891
        // Create relation between new content object and "Media" page
2892
        $contentService->addRelation(
2893
            $draft->getVersionInfo(),
2894
            $media
2895
        );
2896
2897
        $content = $contentService->publishVersion($draft->versionInfo);
2898
        $newDraft = $contentService->createContentDraft($content->contentInfo);
2899
2900
        return $contentService->loadRelations($newDraft->getVersionInfo());
2901
    }
2902
2903
    /**
2904
     * Test for the createContentDraft() method.
2905
     *
2906
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
2907
     *
2908
     * @return \eZ\Publish\API\Repository\Values\Content\Relation[]
2909
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelations
2910
     */
2911
    public function testCreateContentDraftWithRelationsCreatesRelations($relations)
2912
    {
2913
        $this->assertEquals(
2914
            1,
2915
            count($relations)
2916
        );
2917
2918
        return $relations;
2919
    }
2920
2921
    /**
2922
     * Test for the createContentDraft() method.
2923
     *
2924
     * @param \eZ\Publish\API\Repository\Values\Content\Relation[] $relations
2925
     *
2926
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithRelationsCreatesRelations
2927
     */
2928
    public function testCreateContentDraftWithRelationsCreatesExpectedRelations($relations)
2929
    {
2930
        $this->assertExpectedRelations($relations);
2931
    }
2932
2933
    /**
2934
     * Test for the addRelation() method.
2935
     *
2936
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
2937
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
2938
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
2939
     */
2940 View Code Duplication
    public function testAddRelationThrowsBadStateException()
2941
    {
2942
        $repository = $this->getRepository();
2943
2944
        $contentService = $repository->getContentService();
2945
2946
        /* BEGIN: Use Case */
2947
        // RemoteId of the "Media" page of an eZ Publish demo installation
2948
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2949
2950
        $content = $this->createContentVersion1();
2951
2952
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2953
2954
        // This call will fail with a "BadStateException", because content is
2955
        // published and not a draft.
2956
        $contentService->addRelation(
2957
            $content->getVersionInfo(),
2958
            $media
2959
        );
2960
        /* END: Use Case */
2961
    }
2962
2963
    /**
2964
     * Test for the loadRelations() method.
2965
     *
2966
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
2967
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
2968
     */
2969
    public function testLoadRelations()
2970
    {
2971
        $repository = $this->getRepository();
2972
2973
        $contentService = $repository->getContentService();
2974
2975
        /* BEGIN: Use Case */
2976
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
2977
        // of a eZ Publish demo installation.
2978
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
2979
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
2980
2981
        $draft = $this->createContentDraftVersion1();
2982
2983
        // Load other content objects
2984
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
2985
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
2986
2987
        // Create relation between new content object and "Media" page
2988
        $contentService->addRelation(
2989
            $draft->getVersionInfo(),
2990
            $media
2991
        );
2992
2993
        // Create another relation with the "Demo Design" page
2994
        $contentService->addRelation(
2995
            $draft->getVersionInfo(),
2996
            $demoDesign
2997
        );
2998
2999
        // Load all relations
3000
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3001
        /* END: Use Case */
3002
3003
        usort(
3004
            $relations,
3005
            function ($rel1, $rel2) {
3006
                return strcasecmp(
3007
                    $rel2->getDestinationContentInfo()->remoteId,
3008
                    $rel1->getDestinationContentInfo()->remoteId
3009
                );
3010
            }
3011
        );
3012
3013
        $this->assertEquals(
3014
            array(
3015
                array(
3016
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3017
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3018
                ),
3019
                array(
3020
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3021
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3022
                ),
3023
            ),
3024
            array(
3025
                array(
3026
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3027
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3028
                ),
3029
                array(
3030
                    'sourceContentInfo' => $relations[1]->sourceContentInfo->remoteId,
3031
                    'destinationContentInfo' => $relations[1]->destinationContentInfo->remoteId,
3032
                ),
3033
            )
3034
        );
3035
    }
3036
3037
    /**
3038
     * Test for the loadRelations() method.
3039
     *
3040
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3041
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3042
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3043
     */
3044
    public function testLoadRelationsSkipsArchivedContent()
3045
    {
3046
        $repository = $this->getRepository();
3047
3048
        $contentService = $repository->getContentService();
3049
3050
        /* BEGIN: Use Case */
3051
        $trashService = $repository->getTrashService();
3052
        $locationService = $repository->getLocationService();
3053
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3054
        // of a eZ Publish demo installation.
3055
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3056
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3057
3058
        $draft = $this->createContentDraftVersion1();
3059
3060
        // Load other content objects
3061
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3062
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3063
3064
        // Create relation between new content object and "Media" page
3065
        $contentService->addRelation(
3066
            $draft->getVersionInfo(),
3067
            $media
3068
        );
3069
3070
        // Create another relation with the "Demo Design" page
3071
        $contentService->addRelation(
3072
            $draft->getVersionInfo(),
3073
            $demoDesign
3074
        );
3075
3076
        $demoDesignLocation = $locationService->loadLocation($demoDesign->mainLocationId);
3077
3078
        // Trashing Content's last Location will change its status to archived,
3079
        // in this case relation towards it will not be loaded.
3080
        $trashService->trash($demoDesignLocation);
3081
3082
        // Load all relations
3083
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3084
        /* END: Use Case */
3085
3086
        $this->assertCount(1, $relations);
3087
        $this->assertEquals(
3088
            array(
3089
                array(
3090
                    'sourceContentInfo' => 'abcdef0123456789abcdef0123456789',
3091
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3092
                ),
3093
            ),
3094
            array(
3095
                array(
3096
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3097
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3098
                ),
3099
            )
3100
        );
3101
    }
3102
3103
    /**
3104
     * Test for the loadRelations() method.
3105
     *
3106
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
3107
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3108
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3109
     */
3110
    public function testLoadRelationsSkipsDraftContent()
3111
    {
3112
        $repository = $this->getRepository();
3113
3114
        $contentService = $repository->getContentService();
3115
3116
        /* BEGIN: Use Case */
3117
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3118
        // of a eZ Publish demo installation.
3119
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3120
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3121
3122
        $draft = $this->createContentDraftVersion1();
3123
3124
        // Load other content objects
3125
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3126
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3127
3128
        // Create draft of "Media" page
3129
        $mediaDraft = $contentService->createContentDraft($media->contentInfo);
3130
3131
        // Create relation between "Media" page and new content object draft.
3132
        // This relation will not be loaded before the draft is published.
3133
        $contentService->addRelation(
3134
            $mediaDraft->getVersionInfo(),
3135
            $draft->getVersionInfo()->getContentInfo()
3136
        );
3137
3138
        // Create another relation with the "Demo Design" page
3139
        $contentService->addRelation(
3140
            $mediaDraft->getVersionInfo(),
3141
            $demoDesign
3142
        );
3143
3144
        // Load all relations
3145
        $relations = $contentService->loadRelations($mediaDraft->getVersionInfo());
3146
        /* END: Use Case */
3147
3148
        $this->assertCount(1, $relations);
3149
        $this->assertEquals(
3150
            array(
3151
                array(
3152
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3153
                    'destinationContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3154
                ),
3155
            ),
3156
            array(
3157
                array(
3158
                    'sourceContentInfo' => $relations[0]->sourceContentInfo->remoteId,
3159
                    'destinationContentInfo' => $relations[0]->destinationContentInfo->remoteId,
3160
                ),
3161
            )
3162
        );
3163
    }
3164
3165
    /**
3166
     * Test for the loadReverseRelations() method.
3167
     *
3168
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3169
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3170
     */
3171
    public function testLoadReverseRelations()
3172
    {
3173
        $repository = $this->getRepository();
3174
3175
        $contentService = $repository->getContentService();
3176
3177
        /* BEGIN: Use Case */
3178
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3179
        // of a eZ Publish demo installation.
3180
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3181
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3182
3183
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3184
        $contentInfo = $versionInfo->getContentInfo();
3185
3186
        // Create some drafts
3187
        $mediaDraft = $contentService->createContentDraft(
3188
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3189
        );
3190
        $demoDesignDraft = $contentService->createContentDraft(
3191
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3192
        );
3193
3194
        // Create relation between new content object and "Media" page
3195
        $relation1 = $contentService->addRelation(
3196
            $mediaDraft->getVersionInfo(),
3197
            $contentInfo
3198
        );
3199
3200
        // Create another relation with the "Demo Design" page
3201
        $relation2 = $contentService->addRelation(
3202
            $demoDesignDraft->getVersionInfo(),
3203
            $contentInfo
3204
        );
3205
3206
        // Publish drafts, so relations become active
3207
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3208
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3209
3210
        // Load all relations
3211
        $relations = $contentService->loadRelations($versionInfo);
3212
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3213
        /* END: Use Case */
3214
3215
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3216
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3217
3218
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3219
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3220
3221
        $this->assertEquals(0, count($relations));
3222
        $this->assertEquals(2, count($reverseRelations));
3223
3224
        usort(
3225
            $reverseRelations,
3226
            function ($rel1, $rel2) {
3227
                return strcasecmp(
3228
                    $rel2->getSourceContentInfo()->remoteId,
3229
                    $rel1->getSourceContentInfo()->remoteId
3230
                );
3231
            }
3232
        );
3233
3234
        $this->assertEquals(
3235
            array(
3236
                array(
3237
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3238
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3239
                ),
3240
                array(
3241
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3242
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3243
                ),
3244
            ),
3245
            array(
3246
                array(
3247
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3248
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3249
                ),
3250
                array(
3251
                    'sourceContentInfo' => $reverseRelations[1]->sourceContentInfo->remoteId,
3252
                    'destinationContentInfo' => $reverseRelations[1]->destinationContentInfo->remoteId,
3253
                ),
3254
            )
3255
        );
3256
    }
3257
3258
    /**
3259
     * Test for the loadReverseRelations() method.
3260
     *
3261
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3262
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3263
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3264
     */
3265
    public function testLoadReverseRelationsSkipsArchivedContent()
3266
    {
3267
        $repository = $this->getRepository();
3268
3269
        $contentService = $repository->getContentService();
3270
3271
        /* BEGIN: Use Case */
3272
        $trashService = $repository->getTrashService();
3273
        $locationService = $repository->getLocationService();
3274
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3275
        // of a eZ Publish demo installation.
3276
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3277
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3278
3279
        $versionInfo = $this->createContentVersion1()->getVersionInfo();
3280
        $contentInfo = $versionInfo->getContentInfo();
3281
3282
        // Create some drafts
3283
        $mediaDraft = $contentService->createContentDraft(
3284
            $contentService->loadContentInfoByRemoteId($mediaRemoteId)
3285
        );
3286
        $demoDesignDraft = $contentService->createContentDraft(
3287
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3288
        );
3289
3290
        // Create relation between new content object and "Media" page
3291
        $relation1 = $contentService->addRelation(
3292
            $mediaDraft->getVersionInfo(),
3293
            $contentInfo
3294
        );
3295
3296
        // Create another relation with the "Demo Design" page
3297
        $relation2 = $contentService->addRelation(
3298
            $demoDesignDraft->getVersionInfo(),
3299
            $contentInfo
3300
        );
3301
3302
        // Publish drafts, so relations become active
3303
        $contentService->publishVersion($mediaDraft->getVersionInfo());
3304
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3305
3306
        $demoDesignLocation = $locationService->loadLocation($demoDesignDraft->contentInfo->mainLocationId);
3307
3308
        // Trashing Content's last Location will change its status to archived,
3309
        // in this case relation from it will not be loaded.
3310
        $trashService->trash($demoDesignLocation);
3311
3312
        // Load all relations
3313
        $relations = $contentService->loadRelations($versionInfo);
3314
        $reverseRelations = $contentService->loadReverseRelations($contentInfo);
3315
        /* END: Use Case */
3316
3317
        $this->assertEquals($contentInfo->id, $relation1->getDestinationContentInfo()->id);
3318
        $this->assertEquals($mediaDraft->id, $relation1->getSourceContentInfo()->id);
3319
3320
        $this->assertEquals($contentInfo->id, $relation2->getDestinationContentInfo()->id);
3321
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3322
3323
        $this->assertEquals(0, count($relations));
3324
        $this->assertEquals(1, count($reverseRelations));
3325
3326
        $this->assertEquals(
3327
            array(
3328
                array(
3329
                    'sourceContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3330
                    'destinationContentInfo' => 'abcdef0123456789abcdef0123456789',
3331
                ),
3332
            ),
3333
            array(
3334
                array(
3335
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3336
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3337
                ),
3338
            )
3339
        );
3340
    }
3341
3342
    /**
3343
     * Test for the loadReverseRelations() method.
3344
     *
3345
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
3346
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
3347
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
3348
     */
3349
    public function testLoadReverseRelationsSkipsDraftContent()
3350
    {
3351
        $repository = $this->getRepository();
3352
3353
        $contentService = $repository->getContentService();
3354
3355
        /* BEGIN: Use Case */
3356
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
3357
        // of a eZ Publish demo installation.
3358
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3359
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3360
3361
        // Load "Media" page Content
3362
        $media = $contentService->loadContentByRemoteId($mediaRemoteId);
3363
3364
        // Create some drafts
3365
        $newDraftVersionInfo = $this->createContentDraftVersion1()->getVersionInfo();
3366
        $demoDesignDraft = $contentService->createContentDraft(
3367
            $contentService->loadContentInfoByRemoteId($demoDesignRemoteId)
3368
        );
3369
3370
        // Create relation between "Media" page and new content object
3371
        $relation1 = $contentService->addRelation(
3372
            $newDraftVersionInfo,
3373
            $media->contentInfo
3374
        );
3375
3376
        // Create another relation with the "Demo Design" page
3377
        $relation2 = $contentService->addRelation(
3378
            $demoDesignDraft->getVersionInfo(),
3379
            $media->contentInfo
3380
        );
3381
3382
        // Publish drafts, so relations become active
3383
        $contentService->publishVersion($demoDesignDraft->getVersionInfo());
3384
        // We will not publish new Content draft, therefore relation from it
3385
        // will not be loaded as reverse relation for "Media" page
3386
        //$contentService->publishVersion( $newDraftVersionInfo );
3387
3388
        // Load all relations
3389
        $relations = $contentService->loadRelations($media->versionInfo);
3390
        $reverseRelations = $contentService->loadReverseRelations($media->contentInfo);
3391
        /* END: Use Case */
3392
3393
        $this->assertEquals($media->contentInfo->id, $relation1->getDestinationContentInfo()->id);
3394
        $this->assertEquals($newDraftVersionInfo->contentInfo->id, $relation1->getSourceContentInfo()->id);
3395
3396
        $this->assertEquals($media->contentInfo->id, $relation2->getDestinationContentInfo()->id);
3397
        $this->assertEquals($demoDesignDraft->id, $relation2->getSourceContentInfo()->id);
3398
3399
        $this->assertEquals(0, count($relations));
3400
        $this->assertEquals(1, count($reverseRelations));
3401
3402
        $this->assertEquals(
3403
            array(
3404
                array(
3405
                    'sourceContentInfo' => '8b8b22fe3c6061ed500fbd2b377b885f',
3406
                    'destinationContentInfo' => 'a6e35cbcb7cd6ae4b691f3eee30cd262',
3407
                ),
3408
            ),
3409
            array(
3410
                array(
3411
                    'sourceContentInfo' => $reverseRelations[0]->sourceContentInfo->remoteId,
3412
                    'destinationContentInfo' => $reverseRelations[0]->destinationContentInfo->remoteId,
3413
                ),
3414
            )
3415
        );
3416
    }
3417
3418
    /**
3419
     * Test for the deleteRelation() method.
3420
     *
3421
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3422
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
3423
     */
3424
    public function testDeleteRelation()
3425
    {
3426
        $repository = $this->getRepository();
3427
3428
        $contentService = $repository->getContentService();
3429
3430
        /* BEGIN: Use Case */
3431
        // Remote ids of the "Media" and the "Demo Design" page of a eZ Publish
3432
        // demo installation.
3433
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3434
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
3435
3436
        $draft = $this->createContentDraftVersion1();
3437
3438
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3439
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
3440
3441
        // Establish some relations
3442
        $contentService->addRelation($draft->getVersionInfo(), $media);
3443
        $contentService->addRelation($draft->getVersionInfo(), $demoDesign);
3444
3445
        // Delete one of the currently created relations
3446
        $contentService->deleteRelation($draft->getVersionInfo(), $media);
3447
3448
        // The relations array now contains only one element
3449
        $relations = $contentService->loadRelations($draft->getVersionInfo());
3450
        /* END: Use Case */
3451
3452
        $this->assertEquals(1, count($relations));
3453
    }
3454
3455
    /**
3456
     * Test for the deleteRelation() method.
3457
     *
3458
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3459
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
3460
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3461
     */
3462 View Code Duplication
    public function testDeleteRelationThrowsBadStateException()
3463
    {
3464
        $repository = $this->getRepository();
3465
3466
        $contentService = $repository->getContentService();
3467
3468
        /* BEGIN: Use Case */
3469
        // RemoteId of the "Media" page of an eZ Publish demo installation
3470
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3471
3472
        $content = $this->createContentVersion1();
3473
3474
        // Load the destination object
3475
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3476
3477
        // Create a new draft
3478
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
3479
3480
        // Add a relation
3481
        $contentService->addRelation($draftVersion2->getVersionInfo(), $media);
3482
3483
        // Publish new version
3484
        $contentVersion2 = $contentService->publishVersion(
3485
            $draftVersion2->getVersionInfo()
3486
        );
3487
3488
        // This call will fail with a "BadStateException", because content is
3489
        // published and not a draft.
3490
        $contentService->deleteRelation(
3491
            $contentVersion2->getVersionInfo(),
3492
            $media
3493
        );
3494
        /* END: Use Case */
3495
    }
3496
3497
    /**
3498
     * Test for the deleteRelation() method.
3499
     *
3500
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
3501
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
3502
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
3503
     */
3504 View Code Duplication
    public function testDeleteRelationThrowsInvalidArgumentException()
3505
    {
3506
        $repository = $this->getRepository();
3507
3508
        $contentService = $repository->getContentService();
3509
3510
        /* BEGIN: Use Case */
3511
        // RemoteId of the "Media" page of an eZ Publish demo installation
3512
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
3513
3514
        $draft = $this->createContentDraftVersion1();
3515
3516
        // Load the destination object
3517
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
3518
3519
        // This call will fail with a "InvalidArgumentException", because no
3520
        // relation exists between $draft and $media.
3521
        $contentService->deleteRelation(
3522
            $draft->getVersionInfo(),
3523
            $media
3524
        );
3525
        /* END: Use Case */
3526
    }
3527
3528
    /**
3529
     * Test for the createContent() method.
3530
     *
3531
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
3532
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3533
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3534
     */
3535
    public function testCreateContentInTransactionWithRollback()
3536
    {
3537
        if ($this->isVersion4()) {
3538
            $this->markTestSkipped('This test requires eZ Publish 5');
3539
        }
3540
3541
        $repository = $this->getRepository();
3542
3543
        /* BEGIN: Use Case */
3544
        $contentTypeService = $repository->getContentTypeService();
3545
        $contentService = $repository->getContentService();
3546
3547
        // Start a transaction
3548
        $repository->beginTransaction();
3549
3550
        try {
3551
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
3552
3553
            // Get a content create struct and set mandatory properties
3554
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
3555
            $contentCreate->setField('name', 'Sindelfingen forum');
3556
3557
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
3558
            $contentCreate->alwaysAvailable = true;
3559
3560
            // Create a new content object
3561
            $contentId = $contentService->createContent($contentCreate)->id;
3562
        } catch (Exception $e) {
3563
            // Cleanup hanging transaction on error
3564
            $repository->rollback();
3565
            throw $e;
3566
        }
3567
3568
        // Rollback all changes
3569
        $repository->rollback();
3570
3571
        try {
3572
            // This call will fail with a "NotFoundException"
3573
            $contentService->loadContent($contentId);
3574
        } catch (NotFoundException $e) {
3575
            // This is expected
3576
            return;
3577
        }
3578
        /* END: Use Case */
3579
3580
        $this->fail('Content object still exists after rollback.');
3581
    }
3582
3583
    /**
3584
     * Test for the createContent() method.
3585
     *
3586
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
3587
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
3588
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3589
     */
3590
    public function testCreateContentInTransactionWithCommit()
3591
    {
3592
        if ($this->isVersion4()) {
3593
            $this->markTestSkipped('This test requires eZ Publish 5');
3594
        }
3595
3596
        $repository = $this->getRepository();
3597
3598
        /* BEGIN: Use Case */
3599
        $contentTypeService = $repository->getContentTypeService();
3600
        $contentService = $repository->getContentService();
3601
3602
        // Start a transaction
3603
        $repository->beginTransaction();
3604
3605
        try {
3606
            $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
3607
3608
            // Get a content create struct and set mandatory properties
3609
            $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
3610
            $contentCreate->setField('name', 'Sindelfingen forum');
3611
3612
            $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
3613
            $contentCreate->alwaysAvailable = true;
3614
3615
            // Create a new content object
3616
            $contentId = $contentService->createContent($contentCreate)->id;
3617
3618
            // Commit changes
3619
            $repository->commit();
3620
        } catch (Exception $e) {
3621
            // Cleanup hanging transaction on error
3622
            $repository->rollback();
3623
            throw $e;
3624
        }
3625
3626
        // Load the new content object
3627
        $content = $contentService->loadContent($contentId);
3628
        /* END: Use Case */
3629
3630
        $this->assertEquals($contentId, $content->id);
3631
    }
3632
3633
    /**
3634
     * Test for the createContent() method.
3635
     *
3636
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
3637
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
3638
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
3639
     */
3640
    public function testCreateContentWithLocationCreateParameterInTransactionWithRollback()
3641
    {
3642
        $repository = $this->getRepository();
3643
3644
        $contentService = $repository->getContentService();
3645
3646
        /* BEGIN: Use Case */
3647
        // Start a transaction
3648
        $repository->beginTransaction();
3649
3650
        try {
3651
            $draft = $this->createContentDraftVersion1();
3652
        } catch (Exception $e) {
3653
            // Cleanup hanging transaction on error
3654
            $repository->rollback();
3655
            throw $e;
3656
        }
3657
3658
        $contentId = $draft->id;
3659
3660
        // Roleback the transaction
3661
        $repository->rollback();
3662
3663
        try {
3664
            // This call will fail with a "NotFoundException"
3665
            $contentService->loadContent($contentId);
3666
        } catch (NotFoundException $e) {
3667
            return;
3668
        }
3669
        /* END: Use Case */
3670
3671
        $this->fail('Can still load content object after rollback.');
3672
    }
3673
3674
    /**
3675
     * Test for the createContent() method.
3676
     *
3677
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
3678
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
3679
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentThrowsNotFoundException
3680
     */
3681 View Code Duplication
    public function testCreateContentWithLocationCreateParameterInTransactionWithCommit()
3682
    {
3683
        $repository = $this->getRepository();
3684
3685
        $contentService = $repository->getContentService();
3686
3687
        /* BEGIN: Use Case */
3688
        // Start a transaction
3689
        $repository->beginTransaction();
3690
3691
        try {
3692
            $draft = $this->createContentDraftVersion1();
3693
3694
            $contentId = $draft->id;
3695
3696
            // Roleback the transaction
3697
            $repository->commit();
3698
        } catch (Exception $e) {
3699
            // Cleanup hanging transaction on error
3700
            $repository->rollback();
3701
            throw $e;
3702
        }
3703
3704
        // Load the new content object
3705
        $content = $contentService->loadContent($contentId);
3706
        /* END: Use Case */
3707
3708
        $this->assertEquals($contentId, $content->id);
3709
    }
3710
3711
    /**
3712
     * Test for the createContentDraft() method.
3713
     *
3714
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
3715
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
3716
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3717
     */
3718
    public function testCreateContentDraftInTransactionWithRollback()
3719
    {
3720
        $repository = $this->getRepository();
3721
3722
        $contentId = $this->generateId('object', 12);
3723
        /* BEGIN: Use Case */
3724
        // $contentId is the ID of the "Administrator users" user group
3725
3726
        // Get the content service
3727
        $contentService = $repository->getContentService();
3728
3729
        // Load the user group content object
3730
        $content = $contentService->loadContent($contentId);
3731
3732
        // Start a new transaction
3733
        $repository->beginTransaction();
3734
3735
        try {
3736
            // Create a new draft
3737
            $drafted = $contentService->createContentDraft($content->contentInfo);
3738
3739
            // Store version number for later reuse
3740
            $versionNo = $drafted->versionInfo->versionNo;
3741
        } catch (Exception $e) {
3742
            // Cleanup hanging transaction on error
3743
            $repository->rollback();
3744
            throw $e;
3745
        }
3746
3747
        // Rollback
3748
        $repository->rollback();
3749
3750
        try {
3751
            // This call will fail with a "NotFoundException"
3752
            $contentService->loadContent($contentId, null, $versionNo);
3753
        } catch (NotFoundException $e) {
3754
            return;
3755
        }
3756
        /* END: Use Case */
3757
3758
        $this->fail('Can still load content draft after rollback');
3759
    }
3760
3761
    /**
3762
     * Test for the createContentDraft() method.
3763
     *
3764
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
3765
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
3766
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3767
     */
3768 View Code Duplication
    public function testCreateContentDraftInTransactionWithCommit()
3769
    {
3770
        $repository = $this->getRepository();
3771
3772
        $contentId = $this->generateId('object', 12);
3773
        /* BEGIN: Use Case */
3774
        // $contentId is the ID of the "Administrator users" user group
3775
3776
        // Get the content service
3777
        $contentService = $repository->getContentService();
3778
3779
        // Load the user group content object
3780
        $content = $contentService->loadContent($contentId);
3781
3782
        // Start a new transaction
3783
        $repository->beginTransaction();
3784
3785
        try {
3786
            // Create a new draft
3787
            $drafted = $contentService->createContentDraft($content->contentInfo);
3788
3789
            // Store version number for later reuse
3790
            $versionNo = $drafted->versionInfo->versionNo;
3791
3792
            // Commit all changes
3793
            $repository->commit();
3794
        } catch (Exception $e) {
3795
            // Cleanup hanging transaction on error
3796
            $repository->rollback();
3797
            throw $e;
3798
        }
3799
3800
        $content = $contentService->loadContent($contentId, null, $versionNo);
3801
        /* END: Use Case */
3802
3803
        $this->assertEquals(
3804
            $versionNo,
3805
            $content->getVersionInfo()->versionNo
3806
        );
3807
    }
3808
3809
    /**
3810
     * Test for the publishVersion() method.
3811
     *
3812
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
3813
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3814
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3815
     */
3816 View Code Duplication
    public function testPublishVersionInTransactionWithRollback()
3817
    {
3818
        $repository = $this->getRepository();
3819
3820
        $contentId = $this->generateId('object', 12);
3821
        /* BEGIN: Use Case */
3822
        // $contentId is the ID of the "Administrator users" user group
3823
3824
        // Get the content service
3825
        $contentService = $repository->getContentService();
3826
3827
        // Load the user group content object
3828
        $content = $contentService->loadContent($contentId);
3829
3830
        // Start a new transaction
3831
        $repository->beginTransaction();
3832
3833
        try {
3834
            $draftVersion = $contentService->createContentDraft($content->contentInfo)->getVersionInfo();
3835
3836
            // Publish a new version
3837
            $content = $contentService->publishVersion($draftVersion);
3838
3839
            // Store version number for later reuse
3840
            $versionNo = $content->versionInfo->versionNo;
3841
        } catch (Exception $e) {
3842
            // Cleanup hanging transaction on error
3843
            $repository->rollback();
3844
            throw $e;
3845
        }
3846
3847
        // Rollback
3848
        $repository->rollback();
3849
3850
        try {
3851
            // This call will fail with a "NotFoundException"
3852
            $contentService->loadContent($contentId, null, $versionNo);
3853
        } catch (NotFoundException $e) {
3854
            return;
3855
        }
3856
        /* END: Use Case */
3857
3858
        $this->fail('Can still load content draft after rollback');
3859
    }
3860
3861
    /**
3862
     * Test for the publishVersion() method.
3863
     *
3864
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
3865
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
3866
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
3867
     */
3868 View Code Duplication
    public function testPublishVersionInTransactionWithCommit()
3869
    {
3870
        $repository = $this->getRepository();
3871
3872
        /* BEGIN: Use Case */
3873
        // ID of the "Administrator users" user group
3874
        $contentId = 12;
3875
3876
        // Get the content service
3877
        $contentService = $repository->getContentService();
3878
3879
        // Load the user group content object
3880
        $template = $contentService->loadContent($contentId);
3881
3882
        // Start a new transaction
3883
        $repository->beginTransaction();
3884
3885
        try {
3886
            // Publish a new version
3887
            $content = $contentService->publishVersion(
3888
                $contentService->createContentDraft($template->contentInfo)->getVersionInfo()
3889
            );
3890
3891
            // Store version number for later reuse
3892
            $versionNo = $content->versionInfo->versionNo;
3893
3894
            // Commit all changes
3895
            $repository->commit();
3896
        } catch (Exception $e) {
3897
            // Cleanup hanging transaction on error
3898
            $repository->rollback();
3899
            throw $e;
3900
        }
3901
3902
        // Load current version info
3903
        $versionInfo = $contentService->loadVersionInfo($content->contentInfo);
3904
        /* END: Use Case */
3905
3906
        $this->assertEquals($versionNo, $versionInfo->versionNo);
3907
    }
3908
3909
    /**
3910
     * Test for the updateContent() method.
3911
     *
3912
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
3913
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
3914
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3915
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
3916
     */
3917 View Code Duplication
    public function testUpdateContentInTransactionWithRollback()
3918
    {
3919
        $repository = $this->getRepository();
3920
3921
        $contentId = $this->generateId('object', 12);
3922
        /* BEGIN: Use Case */
3923
        // $contentId is the ID of the "Administrator users" user group
3924
3925
        // Load content service
3926
        $contentService = $repository->getContentService();
3927
3928
        // Create a new user group draft
3929
        $draft = $contentService->createContentDraft(
3930
            $contentService->loadContentInfo($contentId)
3931
        );
3932
3933
        // Get an update struct and change the group name
3934
        $contentUpdate = $contentService->newContentUpdateStruct();
3935
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
3936
3937
        // Start a transaction
3938
        $repository->beginTransaction();
3939
3940
        try {
3941
            // Update the group name
3942
            $draft = $contentService->updateContent(
3943
                $draft->getVersionInfo(),
3944
                $contentUpdate
3945
            );
3946
3947
            // Publish updated version
3948
            $contentService->publishVersion($draft->getVersionInfo());
3949
        } catch (Exception $e) {
3950
            // Cleanup hanging transaction on error
3951
            $repository->rollback();
3952
            throw $e;
3953
        }
3954
3955
        // Rollback all changes.
3956
        $repository->rollback();
3957
3958
        // Name will still be "Administrator users"
3959
        $name = $contentService->loadContent($contentId)->getFieldValue('name');
3960
        /* END: Use Case */
3961
3962
        $this->assertEquals('Administrator users', $name);
3963
    }
3964
3965
    /**
3966
     * Test for the updateContent() method.
3967
     *
3968
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
3969
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
3970
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
3971
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
3972
     */
3973 View Code Duplication
    public function testUpdateContentInTransactionWithCommit()
3974
    {
3975
        $repository = $this->getRepository();
3976
3977
        $contentId = $this->generateId('object', 12);
3978
        /* BEGIN: Use Case */
3979
        // $contentId is the ID of the "Administrator users" user group
3980
3981
        // Load content service
3982
        $contentService = $repository->getContentService();
3983
3984
        // Create a new user group draft
3985
        $draft = $contentService->createContentDraft(
3986
            $contentService->loadContentInfo($contentId)
3987
        );
3988
3989
        // Get an update struct and change the group name
3990
        $contentUpdate = $contentService->newContentUpdateStruct();
3991
        $contentUpdate->setField('name', 'Administrators', 'eng-US');
3992
3993
        // Start a transaction
3994
        $repository->beginTransaction();
3995
3996
        try {
3997
            // Update the group name
3998
            $draft = $contentService->updateContent(
3999
                $draft->getVersionInfo(),
4000
                $contentUpdate
4001
            );
4002
4003
            // Publish updated version
4004
            $contentService->publishVersion($draft->getVersionInfo());
4005
4006
            // Commit all changes.
4007
            $repository->commit();
4008
        } catch (Exception $e) {
4009
            // Cleanup hanging transaction on error
4010
            $repository->rollback();
4011
            throw $e;
4012
        }
4013
4014
        // Name is now "Administrators"
4015
        $name = $contentService->loadContent($contentId)->getFieldValue('name', 'eng-US');
4016
        /* END: Use Case */
4017
4018
        $this->assertEquals('Administrators', $name);
4019
    }
4020
4021
    /**
4022
     * Test for the updateContentMetadata() method.
4023
     *
4024
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4025
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4026
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4027
     */
4028 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithRollback()
4029
    {
4030
        $repository = $this->getRepository();
4031
4032
        $contentId = $this->generateId('object', 12);
4033
        /* BEGIN: Use Case */
4034
        // $contentId is the ID of the "Administrator users" user group
4035
4036
        // Get the content service
4037
        $contentService = $repository->getContentService();
4038
4039
        // Load a ContentInfo object
4040
        $contentInfo = $contentService->loadContentInfo($contentId);
4041
4042
        // Store remoteId for later testing
4043
        $remoteId = $contentInfo->remoteId;
4044
4045
        // Start a transaction
4046
        $repository->beginTransaction();
4047
4048
        try {
4049
            // Get metadata update struct and change remoteId
4050
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4051
            $metadataUpdate->remoteId = md5(microtime(true));
4052
4053
            // Update the metadata of the published content object
4054
            $contentService->updateContentMetadata(
4055
                $contentInfo,
4056
                $metadataUpdate
4057
            );
4058
        } catch (Exception $e) {
4059
            // Cleanup hanging transaction on error
4060
            $repository->rollback();
4061
            throw $e;
4062
        }
4063
4064
        // Rollback all changes.
4065
        $repository->rollback();
4066
4067
        // Load current remoteId
4068
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4069
        /* END: Use Case */
4070
4071
        $this->assertEquals($remoteId, $remoteIdReloaded);
4072
    }
4073
4074
    /**
4075
     * Test for the updateContentMetadata() method.
4076
     *
4077
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
4078
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
4079
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4080
     */
4081 View Code Duplication
    public function testUpdateContentMetadataInTransactionWithCommit()
4082
    {
4083
        $repository = $this->getRepository();
4084
4085
        $contentId = $this->generateId('object', 12);
4086
        /* BEGIN: Use Case */
4087
        // $contentId is the ID of the "Administrator users" user group
4088
4089
        // Get the content service
4090
        $contentService = $repository->getContentService();
4091
4092
        // Load a ContentInfo object
4093
        $contentInfo = $contentService->loadContentInfo($contentId);
4094
4095
        // Store remoteId for later testing
4096
        $remoteId = $contentInfo->remoteId;
4097
4098
        // Start a transaction
4099
        $repository->beginTransaction();
4100
4101
        try {
4102
            // Get metadata update struct and change remoteId
4103
            $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
4104
            $metadataUpdate->remoteId = md5(microtime(true));
4105
4106
            // Update the metadata of the published content object
4107
            $contentService->updateContentMetadata(
4108
                $contentInfo,
4109
                $metadataUpdate
4110
            );
4111
4112
            // Commit all changes.
4113
            $repository->commit();
4114
        } catch (Exception $e) {
4115
            // Cleanup hanging transaction on error
4116
            $repository->rollback();
4117
            throw $e;
4118
        }
4119
4120
        // Load current remoteId
4121
        $remoteIdReloaded = $contentService->loadContentInfo($contentId)->remoteId;
4122
        /* END: Use Case */
4123
4124
        $this->assertNotEquals($remoteId, $remoteIdReloaded);
4125
    }
4126
4127
    /**
4128
     * Test for the deleteVersion() method.
4129
     *
4130
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4131
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4132
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4133
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4134
     */
4135
    public function testDeleteVersionInTransactionWithRollback()
4136
    {
4137
        $repository = $this->getRepository();
4138
4139
        $contentId = $this->generateId('object', 12);
4140
        /* BEGIN: Use Case */
4141
        // $contentId is the ID of the "Administrator users" user group
4142
4143
        // Get the content service
4144
        $contentService = $repository->getContentService();
4145
4146
        // Start a new transaction
4147
        $repository->beginTransaction();
4148
4149
        try {
4150
            // Create a new draft
4151
            $draft = $contentService->createContentDraft(
4152
                $contentService->loadContentInfo($contentId)
4153
            );
4154
4155
            $contentService->deleteVersion($draft->getVersionInfo());
4156
        } catch (Exception $e) {
4157
            // Cleanup hanging transaction on error
4158
            $repository->rollback();
4159
            throw $e;
4160
        }
4161
4162
        // Rollback all changes.
4163
        $repository->rollback();
4164
4165
        // This array will be empty
4166
        $drafts = $contentService->loadContentDrafts();
4167
        /* END: Use Case */
4168
4169
        $this->assertSame(array(), $drafts);
4170
    }
4171
4172
    /**
4173
     * Test for the deleteVersion() method.
4174
     *
4175
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
4176
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
4177
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4178
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
4179
     */
4180
    public function testDeleteVersionInTransactionWithCommit()
4181
    {
4182
        $repository = $this->getRepository();
4183
4184
        $contentId = $this->generateId('object', 12);
4185
        /* BEGIN: Use Case */
4186
        // $contentId is the ID of the "Administrator users" user group
4187
4188
        // Get the content service
4189
        $contentService = $repository->getContentService();
4190
4191
        // Start a new transaction
4192
        $repository->beginTransaction();
4193
4194
        try {
4195
            // Create a new draft
4196
            $draft = $contentService->createContentDraft(
4197
                $contentService->loadContentInfo($contentId)
4198
            );
4199
4200
            $contentService->deleteVersion($draft->getVersionInfo());
4201
4202
            // Commit all changes.
4203
            $repository->commit();
4204
        } catch (Exception $e) {
4205
            // Cleanup hanging transaction on error
4206
            $repository->rollback();
4207
            throw $e;
4208
        }
4209
4210
        // This array will contain no element
4211
        $drafts = $contentService->loadContentDrafts();
4212
        /* END: Use Case */
4213
4214
        $this->assertSame(array(), $drafts);
4215
    }
4216
4217
    /**
4218
     * Test for the deleteContent() method.
4219
     *
4220
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4221
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4222
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4223
     */
4224
    public function testDeleteContentInTransactionWithRollback()
4225
    {
4226
        $repository = $this->getRepository();
4227
4228
        $contentId = $this->generateId('object', 11);
4229
        /* BEGIN: Use Case */
4230
        // $contentId is the ID of the "Members" user group in an eZ Publish
4231
        // demo installation
4232
4233
        // Get content service
4234
        $contentService = $repository->getContentService();
4235
4236
        // Load a ContentInfo instance
4237
        $contentInfo = $contentService->loadContentInfo($contentId);
4238
4239
        // Start a new transaction
4240
        $repository->beginTransaction();
4241
4242
        try {
4243
            // Delete content object
4244
            $contentService->deleteContent($contentInfo);
4245
        } catch (Exception $e) {
4246
            // Cleanup hanging transaction on error
4247
            $repository->rollback();
4248
            throw $e;
4249
        }
4250
4251
        // Rollback all changes
4252
        $repository->rollback();
4253
4254
        // This call will return the original content object
4255
        $contentInfo = $contentService->loadContentInfo($contentId);
4256
        /* END: Use Case */
4257
4258
        $this->assertEquals($contentId, $contentInfo->id);
4259
    }
4260
4261
    /**
4262
     * Test for the deleteContent() method.
4263
     *
4264
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
4265
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
4266
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
4267
     */
4268
    public function testDeleteContentInTransactionWithCommit()
4269
    {
4270
        $repository = $this->getRepository();
4271
4272
        $contentId = $this->generateId('object', 11);
4273
        /* BEGIN: Use Case */
4274
        // $contentId is the ID of the "Members" user group in an eZ Publish
4275
        // demo installation
4276
4277
        // Get content service
4278
        $contentService = $repository->getContentService();
4279
4280
        // Load a ContentInfo instance
4281
        $contentInfo = $contentService->loadContentInfo($contentId);
4282
4283
        // Start a new transaction
4284
        $repository->beginTransaction();
4285
4286
        try {
4287
            // Delete content object
4288
            $contentService->deleteContent($contentInfo);
4289
4290
            // Commit all changes
4291
            $repository->commit();
4292
        } catch (Exception $e) {
4293
            // Cleanup hanging transaction on error
4294
            $repository->rollback();
4295
            throw $e;
4296
        }
4297
4298
        // Deleted content info is not found anymore
4299
        try {
4300
            $contentService->loadContentInfo($contentId);
4301
        } catch (NotFoundException $e) {
4302
            return;
4303
        }
4304
        /* END: Use Case */
4305
4306
        $this->fail('Can still load ContentInfo after commit.');
4307
    }
4308
4309
    /**
4310
     * Test for the copyContent() method.
4311
     *
4312
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4313
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4314
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4315
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4316
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4317
     */
4318 View Code Duplication
    public function testCopyContentInTransactionWithRollback()
4319
    {
4320
        $repository = $this->getRepository();
4321
4322
        $contentId = $this->generateId('object', 11);
4323
        $locationId = $this->generateId('location', 13);
4324
        /* BEGIN: Use Case */
4325
        // $contentId is the ID of the "Members" user group in an eZ Publish
4326
        // demo installation
4327
4328
        // $locationId is the ID of the "Administrator users" group location
4329
4330
        // Get services
4331
        $contentService = $repository->getContentService();
4332
        $locationService = $repository->getLocationService();
4333
4334
        // Load content object to copy
4335
        $content = $contentService->loadContent($contentId);
4336
4337
        // Create new target location
4338
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4339
4340
        // Start a new transaction
4341
        $repository->beginTransaction();
4342
4343
        try {
4344
            // Copy content with all versions and drafts
4345
            $contentService->copyContent(
4346
                $content->contentInfo,
4347
                $locationCreate
4348
            );
4349
        } catch (Exception $e) {
4350
            // Cleanup hanging transaction on error
4351
            $repository->rollback();
4352
            throw $e;
4353
        }
4354
4355
        // Rollback all changes
4356
        $repository->rollback();
4357
4358
        // This array will only contain a single admin user object
4359
        $locations = $locationService->loadLocationChildren(
4360
            $locationService->loadLocation($locationId)
4361
        )->locations;
4362
        /* END: Use Case */
4363
4364
        $this->assertEquals(1, count($locations));
4365
    }
4366
4367
    /**
4368
     * Test for the copyContent() method.
4369
     *
4370
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
4371
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
4372
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
4373
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationChildren
4374
     * @depend(s) eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
4375
     */
4376 View Code Duplication
    public function testCopyContentInTransactionWithCommit()
4377
    {
4378
        $repository = $this->getRepository();
4379
4380
        $contentId = $this->generateId('object', 11);
4381
        $locationId = $this->generateId('location', 13);
4382
        /* BEGIN: Use Case */
4383
        // $contentId is the ID of the "Members" user group in an eZ Publish
4384
        // demo installation
4385
4386
        // $locationId is the ID of the "Administrator users" group location
4387
4388
        // Get services
4389
        $contentService = $repository->getContentService();
4390
        $locationService = $repository->getLocationService();
4391
4392
        // Load content object to copy
4393
        $content = $contentService->loadContent($contentId);
4394
4395
        // Create new target location
4396
        $locationCreate = $locationService->newLocationCreateStruct($locationId);
4397
4398
        // Start a new transaction
4399
        $repository->beginTransaction();
4400
4401
        try {
4402
            // Copy content with all versions and drafts
4403
            $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...
4404
                $content->contentInfo,
4405
                $locationCreate
4406
            );
4407
4408
            // Commit all changes
4409
            $repository->commit();
4410
        } catch (Exception $e) {
4411
            // Cleanup hanging transaction on error
4412
            $repository->rollback();
4413
            throw $e;
4414
        }
4415
4416
        $this->refreshSearch($repository);
4417
4418
        // This will contain the admin user and the new child location
4419
        $locations = $locationService->loadLocationChildren(
4420
            $locationService->loadLocation($locationId)
4421
        )->locations;
4422
        /* END: Use Case */
4423
4424
        $this->assertEquals(2, count($locations));
4425
    }
4426
4427
    /**
4428
     */
4429
    public function testURLAliasesCreatedForNewContent()
4430
    {
4431
        $repository = $this->getRepository();
4432
4433
        $contentService = $repository->getContentService();
4434
        $locationService = $repository->getLocationService();
4435
        $urlAliasService = $repository->getURLAliasService();
4436
4437
        /* BEGIN: Use Case */
4438
        $draft = $this->createContentDraftVersion1();
4439
4440
        // Automatically creates a new URLAlias for the content
4441
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
4442
        /* END: Use Case */
4443
4444
        $location = $locationService->loadLocation(
4445
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4446
        );
4447
4448
        $aliases = $urlAliasService->listLocationAliases($location, false);
4449
4450
        $this->assertAliasesCorrect(
4451
            array(
4452
                '/Design/Plain-site/An-awesome-forum' => array(
4453
                    'type' => URLAlias::LOCATION,
4454
                    'destination' => $location->id,
4455
                    'path' => '/Design/Plain-site/An-awesome-forum',
4456
                    'languageCodes' => array('eng-US'),
4457
                    'isHistory' => false,
4458
                    'isCustom' => false,
4459
                    'forward' => false,
4460
                ),
4461
            ),
4462
            $aliases
4463
        );
4464
    }
4465
4466
    /**
4467
     */
4468
    public function testURLAliasesCreatedForUpdatedContent()
4469
    {
4470
        $repository = $this->getRepository();
4471
4472
        $contentService = $repository->getContentService();
4473
        $locationService = $repository->getLocationService();
4474
        $urlAliasService = $repository->getURLAliasService();
4475
4476
        /* BEGIN: Use Case */
4477
        $draft = $this->createUpdatedDraftVersion2();
4478
4479
        $location = $locationService->loadLocation(
4480
            $draft->getVersionInfo()->getContentInfo()->mainLocationId
4481
        );
4482
4483
        // Load and assert URL aliases before publishing updated Content, so that
4484
        // SPI cache is warmed up and cache invalidation is also tested.
4485
        $aliases = $urlAliasService->listLocationAliases($location, false);
4486
4487
        $this->assertAliasesCorrect(
4488
            array(
4489
                '/Design/Plain-site/An-awesome-forum' => array(
4490
                    'type' => URLAlias::LOCATION,
4491
                    'destination' => $location->id,
4492
                    'path' => '/Design/Plain-site/An-awesome-forum',
4493
                    'languageCodes' => array('eng-US'),
4494
                    'alwaysAvailable' => true,
4495
                    'isHistory' => false,
4496
                    'isCustom' => false,
4497
                    'forward' => false,
4498
                ),
4499
            ),
4500
            $aliases
4501
        );
4502
4503
        // Automatically marks old aliases for the content as history
4504
        // and creates new aliases, based on the changes
4505
        $liveContent = $contentService->publishVersion($draft->getVersionInfo());
4506
        /* END: Use Case */
4507
4508
        $location = $locationService->loadLocation(
4509
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4510
        );
4511
4512
        $aliases = $urlAliasService->listLocationAliases($location, false);
4513
4514
        $this->assertAliasesCorrect(
4515
            array(
4516
                '/Design/Plain-site/An-awesome-forum2' => array(
4517
                    'type' => URLAlias::LOCATION,
4518
                    'destination' => $location->id,
4519
                    'path' => '/Design/Plain-site/An-awesome-forum2',
4520
                    'languageCodes' => array('eng-US'),
4521
                    'alwaysAvailable' => true,
4522
                    'isHistory' => false,
4523
                    'isCustom' => false,
4524
                    'forward' => false,
4525
                ),
4526
                '/Design/Plain-site/An-awesome-forum23' => array(
4527
                    'type' => URLAlias::LOCATION,
4528
                    'destination' => $location->id,
4529
                    'path' => '/Design/Plain-site/An-awesome-forum23',
4530
                    'languageCodes' => array('eng-GB'),
4531
                    'alwaysAvailable' => true,
4532
                    'isHistory' => false,
4533
                    'isCustom' => false,
4534
                    'forward' => false,
4535
                ),
4536
            ),
4537
            $aliases
4538
        );
4539
    }
4540
4541
    /**
4542
     */
4543
    public function testCustomURLAliasesNotHistorizedOnUpdatedContent()
4544
    {
4545
        $repository = $this->getRepository();
4546
4547
        $contentService = $repository->getContentService();
4548
4549
        /* BEGIN: Use Case */
4550
        $urlAliasService = $repository->getURLAliasService();
4551
        $locationService = $repository->getLocationService();
4552
4553
        $content = $this->createContentVersion1();
4554
4555
        // Create a custom URL alias
4556
        $urlAliasService->createUrlAlias(
4557
            $locationService->loadLocation(
4558
                $content->getVersionInfo()->getContentInfo()->mainLocationId
4559
            ),
4560
            '/my/fancy/story-about-ez-publish',
4561
            'eng-US'
4562
        );
4563
4564
        $draftVersion2 = $contentService->createContentDraft($content->contentInfo);
4565
4566
        $contentUpdate = $contentService->newContentUpdateStruct();
4567
        $contentUpdate->initialLanguageCode = 'eng-US';
4568
        $contentUpdate->setField('name', 'Amazing Bielefeld forum');
4569
4570
        $draftVersion2 = $contentService->updateContent(
4571
            $draftVersion2->getVersionInfo(),
4572
            $contentUpdate
4573
        );
4574
4575
        // Only marks auto-generated aliases as history
4576
        // the custom one is left untouched
4577
        $liveContent = $contentService->publishVersion($draftVersion2->getVersionInfo());
4578
        /* END: Use Case */
4579
4580
        $location = $locationService->loadLocation(
4581
            $liveContent->getVersionInfo()->getContentInfo()->mainLocationId
4582
        );
4583
4584
        $aliases = $urlAliasService->listLocationAliases($location);
4585
4586
        $this->assertAliasesCorrect(
4587
            array(
4588
                '/my/fancy/story-about-ez-publish' => array(
4589
                    'type' => URLAlias::LOCATION,
4590
                    'destination' => $location->id,
4591
                    'path' => '/my/fancy/story-about-ez-publish',
4592
                    'languageCodes' => array('eng-US'),
4593
                    'isHistory' => false,
4594
                    'isCustom' => true,
4595
                    'forward' => false,
4596
                    'alwaysAvailable' => false,
4597
                ),
4598
            ),
4599
            $aliases
4600
        );
4601
    }
4602
4603
    /**
4604
     * Test to ensure that old versions are not affected by updates to newer
4605
     * drafts.
4606
     */
4607
    public function testUpdatingDraftDoesNotUpdateOldVersions()
4608
    {
4609
        $repository = $this->getRepository();
4610
4611
        $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...
4612
4613
        $contentService = $repository->getContentService();
4614
4615
        $contentVersion2 = $this->createContentVersion2();
4616
4617
        $loadedContent1 = $contentService->loadContent($contentVersion2->id, null, 1);
4618
        $loadedContent2 = $contentService->loadContent($contentVersion2->id, null, 2);
4619
4620
        $this->assertNotEquals(
4621
            $loadedContent1->getFieldValue('name', 'eng-US'),
4622
            $loadedContent2->getFieldValue('name', 'eng-US')
4623
        );
4624
    }
4625
4626
    /**
4627
     * Asserts that all aliases defined in $expectedAliasProperties with the
4628
     * given properties are available in $actualAliases and not more.
4629
     *
4630
     * @param array $expectedAliasProperties
4631
     * @param array $actualAliases
4632
     */
4633
    private function assertAliasesCorrect(array $expectedAliasProperties, array $actualAliases)
4634
    {
4635
        foreach ($actualAliases as $actualAlias) {
4636
            if (!isset($expectedAliasProperties[$actualAlias->path])) {
4637
                $this->fail(
4638
                    sprintf(
4639
                        'Alias with path "%s" in languages "%s" not expected.',
4640
                        $actualAlias->path,
4641
                        implode(', ', $actualAlias->languageCodes)
4642
                    )
4643
                );
4644
            }
4645
4646
            foreach ($expectedAliasProperties[$actualAlias->path] as $propertyName => $propertyValue) {
4647
                $this->assertEquals(
4648
                    $propertyValue,
4649
                    $actualAlias->$propertyName,
4650
                    sprintf(
4651
                        'Property $%s incorrect on alias with path "%s" in languages "%s".',
4652
                        $propertyName,
4653
                        $actualAlias->path,
4654
                        implode(', ', $actualAlias->languageCodes)
4655
                    )
4656
                );
4657
            }
4658
4659
            unset($expectedAliasProperties[$actualAlias->path]);
4660
        }
4661
4662
        if (!empty($expectedAliasProperties)) {
4663
            $this->fail(
4664
                sprintf(
4665
                    'Missing expected aliases with paths "%s".',
4666
                    implode('", "', array_keys($expectedAliasProperties))
4667
                )
4668
            );
4669
        }
4670
    }
4671
4672
    /**
4673
     * Asserts that the given fields are equal to the default fields fixture.
4674
     *
4675
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
4676
     */
4677
    private function assertAllFieldsEquals(array $fields)
4678
    {
4679
        $actual = $this->normalizeFields($fields);
4680
        $expected = $this->normalizeFields($this->createFieldsFixture());
4681
4682
        $this->assertEquals($expected, $actual);
4683
    }
4684
4685
    /**
4686
     * Asserts that the given fields are equal to a language filtered set of the
4687
     * default fields fixture.
4688
     *
4689
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
4690
     * @param string $languageCode
4691
     */
4692
    private function assertLocaleFieldsEquals(array $fields, $languageCode)
4693
    {
4694
        $actual = $this->normalizeFields($fields);
4695
4696
        $expected = array();
4697
        foreach ($this->normalizeFields($this->createFieldsFixture()) as $field) {
4698
            if ($field->languageCode !== $languageCode) {
4699
                continue;
4700
            }
4701
            $expected[] = $field;
4702
        }
4703
4704
        $this->assertEquals($expected, $actual);
4705
    }
4706
4707
    /**
4708
     * This method normalizes a set of fields and returns a normalized set.
4709
     *
4710
     * Normalization means it resets the storage specific field id to zero and
4711
     * it sorts the field by their identifier and their language code. In
4712
     * addition, the field value is removed, since this one depends on the
4713
     * specific FieldType, which is tested in a dedicated integration test.
4714
     *
4715
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
4716
     *
4717
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
4718
     */
4719
    private function normalizeFields(array $fields)
4720
    {
4721
        $normalized = array();
4722 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...
4723
            $normalized[] = new Field(
4724
                array(
4725
                    'id' => 0,
4726
                    'value' => ($field->value !== null ? true : null),
0 ignored issues
show
Documentation introduced by
The property $value is declared protected in eZ\Publish\API\Repository\Values\Content\Field. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
4727
                    'languageCode' => $field->languageCode,
4728
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
4729
                )
4730
            );
4731
        }
4732
        usort(
4733
            $normalized,
4734 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...
4735
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
4736
                    return strcasecmp($field1->languageCode, $field2->languageCode);
4737
                }
4738
4739
                return $return;
4740
            }
4741
        );
4742
4743
        return $normalized;
4744
    }
4745
4746
    /**
4747
     * Returns a filtered set of the default fields fixture.
4748
     *
4749
     * @param string $languageCode
4750
     *
4751
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
4752
     */
4753
    private function createLocaleFieldsFixture($languageCode)
4754
    {
4755
        $fields = array();
4756
        foreach ($this->createFieldsFixture() as $field) {
4757
            if (null === $field->languageCode || $languageCode === $field->languageCode) {
4758
                $fields[] = $field;
4759
            }
4760
        }
4761
4762
        return $fields;
4763
    }
4764
4765
    /**
4766
     * Asserts that given Content has default ContentStates.
4767
     *
4768
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
4769
     */
4770 View Code Duplication
    private function assertDefaultContentStates(ContentInfo $contentInfo)
4771
    {
4772
        $repository = $this->getRepository();
4773
        $objectStateService = $repository->getObjectStateService();
4774
4775
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
4776
4777
        foreach ($objectStateGroups as $objectStateGroup) {
4778
            $contentState = $objectStateService->getContentState($contentInfo, $objectStateGroup);
4779
            foreach ($objectStateService->loadObjectStates($objectStateGroup) as $objectState) {
4780
                // Only check the first object state which is the default one.
4781
                $this->assertEquals(
4782
                    $objectState,
4783
                    $contentState
4784
                );
4785
                break;
4786
            }
4787
        }
4788
    }
4789
4790
    /**
4791
     * Returns the default fixture of fields used in most tests.
4792
     *
4793
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
4794
     */
4795
    private function createFieldsFixture()
4796
    {
4797
        return array(
4798
            new Field(
4799
                array(
4800
                    'id' => 0,
4801
                    'value' => 'Foo',
4802
                    'languageCode' => 'eng-US',
4803
                    'fieldDefIdentifier' => 'description',
4804
                )
4805
            ),
4806
            new Field(
4807
                array(
4808
                    'id' => 0,
4809
                    'value' => 'Bar',
4810
                    'languageCode' => 'eng-GB',
4811
                    'fieldDefIdentifier' => 'description',
4812
                )
4813
            ),
4814
            new Field(
4815
                array(
4816
                    'id' => 0,
4817
                    'value' => 'An awesome multi-lang forum²',
4818
                    'languageCode' => 'eng-US',
4819
                    'fieldDefIdentifier' => 'name',
4820
                )
4821
            ),
4822
            new Field(
4823
                array(
4824
                    'id' => 0,
4825
                    'value' => 'An awesome multi-lang forum²³',
4826
                    'languageCode' => 'eng-GB',
4827
                    'fieldDefIdentifier' => 'name',
4828
                )
4829
            ),
4830
        );
4831
    }
4832
}
4833