SessionHelperTest.php 4.9 KB

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