RoleServiceTest   F
last analyzed

Complexity

Total Complexity 125

Size/Duplication

Total Lines 2735
Duplicated Lines 25.52 %

Coupling/Cohesion

Components 1
Dependencies 24

Importance

Changes 0
Metric Value
dl 698
loc 2735
rs 0.8
c 0
b 0
f 0
wmc 125
lcom 1
cbo 24

71 Methods

Rating   Name   Duplication   Size   Complexity  
A testLoadRoleDraftByRoleId() 0 25 1
A testNewRoleCreateStruct() 0 9 1
A testNewRoleCreateStructSetsNamePropertyOnStruct() 0 13 1
A testCreateRole() 0 26 1
A testRoleCreateStructValues() 0 19 1
A testCreateRoleWithPolicy() 0 40 1
A testRoleCreateStructValuesWithPolicy() 0 23 1
C testCreateRoleWithMultiplePolicies() 24 85 11
A testCreateRoleDraft() 24 24 1
A testCreateRoleThrowsInvalidArgumentException() 0 19 1
A testCreateRoleDraftThrowsInvalidArgumentException() 24 24 1
A testCreateRoleThrowsLimitationValidationException() 34 34 1
A testCreateRoleInTransactionWithRollback() 29 29 2
A testCreateRoleDraftInTransactionWithRollback() 29 29 2
A testLoadRole() 22 22 1
A testLoadRoleDraft() 0 21 1
A testLoadRoleThrowsNotFoundException() 0 16 1
A testLoadRoleDraftThrowsNotFoundException() 0 16 1
A testLoadRoleDraftByRoleIdThrowsNotFoundException() 0 16 1
A testLoadRoleByIdentifier() 22 22 1
A testLoadRoleByIdentifierThrowsNotFoundException() 0 15 1
A testLoadRoles() 0 29 3
A testLoadRolesReturnsExpectedSetOfDefaultRoles() 0 26 2
A testNewRoleUpdateStruct() 0 11 1
A testUpdateRoleDraft() 0 24 1
A testUpdateRoleDraftThrowsInvalidArgumentException() 0 22 1
A testDeleteRole() 0 20 1
A testDeleteRoleDraft() 0 21 1
A testNewPolicyCreateStruct() 0 11 1
A testNewPolicyCreateStructSetsStructProperties() 0 14 1
A testAddPolicyByRoleDraft() 6 52 2
A testAddPolicyByRoleDraftUpdatesRole() 0 32 4
A testAddPolicyByRoleDraftSetsPolicyProperties() 0 9 1
A testAddPolicyByRoleDraftThrowsLimitationValidationException() 32 32 1
A testCreateRoleWithAddPolicy() 3 53 2
A testCreateRoleDraftWithAddPolicy() 3 50 2
A testNewPolicyUpdateStruct() 0 14 1
A testUpdatePolicyByRoleDraftNoLimitation() 0 53 4
B testUpdatePolicyByRoleDraft() 0 69 4
A testUpdatePolicyUpdatesLimitations() 0 17 1
A testUpdatePolicyUpdatesRole() 0 23 3
B testUpdatePolicyByRoleDraftThrowsLimitationValidationException() 0 62 4
A testRemovePolicyByRoleDraft() 32 32 2
A testAddPolicyWithRoleAssignment() 0 39 1
A testLoadRoleAssignment() 0 45 1
A testGetRoleAssignments() 0 27 1
A testGetRoleAssignmentsContainExpectedLimitation() 0 7 1
A testAssignRoleToUser() 21 21 1
B testAssignRoleToUserWithRoleLimitation() 98 98 5
A testAssignRoleToUserWithRoleLimitationThrowsLimitationValidationException() 0 31 1
A testAssignRoleToUserThrowsInvalidArgumentException() 0 35 2
A testAssignRoleToUserWithRoleLimitationThrowsInvalidArgumentException() 0 45 2
A testRemoveRoleAssignment() 28 28 3
A testGetRoleAssignmentsForUserDirect() 0 47 1
A testGetRoleAssignmentsForUserEmpty() 16 16 1
A testGetRoleAssignmentsForUserInherited() 0 20 1
A testAssignRoleToUserGroup() 21 21 1
A testAssignRoleToUserGroupAffectsRoleAssignmentsForUser() 0 22 1
B testAssignRoleToUserGroupWithRoleLimitation() 94 94 5
A testAssignRoleToUserGroupWithRoleLimitationThrowsLimitationValidationException() 32 32 1
A testAssignRoleToUserGroupThrowsInvalidArgumentException() 36 36 2
A testAssignRoleToUserGroupWithRoleLimitationThrowsInvalidArgumentException() 0 46 2
A testRemoveRoleAssignmentFromUserGroup() 0 30 3
A testUnassignRoleByAssignment() 0 19 1
A testUnassignRoleByAssignmentThrowsUnauthorizedException() 19 19 2
A testUnassignRoleByAssignmentThrowsNotFoundException() 19 19 2
A testGetRoleAssignmentsForUserGroup() 0 42 1
B testLoadPoliciesByUserId() 0 69 3
A testPublishRoleDraft() 30 30 1
A testPublishRoleDraftAddPolicies() 0 54 2
A createUserGroupVersion1() 0 27 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like RoleServiceTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use RoleServiceTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\API\Repository\Tests;
8
9
use eZ\Publish\API\Repository\Values\User\Limitation;
10
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation;
11
use eZ\Publish\API\Repository\Values\User\Limitation\LanguageLimitation;
12
use eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation;
13
use eZ\Publish\API\Repository\Values\User\Policy;
14
use eZ\Publish\API\Repository\Values\User\Role;
15
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
16
use Exception;
17
18
/**
19
 * Test case for operations in the RoleService using in memory storage.
20
 *
21
 * The following IDs from the default eZ community edition database are used in
22
 * this test:
23
 *
24
 * <ul>
25
 *   <li>
26
 *     ContentType
27
 *     <ul>
28
 *       <li><strong>28</strong>: File</li>
29
 *       <li><strong>29</strong>: Flash</li>
30
 *       <li><strong>30</strong>: Image</li>
31
 *     </ul>
32
 *   </li>
33
 * <ul>
34
 *
35
 * @see eZ\Publish\API\Repository\RoleService
36
 * @group role
37
 */
38
class RoleServiceTest extends BaseTest
39
{
40
    /**
41
     * Test for the newRoleCreateStruct() method.
42
     *
43
     * @see \eZ\Publish\API\Repository\RoleService::newRoleCreateStruct()
44
     */
45
    public function testNewRoleCreateStruct()
46
    {
47
        $repository = $this->getRepository();
48
49
        $roleService = $repository->getRoleService();
50
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
51
52
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleCreateStruct', $roleCreate);
53
    }
54
55
    /**
56
     * Test for the newRoleCreateStruct() method.
57
     *
58
     * @see \eZ\Publish\API\Repository\RoleService::newRoleCreateStruct()
59
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
60
     */
61
    public function testNewRoleCreateStructSetsNamePropertyOnStruct()
62
    {
63
        $repository = $this->getRepository();
64
65
        /* BEGIN: Use Case */
66
67
        $roleService = $repository->getRoleService();
68
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
69
70
        /* END: Use Case */
71
72
        $this->assertEquals('roleName', $roleCreate->identifier);
73
    }
74
75
    /**
76
     * Test for the createRole() method.
77
     *
78
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
79
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
80
     */
81
    public function testCreateRole()
82
    {
83
        $repository = $this->getRepository();
84
85
        /* BEGIN: Use Case */
86
87
        $roleService = $repository->getRoleService();
88
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
89
90
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
91
        // $roleCreate->mainLanguageCode = 'eng-US';
92
93
        $role = $roleService->createRole($roleCreate);
94
95
        /* END: Use Case */
96
97
        $this->assertInstanceOf(
98
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleDraft',
99
            $role
100
        );
101
102
        return [
103
            'createStruct' => $roleCreate,
104
            'role' => $role,
105
        ];
106
    }
107
108
    /**
109
     * Test for the createRole() method.
110
     *
111
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
112
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
113
     */
114
    public function testRoleCreateStructValues(array $data)
115
    {
116
        $createStruct = $data['createStruct'];
117
        $role = $data['role'];
118
119
        $this->assertEquals(
120
            [
121
                'identifier' => $createStruct->identifier,
122
                'policies' => $createStruct->policies,
123
            ],
124
            [
125
                'identifier' => $role->identifier,
126
                'policies' => $role->policies,
127
            ]
128
        );
129
        $this->assertNotNull($role->id);
130
131
        return $data;
132
    }
133
134
    /**
135
     * Test for the createRole() method.
136
     *
137
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
138
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
139
     */
140
    public function testCreateRoleWithPolicy()
141
    {
142
        $repository = $this->getRepository();
143
144
        /* BEGIN: Use Case */
145
146
        $roleService = $repository->getRoleService();
147
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
148
149
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
150
        // $roleCreate->mainLanguageCode = 'eng-US';
151
152
        // Create new subtree limitation
153
        $limitation = new SubtreeLimitation(
154
            [
155
                'limitationValues' => ['/1/2/'],
156
            ]
157
        );
158
159
        // Create policy create struct and add limitation to it
160
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'read');
161
        $policyCreate->addLimitation($limitation);
162
163
        // Add policy create struct to role create struct
164
        $roleCreate->addPolicy($policyCreate);
165
166
        $role = $roleService->createRole($roleCreate);
167
168
        /* END: Use Case */
169
170
        $this->assertInstanceOf(
171
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleDraft',
172
            $role
173
        );
174
175
        return [
176
            'createStruct' => $roleCreate,
177
            'role' => $role,
178
        ];
179
    }
180
181
    /**
182
     * Test for the createRole() method.
183
     *
184
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
185
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithPolicy
186
     */
187
    public function testRoleCreateStructValuesWithPolicy(array $data)
188
    {
189
        $createStruct = $data['createStruct'];
190
        $role = $data['role'];
191
192
        $this->assertEquals(
193
            [
194
                'identifier' => $createStruct->identifier,
195
                'policy_module' => $createStruct->policies[0]->module,
196
                'policy_function' => $createStruct->policies[0]->function,
197
                'policy_limitation' => array_values($createStruct->policies[0]->limitations),
198
            ],
199
            [
200
                'identifier' => $role->identifier,
201
                'policy_module' => $role->policies[0]->module,
202
                'policy_function' => $role->policies[0]->function,
203
                'policy_limitation' => array_values($role->policies[0]->limitations),
204
            ]
205
        );
206
        $this->assertNotNull($role->id);
207
208
        return $data;
209
    }
210
211
    /**
212
     * Test creating a role with multiple policies.
213
     *
214
     * @covers \eZ\Publish\API\Repository\RoleService::createRole
215
     */
216
    public function testCreateRoleWithMultiplePolicies()
