blob: 60d3a5b30b04e641afe6cc7c3d1fd1de5c775adc [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
32 $obj = new stdClass;
dchill4282003da2012-10-09 13:28:17 -040033 $cfg = $this->ci_core_class('cfg');
34 $obj->config = new $cfg();
35 $ldr = $this->ci_core_class('load');
36 $obj->load = new $ldr();
37 $obj->input = new Mock_Core_Input(NULL, NULL);
dchill4257486002012-07-31 09:39:53 -040038 $this->ci_instance($obj);
39
40 // Attach session instance locally
dchill424153c922012-07-31 10:38:21 -040041 $config = array(
42 'sess_encrypt_cookie' => FALSE,
43 'sess_use_database' => FALSE,
44 'sess_table_name' => '',
45 'sess_expiration' => 7200,
46 'sess_expire_on_close' => FALSE,
47 'sess_match_ip' => FALSE,
48 'sess_match_useragent' => TRUE,
49 'sess_cookie_name' => 'ci_session',
50 'cookie_path' => '',
51 'cookie_domain' => '',
52 'cookie_secure' => FALSE,
53 'cookie_httponly' => FALSE,
54 'sess_time_to_update' => 300,
55 'time_reference' => 'local',
56 'cookie_prefix' => '',
57 'encryption_key' => 'foobar',
58 'sess_valid_drivers' => array(
59 'Mock_Libraries_Session_native',
60 'Mock_Libraries_Session_cookie'
61 )
62 );
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
dchill4208c83042012-08-28 17:35:56 -040084 *
85 * @covers CI_Session::set_userdata
86 * @covers CI_Session::userdata
dchill4257486002012-07-31 09:39:53 -040087 */
88 public function test_set_userdata()
89 {
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';
97 $nmsg2 = true;
98 $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
dchill4208c83042012-08-28 17:35:56 -0400118 *
119 * @covers CI_Session::has_userdata
dchill4257486002012-07-31 09:39:53 -0400120 */
121 public function test_has_userdata()
122 {
dchill4208c83042012-08-28 17:35:56 -0400123 // Set a userdata value for each driver
124 $key = 'hastest';
dchill4257486002012-07-31 09:39:53 -0400125 $cmsg = 'My test data';
dchill4208c83042012-08-28 17:35:56 -0400126 $this->session->cookie->set_userdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400127 $nmsg = 'Your test data';
dchill4208c83042012-08-28 17:35:56 -0400128 $this->session->native->set_userdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400129
dchill4208c83042012-08-28 17:35:56 -0400130 // Verify values exist
131 $this->assertTrue($this->session->cookie->has_userdata($key));
132 $this->assertTrue($this->session->native->has_userdata($key));
133
134 // Verify non-existent values
135 $nokey = 'hasnot';
136 $this->assertFalse($this->session->cookie->has_userdata($nokey));
137 $this->assertFalse($this->session->native->has_userdata($nokey));
dchill4257486002012-07-31 09:39:53 -0400138 }
139
140 /**
141 * Test the all_userdata() function
dchill4208c83042012-08-28 17:35:56 -0400142 *
143 * @covers CI_Session::all_userdata
dchill4257486002012-07-31 09:39:53 -0400144 */
145 public function test_all_userdata()
146 {
147 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400148 $cdata = array(
149 'one' => 'first',
150 'two' => 'second',
151 'three' => 'third',
152 'foo' => 'bar',
153 'bar' => 'baz'
154 );
155 $ndata = array(
156 'one' => 'gold',
157 'two' => 'silver',
158 'three' => 'bronze',
159 'foo' => 'baz',
160 'bar' => 'foo'
161 );
dchill4257486002012-07-31 09:39:53 -0400162 $this->session->cookie->set_userdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400163 $this->session->native->set_userdata($ndata);
164
165 // Make sure all values are present
166 $call = $this->session->cookie->all_userdata();
167 foreach ($cdata as $key => $value) {
168 $this->assertEquals($value, $call[$key]);
169 }
170 $nall = $this->session->native->all_userdata();
171 foreach ($ndata as $key => $value) {
172 $this->assertEquals($value, $nall[$key]);
173 }
174 }
175
176 /**
177 * Test the unset_userdata() function
dchill4208c83042012-08-28 17:35:56 -0400178 *
179 * @covers CI_Session::unset_userdata
dchill4257486002012-07-31 09:39:53 -0400180 */
181 public function test_unset_userdata()
182 {
183 // Set a userdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400184 $key = 'untest';
dchill4257486002012-07-31 09:39:53 -0400185 $cmsg = 'Other test data';
dchill4208c83042012-08-28 17:35:56 -0400186 $this->session->cookie->set_userdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400187 $nmsg = 'Sundry test data';
dchill4208c83042012-08-28 17:35:56 -0400188 $this->session->native->set_userdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400189
190 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400191 $this->assertEquals($this->session->cookie->userdata($key), $cmsg);
192 $this->assertEquals($this->session->native->userdata($key), $nmsg);
dchill4257486002012-07-31 09:39:53 -0400193
194 // Unset them and verify absence
dchill4208c83042012-08-28 17:35:56 -0400195 $this->session->cookie->unset_userdata($key);
196 $this->session->native->unset_userdata($key);
197 $this->assertNull($this->session->cookie->userdata($key));
198 $this->assertNull($this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400199 }
200
201 /**
dchill4208c83042012-08-28 17:35:56 -0400202 * Test the flashdata() functions
203 *
204 * @covers CI_Session::set_flashdata
205 * @covers CI_Session::flashdata
dchill4257486002012-07-31 09:39:53 -0400206 */
dchill4208c83042012-08-28 17:35:56 -0400207 public function test_flashdata()
dchill4257486002012-07-31 09:39:53 -0400208 {
209 // Set flashdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400210 $key = 'fltest';
dchill4257486002012-07-31 09:39:53 -0400211 $cmsg = 'Some flash data';
dchill4208c83042012-08-28 17:35:56 -0400212 $this->session->cookie->set_flashdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400213 $nmsg = 'Other flash data';
dchill4208c83042012-08-28 17:35:56 -0400214 $this->session->native->set_flashdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400215
216 // Simulate page reload
217 $this->session->cookie->reload();
218 $this->session->native->reload();
219
220 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400221 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
222 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400223
224 // Simulate next page reload
225 $this->session->cookie->reload();
226 $this->session->native->reload();
227
228 // Verify absence of messages
dchill4208c83042012-08-28 17:35:56 -0400229 $this->assertNull($this->session->cookie->flashdata($key));
230 $this->assertNull($this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400231 }
232
233 /**
234 * Test the keep_flashdata() function
dchill4208c83042012-08-28 17:35:56 -0400235 *
236 * @covers CI_Session::keep_flashdata
dchill4257486002012-07-31 09:39:53 -0400237 */
238 public function test_keep_flashdata()
239 {
240 // Set flashdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400241 $key = 'kfltest';
dchill4257486002012-07-31 09:39:53 -0400242 $cmsg = 'My flash data';
dchill4208c83042012-08-28 17:35:56 -0400243 $this->session->cookie->set_flashdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400244 $nmsg = 'Your flash data';
dchill4208c83042012-08-28 17:35:56 -0400245 $this->session->native->set_flashdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400246
247 // Simulate page reload and verify independent messages
248 $this->session->cookie->reload();
249 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400250 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
251 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400252
253 // Keep messages
dchill4208c83042012-08-28 17:35:56 -0400254 $this->session->cookie->keep_flashdata($key);
255 $this->session->native->keep_flashdata($key);
dchill4257486002012-07-31 09:39:53 -0400256
257 // Simulate next page reload and verify message persistence
258 $this->session->cookie->reload();
259 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400260 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
261 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400262
263 // Simulate next page reload and verify absence of messages
264 $this->session->cookie->reload();
265 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400266 $this->assertNull($this->session->cookie->flashdata($key));
267 $this->assertNull($this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400268 }
269
270 /**
271 * Test the all_flashdata() function
dchill4208c83042012-08-28 17:35:56 -0400272 *
273 * @covers CI_Session::all_flashdata
dchill4257486002012-07-31 09:39:53 -0400274 */
275 public function test_all_flashdata()
276 {
277 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400278 $cdata = array(
279 'one' => 'first',
280 'two' => 'second',
281 'three' => 'third',
282 'foo' => 'bar',
283 'bar' => 'baz'
284 );
285 $ndata = array(
286 'one' => 'gold',
287 'two' => 'silver',
288 'three' => 'bronze',
289 'foo' => 'baz',
290 'bar' => 'foo'
291 );
dchill4257486002012-07-31 09:39:53 -0400292 $this->session->cookie->set_flashdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400293 $this->session->native->set_flashdata($ndata);
294
295 // Simulate page reload and make sure all values are present
296 $this->session->cookie->reload();
297 $this->session->native->reload();
298 $this->assertEquals($cdata, $this->session->cookie->all_flashdata());
299 $this->assertEquals($ndata, $this->session->native->all_flashdata());
300 }
301
302 /**
dchill4208c83042012-08-28 17:35:56 -0400303 * Test the tempdata() functions
304 *
305 * @covers CI_Session::set_tempdata
306 * @covers CI_Session::tempdata
dchill4257486002012-07-31 09:39:53 -0400307 */
308 public function test_set_tempdata()
309 {
310 // Set tempdata message for each driver - 1 second timeout
dchill4208c83042012-08-28 17:35:56 -0400311 $key = 'tmptest';
dchill4257486002012-07-31 09:39:53 -0400312 $cmsg = 'Some temp data';
dchill4208c83042012-08-28 17:35:56 -0400313 $this->session->cookie->set_tempdata($key, $cmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400314 $nmsg = 'Other temp data';
dchill4208c83042012-08-28 17:35:56 -0400315 $this->session->native->set_tempdata($key, $nmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400316
317 // Simulate page reload and verify independent messages
318 $this->session->cookie->reload();
319 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400320 $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
321 $this->assertEquals($nmsg, $this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400322
323 // Wait 2 seconds, simulate page reload and verify message absence
324 sleep(2);
325 $this->session->cookie->reload();
326 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400327 $this->assertNull($this->session->cookie->tempdata($key));
328 $this->assertNull($this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400329 }
330
331 /**
332 * Test the unset_tempdata() function
dchill4208c83042012-08-28 17:35:56 -0400333 *
334 * @covers CI_Session::unset_tempdata
dchill4257486002012-07-31 09:39:53 -0400335 */
336 public function test_unset_tempdata()
337 {
338 // Set tempdata message for each driver - 1 second timeout
dchill4208c83042012-08-28 17:35:56 -0400339 $key = 'utmptest';
dchill4257486002012-07-31 09:39:53 -0400340 $cmsg = 'My temp data';
dchill4208c83042012-08-28 17:35:56 -0400341 $this->session->cookie->set_tempdata($key, $cmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400342 $nmsg = 'Your temp data';
dchill4208c83042012-08-28 17:35:56 -0400343 $this->session->native->set_tempdata($key, $nmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400344
345 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400346 $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
347 $this->assertEquals($nmsg, $this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400348
349 // Unset data and verify message absence
dchill4208c83042012-08-28 17:35:56 -0400350 $this->session->cookie->unset_tempdata($key);
351 $this->session->native->unset_tempdata($key);
352 $this->assertNull($this->session->cookie->tempdata($key));
353 $this->assertNull($this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400354 }
355
356 /**
357 * Test the sess_regenerate() function
dchill4208c83042012-08-28 17:35:56 -0400358 *
359 * @covers CI_Session::sess_regenerate
dchill4257486002012-07-31 09:39:53 -0400360 */
361 public function test_sess_regenerate()
362 {
363 // Get current session id, regenerate, and compare
364 // Cookie driver
365 $oldid = $this->session->cookie->userdata('session_id');
366 $this->session->cookie->sess_regenerate();
367 $newid = $this->session->cookie->userdata('session_id');
dchill4208c83042012-08-28 17:35:56 -0400368 $this->assertNotEquals($oldid, $newid);
dchill4257486002012-07-31 09:39:53 -0400369
370 // Native driver - bug #55267 (https://bugs.php.net/bug.php?id=55267) prevents testing this
dchill4208c83042012-08-28 17:35:56 -0400371 // $oldid = session_id();
372 // $this->session->native->sess_regenerate();
373 // $oldid = session_id();
374 // $this->assertNotEquals($oldid, $newid);
dchill4257486002012-07-31 09:39:53 -0400375 }
376
377 /**
378 * Test the sess_destroy() function
dchill4208c83042012-08-28 17:35:56 -0400379 *
380 * @covers CI_Session::sess_destroy
dchill4257486002012-07-31 09:39:53 -0400381 */
382 public function test_sess_destroy()
383 {
384 // Set a userdata message, destroy session, and verify absence
dchill4208c83042012-08-28 17:35:56 -0400385 $key = 'dsttest';
dchill4257486002012-07-31 09:39:53 -0400386 $msg = 'More test data';
387
388 // Cookie driver
dchill4208c83042012-08-28 17:35:56 -0400389 $this->session->cookie->set_userdata($key, $msg);
390 $this->assertEquals($msg, $this->session->cookie->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400391 $this->session->cookie->sess_destroy();
dchill4208c83042012-08-28 17:35:56 -0400392 $this->assertNull($this->session->cookie->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400393
394 // Native driver
dchill4208c83042012-08-28 17:35:56 -0400395 $this->session->native->set_userdata($key, $msg);
396 $this->assertEquals($msg, $this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400397 $this->session->native->sess_destroy();
dchill4208c83042012-08-28 17:35:56 -0400398 $this->assertNull($this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400399 }
dchill4282003da2012-10-09 13:28:17 -0400400}