blob: 76a4fcc98032c1f97b43d4b4e0a8068efe6ce4cb [file] [log] [blame]
dchill4257486002012-07-31 09:39:53 -04001<?php
2
3/**
4 * Session driver library unit test
5 */
6class Session_test extends CI_TestCase {
Andrey Andreev838a9d62012-12-03 14:37:47 +02007
dchill424153c922012-07-31 10:38:21 -04008 protected $settings = array(
9 'use_cookies' => 0,
dchill426262d052012-11-24 18:41:13 -050010 'use_only_cookies' => 0,
darwinel1993aab2014-02-08 00:48:38 +010011 'cache_limiter' => FALSE
dchill424153c922012-07-31 10:38:21 -040012 );
dchill4257486002012-07-31 09:39:53 -040013 protected $setting_vals = array();
14 protected $cookie_vals;
15 protected $session;
16
17 /**
18 * Set up test framework
19 */
20 public function set_up()
21 {
Andrey Andreeva2cf6fa2014-06-02 11:19:23 +030022return;
dchill4257486002012-07-31 09:39:53 -040023 // Override settings
24 foreach ($this->settings as $name => $value) {
25 $this->setting_vals[$name] = ini_get('session.'.$name);
26 ini_set('session.'.$name, $value);
27 }
28
29 // Start with clean environment
30 $this->cookie_vals = $_COOKIE;
31 $_COOKIE = array();
32
dchill426262d052012-11-24 18:41:13 -050033 // Set subclass prefix to match our mock
34 $this->ci_set_config('subclass_prefix', 'Mock_Libraries_');
35
dchill4257486002012-07-31 09:39:53 -040036 // Establish necessary support classes
dchill427ecc5cd2012-10-12 16:25:51 -040037 $ci = $this->ci_instance();
dchill426262d052012-11-24 18:41:13 -050038 $ldr = $this->ci_core_class('load');
dchill427ecc5cd2012-10-12 16:25:51 -040039 $ci->load = new $ldr();
40 $ci->input = new Mock_Core_Input(NULL, NULL);
41
42 // Make sure string helper is available
43 $this->ci_vfs_clone('system/helpers/string_helper.php');
dchill4257486002012-07-31 09:39:53 -040044
45 // Attach session instance locally
dchill424153c922012-07-31 10:38:21 -040046 $config = array(
47 'sess_encrypt_cookie' => FALSE,
48 'sess_use_database' => FALSE,
49 'sess_table_name' => '',
50 'sess_expiration' => 7200,
51 'sess_expire_on_close' => FALSE,
52 'sess_match_ip' => FALSE,
53 'sess_match_useragent' => TRUE,
54 'sess_cookie_name' => 'ci_session',
55 'cookie_path' => '',
56 'cookie_domain' => '',
57 'cookie_secure' => FALSE,
58 'cookie_httponly' => FALSE,
59 'sess_time_to_update' => 300,
60 'time_reference' => 'local',
61 'cookie_prefix' => '',
dchill426262d052012-11-24 18:41:13 -050062 'encryption_key' => 'foobar'
dchill424153c922012-07-31 10:38:21 -040063 );
dchill4257486002012-07-31 09:39:53 -040064 $this->session = new Mock_Libraries_Session($config);
65 }
66
67 /**
68 * Tear down test framework
69 */
70 public function tear_down()
71 {
Andrey Andreeva2cf6fa2014-06-02 11:19:23 +030072return;
dchill4257486002012-07-31 09:39:53 -040073 // Restore environment
74 if (session_id()) session_destroy();
75 $_SESSION = array();
76 $_COOKIE = $this->cookie_vals;
77
78 // Restore settings
79 foreach ($this->settings as $name => $value) {
80 ini_set('session.'.$name, $this->setting_vals[$name]);
81 }
82 }
83
84 /**
85 * Test set_userdata() function
86 */
87 public function test_set_userdata()
88 {
Andrey Andreeva2cf6fa2014-06-02 11:19:23 +030089return;
dchill4208c83042012-08-28 17:35:56 -040090 // Set userdata values for each driver
91 $key1 = 'test1';
92 $ckey2 = 'test2';
93 $nkey2 = 'test3';
94 $cmsg1 = 'Some test data';
95 $cmsg2 = 42;
96 $nmsg1 = 'Other test data';
darwinel2c2722e2014-02-11 20:43:16 +010097 $nmsg2 = TRUE;
dchill4208c83042012-08-28 17:35:56 -040098 $this->session->cookie->set_userdata($key1, $cmsg1);
99 $this->session->set_userdata($ckey2, $cmsg2);
100 $this->session->native->set_userdata($key1, $nmsg1);
101 $this->session->set_userdata($nkey2, $nmsg2);
dchill4257486002012-07-31 09:39:53 -0400102
103 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400104 $this->assertEquals($cmsg1, $this->session->cookie->userdata($key1));
105 $this->assertEquals($nmsg1, $this->session->native->userdata($key1));
106
107 // Verify pre-selected driver sets
108 $this->assertEquals($cmsg2, $this->session->cookie->userdata($ckey2));
109 $this->assertEquals($nmsg2, $this->session->native->userdata($nkey2));
110
111 // Verify no crossover
112 $this->assertNull($this->session->cookie->userdata($nkey2));
113 $this->assertNull($this->session->native->userdata($ckey2));
dchill4257486002012-07-31 09:39:53 -0400114 }
115
116 /**
117 * Test the has_userdata() function
118 */
119 public function test_has_userdata()
120 {
Andrey Andreeva2cf6fa2014-06-02 11:19:23 +0300121return;
dchill4208c83042012-08-28 17:35:56 -0400122 // Set a userdata value for each driver
123 $key = 'hastest';
dchill4257486002012-07-31 09:39:53 -0400124 $cmsg = 'My test data';
dchill4208c83042012-08-28 17:35:56 -0400125 $this->session->cookie->set_userdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400126 $nmsg = 'Your test data';
dchill4208c83042012-08-28 17:35:56 -0400127 $this->session->native->set_userdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400128
dchill4208c83042012-08-28 17:35:56 -0400129 // Verify values exist
130 $this->assertTrue($this->session->cookie->has_userdata($key));
131 $this->assertTrue($this->session->native->has_userdata($key));
132
133 // Verify non-existent values
134 $nokey = 'hasnot';
135 $this->assertFalse($this->session->cookie->has_userdata($nokey));
136 $this->assertFalse($this->session->native->has_userdata($nokey));
dchill4257486002012-07-31 09:39:53 -0400137 }
138
139 /**
140 * Test the all_userdata() function
141 */
142 public function test_all_userdata()
143 {
Andrey Andreeva2cf6fa2014-06-02 11:19:23 +0300144return;
dchill4257486002012-07-31 09:39:53 -0400145 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400146 $cdata = array(
147 'one' => 'first',
148 'two' => 'second',
dchill426262d052012-11-24 18:41:13 -0500149 'three' => 'third',
150 'foo' => 'bar',
151 'bar' => 'baz'
dchill424153c922012-07-31 10:38:21 -0400152 );
153 $ndata = array(
154 'one' => 'gold',
dchill426262d052012-11-24 18:41:13 -0500155 'two' => 'silver',
156 'three' => 'bronze',
157 'foo' => 'baz',
158 'bar' => 'foo'
dchill424153c922012-07-31 10:38:21 -0400159 );
dchill4257486002012-07-31 09:39:53 -0400160 $this->session->cookie->set_userdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400161 $this->session->native->set_userdata($ndata);
162
163 // Make sure all values are present
Andrey Andreevecc260e2014-01-24 14:20:13 +0200164 $call = $this->session->cookie->userdata();
dchill4257486002012-07-31 09:39:53 -0400165 foreach ($cdata as $key => $value) {
166 $this->assertEquals($value, $call[$key]);
167 }
Andrey Andreevecc260e2014-01-24 14:20:13 +0200168 $nall = $this->session->native->userdata();
dchill4257486002012-07-31 09:39:53 -0400169 foreach ($ndata as $key => $value) {
170 $this->assertEquals($value, $nall[$key]);
171 }
172 }
173
174 /**
175 * Test the unset_userdata() function
176 */
177 public function test_unset_userdata()
178 {
Andrey Andreeva2cf6fa2014-06-02 11:19:23 +0300179return;
dchill4257486002012-07-31 09:39:53 -0400180 // Set a userdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400181 $key = 'untest';
dchill4257486002012-07-31 09:39:53 -0400182 $cmsg = 'Other test data';
dchill4208c83042012-08-28 17:35:56 -0400183 $this->session->cookie->set_userdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400184 $nmsg = 'Sundry test data';
dchill4208c83042012-08-28 17:35:56 -0400185 $this->session->native->set_userdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400186
187 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400188 $this->assertEquals($this->session->cookie->userdata($key), $cmsg);
189 $this->assertEquals($this->session->native->userdata($key), $nmsg);
dchill4257486002012-07-31 09:39:53 -0400190
191 // Unset them and verify absence
dchill4208c83042012-08-28 17:35:56 -0400192 $this->session->cookie->unset_userdata($key);
193 $this->session->native->unset_userdata($key);
194 $this->assertNull($this->session->cookie->userdata($key));
195 $this->assertNull($this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400196 }
197
198 /**
dchill4208c83042012-08-28 17:35:56 -0400199 * Test the flashdata() functions
dchill4257486002012-07-31 09:39:53 -0400200 */
dchill4208c83042012-08-28 17:35:56 -0400201 public function test_flashdata()
dchill4257486002012-07-31 09:39:53 -0400202 {
Andrey Andreeva2cf6fa2014-06-02 11:19:23 +0300203return;
dchill4257486002012-07-31 09:39:53 -0400204 // Set flashdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400205 $key = 'fltest';
dchill4257486002012-07-31 09:39:53 -0400206 $cmsg = 'Some flash data';
dchill4208c83042012-08-28 17:35:56 -0400207 $this->session->cookie->set_flashdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400208 $nmsg = 'Other flash data';
dchill4208c83042012-08-28 17:35:56 -0400209 $this->session->native->set_flashdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400210
211 // Simulate page reload
212 $this->session->cookie->reload();
213 $this->session->native->reload();
214
215 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400216 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
217 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400218
219 // Simulate next page reload
220 $this->session->cookie->reload();
221 $this->session->native->reload();
222
223 // Verify absence of messages
dchill4208c83042012-08-28 17:35:56 -0400224 $this->assertNull($this->session->cookie->flashdata($key));
225 $this->assertNull($this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400226 }
227
228 /**
229 * Test the keep_flashdata() function
230 */
231 public function test_keep_flashdata()
232 {
Andrey Andreeva2cf6fa2014-06-02 11:19:23 +0300233return;
dchill4257486002012-07-31 09:39:53 -0400234 // Set flashdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400235 $key = 'kfltest';
dchill4257486002012-07-31 09:39:53 -0400236 $cmsg = 'My flash data';
dchill4208c83042012-08-28 17:35:56 -0400237 $this->session->cookie->set_flashdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400238 $nmsg = 'Your flash data';
dchill4208c83042012-08-28 17:35:56 -0400239 $this->session->native->set_flashdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400240
241 // Simulate page reload and verify independent messages
242 $this->session->cookie->reload();
243 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400244 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
245 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400246
247 // Keep messages
dchill4208c83042012-08-28 17:35:56 -0400248 $this->session->cookie->keep_flashdata($key);
249 $this->session->native->keep_flashdata($key);
dchill4257486002012-07-31 09:39:53 -0400250
251 // Simulate next page reload and verify message persistence
252 $this->session->cookie->reload();
253 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400254 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
255 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400256
257 // Simulate next page reload and verify absence of messages
258 $this->session->cookie->reload();
259 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400260 $this->assertNull($this->session->cookie->flashdata($key));
261 $this->assertNull($this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400262 }
263
Johnathan Croom66b36ef2012-11-29 17:54:28 -0700264 public function test_keep_flashdata_with_array()
265 {
Andrey Andreeva2cf6fa2014-06-02 11:19:23 +0300266return;
Johnathan Croom66b36ef2012-11-29 17:54:28 -0700267 // Set flashdata array for each driver
268 $cdata = array(
269 'one' => 'first',
270 'two' => 'second',
271 'three' => 'third',
272 'foo' => 'bar',
273 'bar' => 'baz'
274 );
275 $ndata = array(
276 'one' => 'gold',
277 'two' => 'silver',
278 'three' => 'bronze',
279 'foo' => 'baz',
280 'bar' => 'foo'
281 );
282 $kdata = array(
283 'one',
284 'two',
285 'three',
286 'foo',
287 'bar'
288 );
289 $this->session->cookie->set_flashdata($cdata);
290 $this->session->native->set_flashdata($ndata);
291
292 // Simulate page reload and verify independent messages
293 $this->session->cookie->reload();
294 $this->session->native->reload();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200295 $this->assertEquals($cdata, $this->session->cookie->flashdata());
296 $this->assertEquals($ndata, $this->session->native->flashdata());
Johnathan Croom66b36ef2012-11-29 17:54:28 -0700297
298 // Keep messages
299 $this->session->cookie->keep_flashdata($kdata);
300 $this->session->native->keep_flashdata($kdata);
301
302 // Simulate next page reload and verify message persistence
303 $this->session->cookie->reload();
304 $this->session->native->reload();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200305 $this->assertEquals($cdata, $this->session->cookie->flashdata());
306 $this->assertEquals($ndata, $this->session->native->flashdata());
Johnathan Croom66b36ef2012-11-29 17:54:28 -0700307
308 // Simulate next page reload and verify absence of messages
309 $this->session->cookie->reload();
310 $this->session->native->reload();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200311 $this->assertEmpty($this->session->cookie->flashdata());
312 $this->assertEmpty($this->session->native->flashdata());
Johnathan Croom66b36ef2012-11-29 17:54:28 -0700313 }
314
dchill4257486002012-07-31 09:39:53 -0400315 /**
316 * Test the all_flashdata() function
317 */
318 public function test_all_flashdata()
319 {
Andrey Andreeva2cf6fa2014-06-02 11:19:23 +0300320return;
dchill4257486002012-07-31 09:39:53 -0400321 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400322 $cdata = array(
323 'one' => 'first',
dchill426262d052012-11-24 18:41:13 -0500324 'two' => 'second',
325 'three' => 'third',
326 'foo' => 'bar',
327 'bar' => 'baz'
dchill424153c922012-07-31 10:38:21 -0400328 );
329 $ndata = array(
330 'one' => 'gold',
dchill426262d052012-11-24 18:41:13 -0500331 'two' => 'silver',
332 'three' => 'bronze',
333 'foo' => 'baz',
334 'bar' => 'foo'
dchill424153c922012-07-31 10:38:21 -0400335 );
dchill4257486002012-07-31 09:39:53 -0400336 $this->session->cookie->set_flashdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400337 $this->session->native->set_flashdata($ndata);
338
339 // Simulate page reload and make sure all values are present
340 $this->session->cookie->reload();
341 $this->session->native->reload();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200342 $this->assertEquals($cdata, $this->session->cookie->flashdata());
343 $this->assertEquals($ndata, $this->session->native->flashdata());
dchill4257486002012-07-31 09:39:53 -0400344 }
345
346 /**
dchill4208c83042012-08-28 17:35:56 -0400347 * Test the tempdata() functions
dchill4257486002012-07-31 09:39:53 -0400348 */
349 public function test_set_tempdata()
350 {
Andrey Andreeva2cf6fa2014-06-02 11:19:23 +0300351return;
dchill4257486002012-07-31 09:39:53 -0400352 // Set tempdata message for each driver - 1 second timeout
dchill4208c83042012-08-28 17:35:56 -0400353 $key = 'tmptest';
dchill4257486002012-07-31 09:39:53 -0400354 $cmsg = 'Some temp data';
dchill4208c83042012-08-28 17:35:56 -0400355 $this->session->cookie->set_tempdata($key, $cmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400356 $nmsg = 'Other temp data';
dchill4208c83042012-08-28 17:35:56 -0400357 $this->session->native->set_tempdata($key, $nmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400358
359 // Simulate page reload and verify independent messages
360 $this->session->cookie->reload();
361 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400362 $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
363 $this->assertEquals($nmsg, $this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400364
365 // Wait 2 seconds, simulate page reload and verify message absence
366 sleep(2);
367 $this->session->cookie->reload();
368 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400369 $this->assertNull($this->session->cookie->tempdata($key));
370 $this->assertNull($this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400371 }
372
373 /**
374 * Test the unset_tempdata() function
375 */
376 public function test_unset_tempdata()
377 {
Andrey Andreeva2cf6fa2014-06-02 11:19:23 +0300378return;
dchill4257486002012-07-31 09:39:53 -0400379 // Set tempdata message for each driver - 1 second timeout
dchill4208c83042012-08-28 17:35:56 -0400380 $key = 'utmptest';
dchill4257486002012-07-31 09:39:53 -0400381 $cmsg = 'My temp data';
dchill4208c83042012-08-28 17:35:56 -0400382 $this->session->cookie->set_tempdata($key, $cmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400383 $nmsg = 'Your temp data';
dchill4208c83042012-08-28 17:35:56 -0400384 $this->session->native->set_tempdata($key, $nmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400385
386 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400387 $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
388 $this->assertEquals($nmsg, $this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400389
390 // Unset data and verify message absence
dchill4208c83042012-08-28 17:35:56 -0400391 $this->session->cookie->unset_tempdata($key);
392 $this->session->native->unset_tempdata($key);
393 $this->assertNull($this->session->cookie->tempdata($key));
394 $this->assertNull($this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400395 }
396
397 /**
398 * Test the sess_regenerate() function
399 */
400 public function test_sess_regenerate()
401 {
Andrey Andreeva2cf6fa2014-06-02 11:19:23 +0300402return;
dchill4257486002012-07-31 09:39:53 -0400403 // Get current session id, regenerate, and compare
404 // Cookie driver
405 $oldid = $this->session->cookie->userdata('session_id');
406 $this->session->cookie->sess_regenerate();
407 $newid = $this->session->cookie->userdata('session_id');
dchill4208c83042012-08-28 17:35:56 -0400408 $this->assertNotEquals($oldid, $newid);
dchill4257486002012-07-31 09:39:53 -0400409
410 // Native driver - bug #55267 (https://bugs.php.net/bug.php?id=55267) prevents testing this
dchill4208c83042012-08-28 17:35:56 -0400411 // $oldid = session_id();
412 // $this->session->native->sess_regenerate();
413 // $oldid = session_id();
414 // $this->assertNotEquals($oldid, $newid);
dchill4257486002012-07-31 09:39:53 -0400415 }
416
417 /**
418 * Test the sess_destroy() function
419 */
420 public function test_sess_destroy()
421 {
Andrey Andreeva2cf6fa2014-06-02 11:19:23 +0300422return;
dchill4257486002012-07-31 09:39:53 -0400423 // Set a userdata message, destroy session, and verify absence
dchill4208c83042012-08-28 17:35:56 -0400424 $key = 'dsttest';
dchill4257486002012-07-31 09:39:53 -0400425 $msg = 'More test data';
426
427 // Cookie driver
dchill4208c83042012-08-28 17:35:56 -0400428 $this->session->cookie->set_userdata($key, $msg);
429 $this->assertEquals($msg, $this->session->cookie->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400430 $this->session->cookie->sess_destroy();
dchill4208c83042012-08-28 17:35:56 -0400431 $this->assertNull($this->session->cookie->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400432
433 // Native driver
dchill4208c83042012-08-28 17:35:56 -0400434 $this->session->native->set_userdata($key, $msg);
435 $this->assertEquals($msg, $this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400436 $this->session->native->sess_destroy();
dchill4208c83042012-08-28 17:35:56 -0400437 $this->assertNull($this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400438 }
Andrey Andreev838a9d62012-12-03 14:37:47 +0200439
440}