blob: 7ef3a36678939764d78d8c594a8a7eafac93ce89 [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,
dchill426262d052012-11-24 18:41:13 -05009 'use_only_cookies' => 0,
10 'cache_limiter' => false
dchill424153c922012-07-31 10:38:21 -040011 );
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
dchill426262d052012-11-24 18:41:13 -050031 // Set subclass prefix to match our mock
32 $this->ci_set_config('subclass_prefix', 'Mock_Libraries_');
33
dchill4257486002012-07-31 09:39:53 -040034 // Establish necessary support classes
dchill427ecc5cd2012-10-12 16:25:51 -040035 $ci = $this->ci_instance();
dchill426262d052012-11-24 18:41:13 -050036 $ldr = $this->ci_core_class('load');
dchill427ecc5cd2012-10-12 16:25:51 -040037 $ci->load = new $ldr();
38 $ci->input = new Mock_Core_Input(NULL, NULL);
39
40 // Make sure string helper is available
41 $this->ci_vfs_clone('system/helpers/string_helper.php');
dchill4257486002012-07-31 09:39:53 -040042
43 // Attach session instance locally
dchill424153c922012-07-31 10:38:21 -040044 $config = array(
45 'sess_encrypt_cookie' => FALSE,
46 'sess_use_database' => FALSE,
47 'sess_table_name' => '',
48 'sess_expiration' => 7200,
49 'sess_expire_on_close' => FALSE,
50 'sess_match_ip' => FALSE,
51 'sess_match_useragent' => TRUE,
52 'sess_cookie_name' => 'ci_session',
53 'cookie_path' => '',
54 'cookie_domain' => '',
55 'cookie_secure' => FALSE,
56 'cookie_httponly' => FALSE,
57 'sess_time_to_update' => 300,
58 'time_reference' => 'local',
59 'cookie_prefix' => '',
dchill426262d052012-11-24 18:41:13 -050060 'encryption_key' => 'foobar'
dchill424153c922012-07-31 10:38:21 -040061 );
dchill4257486002012-07-31 09:39:53 -040062 $this->session = new Mock_Libraries_Session($config);
63 }
64
65 /**
66 * Tear down test framework
67 */
68 public function tear_down()
69 {
70 // Restore environment
71 if (session_id()) session_destroy();
72 $_SESSION = array();
73 $_COOKIE = $this->cookie_vals;
74
75 // Restore settings
76 foreach ($this->settings as $name => $value) {
77 ini_set('session.'.$name, $this->setting_vals[$name]);
78 }
79 }
80
81 /**
82 * Test set_userdata() function
83 */
84 public function test_set_userdata()
85 {
dchill4208c83042012-08-28 17:35:56 -040086 // Set userdata values for each driver
87 $key1 = 'test1';
88 $ckey2 = 'test2';
89 $nkey2 = 'test3';
90 $cmsg1 = 'Some test data';
91 $cmsg2 = 42;
92 $nmsg1 = 'Other test data';
93 $nmsg2 = true;
94 $this->session->cookie->set_userdata($key1, $cmsg1);
95 $this->session->set_userdata($ckey2, $cmsg2);
96 $this->session->native->set_userdata($key1, $nmsg1);
97 $this->session->set_userdata($nkey2, $nmsg2);
dchill4257486002012-07-31 09:39:53 -040098
99 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400100 $this->assertEquals($cmsg1, $this->session->cookie->userdata($key1));
101 $this->assertEquals($nmsg1, $this->session->native->userdata($key1));
102
103 // Verify pre-selected driver sets
104 $this->assertEquals($cmsg2, $this->session->cookie->userdata($ckey2));
105 $this->assertEquals($nmsg2, $this->session->native->userdata($nkey2));
106
107 // Verify no crossover
108 $this->assertNull($this->session->cookie->userdata($nkey2));
109 $this->assertNull($this->session->native->userdata($ckey2));
dchill4257486002012-07-31 09:39:53 -0400110 }
111
112 /**
113 * Test the has_userdata() function
114 */
115 public function test_has_userdata()
116 {
dchill4208c83042012-08-28 17:35:56 -0400117 // Set a userdata value for each driver
118 $key = 'hastest';
dchill4257486002012-07-31 09:39:53 -0400119 $cmsg = 'My test data';
dchill4208c83042012-08-28 17:35:56 -0400120 $this->session->cookie->set_userdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400121 $nmsg = 'Your test data';
dchill4208c83042012-08-28 17:35:56 -0400122 $this->session->native->set_userdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400123
dchill4208c83042012-08-28 17:35:56 -0400124 // Verify values exist
125 $this->assertTrue($this->session->cookie->has_userdata($key));
126 $this->assertTrue($this->session->native->has_userdata($key));
127
128 // Verify non-existent values
129 $nokey = 'hasnot';
130 $this->assertFalse($this->session->cookie->has_userdata($nokey));
131 $this->assertFalse($this->session->native->has_userdata($nokey));
dchill4257486002012-07-31 09:39:53 -0400132 }
133
134 /**
135 * Test the all_userdata() function
136 */
137 public function test_all_userdata()
138 {
139 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400140 $cdata = array(
141 'one' => 'first',
142 'two' => 'second',
dchill426262d052012-11-24 18:41:13 -0500143 'three' => 'third',
144 'foo' => 'bar',
145 'bar' => 'baz'
dchill424153c922012-07-31 10:38:21 -0400146 );
147 $ndata = array(
148 'one' => 'gold',
dchill426262d052012-11-24 18:41:13 -0500149 'two' => 'silver',
150 'three' => 'bronze',
151 'foo' => 'baz',
152 'bar' => 'foo'
dchill424153c922012-07-31 10:38:21 -0400153 );
dchill4257486002012-07-31 09:39:53 -0400154 $this->session->cookie->set_userdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400155 $this->session->native->set_userdata($ndata);
156
157 // Make sure all values are present
158 $call = $this->session->cookie->all_userdata();
159 foreach ($cdata as $key => $value) {
160 $this->assertEquals($value, $call[$key]);
161 }
162 $nall = $this->session->native->all_userdata();
163 foreach ($ndata as $key => $value) {
164 $this->assertEquals($value, $nall[$key]);
165 }
166 }
167
168 /**
169 * Test the unset_userdata() function
170 */
171 public function test_unset_userdata()
172 {
173 // Set a userdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400174 $key = 'untest';
dchill4257486002012-07-31 09:39:53 -0400175 $cmsg = 'Other test data';
dchill4208c83042012-08-28 17:35:56 -0400176 $this->session->cookie->set_userdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400177 $nmsg = 'Sundry test data';
dchill4208c83042012-08-28 17:35:56 -0400178 $this->session->native->set_userdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400179
180 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400181 $this->assertEquals($this->session->cookie->userdata($key), $cmsg);
182 $this->assertEquals($this->session->native->userdata($key), $nmsg);
dchill4257486002012-07-31 09:39:53 -0400183
184 // Unset them and verify absence
dchill4208c83042012-08-28 17:35:56 -0400185 $this->session->cookie->unset_userdata($key);
186 $this->session->native->unset_userdata($key);
187 $this->assertNull($this->session->cookie->userdata($key));
188 $this->assertNull($this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400189 }
190
191 /**
dchill4208c83042012-08-28 17:35:56 -0400192 * Test the flashdata() functions
dchill4257486002012-07-31 09:39:53 -0400193 */
dchill4208c83042012-08-28 17:35:56 -0400194 public function test_flashdata()
dchill4257486002012-07-31 09:39:53 -0400195 {
196 // Set flashdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400197 $key = 'fltest';
dchill4257486002012-07-31 09:39:53 -0400198 $cmsg = 'Some flash data';
dchill4208c83042012-08-28 17:35:56 -0400199 $this->session->cookie->set_flashdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400200 $nmsg = 'Other flash data';
dchill4208c83042012-08-28 17:35:56 -0400201 $this->session->native->set_flashdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400202
203 // Simulate page reload
204 $this->session->cookie->reload();
205 $this->session->native->reload();
206
207 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400208 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
209 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400210
211 // Simulate next page reload
212 $this->session->cookie->reload();
213 $this->session->native->reload();
214
215 // Verify absence of messages
dchill4208c83042012-08-28 17:35:56 -0400216 $this->assertNull($this->session->cookie->flashdata($key));
217 $this->assertNull($this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400218 }
219
220 /**
221 * Test the keep_flashdata() function
222 */
223 public function test_keep_flashdata()
224 {
225 // Set flashdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400226 $key = 'kfltest';
dchill4257486002012-07-31 09:39:53 -0400227 $cmsg = 'My flash data';
dchill4208c83042012-08-28 17:35:56 -0400228 $this->session->cookie->set_flashdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400229 $nmsg = 'Your flash data';
dchill4208c83042012-08-28 17:35:56 -0400230 $this->session->native->set_flashdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400231
232 // Simulate page reload and verify independent messages
233 $this->session->cookie->reload();
234 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400235 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
236 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400237
238 // Keep messages
dchill4208c83042012-08-28 17:35:56 -0400239 $this->session->cookie->keep_flashdata($key);
240 $this->session->native->keep_flashdata($key);
dchill4257486002012-07-31 09:39:53 -0400241
242 // Simulate next page reload and verify message persistence
243 $this->session->cookie->reload();
244 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400245 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
246 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400247
248 // Simulate next page reload and verify absence of messages
249 $this->session->cookie->reload();
250 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400251 $this->assertNull($this->session->cookie->flashdata($key));
252 $this->assertNull($this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400253 }
254
Johnathan Croom66b36ef2012-11-29 17:54:28 -0700255 public function test_keep_flashdata_with_array()
256 {
257 // Set flashdata array for each driver
258 $cdata = array(
259 'one' => 'first',
260 'two' => 'second',
261 'three' => 'third',
262 'foo' => 'bar',
263 'bar' => 'baz'
264 );
265 $ndata = array(
266 'one' => 'gold',
267 'two' => 'silver',
268 'three' => 'bronze',
269 'foo' => 'baz',
270 'bar' => 'foo'
271 );
272 $kdata = array(
273 'one',
274 'two',
275 'three',
276 'foo',
277 'bar'
278 );
279 $this->session->cookie->set_flashdata($cdata);
280 $this->session->native->set_flashdata($ndata);
281
282 // Simulate page reload and verify independent messages
283 $this->session->cookie->reload();
284 $this->session->native->reload();
285 $this->assertEquals($cdata, $this->session->cookie->all_flashdata());
286 $this->assertEquals($ndata, $this->session->native->all_flashdata());
287
288 // Keep messages
289 $this->session->cookie->keep_flashdata($kdata);
290 $this->session->native->keep_flashdata($kdata);
291
292 // Simulate next page reload and verify message persistence
293 $this->session->cookie->reload();
294 $this->session->native->reload();
295 $this->assertEquals($cdata, $this->session->cookie->all_flashdata());
296 $this->assertEquals($ndata, $this->session->native->all_flashdata());
297
298 // Simulate next page reload and verify absence of messages
299 $this->session->cookie->reload();
300 $this->session->native->reload();
301 $this->assertEmpty($this->session->cookie->all_flashdata());
302 $this->assertEmpty($this->session->native->all_flashdata());
303 }
304
dchill4257486002012-07-31 09:39:53 -0400305 /**
306 * Test the all_flashdata() function
307 */
308 public function test_all_flashdata()
309 {
310 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400311 $cdata = array(
312 'one' => 'first',
dchill426262d052012-11-24 18:41:13 -0500313 'two' => 'second',
314 'three' => 'third',
315 'foo' => 'bar',
316 'bar' => 'baz'
dchill424153c922012-07-31 10:38:21 -0400317 );
318 $ndata = array(
319 'one' => 'gold',
dchill426262d052012-11-24 18:41:13 -0500320 'two' => 'silver',
321 'three' => 'bronze',
322 'foo' => 'baz',
323 'bar' => 'foo'
dchill424153c922012-07-31 10:38:21 -0400324 );
dchill4257486002012-07-31 09:39:53 -0400325 $this->session->cookie->set_flashdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400326 $this->session->native->set_flashdata($ndata);
327
328 // Simulate page reload and make sure all values are present
329 $this->session->cookie->reload();
330 $this->session->native->reload();
331 $this->assertEquals($cdata, $this->session->cookie->all_flashdata());
332 $this->assertEquals($ndata, $this->session->native->all_flashdata());
333 }
334
335 /**
dchill4208c83042012-08-28 17:35:56 -0400336 * Test the tempdata() functions
dchill4257486002012-07-31 09:39:53 -0400337 */
338 public function test_set_tempdata()
339 {
340 // Set tempdata message for each driver - 1 second timeout
dchill4208c83042012-08-28 17:35:56 -0400341 $key = 'tmptest';
dchill4257486002012-07-31 09:39:53 -0400342 $cmsg = 'Some temp data';
dchill4208c83042012-08-28 17:35:56 -0400343 $this->session->cookie->set_tempdata($key, $cmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400344 $nmsg = 'Other temp data';
dchill4208c83042012-08-28 17:35:56 -0400345 $this->session->native->set_tempdata($key, $nmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400346
347 // Simulate page reload and verify independent messages
348 $this->session->cookie->reload();
349 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400350 $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
351 $this->assertEquals($nmsg, $this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400352
353 // Wait 2 seconds, simulate page reload and verify message absence
354 sleep(2);
355 $this->session->cookie->reload();
356 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400357 $this->assertNull($this->session->cookie->tempdata($key));
358 $this->assertNull($this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400359 }
360
361 /**
362 * Test the unset_tempdata() function
363 */
364 public function test_unset_tempdata()
365 {
366 // Set tempdata message for each driver - 1 second timeout
dchill4208c83042012-08-28 17:35:56 -0400367 $key = 'utmptest';
dchill4257486002012-07-31 09:39:53 -0400368 $cmsg = 'My temp data';
dchill4208c83042012-08-28 17:35:56 -0400369 $this->session->cookie->set_tempdata($key, $cmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400370 $nmsg = 'Your temp data';
dchill4208c83042012-08-28 17:35:56 -0400371 $this->session->native->set_tempdata($key, $nmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400372
373 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400374 $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
375 $this->assertEquals($nmsg, $this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400376
377 // Unset data and verify message absence
dchill4208c83042012-08-28 17:35:56 -0400378 $this->session->cookie->unset_tempdata($key);
379 $this->session->native->unset_tempdata($key);
380 $this->assertNull($this->session->cookie->tempdata($key));
381 $this->assertNull($this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400382 }
383
384 /**
385 * Test the sess_regenerate() function
386 */
387 public function test_sess_regenerate()
388 {
389 // Get current session id, regenerate, and compare
390 // Cookie driver
391 $oldid = $this->session->cookie->userdata('session_id');
392 $this->session->cookie->sess_regenerate();
393 $newid = $this->session->cookie->userdata('session_id');
dchill4208c83042012-08-28 17:35:56 -0400394 $this->assertNotEquals($oldid, $newid);
dchill4257486002012-07-31 09:39:53 -0400395
396 // Native driver - bug #55267 (https://bugs.php.net/bug.php?id=55267) prevents testing this
dchill4208c83042012-08-28 17:35:56 -0400397 // $oldid = session_id();
398 // $this->session->native->sess_regenerate();
399 // $oldid = session_id();
400 // $this->assertNotEquals($oldid, $newid);
dchill4257486002012-07-31 09:39:53 -0400401 }
402
403 /**
404 * Test the sess_destroy() function
405 */
406 public function test_sess_destroy()
407 {
408 // Set a userdata message, destroy session, and verify absence
dchill4208c83042012-08-28 17:35:56 -0400409 $key = 'dsttest';
dchill4257486002012-07-31 09:39:53 -0400410 $msg = 'More test data';
411
412 // Cookie driver
dchill4208c83042012-08-28 17:35:56 -0400413 $this->session->cookie->set_userdata($key, $msg);
414 $this->assertEquals($msg, $this->session->cookie->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400415 $this->session->cookie->sess_destroy();
dchill4208c83042012-08-28 17:35:56 -0400416 $this->assertNull($this->session->cookie->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400417
418 // Native driver
dchill4208c83042012-08-28 17:35:56 -0400419 $this->session->native->set_userdata($key, $msg);
420 $this->assertEquals($msg, $this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400421 $this->session->native->sess_destroy();
dchill4208c83042012-08-28 17:35:56 -0400422 $this->assertNull($this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400423 }
dchill426262d052012-11-24 18:41:13 -0500424}