217
    {
218
        $repository = $this->getRepository();
219
        $roleService = $repository->getRoleService();
220
221
        $limitation1 = new Limitation\ContentTypeLimitation();
222
        $limitation1->limitationValues = ['1', '3', '13'];
223
224
        $limitation2 = new Limitation\SectionLimitation();
225
        $limitation2->limitationValues = ['2', '3'];
226
227
        $limitation3 = new Limitation\OwnerLimitation();
228
        $limitation3->limitationValues = ['1', '2'];
229
230
        $limitation4 = new Limitation\UserGroupLimitation();
231
        $limitation4->limitationValues = ['1'];
232
233
        $policyCreateStruct1 = $roleService->newPolicyCreateStruct('content', 'read');
234
        $policyCreateStruct1->addLimitation($limitation1);
235
        $policyCreateStruct1->addLimitation($limitation2);
236
237
        $policyCreateStruct2 = $roleService->newPolicyCreateStruct('content', 'edit');
238
        $policyCreateStruct2->addLimitation($limitation3);
239
        $policyCreateStruct2->addLimitation($limitation4);
240
241
        $roleCreateStruct = $roleService->newRoleCreateStruct('ultimate_permissions');
242
        $roleCreateStruct->addPolicy($policyCreateStruct1);
243
        $roleCreateStruct->addPolicy($policyCreateStruct2);
244
245
        $createdRole = $roleService->createRole($roleCreateStruct);
246
247
        self::assertInstanceOf(Role::class, $createdRole);
248
        self::assertGreaterThan(0, $createdRole->id);
249
250
        $this->assertPropertiesCorrect(
251
            [
252
                'identifier' => $roleCreateStruct->identifier,
253
            ],
254
            $createdRole
255
        );
256
257
        self::assertCount(2, $createdRole->getPolicies());
258
259
        foreach ($createdRole->getPolicies() as $policy) {
260
            self::assertInstanceOf(Policy::class, $policy);
261
            self::assertGreaterThan(0, $policy->id);
262
            self::assertEquals($createdRole->id, $policy->roleId);
263
264
            self::assertCount(2, $policy->getLimitations());
265
266
            foreach ($policy->getLimitations() as $limitation) {
267
                self::assertInstanceOf(Limitation::class, $limitation);
268
269
                if ($policy->module == 'content' && $policy->function == 'read') {
270 View Code Duplication
                    switch ($limitation->getIdentifier()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
271
                        case Limitation::CONTENTTYPE:
272
                            self::assertEquals($limitation1->limitationValues, $limitation->limitationValues);
273
                            break;
274
275
                        case Limitation::SECTION:
276
                            self::assertEquals($limitation2->limitationValues, $limitation->limitationValues);
277
                            break;
278
279
                        default:
280
                            self::fail('Created role contains limitations not defined with create struct');
281
                    }
282
                } elseif ($policy->module == 'content' && $policy->function == 'edit') {
283 View Code Duplication
                    switch ($limitation->getIdentifier()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
284
                        case Limitation::OWNER:
285
                            self::assertEquals($limitation3->limitationValues, $limitation->limitationValues);
286
                            break;
287
288
                        case Limitation::USERGROUP:
289
                            self::assertEquals($limitation4->limitationValues, $limitation->limitationValues);
290
                            break;
291
292
                        default:
293
                            self::fail('Created role contains limitations not defined with create struct');
294
                    }
295
                } else {
296
                    self::fail('Created role contains policy not defined with create struct');
297
                }
298
            }
299
        }
300
    }
301
302
    /**
303
     * Test for the createRoleDraft() method.
304
     *
305
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
306
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
307
     */
308 View Code Duplication
    public function testCreateRoleDraft()
309
    {
310
        $repository = $this->getRepository();
311
312
        /* BEGIN: Use Case */
313
314
        $roleService = $repository->getRoleService();
315
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
316
317
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
318
        // $roleCreate->mainLanguageCode = 'eng-US';
319
320
        $roleDraft = $roleService->createRole($roleCreate);
321
        $roleService->publishRoleDraft($roleDraft);
322
        $role = $roleService->loadRole($roleDraft->id);
323
        $newRoleDraft = $roleService->createRoleDraft($role);
324
325
        /* END: Use Case */
326
327
        $this->assertInstanceOf(
328
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleDraft',
329
            $newRoleDraft
330
        );
331
    }
332
333
    /**
334
     * Test for the createRole() method.
335
     *
336
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
337
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
338
     */
339
    public function testCreateRoleThrowsInvalidArgumentException()
340
    {
341
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
342
343
        $repository = $this->getRepository();
344
345
        /* BEGIN: Use Case */
346
347
        $roleService = $repository->getRoleService();
348
        $roleCreate = $roleService->newRoleCreateStruct('Editor');
349
350
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
351
        // $roleCreate->mainLanguageCode = 'eng-US';
352
353
        // This call will fail with an InvalidArgumentException, because Editor exists
354
        $roleService->createRole($roleCreate);
355
356
        /* END: Use Case */
357
    }
358
359
    /**
360
     * Test for the createRoleDraft() method.
361
     *
362
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
363
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
364
     */
365 View Code Duplication
    public function testCreateRoleDraftThrowsInvalidArgumentException()
366
    {
367
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
368
369
        $repository = $this->getRepository();
370
371
        /* BEGIN: Use Case */
372
373
        $roleService = $repository->getRoleService();
374
        $roleCreate = $roleService->newRoleCreateStruct('Editor');
375
376
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
377
        // $roleCreate->mainLanguageCode = 'eng-US';
378
379
        $roleDraft = $roleService->createRole($roleCreate);
380
        $roleService->publishRoleDraft($roleDraft);
381
        $role = $roleService->loadRole($roleDraft->id);
382
        $roleService->createRoleDraft($role); // First role draft
383
384
        // This call will fail with an InvalidArgumentException, because there is already a draft
385
        $roleService->createRoleDraft($role);
386
387
        /* END: Use Case */
388
    }
389
390
    /**
391
     * Test for the createRole() method.
392
     *
393
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
394
     */
395 View Code Duplication
    public function testCreateRoleThrowsLimitationValidationException()
396
    {
397
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\LimitationValidationException::class);
398
399
        $repository = $this->getRepository();
400
401
        /* BEGIN: Use Case */
402
        $roleService = $repository->getRoleService();
403
404
        // Create new role create struct
405
        $roleCreate = $roleService->newRoleCreateStruct('Lumberjack');
406
407
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
408
        // $roleCreate->mainLanguageCode = 'eng-US';
409
410
        // Create new subtree limitation
411
        $limitation = new SubtreeLimitation(
412
            [
413
                'limitationValues' => ['/mountain/forest/tree/42/'],
414
            ]
415
        );
416
417
        // Create policy create struct and add limitation to it
418
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'remove');
419
        $policyCreate->addLimitation($limitation);
420
421
        // Add policy create struct to role create struct
422
        $roleCreate->addPolicy($policyCreate);
423
424
        // This call will fail with an LimitationValidationException, because subtree
425
        // "/mountain/forest/tree/42/" does not exist
426
        $roleService->createRole($roleCreate);
427
        /* END: Use Case */
428
    }
429
430
    /**
431
     * Test for the createRole() method.
432
     *
433
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
434
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
435
     */
436 View Code Duplication
    public function testCreateRoleInTransactionWithRollback()
437
    {
438
        $repository = $this->getRepository();
439
440
        /* BEGIN: Use Case */
441
442
        $roleService = $repository->getRoleService();
443
444
        $repository->beginTransaction();
445
446
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
447
448
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
449
        // $roleCreate->mainLanguageCode = 'eng-US';
450
451
        $createdRoleId = $roleService->createRole($roleCreate)->id;
452
453
        $repository->rollback();
454
455
        try {
456
            // This call will fail with a "NotFoundException"
457
            $role = $roleService->loadRole($createdRoleId);
0 ignored issues
show
Unused Code introduced by
$role 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...
458
        } catch (NotFoundException $e) {
459
            return;
460
        }
461
        /* END: Use Case */
462
463
        $this->fail('Role object still exists after rollback.');
464
    }
465
466
    /**
467
     * Test for the createRoleDraft() method.
468
     *
469
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
470
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
471
     */
472 View Code Duplication
    public function testCreateRoleDraftInTransactionWithRollback()
473
    {
474
        $repository = $this->getRepository();
475
476
        /* BEGIN: Use Case */
477
478
        $roleService = $repository->getRoleService();
479
480
        $repository->beginTransaction();
481
482
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
483
484
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
485
        // $roleCreate->mainLanguageCode = 'eng-US';
486
487
        $createdRoleId = $roleService->createRole($roleCreate)->id;
488
489
        $repository->rollback();
490
491
        try {
492
            // This call will fail with a "NotFoundException"
493
            $role = $roleService->loadRoleDraft($createdRoleId);
0 ignored issues
show
Unused Code introduced by
$role 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...
494
        } catch (NotFoundException $e) {
495
            return;
496
        }
497
        /* END: Use Case */
498
499
        $this->fail('Role draft object still exists after rollback.');
500
    }
501
502
    /**
503
     * Test for the loadRole() method.
504
     *
505
     * @see \eZ\Publish\API\Repository\RoleService::loadRole()
506
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
507
     */
508 View Code Duplication
    public function testLoadRole()
509
    {
510
        $repository = $this->getRepository();
511
512
        /* BEGIN: Use Case */
513
514
        $roleService = $repository->getRoleService();
515
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
516
517
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
518
        // $roleCreate->mainLanguageCode = 'eng-US';
519
520
        $roleDraft = $roleService->createRole($roleCreate);
521
        $roleService->publishRoleDraft($roleDraft);
522
523
        // Load the newly created role by its ID
524
        $role = $roleService->loadRole($roleDraft->id);
525
526
        /* END: Use Case */
527
528
        $this->assertEquals('roleName', $role->identifier);
529
    }
530
531
    /**
532
     * Test for the loadRoleDraft() method.
533
     *
534
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleDraft()
535
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
536
     */
537
    public function testLoadRoleDraft()
538
    {
539
        $repository = $this->getRepository();
540
541
        /* BEGIN: Use Case */
542
543
        $roleService = $repository->getRoleService();
544
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
545
546
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
547
        // $roleCreate->mainLanguageCode = 'eng-US';
548
549
        $roleDraft = $roleService->createRole($roleCreate);
550
551
        // Load the newly created role by its ID
552
        $role = $roleService->loadRoleDraft($roleDraft->id);
553
554
        /* END: Use Case */
555
556
        $this->assertEquals('roleName', $role->identifier);
557
    }
558
559
    public function testLoadRoleDraftByRoleId()
560
    {
561
        $repository = $this->getRepository();
562
563
        /* BEGIN: Use Case */
564
565
        $roleService = $repository->getRoleService();
566
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
567
568
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
569
        // $roleCreate->mainLanguageCode = 'eng-US';
570
571
        $role = $roleService->createRole($roleCreate);
572
        $roleService->publishRoleDraft($role);
573
574
        // Now create a new draft based on the role
575
        $newDraft = $roleService->createRoleDraft($role);
576
        $loadedRoleDraft = $roleService->loadRoleDraftByRoleId($role->id);
577
578
        /* END: Use Case */
579
580
        self::assertEquals('roleName', $role->identifier);
581
        self::assertInstanceOf('eZ\Publish\API\Repository\Values\User\RoleDraft', $loadedRoleDraft);
582
        self::assertEquals($newDraft, $loadedRoleDraft);
583
    }
584
585
    /**
586
     * Test for the loadRole() method.
587
     *
588
     * @see \eZ\Publish\API\Repository\RoleService::loadRole()
589
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRole
590
     */
591
    public function testLoadRoleThrowsNotFoundException()
592
    {
593
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
594
595
        $repository = $this->getRepository();
596
597
        $nonExistingRoleId = $this->generateId('role', self::DB_INT_MAX);
598
        /* BEGIN: Use Case */
599
600
        $roleService = $repository->getRoleService();
601
602
        // This call will fail with a NotFoundException, because no such role exists.
603
        $roleService->loadRole($nonExistingRoleId);
604
605
        /* END: Use Case */
606
    }
607
608
    /**
609
     * Test for the loadRoleDraft() method.
610
     *
611
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleDraft()
612
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleDraft
613
     */
614
    public function testLoadRoleDraftThrowsNotFoundException()
615
    {
616
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
617
618
        $repository = $this->getRepository();
619
620
        $nonExistingRoleId = $this->generateId('role', self::DB_INT_MAX);
621
        /* BEGIN: Use Case */
622
623
        $roleService = $repository->getRoleService();
624
625
        // This call will fail with a NotFoundException, because no such role exists.
626
        $roleService->loadRoleDraft($nonExistingRoleId);
627
628
        /* END: Use Case */
629
    }
630
631
    public function testLoadRoleDraftByRoleIdThrowsNotFoundException()
632
    {
633
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
634
635
        $repository = $this->getRepository();
636
637
        $nonExistingRoleId = $this->generateId('role', self::DB_INT_MAX);
638
        /* BEGIN: Use Case */
639
640
        $roleService = $repository->getRoleService();
641
642
        // This call will fail with a NotFoundException, because no such role exists.
643
        $roleService->loadRoleDraftByRoleId($nonExistingRoleId);
644
645
        /* END: Use Case */
646
    }
647
648
    /**
649
     * Test for the loadRoleByIdentifier() method.
650
     *
651
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleByIdentifier()
652
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
653
     */
