PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/View/Helper/FormErrorsTest.php

https://bitbucket.org/dbaltas/zend-framework-1.x-on-git
PHP | 178 lines | 106 code | 18 blank | 54 comment | 3 complexity | e5b1e5029f331efb1a63f944fd25ba7d MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: FormErrorsTest.php 24593 2012-01-05 20:35:02Z matthew $
  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 'Zend/View/Helper/FormErrors.php';
  27. require_once 'Zend/View.php';
  28. /**
  29. * Test class for Zend_View_Helper_FormErrors
  30. *
  31. * @category Zend
  32. * @package Zend_View
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_View
  37. * @group Zend_View_Helper
  38. */
  39. class Zend_View_Helper_FormErrorsTest extends PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * Runs the test methods of this class.
  43. *
  44. * @return void
  45. */
  46. public static function main()
  47. {
  48. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_FormErrorsTest");
  49. $result = PHPUnit_TextUI_TestRunner::run($suite);
  50. }
  51. /**
  52. * Sets up the fixture, for example, open a network connection.
  53. * This method is called before a test is executed.
  54. *
  55. * @return void
  56. */
  57. public function setUp()
  58. {
  59. $this->view = new Zend_View();
  60. $this->helper = new Zend_View_Helper_FormErrors();
  61. $this->helper->setView($this->view);
  62. ob_start();
  63. }
  64. /**
  65. * Tears down the fixture, for example, close a network connection.
  66. * This method is called after a test is executed.
  67. *
  68. * @return void
  69. */
  70. public function tearDown()
  71. {
  72. ob_end_clean();
  73. }
  74. public function testGetElementEndReturnsDefaultValue()
  75. {
  76. $this->assertEquals('</li></ul>', $this->helper->getElementEnd());
  77. }
  78. public function testGetElementSeparatorReturnsDefaultValue()
  79. {
  80. $this->assertEquals('</li><li>', $this->helper->getElementSeparator());
  81. }
  82. public function testGetElementStartReturnsDefaultValue()
  83. {
  84. $this->assertEquals('<ul%s><li>', $this->helper->getElementStart());
  85. }
  86. public function testCanSetElementEndString()
  87. {
  88. $this->testGetElementEndReturnsDefaultValue();
  89. $this->helper->setElementEnd('</pre></div>');
  90. $this->assertEquals('</pre></div>', $this->helper->getElementEnd());
  91. }
  92. public function testCanSetElementSeparatorString()
  93. {
  94. $this->testGetElementSeparatorReturnsDefaultValue();
  95. $this->helper->setElementSeparator('<br />');
  96. $this->assertEquals('<br />', $this->helper->getElementSeparator());
  97. }
  98. public function testCanSetElementStartString()
  99. {
  100. $this->testGetElementStartReturnsDefaultValue();
  101. $this->helper->setElementStart('<div><pre>');
  102. $this->assertEquals('<div><pre>', $this->helper->getElementStart());
  103. }
  104. public function testFormErrorsRendersUnorderedListByDefault()
  105. {
  106. $errors = array('foo', 'bar', 'baz');
  107. $html = $this->helper->formErrors($errors);
  108. $this->assertContains('<ul', $html);
  109. foreach ($errors as $error) {
  110. $this->assertContains('<li>' . $error . '</li>', $html);
  111. }
  112. $this->assertContains('</ul>', $html);
  113. }
  114. public function testFormErrorsRendersWithSpecifiedStrings()
  115. {
  116. $this->helper->setElementStart('<dl><dt>')
  117. ->setElementSeparator('</dt><dt>')
  118. ->setElementEnd('</dt></dl>');
  119. $errors = array('foo', 'bar', 'baz');
  120. $html = $this->helper->formErrors($errors);
  121. $this->assertContains('<dl>', $html);
  122. foreach ($errors as $error) {
  123. $this->assertContains('<dt>' . $error . '</dt>', $html);
  124. }
  125. $this->assertContains('</dl>', $html);
  126. }
  127. public function testFormErrorsPreventsXssAttacks()
  128. {
  129. $errors = array(
  130. 'bad' => '\"><script>alert("xss");</script>',
  131. );
  132. $html = $this->helper->formErrors($errors);
  133. $this->assertNotContains($errors['bad'], $html);
  134. $this->assertContains('&', $html);
  135. }
  136. public function testCanDisableEscapingErrorMessages()
  137. {
  138. $errors = array(
  139. 'foo' => '<b>Field is required</b>',
  140. 'bar' => '<a href="/help">Please click here for more information</a>'
  141. );
  142. $html = $this->helper->formErrors($errors, array('escape' => false));
  143. $this->assertContains($errors['foo'], $html);
  144. $this->assertContains($errors['bar'], $html);
  145. }
  146. /**
  147. * @group ZF-3477
  148. * @link http://framework.zend.com/issues/browse/ZF-3477
  149. */
  150. public function testCanSetClassAttribute()
  151. {
  152. $options = array('class' => 'custom-class');
  153. $acutallHtml = $this->helper->formErrors(array(), $options);
  154. $this->assertEquals('<ul class="custom-class"><li></li></ul>', $acutallHtml);
  155. }
  156. }
  157. // Call Zend_View_Helper_FormErrorsTest::main() if this source file is executed directly.
  158. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormErrorsTest::main") {
  159. Zend_View_Helper_FormErrorsTest::main();
  160. }