Completed
Push — signal_search_issues ( 5556b2...f328ba )
by André
63:06 queued 07:22
created

testCreateContentSetsExpectedContentInfo()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

Loading history...
132
133
        // Set Anonymous user as current
134
        $repository->setCurrentUser($repository->getUserService()->loadUser($anonymousUserId));
135
136
        // Create a new content object:
137
        $contentCreate = $contentService->newContentCreateStruct(
138
            $contentTypeService->loadContentTypeByIdentifier('folder'),
139
            'eng-GB'
140
        );
141
142
        $contentCreate->setField('name', 'Folder 1');
143
144
        $content = $contentService->createContent(
145
            $contentCreate,
146
            array($locationService->newLocationCreateStruct(2))
147
        );
148
149
        $contentService->publishVersion(
150
            $content->getVersionInfo()
151
        );
152
    }
153
154
    /**
155
     * Test for the createContent() method.
156
     *
157
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
158
     *
159
     * @return \eZ\Publish\API\Repository\Values\Content\Content
160
     *
161
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
162
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
163
     */
164
    public function testCreateContentSetsContentInfo($content)
165
    {
166
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $content->contentInfo);
167
168
        return $content;
169
    }
170
171
    /**
172
     * Test for the createContent() method.
173
     *
174
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
175
     *
176
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
177
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsContentInfo
178
     */
179
    public function testCreateContentSetsExpectedContentInfo($content)
180
    {
181
        $this->assertEquals(
182
            array(
183
                $content->id,
184
                28, // id of content type "forum"
185
                true,
186
                1,
187
                'abcdef0123456789abcdef0123456789',
188
                'eng-US',
189
                $this->getRepository()->getCurrentUser()->id,
190
                false,
191
                null,
192
                // Main Location id for unpublished Content should be null
193
                null,
194
            ),
195
            array(
196
                $content->contentInfo->id,
197
                $content->contentInfo->contentTypeId,
198
                $content->contentInfo->alwaysAvailable,
199
                $content->contentInfo->currentVersionNo,
200
                $content->contentInfo->remoteId,
201
                $content->contentInfo->mainLanguageCode,
202
                $content->contentInfo->ownerId,
203
                $content->contentInfo->published,
204
                $content->contentInfo->publishedDate,
205
                $content->contentInfo->mainLocationId,
206
            )
207
        );
208
    }
209
210
    /**
211
     * Test for the createContent() method.
212
     *
213
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
214
     *
215
     * @return \eZ\Publish\API\Repository\Values\Content\Content
216
     *
217
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
218
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
219
     */
220
    public function testCreateContentSetsVersionInfo($content)
221
    {
222
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo', $content->getVersionInfo());
223
224
        return $content;
225
    }
226
227
    /**
228
     * Test for the createContent() method.
229
     *
230
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
231
     *
232
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
233
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentSetsVersionInfo
234
     */
235
    public function testCreateContentSetsExpectedVersionInfo($content)
236
    {
237
        $this->assertEquals(
238
            array(
239
                'status' => VersionInfo::STATUS_DRAFT,
240
                'versionNo' => 1,
241
                'creatorId' => $this->getRepository()->getCurrentUser()->id,
242
                'initialLanguageCode' => 'eng-US',
243
            ),
244
            array(
245
                'status' => $content->getVersionInfo()->status,
246
                'versionNo' => $content->getVersionInfo()->versionNo,
247
                'creatorId' => $content->getVersionInfo()->creatorId,
248
                'initialLanguageCode' => $content->getVersionInfo()->initialLanguageCode,
249
            )
250
        );
251
    }
252
253
    /**
254
     * Test for the createContent() method.
255
     *
256
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
257
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
258
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
259
     */
260
    public function testCreateContentThrowsInvalidArgumentException()
261
    {
262
        if ($this->isVersion4()) {
263
            $this->markTestSkipped('This test requires eZ Publish 5');
264
        }
265
266
        $repository = $this->getRepository();
267
268
        /* BEGIN: Use Case */
269
        $contentTypeService = $repository->getContentTypeService();
270
        $contentService = $repository->getContentService();
271
272
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
273
274
        $contentCreate1 = $contentService->newContentCreateStruct($contentType, 'eng-US');
275
        $contentCreate1->setField('name', 'An awesome Sidelfingen forum');
276
277
        $contentCreate1->remoteId = 'abcdef0123456789abcdef0123456789';
278
        $contentCreate1->alwaysAvailable = true;
279
280
        $draft = $contentService->createContent($contentCreate1);
281
        $contentService->publishVersion($draft->versionInfo);
282
283
        $contentCreate2 = $contentService->newContentCreateStruct($contentType, 'eng-GB');
284
        $contentCreate2->setField('name', 'An awesome Bielefeld forum');
285
286
        $contentCreate2->remoteId = 'abcdef0123456789abcdef0123456789';
287
        $contentCreate2->alwaysAvailable = false;
288
289
        // This call will fail with an "InvalidArgumentException", because the
290
        // remoteId is already in use.
291
        $contentService->createContent($contentCreate2);
292
        /* END: Use Case */
293
    }
294
295
    /**
296
     * Test for the createContent() method.
297
     *
298
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
299
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
300
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
301
     */
302 View Code Duplication
    public function testCreateContentThrowsInvalidArgumentExceptionOnFieldTypeNotAccept()
303
    {
304
        $repository = $this->getRepository();
305
306
        /* BEGIN: Use Case */
307
        $contentTypeService = $repository->getContentTypeService();
308
        $contentService = $repository->getContentService();
309
310
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
311
312
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
313
        // The name field does only accept strings and null as its values
314
        $contentCreate->setField('name', new \stdClass());
315
316
        // Throws InvalidArgumentException since the name field is filled
317
        // improperly
318
        $draft = $contentService->createContent($contentCreate);
0 ignored issues
show
Unused Code introduced by
$draft is not used, you could remove the assignment.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading history...
402
            '0123456789abcdef0123456789abcdef'
403
        );
404
        /* END: Use Case */
405
    }
406
407
    /**
408
     * Test for the createContent() method.
409
     *
410
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
411
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
412
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
413
     */
414
    public function testCreateContentThrowsInvalidArgumentExceptionWithLocationCreateParameter()
415
    {
416
        $repository = $this->getRepository();
417
418
        $parentLocationId = $this->generateId('location', 56);
419
        /* BEGIN: Use Case */
420
        // $parentLocationId is a valid location ID
421
422
        $contentService = $repository->getContentService();
423
        $contentTypeService = $repository->getContentTypeService();
424
        $locationService = $repository->getLocationService();
425
426
        // Load content type
427
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
428
429
        // Configure new locations
430
        $locationCreate1 = $locationService->newLocationCreateStruct($parentLocationId);
431
432
        $locationCreate1->priority = 23;
433
        $locationCreate1->hidden = true;
434
        $locationCreate1->remoteId = '0123456789abcdef0123456789aaaaaa';
435
        $locationCreate1->sortField = Location::SORT_FIELD_NODE_ID;
436
        $locationCreate1->sortOrder = Location::SORT_ORDER_DESC;
437
438
        $locationCreate2 = $locationService->newLocationCreateStruct($parentLocationId);
439
440
        $locationCreate2->priority = 42;
441
        $locationCreate2->hidden = true;
442
        $locationCreate2->remoteId = '0123456789abcdef0123456789bbbbbb';
443
        $locationCreate2->sortField = Location::SORT_FIELD_NODE_ID;
444
        $locationCreate2->sortOrder = Location::SORT_ORDER_DESC;
445
446
        // Configure new content object
447
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
448
449
        $contentCreate->setField('name', 'A awesome Sindelfingen forum');
450
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
451
        $contentCreate->alwaysAvailable = true;
452
453
        // Create new content object under the specified location
454
        $draft = $contentService->createContent(
455
            $contentCreate,
456
            array($locationCreate1)
457
        );
458
        $contentService->publishVersion($draft->versionInfo);
459
460
        // This call will fail with an "InvalidArgumentException", because the
461
        // Content remoteId already exists,
462
        $contentService->createContent(
463
            $contentCreate,
464
            array($locationCreate2)
465
        );
466
        /* END: Use Case */
467
    }
