Completed
Push — master ( cf2d59...36802d )
by André
16:28
created

testLoadObjectStateStructValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 1
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the ObjectStateServiceTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\API\Repository\Tests;
10
11
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct;
12
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct;
13
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup;
14
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct;
15
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct;
16
use eZ\Publish\API\Repository\Values\ObjectState\ObjectState;
17
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
18
19
/**
20
 * Test case for operations in the ObjectStateService using in memory storage.
21
 *
22
 * @see \eZ\Publish\API\Repository\ObjectStateService
23
 * @group object-state
24
 */
25
class ObjectStateServiceTest extends BaseTest
26
{
27
    /**
28
     * Test for the newObjectStateGroupCreateStruct() method.
29
     *
30
     *
31
     * @see \eZ\Publish\API\Repository\ObjectStateService::newObjectStateGroupCreateStruct()
32
     */
33
    public function testNewObjectStateGroupCreateStruct()
34
    {
35
        $repository = $this->getRepository();
36
37
        /* BEGIN: Use Case */
38
        $objectStateService = $repository->getObjectStateService();
39
40
        $objectStateGroupCreate = $objectStateService->newObjectStateGroupCreateStruct(
41
            'publishing'
42
        );
43
        /* END: Use Case */
44
45
        $this->assertInstanceOf(
46
            '\\eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectStateGroupCreateStruct',
47
            $objectStateGroupCreate
48
        );
49
50
        return $objectStateGroupCreate;
51
    }
52
53
    /**
54
     * testNewObjectStateGroupCreateStructValues.
55
     *
56
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct $objectStateGroupCreate
57
     *
58
     *
59
     * @depends testNewObjectStateGroupCreateStruct
60
     */
61
    public function testNewObjectStateGroupCreateStructValues(ObjectStateGroupCreateStruct $objectStateGroupCreate)
62
    {
63
        $this->assertPropertiesCorrect(
64
            array(
65
                'identifier' => 'publishing',
66
                'defaultLanguageCode' => null,
67
                'names' => null,
68
                'descriptions' => null,
69
            ),
70
            $objectStateGroupCreate
71
        );
72
    }
73
74
    /**
75
     * Test for the newObjectStateGroupUpdateStruct() method.
76
     *
77
     *
78
     * @see \eZ\Publish\API\Repository\ObjectStateService::newObjectStateGroupUpdateStruct()
79
     */
80
    public function testNewObjectStateGroupUpdateStruct()
81
    {
82
        $repository = $this->getRepository();
83
84
        /* BEGIN: Use Case */
85
        $objectStateService = $repository->getObjectStateService();
86
87
        $objectStateGroupUpdate = $objectStateService->newObjectStateGroupUpdateStruct();
88
        /* END: Use Case */
89
90
        $this->assertInstanceOf(
91
            '\\eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectStateGroupUpdateStruct',
92
            $objectStateGroupUpdate
93
        );
94
95
        return $objectStateGroupUpdate;
96
    }
97
98
    /**
99
     * testNewObjectStateGroupUpdateStructValues.
100
     *
101
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct $objectStateGroupUpdate
102
     *
103
     *
104
     * @depends testNewObjectStateGroupUpdateStruct
105
     */
106
    public function testNewObjectStateGroupUpdateStructValues(ObjectStateGroupUpdateStruct $objectStateGroupUpdate)
107
    {
108
        $this->assertPropertiesCorrect(
109
            array(
110
                'identifier' => null,
111
                'defaultLanguageCode' => null,
112
                'names' => null,
113
                'descriptions' => null,
114
            ),
115
            $objectStateGroupUpdate
116
        );
117
    }
118
119
    /**
120
     * Test for the newObjectStateCreateStruct() method.
121
     *
122
     *
123
     * @see \eZ\Publish\API\Repository\ObjectStateService::newObjectStateCreateStruct()
124
     */
125
    public function testNewObjectStateCreateStruct()
126
    {
127
        $repository = $this->getRepository();
128
129
        /* BEGIN: Use Case */
130
        $objectStateService = $repository->getObjectStateService();
131
132
        $objectStateCreate = $objectStateService->newObjectStateCreateStruct(
133
            'pending'
134
        );
135
        /* END: Use Case */
136
137
        $this->assertInstanceOf(
138
            '\\eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectStateCreateStruct',
139
            $objectStateCreate
140
        );
141
142
        return $objectStateCreate;
143
    }
144
145
    /**
146
     * testNewObjectStateCreateStructValues.
147
     *
148
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct $objectStateCreate
149
     *
150
     *
151
     * @depends testNewObjectStateCreateStruct
152
     */
153
    public function testNewObjectStateCreateStructValues(ObjectStateCreateStruct $objectStateCreate)
154
    {
155
        $this->assertPropertiesCorrect(
156
            array(
157
                'identifier' => 'pending',
158
                'priority' => false,
159
                'defaultLanguageCode' => null,
160
                'names' => null,
161
                'descriptions' => null,
162
            ),
163
            $objectStateCreate
164
        );
165
    }
166
167
    /**
168
     * Test for the newObjectStateUpdateStruct() method.
169
     *
170
     *
171
     * @see \eZ\Publish\API\Repository\ObjectStateService::newObjectStateUpdateStruct()
172
     */
173
    public function testNewObjectStateUpdateStruct()
174
    {
175
        $repository = $this->getRepository();
176
177
        /* BEGIN: Use Case */
178
        $objectStateService = $repository->getObjectStateService();
179
180
        $objectStateUpdate = $objectStateService->newObjectStateUpdateStruct();
181
        /* END: Use Case */
182
183
        $this->assertInstanceOf(
184
            '\\eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectStateUpdateStruct',
185
            $objectStateUpdate
186
        );
187
188
        return $objectStateUpdate;
189
    }
190
191
    /**
192
     * testNewObjectStateUpdateStructValues.
193
     *
194
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct $objectStateUpdate
195
     *
196
     *
197
     * @depends testNewObjectStateUpdateStruct
198
     */
199
    public function testNewObjectStateUpdateStructValues(ObjectStateUpdateStruct $objectStateUpdate)
200
    {
201
        $this->assertPropertiesCorrect(
202
            array(
203
                'identifier' => null,
204
                'defaultLanguageCode' => null,
205
                'names' => null,
206
                'descriptions' => null,
207
            ),
208
            $objectStateUpdate
209
        );
210
    }
211
212
    /**
213
     * Test for the createObjectStateGroup() method.
214
     *
215
     *
216
     * @see \eZ\Publish\API\Repository\ObjectStateService::createObjectStateGroup()
217
     * @depends testNewObjectStateGroupCreateStructValues
218
     */
219
    public function testCreateObjectStateGroup()
220
    {
221
        $repository = $this->getRepository();
222
223
        /* BEGIN: Use Case */
224
        $objectStateService = $repository->getObjectStateService();
225
226
        $objectStateGroupCreate = $objectStateService->newObjectStateGroupCreateStruct(
227
            'publishing'
228
        );
229
        $objectStateGroupCreate->defaultLanguageCode = 'eng-US';
230
        $objectStateGroupCreate->names = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Publi...-DE' => 'Sindelfingen') of type array<string,string,{"en...ng","ger-DE":"string"}> is incompatible with the declared type array<integer,string> of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
231
            'eng-US' => 'Publishing',
232
            'ger-DE' => 'Sindelfingen',
233
        );
234
        $objectStateGroupCreate->descriptions = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Put s...ing ton Sindelfingen.') of type array<string,string,{"en...ng","ger-DE":"string"}> is incompatible with the declared type array<integer,string> of property $descriptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
235
            'eng-US' => 'Put something online',
236
            'ger-DE' => 'Put something ton Sindelfingen.',
237
        );
238
239
        $createdObjectStateGroup = $objectStateService->createObjectStateGroup(
240
            $objectStateGroupCreate
241
        );
242
        /* END: Use Case */
243
244
        $this->assertInstanceOf(
245
            '\\eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectStateGroup',
246
            $createdObjectStateGroup
247
        );
248
249
        return $createdObjectStateGroup;
250
    }
251
252
    /**
253
     * testCreateObjectStateGroupStructValues.
254
     *
255
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $createdObjectStateGroup
256
     *
257
     *
258
     * @depends testCreateObjectStateGroup
259
     */
