MultiCheckboxTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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_Form
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2014 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_Form_Element_MultiCheckboxTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_MultiCheckboxTest::main");
  25. }
  26. require_once 'Zend/Form/Element/MultiCheckbox.php';
  27. /**
  28. * Test class for Zend_Form_Element_MultiCheckbox
  29. *
  30. * @category Zend
  31. * @package Zend_Form
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Form
  36. */
  37. class Zend_Form_Element_MultiCheckboxTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Runs the test methods of this class.
  41. *
  42. * @return void
  43. */
  44. public static function main()
  45. {
  46. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_MultiCheckboxTest");
  47. $result = PHPUnit_TextUI_TestRunner::run($suite);
  48. }
  49. /**
  50. * Sets up the fixture, for example, open a network connection.
  51. * This method is called before a test is executed.
  52. *
  53. * @return void
  54. */
  55. public function setUp()
  56. {
  57. $this->element = new Zend_Form_Element_MultiCheckbox('foo');
  58. }
  59. /**
  60. * Tears down the fixture, for example, close a network connection.
  61. * This method is called after a test is executed.
  62. *
  63. * @return void
  64. */
  65. public function tearDown()
  66. {
  67. }
  68. public function getView()
  69. {
  70. require_once 'Zend/View.php';
  71. $view = new Zend_View();
  72. $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper');
  73. return $view;
  74. }
  75. public function testMultiCheckboxElementSubclassesMultiElement()
  76. {
  77. $this->assertTrue($this->element instanceof Zend_Form_Element_Multi);
  78. }
  79. public function testMultiCheckboxElementSubclassesXhtmlElement()
  80. {
  81. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  82. }
  83. public function testMultiCheckboxElementInstanceOfBaseElement()
  84. {
  85. $this->assertTrue($this->element instanceof Zend_Form_Element);
  86. }
  87. public function testMultiCheckboxElementIsAnArrayByDefault()
  88. {
  89. $this->assertTrue($this->element->isArray());
  90. }
  91. public function testHelperAttributeSetToFormMultiCheckboxByDefault()
  92. {
  93. $this->assertEquals('formMultiCheckbox', $this->element->getAttrib('helper'));
  94. }
  95. public function testMultiCheckboxElementUsesMultiCheckboxHelperInViewHelperDecoratorByDefault()
  96. {
  97. $this->_checkZf2794();
  98. $decorator = $this->element->getDecorator('viewHelper');
  99. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  100. $decorator->setElement($this->element);
  101. $helper = $decorator->getHelper();
  102. $this->assertEquals('formMultiCheckbox', $helper);
  103. }
  104. public function testCanDisableIndividualMultiCheckboxOptions()
  105. {
  106. $this->element->setMultiOptions(array(
  107. 'foo' => 'Foo',
  108. 'bar' => 'Bar',
  109. 'baz' => 'Baz',
  110. 'bat' => 'Bat',
  111. 'test' => 'Test',
  112. ))
  113. ->setAttrib('disable', array('baz', 'test'));
  114. $html = $this->element->render($this->getView());
  115. foreach (array('baz', 'test') as $test) {
  116. if (!preg_match('/(<input[^>]*?(value="' . $test . '")[^>]*>)/', $html, $m)) {
  117. $this->fail('Unable to find matching disabled option for ' . $test);
  118. }
  119. $this->assertRegexp('/<input[^>]*?(disabled="disabled")/', $m[1]);
  120. }
  121. foreach (array('foo', 'bar', 'bat') as $test) {
  122. if (!preg_match('/(<input[^>]*?(value="' . $test . '")[^>]*>)/', $html, $m)) {
  123. $this->fail('Unable to find matching option for ' . $test);
  124. }
  125. $this->assertNotRegexp('/<input[^>]*?(disabled="disabled")/', $m[1], var_export($m, 1));
  126. }
  127. }
  128. public function testSpecifiedSeparatorIsUsedWhenRendering()
  129. {
  130. $this->element->setMultiOptions(array(
  131. 'foo' => 'Foo',
  132. 'bar' => 'Bar',
  133. 'baz' => 'Baz',
  134. 'bat' => 'Bat',
  135. 'test' => 'Test',
  136. ))
  137. ->setSeparator('--FooBarFunSep--');
  138. $html = $this->element->render($this->getView());
  139. $this->assertContains($this->element->getSeparator(), $html);
  140. $count = substr_count($html, $this->element->getSeparator());
  141. $this->assertEquals(4, $count);
  142. }
  143. /**
  144. * @group ZF-2830
  145. */
  146. public function testRenderingMulticheckboxCreatesCorrectArrayNotation()
  147. {
  148. $this->element->addMultiOption(1, 'A');
  149. $this->element->addMultiOption(2, 'B');
  150. $html = $this->element->render($this->getView());
  151. $this->assertContains('name="foo[]"', $html, $html);
  152. $count = substr_count($html, 'name="foo[]"');
  153. $this->assertEquals(2, $count);
  154. }
  155. /**
  156. * @group ZF-2828
  157. */
  158. public function testCanPopulateCheckboxOptionsFromPostedData()
  159. {
  160. $form = new Zend_Form(array(
  161. 'elements' => array(
  162. '100_1' => array('MultiCheckbox', array(
  163. 'multiOptions' => array(
  164. '100_1_1' => 'Agriculture',
  165. '100_1_2' => 'Automotive',
  166. '100_1_12' => 'Chemical',
  167. '100_1_13' => 'Communications',
  168. ),
  169. 'required' => true,
  170. )),
  171. ),
  172. ));
  173. $data = array(
  174. '100_1' => array(
  175. '100_1_1',
  176. '100_1_2',
  177. '100_1_12',
  178. '100_1_13'
  179. ),
  180. );
  181. $form->populate($data);
  182. $html = $form->render($this->getView());
  183. foreach ($form->getElement('100_1')->getMultiOptions() as $key => $value) {
  184. if (!preg_match('#(<input[^>]*' . $key . '[^>]*>)#', $html, $m)) {
  185. $this->fail('Missing input for a given multi option: ' . $html);
  186. }
  187. $this->assertContains('checked="checked"', $m[1]);
  188. }
  189. }
  190. /**
  191. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  192. *
  193. * @link http://framework.zend.com/issues/browse/ZF-2794
  194. * @return void
  195. */
  196. protected function _checkZf2794()
  197. {
  198. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  199. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  200. }
  201. }
  202. /**#+
  203. * @group ZF-3286
  204. */
  205. public function testShouldRegisterInArrayValidatorByDefault()
  206. {
  207. $this->assertTrue($this->element->registerInArrayValidator());
  208. }
  209. public function testShouldAllowSpecifyingWhetherOrNotToUseInArrayValidator()
  210. {
  211. $this->testShouldRegisterInArrayValidatorByDefault();
  212. $this->element->setRegisterInArrayValidator(false);
  213. $this->assertFalse($this->element->registerInArrayValidator());
  214. $this->element->setRegisterInArrayValidator(true);
  215. $this->assertTrue($this->element->registerInArrayValidator());
  216. }
  217. public function testInArrayValidatorShouldBeRegisteredAfterValidation()
  218. {
  219. $options = array(
  220. 'foo' => 'Foo Value',
  221. 'bar' => 'Bar Value',
  222. 'baz' => 'Baz Value',
  223. );
  224. $this->element->setMultiOptions($options);
  225. $this->assertFalse($this->element->getValidator('InArray'));
  226. $this->element->isValid('test');
  227. $validator = $this->element->getValidator('InArray');
  228. $this->assertTrue($validator instanceof Zend_Validate_InArray);
  229. }
  230. public function testShouldNotValidateIfValueIsNotInArray()
  231. {
  232. $options = array(
  233. 'foo' => 'Foo Value',
  234. 'bar' => 'Bar Value',
  235. 'baz' => 'Baz Value',
  236. );
  237. $this->element->setMultiOptions($options);
  238. $this->assertFalse($this->element->getValidator('InArray'));
  239. $this->assertFalse($this->element->isValid('test'));
  240. }
  241. /**#@-*/
  242. /**
  243. * No assertion; just making sure no error occurs
  244. *
  245. * @group ZF-4915
  246. */
  247. public function testRetrievingErrorMessagesShouldNotResultInError()
  248. {
  249. $this->element->addMultiOptions(array(
  250. 'foo' => 'Foo',
  251. 'bar' => 'Bar',
  252. 'baz' => 'Baz',
  253. ))
  254. ->addErrorMessage('%value% is invalid');
  255. $this->element->isValid(array('foo', 'bogus'));
  256. $html = $this->element->render($this->getView());
  257. }
  258. /**
  259. * @group ZF-11402
  260. */
  261. public function testValidateShouldNotAcceptEmptyArray()
  262. {
  263. $this->element->addMultiOptions(array(
  264. 'foo' => 'Foo',
  265. 'bar' => 'Bar',
  266. 'baz' => 'Baz',
  267. ));
  268. $this->element->setRegisterInArrayValidator(true);
  269. $this->assertTrue($this->element->isValid(array('foo')));
  270. $this->assertTrue($this->element->isValid(array('foo','baz')));
  271. $this->element->setAllowEmpty(true);
  272. $this->assertTrue($this->element->isValid(array()));
  273. // Empty value + AllowEmpty=true = no error messages
  274. $messages = $this->element->getMessages();
  275. $this->assertEquals(0, count($messages), 'Received unexpected error message(s)');
  276. $this->element->setAllowEmpty(false);
  277. $this->assertFalse($this->element->isValid(array()));
  278. // Empty value + AllowEmpty=false = notInArray error message
  279. $messages = $this->element->getMessages();
  280. $this->assertTrue(is_array($messages), 'Expected error message');
  281. $this->assertArrayHasKey('notInArray', $messages, 'Expected \'notInArray\' error message');
  282. $this->element->setRequired(true)->setAllowEmpty(false);
  283. $this->assertFalse($this->element->isValid(array()));
  284. // Empty value + Required=true + AllowEmpty=false = isEmpty error message
  285. $messages = $this->element->getMessages();
  286. $this->assertTrue(is_array($messages), 'Expected error message');
  287. $this->assertArrayHasKey('isEmpty', $messages, 'Expected \'isEmpty\' error message');
  288. }
  289. /**
  290. * @group ZF-12059
  291. */
  292. public function testDisabledForAttribute()
  293. {
  294. $this->element->setLabel('Foo');
  295. $expected = '<dt id="foo-label"><label class="optional">Foo</label></dt>'
  296. . PHP_EOL
  297. . '<dd id="foo-element">'
  298. . PHP_EOL
  299. . '</dd>';
  300. $this->assertSame($expected, $this->element->render($this->getView()));
  301. }
  302. /**
  303. * @group ZF-12059
  304. */
  305. public function testDisabledForAttributeWithoutLabelDecorator()
  306. {
  307. $this->element->setLabel('Foo')->removeDecorator('label');
  308. $expected = '<dd id="foo-element">'
  309. . PHP_EOL
  310. . '</dd>';
  311. $this->assertSame($expected, $this->element->render($this->getView()));
  312. }
  313. }
  314. // Call Zend_Form_Element_MultiCheckboxTest::main() if this source file is executed directly.
  315. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_MultiCheckboxTest::main") {
  316. Zend_Form_Element_MultiCheckboxTest::main();
  317. }