/cake/tests/cases/libs/view/helpers/session.test.php

https://github.com/hardsshah/bookmarks · PHP · 227 lines · 135 code · 13 blank · 79 comment · 1 complexity · 34fc0514fe8984e0168eba0a8b00ef55 MD5 · raw file

  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * SessionHelperTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs.view.helpers
  21. * @since CakePHP(tm) v 1.2.0.4206
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
  28. define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
  29. }
  30. App::import('Core', array('Helper', 'AppHelper', 'Controller', 'View'));
  31. App::import('Helper', array('Session'));
  32. /**
  33. * SessionHelperTest class
  34. *
  35. * @package cake
  36. * @subpackage cake.tests.cases.libs.view.helpers
  37. */
  38. class SessionHelperTest extends CakeTestCase {
  39. /**
  40. * setUp method
  41. *
  42. * @access public
  43. * @return void
  44. */
  45. function setUp() {
  46. $this->Session = new SessionHelper();
  47. $this->Session->__start();
  48. $_SESSION = array(
  49. 'test' => 'info',
  50. 'Message' => array(
  51. 'flash' => array(
  52. 'layout' => 'default',
  53. 'params' => array(),
  54. 'message' => 'This is a calling'
  55. ),
  56. 'notification' => array(
  57. 'layout' => 'session_helper',
  58. 'params' => array('title' => 'Notice!', 'name' => 'Alert!'),
  59. 'message' => 'This is a test of the emergency broadcasting system',
  60. ),
  61. 'classy' => array(
  62. 'layout' => 'default',
  63. 'params' => array('class' => 'positive'),
  64. 'message' => 'Recorded'
  65. ),
  66. 'bare' => array(
  67. 'layout' => null,
  68. 'message' => 'Bare message',
  69. 'params' => array(),
  70. ),
  71. ),
  72. 'Deeply' => array('nested' => array('key' => 'value')),
  73. );
  74. }
  75. /**
  76. * tearDown method
  77. *
  78. * @access public
  79. * @return void
  80. */
  81. function tearDown() {
  82. $_SESSION = array();
  83. unset($this->Session);
  84. }
  85. /**
  86. * testRead method
  87. *
  88. * @access public
  89. * @return void
  90. */
  91. function testRead() {
  92. $result = $this->Session->read('Deeply.nested.key');
  93. $this->assertEqual($result, 'value');
  94. $result = $this->Session->read('test');
  95. $this->assertEqual($result, 'info');
  96. }
  97. /**
  98. * testCheck method
  99. *
  100. * @access public
  101. * @return void
  102. */
  103. function testCheck() {
  104. $this->assertTrue($this->Session->check('test'));
  105. $this->assertTrue($this->Session->check('Message.flash.layout'));
  106. $this->assertFalse($this->Session->check('Does.not.exist'));
  107. $this->assertFalse($this->Session->check('Nope'));
  108. }
  109. /**
  110. * testWrite method
  111. *
  112. * @access public
  113. * @return void
  114. */
  115. function testWrite() {
  116. $this->expectError();
  117. $this->Session->write('NoWay', 'AccessDenied');
  118. }
  119. /**
  120. * testFlash method
  121. *
  122. * @access public
  123. * @return void
  124. */
  125. function testFlash() {
  126. ob_start();
  127. $this->Session->flash();
  128. $result = ob_get_contents();
  129. ob_clean();
  130. $expected = '<div id="flashMessage" class="message">This is a calling</div>';
  131. $this->assertEqual($result, $expected);
  132. $this->assertFalse($this->Session->check('Message.flash'));
  133. $expected = '<div id="classyMessage" class="positive">Recorded</div>';
  134. ob_start();
  135. $this->Session->flash('classy');
  136. $result = ob_get_clean();
  137. $this->assertEqual($result, $expected);
  138. Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS));
  139. $controller = new Controller();
  140. $this->Session->view = new View($controller);
  141. ob_start();
  142. $this->Session->flash('notification');
  143. $result = ob_get_contents();
  144. ob_clean();
  145. $result = str_replace("\r\n", "\n", $result);
  146. $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>";
  147. $this->assertEqual($result, $expected);
  148. $this->assertFalse($this->Session->check('Message.notification'));
  149. ob_start();
  150. $this->Session->flash('bare');
  151. $result = ob_get_contents();
  152. ob_clean();
  153. $expected = 'Bare message';
  154. $this->assertEqual($result, $expected);
  155. $this->assertFalse($this->Session->check('Message.bare'));
  156. }
  157. /**
  158. * testID method
  159. *
  160. * @access public
  161. * @return void
  162. */
  163. function testID() {
  164. $id = session_id();
  165. $result = $this->Session->id();
  166. $this->assertEqual($id, $result);
  167. }
  168. /**
  169. * testError method
  170. *
  171. * @access public
  172. * @return void
  173. */
  174. function testError() {
  175. $result = $this->Session->error();
  176. $this->assertFalse($result);
  177. $this->Session->read('CauseError');
  178. $result = $this->Session->error();
  179. $expected = "CauseError doesn't exist";
  180. $this->assertEqual($result, $expected);
  181. }
  182. /**
  183. * testDisabling method
  184. *
  185. * @access public
  186. * @return void
  187. */
  188. function testDisabling() {
  189. Configure::write('Session.start', false);
  190. $this->Session = new SessionHelper();
  191. $this->assertFalse($this->Session->check('test'));
  192. $this->assertFalse($this->Session->read('test'));
  193. $this->Session->read('CauseError');
  194. $this->assertFalse($this->Session->error());
  195. ob_start();
  196. $this->assertFalse($this->Session->flash('bare'));
  197. $result = ob_get_contents();
  198. ob_clean();
  199. $this->assertFalse($result);
  200. }
  201. /**
  202. * testValid method
  203. *
  204. * @access public
  205. * @return void
  206. */
  207. function testValid() {
  208. //wierd it always ends up false in the test suite
  209. //$this->assertFalse($this->Session->valid());
  210. }
  211. }
  212. ?>