260
    public function testCreateObjectStateGroupStructValues(ObjectStateGroup $createdObjectStateGroup)
261
    {
262
        $this->assertPropertiesCorrect(
263
            [
264
                'identifier' => 'publishing',
265
                'mainLanguageCode' => 'eng-US',
266
                'languageCodes' => ['eng-US', 'ger-DE'],
267
                'names' => [
268
                    'eng-US' => 'Publishing',
269
                    'ger-DE' => 'Sindelfingen',
270
                ],
271
                'descriptions' => [
272
                    'eng-US' => 'Put something online',
273
                    'ger-DE' => 'Put something ton Sindelfingen.',
274
                ],
275
            ],
276
            $createdObjectStateGroup
277
        );
278
        $this->assertNotNull($createdObjectStateGroup->id);
279
    }
280
281
    /**
282
     * Test for the createObjectStateGroup() method.
283
     *
284
     *
285
     * @see \eZ\Publish\API\Repository\ObjectStateService::createObjectStateGroup()
286
     * @depends testCreateObjectStateGroup
287
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
288
     */
289
    public function testCreateObjectStateGroupThrowsInvalidArgumentException()
290
    {
291
        $repository = $this->getRepository();
292
293
        $objectStateService = $repository->getObjectStateService();
294
295
        $objectStateGroupCreate = $objectStateService->newObjectStateGroupCreateStruct(
296
            // 'ez_lock' is already existing identifier
297
            'ez_lock'
298
        );
299
        $objectStateGroupCreate->defaultLanguageCode = 'eng-US';
300
        $objectStateGroupCreate->names = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Publi...-GB' => 'Sindelfingen') of type array<string,string,{"en...ng","eng-GB":"string"}> is incompatible with the declared type array<integer,string> of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
301
            'eng-US' => 'Publishing',
302
            'eng-GB' => 'Sindelfingen',
303
        );
304
        $objectStateGroupCreate->descriptions = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Put s...ing ton Sindelfingen.') of type array<string,string,{"en...ng","eng-GB":"string"}> is incompatible with the declared type array<integer,string> of property $descriptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
305
            'eng-US' => 'Put something online',
306
            'eng-GB' => 'Put something ton Sindelfingen.',
307
        );
308
309
        // This call will fail because group with 'ez_lock' identifier already exists
310
        $objectStateService->createObjectStateGroup(
311
            $objectStateGroupCreate
312
        );
313
    }
314
315
    /**
316
     * Test for the loadObjectStateGroup() method.
317
     *
318
     * @covers \eZ\Publish\API\Repository\ObjectStateService::loadObjectStateGroup
319
     */
320
    public function testLoadObjectStateGroup()
321
    {
322
        $repository = $this->getRepository();
323
324
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
325
        /* BEGIN: Use Case */
326
        // $objectStateGroupId contains the ID of the standard object state
327
        // group ez_lock.
328
        $objectStateService = $repository->getObjectStateService();
329
330
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
331
            $objectStateGroupId
332
        );
333
        /* END: Use Case */
334
335
        $this->assertInstanceOf(
336
            ObjectStateGroup::class,
337
            $loadedObjectStateGroup
338
        );
339
340
        $this->assertPropertiesCorrect(
341
            [
342
                'id' => 2,
343
                'identifier' => 'ez_lock',
344
                'mainLanguageCode' => 'eng-US',
345
                'languageCodes' => ['eng-US'],
346
                'names' => ['eng-US' => 'Lock'],
347
                'descriptions' => ['eng-US' => ''],
348
            ],
349
            $loadedObjectStateGroup
350
        );
351
    }
352
353
    /**
354
     * Test for the loadObjectStateGroup() method.
355
     *
356
     *
357
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectStateGroup()
358
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
359
     * @depends testLoadObjectStateGroup
360
     */
361
    public function testLoadObjectStateGroupThrowsNotFoundException()
362
    {
363
        $repository = $this->getRepository();
364
365
        $nonExistentObjectStateGroupId = $this->generateId('objectstategroup', self::DB_INT_MAX);
366
        /* BEGIN: Use Case */
367
        // $nonExistentObjectStateGroupId contains an ID for an object state
368
        // that does not exist
369
        $objectStateService = $repository->getObjectStateService();
370
371
        // Throws a not found exception
372
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
0 ignored issues
show
Unused Code introduced by
$loadedObjectStateGroup is not used, you could remove the assignment.

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

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

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

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

Loading history...
373
            $nonExistentObjectStateGroupId
374
        );
375
        /* END: Use Case */
376
    }
377
378
    /**
379
     * Test for the loadObjectStateGroups() method.
380
     *
381
     *
382
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectStateGroups()
383
     * @depends testLoadObjectStateGroup
384
     */
385
    public function testLoadObjectStateGroups()
386
    {
387
        $repository = $this->getRepository();
388
389
        $expectedGroupIdentifiers = $this->getGroupIdentifierMap($this->createObjectStateGroups());
390
        $expectedGroupIdentifiers['ez_lock'] = true;
391
392
        /* BEGIN: Use Case */
393
        $objectStateService = $repository->getObjectStateService();
394
395
        $loadedObjectStateGroups = $objectStateService->loadObjectStateGroups();
396
        /* END: Use Case */
397
398
        $this->assertInternalType('array', $loadedObjectStateGroups);
399
400
        $this->assertObjectsLoadedByIdentifiers(
401
            $expectedGroupIdentifiers,
402
            $loadedObjectStateGroups,
403
            'ObjectStateGroup'
404
        );
405
    }
406
407
    /**
408
     * Creates a set of object state groups and returns an array of all
409
     * existing group identifiers after creation.
410
     *
411
     * @return bool[]
412
     */
413
    protected function createObjectStateGroups()
414
    {
415
        $repository = $this->getRepository();
416
        $objectStateService = $repository->getObjectStateService();
417
418
        $identifiersToCreate = [
419
            'first',
420
            'second',
421
            'third',
422
        ];
423
424
        $createdStateGroups = [];
425
426
        $groupCreateStruct = $objectStateService->newObjectStateGroupCreateStruct('dummy');
427
428
        $groupCreateStruct->defaultLanguageCode = 'eng-US';
429
        $groupCreateStruct->names = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Foo', 'ger-DE' => 'GerFoo') of type array<string,string,{"en...ng","ger-DE":"string"}> is incompatible with the declared type array<integer,string> of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
430
            'eng-US' => 'Foo',
431
            'ger-DE' => 'GerFoo',
432
        ];
433
        $groupCreateStruct->descriptions = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Foo B..., 'ger-DE' => 'GerBar') of type array<string,string,{"en...ng","ger-DE":"string"}> is incompatible with the declared type array<integer,string> of property $descriptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
434
            'eng-US' => 'Foo Bar',
435
            'ger-DE' => 'GerBar',
436
        ];
437
438
        foreach ($identifiersToCreate as $identifier) {
439
            $groupCreateStruct->identifier = $identifier;
440
            $createdStateGroups[] = $objectStateService->createObjectStateGroup($groupCreateStruct);
441
        }
442
443
        return $createdStateGroups;
444
    }
445
446
    /**
447
     * Assert object identifiers.
448
     *
449
     * @param array $expectedIdentifiers
450
     * @param array $loadedObjects
451
     * @param string $class
452
     */
453
    protected function assertObjectsLoadedByIdentifiers(array $expectedIdentifiers, array $loadedObjects, $class)
454
    {
455
        foreach ($loadedObjects as $loadedObject) {
456
            if (!isset($expectedIdentifiers[$loadedObject->identifier])) {
457
                $this->fail(
458
                    sprintf(
459
                        'Loaded not expected %s with identifier "%s"',
460
                        $class,
461
                        $loadedObject->identifier
462
                    )
463
                );
464
            }
465
            unset($expectedIdentifiers[$loadedObject->identifier]);
466
        }
467
468
        if (!empty($expectedIdentifiers)) {
469
            $this->fail(
470
                sprintf(
471
                    'Expected %ss with identifiers "%s" not loaded.',
472
                    $class,
473
                    implode('", "', $expectedIdentifiers)
474
                )
475
            );
476
        }
477
    }
478
479
    /**
480
     * Test for the loadObjectStateGroups() method.
481
     *
482
     *
483
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectStateGroups($offset)
484
     * @depends testLoadObjectStateGroups
485
     */
