FlashComponentTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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\Controller\Component;
  16. use Cake\Controller\ComponentRegistry;
  17. use Cake\Controller\Component\FlashComponent;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\Configure;
  20. use Cake\Network\Request;
  21. use Cake\Network\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()
  35. {
  36. parent::setUp();
  37. Configure::write('App.namespace', 'TestApp');
  38. $this->Controller = new Controller(new Request(['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()
  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()
  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()
  104. {
  105. $this->assertNull($this->Session->read('Flash.flash'));
  106. $this->Flash->config('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. /**
  113. * @return void
  114. */
  115. public function testSetEscape()
  116. {
  117. $this->assertNull($this->Session->read('Flash.flash'));
  118. $this->Flash->set('This is a <b>test</b> message', ['escape' => false, 'params' => ['foo' => 'bar']]);
  119. $expected = [
  120. [
  121. 'message' => 'This is a <b>test</b> message',
  122. 'key' => 'flash',
  123. 'element' => 'Flash/default',
  124. 'params' => ['foo' => 'bar', 'escape' => false]
  125. ]
  126. ];
  127. $result = $this->Session->read('Flash.flash');
  128. $this->assertEquals($expected, $result);
  129. $this->Flash->set('This is a test message', ['key' => 'escaped', 'escape' => false, 'params' => ['foo' => 'bar', 'escape' => true]]);
  130. $expected = [
  131. [
  132. 'message' => 'This is a test message',
  133. 'key' => 'escaped',
  134. 'element' => 'Flash/default',
  135. 'params' => ['foo' => 'bar', 'escape' => true]
  136. ]
  137. ];
  138. $result = $this->Session->read('Flash.escaped');
  139. $this->assertEquals($expected, $result);
  140. }
  141. /**
  142. * test setting messages with using the clear option
  143. *
  144. * @return void
  145. * @covers \Cake\Controller\Component\FlashComponent::set
  146. */
  147. public function testSetWithClear()
  148. {
  149. $this->assertNull($this->Session->read('Flash.flash'));
  150. $this->Flash->set('This is a test message');
  151. $expected = [
  152. [
  153. 'message' => 'This is a test message',
  154. 'key' => 'flash',
  155. 'element' => 'Flash/default',
  156. 'params' => []
  157. ]
  158. ];
  159. $result = $this->Session->read('Flash.flash');
  160. $this->assertEquals($expected, $result);
  161. $this->Flash->set('This is another test message', ['clear' => true]);
  162. $expected = [
  163. [
  164. 'message' => 'This is another test message',
  165. 'key' => 'flash',
  166. 'element' => 'Flash/default',
  167. 'params' => []
  168. ]
  169. ];
  170. $result = $this->Session->read('Flash.flash');
  171. $this->assertEquals($expected, $result);
  172. }
  173. /**
  174. * testSetWithException method
  175. *
  176. * @return void
  177. * @covers \Cake\Controller\Component\FlashComponent::set
  178. */
  179. public function testSetWithException()
  180. {
  181. $this->assertNull($this->Session->read('Flash.flash'));
  182. $this->Flash->set(new Exception('This is a test message', 404));
  183. $expected = [
  184. [
  185. 'message' => 'This is a test message',
  186. 'key' => 'flash',
  187. 'element' => 'Flash/default',
  188. 'params' => ['code' => 404]
  189. ]
  190. ];
  191. $result = $this->Session->read('Flash.flash');
  192. $this->assertEquals($expected, $result);
  193. }
  194. /**
  195. * testSetWithComponentConfiguration method
  196. *
  197. * @return void
  198. */
  199. public function testSetWithComponentConfiguration()
  200. {
  201. $this->assertNull($this->Session->read('Flash.flash'));
  202. $this->Controller->loadComponent('Flash', ['element' => 'test']);
  203. $this->Controller->Flash->set('This is a test message');
  204. $expected = [
  205. [
  206. 'message' => 'This is a test message',
  207. 'key' => 'flash',
  208. 'element' => 'Flash/test',
  209. 'params' => []
  210. ]
  211. ];
  212. $result = $this->Session->read('Flash.flash');
  213. $this->assertEquals($expected, $result);
  214. }
  215. /**
  216. * Test magic call method.
  217. *
  218. * @covers \Cake\Controller\Component\FlashComponent::__call
  219. * @return void
  220. */
  221. public function testCall()
  222. {
  223. $this->assertNull($this->Session->read('Flash.flash'));
  224. $this->Flash->success('It worked');
  225. $expected = [
  226. [
  227. 'message' => 'It worked',
  228. 'key' => 'flash',
  229. 'element' => 'Flash/success',
  230. 'params' => []
  231. ]
  232. ];
  233. $result = $this->Session->read('Flash.flash');
  234. $this->assertEquals($expected, $result);
  235. $this->Flash->error('It did not work', ['element' => 'error_thing']);
  236. $expected[] = [
  237. 'message' => 'It did not work',
  238. 'key' => 'flash',
  239. 'element' => 'Flash/error',
  240. 'params' => []
  241. ];
  242. $result = $this->Session->read('Flash.flash');
  243. $this->assertEquals($expected, $result, 'Element is ignored in magic call.');
  244. $this->Flash->success('It worked', ['plugin' => 'MyPlugin']);
  245. $expected[] = [
  246. 'message' => 'It worked',
  247. 'key' => 'flash',
  248. 'element' => 'MyPlugin.Flash/success',
  249. 'params' => []
  250. ];
  251. $result = $this->Session->read('Flash.flash');
  252. $this->assertEquals($expected, $result);
  253. }
  254. /**
  255. * Test a magic call with the "clear" flag to true
  256. *
  257. * @return void
  258. * @covers \Cake\Controller\Component\FlashComponent::set
  259. */
  260. public function testCallWithClear()
  261. {
  262. $this->assertNull($this->Session->read('Flash.flash'));
  263. $this->Flash->success('It worked');
  264. $expected = [
  265. [
  266. 'message' => 'It worked',
  267. 'key' => 'flash',
  268. 'element' => 'Flash/success',
  269. 'params' => []
  270. ]
  271. ];
  272. $result = $this->Session->read('Flash.flash');
  273. $this->assertEquals($expected, $result);
  274. $this->Flash->success('It worked too', ['clear' => true]);
  275. $expected = [
  276. [
  277. 'message' => 'It worked too',
  278. 'key' => 'flash',
  279. 'element' => 'Flash/success',
  280. 'params' => []
  281. ]
  282. ];
  283. $result = $this->Session->read('Flash.flash');
  284. $this->assertEquals($expected, $result);
  285. }
  286. }