2008-05-30 11:40:08 +00:00
< ? php
/**
2009-03-18 17:55:58 +00:00
* SessionComponentTest file
2008-05-30 11:40:08 +00:00
*
2017-06-10 22:15:34 +00:00
* CakePHP ( tm ) Tests < https :// book . cakephp . org / 2.0 / en / development / testing . html >
2017-06-10 22:10:52 +00:00
* Copyright ( c ) Cake Software Foundation , Inc . ( https :// cakefoundation . org )
2008-05-30 11:40:08 +00:00
*
2010-10-03 16:31:21 +00:00
* Licensed under The MIT License
2013-02-08 12:22:51 +00:00
* For full copyright and license information , please see the LICENSE . txt
2010-10-03 16:31:21 +00:00
* Redistributions of files must retain the above copyright notice
2008-05-30 11:40:08 +00:00
*
2017-06-10 22:10:52 +00:00
* @ copyright Copyright ( c ) Cake Software Foundation , Inc . ( https :// cakefoundation . org )
2017-06-10 22:15:34 +00:00
* @ link https :// book . cakephp . org / 2.0 / en / development / testing . html CakePHP ( tm ) Tests
2011-07-26 06:16:14 +00:00
* @ package Cake . Test . Case . Controller . Component
2008-10-30 17:30:26 +00:00
* @ since CakePHP ( tm ) v 1.2 . 0.5436
2017-06-10 22:23:14 +00:00
* @ license https :// opensource . org / licenses / mit - license . php MIT License
2008-05-30 11:40:08 +00:00
*/
2013-05-30 22:11:14 +00:00
2010-12-09 05:55:24 +00:00
App :: uses ( 'Controller' , 'Controller' );
App :: uses ( 'SessionComponent' , 'Controller/Component' );
2009-07-24 19:18:37 +00:00
2008-06-02 19:22:55 +00:00
/**
* SessionTestController class
2008-06-10 22:38:05 +00:00
*
2011-07-26 06:16:14 +00:00
* @ package Cake . Test . Case . Controller . Component
2008-06-02 19:22:55 +00:00
*/
2008-09-19 00:55:42 +00:00
class SessionTestController extends Controller {
2009-07-24 19:18:37 +00:00
2009-03-18 17:55:58 +00:00
/**
* uses property
*
* @ var array
*/
2010-04-04 07:14:00 +00:00
public $uses = array ();
2009-07-24 19:18:37 +00:00
2009-03-18 17:55:58 +00:00
/**
2012-03-12 02:20:25 +00:00
* sessionId method
2009-03-18 17:55:58 +00:00
*
* @ return string
*/
2012-03-12 02:20:25 +00:00
public function sessionId () {
2008-09-19 00:55:42 +00:00
return $this -> Session -> id ();
}
2012-03-12 02:20:25 +00:00
2008-09-19 00:55:42 +00:00
}
2009-07-24 19:18:37 +00:00
2008-09-19 00:55:42 +00:00
/**
* OrangeSessionTestController class
*
2011-07-26 06:16:14 +00:00
* @ package Cake . Test . Case . Controller . Component
2008-09-19 00:55:42 +00:00
*/
class OrangeSessionTestController extends Controller {
2009-07-24 19:18:37 +00:00
2009-03-18 17:55:58 +00:00
/**
* uses property
*
* @ var array
*/
2010-04-04 07:14:00 +00:00
public $uses = array ();
2009-07-24 19:18:37 +00:00
2009-03-18 17:55:58 +00:00
/**
2012-03-12 02:20:25 +00:00
* sessionId method
2009-03-18 17:55:58 +00:00
*
* @ return string
*/
2012-03-12 02:20:25 +00:00
public function sessionId () {
2008-09-19 00:55:42 +00:00
return $this -> Session -> id ();
}
2012-03-12 02:20:25 +00:00
2008-09-19 00:55:42 +00:00
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
2009-03-18 17:55:58 +00:00
* SessionComponentTest class
2008-05-30 11:40:08 +00:00
*
2011-07-26 06:16:14 +00:00
* @ package Cake . Test . Case . Controller . Component
2008-05-30 11:40:08 +00:00
*/
class SessionComponentTest extends CakeTestCase {
2009-07-24 19:18:37 +00:00
2010-07-25 23:57:48 +00:00
protected static $_sessionBackup ;
/**
* fixtures
*
* @ var string
*/
public $fixtures = array ( 'core.session' );
/**
* test case startup
*
* @ return void
*/
2022-12-28 07:03:39 +00:00
public static function setupBeforeClass () : void {
2015-07-21 08:22:53 +00:00
static :: $_sessionBackup = Configure :: read ( 'Session' );
2010-07-25 23:57:48 +00:00
Configure :: write ( 'Session' , array (
'defaults' => 'php' ,
'timeout' => 100 ,
2010-09-20 02:58:30 +00:00
'cookie' => 'test'
2010-07-25 23:57:48 +00:00
));
}
/**
* cleanup after test case .
*
* @ return void
*/
2022-12-28 07:03:39 +00:00
public static function teardownAfterClass () : void {
2015-07-21 08:22:53 +00:00
Configure :: write ( 'Session' , static :: $_sessionBackup );
2010-07-25 23:57:48 +00:00
}
2009-03-21 23:55:39 +00:00
/**
* setUp method
*
* @ return void
*/
2022-12-28 07:03:39 +00:00
public function setUp () : void {
2010-07-25 23:57:48 +00:00
parent :: setUp ();
2015-12-28 22:19:31 +00:00
$_SESSION = null ;
2010-07-31 19:34:27 +00:00
$this -> ComponentCollection = new ComponentCollection ();
2009-03-21 23:55:39 +00:00
}
2009-07-24 19:18:37 +00:00
2009-03-21 23:55:39 +00:00
/**
* tearDown method
*
* @ return void
*/
2022-12-28 07:03:39 +00:00
public function tearDown () : void {
2010-07-25 23:57:48 +00:00
parent :: tearDown ();
2010-07-21 03:35:59 +00:00
CakeSession :: destroy ();
2009-03-21 23:55:39 +00:00
}
2009-07-24 19:18:37 +00:00
2008-06-02 19:22:55 +00:00
/**
2010-07-25 23:57:48 +00:00
* ensure that session ids don ' t change when request action is called .
2008-06-10 22:38:05 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testSessionIdConsistentAcrossRequestAction () {
2016-04-08 12:33:26 +00:00
$Object = new CakeObject ();
2010-07-31 19:34:27 +00:00
$Session = new SessionComponent ( $this -> ComponentCollection );
2008-09-19 00:55:42 +00:00
$expected = $Session -> id ();
2012-03-12 02:20:25 +00:00
$result = $Object -> requestAction ( '/session_test/sessionId' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-09-19 00:55:42 +00:00
2012-03-12 02:20:25 +00:00
$result = $Object -> requestAction ( '/orange_session_test/sessionId' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-06-10 22:38:05 +00:00
/**
2008-06-02 19:22:55 +00:00
* testSessionValid method
2008-06-10 22:38:05 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testSessionValid () {
2010-07-31 19:34:27 +00:00
$Session = new SessionComponent ( $this -> ComponentCollection );
2008-06-10 22:38:05 +00:00
$this -> assertTrue ( $Session -> valid ());
2010-07-21 03:35:59 +00:00
Configure :: write ( 'Session.checkAgent' , true );
2010-06-09 21:15:34 +00:00
$Session -> userAgent ( 'rweerw' );
2008-05-30 11:40:08 +00:00
$this -> assertFalse ( $Session -> valid ());
2008-06-10 22:38:05 +00:00
2010-07-31 19:34:27 +00:00
$Session = new SessionComponent ( $this -> ComponentCollection );
2008-09-19 02:57:58 +00:00
$Session -> time = $Session -> read ( 'Config.time' ) + 1 ;
$this -> assertFalse ( $Session -> valid ());
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-06-10 22:38:05 +00:00
/**
2008-06-02 19:22:55 +00:00
* testSessionError method
2008-06-10 22:38:05 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testSessionError () {
2013-12-08 13:08:57 +00:00
CakeSession :: $lastError = null ;
2010-07-31 19:34:27 +00:00
$Session = new SessionComponent ( $this -> ComponentCollection );
2008-05-30 11:40:08 +00:00
$this -> assertFalse ( $Session -> error ());
}
2009-07-24 19:18:37 +00:00
2008-06-10 22:38:05 +00:00
/**
2008-06-02 19:22:55 +00:00
* testSessionReadWrite method
2008-06-10 22:38:05 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testSessionReadWrite () {
2010-07-31 19:34:27 +00:00
$Session = new SessionComponent ( $this -> ComponentCollection );
2008-06-10 22:38:05 +00:00
2010-06-09 21:15:34 +00:00
$this -> assertNull ( $Session -> read ( 'Test' ));
2008-06-10 22:38:05 +00:00
2008-05-30 11:40:08 +00:00
$this -> assertTrue ( $Session -> write ( 'Test' , 'some value' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'some value' , $Session -> read ( 'Test' ));
2009-09-01 02:50:46 +00:00
$Session -> delete ( 'Test' );
2008-06-10 22:38:05 +00:00
2008-05-30 11:40:08 +00:00
$this -> assertTrue ( $Session -> write ( 'Test.key.path' , 'some value' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'some value' , $Session -> read ( 'Test.key.path' ));
$this -> assertEquals ( array ( 'path' => 'some value' ), $Session -> read ( 'Test.key' ));
2008-05-30 11:40:08 +00:00
$this -> assertTrue ( $Session -> write ( 'Test.key.path2' , 'another value' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( array ( 'path' => 'some value' , 'path2' => 'another value' ), $Session -> read ( 'Test.key' ));
2009-09-01 02:50:46 +00:00
$Session -> delete ( 'Test' );
2008-06-10 22:38:05 +00:00
2008-05-30 11:40:08 +00:00
$array = array ( 'key1' => 'val1' , 'key2' => 'val2' , 'key3' => 'val3' );
$this -> assertTrue ( $Session -> write ( 'Test' , $array ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $Session -> read ( 'Test' ), $array );
2009-09-01 02:50:46 +00:00
$Session -> delete ( 'Test' );
2015-12-29 05:06:44 +00:00
2008-05-30 11:40:08 +00:00
$this -> assertTrue ( $Session -> write ( array ( 'Test' => 'some value' )));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'some value' , $Session -> read ( 'Test' ));
2009-09-01 02:50:46 +00:00
$Session -> delete ( 'Test' );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-06-10 22:38:05 +00:00
/**
2008-06-02 19:22:55 +00:00
* testSessionDelete method
2008-06-10 22:38:05 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testSessionDelete () {
2010-07-31 19:34:27 +00:00
$Session = new SessionComponent ( $this -> ComponentCollection );
2008-06-10 22:38:05 +00:00
2008-05-30 11:40:08 +00:00
$this -> assertFalse ( $Session -> delete ( 'Test' ));
2008-06-10 22:38:05 +00:00
2008-05-30 11:40:08 +00:00
$Session -> write ( 'Test' , 'some value' );
$this -> assertTrue ( $Session -> delete ( 'Test' ));
}
2009-07-24 19:18:37 +00:00
2008-06-10 22:38:05 +00:00
/**
2008-06-05 15:20:45 +00:00
* testSessionCheck method
2008-06-10 22:38:05 +00:00
*
2008-06-05 15:20:45 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testSessionCheck () {
2010-07-31 19:34:27 +00:00
$Session = new SessionComponent ( $this -> ComponentCollection );
2008-06-10 22:38:05 +00:00
2008-05-30 11:40:08 +00:00
$this -> assertFalse ( $Session -> check ( 'Test' ));
2008-06-10 22:38:05 +00:00
2008-05-30 11:40:08 +00:00
$Session -> write ( 'Test' , 'some value' );
$this -> assertTrue ( $Session -> check ( 'Test' ));
$Session -> delete ( 'Test' );
}
2009-07-24 19:18:37 +00:00
2008-06-10 22:38:05 +00:00
/**
2008-06-05 15:20:45 +00:00
* testSessionFlash method
2008-06-10 22:38:05 +00:00
*
2008-06-05 15:20:45 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testSessionFlash () {
2010-07-31 19:34:27 +00:00
$Session = new SessionComponent ( $this -> ComponentCollection );
2008-06-10 22:38:05 +00:00
2008-05-30 11:40:08 +00:00
$this -> assertNull ( $Session -> read ( 'Message.flash' ));
2008-06-10 22:38:05 +00:00
2008-05-30 11:40:08 +00:00
$Session -> setFlash ( 'This is a test message' );
2017-08-30 15:06:56 +00:00
$this -> assertEquals ( array ( 'message' => 'This is a test message' , 'element' => 'default' , 'params' => array ()), $Session -> read ( 'Message.flash.0' ));
2008-06-10 22:38:05 +00:00
2008-05-30 11:40:08 +00:00
$Session -> setFlash ( 'This is a test message' , 'test' , array ( 'name' => 'Joel Moss' ));
2017-08-30 15:06:56 +00:00
$this -> assertEquals ( array ( 'message' => 'This is a test message' , 'element' => 'test' , 'params' => array ( 'name' => 'Joel Moss' )), $Session -> read ( 'Message.flash.1' ));
2008-06-10 22:38:05 +00:00
2008-05-30 11:40:08 +00:00
$Session -> setFlash ( 'This is a test message' , 'default' , array (), 'myFlash' );
2017-08-30 15:06:56 +00:00
$this -> assertEquals ( array ( 'message' => 'This is a test message' , 'element' => 'default' , 'params' => array ()), $Session -> read ( 'Message.myFlash.0' ));
2009-03-21 23:55:39 +00:00
2009-09-01 02:50:46 +00:00
$Session -> delete ( 'Message' );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-06-10 22:38:05 +00:00
/**
* testSessionId method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testSessionId () {
2008-06-10 22:38:05 +00:00
unset ( $_SESSION );
2010-07-31 19:34:27 +00:00
$Session = new SessionComponent ( $this -> ComponentCollection );
2014-01-20 07:56:35 +00:00
CakeSession :: start ();
2010-07-21 03:35:59 +00:00
$this -> assertEquals ( session_id (), $Session -> id ());
2008-06-10 22:38:05 +00:00
}
2009-07-24 19:18:37 +00:00
2008-06-10 22:38:05 +00:00
/**
2008-06-05 15:20:45 +00:00
* testSessionDestroy method
2008-06-10 22:38:05 +00:00
*
2008-06-05 15:20:45 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testSessionDestroy () {
2010-07-31 19:34:27 +00:00
$Session = new SessionComponent ( $this -> ComponentCollection );
2008-06-10 22:38:05 +00:00
2008-05-30 11:40:08 +00:00
$Session -> write ( 'Test' , 'some value' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'some value' , $Session -> read ( 'Test' ));
2008-05-30 11:40:08 +00:00
$Session -> destroy ( 'Test' );
$this -> assertNull ( $Session -> read ( 'Test' ));
}
2010-06-27 16:17:37 +00:00
2008-05-30 11:40:08 +00:00
}