SessionHelperTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 CakePHP(tm) v 1.2.0.4206
  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. $controller = null;
  38. $this->View = new View($controller);
  39. $this->Session = new SessionHelper($this->View);
  40. Session::start();
  41. if (!Session::started()) {
  42. Session::start();
  43. }
  44. $_SESSION = array(
  45. 'test' => 'info',
  46. 'Message' => array(
  47. 'flash' => array(
  48. 'element' => 'default',
  49. 'params' => array(),
  50. 'message' => 'This is a calling'
  51. ),
  52. 'notification' => array(
  53. 'element' => 'session_helper',
  54. 'params' => array('title' => 'Notice!', 'name' => 'Alert!'),
  55. 'message' => 'This is a test of the emergency broadcasting system',
  56. ),
  57. 'classy' => array(
  58. 'element' => 'default',
  59. 'params' => array('class' => 'positive'),
  60. 'message' => 'Recorded'
  61. ),
  62. 'bare' => array(
  63. 'element' => null,
  64. 'message' => 'Bare message',
  65. 'params' => array(),
  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('Message.flash.element'));
  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('flash');
  111. $expected = '<div id="flash-message" class="message">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="positive">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. $result = $this->Session->flash('bare');
  123. $expected = 'Bare message';
  124. $this->assertEquals($expected, $result);
  125. $this->assertFalse($this->Session->check('Message.bare'));
  126. }
  127. /**
  128. * test flash() with the attributes.
  129. *
  130. * @return void
  131. */
  132. public function testFlashAttributes() {
  133. $result = $this->Session->flash('flash', array('params' => array('class' => 'test-message')));
  134. $expected = '<div id="flash-message" class="test-message">This is a calling</div>';
  135. $this->assertEquals($expected, $result);
  136. $this->assertFalse($this->Session->check('Message.flash'));
  137. }
  138. /**
  139. * test setting the element from the attrs.
  140. *
  141. * @return void
  142. */
  143. public function testFlashElementInAttrs() {
  144. $result = $this->Session->flash('flash', array(
  145. 'element' => 'session_helper',
  146. 'params' => array('title' => 'Notice!', 'name' => 'Alert!')
  147. ));
  148. $expected = "<div id=\"notificationLayout\">\n\t<h1>Alert!</h1>\n\t<h3>Notice!</h3>\n\t<p>This is a calling</p>\n</div>";
  149. $this->assertTextEquals($expected, $result);
  150. }
  151. /**
  152. * test using elements in plugins.
  153. *
  154. * @return void
  155. */
  156. public function testFlashWithPluginElement() {
  157. Plugin::load('TestPlugin');
  158. $result = $this->Session->flash('flash', array('element' => 'TestPlugin.plugin_element'));
  159. $expected = 'this is the plugin element using params[plugin]';
  160. $this->assertEquals($expected, $result);
  161. }
  162. }