654 View Code Duplication
    public function testLoadRoleByIdentifier()
655
    {
656
        $repository = $this->getRepository();
657
658
        /* BEGIN: Use Case */
659
660
        $roleService = $repository->getRoleService();
661
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
662
663
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
664
        // $roleCreate->mainLanguageCode = 'eng-US';
665
666
        $roleDraft = $roleService->createRole($roleCreate);
667
        $roleService->publishRoleDraft($roleDraft);
668
669
        // Load the newly created role by its identifier
670
        $role = $roleService->loadRoleByIdentifier('roleName');
671
672
        /* END: Use Case */
673
674
        $this->assertEquals('roleName', $role->identifier);
675
    }
676
677
    /**
678
     * Test for the loadRoleByIdentifier() method.
679
     *
680
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleByIdentifier()
681
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
682
     */
683
    public function testLoadRoleByIdentifierThrowsNotFoundException()
684
    {
685
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
686
687
        $repository = $this->getRepository();
688
689
        /* BEGIN: Use Case */
690
691
        $roleService = $repository->getRoleService();
692
693
        // This call will fail with a NotFoundException, because no such role exists.
694
        $roleService->loadRoleByIdentifier('MissingRole');
695
696
        /* END: Use Case */
697
    }
698
699
    /**
700
     * Test for the loadRoles() method.
701
     *
702
     * @see \eZ\Publish\API\Repository\RoleService::loadRoles()
703
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
704
     */
705
    public function testLoadRoles()
706
    {
707
        $repository = $this->getRepository();
708
709
        /* BEGIN: Use Case */
710
711
        // First create a custom role
712
        $roleService = $repository->getRoleService();
713
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
714
715
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
716
        // $roleCreate->mainLanguageCode = 'eng-US';
717
718
        $roleDraft = $roleService->createRole($roleCreate);
719
        $roleService->publishRoleDraft($roleDraft);
720
721
        // Now load all available roles
722
        $roles = $roleService->loadRoles();
723
724
        foreach ($roles as $role) {
725
            if ($role->identifier === 'roleName') {
726
                break;
727
            }
728
        }
729
730
        /* END: Use Case */
731
732
        $this->assertEquals('roleName', $role->identifier);
0 ignored issues
show
Bug introduced by
The variable $role seems to be defined by a foreach iteration on line 724. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
733
    }
734
735
    /**
736
     * Test for the loadRoles() method.
737
     *
738
     * @see \eZ\Publish\API\Repository\RoleService::loadRoles()
739
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoles
740
     */
741
    public function testLoadRolesReturnsExpectedSetOfDefaultRoles()
742
    {
743
        $repository = $this->getRepository();
744
745
        /* BEGIN: Use Case */
746
        $roleService = $repository->getRoleService();
747
748
        $roles = $roleService->loadRoles();
749
750
        $roleNames = [];
751
        foreach ($roles as $role) {
752
            $roleNames[] = $role->identifier;
753
        }
754
        /* END: Use Case */
755
756
        $this->assertEqualsCanonicalizing(
757
            [
758
                'Administrator',
759
                'Anonymous',
760
                'Editor',
761
                'Member',
762
                'Partner',
763
            ],
764
            $roleNames
765
        );
766
    }
767
768
    /**
769
     * Test for the newRoleUpdateStruct() method.
770
     *
771
     * @see \eZ\Publish\API\Repository\RoleService::newRoleUpdateStruct()
772
     */
773
    public function testNewRoleUpdateStruct()
774
    {
775
        $repository = $this->getRepository();
776
777
        /* BEGIN: Use Case */
778
        $roleService = $repository->getRoleService();
779
        $roleUpdate = $roleService->newRoleUpdateStruct('newRole');
780
        /* END: Use Case */
781
782
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\RoleUpdateStruct', $roleUpdate);
783
    }
784
785
    /**
786
     * Test for the updateRoleDraft() method.
787
     *
788
     * @see \eZ\Publish\API\Repository\RoleService::updateRoleDraft()
789
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleUpdateStruct
790
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleDraft
791
     */
792
    public function testUpdateRoleDraft()
793
    {
794
        $repository = $this->getRepository();
795
796
        /* BEGIN: Use Case */
797
        $roleService = $repository->getRoleService();
798
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
799
800
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
801
        // $roleCreate->mainLanguageCode = 'eng-US';
802
803
        $roleDraft = $roleService->createRole($roleCreate);
804
805
        $roleUpdate = $roleService->newRoleUpdateStruct();
806
        $roleUpdate->identifier = 'updatedRole';
807
808
        $updatedRole = $roleService->updateRoleDraft($roleDraft, $roleUpdate);
809
        /* END: Use Case */
810
811
        // Now verify that our change was saved
812
        $role = $roleService->loadRoleDraft($updatedRole->id);
813
814
        $this->assertEquals($role->identifier, 'updatedRole');
815
    }
816
817
    /**
818
     * Test for the updateRoleDraft() method.
819
     *
820
     * @see \eZ\Publish\API\Repository\RoleService::updateRoleDraft()
821
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdateRoleDraft
822
     */
823
    public function testUpdateRoleDraftThrowsInvalidArgumentException()
824
    {
825
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
826
827
        $repository = $this->getRepository();
828
829
        /* BEGIN: Use Case */
830
        $roleService = $repository->getRoleService();
831
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
832
833
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
834
        // $roleCreate->mainLanguageCode = 'eng-US';
835
836
        $roleDraft = $roleService->createRole($roleCreate);
837
838
        $roleUpdate = $roleService->newRoleUpdateStruct();
839
        $roleUpdate->identifier = 'Editor';
840
841
        // This call will fail with an InvalidArgumentException, because Editor is a predefined role
842
        $roleService->updateRoleDraft($roleDraft, $roleUpdate);
843
        /* END: Use Case */
844
    }
845
846
    /**
847
     * Test for the deleteRole() method.
848
     *
849
     * @see \eZ\Publish\API\Repository\RoleService::deleteRole()
850
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
851
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoles
852
     */
853
    public function testDeleteRole()
854
    {
855
        $repository = $this->getRepository();
856
857
        /* BEGIN: Use Case */
858
        $roleService = $repository->getRoleService();
859
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
860
861
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
862
        // $roleCreate->mainLanguageCode = 'eng-US';
863
864
        $roleDraft = $roleService->createRole($roleCreate);
865
        $roleService->publishRoleDraft($roleDraft);
866
        $role = $roleService->loadRole($roleDraft->id);
867
868
        $roleService->deleteRole($role);
869
        /* END: Use Case */
870
871
        $this->assertCount(5, $roleService->loadRoles());
872
    }
873
874
    /**
875
     * Test for the deleteRoleDraft() method.
876
     *
877
     * @see \eZ\Publish\API\Repository\RoleService::deleteRoleDraft()
878
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleDraft
879
     */
880
    public function testDeleteRoleDraft()
881
    {
882
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
883
884
        $repository = $this->getRepository();
885
886
        /* BEGIN: Use Case */
887
        $roleService = $repository->getRoleService();
888
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
889
890
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
891
        // $roleCreate->mainLanguageCode = 'eng-US';
892
893
        $roleDraft = $roleService->createRole($roleCreate);
894
        $roleID = $roleDraft->id;
895
        $roleService->deleteRoleDraft($roleDraft);
896
897
        // This call will fail with a NotFoundException, because the draft no longer exists
898
        $roleService->loadRoleDraft($roleID);
899
        /* END: Use Case */
900
    }
901
902
    /**
903
     * Test for the newPolicyCreateStruct() method.
904
     *
905
     * @see \eZ\Publish\API\Repository\RoleService::newPolicyCreateStruct()
906
     */
907
    public function testNewPolicyCreateStruct()
908
    {
909
        $repository = $this->getRepository();
910
911
        /* BEGIN: Use Case */
912
        $roleService = $repository->getRoleService();
913
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
914
        /* END: Use Case */
915
916
        $this->assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\PolicyCreateStruct', $policyCreate);
917
    }
918
919
    /**
920
     * Test for the newPolicyCreateStruct() method.
921
     *
922
     * @see \eZ\Publish\API\Repository\RoleService::newPolicyCreateStruct()
923
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
924
     */
925
    public function testNewPolicyCreateStructSetsStructProperties()
926
    {
927
        $repository = $this->getRepository();
928
929
        /* BEGIN: Use Case */
930
        $roleService = $repository->getRoleService();
931
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
932
        /* END: Use Case */
933
934
        $this->assertEquals(
935
            ['content', 'create'],
936
            [$policyCreate->module, $policyCreate->function]
937
        );
938
    }
939
940
    /**
941
     * Test for the addPolicyByRoleDraft() method.
942
     *
943
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
944
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
945
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
946
     */
947
    public function testAddPolicyByRoleDraft()
948
    {
949
        $repository = $this->getRepository();
950
951
        /* BEGIN: Use Case */
952
        $roleService = $repository->getRoleService();
953
954
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
955
956
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
957
        // $roleCreate->mainLanguageCode = 'eng-US';
958
959
        $roleDraft = $roleService->createRole($roleCreate);
960
961
        $roleDraft = $roleService->addPolicyByRoleDraft(
962
            $roleDraft,
963
            $roleService->newPolicyCreateStruct('content', 'delete')
964
        );
965
        $roleDraft = $roleService->addPolicyByRoleDraft(
966
            $roleDraft,
967
            $roleService->newPolicyCreateStruct('content', 'create')
968
        );
969
        /* END: Use Case */
970
971
        $actual = [];
972 View Code Duplication
        foreach ($roleDraft->getPolicies() as $policy) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
973
            $actual[] = [
974
                'module' => $policy->module,
975
                'function' => $policy->function,
976
            ];
977
        }
978
        usort(
979
            $actual,
980
            function ($p1, $p2) {
981
                return strcasecmp($p1['function'], $p2['function']);
982
            }
983
        );
984
985
        $this->assertEquals(
986
            [
987
                [
988
                    'module' => 'content',
989
                    'function' => 'create',
990
                ],
991
                [
992
                    'module' => 'content',
993
                    'function' => 'delete',
994
                ],
995
            ],
996
            $actual
997
        );
998
    }
999
1000
    /**
1001
     * Test for the addPolicyByRoleDraft() method.
1002
     *
1003
     * @return array [\eZ\Publish\API\Repository\Values\User\RoleDraft, \eZ\Publish\API\Repository\Values\User\Policy]
1004
     *
1005
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1006
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
1007
     */
1008
    public function testAddPolicyByRoleDraftUpdatesRole()
1009
    {
1010
        $repository = $this->getRepository();
1011
1012
        /* BEGIN: Use Case */
1013
        $roleService = $repository->getRoleService();
1014
1015
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1016
1017
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1018
        // $roleCreate->mainLanguageCode = 'eng-US';
1019
1020
        $roleDraft = $roleService->createRole($roleCreate);
1021
1022
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
1023
        $roleDraft = $roleService->addPolicyByRoleDraft($roleDraft, $policyCreate);
1024
1025
        $policy = null;
1026
        foreach ($roleDraft->getPolicies() as $policy) {
1027
            if ($policy->module === 'content' && $policy->function === 'create') {
1028
                break;
1029
            }
1030
        }
1031
        /* END: Use Case */
1032
1033
        $this->assertInstanceOf(
1034
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Policy',
1035
            $policy
1036
        );
1037
1038
        return [$roleDraft, $policy];
1039
    }
1040
1041
    /**
1042
     * Test for the addPolicyByRoleDraft() method.
1043
     *
1044
     * @param array $roleAndPolicy
1045
     *
1046
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1047
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraftUpdatesRole
1048
     */
1049
    public function testAddPolicyByRoleDraftSetsPolicyProperties($roleAndPolicy)
1050
    {
1051
        list($role, $policy) = $roleAndPolicy;
1052
1053
        $this->assertEquals(
1054
            [$role->id, 'content', 'create'],
1055
            [$policy->roleId, $policy->module, $policy->function]
1056
        );
1057
    }
1058
1059
    /**
1060
     * Test for the addPolicyByRoleDraft() method.
1061
     *
1062
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1063
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
1064
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
1065
     */
