FlashComponentTest.php 9.6 KB

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