2
0

FormErrorsTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_FormErrorsTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_FormErrorsTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  27. require_once 'Zend/View/Helper/FormErrors.php';
  28. require_once 'Zend/View.php';
  29. /**
  30. * Test class for Zend_View_Helper_FormErrors
  31. *
  32. * @category Zend
  33. * @package Zend_View
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_View
  38. * @group Zend_View_Helper
  39. */
  40. class Zend_View_Helper_FormErrorsTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Runs the test methods of this class.
  44. *
  45. * @return void
  46. */
  47. public static function main()
  48. {
  49. require_once "PHPUnit/TextUI/TestRunner.php";
  50. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_FormErrorsTest");
  51. $result = PHPUnit_TextUI_TestRunner::run($suite);
  52. }
  53. /**
  54. * Sets up the fixture, for example, open a network connection.
  55. * This method is called before a test is executed.
  56. *
  57. * @return void
  58. */
  59. public function setUp()
  60. {
  61. $this->view = new Zend_View();
  62. $this->helper = new Zend_View_Helper_FormErrors();
  63. $this->helper->setView($this->view);
  64. ob_start();
  65. }
  66. /**
  67. * Tears down the fixture, for example, close a network connection.
  68. * This method is called after a test is executed.
  69. *
  70. * @return void
  71. */
  72. public function tearDown()
  73. {
  74. ob_end_clean();
  75. }
  76. public function testGetElementEndReturnsDefaultValue()
  77. {
  78. $this->assertEquals('</li></ul>', $this->helper->getElementEnd());
  79. }
  80. public function testGetElementSeparatorReturnsDefaultValue()
  81. {
  82. $this->assertEquals('</li><li>', $this->helper->getElementSeparator());
  83. }
  84. public function testGetElementStartReturnsDefaultValue()
  85. {
  86. $this->assertEquals('<ul%s><li>', $this->helper->getElementStart());
  87. }
  88. public function testCanSetElementEndString()
  89. {
  90. $this->testGetElementEndReturnsDefaultValue();
  91. $this->helper->setElementEnd('</pre></div>');
  92. $this->assertEquals('</pre></div>', $this->helper->getElementEnd());
  93. }
  94. public function testCanSetElementSeparatorString()
  95. {
  96. $this->testGetElementSeparatorReturnsDefaultValue();
  97. $this->helper->setElementSeparator('<br />');
  98. $this->assertEquals('<br />', $this->helper->getElementSeparator());
  99. }
  100. public function testCanSetElementStartString()
  101. {
  102. $this->testGetElementStartReturnsDefaultValue();
  103. $this->helper->setElementStart('<div><pre>');
  104. $this->assertEquals('<div><pre>', $this->helper->getElementStart());
  105. }
  106. public function testFormErrorsRendersUnorderedListByDefault()
  107. {
  108. $errors = array('foo', 'bar', 'baz');
  109. $html = $this->helper->formErrors($errors);
  110. $this->assertContains('<ul', $html);
  111. foreach ($errors as $error) {
  112. $this->assertContains('<li>' . $error . '</li>', $html);
  113. }
  114. $this->assertContains('</ul>', $html);
  115. }
  116. public function testFormErrorsRendersWithSpecifiedStrings()
  117. {
  118. $this->helper->setElementStart('<dl><dt>')
  119. ->setElementSeparator('</dt><dt>')
  120. ->setElementEnd('</dt></dl>');
  121. $errors = array('foo', 'bar', 'baz');
  122. $html = $this->helper->formErrors($errors);
  123. $this->assertContains('<dl>', $html);
  124. foreach ($errors as $error) {
  125. $this->assertContains('<dt>' . $error . '</dt>', $html);
  126. }
  127. $this->assertContains('</dl>', $html);
  128. }
  129. public function testFormErrorsPreventsXssAttacks()
  130. {
  131. $errors = array(
  132. 'bad' => '\"><script>alert("xss");</script>',
  133. );
  134. $html = $this->helper->formErrors($errors);
  135. $this->assertNotContains($errors['bad'], $html);
  136. $this->assertContains('&', $html);
  137. }
  138. public function testCanDisableEscapingErrorMessages()
  139. {
  140. $errors = array(
  141. 'foo' => '<b>Field is required</b>',
  142. 'bar' => '<a href="/help">Please click here for more information</a>'
  143. );
  144. $html = $this->helper->formErrors($errors, array('escape' => false));
  145. $this->assertContains($errors['foo'], $html);
  146. $this->assertContains($errors['bar'], $html);
  147. }
  148. /**
  149. * @issue ZF-3477
  150. * @link http://framework.zend.com/issues/browse/ZF-3477
  151. */
  152. public function testCanSetClassAttribute()
  153. {
  154. $options = array('class' => 'custom-class');
  155. $acutallHtml = $this->helper->formErrors(array(), $options);
  156. $this->assertEquals('<ul class="custom-class"><li></li></ul>', $acutallHtml);
  157. }
  158. }
  159. // Call Zend_View_Helper_FormErrorsTest::main() if this source file is executed directly.
  160. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormErrorsTest::main") {
  161. Zend_View_Helper_FormErrorsTest::main();
  162. }