2
0

FormRadioTest.php 12 KB

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