Completed
Push — repo_use_canUser ( 4c433f...4b32ed )
by André
22:24
created

testLoadSectionsThrowsUnauthorizedException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30

Duplication

Lines 30
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 30
loc 30
rs 9.44
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the SectionServiceAuthorizationTest 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
/**
12
 * Test case for operations in the SectionService using in memory storage.
13
 *
14
 * @see eZ\Publish\API\Repository\SectionService
15
 * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadAnonymousUser
16
 * @group integration
17
 * @group authorization
18
 */
19
class SectionServiceAuthorizationTest extends BaseTest
20
{
21
    /**
22
     * Test for the createSection() method.
23
     *
24
     * @see \eZ\Publish\API\Repository\SectionService::createSection()
25
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
26
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testCreateSection
27
     */
28
    public function testCreateSectionThrowsUnauthorizedException()
29
    {
30
        $repository = $this->getRepository();
31
32
        $anonymousUserId = $this->generateId('user', 10);
33
        /* BEGIN: Use Case */
34
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
35
        // Publish demo installation.
36
        $userService = $repository->getUserService();
37
        $sectionService = $repository->getSectionService();
38
39
        $sectionCreate = $sectionService->newSectionCreateStruct();
40
        $sectionCreate->name = 'Test Section';
41
        $sectionCreate->identifier = 'uniqueKey';
42
43
        // Set anonymous user
44
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
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...
45
46
        // This call will fail with a "UnauthorizedException"
47
        $sectionService->createSection($sectionCreate);
48
        /* END: Use Case */
49
    }
50
51
    /**
52
     * Test for the loadSection() method.
53
     *
54
     * @see \eZ\Publish\API\Repository\SectionService::loadSection()
55
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
56
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSection
57
     */
58 View Code Duplication
    public function testLoadSectionThrowsUnauthorizedException()
59
    {
60
        $repository = $this->getRepository();
61
62
        $anonymousUserId = $this->generateId('user', 10);
63
        /* BEGIN: Use Case */
64
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
65
        // Publish demo installation.
66
        $userService = $repository->getUserService();
67
        $sectionService = $repository->getSectionService();
68
69
        $sectionCreate = $sectionService->newSectionCreateStruct();
70
        $sectionCreate->name = 'Test Section';
71
        $sectionCreate->identifier = 'uniqueKey';
72
73
        $sectionId = $sectionService->createSection($sectionCreate)->id;
74
75
        // Set anonymous user
76
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
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...
77
78
        // This call will fail with a "UnauthorizedException"
79
        $sectionService->loadSection($sectionId);
80
        /* END: Use Case */
81
    }
82
83
    /**
84
     * Test for the updateSection() method.
85
     *
86
     * @see \eZ\Publish\API\Repository\SectionService::updateSection()
87
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
88
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testUpdateSection
89
     */
90 View Code Duplication
    public function testUpdateSectionThrowsUnauthorizedException()
91
    {
92
        $repository = $this->getRepository();
93
94
        $standardSectionId = $this->generateId('section', 1);
95
        $anonymousUserId = $this->generateId('user', 10);
96
        /* BEGIN: Use Case */
97
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
98
        // Publish demo installation.
99
        // $standardSectionId is the ID of the "Standard" section in a eZ
100
        // Publish demo installation.
101
102
        $userService = $repository->getUserService();
103
        $sectionService = $repository->getSectionService();
104
105
        $section = $sectionService->loadSection($standardSectionId);
106
107
        $sectionUpdate = $sectionService->newSectionUpdateStruct();
108
        $sectionUpdate->name = 'New section name';
109
        $sectionUpdate->identifier = 'newUniqueKey';
110
111
        // Set anonymous user
112
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
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...
113
114
        // This call will fail with a "UnauthorizedException"
115
        $sectionService->updateSection($section, $sectionUpdate);
116
        /* END: Use Case */
117
    }
118
119
    /**
120
     * Test for the loadSections() method.
121
     *
122
     * @see \eZ\Publish\API\Repository\SectionService::loadSections()
123
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
124
     * @depends eZ\Publish\API\Repository\Tests\SectionServiceTest::testLoadSections
125
     */
126 View Code Duplication
    public function testLoadSectionsThrowsUnauthorizedException()
127
    {
128
        $repository = $this->getRepository();
129
130
        $anonymousUserId = $this->generateId('user', 10);
131
        /* BEGIN: Use Case */
132
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
133
        // Publish demo installation.
134
        $userService = $repository->getUserService();
135
        $sectionService = $repository->getSectionService();
136
137
        // Create some sections
138
        $sectionCreateOne = $sectionService->newSectionCreateStruct();
139
        $sectionCreateOne->name = 'Test section one';
140
        $sectionCreateOne->identifier = 'uniqueKeyOne';
141
142
        $sectionCreateTwo = $sectionService->newSectionCreateStruct();
143
        $sectionCreateTwo->name = 'Test section two';
144
        $sectionCreateTwo->identifier = 'uniqueKeyTwo';
145
146
        $sectionService->createSection($sectionCreateOne);
147
        $sectionService->createSection($sectionCreateTwo);
148
149
        // Set anonymous user
150
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
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...
151
152
        // This call will fail with a "UnauthorizedException"
153
        $sectionService->loadSections();
154
        /* END: Use Case */
155
    }
156
157
    /**
158
     * Test for the loadSectionByIdentifier() method.
159
     *
160
     * @see \eZ\Publish\API\Repository\SectionService::loadSectionByIdentifier()
161
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
162
     */
163
    public function testLoadSectionByIdentifierThrowsUnauthorizedException()
164
    {
165
        $repository = $this->getRepository();
166
167
        $anonymousUserId = $this->generateId('user', 10);
168
        /* BEGIN: Use Case */
169
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
170
        // Publish demo installation.
171
        $userService = $repository->getUserService();
172
        $sectionService = $repository->getSectionService();
173
174
        $sectionCreate = $sectionService->newSectionCreateStruct();
175
        $sectionCreate->name = 'Test Section';
176
        $sectionCreate->identifier = 'uniqueKey';
177
178
        $sectionService->createSection($sectionCreate);
179
180
        // Set anonymous user
181
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
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...
182
183
        // This call will fail with a "UnauthorizedException"
184
        $sectionService->loadSectionByIdentifier('uniqueKey');
185
        /* END: Use Case */
186
    }
187
188
    /**
189
     * Test for the assignSection() method.
190
     *
191
     * @see \eZ\Publish\API\Repository\SectionService::assignSection()
192
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
193
     */
194 View Code Duplication
    public function testAssignSectionThrowsUnauthorizedException()
195
    {
196
        $repository = $this->getRepository();
197
198
        $standardSectionId = $this->generateId('section', 1);
199
        $anonymousUserId = $this->generateId('user', 10);
200
        /* BEGIN: Use Case */
201
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
202
        // Publish demo installation.
203
        // $standardSectionId is the ID of the "Standard" section in a eZ
204
        // Publish demo installation.
205
206
        // RemoteId of the "Media" page of an eZ Publish demo installation
207
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
208
209
        $userService = $repository->getUserService();
210
        $contentService = $repository->getContentService();
211
        $sectionService = $repository->getSectionService();
212
213
        // Load a content info instance
214
        $contentInfo = $contentService->loadContentInfoByRemoteId(
215
            $mediaRemoteId
216
        );
217
218
        // Load the "Standard" section
219
        $section = $sectionService->loadSection($standardSectionId);
220
221
        // Set anonymous user
222
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
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...
223
224
        // This call will fail with a "UnauthorizedException"
225
        $sectionService->assignSection($contentInfo, $section);
226
        /* END: Use Case */
227
    }
228
229
    /**
230
     * Test for the deleteSection() method.
231
     *
232
     * @see \eZ\Publish\API\Repository\SectionService::deleteSection()
233
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
234
     */
235 View Code Duplication
    public function testDeleteSectionThrowsUnauthorizedException()
236
    {
237
        $repository = $this->getRepository();
238
239
        $anonymousUserId = $this->generateId('user', 10);
240
        /* BEGIN: Use Case */
241
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
242
        // Publish demo installation.
243
        $userService = $repository->getUserService();
244
        $sectionService = $repository->getSectionService();
245
246
        $sectionCreate = $sectionService->newSectionCreateStruct();
247
        $sectionCreate->name = 'Test Section';
248
        $sectionCreate->identifier = 'uniqueKey';
249
250
        $section = $sectionService->createSection($sectionCreate);
251
252
        // Set anonymous user
253
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
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...
254
255
        // This call will fail with a "UnauthorizedException"
256
        $sectionService->deleteSection($section);
257
        /* END: Use Case */
258
    }
259
}
260