FlashHelperTest.php 6.9 KB

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