1066 View Code Duplication
    public function testAddPolicyByRoleDraftThrowsLimitationValidationException()
1067
    {
1068
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\LimitationValidationException::class);
1069
1070
        $repository = $this->getRepository();
1071
1072
        /* BEGIN: Use Case */
1073
        $roleService = $repository->getRoleService();
1074
1075
        $roleCreate = $roleService->newRoleCreateStruct('Lumberjack');
1076
1077
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1078
        // $roleCreate->mainLanguageCode = 'eng-US';
1079
1080
        $roleDraft = $roleService->createRole($roleCreate);
1081
1082
        // Create new subtree limitation
1083
        $limitation = new SubtreeLimitation(
1084
            [
1085
                'limitationValues' => ['/mountain/forest/tree/42/'],
1086
            ]
1087
        );
1088
1089
        // Create policy create struct and add limitation to it
1090
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'remove');
1091
        $policyCreateStruct->addLimitation($limitation);
1092
1093
        // This call will fail with an LimitationValidationException, because subtree
1094
        // "/mountain/forest/tree/42/" does not exist
1095
        $roleService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
1096
        /* END: Use Case */
1097
    }
1098
1099
    /**
1100
     * Test for the createRole() method.
1101
     *
1102
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
1103
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraftUpdatesRole
1104
     */
1105
    public function testCreateRoleWithAddPolicy()
1106
    {
1107
        $repository = $this->getRepository();
1108
1109
        /* BEGIN: Use Case */
1110
        $roleService = $repository->getRoleService();
1111
1112
        // Instantiate a new create struct
1113
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1114
1115
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1116
        // $roleCreate->mainLanguageCode = 'eng-US';
1117
1118
        // Add some role policies
1119
        $roleCreate->addPolicy(
1120
            $roleService->newPolicyCreateStruct(
1121
                'content',
1122
                'read'
1123
            )
1124
        );
1125
        $roleCreate->addPolicy(
1126
            $roleService->newPolicyCreateStruct(
1127
                'content',
1128
                'translate'
1129
            )
1130
        );
1131
1132
        // Create new role instance
1133
        $roleDraft = $roleService->createRole($roleCreate);
1134
        $roleService->publishRoleDraft($roleDraft);
1135
        $role = $roleService->loadRole($roleDraft->id);
1136
1137
        $policies = [];
1138 View Code Duplication
        foreach ($role->getPolicies() as $policy) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1139
            $policies[] = ['module' => $policy->module, 'function' => $policy->function];
1140
        }
1141
        /* END: Use Case */
1142
        array_multisort($policies);
1143
1144
        $this->assertEquals(
1145
            [
1146
                [
1147
                    'module' => 'content',
1148
                    'function' => 'read',
1149
                ],
1150
                [
1151
                    'module' => 'content',
1152
                    'function' => 'translate',
1153
                ],
1154
            ],
1155
            $policies
1156
        );
1157
    }
1158
1159
    /**
1160
     * Test for the createRoleDraft() method.
1161
     *
1162
     * @see \eZ\Publish\API\Repository\RoleService::createRoleDraft()
1163
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraftUpdatesRole
1164
     */
1165
    public function testCreateRoleDraftWithAddPolicy()
1166
    {
1167
        $repository = $this->getRepository();
1168
1169
        /* BEGIN: Use Case */
1170
        $roleService = $repository->getRoleService();
1171
1172
        // Instantiate a new create struct
1173
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1174
1175
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1176
        // $roleCreate->mainLanguageCode = 'eng-US';
1177
1178
        // Add some role policies
1179
        $roleCreate->addPolicy(
1180
            $roleService->newPolicyCreateStruct(
1181
                'content',
1182
                'read'
1183
            )
1184
        );
1185
        $roleCreate->addPolicy(
1186
            $roleService->newPolicyCreateStruct(
1187
                'content',
1188
                'translate'
1189
            )
1190
        );
1191
1192
        // Create new role instance
1193
        $roleDraft = $roleService->createRole($roleCreate);
1194
1195
        $policies = [];
1196 View Code Duplication
        foreach ($roleDraft->getPolicies() as $policy) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1197
            $policies[] = ['module' => $policy->module, 'function' => $policy->function];
1198
        }
1199
        /* END: Use Case */
1200
1201
        $this->assertEquals(
1202
            [
1203
                [
1204
                    'module' => 'content',
1205
                    'function' => 'read',
1206
                ],
1207
                [
1208
                    'module' => 'content',
1209
                    'function' => 'translate',
1210
                ],
1211
            ],
1212
            $policies
1213
        );
1214
    }
1215
1216
    /**
1217
     * Test for the newPolicyUpdateStruct() method.
1218
     *
1219
     * @see \eZ\Publish\API\Repository\RoleService::newPolicyUpdateStruct()
1220
     */
1221
    public function testNewPolicyUpdateStruct()
1222
    {
1223
        $repository = $this->getRepository();
1224
1225
        /* BEGIN: Use Case */
1226
        $roleService = $repository->getRoleService();
1227
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1228
        /* END: Use Case */
1229
1230
        $this->assertInstanceOf(
1231
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\PolicyUpdateStruct',
1232
            $policyUpdate
1233
        );
1234
    }
1235
1236
    public function testUpdatePolicyByRoleDraftNoLimitation()
1237
    {
1238
        $repository = $this->getRepository();
1239
1240
        /* BEGIN: Use Case */
1241
        $roleService = $repository->getRoleService();
1242
1243
        // Instantiate new policy create
1244
        $policyCreate = $roleService->newPolicyCreateStruct('foo', 'bar');
1245
1246
        // Instantiate a role create and add the policy create
1247
        $roleCreate = $roleService->newRoleCreateStruct('myRole');
1248
1249
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1250
        // $roleCreate->mainLanguageCode = 'eng-US';
1251
1252
        $roleCreate->addPolicy($policyCreate);
1253
1254
        // Create a new role instance.
1255
        $roleDraft = $roleService->createRole($roleCreate);
1256
        $roleService->publishRoleDraft($roleDraft);
1257
        $role = $roleService->loadRole($roleDraft->id);
1258
1259
        $roleDraft = $roleService->createRoleDraft($role);
1260
        // Search for the new policy instance
1261
        $policy = null;
1262
        /** @var \eZ\Publish\API\Repository\Values\User\PolicyDraft $policy */
1263
        foreach ($roleDraft->getPolicies() as $policy) {
1264
            if ($policy->module === 'foo' && $policy->function === 'bar') {
1265
                break;
1266
            }
1267
        }
1268
1269
        // Create an update struct
1270
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1271
1272
        // Update the the policy
1273
        $policy = $roleService->updatePolicyByRoleDraft(
1274
            $roleDraft,
1275
            $policy,
0 ignored issues
show
Compatibility introduced by
$policy of type object<eZ\Publish\API\Re...ory\Values\User\Policy> is not a sub-type of object<eZ\Publish\API\Re...alues\User\PolicyDraft>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\User\Policy to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
1276
            $policyUpdate
1277
        );
1278
        $roleService->publishRoleDraft($roleDraft);
1279
1280
        /* END: Use Case */
1281
1282
        $this->assertInstanceOf(
1283
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Policy',
1284
            $policy
1285
        );
1286
1287
        self::assertEquals([], $policy->getLimitations());
1288
    }
1289
1290
    /**
1291
     * @return array
1292
     *
1293
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicyByRoleDraft()
1294
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
1295
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyUpdateStruct
1296
     */
1297
    public function testUpdatePolicyByRoleDraft()
1298
    {
1299
        $repository = $this->getRepository();
1300
1301
        /* BEGIN: Use Case */
1302
        $roleService = $repository->getRoleService();
1303
1304
        // Instantiate new policy create
1305
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'translate');
1306
1307
        // Add some limitations for the new policy
1308
        $policyCreate->addLimitation(
1309
            new LanguageLimitation(
1310
                [
1311
                    'limitationValues' => ['eng-US', 'eng-GB'],
1312
                ]
1313
            )
1314
        );
1315
1316
        // Instantiate a role create and add the policy create
1317
        $roleCreate = $roleService->newRoleCreateStruct('myRole');
1318
1319
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1320
        // $roleCreate->mainLanguageCode = 'eng-US';
1321
1322
        $roleCreate->addPolicy($policyCreate);
1323
1324
        // Create a new role instance.
1325
        $roleDraft = $roleService->createRole($roleCreate);
1326
        $roleService->publishRoleDraft($roleDraft);
1327
        $role = $roleService->loadRole($roleDraft->id);
1328
1329
        $roleDraft = $roleService->createRoleDraft($role);
1330
        // Search for the new policy instance
1331
        $policy = null;
1332
        /** @var \eZ\Publish\API\Repository\Values\User\PolicyDraft $policy */
1333
        foreach ($roleDraft->getPolicies() as $policy) {
1334
            if ($policy->module === 'content' && $policy->function === 'translate') {
1335
                break;
1336
            }
1337
        }
1338
1339
        // Create an update struct and set a modified limitation
1340
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1341
        $policyUpdate->addLimitation(
1342
            new ContentTypeLimitation(
1343
                [
1344
                    'limitationValues' => [29, 30],
1345
                ]
1346
            )
1347
        );
1348
1349
        // Update the the policy
1350
        $policy = $roleService->updatePolicyByRoleDraft(
1351
            $roleDraft,
1352
            $policy,
0 ignored issues
show
Compatibility introduced by
$policy of type object<eZ\Publish\API\Re...ory\Values\User\Policy> is not a sub-type of object<eZ\Publish\API\Re...alues\User\PolicyDraft>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\User\Policy to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
1353
            $policyUpdate
1354
        );
1355
        $roleService->publishRoleDraft($roleDraft);
1356
1357
        /* END: Use Case */
1358
1359
        $this->assertInstanceOf(
1360
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Policy',
1361
            $policy
1362
        );
1363
1364
        return [$roleService->loadRole($role->id), $policy];
1365
    }
1366
1367
    /**
1368
     * @param array $roleAndPolicy
1369
     *
1370
     * @see \eZ\Publish\API\Repository\RoleService::testUpdatePolicyByRoleDraft()
1371
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdatePolicyByRoleDraft
1372
     */
1373
    public function testUpdatePolicyUpdatesLimitations($roleAndPolicy)
1374
    {
1375
        list($role, $policy) = $roleAndPolicy;
1376
1377
        $this->assertEquals(
1378
            [
1379
                new ContentTypeLimitation(
1380
                    [
1381
                        'limitationValues' => [29, 30],
1382
                    ]
1383
                ),
1384
            ],
1385
            $policy->getLimitations()
1386
        );
1387
1388
        return $role;
1389
    }
1390
1391
    /**
1392
     * Test for the updatePolicy() method.
1393
     *
1394
     * @param \eZ\Publish\API\Repository\Values\User\Role $role
1395
     *
1396
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicyByRoleDraft()
1397
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdatePolicyUpdatesLimitations
1398
     */
1399
    public function testUpdatePolicyUpdatesRole($role)
1400
    {
1401
        $limitations = [];
1402
        foreach ($role->getPolicies() as $policy) {
1403
            foreach ($policy->getLimitations() as $limitation) {
1404
                $limitations[] = $limitation;
1405
            }
1406
        }
1407
1408
        $this->assertCount(1, $limitations);
1409
        $this->assertInstanceOf(
1410
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Limitation',
1411
            $limitations[0]
1412
        );
1413
1414
        $expectedData = [
1415
            'limitationValues' => [29, 30],
1416
        ];
1417
        $this->assertPropertiesCorrectUnsorted(
1418
            $expectedData,
1419
            $limitations[0]
1420
        );
1421
    }
1422
1423
    /**
1424
     * Test for the updatePolicy() method.
1425
     *
1426
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicyByRoleDraft()
1427
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
1428
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyCreateStruct
1429
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewPolicyUpdateStruct
1430
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testNewRoleCreateStruct
1431
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
1432
     */
1433
    public function testUpdatePolicyByRoleDraftThrowsLimitationValidationException()
1434
    {
1435
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\LimitationValidationException::class);
1436
1437
        $repository = $this->getRepository();
1438
1439
        /* BEGIN: Use Case */
1440
        $roleService = $repository->getRoleService();
