Completed
Push — master ( 635b00...c497e6 )
by André
79:58 queued 63:10
created

ObjectStateServiceTest::testUpdateObjectState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 28
nc 1
nop 0
dl 0
loc 48
rs 9.125
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
        // pre populate any kind of cache for all
618
        $objectStateService->loadObjectStateGroups();
619
620
        $groupUpdateStruct = $objectStateService->newObjectStateGroupUpdateStruct();
621
        $groupUpdateStruct->identifier = 'sindelfingen';
622
        $groupUpdateStruct->defaultLanguageCode = 'ger-DE';
623
        $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...
624
            'ger-DE' => 'Sindelfingen',
625
        );
626
        $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...
627
            'ger-DE' => 'Sindelfingen ist nicht nur eine Stadt',
628
        );
629
630
        // Updates the $loadObjectStateGroup with the data from
631
        // $groupUpdateStruct and returns the updated group
632
        $updatedObjectStateGroup = $objectStateService->updateObjectStateGroup(
633
            $loadedObjectStateGroup,
634
            $groupUpdateStruct
635
        );
636
637
        $allObjectGroups = $objectStateService->loadObjectStateGroups();
638
        /* END: Use Case */
639
640
        $this->assertInstanceOf(
641
            '\\eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectStateGroup',
642
            $updatedObjectStateGroup
643
        );
644
645
        return array(
646
            $loadedObjectStateGroup,
647
            $groupUpdateStruct,
648
            $updatedObjectStateGroup,
649
            $allObjectGroups,
650
        );
651
    }
652
653
    /**
654
     * Test service method for partially updating object state group.
655
     *
656
     * @covers \eZ\Publish\API\Repository\ObjectStateService::updateObjectStateGroup
657
     * @depends eZ\Publish\API\Repository\Tests\ObjectStateServiceTest::testLoadObjectStateGroup
658
     */
659
    public function testUpdateObjectStateGroupChosenFieldsOnly()
660
    {
661
        $repository = $this->getRepository();
662
        $objectStateService = $repository->getObjectStateService();
663
664
        $groupUpdateStruct = $objectStateService->newObjectStateGroupUpdateStruct();
665
        $groupUpdateStruct->defaultLanguageCode = 'eng-GB';
666
        $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...
667
668
        $group = $objectStateService->loadObjectStateGroup(2);
669
670
        $updatedGroup = $objectStateService->updateObjectStateGroup($group, $groupUpdateStruct);
671
672
        $this->assertInstanceOf(
673
            ObjectStateGroup::class,
674
            $updatedGroup
675
        );
676
677
        $this->assertPropertiesCorrect(
678
            [
679
                'id' => 2,
680
                'identifier' => 'ez_lock',
681
                'mainLanguageCode' => 'eng-GB',
682
                'languageCodes' => ['eng-GB'],
683
                'names' => ['eng-GB' => 'Test'],
684
                // descriptions array should have an empty value for eng-GB
685
                // without the original descriptions
686
                // since the descriptions were not in the update struct and we're changing default language
687
                'descriptions' => ['eng-GB' => ''],
688
            ],
689
            $updatedGroup
690
        );
691
    }
692
693
    /**
694
     * Test for the updateObjectStateGroup() method.
695
     *
696
     *
697
     * @see \eZ\Publish\API\Repository\ObjectStateService::updateObjectStateGroup()
698
     * @depends testUpdateObjectStateGroup
699
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
700
     */
701
    public function testUpdateObjectStateGroupThrowsInvalidArgumentException()
702
    {
703
        $repository = $this->getRepository();
704
705
        $objectStateService = $repository->getObjectStateService();
706
707
        // Create object state group which we will later update
708
        $objectStateGroupCreate = $objectStateService->newObjectStateGroupCreateStruct(
709
            'publishing'
710
        );
711
        $objectStateGroupCreate->defaultLanguageCode = 'eng-US';
712
        $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...
713
            'eng-US' => 'Publishing',
714
            'eng-GB' => 'Sindelfingen',
715
        );
716
        $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...
717
            'eng-US' => 'Put something online',
718
            'eng-GB' => 'Put something ton Sindelfingen.',
719
        );
720
721
        $createdObjectStateGroup = $objectStateService->createObjectStateGroup(
722
            $objectStateGroupCreate
723
        );
724
725
        $groupUpdateStruct = $objectStateService->newObjectStateGroupUpdateStruct();
726
        // 'ez_lock' is the identifier of already existing group
727
        $groupUpdateStruct->identifier = 'ez_lock';
728
        $groupUpdateStruct->defaultLanguageCode = 'ger-DE';
729
        $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...
730
            'ger-DE' => 'Sindelfingen',
731
        );
732
        $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...
733
            'ger-DE' => 'Sindelfingen ist nicht nur eine Stadt',
734
        );
735
736
        // This call will fail since state group with 'ez_lock' identifier already exists
737
        $objectStateService->updateObjectStateGroup(
738
            $createdObjectStateGroup,
739
            $groupUpdateStruct
740
        );
741
    }
742
743
    /**
744
     * testUpdateObjectStateGroupStructValues.
745
     *
746
     * @param array $testData
747
     *
748
     *
749
     * @depends testUpdateObjectStateGroup
750
     */
751
    public function testUpdateObjectStateGroupStructValues(array $testData)
