Completed
Push — EZP-29891 ( 916cf6...0402ff )
by
unknown
16:53
created

testCreateObjectStateGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 32
rs 9.408
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 View Code Duplication
    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 View Code Duplication
    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 View Code Duplication
    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...-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...
231
            'eng-US' => 'Publishing',
232
            'eng-GB' => '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","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...
235
            'eng-US' => 'Put something online',
236
            'eng-GB' => '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
            array(
264
                'identifier' => 'publishing',
265
                'defaultLanguageCode' => 'eng-US',
266
                'languageCodes' => array('eng-US', 'eng-GB'),
267
                'names' => array(
268
                    'eng-US' => 'Publishing',
269
                    'eng-GB' => 'Sindelfingen',
270
                ),
271
                'descriptions' => array(
272
                    'eng-US' => 'Put something online',
273
                    'eng-GB' => '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
     *
319
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectStateGroup()
320
     */
321 View Code Duplication
    public function testLoadObjectStateGroup()
322
    {
323
        $repository = $this->getRepository();
324
325
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
326
        /* BEGIN: Use Case */
327
        // $objectStateGroupId contains the ID of the standard object state
328
        // group ez_lock.
329
        $objectStateService = $repository->getObjectStateService();
330
331
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
332
            $objectStateGroupId
333
        );
334
        /* END: Use Case */
335
336
        $this->assertInstanceOf(
337
            '\\eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectStateGroup',
338
            $loadedObjectStateGroup
339
        );
340
341
        return $loadedObjectStateGroup;
342
    }
343
344
    /**
345
     * Test for the loadObjectStateGroup() method.
346
     *
347
     *
348
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectStateGroup()
349
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
350
     * @depends testLoadObjectStateGroup
351
     */
352
    public function testLoadObjectStateGroupThrowsNotFoundException()
353
    {
354
        $repository = $this->getRepository();
355
356
        $nonExistentObjectStateGroupId = $this->generateId('objectstategroup', self::DB_INT_MAX);
357
        /* BEGIN: Use Case */
358
        // $nonExistentObjectStateGroupId contains an ID for an object state
359
        // that does not exist
360
        $objectStateService = $repository->getObjectStateService();
361
362
        // Throws a not found exception
363
        $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...
364
            $nonExistentObjectStateGroupId
365
        );
366
        /* END: Use Case */
367
    }
368
369
    /**
370
     * Test for the loadObjectStateGroups() method.
371
     *
372
     *
373
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectStateGroups()
374
     * @depends testLoadObjectStateGroup
375
     */
376
    public function testLoadObjectStateGroups()
377
    {
378
        $repository = $this->getRepository();
379
380
        $expectedGroupIdentifiers = $this->getGroupIdentifierMap($this->createObjectStateGroups());
381
        $expectedGroupIdentifiers['ez_lock'] = true;
382
383
        /* BEGIN: Use Case */
384
        $objectStateService = $repository->getObjectStateService();
385
386
        $loadedObjectStateGroups = $objectStateService->loadObjectStateGroups();
387
        /* END: Use Case */
388
389
        $this->assertInternalType('array', $loadedObjectStateGroups);
390
391
        $this->assertObjectsLoadedByIdentifiers(
392
            $expectedGroupIdentifiers,
393
            $loadedObjectStateGroups,
394
            'ObjectStateGroup'
395
        );
396
    }
397
398
    /**
399
     * Creates a set of object state groups and returns an array of all
400
     * existing group identifiers after creation.
401
     *
402
     * @return bool[]
403
     */
404
    protected function createObjectStateGroups()
405
    {
406
        $repository = $this->getRepository();
407
        $objectStateService = $repository->getObjectStateService();
408
409
        $identifiersToCreate = array(
410
            'first',
411
            'second',
412
            'third',
413
        );
414
415
        $createdStateGroups = array();
416
417
        $groupCreateStruct = $objectStateService->newObjectStateGroupCreateStruct('dummy');
418
419
        $groupCreateStruct->defaultLanguageCode = 'eng-US';
420
        $groupCreateStruct->names = array('eng-US' => 'Foo');
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Foo') 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...
421
        $groupCreateStruct->descriptions = array('eng-US' => 'Foo Bar');
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-US' => 'Foo Bar') 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...
422
423
        foreach ($identifiersToCreate as $identifier) {
424
            $groupCreateStruct->identifier = $identifier;
425
            $createdStateGroups[] = $objectStateService->createObjectStateGroup($groupCreateStruct);
426
        }
427
428
        return $createdStateGroups;
429
    }
430
431
    /**
432
     * testLoadObjectStateGroupsLoadedExpectedGroups.
433
     *
434
     * @param array $loadObjectStateGroups
0 ignored issues
show
Bug introduced by
There is no parameter named $loadObjectStateGroups. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
435
     *
436
     * @depends testLoadObjectStateGroups
437
     */
