PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ZendFramework/tests/Zend/View/Helper/FormSelectTest.php

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 384 lines | 279 code | 27 blank | 78 comment | 3 complexity | 93075d23484fb1bcd2702d8008091dbd MD5 | raw file
  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: FormSelectTest.php 25187 2013-01-08 08:21:00Z frosch $
  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. /**
  74. * @group ZF-10661
  75. */
  76. public function testRenderingWithOptions()
  77. {
  78. $html = $this->helper->formSelect(
  79. 'foo',
  80. null,
  81. null,
  82. array(
  83. 'bar' => 'Bar',
  84. 'baz' => 'Baz',
  85. )
  86. );
  87. $expected = '<select name="foo" id="foo">'
  88. . PHP_EOL
  89. . ' <option value="bar">Bar</option>'
  90. . PHP_EOL
  91. . ' <option value="baz">Baz</option>'
  92. . PHP_EOL
  93. . '</select>';
  94. $this->assertSame($expected, $html);
  95. }
  96. public function testFormSelectWithNameOnlyCreatesEmptySelect()
  97. {
  98. $html = $this->helper->formSelect('foo');
  99. $this->assertRegExp('#<select[^>]+name="foo"#', $html);
  100. $this->assertContains('</select>', $html);
  101. $this->assertNotContains('<option', $html);
  102. }
  103. public function testFormSelectWithOptionsCreatesPopulatedSelect()
  104. {
  105. $html = $this->helper->formSelect('foo', null, null, array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  106. $this->assertRegExp('#<select[^>]+name="foo"#', $html);
  107. $this->assertContains('</select>', $html);
  108. $this->assertRegExp('#<option[^>]+value="foo".*?>Foobar</option>#', $html);
  109. $this->assertRegExp('#<option[^>]+value="baz".*?>Bazbat</option>#', $html);
  110. $this->assertEquals(2, substr_count($html, '<option'));
  111. }
  112. public function testFormSelectWithOptionsAndValueSelectsAppropriateValue()
  113. {
  114. $html = $this->helper->formSelect('foo', 'baz', null, array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  115. $this->assertRegExp('#<option[^>]+value="baz"[^>]*selected.*?>Bazbat</option>#', $html);
  116. }
  117. public function testFormSelectWithMultipleAttributeCreatesMultiSelect()
  118. {
  119. $html = $this->helper->formSelect('foo', null, array('multiple' => true), array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  120. $this->assertRegExp('#<select[^>]+name="foo\[\]"#', $html);
  121. $this->assertRegExp('#<select[^>]+multiple="multiple"#', $html);
  122. }
  123. public function testFormSelectWithMultipleAttributeAndValuesCreatesMultiSelectWithSelectedValues()
  124. {
  125. $html = $this->helper->formSelect('foo', array('foo', 'baz'), array('multiple' => true), array('foo' => 'Foobar', 'baz' => 'Bazbat'));
  126. $this->assertRegExp('#<option[^>]+value="foo"[^>]*selected.*?>Foobar</option>#', $html);
  127. $this->assertRegExp('#<option[^>]+value="baz"[^>]*selected.*?>Bazbat</option>#', $html);
  128. }
  129. /**
  130. * ZF-1930
  131. * @return void
  132. */
  133. public function testFormSelectWithZeroValueSelectsValue()
  134. {
  135. $html = $this->helper->formSelect('foo', 0, null, array('foo' => 'Foobar', 0 => 'Bazbat'));
  136. $this->assertRegExp('#<option[^>]+value="0"[^>]*selected.*?>Bazbat</option>#', $html);
  137. }
  138. /**
  139. * ZF-2513
  140. */
  141. public function testCanDisableEntireSelect()
  142. {
  143. $html = $this->helper->formSelect(array(
  144. 'name' => 'baz',
  145. 'options' => array(
  146. 'foo' => 'Foo',
  147. 'bar' => 'Bar'
  148. ),
  149. 'attribs' => array(
  150. 'disable' => true
  151. ),
  152. ));
  153. $this->assertRegexp('/<select[^>]*?disabled/', $html, $html);
  154. $this->assertNotRegexp('/<option[^>]*?disabled="disabled"/', $html, $html);
  155. }
  156. /**
  157. * ZF-2513
  158. */
  159. public function testCanDisableIndividualSelectOptionsOnly()
  160. {
  161. $html = $this->helper->formSelect(array(
  162. 'name' => 'baz',
  163. 'options' => array(
  164. 'foo' => 'Foo',
  165. 'bar' => 'Bar'
  166. ),
  167. 'attribs' => array(
  168. 'disable' => array('bar')
  169. ),
  170. ));
  171. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  172. $this->assertRegexp('/<option value="bar"[^>]*?disabled="disabled"/', $html, $html);
  173. $html = $this->helper->formSelect(
  174. 'baz',
  175. 'foo',
  176. array(
  177. 'disable' => array('bar')
  178. ),
  179. array(
  180. 'foo' => 'Foo',
  181. 'bar' => 'Bar'
  182. )
  183. );
  184. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  185. $this->assertRegexp('/<option value="bar"[^>]*?disabled="disabled"/', $html, $html);
  186. }
  187. /**
  188. * ZF-2513
  189. */
  190. public function testCanDisableMultipleSelectOptions()
  191. {
  192. $html = $this->helper->formSelect(array(
  193. 'name' => 'baz',
  194. 'options' => array(
  195. 'foo' => 'Foo',
  196. 'bar' => 'Bar',
  197. 'baz' => 'Baz,'
  198. ),
  199. 'attribs' => array(
  200. 'disable' => array('foo', 'baz')
  201. ),
  202. ));
  203. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  204. $this->assertRegexp('/<option value="foo"[^>]*?disabled="disabled"/', $html, $html);
  205. $this->assertRegexp('/<option value="baz"[^>]*?disabled="disabled"/', $html, $html);
  206. }
  207. /**
  208. * ZF-2513
  209. */
  210. public function testCanDisableOptGroups()
  211. {
  212. $html = $this->helper->formSelect(array(
  213. 'name' => 'baz',
  214. 'options' => array(
  215. 'foo' => 'Foo',
  216. 'bar' => array(
  217. '1' => 'one',
  218. '2' => 'two'
  219. ),
  220. 'baz' => 'Baz,'
  221. ),
  222. 'attribs' => array(
  223. 'disable' => array('bar')
  224. ),
  225. ));
  226. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  227. $this->assertRegexp('/<optgroup[^>]*?disabled="disabled"[^>]*?"bar"[^>]*?/', $html, $html);
  228. $this->assertNotRegexp('/<option value="1"[^>]*?disabled="disabled"/', $html, $html);
  229. $this->assertNotRegexp('/<option value="2"[^>]*?disabled="disabled"/', $html, $html);
  230. }
  231. /**
  232. * ZF-2513
  233. */
  234. public function testCanDisableOptGroupOptions()
  235. {
  236. $html = $this->helper->formSelect(array(
  237. 'name' => 'baz',
  238. 'options' => array(
  239. 'foo' => 'Foo',
  240. 'bar' => array(
  241. '1' => 'one',
  242. '2' => 'two'
  243. ),
  244. 'baz' => 'Baz,'
  245. ),
  246. 'attribs' => array(
  247. 'disable' => array('2')
  248. ),
  249. ));
  250. $this->assertNotRegexp('/<select[^>]*?disabled/', $html, $html);
  251. $this->assertNotRegexp('/<optgroup[^>]*?disabled="disabled"[^>]*?"bar"[^>]*?/', $html, $html);
  252. $this->assertNotRegexp('/<option value="1"[^>]*?disabled="disabled"/', $html, $html);
  253. $this->assertRegexp('/<option value="2"[^>]*?disabled="disabled"/', $html, $html);
  254. }
  255. public function testCanSpecifySelectMultipleThroughAttribute()
  256. {
  257. $html = $this->helper->formSelect(array(
  258. 'name' => 'baz',
  259. 'options' => array(
  260. 'foo' => 'Foo',
  261. 'bar' => 'Bar',
  262. 'baz' => 'Baz,'
  263. ),
  264. 'attribs' => array(
  265. 'multiple' => true
  266. ),
  267. ));
  268. $this->assertRegexp('/<select[^>]*?(multiple="multiple")/', $html, $html);
  269. }
  270. public function testSpecifyingSelectMultipleThroughAttributeAppendsNameWithBrackets()
  271. {
  272. $html = $this->helper->formSelect(array(
  273. 'name' => 'baz',
  274. 'options' => array(
  275. 'foo' => 'Foo',
  276. 'bar' => 'Bar',
  277. 'baz' => 'Baz,'
  278. ),
  279. 'attribs' => array(
  280. 'multiple' => true
  281. ),
  282. ));
  283. $this->assertRegexp('/<select[^>]*?(name="baz\[\]")/', $html, $html);
  284. }
  285. public function testCanSpecifySelectMultipleThroughName()
  286. {
  287. $html = $this->helper->formSelect(array(
  288. 'name' => 'baz[]',
  289. 'options' => array(
  290. 'foo' => 'Foo',
  291. 'bar' => 'Bar',
  292. 'baz' => 'Baz,'
  293. ),
  294. ));
  295. $this->assertRegexp('/<select[^>]*?(multiple="multiple")/', $html, $html);
  296. }
  297. /**
  298. * ZF-1639
  299. */
  300. public function testNameCanContainBracketsButNotBeMultiple()
  301. {
  302. $html = $this->helper->formSelect(array(
  303. 'name' => 'baz[]',
  304. 'options' => array(
  305. 'foo' => 'Foo',
  306. 'bar' => 'Bar',
  307. 'baz' => 'Baz,'
  308. ),
  309. 'attribs' => array(
  310. 'multiple' => false
  311. ),
  312. ));
  313. $this->assertRegexp('/<select[^>]*?(name="baz\[\]")/', $html, $html);
  314. $this->assertNotRegexp('/<select[^>]*?(multiple="multiple")/', $html, $html);
  315. }
  316. /**
  317. * @group ZF-8252
  318. */
  319. public function testOptGroupHasAnId()
  320. {
  321. $html = $this->helper->formSelect(array(
  322. 'name' => 'baz',
  323. 'options' => array(
  324. 'foo' => 'Foo',
  325. 'bar' => array(
  326. '1' => 'one',
  327. '2' => 'two'
  328. ),
  329. 'baz' => 'Baz,'
  330. )
  331. ));
  332. $this->assertRegexp('/<optgroup[^>]*?id="baz-optgroup-bar"[^>]*?"bar"[^>]*?/', $html, $html);
  333. }
  334. public function testCanApplyOptionClasses()
  335. {
  336. $html = $this->helper->formSelect(array(
  337. 'name' => 'baz[]',
  338. 'options' => array(
  339. 'foo' => 'Foo',
  340. 'bar' => 'Bar',
  341. 'baz' => 'Baz,'
  342. ),
  343. 'attribs' => array(
  344. 'multiple' => false,
  345. 'optionClasses' => array('foo' => 'fooClass',
  346. 'bar' => 'barClass',
  347. 'baz' => 'bazClass')
  348. ),
  349. ));
  350. $this->assertRegexp('/.*<option[^>]*?(value="foo")?(class="fooClass").*/', $html, $html);
  351. $this->assertRegexp('/.*<option[^>]*?(value="bar")?(class="barClass").*/', $html, $html);
  352. }
  353. }
  354. // Call Zend_View_Helper_FormSelectTest::main() if this source file is executed directly.
  355. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormSelectTest::main") {
  356. Zend_View_Helper_FormSelectTest::main();
  357. }