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

https://github.com/motaheri/Coordino · PHP · 236 lines · 133 code · 20 blank · 83 comment · 1 complexity · 3cc694a1a2b45125a9a757fb08bcfd4e MD5 · raw file

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