438
    protected function assertObjectsLoadedByIdentifiers(array $expectedIdentifiers, array $loadedObjects, $class)
439
    {
440
        foreach ($loadedObjects as $loadedObject) {
441
            if (!isset($expectedIdentifiers[$loadedObject->identifier])) {
442
                $this->fail(
443
                    sprintf(
444
                        'Loaded not expected %s with identifier "%s"',
445
                        $class,
446
                        $loadedObject->identifier
447
                    )
448
                );
449
            }
450
            unset($expectedIdentifiers[$loadedObject->identifier]);
451
        }
452
453
        if (!empty($expectedIdentifiers)) {
454
            $this->fail(
455
                sprintf(
456
                    'Expected %ss with identifiers "%s" not loaded.',
457
                    $class,
458
                    implode('", "', $expectedIdentifiers)
459
                )
460
            );
461
        }
462
    }
463
464
    /**
465
     * Test for the loadObjectStateGroups() method.
466
     *
467
     *
468
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectStateGroups($offset)
469
     * @depends testLoadObjectStateGroups
470
     */
471 View Code Duplication
    public function testLoadObjectStateGroupsWithOffset()
472
    {
473
        $repository = $this->getRepository();
474
        $objectStateService = $repository->getObjectStateService();
475
476
        $this->createObjectStateGroups();
477
478
        $allObjectStateGroups = $objectStateService->loadObjectStateGroups();
479
480
        $existingGroupIdentifiers = $this->getGroupIdentifierMap($allObjectStateGroups);
481
482
        /* BEGIN: Use Case */
483
        $objectStateService = $repository->getObjectStateService();
484
485
        $loadedObjectStateGroups = $objectStateService->loadObjectStateGroups(2);
486
        /* END: Use Case */
487
488
        $this->assertInternalType('array', $loadedObjectStateGroups);
489
490
        $this->assertObjectsLoadedByIdentifiers(
491
            array_slice($existingGroupIdentifiers, 2),
492
            $loadedObjectStateGroups,
493
            'ObjectStateGroup'
494
        );
495
    }
496
497
    /**
498
     * Returns a map of the given object state groups.
499
     *
500
     * @param array $groups
501
     */
502
    protected function getGroupIdentifierMap(array $groups)
503
    {
504
        $existingGroupIdentifiers = array_map(
505
            function ($group) {
506
                return $group->identifier;
507
            },
508
            $groups
509
        );
510
511
        return array_fill_keys($existingGroupIdentifiers, true);
512
    }
513
514
    /**
515
     * Test for the loadObjectStateGroups() method.
516
     *
517
     *
518
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectStateGroups($offset, $limit)
519
     * @depends testLoadObjectStateGroupsWithOffset
520
     */
521 View Code Duplication
    public function testLoadObjectStateGroupsWithOffsetAndLimit()
522
    {
523
        $repository = $this->getRepository();
524
        $objectStateService = $repository->getObjectStateService();
525
526
        $allObjectStateGroups = $objectStateService->loadObjectStateGroups();
527
528
        $existingGroupIdentifiers = $this->getGroupIdentifierMap($allObjectStateGroups);
529
530
        /* BEGIN: Use Case */
531
        $objectStateService = $repository->getObjectStateService();
532
533
        $loadedObjectStateGroups = $objectStateService->loadObjectStateGroups(1, 2);
534
        /* END: Use Case */
535
536
        $this->assertInternalType('array', $loadedObjectStateGroups);
537
538
        $this->assertObjectsLoadedByIdentifiers(
539
            array_slice($existingGroupIdentifiers, 1, 2),
540
            $loadedObjectStateGroups,
541
            'ObjectStateGroup'
542
        );
543
    }
544
545
    /**
546
     * Test for the loadObjectStates() method.
547
     *
548
     *
549
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectStates()
550
     * @depends testLoadObjectStateGroup
551
     */
552
    public function testLoadObjectStates()
553
    {
554
        $repository = $this->getRepository();
555
556
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
557
        /* BEGIN: Use Case */
558
        // $objectStateGroupId contains the ID of the standard object state
559
        // group ez_lock.
560
        $objectStateService = $repository->getObjectStateService();
561
562
        $objectStateGroup = $objectStateService->loadObjectStateGroup(
563
            $objectStateGroupId
564
        );
565
566
        // Loads all object states in $objectStateGroup
567
        $loadedObjectStates = $objectStateService->loadObjectStates($objectStateGroup);
568
        /* END: Use Case */
569
570
        $this->assertInternalType(
571
            'array',
572
            $loadedObjectStates
573
        );
574
        $this->assertObjectsLoadedByIdentifiers(
575
            array('not_locked' => true, 'locked' => true),
576
            $loadedObjectStates,
577
            'ObjectState'
578
        );
579
    }
