SelectTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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-2015 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 'Zend/Form/Element/Select.php';
  27. /**
  28. * Test class for Zend_Form_Element_Select
  29. *
  30. * @category Zend
  31. * @package Zend_Form
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Form
  36. */
  37. class Zend_Form_Element_SelectTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Runs the test methods of this class.
  41. *
  42. * @return void
  43. */
  44. public static function main()
  45. {
  46. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_SelectTest");
  47. $result = PHPUnit_TextUI_TestRunner::run($suite);
  48. }
  49. /**
  50. * Sets up the fixture, for example, open a network connection.
  51. * This method is called before a test is executed.
  52. *
  53. * @return void
  54. */
  55. public function setUp()
  56. {
  57. $this->element = new Zend_Form_Element_Select('foo');
  58. }
  59. /**
  60. * Tears down the fixture, for example, close a network connection.
  61. * This method is called after a test is executed.
  62. *
  63. * @return void
  64. */
  65. public function tearDown()
  66. {
  67. }
  68. public function getView()
  69. {
  70. require_once 'Zend/View.php';
  71. $view = new Zend_View(array(
  72. 'encoding' => 'UTF-8',
  73. ));
  74. $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper');
  75. return $view;
  76. }
  77. public function testSelectElementSubclassesXhtmlElement()
  78. {
  79. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  80. }
  81. public function testSelectElementInstanceOfBaseElement()
  82. {
  83. $this->assertTrue($this->element instanceof Zend_Form_Element);
  84. }
  85. public function testSelectElementIsNotAnArrayByDefault()
  86. {
  87. $this->assertFalse($this->element->isArray());
  88. }
  89. public function testSelectElementUsesSelectHelperInViewHelperDecoratorByDefault()
  90. {
  91. $this->_checkZf2794();
  92. $decorator = $this->element->getDecorator('viewHelper');
  93. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  94. $decorator->setElement($this->element);
  95. $helper = $decorator->getHelper();
  96. $this->assertEquals('formSelect', $helper);
  97. }
  98. public function testCanDisableIndividualSelectOptions()
  99. {
  100. $this->element->setMultiOptions(array(
  101. 'foo' => 'foo',
  102. 'bar' => array(
  103. 'baz' => 'Baz',
  104. 'bat' => 'Bat'
  105. ),
  106. 'test' => 'Test',
  107. ))
  108. ->setAttrib('disable', array('baz', 'test'));
  109. $html = $this->element->render($this->getView());
  110. $this->assertNotRegexp('/<select[^>]*?(disabled="disabled")/', $html, $html);
  111. foreach (array('baz', 'test') as $test) {
  112. if (!preg_match('/(<option[^>]*?(value="' . $test . '")[^>]*>)/', $html, $m)) {
  113. $this->fail('Unable to find matching disabled option for ' . $test);
  114. }
  115. $this->assertRegexp('/<option[^>]*?(disabled="disabled")/', $m[1]);
  116. }
  117. foreach (array('foo', 'bat') as $test) {
  118. if (!preg_match('/(<option[^>]*?(value="' . $test . '")[^>]*>)/', $html, $m)) {
  119. $this->fail('Unable to find matching option for ' . $test);
  120. }
  121. $this->assertNotRegexp('/<option[^>]*?(disabled="disabled")/', $m[1], var_export($m, 1));
  122. }
  123. }
  124. /**
  125. * No explicit assertions; just checking for error conditions
  126. *
  127. * @group ZF-2847
  128. */
  129. public function testTranslationShouldNotRaiseWarningsWithNestedGroups()
  130. {
  131. require_once 'Zend/Translate.php';
  132. require_once 'Zend/View.php';
  133. $translate = new Zend_Translate('array', array('Select Test', 'Select Test Translated'), 'en');
  134. $this->element
  135. ->setLabel('Select Test')
  136. ->setMultiOptions(array(
  137. 'Group 1' => array(
  138. '1-1' => 'Hi 1-1',
  139. '1-2' => 'Hi 1-2',
  140. ),
  141. 'Group 2' => array(
  142. '2-1' => 'Hi 2-1',
  143. '2-2' => 'Hi 2-2',
  144. ),
  145. ))
  146. ->setTranslator($translate)
  147. ->setView(new Zend_View());
  148. $html = $this->element->render();
  149. }
  150. /**
  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. * @group 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. * @group ZF-8342
  224. */
  225. public function testUsingPoundSymbolInOptionLabelShouldRenderCorrectly()
  226. {
  227. $this->element->addMultiOption('1', '£' . number_format(1));
  228. $html = $this->element->render($this->getView());
  229. $this->assertContains('>£', $html);
  230. }
  231. /**
  232. * @group ZF-8452
  233. */
  234. public function testRenderingAsArray()
  235. {
  236. $this->element->addMultiOption('bar', 'Bar')
  237. ->setIsArray(true)
  238. ->setDecorators(array('ViewHelper'));
  239. $actual = $this->element->render($this->getView());
  240. $expected = PHP_EOL
  241. . '<select name="foo[]" id="foo">'
  242. . "\n"
  243. . ' <option value="bar">Bar</option>'
  244. . "\n"
  245. . '</select>';
  246. $this->assertSame($expected, $actual);
  247. }
  248. /**
  249. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  250. *
  251. * @link http://framework.zend.com/issues/browse/ZF-2794
  252. * @return void
  253. */
  254. protected function _checkZf2794()
  255. {
  256. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  257. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  258. }
  259. }
  260. }
  261. // Call Zend_Form_Element_SelectTest::main() if this source file is executed directly.
  262. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_SelectTest::main") {
  263. Zend_Form_Element_SelectTest::main();
  264. }