2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* SessionTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.tests.cases.libs
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4206
|
2010-10-03 16:27:27 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-09 05:13:11 +00:00
|
|
|
|
|
|
|
App::uses('CakeSession', 'Model/Datasource');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-04-04 07:21:01 +00:00
|
|
|
class TestCakeSession extends CakeSession {
|
2010-07-05 07:01:22 +00:00
|
|
|
public static function setUserAgent($value) {
|
|
|
|
self::$_userAgent = $value;
|
2010-04-04 07:21:01 +00:00
|
|
|
}
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-07-22 09:09:48 +00:00
|
|
|
public static function setHost($host) {
|
|
|
|
self::_setHost($host);
|
|
|
|
}
|
2010-04-04 07:21:01 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-17 00:19:12 +00:00
|
|
|
* CakeSessionTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.tests.cases.libs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-01-17 00:19:12 +00:00
|
|
|
class CakeSessionTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-25 04:42:28 +00:00
|
|
|
protected static $_gcDivisor;
|
|
|
|
|
2009-04-13 19:47:40 +00:00
|
|
|
/**
|
|
|
|
* Fixtures used in the SessionTest
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $fixtures = array('core.session');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2010-07-25 04:42:28 +00:00
|
|
|
* setup before class.
|
2009-03-26 14:25:47 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-07-25 04:42:28 +00:00
|
|
|
public static function setupBeforeClass() {
|
2009-03-26 14:25:47 +00:00
|
|
|
// Make sure garbage colector will be called
|
2010-07-25 04:42:28 +00:00
|
|
|
self::$_gcDivisor = ini_get('session.gc_divisor');
|
2009-03-26 14:25:47 +00:00
|
|
|
ini_set('session.gc_divisor', '1');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-26 14:25:47 +00:00
|
|
|
/**
|
2010-07-25 04:42:28 +00:00
|
|
|
* teardown after class
|
2009-03-26 14:25:47 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-07-25 04:42:28 +00:00
|
|
|
public static function teardownAfterClass() {
|
2009-03-26 14:25:47 +00:00
|
|
|
// Revert to the default setting
|
2010-07-25 04:42:28 +00:00
|
|
|
ini_set('session.gc_divisor', self::$_gcDivisor);
|
2009-03-26 14:25:47 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-26 14:25:47 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* setUp method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-07-25 21:50:08 +00:00
|
|
|
function setup() {
|
|
|
|
parent::setup();
|
2010-07-25 04:42:28 +00:00
|
|
|
Configure::write('Session', array(
|
|
|
|
'defaults' => 'php',
|
|
|
|
'cookie' => 'cakephp',
|
|
|
|
'timeout' => 120,
|
|
|
|
'cookieTimeout' => 120,
|
|
|
|
'ini' => array(),
|
|
|
|
));
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::init();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-13 19:47:40 +00:00
|
|
|
/**
|
|
|
|
* tearDown method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
2009-07-24 19:18:37 +00:00
|
|
|
*/
|
2010-07-25 21:50:08 +00:00
|
|
|
function teardown() {
|
2010-07-27 02:38:40 +00:00
|
|
|
if (TestCakeSession::started()) {
|
2010-09-30 03:01:23 +00:00
|
|
|
TestCakeSession::clear();
|
2010-07-27 02:38:40 +00:00
|
|
|
}
|
2010-07-25 04:42:28 +00:00
|
|
|
unset($_SESSION);
|
2010-09-28 04:03:23 +00:00
|
|
|
parent::teardown();
|
2010-07-25 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test setting ini properties with Session configuration.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testSessionConfigIniSetting() {
|
2010-07-27 02:38:40 +00:00
|
|
|
$_SESSION = null;
|
2010-07-25 04:42:28 +00:00
|
|
|
|
|
|
|
Configure::write('Session', array(
|
2010-09-20 02:58:30 +00:00
|
|
|
'cookie' => 'test',
|
2010-07-27 02:38:40 +00:00
|
|
|
'checkAgent' => false,
|
2010-07-25 04:42:28 +00:00
|
|
|
'timeout' => 86400,
|
|
|
|
'ini' => array(
|
|
|
|
'session.referer_check' => 'example.com',
|
|
|
|
'session.use_trans_sid' => false
|
|
|
|
)
|
|
|
|
));
|
|
|
|
TestCakeSession::start();
|
|
|
|
$this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect');
|
|
|
|
$this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect');
|
2010-09-20 02:58:30 +00:00
|
|
|
$this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect');
|
2010-07-25 04:42:28 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-06-01 16:52:05 +00:00
|
|
|
/**
|
|
|
|
* testSessionPath
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testSessionPath() {
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::init('/index.php');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(TestCakeSession::$path, '/');
|
2009-06-03 16:22:00 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::init('/sub_dir/index.php');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(TestCakeSession::$path, '/sub_dir/');
|
2010-07-22 08:42:56 +00:00
|
|
|
}
|
2009-06-03 16:22:00 +00:00
|
|
|
|
2010-07-22 08:42:56 +00:00
|
|
|
/**
|
|
|
|
* testCakeSessionPathEmpty
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
2011-04-17 10:35:21 +00:00
|
|
|
*/
|
2010-07-22 08:42:56 +00:00
|
|
|
function testCakeSessionPathEmpty() {
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::init('');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(TestCakeSession::$path, '/', 'Session path is empty, with "" as $base needs to be /');
|
2009-06-01 16:52:05 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-22 08:42:56 +00:00
|
|
|
/**
|
|
|
|
* testCakeSessionPathContainsParams
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testCakeSessionPathContainsQuestion() {
|
|
|
|
TestCakeSession::init('/index.php?');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(TestCakeSession::$path, '/');
|
2010-07-22 08:42:56 +00:00
|
|
|
}
|
|
|
|
|
2010-07-22 09:09:48 +00:00
|
|
|
/**
|
|
|
|
* testSetHost
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testSetHost() {
|
|
|
|
TestCakeSession::init();
|
|
|
|
TestCakeSession::setHost('cakephp.org');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(TestCakeSession::$host, 'cakephp.org');
|
2010-07-22 09:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSetHostWithPort
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testSetHostWithPort() {
|
|
|
|
TestCakeSession::init();
|
|
|
|
TestCakeSession::setHost('cakephp.org:443');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(TestCakeSession::$host, 'cakephp.org');
|
2010-07-22 09:09:48 +00:00
|
|
|
}
|
|
|
|
|
2010-07-25 23:55:02 +00:00
|
|
|
/**
|
|
|
|
* test valid with bogus user agent.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testValidBogusUserAgent() {
|
|
|
|
Configure::write('Session.checkAgent', true);
|
|
|
|
TestCakeSession::start();
|
|
|
|
$this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid');
|
|
|
|
|
|
|
|
TestCakeSession::userAgent('bogus!');
|
|
|
|
$this->assertFalse(TestCakeSession::valid(), 'user agent mismatch should fail.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test valid with bogus user agent.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testValidTimeExpiry() {
|
|
|
|
Configure::write('Session.checkAgent', true);
|
|
|
|
TestCakeSession::start();
|
|
|
|
$this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid');
|
|
|
|
|
|
|
|
TestCakeSession::$time = strtotime('next year');
|
|
|
|
$this->assertFalse(TestCakeSession::valid(), 'time should cause failure.');
|
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testCheck method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testCheck() {
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'value');
|
|
|
|
$this->assertTrue(TestCakeSession::check('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase'), false);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testSimpleRead method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testSimpleRead() {
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('testing', '1,2,3');
|
|
|
|
$result = TestCakeSession::read('testing');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('1,2,3', $result);
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('testing', array('1' => 'one', '2' => 'two','3' => 'three'));
|
|
|
|
$result = TestCakeSession::read('testing.1');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('one', $result);
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$result = TestCakeSession::read('testing');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(array('1' => 'one', '2' => 'two', '3' => 'three'), $result);
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$result = TestCakeSession::read();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue(isset($result['testing']));
|
|
|
|
$this->assertTrue(isset($result['Config']));
|
|
|
|
$this->assertTrue(isset($result['Config']['userAgent']));
|
2008-11-08 02:58:37 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('This.is.a.deep.array.my.friend', 'value');
|
|
|
|
$result = TestCakeSession::read('This.is.a.deep.array.my.friend');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals($result, 'value');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-22 09:19:53 +00:00
|
|
|
/**
|
|
|
|
* testReadyEmpty
|
|
|
|
*
|
2010-07-22 09:24:11 +00:00
|
|
|
* @return void
|
2010-07-22 09:19:53 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function testReadyEmpty() {
|
|
|
|
$this->assertFalse(TestCakeSession::read(''));
|
|
|
|
}
|
|
|
|
|
2010-07-16 03:54:01 +00:00
|
|
|
/**
|
|
|
|
* test writing a hash of values/
|
|
|
|
*
|
|
|
|
* @return void
|
2010-07-22 09:24:11 +00:00
|
|
|
* @access public
|
2010-07-16 03:54:01 +00:00
|
|
|
*/
|
|
|
|
function testWriteArray() {
|
|
|
|
$result = TestCakeSession::write(array(
|
|
|
|
'one' => 1,
|
|
|
|
'two' => 2,
|
|
|
|
'three' => array('something'),
|
|
|
|
'null' => null
|
|
|
|
));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertEquals(1, TestCakeSession::read('one'));
|
|
|
|
$this->assertEquals(array('something'), TestCakeSession::read('three'));
|
|
|
|
$this->assertEquals(null, TestCakeSession::read('null'));
|
|
|
|
}
|
|
|
|
|
2010-07-22 09:24:11 +00:00
|
|
|
/**
|
|
|
|
* testWriteEmptyKey
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function testWriteEmptyKey() {
|
|
|
|
$this->assertFalse(TestCakeSession::write('', 'graham'));
|
|
|
|
$this->assertFalse(TestCakeSession::write('', ''));
|
|
|
|
$this->assertFalse(TestCakeSession::write(''));
|
|
|
|
}
|
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testId method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testId() {
|
2010-12-11 03:00:44 +00:00
|
|
|
TestCakeSession::destroy();
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$result = TestCakeSession::id();
|
2010-12-10 04:55:38 +00:00
|
|
|
$expected = session_id();
|
2010-12-11 03:00:44 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::id('MySessionId');
|
|
|
|
$result = TestCakeSession::id();
|
2010-12-11 03:00:44 +00:00
|
|
|
$this->assertEquals('MySessionId', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testStarted method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testStarted() {
|
|
|
|
unset($_SESSION);
|
2010-01-15 03:47:49 +00:00
|
|
|
$_SESSION = null;
|
2010-12-10 04:55:38 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::started());
|
|
|
|
$this->assertTrue(TestCakeSession::start());
|
2010-07-25 23:55:02 +00:00
|
|
|
$this->assertTrue(TestCakeSession::started());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testError method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testError() {
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::read('Does.not.exist');
|
|
|
|
$result = TestCakeSession::error();
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals("Does.not.exist doesn't exist", $result);
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::delete('Failing.delete');
|
|
|
|
$result = TestCakeSession::error();
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals("Failing.delete doesn't exist", $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testDel method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2009-10-02 17:11:43 +00:00
|
|
|
function testDelete() {
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::write('Delete.me', 'Clearing out'));
|
|
|
|
$this->assertTrue(TestCakeSession::delete('Delete.me'));
|
|
|
|
$this->assertFalse(TestCakeSession::check('Delete.me'));
|
|
|
|
$this->assertTrue(TestCakeSession::check('Delete'));
|
|
|
|
|
|
|
|
$this->assertTrue(TestCakeSession::write('Clearing.sale', 'everything must go'));
|
|
|
|
$this->assertTrue(TestCakeSession::delete('Clearing'));
|
|
|
|
$this->assertFalse(TestCakeSession::check('Clearing.sale'));
|
|
|
|
$this->assertFalse(TestCakeSession::check('Clearing'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testDestroy method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testDestroy() {
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('bulletProof', 'invicible');
|
|
|
|
$id = TestCakeSession::id();
|
|
|
|
TestCakeSession::destroy();
|
2010-07-08 03:33:38 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::check('bulletProof'));
|
|
|
|
$this->assertNotEqual($id, TestCakeSession::id());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testCheckingSavedEmpty method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testCheckingSavedEmpty() {
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::write('SessionTestCase', 0));
|
|
|
|
$this->assertTrue(TestCakeSession::check('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::write('SessionTestCase', '0'));
|
|
|
|
$this->assertTrue(TestCakeSession::check('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::write('SessionTestCase', false));
|
|
|
|
$this->assertTrue(TestCakeSession::check('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::write('SessionTestCase', null));
|
|
|
|
$this->assertFalse(TestCakeSession::check('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testCheckKeyWithSpaces method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testCheckKeyWithSpaces() {
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::write('Session Test', "test"));
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('test', TestCakeSession::check('Session Test'));
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::delete('Session Test');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::write('Session Test.Test Case', "test"));
|
|
|
|
$this->assertTrue(TestCakeSession::check('Session Test.Test Case'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-22 09:11:53 +00:00
|
|
|
/**
|
|
|
|
* testCheckEmpty
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testCheckEmpty() {
|
|
|
|
$this->assertFalse(TestCakeSession::check());
|
|
|
|
}
|
|
|
|
|
2009-11-22 01:44:35 +00:00
|
|
|
/**
|
|
|
|
* test key exploitation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testKeyExploit() {
|
|
|
|
$key = "a'] = 1; phpinfo(); \$_SESSION['a";
|
2010-07-05 07:01:22 +00:00
|
|
|
$result = TestCakeSession::write($key, 'haxored');
|
2009-11-22 01:44:35 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$result = TestCakeSession::read($key);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('haxored', $result);
|
2009-11-22 01:44:35 +00:00
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testReadingSavedEmpty method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testReadingSavedEmpty() {
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 0);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', '0');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', false);
|
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', null);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 23:57:30 +00:00
|
|
|
/**
|
|
|
|
* testCheckUserAgentFalse method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 23:57:30 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testCheckUserAgentFalse() {
|
|
|
|
Configure::write('Session.checkAgent', false);
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::setUserAgent(md5('http://randomdomainname.com' . Configure::read('Security.salt')));
|
|
|
|
$this->assertTrue(TestCakeSession::valid());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 23:57:30 +00:00
|
|
|
/**
|
|
|
|
* testCheckUserAgentTrue method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 23:57:30 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testCheckUserAgentTrue() {
|
|
|
|
Configure::write('Session.checkAgent', true);
|
2010-07-08 03:33:38 +00:00
|
|
|
TestCakeSession::$error = false;
|
|
|
|
$agent = md5('http://randomdomainname.com' . Configure::read('Security.salt'));
|
|
|
|
|
|
|
|
TestCakeSession::write('Config.userAgent', md5('Hacking you!'));
|
|
|
|
TestCakeSession::setUserAgent($agent);
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::valid());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 23:57:30 +00:00
|
|
|
/**
|
|
|
|
* testReadAndWriteWithDatabaseStorage method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 23:57:30 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-06-10 22:38:05 +00:00
|
|
|
function testReadAndWriteWithCakeStorage() {
|
2010-07-25 05:14:41 +00:00
|
|
|
Configure::write('Session.defaults', 'cake');
|
|
|
|
|
|
|
|
TestCakeSession::init();
|
2010-07-27 02:38:40 +00:00
|
|
|
TestCakeSession::start();
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 0);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', '0');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', false);
|
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', null);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'This is a Test');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'This is a Test');
|
|
|
|
TestCakeSession::write('SessionTestCase', 'This was updated');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::destroy();
|
2010-07-16 03:48:02 +00:00
|
|
|
$this->assertNull(TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-25 23:09:29 +00:00
|
|
|
/**
|
|
|
|
* test using a handler from app/libs.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testUsingAppLibsHandler() {
|
|
|
|
App::build(array(
|
2011-04-10 19:32:19 +00:00
|
|
|
'Model/Datasource/Session' => array(
|
2011-04-17 10:35:21 +00:00
|
|
|
CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS . 'Session' . DS
|
2011-04-10 19:32:19 +00:00
|
|
|
),
|
2011-05-15 16:49:38 +00:00
|
|
|
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
2010-07-25 23:09:29 +00:00
|
|
|
), true);
|
|
|
|
Configure::write('Session', array(
|
|
|
|
'defaults' => 'cake',
|
|
|
|
'handler' => array(
|
|
|
|
'engine' => 'TestAppLibSession'
|
|
|
|
)
|
|
|
|
));
|
|
|
|
TestCakeSession::destroy();
|
|
|
|
$this->assertTrue(TestCakeSession::started());
|
|
|
|
|
|
|
|
App::build();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test using a handler from a plugin.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testUsingPluginHandler() {
|
|
|
|
App::build(array(
|
2011-05-15 16:49:38 +00:00
|
|
|
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
2010-07-25 23:09:29 +00:00
|
|
|
), true);
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-07-25 23:09:29 +00:00
|
|
|
Configure::write('Session', array(
|
|
|
|
'defaults' => 'cake',
|
|
|
|
'handler' => array(
|
|
|
|
'engine' => 'TestPlugin.TestPluginSession'
|
|
|
|
)
|
|
|
|
));
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-07-25 23:09:29 +00:00
|
|
|
TestCakeSession::destroy();
|
|
|
|
$this->assertTrue(TestCakeSession::started());
|
|
|
|
|
|
|
|
App::build();
|
|
|
|
}
|
|
|
|
|
2008-06-10 22:38:05 +00:00
|
|
|
/**
|
|
|
|
* testReadAndWriteWithDatabaseStorage method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testReadAndWriteWithCacheStorage() {
|
2010-07-25 05:14:41 +00:00
|
|
|
Configure::write('Session.defaults', 'cache');
|
|
|
|
|
|
|
|
TestCakeSession::init();
|
|
|
|
TestCakeSession::destroy();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 0);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', '0');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', false);
|
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', null);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'This is a Test');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'This is a Test');
|
|
|
|
TestCakeSession::write('SessionTestCase', 'This was updated');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::destroy();
|
2010-07-16 03:48:02 +00:00
|
|
|
$this->assertNull(TestCakeSession::read('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-25 17:37:56 +00:00
|
|
|
/**
|
|
|
|
* test that changing the config name of the cache config works.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testReadAndWriteWithCustomCacheConfig() {
|
|
|
|
Configure::write('Session.defaults', 'cache');
|
|
|
|
Configure::write('Session.handler.config', 'session_test');
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-07-25 17:37:56 +00:00
|
|
|
Cache::config('session_test', array(
|
|
|
|
'engine' => 'File',
|
|
|
|
'prefix' => 'session_test_',
|
|
|
|
));
|
|
|
|
|
|
|
|
TestCakeSession::init();
|
2010-07-27 02:38:40 +00:00
|
|
|
TestCakeSession::start();
|
2010-07-25 17:37:56 +00:00
|
|
|
|
|
|
|
TestCakeSession::write('SessionTestCase', 'Some value');
|
|
|
|
$this->assertEquals('Some value', TestCakeSession::read('SessionTestCase'));
|
2010-07-27 02:38:40 +00:00
|
|
|
$id = TestCakeSession::id();
|
2010-07-25 17:37:56 +00:00
|
|
|
|
2010-07-27 02:38:40 +00:00
|
|
|
Cache::delete($id, 'session_test');
|
2010-07-25 17:37:56 +00:00
|
|
|
}
|
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-10 22:38:05 +00:00
|
|
|
* testReadAndWriteWithDatabaseStorage method
|
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-06-10 22:38:05 +00:00
|
|
|
function testReadAndWriteWithDatabaseStorage() {
|
2010-07-25 05:14:41 +00:00
|
|
|
Configure::write('Session.defaults', 'database');
|
2010-07-25 15:42:05 +00:00
|
|
|
Configure::write('Session.handler.table', 'sessions');
|
|
|
|
Configure::write('Session.handler.model', 'Session');
|
2010-09-20 02:58:30 +00:00
|
|
|
Configure::write('Session.handler.database', 'test');
|
2010-07-25 05:14:41 +00:00
|
|
|
|
|
|
|
TestCakeSession::init();
|
2010-07-27 02:38:40 +00:00
|
|
|
TestCakeSession::start();
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 0);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', '0');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', false);
|
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', null);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'This is a Test');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-27 02:38:40 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'Some additional data');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('Some additional data', TestCakeSession::read('SessionTestCase'));
|
2009-04-13 19:47:40 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::destroy();
|
2010-07-16 03:48:02 +00:00
|
|
|
$this->assertNull(TestCakeSession::read('SessionTestCase'));
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-09-30 03:01:23 +00:00
|
|
|
Configure::write('Session', array(
|
|
|
|
'defaults' => 'php'
|
|
|
|
));
|
|
|
|
TestCakeSession::init();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-01-21 16:59:49 +00:00
|
|
|
|
2010-07-25 21:50:08 +00:00
|
|
|
/**
|
|
|
|
* testSessionTimeout method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testSessionTimeout() {
|
|
|
|
Configure::write('debug', 2);
|
2010-07-27 02:42:32 +00:00
|
|
|
Configure::write('Session.autoRegenerate', false);
|
2010-07-25 21:50:08 +00:00
|
|
|
|
|
|
|
$timeoutSeconds = Configure::read('Session.timeout') * 60;
|
|
|
|
|
|
|
|
TestCakeSession::destroy();
|
|
|
|
TestCakeSession::write('Test', 'some value');
|
|
|
|
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(time() + $timeoutSeconds, CakeSession::$sessionTime);
|
|
|
|
$this->assertEquals(10, $_SESSION['Config']['countdown']);
|
|
|
|
$this->assertEquals(CakeSession::$sessionTime, $_SESSION['Config']['time']);
|
|
|
|
$this->assertEquals(time(), CakeSession::$time);
|
|
|
|
$this->assertEquals(time() + $timeoutSeconds, $_SESSION['Config']['time']);
|
2010-07-25 21:50:08 +00:00
|
|
|
|
|
|
|
Configure::write('Session.harden', true);
|
|
|
|
TestCakeSession::destroy();
|
|
|
|
|
|
|
|
TestCakeSession::write('Test', 'some value');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(time() + $timeoutSeconds, CakeSession::$sessionTime);
|
|
|
|
$this->assertEquals(10, $_SESSION['Config']['countdown']);
|
|
|
|
$this->assertEquals(CakeSession::$sessionTime, $_SESSION['Config']['time']);
|
|
|
|
$this->assertEquals(time(), CakeSession::$time);
|
|
|
|
$this->assertEquals(CakeSession::$time + $timeoutSeconds, $_SESSION['Config']['time']);
|
2010-07-25 21:50:08 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|