752
    {
753
        list(
754
            $loadedObjectStateGroup,
755
            $groupUpdateStruct,
756
            $updatedObjectStateGroup,
757
            $allObjectGroups
758
        ) = $testData;
759
760
        $this->assertStructPropertiesCorrect(
761
            $groupUpdateStruct,
762
            $updatedObjectStateGroup
763
        );
764
765
        $this->assertContains($updatedObjectStateGroup, $allObjectGroups, '', false, false);
766
        $this->assertNotContains($loadedObjectStateGroup, $allObjectGroups, '', false, false);
767
    }
768
769
    /**
770
     * Test for the createObjectState() method.
771
     *
772
     *
773
     * @see \eZ\Publish\API\Repository\ObjectStateService::createObjectState()
774
     * @depends testLoadObjectStateGroup
775
     * @depends testNewObjectStateCreateStruct
776
     */
777
    public function testCreateObjectState()
778
    {
779
        $repository = $this->getRepository();
780
781
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
782
        /* BEGIN: Use Case */
783
        // $objectStateGroupId contains the ID of the standard object state
784
        // group ez_lock.
785
        $objectStateService = $repository->getObjectStateService();
786
787
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
788
            $objectStateGroupId
789
        );
790
791
        $objectStateCreateStruct = $objectStateService->newObjectStateCreateStruct(
792
            'locked_and_unlocked'
793
        );
794
        $objectStateCreateStruct->priority = 23;
795
        $objectStateCreateStruct->defaultLanguageCode = 'eng-US';
796
        $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...
797
            'eng-US' => 'Locked and Unlocked',
798
            'ger-DE' => 'geschlossen und ungeschlossen',
799
        ];
800
        $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...
801
            'eng-US' => 'A state between locked and unlocked.',
802
            'ger-DE' => 'ein Zustand zwischen geschlossen und ungeschlossen.',
803
        );
804
805
        // Creates a new object state in the $loadObjectStateGroup with the
806
        // data from $objectStateCreateStruct
807
        $createdObjectState = $objectStateService->createObjectState(
808
            $loadedObjectStateGroup,
809
            $objectStateCreateStruct
810
        );
811
        /* END: Use Case */
812
813
        $this->assertInstanceOf(ObjectState::class, $createdObjectState);
814
        // Object sequences are renumbered
815
        $objectStateCreateStruct->priority = 2;
816
817
        return [
818
            $loadedObjectStateGroup,
819
            $objectStateCreateStruct,
820
            $createdObjectState,
821
        ];
822
    }
823
824
    /**
825
     * Test service method for creating object state in empty group.
826
     *
827
     * @covers \eZ\Publish\API\Repository\ObjectStateService::createObjectState
828
     */
829
    public function testCreateObjectStateInEmptyGroup()
830
    {
831
        $repository = $this->getRepository();
832
        $objectStateService = $repository->getObjectStateService();
833
834
        $groupCreateStruct = $objectStateService->newObjectStateGroupCreateStruct('test');
835
        $groupCreateStruct->defaultLanguageCode = 'eng-GB';
836
        $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...
837
        $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...
838
839
        $createdGroup = $objectStateService->createObjectStateGroup($groupCreateStruct);
840
841
        $stateCreateStruct = $objectStateService->newObjectStateCreateStruct('test');
842
        $stateCreateStruct->priority = 2;
843
        $stateCreateStruct->defaultLanguageCode = 'eng-GB';
844
        $stateCreateStruct->names = ['eng-GB' => 'Test'];
845
        $stateCreateStruct->descriptions = ['eng-GB' => 'Test description'];
846
847
        $createdState = $objectStateService->createObjectState(
848
            $createdGroup,
849
            $stateCreateStruct
850
        );
851
852
        $this->assertInstanceOf(
853
            ObjectState::class,
854
            $createdState
855
        );
856
857
        $this->assertNotNull($createdState->id);
858
        $this->assertPropertiesCorrect(
859
            [
860
                'identifier' => 'test',
861
                'priority' => 0,
862
                'mainLanguageCode' => 'eng-GB',
863
                'languageCodes' => ['eng-GB'],
864
                'names' => ['eng-GB' => 'Test'],
865
                'descriptions' => ['eng-GB' => 'Test description'],
866
            ],
867
            $createdState
868
        );
869
870
        $objectStateGroup = $createdState->getObjectStateGroup();
871
        $this->assertInstanceOf(
872
            ObjectStateGroup::class,
873
            $objectStateGroup
874
        );
875
876
        $this->assertEquals($createdGroup->id, $objectStateGroup->id);
877
        $this->assertGreaterThan(0, $objectStateService->getContentCount($createdState));
878
    }
879
880
    /**
881
     * Test for the createObjectState() method.
882
     *
883
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
884
     *
885
     * @see \eZ\Publish\API\Repository\ObjectStateService::createObjectState()
886
     * @depends testLoadObjectStateGroup
887
     * @depends testCreateObjectState
888
     */
889
    public function testCreateObjectStateThrowsInvalidArgumentException()
890
    {
891
        $repository = $this->getRepository();
892
893
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
894
        // $objectStateGroupId contains the ID of the standard object state
895
        // group ez_lock.
896
        $objectStateService = $repository->getObjectStateService();
897
898
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
899
            $objectStateGroupId
900
        );
