Completed
Push — ezp30878_cant_add_image_with_p... ( e19ea7...263f1b )
by
unknown
20:16
created

ContentBase::testLoadContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Repository\Tests\Service\Integration\ContentBase class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Repository\Tests\Service\Integration;
10
11
use eZ\Publish\Core\Repository\Tests\Service\Integration\Base as BaseServiceTest;
12
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
13
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
14
use eZ\Publish\API\Repository\Values\Content\Content as APIContent;
15
use eZ\Publish\API\Repository\Values\Content\Relation;
16
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
17
use eZ\Publish\API\Repository\Tests\BaseTest as APIBaseTest;
18
19
/**
20
 * Test case for Content service.
21
 */
22
abstract class ContentBase extends BaseServiceTest
23
{
24
    protected $testContentType;
25
26
    protected function getContentInfoExpectedValues()
27
    {
28
        // Legacy fixture content ID=4 values
29
        return [
30
            'id' => 4,
31
            'name' => 'Users',
32
            'sectionId' => 2,
33
            'currentVersionNo' => 1,
34
            'published' => true,
35
            'ownerId' => 14,
36
            'modificationDate' => $this->getDateTime(1033917596),
37
            'publishedDate' => $this->getDateTime(1033917596),
38
            'alwaysAvailable' => true,
39
            'remoteId' => 'f5c88a2209584891056f987fd965b0ba',
40
            'mainLanguageCode' => 'eng-US',
41
            'mainLocationId' => 5,
42
        ];
43
    }
44
45
    /**
46
     * @param bool $draft
47
     *
48
     * @return array
49
     */
50
    protected function getVersionInfoExpectedValues($draft = false)
51
    {
52
        // Legacy fixture content 4 current version (1) values
53
        $values = [
54
            //"id" => 4,
55
            'versionNo' => 1,
56
            'modificationDate' => $this->getDateTime(0),
57
            'creatorId' => 14,
58
            'creationDate' => $this->getDateTime(0),
59
            'status' => VersionInfo::STATUS_PUBLISHED,
60
            'initialLanguageCode' => 'eng-US',
61
            'languageCodes' => ['eng-US'],
62
            // Implementation properties
63
            'names' => ['eng-US' => 'Users'],
64
        ];
65
66
        if ($draft) {
67
            //$values["id"] = 675;
68
            $values['creatorId'] = $this->repository->getCurrentUserReference()->getUserId();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...tCurrentUserReference() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user reference.

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...
69
            $values['versionNo'] = 2;
70
            $values['status'] = VersionInfo::STATUS_DRAFT;
71
            unset($values['modificationDate']);
72
            unset($values['creationDate']);
73
        }
74
75
        return $values;
76
    }
77
78
    /**
79
     * @param array $languages
80
     *
81
     * @return mixed
82
     */
83
    protected function getFieldValuesExpectedValues(array $languages = null)
84
    {
85
        // Legacy fixture content ID=4 field values
86
        $fieldValues = [
87
            'eng-US' => [
88
                'name' => ['eng-US' => 'Users'],
89
                'description' => ['eng-US' => 'Main group'],
90
            ],
91
        ];
92
93
        $returnArray = [];
94
        foreach ($fieldValues as $languageCode => $languageFieldValues) {
95
            if (!empty($languages) && !in_array($languageCode, $languages)) {
96
                continue;
97
            }
98
            $returnArray = array_merge_recursive($returnArray, $languageFieldValues);
99
        }
100
101
        return $returnArray;
102
    }
103
104
    protected function getExpectedContentType()
105
    {
106
    }
107
108
    /**
109
     * Test for the loadContentInfo() method.
110
     *
111
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContentInfo
112
     *
113
     * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo
114
     */
115
    public function testLoadContentInfo()
116
    {
117
        /* BEGIN: Use Case */
118
        $contentService = $this->repository->getContentService();
119
120
        $contentInfo = $contentService->loadContentInfo(4);
121
        /* END: Use Case */
122
123
        $this->assertInstanceOf(
124
            'eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
125
            $contentInfo
126
        );
127
128
        return $contentInfo;
129
    }
130
131
    /**
132
     * Test for the loadContentInfo() method.
133
     *
134
     * @depends testLoadContentInfo
135
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContentInfo
136
     *
137
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
138
     */
139
    public function testLoadContentInfoValues($contentInfo)
140
    {
141
        $this->assertPropertiesCorrect(
142
            $this->getContentInfoExpectedValues(),
143
            $contentInfo
144
        );
145
    }
146
147
    /**
148
     * Test for the loadContentInfo() method.
149
     *
150
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContentInfo
151
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
152
     */
153
    public function testLoadContentInfoThrowsNotFoundException()
154
    {
155
        /* BEGIN: Use Case */
156
        $contentService = $this->repository->getContentService();
157
158
        // Throws an exception because given contentId does not exist
159
        $contentInfo = $contentService->loadContentInfo(APIBaseTest::DB_INT_MAX);
0 ignored issues
show
Unused Code introduced by
$contentInfo 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...
160
        /* END: Use Case */
161
    }
162
163
    /**
164
     * Test for the loadContentInfo() method.
165
     *
166
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContentInfo
167
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
168
     */
169
    public function testLoadContentInfoThrowsUnauthorizedException()
170
    {
171
        // Set anonymous as current user
172
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
173
174
        $this->repository->getContentService()->loadContentInfo(4);
175
    }
176
177
    /**
178
     * Test for the loadContentInfoByRemoteId() method.
179
     *
180
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContentInfoByRemoteId
181
     *
182
     * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo
183
     */
184
    public function testLoadContentInfoByRemoteId()
185
    {
186
        /* BEGIN: Use Case */
187
        $contentService = $this->repository->getContentService();
188
189
        $contentInfo = $contentService->loadContentInfoByRemoteId('f5c88a2209584891056f987fd965b0ba');
190
        /* END: Use Case */
191
192
        $this->assertInstanceOf(
193
            'eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo',
194
            $contentInfo
195
        );
196
197
        return $contentInfo;
198
    }
199
200
    /**
201
     * Test for the loadContentInfoByRemoteId() method.
202
     *
203
     * @depends testLoadContentInfoByRemoteId
204
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContentInfoByRemoteId
205
     *
206
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
207
     */
208
    public function testLoadContentInfoByRemoteIdValues($contentInfo)
209
    {
210
        $this->assertPropertiesCorrect(
211
            $this->getContentInfoExpectedValues(),
212
            $contentInfo
213
        );
214
    }
215
216
    /**
217
     * Test for the loadContentInfoByRemoteId() method.
218
     *
219
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContentInfoByRemoteId
220
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
221
     */
222
    public function testLoadContentInfoByRemoteIdThrowsNotFoundException()
223
    {
224
        /* BEGIN: Use Case */
225
        $contentService = $this->repository->getContentService();
226
227
        // Throws an exception because remoteId does not exist
228
        $contentInfo = $contentService->loadContentInfoByRemoteId('this-remote-id-does-not-exist');
0 ignored issues
show
Unused Code introduced by
$contentInfo 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...
229
        /* END: Use Case */
230
    }
231
232
    /**
233
     * Test for the loadContentInfoByRemoteId() method.
234
     *
235
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContentInfoByRemoteId
236
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
237
     */
238
    public function testLoadContentInfoByRemoteIdThrowsUnauthorizedException()
239
    {
240
        // Set anonymous as current user
241
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
242
243
        $this->repository->getContentService()->loadContentInfoByRemoteId('f5c88a2209584891056f987fd965b0ba');
244
    }
245
246
    /**
247
     * Test for the loadVersionInfoById() method.
248
     *
249
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
250
     *
251
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo
252
     */
253 View Code Duplication
    public function testLoadVersionInfoById()
254
    {
255
        /* BEGIN: Use Case */
256
        $contentService = $this->repository->getContentService();
257
258
        $versionInfo = $contentService->loadVersionInfoById(4);
259
        /* END: Use Case */
260
261
        $this->assertInstanceOf(
262
            'eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
263
            $versionInfo
264
        );
265
266
        return $versionInfo;
267
    }
268
269
    /**
270
     * Test for the loadVersionInfoById() method.
271
     *
272
     * @depends testLoadVersionInfoById
273
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
274
     *
275
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
276
     */
277
    public function testLoadVersionInfoByIdValues($versionInfo)
278
    {
279
        $this->assertPropertiesCorrect(
280
            $this->getVersionInfoExpectedValues(),
281
            $versionInfo
282
        );
283
    }
284
285
    /**
286
     * Test for the loadVersionInfoById() method.
287
     *
288
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
289
     *
290
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo
291
     */
292 View Code Duplication
    public function testLoadVersionInfoByIdWithSecondParameter()
293
    {
294
        /* BEGIN: Use Case */
295
        $contentService = $this->repository->getContentService();
296
297
        $versionInfo = $contentService->loadVersionInfoById(4, 1);
298
        /* END: Use Case */
299
300
        $this->assertInstanceOf(
301
            'eZ\\Publish\\API\\Repository\\Values\\Content\\VersionInfo',
302
            $versionInfo
303
        );
304
305
        return $versionInfo;
306
    }
307
308
    /**
309
     * Test for the loadVersionInfoById() method.
310
     *
311
     * @depends testLoadVersionInfoByIdWithSecondParameter
312
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
313
     *
314
     * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
315
     */
316
    public function testLoadVersionInfoByIdWithSecondParameterValues($versionInfo)
317
    {
318
        $this->assertPropertiesCorrect(
319
            $this->getVersionInfoExpectedValues(),
320
            $versionInfo
321
        );
322
    }
323
324
    /**
325
     * Test for the loadVersionInfoById() method.
326
     *
327
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
328
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
329
     */
330
    public function testLoadVersionInfoByIdThrowsNotFoundException()
331
    {
332
        /* BEGIN: Use Case */
333
        $contentService = $this->repository->getContentService();
334
335
        // Throws an exception because version with given number does not exists
336
        $versionInfo = $contentService->loadVersionInfoById(4, APIBaseTest::DB_INT_MAX);
0 ignored issues
show
Unused Code introduced by
$versionInfo 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...
337
        /* END: Use Case */
338
    }
339
340
    /**
341
     * Test for the loadVersionInfoById() method.
342
     *
343
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersionInfoById
344
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
345
     */
346
    public function testLoadVersionInfoByIdThrowsUnauthorizedException()
347
    {
348
        // Set anonymous as current user
349
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
350
351
        $this->repository->getContentService()->loadVersionInfoById(4);
352
    }
353
354
    /**
355
     * Data provider for testLoadContent().
356
     *
357
     * @return array
358
     */
359
    public function testLoadContentArgumentsProvider()
360
    {
361
        return [
362
            [4, null, null],
363
            [4, ['eng-US'], null],
364
            [4, ['eng-US', 'fre-FR'], null],
365
            [4, null, 1],
366
            [4, ['eng-US', 'fre-FR', 'nor-NO', 'eng-DE'], 1],
367
            [4, ['eng-US'], 1],
368
        ];
369
    }
370
371
    /**
372
     * Test for the loadContent() method.
373
     *
374
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContent
375
     * @dataProvider testLoadContentArgumentsProvider
376
     *
377
     * @param int $contentId
378
     * @param array $languages
379
     * @param int $versionNo
380
     *
381
     * @return \eZ\Publish\API\Repository\Values\Content\Content
382
     */
383 View Code Duplication
    public function testLoadContent($contentId, array $languages = null, $versionNo = null)
384
    {
385
        /* BEGIN: Use Case */
386
        $contentService = $this->repository->getContentService();
387
388
        $content = $contentService->loadContent($contentId, $languages, $versionNo);
389
        /* END: Use Case */
390
391
        $this->assertInstanceOf(
392
            'eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
393
            $content
394
        );
395
396
        $this->assertContentValues($content, $languages);
397
    }
398
399
    /**
400
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
401
     * @param array $languages
402
     * @param bool $draft
403
     */
404
    protected function assertContentValues(APIContent $content, array $languages = null, $draft = false)
405
    {
406
        $versionInfoValues = $this->getVersionInfoExpectedValues($draft);
407
        $contentInfoValues = $this->getContentInfoExpectedValues();
408
        $fieldValuesValues = $this->getFieldValuesExpectedValues($languages);
409
410
        $this->assertPropertiesCorrect(
411
            $versionInfoValues,
412
            $content->getVersionInfo()
413
        );
414
415
        $this->assertPropertiesCorrect(
416
            $contentInfoValues,
417
            $content->contentInfo
418
        );
419
420
        $this->assertEquals(
421
            $fieldValuesValues,
422
            $content->fields
423
        );
424
425
        // @todo assert relations
426
427
        $this->assertEquals($content->id, $content->contentInfo->id);
428
    }
429
430
    /**
431
     * Test for the loadContent() method.
432
     *
433
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContent
434
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
435
     */
436
    public function testLoadContentThrowsUnauthorizedException()
437
    {
438
        // Set anonymous as current user
439
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
440
441
        $this->repository->getContentService()->loadContent(4);
442
    }
443
444
    /**
445
     * Test for the loadContent() method.
446
     *
447
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContent
448
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
449
     */
450 View Code Duplication
    public function testLoadContentWithVersionThrowsUnauthorizedException()
451
    {
452
        list($draft) = $this->createTestContent();
453
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
454
455
        $this->repository->getContentService()->loadContent(
456
            $draft->id,
457
            null,
458
            $draft->versionInfo->versionNo
459
        );
460
    }
461
462
    /**
463
     * Test for the loadContent() method.
464
     *
465
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContent
466
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
467
     */
468
    public function testLoadContentThrowsNotFoundExceptionContentNotFound()
469
    {
470
        /* BEGIN: Use Case */
471
        $contentService = $this->repository->getContentService();
472
473
        // Throws an exception because content with id APIBaseTest::DB_INT_MAX does not exist
474
        $content = $contentService->loadContent(APIBaseTest::DB_INT_MAX);
0 ignored issues
show
Unused Code introduced by
$content 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...
475
        /* END: Use Case */
476
    }
477
478
    /**
479
     * Test for the loadContent() method.
480
     *
481
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContent
482
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
483
     */
484
    public function testLoadContentThrowsNotFoundExceptionVersionNotFound()
485
    {
486
        /* BEGIN: Use Case */
487
        $contentService = $this->repository->getContentService();
488
489
        // Throws an exception because version number APIBaseTest::DB_INT_MAX for content with id 4 does not exist
490
        $content = $contentService->loadContent(4, null, APIBaseTest::DB_INT_MAX);
0 ignored issues
show
Unused Code introduced by
$content 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...
491
        /* END: Use Case */
492
    }
493
494
    /**
495
     * Test for the loadContent() method.
496
     *
497
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContent
498
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
499
     */
500
    public function testLoadContentThrowsNotFoundExceptionLanguageNotFound()
501
    {
502
        /* BEGIN: Use Case */
503
        $contentService = $this->repository->getContentService();
504
505
        // Throws an exception because content does not exists in "fre-FR" language
506
        $content = $contentService->loadContent(4, ['fre-FR'], null, false);
0 ignored issues
show
Unused Code introduced by
$content 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...
507
        /* END: Use Case */
508
    }
509
510
    /**
511
     * Test for the loadContent() method.
512
     *
513
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContent
514
     */
515
    public function testLoadContentThrowsNotFoundExceptionLanguageNotFoundVariation()
516
    {
517
        /* BEGIN: Use Case */
518
        $contentService = $this->repository->getContentService();
519
520
        // Content only exists in eng-US, so we should only have it in eng-US.
521
        $content = $contentService->loadContent(4, ['eng-US', 'eng-GB']);
522
        $this->assertInstanceOf(
523
            'eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
524
            $content
525
        );
526
        $this->assertContentValues($content, ['eng-US']);
527
        /* END: Use Case */
528
    }
529
530
    /**
531
     * Data provider for testLoadContentByRemoteId().
532
     *
533
     * @return array
534
     */
535
    public function testLoadContentByRemoteIdArgumentsProvider()
536
    {
537
        return [
538
            ['f5c88a2209584891056f987fd965b0ba', null, null],
539
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], null],
540
            ['f5c88a2209584891056f987fd965b0ba', null, 1],
541
            ['f5c88a2209584891056f987fd965b0ba', ['eng-US'], 1],
542
        ];