1441
1442
        // Instantiate new policy create
1443
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'remove');
1444
1445
        // Add some limitations for the new policy
1446
        $policyCreate->addLimitation(
1447
            new SubtreeLimitation(
1448
                [
1449
                    'limitationValues' => ['/1/2/'],
1450
                ]
1451
            )
1452
        );
1453
1454
        // Instantiate a role create and add the policy create
1455
        $roleCreate = $roleService->newRoleCreateStruct('myRole');
1456
1457
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1458
        // $roleCreate->mainLanguageCode = 'eng-US';
1459
1460
        $roleCreate->addPolicy($policyCreate);
1461
1462
        // Create a new role instance.
1463
        $roleDraft = $roleService->createRole($roleCreate);
1464
        $roleService->publishRoleDraft($roleDraft);
1465
        $role = $roleService->loadRole($roleDraft->id);
1466
        $roleDraft = $roleService->createRoleDraft($role);
1467
        // Search for the new policy instance
1468
        $policy = null;
1469
        /** @var \eZ\Publish\API\Repository\Values\User\PolicyDraft $policy */
1470
        foreach ($roleDraft->getPolicies() as $policy) {
1471
            if ($policy->module === 'content' && $policy->function === 'remove') {
1472
                break;
1473
            }
1474
        }
1475
1476
        // Create an update struct and set a modified limitation
1477
        $policyUpdate = $roleService->newPolicyUpdateStruct();
1478
        $policyUpdate->addLimitation(
1479
            new SubtreeLimitation(
1480
                [
1481
                    'limitationValues' => ['/mountain/forest/tree/42/'],
1482
                ]
1483
            )
1484
        );
1485
1486
        // This call will fail with an LimitationValidationException, because subtree
1487
        // "/mountain/forest/tree/42/" does not exist
1488
        $policy = $roleService->updatePolicyByRoleDraft(
0 ignored issues
show
Unused Code introduced by
$policy 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...
1489
            $roleDraft,
1490
            $policy,
0 ignored issues
show
Compatibility introduced by
$policy of type object<eZ\Publish\API\Re...ory\Values\User\Policy> is not a sub-type of object<eZ\Publish\API\Re...alues\User\PolicyDraft>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\User\Policy to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
1491
            $policyUpdate
1492
        );
1493
        /* END: Use Case */
1494
    }
1495
1496
    /**
1497
     * Test for the removePolicyByRoleDraft() method.
1498
     *
1499
     * @see \eZ\Publish\API\Repository\RoleService::removePolicyByRoleDraft()
1500
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
1501
     */
1502 View Code Duplication
    public function testRemovePolicyByRoleDraft()
1503
    {
1504
        $repository = $this->getRepository();
1505
1506
        /* BEGIN: Use Case */
1507
        $roleService = $repository->getRoleService();
1508
1509
        // Instantiate a new role create
1510
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1511
1512
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
1513
        // $roleCreate->mainLanguageCode = 'eng-US';
1514
1515
        // Create a new role with two policies
1516
        $roleDraft = $roleService->createRole($roleCreate);
1517
        $roleService->addPolicyByRoleDraft(
1518
            $roleDraft,
1519
            $roleService->newPolicyCreateStruct('content', 'create')
1520
        );
1521
        $roleService->addPolicyByRoleDraft(
1522
            $roleDraft,
1523
            $roleService->newPolicyCreateStruct('content', 'delete')
1524
        );
1525
1526
        // Delete all policies from the new role
1527
        foreach ($roleDraft->getPolicies() as $policy) {
1528
            $roleDraft = $roleService->removePolicyByRoleDraft($roleDraft, $policy);
0 ignored issues
show
Compatibility introduced by
$policy of type object<eZ\Publish\API\Re...ory\Values\User\Policy> is not a sub-type of object<eZ\Publish\API\Re...alues\User\PolicyDraft>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\User\Policy to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
1529
        }
1530
        /* END: Use Case */
1531
1532
        $this->assertSame([], $roleDraft->getPolicies());
1533
    }
1534
1535
    /**
1536
     * Test for the addPolicyByRoleDraft() method.
1537
     *
1538
     * @see \eZ\Publish\API\Repository\RoleService::addPolicyByRoleDraft()
1539
     */
1540
    public function testAddPolicyWithRoleAssignment()
1541
    {
1542
        $repository = $this->getRepository();
1543
1544
        /* BEGIN: Use Case */
1545
        $roleService = $repository->getRoleService();
1546
        $userService = $repository->getUserService();
1547
1548
        /* Create new user group */
1549
        $mainGroupId = $this->generateId('group', 4);
1550
        $parentUserGroup = $userService->loadUserGroup($mainGroupId);
1551
        $userGroupCreate = $userService->newUserGroupCreateStruct('eng-US');
1552
        $userGroupCreate->setField('name', 'newUserGroup');
1553
        $userGroup = $userService->createUserGroup($userGroupCreate, $parentUserGroup);
1554
1555
        /* Create Role */
1556
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
1557
        $roleDraft = $roleService->createRole($roleCreate);
1558
        $roleService->publishRoleDraft($roleDraft);
1559
1560
        $role = $roleService->loadRole($roleDraft->id);
1561
        $roleService->assignRoleToUserGroup($role, $userGroup);
1562
1563
        $roleAssignmentsBeforeNewPolicy = $roleService->getRoleAssignments($role)[0];
1564
1565
        /* Add new policy to existing role */
1566
        $roleUpdateDraft = $roleService->createRoleDraft($role);
1567
        $roleUpdateDraft = $roleService->addPolicyByRoleDraft(
1568
            $roleUpdateDraft,
1569
            $roleService->newPolicyCreateStruct('content', 'create')
1570
        );
1571
        $roleService->publishRoleDraft($roleUpdateDraft);
1572
1573
        $roleAfterUpdate = $roleService->loadRole($role->id);
1574
        $roleAssignmentsAfterNewPolicy = $roleService->getRoleAssignments($roleAfterUpdate)[0];
1575
        /* END: Use Case */
1576
1577
        $this->assertNotEquals($roleAssignmentsBeforeNewPolicy->id, $roleAssignmentsAfterNewPolicy->id);
1578
    }
1579
1580
    /**
1581
     * Test loading user/group role assignments.
1582
     *
1583
     * @return \eZ\Publish\API\Repository\Values\User\UserGroupRoleAssignment
1584
     *
1585
     * @covers \eZ\Publish\API\Repository\RoleService::loadRoleAssignment
1586
     */
1587
    public function testLoadRoleAssignment()
1588
    {
1589
        $repository = $this->getRepository();
1590
1591
        /* BEGIN: Use Case */
1592
        $roleService = $repository->getRoleService();
1593
        $user = $repository->getUserService()->loadUser(14);
1594
1595
        // Check inital empty assigments (also warms up potential cache to validate it is correct below)
1596
        $this->assertCount(0, $roleService->getRoleAssignmentsForUser($user));
1597
1598
        // Assignment to user group
1599
        $groupRoleAssignment = $roleService->loadRoleAssignment(25);
1600
1601
        // Assignment to user
1602
        $role = $roleService->loadRole(2);
1603
        $roleService->assignRoleToUser($role, $user);
1604
        $userRoleAssignments = $roleService->getRoleAssignmentsForUser($user);
1605
1606
        $userRoleAssignment = $roleService->loadRoleAssignment($userRoleAssignments[0]->id);
1607
        /* END: Use Case */
1608
1609
        $this->assertInstanceOf(
1610
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
1611
            $groupRoleAssignment
1612
        );
1613
1614
        $this->assertEquals(
1615
            [
1616
                12,
1617
                2,
1618
                25,
1619
            ],
1620
            [
1621
                $groupRoleAssignment->userGroup->id,
0 ignored issues
show
Documentation introduced by
The property userGroup does not exist on object<eZ\Publish\API\Re...es\User\RoleAssignment>. 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...
1622
                $groupRoleAssignment->role->id,
1623
                $groupRoleAssignment->id,
1624
            ]
1625
        );
1626
1627
        self::assertInstanceOf('\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment', $userRoleAssignment);
1628
        self::assertEquals(14, $userRoleAssignment->user->id);
0 ignored issues
show
Documentation introduced by
The property user does not exist on object<eZ\Publish\API\Re...es\User\RoleAssignment>. 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...
1629
1630
        return $groupRoleAssignment;
1631
    }
1632
1633
    /**
1634
     * Test for the getRoleAssignments() method.
1635
     *
1636
     * @return \eZ\Publish\API\Repository\Values\User\RoleAssignment[]
1637
     *
1638
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignments()
1639
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
1640
     */
1641
    public function testGetRoleAssignments()
1642
    {
1643
        $repository = $this->getRepository();
1644
1645
        /* BEGIN: Use Case */
1646
        $roleService = $repository->getRoleService();
1647
1648
        // Load the editor role
1649
        $role = $roleService->loadRoleByIdentifier('Editor');
1650
1651
        // Load all assigned users and user groups
1652
        $roleAssignments = $roleService->getRoleAssignments($role);
1653
1654
        /* END: Use Case */
1655
1656
        $this->assertCount(2, $roleAssignments);
1657
        $this->assertInstanceOf(
1658
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
1659
            $roleAssignments[0]
1660
        );
1661
        $this->assertInstanceOf(
1662
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
1663
            $roleAssignments[1]
1664
        );
1665
1666
        return $roleAssignments;
1667
    }
1668
1669
    /**
1670
     * Test for the getRoleAssignments() method.
1671
     *
1672
     * @param \eZ\Publish\API\Repository\Values\User\RoleAssignment[] $roleAssignments
1673
     *
1674
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignments()
1675
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments
1676
     */
1677
    public function testGetRoleAssignmentsContainExpectedLimitation(array $roleAssignments)
1678
    {
1679
        $this->assertEquals(
1680
            'Subtree',
1681
            reset($roleAssignments)->limitation->getIdentifier()
1682
        );
1683
    }
1684
1685
    /**
1686
     * Test for the assignRoleToUser() method.
1687
     *
1688
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser()
1689
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments
1690
     */
1691 View Code Duplication
    public function testAssignRoleToUser()
1692
    {
1693
        $repository = $this->getRepository();
1694
        $roleService = $repository->getRoleService();
1695
1696
        /* BEGIN: Use Case */
1697
        $user = $this->createUserVersion1();
1698
1699
        // Load the existing "Administrator" role
1700
        $role = $roleService->loadRoleByIdentifier('Administrator');
1701
1702
        // Assign the "Administrator" role to the newly created user
1703
        $roleService->assignRoleToUser($role, $user);
1704
1705
        // The assignments array will contain the new role<->user assignment
1706
        $roleAssignments = $roleService->getRoleAssignments($role);
1707
        /* END: Use Case */
1708
1709
        // Administrator + Example User
1710
        $this->assertCount(2, $roleAssignments);
1711
    }
1712
1713
    /**
1714
     * Test for the assignRoleToUser() method.
1715
     *
1716
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
1717
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1718
     */
1719 View Code Duplication
    public function testAssignRoleToUserWithRoleLimitation()
