FlashHelperTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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' => 'Flash/default',
  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. parent::tearDown();
  73. unset($this->View, $this->Flash);
  74. }
  75. /**
  76. * testFlash method
  77. *
  78. * @return void
  79. */
  80. public function testFlash() {
  81. $result = $this->Flash->render();
  82. $expected = '<div class="message">This is a calling</div>';
  83. $this->assertContains($expected, $result);
  84. $expected = '<div id="classy-message">Recorded</div>';
  85. $result = $this->Flash->render('classy');
  86. $this->assertEquals($expected, $result);
  87. $result = $this->Flash->render('notification');
  88. $expected = [
  89. 'tag' => 'div',
  90. 'id' => 'notificationLayout',
  91. 'child' => []
  92. ];
  93. $expected['child'] = ['tag' => 'h1', 'content' => 'Alert!'];
  94. $this->assertTag($expected, $result);
  95. $expected['child'] = ['tag' => 'h3', 'content' => 'Notice!'];
  96. $this->assertTag($expected, $result);
  97. $expected['child'] = ['tag' => 'p', 'content' => 'This is a test of the emergency broadcasting system'];
  98. $this->assertTag($expected, $result);
  99. $this->assertNull($this->Flash->render('non-existent'));
  100. }
  101. /**
  102. * testFlashThrowsException
  103. *
  104. * @expectedException \UnexpectedValueException
  105. */
  106. public function testFlashThrowsException() {
  107. $this->View->request->session()->write('Flash.foo', 'bar');
  108. $this->Flash->render('foo');
  109. }
  110. /**
  111. * test setting the element from the attrs.
  112. *
  113. * @return void
  114. */
  115. public function testFlashElementInAttrs() {
  116. $result = $this->Flash->render('notification', array(
  117. 'element' => 'flash_helper',
  118. 'params' => array('title' => 'Notice!', 'name' => 'Alert!')
  119. ));
  120. $expected = [
  121. 'tag' => 'div',
  122. 'id' => 'notificationLayout',
  123. 'child' => []
  124. ];
  125. $expected['child'] = ['tag' => 'h1', 'content' => 'Alert!'];
  126. $this->assertTag($expected, $result);
  127. $expected['child'] = ['tag' => 'h3', 'content' => 'Notice!'];
  128. $this->assertTag($expected, $result);
  129. $expected['child'] = ['tag' => 'p', 'content' => 'This is a test of the emergency broadcasting system'];
  130. $this->assertTag($expected, $result);
  131. }
  132. /**
  133. * test using elements in plugins.
  134. *
  135. * @return void
  136. */
  137. public function testFlashWithPluginElement() {
  138. Plugin::load('TestPlugin');
  139. $result = $this->Flash->render('flash', array('element' => 'TestPlugin.Flash/plugin_element'));
  140. $expected = 'this is the plugin element';
  141. $this->assertEquals($expected, $result);
  142. }
  143. }