580
581
    /**
582
     * Test for the updateObjectStateGroup() method.
583
     *
584
     *
585
     * @see \eZ\Publish\API\Repository\ObjectStateService::updateObjectStateGroup()
586
     * @depends testLoadObjectStateGroup
587
     */
588
    public function testUpdateObjectStateGroup()
589
    {
590
        $repository = $this->getRepository();
591
592
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
593
        /* BEGIN: Use Case */
594
        // $objectStateGroupId contains the ID of the standard object state
595
        // group ez_lock.
596
        $objectStateService = $repository->getObjectStateService();
597
598
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
599
            $objectStateGroupId
600
        );
601
602
        // pre populate any kind of cache for all
603
        $objectStateService->loadObjectStateGroups();
604
605
        $groupUpdateStruct = $objectStateService->newObjectStateGroupUpdateStruct();
606
        $groupUpdateStruct->identifier = 'sindelfingen';
607
        $groupUpdateStruct->defaultLanguageCode = 'ger-DE';
608
        $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...
609
            'ger-DE' => 'Sindelfingen',
610
        );
611
        $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...
612
            'ger-DE' => 'Sindelfingen ist nicht nur eine Stadt',
613
        );
614
615
        // Updates the $loadObjectStateGroup with the data from
616
        // $groupUpdateStruct and returns the updated group
617
        $updatedObjectStateGroup = $objectStateService->updateObjectStateGroup(
618
            $loadedObjectStateGroup,
619
            $groupUpdateStruct
620
        );
621
622
        $allObjectGroups = $objectStateService->loadObjectStateGroups();
623
        /* END: Use Case */
624
625
        $this->assertInstanceOf(
626
            '\\eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectStateGroup',
627
            $updatedObjectStateGroup
628
        );
629
630
        return array(
631
            $loadedObjectStateGroup,
632
            $groupUpdateStruct,
633
            $updatedObjectStateGroup,
634
            $allObjectGroups,
635
        );
636
    }
637
638
    /**
639
     * Test for the updateObjectStateGroup() method.
640
     *
641
     *
642
     * @see \eZ\Publish\API\Repository\ObjectStateService::updateObjectStateGroup()
643
     * @depends testUpdateObjectStateGroup
644
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
645
     */
646
    public function testUpdateObjectStateGroupThrowsInvalidArgumentException()
647
    {
648
        $repository = $this->getRepository();
649
650
        $objectStateService = $repository->getObjectStateService();
651
652
        // Create object state group which we will later update
653
        $objectStateGroupCreate = $objectStateService->newObjectStateGroupCreateStruct(
654
            'publishing'
655
        );
656
        $objectStateGroupCreate->defaultLanguageCode = 'eng-US';
657
        $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...
658
            'eng-US' => 'Publishing',
659
            'eng-GB' => 'Sindelfingen',
660
        );
661
        $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...
662
            'eng-US' => 'Put something online',
663
            'eng-GB' => 'Put something ton Sindelfingen.',
664
        );
665
666
        $createdObjectStateGroup = $objectStateService->createObjectStateGroup(
667
            $objectStateGroupCreate
668
        );
669
670
        $groupUpdateStruct = $objectStateService->newObjectStateGroupUpdateStruct();
671
        // 'ez_lock' is the identifier of already existing group
672
        $groupUpdateStruct->identifier = 'ez_lock';
673
        $groupUpdateStruct->defaultLanguageCode = 'ger-DE';
674
        $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...
675
            'ger-DE' => 'Sindelfingen',
676
        );
677
        $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...
678
            'ger-DE' => 'Sindelfingen ist nicht nur eine Stadt',
679
        );
680
681
        // This call will fail since state group with 'ez_lock' identifier already exists
682
        $objectStateService->updateObjectStateGroup(
683
            $createdObjectStateGroup,
684
            $groupUpdateStruct
685
        );
686
    }
687
688
    /**
689
     * testUpdateObjectStateGroupStructValues.
690
     *
691
     * @param array $testData
692
     *
693
     *
694
     * @depends testUpdateObjectStateGroup
695
     */
696
    public function testUpdateObjectStateGroupStructValues(array $testData)
697
    {
698
        list(
699
            $loadedObjectStateGroup,
700
            $groupUpdateStruct,
701
            $updatedObjectStateGroup,
702
            $allObjectGroups
703
        ) = $testData;
704
705
        $this->assertStructPropertiesCorrect(
706
            $groupUpdateStruct,
707
            $updatedObjectStateGroup
708
        );
709
710
        $this->assertContains($updatedObjectStateGroup, $allObjectGroups, '', false, false);
711
        $this->assertNotContains($loadedObjectStateGroup, $allObjectGroups, '', false, false);
712
    }