486 View Code Duplication
    public function testLoadObjectStateGroupsWithOffset()
487
    {
488
        $repository = $this->getRepository();
489
        $objectStateService = $repository->getObjectStateService();
490
491
        $this->createObjectStateGroups();
492
493
        $allObjectStateGroups = $objectStateService->loadObjectStateGroups();
494
495
        $existingGroupIdentifiers = $this->getGroupIdentifierMap($allObjectStateGroups);
496
497
        /* BEGIN: Use Case */
498
        $objectStateService = $repository->getObjectStateService();
499
500
        $loadedObjectStateGroups = $objectStateService->loadObjectStateGroups(2);
501
        /* END: Use Case */
502
503
        $this->assertInternalType('array', $loadedObjectStateGroups);
504
505
        $this->assertObjectsLoadedByIdentifiers(
506
            array_slice($existingGroupIdentifiers, 2),
507
            $loadedObjectStateGroups,
508
            'ObjectStateGroup'
509
        );
510
    }
511
512
    /**
513
     * Returns a map of the given object state groups.
514
     *
515
     * @param array $groups
516
     * @return array
517
     */
518
    protected function getGroupIdentifierMap(array $groups)
519
    {
520
        $existingGroupIdentifiers = array_map(
521
            function ($group) {
522
                return $group->identifier;
523
            },
524
            $groups
525
        );
526
527
        return array_fill_keys($existingGroupIdentifiers, true);
528
    }
529
530
    /**
531
     * Test for the loadObjectStateGroups() method.
532
     *
533
     *
534
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectStateGroups($offset, $limit)
535
     * @depends testLoadObjectStateGroupsWithOffset
536
     */
537 View Code Duplication
    public function testLoadObjectStateGroupsWithOffsetAndLimit()
538
    {
539
        $repository = $this->getRepository();
540
        $objectStateService = $repository->getObjectStateService();
541
542
        $allObjectStateGroups = $objectStateService->loadObjectStateGroups();
543
544
        $existingGroupIdentifiers = $this->getGroupIdentifierMap($allObjectStateGroups);
545
546
        /* BEGIN: Use Case */
547
        $objectStateService = $repository->getObjectStateService();
548
549
        $loadedObjectStateGroups = $objectStateService->loadObjectStateGroups(1, 2);
550
        /* END: Use Case */
551
552
        $this->assertInternalType('array', $loadedObjectStateGroups);
553
554
        $this->assertObjectsLoadedByIdentifiers(
555
            array_slice($existingGroupIdentifiers, 1, 2),
556
            $loadedObjectStateGroups,
557
            'ObjectStateGroup'
558
        );
559
    }
560
561
    /**
562
     * Test for the loadObjectStates() method.
563
     *
564
     *
565
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectStates()
566
     * @depends testLoadObjectStateGroup
567
     */
568
    public function testLoadObjectStates()
569
    {
570
        $repository = $this->getRepository();
571
572
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
573
        /* BEGIN: Use Case */
574
        // $objectStateGroupId contains the ID of the standard object state
575
        // group ez_lock.
576
        $objectStateService = $repository->getObjectStateService();
577
578
        $objectStateGroup = $objectStateService->loadObjectStateGroup(
579
            $objectStateGroupId
580
        );
581
582
        // Loads all object states in $objectStateGroup
583
        $loadedObjectStates = $objectStateService->loadObjectStates($objectStateGroup);
584
        /* END: Use Case */
585
586
        $this->assertInternalType(
587
            'array',
588
            $loadedObjectStates
589
        );
590
        $this->assertObjectsLoadedByIdentifiers(
591
            array('not_locked' => true, 'locked' => true),
592
            $loadedObjectStates,
593
            'ObjectState'
594
        );
595
    }
596
597
    /**
598
     * Test for the updateObjectStateGroup() method.
599
     *
600
     * @covers \eZ\Publish\API\Repository\ObjectStateService::updateObjectStateGroup
601
     * @depends eZ\Publish\API\Repository\Tests\ObjectStateServiceTest::testLoadObjectStateGroup
602
     */
603
    public function testUpdateObjectStateGroup()
604
    {
605
        $repository = $this->getRepository();
606
607
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
608
        /* BEGIN: Use Case */
609
        // $objectStateGroupId contains the ID of the standard object state
610
        // group ez_lock.
611
        $objectStateService = $repository->getObjectStateService();
612
613
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
614
            $objectStateGroupId
615
        );
616
617
        $groupUpdateStruct = $objectStateService->newObjectStateGroupUpdateStruct();
618
        $groupUpdateStruct->identifier = 'sindelfingen';
619
        $groupUpdateStruct->defaultLanguageCode = 'ger-DE';
620
        $groupUpdateStruct->names = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('ger-DE' => 'Sindelfingen') of type array<string,string,{"ger-DE":"string"}> is incompatible with the declared type array<integer,string> of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
621
            'ger-DE' => 'Sindelfingen',
622
        );
623
        $groupUpdateStruct->descriptions = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('ger-DE' => 'Sinde... nicht nur eine Stadt') of type array<string,string,{"ger-DE":"string"}> is incompatible with the declared type array<integer,string> of property $descriptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
624
            'ger-DE' => 'Sindelfingen ist nicht nur eine Stadt',
625
        );
626
627
        // Updates the $loadObjectStateGroup with the data from
628
        // $groupUpdateStruct and returns the updated group
629
        $updatedObjectStateGroup = $objectStateService->updateObjectStateGroup(
630
            $loadedObjectStateGroup,
631
            $groupUpdateStruct
632
        );
633
        /* END: Use Case */
634
635
        $this->assertInstanceOf(
636
            '\\eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectStateGroup',
637
            $updatedObjectStateGroup
638
        );
639
640
        return array(
641
            $loadedObjectStateGroup,
642
            $groupUpdateStruct,
643
            $updatedObjectStateGroup,
644
        );
645
    }
646
647
    /**
648
     * Test service method for partially updating object state group.
649
     *
650
     * @covers \eZ\Publish\API\Repository\ObjectStateService::updateObjectStateGroup
651
     * @depends eZ\Publish\API\Repository\Tests\ObjectStateServiceTest::testLoadObjectStateGroup
652
     */
653
    public function testUpdateObjectStateGroupChosenFieldsOnly()
654
    {
655
        $repository = $this->getRepository();
656
        $objectStateService = $repository->getObjectStateService();
657
658
        $groupUpdateStruct = $objectStateService->newObjectStateGroupUpdateStruct();
659
        $groupUpdateStruct->defaultLanguageCode = 'eng-GB';
660
        $groupUpdateStruct->names = ['eng-GB' => 'Test'];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-GB' => 'Test') of type array<string,string,{"eng-GB":"string"}> is incompatible with the declared type array<integer,string> of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
661
662
        $group = $objectStateService->loadObjectStateGroup(2);
663
664
        $updatedGroup = $objectStateService->updateObjectStateGroup($group, $groupUpdateStruct);
665
666
        $this->assertInstanceOf(
667
            ObjectStateGroup::class,
668
            $updatedGroup
669
        );
670
671
        $this->assertPropertiesCorrect(
672
            [
673
                'id' => 2,
674
                'identifier' => 'ez_lock',
675
                'mainLanguageCode' => 'eng-GB',
676
                'languageCodes' => ['eng-GB'],
677
                'names' => ['eng-GB' => 'Test'],
678
                // descriptions array should have an empty value for eng-GB
679
                // without the original descriptions
680
                // since the descriptions were not in the update struct and we're changing default language
681
                'descriptions' => ['eng-GB' => ''],
682
            ],
683
            $updatedGroup
684
        );
685
    }
686
687
    /**
688
     * Test for the updateObjectStateGroup() method.
689
     *
690
     *
691
     * @see \eZ\Publish\API\Repository\ObjectStateService::updateObjectStateGroup()
692
     * @depends testUpdateObjectStateGroup
693
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
694
     */
695
    public function testUpdateObjectStateGroupThrowsInvalidArgumentException()