468
469
    /**
470
     * Test for the loadContentInfo() method.
471
     *
472
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
473
     * @group user
474
     */
475 View Code Duplication
    public function testLoadContentInfo()
476
    {
477
        $repository = $this->getRepository();
478
479
        $mediaFolderId = $this->generateId('object', 41);
480
        /* BEGIN: Use Case */
481
        $contentService = $repository->getContentService();
482
483
        // Load the ContentInfo for "Media" folder
484
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
485
        /* END: Use Case */
486
487
        $this->assertInstanceOf(
488
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
489
            $contentInfo
490
        );
491
    }
492
493
    /**
494
     * Test for the loadContentInfo() method.
495
     *
496
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
497
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
498
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
499
     */
500 View Code Duplication
    public function testLoadContentInfoThrowsNotFoundException()
501
    {
502
        $repository = $this->getRepository();
503
504
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
505
        /* BEGIN: Use Case */
506
        $contentService = $repository->getContentService();
507
508
        // This call will fail with a NotFoundException
509
        $contentService->loadContentInfo($nonExistentContentId);
510
        /* END: Use Case */
511
    }
512
513
    /**
514
     * Test for the loadContentInfoByRemoteId() method.
515
     *
516
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
517
     */
518
    public function testLoadContentInfoByRemoteId()
519
    {
520
        $repository = $this->getRepository();
521
522
        /* BEGIN: Use Case */
523
        $contentService = $repository->getContentService();
524
525
        // Load the ContentInfo for "Media" folder
526
        $contentInfo = $contentService->loadContentInfoByRemoteId('faaeb9be3bd98ed09f606fc16d144eca');
527
        /* END: Use Case */
528
529
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo', $contentInfo);
530
    }
531
532
    /**
533
     * Test for the loadContentInfoByRemoteId() method.
534
     *
535
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
536
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
537
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
538
     */
539
    public function testLoadContentInfoByRemoteIdThrowsNotFoundException()
540
    {
541
        $repository = $this->getRepository();
542
543
        /* BEGIN: Use Case */
544
        $contentService = $repository->getContentService();
545
546
        // This call will fail with a NotFoundException
547
        $contentService->loadContentInfoByRemoteId('abcdefghijklmnopqrstuvwxyz0123456789');
548
        /* END: Use Case */
549
    }
550
551
    /**
552
     * Test for the loadVersionInfo() method.
553
     *
554
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
555
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
556
     * @group user
557
     */
558
    public function testLoadVersionInfo()
559
    {
560
        $repository = $this->getRepository();
561
562
        $mediaFolderId = $this->generateId('object', 41);
563
        /* BEGIN: Use Case */
564
        // $mediaFolderId contains the ID of the "Media" folder
565
566
        $contentService = $repository->getContentService();
567
568
        // Load the ContentInfo for "Media" folder
569
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
570
571
        // Now load the current version info of the "Media" folder
572
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
573
        /* END: Use Case */
574
575
        $this->assertInstanceOf(
576
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
577
            $versionInfo
578
        );
579
    }
580
581
    /**
582
     * Test for the loadVersionInfoById() method.
583
     *
584
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
585
     */
586 View Code Duplication
    public function testLoadVersionInfoById()
587
    {
588
        $repository = $this->getRepository();
589
590
        $mediaFolderId = $this->generateId('object', 41);
591
        /* BEGIN: Use Case */
592
        // $mediaFolderId contains the ID of the "Media" folder
593
594
        $contentService = $repository->getContentService();
595
596
        // Load the VersionInfo for "Media" folder
597
        $versionInfo = $contentService->loadVersionInfoById($mediaFolderId);
598
        /* END: Use Case */
599
600
        $this->assertInstanceOf(
601
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
602
            $versionInfo
603
        );
604
    }
605
606
    /**
607
     * Test for the loadVersionInfoById() method.
608
     *
609
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
610
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
611
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
612
     */
613 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundException()
614
    {
615
        $repository = $this->getRepository();
616
617
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
618
        /* BEGIN: Use Case */
619
        $contentService = $repository->getContentService();
620
621
        // This call will fail with a "NotFoundException"
622
        $contentService->loadVersionInfoById($nonExistentContentId);
623
        /* END: Use Case */
624
    }
625
626
    /**
627
     * Test for the loadContentByContentInfo() method.
628
     *
629
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
630
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
631
     */
632
    public function testLoadContentByContentInfo()
633
    {
634
        $repository = $this->getRepository();
635
636
        $mediaFolderId = $this->generateId('object', 41);
637
        /* BEGIN: Use Case */
638
        // $mediaFolderId contains the ID of the "Media" folder
639
640
        $contentService = $repository->getContentService();
641
642
        // Load the ContentInfo for "Media" folder
643
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
644
645
        // Now load the current content version for the info instance
646
        $content = $contentService->loadContentByContentInfo($contentInfo);
647
        /* END: Use Case */
648
649
        $this->assertInstanceOf(
650
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
651
            $content
652
        );
653
    }
654
655
    /**
656
     * Test for the loadContentByVersionInfo() method.
657
     *
658
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
659
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
660
     */
661
    public function testLoadContentByVersionInfo()
662
    {
663
        $repository = $this->getRepository();
664
665
        $mediaFolderId = $this->generateId('object', 41);
666
        /* BEGIN: Use Case */
667
        // $mediaFolderId contains the ID of the "Media" folder
668
669
        $contentService = $repository->getContentService();
670
671
        // Load the ContentInfo for "Media" folder
672
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
673
674
        // Load the current VersionInfo
675
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
676
677
        // Now load the current content version for the info instance
678
        $content = $contentService->loadContentByVersionInfo($versionInfo);
679
        /* END: Use Case */
680
681
        $this->assertInstanceOf(
682
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
683
            $content
684
        );
685
    }
686
687
    /**
688
     * Test for the loadContent() method.
689
     *
690
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
691
     * @group user
692
     * @group field-type
693
     */
694 View Code Duplication
    public function testLoadContent()
695
    {
696
        $repository = $this->getRepository();
697
698
        $mediaFolderId = $this->generateId('object', 41);
699
        /* BEGIN: Use Case */
700
        // $mediaFolderId contains the ID of the "Media" folder
701
702
        $contentService = $repository->getContentService();
703
704
        // Load the Content for "Media" folder, any language and current version
705
        $content = $contentService->loadContent($mediaFolderId);
706
        /* END: Use Case */
707
708
        $this->assertInstanceOf(
709
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
710
            $content
711
        );
712
    }
713
714
    /**
715
     * Test for the loadContent() method.
716
     *
717
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
718
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
719
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
720
     */
