blob: 50d263827b826254797c3a1bf98c5d1c09fb1fe4 [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 {
dchill424153c922012-07-31 10:38:21 -04007 protected $settings = array(
8 'use_cookies' => 0,
9 'use_only_cookies' => 0,
10 'cache_limiter' => false
11 );
dchill4257486002012-07-31 09:39:53 -040012 protected $setting_vals = array();
13 protected $cookie_vals;
14 protected $session;
15
16 /**
17 * Set up test framework
18 */
19 public function set_up()
20 {
21 // Override settings
22 foreach ($this->settings as $name => $value) {
23 $this->setting_vals[$name] = ini_get('session.'.$name);
24 ini_set('session.'.$name, $value);
25 }
26
27 // Start with clean environment
28 $this->cookie_vals = $_COOKIE;
29 $_COOKIE = array();
30
31 // Establish necessary support classes
dchill4282003da2012-10-09 13:28:17 -040032 $cfg = $this->ci_core_class('cfg');
dchill4282003da2012-10-09 13:28:17 -040033 $ldr = $this->ci_core_class('load');
dchill427ecc5cd2012-10-12 16:25:51 -040034 $ci = $this->ci_instance();
35 $ci->config = new $cfg();
36 $ci->load = new $ldr();
37 $ci->input = new Mock_Core_Input(NULL, NULL);
38
39 // Make sure string helper is available
40 $this->ci_vfs_clone('system/helpers/string_helper.php');
dchill4257486002012-07-31 09:39:53 -040041
42 // Attach session instance locally
dchill424153c922012-07-31 10:38:21 -040043 $config = array(
44 'sess_encrypt_cookie' => FALSE,
45 'sess_use_database' => FALSE,
46 'sess_table_name' => '',
47 'sess_expiration' => 7200,
48 'sess_expire_on_close' => FALSE,
49 'sess_match_ip' => FALSE,
50 'sess_match_useragent' => TRUE,
51 'sess_cookie_name' => 'ci_session',
52 'cookie_path' => '',
53 'cookie_domain' => '',
54 'cookie_secure' => FALSE,
55 'cookie_httponly' => FALSE,
56 'sess_time_to_update' => 300,
57 'time_reference' => 'local',
58 'cookie_prefix' => '',
59 'encryption_key' => 'foobar',
60 'sess_valid_drivers' => array(
61 'Mock_Libraries_Session_native',
62 'Mock_Libraries_Session_cookie'
63 )
64 );
dchill4257486002012-07-31 09:39:53 -040065 $this->session = new Mock_Libraries_Session($config);
66 }
67
68 /**
69 * Tear down test framework
70 */
71 public function tear_down()
72 {
73 // 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
dchill4208c83042012-08-28 17:35:56 -040086 *
87 * @covers CI_Session::set_userdata
88 * @covers CI_Session::userdata
dchill4257486002012-07-31 09:39:53 -040089 */
90 public function test_set_userdata()
91 {
dchill4208c83042012-08-28 17:35:56 -040092 // Set userdata values for each driver
93 $key1 = 'test1';
94 $ckey2 = 'test2';
95 $nkey2 = 'test3';
96 $cmsg1 = 'Some test data';
97 $cmsg2 = 42;
98 $nmsg1 = 'Other test data';
99 $nmsg2 = true;
100 $this->session->cookie->set_userdata($key1, $cmsg1);
101 $this->session->set_userdata($ckey2, $cmsg2);
102 $this->session->native->set_userdata($key1, $nmsg1);
103 $this->session->set_userdata($nkey2, $nmsg2);
dchill4257486002012-07-31 09:39:53 -0400104
105 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400106 $this->assertEquals($cmsg1, $this->session->cookie->userdata($key1));
107 $this->assertEquals($nmsg1, $this->session->native->userdata($key1));
108
109 // Verify pre-selected driver sets
110 $this->assertEquals($cmsg2, $this->session->cookie->userdata($ckey2));
111 $this->assertEquals($nmsg2, $this->session->native->userdata($nkey2));
112
113 // Verify no crossover
114 $this->assertNull($this->session->cookie->userdata($nkey2));
115 $this->assertNull($this->session->native->userdata($ckey2));
dchill4257486002012-07-31 09:39:53 -0400116 }
117
118 /**
119 * Test the has_userdata() function
dchill4208c83042012-08-28 17:35:56 -0400120 *
121 * @covers CI_Session::has_userdata
dchill4257486002012-07-31 09:39:53 -0400122 */
123 public function test_has_userdata()
124 {
dchill4208c83042012-08-28 17:35:56 -0400125 // Set a userdata value for each driver
126 $key = 'hastest';
dchill4257486002012-07-31 09:39:53 -0400127 $cmsg = 'My test data';
dchill4208c83042012-08-28 17:35:56 -0400128 $this->session->cookie->set_userdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400129 $nmsg = 'Your test data';
dchill4208c83042012-08-28 17:35:56 -0400130 $this->session->native->set_userdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400131
dchill4208c83042012-08-28 17:35:56 -0400132 // Verify values exist
133 $this->assertTrue($this->session->cookie->has_userdata($key));
134 $this->assertTrue($this->session->native->has_userdata($key));
135
136 // Verify non-existent values
137 $nokey = 'hasnot';
138 $this->assertFalse($this->session->cookie->has_userdata($nokey));
139 $this->assertFalse($this->session->native->has_userdata($nokey));
dchill4257486002012-07-31 09:39:53 -0400140 }
141
142 /**
143 * Test the all_userdata() function
dchill4208c83042012-08-28 17:35:56 -0400144 *
145 * @covers CI_Session::all_userdata
dchill4257486002012-07-31 09:39:53 -0400146 */
147 public function test_all_userdata()
148 {
149 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400150 $cdata = array(
151 'one' => 'first',
152 'two' => 'second',
153 'three' => 'third',
154 'foo' => 'bar',
155 'bar' => 'baz'
156 );
157 $ndata = array(
158 'one' => 'gold',
159 'two' => 'silver',
160 'three' => 'bronze',
161 'foo' => 'baz',
162 'bar' => 'foo'
163 );
dchill4257486002012-07-31 09:39:53 -0400164 $this->session->cookie->set_userdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400165 $this->session->native->set_userdata($ndata);
166
167 // Make sure all values are present
168 $call = $this->session->cookie->all_userdata();
169 foreach ($cdata as $key => $value) {
170 $this->assertEquals($value, $call[$key]);
171 }
172 $nall = $this->session->native->all_userdata();
173 foreach ($ndata as $key => $value) {
174 $this->assertEquals($value, $nall[$key]);
175 }
176 }
177
178 /**
179 * Test the unset_userdata() function
dchill4208c83042012-08-28 17:35:56 -0400180 *
181 * @covers CI_Session::unset_userdata
dchill4257486002012-07-31 09:39:53 -0400182 */
183 public function test_unset_userdata()
184 {
185 // Set a userdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400186 $key = 'untest';
dchill4257486002012-07-31 09:39:53 -0400187 $cmsg = 'Other test data';
dchill4208c83042012-08-28 17:35:56 -0400188 $this->session->cookie->set_userdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400189 $nmsg = 'Sundry test data';
dchill4208c83042012-08-28 17:35:56 -0400190 $this->session->native->set_userdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400191
192 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400193 $this->assertEquals($this->session->cookie->userdata($key), $cmsg);
194 $this->assertEquals($this->session->native->userdata($key), $nmsg);
dchill4257486002012-07-31 09:39:53 -0400195
196 // Unset them and verify absence
dchill4208c83042012-08-28 17:35:56 -0400197 $this->session->cookie->unset_userdata($key);
198 $this->session->native->unset_userdata($key);
199 $this->assertNull($this->session->cookie->userdata($key));
200 $this->assertNull($this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400201 }
202
203 /**
dchill4208c83042012-08-28 17:35:56 -0400204 * Test the flashdata() functions
205 *
206 * @covers CI_Session::set_flashdata
207 * @covers CI_Session::flashdata
dchill4257486002012-07-31 09:39:53 -0400208 */
dchill4208c83042012-08-28 17:35:56 -0400209 public function test_flashdata()
dchill4257486002012-07-31 09:39:53 -0400210 {
211 // Set flashdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400212 $key = 'fltest';
dchill4257486002012-07-31 09:39:53 -0400213 $cmsg = 'Some flash data';
dchill4208c83042012-08-28 17:35:56 -0400214 $this->session->cookie->set_flashdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400215 $nmsg = 'Other flash data';
dchill4208c83042012-08-28 17:35:56 -0400216 $this->session->native->set_flashdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400217
218 // Simulate page reload
219 $this->session->cookie->reload();
220 $this->session->native->reload();
221
222 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400223 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
224 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400225
226 // Simulate next page reload
227 $this->session->cookie->reload();
228 $this->session->native->reload();
229
230 // Verify absence of messages
dchill4208c83042012-08-28 17:35:56 -0400231 $this->assertNull($this->session->cookie->flashdata($key));
232 $this->assertNull($this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400233 }
234
235 /**
236 * Test the keep_flashdata() function
dchill4208c83042012-08-28 17:35:56 -0400237 *
238 * @covers CI_Session::keep_flashdata
dchill4257486002012-07-31 09:39:53 -0400239 */
240 public function test_keep_flashdata()
241 {
242 // Set flashdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400243 $key = 'kfltest';
dchill4257486002012-07-31 09:39:53 -0400244 $cmsg = 'My flash data';
dchill4208c83042012-08-28 17:35:56 -0400245 $this->session->cookie->set_flashdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400246 $nmsg = 'Your flash data';
dchill4208c83042012-08-28 17:35:56 -0400247 $this->session->native->set_flashdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400248
249 // Simulate page reload and verify independent messages
250 $this->session->cookie->reload();
251 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400252 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
253 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400254
255 // Keep messages
dchill4208c83042012-08-28 17:35:56 -0400256 $this->session->cookie->keep_flashdata($key);
257 $this->session->native->keep_flashdata($key);
dchill4257486002012-07-31 09:39:53 -0400258
259 // Simulate next page reload and verify message persistence
260 $this->session->cookie->reload();
261 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400262 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
263 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400264
265 // Simulate next page reload and verify absence of messages
266 $this->session->cookie->reload();
267 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400268 $this->assertNull($this->session->cookie->flashdata($key));
269 $this->assertNull($this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400270 }
271
Johnathan Croom66b36ef2012-11-29 17:54:28 -0700272 public function test_keep_flashdata_with_array()
273 {
274 // Set flashdata array for each driver
275 $cdata = array(
276 'one' => 'first',
277 'two' => 'second',
278 'three' => 'third',
279 'foo' => 'bar',
280 'bar' => 'baz'
281 );
282 $ndata = array(
283 'one' => 'gold',
284 'two' => 'silver',
285 'three' => 'bronze',
286 'foo' => 'baz',
287 'bar' => 'foo'
288 );
289 $kdata = array(
290 'one',
291 'two',
292 'three',
293 'foo',
294 'bar'
295 );
296 $this->session->cookie->set_flashdata($cdata);
297 $this->session->native->set_flashdata($ndata);
298
299 // Simulate page reload and verify independent messages
300 $this->session->cookie->reload();
301 $this->session->native->reload();
302 $this->assertEquals($cdata, $this->session->cookie->all_flashdata());
303 $this->assertEquals($ndata, $this->session->native->all_flashdata());
304
305 // Keep messages
306 $this->session->cookie->keep_flashdata($kdata);
307 $this->session->native->keep_flashdata($kdata);
308
309 // Simulate next page reload and verify message persistence
310 $this->session->cookie->reload();
311 $this->session->native->reload();
312 $this->assertEquals($cdata, $this->session->cookie->all_flashdata());
313 $this->assertEquals($ndata, $this->session->native->all_flashdata());
314
315 // Simulate next page reload and verify absence of messages
316 $this->session->cookie->reload();
317 $this->session->native->reload();
318 $this->assertEmpty($this->session->cookie->all_flashdata());
319 $this->assertEmpty($this->session->native->all_flashdata());
320 }
321
dchill4257486002012-07-31 09:39:53 -0400322 /**
323 * Test the all_flashdata() function
dchill4208c83042012-08-28 17:35:56 -0400324 *
325 * @covers CI_Session::all_flashdata
dchill4257486002012-07-31 09:39:53 -0400326 */
327 public function test_all_flashdata()
328 {
329 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400330 $cdata = array(
331 'one' => 'first',
332 'two' => 'second',
333 'three' => 'third',
334 'foo' => 'bar',
335 'bar' => 'baz'
336 );
337 $ndata = array(
338 'one' => 'gold',
339 'two' => 'silver',
340 'three' => 'bronze',
341 'foo' => 'baz',
342 'bar' => 'foo'
343 );
dchill4257486002012-07-31 09:39:53 -0400344 $this->session->cookie->set_flashdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400345 $this->session->native->set_flashdata($ndata);
346
347 // Simulate page reload and make sure all values are present
348 $this->session->cookie->reload();
349 $this->session->native->reload();
350 $this->assertEquals($cdata, $this->session->cookie->all_flashdata());
351 $this->assertEquals($ndata, $this->session->native->all_flashdata());
352 }
353
354 /**
dchill4208c83042012-08-28 17:35:56 -0400355 * Test the tempdata() functions
356 *
357 * @covers CI_Session::set_tempdata
358 * @covers CI_Session::tempdata
dchill4257486002012-07-31 09:39:53 -0400359 */
360 public function test_set_tempdata()
361 {
362 // Set tempdata message for each driver - 1 second timeout
dchill4208c83042012-08-28 17:35:56 -0400363 $key = 'tmptest';
dchill4257486002012-07-31 09:39:53 -0400364 $cmsg = 'Some temp data';
dchill4208c83042012-08-28 17:35:56 -0400365 $this->session->cookie->set_tempdata($key, $cmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400366 $nmsg = 'Other temp data';
dchill4208c83042012-08-28 17:35:56 -0400367 $this->session->native->set_tempdata($key, $nmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400368
369 // Simulate page reload and verify independent messages
370 $this->session->cookie->reload();
371 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400372 $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
373 $this->assertEquals($nmsg, $this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400374
375 // Wait 2 seconds, simulate page reload and verify message absence
376 sleep(2);
377 $this->session->cookie->reload();
378 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400379 $this->assertNull($this->session->cookie->tempdata($key));
380 $this->assertNull($this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400381 }
382
383 /**
384 * Test the unset_tempdata() function
dchill4208c83042012-08-28 17:35:56 -0400385 *
386 * @covers CI_Session::unset_tempdata
dchill4257486002012-07-31 09:39:53 -0400387 */
388 public function test_unset_tempdata()
389 {
390 // Set tempdata message for each driver - 1 second timeout
dchill4208c83042012-08-28 17:35:56 -0400391 $key = 'utmptest';
dchill4257486002012-07-31 09:39:53 -0400392 $cmsg = 'My temp data';
dchill4208c83042012-08-28 17:35:56 -0400393 $this->session->cookie->set_tempdata($key, $cmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400394 $nmsg = 'Your temp data';
dchill4208c83042012-08-28 17:35:56 -0400395 $this->session->native->set_tempdata($key, $nmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400396
397 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400398 $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
399 $this->assertEquals($nmsg, $this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400400
401 // Unset data and verify message absence
dchill4208c83042012-08-28 17:35:56 -0400402 $this->session->cookie->unset_tempdata($key);
403 $this->session->native->unset_tempdata($key);
404 $this->assertNull($this->session->cookie->tempdata($key));
405 $this->assertNull($this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400406 }
407
408 /**
409 * Test the sess_regenerate() function
dchill4208c83042012-08-28 17:35:56 -0400410 *
411 * @covers CI_Session::sess_regenerate
dchill4257486002012-07-31 09:39:53 -0400412 */
413 public function test_sess_regenerate()
414 {
415 // Get current session id, regenerate, and compare
416 // Cookie driver
417 $oldid = $this->session->cookie->userdata('session_id');
418 $this->session->cookie->sess_regenerate();
419 $newid = $this->session->cookie->userdata('session_id');
dchill4208c83042012-08-28 17:35:56 -0400420 $this->assertNotEquals($oldid, $newid);
dchill4257486002012-07-31 09:39:53 -0400421
422 // Native driver - bug #55267 (https://bugs.php.net/bug.php?id=55267) prevents testing this
dchill4208c83042012-08-28 17:35:56 -0400423 // $oldid = session_id();
424 // $this->session->native->sess_regenerate();
425 // $oldid = session_id();
426 // $this->assertNotEquals($oldid, $newid);
dchill4257486002012-07-31 09:39:53 -0400427 }
428
429 /**
430 * Test the sess_destroy() function
dchill4208c83042012-08-28 17:35:56 -0400431 *
432 * @covers CI_Session::sess_destroy
dchill4257486002012-07-31 09:39:53 -0400433 */
434 public function test_sess_destroy()
435 {
436 // Set a userdata message, destroy session, and verify absence
dchill4208c83042012-08-28 17:35:56 -0400437 $key = 'dsttest';
dchill4257486002012-07-31 09:39:53 -0400438 $msg = 'More test data';
439
440 // Cookie driver
dchill4208c83042012-08-28 17:35:56 -0400441 $this->session->cookie->set_userdata($key, $msg);
442 $this->assertEquals($msg, $this->session->cookie->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400443 $this->session->cookie->sess_destroy();
dchill4208c83042012-08-28 17:35:56 -0400444 $this->assertNull($this->session->cookie->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400445
446 // Native driver
dchill4208c83042012-08-28 17:35:56 -0400447 $this->session->native->set_userdata($key, $msg);
448 $this->assertEquals($msg, $this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400449 $this->session->native->sess_destroy();
dchill4208c83042012-08-28 17:35:56 -0400450 $this->assertNull($this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400451 }
Johnathan Croom66b36ef2012-11-29 17:54:28 -0700452}