FormRadioTest.php 11 KB

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