FormSelectTest.php 12 KB

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