2
0

SelectTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. // Call Zend_Form_Element_SelectTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_SelectTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once 'Zend/Form/Element/Select.php';
  8. /**
  9. * Test class for Zend_Form_Element_Select
  10. */
  11. class Zend_Form_Element_SelectTest 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_SelectTest");
  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_Select('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 testSelectElementSubclassesXhtmlElement()
  50. {
  51. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  52. }
  53. public function testSelectElementInstanceOfBaseElement()
  54. {
  55. $this->assertTrue($this->element instanceof Zend_Form_Element);
  56. }
  57. public function testSelectElementIsNotAnArrayByDefault()
  58. {
  59. $this->assertFalse($this->element->isArray());
  60. }
  61. public function testSelectElementUsesSelectHelperInViewHelperDecoratorByDefault()
  62. {
  63. $this->_checkZf2794();
  64. $decorator = $this->element->getDecorator('viewHelper');
  65. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  66. $decorator->setElement($this->element);
  67. $helper = $decorator->getHelper();
  68. $this->assertEquals('formSelect', $helper);
  69. }
  70. public function testCanDisableIndividualSelectOptions()
  71. {
  72. $this->element->setMultiOptions(array(
  73. 'foo' => 'foo',
  74. 'bar' => array(
  75. 'baz' => 'Baz',
  76. 'bat' => 'Bat'
  77. ),
  78. 'test' => 'Test',
  79. ))
  80. ->setAttrib('disable', array('baz', 'test'));
  81. $html = $this->element->render($this->getView());
  82. $this->assertNotRegexp('/<select[^>]*?(disabled="disabled")/', $html, $html);
  83. foreach (array('baz', 'test') as $test) {
  84. if (!preg_match('/(<option[^>]*?(value="' . $test . '")[^>]*>)/', $html, $m)) {
  85. $this->fail('Unable to find matching disabled option for ' . $test);
  86. }
  87. $this->assertRegexp('/<option[^>]*?(disabled="disabled")/', $m[1]);
  88. }
  89. foreach (array('foo', 'bat') as $test) {
  90. if (!preg_match('/(<option[^>]*?(value="' . $test . '")[^>]*>)/', $html, $m)) {
  91. $this->fail('Unable to find matching option for ' . $test);
  92. }
  93. $this->assertNotRegexp('/<option[^>]*?(disabled="disabled")/', $m[1], var_export($m, 1));
  94. }
  95. }
  96. /**
  97. * No explicit assertions; just checking for error conditions
  98. *
  99. * @see ZF-2847
  100. */
  101. public function testTranslationShouldNotRaiseWarningsWithNestedGroups()
  102. {
  103. require_once 'Zend/Translate.php';
  104. require_once 'Zend/View.php';
  105. $translate = new Zend_Translate('array', array('Select Test', 'Select Test Translated'), 'en');
  106. $this->element
  107. ->setLabel('Select Test')
  108. ->setMultiOptions(array(
  109. 'Group 1' => array(
  110. '1-1' => 'Hi 1-1',
  111. '1-2' => 'Hi 1-2',
  112. ),
  113. 'Group 2' => array(
  114. '2-1' => 'Hi 2-1',
  115. '2-2' => 'Hi 2-2',
  116. ),
  117. ))
  118. ->setTranslator($translate)
  119. ->setView(new Zend_View());
  120. $html = $this->element->render();
  121. }
  122. /**
  123. * @see ZF-3953
  124. * @group ZF-3953
  125. */
  126. public function testUsingZeroAsValueShouldSelectAppropriateOption()
  127. {
  128. $this->element->setMultiOptions(array(
  129. array('key' => '1', 'value' => 'Yes'),
  130. array('key' => '0', 'value' => 'No'),
  131. array('key' => 'somewhat', 'value' => 'Somewhat'),
  132. ));
  133. $this->element->setValue(0);
  134. $html = $this->element->render($this->getView());
  135. if (!preg_match('#(<option[^>]*(?:value="somewhat")[^>]*>)#s', $html, $matches)) {
  136. $this->fail('Could not find option: ' . $html);
  137. }
  138. $this->assertNotContains('selected', $matches[1]);
  139. }
  140. /**
  141. * @group ZF-4390
  142. */
  143. public function testEmptyOptionsShouldNotBeTranslated()
  144. {
  145. $translate = new Zend_Translate('array', array('unused', 'foo' => 'bar'), 'en');
  146. $this->element->setTranslator($translate);
  147. $this->element->setMultiOptions(array(
  148. array('key' => '', 'value' => ''),
  149. array('key' => 'foo', 'value' => 'foo'),
  150. ));
  151. $this->element->setView($this->getView());
  152. $html = $this->element->render();
  153. $this->assertNotContains('unused', $html, $html);
  154. $this->assertContains('bar', $html, $html);
  155. }
  156. /**
  157. * Test isValid() on select elements without optgroups. This
  158. * ensures fixing ZF-3985 doesn't break existing functionality.
  159. *
  160. * @see ZF-3985
  161. */
  162. public function testIsValidWithPlainOptions()
  163. {
  164. // test both syntaxes for setting plain options
  165. $this->element->setMultiOptions(array(
  166. array('key' => '1', 'value' => 'Web Developer'),
  167. '2' => 'Software Engineer',
  168. ));
  169. $this->assertTrue($this->element->isValid('1'));
  170. $this->assertTrue($this->element->isValid('2'));
  171. $this->assertFalse($this->element->isValid('3'));
  172. $this->assertFalse($this->element->isValid('Web Developer'));
  173. }
  174. /**
  175. * @group ZF-3985
  176. */
  177. public function testIsValidWithOptionGroups()
  178. {
  179. // test optgroup and both syntaxes for setting plain options
  180. $this->element->setMultiOptions(array(
  181. 'Technology' => array(
  182. '1' => 'Web Developer',
  183. '2' => 'Software Engineer',
  184. ),
  185. array('key' => '3', 'value' => 'Trainee'),
  186. '4' => 'Intern',
  187. ));
  188. $this->assertTrue($this->element->isValid('1'));
  189. $this->assertTrue($this->element->isValid('3'));
  190. $this->assertTrue($this->element->isValid('4'));
  191. $this->assertFalse($this->element->isValid('5'));
  192. $this->assertFalse($this->element->isValid('Technology'));
  193. $this->assertFalse($this->element->isValid('Web Developer'));
  194. }
  195. /**
  196. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  197. *
  198. * @link http://framework.zend.com/issues/browse/ZF-2794
  199. * @return void
  200. */
  201. protected function _checkZf2794()
  202. {
  203. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  204. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  205. }
  206. }
  207. }
  208. // Call Zend_Form_Element_SelectTest::main() if this source file is executed directly.
  209. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_SelectTest::main") {
  210. Zend_Form_Element_SelectTest::main();
  211. }