FlashHelperTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. $result = str_replace("\r\n", "\n", $result);
  94. $expected = "<div id=\"notificationLayout\">\n\t<h1>Alert!</h1>\n\t<h3>Notice!</h3>\n\t<p>This is a test of the emergency broadcasting system</p>\n</div>";
  95. $this->assertEquals($expected, $result);
  96. }
  97. /**
  98. * test flash() with the attributes.
  99. *
  100. * @return void
  101. */
  102. public function testFlashAttributes() {
  103. $result = $this->Flash->render('flash', array('class' => 'crazy'));
  104. $expected = '<div id="flash-message" class="message-crazy">This is a calling</div>';
  105. $this->assertEquals($expected, $result);
  106. }
  107. /**
  108. * test setting the element from the attrs.
  109. *
  110. * @return void
  111. */
  112. public function testFlashElementInAttrs() {
  113. $result = $this->Flash->render('notification', array(
  114. 'element' => 'flash_helper',
  115. 'params' => array('title' => 'Notice!', 'name' => 'Alert!')
  116. ));
  117. $expected = "<div id=\"notificationLayout\">\n\t<h1>Alert!</h1>\n\t<h3>Notice!</h3>\n\t<p>This is a test of the emergency broadcasting system</p>\n</div>";
  118. $this->assertTextEquals($expected, $result);
  119. }
  120. /**
  121. * test using elements in plugins.
  122. *
  123. * @return void
  124. */
  125. public function testFlashWithPluginElement() {
  126. Plugin::load('TestPlugin');
  127. $result = $this->Flash->render('flash', array('element' => 'TestPlugin.plugin_element'));
  128. $expected = 'this is the plugin element using params[plugin]';
  129. $this->assertEquals($expected, $result);
  130. }
  131. }