696
    {
697
        $repository = $this->getRepository();
698
699
        $objectStateService = $repository->getObjectStateService();
700
701
        // Create object state group which we will later update
702
        $objectStateGroupCreate = $objectStateService->newObjectStateGroupCreateStruct(
703
            'publishing'
704
        );
705
        $objectStateGroupCreate->defaultLanguageCode = 'eng-US';
706
        $objectStateGroupCreate->names = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Publi...-GB' => 'Sindelfingen') of type array<string,string,{"en...ng","eng-GB":"string"}> is incompatible with the declared type array<integer,string> of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
707
            'eng-US' => 'Publishing',
708
            'eng-GB' => 'Sindelfingen',
709
        );
710
        $objectStateGroupCreate->descriptions = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Put s...ing ton Sindelfingen.') of type array<string,string,{"en...ng","eng-GB":"string"}> is incompatible with the declared type array<integer,string> of property $descriptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
711
            'eng-US' => 'Put something online',
712
            'eng-GB' => 'Put something ton Sindelfingen.',
713
        );
714
715
        $createdObjectStateGroup = $objectStateService->createObjectStateGroup(
716
            $objectStateGroupCreate
717
        );
718
719
        $groupUpdateStruct = $objectStateService->newObjectStateGroupUpdateStruct();
720
        // 'ez_lock' is the identifier of already existing group
721
        $groupUpdateStruct->identifier = 'ez_lock';
722
        $groupUpdateStruct->defaultLanguageCode = 'ger-DE';
723
        $groupUpdateStruct->names = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('ger-DE' => 'Sindelfingen') of type array<string,string,{"ger-DE":"string"}> is incompatible with the declared type array<integer,string> of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
724
            'ger-DE' => 'Sindelfingen',
725
        );
726
        $groupUpdateStruct->descriptions = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('ger-DE' => 'Sinde... nicht nur eine Stadt') of type array<string,string,{"ger-DE":"string"}> is incompatible with the declared type array<integer,string> of property $descriptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
727
            'ger-DE' => 'Sindelfingen ist nicht nur eine Stadt',
728
        );
729
730
        // This call will fail since state group with 'ez_lock' identifier already exists
731
        $objectStateService->updateObjectStateGroup(
732
            $createdObjectStateGroup,
733
            $groupUpdateStruct
734
        );
735
    }
736
737
    /**
738
     * testUpdateObjectStateGroupStructValues.
739
     *
740
     * @param array $testData
741
     *
742
     *
743
     * @depends testUpdateObjectStateGroup
744
     */
745
    public function testUpdateObjectStateGroupStructValues(array $testData)
746
    {
747
        list(
748
            $loadedObjectStateGroup,
0 ignored issues
show
Unused Code introduced by
The assignment to $loadedObjectStateGroup 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...
749
            $groupUpdateStruct,
750
            $updatedObjectStateGroup
751
        ) = $testData;
752
753
        $this->assertStructPropertiesCorrect(
754
            $groupUpdateStruct,
755
            $updatedObjectStateGroup
756
        );
757
    }
758
759
    /**
760
     * Test for the createObjectState() method.
761
     *
762
     *
763
     * @see \eZ\Publish\API\Repository\ObjectStateService::createObjectState()
764
     * @depends testLoadObjectStateGroup
765
     * @depends testNewObjectStateCreateStruct
766
     */
767 View Code Duplication
    public function testCreateObjectState()
768
    {
769
        $repository = $this->getRepository();
770
771
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
772
        /* BEGIN: Use Case */
773
        // $objectStateGroupId contains the ID of the standard object state
774
        // group ez_lock.
775
        $objectStateService = $repository->getObjectStateService();
776
777
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
778
            $objectStateGroupId
779
        );
780
781
        $objectStateCreateStruct = $objectStateService->newObjectStateCreateStruct(
782
            'locked_and_unlocked'
783
        );
784
        $objectStateCreateStruct->priority = 23;
785
        $objectStateCreateStruct->defaultLanguageCode = 'eng-US';
786
        $objectStateCreateStruct->names = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Locke...sen und ungeschlossen') of type array<string,string,{"en...ng","ger-DE":"string"}> is incompatible with the declared type array<integer,string> of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
787
            'eng-US' => 'Locked and Unlocked',
788
            'ger-DE' => 'geschlossen und ungeschlossen',
789
        ];
790
        $objectStateCreateStruct->descriptions = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'A sta...en und ungeschlossen.') of type array<string,string,{"en...ng","ger-DE":"string"}> is incompatible with the declared type array<integer,string> of property $descriptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
791
            'eng-US' => 'A state between locked and unlocked.',
792
            'ger-DE' => 'ein Zustand zwischen geschlossen und ungeschlossen.',
793
        );
794
795
        // Creates a new object state in the $loadObjectStateGroup with the
796
        // data from $objectStateCreateStruct
797
        $createdObjectState = $objectStateService->createObjectState(
798
            $loadedObjectStateGroup,
799
            $objectStateCreateStruct
800
        );
801
        /* END: Use Case */
802
803
        $this->assertInstanceOf(ObjectState::class, $createdObjectState);
804
        // Object sequences are renumbered
805
        $objectStateCreateStruct->priority = 2;
806
807
        return [
808
            $loadedObjectStateGroup,
809
            $objectStateCreateStruct,
810
            $createdObjectState,
811
        ];
812
    }
813
814
    /**
815
     * Test service method for creating object state in empty group.
816
     *
817
     * @covers \eZ\Publish\API\Repository\ObjectStateService::createObjectState
818
     */
819
    public function testCreateObjectStateInEmptyGroup()
820
    {
821
        $repository = $this->getRepository();
822
        $objectStateService = $repository->getObjectStateService();
823
824
        $groupCreateStruct = $objectStateService->newObjectStateGroupCreateStruct('test');
825
        $groupCreateStruct->defaultLanguageCode = 'eng-GB';
826
        $groupCreateStruct->names = ['eng-GB' => 'Test'];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-GB' => 'Test') of type array<string,string,{"eng-GB":"string"}> is incompatible with the declared type array<integer,string> of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
827
        $groupCreateStruct->descriptions = ['eng-GB' => 'Test description'];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-GB' => 'Test description') of type array<string,string,{"eng-GB":"string"}> is incompatible with the declared type array<integer,string> of property $descriptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
828
829
        $createdGroup = $objectStateService->createObjectStateGroup($groupCreateStruct);
830
831
        $stateCreateStruct = $objectStateService->newObjectStateCreateStruct('test');
832
        $stateCreateStruct->priority = 2;
833
        $stateCreateStruct->defaultLanguageCode = 'eng-GB';
834
        $stateCreateStruct->names = ['eng-GB' => 'Test'];
835
        $stateCreateStruct->descriptions = ['eng-GB' => 'Test description'];
836
837
        $createdState = $objectStateService->createObjectState(
838
            $createdGroup,
839
            $stateCreateStruct
840
        );
841
842
        $this->assertInstanceOf(
843
            ObjectState::class,
844
            $createdState
845
        );
846
847
        $this->assertNotNull($createdState->id);
848
        $this->assertPropertiesCorrect(
849
            [
850
                'identifier' => 'test',
851
                'priority' => 0,
852
                'mainLanguageCode' => 'eng-GB',
853
                'languageCodes' => ['eng-GB'],
854
                'names' => ['eng-GB' => 'Test'],
855
                'descriptions' => ['eng-GB' => 'Test description'],
856
            ],
857
            $createdState
858
        );
859
860
        $objectStateGroup = $createdState->getObjectStateGroup();
861
        $this->assertInstanceOf(
862
            ObjectStateGroup::class,
863
            $objectStateGroup
864
        );
865
866
        $this->assertEquals($createdGroup->id, $objectStateGroup->id);
867
        $this->assertGreaterThan(0, $objectStateService->getContentCount($createdState));
868
    }
869
870
    /**
871
     * Test for the createObjectState() method.
872
     *
873
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
874
     *
875
     * @see \eZ\Publish\API\Repository\ObjectStateService::createObjectState()
876
     * @depends testLoadObjectStateGroup
877
     * @depends testCreateObjectState
878
     */
879
    public function testCreateObjectStateThrowsInvalidArgumentException()
880
    {
881
        $repository = $this->getRepository();
882
883
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
884
        // $objectStateGroupId contains the ID of the standard object state
885
        // group ez_lock.
886
        $objectStateService = $repository->getObjectStateService();
887
888
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
889
            $objectStateGroupId
890
        );
891
892
        $objectStateCreateStruct = $objectStateService->newObjectStateCreateStruct(
893
            // 'not_locked' is the identifier of already existing state
894
            'not_locked'
895
        );
