SelectBoxWidgetTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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\SelectBoxWidget;
  20. /**
  21. * SelectBox test case
  22. */
  23. class SelectBoxWidgetTest extends TestCase
  24. {
  25. /**
  26. * setup method.
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. $templates = [
  34. 'select' => '<select name="{{name}}"{{attrs}}>{{content}}</select>',
  35. 'selectMultiple' => '<select name="{{name}}[]" multiple="multiple"{{attrs}}>{{content}}</select>',
  36. 'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>',
  37. 'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
  38. ];
  39. $this->context = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  40. $this->templates = new StringTemplate($templates);
  41. }
  42. /**
  43. * test render no options
  44. *
  45. * @return void
  46. */
  47. public function testRenderNoOptions()
  48. {
  49. $select = new SelectBoxWidget($this->templates);
  50. $data = [
  51. 'id' => 'BirdName',
  52. 'name' => 'Birds[name]',
  53. 'options' => []
  54. ];
  55. $result = $select->render($data, $this->context);
  56. $expected = [
  57. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  58. '/select'
  59. ];
  60. $this->assertHtml($expected, $result);
  61. }
  62. /**
  63. * test simple rendering
  64. *
  65. * @return void
  66. */
  67. public function testRenderSimple()
  68. {
  69. $select = new SelectBoxWidget($this->templates);
  70. $data = [
  71. 'id' => 'BirdName',
  72. 'name' => 'Birds[name]',
  73. 'options' => ['a' => 'Albatross', 'b' => 'Budgie']
  74. ];
  75. $result = $select->render($data, $this->context);
  76. $expected = [
  77. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  78. ['option' => ['value' => 'a']], 'Albatross', '/option',
  79. ['option' => ['value' => 'b']], 'Budgie', '/option',
  80. '/select'
  81. ];
  82. $this->assertHtml($expected, $result);
  83. }
  84. /**
  85. * Test render boolean options
  86. *
  87. * @return void
  88. */
  89. public function testRenderBoolean()
  90. {
  91. $select = new SelectBoxWidget($this->templates);
  92. $data = [
  93. 'id' => 'enabled',
  94. 'name' => 'enabled',
  95. 'options' => [0 => 'No', 1 => 'Yes'],
  96. 'val' => false
  97. ];
  98. $result = $select->render($data, $this->context);
  99. $this->assertContains('<option value="0" selected="selected">No</option>', $result);
  100. $data['value'] = [false, 2];
  101. $result = $select->render($data, $this->context);
  102. $this->assertContains('<option value="0" selected="selected">No</option>', $result);
  103. }
  104. /**
  105. * test simple iterator rendering
  106. *
  107. * @return void
  108. */
  109. public function testRenderSimpleIterator()
  110. {
  111. $select = new SelectBoxWidget($this->templates);
  112. $options = new \ArrayObject(['a' => 'Albatross', 'b' => 'Budgie']);
  113. $data = [
  114. 'name' => 'Birds[name]',
  115. 'options' => $options,
  116. 'empty' => true
  117. ];
  118. $result = $select->render($data, $this->context);
  119. $expected = [
  120. 'select' => ['name' => 'Birds[name]'],
  121. ['option' => ['value' => '']], '/option',
  122. ['option' => ['value' => 'a']], 'Albatross', '/option',
  123. ['option' => ['value' => 'b']], 'Budgie', '/option',
  124. '/select'
  125. ];
  126. $this->assertHtml($expected, $result);
  127. }
  128. /**
  129. * test simple iterator rendering with empty option
  130. *
  131. * @return void
  132. */
  133. public function testRenderSimpleIteratorWithEmpty()
  134. {
  135. $select = new SelectBoxWidget($this->templates);
  136. $options = new Collection(['a' => 'Albatross', 'b' => 'Budgie']);
  137. $data = [
  138. 'name' => 'Birds[name]',
  139. 'options' => $options,
  140. 'empty' => 'Pick one'
  141. ];
  142. $result = $select->render($data, $this->context);
  143. $expected = [
  144. 'select' => ['name' => 'Birds[name]'],
  145. ['option' => ['value' => '']], 'Pick one', '/option',
  146. ['option' => ['value' => 'a']], 'Albatross', '/option',
  147. ['option' => ['value' => 'b']], 'Budgie', '/option',
  148. '/select'
  149. ];
  150. $this->assertHtml($expected, $result);
  151. }
  152. /**
  153. * test complex option rendering
  154. *
  155. * @return void
  156. */
  157. public function testRenderComplex()
  158. {
  159. $select = new SelectBoxWidget($this->templates);
  160. $data = [
  161. 'id' => 'BirdName',
  162. 'name' => 'Birds[name]',
  163. 'options' => [
  164. ['value' => 'a', 'text' => 'Albatross'],
  165. ['value' => 'b', 'text' => 'Budgie', 'data-foo' => 'bar'],
  166. ]
  167. ];
  168. $result = $select->render($data, $this->context);
  169. $expected = [
  170. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  171. ['option' => ['value' => 'a']],
  172. 'Albatross',
  173. '/option',
  174. ['option' => ['value' => 'b', 'data-foo' => 'bar']],
  175. 'Budgie',
  176. '/option',
  177. '/select'
  178. ];
  179. $this->assertHtml($expected, $result);
  180. }
  181. /**
  182. * test rendering with a selected value
  183. *
  184. * @return void
  185. */
  186. public function testRenderSelected()
  187. {
  188. $select = new SelectBoxWidget($this->templates);
  189. $data = [
  190. 'id' => 'BirdName',
  191. 'name' => 'Birds[name]',
  192. 'val' => '1',
  193. 'options' => [
  194. 1 => 'one',
  195. '1x' => 'one x',
  196. '2' => 'two',
  197. '2x' => 'two x',
  198. ]
  199. ];
  200. $result = $select->render($data, $this->context);
  201. $expected = [
  202. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  203. ['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
  204. ['option' => ['value' => '1x']], 'one x', '/option',
  205. ['option' => ['value' => '2']], 'two', '/option',
  206. ['option' => ['value' => '2x']], 'two x', '/option',
  207. '/select'
  208. ];
  209. $this->assertHtml($expected, $result);
  210. $data['val'] = 2;
  211. $result = $select->render($data, $this->context);
  212. $expected = [
  213. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  214. ['option' => ['value' => '1']], 'one', '/option',
  215. ['option' => ['value' => '1x']], 'one x', '/option',
  216. ['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
  217. ['option' => ['value' => '2x']], 'two x', '/option',
  218. '/select'
  219. ];
  220. $this->assertHtml($expected, $result);
  221. }
  222. /**
  223. * test complex option rendering with a selected value
  224. *
  225. * @return void
  226. */
  227. public function testRenderComplexSelected()
  228. {
  229. $select = new SelectBoxWidget($this->templates);
  230. $data = [
  231. 'id' => 'BirdName',
  232. 'name' => 'Birds[name]',
  233. 'val' => 'a',
  234. 'options' => [
  235. ['value' => 'a', 'text' => 'Albatross'],
  236. ['value' => 'b', 'text' => 'Budgie', 'data-foo' => 'bar'],
  237. ]
  238. ];
  239. $result = $select->render($data, $this->context);
  240. $expected = [
  241. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  242. ['option' => ['value' => 'a', 'selected' => 'selected']],
  243. 'Albatross',
  244. '/option',
  245. ['option' => ['value' => 'b', 'data-foo' => 'bar']],
  246. 'Budgie',
  247. '/option',
  248. '/select'
  249. ];
  250. $this->assertHtml($expected, $result);
  251. }
  252. /**
  253. * test rendering a multi select
  254. *
  255. * @return void
  256. */
  257. public function testRenderMultipleSelect()
  258. {
  259. $select = new SelectBoxWidget($this->templates);
  260. $data = [
  261. 'id' => 'BirdName',
  262. 'name' => 'Birds[name]',
  263. 'multiple' => true,
  264. 'options' => ['a' => 'Albatross', 'b' => 'Budgie']
  265. ];
  266. $result = $select->render($data, $this->context);
  267. $expected = [
  268. 'select' => [
  269. 'name' => 'Birds[name][]',
  270. 'id' => 'BirdName',
  271. 'multiple' => 'multiple',
  272. ],
  273. ['option' => ['value' => 'a']], 'Albatross', '/option',
  274. ['option' => ['value' => 'b']], 'Budgie', '/option',
  275. '/select'
  276. ];
  277. $this->assertHtml($expected, $result);
  278. }
  279. /**
  280. * test rendering multi select & selected values
  281. *
  282. * @return void
  283. */
  284. public function testRenderMultipleSelected()
  285. {
  286. $select = new SelectBoxWidget($this->templates);
  287. $data = [
  288. 'multiple' => true,
  289. 'id' => 'BirdName',
  290. 'name' => 'Birds[name]',
  291. 'val' => ['1', '2', 'burp'],
  292. 'options' => [
  293. 1 => 'one',
  294. '1x' => 'one x',
  295. '2' => 'two',
  296. '2x' => 'two x',
  297. ]
  298. ];
  299. $result = $select->render($data, $this->context);
  300. $expected = [
  301. 'select' => [
  302. 'name' => 'Birds[name][]',
  303. 'multiple' => 'multiple',
  304. 'id' => 'BirdName'
  305. ],
  306. ['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
  307. ['option' => ['value' => '1x']], 'one x', '/option',
  308. ['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
  309. ['option' => ['value' => '2x']], 'two x', '/option',
  310. '/select'
  311. ];
  312. $this->assertHtml($expected, $result);
  313. }
  314. /**
  315. * test rendering with option groups
  316. *
  317. * @return void
  318. */
  319. public function testRenderOptionGroups()
  320. {
  321. $select = new SelectBoxWidget($this->templates);
  322. $data = [
  323. 'name' => 'Birds[name]',
  324. 'options' => [
  325. 'Mammal' => [
  326. 'beaver' => 'Beaver',
  327. 'elk' => 'Elk',
  328. ],
  329. 'Bird' => [
  330. 'budgie' => 'Budgie',
  331. 'eagle' => 'Eagle',
  332. ]
  333. ]
  334. ];
  335. $result = $select->render($data, $this->context);
  336. $expected = [
  337. 'select' => [
  338. 'name' => 'Birds[name]',
  339. ],
  340. ['optgroup' => ['label' => 'Mammal']],
  341. ['option' => ['value' => 'beaver']],
  342. 'Beaver',
  343. '/option',
  344. ['option' => ['value' => 'elk']],
  345. 'Elk',
  346. '/option',
  347. '/optgroup',
  348. ['optgroup' => ['label' => 'Bird']],
  349. ['option' => ['value' => 'budgie']],
  350. 'Budgie',
  351. '/option',
  352. ['option' => ['value' => 'eagle']],
  353. 'Eagle',
  354. '/option',
  355. '/optgroup',
  356. '/select'
  357. ];
  358. $this->assertHtml($expected, $result);
  359. }
  360. /**
  361. * test rendering with numeric option group keys
  362. *
  363. * @return void
  364. */
  365. public function testRenderOptionGroupsIntegerKeys()
  366. {
  367. $select = new SelectBoxWidget($this->templates);
  368. $data = [
  369. 'name' => 'Year[key]',
  370. 'options' => [
  371. 2014 => [
  372. 'key' => 'value'
  373. ],
  374. 2013 => [
  375. 'text' => '2013-text',
  376. 'options' => [
  377. 'key2' => 'value2'
  378. ]
  379. ]
  380. ]
  381. ];
  382. $result = $select->render($data, $this->context);
  383. $expected = [
  384. 'select' => [
  385. 'name' => 'Year[key]',
  386. ],
  387. ['optgroup' => ['label' => '2014']],
  388. ['option' => ['value' => 'key']],
  389. 'value',
  390. '/option',
  391. '/optgroup',
  392. ['optgroup' => ['label' => '2013-text']],
  393. ['option' => ['value' => 'key2']],
  394. 'value2',
  395. '/option',
  396. '/optgroup',
  397. '/select'
  398. ];
  399. $this->assertHtml($expected, $result);
  400. }
  401. /**
  402. * test rendering with option groups and escaping
  403. *
  404. * @return void
  405. */
  406. public function testRenderOptionGroupsEscape()
  407. {
  408. $select = new SelectBoxWidget($this->templates);
  409. $data = [
  410. 'name' => 'Birds[name]',
  411. 'options' => [
  412. '>XSS<' => [
  413. '1' => 'One>',
  414. ],
  415. ]
  416. ];
  417. $result = $select->render($data, $this->context);
  418. $expected = [
  419. 'select' => [
  420. 'name' => 'Birds[name]',
  421. ],
  422. ['optgroup' => ['label' => '&gt;XSS&lt;']],
  423. ['option' => ['value' => '1']],
  424. 'One&gt;',
  425. '/option',
  426. '/optgroup',
  427. '/select'
  428. ];
  429. $this->assertHtml($expected, $result);
  430. $data['escape'] = false;
  431. $result = $select->render($data, $this->context);
  432. $expected = [
  433. 'select' => [
  434. 'name' => 'Birds[name]',
  435. ],
  436. ['optgroup' => ['label' => '>XSS<']],
  437. ['option' => ['value' => '1']],
  438. 'One>',
  439. '/option',
  440. '/optgroup',
  441. '/select'
  442. ];
  443. $this->assertHtml($expected, $result);
  444. }
  445. /**
  446. * test rendering with option groups
  447. *
  448. * @return void
  449. */
  450. public function testRenderOptionGroupsWithAttributes()
  451. {
  452. $select = new SelectBoxWidget($this->templates);
  453. $data = [
  454. 'name' => 'Birds[name]',
  455. 'options' => [
  456. [
  457. 'text' => 'Mammal',
  458. 'data-foo' => 'bar',
  459. 'options' => [
  460. 'beaver' => 'Beaver',
  461. 'elk' => 'Elk',
  462. ]
  463. ]
  464. ]
  465. ];
  466. $result = $select->render($data, $this->context);
  467. $expected = [
  468. 'select' => [
  469. 'name' => 'Birds[name]',
  470. ],
  471. ['optgroup' => ['data-foo' => 'bar', 'label' => 'Mammal']],
  472. ['option' => ['value' => 'beaver']],
  473. 'Beaver',
  474. '/option',
  475. ['option' => ['value' => 'elk']],
  476. 'Elk',
  477. '/option',
  478. '/optgroup',
  479. '/select'
  480. ];
  481. $this->assertHtml($expected, $result);
  482. }
  483. /**
  484. * test rendering with option groups with traversable nodes
  485. *
  486. * @return void
  487. */
  488. public function testRenderOptionGroupsTraversable()
  489. {
  490. $select = new SelectBoxWidget($this->templates);
  491. $mammals = new \ArrayObject(['beaver' => 'Beaver', 'elk' => 'Elk']);
  492. $data = [
  493. 'name' => 'Birds[name]',
  494. 'options' => [
  495. 'Mammal' => $mammals,
  496. 'Bird' => [
  497. 'budgie' => 'Budgie',
  498. 'eagle' => 'Eagle',
  499. ]
  500. ]
  501. ];
  502. $result = $select->render($data, $this->context);
  503. $expected = [
  504. 'select' => [
  505. 'name' => 'Birds[name]',
  506. ],
  507. ['optgroup' => ['label' => 'Mammal']],
  508. ['option' => ['value' => 'beaver']],
  509. 'Beaver',
  510. '/option',
  511. ['option' => ['value' => 'elk']],
  512. 'Elk',
  513. '/option',
  514. '/optgroup',
  515. ['optgroup' => ['label' => 'Bird']],
  516. ['option' => ['value' => 'budgie']],
  517. 'Budgie',
  518. '/option',
  519. ['option' => ['value' => 'eagle']],
  520. 'Eagle',
  521. '/option',
  522. '/optgroup',
  523. '/select'
  524. ];
  525. $this->assertHtml($expected, $result);
  526. }
  527. /**
  528. * test rendering option groups and selected values
  529. *
  530. * @return void
  531. */
  532. public function testRenderOptionGroupsSelectedAndDisabled()
  533. {
  534. $select = new SelectBoxWidget($this->templates);
  535. $data = [
  536. 'name' => 'Birds[name]',
  537. 'val' => ['1', '2', 'burp'],
  538. 'disabled' => ['1x', '2x', 'nope'],
  539. 'options' => [
  540. 'ones' => [
  541. 1 => 'one',
  542. '1x' => 'one x',
  543. ],
  544. 'twos' => [
  545. '2' => 'two',
  546. '2x' => 'two x',
  547. ]
  548. ]
  549. ];
  550. $result = $select->render($data, $this->context);
  551. $expected = [
  552. 'select' => [
  553. 'name' => 'Birds[name]',
  554. ],
  555. ['optgroup' => ['label' => 'ones']],
  556. ['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
  557. ['option' => ['value' => '1x', 'disabled' => 'disabled']], 'one x', '/option',
  558. '/optgroup',
  559. ['optgroup' => ['label' => 'twos']],
  560. ['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
  561. ['option' => ['value' => '2x', 'disabled' => 'disabled']], 'two x', '/option',
  562. '/optgroup',
  563. '/select'
  564. ];
  565. $this->assertHtml($expected, $result);
  566. }
  567. /**
  568. * test rendering a totally disabled element
  569. *
  570. * @return void
  571. */
  572. public function testRenderDisabled()
  573. {
  574. $select = new SelectBoxWidget($this->templates);
  575. $data = [
  576. 'disabled' => true,
  577. 'name' => 'Birds[name]',
  578. 'options' => ['a' => 'Albatross', 'b' => 'Budgie'],
  579. 'val' => 'a',
  580. ];
  581. $result = $select->render($data, $this->context);
  582. $expected = [
  583. 'select' => [
  584. 'name' => 'Birds[name]',
  585. 'disabled' => 'disabled',
  586. ],
  587. ['option' => ['value' => 'a', 'selected' => 'selected']], 'Albatross', '/option',
  588. ['option' => ['value' => 'b']], 'Budgie', '/option',
  589. '/select'
  590. ];
  591. $this->assertHtml($expected, $result);
  592. $select = new SelectBoxWidget($this->templates);
  593. $data = [
  594. 'disabled' => [1],
  595. 'name' => 'numbers',
  596. 'options' => ['1' => 'One', '2' => 'Two'],
  597. ];
  598. $result = $select->render($data, $this->context);
  599. $expected = [
  600. 'select' => [
  601. 'name' => 'numbers',
  602. ],
  603. ['option' => ['value' => '1', 'disabled' => 'disabled']], 'One', '/option',
  604. ['option' => ['value' => '2']], 'Two', '/option',
  605. '/select'
  606. ];
  607. $this->assertHtml($expected, $result);
  608. }
  609. /**
  610. * test rendering a disabled element
  611. *
  612. * @return void
  613. */
  614. public function testRenderDisabledMultiple()
  615. {
  616. $select = new SelectBoxWidget($this->templates);
  617. $data = [
  618. 'disabled' => ['a', 'c'],
  619. 'val' => 'a',
  620. 'name' => 'Birds[name]',
  621. 'options' => [
  622. 'a' => 'Albatross',
  623. 'b' => 'Budgie',
  624. 'c' => 'Canary',
  625. ]
  626. ];
  627. $result = $select->render($data, $this->context);
  628. $expected = [
  629. 'select' => [
  630. 'name' => 'Birds[name]',
  631. ],
  632. ['option' => ['value' => 'a', 'selected' => 'selected', 'disabled' => 'disabled']],
  633. 'Albatross',
  634. '/option',
  635. ['option' => ['value' => 'b']],
  636. 'Budgie',
  637. '/option',
  638. ['option' => ['value' => 'c', 'disabled' => 'disabled']],
  639. 'Canary',
  640. '/option',
  641. '/select'
  642. ];
  643. $this->assertHtml($expected, $result);
  644. }
  645. /**
  646. * test complex option rendering with a disabled element
  647. *
  648. * @return void
  649. */
  650. public function testRenderComplexDisabled()
  651. {
  652. $select = new SelectBoxWidget($this->templates);
  653. $data = [
  654. 'disabled' => ['b'],
  655. 'id' => 'BirdName',
  656. 'name' => 'Birds[name]',
  657. 'options' => [
  658. ['value' => 'a', 'text' => 'Albatross'],
  659. ['value' => 'b', 'text' => 'Budgie', 'data-foo' => 'bar'],
  660. ]
  661. ];
  662. $result = $select->render($data, $this->context);
  663. $expected = [
  664. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  665. ['option' => ['value' => 'a']],
  666. 'Albatross',
  667. '/option',
  668. ['option' => ['value' => 'b', 'data-foo' => 'bar', 'disabled' => 'disabled']],
  669. 'Budgie',
  670. '/option',
  671. '/select'
  672. ];
  673. $this->assertHtml($expected, $result);
  674. }
  675. /**
  676. * test rendering with an empty value
  677. *
  678. * @return void
  679. */
  680. public function testRenderEmptyOption()
  681. {
  682. $select = new SelectBoxWidget($this->templates);
  683. $data = [
  684. 'id' => 'BirdName',
  685. 'name' => 'Birds[name]',
  686. 'empty' => true,
  687. 'options' => ['a' => 'Albatross', 'b' => 'Budgie']
  688. ];
  689. $result = $select->render($data, $this->context);
  690. $expected = [
  691. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  692. ['option' => ['value' => '']], '/option',
  693. ['option' => ['value' => 'a']], 'Albatross', '/option',
  694. ['option' => ['value' => 'b']], 'Budgie', '/option',
  695. '/select'
  696. ];
  697. $this->assertHtml($expected, $result);
  698. $data['empty'] = 'empty';
  699. $result = $select->render($data, $this->context);
  700. $expected = [
  701. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  702. ['option' => ['value' => '']], 'empty', '/option',
  703. ['option' => ['value' => 'a']], 'Albatross', '/option',
  704. ['option' => ['value' => 'b']], 'Budgie', '/option',
  705. '/select'
  706. ];
  707. $this->assertHtml($expected, $result);
  708. $data['empty'] = ['99' => '(choose one)'];
  709. $result = $select->render($data, $this->context);
  710. $expected = [
  711. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  712. ['option' => ['value' => '99']], '(choose one)', '/option',
  713. ['option' => ['value' => 'a']], 'Albatross', '/option',
  714. ['option' => ['value' => 'b']], 'Budgie', '/option',
  715. '/select'
  716. ];
  717. $this->assertHtml($expected, $result);
  718. $data['empty'] = 'empty';
  719. $data['val'] = '';
  720. $result = $select->render($data, $this->context);
  721. $expected = [
  722. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  723. ['option' => ['value' => '', 'selected' => 'selected']], 'empty', '/option',
  724. ['option' => ['value' => 'a']], 'Albatross', '/option',
  725. ['option' => ['value' => 'b']], 'Budgie', '/option',
  726. '/select'
  727. ];
  728. $this->assertHtml($expected, $result);
  729. }
  730. /**
  731. * Test rendering with disabling escaping.
  732. *
  733. * @return void
  734. */
  735. public function testRenderEscapingOption()
  736. {
  737. $select = new SelectBoxWidget($this->templates);
  738. $data = [
  739. 'name' => 'Birds[name]',
  740. 'options' => [
  741. 'a' => '>Albatross',
  742. 'b' => '>Budgie',
  743. 'c' => '>Canary',
  744. ]
  745. ];
  746. $result = $select->render($data, $this->context);
  747. $expected = [
  748. 'select' => [
  749. 'name' => 'Birds[name]',
  750. ],
  751. ['option' => ['value' => 'a']],
  752. '&gt;Albatross',
  753. '/option',
  754. ['option' => ['value' => 'b']],
  755. '&gt;Budgie',
  756. '/option',
  757. ['option' => ['value' => 'c']],
  758. '&gt;Canary',
  759. '/option',
  760. '/select'
  761. ];
  762. $this->assertHtml($expected, $result);
  763. $data = [
  764. 'escape' => false,
  765. 'name' => 'Birds[name]',
  766. 'options' => [
  767. '>a' => '>Albatross',
  768. ]
  769. ];
  770. $result = $select->render($data, $this->context);
  771. $expected = [
  772. 'select' => [
  773. 'name' => 'Birds[name]',
  774. ],
  775. ['option' => ['value' => '>a']],
  776. '>Albatross',
  777. '/option',
  778. '/select'
  779. ];
  780. $this->assertHtml($expected, $result);
  781. }
  782. /**
  783. * test render with null options
  784. *
  785. * @return void
  786. */
  787. public function testRenderNullOptions()
  788. {
  789. $select = new SelectBoxWidget($this->templates);
  790. $data = [
  791. 'id' => 'BirdName',
  792. 'name' => 'Birds[name]',
  793. 'options' => null
  794. ];
  795. $result = $select->render($data, $this->context);
  796. $expected = [
  797. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  798. '/select'
  799. ];
  800. $this->assertHtml($expected, $result);
  801. $data['empty'] = true;
  802. $result = $select->render($data, $this->context);
  803. $expected = [
  804. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  805. ['option' => ['value' => '']], '/option',
  806. '/select'
  807. ];
  808. $this->assertHtml($expected, $result);
  809. $data['empty'] = 'empty';
  810. $result = $select->render($data, $this->context);
  811. $expected = [
  812. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  813. ['option' => ['value' => '']], 'empty', '/option',
  814. '/select'
  815. ];
  816. $this->assertHtml($expected, $result);
  817. }
  818. /**
  819. * Ensure templateVars option is hooked up.
  820. *
  821. * @return void
  822. */
  823. public function testRenderTemplateVars()
  824. {
  825. $this->templates->add([
  826. 'select' => '<select custom="{{custom}}" name="{{name}}"{{attrs}}>{{content}}</select>',
  827. 'option' => '<option opt="{{opt}}" value="{{value}}"{{attrs}}>{{text}}</option>',
  828. ]);
  829. $input = new SelectBoxWidget($this->templates);
  830. $data = [
  831. 'templateVars' => ['custom' => 'value', 'opt' => 'option'],
  832. 'name' => 'birds',
  833. 'options' => [
  834. ['value' => 'a', 'text' => 'Albatross', 'templateVars' => ['opt' => 'opt-1']],
  835. 'b' => 'Budgie',
  836. 'c' => 'Canary',
  837. ]
  838. ];
  839. $result = $input->render($data, $this->context);
  840. $expected = [
  841. 'select' => [
  842. 'name' => 'birds',
  843. 'custom' => 'value',
  844. ],
  845. ['option' => ['value' => 'a', 'opt' => 'opt-1']],
  846. 'Albatross',
  847. '/option',
  848. ['option' => ['value' => 'b', 'opt' => 'option']],
  849. 'Budgie',
  850. '/option',
  851. ['option' => ['value' => 'c', 'opt' => 'option']],
  852. 'Canary',
  853. '/option',
  854. '/select'
  855. ];
  856. $this->assertHtml($expected, $result);
  857. }
  858. }