Completed
Push — 7.0_deprecation_removal ( e8d685 )
by André
23:55
created

testGetRoleAssignmentsThrowsUnauthorizedException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 17
rs 9.4285
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 View Code Duplication
    public function testRemovePolicyThrowsUnauthorizedException()
265
    {
266
        $repository = $this->getRepository();
267
        $roleService = $repository->getRoleService();
268
269
        /* BEGIN: Use Case */
270
        $user = $this->createUserVersion1();
271
272
        $roleCreate = $roleService->newRoleCreateStruct('newRole');
273
274
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
275
        // $roleCreate->mainLanguageCode = 'eng-US';
276
277
        // Create a new role with two policies
278
        $roleDraft = $roleService->createRole($roleCreate);
279
        $roleService->addPolicyByRoleDraft(
280
            $roleDraft,
281
            $roleService->newPolicyCreateStruct('content', 'create')
282
        );
283
        $roleDraft = $roleService->addPolicyByRoleDraft(
284
            $roleDraft,
285
            $roleService->newPolicyCreateStruct('content', 'delete')
286
        );
287
288
        // Set "Editor" user as current user.
289
        $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...
290
291
        // This call will fail with an "UnauthorizedException"
292
        $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...
293
        /* END: Use Case */
294
    }
295
296
    /**
297
     * Test for the deletePolicy() method.
298
     *
299
     * @see \eZ\Publish\API\Repository\RoleService::deletePolicy()
300
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
301
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testDeletePolicy
302
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
303
     */
304
    public function testDeletePolicyThrowsUnauthorizedException()
305
    {
306
        $repository = $this->getRepository();
307
        $roleService = $repository->getRoleService();
308
309
        /* BEGIN: Use Case */
310
        $user = $this->createUserVersion1();
311
312
        $role = $this->createRole();
313
314
        // Get first role policy
315
        $policies = $role->getPolicies();
316
        $policy = reset($policies);
317
318
        // Set "Editor" user as current user.
319
        $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...
320
321
        // This call will fail with an "UnauthorizedException"
322
        $roleService->deletePolicy($policy);
0 ignored issues
show
Security Bug introduced by
It seems like $policy defined by reset($policies) on line 316 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...
323
        /* END: Use Case */
324
    }
325
326
    /**
327
     * Test for the assignRoleToUserGroup() method.
328
     *
329
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup()
330
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
331
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
332
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
333
     */
334
    public function testAssignRoleToUserGroupThrowsUnauthorizedException()
335
    {
336
        $repository = $this->getRepository();
337
        $userService = $repository->getUserService();
338
        $roleService = $repository->getRoleService();
339
340
        $editorsGroupId = $this->generateId('group', 13);
341
342
        /* BEGIN: Use Case */
343
        $user = $this->createUserVersion1();
344
345
        $role = $this->createRole();
346
347
        // Load the "Editors" user group
348
        $userGroup = $userService->loadUserGroup($editorsGroupId);
349
350
        // Set "Editor" user as current user.
351
        $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...
352
353
        // This call will fail with an "UnauthorizedException"
354
        $roleService->assignRoleToUserGroup($role, $userGroup);
355
        /* END: Use Case */
356
    }
357
358
    /**
359
     * Test for the assignRoleToUserGroup() method.
360
     *
361
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation)
362
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
363
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup
364
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
365
     */
366 View Code Duplication
    public function testAssignRoleToUserGroupThrowsUnauthorizedExceptionWithRoleLimitationParameter()
367
    {
368
        $repository = $this->getRepository();
369
        $userService = $repository->getUserService();
370
        $roleService = $repository->getRoleService();
371
372
        $editorsGroupId = $this->generateId('group', 13);
373
374
        /* BEGIN: Use Case */
375
        $user = $this->createUserVersion1();
376
377
        $role = $this->createRole();
378
379
        // Load the "Editors" user group
380
        $userGroup = $userService->loadUserGroup($editorsGroupId);
381
382
        // Set "Editor" user as current user.
383
        $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...
384
385
        // Create a subtree role limitation
386
        $limitation = new SubtreeLimitation(
387
            array(
388
                'limitationValues' => array('/1/2/'),
389
            )
390
        );
391
392
        // This call will fail with an "UnauthorizedException"
393
        $roleService->assignRoleToUserGroup($role, $userGroup, $limitation);
394
        /* END: Use Case */
395
    }
396
397
    /**
398
     * Test for the unassignRoleFromUserGroup() method.
399
     *
400
     * @see \eZ\Publish\API\Repository\RoleService::unassignRoleFromUserGroup()
401
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
402
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUnassignRoleFromUserGroup
403
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
404
     */
405
    public function testUnassignRoleFromUserGroupThrowsUnauthorizedException()
406
    {
407
        $repository = $this->getRepository();
408
        $userService = $repository->getUserService();
409
        $roleService = $repository->getRoleService();
410
411
        $editorsGroupId = $this->generateId('group', 13);
412
413
        /* BEGIN: Use Case */
414
        $user = $this->createUserVersion1();
415
416
        $role = $this->createRole();
417
418
        // Load the "Editors" user group
419
        $userGroup = $userService->loadUserGroup($editorsGroupId);
420
421
        // Assign new role to "Editors" user group
422
        $roleService->assignRoleToUserGroup($role, $userGroup);
423
424
        // Set "Editor" user as current user.
425
        $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...
426
427
        // This call will fail with an "UnauthorizedException"
428
        $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...
429
        /* END: Use Case */
430
    }
