FlashComponentTest.php 9.2 KB

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