FormTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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-2011 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_View_Helper_FormTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_FormTest::main");
  25. }
  26. require_once 'Zend/View.php';
  27. require_once 'Zend/View/Helper/Form.php';
  28. /**
  29. * Test class for Zend_View_Helper_Form.
  30. *
  31. * @category Zend
  32. * @package Zend_View
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2011 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_FormTest extends PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * Runs the test methods of this class.
  43. *
  44. * @access public
  45. * @static
  46. */
  47. public static function main()
  48. {
  49. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_FormTest");
  50. $result = PHPUnit_TextUI_TestRunner::run($suite);
  51. }
  52. /**
  53. * Sets up the fixture, for example, open a network connection.
  54. * This method is called before a test is executed.
  55. *
  56. * @access protected
  57. */
  58. protected function setUp()
  59. {
  60. $this->view = new Zend_View();
  61. $this->helper = new Zend_View_Helper_Form();
  62. $this->helper->setView($this->view);
  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. * @access protected
  69. */
  70. protected function tearDown()
  71. {
  72. }
  73. public function testFormWithSaneInput()
  74. {
  75. $form = $this->helper->form('foo', array('action' => '/foo', 'method' => 'get'));
  76. $this->assertRegexp('/<form[^>]*(id="foo")/', $form);
  77. $this->assertRegexp('/<form[^>]*(action="\/foo")/', $form);
  78. $this->assertRegexp('/<form[^>]*(method="get")/', $form);
  79. }
  80. public function testFormWithInputNeedingEscapesUsesViewEscaping()
  81. {
  82. $form = $this->helper->form('<&foo');
  83. $this->assertContains($this->view->escape('<&foo'), $form);
  84. }
  85. public function testPassingIdAsAttributeShouldRenderIdAttribAndNotName()
  86. {
  87. $form = $this->helper->form('foo', array('action' => '/foo', 'method' => 'get', 'id' => 'bar'));
  88. $this->assertRegexp('/<form[^>]*(id="bar")/', $form);
  89. $this->assertNotRegexp('/<form[^>]*(name="foo")/', $form);
  90. }
  91. /**
  92. * @group ZF-3832
  93. */
  94. public function testEmptyIdShouldNotRenderIdAttribute()
  95. {
  96. $form = $this->helper->form('', array('action' => '/foo', 'method' => 'get'));
  97. $this->assertNotRegexp('/<form[^>]*(id="")/', $form);
  98. $form = $this->helper->form('', array('action' => '/foo', 'method' => 'get', 'id' => null));
  99. $this->assertNotRegexp('/<form[^>]*(id="")/', $form);
  100. }
  101. /**
  102. * @group ZF-10791
  103. */
  104. public function testPassingNameAsAttributeShouldOverrideFormName()
  105. {
  106. $form = $this->helper->form('OrigName', array('action' => '/foo', 'method' => 'get', 'name' => 'SomeNameAttr'));
  107. $this->assertNotRegexp('/<form[^>]*(name="OrigName")/', $form);
  108. $this->assertRegexp('/<form[^>]*(name="SomeNameAttr")/', $form);
  109. }
  110. /**
  111. * @group ZF-10791
  112. */
  113. public function testNotSpecifyingFormNameShouldNotRenderNameAttrib()
  114. {
  115. $form = $this->helper->form('', array('action' => '/foo', 'method' => 'get'));
  116. $this->assertNotRegexp('/<form[^>]*(name=".*")/', $form);
  117. }
  118. /**
  119. * @group ZF-10791
  120. */
  121. public function testSpecifyingFormNameShouldRenderNameAttrib()
  122. {
  123. $form = $this->helper->form('FormName', array('action' => '/foo', 'method' => 'get'));
  124. $this->assertRegexp('/<form[^>]*(name="FormName")/', $form);
  125. }
  126. /**
  127. * @group ZF-10791
  128. */
  129. public function testPassingEmptyNameAttributeToUnnamedFormShouldNotRenderNameAttrib()
  130. {
  131. $form = $this->helper->form('', array('action' => '/foo', 'method' => 'get', 'name' => NULL));
  132. $this->assertNotRegexp('/<form[^>]*(name=".*")/', $form);
  133. }
  134. /**
  135. * @group ZF-10791
  136. */
  137. public function testPassingEmptyNameAttributeToNamedFormShouldNotOverrideNameAttrib()
  138. {
  139. $form = $this->helper->form('RealName', array('action' => '/foo', 'method' => 'get', 'name' => NULL));
  140. $this->assertRegexp('/<form[^>]*(name="RealName")/', $form);
  141. }
  142. /**
  143. * @group ZF-10791
  144. */
  145. public function testNameAttributeShouldBeOmittedWhenUsingXhtml1Strict()
  146. {
  147. $this->view->doctype('XHTML1_STRICT');
  148. $form = $this->helper->form('FormName', array('action' => '/foo', 'method' => 'get'));
  149. $this->assertNotRegexp('/<form[^>]*(name="FormName")/', $form);
  150. }
  151. /**
  152. * @group ZF-10791
  153. */
  154. public function testNameAttributeShouldBeOmittedWhenUsingXhtml11()
  155. {
  156. $this->view->doctype('XHTML11');
  157. $form = $this->helper->form('FormName', array('action' => '/foo', 'method' => 'get'));
  158. $this->assertNotRegexp('/<form[^>]*(name="FormName")/', $form);
  159. }
  160. }
  161. // Call Zend_View_Helper_FormTest::main() if this source file is executed directly.
  162. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormTest::main") {
  163. Zend_View_Helper_FormTest::main();
  164. }