SessionHelperTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\Request;
  22. use Cake\Network\Session;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\View\Helper\SessionHelper;
  25. use Cake\View\View;
  26. /**
  27. * SessionHelperTest class
  28. *
  29. */
  30. class SessionHelperTest extends TestCase {
  31. /**
  32. * setUp method
  33. *
  34. * @return void
  35. */
  36. public function setUp() {
  37. parent::setUp();
  38. $this->View = new View();
  39. $session = new Session();
  40. $this->View->request = new Request(['session' => $session]);
  41. $this->Session = new SessionHelper($this->View);
  42. $session->write(array(
  43. 'test' => 'info',
  44. 'Flash' => array(
  45. 'flash' => array(
  46. 'type' => 'info',
  47. 'params' => array(),
  48. 'message' => 'This is a calling'
  49. ),
  50. 'notification' => array(
  51. 'type' => 'info',
  52. 'params' => array(
  53. 'title' => 'Notice!',
  54. 'name' => 'Alert!',
  55. 'element' => 'session_helper'
  56. ),
  57. 'message' => 'This is a test of the emergency broadcasting system',
  58. ),
  59. 'classy' => array(
  60. 'type' => 'success',
  61. 'params' => array('class' => 'positive'),
  62. 'message' => 'Recorded'
  63. ),
  64. 'incomplete' => [
  65. 'message' => 'A thing happened',
  66. ]
  67. ),
  68. 'Deeply' => array('nested' => array('key' => 'value')),
  69. ));
  70. }
  71. /**
  72. * tearDown method
  73. *
  74. * @return void
  75. */
  76. public function tearDown() {
  77. $_SESSION = array();
  78. unset($this->View, $this->Session);
  79. Plugin::unload();
  80. parent::tearDown();
  81. }
  82. /**
  83. * testRead method
  84. *
  85. * @return void
  86. */
  87. public function testRead() {
  88. $result = $this->Session->read('Deeply.nested.key');
  89. $this->assertEquals('value', $result);
  90. $result = $this->Session->read('test');
  91. $this->assertEquals('info', $result);
  92. }
  93. /**
  94. * testCheck method
  95. *
  96. * @return void
  97. */
  98. public function testCheck() {
  99. $this->assertTrue($this->Session->check('test'));
  100. $this->assertTrue($this->Session->check('Flash.flash'));
  101. $this->assertFalse($this->Session->check('Does.not.exist'));
  102. $this->assertFalse($this->Session->check('Nope'));
  103. }
  104. /**
  105. * testFlash method
  106. *
  107. * @return void
  108. */
  109. public function testFlash() {
  110. $result = $this->Session->flash();
  111. $expected = '<div id="flash-message" class="message-info">This is a calling</div>';
  112. $this->assertEquals($expected, $result);
  113. $this->assertFalse($this->Session->check('Message.flash'));
  114. $expected = '<div id="classy-message" class="message-success">Recorded</div>';
  115. $result = $this->Session->flash('classy');
  116. $this->assertEquals($expected, $result);
  117. $result = $this->Session->flash('notification');
  118. $result = str_replace("\r\n", "\n", $result);
  119. $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>";
  120. $this->assertEquals($expected, $result);
  121. $this->assertFalse($this->Session->check('Message.notification'));
  122. }
  123. /**
  124. * Test rendering a flash message for incomplete data.
  125. *
  126. * @return void
  127. */
  128. public function testFlashIncomplete() {
  129. $result = $this->Session->flash('incomplete');
  130. $expected = '<div id="incomplete-message" class="message-info">A thing happened</div>';
  131. $this->assertEquals($expected, $result);
  132. }
  133. /**
  134. * test flash() with the attributes.
  135. *
  136. * @return void
  137. */
  138. public function testFlashAttributes() {
  139. $result = $this->Session->flash('flash', array('class' => 'crazy'));
  140. $expected = '<div id="flash-message" class="message-crazy">This is a calling</div>';
  141. $this->assertEquals($expected, $result);
  142. $this->assertFalse($this->Session->check('Message.flash'));
  143. }
  144. /**
  145. * test setting the element from the attrs.
  146. *
  147. * @return void
  148. */
  149. public function testFlashElementInAttrs() {
  150. $result = $this->Session->flash('flash', array(
  151. 'element' => 'session_helper',
  152. 'params' => array('title' => 'Notice!', 'name' => 'Alert!')
  153. ));
  154. $expected = "<div id=\"notificationLayout\">\n\t<h1>Alert!</h1>\n\t<h3>Notice!</h3>\n\t<p>This is a calling</p>\n</div>";
  155. $this->assertTextEquals($expected, $result);
  156. }
  157. /**
  158. * test using elements in plugins.
  159. *
  160. * @return void
  161. */
  162. public function testFlashWithPluginElement() {
  163. Plugin::load('TestPlugin');
  164. $result = $this->Session->flash('flash', array('element' => 'TestPlugin.plugin_element'));
  165. $expected = 'this is the plugin element using params[plugin]';
  166. $this->assertEquals($expected, $result);
  167. }
  168. }