2
0

SelectTest.php 8.9 KB

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