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