896
        $objectStateCreateStruct->priority = 23;
897
        $objectStateCreateStruct->defaultLanguageCode = 'eng-US';
898
        $objectStateCreateStruct->names = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Locked and Unlocked') of type array<string,string,{"eng-US":"string"}> is incompatible with the declared type array<integer,string> of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
899
            'eng-US' => 'Locked and Unlocked',
900
        );
901
        $objectStateCreateStruct->descriptions = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'A sta... locked and unlocked.') of type array<string,string,{"eng-US":"string"}> is incompatible with the declared type array<integer,string> of property $descriptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
902
            'eng-US' => 'A state between locked and unlocked.',
903
        );
904
905
        // This call will fail because object state with
906
        // 'not_locked' identifier already exists
907
        $objectStateService->createObjectState(
908
            $loadedObjectStateGroup,
909
            $objectStateCreateStruct
910
        );
911
    }
912
913
    /**
914
     * testCreateObjectStateStructValues.
915
     *
916
     * @param array $testData
917
     *
918
     *
919
     * @depends testCreateObjectState
920
     */
921
    public function testCreateObjectStateStructValues(array $testData)
922
    {
923
        list(
924
            $loadedObjectStateGroup,
925
            $objectStateCreateStruct,
926
            $createdObjectState
927
        ) = $testData;
928
929
        $this->assertStructPropertiesCorrect(
930
            $objectStateCreateStruct,
931
            $createdObjectState
932
        );
933
934
        $this->assertNotNull($createdObjectState->id);
935
936
        $this->assertEquals(
937
            $loadedObjectStateGroup,
938
            $createdObjectState->getObjectStateGroup()
939
        );
940
    }
941
942
    /**
943
     * Test for the loadObjectState() method.
944
     *
945
     *
946
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectState()
947
     * @depends testLoadObjectStateGroup
948
     */
949 View Code Duplication
    public function testLoadObjectState()
950
    {
951
        $repository = $this->getRepository();
952
953
        $objectStateId = $this->generateId('objectstate', 2);
954
        /* BEGIN: Use Case */
955
        // $objectStateId contains the ID of the "locked" state
956
        $objectStateService = $repository->getObjectStateService();
957
958
        $loadedObjectState = $objectStateService->loadObjectState(
959
            $objectStateId
960
        );
961
        /* END: Use Case */
962
963
        $this->assertInstanceOf(
964
            'eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectState',
965
            $loadedObjectState
966
        );
967
968
        return $loadedObjectState;
969
    }
970
971
    /**
972
     * testLoadObjectStateStructValues.
973
     *
974
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $loadedObjectState
975
     *
976
     *
977
     * @depends testLoadObjectState
978
     */
979
    public function testLoadObjectStateStructValues(ObjectState $loadedObjectState)
980
    {
981
        $this->assertPropertiesCorrect(
982
            array(
983
                'id' => 2,
984
                'identifier' => 'locked',
985
                'priority' => 1,
986
                'mainLanguageCode' => 'eng-US',
987
                'languageCodes' => array(0 => 'eng-US'),
988
                'names' => array('eng-US' => 'Locked'),
989
                'descriptions' => array('eng-US' => ''),
990
            ),
991
            $loadedObjectState
992
        );
993
994
        $this->assertEquals(
995
            $this->getRepository()->getObjectStateService()->loadObjectStateGroup(2),
996
            $loadedObjectState->getObjectStateGroup()
997
        );
998
    }
999
1000
    /**
1001
     * Test for the loadObjectState() method.
1002
     *
1003
     *
1004
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectState()
1005
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1006
     * @depends testLoadObjectState
1007
     */
1008
    public function testLoadObjectStateThrowsNotFoundException()
1009
    {
1010
        $repository = $this->getRepository();
1011
1012
        $nonExistingObjectStateId = $this->generateId('objectstate', self::DB_INT_MAX);
1013
        /* BEGIN: Use Case */
1014
        // $nonExistingObjectStateId contains the ID of a non existing state
1015
        $objectStateService = $repository->getObjectStateService();
1016
1017
        // Throws not found exception
1018
        $loadedObjectState = $objectStateService->loadObjectState(
0 ignored issues
show
Unused Code introduced by
$loadedObjectState 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...
1019
            $nonExistingObjectStateId
1020
        );
1021
        /* END: Use Case */
1022
    }
1023
1024
    /**
1025
     * Data provider for PrioritizedLanguageList tests.
1026
     *
1027
     * @return array
1028
     */
1029
    public function getPrioritizedLanguagesList()
1030
    {
1031
        return [
1032
            [[], null],
1033
            [['eng-GB'], null],
1034
            [['eng-US'], 'eng-US'],
1035
            [['ger-DE'], 'ger-DE'],
1036
            [['eng-US', 'ger-DE'], 'eng-US'],
1037
            [['ger-DE', 'eng-US'], 'ger-DE'],
1038
            [['eng-GB', 'ger-DE', 'eng-US'], 'ger-DE'],
1039
        ];
1040
    }
1041
1042
    /**
1043
     * Test that multi-language logic for loadObjectStateGroups respects prioritized language list.
1044
     *
1045
     * @dataProvider getPrioritizedLanguagesList
1046
     * @param string[] $prioritizedLanguages
1047
     * @param string|null $expectedLanguageCode
1048
     */
1049
    public function testLoadObjectStateGroupsWithPrioritizedLanguagesList(
1050
        array $prioritizedLanguages,
1051
        $expectedLanguageCode
1052
    ) {
1053
        // cleanup before the actual test
1054
        $this->deleteExistingObjectStateGroups();
1055
1056
        $repository = $this->getRepository(false);
1057
        $objectStateService = $repository->getObjectStateService();
1058
1059
        $this->createObjectStateGroups();
1060
1061
        $objectStateGroups = $objectStateService->loadObjectStateGroups(
1062
            0,
1063
            -1,
1064
            $prioritizedLanguages
1065
        );
1066
1067
        foreach ($objectStateGroups as $objectStateGroup) {
1068
            $languageCode = $expectedLanguageCode === null ? $objectStateGroup->defaultLanguageCode : $expectedLanguageCode;
1069
1070
            self::assertEquals(
1071
                $objectStateGroup->getName($languageCode),
1072
                $objectStateGroup->getName()
1073
            );
1074
1075
            self::assertEquals(
1076
                $objectStateGroup->getDescription($languageCode),
1077
                $objectStateGroup->getDescription()
1078
            );
1079
        }
1080
    }
1081
1082
    /**
1083
     * Test that multi-language logic for loadObjectStateGroup respects prioritized language list.
1084
     *
1085
     * @dataProvider getPrioritizedLanguagesList
1086
     * @param string[] $prioritizedLanguages
1087
     * @param string|null $expectedLanguageCode
1088
     */
1089 View Code Duplication
    public function testLoadObjectStateGroupWithPrioritizedLanguagesList(
1090
        array $prioritizedLanguages,
1091
        $expectedLanguageCode
1092
    ) {
1093
        $repository = $this->getRepository();
1094
        $objectStateService = $repository->getObjectStateService();
1095
1096
        $objectStateGroup = $this->testCreateObjectStateGroup();
1097
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
1098
            $objectStateGroup->id,
1099
            $prioritizedLanguages
1100
        );
1101
1102
        if ($expectedLanguageCode === null) {
1103
            $expectedLanguageCode = $loadedObjectStateGroup->defaultLanguageCode;
1104
        }
1105
1106
        self::assertEquals(
1107
            $loadedObjectStateGroup->getName($expectedLanguageCode),
1108
            $loadedObjectStateGroup->getName()
1109
        );
1110
1111
        self::assertEquals(
1112
            $loadedObjectStateGroup->getDescription($expectedLanguageCode),
1113
            $loadedObjectStateGroup->getDescription()
1114
        );
1115
    }
1116
1117
    /**
1118
     * Test that multi-language logic for loadObjectState respects prioritized language list.
1119
     *
1120
     * @dataProvider getPrioritizedLanguagesList
1121
     * @param string[] $prioritizedLanguages
1122
     * @param string|null $expectedLanguageCode
1123
     */
