FlashHelperTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Core\Plugin;
  17. use Cake\Network\Request;
  18. use Cake\Network\Session;
  19. use Cake\TestSuite\TestCase;
  20. use Cake\View\Helper\FlashHelper;
  21. use Cake\View\View;
  22. /**
  23. * FlashHelperTest class
  24. */
  25. class FlashHelperTest extends TestCase
  26. {
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. parent::setUp();
  35. $this->View = new View();
  36. $session = new Session();
  37. $this->View->request = new Request(['session' => $session]);
  38. $this->Flash = new FlashHelper($this->View);
  39. $session->write([
  40. 'Flash' => [
  41. 'flash' => [
  42. [
  43. 'key' => 'flash',
  44. 'message' => 'This is a calling',
  45. 'element' => 'Flash/default',
  46. 'params' => []
  47. ]
  48. ],
  49. 'notification' => [
  50. [
  51. 'key' => 'notification',
  52. 'message' => 'This is a test of the emergency broadcasting system',
  53. 'element' => 'flash_helper',
  54. 'params' => [
  55. 'title' => 'Notice!',
  56. 'name' => 'Alert!'
  57. ]
  58. ]
  59. ],
  60. 'classy' => [
  61. [
  62. 'key' => 'classy',
  63. 'message' => 'Recorded',
  64. 'element' => 'flash_classy',
  65. 'params' => []
  66. ]
  67. ],
  68. 'stack' => [
  69. [
  70. 'key' => 'flash',
  71. 'message' => 'This is a calling',
  72. 'element' => 'Flash/default',
  73. 'params' => []
  74. ],
  75. [
  76. 'key' => 'notification',
  77. 'message' => 'This is a test of the emergency broadcasting system',
  78. 'element' => 'flash_helper',
  79. 'params' => [
  80. 'title' => 'Notice!',
  81. 'name' => 'Alert!'
  82. ]
  83. ],
  84. [
  85. 'key' => 'classy',
  86. 'message' => 'Recorded',
  87. 'element' => 'flash_classy',
  88. 'params' => []
  89. ]
  90. ]
  91. ]
  92. ]);
  93. }
  94. /**
  95. * tearDown method
  96. *
  97. * @return void
  98. */
  99. public function tearDown()
  100. {
  101. parent::tearDown();
  102. unset($this->View, $this->Flash);
  103. }
  104. /**
  105. * testFlash method
  106. *
  107. * @return void
  108. */
  109. public function testFlash()
  110. {
  111. $result = $this->Flash->render();
  112. $expected = '<div class="message">This is a calling</div>';
  113. $this->assertContains($expected, $result);
  114. $expected = '<div id="classy-message">Recorded</div>';
  115. $result = $this->Flash->render('classy');
  116. $this->assertEquals($expected, $result);
  117. $result = $this->Flash->render('notification');
  118. $expected = [
  119. 'div' => ['id' => 'notificationLayout'],
  120. '<h1', 'Alert!', '/h1',
  121. '<h3', 'Notice!', '/h3',
  122. '<p', 'This is a test of the emergency broadcasting system', '/p',
  123. '/div'
  124. ];
  125. $this->assertHtml($expected, $result);
  126. $this->assertNull($this->Flash->render('non-existent'));
  127. }
  128. /**
  129. * testFlashThrowsException
  130. *
  131. * @expectedException \UnexpectedValueException
  132. */
  133. public function testFlashThrowsException()
  134. {
  135. $this->View->request->session()->write('Flash.foo', 'bar');
  136. $this->Flash->render('foo');
  137. }
  138. /**
  139. * test setting the element from the attrs.
  140. *
  141. * @return void
  142. */
  143. public function testFlashElementInAttrs()
  144. {
  145. $result = $this->Flash->render('notification', [
  146. 'element' => 'flash_helper',
  147. 'params' => ['title' => 'Notice!', 'name' => 'Alert!']
  148. ]);
  149. $expected = [
  150. 'div' => ['id' => 'notificationLayout'],
  151. '<h1', 'Alert!', '/h1',
  152. '<h3', 'Notice!', '/h3',
  153. '<p', 'This is a test of the emergency broadcasting system', '/p',
  154. '/div'
  155. ];
  156. $this->assertHtml($expected, $result);
  157. }
  158. /**
  159. * test using elements in plugins.
  160. *
  161. * @return void
  162. */
  163. public function testFlashWithPluginElement()
  164. {
  165. Plugin::load('TestPlugin');
  166. $result = $this->Flash->render('flash', ['element' => 'TestPlugin.Flash/plugin_element']);
  167. $expected = 'this is the plugin element';
  168. $this->assertEquals($expected, $result);
  169. }
  170. /**
  171. * test that when View theme is set, flash element from that theme (plugin) is used.
  172. *
  173. * @return void
  174. */
  175. public function testFlashWithTheme()
  176. {
  177. Plugin::load('TestTheme');
  178. $this->View->theme = 'TestTheme';
  179. $result = $this->Flash->render('flash');
  180. $expected = 'flash element from TestTheme';
  181. $this->assertContains($expected, $result);
  182. }
  183. /**
  184. * Test that when rendering a stack, messages are displayed in their
  185. * respective element, in the order they were added in the stack
  186. *
  187. * @return void
  188. */
  189. public function testFlashWithStack()
  190. {
  191. $result = $this->Flash->render('stack');
  192. $expected = [
  193. ['div' => ['class' => 'message']], 'This is a calling', '/div',
  194. ['div' => ['id' => 'notificationLayout']],
  195. '<h1', 'Alert!', '/h1',
  196. '<h3', 'Notice!', '/h3',
  197. '<p', 'This is a test of the emergency broadcasting system', '/p',
  198. '/div',
  199. ['div' => ['id' => 'classy-message']], 'Recorded', '/div'
  200. ];
  201. $this->assertHtml($expected, $result);
  202. $this->assertNull($this->View->request->session()->read('Flash.stack'));
  203. }
  204. /**
  205. * test that when View prefix is set, flash element from that prefix
  206. * is used if available.
  207. *
  208. * @return void
  209. */
  210. public function testFlashWithPrefix()
  211. {
  212. $this->View->request->params['prefix'] = 'Admin';
  213. $result = $this->Flash->render('flash');
  214. $expected = 'flash element from Admin prefix folder';
  215. $this->assertContains($expected, $result);
  216. }
  217. }