Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * SessionTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Model.Datasource
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('CakeSession', 'Model/Datasource');
20
App::uses('DatabaseSession', 'Model/Datasource/Session');
21
App::uses('CacheSession', 'Model/Datasource/Session');
22
 
23
/**
24
 * Class TestCakeSession
25
 *
26
 * @package       Cake.Test.Case.Model.Datasource
27
 */
28
class TestCakeSession extends CakeSession {
29
 
30
	public static function setUserAgent($value) {
31
		self::$_userAgent = $value;
32
	}
33
 
34
	public static function setHost($host) {
35
		self::_setHost($host);
36
	}
37
 
38
}
39
 
40
/**
41
 * Class TestCacheSession
42
 *
43
 * @package       Cake.Test.Case.Model.Datasource
44
 */
45
class TestCacheSession extends CacheSession {
46
 
47
	protected function _writeSession() {
48
		return true;
49
	}
50
 
51
}
52
 
53
/**
54
 * Class TestDatabaseSession
55
 *
56
 * @package       Cake.Test.Case.Model.Datasource
57
 */
58
class TestDatabaseSession extends DatabaseSession {
59
 
60
	protected function _writeSession() {
61
		return true;
62
	}
63
 
64
}
65
 
66
/**
67
 * CakeSessionTest class
68
 *
69
 * @package       Cake.Test.Case.Model.Datasource
70
 */
