ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
UserTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30use ILIAS\LegalDocuments\test\ContainerMock;
35use PHPUnit\Framework\TestCase;
37use ilObjUser;
38use DateTimeImmutable;
39use ilAuthUtils;
40use PHPUnit\Framework\Attributes\DataProvider;
41
42require_once __DIR__ . '/../ContainerMock.php';
43
44class UserTest extends TestCase
45{
46 use ContainerMock;
47
48 public function testConstruct(): void
49 {
50 $this->assertInstanceOf(User::class, new User(
51 $this->mock(ilObjUser::class),
52 $this->mock(Settings::class),
53 $this->mock(UserSettings::class),
54 $this->mock(Provide::class),
55 $this->mock(Clock::class)
56 ));
57 }
58
59 public function testIsLoggedIn(): void
60 {
61 $this->ensureDefined('ANONYMOUS_USER_ID', 13);
62
63 $user = $this->mock(ilObjUser::class);
64 $user->expects(self::exactly(5))->method('getId')->willReturnOnConsecutiveCalls(0, ANONYMOUS_USER_ID, 9, 1, 68);
65
66 $instance = new User(
67 $user,
68 $this->mock(Settings::class),
69 $this->mock(UserSettings::class),
70 $this->mock(Provide::class),
71 $this->mock(Clock::class)
72 );
73
74 $this->assertFalse($instance->isLoggedIn());
75 $this->assertFalse($instance->isLoggedIn());
76 $this->assertTrue($instance->isLoggedIn());
77 $this->assertTrue($instance->isLoggedIn());
78 $this->assertTrue($instance->isLoggedIn());
79 }
80
81 public function testCannotAgree(): void
82 {
83 $this->ensureDefined('ANONYMOUS_USER_ID', 13);
84 $this->ensureDefined('SYSTEM_USER_ID', 9);
85
86 $user = $this->mock(ilObjUser::class);
87 $user->expects(self::exactly(5))->method('getId')->willReturnOnConsecutiveCalls(0, ANONYMOUS_USER_ID, SYSTEM_USER_ID, 1, 68);
88
89 $instance = new User(
90 $user,
91 $this->mock(Settings::class),
92 $this->mock(UserSettings::class),
93 $this->mock(Provide::class),
94 $this->mock(Clock::class)
95 );
96
97 $this->assertTrue($instance->cannotAgree());
98 $this->assertTrue($instance->cannotAgree());
99 $this->assertTrue($instance->cannotAgree());
100 $this->assertFalse($instance->cannotAgree());
101 $this->assertFalse($instance->cannotAgree());
102 }
103
104 public function testNeverAgreed(): void
105 {
106 $setting = $this->mock(Setting::class);
107 $setting->expects(self::exactly(2))->method('value')->willReturnOnConsecutiveCalls(null, new DateTimeImmutable());
108
109 $user_settings = $this->mock(UserSettings::class);
110 $user_settings->expects(self::exactly(2))->method('agreeDate')->willReturn($setting);
111
112 $instance = new User(
113 $this->mock(ilObjUser::class),
114 $this->mock(Settings::class),
115 $user_settings,
116 $this->mock(Provide::class),
117 $this->mock(Clock::class)
118 );
119
120 $this->assertTrue($instance->neverAgreed());
121 $this->assertFalse($instance->neverAgreed());
122 }
123
124 public function testWithdrawalRequested(): void
125 {
126 $setting = $this->mock(Setting::class);
127
128 $instance = new User(
129 $this->mock(ilObjUser::class),
130 $this->mock(Settings::class),
131 $this->mockMethod(UserSettings::class, 'withdrawalRequested', [], $setting),
132 $this->mock(Provide::class),
133 $this->mock(Clock::class)
134 );
135
136 $this->assertSame($setting, $instance->withdrawalRequested());
137 }
138
139 public function testAgreeDate(): void
140 {
141 $setting = $this->mock(Setting::class);
142
143 $instance = new User(
144 $this->mock(ilObjUser::class),
145 $this->mock(Settings::class),
146 $this->mockMethod(UserSettings::class, 'agreeDate', [], $setting),
147 $this->mock(Provide::class),
148 $this->mock(Clock::class)
149 );
150
151 $this->assertSame($setting, $instance->agreeDate());
152 }
153
154 public function testDidNotAcceptCurrentVersion(): void
155 {
156 $user = $this->mock(ilObjUser::class);
157 $document = $this->mock(Document::class);
158
159 $instance = new User(
160 $user,
161 $this->mockTree(Settings::class, ['validateOnLogin' => ['value' => true]]),
162 $this->mock(UserSettings::class),
163 $this->mockTree(Provide::class, [
164 'document' => $this->mockMethod(ProvideDocument::class, 'chooseDocumentFor', [$user], new Ok($document)),
165 'history' => $this->mockMethod(ProvideHistory::class, 'alreadyAccepted', [$user, $document], false),
166 ]),
167 $this->mock(Clock::class)
168 );
169
170 $this->assertTrue($instance->didNotAcceptCurrentVersion());
171 }
172
174 {
175 $instance = new User(
176 $this->mock(ilObjUser::class),
177 $this->mock(Settings::class),
178 $this->mockTree(UserSettings::class, ['agreeDate' => ['value' => null]]),
179 $this->mock(Provide::class),
180 $this->mock(Clock::class)
181 );
182
183 $this->assertTrue($instance->needsToAcceptNewDocument());
184 }
185
187 {
188 $user = $this->mock(ilObjUser::class);
189 $history = $this->mockMethod(ProvideHistory::class, 'currentDocumentOfAcceptedVersion', [$user], new Error('Not found.'));
190
191 $instance = new User(
192 $user,
193 $this->mockTree(Settings::class, ['validateOnLogin' => ['value' => true]]),
194 $this->mockTree(UserSettings::class, ['agreeDate' => ['value' => new DateTimeImmutable()]]),
195 $this->mockTree(Provide::class, ['history' => $history]),
196 $this->mock(Clock::class)
197 );
198
199 $this->assertTrue($instance->needsToAcceptNewDocument());
200 }
201
203 {
204 $user = $this->mock(ilObjUser::class);
205 $document = $this->mock(Document::class);
206 $history = $this->mockMethod(ProvideHistory::class, 'currentDocumentOfAcceptedVersion', [$user], new Ok($document));
207
208 $instance = new User(
209 $user,
210 $this->mockTree(Settings::class, ['validateOnLogin' => ['value' => true]]),
211 $this->mockTree(UserSettings::class, ['agreeDate' => ['value' => new DateTimeImmutable()]]),
212 $this->mockTree(Provide::class, [
213 'document' => $this->mockMethod(ProvideDocument::class, 'documentMatches', [$document, $user], true),
214 'history' => $history,
215 ]),
216 $this->mock(Clock::class)
217 );
218
219 $this->assertFalse($instance->needsToAcceptNewDocument());
220 }
221
222 public function testDoesntMatch(): void
223 {
224 $document = $this->mock(Document::class);
225 $user = $this->mock(ilObjUser::class);
226
227 $instance = new User(
228 $user,
229 $this->mock(Settings::class),
230 $this->mock(UserSettings::class),
231 $this->mockTree(Provide::class, [
232 'document' => $this->mockMethod(ProvideDocument::class, 'documentMatches', [$document, $user], true),
233 ]),
234 $this->mock(Clock::class)
235 );
236
237 $this->assertFalse($instance->doesntMatch($document));
238 }
239
240 public function testMatchingDocument(): void
241 {
242 $user = $this->mock(ilObjUser::class);
243 $result = $this->mock(Result::class);
244
245 $instance = new User(
246 $user,
247 $this->mock(Settings::class),
248 $this->mock(UserSettings::class),
249 $this->mockTree(Provide::class, [
250 'document' => $this->mockMethod(ProvideDocument::class, 'chooseDocumentFor', [$user], $result),
251 ]),
252 $this->mock(Clock::class)
253 );
254
255 $this->assertSame($result, $instance->matchingDocument());
256 }
257
258 public function testAcceptedVersion(): void
259 {
260 $this->ensureDefined('ANONYMOUS_USER_ID', 13);
261 $this->ensureDefined('SYSTEM_USER_ID', 9);
262
263 $result = $this->mock(Result::class);
264
265 $user = $this->mockTree(ilObjUser::class, ['getId' => 67]);
266
267 $instance = new User(
268 $user,
269 $this->mock(Settings::class),
270 $this->mockTree(UserSettings::class, ['agreeDate' => ['value' => new DateTimeImmutable()]]),
271 $this->mockTree(Provide::class, ['history' => $this->mockMethod(ProvideHistory::class, 'acceptedVersion', [$user], $result)]),
272 $this->mock(Clock::class)
273 );
274
275 $this->assertSame($result, $instance->acceptedVersion());
276 }
277
278 public function testAcceptVersionError(): void
279 {
280 $this->ensureDefined('ANONYMOUS_USER_ID', 13);
281 $this->ensureDefined('SYSTEM_USER_ID', 9);
282
283 $user = $this->mockTree(ilObjUser::class, ['getId' => 67]);
284
285 $instance = new User(
286 $user,
287 $this->mock(Settings::class),
288 $this->mockTree(UserSettings::class, ['agreeDate' => ['value' => null]]),
289 $this->mock(Provide::class),
290 $this->mock(Clock::class)
291 );
292
293 $result = $instance->acceptedVersion();
294 $this->assertFalse($result->isOk());
295 $this->assertSame('User never agreed.', $result->error());
296 }
297
298 public function testAcceptMatchingDocument(): void
299 {
300 $user = $this->mock(ilObjUser::class);
301 $document = $this->mock(Document::class);
302 $date = new DateTimeImmutable();
303
304 $history = $this->mock(ProvideHistory::class);
305 $history->expects(self::once())->method('acceptDocument')->with($user, $document);
306
307 $setting = $this->mock(Setting::class);
308 $setting->expects(self::once())->method('update')->with($date);
309
310 $instance = new User(
311 $user,
312 $this->mock(Settings::class),
313 $this->mockTree(UserSettings::class, ['agreeDate' => $setting]),
314 $this->mockTree(Provide::class, [
315 'document' => $this->mockMethod(ProvideDocument::class, 'chooseDocumentFor', [$user], new Ok($document)),
316 'history' => $history,
317 ]),
318 $this->mockTree(Clock::class, ['now' => $date])
319 );
320
321 $instance->acceptMatchingDocument();
322 }
323
324 public function testIsLDAPUser(): void
325 {
326 $instance = new User(
327 $this->mockTree(ilObjUser::class, ['getAuthMode' => 'default']),
328 $this->mockTree(Settings::class, ['authMode' => ['value' => (string) ilAuthUtils::AUTH_LDAP]]),
329 $this->mock(UserSettings::class),
330 $this->mock(Provide::class),
331 $this->mock(Clock::class)
332 );
333
334 $this->assertTrue($instance->isLDAPUser());
335 }
336
337 #[DataProvider('externalAuthModes')]
338 public function testIsExternalUser(int $auth_mode, bool $is_external_account): void
339 {
340 $instance = new User(
341 $this->mockTree(ilObjUser::class, ['getAuthMode' => 'default']),
342 $this->mockTree(Settings::class, ['authMode' => ['value' => (string) $auth_mode]]),
343 $this->mock(UserSettings::class),
344 $this->mock(Provide::class),
345 $this->mock(Clock::class)
346 );
347
348 $this->assertSame($is_external_account, $instance->isExternalAccount());
349 }
350
351 public function testFormat(): void
352 {
353 $instance = new User(
354 $this->mockTree(ilObjUser::class, [
355 'getFullname' => 'foo',
356 'getLogin' => 'bar',
357 'getExternalAccount' => 'baz',
358 ]),
359 $this->mock(Settings::class),
360 $this->mock(UserSettings::class),
361 $this->mock(Provide::class),
362 $this->mock(Clock::class)
363 );
364
365 $this->assertSame('', $instance->format(''));
366 $this->assertSame("hej foo bar\nbaz", $instance->format('hej %s %s[BR]%s'));
367 }
368
369 public function testRaw(): void
370 {
371 $user = $this->mock(ilObjUser::class);
372
373 $instance = new User(
374 $user,
375 $this->mock(Settings::class),
376 $this->mock(UserSettings::class),
377 $this->mock(Provide::class),
378 $this->mock(Clock::class)
379 );
380
381 $this->assertSame($user, $instance->raw());
382 }
383
384 public function testAcceptAnyDocument(): void
385 {
386 $user = $this->mock(ilObjUser::class);
387 $history = $this->mock(ProvideHistory::class);
388 $doc = $this->mock(Document::class);
389 $history->expects(self::once())->method('acceptDocument')->with($user, $doc);
390
391 $instance = new User(
392 $user,
393 $this->mock(Settings::class),
394 $this->mock(UserSettings::class),
395 $this->mockTree(Provide::class, [
396 'document' => ['repository' => ['all' => [$doc]]],
397 'history' => $history,
398 ]),
399 $this->mock(Clock::class)
400 );
401
402 $instance->acceptAnyDocument();
403 }
404
405 public static function externalAuthModes(): array
406 {
407 return [
408 'lti' => [ilAuthUtils::AUTH_PROVIDER_LTI, true],
409 'ecs' => [ilAuthUtils::AUTH_ECS, true],
410 'ldap' => [ilAuthUtils::AUTH_LDAP, false],
411 ];
412 }
413
414 private function ensureDefined(string $name, $value): void
415 {
416 if (!defined($name)) {
417 define($name, $value);
418 }
419 }
420}
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:31
testIsExternalUser(int $auth_mode, bool $is_external_account)
Definition: UserTest.php:338
ilSetting $setting
Definition: class.ilias.php:68
const int AUTH_ECS
const int AUTH_LDAP
const int AUTH_PROVIDER_LTI
User class.
const SYSTEM_USER_ID
This file contains constants for PHPStan analyis, see: https://phpstan.org/config-reference#constants...
Definition: constants.php:26
const ANONYMOUS_USER_ID
Definition: constants.php:27