FormErrorsTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. // Call Zend_FormErrorsTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_FormErrorsTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once 'Zend/View/Helper/FormErrors.php';
  8. require_once 'Zend/View.php';
  9. /**
  10. * Test class for Zend_View_Helper_FormErrors
  11. */
  12. class Zend_View_Helper_FormErrorsTest extends PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * Runs the test methods of this class.
  16. *
  17. * @return void
  18. */
  19. public static function main()
  20. {
  21. require_once "PHPUnit/TextUI/TestRunner.php";
  22. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_FormErrorsTest");
  23. $result = PHPUnit_TextUI_TestRunner::run($suite);
  24. }
  25. /**
  26. * Sets up the fixture, for example, open a network connection.
  27. * This method is called before a test is executed.
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. $this->view = new Zend_View();
  34. $this->helper = new Zend_View_Helper_FormErrors();
  35. $this->helper->setView($this->view);
  36. ob_start();
  37. }
  38. /**
  39. * Tears down the fixture, for example, close a network connection.
  40. * This method is called after a test is executed.
  41. *
  42. * @return void
  43. */
  44. public function tearDown()
  45. {
  46. ob_end_clean();
  47. }
  48. public function testGetElementEndReturnsDefaultValue()
  49. {
  50. $this->assertEquals('</li></ul>', $this->helper->getElementEnd());
  51. }
  52. public function testGetElementSeparatorReturnsDefaultValue()
  53. {
  54. $this->assertEquals('</li><li>', $this->helper->getElementSeparator());
  55. }
  56. public function testGetElementStartReturnsDefaultValue()
  57. {
  58. $this->assertEquals('<ul%s><li>', $this->helper->getElementStart());
  59. }
  60. public function testCanSetElementEndString()
  61. {
  62. $this->testGetElementEndReturnsDefaultValue();
  63. $this->helper->setElementEnd('</pre></div>');
  64. $this->assertEquals('</pre></div>', $this->helper->getElementEnd());
  65. }
  66. public function testCanSetElementSeparatorString()
  67. {
  68. $this->testGetElementSeparatorReturnsDefaultValue();
  69. $this->helper->setElementSeparator('<br />');
  70. $this->assertEquals('<br />', $this->helper->getElementSeparator());
  71. }
  72. public function testCanSetElementStartString()
  73. {
  74. $this->testGetElementStartReturnsDefaultValue();
  75. $this->helper->setElementStart('<div><pre>');
  76. $this->assertEquals('<div><pre>', $this->helper->getElementStart());
  77. }
  78. public function testFormErrorsRendersUnorderedListByDefault()
  79. {
  80. $errors = array('foo', 'bar', 'baz');
  81. $html = $this->helper->formErrors($errors);
  82. $this->assertContains('<ul', $html);
  83. foreach ($errors as $error) {
  84. $this->assertContains('<li>' . $error . '</li>', $html);
  85. }
  86. $this->assertContains('</ul>', $html);
  87. }
  88. public function testFormErrorsRendersWithSpecifiedStrings()
  89. {
  90. $this->helper->setElementStart('<dl><dt>')
  91. ->setElementSeparator('</dt><dt>')
  92. ->setElementEnd('</dt></dl>');
  93. $errors = array('foo', 'bar', 'baz');
  94. $html = $this->helper->formErrors($errors);
  95. $this->assertContains('<dl>', $html);
  96. foreach ($errors as $error) {
  97. $this->assertContains('<dt>' . $error . '</dt>', $html);
  98. }
  99. $this->assertContains('</dl>', $html);
  100. }
  101. public function testFormErrorsPreventsXssAttacks()
  102. {
  103. $errors = array(
  104. 'bad' => '\"><script>alert("xss");</script>',
  105. );
  106. $html = $this->helper->formErrors($errors);
  107. $this->assertNotContains($errors['bad'], $html);
  108. $this->assertContains('&', $html);
  109. }
  110. public function testCanDisableEscapingErrorMessages()
  111. {
  112. $errors = array(
  113. 'foo' => '<b>Field is required</b>',
  114. 'bar' => '<a href="/help">Please click here for more information</a>'
  115. );
  116. $html = $this->helper->formErrors($errors, array('escape' => false));
  117. $this->assertContains($errors['foo'], $html);
  118. $this->assertContains($errors['bar'], $html);
  119. }
  120. /**
  121. * @issue ZF-3477
  122. * @link http://framework.zend.com/issues/browse/ZF-3477
  123. */
  124. public function testCanSetClassAttribute()
  125. {
  126. $options = array('class' => 'custom-class');
  127. $acutallHtml = $this->helper->formErrors(array(), $options);
  128. $this->assertEquals('<ul class="custom-class"><li></li></ul>', $acutallHtml);
  129. }
  130. }
  131. // Call Zend_View_Helper_FormErrorsTest::main() if this source file is executed directly.
  132. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormErrorsTest::main") {
  133. Zend_View_Helper_FormErrorsTest::main();
  134. }