713
714
    /**
715
     * Test for the createObjectState() method.
716
     *
717
     *
718
     * @see \eZ\Publish\API\Repository\ObjectStateService::createObjectState()
719
     * @depends testLoadObjectStateGroup
720
     * @depends testNewObjectStateCreateStruct
721
     */
722
    public function testCreateObjectState()
723
    {
724
        $repository = $this->getRepository();
725
726
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
727
        /* BEGIN: Use Case */
728
        // $objectStateGroupId contains the ID of the standard object state
729
        // group ez_lock.
730
        $objectStateService = $repository->getObjectStateService();
731
732
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
733
            $objectStateGroupId
734
        );
735
736
        $objectStateCreateStruct = $objectStateService->newObjectStateCreateStruct(
737
            'locked_and_unlocked'
738
        );
739
        $objectStateCreateStruct->priority = 23;
740
        $objectStateCreateStruct->defaultLanguageCode = 'eng-US';
741
        $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...
742
            'eng-US' => 'Locked and Unlocked',
743
        );
744
        $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...
745
            'eng-US' => 'A state between locked and unlocked.',
746
        );
747
748
        // Creates a new object state in the $loadObjectStateGroup with the
749
        // data from $objectStateCreateStruct
750
        $createdObjectState = $objectStateService->createObjectState(
751
            $loadedObjectStateGroup,
752
            $objectStateCreateStruct
753
        );
754
        /* END: Use Case */
755
756
        $this->assertInstanceOf(
757
            '\\eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectState',
758
            $createdObjectState
759
        );
760
        // Object sequences are renumbered
761
        $objectStateCreateStruct->priority = 2;
762
763
        return array(
764
            $loadedObjectStateGroup,
765
            $objectStateCreateStruct,
766
            $createdObjectState,
767
        );
768
    }
769
770
    /**
771
     * Test for the createObjectState() method.
772
     *
773
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
774
     *
775
     * @see \eZ\Publish\API\Repository\ObjectStateService::createObjectState()
776
     * @depends testLoadObjectStateGroup
777
     * @depends testCreateObjectState
778
     */
779 View Code Duplication
    public function testCreateObjectStateThrowsInvalidArgumentException()
780
    {
781
        $repository = $this->getRepository();
782
783
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
784
        // $objectStateGroupId contains the ID of the standard object state
785
        // group ez_lock.
786
        $objectStateService = $repository->getObjectStateService();
787
788
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
789
            $objectStateGroupId
790
        );
791
792
        $objectStateCreateStruct = $objectStateService->newObjectStateCreateStruct(
793
            // 'not_locked' is the identifier of already existing state
794
            'not_locked'
795
        );
796
        $objectStateCreateStruct->priority = 23;
797
        $objectStateCreateStruct->defaultLanguageCode = 'eng-US';
798
        $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...
799
            'eng-US' => 'Locked and Unlocked',
800
        );
801
        $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...
802
            'eng-US' => 'A state between locked and unlocked.',
803
        );
804
805
        // This call will fail because object state with
806
        // 'not_locked' identifier already exists
807
        $objectStateService->createObjectState(
808
            $loadedObjectStateGroup,
809
            $objectStateCreateStruct
810
        );
811
    }
812
813
    /**
814
     * testCreateObjectStateStructValues.
815
     *
816
     * @param array $testData
817
     *
818
     *
819
     * @depends testCreateObjectState
820
     */
821
    public function testCreateObjectStateStructValues(array $testData)
822
    {
823
        list(
824
            $loadedObjectStateGroup,
825
            $objectStateCreateStruct,
826
            $createdObjectState
827
        ) = $testData;
828
829
        $this->assertStructPropertiesCorrect(
830
            $objectStateCreateStruct,
831
            $createdObjectState
832
        );
833
834
        $this->assertNotNull($createdObjectState->id);
835
836
        $this->assertEquals(
837
            $loadedObjectStateGroup,
838
            $createdObjectState->getObjectStateGroup()
839
        );
840
    }
841
842
    /**
843
     * Test for the loadObjectState() method.
844
     *
845
     *
846
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectState()
847
     * @depends testLoadObjectStateGroup
848
     */
849 View Code Duplication
    public function testLoadObjectState()
850
    {
851
        $repository = $this->getRepository();
852
853
        $objectStateId = $this->generateId('objectstate', 2);
854
        /* BEGIN: Use Case */
855
        // $objectStateId contains the ID of the "locked" state
856
        $objectStateService = $repository->getObjectStateService();
857
858
        $loadedObjectState = $objectStateService->loadObjectState(
859
            $objectStateId
860
        );
861
        /* END: Use Case */
862
863
        $this->assertInstanceOf(
864
            'eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectState',
865
            $loadedObjectState
866
        );
867
868
        return $loadedObjectState;
869
    }