543
    }
544
545
    /**
546
     * Test for the loadContentByRemoteId() method.
547
     *
548
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContentByRemoteId
549
     * @dataProvider testLoadContentByRemoteIdArgumentsProvider
550
     *
551
     * @param string $remoteId
552
     * @param array|null $languages
553
     * @param int $versionNo
554
     *
555
     * @return \eZ\Publish\API\Repository\Values\Content\Content
556
     */
557 View Code Duplication
    public function testLoadContentByRemoteId($remoteId, $languages, $versionNo)
558
    {
559
        /* BEGIN: Use Case */
560
        $contentService = $this->repository->getContentService();
561
562
        $content = $contentService->loadContentByRemoteId($remoteId, $languages, $versionNo);
563
        /* END: Use Case */
564
565
        $this->assertInstanceOf(
566
            'eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
567
            $content
568
        );
569
570
        $this->assertContentValues($content, $languages);
571
    }
572
573
    /**
574
     * Test for the loadContentByRemoteId() method.
575
     *
576
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContentByRemoteId
577
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
578
     */
579
    public function testLoadContentByRemoteIdThrowsNotFoundException()
580
    {
581
        /* BEGIN: Use Case */
582
        $contentService = $this->repository->getContentService();
583
584
        // Throws an exception because given remoteId does not exist
585
        $content = $contentService->loadContentByRemoteId('non-existent-remote-id');
0 ignored issues
show
Unused Code introduced by
$content 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...
586
        /* END: Use Case */
587
    }
588
589
    /**
590
     * Test for the loadContentByRemoteId() method.
591
     *
592
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContentByRemoteId
593
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
594
     */
595
    public function testLoadContentByRemoteIdThrowsUnauthorizedException()
596
    {
597
        // Set anonymous as current user
598
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
599
600
        $this->repository->getContentService()->loadContentByRemoteId('f5c88a2209584891056f987fd965b0ba');
601
    }
602
603
    /**
604
     * Test for the loadContentByRemoteId() method.
605
     *
606
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContentByRemoteId
607
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
608
     */
609
    public function testLoadContentByRemoteIdWithVersionThrowsUnauthorizedException()
610
    {
611
        $contentService = $this->repository->getContentService();
612
613
        $content = $contentService->loadContent(4);
614
        $draft = $contentService->createContentDraft($content->contentInfo);
615
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
616
617
        $contentService->loadContentByRemoteId(
618
            $draft->contentInfo->remoteId,
619
            null,
620
            $draft->versionInfo->versionNo
621
        );
622
    }
623
624
    /**
625
     * Test for the newContentCreateStruct() method.
626
     *
627
     * @covers \eZ\Publish\Core\Repository\ContentService::newContentCreateStruct
628
     *
629
     * @return \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct
630
     */
631
    public function testNewContentCreateStruct()
632
    {
633
        /* BEGIN: Use Case */
634
        $contentService = $this->repository->getContentService();
635
        $contentTypeService = $this->repository->getContentTypeService();
636
637
        $folderContentType = $contentTypeService->loadContentType(1);
638
639
        $contentCreateStruct = $contentService->newContentCreateStruct(
640
            $folderContentType,
641
            'eng-GB'
642
        );
643
        /* END: Use Case */
644
645
        $this->assertInstanceOf(
646
            'eZ\\Publish\\API\\Repository\\Values\\Content\\ContentCreateStruct',
647
            $contentCreateStruct
648
        );
649
650
        return [
651
            'contentType' => $folderContentType,
652
            'contentCreateStruct' => $contentCreateStruct,
653
        ];
654
    }
655
656
    /**
657
     * Test for the newContentCreateStruct() method.
658
     *
659
     * @depends testNewContentCreateStruct
660
     * @covers \eZ\Publish\Core\Repository\ContentService::newContentCreateStruct
661
     *
662
     * @param array $data
663
     */
664
    public function testNewContentCreateStructValues(array $data)
665
    {
666
        $contentType = $data['contentType'];
667
        $contentCreateStruct = $data['contentCreateStruct'];
668
669
        $expectedValues = [
670
            'fields' => [],
671
            'contentType' => $contentType,
672
            'sectionId' => null,
673
            'ownerId' => null,
674
            'alwaysAvailable' => $contentType->defaultAlwaysAvailable,
675
            'remoteId' => null,
676
            'mainLanguageCode' => 'eng-GB',
677
            'modificationDate' => null,
678
        ];
679
680
        $this->assertPropertiesCorrect(
681
            $expectedValues,
682
            $contentCreateStruct
683
        );
684
    }
685
686
    /**
687
     * Test for the createContent() method.
688
     *
689
     * @depends testNewContentCreateStruct
690
     * @covers \eZ\Publish\Core\Repository\ContentService::createContent
691
     *
692
     * @return array
693
     */
694
    public function testCreateContent()
695
    {
696
        $time = time();
697
        $testContentType = $this->createTestContentType();
698
699
        /* BEGIN: Use Case */
700
        $contentService = $this->repository->getContentService();
701
702
        $contentCreate = $contentService->newContentCreateStruct($testContentType, 'eng-GB');
703
        $contentCreate->setField('test_required_empty', 'value for field definition with empty default value');
704
        $contentCreate->setField('test_translatable', 'and thumbs opposable', 'eng-US');
705
        $contentCreate->sectionId = 1;
706
        $contentCreate->ownerId = $this->repository->getCurrentUserReference()->getUserId();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...tCurrentUserReference() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user reference.

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...
707
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
708
        $contentCreate->alwaysAvailable = true;
709
710
        $locationCreates = [
711
            new LocationCreateStruct(
712
                [
713
                    //priority = 0
714
                    //hidden = false
715
                    'remoteId' => 'db787a9143f57828dd4331573466a013',
716
                    //sortField = Location::SORT_FIELD_NAME
717
                    //sortOrder = Location::SORT_ORDER_ASC
718
                    'parentLocationId' => 2,
719
                ]
720
            ),
721
            new LocationCreateStruct(
722
                [
723
                    //priority = 0
724
                    //hidden = false
725
                    'remoteId' => 'a3dd7c1c9e04c89e446a70f647286e6b',
726
                    //sortField = Location::SORT_FIELD_NAME
727
                    //sortOrder = Location::SORT_ORDER_ASC
728
                    'parentLocationId' => 5,
729
                ]
730
            ),
731
        ];
732
733
        $contentDraft = $contentService->createContent($contentCreate, $locationCreates);
734
        /* END: Use Case */
735
736
        $this->assertInstanceOf('eZ\\Publish\\API\\Repository\\Values\\Content\\Content', $contentDraft);
737
738
        return [
739
            'expected' => $contentCreate,
740
            'actual' => $contentDraft,
741
            'loadedActual' => $contentService->loadContent($contentDraft->id, null, 1),
742
            'contentType' => $testContentType,
743
            'time' => $time,
744
        ];
745
    }
746
747
    /**
748
     * Test for the createContent() method.
749
     *
750
     * @depends testCreateContent
751
     * @covers \eZ\Publish\Core\Repository\ContentService::createContent
752
     *
753
     * @param array $data
754
     */
755
    public function testCreateContentStructValues(array $data)
756
    {
757
        $this->assertCreateContentStructValues($data);
758
    }
759
760
    /**
761
     * Test for the createContent() method.
762
     *
763
     * Because of the way ContentHandler::create() is implemented and tested in legacy storage it is also necessary to
764
     * test loaded content object, not only the one returned by ContentService::createContent
765
     *
766
     * @depends testCreateContent
767
     * @depends testLoadContent
768
     * @covers \eZ\Publish\Core\Repository\ContentService::createContent
769
     *
770
     * @param array $data
771
     */
