Completed
Push — migrate-files-no-interaction ( 025687...608925 )
by
unknown
46:43 queued 18:48
created

testRemovePolicyThrowsUnauthorizedException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 21
rs 9.3142
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
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
108
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoles
109
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
110
     */
111
    public function testLoadRolesThrowsUnauthorizedException()
112
    {
113
        $repository = $this->getRepository();
114
115
        /* BEGIN: Use Case */
116
        $user = $this->createUserVersion1();
117
118
        // Set "Editor" user as current user.
119
        $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...
120
121
        // Get the role service
122
        $roleService = $repository->getRoleService();
123
124
        // This call will fail with an "UnauthorizedException"
125
        $roleService->loadRoles();
126
        /* END: Use Case */
127
    }
128
129
    /**
130
     * Test for the updateRole() method.
131
     *
132
     * @see \eZ\Publish\API\Repository\RoleService::updateRole()
133
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
134
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdateRole
135
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
136
     */
137
    public function testUpdateRoleThrowsUnauthorizedException()
138
    {
139
        $repository = $this->getRepository();
140
        $roleService = $repository->getRoleService();
141
142
        /* BEGIN: Use Case */
143
        $user = $this->createUserVersion1();
144
145
        $role = $this->createRole();
146
147
        // Set "Editor" user as current user.
148
        $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...
149
150
        // Get a new role update struct and set new values
151
        $roleUpdateStruct = $roleService->newRoleUpdateStruct();
152
153
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
154
        // $roleUpdateStruct->mainLanguageCode = 'eng-US';
155
156
        // This call will fail with an "UnauthorizedException"
157
        $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...
158
        /* END: Use Case */
159
    }
160
161
    /**
162
     * Test for the deleteRole() method.
163
     *
164
     * @see \eZ\Publish\API\Repository\RoleService::deleteRole()
165
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
166
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testDeleteRole
167
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
168
     */
169
    public function testDeleteRoleThrowsUnauthorizedException()
170
    {
171
        $repository = $this->getRepository();
172
        $roleService = $repository->getRoleService();
173
174
        /* BEGIN: Use Case */
175
        $user = $this->createUserVersion1();
176
177
        $role = $this->createRole();
178
179
        // Set "Editor" user as current user.
180
        $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...
181
182
        // This call will fail with an "UnauthorizedException"
183
        $roleService->deleteRole($role);
184
        /* END: Use Case */
185
    }
186
187
    /**
188
     * Test for the addPolicy() method.
189
     *
190
     * @see \eZ\Publish\API\Repository\RoleService::addPolicy()
191
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
192
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicy
193
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
194
     */
195
    public function testAddPolicyThrowsUnauthorizedException()
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->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...
210
            $role,
211
            $roleService->newPolicyCreateStruct('content', 'delete')
212
        );
213
        /* END: Use Case */
214
    }
215
216
    /**
217
     * Test for the updatePolicy() method.
218
     *
219
     * @see \eZ\Publish\API\Repository\RoleService::updatePolicy()
220
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
221
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdatePolicy
222
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
223
     */
224 View Code Duplication
    public function testUpdatePolicyThrowsUnauthorizedException()
225
    {
226
        $repository = $this->getRepository();
227
        $roleService = $repository->getRoleService();
228
229
        /* BEGIN: Use Case */
230
        $user = $this->createUserVersion1();
231
232
        $role = $this->createRole();
233
234
        // Get first role policy
235
        $policies = $role->getPolicies();
236
        $policy = reset($policies);
237
238
        // Set "Editor" user as current user.
239
        $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...
240
241
        // Get a policy update struct and add a limitation
242
        $policyUpdate = $roleService->newPolicyUpdateStruct();
243
        $policyUpdate->addLimitation(
244
            new SubtreeLimitation(
245
                array(
246
                    'limitationValues' => array('/1/'),
247
                )
248
            )
249
        );
250
251
        // This call will fail with an "UnauthorizedException"
252
        $roleService->updatePolicy($policy, $policyUpdate);
0 ignored issues
show
Security Bug introduced by
It seems like $policy defined by reset($policies) on line 236 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...
253
        /* END: Use Case */
254
    }
255
256
    /**
257
     * Test for the removePolicy() method.
258
     *
259
     * @see \eZ\Publish\API\Repository\RoleService::removePolicy()
260
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
261
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testRemovePolicy
262
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
263
     */
264
    public function testRemovePolicyThrowsUnauthorizedException()
265
    {
266
        $repository = $this->getRepository();
267
        $roleService = $repository->getRoleService();
268
269
        /* BEGIN: Use Case */
270
        $user = $this->createUserVersion1();
271
272
        $role = $this->createRole();
273
274
        // Get first role policy
275
        $policies = $role->getPolicies();
276
        $policy = reset($policies);
277
278
        // Set "Editor" user as current user.
279
        $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...
280
281
        // This call will fail with an "UnauthorizedException"
282
        $roleService->removePolicy($role, $policy);
0 ignored issues
show
Security Bug introduced by
It seems like $policy defined by reset($policies) on line 276 can also be of type false; however, eZ\Publish\API\Repositor...Service::removePolicy() 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::removePolicy() has been deprecated with message: since 5.3, 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...
283
        /* END: Use Case */
284
    }
