FlashHelperTest.php 7.1 KB

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