FlashHelperTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 3.0.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. 'Flash' => array(
  42. 'flash' => array(
  43. 'key' => 'flash',
  44. 'message' => 'This is a calling',
  45. 'element' => null,
  46. 'params' => array()
  47. ),
  48. 'notification' => array(
  49. 'key' => 'notification',
  50. 'message' => 'This is a test of the emergency broadcasting system',
  51. 'element' => 'flash_helper',
  52. 'params' => array(
  53. 'title' => 'Notice!',
  54. 'name' => 'Alert!'
  55. )
  56. ),
  57. 'classy' => array(
  58. 'key' => 'classy',
  59. 'message' => 'Recorded',
  60. 'element' => 'flash_classy',
  61. 'params' => array()
  62. )
  63. )
  64. ));
  65. }
  66. /**
  67. * tearDown method
  68. *
  69. * @return void
  70. */
  71. public function tearDown() {
  72. $_SESSION = [];
  73. unset($this->View, $this->Session);
  74. Plugin::unload();
  75. parent::tearDown();
  76. }
  77. /**
  78. * testFlash method
  79. *
  80. * @return void
  81. */
  82. public function testFlash() {
  83. $result = $this->Flash->render();
  84. $expected = 'This is a calling';
  85. $this->assertEquals($expected, $result);
  86. $expected = '<div id="classy-message">Recorded</div>';
  87. $result = $this->Flash->render('classy');
  88. $this->assertEquals($expected, $result);
  89. $result = $this->Flash->render('notification');
  90. $expected = [
  91. 'tag' => 'div',
  92. 'id' => 'notificationLayout',
  93. 'child' => []
  94. ];
  95. $expected['child'] = ['tag' => 'h1', 'content' => 'Alert!'];
  96. $this->assertTag($expected, $result);
  97. $expected['child'] = ['tag' => 'h3', 'content' => 'Notice!'];
  98. $this->assertTag($expected, $result);
  99. $expected['child'] = ['tag' => 'p', 'content' => 'This is a test of the emergency broadcasting system'];
  100. $this->assertTag($expected, $result);
  101. }
  102. /**
  103. * test setting the element from the attrs.
  104. *
  105. * @return void
  106. */
  107. public function testFlashElementInAttrs() {
  108. $result = $this->Flash->render('notification', array(
  109. 'element' => 'flash_helper',
  110. 'params' => array('title' => 'Notice!', 'name' => 'Alert!')
  111. ));
  112. $expected = [
  113. 'tag' => 'div',
  114. 'id' => 'notificationLayout',
  115. 'child' => []
  116. ];
  117. $expected['child'] = ['tag' => 'h1', 'content' => 'Alert!'];
  118. $this->assertTag($expected, $result);
  119. $expected['child'] = ['tag' => 'h3', 'content' => 'Notice!'];
  120. $this->assertTag($expected, $result);
  121. $expected['child'] = ['tag' => 'p', 'content' => 'This is a test of the emergency broadcasting system'];
  122. $this->assertTag($expected, $result);
  123. }
  124. /**
  125. * test using elements in plugins.
  126. *
  127. * @return void
  128. */
  129. public function testFlashWithPluginElement() {
  130. Plugin::load('TestPlugin');
  131. $result = $this->Flash->render('flash', array('element' => 'TestPlugin.plugin_element'));
  132. $expected = 'this is the plugin element using params[plugin]';
  133. $this->assertEquals($expected, $result);
  134. }
  135. }