870
871
    /**
872
     * testLoadObjectStateStructValues.
873
     *
874
     * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $loadedObjectState
875
     *
876
     *
877
     * @depends testLoadObjectState
878
     */
879
    public function testLoadObjectStateStructValues(ObjectState $loadedObjectState)
880
    {
881
        $this->assertPropertiesCorrect(
882
            array(
883
                'id' => 2,
884
                'identifier' => 'locked',
885
                'priority' => 1,
886
                'defaultLanguageCode' => 'eng-US',
887
                'languageCodes' => array(0 => 'eng-US'),
888
                'names' => array('eng-US' => 'Locked'),
889
                'descriptions' => array('eng-US' => ''),
890
            ),
891
            $loadedObjectState
892
        );
893
894
        $this->assertEquals(
895
            $this->getRepository()->getObjectStateService()->loadObjectStateGroup(2),
896
            $loadedObjectState->getObjectStateGroup()
897
        );
898
    }
899
900
    /**
901
     * Test for the loadObjectState() method.
902
     *
903
     *
904
     * @see \eZ\Publish\API\Repository\ObjectStateService::loadObjectState()
905
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
906
     * @depends testLoadObjectState
907
     */
908
    public function testLoadObjectStateThrowsNotFoundException()
909
    {
910
        $repository = $this->getRepository();
911
912
        $nonExistingObjectStateId = $this->generateId('objectstate', self::DB_INT_MAX);
913
        /* BEGIN: Use Case */
914
        // $nonExistingObjectStateId contains the ID of a non existing state
915
        $objectStateService = $repository->getObjectStateService();
916
917
        // Throws not found exception
918
        $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...
919
            $nonExistingObjectStateId
920
        );
921
        /* END: Use Case */
922
    }
923
924
    /**
925
     * Test for the updateObjectState() method.
926
     *
927
     *
928
     * @see \eZ\Publish\API\Repository\ObjectStateService::updateObjectState()
929
     * @depends testLoadObjectState
930
     */
931
    public function testUpdateObjectState()
932
    {
933
        $repository = $this->getRepository();
934
935
        $objectStateId = $this->generateId('objectstate', 2);
936
        /* BEGIN: Use Case */
937
        // $objectStateId contains the ID of the "locked" state
938
        $objectStateService = $repository->getObjectStateService();
939
940
        $loadedObjectState = $objectStateService->loadObjectState(
941
            $objectStateId
942
        );
943
944
        // pre load any possile cache loading all
945
        $objectStateService->loadObjectStates($loadedObjectState->getObjectStateGroup());
946
947
        $updateStateStruct = $objectStateService->newObjectStateUpdateStruct();
948
        $updateStateStruct->identifier = 'somehow_locked';
949
        $updateStateStruct->defaultLanguageCode = 'ger-DE';
950
        $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...
951
            'eng-US' => 'Somehow locked',
952
            'ger-DE' => 'Irgendwie gelockt',
953
        );
954
        $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...
955
            'eng-US' => 'The object is somehow locked',
956
            'ger-DE' => 'Sindelfingen',
957
        );
958
959
        $updatedObjectState = $objectStateService->updateObjectState(
960
            $loadedObjectState,
961
            $updateStateStruct
962
        );
963
964
        $allObjectStates = $objectStateService->loadObjectStates($loadedObjectState->getObjectStateGroup());
965
        /* END: Use Case */
966
967
        $this->assertInstanceOf(
968
            'eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectState',
969
            $updatedObjectState
970
        );
971
972
        return array(
973
            $loadedObjectState,
974
            $updateStateStruct,
975
            $updatedObjectState,
976
            $allObjectStates,
977
        );
978
    }
979
980
    /**
981
     * Test for the updateObjectState() method.
982
     *
983
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
984
     *
985
     * @see \eZ\Publish\API\Repository\ObjectStateService::updateObjectState()
986
     * @depends testUpdateObjectState
987
     */
988 View Code Duplication
    public function testUpdateObjectStateThrowsInvalidArgumentException()
989
    {
990
        $repository = $this->getRepository();
991
992
        $objectStateId = $this->generateId('objectstate', 2);
993
        // $objectStateId contains the ID of the "locked" state
994
        $objectStateService = $repository->getObjectStateService();
995
996
        $loadedObjectState = $objectStateService->loadObjectState(
997
            $objectStateId
998
        );
999
1000
        $updateStateStruct = $objectStateService->newObjectStateUpdateStruct();
1001
        // 'not_locked' is the identifier of already existing state
1002
        $updateStateStruct->identifier = 'not_locked';
1003
        $updateStateStruct->defaultLanguageCode = 'ger-DE';
1004
        $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...
1005
            'eng-US' => 'Somehow locked',
1006
            'ger-DE' => 'Irgendwie gelockt',
1007
        );
