dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Session driver library unit test |
| 5 | */ |
| 6 | class Session_test extends CI_TestCase { |
dchill42 | 4153c92 | 2012-07-31 10:38:21 -0400 | [diff] [blame] | 7 | protected $settings = array( |
| 8 | 'use_cookies' => 0, |
dchill42 | 6262d05 | 2012-11-24 18:41:13 -0500 | [diff] [blame] | 9 | 'use_only_cookies' => 0, |
| 10 | 'cache_limiter' => false |
dchill42 | 4153c92 | 2012-07-31 10:38:21 -0400 | [diff] [blame] | 11 | ); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 12 | 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 | |
dchill42 | 6262d05 | 2012-11-24 18:41:13 -0500 | [diff] [blame] | 31 | // Set subclass prefix to match our mock |
| 32 | $this->ci_set_config('subclass_prefix', 'Mock_Libraries_'); |
| 33 | |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 34 | // Establish necessary support classes |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 35 | $ci = $this->ci_instance(); |
dchill42 | 6262d05 | 2012-11-24 18:41:13 -0500 | [diff] [blame] | 36 | $ldr = $this->ci_core_class('load'); |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 37 | $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'); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 42 | |
| 43 | // Attach session instance locally |
dchill42 | 4153c92 | 2012-07-31 10:38:21 -0400 | [diff] [blame] | 44 | $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' => '', |
dchill42 | 6262d05 | 2012-11-24 18:41:13 -0500 | [diff] [blame] | 60 | 'encryption_key' => 'foobar' |
dchill42 | 4153c92 | 2012-07-31 10:38:21 -0400 | [diff] [blame] | 61 | ); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 62 | $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 | { |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 86 | // 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); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 98 | |
| 99 | // Verify independent messages |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 100 | $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)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Test the has_userdata() function |
| 114 | */ |
| 115 | public function test_has_userdata() |
| 116 | { |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 117 | // Set a userdata value for each driver |
| 118 | $key = 'hastest'; |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 119 | $cmsg = 'My test data'; |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 120 | $this->session->cookie->set_userdata($key, $cmsg); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 121 | $nmsg = 'Your test data'; |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 122 | $this->session->native->set_userdata($key, $nmsg); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 123 | |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 124 | // 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)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 132 | } |
| 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 |
dchill42 | 4153c92 | 2012-07-31 10:38:21 -0400 | [diff] [blame] | 140 | $cdata = array( |
| 141 | 'one' => 'first', |
| 142 | 'two' => 'second', |
dchill42 | 6262d05 | 2012-11-24 18:41:13 -0500 | [diff] [blame] | 143 | 'three' => 'third', |
| 144 | 'foo' => 'bar', |
| 145 | 'bar' => 'baz' |
dchill42 | 4153c92 | 2012-07-31 10:38:21 -0400 | [diff] [blame] | 146 | ); |
| 147 | $ndata = array( |
| 148 | 'one' => 'gold', |
dchill42 | 6262d05 | 2012-11-24 18:41:13 -0500 | [diff] [blame] | 149 | 'two' => 'silver', |
| 150 | 'three' => 'bronze', |
| 151 | 'foo' => 'baz', |
| 152 | 'bar' => 'foo' |
dchill42 | 4153c92 | 2012-07-31 10:38:21 -0400 | [diff] [blame] | 153 | ); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 154 | $this->session->cookie->set_userdata($cdata); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 155 | $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 |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 174 | $key = 'untest'; |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 175 | $cmsg = 'Other test data'; |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 176 | $this->session->cookie->set_userdata($key, $cmsg); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 177 | $nmsg = 'Sundry test data'; |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 178 | $this->session->native->set_userdata($key, $nmsg); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 179 | |
| 180 | // Verify independent messages |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 181 | $this->assertEquals($this->session->cookie->userdata($key), $cmsg); |
| 182 | $this->assertEquals($this->session->native->userdata($key), $nmsg); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 183 | |
| 184 | // Unset them and verify absence |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 185 | $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)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 192 | * Test the flashdata() functions |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 193 | */ |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 194 | public function test_flashdata() |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 195 | { |
| 196 | // Set flashdata message for each driver |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 197 | $key = 'fltest'; |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 198 | $cmsg = 'Some flash data'; |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 199 | $this->session->cookie->set_flashdata($key, $cmsg); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 200 | $nmsg = 'Other flash data'; |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 201 | $this->session->native->set_flashdata($key, $nmsg); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 202 | |
| 203 | // Simulate page reload |
| 204 | $this->session->cookie->reload(); |
| 205 | $this->session->native->reload(); |
| 206 | |
| 207 | // Verify independent messages |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 208 | $this->assertEquals($cmsg, $this->session->cookie->flashdata($key)); |
| 209 | $this->assertEquals($nmsg, $this->session->native->flashdata($key)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 210 | |
| 211 | // Simulate next page reload |
| 212 | $this->session->cookie->reload(); |
| 213 | $this->session->native->reload(); |
| 214 | |
| 215 | // Verify absence of messages |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 216 | $this->assertNull($this->session->cookie->flashdata($key)); |
| 217 | $this->assertNull($this->session->native->flashdata($key)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Test the keep_flashdata() function |
| 222 | */ |
| 223 | public function test_keep_flashdata() |
| 224 | { |
| 225 | // Set flashdata message for each driver |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 226 | $key = 'kfltest'; |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 227 | $cmsg = 'My flash data'; |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 228 | $this->session->cookie->set_flashdata($key, $cmsg); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 229 | $nmsg = 'Your flash data'; |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 230 | $this->session->native->set_flashdata($key, $nmsg); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 231 | |
| 232 | // Simulate page reload and verify independent messages |
| 233 | $this->session->cookie->reload(); |
| 234 | $this->session->native->reload(); |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 235 | $this->assertEquals($cmsg, $this->session->cookie->flashdata($key)); |
| 236 | $this->assertEquals($nmsg, $this->session->native->flashdata($key)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 237 | |
| 238 | // Keep messages |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 239 | $this->session->cookie->keep_flashdata($key); |
| 240 | $this->session->native->keep_flashdata($key); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 241 | |
| 242 | // Simulate next page reload and verify message persistence |
| 243 | $this->session->cookie->reload(); |
| 244 | $this->session->native->reload(); |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 245 | $this->assertEquals($cmsg, $this->session->cookie->flashdata($key)); |
| 246 | $this->assertEquals($nmsg, $this->session->native->flashdata($key)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 247 | |
| 248 | // Simulate next page reload and verify absence of messages |
| 249 | $this->session->cookie->reload(); |
| 250 | $this->session->native->reload(); |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 251 | $this->assertNull($this->session->cookie->flashdata($key)); |
| 252 | $this->assertNull($this->session->native->flashdata($key)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 253 | } |
| 254 | |
Johnathan Croom | 66b36ef | 2012-11-29 17:54:28 -0700 | [diff] [blame] | 255 | 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 | |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 305 | /** |
| 306 | * Test the all_flashdata() function |
| 307 | */ |
| 308 | public function test_all_flashdata() |
| 309 | { |
| 310 | // Set a specific series of data for each driver |
dchill42 | 4153c92 | 2012-07-31 10:38:21 -0400 | [diff] [blame] | 311 | $cdata = array( |
| 312 | 'one' => 'first', |
dchill42 | 6262d05 | 2012-11-24 18:41:13 -0500 | [diff] [blame] | 313 | 'two' => 'second', |
| 314 | 'three' => 'third', |
| 315 | 'foo' => 'bar', |
| 316 | 'bar' => 'baz' |
dchill42 | 4153c92 | 2012-07-31 10:38:21 -0400 | [diff] [blame] | 317 | ); |
| 318 | $ndata = array( |
| 319 | 'one' => 'gold', |
dchill42 | 6262d05 | 2012-11-24 18:41:13 -0500 | [diff] [blame] | 320 | 'two' => 'silver', |
| 321 | 'three' => 'bronze', |
| 322 | 'foo' => 'baz', |
| 323 | 'bar' => 'foo' |
dchill42 | 4153c92 | 2012-07-31 10:38:21 -0400 | [diff] [blame] | 324 | ); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 325 | $this->session->cookie->set_flashdata($cdata); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 326 | $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 | /** |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 336 | * Test the tempdata() functions |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 337 | */ |
| 338 | public function test_set_tempdata() |
| 339 | { |
| 340 | // Set tempdata message for each driver - 1 second timeout |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 341 | $key = 'tmptest'; |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 342 | $cmsg = 'Some temp data'; |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 343 | $this->session->cookie->set_tempdata($key, $cmsg, 1); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 344 | $nmsg = 'Other temp data'; |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 345 | $this->session->native->set_tempdata($key, $nmsg, 1); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 346 | |
| 347 | // Simulate page reload and verify independent messages |
| 348 | $this->session->cookie->reload(); |
| 349 | $this->session->native->reload(); |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 350 | $this->assertEquals($cmsg, $this->session->cookie->tempdata($key)); |
| 351 | $this->assertEquals($nmsg, $this->session->native->tempdata($key)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 352 | |
| 353 | // Wait 2 seconds, simulate page reload and verify message absence |
| 354 | sleep(2); |
| 355 | $this->session->cookie->reload(); |
| 356 | $this->session->native->reload(); |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 357 | $this->assertNull($this->session->cookie->tempdata($key)); |
| 358 | $this->assertNull($this->session->native->tempdata($key)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 359 | } |
| 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 |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 367 | $key = 'utmptest'; |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 368 | $cmsg = 'My temp data'; |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 369 | $this->session->cookie->set_tempdata($key, $cmsg, 1); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 370 | $nmsg = 'Your temp data'; |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 371 | $this->session->native->set_tempdata($key, $nmsg, 1); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 372 | |
| 373 | // Verify independent messages |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 374 | $this->assertEquals($cmsg, $this->session->cookie->tempdata($key)); |
| 375 | $this->assertEquals($nmsg, $this->session->native->tempdata($key)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 376 | |
| 377 | // Unset data and verify message absence |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 378 | $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)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 382 | } |
| 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'); |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 394 | $this->assertNotEquals($oldid, $newid); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 395 | |
| 396 | // Native driver - bug #55267 (https://bugs.php.net/bug.php?id=55267) prevents testing this |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 397 | // $oldid = session_id(); |
| 398 | // $this->session->native->sess_regenerate(); |
| 399 | // $oldid = session_id(); |
| 400 | // $this->assertNotEquals($oldid, $newid); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 401 | } |
| 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 |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 409 | $key = 'dsttest'; |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 410 | $msg = 'More test data'; |
| 411 | |
| 412 | // Cookie driver |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 413 | $this->session->cookie->set_userdata($key, $msg); |
| 414 | $this->assertEquals($msg, $this->session->cookie->userdata($key)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 415 | $this->session->cookie->sess_destroy(); |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 416 | $this->assertNull($this->session->cookie->userdata($key)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 417 | |
| 418 | // Native driver |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 419 | $this->session->native->set_userdata($key, $msg); |
| 420 | $this->assertEquals($msg, $this->session->native->userdata($key)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 421 | $this->session->native->sess_destroy(); |
dchill42 | 08c8304 | 2012-08-28 17:35:56 -0400 | [diff] [blame] | 422 | $this->assertNull($this->session->native->userdata($key)); |
dchill42 | 5748600 | 2012-07-31 09:39:53 -0400 | [diff] [blame] | 423 | } |
dchill42 | 6262d05 | 2012-11-24 18:41:13 -0500 | [diff] [blame] | 424 | } |