FlashComponentTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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\Controller\Component;
  16. use Cake\Controller\ComponentRegistry;
  17. use Cake\Controller\Component\FlashComponent;
  18. use Cake\Controller\Controller;
  19. use Cake\Http\ServerRequest;
  20. use Cake\Http\Session;
  21. use Cake\TestSuite\TestCase;
  22. use Exception;
  23. /**
  24. * FlashComponentTest class
  25. */
  26. class FlashComponentTest extends TestCase
  27. {
  28. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. static::setAppNamespace();
  37. $this->Controller = new Controller(new ServerRequest(['session' => new Session()]));
  38. $this->ComponentRegistry = new ComponentRegistry($this->Controller);
  39. $this->Flash = new FlashComponent($this->ComponentRegistry);
  40. $this->Session = new Session();
  41. }
  42. /**
  43. * tearDown method
  44. *
  45. * @return void
  46. */
  47. public function tearDown()
  48. {
  49. parent::tearDown();
  50. $this->Session->destroy();
  51. }
  52. /**
  53. * testSet method
  54. *
  55. * @return void
  56. * @covers \Cake\Controller\Component\FlashComponent::set
  57. */
  58. public function testSet()
  59. {
  60. $this->assertNull($this->Session->read('Flash.flash'));
  61. $this->Flash->set('This is a test message');
  62. $expected = [
  63. [
  64. 'message' => 'This is a test message',
  65. 'key' => 'flash',
  66. 'element' => 'Flash/default',
  67. 'params' => []
  68. ]
  69. ];
  70. $result = $this->Session->read('Flash.flash');
  71. $this->assertEquals($expected, $result);
  72. $this->Flash->set('This is a test message', ['element' => 'test', 'params' => ['foo' => 'bar']]);
  73. $expected[] = [
  74. 'message' => 'This is a test message',
  75. 'key' => 'flash',
  76. 'element' => 'Flash/test',
  77. 'params' => ['foo' => 'bar']
  78. ];
  79. $result = $this->Session->read('Flash.flash');
  80. $this->assertEquals($expected, $result);
  81. $this->Flash->set('This is a test message', ['element' => 'MyPlugin.alert']);
  82. $expected[] = [
  83. 'message' => 'This is a test message',
  84. 'key' => 'flash',
  85. 'element' => 'MyPlugin.Flash/alert',
  86. 'params' => []
  87. ];
  88. $result = $this->Session->read('Flash.flash');
  89. $this->assertEquals($expected, $result);
  90. $this->Flash->set('This is a test message', ['key' => 'foobar']);
  91. $expected = [
  92. [
  93. 'message' => 'This is a test message',
  94. 'key' => 'foobar',
  95. 'element' => 'Flash/default',
  96. 'params' => []
  97. ]
  98. ];
  99. $result = $this->Session->read('Flash.foobar');
  100. $this->assertEquals($expected, $result);
  101. }
  102. public function testDuplicateIgnored()
  103. {
  104. $this->assertNull($this->Session->read('Flash.flash'));
  105. $this->Flash->setConfig('duplicate', false);
  106. $this->Flash->set('This test message should appear once only');
  107. $this->Flash->set('This test message should appear once only');
  108. $result = $this->Session->read('Flash.flash');
  109. $this->assertCount(1, $result);
  110. }
  111. /**
  112. * @return void
  113. */
  114. public function testSetEscape()
  115. {
  116. $this->assertNull($this->Session->read('Flash.flash'));
  117. $this->Flash->set('This is a <b>test</b> message', ['escape' => false, 'params' => ['foo' => 'bar']]);
  118. $expected = [
  119. [
  120. 'message' => 'This is a <b>test</b> message',
  121. 'key' => 'flash',
  122. 'element' => 'Flash/default',
  123. 'params' => ['foo' => 'bar', 'escape' => false]
  124. ]
  125. ];
  126. $result = $this->Session->read('Flash.flash');
  127. $this->assertEquals($expected, $result);
  128. $this->Flash->set('This is a test message', ['key' => 'escaped', 'escape' => false, 'params' => ['foo' => 'bar', 'escape' => true]]);
  129. $expected = [
  130. [
  131. 'message' => 'This is a test message',
  132. 'key' => 'escaped',
  133. 'element' => 'Flash/default',
  134. 'params' => ['foo' => 'bar', 'escape' => true]
  135. ]
  136. ];
  137. $result = $this->Session->read('Flash.escaped');
  138. $this->assertEquals($expected, $result);
  139. }
  140. /**
  141. * test setting messages with using the clear option
  142. *
  143. * @return void
  144. * @covers \Cake\Controller\Component\FlashComponent::set
  145. */
  146. public function testSetWithClear()
  147. {
  148. $this->assertNull($this->Session->read('Flash.flash'));
  149. $this->Flash->set('This is a test message');
  150. $expected = [
  151. [
  152. 'message' => 'This is a test message',
  153. 'key' => 'flash',
  154. 'element' => 'Flash/default',
  155. 'params' => []
  156. ]
  157. ];
  158. $result = $this->Session->read('Flash.flash');
  159. $this->assertEquals($expected, $result);
  160. $this->Flash->set('This is another test message', ['clear' => true]);
  161. $expected = [
  162. [
  163. 'message' => 'This is another test message',
  164. 'key' => 'flash',
  165. 'element' => 'Flash/default',
  166. 'params' => []
  167. ]
  168. ];
  169. $result = $this->Session->read('Flash.flash');
  170. $this->assertEquals($expected, $result);
  171. }
  172. /**
  173. * testSetWithException method
  174. *
  175. * @return void
  176. * @covers \Cake\Controller\Component\FlashComponent::set
  177. */
  178. public function testSetWithException()
  179. {
  180. $this->assertNull($this->Session->read('Flash.flash'));
  181. $this->Flash->set(new Exception('This is a test message', 404));
  182. $expected = [
  183. [
  184. 'message' => 'This is a test message',
  185. 'key' => 'flash',
  186. 'element' => 'Flash/default',
  187. 'params' => ['code' => 404]
  188. ]
  189. ];
  190. $result = $this->Session->read('Flash.flash');
  191. $this->assertEquals($expected, $result);
  192. }
  193. /**
  194. * testSetWithComponentConfiguration method
  195. *
  196. * @return void
  197. */
  198. public function testSetWithComponentConfiguration()
  199. {
  200. $this->assertNull($this->Session->read('Flash.flash'));
  201. $this->Controller->loadComponent('Flash', ['element' => 'test']);
  202. $this->Controller->Flash->set('This is a test message');
  203. $expected = [
  204. [
  205. 'message' => 'This is a test message',
  206. 'key' => 'flash',
  207. 'element' => 'Flash/test',
  208. 'params' => []
  209. ]
  210. ];
  211. $result = $this->Session->read('Flash.flash');
  212. $this->assertEquals($expected, $result);
  213. }
  214. /**
  215. * Test magic call method.
  216. *
  217. * @covers \Cake\Controller\Component\FlashComponent::__call
  218. * @return void
  219. */
  220. public function testCall()
  221. {
  222. $this->assertNull($this->Session->read('Flash.flash'));
  223. $this->Flash->success('It worked');
  224. $expected = [
  225. [
  226. 'message' => 'It worked',
  227. 'key' => 'flash',
  228. 'element' => 'Flash/success',
  229. 'params' => []
  230. ]
  231. ];
  232. $result = $this->Session->read('Flash.flash');
  233. $this->assertEquals($expected, $result);
  234. $this->Flash->error('It did not work', ['element' => 'error_thing']);
  235. $expected[] = [
  236. 'message' => 'It did not work',
  237. 'key' => 'flash',
  238. 'element' => 'Flash/error',
  239. 'params' => []
  240. ];
  241. $result = $this->Session->read('Flash.flash');
  242. $this->assertEquals($expected, $result, 'Element is ignored in magic call.');
  243. $this->Flash->success('It worked', ['plugin' => 'MyPlugin']);
  244. $expected[] = [
  245. 'message' => 'It worked',
  246. 'key' => 'flash',
  247. 'element' => 'MyPlugin.Flash/success',
  248. 'params' => []
  249. ];
  250. $result = $this->Session->read('Flash.flash');
  251. $this->assertEquals($expected, $result);
  252. }
  253. /**
  254. * Test a magic call with the "clear" flag to true
  255. *
  256. * @return void
  257. * @covers \Cake\Controller\Component\FlashComponent::set
  258. */
  259. public function testCallWithClear()
  260. {
  261. $this->assertNull($this->Session->read('Flash.flash'));
  262. $this->Flash->success('It worked');
  263. $expected = [
  264. [
  265. 'message' => 'It worked',
  266. 'key' => 'flash',
  267. 'element' => 'Flash/success',
  268. 'params' => []
  269. ]
  270. ];
  271. $result = $this->Session->read('Flash.flash');
  272. $this->assertEquals($expected, $result);
  273. $this->Flash->success('It worked too', ['clear' => true]);
  274. $expected = [
  275. [
  276. 'message' => 'It worked too',
  277. 'key' => 'flash',
  278. 'element' => 'Flash/success',
  279. 'params' => []
  280. ]
  281. ];
  282. $result = $this->Session->read('Flash.flash');
  283. $this->assertEquals($expected, $result);
  284. }
  285. }