Code Duplication    Length = 53-59 lines in 3 locations

eZ/Publish/API/Repository/Tests/Values/User/Limitation/ContentTypeLimitationTest.php 1 location

@@ 151-203 (lines=53) @@
148
     * @throws \ErrorException
149
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
150
     */
151
    public function testContentTypeLimitationForbidVariant()
152
    {
153
        $repository = $this->getRepository();
154
155
        $contentService = $repository->getContentService();
156
157
        $contentTypeId = $this->generateId('contentType', 33);
158
        /* BEGIN: Use Case */
159
        $user = $this->createUserVersion1();
160
161
        $roleService = $repository->getRoleService();
162
163
        $role = $roleService->loadRoleByIdentifier('Editor');
164
165
        $editPolicy = null;
166
        foreach ($role->getPolicies() as $policy) {
167
            if ('content' != $policy->module || 'edit' != $policy->function) {
168
                continue;
169
            }
170
            $editPolicy = $policy;
171
            break;
172
        }
173
174
        if (null === $editPolicy) {
175
            throw new \ErrorException('No content:edit policy found.');
176
        }
177
178
        $policyUpdate = $roleService->newPolicyUpdateStruct();
179
        $policyUpdate->addLimitation(
180
            new ContentTypeLimitation(
181
                array('limitationValues' => array($contentTypeId))
182
            )
183
        );
184
185
        $roleService->updatePolicy($editPolicy, $policyUpdate);
186
        $roleService->assignRoleToUser($roleService->loadRole($role->id), $user);
187
188
        $content = $this->createWikiPage();
189
190
        $updateDraft = $contentService->createContentDraft($content->contentInfo);
191
192
        $repository->setCurrentUser($user);
193
194
        $contentUpdate = $contentService->newContentUpdateStruct();
195
        $contentUpdate->setField('title', 'Your wiki page');
196
197
        // This call fails with an UnauthorizedException
198
        $contentService->updateContent(
199
            $updateDraft->versionInfo,
200
            $contentUpdate
201
        );
202
        /* END: Use Case */
203
    }
204
}
205

eZ/Publish/API/Repository/Tests/Values/User/Limitation/OwnerLimitationTest.php 1 location

@@ 31-89 (lines=59) @@
28
     *
29
     * @throws \ErrorException
30
     */
31
    public function testOwnerLimitationAllow()
32
    {
33
        $repository = $this->getRepository();
34
35
        $contentService = $repository->getContentService();
36
37
        /* BEGIN: Use Case */
38
        $user = $this->createUserVersion1();
39
40
        $roleService = $repository->getRoleService();
41
42
        $role = $roleService->loadRoleByIdentifier('Editor');
43
44
        $removePolicy = null;
45
        foreach ($role->getPolicies() as $policy) {
46
            if ('content' != $policy->module || 'remove' != $policy->function) {
47
                continue;
48
            }
49
            $removePolicy = $policy;
50
            break;
51
        }
52
53
        if (null === $removePolicy) {
54
            throw new \ErrorException('No content:remove policy found.');
55
        }
56
57
        // Only allow remove for the user's own content
58
        $policyUpdate = $roleService->newPolicyUpdateStruct();
59
        $policyUpdate->addLimitation(
60
            new OwnerLimitation(
61
                array('limitationValues' => array(1))
62
            )
63
        );
64
        $roleService->updatePolicy($removePolicy, $policyUpdate);
65
66
        $roleService->assignRoleToUser($role, $user);
67
68
        $content = $this->createWikiPage();
69
70
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
71
        $metadataUpdate->ownerId = $user->id;
72
73
        $contentService->updateContentMetadata(
74
            $content->contentInfo,
75
            $metadataUpdate
76
        );
77
78
        $repository->setCurrentUser($user);
79
80
        $contentService->deleteContent(
81
            $contentService->loadContentInfo($content->id)
82
        );
83
        /* END: Use Case */
84
85
        $this->setExpectedException(
86
            '\\eZ\\Publish\\API\\Repository\\Exceptions\\NotFoundException'
87
        );
88
        $contentService->loadContent($content->id);
89
    }
90
91
    /**
92
     * Test for the OwnerLimitation.

eZ/Publish/API/Repository/Tests/Values/User/Limitation/ObjectStateLimitationTest.php 1 location

@@ 35-89 (lines=55) @@
32
     *
33
     * @throws \ErrorException
34
     */
35
    public function testObjectStateLimitationAllow()
36
    {
37
        $repository = $this->getRepository();
38
        $notLockedState = $this->generateId('objectstate', 2);
39
40
        $contentService = $repository->getContentService();
41
        /* BEGIN: Use Case */
42
        $user = $this->createUserVersion1();
43
44
        $roleService = $repository->getRoleService();
45
46
        $role = $roleService->loadRoleByIdentifier('Editor');
47
48
        $removePolicy = null;
49
        foreach ($role->getPolicies() as $policy) {
50
            if ('content' != $policy->module || 'remove' != $policy->function) {
51
                continue;
52
            }
53
            $removePolicy = $policy;
54
            break;
55
        }
56
57
        if (null === $removePolicy) {
58
            throw new \ErrorException('No content:remove policy found.');
59
        }
60
61
        // Only allow deletion of content with default state
62
        $policyUpdate = $roleService->newPolicyUpdateStruct();
63
        $policyUpdate->addLimitation(
64
            new ObjectStateLimitation(
65
                array(
66
                    'limitationValues' => array(
67
                        $notLockedState,
68
                    ),
69
                )
70
            )
71
        );
72
        $roleService->updatePolicy($removePolicy, $policyUpdate);
73
74
        // Allow user to create everything
75
        $policyCreate = $roleService->newPolicyCreateStruct('content', 'create');
76
        $roleService->addPolicy($role, $policyCreate);
77
78
        $roleService->assignRoleToUser($role, $user);
79
80
        $repository->setCurrentUser($user);
81
82
        $draft = $this->createWikiPageDraft();
83
84
        $contentService->deleteContent($draft->contentInfo);
85
        /* END: Use Case */
86
87
        $this->setExpectedException('\\eZ\\Publish\\API\\Repository\\Exceptions\\NotFoundException');
88
        $contentService->loadContent($draft->id);
89
    }
90
91
    /**
92
     * Tests a ObjectStateLimitation.