FormRadioTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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-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_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-2015 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->view->doctype('HTML4_LOOSE'); // Set default doctype
  58. $this->helper = new Zend_View_Helper_FormRadio();
  59. $this->helper->setView($this->view);
  60. }
  61. public function testRendersRadioLabelsWhenRenderingMultipleOptions()
  62. {
  63. $options = array(
  64. 'foo' => 'Foo',
  65. 'bar' => 'Bar',
  66. 'baz' => 'Baz'
  67. );
  68. $html = $this->helper->formRadio(array(
  69. 'name' => 'foo',
  70. 'value' => 'bar',
  71. 'options' => $options,
  72. ));
  73. foreach ($options as $key => $value) {
  74. $this->assertRegexp('#<label.*?>.*?' . $value . '.*?</label>#', $html, $html);
  75. $this->assertRegexp('#<label.*?>.*?<input.*?</label>#', $html, $html);
  76. }
  77. }
  78. public function testCanSpecifyRadioLabelPlacement()
  79. {
  80. $options = array(
  81. 'foo' => 'Foo',
  82. 'bar' => 'Bar',
  83. 'baz' => 'Baz'
  84. );
  85. $html = $this->helper->formRadio(array(
  86. 'name' => 'foo',
  87. 'value' => 'bar',
  88. 'options' => $options,
  89. 'attribs' => array('labelPlacement' => 'append')
  90. ));
  91. foreach ($options as $key => $value) {
  92. $this->assertRegexp('#<label.*?>.*?<input .*?' . $value . '</label>#', $html, $html);
  93. }
  94. $html = $this->helper->formRadio(array(
  95. 'name' => 'foo',
  96. 'value' => 'bar',
  97. 'options' => $options,
  98. 'attribs' => array('labelPlacement' => 'prepend')
  99. ));
  100. foreach ($options as $key => $value) {
  101. $this->assertRegexp('#<label.*?>' . $value . '<input .*?</label>#', $html, $html);
  102. }
  103. }
  104. /**
  105. * @group ZF-3206
  106. */
  107. public function testSpecifyingLabelPlacementShouldNotOverwriteValue()
  108. {
  109. $options = array(
  110. 'bar' => 'Bar',
  111. );
  112. $html = $this->helper->formRadio(array(
  113. 'name' => 'foo',
  114. 'value' => 'bar',
  115. 'options' => $options,
  116. 'attribs' => array(
  117. 'labelPlacement' => 'append',
  118. )
  119. ));
  120. $this->assertRegexp('#<input[^>]*(checked="checked")#', $html, $html);
  121. }
  122. public function testCanSpecifyRadioLabelAttribs()
  123. {
  124. $options = array(
  125. 'foo' => 'Foo',
  126. 'bar' => 'Bar',
  127. 'baz' => 'Baz'
  128. );
  129. $html = $this->helper->formRadio(array(
  130. 'name' => 'foo',
  131. 'value' => 'bar',
  132. 'options' => $options,
  133. 'attribs' => array('labelClass' => 'testclass', 'label_id' => 'testid')
  134. ));
  135. foreach ($options as $key => $value) {
  136. $this->assertRegexp('#<label[^>]*?class="testclass"[^>]*>.*?' . $value . '#', $html, $html);
  137. $this->assertRegexp('#<label[^>]*?id="testid"[^>]*>.*?' . $value . '#', $html, $html);
  138. }
  139. }
  140. public function testCanSpecifyRadioSeparator()
  141. {
  142. $options = array(
  143. 'foo' => 'Foo',
  144. 'bar' => 'Bar',
  145. 'baz' => 'Baz'
  146. );
  147. $html = $this->helper->formRadio(array(
  148. 'name' => 'foo',
  149. 'value' => 'bar',
  150. 'options' => $options,
  151. 'listsep' => '--FunkySep--',
  152. ));
  153. $this->assertContains('--FunkySep--', $html);
  154. $count = substr_count($html, '--FunkySep--');
  155. $this->assertEquals(2, $count);
  156. }
  157. /**
  158. * ZF-2513
  159. */
  160. public function testCanDisableAllRadios()
  161. {
  162. $options = array(
  163. 'foo' => 'Foo',
  164. 'bar' => 'Bar',
  165. 'baz' => 'Baz'
  166. );
  167. $html = $this->helper->formRadio(array(
  168. 'name' => 'foo',
  169. 'value' => 'bar',
  170. 'options' => $options,
  171. 'attribs' => array('disable' => true)
  172. ));
  173. $this->assertRegexp('/<input[^>]*?(disabled="disabled")/', $html, $html);
  174. $count = substr_count($html, 'disabled="disabled"');
  175. $this->assertEquals(3, $count);
  176. }
  177. /**
  178. * ZF-2513
  179. */
  180. public function testCanDisableIndividualRadios()
  181. {
  182. $options = array(
  183. 'foo' => 'Foo',
  184. 'bar' => 'Bar',
  185. 'baz' => 'Baz'
  186. );
  187. $html = $this->helper->formRadio(array(
  188. 'name' => 'foo',
  189. 'value' => 'bar',
  190. 'options' => $options,
  191. 'attribs' => array('disable' => array('bar'))
  192. ));
  193. $this->assertRegexp('/<input[^>]*?(value="bar")[^>]*(disabled="disabled")/', $html, $html);
  194. $count = substr_count($html, 'disabled="disabled"');
  195. $this->assertEquals(1, $count);
  196. }
  197. /**
  198. * ZF-2513
  199. */
  200. public function testCanDisableMultipleRadios()
  201. {
  202. $options = array(
  203. 'foo' => 'Foo',
  204. 'bar' => 'Bar',
  205. 'baz' => 'Baz'
  206. );
  207. $html = $this->helper->formRadio(array(
  208. 'name' => 'foo',
  209. 'value' => 'bar',
  210. 'options' => $options,
  211. 'attribs' => array('disable' => array('foo', 'baz'))
  212. ));
  213. foreach (array('foo', 'baz') as $test) {
  214. $this->assertRegexp('/<input[^>]*?(value="' . $test . '")[^>]*?(disabled="disabled")/', $html, $html);
  215. }
  216. $this->assertNotRegexp('/<input[^>]*?(value="bar")[^>]*?(disabled="disabled")/', $html, $html);
  217. $count = substr_count($html, 'disabled="disabled"');
  218. $this->assertEquals(2, $count);
  219. }
  220. public function testLabelsAreEscapedByDefault()
  221. {
  222. $options = array(
  223. 'bar' => '<b>Bar</b>',
  224. );
  225. $html = $this->helper->formRadio(array(
  226. 'name' => 'foo',
  227. 'options' => $options,
  228. ));
  229. $this->assertNotContains($options['bar'], $html);
  230. $this->assertContains('&lt;b&gt;Bar&lt;/b&gt;', $html);
  231. }
  232. public function testXhtmlLabelsAreAllowed()
  233. {
  234. $options = array(
  235. 'bar' => '<b>Bar</b>',
  236. );
  237. $html = $this->helper->formRadio(array(
  238. 'name' => 'foo',
  239. 'options' => $options,
  240. 'attribs' => array('escape' => false)
  241. ));
  242. $this->assertContains($options['bar'], $html);
  243. }
  244. /**
  245. * ZF-1666
  246. */
  247. public function testDoesNotRenderHiddenElements()
  248. {
  249. $options = array(
  250. 'foo' => 'Foo',
  251. 'bar' => 'Bar',
  252. 'baz' => 'Baz'
  253. );
  254. $html = $this->helper->formRadio(array(
  255. 'name' => 'foo',
  256. 'options' => $options,
  257. ));
  258. $this->assertNotRegexp('/<input[^>]*?(type="hidden")/', $html);
  259. }
  260. public function testSpecifyingAValueThatMatchesAnOptionChecksIt()
  261. {
  262. $options = array(
  263. 'foo' => 'Foo',
  264. 'bar' => 'Bar',
  265. 'baz' => 'Baz'
  266. );
  267. $html = $this->helper->formRadio(array(
  268. 'name' => 'foo',
  269. 'value' => 'bar',
  270. 'options' => $options,
  271. ));
  272. if (!preg_match('/(<input[^>]*?(value="bar")[^>]*>)/', $html, $matches)) {
  273. $this->fail('Radio for a given option was not found?');
  274. }
  275. $this->assertContains('checked="checked"', $matches[1], var_export($matches, 1));
  276. }
  277. public function testOptionsWithMatchesInAnArrayOfValuesAreChecked()
  278. {
  279. $options = array(
  280. 'foo' => 'Foo',
  281. 'bar' => 'Bar',
  282. 'baz' => 'Baz'
  283. );
  284. $html = $this->helper->formRadio(array(
  285. 'name' => 'foo',
  286. 'value' => array('foo', 'baz'),
  287. 'options' => $options,
  288. ));
  289. foreach (array('foo', 'baz') as $value) {
  290. if (!preg_match('/(<input[^>]*?(value="' . $value . '")[^>]*>)/', $html, $matches)) {
  291. $this->fail('Radio for a given option was not found?');
  292. }
  293. $this->assertContains('checked="checked"', $matches[1], var_export($matches, 1));
  294. }
  295. }
  296. public function testEachRadioShouldHaveIdCreatedByAppendingFilteredValue()
  297. {
  298. $options = array(
  299. 'foo bar' => 'Foo',
  300. 'bar baz' => 'Bar',
  301. 'baz' => 'Baz'
  302. );
  303. $html = $this->helper->formRadio(array(
  304. 'name' => 'foo[]',
  305. 'value' => 'bar',
  306. 'options' => $options,
  307. ));
  308. require_once 'Zend/Filter/Alnum.php';
  309. $filter = new Zend_Filter_Alnum();
  310. foreach ($options as $key => $value) {
  311. $id = 'foo-' . $filter->filter($key);
  312. $this->assertRegexp('/<input([^>]*)(id="' . $id . '")/', $html);
  313. }
  314. }
  315. public function testEachRadioShouldUseAttributeIdWhenSpecified()
  316. {
  317. $options = array(
  318. 'foo bar' => 'Foo',
  319. 'bar baz' => 'Bar',
  320. 'baz' => 'Baz'
  321. );
  322. $html = $this->helper->formRadio(array(
  323. 'name' => 'foo[bar]',
  324. 'value' => 'bar',
  325. 'attribs' => array('id' => 'foo-bar'),
  326. 'options' => $options,
  327. ));
  328. require_once 'Zend/Filter/Alnum.php';
  329. $filter = new Zend_Filter_Alnum();
  330. foreach ($options as $key => $value) {
  331. $id = 'foo-bar-' . $filter->filter($key);
  332. $this->assertRegexp('/<input([^>]*)(id="' . $id . '")/', $html);
  333. }
  334. }
  335. /**
  336. * @group ZF-5681
  337. */
  338. public function testRadioLabelDoesNotContainHardCodedStyle()
  339. {
  340. $options = array(
  341. 'foo' => 'Foo',
  342. 'bar' => 'Bar',
  343. 'baz' => 'Baz'
  344. );
  345. $html = $this->helper->formRadio(array(
  346. 'name' => 'foo',
  347. 'value' => 'bar',
  348. 'options' => $options,
  349. ));
  350. $this->assertNotContains('style="white-space: nowrap;"', $html);
  351. }
  352. /**
  353. * @group ZF-8709
  354. */
  355. public function testRadioLabelContainsNotForAttributeTag()
  356. {
  357. $actual = $this->helper->formRadio(
  358. array(
  359. 'name' => 'foo',
  360. 'options' => array(
  361. 'bar' => 'Bar',
  362. 'baz' => 'Baz'
  363. ),
  364. )
  365. );
  366. $expected = '<label><input type="radio" name="foo" id="foo-bar" value="bar">Bar</label><br>'
  367. . "\n"
  368. . '<label><input type="radio" name="foo" id="foo-baz" value="baz">Baz</label>';
  369. $this->assertSame($expected, $actual);
  370. }
  371. /**
  372. * @group ZF-4191
  373. */
  374. public function testDashesShouldNotBeFilteredFromId()
  375. {
  376. $name = "Foo";
  377. $options = array(
  378. -1 => 'Test -1',
  379. 0 => 'Test 0',
  380. 1 => 'Test 1'
  381. );
  382. $formRadio = new Zend_View_Helper_FormRadio();
  383. $formRadio->setView(new Zend_View());
  384. $html = $formRadio->formRadio($name, -1, null, $options);
  385. foreach ( $options as $key=>$value ) {
  386. $fid = "{$name}-{$key}";
  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. * @group ZF-11477
  394. */
  395. public function testRendersAsHtmlByDefault()
  396. {
  397. $options = array(
  398. 'foo' => 'Foo',
  399. 'bar' => 'Bar',
  400. 'baz' => 'Baz'
  401. );
  402. $html = $this->helper->formRadio(array(
  403. 'name' => 'foo',
  404. 'options' => $options,
  405. ));
  406. $this->assertContains('value="foo">', $html);
  407. $this->assertContains('value="bar">', $html);
  408. $this->assertContains('value="baz">', $html);
  409. }
  410. /**
  411. * @group ZF-11477
  412. */
  413. public function testCanRendersAsXHtml()
  414. {
  415. $this->view->doctype('XHTML1_STRICT');
  416. $options = array(
  417. 'foo' => 'Foo',
  418. 'bar' => 'Bar',
  419. 'baz' => 'Baz'
  420. );
  421. $html = $this->helper->formRadio(array(
  422. 'name' => 'foo',
  423. 'options' => $options,
  424. ));
  425. $this->assertContains('value="foo" />', $html);
  426. $this->assertContains('value="bar" />', $html);
  427. $this->assertContains('value="baz" />', $html);
  428. }
  429. /**
  430. * @group ZF-11620
  431. */
  432. public function testSeparatorCanRendersAsXhtmlByDefault()
  433. {
  434. $this->view->doctype('XHTML1_STRICT');
  435. $options = array(
  436. 'foo' => 'Foo',
  437. 'bar' => 'Bar',
  438. 'baz' => 'Baz'
  439. );
  440. $html = $this->helper->formRadio(array(
  441. 'name' => 'foo',
  442. 'value' => 'bar',
  443. 'options' => $options,
  444. ));
  445. $this->assertContains('<br />', $html);
  446. $count = substr_count($html, '<br />');
  447. $this->assertEquals(2, $count);
  448. }
  449. /**
  450. * @group ZF-11620
  451. */
  452. public function testeparatorCanRendersAsHtml()
  453. {
  454. $this->view->doctype('HTML4_STRICT');
  455. $options = array(
  456. 'foo' => 'Foo',
  457. 'bar' => 'Bar',
  458. 'baz' => 'Baz'
  459. );
  460. $html = $this->helper->formRadio(array(
  461. 'name' => 'foo',
  462. 'value' => 'bar',
  463. 'options' => $options,
  464. ));
  465. $this->assertContains('<br>', $html);
  466. $count = substr_count($html, '<br>');
  467. $this->assertEquals(2, $count);
  468. }
  469. }
  470. // Call Zend_View_Helper_FormRadioTest::main() if this source file is executed directly.
  471. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormRadioTest::main") {
  472. Zend_View_Helper_FormRadioTest::main();
  473. }