1124 View Code Duplication
    public function testLoadObjectStateWithPrioritizedLanguagesList(
1125
        array $prioritizedLanguages,
1126
        $expectedLanguageCode
1127
    ) {
1128
        $repository = $this->getRepository();
1129
        $objectStateService = $repository->getObjectStateService();
1130
1131
        $objectStateData = $this->testCreateObjectState();
1132
        /** @see \eZ\Publish\API\Repository\Tests\ObjectStateServiceTest::testCreateObjectState */
1133
        $objectState = $objectStateData[2];
1134
        /** @var \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState */
1135
        $loadedObjectState = $objectStateService->loadObjectState($objectState->id, $prioritizedLanguages);
1136
1137
        if ($expectedLanguageCode === null) {
1138
            $expectedLanguageCode = $objectState->defaultLanguageCode;
1139
        }
1140
1141
        self::assertEquals(
1142
            $loadedObjectState->getName($expectedLanguageCode),
1143
            $loadedObjectState->getName()
1144
        );
1145
1146
        self::assertEquals(
1147
            $loadedObjectState->getDescription($expectedLanguageCode),
1148
            $loadedObjectState->getDescription()
1149
        );
1150
    }
1151
1152
    /**
1153
     * Test that multi-language logic for loadObjectStates respects prioritized language list.
1154
     *
1155
     * @dataProvider getPrioritizedLanguagesList
1156
     * @param string[] $languageCodes
1157
     * @param string|null $expectedLanguageCode
1158
     */
1159
    public function testLoadObjectStatesWithPrioritizedLanguagesList($languageCodes, $expectedLanguageCode)
1160
    {
1161
        $repository = $this->getRepository();
1162
        $objectStateService = $repository->getObjectStateService();
1163
1164
        $objectStateGroup = $this->testCreateObjectStateGroup();
1165
        $this->createObjectState(
1166
            $objectStateGroup,
1167
            'state_1',
1168
            [
1169
                'eng-US' => 'One',
1170
                'ger-DE' => 'ein',
1171
            ],
1172
            [
1173
                'eng-US' => 'State one',
1174
                'ger-DE' => 'ein Zustand',
1175
            ]
1176
        );
1177
        $this->createObjectState(
1178
            $objectStateGroup,
1179
            'state_2',
1180
            [
1181
                'eng-US' => 'Two',
1182
                'ger-DE' => 'zwei',
1183
            ],
1184
            [
1185
                'eng-US' => 'State two',
1186
                'ger-DE' => 'zwei Zustand',
1187
            ]
1188
        );
1189
1190
        // Loads all object states in $objectStateGroup
1191
        $loadedObjectStates = $objectStateService->loadObjectStates($objectStateGroup, $languageCodes);
1192
1193
        foreach ($loadedObjectStates as $objectState) {
1194
            self::assertEquals(
1195
                $objectState->getName($expectedLanguageCode),
1196
                $objectState->getName()
1197
            );
1198
1199
            self::assertEquals(
1200
                $objectState->getDescription($expectedLanguageCode),
1201
                $objectState->getDescription()
1202
            );
1203
        }
1204
    }
1205
1206
    /**
1207
     * Test for the updateObjectState() method.
1208
     *
1209
     * @covers \eZ\Publish\API\Repository\ObjectStateService::updateObjectState
1210
     * @depends eZ\Publish\API\Repository\Tests\ObjectStateServiceTest::testLoadObjectState
1211
     */
1212 View Code Duplication
    public function testUpdateObjectState()
1213
    {
1214
        $repository = $this->getRepository();
1215
1216
        $objectStateId = $this->generateId('objectstate', 2);
1217
        /* BEGIN: Use Case */
1218
        // $objectStateId contains the ID of the "locked" state
1219
        $objectStateService = $repository->getObjectStateService();
1220
1221
        $loadedObjectState = $objectStateService->loadObjectState(
1222
            $objectStateId
1223
        );
1224
1225
        $updateStateStruct = $objectStateService->newObjectStateUpdateStruct();
1226
        $updateStateStruct->identifier = 'somehow_locked';
1227
        $updateStateStruct->defaultLanguageCode = 'ger-DE';
1228
        $updateStateStruct->names = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Someh...=> 'Irgendwie gelockt') of type array<string,string,{"en...ng","ger-DE":"string"}> is incompatible with the declared type array<integer,string> of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
1229
            'eng-US' => 'Somehow locked',
1230
            'ger-DE' => 'Irgendwie gelockt',
1231
        );
1232
        $updateStateStruct->descriptions = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'The o...-DE' => 'Sindelfingen') of type array<string,string,{"en...ng","ger-DE":"string"}> is incompatible with the declared type array<integer,string> of property $descriptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
1233
            'eng-US' => 'The object is somehow locked',
1234
            'ger-DE' => 'Sindelfingen',
1235
        );
1236
1237
        $updatedObjectState = $objectStateService->updateObjectState(
1238
            $loadedObjectState,
1239
            $updateStateStruct
1240
        );
1241
        /* END: Use Case */
1242
1243
        $this->assertInstanceOf(
1244
            ObjectState::class,
1245
            $updatedObjectState
1246
        );
1247
1248
        return array(
1249
            $loadedObjectState,
1250
            $updateStateStruct,
1251
            $updatedObjectState,
1252
        );
1253
    }
1254
1255
    /**
1256
     * Test service method for partially updating object state.
1257
     *
1258
     * @covers \eZ\Publish\API\Repository\ObjectStateService::updateObjectState
1259
     * @depends eZ\Publish\API\Repository\Tests\ObjectStateServiceTest::testLoadObjectState
1260
     */
1261
    public function testUpdateObjectStateChosenFieldsOnly()
1262
    {
1263
        $repository = $this->getRepository();
1264
        $objectStateService = $repository->getObjectStateService();
1265
1266
        $stateUpdateStruct = $objectStateService->newObjectStateUpdateStruct();
1267
        $stateUpdateStruct->identifier = 'test';
1268
        $stateUpdateStruct->names = ['eng-US' => 'Test'];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Test') of type array<string,string,{"eng-US":"string"}> is incompatible with the declared type array<integer,string> of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
1269
1270
        $state = $objectStateService->loadObjectState(1);
1271
1272
        $updatedState = $objectStateService->updateObjectState($state, $stateUpdateStruct);
1273
1274
        $this->assertInstanceOf(
1275
            ObjectState::class,
1276
            $updatedState
1277
        );
1278
1279
        $this->assertPropertiesCorrect(
1280
            [
1281
                'id' => 1,
1282
                'identifier' => 'test',
1283
                'priority' => 0,
1284
                'mainLanguageCode' => 'eng-US',
1285
                'languageCodes' => ['eng-US'],
1286
                'names' => ['eng-US' => 'Test'],
1287
                // Original value of empty description for eng-US should be kept
1288
                'descriptions' => ['eng-US' => ''],
1289
            ],
1290
            $updatedState
1291
        );
1292
1293
        $this->assertInstanceOf(
1294
            ObjectStateGroup::class,
1295
            $updatedState->getObjectStateGroup()
1296
        );
1297
1298
        $this->assertEquals($state->getObjectStateGroup()->id, $updatedState->getObjectStateGroup()->id);
1299
    }
1300
1301
    /**
1302
     * Test for the updateObjectState() method.
1303
     *
1304
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1305
     *
1306
     * @see \eZ\Publish\API\Repository\ObjectStateService::updateObjectState()
1307
     * @depends testUpdateObjectState
1308
     */
1309 View Code Duplication
    public function testUpdateObjectStateThrowsInvalidArgumentException()
1310
    {
1311
        $repository = $this->getRepository();
1312
1313
        $objectStateId = $this->generateId('objectstate', 2);
1314
        // $objectStateId contains the ID of the "locked" state
1315
        $objectStateService = $repository->getObjectStateService();
1316
1317
        $loadedObjectState = $objectStateService->loadObjectState(
1318
            $objectStateId
1319
        );
1320
1321
        $updateStateStruct = $objectStateService->newObjectStateUpdateStruct();
1322
        // 'not_locked' is the identifier of already existing state
1323
        $updateStateStruct->identifier = 'not_locked';
1324
        $updateStateStruct->defaultLanguageCode = 'ger-DE';
1325
        $updateStateStruct->names = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Someh...=> 'Irgendwie gelockt') of type array<string,string,{"en...ng","ger-DE":"string"}> is incompatible with the declared type array<integer,string> of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
1326
            'eng-US' => 'Somehow locked',