285
286
    /**
287
     * Test for the deletePolicy() method.
288
     *
289
     * @see \eZ\Publish\API\Repository\RoleService::deletePolicy()
290
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
291
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testDeletePolicy
292
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
293
     */
294
    public function testDeletePolicyThrowsUnauthorizedException()
295
    {
296
        $repository = $this->getRepository();
297
        $roleService = $repository->getRoleService();
298
299
        /* BEGIN: Use Case */
300
        $user = $this->createUserVersion1();
301
302
        $role = $this->createRole();
303
304
        // Get first role policy
305
        $policies = $role->getPolicies();
306
        $policy = reset($policies);
307
308
        // Set "Editor" user as current user.
309
        $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...
310
311
        // This call will fail with an "UnauthorizedException"
312
        $roleService->deletePolicy($policy);
0 ignored issues
show
Security Bug introduced by
It seems like $policy defined by reset($policies) on line 306 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...
313
        /* END: Use Case */
314
    }
315
316
    /**
317
     * Test for the assignRoleToUserGroup() method.
318
     *
319
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup()
320
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
321
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
322
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
323
     */
324
    public function testAssignRoleToUserGroupThrowsUnauthorizedException()
325
    {
326
        $repository = $this->getRepository();
327
        $userService = $repository->getUserService();
328
        $roleService = $repository->getRoleService();
329
330
        $editorsGroupId = $this->generateId('group', 13);
331
332
        /* BEGIN: Use Case */
333
        $user = $this->createUserVersion1();
334
335
        $role = $this->createRole();
336
337
        // Load the "Editors" user group
338
        $userGroup = $userService->loadUserGroup($editorsGroupId);
339
340
        // Set "Editor" user as current user.
341
        $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...
342
343
        // This call will fail with an "UnauthorizedException"
344
        $roleService->assignRoleToUserGroup($role, $userGroup);
345
        /* END: Use Case */
346
    }
347
348
    /**
349
     * Test for the assignRoleToUserGroup() method.
350
     *
351
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
352
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
353
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
354
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
355
     */
356 View Code Duplication
    public function testAssignRoleToUserGroupThrowsUnauthorizedExceptionWithRoleLimitationParameter()
357
    {
358
        $repository = $this->getRepository();
359
        $userService = $repository->getUserService();
360
        $roleService = $repository->getRoleService();
361
362
        $editorsGroupId = $this->generateId('group', 13);
363
364
        /* BEGIN: Use Case */
365
        $user = $this->createUserVersion1();
366
367
        $role = $this->createRole();
368
369
        // Load the "Editors" user group
370
        $userGroup = $userService->loadUserGroup($editorsGroupId);
371
372
        // Set "Editor" user as current user.
373
        $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...
374
375
        // Create a subtree role limitation
376
        $limitation = new SubtreeLimitation(
377
            array(
378
                'limitationValues' => array('/1/2/'),
379
            )
380
        );
381
382
        // This call will fail with an "UnauthorizedException"
383
        $roleService->assignRoleToUserGroup($role, $userGroup, $limitation);
384
        /* END: Use Case */
385
    }
386
387
    /**
388
     * Test for the unassignRoleFromUserGroup() method.
389
     *
390
     * @see \eZ\Publish\API\Repository\RoleService::unassignRoleFromUserGroup()
391
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
392
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUnassignRoleFromUserGroup
393
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
394
     */
395
    public function testUnassignRoleFromUserGroupThrowsUnauthorizedException()
396
    {
397
        $repository = $this->getRepository();
398
        $userService = $repository->getUserService();
399
        $roleService = $repository->getRoleService();
400
401
        $editorsGroupId = $this->generateId('group', 13);
402
403
        /* BEGIN: Use Case */
404
        $user = $this->createUserVersion1();
405
406
        $role = $this->createRole();
407
408
        // Load the "Editors" user group
409
        $userGroup = $userService->loadUserGroup($editorsGroupId);
410
411
        // Assign new role to "Editors" user group
412
        $roleService->assignRoleToUserGroup($role, $userGroup);
413
414
        // Set "Editor" user as current user.
415
        $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...
416
417
        // This call will fail with an "UnauthorizedException"
418
        $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...
419
        /* END: Use Case */
420
    }
421
422
    /**
423
     * Test for the assignRoleToUser() method.
424
     *
425
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser()
426
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
427
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
428
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
429
     */
430
    public function testAssignRoleToUserThrowsUnauthorizedException()
431
    {
432
        $repository = $this->getRepository();
433
        $roleService = $repository->getRoleService();
434
435
        /* BEGIN: Use Case */
436
        $user = $this->createUserVersion1();
437
438
        $role = $this->createRole();
439
440
        // Set "Editor" user as current user.
441
        $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...
442
443
        // This call will fail with an "UnauthorizedException"
444
        $roleService->assignRoleToUser($role, $user);
445
        /* END: Use Case */
446
    }
447
448
    /**
449
     * Test for the assignRoleToUser() method.
450
     *
451
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
452
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
453
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
454
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
455
     */
