SelectBoxWidgetTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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->getMock('Cake\View\Form\ContextInterface');
  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 simple iterator rendering
  86. *
  87. * @return void
  88. */
  89. public function testRenderSimpleIterator()
  90. {
  91. $select = new SelectBoxWidget($this->templates);
  92. $options = new \ArrayObject(['a' => 'Albatross', 'b' => 'Budgie']);
  93. $data = [
  94. 'name' => 'Birds[name]',
  95. 'options' => $options,
  96. 'empty' => true
  97. ];
  98. $result = $select->render($data, $this->context);
  99. $expected = [
  100. 'select' => ['name' => 'Birds[name]'],
  101. ['option' => ['value' => '']], '/option',
  102. ['option' => ['value' => 'a']], 'Albatross', '/option',
  103. ['option' => ['value' => 'b']], 'Budgie', '/option',
  104. '/select'
  105. ];
  106. $this->assertHtml($expected, $result);
  107. }
  108. /**
  109. * test simple iterator rendering with empty option
  110. *
  111. * @return void
  112. */
  113. public function testRenderSimpleIteratorWithEmpty()
  114. {
  115. $select = new SelectBoxWidget($this->templates);
  116. $options = new Collection(['a' => 'Albatross', 'b' => 'Budgie']);
  117. $data = [
  118. 'name' => 'Birds[name]',
  119. 'options' => $options,
  120. 'empty' => 'Pick one'
  121. ];
  122. $result = $select->render($data, $this->context);
  123. $expected = [
  124. 'select' => ['name' => 'Birds[name]'],
  125. ['option' => ['value' => '']], 'Pick one', '/option',
  126. ['option' => ['value' => 'a']], 'Albatross', '/option',
  127. ['option' => ['value' => 'b']], 'Budgie', '/option',
  128. '/select'
  129. ];
  130. $this->assertHtml($expected, $result);
  131. }
  132. /**
  133. * test complex option rendering
  134. *
  135. * @return void
  136. */
  137. public function testRenderComplex()
  138. {
  139. $select = new SelectBoxWidget($this->templates);
  140. $data = [
  141. 'id' => 'BirdName',
  142. 'name' => 'Birds[name]',
  143. 'options' => [
  144. ['value' => 'a', 'text' => 'Albatross'],
  145. ['value' => 'b', 'text' => 'Budgie', 'data-foo' => 'bar'],
  146. ]
  147. ];
  148. $result = $select->render($data, $this->context);
  149. $expected = [
  150. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  151. ['option' => ['value' => 'a']],
  152. 'Albatross',
  153. '/option',
  154. ['option' => ['value' => 'b', 'data-foo' => 'bar']],
  155. 'Budgie',
  156. '/option',
  157. '/select'
  158. ];
  159. $this->assertHtml($expected, $result);
  160. }
  161. /**
  162. * test rendering with a selected value
  163. *
  164. * @return void
  165. */
  166. public function testRenderSelected()
  167. {
  168. $select = new SelectBoxWidget($this->templates);
  169. $data = [
  170. 'id' => 'BirdName',
  171. 'name' => 'Birds[name]',
  172. 'val' => '1',
  173. 'options' => [
  174. 1 => 'one',
  175. '1x' => 'one x',
  176. '2' => 'two',
  177. '2x' => 'two x',
  178. ]
  179. ];
  180. $result = $select->render($data, $this->context);
  181. $expected = [
  182. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  183. ['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
  184. ['option' => ['value' => '1x']], 'one x', '/option',
  185. ['option' => ['value' => '2']], 'two', '/option',
  186. ['option' => ['value' => '2x']], 'two x', '/option',
  187. '/select'
  188. ];
  189. $this->assertHtml($expected, $result);
  190. $data['val'] = 2;
  191. $result = $select->render($data, $this->context);
  192. $expected = [
  193. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  194. ['option' => ['value' => '1']], 'one', '/option',
  195. ['option' => ['value' => '1x']], 'one x', '/option',
  196. ['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
  197. ['option' => ['value' => '2x']], 'two x', '/option',
  198. '/select'
  199. ];
  200. $this->assertHtml($expected, $result);
  201. }
  202. /**
  203. * test complex option rendering with a selected value
  204. *
  205. * @return void
  206. */
  207. public function testRenderComplexSelected()
  208. {
  209. $select = new SelectBoxWidget($this->templates);
  210. $data = [
  211. 'id' => 'BirdName',
  212. 'name' => 'Birds[name]',
  213. 'val' => 'a',
  214. 'options' => [
  215. ['value' => 'a', 'text' => 'Albatross'],
  216. ['value' => 'b', 'text' => 'Budgie', 'data-foo' => 'bar'],
  217. ]
  218. ];
  219. $result = $select->render($data, $this->context);
  220. $expected = [
  221. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  222. ['option' => ['value' => 'a', 'selected' => 'selected']],
  223. 'Albatross',
  224. '/option',
  225. ['option' => ['value' => 'b', 'data-foo' => 'bar']],
  226. 'Budgie',
  227. '/option',
  228. '/select'
  229. ];
  230. $this->assertHtml($expected, $result);
  231. }
  232. /**
  233. * test rendering a multi select
  234. *
  235. * @return void
  236. */
  237. public function testRenderMultipleSelect()
  238. {
  239. $select = new SelectBoxWidget($this->templates);
  240. $data = [
  241. 'id' => 'BirdName',
  242. 'name' => 'Birds[name]',
  243. 'multiple' => true,
  244. 'options' => ['a' => 'Albatross', 'b' => 'Budgie']
  245. ];
  246. $result = $select->render($data, $this->context);
  247. $expected = [
  248. 'select' => [
  249. 'name' => 'Birds[name][]',
  250. 'id' => 'BirdName',
  251. 'multiple' => 'multiple',
  252. ],
  253. ['option' => ['value' => 'a']], 'Albatross', '/option',
  254. ['option' => ['value' => 'b']], 'Budgie', '/option',
  255. '/select'
  256. ];
  257. $this->assertHtml($expected, $result);
  258. }
  259. /**
  260. * test rendering multi select & selected values
  261. *
  262. * @return void
  263. */
  264. public function testRenderMultipleSelected()
  265. {
  266. $select = new SelectBoxWidget($this->templates);
  267. $data = [
  268. 'multiple' => true,
  269. 'id' => 'BirdName',
  270. 'name' => 'Birds[name]',
  271. 'val' => ['1', '2', 'burp'],
  272. 'options' => [
  273. 1 => 'one',
  274. '1x' => 'one x',
  275. '2' => 'two',
  276. '2x' => 'two x',
  277. ]
  278. ];
  279. $result = $select->render($data, $this->context);
  280. $expected = [
  281. 'select' => [
  282. 'name' => 'Birds[name][]',
  283. 'multiple' => 'multiple',
  284. 'id' => 'BirdName'
  285. ],
  286. ['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
  287. ['option' => ['value' => '1x']], 'one x', '/option',
  288. ['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
  289. ['option' => ['value' => '2x']], 'two x', '/option',
  290. '/select'
  291. ];
  292. $this->assertHtml($expected, $result);
  293. }
  294. /**
  295. * test rendering with option groups
  296. *
  297. * @return void
  298. */
  299. public function testRenderOptionGroups()
  300. {
  301. $select = new SelectBoxWidget($this->templates);
  302. $data = [
  303. 'name' => 'Birds[name]',
  304. 'options' => [
  305. 'Mammal' => [
  306. 'beaver' => 'Beaver',
  307. 'elk' => 'Elk',
  308. ],
  309. 'Bird' => [
  310. 'budgie' => 'Budgie',
  311. 'eagle' => 'Eagle',
  312. ]
  313. ]
  314. ];
  315. $result = $select->render($data, $this->context);
  316. $expected = [
  317. 'select' => [
  318. 'name' => 'Birds[name]',
  319. ],
  320. ['optgroup' => ['label' => 'Mammal']],
  321. ['option' => ['value' => 'beaver']],
  322. 'Beaver',
  323. '/option',
  324. ['option' => ['value' => 'elk']],
  325. 'Elk',
  326. '/option',
  327. '/optgroup',
  328. ['optgroup' => ['label' => 'Bird']],
  329. ['option' => ['value' => 'budgie']],
  330. 'Budgie',
  331. '/option',
  332. ['option' => ['value' => 'eagle']],
  333. 'Eagle',
  334. '/option',
  335. '/optgroup',
  336. '/select'
  337. ];
  338. $this->assertHtml($expected, $result);
  339. }
  340. /**
  341. * test rendering with option groups and escaping
  342. *
  343. * @return void
  344. */
  345. public function testRenderOptionGroupsEscape()
  346. {
  347. $select = new SelectBoxWidget($this->templates);
  348. $data = [
  349. 'name' => 'Birds[name]',
  350. 'options' => [
  351. '>XSS<' => [
  352. '1' => 'One>',
  353. ],
  354. ]
  355. ];
  356. $result = $select->render($data, $this->context);
  357. $expected = [
  358. 'select' => [
  359. 'name' => 'Birds[name]',
  360. ],
  361. ['optgroup' => ['label' => '&gt;XSS&lt;']],
  362. ['option' => ['value' => '1']],
  363. 'One&gt;',
  364. '/option',
  365. '/optgroup',
  366. '/select'
  367. ];
  368. $this->assertHtml($expected, $result);
  369. $data['escape'] = false;
  370. $result = $select->render($data, $this->context);
  371. $expected = [
  372. 'select' => [
  373. 'name' => 'Birds[name]',
  374. ],
  375. ['optgroup' => ['label' => '>XSS<']],
  376. ['option' => ['value' => '1']],
  377. 'One>',
  378. '/option',
  379. '/optgroup',
  380. '/select'
  381. ];
  382. $this->assertHtml($expected, $result);
  383. }
  384. /**
  385. * test rendering with option groups
  386. *
  387. * @return void
  388. */
  389. public function testRenderOptionGroupsWithAttributes()
  390. {
  391. $select = new SelectBoxWidget($this->templates);
  392. $data = [
  393. 'name' => 'Birds[name]',
  394. 'options' => [
  395. [
  396. 'text' => 'Mammal',
  397. 'data-foo' => 'bar',
  398. 'options' => [
  399. 'beaver' => 'Beaver',
  400. 'elk' => 'Elk',
  401. ]
  402. ]
  403. ]
  404. ];
  405. $result = $select->render($data, $this->context);
  406. $expected = [
  407. 'select' => [
  408. 'name' => 'Birds[name]',
  409. ],
  410. ['optgroup' => ['data-foo' => 'bar', 'label' => 'Mammal']],
  411. ['option' => ['value' => 'beaver']],
  412. 'Beaver',
  413. '/option',
  414. ['option' => ['value' => 'elk']],
  415. 'Elk',
  416. '/option',
  417. '/optgroup',
  418. '/select'
  419. ];
  420. $this->assertHtml($expected, $result);
  421. }
  422. /**
  423. * test rendering with option groups with traversable nodes
  424. *
  425. * @return void
  426. */
  427. public function testRenderOptionGroupsTraversable()
  428. {
  429. $select = new SelectBoxWidget($this->templates);
  430. $mammals = new \ArrayObject(['beaver' => 'Beaver', 'elk' => 'Elk']);
  431. $data = [
  432. 'name' => 'Birds[name]',
  433. 'options' => [
  434. 'Mammal' => $mammals,
  435. 'Bird' => [
  436. 'budgie' => 'Budgie',
  437. 'eagle' => 'Eagle',
  438. ]
  439. ]
  440. ];
  441. $result = $select->render($data, $this->context);
  442. $expected = [
  443. 'select' => [
  444. 'name' => 'Birds[name]',
  445. ],
  446. ['optgroup' => ['label' => 'Mammal']],
  447. ['option' => ['value' => 'beaver']],
  448. 'Beaver',
  449. '/option',
  450. ['option' => ['value' => 'elk']],
  451. 'Elk',
  452. '/option',
  453. '/optgroup',
  454. ['optgroup' => ['label' => 'Bird']],
  455. ['option' => ['value' => 'budgie']],
  456. 'Budgie',
  457. '/option',
  458. ['option' => ['value' => 'eagle']],
  459. 'Eagle',
  460. '/option',
  461. '/optgroup',
  462. '/select'
  463. ];
  464. $this->assertHtml($expected, $result);
  465. }
  466. /**
  467. * test rendering option groups and selected values
  468. *
  469. * @return void
  470. */
  471. public function testRenderOptionGroupsSelectedAndDisabled()
  472. {
  473. $select = new SelectBoxWidget($this->templates);
  474. $data = [
  475. 'name' => 'Birds[name]',
  476. 'val' => ['1', '2', 'burp'],
  477. 'disabled' => ['1x', '2x', 'nope'],
  478. 'options' => [
  479. 'ones' => [
  480. 1 => 'one',
  481. '1x' => 'one x',
  482. ],
  483. 'twos' => [
  484. '2' => 'two',
  485. '2x' => 'two x',
  486. ]
  487. ]
  488. ];
  489. $result = $select->render($data, $this->context);
  490. $expected = [
  491. 'select' => [
  492. 'name' => 'Birds[name]',
  493. ],
  494. ['optgroup' => ['label' => 'ones']],
  495. ['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
  496. ['option' => ['value' => '1x', 'disabled' => 'disabled']], 'one x', '/option',
  497. '/optgroup',
  498. ['optgroup' => ['label' => 'twos']],
  499. ['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
  500. ['option' => ['value' => '2x', 'disabled' => 'disabled']], 'two x', '/option',
  501. '/optgroup',
  502. '/select'
  503. ];
  504. $this->assertHtml($expected, $result);
  505. }
  506. /**
  507. * test rendering a totally disabled element
  508. *
  509. * @return void
  510. */
  511. public function testRenderDisabled()
  512. {
  513. $select = new SelectBoxWidget($this->templates);
  514. $data = [
  515. 'disabled' => true,
  516. 'name' => 'Birds[name]',
  517. 'options' => ['a' => 'Albatross', 'b' => 'Budgie'],
  518. 'val' => 'a',
  519. ];
  520. $result = $select->render($data, $this->context);
  521. $expected = [
  522. 'select' => [
  523. 'name' => 'Birds[name]',
  524. 'disabled' => 'disabled',
  525. ],
  526. ['option' => ['value' => 'a', 'selected' => 'selected']], 'Albatross', '/option',
  527. ['option' => ['value' => 'b']], 'Budgie', '/option',
  528. '/select'
  529. ];
  530. $this->assertHtml($expected, $result);
  531. $select = new SelectBoxWidget($this->templates);
  532. $data = [
  533. 'disabled' => [1],
  534. 'name' => 'numbers',
  535. 'options' => ['1' => 'One', '2' => 'Two'],
  536. ];
  537. $result = $select->render($data, $this->context);
  538. $expected = [
  539. 'select' => [
  540. 'name' => 'numbers',
  541. ],
  542. ['option' => ['value' => '1', 'disabled' => 'disabled']], 'One', '/option',
  543. ['option' => ['value' => '2']], 'Two', '/option',
  544. '/select'
  545. ];
  546. $this->assertHtml($expected, $result);
  547. }
  548. /**
  549. * test rendering a disabled element
  550. *
  551. * @return void
  552. */
  553. public function testRenderDisabledMultiple()
  554. {
  555. $select = new SelectBoxWidget($this->templates);
  556. $data = [
  557. 'disabled' => ['a', 'c'],
  558. 'val' => 'a',
  559. 'name' => 'Birds[name]',
  560. 'options' => [
  561. 'a' => 'Albatross',
  562. 'b' => 'Budgie',
  563. 'c' => 'Canary',
  564. ]
  565. ];
  566. $result = $select->render($data, $this->context);
  567. $expected = [
  568. 'select' => [
  569. 'name' => 'Birds[name]',
  570. ],
  571. ['option' => ['value' => 'a', 'selected' => 'selected', 'disabled' => 'disabled']],
  572. 'Albatross',
  573. '/option',
  574. ['option' => ['value' => 'b']],
  575. 'Budgie',
  576. '/option',
  577. ['option' => ['value' => 'c', 'disabled' => 'disabled']],
  578. 'Canary',
  579. '/option',
  580. '/select'
  581. ];
  582. $this->assertHtml($expected, $result);
  583. }
  584. /**
  585. * test complex option rendering with a disabled element
  586. *
  587. * @return void
  588. */
  589. public function testRenderComplexDisabled()
  590. {
  591. $select = new SelectBoxWidget($this->templates);
  592. $data = [
  593. 'disabled' => ['b'],
  594. 'id' => 'BirdName',
  595. 'name' => 'Birds[name]',
  596. 'options' => [
  597. ['value' => 'a', 'text' => 'Albatross'],
  598. ['value' => 'b', 'text' => 'Budgie', 'data-foo' => 'bar'],
  599. ]
  600. ];
  601. $result = $select->render($data, $this->context);
  602. $expected = [
  603. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  604. ['option' => ['value' => 'a']],
  605. 'Albatross',
  606. '/option',
  607. ['option' => ['value' => 'b', 'data-foo' => 'bar', 'disabled' => 'disabled']],
  608. 'Budgie',
  609. '/option',
  610. '/select'
  611. ];
  612. $this->assertHtml($expected, $result);
  613. }
  614. /**
  615. * test rendering with an empty value
  616. *
  617. * @return void
  618. */
  619. public function testRenderEmptyOption()
  620. {
  621. $select = new SelectBoxWidget($this->templates);
  622. $data = [
  623. 'id' => 'BirdName',
  624. 'name' => 'Birds[name]',
  625. 'empty' => true,
  626. 'options' => ['a' => 'Albatross', 'b' => 'Budgie']
  627. ];
  628. $result = $select->render($data, $this->context);
  629. $expected = [
  630. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  631. ['option' => ['value' => '']], '/option',
  632. ['option' => ['value' => 'a']], 'Albatross', '/option',
  633. ['option' => ['value' => 'b']], 'Budgie', '/option',
  634. '/select'
  635. ];
  636. $this->assertHtml($expected, $result);
  637. $data['empty'] = 'empty';
  638. $result = $select->render($data, $this->context);
  639. $expected = [
  640. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  641. ['option' => ['value' => '']], 'empty', '/option',
  642. ['option' => ['value' => 'a']], 'Albatross', '/option',
  643. ['option' => ['value' => 'b']], 'Budgie', '/option',
  644. '/select'
  645. ];
  646. $this->assertHtml($expected, $result);
  647. $data['empty'] = 'empty';
  648. $data['val'] = '';
  649. $result = $select->render($data, $this->context);
  650. $expected = [
  651. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  652. ['option' => ['value' => '', 'selected' => 'selected']], 'empty', '/option',
  653. ['option' => ['value' => 'a']], 'Albatross', '/option',
  654. ['option' => ['value' => 'b']], 'Budgie', '/option',
  655. '/select'
  656. ];
  657. $this->assertHtml($expected, $result);
  658. $data['val'] = false;
  659. $result = $select->render($data, $this->context);
  660. $this->assertHtml($expected, $result);
  661. }
  662. /**
  663. * Test rendering with disabling escaping.
  664. *
  665. * @return void
  666. */
  667. public function testRenderEscapingOption()
  668. {
  669. $select = new SelectBoxWidget($this->templates);
  670. $data = [
  671. 'name' => 'Birds[name]',
  672. 'options' => [
  673. 'a' => '>Albatross',
  674. 'b' => '>Budgie',
  675. 'c' => '>Canary',
  676. ]
  677. ];
  678. $result = $select->render($data, $this->context);
  679. $expected = [
  680. 'select' => [
  681. 'name' => 'Birds[name]',
  682. ],
  683. ['option' => ['value' => 'a']],
  684. '&gt;Albatross',
  685. '/option',
  686. ['option' => ['value' => 'b']],
  687. '&gt;Budgie',
  688. '/option',
  689. ['option' => ['value' => 'c']],
  690. '&gt;Canary',
  691. '/option',
  692. '/select'
  693. ];
  694. $this->assertHtml($expected, $result);
  695. $data = [
  696. 'escape' => false,
  697. 'name' => 'Birds[name]',
  698. 'options' => [
  699. '>a' => '>Albatross',
  700. ]
  701. ];
  702. $result = $select->render($data, $this->context);
  703. $expected = [
  704. 'select' => [
  705. 'name' => 'Birds[name]',
  706. ],
  707. ['option' => ['value' => '>a']],
  708. '>Albatross',
  709. '/option',
  710. '/select'
  711. ];
  712. $this->assertHtml($expected, $result);
  713. }
  714. /**
  715. * test render with null options
  716. *
  717. * @return void
  718. */
  719. public function testRenderNullOptions()
  720. {
  721. $select = new SelectBoxWidget($this->templates);
  722. $data = [
  723. 'id' => 'BirdName',
  724. 'name' => 'Birds[name]',
  725. 'options' => null
  726. ];
  727. $result = $select->render($data, $this->context);
  728. $expected = [
  729. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  730. '/select'
  731. ];
  732. $this->assertHtml($expected, $result);
  733. $data['empty'] = true;
  734. $result = $select->render($data, $this->context);
  735. $expected = [
  736. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  737. ['option' => ['value' => '']], '/option',
  738. '/select'
  739. ];
  740. $this->assertHtml($expected, $result);
  741. $data['empty'] = 'empty';
  742. $result = $select->render($data, $this->context);
  743. $expected = [
  744. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  745. ['option' => ['value' => '']], 'empty', '/option',
  746. '/select'
  747. ];
  748. $this->assertHtml($expected, $result);
  749. }
  750. }