71
class CakeSessionTest extends CakeTestCase {
72
 
73
	protected static $_gcDivisor;
74
 
75
/**
76
 * Fixtures used in the SessionTest
77
 *
78
 * @var array
79
 */
80
	public $fixtures = array('core.session');
81
 
82
/**
83
 * setup before class.
84
 *
85
 * @return void
86
 */
87
	public static function setupBeforeClass() {
88
		// Make sure garbage colector will be called
89
		self::$_gcDivisor = ini_get('session.gc_divisor');
90
		ini_set('session.gc_divisor', '1');
91
	}
92
 
93
/**
94
 * teardown after class
95
 *
96
 * @return void
97
 */
98
	public static function teardownAfterClass() {
99
		// Revert to the default setting
100
		ini_set('session.gc_divisor', self::$_gcDivisor);
101
	}
102
 
103
/**
104
 * setUp method
105
 *
106
 * @return void
107
 */
108
	public function setUp() {
109
		parent::setUp();
110
		Configure::write('Session', array(
111
			'defaults' => 'php',
112
			'cookie' => 'cakephp',
113
			'timeout' => 120,
114
			'cookieTimeout' => 120,
115
			'ini' => array(),
116
		));
117
		TestCakeSession::init();
118
	}
119
 
120
/**
121
 * tearDown method
122
 *
123
 * @return void
124
 */
125
	public function tearDown() {
126
		if (TestCakeSession::started()) {
127
			session_write_close();
128
		}
129
		unset($_SESSION);
130
		parent::tearDown();
131
	}
132
 
133
/**
134
 * test setting ini properties with Session configuration.
135
 *
136
 * @return void
137
 */
138
	public function testSessionConfigIniSetting() {
139
		$_SESSION = null;
140
 
141
		Configure::write('Session', array(
142
			'cookie' => 'test',
143
			'checkAgent' => false,
144
			'timeout' => 86400,
145
			'ini' => array(
146
				'session.referer_check' => 'example.com',
147
				'session.use_trans_sid' => false
148
			)
149
		));
150
		TestCakeSession::start();
151
		$this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect');
152
		$this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect');
153
		$this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect');
154
	}
155
 
156
/**
157
 * testSessionPath
158
 *
159
 * @return void
160
 */
161
	public function testSessionPath() {
162
		TestCakeSession::init('/index.php');
163
		$this->assertEquals('/', TestCakeSession::$path);
164
 
165
		TestCakeSession::init('/sub_dir/index.php');
166
		$this->assertEquals('/sub_dir/', TestCakeSession::$path);
167
	}
168
 
169
/**
170
 * testCakeSessionPathEmpty
171
 *
172
 * @return void
173
 */
174
	public function testCakeSessionPathEmpty() {
175
		TestCakeSession::init('');
176
		$this->assertEquals('/', TestCakeSession::$path, 'Session path is empty, with "" as $base needs to be /');
177
	}
178
 
179
/**
180
 * testCakeSessionPathContainsParams
181
 *
182
 * @return void
183
 */
184
	public function testCakeSessionPathContainsQuestion() {
185
		TestCakeSession::init('/index.php?');
186
		$this->assertEquals('/', TestCakeSession::$path);
187
	}
188
 
189
/**
190
 * testSetHost
191
 *
192
 * @return void
193
 */
194
	public function testSetHost() {
195
		TestCakeSession::init();
196
		TestCakeSession::setHost('cakephp.org');
197
		$this->assertEquals('cakephp.org', TestCakeSession::$host);
198
	}
199
 
200
/**
201
 * testSetHostWithPort
202
 *
203
 * @return void
204
 */
205
	public function testSetHostWithPort() {
206
		TestCakeSession::init();
207
		TestCakeSession::setHost('cakephp.org:443');
208
		$this->assertEquals('cakephp.org', TestCakeSession::$host);
209
	}
210
 
211
/**
212
 * test valid with bogus user agent.
213
 *
214
 * @return void
215
 */
216
	public function testValidBogusUserAgent() {
217
		Configure::write('Session.checkAgent', true);
218
		TestCakeSession::start();
219
		$this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid');
220
 
221
		TestCakeSession::userAgent('bogus!');
222
		$this->assertFalse(TestCakeSession::valid(), 'user agent mismatch should fail.');
223
	}
224
 
225
/**
226
 * test valid with bogus user agent.
227
 *
228
 * @return void
229
 */
230
	public function testValidTimeExpiry() {
231
		Configure::write('Session.checkAgent', true);
232
		TestCakeSession::start();
233
		$this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid');
234
 
235
		TestCakeSession::$time = strtotime('next year');
236
		$this->assertFalse(TestCakeSession::valid(), 'time should cause failure.');
237
	}
238
 
239
/**
240
 * testCheck method
241
 *
242
 * @return void
243
 */
244
	public function testCheck() {
245
		TestCakeSession::write('SessionTestCase', 'value');
246
		$this->assertTrue(TestCakeSession::check('SessionTestCase'));
247
 
248
		$this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase'));
249
	}
250
 
251
/**
252
 * testSimpleRead method
253
 *
254
 * @return void
255
 */
256
	public function testSimpleRead() {
257
		TestCakeSession::write('testing', '1,2,3');
258
		$result = TestCakeSession::read('testing');
259
		$this->assertEquals('1,2,3', $result);
260
 
261
		TestCakeSession::write('testing', array('1' => 'one', '2' => 'two', '3' => 'three'));
262
		$result = TestCakeSession::read('testing.1');
263
		$this->assertEquals('one', $result);
264
 
265
		$result = TestCakeSession::read('testing');
266
		$this->assertEquals(array('1' => 'one', '2' => 'two', '3' => 'three'), $result);
267
 
268
		$result = TestCakeSession::read();
269
		$this->assertTrue(isset($result['testing']));
270
		$this->assertTrue(isset($result['Config']));
271
		$this->assertTrue(isset($result['Config']['userAgent']));
272
 
273
		TestCakeSession::write('This.is.a.deep.array.my.friend', 'value');
274
		$result = TestCakeSession::read('This.is.a.deep.array.my.friend');
275
		$this->assertEquals('value', $result);
276
	}
277
 
278
/**
279
 * testReadyEmpty
280
 *
281
 * @return void
282
 */
283
	public function testReadyEmpty() {
284
		$this->assertFalse(TestCakeSession::read(''));
285
	}
286
 
287
/**
288
 * test writing a hash of values/
289
 *
290
 * @return void
291
 */
292
	public function testWriteArray() {
293
		$result = TestCakeSession::write(array(
294
			'one' => 1,
295
			'two' => 2,
296
			'three' => array('something'),
297
			'null' => null
298
		));
299
		$this->assertTrue($result);
300
		$this->assertEquals(1, TestCakeSession::read('one'));
301
		$this->assertEquals(array('something'), TestCakeSession::read('three'));
302
		$this->assertEquals(null, TestCakeSession::read('null'));
303
	}
304
 
305
/**
306
 * testWriteEmptyKey
307
 *
308
 * @return void
309
 */
310
	public function testWriteEmptyKey() {
311
		$this->assertFalse(TestCakeSession::write('', 'graham'));
312
		$this->assertFalse(TestCakeSession::write('', ''));
313
		$this->assertFalse(TestCakeSession::write(''));
314
	}
315
 
316
/**
317
 * Test overwriting a string value as if it were an array.
318
 *
319
 * @return void
320
 */
321
	public function testWriteOverwriteStringValue() {
322
		TestCakeSession::write('Some.string', 'value');
323
		$this->assertEquals('value', TestCakeSession::read('Some.string'));
324
 
325
		TestCakeSession::write('Some.string.array', array('values'));
326
		$this->assertEquals(
327
			array('values'),
328
			TestCakeSession::read('Some.string.array')
329
		);
330
	}
331
 
332
/**
333
 * testId method
334
 *
335
 * @return void
336
 */
337
	public function testId() {
338
		TestCakeSession::destroy();
339
 
340
		$result = TestCakeSession::id();
341
		$expected = session_id();
342
		$this->assertEquals($expected, $result);
343
 
344
		TestCakeSession::id('MySessionId');
345
		$result = TestCakeSession::id();
346
		$this->assertEquals('MySessionId', $result);
347
	}
348
 
349
/**
350
 * testStarted method
351
 *
352
 * @return void
353
 */
354
	public function testStarted() {
355
		unset($_SESSION);
356
		$_SESSION = null;
357
 
358
		$this->assertFalse(TestCakeSession::started());
359
		$this->assertTrue(TestCakeSession::start());
360
		$this->assertTrue(TestCakeSession::started());
361
	}
362
 
363
/**
364
 * testDel method
365
 *
366
 * @return void
367
 */
368
	public function testDelete() {
369
		$this->assertTrue(TestCakeSession::write('Delete.me', 'Clearing out'));
370
		$this->assertTrue(TestCakeSession::delete('Delete.me'));
371
		$this->assertFalse(TestCakeSession::check('Delete.me'));
372
		$this->assertTrue(TestCakeSession::check('Delete'));
373
 
374
		$this->assertTrue(TestCakeSession::write('Clearing.sale', 'everything must go'));
375
		$this->assertTrue(TestCakeSession::delete('Clearing'));
376
		$this->assertFalse(TestCakeSession::check('Clearing.sale'));
377
		$this->assertFalse(TestCakeSession::check('Clearing'));
378
	}
379
 
380
/**
381
 * testDestroy method
382
 *
383
 * @return void
384
 */
385
	public function testDestroy() {
386
		TestCakeSession::write('bulletProof', 'invincible');
387
		$id = TestCakeSession::id();
388
		TestCakeSession::destroy();
389
 
390
		$this->assertFalse(TestCakeSession::check('bulletProof'));
391
		$this->assertNotEquals(TestCakeSession::id(), $id);
392
	}
393
 
394
/**
395
 * testCheckingSavedEmpty method
396
 *
397
 * @return void
398
 */
399
	public function testCheckingSavedEmpty() {
400
		$this->assertTrue(TestCakeSession::write('SessionTestCase', 0));
401
		$this->assertTrue(TestCakeSession::check('SessionTestCase'));
402
 
403
		$this->assertTrue(TestCakeSession::write('SessionTestCase', '0'));
404
		$this->assertTrue(TestCakeSession::check('SessionTestCase'));
405
 
406
		$this->assertTrue(TestCakeSession::write('SessionTestCase', false));
407
		$this->assertTrue(TestCakeSession::check('SessionTestCase'));
408
 
409
		$this->assertTrue(TestCakeSession::write('SessionTestCase', null));
410
		$this->assertFalse(TestCakeSession::check('SessionTestCase'));
411
	}
412
 
413
/**
414
 * testCheckKeyWithSpaces method
415
 *
416
 * @return void
417
 */
418
	public function testCheckKeyWithSpaces() {
419
		$this->assertTrue(TestCakeSession::write('Session Test', "test"));
420
		$this->assertTrue(TestCakeSession::check('Session Test'));
421
		TestCakeSession::delete('Session Test');
422
 
423
		$this->assertTrue(TestCakeSession::write('Session Test.Test Case', "test"));
424
		$this->assertTrue(TestCakeSession::check('Session Test.Test Case'));
425
	}
426
 
427
/**
428
 * testCheckEmpty
429
 *
430
 * @return void
431
 */
432
	public function testCheckEmpty() {
433
		$this->assertFalse(TestCakeSession::check());
434
	}
435
 
436
/**
437
 * test key exploitation
438
 *
439
 * @return void
440
 */
441
	public function testKeyExploit() {
442
		$key = "a'] = 1; phpinfo(); \$_SESSION['a";
443
		$result = TestCakeSession::write($key, 'haxored');
444
		$this->assertTrue($result);
445
 
446
		$result = TestCakeSession::read($key);
447
		$this->assertEquals('haxored', $result);
448
	}
449
 
450
/**
451
 * testReadingSavedEmpty method
452
 *
453
 * @return void
454
 */
455
	public function testReadingSavedEmpty() {
456
		TestCakeSession::write('SessionTestCase', 0);
457
		$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
458
 
459
		TestCakeSession::write('SessionTestCase', '0');
460
		$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
461
		$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
462
 
463
		TestCakeSession::write('SessionTestCase', false);
464
		$this->assertFalse(TestCakeSession::read('SessionTestCase'));
465
 
466
		TestCakeSession::write('SessionTestCase', null);
467
		$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
468
	}
469
 
470
/**
471
 * testCheckUserAgentFalse method
472
 *
473
 * @return void
474
 */
475
	public function testCheckUserAgentFalse() {
476
		Configure::write('Session.checkAgent', false);
477
		TestCakeSession::setUserAgent(md5('http://randomdomainname.com' . Configure::read('Security.salt')));
478
		$this->assertTrue(TestCakeSession::valid());
479
	}
480
 
481
/**
482
 * testCheckUserAgentTrue method
483
 *
484
 * @return void
485
 */
486
	public function testCheckUserAgentTrue() {
487
		Configure::write('Session.checkAgent', true);
488
		TestCakeSession::$error = false;
489
		$agent = md5('http://randomdomainname.com' . Configure::read('Security.salt'));
490
 
491
		TestCakeSession::write('Config.userAgent', md5('Hacking you!'));
492
		TestCakeSession::setUserAgent($agent);
493
		$this->assertFalse(TestCakeSession::valid());
494
	}
495
 
496
/**
497
 * testReadAndWriteWithCakeStorage method
498
 *
499
 * @return void
500
 */
501
	public function testReadAndWriteWithCakeStorage() {
502
		Configure::write('Session.defaults', 'cake');
503
 
504
		TestCakeSession::init();
505
		TestCakeSession::start();
506
 
507
		TestCakeSession::write('SessionTestCase', 0);
508
		$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
509
 
510
		TestCakeSession::write('SessionTestCase', '0');
511
		$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
512
		$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
513
 
514
		TestCakeSession::write('SessionTestCase', false);
515
		$this->assertFalse(TestCakeSession::read('SessionTestCase'));
516
 
517
		TestCakeSession::write('SessionTestCase', null);
518
		$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
519
 
520
		TestCakeSession::write('SessionTestCase', 'This is a Test');
521
		$this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
522
 
523
		TestCakeSession::write('SessionTestCase', 'This is a Test');
524
		TestCakeSession::write('SessionTestCase', 'This was updated');
525
		$this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
526
 
527
		TestCakeSession::destroy();
528
		$this->assertNull(TestCakeSession::read('SessionTestCase'));
529
	}
530
 
531
/**
532
 * test using a handler from app/Model/Datasource/Session.
533
 *
534
 * @return void
535
 */
536
	public function testUsingAppLibsHandler() {
537
		App::build(array(
538
			'Model/Datasource/Session' => array(
539
				CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS . 'Session' . DS
540
			),
541
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
542
		), App::RESET);
543
		Configure::write('Session', array(
544
			'defaults' => 'cake',
545
			'handler' => array(
546
				'engine' => 'TestAppLibSession'
547
			)
548
		));
549
 
550
		TestCakeSession::start();
551
		$this->assertTrue(TestCakeSession::started());
552
 
553
		TestCakeSession::destroy();
554
		$this->assertFalse(TestCakeSession::started());
555
 
556
		App::build();
557
	}
558
 
559
/**
560
 * test using a handler from a plugin.
561
 *
562
 * @return void
563
 */
564
	public function testUsingPluginHandler() {
565
		App::build(array(
566
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
567
		), App::RESET);
568
		CakePlugin::load('TestPlugin');
569
 
570
		Configure::write('Session', array(
571
			'defaults' => 'cake',
572
			'handler' => array(
573
				'engine' => 'TestPlugin.TestPluginSession'
574
			)
575
		));
576
 
577
		TestCakeSession::start();
578
		$this->assertTrue(TestCakeSession::started());
579
 
580
		TestCakeSession::destroy();
581
		$this->assertFalse(TestCakeSession::started());
582
 
583
		App::build();
584
	}
585
 
586
/**
587
 * testReadAndWriteWithCacheStorage method
588
 *
589
 * @return void
590
 */
591
	public function testReadAndWriteWithCacheStorage() {
592
		Configure::write('Session.defaults', 'cache');
593
		Configure::write('Session.handler.engine', 'TestCacheSession');
594
 
595
		TestCakeSession::init();
596
		TestCakeSession::destroy();
597
 
598
		TestCakeSession::write('SessionTestCase', 0);
599
		$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
600
 
601
		TestCakeSession::write('SessionTestCase', '0');
602
		$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
603
		$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
604
 
605
		TestCakeSession::write('SessionTestCase', false);
606
		$this->assertFalse(TestCakeSession::read('SessionTestCase'));
607
 
608
		TestCakeSession::write('SessionTestCase', null);
609
		$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
610
 
611
		TestCakeSession::write('SessionTestCase', 'This is a Test');
612
		$this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
613
 
614
		TestCakeSession::write('SessionTestCase', 'This is a Test');
615
		TestCakeSession::write('SessionTestCase', 'This was updated');
616
		$this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
617
 
618
		TestCakeSession::destroy();
619
		$this->assertNull(TestCakeSession::read('SessionTestCase'));
620
	}
621
 
622
/**
623
 * test that changing the config name of the cache config works.
624
 *
625
 * @return void
626
 */
627
	public function testReadAndWriteWithCustomCacheConfig() {
628
		Configure::write('Session.defaults', 'cache');
629
		Configure::write('Session.handler.engine', 'TestCacheSession');
630
		Configure::write('Session.handler.config', 'session_test');
631
 
632
		Cache::config('session_test', array(
633
			'engine' => 'File',
634
			'prefix' => 'session_test_',
635
		));
636
 
637
		TestCakeSession::init();
638
		TestCakeSession::start();
639
 
640
		TestCakeSession::write('SessionTestCase', 'Some value');
641
		$this->assertEquals('Some value', TestCakeSession::read('SessionTestCase'));
642
		$id = TestCakeSession::id();
643
 
644
		Cache::delete($id, 'session_test');
645
	}
646
 
647
/**
648
 * testReadAndWriteWithDatabaseStorage method
649
 *
650
 * @return void
651
 */
652
	public function testReadAndWriteWithDatabaseStorage() {
653
		Configure::write('Session.defaults', 'database');
654
		Configure::write('Session.handler.engine', 'TestDatabaseSession');
655
		Configure::write('Session.handler.table', 'sessions');
656
		Configure::write('Session.handler.model', 'Session');
657
		Configure::write('Session.handler.database', 'test');
658
 
659
		TestCakeSession::init();
660
		$this->assertNull(TestCakeSession::id());
661
 
662
		TestCakeSession::start();
663
		$expected = session_id();
664
		$this->assertEquals($expected, TestCakeSession::id());
665
 
666
		TestCakeSession::renew();
667
		$this->assertFalse($expected == TestCakeSession::id());
668
 
669
		$expected = session_id();
670
		$this->assertEquals($expected, TestCakeSession::id());
671
 
672
		TestCakeSession::write('SessionTestCase', 0);
673
		$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
674
 
675
		TestCakeSession::write('SessionTestCase', '0');
676
		$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
677
		$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
678
 
679
		TestCakeSession::write('SessionTestCase', false);
680
		$this->assertFalse(TestCakeSession::read('SessionTestCase'));
681
 
682
		TestCakeSession::write('SessionTestCase', null);
683
		$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
684
 
685
		TestCakeSession::write('SessionTestCase', 'This is a Test');
686
		$this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
687
 
688
		TestCakeSession::write('SessionTestCase', 'Some additional data');
689
		$this->assertEquals('Some additional data', TestCakeSession::read('SessionTestCase'));
690
 
691
		TestCakeSession::destroy();
692
		$this->assertNull(TestCakeSession::read('SessionTestCase'));
693
 
694
		Configure::write('Session', array(
695
			'defaults' => 'php'
696
		));
697
		TestCakeSession::init();
698
	}
699
 
700
/**
701
 * testSessionTimeout method
702
 *
703
 * @return void
704
 */
705
	public function testSessionTimeout() {
706
		Configure::write('debug', 2);
707
		Configure::write('Session.defaults', 'cake');
708
		Configure::write('Session.autoRegenerate', false);
709
 
710
		$timeoutSeconds = Configure::read('Session.timeout') * 60;
711
 
712
		TestCakeSession::destroy();
713
		TestCakeSession::write('Test', 'some value');
714
 
715
		$this->assertWithinMargin(time() + $timeoutSeconds, CakeSession::$sessionTime, 1);
716
		$this->assertEquals(10, $_SESSION['Config']['countdown']);
717
		$this->assertWithinMargin(CakeSession::$sessionTime, $_SESSION['Config']['time'], 1);
718
		$this->assertWithinMargin(time(), CakeSession::$time, 1);
719
		$this->assertWithinMargin(time() + $timeoutSeconds, $_SESSION['Config']['time'], 1);
720
 
721
		Configure::write('Session.harden', true);
722
		TestCakeSession::destroy();
723
 
724
		TestCakeSession::write('Test', 'some value');
725
		$this->assertWithinMargin(time() + $timeoutSeconds, CakeSession::$sessionTime, 1);
726
		$this->assertEquals(10, $_SESSION['Config']['countdown']);
727
		$this->assertWithinMargin(CakeSession::$sessionTime, $_SESSION['Config']['time'], 1);
728
		$this->assertWithinMargin(time(), CakeSession::$time, 1);
729
		$this->assertWithinMargin(CakeSession::$time + $timeoutSeconds, $_SESSION['Config']['time'], 1);
730
	}
731
 
732
/**
733
 * Test that cookieTimeout matches timeout when unspecified.
734
 *
735
 * @return void
736
 */
737
	public function testCookieTimeoutFallback() {
738
		$_SESSION = null;
739
		Configure::write('Session', array(
740
			'defaults' => 'cake',
741
			'timeout' => 400,
742
		));
743
		TestCakeSession::start();
744
		$this->assertEquals(400, Configure::read('Session.cookieTimeout'));
745
		$this->assertEquals(400, Configure::read('Session.timeout'));
746
		$this->assertEquals(400 * 60, ini_get('session.cookie_lifetime'));
747
		$this->assertEquals(400 * 60, ini_get('session.gc_maxlifetime'));
748
 
749
		$_SESSION = null;
750
		Configure::write('Session', array(
751
			'defaults' => 'cake',
752
			'timeout' => 400,
753
			'cookieTimeout' => 600
754
		));
755
		TestCakeSession::start();
756
		$this->assertEquals(600, Configure::read('Session.cookieTimeout'));
757
		$this->assertEquals(400, Configure::read('Session.timeout'));
758
	}
759
 
760
/**
761
 * Proves that invalid sessions will be destroyed and re-created
762
 * if invalid
763
 *
764
 * @return void
765
 */
766
	public function testInvalidSessionRenew() {
767
		TestCakeSession::start();
768
		$this->assertNotEmpty($_SESSION['Config']);
769
		$data = $_SESSION;
770
 
771
		session_write_close();
772
		$_SESSION = null;
773
 
774
		TestCakeSession::start();
775
		$this->assertEquals($data, $_SESSION);
776
		TestCakeSession::write('Foo', 'Bar');
777
 
778
		session_write_close();
779
		$_SESSION = null;
780
 
781
		TestCakeSession::userAgent('bogus!');
782
		TestCakeSession::start();
783
		$this->assertNotEquals($data, $_SESSION);
784
		$this->assertEquals('bogus!', $_SESSION['Config']['userAgent']);
785
	}
786
 
787
}