blob: 5d58a9e697579c7b5ae4af9c29fa5e28660c5a41 [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;
dchill424153c922012-07-31 10:38:21 -040033 $classes = array(
34 'config' => 'cfg',
35 'load' => 'load',
36 'input' => 'in'
37 );
38 foreach ($classes as $name => $abbr) {
dchill4257486002012-07-31 09:39:53 -040039 $class = $this->ci_core_class($abbr);
40 $obj->$name = new $class;
41 }
42 $this->ci_instance($obj);
43
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' => '',
61 'encryption_key' => 'foobar',
62 'sess_valid_drivers' => array(
63 'Mock_Libraries_Session_native',
64 'Mock_Libraries_Session_cookie'
65 )
66 );
dchill4257486002012-07-31 09:39:53 -040067 $this->session = new Mock_Libraries_Session($config);
68 }
69
70 /**
71 * Tear down test framework
72 */
73 public function tear_down()
74 {
75 // Restore environment
76 if (session_id()) session_destroy();
77 $_SESSION = array();
78 $_COOKIE = $this->cookie_vals;
79
80 // Restore settings
81 foreach ($this->settings as $name => $value) {
82 ini_set('session.'.$name, $this->setting_vals[$name]);
83 }
84 }
85
86 /**
87 * Test set_userdata() function
88 */
89 public function test_set_userdata()
90 {
91 // Set a userdata message for each driver
92 $cmsg = 'Some test data';
93 $this->session->cookie->set_userdata('test1', $cmsg);
94 $nmsg = 'Other test data';
95 $this->session->native->set_userdata('test1', $nmsg);
96
97 // Verify independent messages
98 $this->assertEquals($this->session->cookie->userdata('test1'), $cmsg);
99 $this->assertEquals($this->session->native->userdata('test1'), $nmsg);
100 }
101
102 /**
103 * Test the has_userdata() function
104 */
105 public function test_has_userdata()
106 {
107 // Set a userdata message for each driver
108 $cmsg = 'My test data';
109 $this->session->cookie->set_userdata('test2', $cmsg);
110 $nmsg = 'Your test data';
111 $this->session->native->set_userdata('test2', $nmsg);
112
113 // Verify independent messages
114 $this->assertTrue($this->session->cookie->has_userdata('test2'));
115 $this->assertTrue($this->session->native->has_userdata('test2'));
116 }
117
118 /**
119 * Test the all_userdata() function
120 */
121 public function test_all_userdata()
122 {
123 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400124 $cdata = array(
125 'one' => 'first',
126 'two' => 'second',
127 'three' => 'third',
128 'foo' => 'bar',
129 'bar' => 'baz'
130 );
131 $ndata = array(
132 'one' => 'gold',
133 'two' => 'silver',
134 'three' => 'bronze',
135 'foo' => 'baz',
136 'bar' => 'foo'
137 );
dchill4257486002012-07-31 09:39:53 -0400138 $this->session->cookie->set_userdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400139 $this->session->native->set_userdata($ndata);
140
141 // Make sure all values are present
142 $call = $this->session->cookie->all_userdata();
143 foreach ($cdata as $key => $value) {
144 $this->assertEquals($value, $call[$key]);
145 }
146 $nall = $this->session->native->all_userdata();
147 foreach ($ndata as $key => $value) {
148 $this->assertEquals($value, $nall[$key]);
149 }
150 }
151
152 /**
153 * Test the unset_userdata() function
154 */
155 public function test_unset_userdata()
156 {
157 // Set a userdata message for each driver
158 $cmsg = 'Other test data';
159 $this->session->cookie->set_userdata('test3', $cmsg);
160 $nmsg = 'Sundry test data';
161 $this->session->native->set_userdata('test3', $nmsg);
162
163 // Verify independent messages
164 $this->assertEquals($this->session->cookie->userdata('test3'), $cmsg);
165 $this->assertEquals($this->session->native->userdata('test3'), $nmsg);
166
167 // Unset them and verify absence
168 $this->session->cookie->unset_userdata('test3');
169 $this->session->native->unset_userdata('test3');
170 $this->assertEquals($this->session->cookie->userdata('test3'), NULL);
171 $this->assertEquals($this->session->native->userdata('test3'), NULL);
172 }
173
174 /**
175 * Test the set_flashdata() function
176 */
177 public function test_set_flashdata()
178 {
179 // Set flashdata message for each driver
180 $cmsg = 'Some flash data';
181 $this->session->cookie->set_flashdata('test4', $cmsg);
182 $nmsg = 'Other flash data';
183 $this->session->native->set_flashdata('test4', $nmsg);
184
185 // Simulate page reload
186 $this->session->cookie->reload();
187 $this->session->native->reload();
188
189 // Verify independent messages
190 $this->assertEquals($this->session->cookie->flashdata('test4'), $cmsg);
191 $this->assertEquals($this->session->native->flashdata('test4'), $nmsg);
192
193 // Simulate next page reload
194 $this->session->cookie->reload();
195 $this->session->native->reload();
196
197 // Verify absence of messages
198 $this->assertEquals($this->session->cookie->flashdata('test4'), NULL);
199 $this->assertEquals($this->session->native->flashdata('test4'), NULL);
200 }
201
202 /**
203 * Test the keep_flashdata() function
204 */
205 public function test_keep_flashdata()
206 {
207 // Set flashdata message for each driver
208 $cmsg = 'My flash data';
209 $this->session->cookie->set_flashdata('test5', $cmsg);
210 $nmsg = 'Your flash data';
211 $this->session->native->set_flashdata('test5', $nmsg);
212
213 // Simulate page reload and verify independent messages
214 $this->session->cookie->reload();
215 $this->session->native->reload();
216 $this->assertEquals($this->session->cookie->flashdata('test5'), $cmsg);
217 $this->assertEquals($this->session->native->flashdata('test5'), $nmsg);
218
219 // Keep messages
220 $this->session->cookie->keep_flashdata('test5');
221 $this->session->native->keep_flashdata('test5');
222
223 // Simulate next page reload and verify message persistence
224 $this->session->cookie->reload();
225 $this->session->native->reload();
226 $this->assertEquals($this->session->cookie->flashdata('test5'), $cmsg);
227 $this->assertEquals($this->session->native->flashdata('test5'), $nmsg);
228
229 // Simulate next page reload and verify absence of messages
230 $this->session->cookie->reload();
231 $this->session->native->reload();
232 $this->assertEquals($this->session->cookie->flashdata('test5'), NULL);
233 $this->assertEquals($this->session->native->flashdata('test5'), NULL);
234 }
235
236 /**
237 * Test the all_flashdata() function
238 */
239 public function test_all_flashdata()
240 {
241 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400242 $cdata = array(
243 'one' => 'first',
244 'two' => 'second',
245 'three' => 'third',
246 'foo' => 'bar',
247 'bar' => 'baz'
248 );
249 $ndata = array(
250 'one' => 'gold',
251 'two' => 'silver',
252 'three' => 'bronze',
253 'foo' => 'baz',
254 'bar' => 'foo'
255 );
dchill4257486002012-07-31 09:39:53 -0400256 $this->session->cookie->set_flashdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400257 $this->session->native->set_flashdata($ndata);
258
259 // Simulate page reload and make sure all values are present
260 $this->session->cookie->reload();
261 $this->session->native->reload();
262 $this->assertEquals($cdata, $this->session->cookie->all_flashdata());
263 $this->assertEquals($ndata, $this->session->native->all_flashdata());
264 }
265
266 /**
267 * Test the set_tempdata() function
268 */
269 public function test_set_tempdata()
270 {
271 // Set tempdata message for each driver - 1 second timeout
272 $cmsg = 'Some temp data';
273 $this->session->cookie->set_tempdata('test6', $cmsg, 1);
274 $nmsg = 'Other temp data';
275 $this->session->native->set_tempdata('test6', $nmsg, 1);
276
277 // Simulate page reload and verify independent messages
278 $this->session->cookie->reload();
279 $this->session->native->reload();
280 $this->assertEquals($this->session->cookie->tempdata('test6'), $cmsg);
281 $this->assertEquals($this->session->native->tempdata('test6'), $nmsg);
282
283 // Wait 2 seconds, simulate page reload and verify message absence
284 sleep(2);
285 $this->session->cookie->reload();
286 $this->session->native->reload();
287 $this->assertEquals($this->session->cookie->tempdata('test6'), NULL);
288 $this->assertEquals($this->session->native->tempdata('test6'), NULL);
289 }
290
291 /**
292 * Test the unset_tempdata() function
293 */
294 public function test_unset_tempdata()
295 {
296 // Set tempdata message for each driver - 1 second timeout
297 $cmsg = 'My temp data';
298 $this->session->cookie->set_tempdata('test7', $cmsg, 1);
299 $nmsg = 'Your temp data';
300 $this->session->native->set_tempdata('test7', $nmsg, 1);
301
302 // Verify independent messages
303 $this->assertEquals($this->session->cookie->tempdata('test7'), $cmsg);
304 $this->assertEquals($this->session->native->tempdata('test7'), $nmsg);
305
306 // Unset data and verify message absence
307 $this->session->cookie->unset_tempdata('test7');
308 $this->session->native->unset_tempdata('test7');
309 $this->assertEquals($this->session->cookie->tempdata('test7'), NULL);
310 $this->assertEquals($this->session->native->tempdata('test7'), NULL);
311 }
312
313 /**
314 * Test the sess_regenerate() function
315 */
316 public function test_sess_regenerate()
317 {
318 // Get current session id, regenerate, and compare
319 // Cookie driver
320 $oldid = $this->session->cookie->userdata('session_id');
321 $this->session->cookie->sess_regenerate();
322 $newid = $this->session->cookie->userdata('session_id');
323 $this->assertFalse($oldid === $newid);
324
325 // Native driver - bug #55267 (https://bugs.php.net/bug.php?id=55267) prevents testing this
326 /*$oldid = session_id();
327 $this->session->native->sess_regenerate();
328 $oldid = session_id();
329 $this->assertFalse($oldid === $newid);*/
330 }
331
332 /**
333 * Test the sess_destroy() function
334 */
335 public function test_sess_destroy()
336 {
337 // Set a userdata message, destroy session, and verify absence
338 $msg = 'More test data';
339
340 // Cookie driver
341 $this->session->cookie->set_userdata('test8', $msg);
342 $this->assertEquals($this->session->cookie->userdata('test8'), $msg);
343 $this->session->cookie->sess_destroy();
344 $this->assertEquals($this->session->cookie->userdata('test8'), NULL);
345
346 // Native driver
347 $this->session->native->set_userdata('test8', $msg);
348 $this->assertEquals($this->session->native->userdata('test8'), $msg);
349 $this->session->native->sess_destroy();
350 $this->assertEquals($this->session->native->userdata('test8'), NULL);
351 }
352}
353