721 View Code Duplication
    public function testLoadContentThrowsNotFoundException()
722
    {
723
        $repository = $this->getRepository();
724
725
        $nonExistentContentId = $this->generateId('object', self::DB_INT_MAX);
726
        /* BEGIN: Use Case */
727
        $contentService = $repository->getContentService();
728
729
        // This call will fail with a "NotFoundException"
730
        $contentService->loadContent($nonExistentContentId);
731
        /* END: Use Case */
732
    }
733
734
    /**
735
     * Test for the loadContentByRemoteId() method.
736
     *
737
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
738
     */
739
    public function testLoadContentByRemoteId()
740
    {
741
        $repository = $this->getRepository();
742
743
        /* BEGIN: Use Case */
744
        // Remote id of the "Media" folder
745
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
746
747
        $contentService = $repository->getContentService();
748
749
        // Load the Content for "Media" folder
750
        $content = $contentService->loadContentByRemoteId($mediaRemoteId);
751
        /* END: Use Case */
752
753
        $this->assertInstanceOf(
754
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
755
            $content
756
        );
757
    }
758
759
    /**
760
     * Test for the loadContentByRemoteId() method.
761
     *
762
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
763
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
764
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
765
     */
766
    public function testLoadContentByRemoteIdThrowsNotFoundException()
767
    {
768
        $repository = $this->getRepository();
769
770
        /* BEGIN: Use Case */
771
        $contentService = $repository->getContentService();
772
773
        // This call will fail with a "NotFoundException", because no content
774
        // object exists for the given remoteId
775
        $contentService->loadContentByRemoteId('a1b1c1d1e1f1a2b2c2d2e2f2a3b3c3d3');
776
        /* END: Use Case */
777
    }
778
779
    /**
780
     * Test for the publishVersion() method.
781
     *
782
     * @return \eZ\Publish\API\Repository\Values\Content\Content
783
     *
784
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
785
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
786
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
787
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
788
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
789
     * @group user
790
     * @group field-type
791
     */
792
    public function testPublishVersion()
793
    {
794
        /* BEGIN: Use Case */
795
        $content = $this->createContentVersion1();
796
        /* END: Use Case */
797
798
        $this->assertInstanceOf(
799
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
800
            $content
801
        );
802
803
        return $content;
804
    }
805
806
    /**
807
     * Test for the publishVersion() method.
808
     *
809
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
810
     *
811
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
812
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
813
     */
814
    public function testPublishVersionSetsExpectedContentInfo($content)
815
    {
816
        $this->assertEquals(
817
            array(
818
                $content->id,
819
                true,
820
                1,
821
                'abcdef0123456789abcdef0123456789',
822
                'eng-US',
823
                $this->getRepository()->getCurrentUser()->id,
824
                true,
825
            ),
826
            array(
827
                $content->contentInfo->id,
828
                $content->contentInfo->alwaysAvailable,
829
                $content->contentInfo->currentVersionNo,
830
                $content->contentInfo->remoteId,
831
                $content->contentInfo->mainLanguageCode,
832
                $content->contentInfo->ownerId,
833
                $content->contentInfo->published,
834
            )
835
        );
836
837
        $this->assertNotNull($content->contentInfo->mainLocationId);
838
        $date = new \DateTime('1984/01/01');
839
        $this->assertGreaterThan(
840
            $date->getTimestamp(),
841
            $content->contentInfo->publishedDate->getTimestamp()
842
        );
843
    }
844
845
    /**
846
     * Test for the publishVersion() method.
847
     *
848
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
849
     *
850
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
851
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
852
     */
853
    public function testPublishVersionSetsExpectedVersionInfo($content)
854
    {
855
        $this->assertEquals(
856
            array(
857
                $this->getRepository()->getCurrentUser()->id,
858
                'eng-US',
859
                VersionInfo::STATUS_PUBLISHED,
860
                1,
861
            ),
862
            array(
863
                $content->getVersionInfo()->creatorId,
864
                $content->getVersionInfo()->initialLanguageCode,
865
                $content->getVersionInfo()->status,
866
                $content->getVersionInfo()->versionNo,
867
            )
868
        );
869
870
        $date = new \DateTime('1984/01/01');
871
        $this->assertGreaterThan(
872
            $date->getTimestamp(),
873
            $content->getVersionInfo()->modificationDate->getTimestamp()
874
        );
875
876
        $this->assertNotNull($content->getVersionInfo()->modificationDate);
877
    }
878
879
    /**
880
     * Test for the publishVersion() method.
881
     *
882
     * @return \eZ\Publish\API\Repository\Values\Content\Content
883
     *
884
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
885
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentWithLocationCreateParameterDoesNotCreateLocationImmediately
886
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
887
     */
888
    public function testPublishVersionCreatesLocationsDefinedOnCreate()
889
    {
890
        $repository = $this->getRepository();
891
892
        /* BEGIN: Use Case */
893
        $content = $this->createContentVersion1();
894
        /* END: Use Case */
895
896
        $locationService = $repository->getLocationService();
897
        $location = $locationService->loadLocationByRemoteId(
898
            '0123456789abcdef0123456789abcdef'
899
        );
900
901
        $this->assertEquals(
902
            $location->getContentInfo(),
903
            $content->getVersionInfo()->getContentInfo()
904
        );
905
906
        return array($content, $location);
907
    }
908
909
    /**
910
     * Test for the publishVersion() method.
911
     *
912
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
913
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionCreatesLocationsDefinedOnCreate
914
     */
915
    public function testCreateContentWithLocationCreateParameterCreatesExpectedLocation(array $testData)
916
    {
917
        /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
918
        /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
919
        list($content, $location) = $testData;
920
921
        $parentLocationId = $this->generateId('location', 56);
922
        $parentLocation = $this->getRepository()->getLocationService()->loadLocation($parentLocationId);
923
        $mainLocationId = $content->getVersionInfo()->getContentInfo()->mainLocationId;
924
925
        $this->assertPropertiesCorrect(
926
            array(
927
                'id' => $mainLocationId,
928
                'priority' => 23,
929
                'hidden' => true,
930
                'invisible' => true,
931
                'remoteId' => '0123456789abcdef0123456789abcdef',
932
                'parentLocationId' => $parentLocationId,
933
                'pathString' => $parentLocation->pathString . $mainLocationId . '/',
934
                'depth' => $parentLocation->depth + 1,
935
                'sortField' => Location::SORT_FIELD_NODE_ID,
936
                'sortOrder' => Location::SORT_ORDER_DESC,
937
            ),
938
            $location
939
        );
940
    }
941
942
    /**
943
     * Test for the publishVersion() method.
944
     *
945
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
946
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
947
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
948
     */
949 View Code Duplication
    public function testPublishVersionThrowsBadStateException()
950
    {
951
        $repository = $this->getRepository();
952
953
        $contentService = $repository->getContentService();
954
955
        /* BEGIN: Use Case */
956
        $draft = $this->createContentDraftVersion1();
957
958
        // Publish the content draft
959
        $contentService->publishVersion($draft->getVersionInfo());
960
961
        // This call will fail with a "BadStateException", because the version
962
        // is already published.
963
        $contentService->publishVersion($draft->getVersionInfo());
964
        /* END: Use Case */
965
    }
966
967
    /**
968
     * Test for the createContentDraft() method.
969
     *
970
     * @return \eZ\Publish\API\Repository\Values\Content\Content
971
     *
972
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
973
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
974
     * @group user
975
     */