1720
    {
1721
        $repository = $this->getRepository();
1722
        $roleService = $repository->getRoleService();
1723
1724
        /* BEGIN: Use Case */
1725
        $user = $this->createUserVersion1();
1726
1727
        // Load the existing "Anonymous" role
1728
        $role = $roleService->loadRoleByIdentifier('Anonymous');
1729
1730
        // Assign the "Anonymous" role to the newly created user
1731
        $roleService->assignRoleToUser(
1732
            $role,
1733
            $user,
1734
            new SubtreeLimitation(
1735
                [
1736
                    'limitationValues' => ['/1/43/'],
1737
                ]
1738
            )
1739
        );
1740
1741
        // The assignments array will contain the new role<->user assignment
1742
        $roleAssignments = $roleService->getRoleAssignments($role);
1743
        /* END: Use Case */
1744
1745
        // Members + Partners + Anonymous + Example User
1746
        $this->assertCount(4, $roleAssignments);
1747
1748
        // Get the role limitation
1749
        $roleLimitation = null;
1750
        foreach ($roleAssignments as $roleAssignment) {
1751
            $roleLimitation = $roleAssignment->getRoleLimitation();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $roleLimitation is correct as $roleAssignment->getRoleLimitation() (which targets eZ\Publish\API\Repositor...nt::getRoleLimitation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
1752
            if ($roleLimitation) {
1753
                $this->assertInstanceOf(
1754
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
1755
                    $roleAssignment
1756
                );
1757
                break;
1758
            }
1759
        }
1760
1761
        $this->assertEquals(
1762
            new SubtreeLimitation(
1763
                [
1764
                    'limitationValues' => ['/1/43/'],
1765
                ]
1766
            ),
1767
            $roleLimitation
1768
        );
1769
1770
        // Test again to see values being merged
1771
        $roleService->assignRoleToUser(
1772
            $role,
1773
            $user,
1774
            new SubtreeLimitation(
1775
                [
1776
                    'limitationValues' => ['/1/43/', '/1/2/'],
1777
                ]
1778
            )
1779
        );
1780
1781
        // The assignments array will contain the new role<->user assignment
1782
        $roleAssignments = $roleService->getRoleAssignments($role);
1783
1784
        // Members + Partners + Anonymous + Example User
1785
        $this->assertCount(5, $roleAssignments);
1786
1787
        // Get the role limitation
1788
        $roleLimitations = [];
1789
        foreach ($roleAssignments as $roleAssignment) {
1790
            $roleLimitation = $roleAssignment->getRoleLimitation();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $roleLimitation is correct as $roleAssignment->getRoleLimitation() (which targets eZ\Publish\API\Repositor...nt::getRoleLimitation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
1791
            if ($roleLimitation) {
1792
                $this->assertInstanceOf(
1793
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
1794
                    $roleAssignment
1795
                );
1796
                $roleLimitations[] = $roleLimitation;
1797
            }
1798
        }
1799
        array_multisort($roleLimitations);
1800
1801
        $this->assertEquals(
1802
            [
1803
                new SubtreeLimitation(
1804
                    [
1805
                        'limitationValues' => ['/1/2/'],
1806
                    ]
1807
                ),
1808
                new SubtreeLimitation(
1809
                    [
1810
                        'limitationValues' => ['/1/43/'],
1811
                    ]
1812
                ),
1813
            ],
1814
            $roleLimitations
1815
        );
1816
    }
1817
1818
    /**
1819
     * Test for the assignRoleToUser() method.
1820
     *
1821
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
1822
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1823
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
1824
     */
1825
    public function testAssignRoleToUserWithRoleLimitationThrowsLimitationValidationException()
1826
    {
1827
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\LimitationValidationException::class);
1828
1829
        $repository = $this->getRepository();
1830
1831
        /* BEGIN: Use Case */
1832
        $roleService = $repository->getRoleService();
1833
1834
        // Load the existing "Anonymous" role
1835
        $role = $roleService->loadRoleByIdentifier('Anonymous');
1836
1837
        // Get current user
1838
        $permissionResolver = $this->getRepository()->getPermissionResolver();
1839
        $userService = $repository->getUserService();
1840
        $currentUser = $userService->loadUser($permissionResolver->getCurrentUserReference()->getUserId());
1841
1842
        // Assign the "Anonymous" role to the current user
1843
        // This call will fail with an LimitationValidationException, because subtree "/lorem/ipsum/42/"
1844
        // does not exists
1845
        $roleService->assignRoleToUser(
1846
            $role,
1847
            $currentUser,
1848
            new SubtreeLimitation(
1849
                [
1850
                    'limitationValues' => ['/lorem/ipsum/42/'],
1851
                ]
1852
            )
1853
        );
1854
        /* END: Use Case */
1855
    }
1856
1857
    /**
1858
     * Test for the assignRoleToUser() method.
1859
     *
1860
     * Makes sure assigning role several times throws.
1861
     *
1862
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
1863
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1864
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
1865
     */
1866
    public function testAssignRoleToUserThrowsInvalidArgumentException()
1867
    {
1868
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
1869
1870
        $repository = $this->getRepository();
1871
1872
        /* BEGIN: Use Case */
1873
        $roleService = $repository->getRoleService();
1874
1875
        // Load the existing "Anonymous" role
1876
        $role = $roleService->loadRoleByIdentifier('Anonymous');
1877
1878
        // Get current user
1879
        $permissionResolver = $this->getRepository()->getPermissionResolver();
1880
        $userService = $repository->getUserService();
1881
        $currentUser = $userService->loadUser($permissionResolver->getCurrentUserReference()->getUserId());
1882
1883
        // Assign the "Anonymous" role to the current user
1884
        try {
1885
            $roleService->assignRoleToUser(
1886
                $role,
1887
                $currentUser
1888
            );
1889
        } catch (Exception $e) {
1890
            $this->fail('Got exception at first valid attempt to assign role');
1891
        }
1892
1893
        // Re-Assign the "Anonymous" role to the current user
1894
        // This call will fail with an InvalidArgumentException, because limitation is already assigned
1895
        $roleService->assignRoleToUser(
1896
            $role,
1897
            $currentUser
1898
        );
1899
        /* END: Use Case */
1900
    }
1901
1902
    /**
1903
     * Test for the assignRoleToUser() method.
1904
     *
1905
     * Makes sure assigning role several times with same limitations throws.
1906
     *
1907
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
1908
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1909
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
1910
     */
1911
    public function testAssignRoleToUserWithRoleLimitationThrowsInvalidArgumentException()
1912
    {
1913
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
1914
1915
        $repository = $this->getRepository();
1916
1917
        /* BEGIN: Use Case */
1918
        $roleService = $repository->getRoleService();
1919
1920
        // Load the existing "Anonymous" role
1921
        $role = $roleService->loadRoleByIdentifier('Anonymous');
1922
1923
        // Get current user
1924
        $permissionResolver = $this->getRepository()->getPermissionResolver();
1925
        $userService = $repository->getUserService();
1926
        $currentUser = $userService->loadUser($permissionResolver->getCurrentUserReference()->getUserId());
1927
1928
        // Assign the "Anonymous" role to the current user
1929
        try {
1930
            $roleService->assignRoleToUser(
1931
                $role,
1932
                $currentUser,
1933
                new SubtreeLimitation(
1934
                    [
1935
                        'limitationValues' => ['/1/43/', '/1/2/'],
1936
                    ]
1937
                )
1938
            );
1939
        } catch (Exception $e) {
1940
            $this->fail('Got exception at first valid attempt to assign role');
1941
        }
1942
1943
        // Re-Assign the "Anonymous" role to the current user
1944
        // This call will fail with an InvalidArgumentException, because limitation is already assigned
1945
        $roleService->assignRoleToUser(
1946
            $role,
1947
            $currentUser,
1948
            new SubtreeLimitation(
1949
                [
1950
                    'limitationValues' => ['/1/43/'],
1951
                ]
1952
            )
1953
        );
1954
        /* END: Use Case */
1955
    }
1956
1957
    /**
1958
     * Test for the removeRoleAssignment() method.
1959
     *
1960
     * @see \eZ\Publish\API\Repository\RoleService::removeRoleAssignment()
1961
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1962
     */
1963 View Code Duplication
    public function testRemoveRoleAssignment()
1964
    {
1965
        $repository = $this->getRepository();
1966
        $roleService = $repository->getRoleService();
1967
1968
        /* BEGIN: Use Case */
1969
        $user = $this->createUserVersion1();
1970
1971
        // Load the existing "Member" role
1972
        $role = $roleService->loadRoleByIdentifier('Member');
1973
1974
        // Assign the "Member" role to the newly created user
1975
        $roleService->assignRoleToUser($role, $user);
1976
1977
        // Unassign user from role
1978
        $roleAssignments = $roleService->getRoleAssignmentsForUser($user);
1979
        foreach ($roleAssignments as $roleAssignment) {
1980
            if ($roleAssignment->role->id === $role->id) {
1981
                $roleService->removeRoleAssignment($roleAssignment);
1982
            }
1983
        }
1984
        // The assignments array will not contain the new role<->user assignment
1985
        $roleAssignments = $roleService->getRoleAssignments($role);
1986
        /* END: Use Case */
1987
1988
        // Members + Editors + Partners
1989
        $this->assertCount(3, $roleAssignments);
1990
    }
1991
1992
    /**
1993
     * Test for the getRoleAssignmentsForUser() method.
1994
     *
1995
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
1996
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
1997
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
1998
     */
1999
    public function testGetRoleAssignmentsForUserDirect()
2000
    {
2001
        $repository = $this->getRepository();
2002
        $roleService = $repository->getRoleService();
2003
2004
        /* BEGIN: Use Case */
2005
        $user = $this->createUserVersion1();
2006
2007
        // Instantiate a role create and add some policies
2008
        $roleCreate = $roleService->newRoleCreateStruct('Example Role');
2009
2010
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2011
        // $roleCreate->mainLanguageCode = 'eng-US';
2012
2013
        $roleCreate->addPolicy(
2014
            $roleService->newPolicyCreateStruct('user', 'login')
2015
        );
2016
        $roleCreate->addPolicy(
2017
            $roleService->newPolicyCreateStruct('content', 'read')
2018
        );
2019
        $roleCreate->addPolicy(
2020
            $roleService->newPolicyCreateStruct('content', 'edit')
2021
        );
2022
2023
        // Create the new role instance
2024
        $roleDraft = $roleService->createRole($roleCreate);
2025
        $roleService->publishRoleDraft($roleDraft);
2026
        $role = $roleService->loadRole($roleDraft->id);
2027
2028
        // Check inital empty assigments (also warms up potential cache to validate it is correct below)
2029
        $this->assertCount(0, $roleService->getRoleAssignmentsForUser($user));
2030
        $this->assertCount(0, $roleService->getRoleAssignments($role));
2031
2032
        // Assign role to new user
2033
        $roleService->assignRoleToUser($role, $user);
2034
2035
        // Load the currently assigned role
2036
        $roleAssignments = $roleService->getRoleAssignmentsForUser($user);
2037
        /* END: Use Case */
2038
2039
        $this->assertCount(1, $roleAssignments);
2040
        $this->assertInstanceOf(
2041
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserRoleAssignment',
2042
            reset($roleAssignments)
2043
        );
2044
        $this->assertCount(1, $roleService->getRoleAssignments($role));
2045
    }
2046
2047
    /**
2048
     * Test for the getRoleAssignmentsForUser() method.
2049
     *
2050
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
2051
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2052
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
2053
     */
2054 View Code Duplication
    public function testGetRoleAssignmentsForUserEmpty()
2055
    {
2056
        $repository = $this->getRepository();
2057
        $roleService = $repository->getRoleService();
2058
2059
        /* BEGIN: Use Case */
2060
        $permissionResolver = $this->getRepository()->getPermissionResolver();
2061
        $userService = $repository->getUserService();
2062
        $adminUser = $userService->loadUser($permissionResolver->getCurrentUserReference()->getUserId());
2063
2064
        // Load the currently assigned role
2065
        $roleAssignments = $roleService->getRoleAssignmentsForUser($adminUser);
2066
        /* END: Use Case */
2067
2068
        $this->assertCount(0, $roleAssignments);
2069
    }
2070
2071
    /**
2072
     * Test for the getRoleAssignmentsForUser() method.
2073
     *
2074
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
2075
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2076
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
2077
     */
2078
    public function testGetRoleAssignmentsForUserInherited()
2079
    {
2080
        $repository = $this->getRepository();
2081
        $roleService = $repository->getRoleService();
2082
2083
        /* BEGIN: Use Case */
2084
        $permissionResolver = $this->getRepository()->getPermissionResolver();
2085
        $userService = $repository->getUserService();
2086
        $adminUser = $userService->loadUser($permissionResolver->getCurrentUserReference()->getUserId());
2087
2088
        // Load the currently assigned role + inherited role assignments
2089
        $roleAssignments = $roleService->getRoleAssignmentsForUser($adminUser, true);
2090
        /* END: Use Case */
2091
2092
        $this->assertCount(1, $roleAssignments);
2093
        $this->assertInstanceOf(
2094
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2095
            reset($roleAssignments)
2096
        );
2097
    }
2098
2099
    /**
2100
     * Test for the assignRoleToUserGroup() method.
2101
     *
2102
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup()
2103
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments
2104
     */
2105 View Code Duplication
    public function testAssignRoleToUserGroup()
2106
    {
2107
        $repository = $this->getRepository();
2108
        $roleService = $repository->getRoleService();
2109
2110
        /* BEGIN: Use Case */
2111
        $userGroup = $this->createUserGroupVersion1();
2112
2113
        // Load the existing "Administrator" role
2114
        $role = $roleService->loadRoleByIdentifier('Administrator');
2115
2116
        // Assign the "Administrator" role to the newly created user group
2117
        $roleService->assignRoleToUserGroup($role, $userGroup);
2118
2119
        // The assignments array will contain the new role<->group assignment
2120
        $roleAssignments = $roleService->getRoleAssignments($role);
2121
        /* END: Use Case */
2122
2123
        // Administrator + Example Group
2124
        $this->assertCount(2, $roleAssignments);
2125
    }
2126
2127
    /**
2128
     * Test for the assignRoleToUserGroup() method.
2129
     *
2130
     * Related issue: EZP-29113
2131
     *
2132
     * @covers \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup()
2133
     */
2134
    public function testAssignRoleToUserGroupAffectsRoleAssignmentsForUser()
2135
    {
2136
        $roleService = $this->getRepository()->getRoleService();
2137
2138
        /* BEGIN: Use Case */
2139
        $userGroup = $this->createUserGroupVersion1();
2140
        $user = $this->createUser('user', 'John', 'Doe', $userGroup);
2141
2142
        $initRoleAssignments = $roleService->getRoleAssignmentsForUser($user, true);
2143
2144
        // Load the existing "Administrator" role
2145
        $role = $roleService->loadRoleByIdentifier('Administrator');
2146
2147
        // Assign the "Administrator" role to the newly created user group
2148
        $roleService->assignRoleToUserGroup($role, $userGroup);
2149
2150
        $updatedRoleAssignments = $roleService->getRoleAssignmentsForUser($user, true);
2151
        /* END: Use Case */
2152
2153
        $this->assertEmpty($initRoleAssignments);
2154
        $this->assertCount(1, $updatedRoleAssignments);
2155
    }
2156
2157
    /**
2158
     * Test for the assignRoleToUserGroup() method.
2159
     *
2160
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2161
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2162
     */
2163 View Code Duplication
    public function testAssignRoleToUserGroupWithRoleLimitation()
2164
    {
2165
        $repository = $this->getRepository();
2166
        $roleService = $repository->getRoleService();
2167
2168
        /* BEGIN: Use Case */
2169
        $userGroup = $this->createUserGroupVersion1();
2170
2171
        // Load the existing "Anonymous" role
2172
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2173
2174
        // Assign the "Anonymous" role to the newly created user group
2175
        $roleService->assignRoleToUserGroup(
2176
            $role,
2177
            $userGroup,
2178
            new SubtreeLimitation(
2179
                [
2180
                    'limitationValues' => ['/1/43/'],
2181
                ]
2182
            )
2183
        );
2184
2185
        // The assignments array will contain the new role<->group assignment
2186
        $roleAssignments = $roleService->getRoleAssignments($role);
2187
        /* END: Use Case */
2188
2189
        // Members + Partners + Anonymous + Example Group
2190
        $this->assertCount(4, $roleAssignments);
2191
2192
        // Get the role limitation
2193
        $roleLimitation = null;
2194
        foreach ($roleAssignments as $roleAssignment) {
2195
            $roleLimitation = $roleAssignment->getRoleLimitation();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $roleLimitation is correct as $roleAssignment->getRoleLimitation() (which targets eZ\Publish\API\Repositor...nt::getRoleLimitation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
2196
            if ($roleLimitation) {
2197
                break;
2198
            }
2199
        }
2200
2201
        $this->assertEquals(
2202
            new SubtreeLimitation(
2203
                [
2204
                    'limitationValues' => ['/1/43/'],
2205
                ]
2206
            ),
2207
            $roleLimitation
2208
        );
2209
2210
        // Test again to see values being merged
2211
        $roleService->assignRoleToUserGroup(
2212
            $role,
2213
            $userGroup,
2214
            new SubtreeLimitation(
2215
                [
2216
                    'limitationValues' => ['/1/43/', '/1/2/'],
2217
                ]
2218
            )
2219
        );
2220
2221
        // The assignments array will contain the new role<->user assignment
2222
        $roleAssignments = $roleService->getRoleAssignments($role);
2223
2224
        // Members + Partners + Anonymous + Example User
2225
        $this->assertCount(5, $roleAssignments);
2226
2227
        // Get the role limitation
2228
        $roleLimitations = [];
2229
        foreach ($roleAssignments as $roleAssignment) {
2230
            $roleLimitation = $roleAssignment->getRoleLimitation();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $roleLimitation is correct as $roleAssignment->getRoleLimitation() (which targets eZ\Publish\API\Repositor...nt::getRoleLimitation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
2231
            if ($roleLimitation) {
2232
                $this->assertInstanceOf(
2233
                    '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2234
                    $roleAssignment
2235
                );
2236
                $roleLimitations[] = $roleLimitation;
2237
            }
2238
        }
2239
        array_multisort($roleLimitations);
2240
2241
        $this->assertEquals(
2242
            [
2243
                new SubtreeLimitation(
2244
                    [
2245
                        'limitationValues' => ['/1/2/'],
2246
                    ]
2247
                ),
2248
                new SubtreeLimitation(
2249
                    [
2250
                        'limitationValues' => ['/1/43/'],
2251
                    ]
2252
                ),
2253
            ],
2254
            $roleLimitations
2255
        );
2256
    }
2257
2258
    /**
2259
     * Test for the assignRoleToUserGroup() method.
2260
     *
2261
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2262
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2263
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2264
     */
2265 View Code Duplication
    public function testAssignRoleToUserGroupWithRoleLimitationThrowsLimitationValidationException()
2266
    {
2267
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\LimitationValidationException::class);
2268
2269
        $repository = $this->getRepository();
2270
2271
        $mainGroupId = $this->generateId('group', 4);
2272
        /* BEGIN: Use Case */
2273
        // $mainGroupId is the ID of the main "Users" group
2274
2275
        $userService = $repository->getUserService();
2276
        $roleService = $repository->getRoleService();
2277
2278
        $userGroup = $userService->loadUserGroup($mainGroupId);
2279
2280
        // Load the existing "Anonymous" role
2281
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2282
2283
        // Assign the "Anonymous" role to the newly created user group
2284
        // This call will fail with an LimitationValidationException, because subtree "/lorem/ipsum/42/"
2285
        // does not exists
2286
        $roleService->assignRoleToUserGroup(
2287
            $role,
2288
            $userGroup,
2289
            new SubtreeLimitation(
2290
                [
2291
                    'limitationValues' => ['/lorem/ipsum/42/'],
2292
                ]
2293
            )
2294
        );
2295
        /* END: Use Case */
2296
    }
2297
2298
    /**
2299
     * Test for the assignRoleToUserGroup() method.
2300
     *
2301
     * Makes sure assigning role several times throws.
2302
     *
2303
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2304
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2305
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2306
     */
2307 View Code Duplication
    public function testAssignRoleToUserGroupThrowsInvalidArgumentException()
2308
    {
2309
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
2310
2311
        $repository = $this->getRepository();
2312
2313
        $mainGroupId = $this->generateId('group', 4);
2314
        /* BEGIN: Use Case */
2315
        // $mainGroupId is the ID of the main "Users" group
2316
2317
        $userService = $repository->getUserService();
2318
        $roleService = $repository->getRoleService();
2319
2320
        $userGroup = $userService->loadUserGroup($mainGroupId);
2321
2322
        // Load the existing "Anonymous" role
2323
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2324
2325
        // Assign the "Anonymous" role to the newly created user group
2326
        try {
2327
            $roleService->assignRoleToUserGroup(
2328
                $role,
2329
                $userGroup
2330
            );
2331
        } catch (Exception $e) {
2332
            $this->fail('Got exception at first valid attempt to assign role');
2333
        }
2334
2335
        // Re-Assign the "Anonymous" role to the newly created user group
2336
        // This call will fail with an InvalidArgumentException, because role is already assigned
2337
        $roleService->assignRoleToUserGroup(
2338
            $role,
2339
            $userGroup
2340
        );
2341
        /* END: Use Case */
2342
    }
2343
2344
    /**
2345
     * Test for the assignRoleToUserGroup() method.
2346
     *
2347
     * Makes sure assigning role several times with same limitations throws.
2348
     *
2349
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
2350
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
2351
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2352
     */
2353
    public function testAssignRoleToUserGroupWithRoleLimitationThrowsInvalidArgumentException()
2354
    {
2355
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
2356
2357
        $repository = $this->getRepository();
2358
2359
        $mainGroupId = $this->generateId('group', 4);
2360
        /* BEGIN: Use Case */
2361
        // $mainGroupId is the ID of the main "Users" group
2362
2363
        $userService = $repository->getUserService();
2364
        $roleService = $repository->getRoleService();
2365
2366
        $userGroup = $userService->loadUserGroup($mainGroupId);
2367
2368
        // Load the existing "Anonymous" role
2369
        $role = $roleService->loadRoleByIdentifier('Anonymous');
2370
2371
        // Assign the "Anonymous" role to the newly created user group
2372
        try {
2373
            $roleService->assignRoleToUserGroup(
2374
                $role,
2375
                $userGroup,
2376
                new SubtreeLimitation(
2377
                    [
2378
                        'limitationValues' => ['/1/43/', '/1/2/'],
2379
                    ]
2380
                )
2381
            );
2382
        } catch (Exception $e) {
2383
            $this->fail('Got exception at first valid attempt to assign role');
2384
        }
2385
2386
        // Re-Assign the "Anonymous" role to the newly created user group
2387
        // This call will fail with an InvalidArgumentException, because limitation is already assigned
2388
        $roleService->assignRoleToUserGroup(
2389
            $role,
2390
            $userGroup,
2391
            new SubtreeLimitation(
2392
                [
2393
                    'limitationValues' => ['/1/43/'],
2394
                ]
2395
            )
2396
        );
2397
        /* END: Use Case */
2398
    }
2399
2400
    /**
2401
     * Test for the removeRoleAssignment() method.
2402
     *
2403
     * @see \eZ\Publish\API\Repository\RoleService::removeRoleAssignment()
2404
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2405
     */
2406
    public function testRemoveRoleAssignmentFromUserGroup()
2407
    {
2408
        $repository = $this->getRepository();
2409
        $roleService = $repository->getRoleService();
2410
2411
        /* BEGIN: Use Case */
2412
        $userGroup = $this->createUserGroupVersion1();
2413
2414
        // Load the existing "Member" role
2415
        $role = $roleService->loadRoleByIdentifier('Member');
2416
2417
        // Assign the "Member" role to the newly created user group
2418
        $roleService->assignRoleToUserGroup($role, $userGroup);
2419
2420
        // Unassign group from role
2421
        $roleAssignments = $roleService->getRoleAssignmentsForUserGroup($userGroup);
2422
2423
        // This call will fail with an "UnauthorizedException"
2424
        foreach ($roleAssignments as $roleAssignment) {
2425
            if ($roleAssignment->role->id === $role->id) {
2426
                $roleService->removeRoleAssignment($roleAssignment);
2427
            }
2428
        }
2429
        // The assignments array will not contain the new role<->group assignment
2430
        $roleAssignments = $roleService->getRoleAssignments($role);
2431
        /* END: Use Case */
2432
2433
        // Members + Editors + Partners
2434
        $this->assertCount(3, $roleAssignments);
2435
    }
2436
2437
    /**
2438
     * Test unassigning role by assignment.
2439
     *
2440
     * @covers \eZ\Publish\API\Repository\RoleService::removeRoleAssignment
2441
     */
2442
    public function testUnassignRoleByAssignment()
2443
    {
2444
        $repository = $this->getRepository();
2445
        $roleService = $repository->getRoleService();
2446
2447
        $role = $roleService->loadRole(2);
2448
        $user = $repository->getUserService()->loadUser(14);
2449
2450
        $originalAssignmentCount = count($roleService->getRoleAssignmentsForUser($user));
2451
2452
        $roleService->assignRoleToUser($role, $user);
2453
        $newAssignmentCount = count($roleService->getRoleAssignmentsForUser($user));
2454
        self::assertEquals($originalAssignmentCount + 1, $newAssignmentCount);
2455
2456
        $assignments = $roleService->getRoleAssignmentsForUser($user);
2457
        $roleService->removeRoleAssignment($assignments[0]);
2458
        $finalAssignmentCount = count($roleService->getRoleAssignmentsForUser($user));
2459
        self::assertEquals($newAssignmentCount - 1, $finalAssignmentCount);
2460
    }
2461
2462
    /**
2463
     * Test unassigning role by assignment.
2464
     *
2465
     * But on current admin user so he lacks access to read roles.
2466
     *
2467
     * @covers \eZ\Publish\API\Repository\RoleService::removeRoleAssignment
2468
     */
2469 View Code Duplication
    public function testUnassignRoleByAssignmentThrowsUnauthorizedException()
2470
    {
2471
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class);
2472
2473
        $repository = $this->getRepository();
2474
        $roleService = $repository->getRoleService();
2475
2476
        try {
2477
            $adminUserGroup = $repository->getUserService()->loadUserGroup(12);
2478
            $assignments = $roleService->getRoleAssignmentsForUserGroup($adminUserGroup);
2479
            $roleService->removeRoleAssignment($assignments[0]);
2480
        } catch (Exception $e) {
2481
            self::fail(
2482
                'Unexpected exception: ' . $e->getMessage() . " \n[" . $e->getFile() . ' (' . $e->getLine() . ')]'
2483
            );
2484
        }
2485
2486
        $roleService->removeRoleAssignment($assignments[0]);
0 ignored issues
show
Bug introduced by
The variable $assignments does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
2487
    }
2488
2489
    /**
2490
     * Test unassigning role by non-existing assignment.
2491
     *
2492
     * @covers \eZ\Publish\API\Repository\RoleService::removeRoleAssignment
2493
     */
2494 View Code Duplication
    public function testUnassignRoleByAssignmentThrowsNotFoundException()
2495
    {
2496
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class);
2497
2498
        $repository = $this->getRepository();
2499
        $roleService = $repository->getRoleService();
2500
2501
        try {
2502
            $editorsUserGroup = $repository->getUserService()->loadUserGroup(13);
2503
            $assignments = $roleService->getRoleAssignmentsForUserGroup($editorsUserGroup);
2504
            $roleService->removeRoleAssignment($assignments[0]);
2505
        } catch (Exception $e) {
2506
            self::fail(
2507
                'Unexpected exception: ' . $e->getMessage() . " \n[" . $e->getFile() . ' (' . $e->getLine() . ')]'
2508
            );
2509
        }
2510
2511
        $roleService->removeRoleAssignment($assignments[0]);
0 ignored issues
show
Bug introduced by
The variable $assignments does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
2512
    }