901
902
        $objectStateCreateStruct = $objectStateService->newObjectStateCreateStruct(
903
            // 'not_locked' is the identifier of already existing state
904
            'not_locked'
905
        );
906
        $objectStateCreateStruct->priority = 23;
907
        $objectStateCreateStruct->defaultLanguageCode = 'eng-US';
908
        $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...
909
            'eng-US' => 'Locked and Unlocked',
910
        );
911
        $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...
912
            'eng-US' => 'A state between locked and unlocked.',
913
        );
914
915
        // This call will fail because object state with
916
        // 'not_locked' identifier already exists
917
        $objectStateService->createObjectState(
918
            $loadedObjectStateGroup,
919
            $objectStateCreateStruct
920
        );
921
    }
922
923
    /**
924
     * testCreateObjectStateStructValues.
925
     *
926
     * @param array $testData
927
     *
928
     *
929
     * @depends testCreateObjectState
930
     */
931
    public function testCreateObjectStateStructValues(array $testData)
932
    {
933
        list(
934
            $loadedObjectStateGroup,
935
            $objectStateCreateStruct,
936
            $createdObjectState
937
        ) = $testData;
938
939
        $this->assertStructPropertiesCorrect(
940
            $objectStateCreateStruct,
941
            $createdObjectState
942
        );
943
944
        $this->assertNotNull($createdObjectState->id);
945
946
        $this->assertEquals(
947
            $loadedObjectStateGroup,
948
            $createdObjectState->getObjectStateGroup()
949
        );
950
    }
951
952
    /**
953
     * Test for the loadObjectState() method.
954
     *
955
     *
956
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectState()
957
     * @depends testLoadObjectStateGroup
958
     */
959 View Code Duplication
    public function testLoadObjectState()
960
    {
961
        $repository = $this->getRepository();
962
963
        $objectStateId = $this->generateId('objectstate', 2);
964
        /* BEGIN: Use Case */
965
        // $objectStateId contains the ID of the "locked" state
966
        $objectStateService = $repository->getObjectStateService();
967
968
        $loadedObjectState = $objectStateService->loadObjectState(
969
            $objectStateId
970
        );
971
        /* END: Use Case */
972
973
        $this->assertInstanceOf(
974
            'eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectState',
975
            $loadedObjectState
976
        );
977
978
        return $loadedObjectState;
979
    }
980
981
    /**
982
     * testLoadObjectStateStructValues.
983
     *
984
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $loadedObjectState
985
     *
986
     *
987
     * @depends testLoadObjectState
988
     */
989
    public function testLoadObjectStateStructValues(ObjectState $loadedObjectState)
990
    {
991
        $this->assertPropertiesCorrect(
992
            array(
993
                'id' => 2,
994
                'identifier' => 'locked',
995
                'priority' => 1,
996
                'mainLanguageCode' => 'eng-US',
997
                'languageCodes' => array(0 => 'eng-US'),
998
                'names' => array('eng-US' => 'Locked'),
999
                'descriptions' => array('eng-US' => ''),
1000
            ),
1001
            $loadedObjectState
1002
        );
1003
1004
        $this->assertEquals(
1005
            $this->getRepository()->getObjectStateService()->loadObjectStateGroup(2),
1006
            $loadedObjectState->getObjectStateGroup()
1007
        );
1008
    }
1009
1010
    /**
1011
     * Test for the loadObjectState() method.
1012
     *
1013
     *
1014
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectState()
1015
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
1016
     * @depends testLoadObjectState
1017
     */
1018
    public function testLoadObjectStateThrowsNotFoundException()
1019
    {
1020
        $repository = $this->getRepository();
1021
1022
        $nonExistingObjectStateId = $this->generateId('objectstate', self::DB_INT_MAX);
1023
        /* BEGIN: Use Case */
1024
        // $nonExistingObjectStateId contains the ID of a non existing state
1025
        $objectStateService = $repository->getObjectStateService();
1026
1027
        // Throws not found exception
1028
        $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...
1029
            $nonExistingObjectStateId
1030
        );
1031
        /* END: Use Case */
1032
    }
1033
1034
    /**
1035
     * Data provider for PrioritizedLanguageList tests.
1036
     *
1037
     * @return array
1038
     */
1039 View Code Duplication
    public function getPrioritizedLanguagesList()
1040
    {
1041
        return [
1042
            [[], null],
1043
            [['eng-GB'], null],
1044
            [['eng-US'], 'eng-US'],
1045
            [['ger-DE'], 'ger-DE'],
1046
            [['eng-US', 'ger-DE'], 'eng-US'],
1047
            [['ger-DE', 'eng-US'], 'ger-DE'],
1048
            [['eng-GB', 'ger-DE', 'eng-US'], 'ger-DE'],
1049
        ];
1050
    }
1051
1052
    /**
1053
     * Test that multi-language logic for loadObjectStateGroups respects prioritized language list.
1054
     *
1055
     * @dataProvider getPrioritizedLanguagesList
1056
     * @param string[] $prioritizedLanguages
1057
     * @param string|null $expectedLanguageCode
1058
     */
