SelectTest.php 8.6 KB

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