772
    public function testCreateContentStructValuesLoaded(array $data)
773
    {
774
        $data['actual'] = $data['loadedActual'];
775
776
        $this->assertCreateContentStructValues($data);
777
    }
778
779
    /**
780
     * Test for the createContent() method.
781
     *
782
     * @covers \eZ\Publish\Core\Repository\ContentService::createContent
783
     *
784
     * @param array $data
785
     */
786
    protected function assertCreateContentStructValues(array $data)
787
    {
788
        $this->assertCreateContentStructValuesContentInfo($data);
789
        $this->assertCreateContentStructValuesVersionInfo($data);
790
        $this->assertCreateContentStructValuesRelations($data);
791
        $this->assertCreateContentStructValuesFields($data);
792
    }
793
794
    /**
795
     * Asserts that ContentInfo is valid after Content creation.
796
     *
797
     * @param array $data
798
     */
799
    protected function assertCreateContentStructValuesContentInfo(array $data)
800
    {
801
        /** @var $contentDraft \eZ\Publish\API\Repository\Values\Content\Content */
802
        $contentDraft = $data['actual'];
803
        /** @var $contentCreate \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct */
804
        $contentCreate = $data['expected'];
805
806
        $this->assertPropertiesCorrect(
807
            [
808
                'id' => $contentDraft->id,
809
                'name' => $contentCreate->fields[0]->value,
810
                'sectionId' => $contentCreate->sectionId,
811
                'currentVersionNo' => 1,
812
                'published' => false,
813
                'ownerId' => $contentCreate->ownerId,
814
                'modificationDate' => null,
815
                'publishedDate' => null,
816
                'alwaysAvailable' => $contentCreate->alwaysAvailable,
817
                'remoteId' => $contentCreate->remoteId,
818
                'mainLanguageCode' => $contentCreate->mainLanguageCode,
819
                // @todo: should be null, InMemory skips creating node assignments and creates locations right away
820
                //"mainLocationId" => null,
821
                //"contentType"
822
            ],
823
            $contentDraft->versionInfo->contentInfo
824
        );
825
        $this->assertNotNull($contentDraft->id);
826
        $this->assertEquals(
827
            $contentCreate->contentType->id,
828
            $contentDraft->versionInfo->contentInfo->contentTypeId
829
        );
830
    }
831
832
    /**
833
     * Asserts that VersionInfo is valid after Content creation.
834
     *
835
     * @param array $data
836
     */
837
    protected function assertCreateContentStructValuesVersionInfo(array $data)
838
    {
839
        /** @var $contentDraft \eZ\Publish\API\Repository\Values\Content\Content */
840
        $contentDraft = $data['actual'];
841
        /** @var $contentCreate \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct */
842
        $contentCreate = $data['expected'];
843
        $time = $data['time'];
844
845
        $this->assertPropertiesCorrect(
846
            [
847
                //"id"
848
                'versionNo' => 1,
849
                //"creationDate"
850
                //"modificationDate"
851
                'creatorId' => $contentCreate->ownerId,
852
                'status' => VersionInfo::STATUS_DRAFT,
853
                'initialLanguageCode' => $contentCreate->mainLanguageCode,
854
                //"languageCodes"
855
                'names' => [
856
                    'eng-GB' => 'value for field definition with empty default value',
857
                    'eng-US' => 'value for field definition with empty default value',
858
                ],
859
            ],
860
            $contentDraft->versionInfo
861
        );
862
863
        $languageCodes = $this->getLanguageCodesFromFields($contentCreate->fields, $contentCreate->mainLanguageCode);
864
865
        $this->assertCount(count($languageCodes), $contentDraft->versionInfo->languageCodes);
866
        foreach ($contentDraft->versionInfo->languageCodes as $languageCode) {
867
            $this->assertTrue(in_array($languageCode, $languageCodes));
868
        }
869
        $this->assertNotNull($contentDraft->versionInfo->id);
870
        $this->assertGreaterThanOrEqual($this->getDateTime($time), $contentDraft->versionInfo->creationDate);
871
        $this->assertGreaterThanOrEqual($this->getDateTime($time), $contentDraft->versionInfo->modificationDate);
872
    }
873
874
    /**
875
     * @param array $data
876
     */
877
    protected function assertCreateContentStructValuesRelations(array $data)
0 ignored issues
show
Unused Code introduced by
The parameter $data 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...
878
    {
879
        // @todo: relations not implemented yet
880
    }
881
882
    /**
883
     * Asserts that fields are valid after Content creation.
884
     *
885
     * @param array $data
886
     */
887
    protected function assertCreateContentStructValuesFields(array $data)
888
    {
889
        /** @var $contentDraft \eZ\Publish\API\Repository\Values\Content\Content */
890
        $contentDraft = $data['actual'];
891
        /** @var $contentCreate \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct */
892
        $contentCreate = $data['expected'];
893
        /** @var $contentType \eZ\Publish\API\Repository\Values\ContentType\ContentType */
894
        $contentType = $data['contentType'];
895
896
        $createdFields = $contentDraft->getFields();
897
        $createdInLanguageCodes = $this->getLanguageCodesFromFields(
898
            $contentCreate->fields,
899
            $contentCreate->mainLanguageCode
900
        );
901
902
        $this->assertCount(
903
            count($contentType->fieldDefinitions) * count($createdInLanguageCodes),
904
            $createdFields,
905
            'Number of created fields does not match number of content type field definitions multiplied by number of languages the content is created in'
906
        );
907
908
        // Check field values
909
        $structFields = [];
910
        foreach ($contentCreate->fields as $field) {
911
            $structFields[$field->fieldDefIdentifier][$field->languageCode] = $field;
912
        }
913
        foreach ($contentType->fieldDefinitions as $fieldDefinition) {
914
            $this->assertArrayHasKey(
915
                $fieldDefinition->identifier,
916
                $contentDraft->fields,
917
                "Field values are missing for field definition '{$fieldDefinition->identifier}'"
918
            );
919
920
            foreach ($createdInLanguageCodes as $languageCode) {
921
                $this->assertArrayHasKey(
922
                    $languageCode,
923
                    $contentDraft->fields[$fieldDefinition->identifier],
924
                    "Field value is missing for field definition '{$fieldDefinition->identifier}' in language '{$languageCode}'"
925
                );
926
927
                // If field is not set in create struct, it should have default value
928
                $valueLanguageCode = $fieldDefinition->isTranslatable ? $languageCode : $contentCreate->mainLanguageCode;
929
                if (isset($structFields[$fieldDefinition->identifier][$valueLanguageCode])) {
930
                    $this->assertEquals(
931
                        $structFields[$fieldDefinition->identifier][$valueLanguageCode]->value,
932
                        $contentDraft->fields[$fieldDefinition->identifier][$languageCode],
933
                        "Field value for field definition '{$fieldDefinition->identifier}' in language '{$languageCode}' is not equal to given struct field value"
934
                    );
935
                } else {
936
                    $this->assertEquals(
937
                        $fieldDefinition->defaultValue,
938
                        $contentDraft->fields[$fieldDefinition->identifier][$languageCode],
939
                        "Field value for field definition '{$fieldDefinition->identifier}' in language '{$languageCode}' is not equal to default value"
940
                    );
941
                }
942
            }
943
        }
944
    }
945
946
    /**
947
     * Gathers language codes from an array of fields.
948
     *
949
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
950
     * @param string $mainLanguageCode
951
     *
952
     * @return array an array of language code strings
953
     */
954
    protected function getLanguageCodesFromFields(array $fields, $mainLanguageCode)
955
    {
956
        $languageCodes = [$mainLanguageCode];
957
        foreach ($fields as $field) {
958
            $languageCodes[] = $field->languageCode;
959
        }
960
961
        return array_unique($languageCodes);
962
    }
963
964
    /**
965
     * Test for the createContent() method.
966
     *
967
     * @covers \eZ\Publish\Core\Repository\ContentService::createContent
968
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
969
     */
970
    public function testCreateContentThrowsUnauthorizedException()
971
    {
972
        $testContentType = $this->createTestContentType();
973
        $contentService = $this->repository->getContentService();
974
975
        $contentCreate = $contentService->newContentCreateStruct($testContentType, 'eng-GB');
976
        $contentCreate->setField('test_required_empty', 'value for field definition with empty default value');
977
        $contentCreate->setField('test_translatable', 'and thumbs opposable', 'eng-US');
978
        $contentCreate->sectionId = 1;
979
        $contentCreate->ownerId = $this->repository->getCurrentUserReference()->getUserId();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...tCurrentUserReference() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user reference.

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...
980
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
981
        $contentCreate->alwaysAvailable = true;
982
983
        $locationCreates = [
984
            new LocationCreateStruct(
985
                [
986
                    'remoteId' => 'db787a9143f57828dd4331573466a013',
987
                    'parentLocationId' => 2,
988
                ]
989
            ),
990
        ];
991
992
        // Set anonymous as current user
993
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
994
995
        $contentService->createContent($contentCreate, $locationCreates);
996
    }
997
998
    /**
999
     * Test for the createContent() method.
1000
     *
1001
     * @covers \eZ\Publish\Core\Repository\ContentService::createContent
1002
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1003
     */
1004
    public function testCreateContentWithoutLocationsThrowsUnauthorizedException()
1005
    {
1006
        $testContentType = $this->createTestContentType();
1007
        $contentService = $this->repository->getContentService();
1008
1009
        $contentCreate = $contentService->newContentCreateStruct($testContentType, 'eng-GB');
1010
        $contentCreate->setField('test_required_empty', 'value for field definition with empty default value');
1011
        $contentCreate->setField('test_translatable', 'and thumbs opposable', 'eng-US');
1012
        $contentCreate->sectionId = 1;
1013
        $contentCreate->ownerId = $this->repository->getCurrentUserReference()->getUserId();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...tCurrentUserReference() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user reference.

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

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

Loading history...
1014
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
1015
        $contentCreate->alwaysAvailable = true;
1016
1017
        // Set anonymous as current user
1018
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
1019
1020
        $contentService->createContent($contentCreate, []);
1021
    }
1022
1023
    /**
1024
     * Test for the createContent() method.
1025
     *
1026
     * @covers \eZ\Publish\Core\Repository\ContentService::createContent
1027
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1028
     */
1029
    public function testCreateContentThrowsInvalidArgumentException()
1030
    {
1031
        $testContentType = $this->createTestContentType();
1032
1033
        /* BEGIN: Use Case */
1034
        $contentService = $this->repository->getContentService();
1035
1036
        $contentCreate = $contentService->newContentCreateStruct($testContentType, 'eng-GB');
1037
        $contentCreate->setField('test_required_empty', 'value for field definition with empty default value');
1038
        $contentCreate->sectionId = 1;
1039
        $contentCreate->ownerId = 14;
1040
        $contentCreate->remoteId = 'f5c88a2209584891056f987fd965b0ba';
1041
        $contentCreate->alwaysAvailable = true;
1042
1043
        // Throws an exception because remoteId "f5c88a2209584891056f987fd965b0ba" already exists
1044
        $contentService->createContent($contentCreate);
1045
        /* END: Use Case */
1046
    }
1047
1048
    /**
1049
     * Test for the createContent() method.
1050
     *
1051
     * @covers \eZ\Publish\Core\Repository\ContentService::createContent
1052
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentValidationException
1053
     *
1054
     * @return array
1055
     */
1056 View Code Duplication
    public function testCreateContentThrowsContentValidationExceptionFieldDefinitionUnexisting()
1057
    {
1058
        $testContentType = $this->createTestContentType();
1059
1060
        /* BEGIN: Use Case */
1061
        $contentService = $this->repository->getContentService();
1062
1063
        $contentCreate = $contentService->newContentCreateStruct($testContentType, 'eng-GB');
1064
        $contentCreate->setField('test_required_empty', 'value for field definition with empty default value');
1065
        $contentCreate->setField('humpty_dumpty', 'no such field definition');
1066
        $contentCreate->sectionId = 1;
1067
        $contentCreate->ownerId = 14;
1068
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
1069
        $contentCreate->alwaysAvailable = true;
1070
1071
        // Throws an exception because field definition with identifier "humpty_dumpty" does not exist
1072
        $contentService->createContent($contentCreate);
1073
        /* END: Use Case */
1074
    }