1327
            'ger-DE' => 'Irgendwie gelockt',
1328
        );
1329
        $updateStateStruct->descriptions = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'The o...-DE' => 'Sindelfingen') of type array<string,string,{"en...ng","ger-DE":"string"}> is incompatible with the declared type array<integer,string> of property $descriptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
1330
            'eng-US' => 'The object is somehow locked',
1331
            'ger-DE' => 'Sindelfingen',
1332
        );
1333
1334
        // This call will fail because state with
1335
        // 'not_locked' identifier already exists
1336
        $objectStateService->updateObjectState(
1337
            $loadedObjectState,
1338
            $updateStateStruct
1339
        );
1340
    }
1341
1342
    /**
1343
     * testUpdateObjectStateStructValues.
1344
     *
1345
     * @param array $testData
1346
     *
1347
     *
1348
     * @depends testUpdateObjectState
1349
     */
1350
    public function testUpdateObjectStateStructValues(array $testData)
1351
    {
1352
        list(
1353
            $loadedObjectState,
1354
            $updateStateStruct,
1355
            $updatedObjectState
1356
        ) = $testData;
1357
1358
        $this->assertPropertiesCorrect(
1359
            array(
1360
                'id' => $loadedObjectState->id,
1361
                'identifier' => $updateStateStruct->identifier,
1362
                'priority' => $loadedObjectState->priority,
1363
                'mainLanguageCode' => $updateStateStruct->defaultLanguageCode,
1364
                'languageCodes' => array('eng-US', 'ger-DE'),
1365
                'names' => $updateStateStruct->names,
1366
                'descriptions' => $updateStateStruct->descriptions,
1367
            ),
1368
            $updatedObjectState
1369
        );
1370
1371
        $this->assertEquals(
1372
            $loadedObjectState->getObjectStateGroup(),
1373
            $updatedObjectState->getObjectStateGroup()
1374
        );
1375
    }
1376
1377
    /**
1378
     * Test for the setPriorityOfObjectState() method.
1379
     *
1380
     *
1381
     * @see \eZ\Publish\API\Repository\ObjectStateService::setPriorityOfObjectState()
1382
     * @depends testLoadObjectState
1383
     */
1384 View Code Duplication
    public function testSetPriorityOfObjectState()
1385
    {
1386
        $repository = $this->getRepository();
1387
1388
        $objectStateId = $this->generateId('objectstate', 1);
1389
        /* BEGIN: Use Case */
1390
        // $objectStateId contains the ID of the "not_locked" state
1391
        $objectStateService = $repository->getObjectStateService();
1392
1393
        $initiallyLoadedObjectState = $objectStateService->loadObjectState(
1394
            $objectStateId
1395
        );
1396
1397
        // Sets the given priority on $initiallyLoadedObjectState
1398
        $objectStateService->setPriorityOfObjectState(
1399
            $initiallyLoadedObjectState,
1400
            23
1401
        );
1402
        // $loadObjectState now has the priority 1, since object state
1403
        // priorities are always made sequential
1404
        $loadedObjectState = $objectStateService->loadObjectState(
1405
            $objectStateId
1406
        );
1407
        /* END: Use Case */
1408
1409
        $this->assertInstanceOf(
1410
            'eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectState',
1411
            $loadedObjectState
1412
        );
1413
        $this->assertEquals(1, $loadedObjectState->priority);
1414
    }
1415
1416
    /**
1417
     * Test for the getContentState() method.
1418
     *
1419
     *
1420
     * @see \eZ\Publish\API\Repository\ObjectStateService::getContentState()
1421
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
1422
     * @depends testLoadObjectState
1423
     */
1424
    public function testGetContentState()
1425
    {
1426
        $repository = $this->getRepository();
1427
1428
        $anonymousUserId = $this->generateId('user', 10);
1429
        $ezLockObjectStateGroupId = $this->generateId('objectstategroup', 2);
1430
        /* BEGIN: Use Case */
1431
        // $anonymousUserId is the content ID of "Anonymous User"
1432
        $contentService = $repository->getContentService();
1433
        $objectStateService = $repository->getObjectStateService();
1434
1435
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
1436
1437
        $ezLockObjectStateGroup = $objectStateService->loadObjectStateGroup(
1438
            $ezLockObjectStateGroupId
1439
        );
1440
1441
        // Loads the state of $contentInfo in the "ez_lock" object state group
1442
        $ezLockObjectState = $objectStateService->getContentState(
1443
            $contentInfo,
1444
            $ezLockObjectStateGroup
1445
        );
1446
        /* END: Use Case */
1447
1448
        $this->assertInstanceOf(
1449
            'eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectState',
1450
            $ezLockObjectState
1451
        );
1452
        $this->assertEquals('not_locked', $ezLockObjectState->identifier);
1453
    }
1454
1455
    /**
1456
     * testGetInitialObjectState.
1457
     *
1458
     *
1459
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
1460
     * @depends testLoadObjectState
1461
     */
1462
    public function testGetInitialObjectState()
1463
    {
1464
        $repository = $this->getRepository();
1465
        $objectStateService = $repository->getObjectStateService();
1466
1467
        // Create object state group with custom state
1468
        $createdStateGroups = $this->createObjectStateGroups();
1469
1470
        $customObjectStateGroupId = $createdStateGroups[1]->id;
1471
        $anonymousUserId = $this->generateId('user', 10);
1472
1473
        $customGroup = $objectStateService->loadObjectStateGroup(
1474
            $customObjectStateGroupId
1475
        );
1476
1477
        $objectStateCreateStruct = $objectStateService->newObjectStateCreateStruct(
1478
            'sindelfingen'
1479
        );
1480
        $objectStateCreateStruct->priority = 1;
1481
        $objectStateCreateStruct->defaultLanguageCode = 'eng-US';
1482
        $objectStateCreateStruct->names = array('eng-US' => 'Sindelfingen');
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Sindelfingen') of type array<string,string,{"eng-US":"string"}> is incompatible with the declared type array<integer,string> of property $names.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
1483
1484
        $createdState = $objectStateService->createObjectState(
1485
            $customGroup,
1486
            $objectStateCreateStruct
1487
        );
1488
1489
        // Store state ID to be used
1490
        $customObjectStateId = $createdState->id;
0 ignored issues
show
Unused Code introduced by
$customObjectStateId 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...
1491
1492
        /* BEGIN: Use Case */
1493
        // $anonymousUserId is the content ID of "Anonymous User"
1494
        // $customObjectStateGroupId is the ID of a state group, from which no
1495
        // state has been assigned to $anonymousUserId, yet
1496
        $contentService = $repository->getContentService();
1497
        $objectStateService = $repository->getObjectStateService();
1498
1499
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
1500
1501
        $customObjectStateGroup = $objectStateService->loadObjectStateGroup(
1502
            $customObjectStateGroupId
1503
        );
1504
1505
        // Loads the initial state of the custom state group
1506
        $initialObjectState = $objectStateService->getContentState(
1507
            $contentInfo,
1508
            $customObjectStateGroup
1509
        );
1510
        /* END: Use Case */
1511
1512
        $this->assertInstanceOf(
1513
            'eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectState',
1514
            $initialObjectState
1515
        );
1516
        $this->assertEquals('sindelfingen', $initialObjectState->identifier);
1517
        $this->assertEquals(array('eng-US' => 'Sindelfingen'), $initialObjectState->names);
0 ignored issues
show
Documentation introduced by
The property names does not exist on object<eZ\Publish\API\Re...bjectState\ObjectState>. Since you implemented __get, maybe consider adding a @property annotation.

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

<?php

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

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

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

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

}

If the property has read access only, you can use the @property-read annotation instead.

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

See also the PhpDoc documentation for @property.

Loading history...
1518
        $this->assertEquals('eng-US', $initialObjectState->defaultLanguageCode);
1519
    }
1520
1521
    /**
1522
     * Test for the setContentState() method.
1523
     *
1524
     *
1525
     * @see \eZ\Publish\API\Repository\ObjectStateService::setContentState()
1526
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
1527
     * @depends testLoadObjectState
1528
     */
1529 View Code Duplication
    public function testSetContentState()
