RadioTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Widget;
  16. use Cake\Collection\Collection;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\View\StringTemplate;
  19. use Cake\View\Widget\Label;
  20. use Cake\View\Widget\Radio;
  21. /**
  22. * Radio test case
  23. */
  24. class RadioTest extends TestCase {
  25. /**
  26. * setup method.
  27. *
  28. * @return void
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. $templates = [
  33. 'radio' => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>',
  34. 'label' => '<label{{attrs}}>{{text}}</label>',
  35. 'radioContainer' => '{{input}}{{label}}',
  36. ];
  37. $this->templates = new StringTemplate($templates);
  38. }
  39. /**
  40. * Test rendering basic radio buttons.
  41. *
  42. * @return void
  43. */
  44. public function testRenderSimple() {
  45. $label = new Label($this->templates);
  46. $radio = new Radio($this->templates, $label);
  47. $data = [
  48. 'name' => 'Crayons[color]',
  49. 'label' => null,
  50. 'options' => ['r' => 'Red', 'b' => 'Black']
  51. ];
  52. $result = $radio->render($data);
  53. $expected = [
  54. ['input' => [
  55. 'type' => 'radio',
  56. 'name' => 'Crayons[color]',
  57. 'value' => 'r',
  58. 'id' => 'crayons-color-r'
  59. ]],
  60. ['label' => ['for' => 'crayons-color-r']],
  61. 'Red',
  62. '/label',
  63. ['input' => [
  64. 'type' => 'radio',
  65. 'name' => 'Crayons[color]',
  66. 'value' => 'b',
  67. 'id' => 'crayons-color-b'
  68. ]],
  69. ['label' => ['for' => 'crayons-color-b']],
  70. 'Black',
  71. '/label',
  72. ];
  73. $this->assertTags($result, $expected);
  74. $data = [
  75. 'name' => 'Crayons[color]',
  76. 'options' => new Collection(['r' => 'Red', 'b' => 'Black'])
  77. ];
  78. $result = $radio->render($data);
  79. $this->assertTags($result, $expected);
  80. }
  81. /**
  82. * Test rendering inputs with the complex option form.
  83. *
  84. * @return void
  85. */
  86. public function testRenderComplex() {
  87. $label = new Label($this->templates);
  88. $radio = new Radio($this->templates, $label);
  89. $data = [
  90. 'name' => 'Crayons[color]',
  91. 'options' => [
  92. ['value' => 'r', 'text' => 'Red', 'id' => 'my_id'],
  93. ['value' => 'b', 'text' => 'Black', 'id' => 'my_id_2', 'data-test' => 'test'],
  94. ]
  95. ];
  96. $result = $radio->render($data);
  97. $expected = [
  98. ['input' => [
  99. 'type' => 'radio',
  100. 'name' => 'Crayons[color]',
  101. 'value' => 'r',
  102. 'id' => 'my_id'
  103. ]],
  104. ['label' => ['for' => 'my_id']],
  105. 'Red',
  106. '/label',
  107. ['input' => [
  108. 'type' => 'radio',
  109. 'name' => 'Crayons[color]',
  110. 'value' => 'b',
  111. 'id' => 'my_id_2',
  112. 'data-test' => 'test'
  113. ]],
  114. ['label' => ['for' => 'my_id_2']],
  115. 'Black',
  116. '/label',
  117. ];
  118. $this->assertTags($result, $expected);
  119. }
  120. /**
  121. * Test that id suffixes are generated to not collide
  122. *
  123. * @return void
  124. */
  125. public function testRenderIdSuffixGeneration() {
  126. $label = new Label($this->templates);
  127. $radio = new Radio($this->templates, $label);
  128. $data = [
  129. 'name' => 'Thing[value]',
  130. 'options' => ['a>b' => 'First', 'a<b' => 'Second']
  131. ];
  132. $result = $radio->render($data);
  133. $expected = [
  134. ['input' => [
  135. 'type' => 'radio',
  136. 'name' => 'Thing[value]',
  137. 'value' => 'a&gt;b',
  138. 'id' => 'thing-value-a-b'
  139. ]],
  140. ['label' => ['for' => 'thing-value-a-b']],
  141. 'First',
  142. '/label',
  143. ['input' => [
  144. 'type' => 'radio',
  145. 'name' => 'Thing[value]',
  146. 'value' => 'a&lt;b',
  147. 'id' => 'thing-value-a-b1',
  148. ]],
  149. ['label' => ['for' => 'thing-value-a-b1']],
  150. 'Second',
  151. '/label',
  152. ];
  153. $this->assertTags($result, $expected);
  154. }
  155. /**
  156. * Test rendering checks the right option with booleanish values.
  157. *
  158. * @return void
  159. */
  160. public function testRenderBooleanishValues() {
  161. $label = new Label($this->templates);
  162. $radio = new Radio($this->templates, $label);
  163. $data = [
  164. 'name' => 'Model[field]',
  165. 'options' => ['1' => 'Yes', '0' => 'No'],
  166. 'val' => '0'
  167. ];
  168. $result = $radio->render($data);
  169. $expected = array(
  170. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  171. array('label' => array('for' => 'model-field-1')),
  172. 'Yes',
  173. '/label',
  174. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0', 'checked' => 'checked')),
  175. array('label' => array('for' => 'model-field-0')),
  176. 'No',
  177. '/label',
  178. );
  179. $this->assertTags($result, $expected);
  180. $data['val'] = 0;
  181. $result = $radio->render($data);
  182. $this->assertTags($result, $expected);
  183. $data['val'] = false;
  184. $result = $radio->render($data);
  185. $this->assertTags($result, $expected);
  186. $expected = array(
  187. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  188. array('label' => array('for' => 'model-field-1')),
  189. 'Yes',
  190. '/label',
  191. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  192. array('label' => array('for' => 'model-field-0')),
  193. 'No',
  194. '/label',
  195. );
  196. $data['val'] = null;
  197. $result = $radio->render($data);
  198. $this->assertTags($result, $expected);
  199. $data['val'] = '';
  200. $result = $radio->render($data);
  201. $this->assertTags($result, $expected);
  202. $expected = array(
  203. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1', 'checked' => 'checked')),
  204. array('label' => array('for' => 'model-field-1')),
  205. 'Yes',
  206. '/label',
  207. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  208. array('label' => array('for' => 'model-field-0')),
  209. 'No',
  210. '/label',
  211. );
  212. $data['val'] = '1';
  213. $result = $radio->render($data);
  214. $this->assertTags($result, $expected);
  215. $data['val'] = 1;
  216. $result = $radio->render($data);
  217. $this->assertTags($result, $expected);
  218. $data['val'] = true;
  219. $result = $radio->render($data);
  220. $this->assertTags($result, $expected);
  221. }
  222. /**
  223. * Test that render() works with the required attribute.
  224. *
  225. * @return void
  226. */
  227. public function testRenderRequiredAndFormAttribute() {
  228. $label = new Label($this->templates);
  229. $radio = new Radio($this->templates, $label);
  230. $data = [
  231. 'name' => 'published',
  232. 'options' => ['option A', 'option B'],
  233. 'required' => true,
  234. 'form' => 'my-form',
  235. ];
  236. $result = $radio->render($data);
  237. $expected = [
  238. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => '0',
  239. 'id' => 'published-0', 'required' => 'required', 'form' => 'my-form']],
  240. ['label' => ['for' => 'published-0']],
  241. 'option A',
  242. '/label',
  243. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => '1',
  244. 'id' => 'published-1', 'required' => 'required', 'form' => 'my-form']],
  245. ['label' => ['for' => 'published-1']],
  246. 'option B',
  247. '/label',
  248. ];
  249. $this->assertTags($result, $expected);
  250. }
  251. /**
  252. * Test rendering the empty option.
  253. *
  254. * @return void
  255. */
  256. public function testRenderEmptyOption() {
  257. $label = new Label($this->templates);
  258. $radio = new Radio($this->templates, $label);
  259. $data = [
  260. 'name' => 'Crayons[color]',
  261. 'options' => ['r' => 'Red'],
  262. 'empty' => true,
  263. ];
  264. $result = $radio->render($data);
  265. $expected = [
  266. ['input' => [
  267. 'type' => 'radio',
  268. 'name' => 'Crayons[color]',
  269. 'value' => '',
  270. 'id' => 'crayons-color'
  271. ]],
  272. ['label' => ['for' => 'crayons-color']],
  273. 'empty',
  274. '/label',
  275. ['input' => [
  276. 'type' => 'radio',
  277. 'name' => 'Crayons[color]',
  278. 'value' => 'r',
  279. 'id' => 'crayons-color-r'
  280. ]],
  281. ['label' => ['for' => 'crayons-color-r']],
  282. 'Red',
  283. '/label',
  284. ];
  285. $this->assertTags($result, $expected);
  286. $data['empty'] = 'Choose one';
  287. $result = $radio->render($data);
  288. $expected = [
  289. ['input' => [
  290. 'type' => 'radio',
  291. 'name' => 'Crayons[color]',
  292. 'value' => '',
  293. 'id' => 'crayons-color'
  294. ]],
  295. ['label' => ['for' => 'crayons-color']],
  296. 'Choose one',
  297. '/label',
  298. ['input' => [
  299. 'type' => 'radio',
  300. 'name' => 'Crayons[color]',
  301. 'value' => 'r',
  302. 'id' => 'crayons-color-r'
  303. ]],
  304. ['label' => ['for' => 'crayons-color-r']],
  305. 'Red',
  306. '/label',
  307. ];
  308. $this->assertTags($result, $expected);
  309. }
  310. /**
  311. * Test rendering the input inside the label.
  312. *
  313. * @return void
  314. */
  315. public function testRenderInputInsideLabel() {
  316. $this->templates->add([
  317. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  318. 'radioContainer' => '{{label}}',
  319. ]);
  320. $label = new Label($this->templates);
  321. $radio = new Radio($this->templates, $label);
  322. $data = [
  323. 'name' => 'Crayons[color]',
  324. 'options' => ['r' => 'Red'],
  325. ];
  326. $result = $radio->render($data);
  327. $expected = [
  328. ['label' => ['for' => 'crayons-color-r']],
  329. ['input' => [
  330. 'type' => 'radio',
  331. 'name' => 'Crayons[color]',
  332. 'value' => 'r',
  333. 'id' => 'crayons-color-r'
  334. ]],
  335. 'Red',
  336. '/label',
  337. ];
  338. $this->assertTags($result, $expected);
  339. }
  340. /**
  341. * test render() and selected inputs.
  342. *
  343. * @return void
  344. */
  345. public function testRenderSelected() {
  346. $label = new Label($this->templates);
  347. $radio = new Radio($this->templates, $label);
  348. $data = [
  349. 'name' => 'Versions[ver]',
  350. 'val' => '1',
  351. 'options' => [
  352. 1 => 'one',
  353. '1x' => 'one x',
  354. '2' => 'two',
  355. ]
  356. ];
  357. $result = $radio->render($data);
  358. $expected = [
  359. ['input' => [
  360. 'id' => 'versions-ver-1',
  361. 'name' => 'Versions[ver]',
  362. 'type' => 'radio',
  363. 'value' => '1',
  364. 'checked' => 'checked'
  365. ]],
  366. ['label' => ['for' => 'versions-ver-1']],
  367. 'one',
  368. '/label',
  369. ['input' => [
  370. 'id' => 'versions-ver-1x',
  371. 'name' => 'Versions[ver]',
  372. 'type' => 'radio',
  373. 'value' => '1x'
  374. ]],
  375. ['label' => ['for' => 'versions-ver-1x']],
  376. 'one x',
  377. '/label',
  378. ['input' => [
  379. 'id' => 'versions-ver-2',
  380. 'name' => 'Versions[ver]',
  381. 'type' => 'radio',
  382. 'value' => '2'
  383. ]],
  384. ['label' => ['for' => 'versions-ver-2']],
  385. 'two',
  386. '/label',
  387. ];
  388. $this->assertTags($result, $expected);
  389. }
  390. /**
  391. * Test rendering with disable inputs
  392. *
  393. * @return void
  394. */
  395. public function testRenderDisabled() {
  396. $label = new Label($this->templates);
  397. $radio = new Radio($this->templates, $label);
  398. $data = [
  399. 'name' => 'Versions[ver]',
  400. 'options' => [
  401. 1 => 'one',
  402. '1x' => 'one x',
  403. '2' => 'two',
  404. ],
  405. 'disabled' => true,
  406. ];
  407. $result = $radio->render($data);
  408. $expected = [
  409. ['input' => [
  410. 'id' => 'versions-ver-1',
  411. 'name' => 'Versions[ver]',
  412. 'type' => 'radio',
  413. 'value' => '1',
  414. 'disabled' => 'disabled'
  415. ]],
  416. ['label' => ['for' => 'versions-ver-1']],
  417. 'one',
  418. '/label',
  419. ['input' => [
  420. 'id' => 'versions-ver-1x',
  421. 'name' => 'Versions[ver]',
  422. 'type' => 'radio',
  423. 'value' => '1x',
  424. 'disabled' => 'disabled'
  425. ]],
  426. ['label' => ['for' => 'versions-ver-1x']],
  427. 'one x',
  428. '/label',
  429. ];
  430. $this->assertTags($result, $expected);
  431. $data['disabled'] = 'a string';
  432. $result = $radio->render($data);
  433. $this->assertTags($result, $expected);
  434. $data['disabled'] = ['1'];
  435. $result = $radio->render($data);
  436. $expected = [
  437. ['input' => [
  438. 'id' => 'versions-ver-1',
  439. 'name' => 'Versions[ver]',
  440. 'type' => 'radio',
  441. 'value' => '1',
  442. 'disabled' => 'disabled'
  443. ]],
  444. ['label' => ['for' => 'versions-ver-1']],
  445. 'one',
  446. '/label',
  447. ['input' => [
  448. 'id' => 'versions-ver-1x',
  449. 'name' => 'Versions[ver]',
  450. 'type' => 'radio',
  451. 'value' => '1x',
  452. ]],
  453. ['label' => ['for' => 'versions-ver-1x']],
  454. 'one x',
  455. '/label',
  456. ];
  457. $this->assertTags($result, $expected);
  458. }
  459. /**
  460. * Test rendering with label options.
  461. *
  462. * @return void
  463. */
  464. public function testRenderLabelOptions() {
  465. $label = new Label($this->templates);
  466. $radio = new Radio($this->templates, $label);
  467. $data = [
  468. 'name' => 'Versions[ver]',
  469. 'options' => [
  470. 1 => 'one',
  471. '1x' => 'one x',
  472. '2' => 'two',
  473. ],
  474. 'label' => false,
  475. ];
  476. $result = $radio->render($data);
  477. $expected = [
  478. ['input' => [
  479. 'id' => 'versions-ver-1',
  480. 'name' => 'Versions[ver]',
  481. 'type' => 'radio',
  482. 'value' => '1',
  483. ]],
  484. ['input' => [
  485. 'id' => 'versions-ver-1x',
  486. 'name' => 'Versions[ver]',
  487. 'type' => 'radio',
  488. 'value' => '1x',
  489. ]],
  490. ];
  491. $this->assertTags($result, $expected);
  492. $data = [
  493. 'name' => 'Versions[ver]',
  494. 'options' => [
  495. 1 => 'one',
  496. '1x' => 'one x',
  497. '2' => 'two',
  498. ],
  499. 'label' => [
  500. 'class' => 'my-class',
  501. ]
  502. ];
  503. $result = $radio->render($data);
  504. $expected = [
  505. ['input' => [
  506. 'id' => 'versions-ver-1',
  507. 'name' => 'Versions[ver]',
  508. 'type' => 'radio',
  509. 'value' => '1',
  510. ]],
  511. ['label' => ['class' => 'my-class', 'for' => 'versions-ver-1']],
  512. 'one',
  513. '/label',
  514. ['input' => [
  515. 'id' => 'versions-ver-1x',
  516. 'name' => 'Versions[ver]',
  517. 'type' => 'radio',
  518. 'value' => '1x',
  519. ]],
  520. ['label' => ['class' => 'my-class', 'for' => 'versions-ver-1x']],
  521. 'one x',
  522. '/label',
  523. ];
  524. $this->assertTags($result, $expected);
  525. }
  526. /**
  527. * Ensure that the input + label are composed with
  528. * a template.
  529. *
  530. * @return void
  531. */
  532. public function testRenderContainerTemplate() {
  533. $this->templates->add([
  534. 'radioContainer' => '<div class="radio">{{input}}{{label}}</div>'
  535. ]);
  536. $label = new Label($this->templates);
  537. $radio = new Radio($this->templates, $label);
  538. $data = [
  539. 'name' => 'Versions[ver]',
  540. 'options' => [
  541. 1 => 'one',
  542. '1x' => 'one x',
  543. '2' => 'two',
  544. ],
  545. ];
  546. $result = $radio->render($data);
  547. $this->assertContains(
  548. '<div class="radio"><input type="radio"',
  549. $result
  550. );
  551. $this->assertContains(
  552. '</label></div>',
  553. $result
  554. );
  555. }
  556. }