FormRadioTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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_FormRadioTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_FormRadioTest::main");
  25. }
  26. require_once 'Zend/View/Helper/FormRadio.php';
  27. require_once 'Zend/View.php';
  28. /**
  29. * Zend_View_Helper_FormRadioTest
  30. *
  31. * Tests formRadio helper
  32. *
  33. * @category Zend
  34. * @package Zend_View
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_View
  39. * @group Zend_View_Helper
  40. */
  41. class Zend_View_Helper_FormRadioTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * Runs the test methods of this class.
  45. *
  46. * @access public
  47. * @static
  48. */
  49. public static function main()
  50. {
  51. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_FormRadioTest");
  52. $result = PHPUnit_TextUI_TestRunner::run($suite);
  53. }
  54. public function setUp()
  55. {
  56. $this->view = new Zend_View();
  57. $this->helper = new Zend_View_Helper_FormRadio();
  58. $this->helper->setView($this->view);
  59. }
  60. public function testRendersRadioLabelsWhenRenderingMultipleOptions()
  61. {
  62. $options = array(
  63. 'foo' => 'Foo',
  64. 'bar' => 'Bar',
  65. 'baz' => 'Baz'
  66. );
  67. $html = $this->helper->formRadio(array(
  68. 'name' => 'foo',
  69. 'value' => 'bar',
  70. 'options' => $options,
  71. ));
  72. foreach ($options as $key => $value) {
  73. $this->assertRegexp('#<label.*?>.*?' . $value . '.*?</label>#', $html, $html);
  74. $this->assertRegexp('#<label.*?>.*?<input.*?</label>#', $html, $html);
  75. }
  76. }
  77. public function testCanSpecifyRadioLabelPlacement()
  78. {
  79. $options = array(
  80. 'foo' => 'Foo',
  81. 'bar' => 'Bar',
  82. 'baz' => 'Baz'
  83. );
  84. $html = $this->helper->formRadio(array(
  85. 'name' => 'foo',
  86. 'value' => 'bar',
  87. 'options' => $options,
  88. 'attribs' => array('labelPlacement' => 'append')
  89. ));
  90. foreach ($options as $key => $value) {
  91. $this->assertRegexp('#<label.*?>.*?<input .*?' . $value . '</label>#', $html, $html);
  92. }
  93. $html = $this->helper->formRadio(array(
  94. 'name' => 'foo',
  95. 'value' => 'bar',
  96. 'options' => $options,
  97. 'attribs' => array('labelPlacement' => 'prepend')
  98. ));
  99. foreach ($options as $key => $value) {
  100. $this->assertRegexp('#<label.*?>' . $value . '<input .*?</label>#', $html, $html);
  101. }
  102. }
  103. /**
  104. * @group ZF-3206
  105. */
  106. public function testSpecifyingLabelPlacementShouldNotOverwriteValue()
  107. {
  108. $options = array(
  109. 'bar' => 'Bar',
  110. );
  111. $html = $this->helper->formRadio(array(
  112. 'name' => 'foo',
  113. 'value' => 'bar',
  114. 'options' => $options,
  115. 'attribs' => array(
  116. 'labelPlacement' => 'append',
  117. )
  118. ));
  119. $this->assertRegexp('#<input[^>]*(checked="checked")#', $html, $html);
  120. }
  121. public function testCanSpecifyRadioLabelAttribs()
  122. {
  123. $options = array(
  124. 'foo' => 'Foo',
  125. 'bar' => 'Bar',
  126. 'baz' => 'Baz'
  127. );
  128. $html = $this->helper->formRadio(array(
  129. 'name' => 'foo',
  130. 'value' => 'bar',
  131. 'options' => $options,
  132. 'attribs' => array('labelClass' => 'testclass', 'label_id' => 'testid')
  133. ));
  134. foreach ($options as $key => $value) {
  135. $this->assertRegexp('#<label[^>]*?class="testclass"[^>]*>.*?' . $value . '#', $html, $html);
  136. $this->assertRegexp('#<label[^>]*?id="testid"[^>]*>.*?' . $value . '#', $html, $html);
  137. }
  138. }
  139. public function testCanSpecifyRadioSeparator()
  140. {
  141. $options = array(
  142. 'foo' => 'Foo',
  143. 'bar' => 'Bar',
  144. 'baz' => 'Baz'
  145. );
  146. $html = $this->helper->formRadio(array(
  147. 'name' => 'foo',
  148. 'value' => 'bar',
  149. 'options' => $options,
  150. 'listsep' => '--FunkySep--',
  151. ));
  152. $this->assertContains('--FunkySep--', $html);
  153. $count = substr_count($html, '--FunkySep--');
  154. $this->assertEquals(2, $count);
  155. }
  156. /**
  157. * ZF-2513
  158. */
  159. public function testCanDisableAllRadios()
  160. {
  161. $options = array(
  162. 'foo' => 'Foo',
  163. 'bar' => 'Bar',
  164. 'baz' => 'Baz'
  165. );
  166. $html = $this->helper->formRadio(array(
  167. 'name' => 'foo',
  168. 'value' => 'bar',
  169. 'options' => $options,
  170. 'attribs' => array('disable' => true)
  171. ));
  172. $this->assertRegexp('/<input[^>]*?(disabled="disabled")/', $html, $html);
  173. $count = substr_count($html, 'disabled="disabled"');
  174. $this->assertEquals(3, $count);
  175. }
  176. /**
  177. * ZF-2513
  178. */
  179. public function testCanDisableIndividualRadios()
  180. {
  181. $options = array(
  182. 'foo' => 'Foo',
  183. 'bar' => 'Bar',
  184. 'baz' => 'Baz'
  185. );
  186. $html = $this->helper->formRadio(array(
  187. 'name' => 'foo',
  188. 'value' => 'bar',
  189. 'options' => $options,
  190. 'attribs' => array('disable' => array('bar'))
  191. ));
  192. $this->assertRegexp('/<input[^>]*?(value="bar")[^>]*(disabled="disabled")/', $html, $html);
  193. $count = substr_count($html, 'disabled="disabled"');
  194. $this->assertEquals(1, $count);
  195. }
  196. /**
  197. * ZF-2513
  198. */
  199. public function testCanDisableMultipleRadios()
  200. {
  201. $options = array(
  202. 'foo' => 'Foo',
  203. 'bar' => 'Bar',
  204. 'baz' => 'Baz'
  205. );
  206. $html = $this->helper->formRadio(array(
  207. 'name' => 'foo',
  208. 'value' => 'bar',
  209. 'options' => $options,
  210. 'attribs' => array('disable' => array('foo', 'baz'))
  211. ));
  212. foreach (array('foo', 'baz') as $test) {
  213. $this->assertRegexp('/<input[^>]*?(value="' . $test . '")[^>]*?(disabled="disabled")/', $html, $html);
  214. }
  215. $this->assertNotRegexp('/<input[^>]*?(value="bar")[^>]*?(disabled="disabled")/', $html, $html);
  216. $count = substr_count($html, 'disabled="disabled"');
  217. $this->assertEquals(2, $count);
  218. }
  219. public function testLabelsAreEscapedByDefault()
  220. {
  221. $options = array(
  222. 'bar' => '<b>Bar</b>',
  223. );
  224. $html = $this->helper->formRadio(array(
  225. 'name' => 'foo',
  226. 'options' => $options,
  227. ));
  228. $this->assertNotContains($options['bar'], $html);
  229. $this->assertContains('&lt;b&gt;Bar&lt;/b&gt;', $html);
  230. }
  231. public function testXhtmlLabelsAreAllowed()
  232. {
  233. $options = array(
  234. 'bar' => '<b>Bar</b>',
  235. );
  236. $html = $this->helper->formRadio(array(
  237. 'name' => 'foo',
  238. 'options' => $options,
  239. 'attribs' => array('escape' => false)
  240. ));
  241. $this->assertContains($options['bar'], $html);
  242. }
  243. /**
  244. * ZF-1666
  245. */
  246. public function testDoesNotRenderHiddenElements()
  247. {
  248. $options = array(
  249. 'foo' => 'Foo',
  250. 'bar' => 'Bar',
  251. 'baz' => 'Baz'
  252. );
  253. $html = $this->helper->formRadio(array(
  254. 'name' => 'foo',
  255. 'options' => $options,
  256. ));
  257. $this->assertNotRegexp('/<input[^>]*?(type="hidden")/', $html);
  258. }
  259. public function testSpecifyingAValueThatMatchesAnOptionChecksIt()
  260. {
  261. $options = array(
  262. 'foo' => 'Foo',
  263. 'bar' => 'Bar',
  264. 'baz' => 'Baz'
  265. );
  266. $html = $this->helper->formRadio(array(
  267. 'name' => 'foo',
  268. 'value' => 'bar',
  269. 'options' => $options,
  270. ));
  271. if (!preg_match('/(<input[^>]*?(value="bar")[^>]*>)/', $html, $matches)) {
  272. $this->fail('Radio for a given option was not found?');
  273. }
  274. $this->assertContains('checked="checked"', $matches[1], var_export($matches, 1));
  275. }
  276. public function testOptionsWithMatchesInAnArrayOfValuesAreChecked()
  277. {
  278. $options = array(
  279. 'foo' => 'Foo',
  280. 'bar' => 'Bar',
  281. 'baz' => 'Baz'
  282. );
  283. $html = $this->helper->formRadio(array(
  284. 'name' => 'foo',
  285. 'value' => array('foo', 'baz'),
  286. 'options' => $options,
  287. ));
  288. foreach (array('foo', 'baz') as $value) {
  289. if (!preg_match('/(<input[^>]*?(value="' . $value . '")[^>]*>)/', $html, $matches)) {
  290. $this->fail('Radio for a given option was not found?');
  291. }
  292. $this->assertContains('checked="checked"', $matches[1], var_export($matches, 1));
  293. }
  294. }
  295. public function testEachRadioShouldHaveIdCreatedByAppendingFilteredValue()
  296. {
  297. $options = array(
  298. 'foo bar' => 'Foo',
  299. 'bar baz' => 'Bar',
  300. 'baz' => 'Baz'
  301. );
  302. $html = $this->helper->formRadio(array(
  303. 'name' => 'foo[]',
  304. 'value' => 'bar',
  305. 'options' => $options,
  306. ));
  307. require_once 'Zend/Filter/Alnum.php';
  308. $filter = new Zend_Filter_Alnum();
  309. foreach ($options as $key => $value) {
  310. $id = 'foo-' . $filter->filter($key);
  311. $this->assertRegexp('/<input([^>]*)(id="' . $id . '")/', $html);
  312. }
  313. }
  314. public function testEachRadioShouldUseAttributeIdWhenSpecified()
  315. {
  316. $options = array(
  317. 'foo bar' => 'Foo',
  318. 'bar baz' => 'Bar',
  319. 'baz' => 'Baz'
  320. );
  321. $html = $this->helper->formRadio(array(
  322. 'name' => 'foo[bar]',
  323. 'value' => 'bar',
  324. 'attribs' => array('id' => 'foo-bar'),
  325. 'options' => $options,
  326. ));
  327. require_once 'Zend/Filter/Alnum.php';
  328. $filter = new Zend_Filter_Alnum();
  329. foreach ($options as $key => $value) {
  330. $id = 'foo-bar-' . $filter->filter($key);
  331. $this->assertRegexp('/<input([^>]*)(id="' . $id . '")/', $html);
  332. }
  333. }
  334. /**
  335. * @group ZF-5681
  336. */
  337. public function testRadioLabelDoesNotContainHardCodedStyle()
  338. {
  339. $options = array(
  340. 'foo' => 'Foo',
  341. 'bar' => 'Bar',
  342. 'baz' => 'Baz'
  343. );
  344. $html = $this->helper->formRadio(array(
  345. 'name' => 'foo',
  346. 'value' => 'bar',
  347. 'options' => $options,
  348. ));
  349. $this->assertNotContains('style="white-space: nowrap;"', $html);
  350. }
  351. public function testRadioLabelContainsForAttributeTag()
  352. {
  353. $options = array(
  354. 'foo bar' => 'Foo',
  355. 'bar baz' => 'Bar',
  356. 'baz' => 'Baz'
  357. );
  358. $html = $this->helper->formRadio(array(
  359. 'name' => 'foo[bar]',
  360. 'value' => 'bar',
  361. 'options' => $options,
  362. ));
  363. require_once 'Zend/Filter/Alnum.php';
  364. $filter = new Zend_Filter_Alnum();
  365. foreach ($options as $key => $value) {
  366. $id = 'foo-bar-' . $filter->filter($key);
  367. $this->assertRegexp('/<label([^>]*)(for="' . $id . '")/', $html);
  368. }
  369. }
  370. /**
  371. * @group ZF-4191
  372. */
  373. public function testDashesShouldNotBeFilteredFromId()
  374. {
  375. $name = "Foo";
  376. $options = array(
  377. -1 => 'Test -1',
  378. 0 => 'Test 0',
  379. 1 => 'Test 1'
  380. );
  381. $formRadio = new Zend_View_Helper_FormRadio();
  382. $formRadio->setView(new Zend_View());
  383. $html = $formRadio->formRadio($name, -1, null, $options);
  384. foreach ( $options as $key=>$value ) {
  385. $fid = "{$name}-{$key}";
  386. $this->assertRegExp('/<label([^>]*)(for="'.$fid.'")/', $html);
  387. $this->assertRegExp('/<input([^>]*)(id="'.$fid.'")/', $html);
  388. }
  389. // Assert that radio for value -1 is the selected one
  390. $this->assertRegExp('/<input([^>]*)(id="'.$name.'--1")([^>]*)(checked="checked")/', $html);
  391. }
  392. }
  393. // Call Zend_View_Helper_FormRadioTest::main() if this source file is executed directly.
  394. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormRadioTest::main") {
  395. Zend_View_Helper_FormRadioTest::main();
  396. }