FlashComponentTest.php 9.8 KB

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