976
    public function testCreateContentDraft()
977
    {
978
        $repository = $this->getRepository();
979
980
        $contentService = $repository->getContentService();
981
982
        /* BEGIN: Use Case */
983
        $content = $this->createContentVersion1();
984
985
        // Now we create a new draft from the published content
986
        $draftedContent = $contentService->createContentDraft($content->contentInfo);
987
        /* END: Use Case */
988
989
        $this->assertInstanceOf(
990
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
991
            $draftedContent
992
        );
993
994
        return $draftedContent;
995
    }
996
997
    /**
998
     * Test for the createContentDraft() method.
999
     *
1000
     * Test that editor has access to edit own draft.
1001
     * Note: Editors have access to version_read, which is needed to load content drafts.
1002
     *
1003
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1004
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1005
     * @group user
1006
     */
1007
    public function testCreateContentDraftAndLoadAccess()
1008
    {
1009
        $repository = $this->getRepository();
1010
1011
        /* BEGIN: Use Case */
1012
        $user = $this->createUserVersion1();
1013
1014
        // Set new editor as user
1015
        $repository->setCurrentUser($user);
1016
1017
        // Create draft
1018
        $draft = $this->createContentDraftVersion1(2, 'folder');
1019
1020
        // Try to load the draft
1021
        $contentService = $repository->getContentService();
1022
        $loadedDraft = $contentService->loadContent($draft->id);
1023
1024
        /* END: Use Case */
1025
1026
        $this->assertEquals($draft->id, $loadedDraft->id);
1027
    }
1028
1029
    /**
1030
     * Test for the createContentDraft() method.
1031
     *
1032
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1033
     *
1034
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1035
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1036
     */
1037
    public function testCreateContentDraftSetsExpectedProperties($draft)
1038
    {
1039
        $this->assertEquals(
1040
            array(
1041
                'fieldCount' => 2,
1042
                'relationCount' => 0,
1043
            ),
1044
            array(
1045
                'fieldCount' => count($draft->getFields()),
1046
                'relationCount' => count($this->getRepository()->getContentService()->loadRelations($draft->getVersionInfo())),
1047
            )
1048
        );
1049
    }
1050
1051
    /**
1052
     * Test for the createContentDraft() method.
1053
     *
1054
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1055
     *
1056
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1057
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1058
     */
1059
    public function testCreateContentDraftSetsContentInfo($draft)
1060
    {
1061
        $contentInfo = $draft->contentInfo;
1062
1063
        $this->assertEquals(
1064
            array(
1065
                $draft->id,
1066
                true,
1067
                1,
1068
                'eng-US',
1069
                $this->getRepository()->getCurrentUser()->id,
1070
                'abcdef0123456789abcdef0123456789',
1071
                1,
1072
            ),
1073
            array(
1074
                $contentInfo->id,
1075
                $contentInfo->alwaysAvailable,
1076
                $contentInfo->currentVersionNo,
1077
                $contentInfo->mainLanguageCode,
1078
                $contentInfo->ownerId,
1079
                $contentInfo->remoteId,
1080
                $contentInfo->sectionId,
1081
            )
1082
        );
1083
    }
1084
1085
    /**
1086
     * Test for the createContentDraft() method.
1087
     *
1088
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1089
     *
1090
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1091
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1092
     */
1093
    public function testCreateContentDraftSetsVersionInfo($draft)
1094
    {
1095
        $versionInfo = $draft->getVersionInfo();
1096
1097
        $this->assertEquals(
1098
            array(
1099
                'creatorId' => $this->getRepository()->getCurrentUser()->id,
1100
                'initialLanguageCode' => 'eng-US',
1101
                'languageCodes' => array(0 => 'eng-US'),
1102
                'status' => VersionInfo::STATUS_DRAFT,
1103
                'versionNo' => 2,
1104
            ),
1105
            array(
1106
                'creatorId' => $versionInfo->creatorId,
1107
                'initialLanguageCode' => $versionInfo->initialLanguageCode,
1108
                'languageCodes' => $versionInfo->languageCodes,
1109
                'status' => $versionInfo->status,
1110
                'versionNo' => $versionInfo->versionNo,
1111
            )
1112
        );
1113
    }
1114
1115
    /**
1116
     * Test for the createContentDraft() method.
1117
     *
1118
     * @param \eZ\Publish\API\Repository\Values\Content\Content $draft
1119
     *
1120
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
1121
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1122
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
1123
     */
1124
    public function testCreateContentDraftLoadVersionInfoStillLoadsPublishedVersion($draft)
0 ignored issues
show
Unused Code introduced by
The parameter $draft is not used and could be removed.

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

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

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

<?php

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

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

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

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

}

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

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

See also the PhpDoc documentation for @property.

Loading history...
1517
                // Found field
1518
                return;
1519
            }
1520
        }
1521
        $this->fail(
1522
            'Field with identifier "name" in language "eng-US" could not be found or has empty value.'
1523
        );
1524
    }
1525
1526
    /**
1527
     * Test for the createContentDraft() method.
1528
     *
1529
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
1530
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1531
     */
1532 View Code Duplication
    public function testCreateContentDraftWithSecondParameter()
1533
    {
1534
        $repository = $this->getRepository();
1535
1536
        $contentService = $repository->getContentService();
1537
1538
        /* BEGIN: Use Case */
1539
        $contentVersion2 = $this->createContentVersion2();
1540
1541
        // Now we create a new draft from the initial version
1542
        $draftedContentReloaded = $contentService->createContentDraft(
1543
            $contentVersion2->contentInfo,
1544
            $contentVersion2->getVersionInfo()
1545
        );
1546
        /* END: Use Case */
1547
1548
        $this->assertEquals(3, $draftedContentReloaded->getVersionInfo()->versionNo);
1549
    }
1550
1551
    /**
1552
     * Test for the publishVersion() method.
1553
     *
1554
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1555
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1556
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1557
     */
1558 View Code Duplication
    public function testPublishVersionFromContentDraft()
1559
    {
1560
        $repository = $this->getRepository();
1561
1562
        $contentService = $repository->getContentService();
1563
1564
        /* BEGIN: Use Case */
1565
        $contentVersion2 = $this->createContentVersion2();
1566
        /* END: Use Case */
1567
1568
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo);
1569
1570
        $this->assertEquals(
1571
            array(
1572
                'status' => VersionInfo::STATUS_PUBLISHED,
1573
                'versionNo' => 2,
1574
            ),
1575
            array(
1576
                'status' => $versionInfo->status,
1577
                'versionNo' => $versionInfo->versionNo,
1578
            )
1579
        );
1580
    }
1581
1582
    /**
1583
     * Test for the publishVersion() method.
1584
     *
1585
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1586
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1587
     */
1588 View Code Duplication
    public function testPublishVersionFromContentDraftArchivesOldVersion()
1589
    {
1590
        $repository = $this->getRepository();
1591
1592
        $contentService = $repository->getContentService();
1593
1594
        /* BEGIN: Use Case */
1595
        $contentVersion2 = $this->createContentVersion2();
1596
        /* END: Use Case */
1597
1598
        $versionInfo = $contentService->loadVersionInfo($contentVersion2->contentInfo, 1);
1599
1600
        $this->assertEquals(
1601
            array(
1602
                'status' => VersionInfo::STATUS_ARCHIVED,
1603
                'versionNo' => 1,
1604
            ),
1605
            array(
1606
                'status' => $versionInfo->status,
1607
                'versionNo' => $versionInfo->versionNo,
1608
            )
1609
        );
1610
    }