1008
        $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...
1009
            'eng-US' => 'The object is somehow locked',
1010
            'ger-DE' => 'Sindelfingen',
1011
        );
1012
1013
        // This call will fail because state with
1014
        // 'not_locked' identifier already exists
1015
        $objectStateService->updateObjectState(
1016
            $loadedObjectState,
1017
            $updateStateStruct
1018
        );
1019
    }
1020
1021
    /**
1022
     * testUpdateObjectStateStructValues.
1023
     *
1024
     * @param array $testData
1025
     *
1026
     *
1027
     * @depends testUpdateObjectState
1028
     */
1029
    public function testUpdateObjectStateStructValues(array $testData)
1030
    {
1031
        list(
1032
            $loadedObjectState,
1033
            $updateStateStruct,
1034
            $updatedObjectState,
1035
            $allObjectStates
1036
        ) = $testData;
1037
1038
        $this->assertPropertiesCorrect(
1039
            array(
1040
                'id' => $loadedObjectState->id,
1041
                'identifier' => $updateStateStruct->identifier,
1042
                'priority' => $loadedObjectState->priority,
1043
                'defaultLanguageCode' => $updateStateStruct->defaultLanguageCode,
1044
                'languageCodes' => array('eng-US', 'ger-DE'),
1045
                'names' => $updateStateStruct->names,
1046
                'descriptions' => $updateStateStruct->descriptions,
1047
            ),
1048
            $updatedObjectState
1049
        );
1050
1051
        $this->assertEquals(
1052
            $loadedObjectState->getObjectStateGroup(),
1053
            $updatedObjectState->getObjectStateGroup()
1054
        );
1055
1056
        $this->assertContains($updatedObjectState, $allObjectStates, '', false, false);
1057
        $this->assertNotContains($loadedObjectState, $allObjectStates, '', false, false);
1058
    }
1059
1060
    /**
1061
     * Test for the setPriorityOfObjectState() method.
1062
     *
1063
     *
1064
     * @see \eZ\Publish\API\Repository\ObjectStateService::setPriorityOfObjectState()
1065
     * @depends testLoadObjectState
1066
     */
1067 View Code Duplication
    public function testSetPriorityOfObjectState()
1068
    {
1069
        $repository = $this->getRepository();
1070
1071
        $objectStateId = $this->generateId('objectstate', 1);
1072
        /* BEGIN: Use Case */
1073
        // $objectStateId contains the ID of the "not_locked" state
1074
        $objectStateService = $repository->getObjectStateService();
1075
1076
        $initiallyLoadedObjectState = $objectStateService->loadObjectState(
1077
            $objectStateId
1078
        );
1079
1080
        // Sets the given priority on $initiallyLoadedObjectState
1081
        $objectStateService->setPriorityOfObjectState(
1082
            $initiallyLoadedObjectState,
1083
            23
1084
        );
1085
        // $loadObjectState now has the priority 1, since object state
1086
        // priorities are always made sequential
1087
        $loadedObjectState = $objectStateService->loadObjectState(
1088
            $objectStateId
1089
        );
1090
        /* END: Use Case */
1091
1092
        $this->assertInstanceOf(
1093
            'eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectState',
1094
            $loadedObjectState
1095
        );
1096
        $this->assertEquals(1, $loadedObjectState->priority);
1097
    }
1098
1099
    /**
1100
     * Test for the getContentState() method.
1101
     *
1102
     *
1103
     * @see \eZ\Publish\API\Repository\ObjectStateService::getContentState()
1104
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
1105
     * @depends testLoadObjectState
1106
     */
1107
    public function testGetContentState()
1108
    {
1109
        $repository = $this->getRepository();
1110
1111
        $anonymousUserId = $this->generateId('user', 10);
1112
        $ezLockObjectStateGroupId = $this->generateId('objectstategroup', 2);
1113
        /* BEGIN: Use Case */
1114
        // $anonymousUserId is the content ID of "Anonymous User"
1115
        $contentService = $repository->getContentService();
1116
        $objectStateService = $repository->getObjectStateService();
1117
1118
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
1119
1120
        $ezLockObjectStateGroup = $objectStateService->loadObjectStateGroup(
1121
            $ezLockObjectStateGroupId
1122
        );
1123
1124
        // Loads the state of $contentInfo in the "ez_lock" object state group
1125
        $ezLockObjectState = $objectStateService->getContentState(
1126
            $contentInfo,
1127
            $ezLockObjectStateGroup
1128
        );
1129
        /* END: Use Case */
1130
1131
        $this->assertInstanceOf(
1132
            'eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectState',
1133
            $ezLockObjectState
1134
        );
1135
        $this->assertEquals('not_locked', $ezLockObjectState->identifier);
1136
    }
