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
*
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:15:01 +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:15:01 +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 . View . Helper
2008-10-30 17:30:26 +00:00
* @ since CakePHP ( tm ) v 1.2 . 0.4206
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
*/
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
*/
2022-12-28 07:03:39 +00:00
public function setUp () : void {
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 (
2017-08-22 15:06:37 +00:00
array (
'element' => 'default' ,
'params' => array (),
'message' => 'This is a calling'
),
2008-05-30 11:40:08 +00:00
),
'notification' => array (
2017-08-22 15:06:37 +00:00
array (
'element' => 'session_helper' ,
'params' => array ( 'title' => 'Notice!' , 'name' => 'Alert!' ),
'message' => 'This is a test of the emergency broadcasting system' ,
),
2008-05-30 11:40:08 +00:00
),
'classy' => array (
2017-08-22 15:06:37 +00:00
array (
'element' => 'default' ,
'params' => array ( 'class' => 'positive' ),
'message' => 'Recorded'
),
2008-05-30 11:40:08 +00:00
),
'bare' => array (
2017-08-22 15:06:37 +00:00
array (
'element' => null ,
'message' => 'Bare message' ,
'params' => array (),
),
2008-05-30 11:40:08 +00:00
),
),
'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
*/
2022-12-28 07:03:39 +00:00
public function tearDown () : void {
2008-05-30 11:40:08 +00:00
$_SESSION = array ();
2010-07-03 16:16:40 +00:00
unset ( $this -> View , $this -> Session );
2018-01-15 15:43:16 +00:00
CakeSession :: destroy ();
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' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'value' , $result );
2008-05-30 11:40:08 +00:00
$result = $this -> Session -> read ( 'test' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'info' , $result );
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' ));
2017-08-22 15:06:37 +00:00
$this -> assertTrue ( $this -> Session -> check ( 'Message.flash.0.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 (
2012-03-16 02:50:05 +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
2018-01-24 10:13:56 +00:00
/**
* Test the flash method works without any params being passed
*
* @ return void
*/
2018-01-25 02:17:52 +00:00
public function testFlashWithNoParams () {
2018-01-24 10:33:45 +00:00
$result = $this -> Session -> flash ();
$expected = '<div id="flashMessage" class="message">This is a calling</div>' ;
$this -> assertEquals ( $expected , $result );
$this -> assertFalse ( $this -> Session -> check ( 'Message.flash' ));
2018-01-24 10:13:56 +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 (
2012-03-16 02:50:05 +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-04-20 14:38:50 +00:00
$this -> assertTextEquals ( $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 (
2012-03-16 02:50:05 +00:00
'Plugin' => array ( CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS )
2011-11-12 16:03:17 +00:00
));
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
}