1611
1612
    /**
1613
     * Test for the publishVersion() method.
1614
     *
1615
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1616
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1617
     */
1618
    public function testPublishVersionFromContentDraftUpdatesContentInfoCurrentVersion()
1619
    {
1620
        /* BEGIN: Use Case */
1621
        $contentVersion2 = $this->createContentVersion2();
1622
        /* END: Use Case */
1623
1624
        $this->assertEquals(2, $contentVersion2->contentInfo->currentVersionNo);
1625
    }
1626
1627
    /**
1628
     * Test for the publishVersion() method.
1629
     *
1630
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1631
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1632
     */
1633
    public function testPublishVersionFromOldContentDraftArchivesNewerVersionNo()
1634
    {
1635
        $repository = $this->getRepository();
1636
1637
        $contentService = $repository->getContentService();
1638
1639
        /* BEGIN: Use Case */
1640
        $content = $this->createContentVersion1();
1641
1642
        // Create a new draft with versionNo = 2
1643
        $draftedContentVersion2 = $contentService->createContentDraft($content->contentInfo);
1644
1645
        // Create another new draft with versionNo = 3
1646
        $draftedContentVersion3 = $contentService->createContentDraft($content->contentInfo);
1647
1648
        // Publish draft with versionNo = 3
1649
        $contentService->publishVersion($draftedContentVersion3->getVersionInfo());
1650
1651
        // Publish the first draft with versionNo = 2
1652
        // currentVersionNo is now 2, versionNo 3 will be archived
1653
        $publishedDraft = $contentService->publishVersion($draftedContentVersion2->getVersionInfo());
1654
        /* END: Use Case */
1655
1656
        $this->assertEquals(2, $publishedDraft->contentInfo->currentVersionNo);
1657
    }
1658
1659
    /**
1660
     * Test for the newContentMetadataUpdateStruct() method.
1661
     *
1662
     * @see \eZ\Publish\API\Repository\ContentService::newContentMetadataUpdateStruct()
1663
     * @group user
1664
     */
1665
    public function testNewContentMetadataUpdateStruct()
1666
    {
1667
        $repository = $this->getRepository();
1668
1669
        /* BEGIN: Use Case */
1670
        $contentService = $repository->getContentService();
1671
1672
        // Creates a new metadata update struct
1673
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1674
1675
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1676
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1677
        $metadataUpdate->alwaysAvailable = false;
1678
        /* END: Use Case */
1679
1680
        $this->assertInstanceOf(
1681
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\ContentMetadataUpdateStruct',
1682
            $metadataUpdate
1683
        );
1684
    }
1685
1686
    /**
1687
     * Test for the updateContentMetadata() method.
1688
     *
1689
     * @return \eZ\Publish\API\Repository\Values\Content\Content
1690
     *
1691
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1692
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1693
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testNewContentMetadataUpdateStruct
1694
     * @group user
1695
     */
1696
    public function testUpdateContentMetadata()
1697
    {
1698
        $repository = $this->getRepository();
1699
1700
        $contentService = $repository->getContentService();
1701
1702
        /* BEGIN: Use Case */
1703
        $content = $this->createContentVersion1();
1704
1705
        // Creates a metadata update struct
1706
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1707
1708
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
1709
        $metadataUpdate->mainLanguageCode = 'eng-GB';
1710
        $metadataUpdate->alwaysAvailable = false;
1711
        $metadataUpdate->publishedDate = $this->createDateTime(441759600); // 1984/01/01
1712
        $metadataUpdate->modificationDate = $this->createDateTime(441759600); // 1984/01/01
1713
1714
        // Update the metadata of the published content object
1715
        $content = $contentService->updateContentMetadata(
1716
            $content->contentInfo,
1717
            $metadataUpdate
1718
        );
1719
        /* END: Use Case */
1720
1721
        $this->assertInstanceOf(
1722
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1723
            $content
1724
        );
1725
1726
        return $content;
1727
    }
1728
1729
    /**
1730
     * Test for the updateContentMetadata() method.
1731
     *
1732
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1733
     *
1734
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1735
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
1736
     */
1737
    public function testUpdateContentMetadataSetsExpectedProperties($content)
1738
    {
1739
        $contentInfo = $content->contentInfo;
1740
1741
        $this->assertEquals(
1742
            array(
1743
                'remoteId' => 'aaaabbbbccccddddeeeeffff11112222',
1744
                'sectionId' => $this->generateId('section', 1),
1745
                'alwaysAvailable' => false,
1746
                'currentVersionNo' => 1,
1747
                'mainLanguageCode' => 'eng-GB',
1748
                'modificationDate' => $this->createDateTime(441759600),
1749
                'ownerId' => $this->getRepository()->getCurrentUser()->id,
1750
                'published' => true,
1751
                'publishedDate' => $this->createDateTime(441759600),
1752
            ),
1753
            array(
1754
                'remoteId' => $contentInfo->remoteId,
1755
                'sectionId' => $contentInfo->sectionId,
1756
                'alwaysAvailable' => $contentInfo->alwaysAvailable,
1757
                'currentVersionNo' => $contentInfo->currentVersionNo,
1758
                'mainLanguageCode' => $contentInfo->mainLanguageCode,
1759
                'modificationDate' => $contentInfo->modificationDate,
1760
                'ownerId' => $contentInfo->ownerId,
1761
                'published' => $contentInfo->published,
1762
                'publishedDate' => $contentInfo->publishedDate,
1763
            )
1764
        );
1765
    }
1766
1767
    /**
1768
     * Test for the updateContentMetadata() method.
1769
     *
1770
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
1771
     *
1772
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1773
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
1774
     */
1775
    public function testUpdateContentMetadataNotUpdatesContentVersion($content)
1776
    {
1777
        $this->assertEquals(1, $content->getVersionInfo()->versionNo);
1778
    }
1779
1780
    /**
1781
     * Test for the updateContentMetadata() method.
1782
     *
1783
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
1784
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1785
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
1786
     */
1787
    public function testUpdateContentMetadataThrowsInvalidArgumentException()
1788
    {
1789
        $repository = $this->getRepository();
1790
1791
        $contentService = $repository->getContentService();
1792
1793
        /* BEGIN: Use Case */
1794
        // RemoteId of the "Media" page of an eZ Publish demo installation
1795
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1796
1797
        $content = $this->createContentVersion1();
1798
1799
        // Creates a metadata update struct
1800
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
1801
        $metadataUpdate->remoteId = $mediaRemoteId;
1802
1803
        // This call will fail with an "InvalidArgumentException", because the
1804
        // specified remoteId is already used by the "Media" page.
1805
        $contentService->updateContentMetadata(
1806
            $content->contentInfo,
1807
            $metadataUpdate
1808
        );
1809
        /* END: Use Case */
1810
    }
1811
1812
    /**
1813
     * Test for the deleteContent() method.
1814
     *
1815
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
1816
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1817
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1818
     */
1819 View Code Duplication
    public function testDeleteContent()
