2008-05-30 11:40:08 +00:00
< ? php
/**
2009-03-19 21:10:13 +00:00
* SessionHelperTest file
2008-05-30 11:40:08 +00:00
*
2010-10-03 16:15:01 +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 >
2011-05-29 21:31:39 +00:00
* Copyright 2005 - 2011 , Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2008-05-30 11:40:08 +00:00
*
2010-10-03 16:15:01 +00:00
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
2008-05-30 11:40:08 +00:00
*
2011-05-29 21:31:39 +00:00
* @ copyright Copyright 2005 - 2011 , 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
2011-07-26 06:16:14 +00:00
* @ package Cake . Test . Case . View . Helper
2008-10-30 17:30:26 +00:00
* @ since CakePHP ( tm ) v 1.2 . 0.4206
2010-10-03 16:15:01 +00:00
* @ license MIT License ( http :// www . opensource . org / licenses / mit - license . php )
2008-05-30 11:40:08 +00:00
*/
2010-12-10 06:23:27 +00:00
App :: uses ( 'Controller' , 'Controller' );
App :: uses ( 'View' , 'View' );
App :: uses ( 'SessionHelper' , 'View/Helper' );
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
2009-03-19 21:10:13 +00:00
* SessionHelperTest class
2008-05-30 11:40:08 +00:00
*
2011-07-26 06:16:14 +00:00
* @ package Cake . Test . Case . View . Helper
2008-05-30 11:40:08 +00:00
*/
class SessionHelperTest extends CakeTestCase {
2009-07-24 19:18:37 +00:00
2008-06-02 19:22:55 +00:00
/**
* setUp method
2008-06-20 19:30:29 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function setUp () {
2010-10-03 16:15:01 +00:00
parent :: setUp ();
2010-07-03 16:16:40 +00:00
$controller = null ;
$this -> View = new View ( $controller );
$this -> Session = new SessionHelper ( $this -> View );
2010-12-20 04:28:47 +00:00
CakeSession :: start ();
2008-05-30 11:40:08 +00:00
2011-01-09 05:09:01 +00:00
if ( ! CakeSession :: started ()) {
CakeSession :: start ();
}
2008-05-30 11:40:08 +00:00
$_SESSION = array (
'test' => 'info' ,
'Message' => array (
'flash' => array (
2009-09-01 04:03:56 +00:00
'element' => 'default' ,
2008-05-30 11:40:08 +00:00
'params' => array (),
'message' => 'This is a calling'
),
'notification' => array (
2009-09-01 04:03:56 +00:00
'element' => 'session_helper' ,
2008-05-30 11:40:08 +00:00
'params' => array ( 'title' => 'Notice!' , 'name' => 'Alert!' ),
'message' => 'This is a test of the emergency broadcasting system' ,
),
'classy' => array (
2009-09-01 04:03:56 +00:00
'element' => 'default' ,
2008-05-30 11:40:08 +00:00
'params' => array ( 'class' => 'positive' ),
'message' => 'Recorded'
),
'bare' => array (
2009-09-01 04:03:56 +00:00
'element' => null ,
2008-05-30 11:40:08 +00:00
'message' => 'Bare message' ,
'params' => array (),
),
),
'Deeply' => array ( 'nested' => array ( 'key' => 'value' )),
);
}
2009-07-24 19:18:37 +00:00
2008-06-02 19:22:55 +00:00
/**
* tearDown method
2008-06-20 19:30:29 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function tearDown () {
2008-05-30 11:40:08 +00:00
$_SESSION = array ();
2010-07-03 16:16:40 +00:00
unset ( $this -> View , $this -> Session );
2011-11-12 16:03:17 +00:00
CakePlugin :: unload ();
2010-10-03 16:15:01 +00:00
parent :: tearDown ();
2009-07-24 19:18:37 +00:00
}
2008-06-02 19:22:55 +00:00
/**
* testRead method
2008-06-20 19:30:29 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testRead () {
2008-05-30 11:40:08 +00:00
$result = $this -> Session -> read ( 'Deeply.nested.key' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $result , 'value' );
2008-05-30 11:40:08 +00:00
$result = $this -> Session -> read ( 'test' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $result , 'info' );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-06-02 19:22:55 +00:00
/**
* testCheck method
2008-06-20 19:30:29 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testCheck () {
2008-05-30 11:40:08 +00:00
$this -> assertTrue ( $this -> Session -> check ( 'test' ));
2009-09-01 04:03:56 +00:00
$this -> assertTrue ( $this -> Session -> check ( 'Message.flash.element' ));
2008-05-30 11:40:08 +00:00
$this -> assertFalse ( $this -> Session -> check ( 'Does.not.exist' ));
$this -> assertFalse ( $this -> Session -> check ( 'Nope' ));
}
2009-07-24 19:18:37 +00:00
2008-06-02 19:22:55 +00:00
/**
* testFlash method
2008-06-20 19:30:29 +00:00
*
2008-06-02 19:22:55 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testFlash () {
2011-02-21 19:07:33 +00:00
$result = $this -> Session -> flash ( 'flash' );
2008-05-30 11:40:08 +00:00
$expected = '<div id="flashMessage" class="message">This is a calling</div>' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$this -> assertFalse ( $this -> Session -> check ( 'Message.flash' ));
$expected = '<div id="classyMessage" class="positive">Recorded</div>' ;
2011-02-21 19:07:33 +00:00
$result = $this -> Session -> flash ( 'classy' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-06-20 19:30:29 +00:00
2009-06-11 16:13:16 +00:00
App :: build ( array (
2011-04-17 10:35:21 +00:00
'View' => array ( CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS )
2009-06-11 16:13:16 +00:00
));
2011-02-21 19:07:33 +00:00
$result = $this -> Session -> flash ( 'notification' );
2008-06-20 19:30:29 +00:00
$result = str_replace ( " \r \n " , " \n " , $result );
2008-05-30 11:40:08 +00:00
$expected = " <div id= \" notificationLayout \" > \n \t <h1>Alert!</h1> \n \t <h3>Notice!</h3> \n \t <p>This is a test of the emergency broadcasting system</p> \n </div> " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$this -> assertFalse ( $this -> Session -> check ( 'Message.notification' ));
2009-11-16 23:15:24 +00:00
$result = $this -> Session -> flash ( 'bare' );
2008-05-30 11:40:08 +00:00
$expected = 'Bare message' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$this -> assertFalse ( $this -> Session -> check ( 'Message.bare' ));
}
2009-07-24 19:18:37 +00:00
2011-02-21 19:07:33 +00:00
/**
* test flash () with the attributes .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testFlashAttributes () {
2011-02-21 19:07:33 +00:00
$result = $this -> Session -> flash ( 'flash' , array ( 'params' => array ( 'class' => 'test-message' )));
$expected = '<div id="flashMessage" class="test-message">This is a calling</div>' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2011-02-21 19:07:33 +00:00
$this -> assertFalse ( $this -> Session -> check ( 'Message.flash' ));
}
/**
* test setting the element from the attrs .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testFlashElementInAttrs () {
2011-02-21 19:07:33 +00:00
App :: build ( array (
2011-11-12 16:03:17 +00:00
'View' => array ( CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS )
2011-02-21 19:07:33 +00:00
));
$result = $this -> Session -> flash ( 'flash' , array (
2011-04-17 10:35:21 +00:00
'element' => 'session_helper' ,
2011-02-21 19:07:33 +00:00
'params' => array ( 'title' => 'Notice!' , 'name' => 'Alert!' )
));
$expected = " <div id= \" notificationLayout \" > \n \t <h1>Alert!</h1> \n \t <h3>Notice!</h3> \n \t <p>This is a calling</p> \n </div> " ;
2012-02-16 18:28:21 +00:00
$this -> assertEquals ( $expected , $result );
2011-02-21 19:07:33 +00:00
}
2011-11-12 16:03:17 +00:00
/**
2012-02-23 23:29:53 +00:00
* test using elements in plugins .
2011-11-12 16:03:17 +00:00
*
* @ return void
*/
public function testFlashWithPluginElement () {
App :: build ( array (
'Plugin' => array ( CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS )
));
CakePlugin :: load ( 'TestPlugin' );
$result = $this -> Session -> flash ( 'flash' , array (
'element' => 'plugin_element' ,
2011-11-12 16:54:57 +00:00
'params' => array ( 'plugin' => 'TestPlugin' )
2011-11-12 16:03:17 +00:00
));
$expected = 'this is the plugin element using params[plugin]' ;
$this -> assertEquals ( $expected , $result );
}
2008-05-30 11:40:08 +00:00
}