MultiCheckboxTest.php 9.5 KB

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