ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilSessionTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use PHPUnit\Framework\TestCase;
23
24class ilSessionTest extends TestCase
25{
26 private ?Container $dic_backup = null;
27
28 protected function setUp(): void
29 {
30 global $DIC;
31
32 $this->dic_backup = $DIC;
33
34 if (!isset($DIC)) {
35 $DIC = new Container();
36 }
37
38 $this->setGlobalVariable(
39 'ilDB',
40 $this->createMock(ilDBInterface::class)
41 );
42
43 parent::setUp();
44 }
45
46 protected function tearDown(): void
47 {
48 global $DIC;
49
51
52 parent::tearDown();
53 }
54
59 protected function setGlobalVariable(string $name, $value): void
60 {
61 global $DIC;
62
63 $GLOBALS[$name] = $value;
64
65 unset($DIC[$name]);
66 $DIC[$name] = static function (Container $c) use ($name) {
67 return $GLOBALS[$name];
68 };
69 }
70
71 public function testBasicSessionBehaviour(): void
72 {
73 global $DIC;
74
75 $this->setGlobalVariable(
76 'ilClientIniFile',
77 $this->getMockBuilder(ilIniFile::class)->disableOriginalConstructor()->getMock()
78 );
79
81 $settings = $this->getMockBuilder(ilSetting::class)->getMock();
82 $settings->method('get')->willReturnCallback(
83 function ($arg) {
84 if ($arg === 'session_statistics') {
85 return '0';
86 }
87
88 throw new \RuntimeException($arg);
89 }
90 );
91 $this->setGlobalVariable(
92 'ilSetting',
93 $settings
94 );
95
97 $ilDB = $DIC['ilDB'];
98 $ilDB->method('update')->
99 with('usr_session')->willReturn(1);
100
101
102 $consecutive_quote = [
103 '123456',
104 '123456',
105 '123456',
106 'e10adc3949ba59abbe56e057f20f883e',
107 '123456',
108 'e10adc3949ba59abbe56e057f20f883e',
109 'e10adc3949ba59abbe56e057f20f883e',
110 '123456',
111 '123456',
112 '123456',
113 time() - 100,
114 'e10adc3949ba59abbe56e057f20f883e',
115 17,
116 'e10adc3949ba59abbe56e057f20f883e'
117 ];
118 $ilDB->method('quote')->with(
119 $this->callback(function ($value) use (&$consecutive_quote) {
120 if (count($consecutive_quote) === 4) {
121 $this->assertGreaterThan(array_shift($consecutive_quote), $value);
122 } else {
123 $this->assertSame(array_shift($consecutive_quote), $value);
124 }
125 return true;
126 })
127 )->willReturnOnConsecutiveCalls(
128 '123456',
129 '123456',
130 '123456',
131 'e10adc3949ba59abbe56e057f20f883e',
132 'e10adc3949ba59abbe56e057f20f883e',
133 '123456',
134 'e10adc3949ba59abbe56e057f20f883e',
135 'e10adc3949ba59abbe56e057f20f883e',
136 '123456',
137 '123456',
138 '123456',
139 (string) time(),
140 'e10adc3949ba59abbe56e057f20f883e',
141 '17',
142 'e10adc3949ba59abbe56e057f20f883e'
143 );
144 $ilDB->expects($this->exactly(6))->method('numRows')->willReturn(1, 1, 1, 0, 1, 0);
145
146 $consecutive_select = [
147 'SELECT 1 FROM usr_session WHERE session_id = 123456',
148 'SELECT 1 FROM usr_session WHERE session_id = 123456',
149 'SELECT data FROM usr_session WHERE session_id = 123456',
150 'SELECT * FROM usr_session WHERE session_id = e10adc3949ba59abbe56e057f20f883e',
151 'SELECT * FROM usr_session WHERE session_id = e10adc3949ba59abbe56e057f20f883e',
152 'SELECT 1 FROM usr_session WHERE session_id = 123456',
153 'SELECT data FROM usr_session WHERE session_id = e10adc3949ba59abbe56e057f20f883e',
154 'SELECT 1 FROM usr_session WHERE session_id = 123456',
155 'SELECT session_id, expires FROM usr_session WHERE expires < 123456',
156 'SELECT 1 FROM usr_session WHERE session_id = ',
157 'SELECT 1 FROM usr_session WHERE session_id = 17'
158 ];
159 $ilDB->method('query')->with(
160 $this->callback(function ($value) use (&$consecutive_select) {
161 if (count($consecutive_select) === 2) {
162 $this->assertStringStartsWith(array_shift($consecutive_select), $value);
163 } else {
164 $this->assertSame(array_shift($consecutive_select), $value);
165 }
166 return true;
167 })
168 )->willReturnOnConsecutiveCalls(
169 $this->createMock(ilDBStatement::class),
170 $this->createMock(ilDBStatement::class),
171 $this->createMock(ilDBStatement::class),
172 $this->createMock(ilDBStatement::class),
173 $this->createMock(ilDBStatement::class),
174 $this->createMock(ilDBStatement::class),
175 $this->createMock(ilDBStatement::class),
176 $this->createMock(ilDBStatement::class),
177 $this->createMock(ilDBStatement::class),
178 $this->createMock(ilDBStatement::class),
179 $this->createMock(ilDBStatement::class)
180 );
181 $ilDB->expects($this->exactly(4))->method('fetchAssoc')->willReturn(
182 ['data' => 'Testdata'],
183 [],
184 ['data' => 'Testdata'],
185 []
186 );
187 $ilDB->expects($this->once())->method('fetchObject')->willReturn(
188 (object) [
189 'data' => 'Testdata'
190 ]
191 );
192
193 $consecutive_delete = [
194 'DELETE FROM usr_sess_istorage WHERE session_id = 123456',
195 'DELETE FROM usr_session WHERE session_id = e10adc3949ba59abbe56e057f20f883e',
196 'DELETE FROM usr_session WHERE user_id = e10adc3949ba59abbe56e057f20f883e'
197 ];
198 $ilDB->method('manipulate')->with(
199 $this->callback(function ($value) use (&$consecutive_delete) {
200 $this->assertSame(array_shift($consecutive_delete), $value);
201 return true;
202 })
203 )->willReturnOnConsecutiveCalls(
204 1,
205 1,
206 1
207 );
208
209 $cron_manager = $this->createMock(\ILIAS\Cron\Job\JobManager::class);
210 $cron_manager->method('isJobActive')->with($this->isType('string'))->willReturn(true);
211 $this->setGlobalVariable(
212 'cron.manager',
213 $cron_manager
214 );
215
216 $result = '';
217 ilSession::_writeData('123456', 'Testdata');
218 if (ilSession::_exists('123456')) {
219 $result .= 'exists-';
220 }
221 if (ilSession::_getData('123456') === 'Testdata') {
222 $result .= 'write-get-';
223 }
224 $duplicate = ilSession::_duplicate('123456');
225 if (ilSession::_getData($duplicate) === 'Testdata') {
226 $result .= 'duplicate-';
227 }
228 ilSession::_destroy('123456');
229 if (!ilSession::_exists('123456')) {
230 $result .= 'destroy-';
231 }
233 if (ilSession::_exists($duplicate)) {
234 $result .= 'destroyExp-';
235 }
236
238 if (!ilSession::_exists($duplicate)) {
239 $result .= 'destroyByUser-';
240 }
241
242 $this->assertEquals('exists-write-get-duplicate-destroy-destroyExp-destroyByUser-', $result);
243 }
244
245 public function testPasswordAssisstanceSession(): void
246 {
247 $actual = '';
248 $usr_id = 4711;
249
250 try {
251 $sqlite = new PDO('sqlite::memory:');
252 $create_table = <<<SQL
253create table usr_pwassist
254(
255 pwassist_id char(180) default '' not null primary key,
256 expires int default 0 not null,
257 ctime int default 0 not null,
258 user_id int default 0 not null,
259 constraint c1_idx
260 unique (user_id)
261);
262SQL;
263
264 $sqlite->query($create_table);
265 } catch (Exception $e) {
266 $this->markTestIncomplete(
267 'Cannot test the password assistance session storage because of missing sqlite: ' . $e->getMessage()
268 );
269 }
270
271 $db = $this->createMock(ilDBInterface::class);
272 $db->method('quote')->willReturnCallback(static function ($value, ?string $type = null) use ($sqlite): string {
273 if ($value === null) {
274 return 'NULL';
275 }
276
277 $pdo_type = PDO::PARAM_STR;
278 switch ($type) {
282 if ($value === '') {
283 return 'NULL';
284 }
285 if ($value === 'NOW()') {
286 return $value;
287 }
288 $value = (string) $value;
289 break;
291 return (string) (int) $value;
293 $pdo_type = PDO::PARAM_INT;
294 $value = (string) $value;
295 break;
297 default:
298 $value = (string) $value;
299 $pdo_type = PDO::PARAM_STR;
300 break;
301 }
302
303 return $sqlite->quote((string) $value, $pdo_type);
304 });
305 $db->method('query')->willReturnCallback(static function (string $query) use ($sqlite): ilDBStatement {
306 return new ilPDOStatement($sqlite->query($query));
307 });
308 $db->method('manipulate')->willReturnCallback(static function (string $query) use ($sqlite): int {
309 return (int) $sqlite->exec($query);
310 });
311 $db->method('manipulateF')->willReturnCallback(static function (...$args) use ($db): int {
312 $query = $args[0];
313
314 $quoted_values = [];
315 foreach ($args[1] as $k => $t) {
316 $quoted_values[] = $db->quote($args[2][$k], $t);
317 }
318 $query = vsprintf($query, $quoted_values);
319
320 return $db->manipulate($query);
321 });
322 $db->method('fetchAssoc')->willReturnCallback(static function (ilDBStatement $statement): ?array {
323 $res = $statement->fetch(PDO::FETCH_ASSOC);
324 if ($res === null || $res === false) {
325 $statement->closeCursor();
326
327 return null;
328 }
329
330 return $res;
331 });
332
333 $pwa_repository = new \ILIAS\Init\PasswordAssitance\Repository\PasswordAssistanceDbRepository(
334 $db,
335 (new \ILIAS\Data\Factory())->clock()->system()
336 );
337
338 $hash = new \ILIAS\Init\PasswordAssitance\ValueObject\PasswordAssistanceHash(
339 'ae869e66007cc9812f1752f7a3a59f07d3e28bed8361827d0a05563e5c2f4b11'
340 );
341 $session = $pwa_repository->createSession(
342 $hash,
343 (new \ILIAS\Data\Factory())->objId($usr_id)
344 );
345
346 $result = $pwa_repository->getSessionByUsrId($session->usrId());
347 if ($result->value()->hash()->value() === $session->hash()->value()) {
348 $actual .= 'find-';
349 }
350
351 $result = $pwa_repository->getSessionByHash($session->hash());
352 if ($result->value()->usrId()->toInt() === $usr_id) {
353 $actual .= 'read-';
354 }
355
356 $pwa_repository->deleteSession($session);
357 $result = $pwa_repository->getSessionByHash($session->hash());
358 if ($result->isError()) {
359 $actual .= 'destroy-';
360 }
361
362 $this->assertEquals('find-read-destroy-', $actual);
363
364 $sqlite = null;
365 }
366}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
Class ilPDOStatement is a Wrapper Class for PDOStatement.
testPasswordAssisstanceSession()
setGlobalVariable(string $name, $value)
Container $dic_backup
static _destroyByUserId(int $a_user_id)
Destroy session.
static _exists(string $a_session_id)
static _duplicate(string $a_session_id)
Duplicate session.
static _destroyExpiredSessions()
Destroy expired sessions.
static _destroy($a_session_id, ?int $a_closing_context=null, $a_expired_at=null)
Destroy session.
static _getData(string $a_session_id)
Get session data from table.
$c
Definition: deliver.php:25
Interface ilDBStatement.
fetch(int $fetch_mode=ilDBConstants::FETCHMODE_ASSOC)
$res
Definition: ltiservices.php:69
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26
$GLOBALS["DIC"]
Definition: wac.php:54