SessionHelperTest.php 4.8 KB

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