1075
1076
    /**
1077
     * Test for the createContent() method.
1078
     *
1079
     * @covers \eZ\Publish\Core\Repository\ContentService::createContent
1080
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentValidationException
1081
     *
1082
     * @return array
1083
     */
1084 View Code Duplication
    public function testCreateContentThrowsContentValidationExceptionUntranslatableField()
1085
    {
1086
        $testContentType = $this->createTestContentType();
1087
1088
        /* BEGIN: Use Case */
1089
        $contentService = $this->repository->getContentService();
1090
1091
        $contentCreate = $contentService->newContentCreateStruct($testContentType, 'eng-GB');
1092
        $contentCreate->setField('test_required_empty', 'value for field definition with empty default value');
1093
        $contentCreate->setField('test_untranslatable', 'Bandersnatch', 'eng-US');
1094
        $contentCreate->sectionId = 1;
1095
        $contentCreate->ownerId = 14;
1096
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
1097
        $contentCreate->alwaysAvailable = true;
1098
1099
        // Throws an exception because translation was given for a untranslatable field
1100
        // Note that it is still permissible to set untranslatable field with main language
1101
        $contentService->createContent($contentCreate);
1102
        /* END: Use Case */
1103
    }
1104
1105
    /**
1106
     * Test for the createContent() method.
1107
     *
1108
     * @covers \eZ\Publish\Core\Repository\ContentService::createContent
1109
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1110
     *
1111
     * @return array
1112
     */
1113
    public function testCreateContentRequiredFieldDefaultValueEmpty()
1114
    {
1115
        $testContentType = $this->createTestContentType();
1116
1117
        /* BEGIN: Use Case */
1118
        $contentService = $this->repository->getContentService();
1119
1120
        $contentCreate = $contentService->newContentCreateStruct($testContentType, 'eng-GB');
1121
        $contentCreate->setField('test_translatable', 'Jabberwock');
1122
        $contentCreate->sectionId = 1;
1123
        $contentCreate->ownerId = 14;
1124
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
1125
        $contentCreate->alwaysAvailable = true;
1126
1127
        // Throws an exception because required field is not set and its default value is empty
1128
        $contentService->createContent($contentCreate);
1129
        /* END: Use Case */
1130
    }
1131
1132
    /**
1133
     * Test for the createContent() method.
1134
     *
1135
     * @covers \eZ\Publish\Core\Repository\ContentService::createContent
1136
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1137
     */
1138 View Code Duplication
    public function testCreateContentThrowsContentFieldValidationException()
1139
    {
1140
        $testContentType = $this->createTestContentType();
1141
1142
        /* BEGIN: Use Case */
1143
        $contentService = $this->repository->getContentService();
1144
1145
        $contentCreate = $contentService->newContentCreateStruct($testContentType, 'eng-GB');
1146
        $contentCreate->setField(
1147
            'test_required_empty',
1148
            'a string that is too long and will not validate 12345678901234567890123456789012345678901234567890'
1149
        );
1150
        $contentCreate->setField('test_translatable', 'and thumbs opposable', 'eng-US');
1151
        $contentCreate->sectionId = 1;
1152
        $contentCreate->ownerId = 14;
1153
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
1154
        $contentCreate->alwaysAvailable = true;
1155
1156
        // Throws an exception because "test_required_empty" field value is too long and fails
1157
        // field definition's string length validator
1158
        $contentService->createContent($contentCreate);
1159
        /* END: Use Case */
1160
    }
1161
1162
    /**
1163
     * Test for the newContentMetadataUpdateStruct() method.
1164
     *
1165
     * @covers \eZ\Publish\Core\Repository\ContentService::newContentMetadataUpdateStruct
1166
     */
1167 View Code Duplication
    public function testNewContentMetadataUpdateStruct()
1168
    {
1169
        /* BEGIN: Use Case */
1170
        $contentService = $this->repository->getContentService();
1171
1172
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
1173
        /* END: Use Case */
1174
1175
        $this->assertInstanceOf(
1176
            'eZ\\Publish\\API\\Repository\\Values\\Content\\ContentMetadataUpdateStruct',
1177
            $contentMetadataUpdateStruct
1178
        );
1179
1180
        foreach ($contentMetadataUpdateStruct as $propertyName => $propertyValue) {
0 ignored issues
show
Bug introduced by
The expression $contentMetadataUpdateStruct of type object<eZ\Publish\API\Re...ntMetadataUpdateStruct> is not traversable.
Loading history...
1181
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
1182
        }
1183
    }
1184
1185
    /**
1186
     * Test for the updateContentMetadata() method.
1187
     *
1188
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContentMetadata
1189
     * @depends testNewContentMetadataUpdateStruct
1190
     *
1191
     * @return array
1192
     */
1193
    public function testUpdateContentMetadata()
1194
    {
1195
        // Create one additional location for content to be set as main location
1196
        $locationService = $this->repository->getLocationService();
1197
        $contentInfo = $this->repository->getContentService()->loadContentInfo(12);
1198
        $locationCreateStruct = $locationService->newLocationCreateStruct(44);
1199
        $locationCreateStruct->remoteId = 'test-location-remote-id-1234';
1200
        $newLocation = $locationService->createLocation(
1201
            $contentInfo,
1202
            $locationCreateStruct
1203
        );
1204
        $newSectionId = $this->repository->getContentService()->loadContentInfo(
1205
            $locationService->loadLocation($newLocation->parentLocationId)->contentId
1206
        )->sectionId;
1207
        // Change content section to be different from new main location parent location content
1208
        $sectionService = $this->repository->getSectionService();
1209
        $sectionService->assignSection(
1210
            $contentInfo,
1211
            $sectionService->loadSection($newSectionId === 1 ? $newSectionId + 1 : $newSectionId - 1)
1212
        );
1213
1214
        /* BEGIN: Use Case */
1215
        $contentService = $this->repository->getContentService();
1216
1217
        $contentInfo = $contentService->loadContentInfo(12);
1218
1219
        $newMainLocationId = $newLocation->id;
1220
        $time = time();
1221
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
1222
        $contentMetadataUpdateStruct->ownerId = 10;
1223
        $contentMetadataUpdateStruct->publishedDate = $this->getDateTime($time);
1224
        $contentMetadataUpdateStruct->modificationDate = $this->getDateTime($time);
1225
        $contentMetadataUpdateStruct->mainLanguageCode = 'eng-GB';
1226
        $contentMetadataUpdateStruct->alwaysAvailable = false;
1227
        $contentMetadataUpdateStruct->remoteId = 'the-all-new-remoteid';
1228
        $contentMetadataUpdateStruct->mainLocationId = $newMainLocationId;
1229
1230
        $content = $contentService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
1231
        /* END: Use Case */
1232
1233
        $this->assertInstanceOf('eZ\\Publish\\API\\Repository\\Values\\Content\\Content', $content);
1234
1235
        return [
1236
            'expected' => $contentMetadataUpdateStruct,
1237
            'actual' => $content,
1238
            'newSectionId' => $newSectionId,
1239
        ];
1240
    }
1241
1242
    /**
1243
     * Test for the updateContentMetadata() method.
1244
     *
1245
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContentMetadata
1246
     * @depends testUpdateContentMetadata
1247
     *
1248
     * @param array $data
1249
     */
1250
    public function testUpdateContentMetadataStructValues(array $data)
1251
    {
1252
        /** @var $updateStruct \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct */
1253
        $updateStruct = $data['expected'];
1254
        /** @var $content \eZ\Publish\API\Repository\Values\Content\Content */
1255
        $content = $data['actual'];
1256
1257
        $this->assertPropertiesCorrect(
1258
            [
1259
                'ownerId' => $updateStruct->ownerId,
1260
                // not changeable through MetadataUpdateStruct
1261
                //"name"
1262
                'publishedDate' => $updateStruct->publishedDate,
1263
                'modificationDate' => $updateStruct->modificationDate,
1264
                'mainLanguageCode' => $updateStruct->mainLanguageCode,
1265
                'alwaysAvailable' => $updateStruct->alwaysAvailable,
1266
                'remoteId' => $updateStruct->remoteId,
1267
                'mainLocationId' => $updateStruct->mainLocationId,
1268
                'sectionId' => $data['newSectionId'],
1269
            ],
1270
            $content->contentInfo
1271
        );
1272
    }
1273
1274
    /**
1275
     * Test for the updateContentMetadata() method.
1276
     *
1277
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContentMetadata
1278
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1279
     */
1280
    public function testUpdateContentMetadataThrowsUnauthorizedException()
1281
    {
1282
        $contentInfo = $this->repository->getContentService()->loadContentInfo(12);
1283
        $contentMetadataUpdateStruct = $this->repository->getContentService()->newContentMetadataUpdateStruct();
1284
        $contentMetadataUpdateStruct->remoteId = 'the-all-new-remoteid';
1285
1286
        // Set anonymous as current user
1287
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
1288
1289
        $this->repository->getContentService()->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
1290
    }
1291
1292
    /**
1293
     * Test for the updateContentMetadata() method.
1294
     *
1295
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContentMetadata
1296
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1297
     * @depends testNewContentMetadataUpdateStruct
1298
     */
1299
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionDuplicateRemoteId()
1300
    {
1301
        /* BEGIN: Use Case */
1302
        $contentService = $this->repository->getContentService();
1303
        $contentInfo = $contentService->loadContentInfo(4);
1304
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
1305
        $contentMetadataUpdateStruct->remoteId = '9b47a45624b023b1a76c73b74d704acf';
1306
1307
        // Throws an exception because remoteId "9b47a45624b023b1a76c73b74d704acf" is already in use
1308
        $contentService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
1309
        /* END: Use Case */
1310
    }
1311
1312
    /**
1313
     * Test for the updateContentMetadata() method.
1314
     *
1315
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContentMetadata
1316
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1317
     * @depends testNewContentMetadataUpdateStruct
1318
     */
1319
    public function testUpdateContentMetadataThrowsInvalidArgumentExceptionNoMetadataPropertiesSet()
1320
    {
1321
        /* BEGIN: Use Case */
1322
        $contentService = $this->repository->getContentService();
1323
        $contentInfo = $contentService->loadContentInfo(4);
1324
        $contentMetadataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
1325
1326
        // Throws an exception because no properties are set in $contentMetadataUpdateStruct
1327
        $contentService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
1328
        /* END: Use Case */
1329
    }
1330
1331
    /**
1332
     * Test for the newContentUpdateStruct() method.
1333
     *
1334
     * @covers \eZ\Publish\Core\Repository\ContentService::newContentUpdateStruct
1335
     */
1336 View Code Duplication
    public function testNewContentUpdateStruct()
1337
    {
1338
        /* BEGIN: Use Case */
1339
        $contentService = $this->repository->getContentService();
1340
1341
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1342
        /* END: Use Case */
1343
1344
        $this->assertInstanceOf(
1345
            'eZ\\Publish\\API\\Repository\\Values\\Content\\ContentUpdateStruct',
1346
            $contentUpdateStruct
1347
        );
1348
1349
        $this->assertPropertiesCorrect(
1350
            [
1351
                'initialLanguageCode' => null,
1352
                'fields' => [],
1353
            ],
1354
            $contentUpdateStruct
1355
        );
1356
    }
1357
1358
    /**
1359
     * Test for the updateContent() method.
1360
     *
1361
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContent
1362
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1363
     */
1364
    public function testUpdateContentThrowsUnauthorizedException()