1820
    {
1821
        $repository = $this->getRepository();
1822
1823
        $contentService = $repository->getContentService();
1824
        $locationService = $repository->getLocationService();
1825
1826
        /* BEGIN: Use Case */
1827
        $contentVersion2 = $this->createContentVersion2();
1828
1829
        // Load the locations for this content object
1830
        $locations = $locationService->loadLocations($contentVersion2->contentInfo);
1831
1832
        // This will delete the content, all versions and the associated locations
1833
        $contentService->deleteContent($contentVersion2->contentInfo);
1834
        /* END: Use Case */
1835
1836
        foreach ($locations as $location) {
1837
            $locationService->loadLocation($location->id);
1838
        }
1839
    }
1840
1841
    /**
1842
     * Test for the deleteContent() method.
1843
     *
1844
     * Test for issue EZP-21057:
1845
     * "contentService: Unable to delete a content with an empty file attribute"
1846
     *
1847
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
1848
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1849
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1850
     */
1851 View Code Duplication
    public function testDeleteContentWithEmptyBinaryField()
1852
    {
1853
        $repository = $this->getRepository();
1854
1855
        $contentService = $repository->getContentService();
1856
        $locationService = $repository->getLocationService();
1857
1858
        /* BEGIN: Use Case */
1859
        $contentVersion = $this->createContentVersion1EmptyBinaryField();
1860
1861
        // Load the locations for this content object
1862
        $locations = $locationService->loadLocations($contentVersion->contentInfo);
1863
1864
        // This will delete the content, all versions and the associated locations
1865
        $contentService->deleteContent($contentVersion->contentInfo);
1866
        /* END: Use Case */
1867
1868
        foreach ($locations as $location) {
1869
            $locationService->loadLocation($location->id);
1870
        }
1871
    }
1872
1873
    /**
1874
     * Test for the loadContentDrafts() method.
1875
     *
1876
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
1877
     */
1878
    public function testLoadContentDraftsReturnsEmptyArrayByDefault()
1879
    {
1880
        $repository = $this->getRepository();
1881
1882
        /* BEGIN: Use Case */
1883
        $contentService = $repository->getContentService();
1884
1885
        $contentDrafts = $contentService->loadContentDrafts();
1886
        /* END: Use Case */
1887
1888
        $this->assertSame(array(), $contentDrafts);
1889
    }
1890
1891
    /**
1892
     * Test for the loadContentDrafts() method.
1893
     *
1894
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
1895
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
1896
     */
1897
    public function testLoadContentDrafts()
1898
    {
1899
        $repository = $this->getRepository();
1900
1901
        /* BEGIN: Use Case */
1902
        // Remote ids of the "Media" and the "eZ Publish Demo Design ..." page
1903
        // of a eZ Publish demo installation.
1904
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1905
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
1906
1907
        $contentService = $repository->getContentService();
1908
1909
        // "Media" content object
1910
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
1911
1912
        // "eZ Publish Demo Design ..." content object
1913
        $demoDesignContentInfo = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
1914
1915
        // Create some drafts
1916
        $contentService->createContentDraft($mediaContentInfo);
1917
        $contentService->createContentDraft($demoDesignContentInfo);
1918
1919
        // Now $contentDrafts should contain two drafted versions
1920
        $draftedVersions = $contentService->loadContentDrafts();
1921
        /* END: Use Case */
1922
1923
        $actual = array(
1924
            $draftedVersions[0]->status,
1925
            $draftedVersions[0]->getContentInfo()->remoteId,
1926
            $draftedVersions[1]->status,
1927
            $draftedVersions[1]->getContentInfo()->remoteId,
1928
        );
1929
        sort($actual, SORT_STRING);
1930
1931
        $this->assertEquals(
1932
            array(
1933
                VersionInfo::STATUS_DRAFT,
1934
                VersionInfo::STATUS_DRAFT,
1935
                $demoDesignRemoteId,
1936
                $mediaRemoteId,
1937
            ),
1938
            $actual
1939
        );
1940
    }
1941
1942
    /**
1943
     * Test for the loadContentDrafts() method.
1944
     *
1945
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
1946
     */
1947
    public function testLoadContentDraftsWithFirstParameter()
1948
    {
1949
        $repository = $this->getRepository();
1950
1951
        /* BEGIN: Use Case */
1952
        $user = $this->createUserVersion1();
1953
1954
        // Get current user
1955
        $oldCurrentUser = $repository->getCurrentUser();
1956
1957
        // Set new editor as user
1958
        $repository->setCurrentUser($user);
1959
1960
        // Remote id of the "Media" content object in an eZ Publish demo installation.
1961
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1962
1963
        $contentService = $repository->getContentService();
1964
1965
        // "Media" content object
1966
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
1967
1968
        // Create a content draft
1969
        $contentService->createContentDraft($mediaContentInfo);
1970
1971
        // Reset to previous current user
1972
        $repository->setCurrentUser($oldCurrentUser);
1973
1974
        // Now $contentDrafts for the previous current user and the new user
1975
        $newCurrentUserDrafts = $contentService->loadContentDrafts($user);
1976
        $oldCurrentUserDrafts = $contentService->loadContentDrafts($oldCurrentUser);
1977
        /* END: Use Case */
1978
1979
        $this->assertSame(array(), $oldCurrentUserDrafts);
1980
1981
        $this->assertEquals(
1982
            array(
1983
                VersionInfo::STATUS_DRAFT,
1984
                $mediaRemoteId,
1985
            ),
1986
            array(
1987
                $newCurrentUserDrafts[0]->status,
1988
                $newCurrentUserDrafts[0]->getContentInfo()->remoteId,
1989
            )
1990
        );
1991
    }
1992
1993
    /**
1994
     * Test for the loadVersionInfo() method.
1995
     *
1996
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
1997
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
1998
     */
1999 View Code Duplication
    public function testLoadVersionInfoWithSecondParameter()
2000
    {
2001
        $repository = $this->getRepository();
2002
2003
        $contentService = $repository->getContentService();
2004
2005
        /* BEGIN: Use Case */
2006
        $publishedContent = $this->createContentVersion1();
2007
2008
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
0 ignored issues
show
Unused Code introduced by
$draftContent is not used, you could remove the assignment.

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

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

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

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

Loading history...
2009
2010
        // Will return the VersionInfo of the $draftContent
2011
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2012
        /* END: Use Case */
2013
2014
        $this->assertEquals(2, $versionInfo->versionNo);
2015
2016
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2017
        $this->assertEquals(
2018
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2019
            $versionInfo->getContentInfo()->mainLocationId
2020
        );
2021
    }
2022
2023
    /**
2024
     * Test for the loadVersionInfo() method.
2025
     *
2026
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
2027
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2028
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2029
     */
2030
    public function testLoadVersionInfoThrowsNotFoundExceptionWithSecondParameter()
2031
    {
2032
        $repository = $this->getRepository();
2033
2034
        $contentService = $repository->getContentService();
2035
2036
        /* BEGIN: Use Case */
2037
        $draft = $this->createContentDraftVersion1();
2038
2039
        // This call will fail with a "NotFoundException", because not versionNo
2040
        // 2 exists for this content object.
2041
        $contentService->loadVersionInfo($draft->contentInfo, 2);
2042
        /* END: Use Case */
2043
    }
2044
2045
    /**
2046
     * Test for the loadVersionInfoById() method.
2047
     *
2048
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2049
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
2050
     */
2051 View Code Duplication
    public function testLoadVersionInfoByIdWithSecondParameter()
