FormTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. // Call Zend_View_Helper_FormTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_FormTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once 'Zend/View.php';
  8. require_once 'Zend/View/Helper/Form.php';
  9. /**
  10. * Test class for Zend_View_Helper_Form.
  11. */
  12. class Zend_View_Helper_FormTest extends PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * Runs the test methods of this class.
  16. *
  17. * @access public
  18. * @static
  19. */
  20. public static function main()
  21. {
  22. require_once "PHPUnit/TextUI/TestRunner.php";
  23. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_FormTest");
  24. $result = PHPUnit_TextUI_TestRunner::run($suite);
  25. }
  26. /**
  27. * Sets up the fixture, for example, open a network connection.
  28. * This method is called before a test is executed.
  29. *
  30. * @access protected
  31. */
  32. protected function setUp()
  33. {
  34. $this->view = new Zend_View();
  35. $this->helper = new Zend_View_Helper_Form();
  36. $this->helper->setView($this->view);
  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. * @access protected
  43. */
  44. protected function tearDown()
  45. {
  46. }
  47. public function testFormWithSaneInput()
  48. {
  49. $form = $this->helper->form('foo', array('action' => '/foo', 'method' => 'get'));
  50. $this->assertRegexp('/<form[^>]*(id="foo")/', $form);
  51. $this->assertRegexp('/<form[^>]*(action="\/foo")/', $form);
  52. $this->assertRegexp('/<form[^>]*(method="get")/', $form);
  53. }
  54. public function testFormWithInputNeedingEscapesUsesViewEscaping()
  55. {
  56. $form = $this->helper->form('<&foo');
  57. $this->assertContains($this->view->escape('<&foo'), $form);
  58. }
  59. public function testPassingIdAsAttributeShouldRenderIdAttribAndNotName()
  60. {
  61. $form = $this->helper->form('foo', array('action' => '/foo', 'method' => 'get', 'id' => 'bar'));
  62. $this->assertRegexp('/<form[^>]*(id="bar")/', $form);
  63. $this->assertNotRegexp('/<form[^>]*(name="foo")/', $form);
  64. }
  65. /**
  66. * @see ZF-3832
  67. */
  68. public function testEmptyIdShouldNotRenderIdAttribute()
  69. {
  70. $form = $this->helper->form('', array('action' => '/foo', 'method' => 'get'));
  71. $this->assertNotRegexp('/<form[^>]*(id="")/', $form);
  72. $form = $this->helper->form('', array('action' => '/foo', 'method' => 'get', 'id' => null));
  73. $this->assertNotRegexp('/<form[^>]*(id="")/', $form);
  74. }
  75. }
  76. // Call Zend_View_Helper_FormTest::main() if this source file is executed directly.
  77. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormTest::main") {
  78. Zend_View_Helper_FormTest::main();
  79. }