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
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
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
|
|
|
*
|
|
|
|
* Licensed under The Open Group Test Suite License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
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
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4206
|
|
|
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-07-30 14:00:36 +00:00
|
|
|
if (!class_exists('CakeSession')) {
|
2009-03-16 02:07:12 +00:00
|
|
|
App::import('Core', 'CakeSession');
|
2008-07-30 14:00:36 +00:00
|
|
|
}
|
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
|
|
|
}
|
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
|
|
|
*
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage 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();
|
|
|
|
TestCakeSession::destroy();
|
|
|
|
TestCakeSession::$watchKeys = array();
|
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() {
|
|
|
|
parent::teardown();
|
2010-07-25 04:42:28 +00:00
|
|
|
unset($_SESSION);
|
2010-07-05 07:01:22 +00:00
|
|
|
@session_destroy();
|
2010-07-25 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test setting ini properties with Session configuration.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testSessionConfigIniSetting() {
|
2010-07-25 23:55:02 +00:00
|
|
|
$_SESSION = null;
|
2010-07-25 04:42:28 +00:00
|
|
|
session_destroy();
|
|
|
|
|
|
|
|
Configure::write('Session', array(
|
|
|
|
'cookie' => 'test_suite',
|
|
|
|
'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');
|
|
|
|
$this->assertEquals('test_suite', ini_get('session.name'), 'Ini value is incorrect');
|
|
|
|
}
|
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');
|
|
|
|
$this->assertEqual('/', TestCakeSession::$path);
|
2009-06-03 16:22:00 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::init('/sub_dir/index.php');
|
|
|
|
$this->assertEqual('/sub_dir/', TestCakeSession::$path);
|
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
|
|
|
|
*/
|
|
|
|
function testCakeSessionPathEmpty() {
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::init('');
|
|
|
|
$this->assertEqual('/', TestCakeSession::$path, 'Session path is empty, with "" as $base needs to be / %s');
|
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?');
|
|
|
|
$this->assertEqual('/', TestCakeSession::$path);
|
|
|
|
}
|
|
|
|
|
2010-07-22 09:09:48 +00:00
|
|
|
/**
|
|
|
|
* testSetHost
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testSetHost() {
|
|
|
|
TestCakeSession::init();
|
|
|
|
TestCakeSession::setHost('cakephp.org');
|
|
|
|
$this->assertEqual('cakephp.org', TestCakeSession::$host);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSetHostWithPort
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testSetHostWithPort() {
|
|
|
|
TestCakeSession::init();
|
|
|
|
TestCakeSession::setHost('cakephp.org:443');
|
|
|
|
$this->assertEqual('cakephp.org', TestCakeSession::$host);
|
|
|
|
}
|
|
|
|
|
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');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertEqual($result, '1,2,3');
|
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');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertEqual($result, 'one');
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$result = TestCakeSession::read('testing');
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertEqual($result, array('1' => 'one', '2' => 'two', '3' => 'three'));
|
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');
|
2008-07-01 23:55:46 +00:00
|
|
|
$this->assertEqual('value', $result);
|
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() {
|
|
|
|
$expected = session_id();
|
2010-07-05 07:01:22 +00:00
|
|
|
$result = TestCakeSession::id();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::id('MySessionId');
|
|
|
|
$result = TestCakeSession::id();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertEqual($result, 'MySessionId');
|
|
|
|
}
|
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() {
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::started());
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($_SESSION);
|
2010-01-15 03:47:49 +00:00
|
|
|
$_SESSION = null;
|
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();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertEqual($result, "Does.not.exist doesn't exist");
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::delete('Failing.delete');
|
|
|
|
$result = TestCakeSession::error();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertEqual($result, "Failing.delete doesn't exist");
|
|
|
|
}
|
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
|
|
|
* testWatchVar method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2010-07-16 03:48:02 +00:00
|
|
|
* @expectedException Exception
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-07-16 03:48:02 +00:00
|
|
|
function testWatchVarWrite() {
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::watch(null));
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('Watching', "I'm watching you");
|
|
|
|
TestCakeSession::watch('Watching');
|
|
|
|
TestCakeSession::write('Watching', 'They found us!');
|
2010-07-16 03:48:02 +00:00
|
|
|
}
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2010-07-16 03:48:02 +00:00
|
|
|
/**
|
|
|
|
* undocumented function
|
|
|
|
*
|
|
|
|
* @expectedException Exception
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testWatchVarDelete() {
|
|
|
|
TestCakeSession::watch('Watching');
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::delete('Watching');
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::watch('Invalid.key'));
|
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
|
|
|
* testIgnore 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 testIgnore() {
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('Watching', "I'm watching you");
|
|
|
|
TestCakeSession::watch('Watching');
|
|
|
|
TestCakeSession::ignore('Watching');
|
|
|
|
$this->assertTrue(TestCakeSession::write('Watching', 'They found us!'));
|
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"));
|
|
|
|
$this->assertEqual(TestCakeSession::check('Session Test'), 'test');
|
|
|
|
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);
|
2009-11-22 01:44:35 +00:00
|
|
|
$this->assertEqual($result, 'haxored');
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), 0);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', '0');
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), '0');
|
|
|
|
$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);
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), null);
|
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-08 03:40:33 +00:00
|
|
|
session_write_close();
|
2010-07-25 05:14:41 +00:00
|
|
|
Configure::write('Session.defaults', 'cake');
|
|
|
|
|
|
|
|
TestCakeSession::init();
|
|
|
|
TestCakeSession::destroy();
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 0);
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), 0);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', '0');
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), '0');
|
|
|
|
$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);
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), null);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'This is a Test');
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), 'This is a Test');
|
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');
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), 'This was updated');
|
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(
|
|
|
|
'libs' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'libs' . DS),
|
|
|
|
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
|
|
|
|
), 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(
|
|
|
|
'libs' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'libs' . DS),
|
|
|
|
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
|
|
|
|
), true);
|
|
|
|
|
|
|
|
Configure::write('Session', array(
|
|
|
|
'defaults' => 'cake',
|
|
|
|
'handler' => array(
|
|
|
|
'engine' => 'TestPlugin.TestPluginSession'
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
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-08 03:40:33 +00:00
|
|
|
session_write_close();
|
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);
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), 0);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', '0');
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), '0');
|
|
|
|
$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);
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), null);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'This is a Test');
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), 'This is a Test');
|
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');
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), 'This was updated');
|
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() {
|
|
|
|
session_write_close();
|
|
|
|
Configure::write('Session.defaults', 'cache');
|
|
|
|
Configure::write('Session.handler.config', 'session_test');
|
|
|
|
|
|
|
|
Cache::config('session_test', array(
|
|
|
|
'engine' => 'File',
|
|
|
|
'prefix' => 'session_test_',
|
|
|
|
));
|
|
|
|
|
|
|
|
TestCakeSession::init();
|
|
|
|
TestCakeSession::destroy();
|
|
|
|
|
|
|
|
TestCakeSession::write('SessionTestCase', 'Some value');
|
|
|
|
$this->assertEquals('Some value', TestCakeSession::read('SessionTestCase'));
|
|
|
|
|
|
|
|
session_write_close();
|
|
|
|
|
|
|
|
$this->assertContains('Some value', Cache::read(TestCakeSession::id(), 'session_test'));
|
|
|
|
$this->assertFalse(Cache::read(TestCakeSession::id(), 'default'));
|
|
|
|
|
|
|
|
TestCakeSession::destroy();
|
|
|
|
Cache::delete(TestCakeSession::id(), 'session_test');
|
|
|
|
Cache::drop('session_test');
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
Configure::write('Session.handler.database', 'test_suite');
|
2010-07-25 05:14:41 +00:00
|
|
|
|
|
|
|
TestCakeSession::init();
|
|
|
|
TestCakeSession::destroy();
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 0);
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), 0);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', '0');
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), '0');
|
|
|
|
$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);
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), null);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'This is a Test');
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), 'This is a Test');
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'Some additional data');
|
|
|
|
$this->assertEqual(TestCakeSession::read('SessionTestCase'), 'Some additional data');
|
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'));
|
2008-06-11 08:54:27 +00:00
|
|
|
session_write_close();
|
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);
|
|
|
|
Configure::write('Session.harden', false);
|
|
|
|
|
|
|
|
$timeoutSeconds = Configure::read('Session.timeout') * 60;
|
|
|
|
|
|
|
|
session_destroy();
|
|
|
|
TestCakeSession::destroy();
|
|
|
|
TestCakeSession::write('Test', 'some value');
|
|
|
|
|
|
|
|
$this->assertEqual(CakeSession::$sessionTime, time() + $timeoutSeconds);
|
|
|
|
$this->assertEqual($_SESSION['Config']['countdown'], 10);
|
|
|
|
$this->assertEqual($_SESSION['Config']['time'], CakeSession::$sessionTime);
|
|
|
|
$this->assertEqual(CakeSession::$time, time());
|
|
|
|
$this->assertEqual($_SESSION['Config']['time'], time() + $timeoutSeconds);
|
|
|
|
|
|
|
|
Configure::write('Session.harden', true);
|
|
|
|
TestCakeSession::destroy();
|
|
|
|
|
|
|
|
TestCakeSession::write('Test', 'some value');
|
|
|
|
$this->assertEqual(CakeSession::$sessionTime, time() + $timeoutSeconds);
|
|
|
|
$this->assertEqual($_SESSION['Config']['countdown'], 10);
|
|
|
|
$this->assertEqual($_SESSION['Config']['time'], CakeSession::$sessionTime);
|
|
|
|
$this->assertEqual(CakeSession::$time, time());
|
|
|
|
$this->assertEqual($_SESSION['Config']['time'], CakeSession::$time + $timeoutSeconds);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|