1059
    public function testLoadObjectStateGroupsWithPrioritizedLanguagesList(
1060
        array $prioritizedLanguages,
1061
        $expectedLanguageCode
1062
    ) {
1063
        // cleanup before the actual test
1064
        $this->deleteExistingObjectStateGroups();
1065
1066
        $repository = $this->getRepository(false);
1067
        $objectStateService = $repository->getObjectStateService();
1068
1069
        $this->createObjectStateGroups();
1070
1071
        $objectStateGroups = $objectStateService->loadObjectStateGroups(
1072
            0,
1073
            -1,
1074
            $prioritizedLanguages
1075
        );
1076
1077
        foreach ($objectStateGroups as $objectStateGroup) {
1078
            $languageCode = $expectedLanguageCode === null ? $objectStateGroup->defaultLanguageCode : $expectedLanguageCode;
1079
1080
            self::assertEquals(
1081
                $objectStateGroup->getName($languageCode),
1082
                $objectStateGroup->getName()
1083
            );
1084
1085
            self::assertEquals(
1086
                $objectStateGroup->getDescription($languageCode),
1087
                $objectStateGroup->getDescription()
1088
            );
1089
        }
1090
    }
1091
1092
    /**
1093
     * Test that multi-language logic for loadObjectStateGroup respects prioritized language list.
1094
     *
1095
     * @dataProvider getPrioritizedLanguagesList
1096
     * @param string[] $prioritizedLanguages
1097
     * @param string|null $expectedLanguageCode
1098
     */
1099 View Code Duplication
    public function testLoadObjectStateGroupWithPrioritizedLanguagesList(
1100
        array $prioritizedLanguages,
1101
        $expectedLanguageCode
1102
    ) {
1103
        $repository = $this->getRepository();
1104
        $objectStateService = $repository->getObjectStateService();
1105
1106
        $objectStateGroup = $this->testCreateObjectStateGroup();
1107
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
1108
            $objectStateGroup->id,
1109
            $prioritizedLanguages
1110
        );
1111
1112
        if ($expectedLanguageCode === null) {
1113
            $expectedLanguageCode = $loadedObjectStateGroup->defaultLanguageCode;
1114
        }
1115
1116
        self::assertEquals(
1117
            $loadedObjectStateGroup->getName($expectedLanguageCode),
1118
            $loadedObjectStateGroup->getName()
1119
        );
1120
1121
        self::assertEquals(
1122
            $loadedObjectStateGroup->getDescription($expectedLanguageCode),
1123
            $loadedObjectStateGroup->getDescription()
1124
        );
1125
    }
1126
1127
    /**
1128
     * Test that multi-language logic for loadObjectState respects prioritized language list.
1129
     *
1130
     * @dataProvider getPrioritizedLanguagesList
1131
     * @param string[] $prioritizedLanguages
1132
     * @param string|null $expectedLanguageCode
1133
     */
1134 View Code Duplication
    public function testLoadObjectStateWithPrioritizedLanguagesList(
1135
        array $prioritizedLanguages,
1136
        $expectedLanguageCode
1137
    ) {
1138
        $repository = $this->getRepository();
1139
        $objectStateService = $repository->getObjectStateService();
1140
1141
        $objectStateData = $this->testCreateObjectState();
1142
        /** @see \eZ\Publish\API\Repository\Tests\ObjectStateServiceTest::testCreateObjectState */
1143
        $objectState = $objectStateData[2];
1144
        /** @var \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState */
1145
        $loadedObjectState = $objectStateService->loadObjectState($objectState->id, $prioritizedLanguages);
1146
1147
        if ($expectedLanguageCode === null) {
1148
            $expectedLanguageCode = $objectState->defaultLanguageCode;
1149
        }
1150
1151
        self::assertEquals(
1152
            $loadedObjectState->getName($expectedLanguageCode),
1153
            $loadedObjectState->getName()
1154
        );
1155
1156
        self::assertEquals(
1157
            $loadedObjectState->getDescription($expectedLanguageCode),
1158
            $loadedObjectState->getDescription()
1159
        );
1160
    }
1161
1162
    /**
1163
     * Test that multi-language logic for loadObjectStates respects prioritized language list.
1164
     *
1165
     * @dataProvider getPrioritizedLanguagesList
1166
     * @param string[] $languageCodes
1167
     * @param string|null $expectedLanguageCode
1168
     */
1169
    public function testLoadObjectStatesWithPrioritizedLanguagesList($languageCodes, $expectedLanguageCode)
1170
    {
1171
        $repository = $this->getRepository();
1172
        $objectStateService = $repository->getObjectStateService();
1173
1174
        $objectStateGroup = $this->testCreateObjectStateGroup();
1175
        $this->createObjectState(
1176
            $objectStateGroup,
1177
            'state_1',
1178
            [
1179
                'eng-US' => 'One',
1180
                'ger-DE' => 'ein',
1181
            ],
1182
            [
1183
                'eng-US' => 'State one',
1184
                'ger-DE' => 'ein Zustand',
1185
            ]
1186
        );
1187
        $this->createObjectState(
1188
            $objectStateGroup,
1189
            'state_2',
1190
            [
1191
                'eng-US' => 'Two',
1192
                'ger-DE' => 'zwei',
1193
            ],
1194
            [
1195
                'eng-US' => 'State two',
1196
                'ger-DE' => 'zwei Zustand',
1197
            ]
1198
        );
1199
1200
        // Loads all object states in $objectStateGroup
1201
        $loadedObjectStates = $objectStateService->loadObjectStates($objectStateGroup, $languageCodes);
1202
1203
        foreach ($loadedObjectStates as $objectState) {
1204
            self::assertEquals(
1205
                $objectState->getName($expectedLanguageCode),
1206
                $objectState->getName()
1207
            );
1208
1209
            self::assertEquals(
1210
                $objectState->getDescription($expectedLanguageCode),
1211
                $objectState->getDescription()
1212
            );
1213
        }
1214
    }
