FormCheckboxTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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-2015 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_FormCheckboxTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_FormCheckboxTest::main");
  25. }
  26. require_once 'Zend/View/Helper/FormCheckbox.php';
  27. require_once 'Zend/View.php';
  28. require_once 'Zend/Registry.php';
  29. /**
  30. * Zend_View_Helper_FormCheckboxTest
  31. *
  32. * Tests formCheckbox helper
  33. *
  34. * @category Zend
  35. * @package Zend_View
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_View
  40. * @group Zend_View_Helper
  41. */
  42. class Zend_View_Helper_FormCheckboxTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * Runs the test methods of this class.
  46. *
  47. * @access public
  48. * @static
  49. */
  50. public static function main()
  51. {
  52. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_FormCheckboxTest");
  53. $result = PHPUnit_TextUI_TestRunner::run($suite);
  54. }
  55. public function setUp()
  56. {
  57. if (Zend_Registry::isRegistered('Zend_View_Helper_Doctype')) {
  58. $registry = Zend_Registry::getInstance();
  59. unset($registry['Zend_View_Helper_Doctype']);
  60. }
  61. $this->view = new Zend_View();
  62. $this->helper = new Zend_View_Helper_FormCheckbox();
  63. $this->helper->setView($this->view);
  64. }
  65. public function testIdSetFromName()
  66. {
  67. $element = $this->helper->formCheckbox('foo');
  68. $this->assertContains('name="foo"', $element);
  69. $this->assertContains('id="foo"', $element);
  70. }
  71. public function testSetIdFromAttribs()
  72. {
  73. $element = $this->helper->formCheckbox('foo', null, array('id' => 'bar'));
  74. $this->assertContains('name="foo"', $element);
  75. $this->assertContains('id="bar"', $element);
  76. }
  77. /**
  78. * ZF-2513
  79. */
  80. public function testCanDisableCheckbox()
  81. {
  82. $html = $this->helper->formCheckbox(array(
  83. 'name' => 'foo',
  84. 'value' => 'bar',
  85. 'attribs'=> array('disable' => true)
  86. ));
  87. $this->assertRegexp('/<input[^>]*?(disabled="disabled")/', $html);
  88. }
  89. /**
  90. * ZF-3505
  91. */
  92. public function testCheckboxNotDisabled()
  93. {
  94. $html = $this->helper->formCheckbox(array(
  95. 'name' => 'foo',
  96. 'value' => 'bar',
  97. 'attribs'=> array('disable' => false)
  98. ));
  99. $this->assertNotContains('disabled="disabled"', $html);
  100. }
  101. public function testCanSelectCheckbox()
  102. {
  103. $html = $this->helper->formCheckbox(array(
  104. 'name' => 'foo',
  105. 'value' => 'bar',
  106. 'attribs'=> array('checked' => true)
  107. ));
  108. $this->assertRegexp('/<input[^>]*?(checked="checked")/', $html);
  109. $count = substr_count($html, 'checked');
  110. $this->assertEquals(2, $count);
  111. }
  112. /**
  113. * ZF-1955
  114. */
  115. public function testNameBracketsStrippedWhenCreatingId()
  116. {
  117. $html = $this->helper->formCheckbox(array(
  118. 'name' => 'foo[]',
  119. 'value' => 'bar'
  120. ));
  121. $this->assertRegexp('/<input[^>]*?(id="foo")/', $html);
  122. $html = $this->helper->formCheckbox(array(
  123. 'name' => 'foo[bar]',
  124. 'value' => 'bar'
  125. ));
  126. $this->assertRegexp('/<input[^>]*?(id="foo-bar")/', $html);
  127. $html = $this->helper->formCheckbox(array(
  128. 'name' => 'foo[bar][baz]',
  129. 'value' => 'bar'
  130. ));
  131. $this->assertRegexp('/<input[^>]*?(id="foo-bar-baz")/', $html);
  132. }
  133. /**
  134. * @group ZF-2230
  135. */
  136. public function testDoesNotRenderHiddenElementsForCheckboxArray()
  137. {
  138. $html = $this->helper->formCheckbox(array(
  139. 'name' => 'foo[]',
  140. 'value' => 'bar'
  141. ));
  142. $this->assertNotRegexp('/<input[^>]*?(type="hidden")/', $html);
  143. }
  144. /**
  145. * @group ZF-3149
  146. */
  147. public function testShouldRenderHiddenElementShowingUncheckedOptionForNonArrayNames()
  148. {
  149. $html1 = $this->helper->formCheckbox(
  150. 'foo',
  151. 'bar',
  152. array('checked' => true),
  153. array(
  154. 'checked' => 'bar',
  155. 'unChecked' => 'baz'
  156. )
  157. );
  158. $html2 = $this->helper->formCheckbox(
  159. 'foo',
  160. 'bar',
  161. array('checked' => true),
  162. array(
  163. 'bar',
  164. 'baz'
  165. )
  166. );
  167. $html3 = $this->helper->formCheckbox(
  168. 'foo',
  169. 'bar',
  170. array('checked' => false),
  171. array(
  172. 'checked' => 'bar',
  173. 'unChecked' => 'baz'
  174. )
  175. );
  176. $html4 = $this->helper->formCheckbox(
  177. 'foo',
  178. 'bar',
  179. array('checked' => false),
  180. array(
  181. 'bar',
  182. 'baz'
  183. )
  184. );
  185. foreach (array('html1', 'html2', 'html3', 'html4') as $html) {
  186. if (!preg_match_all('/(<input [^>]+>)/', $$html, $matches)) {
  187. $this->fail('Unexpected output generated by helper');
  188. }
  189. $this->assertEquals(2, count($matches[1]));
  190. foreach ($matches[1] as $element) {
  191. if (strstr($element, 'hidden')) {
  192. $this->assertContains('baz', $element, 'Failed using ' . $html);
  193. } else {
  194. $this->assertContains('bar', $element, 'Failed using ' . $html);
  195. $this->assertContains('checked', $element, 'Failed using ' . $html);
  196. }
  197. }
  198. }
  199. }
  200. /**
  201. * @group ZF-3149
  202. */
  203. public function testCheckedAttributeNotRenderedIfItEvaluatesToFalse()
  204. {
  205. $test = $this->helper->formCheckbox('foo', 'value', array('checked' => false));
  206. $this->assertNotContains('checked', $test);
  207. }
  208. public function testCanSpecifyValue()
  209. {
  210. $test = $this->helper->formCheckbox('foo', 'bar');
  211. $this->assertContains('value="bar"', $test);
  212. }
  213. /**
  214. * @group ZF-3149
  215. */
  216. public function testShouldCheckValueIfValueMatchesCheckedOption()
  217. {
  218. $test = $this->helper->formCheckbox('foo', 'bar', array(), array('bar', 'baz'));
  219. $this->assertContains('value="bar"', $test);
  220. $this->assertContains('checked', $test);
  221. $test = $this->helper->formCheckbox('foo', 'bar', array(), array('checked' => 'bar', 'unChecked' => 'baz'));
  222. $this->assertContains('value="bar"', $test);
  223. $this->assertContains('checked', $test);
  224. }
  225. /**
  226. * @group ZF-3149
  227. */
  228. public function testShouldOnlySetValueIfValueMatchesCheckedOption()
  229. {
  230. $test = $this->helper->formCheckbox('foo', 'baz', array(), array('bar', 'baz'));
  231. $this->assertContains('value="bar"', $test);
  232. }
  233. /**
  234. * @group ZF-3149
  235. */
  236. public function testShouldNotCheckValueIfValueDoesNotMatchCheckedOption()
  237. {
  238. $test = $this->helper->formCheckbox('foo', 'baz', array(), array('bar', 'baz'));
  239. $this->assertContains('value="bar"', $test);
  240. $this->assertNotContains('checked', $test);
  241. }
  242. public function testRendersAsHtmlByDefault()
  243. {
  244. $test = $this->helper->formCheckbox('foo', 'bar');
  245. $this->assertNotContains(' />', $test, $test);
  246. }
  247. public function testCanRendersAsXHtml()
  248. {
  249. $this->view->doctype('XHTML1_STRICT');
  250. $test = $this->helper->formCheckbox('foo', 'bar');
  251. $this->assertContains(' />', $test);
  252. }
  253. /**
  254. * @group ZF-6467
  255. */
  256. public function testShouldNotShowHiddenFieldIfDisableIsTrue()
  257. {
  258. $test = $this->helper->formCheckbox('foo', 'bar', array('disable' => true));
  259. $this->assertNotContains('type="hidden"', $test);
  260. }
  261. public function testIntValueIsChecked()
  262. {
  263. $test = $this->helper->formCheckbox('foo', '1', array(), array('checked'=>1, 'unchecked'=>0));
  264. $this->assertContains('checked="checked"', $test);
  265. $test = $this->helper->formCheckbox('foo', '1', array(), array(1,0));
  266. $this->assertContains('checked="checked"', $test);
  267. $test = $this->helper->formCheckbox('foo', 1, array(), array('checked'=>1, 'unchecked'=>0));
  268. $this->assertContains('checked="checked"', $test);
  269. $test = $this->helper->formCheckbox('foo', 1, array(), array(1,0));
  270. $this->assertContains('checked="checked"', $test);
  271. $test = $this->helper->formCheckbox('foo', 0, array(), array('checked'=>1, 'unchecked'=>0));
  272. $this->assertNotContains('checked="checked"', $test);
  273. $test = $this->helper->formCheckbox('foo', 0, array(), array(1,0));
  274. $this->assertNotContains('checked="checked"', $test);
  275. }
  276. /**
  277. * @group ZF-6624
  278. */
  279. public function testRenderingWithoutHiddenElement()
  280. {
  281. $html = $this->helper->formCheckbox(
  282. 'foo',
  283. 'bar',
  284. array(
  285. 'disableHidden' => true,
  286. )
  287. );
  288. $this->assertSame(
  289. '<input type="checkbox" name="foo" id="foo" value="bar">',
  290. $html
  291. );
  292. $html = $this->helper->formCheckbox(
  293. 'foo',
  294. 'bar');
  295. $this->assertSame(
  296. '<input type="hidden" name="foo" value="0"><input type="checkbox" name="foo" id="foo" value="bar">',
  297. $html
  298. );
  299. $html = $this->helper->formCheckbox(
  300. 'foo',
  301. 'bar',
  302. array(
  303. 'disableHidden' => false,
  304. )
  305. );
  306. $this->assertSame(
  307. '<input type="hidden" name="foo" value="0"><input type="checkbox" name="foo" id="foo" value="bar">',
  308. $html
  309. );
  310. }
  311. }
  312. // Call Zend_View_Helper_FormCheckboxTest::main() if this source file is executed directly.
  313. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormCheckboxTest::main") {
  314. Zend_View_Helper_FormCheckboxTest::main();
  315. }