PageRenderTime 23ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/View/Helper/FormCheckboxTest.php

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