MultiCheckboxTest.php 8.0 KB

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