431
432
    /**
433
     * Test for the assignRoleToUser() method.
434
     *
435
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser()
436
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
437
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser
438
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
439
     */
440
    public function testAssignRoleToUserThrowsUnauthorizedException()
441
    {
442
        $repository = $this->getRepository();
443
        $roleService = $repository->getRoleService();
444
445
        /* BEGIN: Use Case */
446
        $user = $this->createUserVersion1();
447
448
        $role = $this->createRole();
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->assignRoleToUser($role, $user);
455
        /* END: Use Case */
456
    }
457
458
    /**
459
     * Test for the assignRoleToUser() method.
460
     *
461
     * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation)
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 testAssignRoleToUserThrowsUnauthorizedExceptionWithRoleLimitationParameter()
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
        // Create a subtree role limitation
480
        $limitation = new SubtreeLimitation(
481
            array(
482
                'limitationValues' => array('/1/2/'),
483
            )
484
        );
485
486
        // This call will fail with an "UnauthorizedException"
487
        $roleService->assignRoleToUser($role, $user, $limitation);
488
        /* END: Use Case */
489
    }
490
491
    /**
492
     * Test for the unassignRoleFromUser() method.
493
     *
494
     * @see \eZ\Publish\API\Repository\RoleService::unassignRoleFromUser()
495
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
496
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUnassignRoleFromUser
497
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
498
     */
499
    public function testUnassignRoleFromUserThrowsUnauthorizedException()
500
    {
501
        $repository = $this->getRepository();
502
        $roleService = $repository->getRoleService();
503
504
        /* BEGIN: Use Case */
505
        $user = $this->createUserVersion1();
506
507
        $role = $this->createRole();
508
509
        // Assign new role to "Editor" user
510
        $roleService->assignRoleToUser($role, $user);
511
512
        // Set "Editor" user as current user.
513
        $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...
514
515
        // This call will fail with an "UnauthorizedException"
516
        $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...
517
        /* END: Use Case */
518
    }
519
520
    /**
521
     * Test for the getRoleAssignments() method.
522
     *
523
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignments()
524
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
525
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments
526
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
527
     */
528
    public function testGetRoleAssignmentsThrowsUnauthorizedException()
529
    {
530
        $repository = $this->getRepository();
531
        $roleService = $repository->getRoleService();
532
533
        /* BEGIN: Use Case */
534
        $user = $this->createUserVersion1();
535
536
        $role = $this->createRole();
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->getRoleAssignments($role);
543
        /* END: Use Case */
544
    }
545
546
    /**
547
     * Test for the getRoleAssignmentsForUser() method.
548
     *
549
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser()
550
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
551
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignmentsForUser
552
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
553
     */
554
    public function testGetRoleAssignmentsForUserThrowsUnauthorizedException()
555
    {
556
        $repository = $this->getRepository();
557
        $roleService = $repository->getRoleService();
558
559
        /* BEGIN: Use Case */
560
        $user = $this->createUserVersion1();
561
562
        $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->getRoleAssignmentsForUser($user);
569
        /* END: Use Case */
570
    }
571
572
    /**
573
     * Test for the getRoleAssignmentsForUserGroup() method.
574
     *
575
     * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUserGroup()
576
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
577
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignmentsForUserGroup
578
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
579
     */
580 View Code Duplication
    public function testGetRoleAssignmentsForUserGroupThrowsUnauthorizedException()
581
    {
582
        $repository = $this->getRepository();
583
        $roleService = $repository->getRoleService();
584
        $userService = $repository->getUserService();
585
586
        $editorsGroupId = $this->generateId('group', 13);
587
588
        /* BEGIN: Use Case */
589
        $user = $this->createUserVersion1();
590
591
        $this->createRole();
592
593
        // Load the "Editors" user group
594
        $userGroup = $userService->loadUserGroup($editorsGroupId);
595
596
        // Set "Editor" user as current user.
597
        $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...
598
599
        // This call will fail with an "UnauthorizedException"
600
        $roleService->getRoleAssignmentsForUserGroup($userGroup);
601
        /* END: Use Case */
602
    }
603
604
    /**
605
     * Create a role fixture in a variable named <b>$role</b>,.
606
     *
607
     * @return \eZ\Publish\API\Repository\Values\User\Role
608
     */
609
    private function createRole()
610
    {
611
        $repository = $this->getRepository();
612
613
        /* BEGIN: Inline */
614
        // Get the role service
615
        $roleService = $repository->getRoleService();
616
617
        // Get new policy create struct
618
        $policyCreate = $roleService->newPolicyCreateStruct('content', '*');
619
620
        // Get a role create struct instance and set properties
621
        $roleCreate = $roleService->newRoleCreateStruct('testRole');
622
623
        // @todo uncomment when support for multilingual names and descriptions is added EZP-24776
624
        // $roleCreate->mainLanguageCode = 'eng-GB';
625
626
        $roleCreate->addPolicy($policyCreate);
627
628
        // Create a new role instance.
629
        $roleDraft = $roleService->createRole($roleCreate);
630
        $roleService->publishRoleDraft($roleDraft);
631
        $role = $roleService->loadRole($roleDraft->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\User\Role. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
632
        /* END: Inline */
633
634
        return $role;
635
    }
636
}
637