1215
1216
    /**
1217
     * Test for the updateObjectState() method.
1218
     *
1219
     * @covers \eZ\Publish\API\Repository\ObjectStateService::updateObjectState
1220
     * @depends eZ\Publish\API\Repository\Tests\ObjectStateServiceTest::testLoadObjectState
1221
     */
1222
    public function testUpdateObjectState()
1223
    {
1224
        $repository = $this->getRepository();
1225
1226
        $objectStateId = $this->generateId('objectstate', 2);
1227
        /* BEGIN: Use Case */
1228
        // $objectStateId contains the ID of the "locked" state
1229
        $objectStateService = $repository->getObjectStateService();
1230
1231
        $loadedObjectState = $objectStateService->loadObjectState(
1232
            $objectStateId
1233
        );
1234
1235
        // pre load any possile cache loading all
1236
        $objectStateService->loadObjectStates($loadedObjectState->getObjectStateGroup());
1237
1238
        $updateStateStruct = $objectStateService->newObjectStateUpdateStruct();
1239
        $updateStateStruct->identifier = 'somehow_locked';
1240
        $updateStateStruct->defaultLanguageCode = 'ger-DE';
1241
        $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...
1242
            'eng-US' => 'Somehow locked',
1243
            'ger-DE' => 'Irgendwie gelockt',
1244
        );
1245
        $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...
1246
            'eng-US' => 'The object is somehow locked',
1247
            'ger-DE' => 'Sindelfingen',
1248
        );
1249
1250
        $updatedObjectState = $objectStateService->updateObjectState(
1251
            $loadedObjectState,
1252
            $updateStateStruct
1253
        );
1254
1255
        $allObjectStates = $objectStateService->loadObjectStates($loadedObjectState->getObjectStateGroup());
1256
        /* END: Use Case */
1257
1258
        $this->assertInstanceOf(
1259
            ObjectState::class,
1260
            $updatedObjectState
1261
        );
1262
1263
        return array(
1264
            $loadedObjectState,
1265
            $updateStateStruct,
1266
            $updatedObjectState,
1267
            $allObjectStates,
1268
        );
1269
    }
1270
1271
    /**
1272
     * Test service method for partially updating object state.
1273
     *
1274
     * @covers \eZ\Publish\API\Repository\ObjectStateService::updateObjectState
1275
     * @depends eZ\Publish\API\Repository\Tests\ObjectStateServiceTest::testLoadObjectState
1276
     */
1277
    public function testUpdateObjectStateChosenFieldsOnly()
1278
    {
1279
        $repository = $this->getRepository();
1280
        $objectStateService = $repository->getObjectStateService();
1281
1282
        $stateUpdateStruct = $objectStateService->newObjectStateUpdateStruct();
1283
        $stateUpdateStruct->identifier = 'test';
1284
        $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...
1285
1286
        $state = $objectStateService->loadObjectState(1);
1287
1288
        $updatedState = $objectStateService->updateObjectState($state, $stateUpdateStruct);
1289
1290
        $this->assertInstanceOf(
1291
            ObjectState::class,
1292
            $updatedState
1293
        );
1294
1295
        $this->assertPropertiesCorrect(
1296
            [
1297
                'id' => 1,
1298
                'identifier' => 'test',
1299
                'priority' => 0,
1300
                'mainLanguageCode' => 'eng-US',
1301
                'languageCodes' => ['eng-US'],
1302
                'names' => ['eng-US' => 'Test'],
1303
                // Original value of empty description for eng-US should be kept
1304
                'descriptions' => ['eng-US' => ''],
1305
            ],
1306
            $updatedState
1307
        );
1308
1309
        $this->assertInstanceOf(
1310
            ObjectStateGroup::class,
1311
            $updatedState->getObjectStateGroup()
1312
        );
1313
1314
        $this->assertEquals($state->getObjectStateGroup()->id, $updatedState->getObjectStateGroup()->id);
1315
    }
1316
1317
    /**
1318
     * Test for the updateObjectState() method.
1319
     *
1320
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1321
     *
1322
     * @see \eZ\Publish\API\Repository\ObjectStateService::updateObjectState()
1323
     * @depends testUpdateObjectState
1324
     */
1325 View Code Duplication
    public function testUpdateObjectStateThrowsInvalidArgumentException()
1326
    {
1327
        $repository = $this->getRepository();
1328
1329
        $objectStateId = $this->generateId('objectstate', 2);
1330
        // $objectStateId contains the ID of the "locked" state
1331
        $objectStateService = $repository->getObjectStateService();
1332
1333
        $loadedObjectState = $objectStateService->loadObjectState(
1334
            $objectStateId
1335
        );
1336
1337
        $updateStateStruct = $objectStateService->newObjectStateUpdateStruct();
1338
        // 'not_locked' is the identifier of already existing state
1339
        $updateStateStruct->identifier = 'not_locked';
1340
        $updateStateStruct->defaultLanguageCode = 'ger-DE';
1341
        $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...
1342
            'eng-US' => 'Somehow locked',
1343
            'ger-DE' => 'Irgendwie gelockt',
1344
        );