2052
    {
2053
        $repository = $this->getRepository();
2054
2055
        $contentService = $repository->getContentService();
2056
2057
        /* BEGIN: Use Case */
2058
        $publishedContent = $this->createContentVersion1();
2059
2060
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
0 ignored issues
show
Unused Code introduced by
$draftContent is not used, you could remove the assignment.

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

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

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

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

Loading history...
2061
2062
        // Will return the VersionInfo of the $draftContent
2063
        $versionInfo = $contentService->loadVersionInfoById($publishedContent->id, 2);
2064
        /* END: Use Case */
2065
2066
        $this->assertEquals(2, $versionInfo->versionNo);
2067
2068
        // Check that ContentInfo contained in VersionInfo has correct main Location id set
2069
        $this->assertEquals(
2070
            $publishedContent->getVersionInfo()->getContentInfo()->mainLocationId,
2071
            $versionInfo->getContentInfo()->mainLocationId
2072
        );
2073
    }
2074
2075
    /**
2076
     * Test for the loadVersionInfoById() method.
2077
     *
2078
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
2079
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2080
     */
2081 View Code Duplication
    public function testLoadVersionInfoByIdThrowsNotFoundExceptionWithSecondParameter()
2082
    {
2083
        $repository = $this->getRepository();
2084
2085
        $contentService = $repository->getContentService();
2086
2087
        /* BEGIN: Use Case */
2088
        $content = $this->createContentVersion1();
2089
2090
        // This call will fail with a "NotFoundException", because not versionNo
2091
        // 2 exists for this content object.
2092
        $contentService->loadVersionInfoById($content->id, 2);
2093
        /* END: Use Case */
2094
    }
2095
2096
    /**
2097
     * Test for the loadContentByVersionInfo() method.
2098
     *
2099
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
2100
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
2101
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
2102
     */
2103
    public function testLoadContentByVersionInfoWithSecondParameter()
2104
    {
2105
        $repository = $this->getRepository();
2106
2107
        $sectionId = $this->generateId('section', 1);
2108
        /* BEGIN: Use Case */
2109
        $contentTypeService = $repository->getContentTypeService();
2110
2111
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2112
2113
        $contentService = $repository->getContentService();
2114
2115
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2116
2117
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2118
2119
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2120
2121
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2122
        // $sectionId contains the ID of section 1
2123
        $contentCreateStruct->sectionId = $sectionId;
2124
        $contentCreateStruct->alwaysAvailable = true;
2125
2126
        // Create a new content draft
2127
        $content = $contentService->createContent($contentCreateStruct);
2128
2129
        // Now publish this draft
2130
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2131
2132
        // Will return a content instance with fields in "eng-US"
2133
        $reloadedContent = $contentService->loadContentByVersionInfo(
2134
            $publishedContent->getVersionInfo(),
2135
            array(
2136
                'eng-GB',
2137
            ),
2138
            false
2139
        );
2140
        /* END: Use Case */
2141
2142
        $actual = array();
2143 View Code Duplication
        foreach ($reloadedContent->getFields() as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2144
            $actual[] = new Field(
2145
                array(
2146
                    'id' => 0,
2147
                    'value' => ($field->value !== null ? true : null), // Actual value tested by FieldType integration tests
0 ignored issues
show
Documentation introduced by
The property $value is declared protected in eZ\Publish\API\Repository\Values\Content\Field. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

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

<?php

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

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

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

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

}

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

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

See also the PhpDoc documentation for @property.

Loading history...
2148
                    'languageCode' => $field->languageCode,
2149
                    'fieldDefIdentifier' => $field->fieldDefIdentifier,
2150
                )
2151
            );
2152
        }
2153
        usort(
2154
            $actual,
2155 View Code Duplication
            function ($field1, $field2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2156
                if (0 === ($return = strcasecmp($field1->fieldDefIdentifier, $field2->fieldDefIdentifier))) {
2157
                    return strcasecmp($field1->languageCode, $field2->languageCode);
2158
                }
2159
2160
                return $return;
2161
            }
2162
        );
2163
2164
        $expected = array(
2165
            new Field(
2166
                array(
2167
                    'id' => 0,
2168
                    'value' => true,
2169
                    'languageCode' => 'eng-GB',
2170
                    'fieldDefIdentifier' => 'description',
2171
                )
2172
            ),
2173
            new Field(
2174
                array(
2175
                    'id' => 0,
2176
                    'value' => true,
2177
                    'languageCode' => 'eng-GB',
2178
                    'fieldDefIdentifier' => 'name',
2179
                )
2180
            ),
2181
        );
2182
2183
        $this->assertEquals($expected, $actual);
2184
    }
2185
2186
    /**
2187
     * Test for the loadContentByContentInfo() method.
2188
     *
2189
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages)
2190
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2191
     */
2192
    public function testLoadContentByContentInfoWithLanguageParameters()
2193
    {
2194
        $repository = $this->getRepository();
2195
2196
        $sectionId = $this->generateId('section', 1);
2197
        /* BEGIN: Use Case */
2198
        $contentTypeService = $repository->getContentTypeService();
2199
2200
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
2201
2202
        $contentService = $repository->getContentService();
2203
2204
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
2205
2206
        $contentCreateStruct->setField('name', 'Sindelfingen forum²');
2207
2208
        $contentCreateStruct->setField('name', 'Sindelfingen forum²³', 'eng-GB');
2209
2210
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
2211
        // $sectionId contains the ID of section 1
2212
        $contentCreateStruct->sectionId = $sectionId;
2213
        $contentCreateStruct->alwaysAvailable = true;
2214
2215
        // Create a new content draft
2216
        $content = $contentService->createContent($contentCreateStruct);
2217
2218
        // Now publish this draft
2219
        $publishedContent = $contentService->publishVersion($content->getVersionInfo());
2220
2221
        // Will return a content instance with fields in "eng-US"
2222
        $reloadedContent = $contentService->loadContentByContentInfo(
2223
            $publishedContent->contentInfo,
2224
            array(
2225
                'eng-US',
2226
            ),
2227
            null,
2228
            false
2229
        );
2230
        /* END: Use Case */
2231
2232
        $actual = $this->normalizeFields($reloadedContent->getFields());
2233
2234
        $expected = array(
2235
            new Field(
2236
                array(
2237
                    'id' => 0,
2238
                    'value' => true,
2239
                    'languageCode' => 'eng-US',
2240
                    'fieldDefIdentifier' => 'description',
2241
                )
2242
            ),
2243
            new Field(
2244
                array(
2245
                    'id' => 0,
2246
                    'value' => true,
2247
                    'languageCode' => 'eng-US',
2248
                    'fieldDefIdentifier' => 'name',
2249
                )
2250
            ),
2251
        );
2252
2253
        $this->assertEquals($expected, $actual);
2254
2255
        // Will return a content instance with fields in "eng-GB" (versions prior to 6.0.0-beta9 returned "eng-US" also)
2256
        $reloadedContent = $contentService->loadContentByContentInfo(
2257
            $publishedContent->contentInfo,
2258
            array(
2259
                'eng-GB',
2260
            ),
2261
            null,
2262
            true
2263
        );
2264
2265
        $actual = $this->normalizeFields($reloadedContent->getFields());
2266
2267
        $expected = array(
2268
            new Field(
2269
                array(
2270
                    'id' => 0,
2271
                    'value' => true,
2272
                    'languageCode' => 'eng-GB',
2273
                    'fieldDefIdentifier' => 'description',
2274
                )
2275
            ),
2276
            new Field(
2277
                array(
2278
                    'id' => 0,
2279
                    'value' => true,
2280
                    'languageCode' => 'eng-GB',
2281
                    'fieldDefIdentifier' => 'name',
2282
                )
2283
            ),
2284
        );
2285
2286
        $this->assertEquals($expected, $actual);
2287
2288
        // Will return a content instance with fields in main language "eng-US", as "fre-FR" does not exists
2289
        $reloadedContent = $contentService->loadContentByContentInfo(
2290
            $publishedContent->contentInfo,
2291
            array(
2292
                'fre-FR',
2293
            ),
2294
            null,
2295
            true
2296
        );
2297
2298
        $actual = $this->normalizeFields($reloadedContent->getFields());
2299
2300
        $expected = array(
2301
            new Field(
2302
                array(
2303
                    'id' => 0,
2304
                    'value' => true,
2305
                    'languageCode' => 'eng-US',
2306
                    'fieldDefIdentifier' => 'description',
2307
                )
2308
            ),
2309
            new Field(
2310
                array(
2311
                    'id' => 0,
2312
                    'value' => true,
2313
                    'languageCode' => 'eng-US',
2314
                    'fieldDefIdentifier' => 'name',
2315
                )
2316
            ),
2317
        );
2318
2319
        $this->assertEquals($expected, $actual);
2320
    }
