SessionHelperTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * SessionHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Controller', 'Controller');
  20. App::uses('View', 'View');
  21. App::uses('SessionHelper', 'View/Helper');
  22. /**
  23. * SessionHelperTest class
  24. *
  25. * @package Cake.Test.Case.View.Helper
  26. */
  27. class SessionHelperTest extends CakeTestCase {
  28. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. parent::setUp();
  35. $controller = null;
  36. $this->View = new View($controller);
  37. $this->Session = new SessionHelper($this->View);
  38. CakeSession::start();
  39. if (!CakeSession::started()) {
  40. CakeSession::start();
  41. }
  42. $_SESSION = array(
  43. 'test' => 'info',
  44. 'Message' => array(
  45. 'flash' => array(
  46. 'element' => 'default',
  47. 'params' => array(),
  48. 'message' => 'This is a calling'
  49. ),
  50. 'notification' => array(
  51. 'element' => 'session_helper',
  52. 'params' => array('title' => 'Notice!', 'name' => 'Alert!'),
  53. 'message' => 'This is a test of the emergency broadcasting system',
  54. ),
  55. 'classy' => array(
  56. 'element' => 'default',
  57. 'params' => array('class' => 'positive'),
  58. 'message' => 'Recorded'
  59. ),
  60. 'bare' => array(
  61. 'element' => null,
  62. 'message' => 'Bare message',
  63. 'params' => array(),
  64. ),
  65. ),
  66. 'Deeply' => array('nested' => array('key' => 'value')),
  67. );
  68. }
  69. /**
  70. * tearDown method
  71. *
  72. * @return void
  73. */
  74. public function tearDown() {
  75. $_SESSION = array();
  76. unset($this->View, $this->Session);
  77. CakePlugin::unload();
  78. parent::tearDown();
  79. }
  80. /**
  81. * testRead method
  82. *
  83. * @return void
  84. */
  85. public function testRead() {
  86. $result = $this->Session->read('Deeply.nested.key');
  87. $this->assertEquals('value', $result);
  88. $result = $this->Session->read('test');
  89. $this->assertEquals('info', $result);
  90. }
  91. /**
  92. * testCheck method
  93. *
  94. * @return void
  95. */
  96. public function testCheck() {
  97. $this->assertTrue($this->Session->check('test'));
  98. $this->assertTrue($this->Session->check('Message.flash.element'));
  99. $this->assertFalse($this->Session->check('Does.not.exist'));
  100. $this->assertFalse($this->Session->check('Nope'));
  101. }
  102. /**
  103. * testFlash method
  104. *
  105. * @return void
  106. */
  107. public function testFlash() {
  108. $result = $this->Session->flash('flash');
  109. $expected = '<div id="flashMessage" class="message">This is a calling</div>';
  110. $this->assertEquals($expected, $result);
  111. $this->assertFalse($this->Session->check('Message.flash'));
  112. $expected = '<div id="classyMessage" class="positive">Recorded</div>';
  113. $result = $this->Session->flash('classy');
  114. $this->assertEquals($expected, $result);
  115. App::build(array(
  116. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  117. ));
  118. $result = $this->Session->flash('notification');
  119. $result = str_replace("\r\n", "\n", $result);
  120. $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>";
  121. $this->assertEquals($expected, $result);
  122. $this->assertFalse($this->Session->check('Message.notification'));
  123. $result = $this->Session->flash('bare');
  124. $expected = 'Bare message';
  125. $this->assertEquals($expected, $result);
  126. $this->assertFalse($this->Session->check('Message.bare'));
  127. }
  128. /**
  129. * test flash() with the attributes.
  130. *
  131. * @return void
  132. */
  133. public function testFlashAttributes() {
  134. $result = $this->Session->flash('flash', array('params' => array('class' => 'test-message')));
  135. $expected = '<div id="flashMessage" class="test-message">This is a calling</div>';
  136. $this->assertEquals($expected, $result);
  137. $this->assertFalse($this->Session->check('Message.flash'));
  138. }
  139. /**
  140. * test setting the element from the attrs.
  141. *
  142. * @return void
  143. */
  144. public function testFlashElementInAttrs() {
  145. App::build(array(
  146. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  147. ));
  148. $result = $this->Session->flash('flash', array(
  149. 'element' => 'session_helper',
  150. 'params' => array('title' => 'Notice!', 'name' => 'Alert!')
  151. ));
  152. $expected = "<div id=\"notificationLayout\">\n\t<h1>Alert!</h1>\n\t<h3>Notice!</h3>\n\t<p>This is a calling</p>\n</div>";
  153. $this->assertTextEquals($expected, $result);
  154. }
  155. /**
  156. * test using elements in plugins.
  157. *
  158. * @return void
  159. */
  160. public function testFlashWithPluginElement() {
  161. App::build(array(
  162. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  163. ));
  164. CakePlugin::load('TestPlugin');
  165. $result = $this->Session->flash('flash', array(
  166. 'element' => 'plugin_element',
  167. 'params' => array('plugin' => 'TestPlugin')
  168. ));
  169. $expected = 'this is the plugin element using params[plugin]';
  170. $this->assertEquals($expected, $result);
  171. }
  172. }