Skip to content

Commit

Permalink
Adding test cases for DatabaseSession and fixing a test case in Cache…
Browse files Browse the repository at this point in the history
…Session.
  • Loading branch information
markstory committed Jul 28, 2010
1 parent 7d2d2aa commit 28b9ed7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cake/libs/session/database_session.php
Expand Up @@ -77,7 +77,7 @@ public static function read($id) {
* @access private
*/
public static function write($id, $data) {
$expires = time() + Configure::read('Session.timeout') * Security::inactiveMins();
$expires = time() + (Configure::read('Session.timeout') * 60);
$model =& ClassRegistry::getObject('Session');
$return = $model->save(compact('id', 'data', 'expires'));
return $return;
Expand Down
3 changes: 2 additions & 1 deletion cake/tests/cases/libs/session/cache_session.test.php
Expand Up @@ -35,8 +35,9 @@ public static function setupBeforeClass() {
'engine' => 'File',
'prefix' => 'session_test_'
));
Configure::write('Session.handler.config', 'session_test');
self::$_sessionBackup = Configure::read('Session');

Configure::write('Session.handler.config', 'session_test');
}

/**
Expand Down
100 changes: 99 additions & 1 deletion cake/tests/cases/libs/session/database_session.test.php
Expand Up @@ -18,9 +18,107 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::import('Core', 'Model');
App::import('Core', 'CakeSession');
App::import('Core', 'session/DatabaseSession');

class SessionTestModel extends Model {
var $name = 'SessionTestModel';
var $useTable = 'sessions';
}

/**
* Database session test.
*
* @package cake.tests.cases.libs.session
*/
class DatabaseSessionTest extends CakeTestCase {


protected static $_sessionBackup;

/**
* fixtures
*
* @var string
*/
public $fixtures = array('core.session');

/**
* test case startup
*
* @return void
*/
public static function setupBeforeClass() {
self::$_sessionBackup = Configure::read('Session');
ClassRegistry::init(array(
'class' => 'SessionTestModel',
'alias' => 'Session',
'ds' => 'test_suite'
));
Configure::write('Session.handler.model', 'SessionTestModel');
Configure::write('Session.timeout', 100);
}

/**
* cleanup after test case.
*
* @return void
*/
public static function teardownAfterClass() {
Configure::write('Session', self::$_sessionBackup);
}

/**
* test opening the session
*
* @return void
*/
function testOpen() {
$this->assertTrue(DatabaseSession::open());
}

/**
* test write()
*
* @return void
*/
function testWrite() {
$result = DatabaseSession::write('foo', 'Some value');
$expected = array(
'Session' => array(
'id' => 'foo',
'data' => 'Some value',
'expires' => time() + (Configure::read('Session.timeout') * 60)
)
);
$this->assertEquals($expected, $result);
}

/**
* test read()
*
* @return void
*/
function testRead() {
DatabaseSession::write('foo', 'Some value');

$result = DatabaseSession::read('foo');
$expected = 'Some value';
$this->assertEquals($expected, $result);

$result = DatabaseSession::read('made up value');
$this->assertFalse($result);
}

/**
* test blowing up the session.
*
* @return void
*/
function testDestroy() {
DatabaseSession::write('foo', 'Some value');

$this->assertTrue(DatabaseSession::destroy('foo'), 'Destroy failed');
$this->assertFalse(DatabaseSession::read('foo'), 'Value still present.');
}
}

0 comments on commit 28b9ed7

Please sign in to comment.