2321
2322
    /**
2323
     * Test for the loadContentByContentInfo() method.
2324
     *
2325
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2326
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
2327
     */
2328 View Code Duplication
    public function testLoadContentByContentInfoWithVersionNumberParameter()
2329
    {
2330
        $repository = $this->getRepository();
2331
2332
        $contentService = $repository->getContentService();
2333
2334
        /* BEGIN: Use Case */
2335
        $publishedContent = $this->createContentVersion1();
2336
2337
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
0 ignored issues
show
Unused Code introduced by
$draftContent is not used, you could remove the assignment.

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

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

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

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

Loading history...
2338
2339
        // This content instance is identical to $draftContent
2340
        $draftContentReloaded = $contentService->loadContentByContentInfo(
2341
            $publishedContent->contentInfo,
2342
            null,
2343
            2
2344
        );
2345
        /* END: Use Case */
2346
2347
        $this->assertEquals(
2348
            2,
2349
            $draftContentReloaded->getVersionInfo()->versionNo
2350
        );
2351
2352
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2353
        $this->assertEquals(
2354
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2355
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2356
        );
2357
    }
2358
2359
    /**
2360
     * Test for the loadContentByContentInfo() method.
2361
     *
2362
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
2363
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2364
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithThirdParameter
2365
     */
2366 View Code Duplication
    public function testLoadContentByContentInfoThrowsNotFoundExceptionWithVersionNumberParameter()
2367
    {
2368
        $repository = $this->getRepository();
2369
2370
        $contentService = $repository->getContentService();
2371
2372
        /* BEGIN: Use Case */
2373
        $content = $this->createContentVersion1();
2374
2375
        // This call will fail with a "NotFoundException", because no content
2376
        // with versionNo = 2 exists.
2377
        $contentService->loadContentByContentInfo($content->contentInfo, null, 2);
2378
        /* END: Use Case */
2379
    }
2380
2381
    /**
2382
     * Test for the loadContent() method.
2383
     *
2384
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages)
2385
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2386
     */
2387
    public function testLoadContentWithSecondParameter()
2388
    {
2389
        $repository = $this->getRepository();
2390
2391
        $contentService = $repository->getContentService();
2392
2393
        /* BEGIN: Use Case */
2394
        $draft = $this->createMultipleLanguageDraftVersion1();
2395
2396
        // This draft contains those fields localized with "eng-GB"
2397
        $draftLocalized = $contentService->loadContent($draft->id, array('eng-GB'), null, false);
2398
        /* END: Use Case */
2399
2400
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2401
    }
2402
2403
    /**
2404
     * Test for the loadContent() method.
2405
     *
2406
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2407
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2408
     */
2409 View Code Duplication
    public function testLoadContentWithThirdParameter()
2410
    {
2411
        $repository = $this->getRepository();
2412
2413
        $contentService = $repository->getContentService();
2414
2415
        /* BEGIN: Use Case */
2416
        $publishedContent = $this->createContentVersion1();
2417
2418
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
0 ignored issues
show
Unused Code introduced by
$draftContent is not used, you could remove the assignment.

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

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

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

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

Loading history...
2419
2420
        // This content instance is identical to $draftContent
2421
        $draftContentReloaded = $contentService->loadContent($publishedContent->id, null, 2);
2422
        /* END: Use Case */
2423
2424
        $this->assertEquals(2, $draftContentReloaded->getVersionInfo()->versionNo);
2425
2426
        // Check that ContentInfo contained in reloaded draft Content has correct main Location id set
2427
        $this->assertEquals(
2428
            $publishedContent->versionInfo->contentInfo->mainLocationId,
2429
            $draftContentReloaded->versionInfo->contentInfo->mainLocationId
2430
        );
2431
    }
2432
2433
    /**
2434
     * Test for the loadContent() method.
2435
     *
2436
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
2437
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
2438
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithThirdParameter
2439
     */
2440 View Code Duplication
    public function testLoadContentThrowsNotFoundExceptionWithThirdParameter()
2441
    {
2442
        $repository = $this->getRepository();
2443
2444
        $contentService = $repository->getContentService();
2445
2446
        /* BEGIN: Use Case */
2447
        $content = $this->createContentVersion1();
2448
2449
        // This call will fail with a "NotFoundException", because for this
2450
        // content object no versionNo=2 exists.
2451
        $contentService->loadContent($content->id, null, 2);
2452
        /* END: Use Case */
2453
    }
2454
2455
    /**
2456
     * Test for the loadContentByRemoteId() method.
2457
     *
2458
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages)
2459
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2460
     */
2461
    public function testLoadContentByRemoteIdWithSecondParameter()
2462
    {
2463
        $repository = $this->getRepository();
2464
2465
        $contentService = $repository->getContentService();
2466
2467
        /* BEGIN: Use Case */
2468
        $draft = $this->createMultipleLanguageDraftVersion1();
2469
2470
        $contentService->publishVersion($draft->versionInfo);
2471
2472
        // This draft contains those fields localized with "eng-GB"
2473
        $draftLocalized = $contentService->loadContentByRemoteId(
2474
            $draft->contentInfo->remoteId,
2475
            array('eng-GB'),
2476
            null,
2477
            false
2478
        );
2479
        /* END: Use Case */
2480
2481
        $this->assertLocaleFieldsEquals($draftLocalized->getFields(), 'eng-GB');
2482
    }
2483
2484
    /**
2485
     * Test for the loadContentByRemoteId() method.
2486
     *
2487
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
2488
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersionFromContentDraft
2489
     */
2490 View Code Duplication
    public function testLoadContentByRemoteIdWithThirdParameter()
2491
    {
2492
        $repository = $this->getRepository();
2493
2494
        $contentService = $repository->getContentService();
2495
2496
        /* BEGIN: Use Case */
2497
        $publishedContent = $this->createContentVersion1();
2498
2499
        $draftContent = $contentService->createContentDraft($publishedContent->contentInfo);
0 ignored issues
show
Unused Code introduced by
$draftContent is not used, you could remove the assignment.

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

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

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

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

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