1345
        $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...
1346
            'eng-US' => 'The object is somehow locked',
1347
            'ger-DE' => 'Sindelfingen',
1348
        );
1349
1350
        // This call will fail because state with
1351
        // 'not_locked' identifier already exists
1352
        $objectStateService->updateObjectState(
1353
            $loadedObjectState,
1354
            $updateStateStruct
1355
        );
1356
    }
1357
1358
    /**
1359
     * testUpdateObjectStateStructValues.
1360
     *
1361
     * @param array $testData
1362
     *
1363
     *
1364
     * @depends testUpdateObjectState
1365
     */
1366
    public function testUpdateObjectStateStructValues(array $testData)
1367
    {
1368
        list(
1369
            $loadedObjectState,
1370
            $updateStateStruct,
1371
            $updatedObjectState,
1372
            $allObjectStates
1373
        ) = $testData;
1374
1375
        $this->assertPropertiesCorrect(
1376
            array(
1377
                'id' => $loadedObjectState->id,
1378
                'identifier' => $updateStateStruct->identifier,
1379
                'priority' => $loadedObjectState->priority,
1380
                'mainLanguageCode' => $updateStateStruct->defaultLanguageCode,
1381
                'languageCodes' => array('eng-US', 'ger-DE'),
1382
                'names' => $updateStateStruct->names,
1383
                'descriptions' => $updateStateStruct->descriptions,
1384
            ),
1385
            $updatedObjectState
1386
        );
1387
1388
        $this->assertEquals(
1389
            $loadedObjectState->getObjectStateGroup(),
1390
            $updatedObjectState->getObjectStateGroup()
1391
        );
1392
1393
        $this->assertContains($updatedObjectState, $allObjectStates, '', false, false);
1394
        $this->assertNotContains($loadedObjectState, $allObjectStates, '', false, false);
1395
    }
1396
1397
    /**
1398
     * Test for the setPriorityOfObjectState() method.
1399
     *
1400
     *
1401
     * @see \eZ\Publish\API\Repository\ObjectStateService::setPriorityOfObjectState()
1402
     * @depends testLoadObjectState
1403
     */
1404 View Code Duplication
    public function testSetPriorityOfObjectState()
1405
    {
1406
        $repository = $this->getRepository();
1407
1408
        $objectStateId = $this->generateId('objectstate', 1);
1409
        /* BEGIN: Use Case */
1410
        // $objectStateId contains the ID of the "not_locked" state
1411
        $objectStateService = $repository->getObjectStateService();
1412
1413
        $initiallyLoadedObjectState = $objectStateService->loadObjectState(
1414
            $objectStateId
1415
        );
1416
1417
        // Sets the given priority on $initiallyLoadedObjectState
1418
        $objectStateService->setPriorityOfObjectState(
1419
            $initiallyLoadedObjectState,
1420
            23
1421
        );
1422
        // $loadObjectState now has the priority 1, since object state
1423
        // priorities are always made sequential
1424
        $loadedObjectState = $objectStateService->loadObjectState(
1425
            $objectStateId
1426
        );
1427
        /* END: Use Case */
1428
1429
        $this->assertInstanceOf(
1430
            'eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectState',
1431
            $loadedObjectState
1432
        );
1433
        $this->assertEquals(1, $loadedObjectState->priority);
1434
    }
1435
1436
    /**
1437
     * Test for the getContentState() method.
1438
     *
1439
     *
1440
     * @see \eZ\Publish\API\Repository\ObjectStateService::getContentState()
1441
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
1442
     * @depends testLoadObjectState
1443
     */
1444
    public function testGetContentState()
1445
    {
1446
        $repository = $this->getRepository();
1447
1448
        $anonymousUserId = $this->generateId('user', 10);
1449
        $ezLockObjectStateGroupId = $this->generateId('objectstategroup', 2);
1450
        /* BEGIN: Use Case */
1451
        // $anonymousUserId is the content ID of "Anonymous User"
1452
        $contentService = $repository->getContentService();
1453
        $objectStateService = $repository->getObjectStateService();
1454
1455
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
1456
1457
        $ezLockObjectStateGroup = $objectStateService->loadObjectStateGroup(
1458
            $ezLockObjectStateGroupId
1459
        );
1460
1461
        // Loads the state of $contentInfo in the "ez_lock" object state group
1462
        $ezLockObjectState = $objectStateService->getContentState(
1463
            $contentInfo,
1464
            $ezLockObjectStateGroup
1465
        );
1466
        /* END: Use Case */
1467
1468
        $this->assertInstanceOf(
1469
            'eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectState',
1470
            $ezLockObjectState
1471
        );
1472
        $this->assertEquals('not_locked', $ezLockObjectState->identifier);
1473
    }
1474
1475
    /**
1476
     * testGetInitialObjectState.
1477
     *
1478
     *
1479
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
1480
     * @depends testLoadObjectState
1481
     */
1482
    public function testGetInitialObjectState()
