SessionHelperTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. 'Message' => 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. ),
  65. 'Deeply' => array('nested' => array('key' => 'value')),
  66. ));
  67. }
  68. /**
  69. * tearDown method
  70. *
  71. * @return void
  72. */
  73. public function tearDown() {
  74. $_SESSION = array();
  75. unset($this->View, $this->Session);
  76. Plugin::unload();
  77. parent::tearDown();
  78. }
  79. /**
  80. * testRead method
  81. *
  82. * @return void
  83. */
  84. public function testRead() {
  85. $result = $this->Session->read('Deeply.nested.key');
  86. $this->assertEquals('value', $result);
  87. $result = $this->Session->read('test');
  88. $this->assertEquals('info', $result);
  89. }
  90. /**
  91. * testCheck method
  92. *
  93. * @return void
  94. */
  95. public function testCheck() {
  96. $this->assertTrue($this->Session->check('test'));
  97. $this->assertTrue($this->Session->check('Message.flash'));
  98. $this->assertFalse($this->Session->check('Does.not.exist'));
  99. $this->assertFalse($this->Session->check('Nope'));
  100. }
  101. /**
  102. * testFlash method
  103. *
  104. * @return void
  105. */
  106. public function testFlash() {
  107. $result = $this->Session->flash();
  108. $expected = '<div id="flash-message" class="message-info">This is a calling</div>';
  109. $this->assertEquals($expected, $result);
  110. $this->assertFalse($this->Session->check('Message.flash'));
  111. $expected = '<div id="classy-message" class="message-success">Recorded</div>';
  112. $result = $this->Session->flash('classy');
  113. $this->assertEquals($expected, $result);
  114. $result = $this->Session->flash('notification');
  115. $result = str_replace("\r\n", "\n", $result);
  116. $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>";
  117. $this->assertEquals($expected, $result);
  118. $this->assertFalse($this->Session->check('Message.notification'));
  119. }
  120. /**
  121. * test flash() with the attributes.
  122. *
  123. * @return void
  124. */
  125. public function testFlashAttributes() {
  126. $result = $this->Session->flash('flash', array('class' => 'crazy'));
  127. $expected = '<div id="flash-message" class="message-crazy">This is a calling</div>';
  128. $this->assertEquals($expected, $result);
  129. $this->assertFalse($this->Session->check('Message.flash'));
  130. }
  131. /**
  132. * test setting the element from the attrs.
  133. *
  134. * @return void
  135. */
  136. public function testFlashElementInAttrs() {
  137. $result = $this->Session->flash('flash', array(
  138. 'element' => 'session_helper',
  139. 'params' => array('title' => 'Notice!', 'name' => 'Alert!')
  140. ));
  141. $expected = "<div id=\"notificationLayout\">\n\t<h1>Alert!</h1>\n\t<h3>Notice!</h3>\n\t<p>This is a calling</p>\n</div>";
  142. $this->assertTextEquals($expected, $result);
  143. }
  144. /**
  145. * test using elements in plugins.
  146. *
  147. * @return void
  148. */
  149. public function testFlashWithPluginElement() {
  150. Plugin::load('TestPlugin');
  151. $result = $this->Session->flash('flash', array('element' => 'TestPlugin.plugin_element'));
  152. $expected = 'this is the plugin element using params[plugin]';
  153. $this->assertEquals($expected, $result);
  154. }
  155. }