MultiCheckboxWidgetTest.php 23 KB

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