1137
1138
    /**
1139
     * testGetInitialObjectState.
1140
     *
1141
     *
1142
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
1143
     * @depends testLoadObjectState
1144
     */
1145
    public function testGetInitialObjectState()
1146
    {
1147
        $repository = $this->getRepository();
1148
        $objectStateService = $repository->getObjectStateService();
1149
1150
        // Create object state group with custom state
1151
        $createdStateGroups = $this->createObjectStateGroups();
1152
1153
        $customObjectStateGroupId = $createdStateGroups[1]->id;
1154
        $anonymousUserId = $this->generateId('user', 10);
1155
1156
        $customGroup = $objectStateService->loadObjectStateGroup(
1157
            $customObjectStateGroupId
1158
        );
1159
1160
        $objectStateCreateStruct = $objectStateService->newObjectStateCreateStruct(
1161
            'sindelfingen'
1162
        );
1163
        $objectStateCreateStruct->priority = 1;
1164
        $objectStateCreateStruct->defaultLanguageCode = 'eng-US';
1165
        $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...
1166
1167
        $createdState = $objectStateService->createObjectState(
1168
            $customGroup,
1169
            $objectStateCreateStruct
1170
        );
1171
1172
        // Store state ID to be used
1173
        $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...
1174
1175
        /* BEGIN: Use Case */
1176
        // $anonymousUserId is the content ID of "Anonymous User"
1177
        // $customObjectStateGroupId is the ID of a state group, from which no
1178
        // state has been assigned to $anonymousUserId, yet
1179
        $contentService = $repository->getContentService();
1180
        $objectStateService = $repository->getObjectStateService();
1181
1182
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
1183
1184
        $customObjectStateGroup = $objectStateService->loadObjectStateGroup(
1185
            $customObjectStateGroupId
1186
        );
1187
1188
        // Loads the initial state of the custom state group
1189
        $initialObjectState = $objectStateService->getContentState(
1190
            $contentInfo,
1191
            $customObjectStateGroup
1192
        );
1193
        /* END: Use Case */
1194
1195
        $this->assertInstanceOf(
1196
            'eZ\\Publish\\API\\Repository\\Values\\ObjectState\\ObjectState',
1197
            $initialObjectState
1198
        );
1199
        $this->assertEquals('sindelfingen', $initialObjectState->identifier);
1200
        $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...
1201
        $this->assertEquals('eng-US', $initialObjectState->defaultLanguageCode);
1202
    }
1203
1204
    /**
1205
     * Test for the setContentState() method.
1206
     *
1207
     *
1208
     * @see \eZ\Publish\API\Repository\ObjectStateService::setContentState()
1209
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
1210
     * @depends testLoadObjectState
1211
     */
1212 View Code Duplication
    public function testSetContentState()
1213
    {
1214
        $repository = $this->getRepository();
1215
1216
        $anonymousUserId = $this->generateId('user', 10);
1217
        $ezLockObjectStateGroupId = $this->generateId('objectstategroup', 2);
1218
        $lockedObjectStateId = $this->generateId('objectstate', 2);
1219
        /* BEGIN: Use Case */
1220
        // $anonymousUserId is the content ID of "Anonymous User"
1221
        // $ezLockObjectStateGroupId contains the ID of the "ez_lock" object
1222
        // state group
1223
        // $lockedObjectStateId is the ID of the state "locked"
1224
        $contentService = $repository->getContentService();
1225
        $objectStateService = $repository->getObjectStateService();
1226
1227
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
1228
1229
        $ezLockObjectStateGroup = $objectStateService->loadObjectStateGroup(
1230
            $ezLockObjectStateGroupId
1231
        );
1232
        $lockedObjectState = $objectStateService->loadObjectState($lockedObjectStateId);
1233
1234
        // Sets the state of $contentInfo from "not_locked" to "locked"
1235
        $objectStateService->setContentState(
1236
            $contentInfo,
1237
            $ezLockObjectStateGroup,
1238
            $lockedObjectState
1239
        );
1240
        /* END: Use Case */
1241
1242
        $ezLockObjectState = $objectStateService->getContentState(
1243
            $contentInfo,
1244
            $ezLockObjectStateGroup
1245
        );
1246
1247
        $this->assertEquals('locked', $ezLockObjectState->identifier);
1248
    }
1249
1250
    /**
1251
     * Test for the setContentState() method.
1252
     *
1253
     *
1254
     * @see \eZ\Publish\API\Repository\ObjectStateService::setContentState()
1255
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1256
     * @depends testSetContentState
1257
     */
1258
    public function testSetContentStateThrowsInvalidArgumentExceptioon()