2513
2514
    /**
2515
     * Test for the getRoleAssignmentsForUserGroup() method.
2516
     *
2517
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUserGroup()
2518
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2519
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleWithAddPolicy
2520
     */
2521
    public function testGetRoleAssignmentsForUserGroup()
2522
    {
2523
        $repository = $this->getRepository();
2524
        $roleService = $repository->getRoleService();
2525
2526
        /* BEGIN: Use Case */
2527
        $userGroup = $this->createUserGroupVersion1();
2528
2529
        // Instantiate a role create and add some policies
2530
        $roleCreate = $roleService->newRoleCreateStruct('Example Role');
2531
2532
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2533
        // $roleCreate->mainLanguageCode = 'eng-US';
2534
2535
        $roleCreate->addPolicy(
2536
            $roleService->newPolicyCreateStruct('user', 'login')
2537
        );
2538
        $roleCreate->addPolicy(
2539
            $roleService->newPolicyCreateStruct('content', 'read')
2540
        );
2541
        $roleCreate->addPolicy(
2542
            $roleService->newPolicyCreateStruct('content', 'edit')
2543
        );
2544
2545
        // Create the new role instance
2546
        $roleDraft = $roleService->createRole($roleCreate);
2547
        $roleService->publishRoleDraft($roleDraft);
2548
        $role = $roleService->loadRole($roleDraft->id);
2549
2550
        // Assign role to new user group
2551
        $roleService->assignRoleToUserGroup($role, $userGroup);
2552
2553
        // Load the currently assigned role
2554
        $roleAssignments = $roleService->getRoleAssignmentsForUserGroup($userGroup);
2555
        /* END: Use Case */
2556
2557
        $this->assertCount(1, $roleAssignments);
2558
        $this->assertInstanceOf(
2559
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\UserGroupRoleAssignment',
2560
            reset($roleAssignments)
2561
        );
2562
    }