1483
    {
1484
        $repository = $this->getRepository();
1485
        $objectStateService = $repository->getObjectStateService();
1486
1487
        // Create object state group with custom state
1488
        $createdStateGroups = $this->createObjectStateGroups();
1489
1490
        $customObjectStateGroupId = $createdStateGroups[1]->id;
1491
        $anonymousUserId = $this->generateId('user', 10);
1492
1493
        $customGroup = $objectStateService->loadObjectStateGroup(
1494
            $customObjectStateGroupId
1495
        );
1496
1497
        $objectStateCreateStruct = $objectStateService->newObjectStateCreateStruct(
1498
            'sindelfingen'
1499
        );
1500
        $objectStateCreateStruct->priority = 1;
1501
        $objectStateCreateStruct->defaultLanguageCode = 'eng-US';
1502
        $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...
1503
1504
        $createdState = $objectStateService->createObjectState(
1505
            $customGroup,
1506
            $objectStateCreateStruct
1507
        );
1508
1509
        // Store state ID to be used
1510
        $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...
1511
1512
        /* BEGIN: Use Case */
1513
        // $anonymousUserId is the content ID of "Anonymous User"
1514
        // $customObjectStateGroupId is the ID of a state group, from which no
1515
        // state has been assigned to $anonymousUserId, yet
1516
        $contentService = $repository->getContentService();
1517
        $objectStateService = $repository->getObjectStateService();
1518
1519
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
1520
1521
        $customObjectStateGroup = $objectStateService->loadObjectStateGroup(
1522
            $customObjectStateGroupId
1523
        );
1524
1525
        // Loads the initial state of the custom state group
1526
        $initialObjectState = $objectStateService->getContentState(
1527
            $contentInfo,
1528
            $customObjectStateGroup
1529
        );
1530
        /* END: Use Case */
1531
1532
        $this->assertInstanceOf(
1533
            'eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectState',
1534
            $initialObjectState
1535
        );
1536
        $this->assertEquals('sindelfingen', $initialObjectState->identifier);
1537
        $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...
1538
        $this->assertEquals('eng-US', $initialObjectState->defaultLanguageCode);
1539
    }
1540
1541
    /**
1542
     * Test for the setContentState() method.
1543
     *
1544
     *
1545
     * @see \eZ\Publish\API\Repository\ObjectStateService::setContentState()
1546
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
1547
     * @depends testLoadObjectState
1548
     */
1549 View Code Duplication
    public function testSetContentState()
1550
    {
1551
        $repository = $this->getRepository();
1552
1553
        $anonymousUserId = $this->generateId('user', 10);
1554
        $ezLockObjectStateGroupId = $this->generateId('objectstategroup', 2);
1555
        $lockedObjectStateId = $this->generateId('objectstate', 2);
1556
        /* BEGIN: Use Case */
1557
        // $anonymousUserId is the content ID of "Anonymous User"
1558
        // $ezLockObjectStateGroupId contains the ID of the "ez_lock" object
1559
        // state group
1560
        // $lockedObjectStateId is the ID of the state "locked"
1561
        $contentService = $repository->getContentService();
1562
        $objectStateService = $repository->getObjectStateService();
1563
1564
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
1565
1566
        $ezLockObjectStateGroup = $objectStateService->loadObjectStateGroup(
1567
            $ezLockObjectStateGroupId
1568
        );
1569
        $lockedObjectState = $objectStateService->loadObjectState($lockedObjectStateId);
1570
1571
        // Sets the state of $contentInfo from "not_locked" to "locked"
1572
        $objectStateService->setContentState(
1573
            $contentInfo,
1574
            $ezLockObjectStateGroup,
1575
            $lockedObjectState
1576
        );
1577
        /* END: Use Case */
1578
1579
        $ezLockObjectState = $objectStateService->getContentState(
1580
            $contentInfo,
1581
            $ezLockObjectStateGroup
1582
        );
1583
1584
        $this->assertEquals('locked', $ezLockObjectState->identifier);
1585
    }
1586
1587
    /**
1588
     * Test for the setContentState() method.
1589
     *
1590
     * @covers \eZ\Publish\API\Repository\ObjectStateService::setContentState
1591
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1592
     * @depends eZ\Publish\API\Repository\Tests\ObjectStateServiceTest::testSetContentState
1593
     */
1594
    public function testSetContentStateThrowsInvalidArgumentException()
1595
    {
1596
        $repository = $this->getRepository();
1597
1598
        $createdStateGroups = $this->createObjectStateGroups();
1599
1600
        $anonymousUserId = $this->generateId('user', 10);
1601
        $differentObjectStateGroupId = $createdStateGroups[1]->id;
1602
        $lockedObjectStateId = $this->generateId('objectstate', 2);
1603
1604
        /* BEGIN: Use Case */
1605
        // $anonymousUserId is the content ID of "Anonymous User"
1606
        // $differentObjectStateGroupId contains the ID of an object state
1607
        // group which does not contain $lockedObjectStateId
1608
        // $lockedObjectStateId is the ID of the state "locked"
1609
        $contentService = $repository->getContentService();
1610
        $objectStateService = $repository->getObjectStateService();
1611
1612
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
1613
1614
        $differentObjectStateGroup = $objectStateService->loadObjectStateGroup(
1615
            $differentObjectStateGroupId
1616
        );
1617
        $lockedObjectState = $objectStateService->loadObjectState($lockedObjectStateId);
1618
1619
        // Throws an invalid argument exception since $lockedObjectState does
1620
        // not belong to $differentObjectStateGroup
1621
        $objectStateService->setContentState(
1622
            $contentInfo,
1623
            $differentObjectStateGroup,
1624
            $lockedObjectState
1625
        );
1626
        /* END: Use Case */
1627
    }
