FormCheckboxTest.php 8.5 KB

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