RadioWidgetTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://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\NestingLabelWidget;
  20. use Cake\View\Widget\RadioWidget;
  21. /**
  22. * Radio test case
  23. */
  24. class RadioWidgetTest extends TestCase
  25. {
  26. /**
  27. * setup method.
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. $templates = [
  35. 'radio' => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>',
  36. 'nestingLabel' => '<label{{attrs}}>{{input}}{{text}}</label>',
  37. 'radioWrapper' => '{{label}}',
  38. ];
  39. $this->templates = new StringTemplate($templates);
  40. $this->context = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  41. }
  42. /**
  43. * Test rendering basic radio buttons without nested inputs
  44. *
  45. * @return void
  46. */
  47. public function testRenderSimpleNotNested()
  48. {
  49. $this->templates->add([
  50. 'nestingLabel' => '<label{{attrs}}>{{text}}</label>',
  51. 'radioWrapper' => '{{input}}{{label}}'
  52. ]);
  53. $label = new NestingLabelWidget($this->templates);
  54. $radio = new RadioWidget($this->templates, $label);
  55. $data = [
  56. 'name' => 'Crayons[color]',
  57. 'label' => null,
  58. 'options' => ['r' => 'Red', 'b' => 'Black']
  59. ];
  60. $result = $radio->render($data, $this->context);
  61. $expected = [
  62. ['input' => [
  63. 'type' => 'radio',
  64. 'name' => 'Crayons[color]',
  65. 'value' => 'r',
  66. 'id' => 'crayons-color-r'
  67. ]],
  68. ['label' => ['for' => 'crayons-color-r']],
  69. 'Red',
  70. '/label',
  71. ['input' => [
  72. 'type' => 'radio',
  73. 'name' => 'Crayons[color]',
  74. 'value' => 'b',
  75. 'id' => 'crayons-color-b'
  76. ]],
  77. ['label' => ['for' => 'crayons-color-b']],
  78. 'Black',
  79. '/label',
  80. ];
  81. $this->assertHtml($expected, $result);
  82. $data = [
  83. 'name' => 'Crayons[color]',
  84. 'label' => false,
  85. 'options' => ['r' => 'Red', 'b' => 'Black']
  86. ];
  87. $result = $radio->render($data, $this->context);
  88. $expected = [
  89. ['input' => [
  90. 'type' => 'radio',
  91. 'name' => 'Crayons[color]',
  92. 'value' => 'r',
  93. 'id' => 'crayons-color-r'
  94. ]],
  95. ['input' => [
  96. 'type' => 'radio',
  97. 'name' => 'Crayons[color]',
  98. 'value' => 'b',
  99. 'id' => 'crayons-color-b'
  100. ]],
  101. ];
  102. $this->assertHtml($expected, $result);
  103. }
  104. /**
  105. * Test rendering basic radio buttons.
  106. *
  107. * @return void
  108. */
  109. public function testRenderSimple()
  110. {
  111. $label = new NestingLabelWidget($this->templates);
  112. $radio = new RadioWidget($this->templates, $label);
  113. $data = [
  114. 'name' => 'Crayons[color]',
  115. 'label' => null,
  116. 'options' => ['r' => 'Red', 'b' => 'Black']
  117. ];
  118. $result = $radio->render($data, $this->context);
  119. $expected = [
  120. ['label' => ['for' => 'crayons-color-r']],
  121. ['input' => [
  122. 'type' => 'radio',
  123. 'name' => 'Crayons[color]',
  124. 'value' => 'r',
  125. 'id' => 'crayons-color-r'
  126. ]],
  127. 'Red',
  128. '/label',
  129. ['label' => ['for' => 'crayons-color-b']],
  130. ['input' => [
  131. 'type' => 'radio',
  132. 'name' => 'Crayons[color]',
  133. 'value' => 'b',
  134. 'id' => 'crayons-color-b'
  135. ]],
  136. 'Black',
  137. '/label',
  138. ];
  139. $this->assertHtml($expected, $result);
  140. $data = [
  141. 'name' => 'Crayons[color]',
  142. 'options' => new Collection(['r' => 'Red', 'b' => 'Black'])
  143. ];
  144. $result = $radio->render($data, $this->context);
  145. $this->assertHtml($expected, $result);
  146. }
  147. /**
  148. * Test rendering the activeClass template var
  149. *
  150. * @return void
  151. */
  152. public function testRenderSimpleActiveTemplateVar()
  153. {
  154. $this->templates->add([
  155. 'nestingLabel' => '<label class="{{activeClass}}"{{attrs}}>{{text}}</label>',
  156. 'radioWrapper' => '{{input}}{{label}}'
  157. ]);
  158. $label = new NestingLabelWidget($this->templates);
  159. $radio = new RadioWidget($this->templates, $label);
  160. $data = [
  161. 'name' => 'Crayons[color]',
  162. 'val' => 'r',
  163. 'options' => ['r' => 'Red', 'b' => 'Black']
  164. ];
  165. $result = $radio->render($data, $this->context);
  166. $expected = [
  167. ['input' => [
  168. 'type' => 'radio',
  169. 'name' => 'Crayons[color]',
  170. 'value' => 'r',
  171. 'id' => 'crayons-color-r',
  172. 'checked' => 'checked',
  173. ]],
  174. ['label' => ['class' => 'active', 'for' => 'crayons-color-r']],
  175. 'Red',
  176. '/label',
  177. ['input' => [
  178. 'type' => 'radio',
  179. 'name' => 'Crayons[color]',
  180. 'value' => 'b',
  181. 'id' => 'crayons-color-b'
  182. ]],
  183. ['label' => ['class' => '', 'for' => 'crayons-color-b']],
  184. 'Black',
  185. '/label',
  186. ];
  187. $this->assertHtml($expected, $result);
  188. }
  189. /**
  190. * Test rendering inputs with the complex option form.
  191. *
  192. * @return void
  193. */
  194. public function testRenderComplex()
  195. {
  196. $label = new NestingLabelWidget($this->templates);
  197. $radio = new RadioWidget($this->templates, $label);
  198. $data = [
  199. 'name' => 'Crayons[color]',
  200. 'options' => [
  201. ['value' => 'r', 'text' => 'Red', 'id' => 'my_id'],
  202. ['value' => 'b', 'text' => 'Black', 'id' => 'my_id_2', 'data-test' => 'test'],
  203. ]
  204. ];
  205. $result = $radio->render($data, $this->context);
  206. $expected = [
  207. ['label' => ['for' => 'my_id']],
  208. ['input' => [
  209. 'type' => 'radio',
  210. 'name' => 'Crayons[color]',
  211. 'value' => 'r',
  212. 'id' => 'my_id'
  213. ]],
  214. 'Red',
  215. '/label',
  216. ['label' => ['for' => 'my_id_2']],
  217. ['input' => [
  218. 'type' => 'radio',
  219. 'name' => 'Crayons[color]',
  220. 'value' => 'b',
  221. 'id' => 'my_id_2',
  222. 'data-test' => 'test'
  223. ]],
  224. 'Black',
  225. '/label',
  226. ];
  227. $this->assertHtml($expected, $result);
  228. }
  229. /**
  230. * Test that id suffixes are generated to not collide
  231. *
  232. * @return void
  233. */
  234. public function testRenderIdSuffixGeneration()
  235. {
  236. $label = new NestingLabelWidget($this->templates);
  237. $radio = new RadioWidget($this->templates, $label);
  238. $data = [
  239. 'name' => 'Thing[value]',
  240. 'options' => ['a>b' => 'First', 'a<b' => 'Second']
  241. ];
  242. $result = $radio->render($data, $this->context);
  243. $expected = [
  244. ['label' => ['for' => 'thing-value-a-b']],
  245. ['input' => [
  246. 'type' => 'radio',
  247. 'name' => 'Thing[value]',
  248. 'value' => 'a&gt;b',
  249. 'id' => 'thing-value-a-b'
  250. ]],
  251. 'First',
  252. '/label',
  253. ['label' => ['for' => 'thing-value-a-b1']],
  254. ['input' => [
  255. 'type' => 'radio',
  256. 'name' => 'Thing[value]',
  257. 'value' => 'a&lt;b',
  258. 'id' => 'thing-value-a-b1',
  259. ]],
  260. 'Second',
  261. '/label',
  262. ];
  263. $this->assertHtml($expected, $result);
  264. }
  265. /**
  266. * Test rendering checks the right option with booleanish values.
  267. *
  268. * @return void
  269. */
  270. public function testRenderBooleanishValues()
  271. {
  272. $label = new NestingLabelWidget($this->templates);
  273. $radio = new RadioWidget($this->templates, $label);
  274. $data = [
  275. 'name' => 'Model[field]',
  276. 'options' => ['1' => 'Yes', '0' => 'No'],
  277. 'val' => '0'
  278. ];
  279. $result = $radio->render($data, $this->context);
  280. $expected = [
  281. ['label' => ['for' => 'model-field-1']],
  282. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  283. 'Yes',
  284. '/label',
  285. ['label' => ['for' => 'model-field-0']],
  286. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0', 'checked' => 'checked']],
  287. 'No',
  288. '/label',
  289. ];
  290. $this->assertHtml($expected, $result);
  291. $data['val'] = 0;
  292. $result = $radio->render($data, $this->context);
  293. $this->assertHtml($expected, $result);
  294. $data['val'] = false;
  295. $result = $radio->render($data, $this->context);
  296. $this->assertHtml($expected, $result);
  297. $expected = [
  298. ['label' => ['for' => 'model-field-1']],
  299. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  300. 'Yes',
  301. '/label',
  302. ['label' => ['for' => 'model-field-0']],
  303. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  304. 'No',
  305. '/label',
  306. ];
  307. $data['val'] = null;
  308. $result = $radio->render($data, $this->context);
  309. $this->assertHtml($expected, $result);
  310. $data['val'] = '';
  311. $result = $radio->render($data, $this->context);
  312. $this->assertHtml($expected, $result);
  313. $expected = [
  314. ['label' => ['for' => 'model-field-1']],
  315. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1', 'checked' => 'checked']],
  316. 'Yes',
  317. '/label',
  318. ['label' => ['for' => 'model-field-0']],
  319. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  320. 'No',
  321. '/label',
  322. ];
  323. $data['val'] = '1';
  324. $result = $radio->render($data, $this->context);
  325. $this->assertHtml($expected, $result);
  326. $data['val'] = 1;
  327. $result = $radio->render($data, $this->context);
  328. $this->assertHtml($expected, $result);
  329. $data['val'] = true;
  330. $result = $radio->render($data, $this->context);
  331. $this->assertHtml($expected, $result);
  332. }
  333. /**
  334. * Test that render() works with the required attribute.
  335. *
  336. * @return void
  337. */
  338. public function testRenderRequiredAndFormAttribute()
  339. {
  340. $label = new NestingLabelWidget($this->templates);
  341. $radio = new RadioWidget($this->templates, $label);
  342. $data = [
  343. 'name' => 'published',
  344. 'options' => ['option A', 'option B'],
  345. 'required' => true,
  346. 'form' => 'my-form',
  347. ];
  348. $result = $radio->render($data, $this->context);
  349. $expected = [
  350. ['label' => ['for' => 'published-0']],
  351. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => '0',
  352. 'id' => 'published-0', 'required' => 'required', 'form' => 'my-form']],
  353. 'option A',
  354. '/label',
  355. ['label' => ['for' => 'published-1']],
  356. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => '1',
  357. 'id' => 'published-1', 'required' => 'required', 'form' => 'my-form']],
  358. 'option B',
  359. '/label',
  360. ];
  361. $this->assertHtml($expected, $result);
  362. }
  363. /**
  364. * Test rendering the empty option.
  365. *
  366. * @return void
  367. */
  368. public function testRenderEmptyOption()
  369. {
  370. $label = new NestingLabelWidget($this->templates);
  371. $radio = new RadioWidget($this->templates, $label);
  372. $data = [
  373. 'name' => 'Crayons[color]',
  374. 'options' => ['r' => 'Red'],
  375. 'empty' => true,
  376. ];
  377. $result = $radio->render($data, $this->context);
  378. $expected = [
  379. ['label' => ['for' => 'crayons-color']],
  380. ['input' => [
  381. 'type' => 'radio',
  382. 'name' => 'Crayons[color]',
  383. 'value' => '',
  384. 'id' => 'crayons-color'
  385. ]],
  386. 'empty',
  387. '/label',
  388. ['label' => ['for' => 'crayons-color-r']],
  389. ['input' => [
  390. 'type' => 'radio',
  391. 'name' => 'Crayons[color]',
  392. 'value' => 'r',
  393. 'id' => 'crayons-color-r'
  394. ]],
  395. 'Red',
  396. '/label',
  397. ];
  398. $this->assertHtml($expected, $result);
  399. $data['empty'] = 'Choose one';
  400. $result = $radio->render($data, $this->context);
  401. $expected = [
  402. ['label' => ['for' => 'crayons-color']],
  403. ['input' => [
  404. 'type' => 'radio',
  405. 'name' => 'Crayons[color]',
  406. 'value' => '',
  407. 'id' => 'crayons-color'
  408. ]],
  409. 'Choose one',
  410. '/label',
  411. ['label' => ['for' => 'crayons-color-r']],
  412. ['input' => [
  413. 'type' => 'radio',
  414. 'name' => 'Crayons[color]',
  415. 'value' => 'r',
  416. 'id' => 'crayons-color-r'
  417. ]],
  418. 'Red',
  419. '/label',
  420. ];
  421. $this->assertHtml($expected, $result);
  422. }
  423. /**
  424. * Test rendering the input inside the label.
  425. *
  426. * @return void
  427. */
  428. public function testRenderInputInsideLabel()
  429. {
  430. $this->templates->add([
  431. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  432. 'radioWrapper' => '{{label}}',
  433. ]);
  434. $label = new NestingLabelWidget($this->templates);
  435. $radio = new RadioWidget($this->templates, $label);
  436. $data = [
  437. 'name' => 'Crayons[color]',
  438. 'options' => ['r' => 'Red'],
  439. ];
  440. $result = $radio->render($data, $this->context);
  441. $expected = [
  442. ['label' => ['for' => 'crayons-color-r']],
  443. ['input' => [
  444. 'type' => 'radio',
  445. 'name' => 'Crayons[color]',
  446. 'value' => 'r',
  447. 'id' => 'crayons-color-r'
  448. ]],
  449. 'Red',
  450. '/label',
  451. ];
  452. $this->assertHtml($expected, $result);
  453. }
  454. /**
  455. * test render() and selected inputs.
  456. *
  457. * @return void
  458. */
  459. public function testRenderSelected()
  460. {
  461. $label = new NestingLabelWidget($this->templates);
  462. $radio = new RadioWidget($this->templates, $label);
  463. $data = [
  464. 'name' => 'Versions[ver]',
  465. 'val' => '1',
  466. 'options' => [
  467. 1 => 'one',
  468. '1x' => 'one x',
  469. '2' => 'two',
  470. ]
  471. ];
  472. $result = $radio->render($data, $this->context);
  473. $expected = [
  474. ['label' => ['for' => 'versions-ver-1']],
  475. ['input' => [
  476. 'id' => 'versions-ver-1',
  477. 'name' => 'Versions[ver]',
  478. 'type' => 'radio',
  479. 'value' => '1',
  480. 'checked' => 'checked'
  481. ]],
  482. 'one',
  483. '/label',
  484. ['label' => ['for' => 'versions-ver-1x']],
  485. ['input' => [
  486. 'id' => 'versions-ver-1x',
  487. 'name' => 'Versions[ver]',
  488. 'type' => 'radio',
  489. 'value' => '1x'
  490. ]],
  491. 'one x',
  492. '/label',
  493. ['label' => ['for' => 'versions-ver-2']],
  494. ['input' => [
  495. 'id' => 'versions-ver-2',
  496. 'name' => 'Versions[ver]',
  497. 'type' => 'radio',
  498. 'value' => '2'
  499. ]],
  500. 'two',
  501. '/label',
  502. ];
  503. $this->assertHtml($expected, $result);
  504. }
  505. /**
  506. * Test rendering with disable inputs
  507. *
  508. * @return void
  509. */
  510. public function testRenderDisabled()
  511. {
  512. $label = new NestingLabelWidget($this->templates);
  513. $radio = new RadioWidget($this->templates, $label);
  514. $data = [
  515. 'name' => 'Versions[ver]',
  516. 'options' => [
  517. 1 => 'one',
  518. '1x' => 'one x',
  519. '2' => 'two',
  520. ],
  521. 'disabled' => true,
  522. ];
  523. $result = $radio->render($data, $this->context);
  524. $expected = [
  525. ['label' => ['for' => 'versions-ver-1']],
  526. ['input' => [
  527. 'id' => 'versions-ver-1',
  528. 'name' => 'Versions[ver]',
  529. 'type' => 'radio',
  530. 'value' => '1',
  531. 'disabled' => 'disabled'
  532. ]],
  533. 'one',
  534. '/label',
  535. ['label' => ['for' => 'versions-ver-1x']],
  536. ['input' => [
  537. 'id' => 'versions-ver-1x',
  538. 'name' => 'Versions[ver]',
  539. 'type' => 'radio',
  540. 'value' => '1x',
  541. 'disabled' => 'disabled'
  542. ]],
  543. 'one x',
  544. '/label',
  545. ];
  546. $this->assertHtml($expected, $result);
  547. $data['disabled'] = 'a string';
  548. $result = $radio->render($data, $this->context);
  549. $this->assertHtml($expected, $result);
  550. $data['disabled'] = ['1'];
  551. $result = $radio->render($data, $this->context);
  552. $expected = [
  553. ['label' => ['for' => 'versions-ver-1']],
  554. ['input' => [
  555. 'id' => 'versions-ver-1',
  556. 'name' => 'Versions[ver]',
  557. 'type' => 'radio',
  558. 'value' => '1',
  559. 'disabled' => 'disabled'
  560. ]],
  561. 'one',
  562. '/label',
  563. ['label' => ['for' => 'versions-ver-1x']],
  564. ['input' => [
  565. 'id' => 'versions-ver-1x',
  566. 'name' => 'Versions[ver]',
  567. 'type' => 'radio',
  568. 'value' => '1x',
  569. ]],
  570. 'one x',
  571. '/label',
  572. ];
  573. $this->assertHtml($expected, $result);
  574. }
  575. /**
  576. * Test rendering with label options.
  577. *
  578. * @return void
  579. */
  580. public function testRenderLabelOptions()
  581. {
  582. $label = new NestingLabelWidget($this->templates);
  583. $radio = new RadioWidget($this->templates, $label);
  584. $data = [
  585. 'name' => 'Versions[ver]',
  586. 'options' => [
  587. 1 => 'one',
  588. '1x' => 'one x',
  589. '2' => 'two',
  590. ],
  591. 'label' => false,
  592. ];
  593. $result = $radio->render($data, $this->context);
  594. $expected = [
  595. ['input' => [
  596. 'id' => 'versions-ver-1',
  597. 'name' => 'Versions[ver]',
  598. 'type' => 'radio',
  599. 'value' => '1',
  600. ]],
  601. ['input' => [
  602. 'id' => 'versions-ver-1x',
  603. 'name' => 'Versions[ver]',
  604. 'type' => 'radio',
  605. 'value' => '1x',
  606. ]],
  607. ];
  608. $this->assertHtml($expected, $result);
  609. $data = [
  610. 'name' => 'Versions[ver]',
  611. 'options' => [
  612. 1 => 'one',
  613. '1x' => 'one x',
  614. '2' => 'two',
  615. ],
  616. 'label' => [
  617. 'class' => 'my-class',
  618. ]
  619. ];
  620. $result = $radio->render($data, $this->context);
  621. $expected = [
  622. ['label' => ['class' => 'my-class', 'for' => 'versions-ver-1']],
  623. ['input' => [
  624. 'id' => 'versions-ver-1',
  625. 'name' => 'Versions[ver]',
  626. 'type' => 'radio',
  627. 'value' => '1',
  628. ]],
  629. 'one',
  630. '/label',
  631. ['label' => ['class' => 'my-class', 'for' => 'versions-ver-1x']],
  632. ['input' => [
  633. 'id' => 'versions-ver-1x',
  634. 'name' => 'Versions[ver]',
  635. 'type' => 'radio',
  636. 'value' => '1x',
  637. ]],
  638. 'one x',
  639. '/label',
  640. ];
  641. $this->assertHtml($expected, $result);
  642. }
  643. /**
  644. * Ensure that the input + label are composed with
  645. * a template.
  646. *
  647. * @return void
  648. */
  649. public function testRenderContainerTemplate()
  650. {
  651. $this->templates->add([
  652. 'radioWrapper' => '<div class="radio">{{input}}{{label}}</div>'
  653. ]);
  654. $label = new NestingLabelWidget($this->templates);
  655. $radio = new RadioWidget($this->templates, $label);
  656. $data = [
  657. 'name' => 'Versions[ver]',
  658. 'options' => [
  659. 1 => 'one',
  660. '1x' => 'one x',
  661. '2' => 'two',
  662. ],
  663. ];
  664. $result = $radio->render($data, $this->context);
  665. $this->assertContains(
  666. '<div class="radio"><input type="radio"',
  667. $result
  668. );
  669. $this->assertContains(
  670. '</label></div>',
  671. $result
  672. );
  673. }
  674. /**
  675. * Ensure that template vars work.
  676. *
  677. * @return void
  678. */
  679. public function testRenderTemplateVars()
  680. {
  681. $this->templates->add([
  682. 'radioWrapper' => '<div class="radio" data-var="{{wrapperVar}}">{{label}}</div>',
  683. 'radio' => '<input type="radio" data-i="{{inputVar}}" name="{{name}}" value="{{value}}"{{attrs}}>',
  684. 'nestingLabel' => '<label{{attrs}}>{{input}}{{text}} {{labelVar}} {{wrapperVar}}</label>',
  685. ]);
  686. $label = new NestingLabelWidget($this->templates);
  687. $radio = new RadioWidget($this->templates, $label);
  688. $data = [
  689. 'name' => 'Versions[ver]',
  690. 'options' => [
  691. ['value' => '1x', 'text' => 'one x', 'templateVars' => ['labelVar' => 'l-var', 'inputVar' => 'i-var']],
  692. '2' => 'two',
  693. ],
  694. 'templateVars' => [
  695. 'wrapperVar' => 'wrap-var',
  696. ]
  697. ];
  698. $result = $radio->render($data, $this->context);
  699. $this->assertContains('data-var="wrap-var"><label', $result);
  700. $this->assertContains('type="radio" data-i="i-var"', $result);
  701. $this->assertContains('one x l-var wrap-var</label>', $result);
  702. $this->assertContains('two wrap-var</label>', $result);
  703. }
  704. /**
  705. * testRenderCustomAttributes method
  706. *
  707. * Test render with custom attributes.
  708. *
  709. * @return void
  710. */
  711. public function testRenderCustomAttributes()
  712. {
  713. $label = new NestingLabelWidget($this->templates);
  714. $radio = new RadioWidget($this->templates, $label);
  715. $result = $radio->render([
  716. 'name' => 'Model[field]',
  717. 'label' => null,
  718. 'options' => ['option A', 'option B'],
  719. 'class' => 'my-class',
  720. 'data-ref' => 'custom-attr'
  721. ], $this->context);
  722. $expected = [
  723. ['label' => ['for' => 'model-field-0']],
  724. [
  725. 'input' => [
  726. 'type' => 'radio',
  727. 'name' => 'Model[field]',
  728. 'value' => '0',
  729. 'id' => 'model-field-0',
  730. 'class' => 'my-class',
  731. 'data-ref' => 'custom-attr'
  732. ]
  733. ],
  734. 'option A',
  735. '/label',
  736. ['label' => ['for' => 'model-field-1']],
  737. [
  738. 'input' => [
  739. 'type' => 'radio',
  740. 'name' => 'Model[field]',
  741. 'value' => '1',
  742. 'id' => 'model-field-1',
  743. 'class' => 'my-class',
  744. 'data-ref' => 'custom-attr'
  745. ]
  746. ],
  747. 'option B',
  748. '/label'
  749. ];
  750. $this->assertHtml($expected, $result);
  751. }
  752. }