2
0

FormSelectTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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_View
  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_View_Helper_FormSelectTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_FormSelectTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  27. require_once 'Zend/View/Helper/FormSelect.php';
  28. require_once 'Zend/View.php';
  29. /**
  30. * Test class for Zend_View_Helper_FormSelect.
  31. *
  32. * @category Zend
  33. * @package Zend_View
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_View
  38. * @group Zend_View_Helper
  39. */
  40. class Zend_View_Helper_FormSelectTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Runs the test methods of this class.
  44. *
  45. * @return void
  46. */
  47. public static function main()
  48. {
  49. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_FormSelectTest");
  50. $result = PHPUnit_TextUI_TestRunner::run($suite);
  51. }
  52. /**
  53. * Sets up the fixture, for example, open a network connection.
  54. * This method is called before a test is executed.
  55. *
  56. * @return void
  57. */
  58. public function setUp()
  59. {
  60. $this->view = new Zend_View();
  61. $this->helper = new Zend_View_Helper_FormSelect();
  62. $this->helper->setView($this->view);
  63. }
  64. /**
  65. * Tears down the fixture, for example, close a network connection.
  66. * This method is called after a test is executed.
  67. *
  68. * @return void
  69. */
  70. public function tearDown()
  71. {
  72. unset($this->helper, $this->view);
  73. }
  74. public function testFormSelectWithNameOnlyCreatesEmptySelect()
  75. {
  76. $html = $this->helper->formSelect('foo');
  77. $this->assertRegExp('#<select[^>]+name="foo"#', $html);
  78. $this->assertContains('</select>', $html);
  79. $this->assertNotContains('<option', $html);
  80. }
  81. public function testFormSelectWithOptionsCreatesPopulatedSelect()
  82. {
  83. $html = $this->helper->formSelect('foo', null, null, array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  84. $this->assertRegExp('#<select[^>]+name="foo"#', $html);
  85. $this->assertContains('</select>', $html);
  86. $this->assertRegExp('#<option[^>]+value="foo".*?>Foobar</option>#', $html);
  87. $this->assertRegExp('#<option[^>]+value="baz".*?>Bazbat</option>#', $html);
  88. $this->assertEquals(2, substr_count($html, '<option'));
  89. }
  90. public function testFormSelectWithOptionsAndValueSelectsAppropriateValue()
  91. {
  92. $html = $this->helper->formSelect('foo', 'baz', null, array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  93. $this->assertRegExp('#<option[^>]+value="baz"[^>]*selected.*?>Bazbat</option>#', $html);
  94. }
  95. public function testFormSelectWithMultipleAttributeCreatesMultiSelect()
  96. {
  97. $html = $this->helper->formSelect('foo', null, array('multiple' => true), array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  98. $this->assertRegExp('#<select[^>]+name="foo\[\]"#', $html);
  99. $this->assertRegExp('#<select[^>]+multiple="multiple"#', $html);
  100. }
  101. public function testFormSelectWithMultipleAttributeAndValuesCreatesMultiSelectWithSelectedValues()
  102. {
  103. $html = $this->helper->formSelect('foo', array('foo', 'baz'), array('multiple' => true), array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  104. $this->assertRegExp('#<option[^>]+value="foo"[^>]*selected.*?>Foobar</option>#', $html);
  105. $this->assertRegExp('#<option[^>]+value="baz"[^>]*selected.*?>Bazbat</option>#', $html);
  106. }
  107. /**
  108. * ZF-1930
  109. * @return void
  110. */
  111. public function testFormSelectWithZeroValueSelectsValue()
  112. {
  113. $html = $this->helper->formSelect('foo', 0, null, array('foo' => 'Foobar', 0 => 'Bazbat'));
  114. $this->assertRegExp('#<option[^>]+value="0"[^>]*selected.*?>Bazbat</option>#', $html);
  115. }
  116. /**
  117. * ZF-2513
  118. */
  119. public function testCanDisableEntireSelect()
  120. {
  121. $html = $this->helper->formSelect(array(
  122. 'name' => 'baz',
  123. 'options' => array(
  124. 'foo' => 'Foo',
  125. 'bar' => 'Bar'
  126. ),
  127. 'attribs' => array(
  128. 'disable' => true
  129. ),
  130. ));
  131. $this->assertRegexp('/<select[^>]*?disabled/', $html, $html);
  132. $this->assertNotRegexp('/<option[^>]*?disabled="disabled"/', $html, $html);
  133. }
  134. /**
  135. * ZF-2513
  136. */
  137. public function testCanDisableIndividualSelectOptionsOnly()
  138. {
  139. $html = $this->helper->formSelect(array(
  140. 'name' => 'baz',
  141. 'options' => array(
  142. 'foo' => 'Foo',
  143. 'bar' => 'Bar'
  144. ),
  145. 'attribs' => array(
  146. 'disable' => array('bar')
  147. ),
  148. ));
  149. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  150. $this->assertRegexp('/<option value="bar"[^>]*?disabled="disabled"/', $html, $html);
  151. $html = $this->helper->formSelect(
  152. 'baz',
  153. 'foo',
  154. array(
  155. 'disable' => array('bar')
  156. ),
  157. array(
  158. 'foo' => 'Foo',
  159. 'bar' => 'Bar'
  160. )
  161. );
  162. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  163. $this->assertRegexp('/<option value="bar"[^>]*?disabled="disabled"/', $html, $html);
  164. }
  165. /**
  166. * ZF-2513
  167. */
  168. public function testCanDisableMultipleSelectOptions()
  169. {
  170. $html = $this->helper->formSelect(array(
  171. 'name' => 'baz',
  172. 'options' => array(
  173. 'foo' => 'Foo',
  174. 'bar' => 'Bar',
  175. 'baz' => 'Baz,'
  176. ),
  177. 'attribs' => array(
  178. 'disable' => array('foo', 'baz')
  179. ),
  180. ));
  181. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  182. $this->assertRegexp('/<option value="foo"[^>]*?disabled="disabled"/', $html, $html);
  183. $this->assertRegexp('/<option value="baz"[^>]*?disabled="disabled"/', $html, $html);
  184. }
  185. /**
  186. * ZF-2513
  187. */
  188. public function testCanDisableOptGroups()
  189. {
  190. $html = $this->helper->formSelect(array(
  191. 'name' => 'baz',
  192. 'options' => array(
  193. 'foo' => 'Foo',
  194. 'bar' => array(
  195. '1' => 'one',
  196. '2' => 'two'
  197. ),
  198. 'baz' => 'Baz,'
  199. ),
  200. 'attribs' => array(
  201. 'disable' => array('bar')
  202. ),
  203. ));
  204. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  205. $this->assertRegexp('/<optgroup[^>]*?disabled="disabled"[^>]*?"bar"[^>]*?/', $html, $html);
  206. $this->assertNotRegexp('/<option value="1"[^>]*?disabled="disabled"/', $html, $html);
  207. $this->assertNotRegexp('/<option value="2"[^>]*?disabled="disabled"/', $html, $html);
  208. }
  209. /**
  210. * ZF-2513
  211. */
  212. public function testCanDisableOptGroupOptions()
  213. {
  214. $html = $this->helper->formSelect(array(
  215. 'name' => 'baz',
  216. 'options' => array(
  217. 'foo' => 'Foo',
  218. 'bar' => array(
  219. '1' => 'one',
  220. '2' => 'two'
  221. ),
  222. 'baz' => 'Baz,'
  223. ),
  224. 'attribs' => array(
  225. 'disable' => array('2')
  226. ),
  227. ));
  228. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  229. $this->assertNotRegexp('/<optgroup[^>]*?disabled="disabled"[^>]*?"bar"[^>]*?/', $html, $html);
  230. $this->assertNotRegexp('/<option value="1"[^>]*?disabled="disabled"/', $html, $html);
  231. $this->assertRegexp('/<option value="2"[^>]*?disabled="disabled"/', $html, $html);
  232. }
  233. public function testCanSpecifySelectMultipleThroughAttribute()
  234. {
  235. $html = $this->helper->formSelect(array(
  236. 'name' => 'baz',
  237. 'options' => array(
  238. 'foo' => 'Foo',
  239. 'bar' => 'Bar',
  240. 'baz' => 'Baz,'
  241. ),
  242. 'attribs' => array(
  243. 'multiple' => true
  244. ),
  245. ));
  246. $this->assertRegexp('/<select[^>]*?(multiple="multiple")/', $html, $html);
  247. }
  248. public function testSpecifyingSelectMultipleThroughAttributeAppendsNameWithBrackets()
  249. {
  250. $html = $this->helper->formSelect(array(
  251. 'name' => 'baz',
  252. 'options' => array(
  253. 'foo' => 'Foo',
  254. 'bar' => 'Bar',
  255. 'baz' => 'Baz,'
  256. ),
  257. 'attribs' => array(
  258. 'multiple' => true
  259. ),
  260. ));
  261. $this->assertRegexp('/<select[^>]*?(name="baz\[\]")/', $html, $html);
  262. }
  263. public function testCanSpecifySelectMultipleThroughName()
  264. {
  265. $html = $this->helper->formSelect(array(
  266. 'name' => 'baz[]',
  267. 'options' => array(
  268. 'foo' => 'Foo',
  269. 'bar' => 'Bar',
  270. 'baz' => 'Baz,'
  271. ),
  272. ));
  273. $this->assertRegexp('/<select[^>]*?(multiple="multiple")/', $html, $html);
  274. }
  275. /**
  276. * ZF-1639
  277. */
  278. public function testNameCanContainBracketsButNotBeMultiple()
  279. {
  280. $html = $this->helper->formSelect(array(
  281. 'name' => 'baz[]',
  282. 'options' => array(
  283. 'foo' => 'Foo',
  284. 'bar' => 'Bar',
  285. 'baz' => 'Baz,'
  286. ),
  287. 'attribs' => array(
  288. 'multiple' => false
  289. ),
  290. ));
  291. $this->assertRegexp('/<select[^>]*?(name="baz\[\]")/', $html, $html);
  292. $this->assertNotRegexp('/<select[^>]*?(multiple="multiple")/', $html, $html);
  293. }
  294. }
  295. // Call Zend_View_Helper_FormSelectTest::main() if this source file is executed directly.
  296. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormSelectTest::main") {
  297. Zend_View_Helper_FormSelectTest::main();
  298. }