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