blob: cff0fdbd87fdb891f3addefea7ef3184f1d72464 [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,
11 '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 {
22 // Override settings
23 foreach ($this->settings as $name => $value) {
24 $this->setting_vals[$name] = ini_get('session.'.$name);
25 ini_set('session.'.$name, $value);
26 }
27
28 // Start with clean environment
29 $this->cookie_vals = $_COOKIE;
30 $_COOKIE = array();
31
dchill426262d052012-11-24 18:41:13 -050032 // Set subclass prefix to match our mock
33 $this->ci_set_config('subclass_prefix', 'Mock_Libraries_');
34
dchill4257486002012-07-31 09:39:53 -040035 // Establish necessary support classes
dchill427ecc5cd2012-10-12 16:25:51 -040036 $ci = $this->ci_instance();
dchill426262d052012-11-24 18:41:13 -050037 $ldr = $this->ci_core_class('load');
dchill427ecc5cd2012-10-12 16:25:51 -040038 $ci->load = new $ldr();
39 $ci->input = new Mock_Core_Input(NULL, NULL);
40
41 // Make sure string helper is available
42 $this->ci_vfs_clone('system/helpers/string_helper.php');
dchill4257486002012-07-31 09:39:53 -040043
44 // Attach session instance locally
dchill424153c922012-07-31 10:38:21 -040045 $config = array(
46 'sess_encrypt_cookie' => FALSE,
47 'sess_use_database' => FALSE,
48 'sess_table_name' => '',
49 'sess_expiration' => 7200,
50 'sess_expire_on_close' => FALSE,
51 'sess_match_ip' => FALSE,
52 'sess_match_useragent' => TRUE,
53 'sess_cookie_name' => 'ci_session',
54 'cookie_path' => '',
55 'cookie_domain' => '',
56 'cookie_secure' => FALSE,
57 'cookie_httponly' => FALSE,
58 'sess_time_to_update' => 300,
59 'time_reference' => 'local',
60 'cookie_prefix' => '',
dchill426262d052012-11-24 18:41:13 -050061 'encryption_key' => 'foobar'
dchill424153c922012-07-31 10:38:21 -040062 );
dchill4257486002012-07-31 09:39:53 -040063 $this->session = new Mock_Libraries_Session($config);
64 }
65
66 /**
67 * Tear down test framework
68 */
69 public function tear_down()
70 {
71 // Restore environment
72 if (session_id()) session_destroy();
73 $_SESSION = array();
74 $_COOKIE = $this->cookie_vals;
75
76 // Restore settings
77 foreach ($this->settings as $name => $value) {
78 ini_set('session.'.$name, $this->setting_vals[$name]);
79 }
80 }
81
82 /**
83 * Test set_userdata() function
84 */
85 public function test_set_userdata()
86 {
dchill4208c83042012-08-28 17:35:56 -040087 // Set userdata values for each driver
88 $key1 = 'test1';
89 $ckey2 = 'test2';
90 $nkey2 = 'test3';
91 $cmsg1 = 'Some test data';
92 $cmsg2 = 42;
93 $nmsg1 = 'Other test data';
94 $nmsg2 = true;
95 $this->session->cookie->set_userdata($key1, $cmsg1);
96 $this->session->set_userdata($ckey2, $cmsg2);
97 $this->session->native->set_userdata($key1, $nmsg1);
98 $this->session->set_userdata($nkey2, $nmsg2);
dchill4257486002012-07-31 09:39:53 -040099
100 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400101 $this->assertEquals($cmsg1, $this->session->cookie->userdata($key1));
102 $this->assertEquals($nmsg1, $this->session->native->userdata($key1));
103
104 // Verify pre-selected driver sets
105 $this->assertEquals($cmsg2, $this->session->cookie->userdata($ckey2));
106 $this->assertEquals($nmsg2, $this->session->native->userdata($nkey2));
107
108 // Verify no crossover
109 $this->assertNull($this->session->cookie->userdata($nkey2));
110 $this->assertNull($this->session->native->userdata($ckey2));
dchill4257486002012-07-31 09:39:53 -0400111 }
112
113 /**
114 * Test the has_userdata() function
115 */
116 public function test_has_userdata()
117 {
dchill4208c83042012-08-28 17:35:56 -0400118 // Set a userdata value for each driver
119 $key = 'hastest';
dchill4257486002012-07-31 09:39:53 -0400120 $cmsg = 'My test data';
dchill4208c83042012-08-28 17:35:56 -0400121 $this->session->cookie->set_userdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400122 $nmsg = 'Your test data';
dchill4208c83042012-08-28 17:35:56 -0400123 $this->session->native->set_userdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400124
dchill4208c83042012-08-28 17:35:56 -0400125 // Verify values exist
126 $this->assertTrue($this->session->cookie->has_userdata($key));
127 $this->assertTrue($this->session->native->has_userdata($key));
128
129 // Verify non-existent values
130 $nokey = 'hasnot';
131 $this->assertFalse($this->session->cookie->has_userdata($nokey));
132 $this->assertFalse($this->session->native->has_userdata($nokey));
dchill4257486002012-07-31 09:39:53 -0400133 }
134
135 /**
136 * Test the all_userdata() function
137 */
138 public function test_all_userdata()
139 {
140 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400141 $cdata = array(
142 'one' => 'first',
143 'two' => 'second',
dchill426262d052012-11-24 18:41:13 -0500144 'three' => 'third',
145 'foo' => 'bar',
146 'bar' => 'baz'
dchill424153c922012-07-31 10:38:21 -0400147 );
148 $ndata = array(
149 'one' => 'gold',
dchill426262d052012-11-24 18:41:13 -0500150 'two' => 'silver',
151 'three' => 'bronze',
152 'foo' => 'baz',
153 'bar' => 'foo'
dchill424153c922012-07-31 10:38:21 -0400154 );
dchill4257486002012-07-31 09:39:53 -0400155 $this->session->cookie->set_userdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400156 $this->session->native->set_userdata($ndata);
157
158 // Make sure all values are present
Andrey Andreevecc260e2014-01-24 14:20:13 +0200159 $call = $this->session->cookie->userdata();
dchill4257486002012-07-31 09:39:53 -0400160 foreach ($cdata as $key => $value) {
161 $this->assertEquals($value, $call[$key]);
162 }
Andrey Andreevecc260e2014-01-24 14:20:13 +0200163 $nall = $this->session->native->userdata();
dchill4257486002012-07-31 09:39:53 -0400164 foreach ($ndata as $key => $value) {
165 $this->assertEquals($value, $nall[$key]);
166 }
167 }
168
169 /**
170 * Test the unset_userdata() function
171 */
172 public function test_unset_userdata()
173 {
174 // Set a userdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400175 $key = 'untest';
dchill4257486002012-07-31 09:39:53 -0400176 $cmsg = 'Other test data';
dchill4208c83042012-08-28 17:35:56 -0400177 $this->session->cookie->set_userdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400178 $nmsg = 'Sundry test data';
dchill4208c83042012-08-28 17:35:56 -0400179 $this->session->native->set_userdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400180
181 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400182 $this->assertEquals($this->session->cookie->userdata($key), $cmsg);
183 $this->assertEquals($this->session->native->userdata($key), $nmsg);
dchill4257486002012-07-31 09:39:53 -0400184
185 // Unset them and verify absence
dchill4208c83042012-08-28 17:35:56 -0400186 $this->session->cookie->unset_userdata($key);
187 $this->session->native->unset_userdata($key);
188 $this->assertNull($this->session->cookie->userdata($key));
189 $this->assertNull($this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400190 }
191
192 /**
dchill4208c83042012-08-28 17:35:56 -0400193 * Test the flashdata() functions
dchill4257486002012-07-31 09:39:53 -0400194 */
dchill4208c83042012-08-28 17:35:56 -0400195 public function test_flashdata()
dchill4257486002012-07-31 09:39:53 -0400196 {
197 // Set flashdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400198 $key = 'fltest';
dchill4257486002012-07-31 09:39:53 -0400199 $cmsg = 'Some flash data';
dchill4208c83042012-08-28 17:35:56 -0400200 $this->session->cookie->set_flashdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400201 $nmsg = 'Other flash data';
dchill4208c83042012-08-28 17:35:56 -0400202 $this->session->native->set_flashdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400203
204 // Simulate page reload
205 $this->session->cookie->reload();
206 $this->session->native->reload();
207
208 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400209 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
210 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400211
212 // Simulate next page reload
213 $this->session->cookie->reload();
214 $this->session->native->reload();
215
216 // Verify absence of messages
dchill4208c83042012-08-28 17:35:56 -0400217 $this->assertNull($this->session->cookie->flashdata($key));
218 $this->assertNull($this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400219 }
220
221 /**
222 * Test the keep_flashdata() function
223 */
224 public function test_keep_flashdata()
225 {
226 // Set flashdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400227 $key = 'kfltest';
dchill4257486002012-07-31 09:39:53 -0400228 $cmsg = 'My flash data';
dchill4208c83042012-08-28 17:35:56 -0400229 $this->session->cookie->set_flashdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400230 $nmsg = 'Your flash data';
dchill4208c83042012-08-28 17:35:56 -0400231 $this->session->native->set_flashdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400232
233 // Simulate page reload and verify independent messages
234 $this->session->cookie->reload();
235 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400236 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
237 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400238
239 // Keep messages
dchill4208c83042012-08-28 17:35:56 -0400240 $this->session->cookie->keep_flashdata($key);
241 $this->session->native->keep_flashdata($key);
dchill4257486002012-07-31 09:39:53 -0400242
243 // Simulate next page reload and verify message persistence
244 $this->session->cookie->reload();
245 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400246 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
247 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400248
249 // Simulate next page reload and verify absence of messages
250 $this->session->cookie->reload();
251 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400252 $this->assertNull($this->session->cookie->flashdata($key));
253 $this->assertNull($this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400254 }
255
Johnathan Croom66b36ef2012-11-29 17:54:28 -0700256 public function test_keep_flashdata_with_array()
257 {
258 // Set flashdata array for each driver
259 $cdata = array(
260 'one' => 'first',
261 'two' => 'second',
262 'three' => 'third',
263 'foo' => 'bar',
264 'bar' => 'baz'
265 );
266 $ndata = array(
267 'one' => 'gold',
268 'two' => 'silver',
269 'three' => 'bronze',
270 'foo' => 'baz',
271 'bar' => 'foo'
272 );
273 $kdata = array(
274 'one',
275 'two',
276 'three',
277 'foo',
278 'bar'
279 );
280 $this->session->cookie->set_flashdata($cdata);
281 $this->session->native->set_flashdata($ndata);
282
283 // Simulate page reload and verify independent messages
284 $this->session->cookie->reload();
285 $this->session->native->reload();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200286 $this->assertEquals($cdata, $this->session->cookie->flashdata());
287 $this->assertEquals($ndata, $this->session->native->flashdata());
Johnathan Croom66b36ef2012-11-29 17:54:28 -0700288
289 // Keep messages
290 $this->session->cookie->keep_flashdata($kdata);
291 $this->session->native->keep_flashdata($kdata);
292
293 // Simulate next page reload and verify message persistence
294 $this->session->cookie->reload();
295 $this->session->native->reload();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200296 $this->assertEquals($cdata, $this->session->cookie->flashdata());
297 $this->assertEquals($ndata, $this->session->native->flashdata());
Johnathan Croom66b36ef2012-11-29 17:54:28 -0700298
299 // Simulate next page reload and verify absence of messages
300 $this->session->cookie->reload();
301 $this->session->native->reload();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200302 $this->assertEmpty($this->session->cookie->flashdata());
303 $this->assertEmpty($this->session->native->flashdata());
Johnathan Croom66b36ef2012-11-29 17:54:28 -0700304 }
305
dchill4257486002012-07-31 09:39:53 -0400306 /**
307 * Test the all_flashdata() function
308 */
309 public function test_all_flashdata()
310 {
311 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400312 $cdata = array(
313 'one' => 'first',
dchill426262d052012-11-24 18:41:13 -0500314 'two' => 'second',
315 'three' => 'third',
316 'foo' => 'bar',
317 'bar' => 'baz'
dchill424153c922012-07-31 10:38:21 -0400318 );
319 $ndata = array(
320 'one' => 'gold',
dchill426262d052012-11-24 18:41:13 -0500321 'two' => 'silver',
322 'three' => 'bronze',
323 'foo' => 'baz',
324 'bar' => 'foo'
dchill424153c922012-07-31 10:38:21 -0400325 );
dchill4257486002012-07-31 09:39:53 -0400326 $this->session->cookie->set_flashdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400327 $this->session->native->set_flashdata($ndata);
328
329 // Simulate page reload and make sure all values are present
330 $this->session->cookie->reload();
331 $this->session->native->reload();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200332 $this->assertEquals($cdata, $this->session->cookie->flashdata());
333 $this->assertEquals($ndata, $this->session->native->flashdata());
dchill4257486002012-07-31 09:39:53 -0400334 }
335
336 /**
dchill4208c83042012-08-28 17:35:56 -0400337 * Test the tempdata() functions
dchill4257486002012-07-31 09:39:53 -0400338 */
339 public function test_set_tempdata()
340 {
341 // Set tempdata message for each driver - 1 second timeout
dchill4208c83042012-08-28 17:35:56 -0400342 $key = 'tmptest';
dchill4257486002012-07-31 09:39:53 -0400343 $cmsg = 'Some temp data';
dchill4208c83042012-08-28 17:35:56 -0400344 $this->session->cookie->set_tempdata($key, $cmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400345 $nmsg = 'Other temp data';
dchill4208c83042012-08-28 17:35:56 -0400346 $this->session->native->set_tempdata($key, $nmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400347
348 // Simulate page reload and verify independent messages
349 $this->session->cookie->reload();
350 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400351 $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
352 $this->assertEquals($nmsg, $this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400353
354 // Wait 2 seconds, simulate page reload and verify message absence
355 sleep(2);
356 $this->session->cookie->reload();
357 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400358 $this->assertNull($this->session->cookie->tempdata($key));
359 $this->assertNull($this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400360 }
361
362 /**
363 * Test the unset_tempdata() function
364 */
365 public function test_unset_tempdata()
366 {
367 // Set tempdata message for each driver - 1 second timeout
dchill4208c83042012-08-28 17:35:56 -0400368 $key = 'utmptest';
dchill4257486002012-07-31 09:39:53 -0400369 $cmsg = 'My temp data';
dchill4208c83042012-08-28 17:35:56 -0400370 $this->session->cookie->set_tempdata($key, $cmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400371 $nmsg = 'Your temp data';
dchill4208c83042012-08-28 17:35:56 -0400372 $this->session->native->set_tempdata($key, $nmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400373
374 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400375 $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
376 $this->assertEquals($nmsg, $this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400377
378 // Unset data and verify message absence
dchill4208c83042012-08-28 17:35:56 -0400379 $this->session->cookie->unset_tempdata($key);
380 $this->session->native->unset_tempdata($key);
381 $this->assertNull($this->session->cookie->tempdata($key));
382 $this->assertNull($this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400383 }
384
385 /**
386 * Test the sess_regenerate() function
387 */
388 public function test_sess_regenerate()
389 {
390 // Get current session id, regenerate, and compare
391 // Cookie driver
392 $oldid = $this->session->cookie->userdata('session_id');
393 $this->session->cookie->sess_regenerate();
394 $newid = $this->session->cookie->userdata('session_id');
dchill4208c83042012-08-28 17:35:56 -0400395 $this->assertNotEquals($oldid, $newid);
dchill4257486002012-07-31 09:39:53 -0400396
397 // Native driver - bug #55267 (https://bugs.php.net/bug.php?id=55267) prevents testing this
dchill4208c83042012-08-28 17:35:56 -0400398 // $oldid = session_id();
399 // $this->session->native->sess_regenerate();
400 // $oldid = session_id();
401 // $this->assertNotEquals($oldid, $newid);
dchill4257486002012-07-31 09:39:53 -0400402 }
403
404 /**
405 * Test the sess_destroy() function
406 */
407 public function test_sess_destroy()
408 {
409 // Set a userdata message, destroy session, and verify absence
dchill4208c83042012-08-28 17:35:56 -0400410 $key = 'dsttest';
dchill4257486002012-07-31 09:39:53 -0400411 $msg = 'More test data';
412
413 // Cookie driver
dchill4208c83042012-08-28 17:35:56 -0400414 $this->session->cookie->set_userdata($key, $msg);
415 $this->assertEquals($msg, $this->session->cookie->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400416 $this->session->cookie->sess_destroy();
dchill4208c83042012-08-28 17:35:56 -0400417 $this->assertNull($this->session->cookie->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400418
419 // Native driver
dchill4208c83042012-08-28 17:35:56 -0400420 $this->session->native->set_userdata($key, $msg);
421 $this->assertEquals($msg, $this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400422 $this->session->native->sess_destroy();
dchill4208c83042012-08-28 17:35:56 -0400423 $this->assertNull($this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400424 }
Andrey Andreev838a9d62012-12-03 14:37:47 +0200425
426}