Completed
Push — ezp-30928-as_a_developer_i_wan... ( 0570c5 )
by
unknown
13:48
created

testParentUserGroupLimitationForbid()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 55

Duplication

Lines 55
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 55
loc 55
rs 8.9818
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * File containing the ParentUserGroupLimitationTest 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\Values\User\Limitation;
10
11
use eZ\Publish\API\Repository\Values\User\Limitation\ParentUserGroupLimitation;
12
13
/**
14
 * Test case for the {@link \eZ\Publish\API\Repository\Values\User\Limitation\ParentUserGroupLimitation}
15
 * class.
16
 *
17
 * @see eZ\Publish\API\Repository\Values\User\Limitation
18
 * @see eZ\Publish\API\Repository\Values\User\Limitation\ParentUserGroupLimitation
19
 * @group integration
20
 * @group limitation
21
 */
22
class ParentUserGroupLimitationTest extends BaseLimitationTest
23
{
24
    /**
25
     * Tests a ParentUserGroupLimitation.
26
     *
27
     * @see eZ\Publish\API\Repository\Values\User\Limitation\ParentUserGroupLimitation
28
     */
29
    public function testParentUserGroupLimitationAllow()
30
    {
31
        $repository = $this->getRepository();
32
        $userService = $repository->getUserService();
33
        $permissionResolver = $repository->getPermissionResolver();
34
35
        $parentUserGroupId = $this->generateId('location', 4);
36
        /* BEGIN: Use Case */
37
        $user = $this->createUserVersion1();
38
        $currentUser = $userService->loadUser(
39
            $permissionResolver->getCurrentUserReference()->getUserId()
40
        );
41
42
        $userGroupCreate = $userService->newUserGroupCreateStruct('eng-GB');
43
        $userGroupCreate->setField('name', 'Shared wiki');
44
45
        $userGroup = $userService->createUserGroup(
46
            $userGroupCreate,
47
            $userService->loadUserGroup(
48
                $parentUserGroupId
49
            )
50
        );
51
52
        // Assign system user and example user to same group
53
        $userService->assignUserToUserGroup($user, $userGroup);
54
        $userService->assignUserToUserGroup($currentUser, $userGroup);
55
56
        $roleService = $repository->getRoleService();
57
58
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
59
        $policyCreate->addLimitation(
60
            new ParentUserGroupLimitation(
61
                [
62
                    'limitationValues' => [true],
63
                ]
64
            )
65
        );
66
        $role = $roleService->loadRoleByIdentifier('Editor');
67
        $roleDraft = $roleService->createRoleDraft($role);
68
        $roleService->addPolicyByRoleDraft(
69
            $roleDraft,
70
            $policyCreate
71
        );
72
        $roleService->addPolicyByRoleDraft(
73
            $roleDraft,
74
            $roleService->newPolicyCreateStruct('content', 'read')
75
        );
76
        $roleService->publishRoleDraft($roleDraft);
77
78
        $roleService->assignRoleToUserGroup($role, $userGroup);
79
80
        $permissionResolver->setCurrentUserReference($user);
81
82
        $draft = $this->createWikiPageDraft();
83
        /* END: Use Case */
84
85
        $this->assertEquals(
86
            'An awesome wiki page',
87
            $draft->getFieldValue('title')->text
88
        );
89
    }
90
91
    /**
92
     * Tests a ParentUserGroupLimitation.
93
     *
94
     * @see eZ\Publish\API\Repository\Values\User\Limitation\ParentUserGroupLimitation
95
     */
96 View Code Duplication
    public function testParentUserGroupLimitationForbid()
97
    {
98
        $this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class);
99
100
        $repository = $this->getRepository();
101
        $userService = $repository->getUserService();
102
        $permissionResolver = $repository->getPermissionResolver();
103
104
        $parentUserGroupId = $this->generateId('location', 4);
105
        /* BEGIN: Use Case */
106
        $user = $this->createUserVersion1();
107
108
        $userGroupCreate = $userService->newUserGroupCreateStruct('eng-GB');
109
        $userGroupCreate->setField('name', 'Shared wiki');
110
111
        $userGroup = $userService->createUserGroup(
112
            $userGroupCreate,
113
            $userService->loadUserGroup(
114
                $parentUserGroupId
115
            )
116
        );
117
118
        // Assign only example user to new group
119
        $userService->assignUserToUserGroup($user, $userGroup);
120
121
        $roleService = $repository->getRoleService();
122
123
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
124
        $policyCreate->addLimitation(
125
            new ParentUserGroupLimitation(
126
                [
127
                    'limitationValues' => [true],
128
                ]
129
            )
130
        );
131
132
        $role = $roleService->loadRoleByIdentifier('Editor');
133
        $roleDraft = $roleService->createRoleDraft($role);
134
        $roleService->addPolicyByRoleDraft(
135
            $roleDraft,
136
            $policyCreate
137
        );
138
        $roleService->addPolicyByRoleDraft(
139
            $roleDraft,
140
            $roleService->newPolicyCreateStruct('content', 'read')
141
        );
142
        $roleService->publishRoleDraft($roleDraft);
143
144
        $roleService->assignRoleToUserGroup($role, $userGroup);
145
146
        $permissionResolver->setCurrentUserReference($user);
147
148
        $this->createWikiPageDraft();
149
        /* END: Use Case */
150
    }
151
}
152