ILIAS  release_8 Revision v8.24
ilServicesGlobalScreenTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\TestCase;
24
25class ilServicesGlobalScreenTest extends TestCase
26{
27 private ?Container $dic_backup = null;
36 private int $SYSTEM_FOLDER_ID;
37 private int $ROOT_FOLDER_ID;
38
39 protected function setUp(): void
40 {
41 global $DIC;
42 $this->dic_backup = is_object($DIC) ? clone $DIC : $DIC;
43 $DIC = new Container();
44 if (!defined('SYSTEM_FOLDER_ID')) {
45 define('SYSTEM_FOLDER_ID', 42);
46 }
48 if (!defined('ROOT_FOLDER_ID')) {
49 define('ROOT_FOLDER_ID', 24);
50 }
52 }
53
54 protected function tearDown(): void
55 {
56 global $DIC;
58 }
59
60 public function testAdminAccessTrue(): void
61 {
62 global $DIC;
63 $DIC['rbacsystem'] = $rbac_mock = $this->createMock(ilRbacSystem::class);
64 $class = new BasicAccessCheckClosures($DIC);
65
66 $rbac_mock->expects($this->once())
67 ->method('checkAccess')
68 ->with('visible', $this->SYSTEM_FOLDER_ID)
69 ->willReturn(true);
70
71 $this->assertTrue($class->hasAdministrationAccess()());
72 $this->assertTrue(
73 $class->hasAdministrationAccess()()
74 ); // second call to check caching, see expectation $this->once()
75 }
76
77 public function testAdminAccessFalse(): void
78 {
79 global $DIC;
80 $DIC['rbacsystem'] = $rbac_mock = $this->createMock(ilRbacSystem::class);
81 $class = new BasicAccessCheckClosures($DIC);
82
83 $rbac_mock->expects($this->once())
84 ->method('checkAccess')
85 ->with('visible', $this->SYSTEM_FOLDER_ID)
86 ->willReturn(false);
87
88 $this->assertFalse($class->hasAdministrationAccess()());
89 $this->assertFalse(
90 $class->hasAdministrationAccess()()
91 ); // second call to check caching, see expectation $this->once()
92 }
93
94 public function testAdminAcessTrueButWithClosure(): void
95 {
96 global $DIC;
97 $DIC['rbacsystem'] = $rbac_mock = $this->createMock(ilRbacSystem::class);
98 $class = new BasicAccessCheckClosures($DIC);
99
100 $rbac_mock->expects($this->once())
101 ->method('checkAccess')
102 ->with('visible', $this->SYSTEM_FOLDER_ID)
103 ->willReturn(true);
104
105 $closure_returning_false = function (): bool {
106 return false;
107 };
108
109 $this->assertTrue($class->hasAdministrationAccess()());
110 $this->assertFalse(
111 $class->hasAdministrationAccess($closure_returning_false)()
112 );
113 }
114
116 {
117 global $DIC;
118
119 $DIC['ilUser'] = $user_mock = $this->createMock(ilObjUser::class);
120 $DIC['ilSetting'] = $settings_mock = $this->createMock(ilSetting::class);
121 $DIC['ilAccess'] = $access_mock = $this->createMock(ilAccessHandler::class);
122
123 $class = new BasicAccessCheckClosures($DIC);
124
125 $user_mock->expects($this->once())
126 ->method('isAnonymous')
127 ->willReturn(true);
128
129 $settings_mock->expects($this->once())
130 ->method('get')
131 ->with('pub_section')
132 ->willReturn('1');
133
134 $access_mock->expects($this->once())
135 ->method('checkAccessOfUser')
136 ->with($this->isType('integer'), 'read', '', $this->ROOT_FOLDER_ID)
137 ->willReturn(true);
138
139 $this->assertTrue($class->isRepositoryReadable()());
140 $this->assertTrue(
141 $class->isRepositoryReadable()()
142 ); // second call to check caching, see expectation $this->once()
143 $this->assertTrue(
144 $class->isRepositoryReadable(function (): bool {
145 return true;
146 })()
147 );
148 $this->assertFalse(
149 $class->isRepositoryReadable(function (): bool {
150 return false;
151 })()
152 );
153 }
154
156 {
157 global $DIC;
158
159 $DIC['ilUser'] = $user_mock = $this->createMock(ilObjUser::class);
160 $DIC['ilSetting'] = $settings_mock = $this->createMock(ilSetting::class);
161 $DIC['ilAccess'] = $access_mock = $this->createMock(ilAccessHandler::class);
162
163 $class = new BasicAccessCheckClosures($DIC);
164
165 $user_mock->expects($this->once())
166 ->method('isAnonymous')
167 ->willReturn(true);
168
169 $settings_mock->expects($this->once())
170 ->method('get')
171 ->with('pub_section')
172 ->willReturn('0');
173
174 $access_mock->expects($this->never())
175 ->method('checkAccessOfUser');
176
177 $this->assertFalse($class->isRepositoryReadable()());
178 $this->assertFalse(
179 $class->isRepositoryReadable()()
180 ); // second call to check caching, see expectation $this->once()
181 $this->assertFalse(
182 $class->isRepositoryReadable(function (): bool {
183 return true;
184 })()
185 );
186 $this->assertFalse(
187 $class->isRepositoryReadable(function (): bool {
188 return false;
189 })()
190 );
191 }
192
193 public function testRepoAccessTrueLoggedIn(): void
194 {
195 global $DIC;
196
197 $DIC['ilUser'] = $user_mock = $this->createMock(ilObjUser::class);
198 $DIC['ilSetting'] = $settings_mock = $this->createMock(ilSetting::class);
199 $DIC['ilAccess'] = $access_mock = $this->createMock(ilAccessHandler::class);
200
201 $class = new BasicAccessCheckClosures($DIC);
202
203 $user_mock->expects($this->once())
204 ->method('isAnonymous')
205 ->willReturn(false);
206
207 $user_mock->expects($this->once())
208 ->method('getId')
209 ->willReturn(6);
210
211 $settings_mock->expects($this->never())
212 ->method('get');
213
214 $access_mock->expects($this->once())
215 ->method('checkAccess')
216 ->with('read', '', $this->ROOT_FOLDER_ID)
217 ->willReturn(true);
218
219 $this->assertTrue($class->isRepositoryReadable()());
220 $this->assertTrue(
221 $class->isRepositoryReadable()()
222 ); // second call to check caching, see expectation $this->once()
223 $this->assertTrue(
224 $class->isRepositoryReadable(function (): bool {
225 return true;
226 })()
227 );
228 $this->assertFalse(
229 $class->isRepositoryReadable(function (): bool {
230 return false;
231 })()
232 );
233 }
234
235 public function testRepoAccessFalseLoggedIn(): void
236 {
237 global $DIC;
238
239 $DIC['ilUser'] = $user_mock = $this->createMock(ilObjUser::class);
240 $DIC['ilSetting'] = $settings_mock = $this->createMock(ilSetting::class);
241 $DIC['ilAccess'] = $access_mock = $this->createMock(ilAccessHandler::class);
242
243 $class = new BasicAccessCheckClosures($DIC);
244
245 $user_mock->expects($this->once())
246 ->method('isAnonymous')
247 ->willReturn(false);
248
249 $user_mock->expects($this->once())
250 ->method('getId')
251 ->willReturn(6);
252
253 $settings_mock->expects($this->never())
254 ->method('get');
255
256 $access_mock->expects($this->once())
257 ->method('checkAccess')
258 ->with('read', '', $this->ROOT_FOLDER_ID)
259 ->willReturn(false);
260
261 $this->assertFalse($class->isRepositoryReadable()());
262 $this->assertFalse(
263 $class->isRepositoryReadable()()
264 ); // second call to check caching, see expectation $this->once()
265 $this->assertFalse(
266 $class->isRepositoryReadable(function (): bool {
267 return true;
268 })()
269 );
270 $this->assertFalse(
271 $class->isRepositoryReadable(function (): bool {
272 return false;
273 })()
274 );
275 }
276}
static return function(ContainerConfigurator $containerConfigurator)
Definition: basic_rector.php:9
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:32
User class.
class ilRbacSystem system function like checkAccess, addActiveRole ... Supporting system functions ar...
const SYSTEM_FOLDER_ID
Definition: constants.php:35
const ROOT_FOLDER_ID
Definition: constants.php:32
global $DIC
Definition: feed.php:28