MultiCheckboxWidgetTest.php 23 KB

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