456
    public function testAssignRoleToUserThrowsUnauthorizedExceptionWithRoleLimitationParameter()
457
    {
458
        $repository = $this->getRepository();
459
        $roleService = $repository->getRoleService();
460
461
        /* BEGIN: Use Case */
462
        $user = $this->createUserVersion1();
463
464
        $role = $this->createRole();
465
466
        // Set "Editor" user as current user.
467
        $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...
468
469
        // Create a subtree role limitation
470
        $limitation = new SubtreeLimitation(
471
            array(
472
                'limitationValues' => array('/1/2/'),
473
            )
474
        );
475
476
        // This call will fail with an "UnauthorizedException"
477
        $roleService->assignRoleToUser($role, $user, $limitation);
478
        /* END: Use Case */
479
    }
480
481
    /**
482
     * Test for the unassignRoleFromUser() method.
483
     *
484
     * @see \eZ\Publish\API\Repository\RoleService::unassignRoleFromUser()
485
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
486
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUnassignRoleFromUser
487
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
488
     */
489
    public function testUnassignRoleFromUserThrowsUnauthorizedException()
490
    {
491
        $repository = $this->getRepository();
492
        $roleService = $repository->getRoleService();
493
494
        /* BEGIN: Use Case */
495
        $user = $this->createUserVersion1();
496
497
        $role = $this->createRole();
498
499
        // Assign new role to "Editor" user
500
        $roleService->assignRoleToUser($role, $user);
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
        // This call will fail with an "UnauthorizedException"
506
        $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...
507
        /* END: Use Case */
508
    }
509
510
    /**
511
     * Test for the getRoleAssignments() method.
512
     *
513
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignments()
514
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
515
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments
516
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
517
     */
518
    public function testGetRoleAssignmentsThrowsUnauthorizedException()
519
    {
520
        $repository = $this->getRepository();
521
        $roleService = $repository->getRoleService();
522
523
        /* BEGIN: Use Case */
524
        $user = $this->createUserVersion1();
525
526
        $role = $this->createRole();
527
528
        // Set "Editor" user as current user.
529
        $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...
530
531
        // This call will fail with an "UnauthorizedException"
532
        $roleService->getRoleAssignments($role);
533
        /* END: Use Case */
534
    }
535
536
    /**
537
     * Test for the getRoleAssignmentsForUser() method.
538
     *
539
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
540
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
541
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignmentsForUser
542
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
543
     */
544
    public function testGetRoleAssignmentsForUserThrowsUnauthorizedException()
545
    {
546
        $repository = $this->getRepository();
547
        $roleService = $repository->getRoleService();
548
549
        /* BEGIN: Use Case */
550
        $user = $this->createUserVersion1();
551
552
        $this->createRole();
553
554
        // Set "Editor" user as current user.
555
        $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...
556
557
        // This call will fail with an "UnauthorizedException"
558
        $roleService->getRoleAssignmentsForUser($user);
559
        /* END: Use Case */
560
    }
561
562
    /**
563
     * Test for the getRoleAssignmentsForUserGroup() method.
564
     *
565
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUserGroup()
566
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
567
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignmentsForUserGroup
568
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
569
     */
570 View Code Duplication
    public function testGetRoleAssignmentsForUserGroupThrowsUnauthorizedException()
571
    {
572
        $repository = $this->getRepository();
573
        $roleService = $repository->getRoleService();
574
        $userService = $repository->getUserService();
575
576
        $editorsGroupId = $this->generateId('group', 13);
577
578
        /* BEGIN: Use Case */
579
        $user = $this->createUserVersion1();
580
581
        $this->createRole();
582
583
        // Load the "Editors" user group
584
        $userGroup = $userService->loadUserGroup($editorsGroupId);
585
586
        // Set "Editor" user as current user.
587
        $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...
588
589
        // This call will fail with an "UnauthorizedException"
590
        $roleService->getRoleAssignmentsForUserGroup($userGroup);
591
        /* END: Use Case */
592
    }
593
594
    /**
595
     * Create a role fixture in a variable named <b>$role</b>,.
596
     *
597
     * @return \eZ\Publish\API\Repository\Values\User\Role
598
     */
599
    private function createRole()
600
    {
601
        $repository = $this->getRepository();
602
603
        /* BEGIN: Inline */
604
        // Get the role service
605
        $roleService = $repository->getRoleService();
606
607
        // Get new policy create struct
608
        $policyCreate = $roleService->newPolicyCreateStruct('content', '*');
609
610
        // Get a role create struct instance and set properties
611
        $roleCreate = $roleService->newRoleCreateStruct('testRole');
612
613
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
614
        // $roleCreate->mainLanguageCode = 'eng-GB';
615
616
        $roleCreate->addPolicy($policyCreate);
617
618
        // Create a new role instance.
619
        $roleDraft = $roleService->createRole($roleCreate);
620
        $roleService->publishRoleDraft($roleDraft);
621
        $role = $roleService->loadRole($roleDraft->id);
622
        /* END: Inline */
623
624
        return $role;
625
    }
626
}
627