SessionHelperTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * SessionHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Controller', 'Controller');
  20. App::uses('View', 'View');
  21. App::uses('SessionHelper', 'View/Helper');
  22. /**
  23. * SessionHelperTest class
  24. *
  25. * @package Cake.Test.Case.View.Helper
  26. */
  27. class SessionHelperTest extends CakeTestCase {
  28. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. parent::setUp();
  35. $controller = null;
  36. $this->View = new View($controller);
  37. $this->Session = new SessionHelper($this->View);
  38. CakeSession::start();
  39. if (!CakeSession::started()) {
  40. CakeSession::start();
  41. }
  42. $_SESSION = array(
  43. 'test' => 'info',
  44. 'Message' => array(
  45. 'flash' => array(
  46. 'element' => 'default',
  47. 'params' => array(),
  48. 'message' => 'This is a calling'
  49. ),
  50. 'notification' => array(
  51. 'element' => 'session_helper',
  52. 'params' => array('title' => 'Notice!', 'name' => 'Alert!'),
  53. 'message' => 'This is a test of the emergency broadcasting system',
  54. ),
  55. 'classy' => array(
  56. 'element' => 'default',
  57. 'params' => array('class' => 'positive'),
  58. 'message' => 'Recorded'
  59. ),
  60. 'bare' => array(
  61. 'element' => null,
  62. 'message' => 'Bare message',
  63. 'params' => array(),
  64. ),
  65. ),
  66. 'Deeply' => array('nested' => array('key' => 'value')),
  67. );
  68. }
  69. /**
  70. * tearDown method
  71. *
  72. * @return void
  73. */
  74. public function tearDown() {
  75. $_SESSION = array();
  76. unset($this->View, $this->Session);
  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->assertEqual($result, 'value');
  87. $result = $this->Session->read('test');
  88. $this->assertEqual($result, 'info');
  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.element'));
  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('flash');
  108. $expected = '<div id="flashMessage" class="message">This is a calling</div>';
  109. $this->assertEqual($expected, $result);
  110. $this->assertFalse($this->Session->check('Message.flash'));
  111. $expected = '<div id="classyMessage" class="positive">Recorded</div>';
  112. $result = $this->Session->flash('classy');
  113. $this->assertEqual($expected, $result);
  114. App::build(array(
  115. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  116. ));
  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->assertEqual($expected, $result);
  121. $this->assertFalse($this->Session->check('Message.notification'));
  122. $result = $this->Session->flash('bare');
  123. $expected = 'Bare message';
  124. $this->assertEqual($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="flashMessage" class="test-message">This is a calling</div>';
  135. $this->assertEqual($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. App::build(array(
  145. 'views' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  146. ));
  147. $result = $this->Session->flash('flash', array(
  148. 'element' => 'session_helper',
  149. 'params' => array('title' => 'Notice!', 'name' => 'Alert!')
  150. ));
  151. $expected = "<div id=\"notificationLayout\">\n\t<h1>Alert!</h1>\n\t<h3>Notice!</h3>\n\t<p>This is a calling</p>\n</div>";
  152. }
  153. }