1365
    {
1366
        list($content, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
1367
1368
        $contentUpdateStruct = $this->repository->getContentService()->newContentUpdateStruct();
1369
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1370
        $contentUpdateStruct->setField('test_required_empty', 'new value for test_required_empty');
1371
1372
        // Set anonymous as current user
1373
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
1374
1375
        $this->repository->getContentService()->updateContent(
1376
            $content->versionInfo,
1377
            $contentUpdateStruct
1378
        );
1379
    }
1380
1381
    /**
1382
     * Test for the updateContent() method.
1383
     *
1384
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContent
1385
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1386
     */
1387
    public function testUpdateContentThrowsBadStateException()
1388
    {
1389
        /* BEGIN: Use Case */
1390
        $contentService = $this->repository->getContentService();
1391
1392
        $versionInfo = $contentService->loadVersionInfoById(4);
1393
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1394
1395
        // Throws an exception because version is not a draft
1396
        $updatedContent = $contentService->updateContent($versionInfo, $contentUpdateStruct);
0 ignored issues
show
Unused Code introduced by
$updatedContent 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...
1397
        /* END: Use Case */
1398
    }
1399
1400
    /**
1401
     * Test for the updateContent() method.
1402
     *
1403
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContent
1404
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1405
     */
1406 View Code Duplication
    public function testUpdateContentThrowsContentFieldValidationException()
1407
    {
1408
        list($content, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
1409
1410
        /* BEGIN: Use Case */
1411
        $contentService = $this->repository->getContentService();
1412
1413
        $versionInfo = $contentService->loadVersionInfoById(
1414
            $content->id,
1415
            $content->getVersionInfo()->versionNo
1416
        );
1417
1418
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1419
        $contentUpdateStruct->initialLanguageCode = 'eng-US';
1420
        $contentUpdateStruct->setField(
1421
            'test_required_empty',
1422
            'a string that is too long and will not validate 12345678901234567890123456789012345678901234567890'
1423
        );
1424
1425
        // Throws an exception because "test_required_empty" field value is too long and fails
1426
        // field definition's string length validator
1427
        $updatedContent = $contentService->updateContent($versionInfo, $contentUpdateStruct);
0 ignored issues
show
Unused Code introduced by
$updatedContent 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...
1428
        /* END: Use Case */
1429
    }
1430
1431
    /**
1432
     * Test for the updateContent() method.
1433
     *
1434
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContent
1435
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
1436
     */
1437 View Code Duplication
    public function testUpdateContentRequiredFieldEmpty()
1438
    {
1439
        list($content, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
1440
1441
        /* BEGIN: Use Case */
1442
        $contentService = $this->repository->getContentService();
1443
1444
        $versionInfo = $contentService->loadVersionInfoById(
1445
            $content->id,
1446
            $content->getVersionInfo()->versionNo
1447
        );
1448
1449
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1450
        $contentUpdateStruct->initialLanguageCode = 'eng-GB';
1451
        $contentUpdateStruct->setField('test_required_empty', '');
1452
1453
        // Throws an exception because required field is being updated with empty value
1454
        $updatedContent = $contentService->updateContent($versionInfo, $contentUpdateStruct);
0 ignored issues
show
Unused Code introduced by
$updatedContent 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...
1455
        /* END: Use Case */
1456
    }
1457
1458
    /**
1459
     * Test for the updateContent() method.
1460
     *
1461
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContent
1462
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentValidationException
1463
     */
1464 View Code Duplication
    public function testUpdateContentThrowsContentValidationExceptionFieldDefinitionNonexistent()
1465
    {
1466
        list($content, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
1467
1468
        /* BEGIN: Use Case */
1469
        $contentService = $this->repository->getContentService();
1470
1471
        $versionInfo = $contentService->loadVersionInfoById(
1472
            $content->id,
1473
            $content->getVersionInfo()->versionNo
1474
        );
1475
1476
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1477
        $contentUpdateStruct->initialLanguageCode = 'eng-GB';
1478
        $contentUpdateStruct->setField('nonexistent_field_definition_identifier', 'eng-GB');
1479
1480
        // Throws an exception because field definition with identifier "nonexistent_field_definition_identifier"
1481
        // does not exist in content draft content type
1482
        $updatedContent = $contentService->updateContent($versionInfo, $contentUpdateStruct);
0 ignored issues
show
Unused Code introduced by
$updatedContent 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...
1483
        /* END: Use Case */
1484
    }
1485
1486
    /**
1487
     * Test for the updateContent() method.
1488
     *
1489
     * @covers \eZ\Publish\Core\Repository\ContentService::updateContent
1490
     * @expectedException \eZ\Publish\API\Repository\Exceptions\ContentValidationException
1491
     *
1492
     * @return array
1493
     */
1494 View Code Duplication
    public function testUpdateContentThrowsContentValidationExceptionUntranslatableField()
1495
    {
1496
        list($content, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
1497
1498
        /* BEGIN: Use Case */
1499
        $contentService = $this->repository->getContentService();
1500
1501
        $versionInfo = $contentService->loadVersionInfoById(
1502
            $content->id,
1503
            $content->getVersionInfo()->versionNo
1504
        );
1505
1506
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1507
        $contentUpdateStruct->initialLanguageCode = 'eng-GB';
1508
        $contentUpdateStruct->setField('test_untranslatable', 'Jabberwock', 'eng-US');
1509
1510
        // Throws an exception because translation was given for a untranslatable field
1511
        // Note that it is still permissible to set untranslatable field with main language
1512
        $updatedContent = $contentService->updateContent($versionInfo, $contentUpdateStruct);
0 ignored issues
show
Unused Code introduced by
$updatedContent 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...
1513
        /* END: Use Case */
1514
    }
1515
1516
    /**
1517
     * Test for the publishVersion() method.
1518
     *
1519
     * @depends testCreateContent
1520
     * @covers \eZ\Publish\Core\Repository\ContentService::publishVersion
1521
     */
1522
    public function testPublishVersion()
1523
    {
1524
        $time = time();
1525
        list($draftContent, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
1526
1527
        /* BEGIN: Use Case */
1528
        $contentService = $this->repository->getContentService();
1529
1530
        $versionInfo = $contentService->loadVersionInfoById(
1531
            $draftContent->id,
1532
            $draftContent->getVersionInfo()->versionNo
1533
        );
1534
1535
        $publishedContent = $contentService->publishVersion($versionInfo);
1536
        /* END: Use Case */
1537
1538
        $this->assertInstanceOf('eZ\\Publish\\API\\Repository\\Values\\Content\\Content', $publishedContent);
1539
        $this->assertTrue($publishedContent->contentInfo->published);
1540
        $this->assertEquals(VersionInfo::STATUS_PUBLISHED, $publishedContent->versionInfo->status);
1541
        $this->assertGreaterThanOrEqual($time, $publishedContent->contentInfo->publishedDate->getTimestamp());
1542
        $this->assertGreaterThanOrEqual($time, $publishedContent->contentInfo->modificationDate->getTimestamp());
1543
    }
1544
1545
    public function testPublishVersionDoesNotChangePublishedDate()
1546
    {
1547
        list($draftContent, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
1548
1549
        $contentService = $this->repository->getContentService();
1550
1551
        $versionInfo = $contentService->loadVersionInfoById(
1552
            $draftContent->id,
1553
            $draftContent->getVersionInfo()->versionNo
1554
        );
1555
1556
        $publishedContent = $contentService->publishVersion($versionInfo);
1557
1558
        sleep(1);
1559
1560
        /* BEGIN: Use Case */
1561
        $contentDraft = $contentService->createContentDraft($publishedContent->contentInfo);
1562
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
1563
        $contentUpdateStruct->initialLanguageCode = 'eng-GB';
1564
        $contentDraft = $contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
1565
        $republishedContent = $contentService->publishVersion($contentDraft->versionInfo);
1566
        /* END: Use Case */
1567
1568
        $this->assertEquals(
1569
            $publishedContent->contentInfo->publishedDate->getTimestamp(),
1570
            $republishedContent->contentInfo->publishedDate->getTimestamp()
1571
        );
1572
        $this->assertGreaterThan(
1573
            $publishedContent->contentInfo->modificationDate->getTimestamp(),
1574
            $republishedContent->contentInfo->modificationDate->getTimestamp()
1575
        );
1576
    }
1577
1578
    /**
1579
     * Test for the publishVersion() method.
1580
     *
1581
     * @covers \eZ\Publish\Core\Repository\ContentService::publishVersion
1582
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1583
     */
1584
    public function testPublishVersionThrowsBadStateException()
1585
    {
1586
        /* BEGIN: Use Case */
1587
        $contentService = $this->repository->getContentService();
1588
1589
        $versionInfo = $contentService->loadVersionInfoById(4);
1590
1591
        // Throws an exception because version is already published
1592
        $publishedContent = $contentService->publishVersion($versionInfo);
0 ignored issues
show
Unused Code introduced by
$publishedContent 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...
1593
        /* END: Use Case */
1594
    }
1595
1596
    /**
1597
     * Test for the publishVersion() method.
1598
     *
1599
     * @covers \eZ\Publish\Core\Repository\ContentService::publishVersion
1600
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1601
     */
1602 View Code Duplication
    public function testPublishVersionThrowsUnauthorizedException()
1603
    {
1604
        list($draftContent, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
1605
1606
        // Set anonymous as current user
1607
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
1608
1609
        $this->repository->getContentService()->publishVersion($draftContent->versionInfo);
1610
    }
1611
1612
    /**
1613
     * Test for the createContentDraft() method.
1614
     *
1615
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1616
     *
1617
     * @return array
1618
     */
1619 View Code Duplication
    public function testCreateContentDraft()
1620
    {
1621
        $time = time();
1622
1623
        /* BEGIN: Use Case */
1624
        $contentService = $this->repository->getContentService();
1625
1626
        $contentInfo = $contentService->loadContentInfo(4);
1627
1628
        $draftContent = $contentService->createContentDraft($contentInfo);
1629
        /* END: Use Case */
1630
1631
        $this->assertInstanceOf(
1632
            'eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1633
            $draftContent
1634
        );
1635
1636
        return [
1637
            'draftContent' => $draftContent,
1638
            'time' => $time,
1639
        ];
1640
    }
1641
1642
    /**
1643
     * @param array $data
1644
     */
1645
    protected function assertDraftContentValues(array $data)
1646
    {
1647
        /** @var $draftContent \eZ\Publish\API\Repository\Values\Content\Content */
1648
        $draftContent = $data['draftContent'];
1649
        $time = $data['time'];
1650
1651
        $this->assertContentValues($data['draftContent'], null, true);
1652
        $this->assertGreaterThanOrEqual(
1653
            $this->getDateTime($time),
1654
            $draftContent->getVersionInfo()->creationDate
1655
        );
1656
        $this->assertGreaterThanOrEqual(
1657
            $this->getDateTime($time),
1658
            $draftContent->getVersionInfo()->modificationDate
1659
        );
1660
    }
1661
1662
    /**
1663
     * Test for the createContentDraft() method.
1664
     *
1665
     * @depends testCreateContentDraft
1666
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1667
     *
1668
     * @param array $data
1669
     */
1670
    public function testCreateContentDraftValues(array $data)
1671
    {
1672
        $this->assertDraftContentValues($data);
1673
    }
1674
1675
    /**
1676
     * Test for the createContentDraft() method.
1677
     *
1678
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1679
     *
1680
     * @return array
1681
     */
1682 View Code Duplication
    public function testCreateContentDraftWithSecondArgument()
1683
    {
1684
        $time = time();
1685
1686
        /* BEGIN: Use Case */
1687
        $contentService = $this->repository->getContentService();
1688
        $content = $contentService->loadContent(4);
1689
1690
        $draftContent = $contentService->createContentDraft(
1691
            $content->contentInfo,
1692
            $content->getVersionInfo()
1693
        );
1694
        /* END: Use Case */
1695
1696
        $this->assertInstanceOf(
1697
            'eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1698
            $draftContent
1699
        );
1700
1701
        return [
1702
            'draftContent' => $draftContent,
1703
            'time' => $time,
1704
        ];
1705
    }
1706
1707
    /**
1708
     * Test for the createContentDraft() method.
1709
     *
1710
     * @depends testCreateContentDraftWithSecondArgument
1711
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1712
     *
1713
     * @param array $data
1714
     */
1715
    public function testCreateContentDraftWithSecondArgumentValues(array $data)
1716
    {
1717
        $this->assertDraftContentValues($data);
1718
    }
1719
1720
    /**
1721
     * Test for the createContentDraft() method.
1722
     *
1723
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1724
     *
1725
     * @return array
1726
     */
1727
    public function testCreateContentDraftWithThirdArgument()
1728
    {
1729
        $time = time();
1730
1731
        /* BEGIN: Use Case */
1732
        $contentService = $this->repository->getContentService();
1733
        $content = $contentService->loadContent(4);
1734
1735
        $draftContent = $contentService->createContentDraft(
1736
            $content->contentInfo,
1737
            $content->getVersionInfo(),
1738
            $this->repository->getCurrentUser()
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

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

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

Loading history...
1739
        );
1740
        /* END: Use Case */
1741
1742
        $this->assertInstanceOf(
1743
            'eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
1744
            $draftContent
1745
        );
1746
1747
        return [
1748
            'draftContent' => $draftContent,
1749
            'time' => $time,
1750
        ];
1751
    }
1752
1753
    /**
1754
     * Test for the createContentDraft() method.
1755
     *
1756
     * @depends testCreateContentDraftWithThirdArgument
1757
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1758
     *
1759
     * @param array $data
1760
     */
1761
    public function testCreateContentDraftWithThirdArgumentValues(array $data)
1762
    {
1763
        $this->assertDraftContentValues($data);
1764
    }
1765
1766
    /**
1767
     * Test for the createContentDraft() method.
1768
     *
1769
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1770
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1771
     */
1772 View Code Duplication
    public function testCreateContentDraftThrowsBadStateException()
1773
    {
1774
        $contentService = $this->repository->getContentService();
1775
        $contentInfo = $contentService->loadContentInfo(4);
1776
        $draftContent = $contentService->createContentDraft($contentInfo);
1777
1778
        // Throws an exception because version status is not
1779
        // VersionInfo::STATUS_PUBLISHED nor VersionInfo::STATUS_ARCHIVED
1780
        $draftContent = $contentService->createContentDraft(
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...
1781
            $draftContent->contentInfo,
1782
            $draftContent->getVersionInfo()
1783
        );
1784
    }
1785
1786
    /**
1787
     * Test for the createContentDraft() method.
1788
     *
1789
     * @covers \eZ\Publish\Core\Repository\ContentService::createContentDraft
1790
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1791
     */
1792 View Code Duplication
    public function testCreateContentDraftThrowsUnauthorizedException()
1793
    {
1794
        $contentService = $this->repository->getContentService();
1795
1796
        $contentInfo = $contentService->loadContentInfo(4);
1797
1798
        // Set anonymous as current user
1799
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
1800
1801
        $contentService->createContentDraft($contentInfo);
1802
    }
1803
1804
    /**
1805
     * Test for the loadContentDrafts() method.
1806
     *
1807
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContentDrafts
1808
     */
1809
    public function testLoadContentDrafts()
1810
    {
1811
        $contentService = $this->repository->getContentService();
1812
1813
        // delete existing drafts before we begin
1814
        $draftedVersions = $contentService->loadContentDrafts();
1815
        foreach ($draftedVersions as $draftedVersion) {
1816
            $contentService->deleteVersion($draftedVersion);
1817
        }
1818
1819
        /* BEGIN: Use Case */
1820
        // Remote ids of the "Users" user group of a eZ Publish demo installation.
1821
        $usersUserGroupRemoteId = 'f5c88a2209584891056f987fd965b0ba';
1822
        $membersUserGroupRemoteId = '5f7f0bdb3381d6a461d8c29ff53d908f';
1823
1824
        // "Users" user group content object
1825
        $usersUserGroupContentInfo = $contentService->loadContentInfoByRemoteId($usersUserGroupRemoteId);
1826
        $membersUserGroupContentInfo = $contentService->loadContentInfoByRemoteId($membersUserGroupRemoteId);
1827
1828
        // Create some drafts
1829
        $contentService->createContentDraft($usersUserGroupContentInfo);
1830
        $contentService->createContentDraft($membersUserGroupContentInfo);
1831
1832
        // Now $contentDrafts should contain two drafted versions
1833
        $draftedVersions = $contentService->loadContentDrafts();
1834
        /* END: Use Case */
1835
1836
        $actual = [
1837
            $draftedVersions[0]->status,
1838
            $draftedVersions[1]->status,
1839
            count($draftedVersions),
1840
            $draftedVersions[0]->getContentInfo()->remoteId,
1841
            $draftedVersions[1]->getContentInfo()->remoteId,
1842
        ];
1843
1844
        $this->assertEquals(
1845
            [
1846
                VersionInfo::STATUS_DRAFT,
1847
                VersionInfo::STATUS_DRAFT,
1848
                2,
1849
                $usersUserGroupRemoteId,
1850
                $membersUserGroupRemoteId,
1851
            ],
1852
            $actual
1853
        );
1854
    }
1855
1856
    /**
1857
     * Test for the loadContentDrafts() method.
1858
     *
1859
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContentDrafts
1860
     */
1861
    public function testLoadContentDraftsWithFirstArgument()
1862
    {
1863
    }
1864
1865
    /**
1866
     * Test for the loadContentDrafts() method.
1867
     *
1868
     * @covers \eZ\Publish\Core\Repository\ContentService::loadContentDrafts
1869
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1870
     */
1871
    public function testLoadContentDraftsThrowsUnauthorizedException()
1872
    {
1873
        // Set anonymous as current user
1874
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
1875
1876
        // Now $contentDrafts should contain two drafted versions
1877
        $this->repository->getContentService()->loadContentDrafts();
1878
    }
1879
1880
    /**
1881
     * Test for the loadVersions() method.
1882
     *
1883
     * @depends testLoadContentInfo
1884
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersions
1885
     *
1886
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[]
1887
     */
1888
    public function testLoadVersions()
1889
    {
1890
        /* BEGIN: Use Case */
1891
        $contentService = $this->repository->getContentService();
1892
1893
        $contentInfo = $contentService->loadContentInfo(4);
1894
        $versions = $contentService->loadVersions($contentInfo);
1895
        /* END: Use Case */
1896
1897
        return $versions;
1898
    }
1899
1900
    /**
1901
     * Test for the loadVersions() method.
1902
     *
1903
     * @depends testLoadVersions
1904
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersions
1905
     *
1906
     * @param array $versions
1907
     */
1908
    public function testLoadVersionsValues(array $versions)
1909
    {
1910
        $versionInfoValues = $this->getVersionInfoExpectedValues();
1911
1912
        $this->assertPropertiesCorrect(
1913
            $versionInfoValues,
1914
            $versions[0]
1915
        );
1916
    }
1917
1918
    /**
1919
     * Test for the loadVersions() method.
1920
     *
1921
     * @depends testLoadContentInfo
1922
     * @depends testCreateContentDraft
1923
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersions
1924
     *
1925
     * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[]
1926
     */
1927 View Code Duplication
    public function testLoadVersionsMultiple()
1928
    {
1929
        $time = time();
1930
1931
        /* BEGIN: Use Case */
1932
        $contentService = $this->repository->getContentService();
1933
1934
        $contentInfo = $contentService->loadContentInfo(4);
1935
        // Create one additional version
1936
        $draftContent = $contentService->createContentDraft($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...
1937
        $versions = $contentService->loadVersions($contentInfo);
1938
        /* END: Use Case */
1939
1940
        return [
1941
            'versions' => $versions,
1942
            'time' => $time,
1943
        ];
1944
    }
1945
1946
    /**
1947
     * Test for the loadVersions() method.
1948
     *
1949
     * @depends testLoadVersionsMultiple
1950
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersions
1951
     *
1952
     * @param array $data
1953
     */
1954
    public function testLoadVersionsMultipleValues(array $data)
1955
    {
1956
        $versions = $data['versions'];
1957
        $time = $data['time'];
1958
1959
        $versionInfoValues = $this->getVersionInfoExpectedValues();
1960
        $this->assertPropertiesCorrect(
1961
            $versionInfoValues,
1962
            $versions[0]
1963
        );
1964
1965
        $versionInfoValues = $this->getVersionInfoExpectedValues(true);
1966
        $this->assertPropertiesCorrect(
1967
            $versionInfoValues,
1968
            $versions[1]
1969
        );
1970
        $this->assertGreaterThanOrEqual(
1971
            $this->getDateTime($time),
1972
            $versions[1]->creationDate
1973
        );
1974
        $this->assertGreaterThanOrEqual(
1975
            $this->getDateTime($time),
1976
            $versions[1]->modificationDate
1977
        );
1978
    }
1979
1980
    /**
1981
     * Test for the loadVersions() method.
1982
     *
1983
     * @covers \eZ\Publish\Core\Repository\ContentService::loadVersions
1984
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1985
     */
1986 View Code Duplication
    public function testLoadVersionsThrowsUnauthorizedException()
1987
    {
1988
        $contentService = $this->repository->getContentService();
1989
1990
        $contentInfo = $contentService->loadContentInfo(4);
1991
1992
        // Set anonymous as current user
1993
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
1994
1995
        $contentService->loadVersions($contentInfo);
1996
    }
1997
1998
    /**
1999
     * Test for the deleteVersion() method.
2000
     *
2001
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteVersion
2002
     */
2003
    public function testDeleteVersion()
2004
    {
2005
        /* BEGIN: Use Case */
2006
        $contentService = $this->repository->getContentService();
2007
2008
        $contentInfo = $contentService->loadContentInfo(4);
2009
2010
        // Create a version to delete
2011
        $draftContent = $contentService->createContentDraft($contentInfo);
2012
2013
        $contentService->deleteVersion($draftContent->versionInfo);
2014
        /* END: Use Case */
2015
2016
        try {
2017
            $contentService->loadVersionInfo(
2018
                $draftContent->contentInfo,
2019
                $draftContent->versionInfo->versionNo
2020
            );
2021
2022
            $this->fail('Version was not successfully deleted!');
2023
        } catch (NotFoundException $e) {
2024
            // Do nothing
2025
        }
2026
    }
2027
2028
    /**
2029
     * Test for the deleteVersion() method.
2030
     *
2031
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteVersion
2032
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
2033
     */
2034
    public function testDeleteVersionThrowsBadStateException()
2035
    {
2036
        /* BEGIN: Use Case */
2037
        $contentService = $this->repository->getContentService();
2038
2039
        $versionInfo = $contentService->loadVersionInfoById(4);
2040
2041
        // Throws an exception because version is published
2042
        $contentService->deleteVersion($versionInfo);
2043
        /* END: Use Case */
2044
    }
2045
2046
    /**
2047
     * Test for the deleteVersion() method.
2048
     *
2049
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteVersion
2050
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
2051
     */
2052 View Code Duplication
    public function testDeleteVersionThrowsUnauthorizedException()
2053
    {
2054
        $contentService = $this->repository->getContentService();
2055
2056
        $contentInfo = $contentService->loadContentInfo(4);
2057
2058
        // Create a version to delete
2059
        $draftContent = $contentService->createContentDraft($contentInfo);
2060
2061
        // Set anonymous as current user
2062
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
2063
2064
        $contentService->deleteVersion($draftContent->versionInfo);
2065
    }
2066
2067
    /**
2068
     * Test for the copyContent() method.
2069
     *
2070
     * @dep_ends testCreate
2071
     * @dep_ends testLoadContentInfo
2072
     * @dep_ends testLoadVersionInfoById
2073
     * @covers \eZ\Publish\Core\Repository\ContentService::copyContent
2074
     */
2075
    public function testCopyContentSingleVersion()
2076
    {
2077
        $time = time();
2078
2079
        /* BEGIN: Use Case */
2080
        $contentService = $this->repository->getContentService();
2081
        $locationService = $this->repository->getLocationService();
2082
2083
        $contentInfo = $contentService->loadContentInfo(11);
2084
        $versionInfo = $contentService->loadVersionInfoById(11, 1);
2085
        $destinationLocationCreateStruct = $locationService->newLocationCreateStruct(5);
2086
2087
        $copiedContent = $contentService->copyContent(
2088
            $contentInfo,
2089
            $destinationLocationCreateStruct,
2090
            $versionInfo
2091
        );
2092
        /* END: Use Case */
2093
2094
        $this->assertInstanceOf(
2095
            'eZ\\Publish\\Core\\Repository\\Values\\Content\\Content',
2096
            $copiedContent
2097
        );
2098
2099
        $this->assertEquals(1, $copiedContent->contentInfo->currentVersionNo);
2100
        $this->assertGreaterThanOrEqual($time, $copiedContent->contentInfo->modificationDate->getTimestamp());
2101
        $this->assertGreaterThanOrEqual($time, $copiedContent->contentInfo->publishedDate->getTimestamp());
2102
        $this->assertCopyContentValues(
2103
            $contentService->loadContent(11, null, 1),
2104
            $copiedContent
2105
        );
2106
    }
2107
2108
    /**
2109
     * Test for the copyContent() method.
2110
     *
2111
     * @dep_ends testLoadVersions
2112
     * @covers \eZ\Publish\Core\Repository\ContentService::copyContent
2113
     */
2114
    public function testCopyContentAllVersions()
2115
    {
2116
        $time = time();
2117
2118
        /* BEGIN: Use Case */
2119
        $contentService = $this->repository->getContentService();
2120
        $locationService = $this->repository->getLocationService();
2121
2122
        $contentInfo = $contentService->loadContentInfo(11);
2123
        $destinationLocationCreateStruct = $locationService->newLocationCreateStruct(5);
2124
2125
        $copiedContent = $contentService->copyContent(
2126
            $contentInfo,
2127
            $destinationLocationCreateStruct
2128
        );
2129
        /* END: Use Case */
2130
2131
        $this->assertInstanceOf(
2132
            'eZ\\Publish\\Core\\Repository\\Values\\Content\\Content',
2133
            $copiedContent
2134
        );
2135
2136
        $this->assertGreaterThanOrEqual($time, $copiedContent->contentInfo->modificationDate->getTimestamp());
2137
        $this->assertGreaterThanOrEqual($time, $copiedContent->contentInfo->publishedDate->getTimestamp());
2138
2139
        $originalVersionInfos = $contentService->loadVersions($contentInfo);
2140
        $copiedVersionInfos = $contentService->loadVersions($copiedContent->contentInfo);
2141
        $sorter =
2142
            function (VersionInfo $a, VersionInfo $b) {
2143
                return strcmp($a->versionNo, $b->versionNo);
2144
            };
2145
        usort($originalVersionInfos, $sorter);
2146
        usort($copiedVersionInfos, $sorter);
2147
        $this->assertCount(
2148
            count($originalVersionInfos),
2149
            $copiedVersionInfos,
2150
            'Count of versions copied does not match the count of original versions'
2151
        );
2152
        $this->assertEquals($contentInfo->currentVersionNo, $copiedContent->contentInfo->currentVersionNo);
2153
        foreach ($originalVersionInfos as $index => $versionInfo) {
2154
            $this->assertEquals($versionInfo->versionNo, $copiedVersionInfos[$index]->versionNo);
2155
            $this->assertCopyContentValues(
2156
                $contentService->loadContent(
2157
                    $contentInfo->id,
2158
                    null,
2159
                    $versionInfo->versionNo
2160
                ),
2161
                $contentService->loadContent(
2162
                    $copiedContent->id,
2163
                    null,
2164
                    $copiedVersionInfos[$index]->versionNo
2165
                )
2166
            );
2167
        }
2168
    }
2169
2170
    /**
2171
     * Asserts that $copiedContent is valid copy of $originalContent.
2172
     *
2173
     * @param \eZ\Publish\API\Repository\Values\Content\Content $originalContent
2174
     * @param \eZ\Publish\API\Repository\Values\Content\Content $copiedContent
2175
     */
2176
    protected function assertCopyContentValues(APIContent $originalContent, APIContent $copiedContent)
2177
    {
2178
        $this->assertNotEquals(
2179
            $originalContent->id,
2180
            $copiedContent->id,
2181
            'Id of content copy is equal to id od original content'
2182
        );
2183
2184
        $this->assertSameClassPropertiesCorrect(
2185
            [
2186
                //"name",
2187
                'contentTypeId',
2188
                'sectionId',
2189
                //"currentVersionNo",
2190
                'published',
2191
                'ownerId',
2192
                'alwaysAvailable',
2193
                'mainLanguageCode',
2194
                //"mainLocationId"
2195
            ],
2196
            $originalContent->contentInfo,
2197
            $copiedContent->contentInfo
2198
        );
2199
        $this->assertNotEquals($originalContent->contentInfo->id, $copiedContent->contentInfo->id);
2200
        $this->assertNotEquals($originalContent->contentInfo->remoteId, $copiedContent->contentInfo->remoteId);
2201
2202
        $this->assertSameClassPropertiesCorrect(
2203
            [
2204
                'versionNo',
2205
                //"contentId",
2206
                'names',
2207
                //"creationDate",
2208
                //"modificationDate",
2209
                'creatorId',
2210
                //"status",
2211
                'initialLanguageCode',
2212
                'languageCodes',
2213
            ],
2214
            $originalContent->versionInfo,
2215
            $copiedContent->versionInfo
2216
        );
2217
        $this->assertNotEquals($originalContent->versionInfo->id, $copiedContent->versionInfo->id);
2218
2219
        $originalFields = $originalContent->getFields();
2220
        $copiedFields = $copiedContent->getFields();
2221
        $this->assertCount(
2222
            count($originalFields),
2223
            $copiedFields,
2224
            'Count of fields copied does not match the count of original fields'
2225
        );
2226
        foreach ($originalFields as $fieldIndex => $originalField) {
2227
            $this->assertSameClassPropertiesCorrect(
2228
                [
2229
                    'fieldDefIdentifier',
2230
                    'value',
2231
                    'languageCode',
2232
                ],
2233
                $originalField,
2234
                $copiedFields[$fieldIndex]
2235
            );
2236
            $this->assertNotEquals(
2237
                $originalField->id,
2238
                $copiedFields[$fieldIndex]->id
2239
            );
2240
        }
2241
    }
2242
2243
    /**
2244
     * Test for the copyContent() method.
2245
     *
2246
     * @covers \eZ\Publish\Core\Repository\ContentService::copyContent
2247
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
2248
     */
2249 View Code Duplication
    public function testCopyContentThrowsUnauthorizedException()
2250
    {
2251
        $contentService = $this->repository->getContentService();
2252
        $locationService = $this->repository->getLocationService();
2253
2254
        $contentInfo = $contentService->loadContentInfo(11);
2255
        $destinationLocationCreateStruct = $locationService->newLocationCreateStruct(5);
2256
2257
        // Set anonymous as current user
2258
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
2259
2260
        $contentService->copyContent($contentInfo, $destinationLocationCreateStruct);
2261
    }
2262
2263
    /**
2264
     * Test for the newTranslationInfo() method.
2265
     *
2266
     * @covers \eZ\Publish\Core\Repository\ContentService::newTranslationInfo
2267
     */
2268 View Code Duplication
    public function testNewTranslationInfo()
2269
    {
2270
        /* BEGIN: Use Case */
2271
        $contentService = $this->repository->getContentService();
2272
2273
        $translationInfo = $contentService->newTranslationInfo();
2274
        /* END: Use Case */
2275
2276
        $this->assertInstanceOf(
2277
            'eZ\\Publish\\API\\Repository\\Values\\Content\\TranslationInfo',
2278
            $translationInfo
2279
        );
2280
2281
        foreach ($translationInfo as $propertyName => $propertyValue) {
0 ignored issues
show
Bug introduced by
The expression $translationInfo of type object<eZ\Publish\API\Re...ontent\TranslationInfo> is not traversable.
Loading history...
2282
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
2283
        }
2284
    }
2285
2286
    /**
2287
     * Test for the newTranslationValues() method.
2288
     *
2289
     * @covers \eZ\Publish\Core\Repository\ContentService::newTranslationValues
2290
     */
2291 View Code Duplication
    public function testNewTranslationValues()
2292
    {
2293
        /* BEGIN: Use Case */
2294
        $contentService = $this->repository->getContentService();
2295
2296
        $translationValues = $contentService->newTranslationValues();
2297
        /* END: Use Case */
2298
2299
        $this->assertInstanceOf(
2300
            'eZ\\Publish\\API\\Repository\\Values\\Content\\TranslationValues',
2301
            $translationValues
2302
        );
2303
2304
        foreach ($translationValues as $propertyName => $propertyValue) {
0 ignored issues
show
Bug introduced by
The expression $translationValues of type object<eZ\Publish\API\Re...tent\TranslationValues> is not traversable.
Loading history...
2305
            $this->assertNull($propertyValue, "Property '{$propertyName}' initial value should be null'");
2306
        }
2307
    }
2308
2309
    /**
2310
     * Test for the loadRelations() method.
2311
     *
2312
     * @covers \eZ\Publish\Core\Repository\ContentService::loadRelations
2313
     * @covers \eZ\Publish\Core\Repository\ContentService::addRelation
2314
     */
2315 View Code Duplication
    public function testLoadRelations()
2316
    {
2317
        list($contentDraft, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
2318
        $contentService = $this->repository->getContentService();
2319
2320
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId('a6e35cbcb7cd6ae4b691f3eee30cd262');
2321
2322
        $contentService->addRelation(
2323
            $contentDraft->getVersionInfo(),
2324
            $mediaContentInfo
2325
        );
2326
2327
        $relations = $contentService->loadRelations($contentDraft->versionInfo);
2328
2329
        $this->assertRelations($relations, $contentDraft->contentInfo, $mediaContentInfo);
2330
    }
2331
2332
    protected function assertRelations($relations, $sourceContentInfo, $destinationContentInfo)
2333
    {
2334
        self::assertInternalType('array', $relations);
2335
        self::assertCount(1, $relations);
2336
        self::assertInstanceOf('eZ\\Publish\\API\\Repository\\Values\\Content\\Relation', $relations[0]);
2337
        self::assertNotNull($relations[0]->id);
2338
        self::assertEquals(Relation::COMMON, $relations[0]->type);
2339
        self::assertNull($relations[0]->sourceFieldDefinitionIdentifier);
2340
        self::assertEquals($sourceContentInfo, $relations[0]->sourceContentInfo);
2341
        self::assertEquals($destinationContentInfo, $relations[0]->destinationContentInfo);
2342
    }
2343
2344
    /**
2345
     * Test for the loadRelations() method.
2346
     *
2347
     * @covers \eZ\Publish\Core\Repository\ContentService::loadRelations
2348
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
2349
     */
2350 View Code Duplication
    public function testLoadRelationsThrowsUnauthorizedException()
2351
    {
2352
        list($contentDraft, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
2353
        $contentService = $this->repository->getContentService();
2354
2355
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId('a6e35cbcb7cd6ae4b691f3eee30cd262');
2356
2357
        $contentService->addRelation(
2358
            $contentDraft->getVersionInfo(),
2359
            $mediaContentInfo
2360
        );
2361
2362
        // Set anonymous as current user
2363
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
2364
2365
        $contentService->loadRelations($contentDraft->versionInfo);
2366
    }
2367
2368
    /**
2369
     * Test for the loadReverseRelations() method.
2370
     *
2371
     * @covers \eZ\Publish\Core\Repository\ContentService::loadReverseRelations
2372
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
2373
     */
2374
    public function testLoadReverseRelationsThrowsUnauthorizedException()
2375
    {
2376
        list($contentDraft, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
2377
        $contentService = $this->repository->getContentService();
2378
2379
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId('a6e35cbcb7cd6ae4b691f3eee30cd262');
2380
2381
        $contentService->addRelation(
2382
            $contentDraft->getVersionInfo(),
2383
            $mediaContentInfo
2384
        );
2385
2386
        // Set anonymous as current user
2387
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
2388
2389
        $contentService->loadReverseRelations($mediaContentInfo);
2390
    }
2391
2392
    /**
2393
     * Test for the addRelation() method.
2394
     *
2395
     * @covers \eZ\Publish\Core\Repository\ContentService::addRelation
2396
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
2397
     */
2398
    public function testAddRelationThrowsUnauthorizedException()
2399
    {
2400
        list($contentDraft, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
2401
        $contentService = $this->repository->getContentService();
2402
2403
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId('a6e35cbcb7cd6ae4b691f3eee30cd262');
2404
2405
        // Set anonymous as current user
2406
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
2407
2408
        $contentService->addRelation(
2409
            $contentDraft->getVersionInfo(),
2410
            $mediaContentInfo
2411
        );
2412
    }
2413
2414
    /**
2415
     * Test for the addRelation() method.
2416
     *
2417
     * @covers \eZ\Publish\Core\Repository\ContentService::addRelation
2418
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
2419
     */
2420 View Code Duplication
    public function testAddRelationThrowsBadStateException()
2421
    {
2422
        $contentService = $this->repository->getContentService();
2423
        list($contentDraft, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
2424
        $publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
2425
2426
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId('a6e35cbcb7cd6ae4b691f3eee30cd262');
2427
2428
        $contentService->addRelation(
2429
            $publishedContent->getVersionInfo(),
2430
            $mediaContentInfo
2431
        );
2432
    }
2433
2434
    /**
2435
     * Test for the deleteRelation() method.
2436
     *
2437
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteRelation
2438
     */
2439 View Code Duplication
    public function testDeleteRelation()
2440
    {
2441
        list($contentDraft, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
2442
        $contentService = $this->repository->getContentService();
2443
2444
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId('a6e35cbcb7cd6ae4b691f3eee30cd262');
2445
2446
        $contentService->addRelation(
2447
            $contentDraft->getVersionInfo(),
2448
            $mediaContentInfo
2449
        );
2450
2451
        $contentService->deleteRelation(
2452
            $contentDraft->getVersionInfo(),
2453
            $mediaContentInfo
2454
        );
2455
2456
        $relations = $contentService->loadRelations($contentDraft->versionInfo);
2457
2458
        self::assertCount(0, $relations);
2459
    }
2460
2461
    /**
2462
     * Test for the deleteRelation() method.
2463
     *
2464
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteRelation
2465
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
2466
     */
2467 View Code Duplication
    public function testDeleteRelationThrowsUnauthorizedException()
2468
    {
2469
        list($contentDraft, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
2470
        $contentService = $this->repository->getContentService();
2471
2472
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId('a6e35cbcb7cd6ae4b691f3eee30cd262');
2473
2474
        $contentService->addRelation(
2475
            $contentDraft->getVersionInfo(),
2476
            $mediaContentInfo
2477
        );
2478
2479
        // Set anonymous as current user
2480
        $this->repository->setCurrentUser($this->getStubbedUser(10));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
2481
2482
        $contentService->deleteRelation(
2483
            $contentDraft->getVersionInfo(),
2484
            $mediaContentInfo
2485
        );
2486
    }
2487
2488
    /**
2489
     * Test for the deleteRelation() method.
2490
     *
2491
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteRelation
2492
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
2493
     */
2494 View Code Duplication
    public function testDeleteRelationThrowsBadStateException()
2495
    {
2496
        $contentService = $this->repository->getContentService();
2497
        list($contentDraft, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
2498
2499
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId('a6e35cbcb7cd6ae4b691f3eee30cd262');
2500
2501
        $contentService->addRelation(
2502
            $contentDraft->getVersionInfo(),
2503
            $mediaContentInfo
2504
        );
2505
2506
        $publishedContent = $contentService->publishVersion($contentDraft->versionInfo);
2507
2508
        $contentService->deleteRelation(
2509
            $publishedContent->getVersionInfo(),
2510
            $mediaContentInfo
2511
        );
2512
    }
2513
2514
    /**
2515
     * Test for the deleteRelation() method.
2516
     *
2517
     * @covers \eZ\Publish\Core\Repository\ContentService::deleteRelation
2518
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
2519
     */
2520
    public function testDeleteRelationThrowsInvalidArgumentException()
2521
    {
2522
        list($contentDraft, $contentType) = $this->createTestContent();
0 ignored issues
show
Unused Code introduced by
The assignment to $contentType is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
2523
        $contentService = $this->repository->getContentService();
2524
2525
        $mediaContentInfo = $contentService->loadContentInfoByRemoteId('a6e35cbcb7cd6ae4b691f3eee30cd262');
2526
2527
        $contentService->deleteRelation(
2528
            $contentDraft->getVersionInfo(),
2529
            $mediaContentInfo
2530
        );
2531
    }
2532
2533
    /**
2534
     * Creates and returns content draft used in testing.
2535
     *
2536
     * @return \eZ\Publish\API\Repository\Values\Content\Content
2537
     */
2538
    protected function createTestContent()
2539
    {
2540
        $contentService = $this->repository->getContentService();
2541
        $testContentType = $this->createTestContentType();
2542
2543
        $contentCreate = $contentService->newContentCreateStruct($testContentType, 'eng-GB');
2544
        $contentCreate->setField('test_required_empty', 'val-11');
2545
        $contentCreate->setField('test_required_not_empty', 'val-12');
2546
        $contentCreate->setField('test_translatable', 'val-13');
2547
        $contentCreate->setField('test_untranslatable', 'val-14');
2548
        $contentCreate->setField('test_translatable', 'val-23', 'eng-US');
2549
        $contentCreate->sectionId = 1;
2550
        $contentCreate->ownerId = 14;
2551
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
2552
        $contentCreate->alwaysAvailable = true;
2553
2554
        $locationCreates = [
2555
            new LocationCreateStruct(
2556
                [
2557
                    //priority = 0
2558
                    //hidden = false
2559
                    'remoteId' => 'db787a9143f57828dd4331573466a013',
2560
                    //sortField = Location::SORT_FIELD_NAME
2561
                    //sortOrder = Location::SORT_ORDER_ASC
2562
                    'parentLocationId' => 2,
2563
                ]
2564
            ),
2565
            new LocationCreateStruct(
2566
                [
2567
                    //priority = 0
2568
                    //hidden = false
2569
                    'remoteId' => 'a3dd7c1c9e04c89e446a70f647286e6b',
2570
                    //sortField = Location::SORT_FIELD_NAME
2571
                    //sortOrder = Location::SORT_ORDER_ASC
2572
                    'parentLocationId' => 5,
2573
                ]
2574
            ),
2575
        ];
2576
2577
        return [$contentService->createContent($contentCreate, $locationCreates), $testContentType];
2578
    }
2579
2580
    /**
2581
     * Returns ContentType used in testing.
2582
     *
2583
     * @return \eZ\Publish\API\Repository\Values\ContentType\ContentType
2584
     */
2585
    protected function createTestContentType()
2586
    {
2587
        $contentTypeService = $this->repository->getContentTypeService();
2588
        $typeCreateStruct = $contentTypeService->newContentTypeCreateStruct(
2589
            'test-type'
2590
        );
2591
        $typeCreateStruct->names = ['eng-GB' => 'Test type name'];
2592
        $typeCreateStruct->descriptions = ['eng-GB' => 'Test type description'];
2593
        $typeCreateStruct->remoteId = 'test-type-remoteid';
2594
        $typeCreateStruct->creatorId = $this->repository->getCurrentUserReference()->getUserId();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Reposito...tCurrentUserReference() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user reference.

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...
2595
        $typeCreateStruct->creationDate = $this->getDateTime(0);
2596
        $typeCreateStruct->mainLanguageCode = 'eng-GB';
2597
        $typeCreateStruct->nameSchema = '<test_required_empty>';
2598
        $typeCreateStruct->urlAliasSchema = '<test_required_empty>';
2599
2600
        $fieldCreate = $contentTypeService->newFieldDefinitionCreateStruct('test_required_empty', 'ezstring');
2601
        $fieldCreate->names = ['eng-GB' => 'Test required empty'];
2602
        $fieldCreate->descriptions = ['eng-GB' => 'Required field with empty default value'];
2603
        $fieldCreate->fieldGroup = 'test-field-group';
2604
        $fieldCreate->position = 1;
2605
        $fieldCreate->isTranslatable = false;
2606
        $fieldCreate->isRequired = true;
2607
        $fieldCreate->isInfoCollector = false;
2608
        $fieldCreate->isSearchable = true;
2609
        $fieldCreate->defaultValue = '';
2610
        //$validator = new StringLengthValidator();
2611
        //$validator->maxStringLength = 64;
2612
        //$fieldCreate->validatorConfiguration = array( $validator );
2613
        $fieldCreate->validatorConfiguration = [
2614
            'StringLengthValidator' => [
2615
                'maxStringLength' => 64,
2616
            ],
2617
        ];
2618
        //$fieldCreate->fieldSettings
2619
        $typeCreateStruct->addFieldDefinition($fieldCreate);
2620
2621
        $fieldCreate = $contentTypeService->newFieldDefinitionCreateStruct('test_required_not_empty', 'ezstring');
2622
        $fieldCreate->names = ['eng-GB' => 'Test required not empty'];
2623
        $fieldCreate->descriptions = ['eng-GB' => 'Required field with default value not empty'];
2624
        $fieldCreate->fieldGroup = 'test-field-group';
2625
        $fieldCreate->position = 2;
2626
        $fieldCreate->isTranslatable = false;
2627
        $fieldCreate->isRequired = true;
2628
        $fieldCreate->isInfoCollector = false;
2629
        $fieldCreate->isSearchable = true;
2630
        $fieldCreate->defaultValue = 'dummy default data';
2631
        //$fieldCreate->validators
2632
        //$fieldCreate->fieldSettings
2633
        $typeCreateStruct->addFieldDefinition($fieldCreate);
2634
2635
        $fieldCreate = $contentTypeService->newFieldDefinitionCreateStruct('test_translatable', 'ezstring');
2636
        $fieldCreate->names = ['eng-GB' => 'Test translatable'];
2637
        $fieldCreate->descriptions = ['eng-GB' => 'Translatable field'];
2638
        $fieldCreate->fieldGroup = 'test-field-group';
2639
        $fieldCreate->position = 3;
2640
        $fieldCreate->isTranslatable = true;
2641
        $fieldCreate->isRequired = false;
2642
        $fieldCreate->isInfoCollector = false;
2643
        $fieldCreate->isSearchable = true;
2644
        $fieldCreate->defaultValue = '';
2645
        //$fieldCreate->validators
2646
        //$fieldCreate->fieldSettings
2647
        $typeCreateStruct->addFieldDefinition($fieldCreate);
2648
2649
        $fieldCreate = $contentTypeService->newFieldDefinitionCreateStruct('test_untranslatable', 'ezstring');
2650
        $fieldCreate->names = ['eng-GB' => 'Test not translatable'];
2651
        $fieldCreate->descriptions = ['eng-GB' => 'Untranslatable field'];
2652
        $fieldCreate->fieldGroup = 'test-field-group';
2653
        $fieldCreate->position = 4;
2654
        $fieldCreate->isTranslatable = false;
2655
        $fieldCreate->isRequired = false;
2656
        $fieldCreate->isInfoCollector = false;
2657
        $fieldCreate->isSearchable = true;
2658
        $fieldCreate->defaultValue = '';
2659
        //$fieldCreate->validators
2660
        //$fieldCreate->fieldSettings
2661
        $typeCreateStruct->addFieldDefinition($fieldCreate);
2662
2663
        $contentTypeDraft = $contentTypeService->createContentType(
2664
            $typeCreateStruct,
2665
            [$contentTypeService->loadContentTypeGroup(1)]
2666
        );
2667
        $contentTypeId = $contentTypeDraft->id;
2668
2669
        $contentTypeService->publishContentTypeDraft($contentTypeDraft);
2670
2671
        return $contentTypeService->loadContentType($contentTypeId);
2672
    }
2673
}
2674