RadioTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 CakePHP(tm) v3.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. 'options' => ['r' => 'Red', 'b' => 'Black']
  50. ];
  51. $result = $radio->render($data);
  52. $expected = [
  53. ['input' => [
  54. 'type' => 'radio',
  55. 'name' => 'Crayons[color]',
  56. 'value' => 'r',
  57. 'id' => 'crayons-color-r'
  58. ]],
  59. ['label' => ['for' => 'crayons-color-r']],
  60. 'Red',
  61. '/label',
  62. ['input' => [
  63. 'type' => 'radio',
  64. 'name' => 'Crayons[color]',
  65. 'value' => 'b',
  66. 'id' => 'crayons-color-b'
  67. ]],
  68. ['label' => ['for' => 'crayons-color-b']],
  69. 'Black',
  70. '/label',
  71. ];
  72. $this->assertTags($result, $expected);
  73. $data = [
  74. 'name' => 'Crayons[color]',
  75. 'options' => new Collection(['r' => 'Red', 'b' => 'Black'])
  76. ];
  77. $result = $radio->render($data);
  78. $this->assertTags($result, $expected);
  79. }
  80. /**
  81. * Test rendering inputs with the complex option form.
  82. *
  83. * @return void
  84. */
  85. public function testRenderComplex() {
  86. $label = new Label($this->templates);
  87. $radio = new Radio($this->templates, $label);
  88. $data = [
  89. 'name' => 'Crayons[color]',
  90. 'options' => [
  91. ['value' => 'r', 'text' => 'Red', 'id' => 'my_id'],
  92. ['value' => 'b', 'text' => 'Black', 'id' => 'my_id_2', 'data-test' => 'test'],
  93. ]
  94. ];
  95. $result = $radio->render($data);
  96. $expected = [
  97. ['input' => [
  98. 'type' => 'radio',
  99. 'name' => 'Crayons[color]',
  100. 'value' => 'r',
  101. 'id' => 'my_id'
  102. ]],
  103. ['label' => ['for' => 'my_id']],
  104. 'Red',
  105. '/label',
  106. ['input' => [
  107. 'type' => 'radio',
  108. 'name' => 'Crayons[color]',
  109. 'value' => 'b',
  110. 'id' => 'my_id_2',
  111. 'data-test' => 'test'
  112. ]],
  113. ['label' => ['for' => 'my_id_2']],
  114. 'Black',
  115. '/label',
  116. ];
  117. $this->assertTags($result, $expected);
  118. }
  119. /**
  120. * Test rendering the empty option.
  121. *
  122. * @return void
  123. */
  124. public function testRenderEmptyOption() {
  125. $label = new Label($this->templates);
  126. $radio = new Radio($this->templates, $label);
  127. $data = [
  128. 'name' => 'Crayons[color]',
  129. 'options' => ['r' => 'Red'],
  130. 'empty' => true,
  131. ];
  132. $result = $radio->render($data);
  133. $expected = [
  134. ['input' => [
  135. 'type' => 'radio',
  136. 'name' => 'Crayons[color]',
  137. 'value' => '',
  138. 'id' => 'crayons-color'
  139. ]],
  140. ['label' => ['for' => 'crayons-color']],
  141. 'empty',
  142. '/label',
  143. ['input' => [
  144. 'type' => 'radio',
  145. 'name' => 'Crayons[color]',
  146. 'value' => 'r',
  147. 'id' => 'crayons-color-r'
  148. ]],
  149. ['label' => ['for' => 'crayons-color-r']],
  150. 'Red',
  151. '/label',
  152. ];
  153. $this->assertTags($result, $expected);
  154. $data['empty'] = 'Choose one';
  155. $result = $radio->render($data);
  156. $expected = [
  157. ['input' => [
  158. 'type' => 'radio',
  159. 'name' => 'Crayons[color]',
  160. 'value' => '',
  161. 'id' => 'crayons-color'
  162. ]],
  163. ['label' => ['for' => 'crayons-color']],
  164. 'Choose one',
  165. '/label',
  166. ['input' => [
  167. 'type' => 'radio',
  168. 'name' => 'Crayons[color]',
  169. 'value' => 'r',
  170. 'id' => 'crayons-color-r'
  171. ]],
  172. ['label' => ['for' => 'crayons-color-r']],
  173. 'Red',
  174. '/label',
  175. ];
  176. $this->assertTags($result, $expected);
  177. }
  178. /**
  179. * Test rendering the input inside the label.
  180. *
  181. * @return void
  182. */
  183. public function testRenderInputInsideLabel() {
  184. $this->templates->add([
  185. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  186. 'radioContainer' => '{{label}}',
  187. ]);
  188. $label = new Label($this->templates);
  189. $radio = new Radio($this->templates, $label);
  190. $data = [
  191. 'name' => 'Crayons[color]',
  192. 'options' => ['r' => 'Red'],
  193. ];
  194. $result = $radio->render($data);
  195. $expected = [
  196. ['label' => ['for' => 'crayons-color-r']],
  197. ['input' => [
  198. 'type' => 'radio',
  199. 'name' => 'Crayons[color]',
  200. 'value' => 'r',
  201. 'id' => 'crayons-color-r'
  202. ]],
  203. 'Red',
  204. '/label',
  205. ];
  206. $this->assertTags($result, $expected);
  207. }
  208. /**
  209. * test render() and selected inputs.
  210. *
  211. * @return void
  212. */
  213. public function testRenderSelected() {
  214. $label = new Label($this->templates);
  215. $radio = new Radio($this->templates, $label);
  216. $data = [
  217. 'name' => 'Versions[ver]',
  218. 'val' => '1',
  219. 'options' => [
  220. 1 => 'one',
  221. '1x' => 'one x',
  222. '2' => 'two',
  223. ]
  224. ];
  225. $result = $radio->render($data);
  226. $expected = [
  227. ['input' => [
  228. 'id' => 'versions-ver-1',
  229. 'name' => 'Versions[ver]',
  230. 'type' => 'radio',
  231. 'value' => '1',
  232. 'checked' => 'checked'
  233. ]],
  234. ['label' => ['for' => 'versions-ver-1']],
  235. 'one',
  236. '/label',
  237. ['input' => [
  238. 'id' => 'versions-ver-1x',
  239. 'name' => 'Versions[ver]',
  240. 'type' => 'radio',
  241. 'value' => '1x'
  242. ]],
  243. ['label' => ['for' => 'versions-ver-1x']],
  244. 'one x',
  245. '/label',
  246. ['input' => [
  247. 'id' => 'versions-ver-2',
  248. 'name' => 'Versions[ver]',
  249. 'type' => 'radio',
  250. 'value' => '2'
  251. ]],
  252. ['label' => ['for' => 'versions-ver-2']],
  253. 'two',
  254. '/label',
  255. ];
  256. $this->assertTags($result, $expected);
  257. }
  258. /**
  259. * Test rendering with disable inputs
  260. *
  261. * @return void
  262. */
  263. public function testRenderDisabled() {
  264. $label = new Label($this->templates);
  265. $radio = new Radio($this->templates, $label);
  266. $data = [
  267. 'name' => 'Versions[ver]',
  268. 'options' => [
  269. 1 => 'one',
  270. '1x' => 'one x',
  271. '2' => 'two',
  272. ],
  273. 'disabled' => true,
  274. ];
  275. $result = $radio->render($data);
  276. $expected = [
  277. ['input' => [
  278. 'id' => 'versions-ver-1',
  279. 'name' => 'Versions[ver]',
  280. 'type' => 'radio',
  281. 'value' => '1',
  282. 'disabled' => 'disabled'
  283. ]],
  284. ['label' => ['for' => 'versions-ver-1']],
  285. 'one',
  286. '/label',
  287. ['input' => [
  288. 'id' => 'versions-ver-1x',
  289. 'name' => 'Versions[ver]',
  290. 'type' => 'radio',
  291. 'value' => '1x',
  292. 'disabled' => 'disabled'
  293. ]],
  294. ['label' => ['for' => 'versions-ver-1x']],
  295. 'one x',
  296. '/label',
  297. ];
  298. $this->assertTags($result, $expected);
  299. $data['disabled'] = ['1'];
  300. $result = $radio->render($data);
  301. $expected = [
  302. ['input' => [
  303. 'id' => 'versions-ver-1',
  304. 'name' => 'Versions[ver]',
  305. 'type' => 'radio',
  306. 'value' => '1',
  307. 'disabled' => 'disabled'
  308. ]],
  309. ['label' => ['for' => 'versions-ver-1']],
  310. 'one',
  311. '/label',
  312. ['input' => [
  313. 'id' => 'versions-ver-1x',
  314. 'name' => 'Versions[ver]',
  315. 'type' => 'radio',
  316. 'value' => '1x',
  317. ]],
  318. ['label' => ['for' => 'versions-ver-1x']],
  319. 'one x',
  320. '/label',
  321. ];
  322. $this->assertTags($result, $expected);
  323. }
  324. /**
  325. * Test rendering with label options.
  326. *
  327. * @return void
  328. */
  329. public function testRenderLabelOptions() {
  330. $label = new Label($this->templates);
  331. $radio = new Radio($this->templates, $label);
  332. $data = [
  333. 'name' => 'Versions[ver]',
  334. 'options' => [
  335. 1 => 'one',
  336. '1x' => 'one x',
  337. '2' => 'two',
  338. ],
  339. 'label' => false,
  340. ];
  341. $result = $radio->render($data);
  342. $expected = [
  343. ['input' => [
  344. 'id' => 'versions-ver-1',
  345. 'name' => 'Versions[ver]',
  346. 'type' => 'radio',
  347. 'value' => '1',
  348. ]],
  349. ['input' => [
  350. 'id' => 'versions-ver-1x',
  351. 'name' => 'Versions[ver]',
  352. 'type' => 'radio',
  353. 'value' => '1x',
  354. ]],
  355. ];
  356. $this->assertTags($result, $expected);
  357. $data = [
  358. 'name' => 'Versions[ver]',
  359. 'options' => [
  360. 1 => 'one',
  361. '1x' => 'one x',
  362. '2' => 'two',
  363. ],
  364. 'label' => [
  365. 'class' => 'my-class',
  366. ]
  367. ];
  368. $result = $radio->render($data);
  369. $expected = [
  370. ['input' => [
  371. 'id' => 'versions-ver-1',
  372. 'name' => 'Versions[ver]',
  373. 'type' => 'radio',
  374. 'value' => '1',
  375. ]],
  376. ['label' => ['class' => 'my-class', 'for' => 'versions-ver-1']],
  377. 'one',
  378. '/label',
  379. ['input' => [
  380. 'id' => 'versions-ver-1x',
  381. 'name' => 'Versions[ver]',
  382. 'type' => 'radio',
  383. 'value' => '1x',
  384. ]],
  385. ['label' => ['class' => 'my-class', 'for' => 'versions-ver-1x']],
  386. 'one x',
  387. '/label',
  388. ];
  389. $this->assertTags($result, $expected);
  390. }
  391. /**
  392. * Ensure that the input + label are composed with
  393. * a template.
  394. *
  395. * @return void
  396. */
  397. public function testRenderContainerTemplate() {
  398. $this->templates->add([
  399. 'radioContainer' => '<div class="radio">{{input}}{{label}}</div>'
  400. ]);
  401. $label = new Label($this->templates);
  402. $radio = new Radio($this->templates, $label);
  403. $data = [
  404. 'name' => 'Versions[ver]',
  405. 'options' => [
  406. 1 => 'one',
  407. '1x' => 'one x',
  408. '2' => 'two',
  409. ],
  410. ];
  411. $result = $radio->render($data);
  412. $this->assertContains(
  413. '<div class="radio"><input type="radio"',
  414. $result
  415. );
  416. $this->assertContains(
  417. '</label></div>',
  418. $result
  419. );
  420. }
  421. }