ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ilSessionTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 
30 class ilSessionTest extends TestCase
31 {
32  protected function setUp(): void
33  {
34  $this->dic = new Container();
35  $GLOBALS['DIC'] = $this->dic;
36 
37  //$this->setGlobalVariable('lng', $this->getLanguageMock());
38  $this->setGlobalVariable(
39  'ilCtrl',
40  $this->getMockBuilder(ilCtrl::class)->disableOriginalConstructor()->getMock()
41  );
42  $this->setGlobalVariable(
43  'ilUser',
44  $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->getMock()
45  );
46  $this->setGlobalVariable(
47  'ilDB',
48  $this->getMockBuilder(ilDBInterface::class)->disableAutoReturnValueGeneration()->getMock()
49  );
50  $this->setGlobalVariable(
51  'ilClientIniFile',
52  $this->getMockBuilder(ilIniFile::class)->disableOriginalConstructor()->getMock()
53  );
54  $this->setGlobalVariable(
55  'ilSetting',
56  $this->getMockBuilder(\ILIAS\Administration\Setting::class)->getMock()
57  );
58  parent::setUp();
59  }
60 
65  protected function setGlobalVariable(string $name, $value): void
66  {
67  global $DIC;
68 
69  $GLOBALS[$name] = $value;
70 
71  unset($DIC[$name]);
72  $DIC[$name] = static function ($c) use ($name) {
73  return $GLOBALS[$name];
74  };
75  }
76 
77  public function tstBasicSessionBehaviour(): void
78  {
79  global $DIC;
80 
81  //setup some method calls
83  $setting = $DIC['ilSetting'];
84  $setting->method("get")->willReturnCallback(
85  function ($arg) {
86  if ($arg === 'session_handling_type') {
87  return (string) ilSession::SESSION_HANDLING_FIXED;
88  }
89  if ($arg === 'session_statistics') {
90  return "0";
91  }
92 
93  throw new \RuntimeException($arg);
94  }
95  );
97  $data = null;
98  $ilDB = $DIC['ilDB'];
99  $ilDB->method("update")->
100  with("usr_session")->willReturn(1);
101 
102  $ilDB->method("quote")->withConsecutive(
103  ["123456"],
104  ["123456"],
105  ["123456"],
106  ["e10adc3949ba59abbe56e057f20f883e"],
107  ["123456"],
108  ["e10adc3949ba59abbe56e057f20f883e"],
109  ["e10adc3949ba59abbe56e057f20f883e"],
110  ["123456"],
111  ["123456"],
112  ["123456"],
113  [$this->greaterThan(time() - 100)],
114  ["e10adc3949ba59abbe56e057f20f883e"],
115  ["17"]
116  )->
117  willReturnOnConsecutiveCalls(
118  "123456",
119  "123456",
120  "123456",
121  "e10adc3949ba59abbe56e057f20f883e",
122  "e10adc3949ba59abbe56e057f20f883e",
123  "123456",
124  "e10adc3949ba59abbe56e057f20f883e",
125  "e10adc3949ba59abbe56e057f20f883e",
126  "123456",
127  "123456",
128  "123456",
129  (string) time(),
130  "e10adc3949ba59abbe56e057f20f883e",
131  "17"
132  );
133  $ilDB->expects($this->exactly(6))->method("numRows")->willReturn(1, 1, 1, 0, 1, 0);
134 
135 
136  $ilDB->method("query")->withConsecutive(
137  ["SELECT 1 FROM usr_session WHERE session_id = 123456"],
138  ["SELECT 1 FROM usr_session WHERE session_id = 123456"],
139  ["SELECT data FROM usr_session WHERE session_id = 123456"],
140  ['SELECT * FROM usr_session WHERE session_id = e10adc3949ba59abbe56e057f20f883e'],
141  ['SELECT * FROM usr_session WHERE session_id = e10adc3949ba59abbe56e057f20f883e'],
142  ["SELECT 1 FROM usr_session WHERE session_id = 123456"],
143  ['SELECT data FROM usr_session WHERE session_id = e10adc3949ba59abbe56e057f20f883e'],
144  ['SELECT 1 FROM usr_session WHERE session_id = 123456'],
145  ['SELECT session_id,expires FROM usr_session WHERE expires < 123456'],
146  [$this->stringStartsWith('SELECT 1 FROM usr_session WHERE session_id = ')],
147  ['SELECT 1 FROM usr_session WHERE session_id = 17']
148  )->
149  willReturnOnConsecutiveCalls(
150  $this->getMockBuilder(ilDBStatement::class)->disableAutoReturnValueGeneration()->getMock(),
151  $this->getMockBuilder(ilDBStatement::class)->disableAutoReturnValueGeneration()->getMock(),
152  $this->getMockBuilder(ilDBStatement::class)->disableAutoReturnValueGeneration()->getMock(),
153  $this->getMockBuilder(ilDBStatement::class)->disableAutoReturnValueGeneration()->getMock(),
154  $this->getMockBuilder(ilDBStatement::class)->disableAutoReturnValueGeneration()->getMock(),
155  $this->getMockBuilder(ilDBStatement::class)->disableAutoReturnValueGeneration()->getMock(),
156  $this->getMockBuilder(ilDBStatement::class)->disableAutoReturnValueGeneration()->getMock(),
157  $this->getMockBuilder(ilDBStatement::class)->disableAutoReturnValueGeneration()->getMock(),
158  $this->getMockBuilder(ilDBStatement::class)->disableAutoReturnValueGeneration()->getMock(),
159  $this->getMockBuilder(ilDBStatement::class)->disableAutoReturnValueGeneration()->getMock(),
160  $this->getMockBuilder(ilDBStatement::class)->disableAutoReturnValueGeneration()->getMock()
161  );
162  $ilDB->expects($this->exactly(4))->method("fetchAssoc")->willReturn(
163  ["data" => "Testdata"],
164  [],
165  ["data" => "Testdata"],
166  []
167  );
168  $ilDB->expects($this->exactly(1))->method("fetchObject")->willReturn((object) array(
169  'data' => "Testdata"
170  ));
171 
172  $ilDB->method("manipulate")->withConsecutive(
173  ['DELETE FROM usr_sess_istorage WHERE session_id = 123456'],
174  ['DELETE FROM usr_session WHERE session_id = e10adc3949ba59abbe56e057f20f883e'],
175  ['DELETE FROM usr_session WHERE user_id = e10adc3949ba59abbe56e057f20f883e']
176  )->
177  willReturnOnConsecutiveCalls(
178  1,
179  1,
180  1
181  );
182 
183  $result = "";
184  ilSession::_writeData("123456", "Testdata");
185  if (ilSession::_exists("123456")) {
186  $result .= "exists-";
187  }
188  if (ilSession::_getData("123456") === "Testdata") {
189  $result .= "write-get-";
190  }
191  $duplicate = ilSession::_duplicate("123456");
192  if (ilSession::_getData($duplicate) === "Testdata") {
193  $result .= "duplicate-";
194  }
195  ilSession::_destroy("123456");
196  if (!ilSession::_exists("123456")) {
197  $result .= "destroy-";
198  }
200  if (ilSession::_exists($duplicate)) {
201  $result .= "destroyExp-";
202  }
203 
205  if (!ilSession::_exists($duplicate)) {
206  $result .= "destroyByUser-";
207  }
208  $this->assertEquals("exists-write-get-duplicate-destroy-destroyExp-destroyByUser-", $result);
209  }
210 
211  public function testPasswordAssisstanceSession(): void
212  {
213  global $DIC;
214 
215  $ilUser = $DIC['ilUser'];
216 
217  $result = "";
218  $this->markTestIncomplete(
219  'This test has not been implemented yet.'
220  );
221  return;
222  // write session
223  db_pwassist_session_write("12345", 60, $ilUser->getId());
224 
225  // find
227  if ($res["pwassist_id"] === "12345") {
228  $result .= "find-";
229  }
230 
231  // read
232  $res = db_pwassist_session_read("12345");
233  if ((int) $res["user_id"] === $ilUser->getId()) {
234  $result .= "read-";
235  }
236 
237  // destroy
239  $res = db_pwassist_session_read("12345");
240  if (!$res) {
241  $result .= "destroy-";
242  }
243 
245 
246  $this->assertEquals("find-read-destroy-", $result);// TODO PHP8-REVIEW This method does not exists (because this class has no parent class)
247  }
248 }
setGlobalVariable(string $name, $value)
$res
Definition: ltiservices.php:69
static _duplicate(string $a_session_id)
Duplicate session.
static _destroyByUserId(int $a_user_id)
Destroy session.
db_pwassist_session_destroy($pwassist_id)
destroy session
$c
Definition: cli.php:38
static _getData(string $a_session_id)
Get session data from table.
db_pwassist_session_read($pwassist_id)
static _exists(string $a_session_id)
Check whether session exists.
Class ChatMainBarProvider .
const SESSION_HANDLING_FIXED
static _destroyExpiredSessions()
Destroy expired sessions.
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:31
global $DIC
Definition: feed.php:28
if($format !==null) $name
Definition: metadata.php:247
db_pwassist_session_find($user_id)
Class ilSessionTest.
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
static _destroy($a_session_id, ?int $a_closing_context=null, $a_expired_at=null)
Destroy session.
db_pwassist_session_gc()
removes all expired sessions
$ilUser
Definition: imgupload.php:34
testPasswordAssisstanceSession()
$dic
Definition: result.php:32
db_pwassist_session_write($pwassist_id, $maxlifetime, $user_id)
Writes serialized session data to the database.