Completed
Push — 7.5 ( 2fd8d6...0209fd )
by Łukasz
18:31
created

testLoadRolesLoadsEmptyListForAnonymousUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the RoleServiceAuthorizationTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\API\Repository\Tests;
10
11
use eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation;
12
13
/**
14
 * Test case for operations in the RoleService using in memory storage.
15
 *
16
 * @see eZ\Publish\API\Repository\RoleService
17
 * @group integration
18
 * @group authorization
19
 */
20
class RoleServiceAuthorizationTest extends BaseTest
21
{
22
    /**
23
     * Test for the createRole() method.
24
     *
25
     * @see \eZ\Publish\API\Repository\RoleService::createRole()
26
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
27
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole
28
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
29
     */
30
    public function testCreateRoleThrowsUnauthorizedException()
31
    {
32
        $repository = $this->getRepository();
33
34
        /* BEGIN: Use Case */
35
        $user = $this->createUserVersion1();
36
37
        // Set "Editor" user as current user.
38
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
39
40
        // Get the role service
41
        $roleService = $repository->getRoleService();
42
43
        // Instantiate a role create struct.
44
        $roleCreate = $roleService->newRoleCreateStruct('roleName');
45
46
        // This call will fail with an "UnauthorizedException"
47
        $roleService->createRole($roleCreate);
48
        /* END: Use Case */
49
    }
50
51
    /**
52
     * Test for the loadRole() method.
53
     *
54
     * @see \eZ\Publish\API\Repository\RoleService::loadRole()
55
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
56
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRole
57
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
58
     */
59
    public function testLoadRoleThrowsUnauthorizedException()
60
    {
61
        $repository = $this->getRepository();
62
        $roleService = $repository->getRoleService();
63
64
        /* BEGIN: Use Case */
65
        $user = $this->createUserVersion1();
66
67
        $role = $this->createRole();
68
69
        // Set "Editor" user as current user.
70
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
71
72
        // This call will fail with an "UnauthorizedException"
73
        $roleService->loadRole($role->id);
74
        /* END: Use Case */
75
    }
76
77
    /**
78
     * Test for the loadRoleByIdentifier() method.
79
     *
80
     * @see \eZ\Publish\API\Repository\RoleService::loadRoleByIdentifier()
81
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
82
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier
83
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
84
     */
85
    public function testLoadRoleByIdentifierThrowsUnauthorizedException()
86
    {
87
        $repository = $this->getRepository();
88
        $roleService = $repository->getRoleService();
89
90
        /* BEGIN: Use Case */
91
        $user = $this->createUserVersion1();
92
93
        $role = $this->createRole();
94
95
        // Set "Editor" user as current user.
96
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
97
98
        // This call will fail with an "UnauthorizedException"
99
        $roleService->loadRoleByIdentifier($role->identifier);
100
        /* END: Use Case */
101
    }
102
103
    /**
104
     * Test for the loadRoles() method.
105
     *
106
     * @see \eZ\Publish\API\Repository\RoleService::loadRoles()
107
     */
108
    public function testLoadRolesLoadsEmptyListForAnonymousUser()
109
    {
110
        $repository = $this->getRepository();
111
112
        /* BEGIN: Use Case */
113
        $user = $this->createUserVersion1();
114
115
        // Set "Editor" user as current user.
116
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
117
118
        // Get the role service
119
        $roleService = $repository->getRoleService();
120
        /* END: Use Case */
121
122
        $this->assertEquals([], $roleService->loadRoles());
123
    }
124
125
    /**
126
     * Test for the loadRoles() method.
127
     *
128
     * @see \eZ\Publish\API\Repository\RoleService::loadRoles()
129
     */
130
    public function testLoadRolesForUserWithSubtreeLimitation()
131
    {
132
        $repository = $this->getRepository();
133
        $roleService = $repository->getRoleService();
134
135
        /* BEGIN: Use Case */
136
        // create user that can read/create/delete but cannot edit or content
137
        $this->createRoleWithPolicies('roleReader', [
138
            ['module' => 'role', 'function' => 'read'],
139
        ]);
140
141
        $user = $this->createCustomUserWithLogin(
142
            'user',
143
            '[email protected]',
144
            'roleReaders',
145
            'roleReader',
146
            new SubtreeLimitation(['limitationValues' => ['/1/2/']])
147
        );
148
149
        $repository->getPermissionResolver()->setCurrentUserReference($user);
150
        /* END: Use Case */
151
152
        $this->assertCount(6, $roleService->loadRoles());
153
    }
154
155
    /**
156
     * Test for the updateRole() method.
157
     *
158
     * @see \eZ\Publish\API\Repository\RoleService::updateRole()
159
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
160
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdateRole
161
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
162
     */
163 View Code Duplication
    public function testUpdateRoleThrowsUnauthorizedException()
164
    {
165
        $repository = $this->getRepository();
166
        $roleService = $repository->getRoleService();
167
168
        /* BEGIN: Use Case */
169
        $user = $this->createUserVersion1();
170
171
        $role = $this->createRole();
172
173
        // Set "Editor" user as current user.
174
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
175
176
        // Get a new role update struct and set new values
177
        $roleUpdateStruct = $roleService->newRoleUpdateStruct();
178
179
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
180
        // $roleUpdateStruct->mainLanguageCode = 'eng-US';
181
182
        // This call will fail with an "UnauthorizedException"
183
        $roleService->updateRole($role, $roleUpdateStruct);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...leService::updateRole() has been deprecated with message: since 6.0, use {@see updateRoleDraft}

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

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

Loading history...
184
        /* END: Use Case */
185
    }
186
187
    /**
188
     * Test for the deleteRole() method.
189
     *
190
     * @see \eZ\Publish\API\Repository\RoleService::deleteRole()
191
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
192
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testDeleteRole
193
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
194
     */
195
    public function testDeleteRoleThrowsUnauthorizedException()
196
    {
197
        $repository = $this->getRepository();
198
        $roleService = $repository->getRoleService();
199
200
        /* BEGIN: Use Case */
201
        $user = $this->createUserVersion1();
202
203
        $role = $this->createRole();
204
205
        // Set "Editor" user as current user.
206
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
207
208
        // This call will fail with an "UnauthorizedException"
209
        $roleService->deleteRole($role);
210
        /* END: Use Case */
211
    }
212
213
    /**
214
     * Test for the addPolicy() method.
215
     *
216
     * @see \eZ\Publish\API\Repository\RoleService::addPolicy()
217
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
218
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicy
219
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
220
     */
221
    public function testAddPolicyThrowsUnauthorizedException()
222
    {
223
        $repository = $this->getRepository();
224
        $roleService = $repository->getRoleService();
225
226
        /* BEGIN: Use Case */
227
        $user = $this->createUserVersion1();
228
229
        $role = $this->createRole();
230
231
        // Set "Editor" user as current user.
232
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
233
234
        // This call will fail with an "UnauthorizedException"
235
        $roleService->addPolicy(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\RoleService::addPolicy() has been deprecated with message: since 6.0, use {@see addPolicyByRoleDraft}

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

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

Loading history...
236
            $role,
237
            $roleService->newPolicyCreateStruct('content', 'delete')
238
        );
239
        /* END: Use Case */
240
    }
241
242
    /**
243
     * Test for the updatePolicy() method.
244
     *
245
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicy()
246
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
247
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdatePolicy
248
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
249
     */
250 View Code Duplication
    public function testUpdatePolicyThrowsUnauthorizedException()
251
    {
252
        $repository = $this->getRepository();
253
        $roleService = $repository->getRoleService();
254
255
        /* BEGIN: Use Case */
256
        $user = $this->createUserVersion1();
257
258
        $role = $this->createRole();
259
260
        // Get first role policy
261
        $policies = $role->getPolicies();
262
        $policy = reset($policies);
263
264
        // Set "Editor" user as current user.
265
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
266
267
        // Get a policy update struct and add a limitation
268
        $policyUpdate = $roleService->newPolicyUpdateStruct();
269
        $policyUpdate->addLimitation(
270
            new SubtreeLimitation(
271
                [
272
                    'limitationValues' => ['/1/'],
273
                ]
274
            )
275
        );
276
277
        // This call will fail with an "UnauthorizedException"
278
        $roleService->updatePolicy($policy, $policyUpdate);
0 ignored issues
show
Security Bug introduced by
It seems like $policy defined by reset($policies) on line 262 can also be of type false; however, eZ\Publish\API\Repositor...Service::updatePolicy() does only seem to accept object<eZ\Publish\API\Re...ory\Values\User\Policy>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...Service::updatePolicy() has been deprecated with message: since 6.0, use {@link updatePolicyByRoleDraft()} instead.

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

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

Loading history...
279
        /* END: Use Case */
280
    }
281
282
    /**
283
     * Test for the removePolicy() method.
284
     *
285
     * @see \eZ\Publish\API\Repository\RoleService::removePolicy()
286
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
287
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testRemovePolicyByRoleDraft
288
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
289
     */
290 View Code Duplication
    public function testRemovePolicyThrowsUnauthorizedException()
291
    {
292
        $repository = $this->getRepository();
293
        $roleService = $repository->getRoleService();
294
295
        /* BEGIN: Use Case */
296
        $user = $this->createUserVersion1();
297
298
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
299
300
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
301
        // $roleCreate->mainLanguageCode = 'eng-US';
302
303
        // Create a new role with two policies
304
        $roleDraft = $roleService->createRole($roleCreate);
305
        $roleService->addPolicyByRoleDraft(
306
            $roleDraft,
307
            $roleService->newPolicyCreateStruct('content', 'create')
308
        );
309
        $roleDraft = $roleService->addPolicyByRoleDraft(
310
            $roleDraft,
311
            $roleService->newPolicyCreateStruct('content', 'delete')
312
        );
313
314
        // Set "Editor" user as current user.
315
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
316
317
        // This call will fail with an "UnauthorizedException"
318
        $roleService->removePolicyByRoleDraft($roleDraft, $roleDraft->getPolicies()[0]);
0 ignored issues
show
Compatibility introduced by
$roleDraft->getPolicies()[0] 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...
319
        /* END: Use Case */
320
    }
321
322
    /**
323
     * Test for the deletePolicy() method.
324
     *
325
     * @see \eZ\Publish\API\Repository\RoleService::deletePolicy()
326
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
327
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testDeletePolicy
328
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
329
     */
330 View Code Duplication
    public function testDeletePolicyThrowsUnauthorizedException()
331
    {
332
        $repository = $this->getRepository();
333
        $roleService = $repository->getRoleService();
334
335
        /* BEGIN: Use Case */
336
        $user = $this->createUserVersion1();
337
338
        $role = $this->createRole();
339
340
        // Get first role policy
341
        $policies = $role->getPolicies();
342
        $policy = reset($policies);
343
344
        // Set "Editor" user as current user.
345
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
346
347
        // This call will fail with an "UnauthorizedException"
348
        $roleService->deletePolicy($policy);
0 ignored issues
show
Security Bug introduced by
It seems like $policy defined by reset($policies) on line 342 can also be of type false; however, eZ\Publish\API\Repositor...Service::deletePolicy() does only seem to accept object<eZ\Publish\API\Re...ory\Values\User\Policy>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...Service::deletePolicy() has been deprecated with message: since 6.0, use {@link removePolicyByRoleDraft()} instead.

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

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

Loading history...
349
        /* END: Use Case */
350
    }
351
352
    /**
353
     * Test for the assignRoleToUserGroup() method.
354
     *
355
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup()
356
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
357
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
358
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
359
     */
360
    public function testAssignRoleToUserGroupThrowsUnauthorizedException()
361
    {
362
        $repository = $this->getRepository();
363
        $userService = $repository->getUserService();
364
        $roleService = $repository->getRoleService();
365
366
        $editorsGroupId = $this->generateId('group', 13);
367
368
        /* BEGIN: Use Case */
369
        $user = $this->createUserVersion1();
370
371
        $role = $this->createRole();
372
373
        // Load the "Editors" user group
374
        $userGroup = $userService->loadUserGroup($editorsGroupId);
375
376
        // Set "Editor" user as current user.
377
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
378
379
        // This call will fail with an "UnauthorizedException"
380
        $roleService->assignRoleToUserGroup($role, $userGroup);
381
        /* END: Use Case */
382
    }
383
384
    /**
385
     * Test for the assignRoleToUserGroup() method.
386
     *
387
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
388
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
389
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
390
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
391
     */
392 View Code Duplication
    public function testAssignRoleToUserGroupThrowsUnauthorizedExceptionWithRoleLimitationParameter()
393
    {
394
        $repository = $this->getRepository();
395
        $userService = $repository->getUserService();
396
        $roleService = $repository->getRoleService();
397
398
        $editorsGroupId = $this->generateId('group', 13);
399
400
        /* BEGIN: Use Case */
401
        $user = $this->createUserVersion1();
402
403
        $role = $this->createRole();
404
405
        // Load the "Editors" user group
406
        $userGroup = $userService->loadUserGroup($editorsGroupId);
407
408
        // Set "Editor" user as current user.
409
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
410
411
        // Create a subtree role limitation
412
        $limitation = new SubtreeLimitation(
413
            [
414
                'limitationValues' => ['/1/2/'],
415
            ]
416
        );
417
418
        // This call will fail with an "UnauthorizedException"
419
        $roleService->assignRoleToUserGroup($role, $userGroup, $limitation);
420
        /* END: Use Case */
421
    }
422
423
    /**
424
     * Test for the unassignRoleFromUserGroup() method.
425
     *
426
     * @see \eZ\Publish\API\Repository\RoleService::unassignRoleFromUserGroup()
427
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
428
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUnassignRoleFromUserGroup
429
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
430
     */
431
    public function testUnassignRoleFromUserGroupThrowsUnauthorizedException()
432
    {
433
        $repository = $this->getRepository();
434
        $userService = $repository->getUserService();
435
        $roleService = $repository->getRoleService();
436
437
        $editorsGroupId = $this->generateId('group', 13);
438
439
        /* BEGIN: Use Case */
440
        $user = $this->createUserVersion1();
441
442
        $role = $this->createRole();
443
444
        // Load the "Editors" user group
445
        $userGroup = $userService->loadUserGroup($editorsGroupId);
446
447
        // Assign new role to "Editors" user group
448
        $roleService->assignRoleToUserGroup($role, $userGroup);
449
450
        // Set "Editor" user as current user.
451
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
452
453
        // This call will fail with an "UnauthorizedException"
454
        $roleService->unassignRoleFromUserGroup($role, $userGroup);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...signRoleFromUserGroup() has been deprecated with message: since 6.0, use {@see removeRoleAssignment} instead.

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

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

Loading history...
455
        /* END: Use Case */
456
    }
457
458
    /**
459
     * Test for the assignRoleToUser() method.
460
     *
461
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser()
462
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
463
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
464
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
465
     */
466
    public function testAssignRoleToUserThrowsUnauthorizedException()
467
    {
468
        $repository = $this->getRepository();
469
        $roleService = $repository->getRoleService();
470
471
        /* BEGIN: Use Case */
472
        $user = $this->createUserVersion1();
473
474
        $role = $this->createRole();
475
476
        // Set "Editor" user as current user.
477
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
478
479
        // This call will fail with an "UnauthorizedException"
480
        $roleService->assignRoleToUser($role, $user);
481
        /* END: Use Case */
482
    }
483
484
    /**
485
     * Test for the assignRoleToUser() method.
486
     *
487
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
488
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
489
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
490
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
491
     */
492
    public function testAssignRoleToUserThrowsUnauthorizedExceptionWithRoleLimitationParameter()
493
    {
494
        $repository = $this->getRepository();
495
        $roleService = $repository->getRoleService();
496
497
        /* BEGIN: Use Case */
498
        $user = $this->createUserVersion1();
499
500
        $role = $this->createRole();
501
502
        // Set "Editor" user as current user.
503
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
504
505
        // Create a subtree role limitation
506
        $limitation = new SubtreeLimitation(
507
            [
508
                'limitationValues' => ['/1/2/'],
509
            ]
510
        );
511
512
        // This call will fail with an "UnauthorizedException"
513
        $roleService->assignRoleToUser($role, $user, $limitation);
514
        /* END: Use Case */
515
    }
516
517
    /**
518
     * Test for the unassignRoleFromUser() method.
519
     *
520
     * @see \eZ\Publish\API\Repository\RoleService::unassignRoleFromUser()
521
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
522
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUnassignRoleFromUser
523
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
524
     */
525 View Code Duplication
    public function testUnassignRoleFromUserThrowsUnauthorizedException()
526
    {
527
        $repository = $this->getRepository();
528
        $roleService = $repository->getRoleService();
529
530
        /* BEGIN: Use Case */
531
        $user = $this->createUserVersion1();
532
533
        $role = $this->createRole();
534
535
        // Assign new role to "Editor" user
536
        $roleService->assignRoleToUser($role, $user);
537
538
        // Set "Editor" user as current user.
539
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
540
541
        // This call will fail with an "UnauthorizedException"
542
        $roleService->unassignRoleFromUser($role, $user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...:unassignRoleFromUser() has been deprecated with message: since 6.0, use {@see removeRoleAssignment} instead.

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

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

Loading history...
543
        /* END: Use Case */
544
    }
545
546
    /**
547
     * Test for the getRoleAssignments() method.
548
     *
549
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignments()
550
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
551
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments
552
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
553
     */
554
    public function testGetRoleAssignmentsThrowsUnauthorizedException()
555
    {
556
        $repository = $this->getRepository();
557
        $roleService = $repository->getRoleService();
558
559
        /* BEGIN: Use Case */
560
        $user = $this->createUserVersion1();
561
562
        $role = $this->createRole();
563
564
        // Set "Editor" user as current user.
565
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
566
567
        // This call will fail with an "UnauthorizedException"
568
        $roleService->getRoleAssignments($role);
569
        /* END: Use Case */
570
    }
571
572
    /**
573
     * Test for the getRoleAssignmentsForUser() method.
574
     *
575
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
576
     */
577
    public function testGetRoleAssignmentsForUserLoadsEmptyListForAnonymousUser()
578
    {
579
        $repository = $this->getRepository();
580
        $roleService = $repository->getRoleService();
581
582
        /* BEGIN: Use Case */
583
        $user = $this->createUserVersion1();
584
585
        $this->createRole();
586
587
        // Set "Editor" user as current user.
588
        $repository->getPermissionResolver()->setCurrentUserReference($user);
589
        /* END: Use Case */
590
591
        $this->assertSame([], $roleService->getRoleAssignmentsForUser($user));
592
    }
593
594
    /**
595
     * Test for the getRoleAssignmentsForUser() method.
596
     *
597
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
598
     */
599
    public function testGetRoleAssignmentsForUserWithSubtreeLimitation()
600
    {
601
        $repository = $this->getRepository();
602
        $roleService = $repository->getRoleService();
603
604
        /* BEGIN: Use Case */
605
        $user = $this->createUserWithPolicies(
606
            'trash_test_user',
607
            [
608
                ['module' => 'role', 'function' => 'read'],
609
            ],
610
            new SubtreeLimitation(['limitationValues' => ['/1/2/']])
611
        );
612
613
        $repository->getPermissionResolver()->setCurrentUserReference($user);
614
        /* END: Use Case */
615
616
        $roleAssignments = $roleService->getRoleAssignmentsForUser($user);
617
        $this->assertCount(1, $roleAssignments);
618
619
        $roleAssignment = $roleAssignments[0];
620
        $this->assertSame($user, $roleAssignment->user);
621
    }
622
623
    /**
624
     * Test for the getRoleAssignmentsForUserGroup() method.
625
     *
626
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUserGroup()
627
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
628
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignmentsForUserGroup
629
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
630
     */
631 View Code Duplication
    public function testGetRoleAssignmentsForUserGroupThrowsUnauthorizedException()
632
    {
633
        $repository = $this->getRepository();
634
        $roleService = $repository->getRoleService();
635
        $userService = $repository->getUserService();
636
637
        $editorsGroupId = $this->generateId('group', 13);
638
639
        /* BEGIN: Use Case */
640
        $user = $this->createUserVersion1();
641
642
        $this->createRole();
643
644
        // Load the "Editors" user group
645
        $userGroup = $userService->loadUserGroup($editorsGroupId);
646
647
        // Set "Editor" user as current user.
648
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
649
650
        // This call will fail with an "UnauthorizedException"
651
        $roleService->getRoleAssignmentsForUserGroup($userGroup);
652
        /* END: Use Case */
653
    }
654
655
    /**
656
     * Create a role fixture in a variable named <b>$role</b>,.
657
     *
658
     * @return \eZ\Publish\API\Repository\Values\User\Role
659
     */
660 View Code Duplication
    private function createRole()
661
    {
662
        $repository = $this->getRepository();
663
664
        /* BEGIN: Inline */
665
        // Get the role service
666
        $roleService = $repository->getRoleService();
667
668
        // Get new policy create struct
669
        $policyCreate = $roleService->newPolicyCreateStruct('content', '*');
670
671
        // Get a role create struct instance and set properties
672
        $roleCreate = $roleService->newRoleCreateStruct('testRole');
673
674
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
675
        // $roleCreate->mainLanguageCode = 'eng-GB';
676
677
        $roleCreate->addPolicy($policyCreate);
678
679
        // Create a new role instance.
680
        $roleDraft = $roleService->createRole($roleCreate);
681
        $roleService->publishRoleDraft($roleDraft);
682
        $role = $roleService->loadRole($roleDraft->id);
683
        /* END: Inline */
684
685
        return $role;
686
    }
687
}
688