FormCheckboxTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. // Call Zend_View_Helper_FormCheckboxTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_FormCheckboxTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once 'Zend/View/Helper/FormCheckbox.php';
  8. require_once 'Zend/View.php';
  9. require_once 'Zend/Registry.php';
  10. /**
  11. * Zend_View_Helper_FormCheckboxTest
  12. *
  13. * Tests formCheckbox helper
  14. *
  15. * @uses PHPUnit_Framework_TestCase
  16. */
  17. class Zend_View_Helper_FormCheckboxTest extends PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * Runs the test methods of this class.
  21. *
  22. * @access public
  23. * @static
  24. */
  25. public static function main()
  26. {
  27. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_FormCheckboxTest");
  28. $result = PHPUnit_TextUI_TestRunner::run($suite);
  29. }
  30. public function setUp()
  31. {
  32. if (Zend_Registry::isRegistered('Zend_View_Helper_Doctype')) {
  33. $registry = Zend_Registry::getInstance();
  34. unset($registry['Zend_View_Helper_Doctype']);
  35. }
  36. $this->view = new Zend_View();
  37. $this->helper = new Zend_View_Helper_FormCheckbox();
  38. $this->helper->setView($this->view);
  39. }
  40. public function testIdSetFromName()
  41. {
  42. $element = $this->helper->formCheckbox('foo');
  43. $this->assertContains('name="foo"', $element);
  44. $this->assertContains('id="foo"', $element);
  45. }
  46. public function testSetIdFromAttribs()
  47. {
  48. $element = $this->helper->formCheckbox('foo', null, array('id' => 'bar'));
  49. $this->assertContains('name="foo"', $element);
  50. $this->assertContains('id="bar"', $element);
  51. }
  52. /**
  53. * ZF-2513
  54. */
  55. public function testCanDisableCheckbox()
  56. {
  57. $html = $this->helper->formCheckbox(array(
  58. 'name' => 'foo',
  59. 'value' => 'bar',
  60. 'attribs'=> array('disable' => true)
  61. ));
  62. $this->assertRegexp('/<input[^>]*?(disabled="disabled")/', $html);
  63. }
  64. /**
  65. * ZF-3505
  66. */
  67. public function testCheckboxNotDisabled()
  68. {
  69. $html = $this->helper->formCheckbox(array(
  70. 'name' => 'foo',
  71. 'value' => 'bar',
  72. 'attribs'=> array('disable' => false)
  73. ));
  74. $this->assertNotContains('disabled="disabled"', $html);
  75. }
  76. public function testCanSelectCheckbox()
  77. {
  78. $html = $this->helper->formCheckbox(array(
  79. 'name' => 'foo',
  80. 'value' => 'bar',
  81. 'attribs'=> array('checked' => true)
  82. ));
  83. $this->assertRegexp('/<input[^>]*?(checked="checked")/', $html);
  84. $count = substr_count($html, 'checked');
  85. $this->assertEquals(2, $count);
  86. }
  87. /**
  88. * ZF-1955
  89. */
  90. public function testNameBracketsStrippedWhenCreatingId()
  91. {
  92. $html = $this->helper->formCheckbox(array(
  93. 'name' => 'foo[]',
  94. 'value' => 'bar'
  95. ));
  96. $this->assertRegexp('/<input[^>]*?(id="foo")/', $html);
  97. $html = $this->helper->formCheckbox(array(
  98. 'name' => 'foo[bar]',
  99. 'value' => 'bar'
  100. ));
  101. $this->assertRegexp('/<input[^>]*?(id="foo-bar")/', $html);
  102. $html = $this->helper->formCheckbox(array(
  103. 'name' => 'foo[bar][baz]',
  104. 'value' => 'bar'
  105. ));
  106. $this->assertRegexp('/<input[^>]*?(id="foo-bar-baz")/', $html);
  107. }
  108. /**
  109. * @see ZF-2230
  110. */
  111. public function testDoesNotRenderHiddenElementsForCheckboxArray()
  112. {
  113. $html = $this->helper->formCheckbox(array(
  114. 'name' => 'foo[]',
  115. 'value' => 'bar'
  116. ));
  117. $this->assertNotRegexp('/<input[^>]*?(type="hidden")/', $html);
  118. }
  119. /**
  120. * @see ZF-3149
  121. */
  122. public function testShouldRenderHiddenElementShowingUncheckedOptionForNonArrayNames()
  123. {
  124. $html1 = $this->helper->formCheckbox(
  125. 'foo',
  126. 'bar',
  127. array('checked' => true),
  128. array(
  129. 'checked' => 'bar',
  130. 'unChecked' => 'baz'
  131. )
  132. );
  133. $html2 = $this->helper->formCheckbox(
  134. 'foo',
  135. 'bar',
  136. array('checked' => true),
  137. array(
  138. 'bar',
  139. 'baz'
  140. )
  141. );
  142. $html3 = $this->helper->formCheckbox(
  143. 'foo',
  144. 'bar',
  145. array('checked' => false),
  146. array(
  147. 'checked' => 'bar',
  148. 'unChecked' => 'baz'
  149. )
  150. );
  151. $html4 = $this->helper->formCheckbox(
  152. 'foo',
  153. 'bar',
  154. array('checked' => false),
  155. array(
  156. 'bar',
  157. 'baz'
  158. )
  159. );
  160. foreach (array('html1', 'html2', 'html3', 'html4') as $html) {
  161. if (!preg_match_all('/(<input [^>]+>)/', $$html, $matches)) {
  162. $this->fail('Unexpected output generated by helper');
  163. }
  164. $this->assertEquals(2, count($matches[1]));
  165. foreach ($matches[1] as $element) {
  166. if (strstr($element, 'hidden')) {
  167. $this->assertContains('baz', $element, 'Failed using ' . $html);
  168. } else {
  169. $this->assertContains('bar', $element, 'Failed using ' . $html);
  170. $this->assertContains('checked', $element, 'Failed using ' . $html);
  171. }
  172. }
  173. }
  174. }
  175. /**
  176. * @see ZF-3149
  177. */
  178. public function testCheckedAttributeNotRenderedIfItEvaluatesToFalse()
  179. {
  180. $test = $this->helper->formCheckbox('foo', 'value', array('checked' => false));
  181. $this->assertNotContains('checked', $test);
  182. }
  183. public function testCanSpecifyValue()
  184. {
  185. $test = $this->helper->formCheckbox('foo', 'bar');
  186. $this->assertContains('value="bar"', $test);
  187. }
  188. /**
  189. * @see ZF-3149
  190. */
  191. public function testShouldCheckValueIfValueMatchesCheckedOption()
  192. {
  193. $test = $this->helper->formCheckbox('foo', 'bar', array(), array('bar', 'baz'));
  194. $this->assertContains('value="bar"', $test);
  195. $this->assertContains('checked', $test);
  196. $test = $this->helper->formCheckbox('foo', 'bar', array(), array('checked' => 'bar', 'unChecked' => 'baz'));
  197. $this->assertContains('value="bar"', $test);
  198. $this->assertContains('checked', $test);
  199. }
  200. /**
  201. * @see ZF-3149
  202. */
  203. public function testShouldOnlySetValueIfValueMatchesCheckedOption()
  204. {
  205. $test = $this->helper->formCheckbox('foo', 'baz', array(), array('bar', 'baz'));
  206. $this->assertContains('value="bar"', $test);
  207. }
  208. /**
  209. * @see ZF-3149
  210. */
  211. public function testShouldNotCheckValueIfValueDoesNotMatchCheckedOption()
  212. {
  213. $test = $this->helper->formCheckbox('foo', 'baz', array(), array('bar', 'baz'));
  214. $this->assertContains('value="bar"', $test);
  215. $this->assertNotContains('checked', $test);
  216. }
  217. public function testRendersAsHtmlByDefault()
  218. {
  219. $test = $this->helper->formCheckbox('foo', 'bar');
  220. $this->assertNotContains(' />', $test, $test);
  221. }
  222. public function testCanRendersAsXHtml()
  223. {
  224. $this->view->doctype('XHTML1_STRICT');
  225. $test = $this->helper->formCheckbox('foo', 'bar');
  226. $this->assertContains(' />', $test);
  227. }
  228. }
  229. // Call Zend_View_Helper_FormCheckboxTest::main() if this source file is executed directly.
  230. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormCheckboxTest::main") {
  231. Zend_View_Helper_FormCheckboxTest::main();
  232. }