19declare(strict_types=1);
22use PHPUnit\Framework\TestCase;
28 protected function setUp(): void
32 $this->dic_backup =
$DIC;
40 $this->createMock(ilDBInterface::class)
71 public function testBasicSessionBehaviour(): void
77 $this->getMockBuilder(ilIniFile::class)->disableOriginalConstructor()->getMock()
81 $settings = $this->getMockBuilder(ilSetting::class)->getMock();
83 ->expects($this->once())
87 if ($arg ===
'session_statistics') {
91 throw new RuntimeException($arg);
102 ->expects($this->atLeastOnce())
104 ->with(
'usr_session')
107 $consecutive_quote = [
111 'e10adc3949ba59abbe56e057f20f883e',
113 'e10adc3949ba59abbe56e057f20f883e',
114 'e10adc3949ba59abbe56e057f20f883e',
119 'e10adc3949ba59abbe56e057f20f883e',
121 'e10adc3949ba59abbe56e057f20f883e'
124 ->expects($this->exactly(count($consecutive_quote)))
127 $this->callback(
function ($value) use (&$consecutive_quote) {
128 if (count($consecutive_quote) === 4) {
129 $this->assertGreaterThan(array_shift($consecutive_quote), $value);
131 $this->assertSame(array_shift($consecutive_quote), $value);
135 )->willReturnOnConsecutiveCalls(
139 'e10adc3949ba59abbe56e057f20f883e',
140 'e10adc3949ba59abbe56e057f20f883e',
142 'e10adc3949ba59abbe56e057f20f883e',
143 'e10adc3949ba59abbe56e057f20f883e',
148 'e10adc3949ba59abbe56e057f20f883e',
150 'e10adc3949ba59abbe56e057f20f883e'
153 ->expects($this->exactly(6))
155 ->willReturn(1, 1, 1, 0, 1, 0);
157 $consecutive_select = [
158 'SELECT 1 FROM usr_session WHERE session_id = 123456',
159 'SELECT 1 FROM usr_session WHERE session_id = 123456',
160 'SELECT data FROM usr_session WHERE session_id = 123456',
161 'SELECT * FROM usr_session WHERE session_id = e10adc3949ba59abbe56e057f20f883e',
162 'SELECT * FROM usr_session WHERE session_id = e10adc3949ba59abbe56e057f20f883e',
163 'SELECT 1 FROM usr_session WHERE session_id = 123456',
164 'SELECT data FROM usr_session WHERE session_id = e10adc3949ba59abbe56e057f20f883e',
165 'SELECT 1 FROM usr_session WHERE session_id = 123456',
166 'SELECT session_id, expires FROM usr_session WHERE expires < 123456',
167 'SELECT 1 FROM usr_session WHERE session_id = ',
168 'SELECT 1 FROM usr_session WHERE session_id = 17'
171 ->expects($this->exactly(count($consecutive_select)))
174 $this->callback(
function ($value) use (&$consecutive_select) {
175 if (count($consecutive_select) === 2) {
176 $this->assertStringStartsWith(array_shift($consecutive_select), $value);
178 $this->assertSame(array_shift($consecutive_select), $value);
182 )->willReturnOnConsecutiveCalls(
183 $this->createMock(ilDBStatement::class),
184 $this->createMock(ilDBStatement::class),
185 $this->createMock(ilDBStatement::class),
186 $this->createMock(ilDBStatement::class),
187 $this->createMock(ilDBStatement::class),
188 $this->createMock(ilDBStatement::class),
189 $this->createMock(ilDBStatement::class),
190 $this->createMock(ilDBStatement::class),
191 $this->createMock(ilDBStatement::class),
192 $this->createMock(ilDBStatement::class),
193 $this->createMock(ilDBStatement::class)
196 ->expects($this->exactly(4))
197 ->method(
'fetchAssoc')->willReturn(
198 [
'data' =>
'Testdata'],
200 [
'data' =>
'Testdata'],
204 ->expects($this->once())
205 ->method(
'fetchObject')->willReturn(
211 $consecutive_delete = [
212 'DELETE FROM usr_sess_istorage WHERE session_id = 123456',
213 'DELETE FROM usr_session WHERE session_id = e10adc3949ba59abbe56e057f20f883e',
214 'DELETE FROM usr_session WHERE user_id = e10adc3949ba59abbe56e057f20f883e'
217 ->expects($this->exactly(count($consecutive_delete)))
218 ->method(
'manipulate')->with(
219 $this->callback(
function ($value) use (&$consecutive_delete) {
220 $this->assertSame(array_shift($consecutive_delete), $value);
223 )->willReturnOnConsecutiveCalls(
229 $cron_manager = $this->createMock(\
ILIAS\Cron\Job\JobManager::class);
231 ->expects($this->atLeastOnce())
232 ->method(
'isJobActive')
233 ->with($this->isString())
241 ilSession::_writeData(
'123456',
'Testdata');
243 $result .=
'exists-';
246 $result .=
'write-get-';
250 $result .=
'duplicate-';
254 $result .=
'destroy-';
258 $result .=
'destroyExp-';
263 $result .=
'destroyByUser-';
266 $this->assertEquals(
'exists-write-get-duplicate-destroy-destroyExp-destroyByUser-', $result);
275 $sqlite =
new PDO(
'sqlite::memory:');
276 $create_table = <<<SQL
277create table usr_pwassist
279 pwassist_id
char(180)
default '' not
null primary key,
280 expires
int default 0 not
null,
281 ctime
int default 0 not
null,
282 user_id
int default 0 not
null,
288 $sqlite->query($create_table);
289 }
catch (Exception
$e) {
290 $this->markTestIncomplete(
291 'Cannot test the password assistance session storage because of missing sqlite: ' .
$e->getMessage()
295 $db = $this->createMock(ilDBInterface::class);
296 $db->method(
'quote')->willReturnCallback(
static function ($value, ?
string $type =
null) use ($sqlite):
string {
297 if ($value ===
null) {
301 $pdo_type = PDO::PARAM_STR;
309 if ($value ===
'NOW()') {
312 $value = (string) $value;
315 return (
string) (
int) $value;
317 $pdo_type = PDO::PARAM_INT;
318 $value = (string) $value;
322 $value = (string) $value;
323 $pdo_type = PDO::PARAM_STR;
327 return $sqlite->quote((
string) $value, $pdo_type);
329 $db->method(
'query')->willReturnCallback(
static function (
string $query) use ($sqlite):
ilDBStatement {
332 $db->method(
'manipulate')->willReturnCallback(
static function (
string $query) use ($sqlite):
int {
333 return (
int) $sqlite->exec($query);
335 $db->method(
'manipulateF')->willReturnCallback(
static function (...$args) use ($db):
int {
339 foreach ($args[1] as $k => $t) {
340 $quoted_values[] = $db->quote($args[2][$k], $t);
342 $query = vsprintf($query, $quoted_values);
344 return $db->manipulate($query);
346 $db->method(
'fetchAssoc')->willReturnCallback(
static function (
ilDBStatement $statement): ?array {
347 $res = $statement->
fetch(PDO::FETCH_ASSOC);
348 if (
$res ===
null ||
$res ===
false) {
349 $statement->closeCursor();
357 $pwa_repository = new \ILIAS\Init\PasswordAssitance\Repository\PasswordAssistanceDbRepository(
359 (
new \
ILIAS\Data\Factory())->clock()->system()
362 $hash = new \ILIAS\Init\PasswordAssitance\ValueObject\PasswordAssistanceHash(
363 'ae869e66007cc9812f1752f7a3a59f07d3e28bed8361827d0a05563e5c2f4b11'
365 $session = $pwa_repository->createSession(
367 (
new \
ILIAS\Data\Factory())->objId($usr_id)
370 $result = $pwa_repository->getSessionByUsrId($session->usrId());
371 if ($result->value()->hash()->value() === $session->hash()->value()) {
375 $result = $pwa_repository->getSessionByHash($session->hash());
376 if ($result->value()->usrId()->toInt() === $usr_id) {
380 $pwa_repository->deleteSession($session);
381 $result = $pwa_repository->getSessionByHash($session->hash());
382 if ($result->isError()) {
383 $actual .=
'destroy-';
386 $this->assertEquals(
'find-read-destroy-', $actual);
Customizing of pimple-DIC for ILIAS.
Class ilPDOStatement is a Wrapper Class for PDOStatement.
testPasswordAssisstanceSession()
setGlobalVariable(string $name, $value)
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.
fetch(int $fetch_mode=ilDBConstants::FETCHMODE_ASSOC)
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.