1259
    {
1260
        $repository = $this->getRepository();
1261
1262
        $createdStateGroups = $this->createObjectStateGroups();
1263
1264
        $anonymousUserId = $this->generateId('user', 10);
1265
        $differentObjectStateGroupId = $createdStateGroups[1]->id;
1266
        $lockedObjectStateId = $this->generateId('objectstate', 2);
1267
1268
        /* BEGIN: Use Case */
1269
        // $anonymousUserId is the content ID of "Anonymous User"
1270
        // $differentObjectStateGroupId contains the ID of an object state
1271
        // group which does not contain $lockedObjectStateId
1272
        // $lockedObjectStateId is the ID of the state "locked"
1273
        $contentService = $repository->getContentService();
1274
        $objectStateService = $repository->getObjectStateService();
1275
1276
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
1277
1278
        $differentObjectStateGroup = $objectStateService->loadObjectStateGroup(
1279
            $differentObjectStateGroupId
1280
        );
1281
        $lockedObjectState = $objectStateService->loadObjectState($lockedObjectStateId);
1282
1283
        // Throws an invalid argument exception since $lockedObjectState does
1284
        // not belong to $differentObjectStateGroup
1285
        $objectStateService->setContentState(
1286
            $contentInfo,
1287
            $differentObjectStateGroup,
1288
            $lockedObjectState
1289
        );
1290
        /* END: Use Case */
1291
    }
1292
1293
    /**
1294
     * Test for the getContentCount() method.
1295
     *
1296
     *
1297
     * @see \eZ\Publish\API\Repository\ObjectStateService::getContentCount()
1298
     * @depends testLoadObjectState
1299
     */
1300 View Code Duplication
    public function testGetContentCount()
1301
    {
1302
        $repository = $this->getRepository();
1303
1304
        $notLockedObjectStateId = $this->generateId('objectstate', 1);
1305
        /* BEGIN: Use Case */
1306
        // $notLockedObjectStateId is the ID of the state "not_locked"
1307
        $objectStateService = $repository->getObjectStateService();
1308
1309
        $notLockedObjectState = $objectStateService->loadObjectState($notLockedObjectStateId);
1310
1311
        $objectCount = $objectStateService->getContentCount($notLockedObjectState);
1312
        /* END: Use Case */
1313
1314
        $this->assertEquals(18, $objectCount);
1315
    }
1316
1317
    /**
1318
     * Test for the deleteObjectState() method.
1319
     *
1320
     *
1321
     * @see \eZ\Publish\API\Repository\ObjectStateService::deleteObjectState()
1322
     * @depends testLoadObjectState
1323
     */
1324 View Code Duplication
    public function testDeleteObjectState()
1325
    {
1326
        $repository = $this->getRepository();
1327
1328
        $notLockedObjectStateId = $this->generateId('objectstate', 1);
1329
        $lockedObjectStateId = $this->generateId('objectstate', 2);
1330
        /* BEGIN: Use Case */
1331
        // $notLockedObjectStateId is the ID of the state "not_locked"
1332
        $objectStateService = $repository->getObjectStateService();
1333
1334
        $notLockedObjectState = $objectStateService->loadObjectState($notLockedObjectStateId);
1335
1336
        // Deletes the object state and sets all objects, which where in that
1337
        // state, to the first state of the same object state group
1338
        $objectStateService->deleteObjectState($notLockedObjectState);
1339
        /* END: Use Case */
1340
1341
        $lockedObjectState = $objectStateService->loadObjectState($lockedObjectStateId);
1342
1343
        // All objects transferred
1344
        $this->assertEquals(
1345
            18,
1346
            $objectStateService->getContentCount($lockedObjectState)
1347
        );
1348
    }
1349
1350
    /**
1351
     * Test for the deleteObjectStateGroup() method.
1352
     *
1353
     *
1354
     * @see \eZ\Publish\API\Repository\ObjectStateService::deleteObjectStateGroup()
1355
     * @depends testLoadObjectStateGroup
1356
     */
1357
    public function testDeleteObjectStateGroup()
1358
    {
1359
        $repository = $this->getRepository();
1360
1361
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
1362
        /* BEGIN: Use Case */
1363
        // $objectStateGroupId contains the ID of the standard object state
1364
        // group ez_lock.
1365
        $objectStateService = $repository->getObjectStateService();
1366
1367
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
1368
            $objectStateGroupId
1369
        );
1370
1371
        $objectStateService->deleteObjectStateGroup($loadedObjectStateGroup);
1372
        /* END: Use Case */
1373
1374
        try {
1375
            $objectStateService->loadObjectStateGroup($objectStateGroupId);
1376
            $this->fail(
1377
                sprintf(
1378
                    'ObjectStateGroup with ID "%s" not deleted.',
1379
                    $objectStateGroupId
1380
                )
1381
            );
1382
        } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1383
        }
1384
    }
1385
}
1386