1530
    {
1531
        $repository = $this->getRepository();
1532
1533
        $anonymousUserId = $this->generateId('user', 10);
1534
        $ezLockObjectStateGroupId = $this->generateId('objectstategroup', 2);
1535
        $lockedObjectStateId = $this->generateId('objectstate', 2);
1536
        /* BEGIN: Use Case */
1537
        // $anonymousUserId is the content ID of "Anonymous User"
1538
        // $ezLockObjectStateGroupId contains the ID of the "ez_lock" object
1539
        // state group
1540
        // $lockedObjectStateId is the ID of the state "locked"
1541
        $contentService = $repository->getContentService();
1542
        $objectStateService = $repository->getObjectStateService();
1543
1544
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
1545
1546
        $ezLockObjectStateGroup = $objectStateService->loadObjectStateGroup(
1547
            $ezLockObjectStateGroupId
1548
        );
1549
        $lockedObjectState = $objectStateService->loadObjectState($lockedObjectStateId);
1550
1551
        // Sets the state of $contentInfo from "not_locked" to "locked"
1552
        $objectStateService->setContentState(
1553
            $contentInfo,
1554
            $ezLockObjectStateGroup,
1555
            $lockedObjectState
1556
        );
1557
        /* END: Use Case */
1558
1559
        $ezLockObjectState = $objectStateService->getContentState(
1560
            $contentInfo,
1561
            $ezLockObjectStateGroup
1562
        );
1563
1564
        $this->assertEquals('locked', $ezLockObjectState->identifier);
1565
    }
1566
1567
    /**
1568
     * Test for the setContentState() method.
1569
     *
1570
     * @covers \eZ\Publish\API\Repository\ObjectStateService::setContentState
1571
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1572
     * @depends eZ\Publish\API\Repository\Tests\ObjectStateServiceTest::testSetContentState
1573
     */
1574
    public function testSetContentStateThrowsInvalidArgumentException()
1575
    {
1576
        $repository = $this->getRepository();
1577
1578
        $createdStateGroups = $this->createObjectStateGroups();
1579
1580
        $anonymousUserId = $this->generateId('user', 10);
1581
        $differentObjectStateGroupId = $createdStateGroups[1]->id;
1582
        $lockedObjectStateId = $this->generateId('objectstate', 2);
1583
1584
        /* BEGIN: Use Case */
1585
        // $anonymousUserId is the content ID of "Anonymous User"
1586
        // $differentObjectStateGroupId contains the ID of an object state
1587
        // group which does not contain $lockedObjectStateId
1588
        // $lockedObjectStateId is the ID of the state "locked"
1589
        $contentService = $repository->getContentService();
1590
        $objectStateService = $repository->getObjectStateService();
1591
1592
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
1593
1594
        $differentObjectStateGroup = $objectStateService->loadObjectStateGroup(
1595
            $differentObjectStateGroupId
1596
        );
1597
        $lockedObjectState = $objectStateService->loadObjectState($lockedObjectStateId);
1598
1599
        // Throws an invalid argument exception since $lockedObjectState does
1600
        // not belong to $differentObjectStateGroup
1601
        $objectStateService->setContentState(
1602
            $contentInfo,
1603
            $differentObjectStateGroup,
1604
            $lockedObjectState
1605
        );
1606
        /* END: Use Case */
1607
    }
1608
1609
    /**
1610
     * Test for the getContentCount() method.
1611
     *
1612
     *
1613
     * @see \eZ\Publish\API\Repository\ObjectStateService::getContentCount()
1614
     * @depends testLoadObjectState
1615
     */
1616 View Code Duplication
    public function testGetContentCount()
1617
    {
1618
        $repository = $this->getRepository();
1619
1620
        $notLockedObjectStateId = $this->generateId('objectstate', 1);
1621
        /* BEGIN: Use Case */
1622
        // $notLockedObjectStateId is the ID of the state "not_locked"
1623
        $objectStateService = $repository->getObjectStateService();
1624
1625
        $notLockedObjectState = $objectStateService->loadObjectState($notLockedObjectStateId);
1626
1627
        $objectCount = $objectStateService->getContentCount($notLockedObjectState);
1628
        /* END: Use Case */
1629
1630
        $this->assertEquals(18, $objectCount);
1631
    }
1632
1633
    /**
1634
     * Test for the deleteObjectState() method.
1635
     *
1636
     *
1637
     * @see \eZ\Publish\API\Repository\ObjectStateService::deleteObjectState()
1638
     * @depends testLoadObjectState
1639
     */
1640 View Code Duplication
    public function testDeleteObjectState()
1641
    {
1642
        $repository = $this->getRepository();
1643
1644
        $notLockedObjectStateId = $this->generateId('objectstate', 1);
1645
        $lockedObjectStateId = $this->generateId('objectstate', 2);
1646
        /* BEGIN: Use Case */
1647
        // $notLockedObjectStateId is the ID of the state "not_locked"
1648
        $objectStateService = $repository->getObjectStateService();
1649
1650
        $notLockedObjectState = $objectStateService->loadObjectState($notLockedObjectStateId);
1651
1652
        // Deletes the object state and sets all objects, which where in that
1653
        // state, to the first state of the same object state group
1654
        $objectStateService->deleteObjectState($notLockedObjectState);
1655
        /* END: Use Case */
1656
1657
        $lockedObjectState = $objectStateService->loadObjectState($lockedObjectStateId);
1658
1659
        // All objects transferred
1660
        $this->assertEquals(
1661
            18,
1662
            $objectStateService->getContentCount($lockedObjectState)
1663
        );
1664
    }
1665
1666
    /**
1667
     * Test for the deleteObjectStateGroup() method.
1668
     *
1669
     *
1670
     * @see \eZ\Publish\API\Repository\ObjectStateService::deleteObjectStateGroup()
1671
     * @depends testLoadObjectStateGroup
1672
     */
1673
    public function testDeleteObjectStateGroup()
1674
    {
1675
        $repository = $this->getRepository();
1676
1677
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
1678
        /* BEGIN: Use Case */
1679
        // $objectStateGroupId contains the ID of the standard object state
1680
        // group ez_lock.
1681
        $objectStateService = $repository->getObjectStateService();
1682
1683
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
1684
            $objectStateGroupId
1685
        );
1686
1687
        $objectStateService->deleteObjectStateGroup($loadedObjectStateGroup);
1688
        /* END: Use Case */
1689
1690
        try {
1691
            $objectStateService->loadObjectStateGroup($objectStateGroupId);
1692
            $this->fail(
1693
                sprintf(
1694
                    'ObjectStateGroup with ID "%s" not deleted.',
1695
                    $objectStateGroupId
1696
                )
1697
            );
1698
        } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1699
        }
1700
    }
1701
1702
    /**
1703
     * Delete existing (e.g. initial) object state groups.
1704
     */
1705
    private function deleteExistingObjectStateGroups()
1706
    {
1707
        $repository = $this->getRepository();
1708
        $objectStateService = $repository->getObjectStateService();
1709
1710
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
1711
1712
        foreach ($objectStateGroups as $objectStateGroup) {
1713
            $objectStateService->deleteObjectStateGroup($objectStateGroup);
1714
        }
1715
    }
1716
1717
    /**
1718
     * Create Object State within the given Object State Group.
1719
     *
1720
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
1721
     * @param string $identifier
1722
     * @param array $names multi-language names
1723
     * @param array $descriptions multi-language descriptions
1724
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
1725
     */
1726
    private function createObjectState(
1727
        ObjectStateGroup $objectStateGroup,
1728
        $identifier,
1729
        array $names,
1730
        array $descriptions
1731
    ) {
1732
        $objectStateService = $this->getRepository(false)->getObjectStateService();
1733
        $objectStateCreateStruct = $objectStateService->newObjectStateCreateStruct(
1734
            $identifier
1735
        );
1736
        $objectStateCreateStruct->priority = 23;
1737
        $objectStateCreateStruct->defaultLanguageCode = array_keys($names)[0];
0 ignored issues
show
Documentation Bug introduced by
It seems like array_keys($names)[0] can also be of type integer. However, the property $defaultLanguageCode is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
1738
        $objectStateCreateStruct->names = $names;
1739
        $objectStateCreateStruct->descriptions = $descriptions;
1740
1741
        // Create a new object state in the $objectStateGroup with the
1742
        // data from $objectStateCreateStruct
1743
        return $objectStateService->createObjectState(
1744
            $objectStateGroup,
1745
            $objectStateCreateStruct
1746
        );
1747
    }
1748
}
1749