FlashHelperTest.php 7.2 KB

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