1628
1629
    /**
1630
     * Test for the getContentCount() method.
1631
     *
1632
     *
1633
     * @see \eZ\Publish\API\Repository\ObjectStateService::getContentCount()
1634
     * @depends testLoadObjectState
1635
     */
1636 View Code Duplication
    public function testGetContentCount()
1637
    {
1638
        $repository = $this->getRepository();
1639
1640
        $notLockedObjectStateId = $this->generateId('objectstate', 1);
1641
        /* BEGIN: Use Case */
1642
        // $notLockedObjectStateId is the ID of the state "not_locked"
1643
        $objectStateService = $repository->getObjectStateService();
1644
1645
        $notLockedObjectState = $objectStateService->loadObjectState($notLockedObjectStateId);
1646
1647
        $objectCount = $objectStateService->getContentCount($notLockedObjectState);
1648
        /* END: Use Case */
1649
1650
        $this->assertEquals(18, $objectCount);
1651
    }
1652
1653
    /**
1654
     * Test for the deleteObjectState() method.
1655
     *
1656
     *
1657
     * @see \eZ\Publish\API\Repository\ObjectStateService::deleteObjectState()
1658
     * @depends testLoadObjectState
1659
     */
1660 View Code Duplication
    public function testDeleteObjectState()
1661
    {
1662
        $repository = $this->getRepository();
1663
1664
        $notLockedObjectStateId = $this->generateId('objectstate', 1);
1665
        $lockedObjectStateId = $this->generateId('objectstate', 2);
1666
        /* BEGIN: Use Case */
1667
        // $notLockedObjectStateId is the ID of the state "not_locked"
1668
        $objectStateService = $repository->getObjectStateService();
1669
1670
        $notLockedObjectState = $objectStateService->loadObjectState($notLockedObjectStateId);
1671
1672
        // Deletes the object state and sets all objects, which where in that
1673
        // state, to the first state of the same object state group
1674
        $objectStateService->deleteObjectState($notLockedObjectState);
1675
        /* END: Use Case */
1676
1677
        $lockedObjectState = $objectStateService->loadObjectState($lockedObjectStateId);
1678
1679
        // All objects transferred
1680
        $this->assertEquals(
1681
            18,
1682
            $objectStateService->getContentCount($lockedObjectState)
1683
        );
1684
    }
1685
1686
    /**
1687
     * Test for the deleteObjectStateGroup() method.
1688
     *
1689
     *
1690
     * @see \eZ\Publish\API\Repository\ObjectStateService::deleteObjectStateGroup()
1691
     * @depends testLoadObjectStateGroup
1692
     */
1693
    public function testDeleteObjectStateGroup()
1694
    {
1695
        $repository = $this->getRepository();
1696
1697
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
1698
        /* BEGIN: Use Case */
1699
        // $objectStateGroupId contains the ID of the standard object state
1700
        // group ez_lock.
1701
        $objectStateService = $repository->getObjectStateService();
1702
1703
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
1704
            $objectStateGroupId
1705
        );
1706
1707
        $objectStateService->deleteObjectStateGroup($loadedObjectStateGroup);
1708
        /* END: Use Case */
1709
1710
        try {
1711
            $objectStateService->loadObjectStateGroup($objectStateGroupId);
1712
            $this->fail(
1713
                sprintf(
1714
                    'ObjectStateGroup with ID "%s" not deleted.',
1715
                    $objectStateGroupId
1716
                )
1717
            );
1718
        } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1719
        }
1720
    }
1721
1722
    /**
1723
     * Delete existing (e.g. initial) object state groups.
1724
     */
1725
    private function deleteExistingObjectStateGroups()
1726
    {
1727
        $repository = $this->getRepository();
1728
        $objectStateService = $repository->getObjectStateService();
1729
1730
        $objectStateGroups = $objectStateService->loadObjectStateGroups();
1731
1732
        foreach ($objectStateGroups as $objectStateGroup) {
1733
            $objectStateService->deleteObjectStateGroup($objectStateGroup);
1734
        }
1735
    }
1736
1737
    /**
1738
     * Create Object State within the given Object State Group.
1739
     *
1740
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
1741
     * @param string $identifier
1742
     * @param array $names multi-language names
1743
     * @param array $descriptions multi-language descriptions
1744
     * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
1745
     */
1746
    private function createObjectState(
1747
        ObjectStateGroup $objectStateGroup,
1748
        $identifier,
1749
        array $names,
1750
        array $descriptions
1751
    ) {
1752
        $objectStateService = $this->getRepository(false)->getObjectStateService();
1753
        $objectStateCreateStruct = $objectStateService->newObjectStateCreateStruct(
1754
            $identifier
1755
        );
1756
        $objectStateCreateStruct->priority = 23;
1757
        $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...
1758
        $objectStateCreateStruct->names = $names;
1759
        $objectStateCreateStruct->descriptions = $descriptions;
1760
1761
        // Create a new object state in the $objectStateGroup with the
1762
        // data from $objectStateCreateStruct
1763
        return $objectStateService->createObjectState(
1764
            $objectStateGroup,
1765
            $objectStateCreateStruct
1766
        );
1767
    }
1768
}
1769