FlashHelperTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Controller\Controller;
  17. use Cake\Core\App;
  18. use Cake\Core\Plugin;
  19. use Cake\Network\Request;
  20. use Cake\Network\Session;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\View\Helper\FlashHelper;
  23. use Cake\View\View;
  24. /**
  25. * FlashHelperTest class
  26. *
  27. */
  28. class FlashHelperTest extends TestCase {
  29. /**
  30. * setUp method
  31. *
  32. * @return void
  33. */
  34. public function setUp() {
  35. parent::setUp();
  36. $this->View = new View();
  37. $session = new Session();
  38. $this->View->request = new Request(['session' => $session]);
  39. $this->Flash = new FlashHelper($this->View);
  40. $session->write(array(
  41. 'Message' => array(
  42. 'flash' => array(
  43. 'key' => 'flash',
  44. 'message' => 'This is a calling',
  45. 'element' => null,
  46. 'class' => 'info',
  47. 'params' => array()
  48. ),
  49. 'notification' => array(
  50. 'key' => 'notification',
  51. 'message' => 'This is a test of the emergency broadcasting system',
  52. 'element' => 'flash_helper',
  53. 'class' => 'info',
  54. 'params' => array(
  55. 'title' => 'Notice!',
  56. 'name' => 'Alert!'
  57. )
  58. ),
  59. 'classy' => array(
  60. 'key' => 'classy',
  61. 'message' => 'Recorded',
  62. 'element' => null,
  63. 'class' => 'positive',
  64. 'params' => array()
  65. )
  66. )
  67. ));
  68. }
  69. /**
  70. * tearDown method
  71. *
  72. * @return void
  73. */
  74. public function tearDown() {
  75. $_SESSION = [];
  76. unset($this->View, $this->Session);
  77. Plugin::unload();
  78. parent::tearDown();
  79. }
  80. /**
  81. * testFlash method
  82. *
  83. * @return void
  84. */
  85. public function testFlash() {
  86. $result = $this->Flash->render();
  87. $expected = '<div id="flash-message" class="message-info">This is a calling</div>';
  88. $this->assertEquals($expected, $result);
  89. $expected = '<div id="classy-message" class="message-positive">Recorded</div>';
  90. $result = $this->Flash->render('classy');
  91. $this->assertEquals($expected, $result);
  92. $result = $this->Flash->render('notification');
  93. $children = [
  94. ['tag' => 'h1', 'content' => 'Alert!'],
  95. ['tag' => 'h3', 'content' => 'Notice!'],
  96. ['tag' => 'p', 'content' => 'This is a test of the emergency broadcasting system']
  97. ];
  98. $expected = [
  99. 'tag' => 'div',
  100. 'id' => 'notificationLayout',
  101. 'child' => []
  102. ];
  103. $expected['child'] = ['tag' => 'h1', 'content' => 'Alert!'];
  104. $this->assertTag($expected, $result);
  105. $expected['child'] = ['tag' => 'h3', 'content' => 'Notice!'];
  106. $this->assertTag($expected, $result);
  107. $expected['child'] = ['tag' => 'p', 'content' => 'This is a test of the emergency broadcasting system'];
  108. $this->assertTag($expected, $result);
  109. }
  110. /**
  111. * test flash() with the attributes.
  112. *
  113. * @return void
  114. */
  115. public function testFlashAttributes() {
  116. $result = $this->Flash->render('flash', array('class' => 'crazy'));
  117. $expected = '<div id="flash-message" class="message-crazy">This is a calling</div>';
  118. $this->assertEquals($expected, $result);
  119. }
  120. /**
  121. * test setting the element from the attrs.
  122. *
  123. * @return void
  124. */
  125. public function testFlashElementInAttrs() {
  126. $result = $this->Flash->render('notification', array(
  127. 'element' => 'flash_helper',
  128. 'params' => array('title' => 'Notice!', 'name' => 'Alert!')
  129. ));
  130. $children = [
  131. ['tag' => 'h1', 'content' => 'Alert!'],
  132. ['tag' => 'h3', 'content' => 'Notice!'],
  133. ['tag' => 'p', 'content' => 'This is a test of the emergency broadcasting system']
  134. ];
  135. $expected = [
  136. 'tag' => 'div',
  137. 'id' => 'notificationLayout',
  138. 'child' => []
  139. ];
  140. $expected['child'] = ['tag' => 'h1', 'content' => 'Alert!'];
  141. $this->assertTag($expected, $result);
  142. $expected['child'] = ['tag' => 'h3', 'content' => 'Notice!'];
  143. $this->assertTag($expected, $result);
  144. $expected['child'] = ['tag' => 'p', 'content' => 'This is a test of the emergency broadcasting system'];
  145. $this->assertTag($expected, $result);
  146. }
  147. /**
  148. * test using elements in plugins.
  149. *
  150. * @return void
  151. */
  152. public function testFlashWithPluginElement() {
  153. Plugin::load('TestPlugin');
  154. $result = $this->Flash->render('flash', array('element' => 'TestPlugin.plugin_element'));
  155. $expected = 'this is the plugin element using params[plugin]';
  156. $this->assertEquals($expected, $result);
  157. }
  158. }