2563
2564
    /**
2565
     * Test for the getRoleAssignmentsForUser() method.
2566
     *
2567
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
2568
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
2569
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
2570
     */
2571
    public function testLoadPoliciesByUserId()
2572
    {
2573
        $repository = $this->getRepository();
2574
2575
        $anonUserId = $this->generateId('user', 10);
2576
        /* BEGIN: Use Case */
2577
        // $anonUserId is the ID of the "Anonymous" user.
2578
2579
        $userService = $repository->getUserService();
2580
        $roleService = $repository->getRoleService();
2581
2582
        // Load "Anonymous" user
2583
        $user = $userService->loadUser($anonUserId);
2584
2585
        // Instantiate a role create and add some policies
2586
        $roleCreate = $roleService->newRoleCreateStruct('User Role');
2587
2588
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2589
        // $roleCreate->mainLanguageCode = 'eng-US';
2590
2591
        $roleCreate->addPolicy(
2592
            $roleService->newPolicyCreateStruct('notification', 'use')
2593
        );
2594
        $roleCreate->addPolicy(
2595
            $roleService->newPolicyCreateStruct('user', 'password')
2596
        );
2597
        $roleCreate->addPolicy(
2598
            $roleService->newPolicyCreateStruct('user', 'selfedit')
2599
        );
2600
2601
        // Create the new role instance
2602
        $roleDraft = $roleService->createRole($roleCreate);
2603
        $roleService->publishRoleDraft($roleDraft);
2604
        $role = $roleService->loadRole($roleDraft->id);
2605
2606
        // Assign role to anon user
2607
        $roleService->assignRoleToUser($role, $user);
2608
        $roleAssignments = $roleService->getRoleAssignmentsForUser($user, true);
2609
2610
        $policies = [];
2611
        foreach ($roleAssignments as $roleAssignment) {
2612
            $policies[] = $roleAssignment->getRole()->getPolicies();
2613
        }
2614
        $policies = array_merge(...$policies);
2615
2616
        $simplePolicyList = [];
2617
        foreach ($policies as $simplePolicy) {
2618
            $simplePolicyList[] = [$simplePolicy->roleId, $simplePolicy->module, $simplePolicy->function];
2619
        }
2620
        /* END: Use Case */
2621
        array_multisort($simplePolicyList);
2622
2623
        $this->assertEquals(
2624
            [
2625
                [1, 'content', 'pdf'],
2626
                [1, 'content', 'read'],
2627
                [1, 'content', 'read'],
2628
                [1, 'rss', 'feed'],
2629
                [1, 'user', 'login'],
2630
                [1, 'user', 'login'],
2631
                [1, 'user', 'login'],
2632
                [1, 'user', 'login'],
2633
                [$role->id, 'notification', 'use'],
2634
                [$role->id, 'user', 'password'],
2635
                [$role->id, 'user', 'selfedit'],
2636
            ],
2637
            $simplePolicyList
2638
        );
2639
    }
2640
2641
    /**
2642
     * Test for the publishRoleDraft() method.
2643
     *
2644
     * @see \eZ\Publish\API\Repository\RoleService::publishRoleDraft()
2645
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
2646
     */
2647 View Code Duplication
    public function testPublishRoleDraft()
2648
    {
2649
        $repository = $this->getRepository();
2650
2651
        /* BEGIN: Use Case */
2652
        $roleService = $repository->getRoleService();
2653
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
2654
2655
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2656
        // $roleCreate->mainLanguageCode = 'eng-US';
2657
2658
        $roleDraft = $roleService->createRole($roleCreate);
2659
2660
        $roleDraft = $roleService->addPolicyByRoleDraft(
2661
            $roleDraft,
2662
            $roleService->newPolicyCreateStruct('content', 'delete')
2663
        );
2664
        $roleDraft = $roleService->addPolicyByRoleDraft(
2665
            $roleDraft,
2666
            $roleService->newPolicyCreateStruct('content', 'create')
2667
        );
2668
2669
        $roleService->publishRoleDraft($roleDraft);
2670
        /* END: Use Case */
2671
2672
        $this->assertInstanceOf(
2673
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\Role',
2674
            $roleService->loadRoleByIdentifier($roleCreate->identifier)
2675
        );
2676
    }
2677
2678
    /**
2679
     * Test for the publishRoleDraft() method.
2680
     *
2681
     * @see \eZ\Publish\API\Repository\RoleService::publishRoleDraft()
2682
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRoleDraft
2683
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicyByRoleDraft
2684
     */
2685
    public function testPublishRoleDraftAddPolicies()
2686
    {
2687
        $repository = $this->getRepository();
2688
2689
        /* BEGIN: Use Case */
2690
        $roleService = $repository->getRoleService();
2691
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
2692
2693
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
2694
        // $roleCreate->mainLanguageCode = 'eng-US';
2695
2696
        $roleDraft = $roleService->createRole($roleCreate);
2697
2698
        $roleDraft = $roleService->addPolicyByRoleDraft(
2699
            $roleDraft,
2700
            $roleService->newPolicyCreateStruct('content', 'delete')
2701
        );
2702
        $roleDraft = $roleService->addPolicyByRoleDraft(
2703
            $roleDraft,
2704
            $roleService->newPolicyCreateStruct('content', 'create')
2705
        );
2706
2707
        $roleService->publishRoleDraft($roleDraft);
2708
        $role = $roleService->loadRoleByIdentifier($roleCreate->identifier);
2709
        /* END: Use Case */
2710
2711
        $actual = [];
2712
        foreach ($role->getPolicies() as $policy) {
2713
            $actual[] = [
2714
                'module' => $policy->module,
2715
                'function' => $policy->function,
2716
            ];
2717
        }
2718
        usort(
2719
            $actual,
2720
            function ($p1, $p2) {
2721
                return strcasecmp($p1['function'], $p2['function']);
2722
            }
2723
        );
2724
2725
        $this->assertEquals(
2726
            [
2727
                [
2728
                    'module' => 'content',
2729
                    'function' => 'create',
2730
                ],
2731
                [
2732
                    'module' => 'content',
2733
                    'function' => 'delete',
2734
                ],
2735
            ],
2736
            $actual
2737
        );
2738
    }
2739
2740
    /**
2741
     * Create a user group fixture in a variable named <b>$userGroup</b>,.
2742
     *
2743
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup
2744
     */
2745
    private function createUserGroupVersion1()
2746
    {
2747
        $repository = $this->getRepository();
2748
2749
        $mainGroupId = $this->generateId('group', 4);
2750
        /* BEGIN: Inline */
2751
        // $mainGroupId is the ID of the main "Users" group
2752
2753
        $roleService = $repository->getRoleService();
0 ignored issues
show
Unused Code introduced by
$roleService 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...
2754
        $userService = $repository->getUserService();
2755
2756
        // Load main group
2757
        $parentUserGroup = $userService->loadUserGroup($mainGroupId);
2758
2759
        // Instantiate a new create struct
2760
        $userGroupCreate = $userService->newUserGroupCreateStruct('eng-US');
2761
        $userGroupCreate->setField('name', 'Example Group');
2762
2763
        // Create the new user group
2764
        $userGroup = $userService->createUserGroup(
2765
            $userGroupCreate,
2766
            $parentUserGroup
2767
        );
2768
        /* END: Inline */
2769
2770
        return $userGroup;
2771
    }
2772
}
2773