FlashHelperTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. 'key' => 'flash',
  46. 'message' => 'This is a calling',
  47. 'element' => 'Flash/default',
  48. 'params' => []
  49. ],
  50. 'notification' => [
  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. 'classy' => [
  60. 'key' => 'classy',
  61. 'message' => 'Recorded',
  62. 'element' => 'flash_classy',
  63. 'params' => []
  64. ]
  65. ]
  66. ]);
  67. }
  68. /**
  69. * tearDown method
  70. *
  71. * @return void
  72. */
  73. public function tearDown()
  74. {
  75. parent::tearDown();
  76. unset($this->View, $this->Flash);
  77. }
  78. /**
  79. * testFlash method
  80. *
  81. * @return void
  82. */
  83. public function testFlash()
  84. {
  85. $result = $this->Flash->render();
  86. $expected = '<div class="message">This is a calling</div>';
  87. $this->assertContains($expected, $result);
  88. $expected = '<div id="classy-message">Recorded</div>';
  89. $result = $this->Flash->render('classy');
  90. $this->assertEquals($expected, $result);
  91. $result = $this->Flash->render('notification');
  92. $expected = [
  93. 'div' => ['id' => 'notificationLayout'],
  94. '<h1', 'Alert!', '/h1',
  95. '<h3', 'Notice!', '/h3',
  96. '<p', 'This is a test of the emergency broadcasting system', '/p',
  97. '/div'
  98. ];
  99. $this->assertHtml($expected, $result);
  100. $this->assertNull($this->Flash->render('non-existent'));
  101. }
  102. /**
  103. * testFlashThrowsException
  104. *
  105. * @expectedException \UnexpectedValueException
  106. */
  107. public function testFlashThrowsException()
  108. {
  109. $this->View->request->session()->write('Flash.foo', 'bar');
  110. $this->Flash->render('foo');
  111. }
  112. /**
  113. * test setting the element from the attrs.
  114. *
  115. * @return void
  116. */
  117. public function testFlashElementInAttrs()
  118. {
  119. $result = $this->Flash->render('notification', [
  120. 'element' => 'flash_helper',
  121. 'params' => ['title' => 'Notice!', 'name' => 'Alert!']
  122. ]);
  123. $expected = [
  124. 'div' => ['id' => 'notificationLayout'],
  125. '<h1', 'Alert!', '/h1',
  126. '<h3', 'Notice!', '/h3',
  127. '<p', 'This is a test of the emergency broadcasting system', '/p',
  128. '/div'
  129. ];
  130. $this->assertHtml($expected, $result);
  131. }
  132. /**
  133. * test using elements in plugins.
  134. *
  135. * @return void
  136. */
  137. public function testFlashWithPluginElement()
  138. {
  139. Plugin::load('TestPlugin');
  140. $result = $this->Flash->render('flash', ['element' => 'TestPlugin.Flash/plugin_element']);
  141. $expected = 'this is the plugin element';
  142. $this->assertEquals($expected, $result);
  143. }
  144. /**
  145. * test that when View theme is set, flash element from that theme (plugin) is used.
  146. *
  147. * @return void
  148. */
  149. public function testFlashWithTheme()
  150. {
  151. Plugin::load('TestTheme');
  152. $this->View->theme = 'TestTheme';
  153. $result = $this->Flash->render('flash');
  154. $expected = 'flash element from TestTheme';
  155. $this->assertContains($expected, $result);
  156. }
  157. /**
  158. * test that when View prefix is set, flash element from that prefix
  159. * is used if available.
  160. *
  161. * @return void
  162. */
  163. public function testFlashWithPrefix()
  164. {
  165. $this->View->request->params['prefix'] = 'Admin';
  166. $result = $this->Flash->render('flash');
  167. $expected = 'flash element from Admin prefix folder';
  168. $this->assertContains($expected, $result);
  169. }
  170. }