RadioTest.php 9.1 KB

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