SelectBoxWidgetTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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 numeric option group keys
  342. *
  343. * @return void
  344. */
  345. public function testRenderOptionGroupsIntegerKeys()
  346. {
  347. $select = new SelectBoxWidget($this->templates);
  348. $data = [
  349. 'name' => 'Year[key]',
  350. 'options' => [
  351. 2014 => [
  352. 'key' => 'value'
  353. ],
  354. 2013 => [
  355. 'text' => '2013-text',
  356. 'options' => [
  357. 'key2' => 'value2'
  358. ]
  359. ]
  360. ]
  361. ];
  362. $result = $select->render($data, $this->context);
  363. $expected = [
  364. 'select' => [
  365. 'name' => 'Year[key]',
  366. ],
  367. ['optgroup' => ['label' => '2014']],
  368. ['option' => ['value' => 'key']],
  369. 'value',
  370. '/option',
  371. '/optgroup',
  372. ['optgroup' => ['label' => '2013-text']],
  373. ['option' => ['value' => 'key2']],
  374. 'value2',
  375. '/option',
  376. '/optgroup',
  377. '/select'
  378. ];
  379. $this->assertHtml($expected, $result);
  380. }
  381. /**
  382. * test rendering with option groups and escaping
  383. *
  384. * @return void
  385. */
  386. public function testRenderOptionGroupsEscape()
  387. {
  388. $select = new SelectBoxWidget($this->templates);
  389. $data = [
  390. 'name' => 'Birds[name]',
  391. 'options' => [
  392. '>XSS<' => [
  393. '1' => 'One>',
  394. ],
  395. ]
  396. ];
  397. $result = $select->render($data, $this->context);
  398. $expected = [
  399. 'select' => [
  400. 'name' => 'Birds[name]',
  401. ],
  402. ['optgroup' => ['label' => '&gt;XSS&lt;']],
  403. ['option' => ['value' => '1']],
  404. 'One&gt;',
  405. '/option',
  406. '/optgroup',
  407. '/select'
  408. ];
  409. $this->assertHtml($expected, $result);
  410. $data['escape'] = false;
  411. $result = $select->render($data, $this->context);
  412. $expected = [
  413. 'select' => [
  414. 'name' => 'Birds[name]',
  415. ],
  416. ['optgroup' => ['label' => '>XSS<']],
  417. ['option' => ['value' => '1']],
  418. 'One>',
  419. '/option',
  420. '/optgroup',
  421. '/select'
  422. ];
  423. $this->assertHtml($expected, $result);
  424. }
  425. /**
  426. * test rendering with option groups
  427. *
  428. * @return void
  429. */
  430. public function testRenderOptionGroupsWithAttributes()
  431. {
  432. $select = new SelectBoxWidget($this->templates);
  433. $data = [
  434. 'name' => 'Birds[name]',
  435. 'options' => [
  436. [
  437. 'text' => 'Mammal',
  438. 'data-foo' => 'bar',
  439. 'options' => [
  440. 'beaver' => 'Beaver',
  441. 'elk' => 'Elk',
  442. ]
  443. ]
  444. ]
  445. ];
  446. $result = $select->render($data, $this->context);
  447. $expected = [
  448. 'select' => [
  449. 'name' => 'Birds[name]',
  450. ],
  451. ['optgroup' => ['data-foo' => 'bar', 'label' => 'Mammal']],
  452. ['option' => ['value' => 'beaver']],
  453. 'Beaver',
  454. '/option',
  455. ['option' => ['value' => 'elk']],
  456. 'Elk',
  457. '/option',
  458. '/optgroup',
  459. '/select'
  460. ];
  461. $this->assertHtml($expected, $result);
  462. }
  463. /**
  464. * test rendering with option groups with traversable nodes
  465. *
  466. * @return void
  467. */
  468. public function testRenderOptionGroupsTraversable()
  469. {
  470. $select = new SelectBoxWidget($this->templates);
  471. $mammals = new \ArrayObject(['beaver' => 'Beaver', 'elk' => 'Elk']);
  472. $data = [
  473. 'name' => 'Birds[name]',
  474. 'options' => [
  475. 'Mammal' => $mammals,
  476. 'Bird' => [
  477. 'budgie' => 'Budgie',
  478. 'eagle' => 'Eagle',
  479. ]
  480. ]
  481. ];
  482. $result = $select->render($data, $this->context);
  483. $expected = [
  484. 'select' => [
  485. 'name' => 'Birds[name]',
  486. ],
  487. ['optgroup' => ['label' => 'Mammal']],
  488. ['option' => ['value' => 'beaver']],
  489. 'Beaver',
  490. '/option',
  491. ['option' => ['value' => 'elk']],
  492. 'Elk',
  493. '/option',
  494. '/optgroup',
  495. ['optgroup' => ['label' => 'Bird']],
  496. ['option' => ['value' => 'budgie']],
  497. 'Budgie',
  498. '/option',
  499. ['option' => ['value' => 'eagle']],
  500. 'Eagle',
  501. '/option',
  502. '/optgroup',
  503. '/select'
  504. ];
  505. $this->assertHtml($expected, $result);
  506. }
  507. /**
  508. * test rendering option groups and selected values
  509. *
  510. * @return void
  511. */
  512. public function testRenderOptionGroupsSelectedAndDisabled()
  513. {
  514. $select = new SelectBoxWidget($this->templates);
  515. $data = [
  516. 'name' => 'Birds[name]',
  517. 'val' => ['1', '2', 'burp'],
  518. 'disabled' => ['1x', '2x', 'nope'],
  519. 'options' => [
  520. 'ones' => [
  521. 1 => 'one',
  522. '1x' => 'one x',
  523. ],
  524. 'twos' => [
  525. '2' => 'two',
  526. '2x' => 'two x',
  527. ]
  528. ]
  529. ];
  530. $result = $select->render($data, $this->context);
  531. $expected = [
  532. 'select' => [
  533. 'name' => 'Birds[name]',
  534. ],
  535. ['optgroup' => ['label' => 'ones']],
  536. ['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
  537. ['option' => ['value' => '1x', 'disabled' => 'disabled']], 'one x', '/option',
  538. '/optgroup',
  539. ['optgroup' => ['label' => 'twos']],
  540. ['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
  541. ['option' => ['value' => '2x', 'disabled' => 'disabled']], 'two x', '/option',
  542. '/optgroup',
  543. '/select'
  544. ];
  545. $this->assertHtml($expected, $result);
  546. }
  547. /**
  548. * test rendering a totally disabled element
  549. *
  550. * @return void
  551. */
  552. public function testRenderDisabled()
  553. {
  554. $select = new SelectBoxWidget($this->templates);
  555. $data = [
  556. 'disabled' => true,
  557. 'name' => 'Birds[name]',
  558. 'options' => ['a' => 'Albatross', 'b' => 'Budgie'],
  559. 'val' => 'a',
  560. ];
  561. $result = $select->render($data, $this->context);
  562. $expected = [
  563. 'select' => [
  564. 'name' => 'Birds[name]',
  565. 'disabled' => 'disabled',
  566. ],
  567. ['option' => ['value' => 'a', 'selected' => 'selected']], 'Albatross', '/option',
  568. ['option' => ['value' => 'b']], 'Budgie', '/option',
  569. '/select'
  570. ];
  571. $this->assertHtml($expected, $result);
  572. $select = new SelectBoxWidget($this->templates);
  573. $data = [
  574. 'disabled' => [1],
  575. 'name' => 'numbers',
  576. 'options' => ['1' => 'One', '2' => 'Two'],
  577. ];
  578. $result = $select->render($data, $this->context);
  579. $expected = [
  580. 'select' => [
  581. 'name' => 'numbers',
  582. ],
  583. ['option' => ['value' => '1', 'disabled' => 'disabled']], 'One', '/option',
  584. ['option' => ['value' => '2']], 'Two', '/option',
  585. '/select'
  586. ];
  587. $this->assertHtml($expected, $result);
  588. }
  589. /**
  590. * test rendering a disabled element
  591. *
  592. * @return void
  593. */
  594. public function testRenderDisabledMultiple()
  595. {
  596. $select = new SelectBoxWidget($this->templates);
  597. $data = [
  598. 'disabled' => ['a', 'c'],
  599. 'val' => 'a',
  600. 'name' => 'Birds[name]',
  601. 'options' => [
  602. 'a' => 'Albatross',
  603. 'b' => 'Budgie',
  604. 'c' => 'Canary',
  605. ]
  606. ];
  607. $result = $select->render($data, $this->context);
  608. $expected = [
  609. 'select' => [
  610. 'name' => 'Birds[name]',
  611. ],
  612. ['option' => ['value' => 'a', 'selected' => 'selected', 'disabled' => 'disabled']],
  613. 'Albatross',
  614. '/option',
  615. ['option' => ['value' => 'b']],
  616. 'Budgie',
  617. '/option',
  618. ['option' => ['value' => 'c', 'disabled' => 'disabled']],
  619. 'Canary',
  620. '/option',
  621. '/select'
  622. ];
  623. $this->assertHtml($expected, $result);
  624. }
  625. /**
  626. * test complex option rendering with a disabled element
  627. *
  628. * @return void
  629. */
  630. public function testRenderComplexDisabled()
  631. {
  632. $select = new SelectBoxWidget($this->templates);
  633. $data = [
  634. 'disabled' => ['b'],
  635. 'id' => 'BirdName',
  636. 'name' => 'Birds[name]',
  637. 'options' => [
  638. ['value' => 'a', 'text' => 'Albatross'],
  639. ['value' => 'b', 'text' => 'Budgie', 'data-foo' => 'bar'],
  640. ]
  641. ];
  642. $result = $select->render($data, $this->context);
  643. $expected = [
  644. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  645. ['option' => ['value' => 'a']],
  646. 'Albatross',
  647. '/option',
  648. ['option' => ['value' => 'b', 'data-foo' => 'bar', 'disabled' => 'disabled']],
  649. 'Budgie',
  650. '/option',
  651. '/select'
  652. ];
  653. $this->assertHtml($expected, $result);
  654. }
  655. /**
  656. * test rendering with an empty value
  657. *
  658. * @return void
  659. */
  660. public function testRenderEmptyOption()
  661. {
  662. $select = new SelectBoxWidget($this->templates);
  663. $data = [
  664. 'id' => 'BirdName',
  665. 'name' => 'Birds[name]',
  666. 'empty' => true,
  667. 'options' => ['a' => 'Albatross', 'b' => 'Budgie']
  668. ];
  669. $result = $select->render($data, $this->context);
  670. $expected = [
  671. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  672. ['option' => ['value' => '']], '/option',
  673. ['option' => ['value' => 'a']], 'Albatross', '/option',
  674. ['option' => ['value' => 'b']], 'Budgie', '/option',
  675. '/select'
  676. ];
  677. $this->assertHtml($expected, $result);
  678. $data['empty'] = 'empty';
  679. $result = $select->render($data, $this->context);
  680. $expected = [
  681. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  682. ['option' => ['value' => '']], 'empty', '/option',
  683. ['option' => ['value' => 'a']], 'Albatross', '/option',
  684. ['option' => ['value' => 'b']], 'Budgie', '/option',
  685. '/select'
  686. ];
  687. $this->assertHtml($expected, $result);
  688. $data['empty'] = ['99' => '(choose one)'];
  689. $result = $select->render($data, $this->context);
  690. $expected = [
  691. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  692. ['option' => ['value' => '99']], '(choose one)', '/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. $data['val'] = '';
  700. $result = $select->render($data, $this->context);
  701. $expected = [
  702. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  703. ['option' => ['value' => '', 'selected' => 'selected']], 'empty', '/option',
  704. ['option' => ['value' => 'a']], 'Albatross', '/option',
  705. ['option' => ['value' => 'b']], 'Budgie', '/option',
  706. '/select'
  707. ];
  708. $this->assertHtml($expected, $result);
  709. $data['val'] = false;
  710. $result = $select->render($data, $this->context);
  711. $this->assertHtml($expected, $result);
  712. }
  713. /**
  714. * Test rendering with disabling escaping.
  715. *
  716. * @return void
  717. */
  718. public function testRenderEscapingOption()
  719. {
  720. $select = new SelectBoxWidget($this->templates);
  721. $data = [
  722. 'name' => 'Birds[name]',
  723. 'options' => [
  724. 'a' => '>Albatross',
  725. 'b' => '>Budgie',
  726. 'c' => '>Canary',
  727. ]
  728. ];
  729. $result = $select->render($data, $this->context);
  730. $expected = [
  731. 'select' => [
  732. 'name' => 'Birds[name]',
  733. ],
  734. ['option' => ['value' => 'a']],
  735. '&gt;Albatross',
  736. '/option',
  737. ['option' => ['value' => 'b']],
  738. '&gt;Budgie',
  739. '/option',
  740. ['option' => ['value' => 'c']],
  741. '&gt;Canary',
  742. '/option',
  743. '/select'
  744. ];
  745. $this->assertHtml($expected, $result);
  746. $data = [
  747. 'escape' => false,
  748. 'name' => 'Birds[name]',
  749. 'options' => [
  750. '>a' => '>Albatross',
  751. ]
  752. ];
  753. $result = $select->render($data, $this->context);
  754. $expected = [
  755. 'select' => [
  756. 'name' => 'Birds[name]',
  757. ],
  758. ['option' => ['value' => '>a']],
  759. '>Albatross',
  760. '/option',
  761. '/select'
  762. ];
  763. $this->assertHtml($expected, $result);
  764. }
  765. /**
  766. * test render with null options
  767. *
  768. * @return void
  769. */
  770. public function testRenderNullOptions()
  771. {
  772. $select = new SelectBoxWidget($this->templates);
  773. $data = [
  774. 'id' => 'BirdName',
  775. 'name' => 'Birds[name]',
  776. 'options' => null
  777. ];
  778. $result = $select->render($data, $this->context);
  779. $expected = [
  780. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  781. '/select'
  782. ];
  783. $this->assertHtml($expected, $result);
  784. $data['empty'] = true;
  785. $result = $select->render($data, $this->context);
  786. $expected = [
  787. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  788. ['option' => ['value' => '']], '/option',
  789. '/select'
  790. ];
  791. $this->assertHtml($expected, $result);
  792. $data['empty'] = 'empty';
  793. $result = $select->render($data, $this->context);
  794. $expected = [
  795. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  796. ['option' => ['value' => '']], 'empty', '/option',
  797. '/select'
  798. ];
  799. $this->assertHtml($expected, $result);
  800. }
  801. }