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