SelectBoxTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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\TestSuite\TestCase;
  17. use Cake\View\StringTemplate;
  18. use Cake\View\Widget\SelectBox;
  19. /**
  20. * SelectBox test case
  21. */
  22. class SelectBoxTest extends TestCase {
  23. /**
  24. * setup method.
  25. *
  26. * @return void
  27. */
  28. public function setUp() {
  29. parent::setUp();
  30. $templates = [
  31. 'select' => '<select name="{{name}}"{{attrs}}>{{content}}</select>',
  32. 'selectMultiple' => '<select name="{{name}}[]" multiple="multiple"{{attrs}}>{{content}}</select>',
  33. 'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>',
  34. 'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
  35. ];
  36. $this->templates = new StringTemplate($templates);
  37. }
  38. /**
  39. * test render no options
  40. *
  41. * @return void
  42. */
  43. public function testRenderNoOptions() {
  44. $select = new SelectBox($this->templates);
  45. $data = [
  46. 'id' => 'BirdName',
  47. 'name' => 'Birds[name]',
  48. 'options' => []
  49. ];
  50. $result = $select->render($data);
  51. $expected = [
  52. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  53. '/select'
  54. ];
  55. $this->assertTags($result, $expected);
  56. }
  57. /**
  58. * test simple rendering
  59. *
  60. * @return void
  61. */
  62. public function testRenderSimple() {
  63. $select = new SelectBox($this->templates);
  64. $data = [
  65. 'id' => 'BirdName',
  66. 'name' => 'Birds[name]',
  67. 'options' => ['a' => 'Albatross', 'b' => 'Budgie']
  68. ];
  69. $result = $select->render($data);
  70. $expected = [
  71. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  72. ['option' => ['value' => 'a']], 'Albatross', '/option',
  73. ['option' => ['value' => 'b']], 'Budgie', '/option',
  74. '/select'
  75. ];
  76. $this->assertTags($result, $expected);
  77. }
  78. /**
  79. * test simple iterator rendering
  80. *
  81. * @return void
  82. */
  83. public function testRenderSimpleIterator() {
  84. $select = new SelectBox($this->templates);
  85. $options = new \ArrayObject(['a' => 'Albatross', 'b' => 'Budgie']);
  86. $data = [
  87. 'name' => 'Birds[name]',
  88. 'options' => $options,
  89. 'empty' => true
  90. ];
  91. $result = $select->render($data);
  92. $expected = [
  93. 'select' => ['name' => 'Birds[name]'],
  94. ['option' => ['value' => '']], '/option',
  95. ['option' => ['value' => 'a']], 'Albatross', '/option',
  96. ['option' => ['value' => 'b']], 'Budgie', '/option',
  97. '/select'
  98. ];
  99. $this->assertTags($result, $expected);
  100. }
  101. /**
  102. * test complex option rendering
  103. *
  104. * @return void
  105. */
  106. public function testRenderComplex() {
  107. $select = new SelectBox($this->templates);
  108. $data = [
  109. 'id' => 'BirdName',
  110. 'name' => 'Birds[name]',
  111. 'options' => [
  112. ['value' => 'a', 'text' => 'Albatross'],
  113. ['value' => 'b', 'text' => 'Budgie', 'data-foo' => 'bar'],
  114. ]
  115. ];
  116. $result = $select->render($data);
  117. $expected = [
  118. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  119. ['option' => ['value' => 'a']],
  120. 'Albatross',
  121. '/option',
  122. ['option' => ['value' => 'b', 'data-foo' => 'bar']],
  123. 'Budgie',
  124. '/option',
  125. '/select'
  126. ];
  127. $this->assertTags($result, $expected);
  128. }
  129. /**
  130. * test rendering with a selected value
  131. *
  132. * @return void
  133. */
  134. public function testRenderSelected() {
  135. $select = new SelectBox($this->templates);
  136. $data = [
  137. 'id' => 'BirdName',
  138. 'name' => 'Birds[name]',
  139. 'val' => '1',
  140. 'options' => [
  141. 1 => 'one',
  142. '1x' => 'one x',
  143. '2' => 'two',
  144. '2x' => 'two x',
  145. ]
  146. ];
  147. $result = $select->render($data);
  148. $expected = [
  149. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  150. ['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
  151. ['option' => ['value' => '1x']], 'one x', '/option',
  152. ['option' => ['value' => '2']], 'two', '/option',
  153. ['option' => ['value' => '2x']], 'two x', '/option',
  154. '/select'
  155. ];
  156. $this->assertTags($result, $expected);
  157. $data['val'] = 2;
  158. $result = $select->render($data);
  159. $expected = [
  160. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  161. ['option' => ['value' => '1']], 'one', '/option',
  162. ['option' => ['value' => '1x']], 'one x', '/option',
  163. ['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
  164. ['option' => ['value' => '2x']], 'two x', '/option',
  165. '/select'
  166. ];
  167. $this->assertTags($result, $expected);
  168. }
  169. /**
  170. * test rendering a multi select
  171. *
  172. * @return void
  173. */
  174. public function testRenderMultipleSelect() {
  175. $select = new SelectBox($this->templates);
  176. $data = [
  177. 'id' => 'BirdName',
  178. 'name' => 'Birds[name]',
  179. 'multiple' => true,
  180. 'options' => ['a' => 'Albatross', 'b' => 'Budgie']
  181. ];
  182. $result = $select->render($data);
  183. $expected = [
  184. 'select' => [
  185. 'name' => 'Birds[name][]',
  186. 'id' => 'BirdName',
  187. 'multiple' => 'multiple',
  188. ],
  189. ['option' => ['value' => 'a']], 'Albatross', '/option',
  190. ['option' => ['value' => 'b']], 'Budgie', '/option',
  191. '/select'
  192. ];
  193. $this->assertTags($result, $expected);
  194. }
  195. /**
  196. * test rendering multi select & selected values
  197. *
  198. * @return void
  199. */
  200. public function testRenderMultipleSelected() {
  201. $select = new SelectBox($this->templates);
  202. $data = [
  203. 'multiple' => true,
  204. 'id' => 'BirdName',
  205. 'name' => 'Birds[name]',
  206. 'val' => ['1', '2', 'burp'],
  207. 'options' => [
  208. 1 => 'one',
  209. '1x' => 'one x',
  210. '2' => 'two',
  211. '2x' => 'two x',
  212. ]
  213. ];
  214. $result = $select->render($data);
  215. $expected = [
  216. 'select' => [
  217. 'name' => 'Birds[name][]',
  218. 'multiple' => 'multiple',
  219. 'id' => 'BirdName'
  220. ],
  221. ['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
  222. ['option' => ['value' => '1x']], 'one x', '/option',
  223. ['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
  224. ['option' => ['value' => '2x']], 'two x', '/option',
  225. '/select'
  226. ];
  227. $this->assertTags($result, $expected);
  228. }
  229. /**
  230. * test rendering with option groups
  231. *
  232. * @return void
  233. */
  234. public function testRenderOptionGroups() {
  235. $select = new SelectBox($this->templates);
  236. $data = [
  237. 'name' => 'Birds[name]',
  238. 'options' => [
  239. 'Mammal' => [
  240. 'beaver' => 'Beaver',
  241. 'elk' => 'Elk',
  242. ],
  243. 'Bird' => [
  244. 'budgie' => 'Budgie',
  245. 'eagle' => 'Eagle',
  246. ]
  247. ]
  248. ];
  249. $result = $select->render($data);
  250. $expected = [
  251. 'select' => [
  252. 'name' => 'Birds[name]',
  253. ],
  254. ['optgroup' => ['label' => 'Mammal']],
  255. ['option' => ['value' => 'beaver']],
  256. 'Beaver',
  257. '/option',
  258. ['option' => ['value' => 'elk']],
  259. 'Elk',
  260. '/option',
  261. '/optgroup',
  262. ['optgroup' => ['label' => 'Bird']],
  263. ['option' => ['value' => 'budgie']],
  264. 'Budgie',
  265. '/option',
  266. ['option' => ['value' => 'eagle']],
  267. 'Eagle',
  268. '/option',
  269. '/optgroup',
  270. '/select'
  271. ];
  272. $this->assertTags($result, $expected);
  273. }
  274. /**
  275. * test rendering with option groups and escaping
  276. *
  277. * @return void
  278. */
  279. public function testRenderOptionGroupsEscape() {
  280. $select = new SelectBox($this->templates);
  281. $data = [
  282. 'name' => 'Birds[name]',
  283. 'options' => [
  284. '>XSS<' => [
  285. '1' => 'One>',
  286. ],
  287. ]
  288. ];
  289. $result = $select->render($data);
  290. $expected = [
  291. 'select' => [
  292. 'name' => 'Birds[name]',
  293. ],
  294. ['optgroup' => ['label' => '&gt;XSS&lt;']],
  295. ['option' => ['value' => '1']],
  296. 'One&gt;',
  297. '/option',
  298. '/optgroup',
  299. '/select'
  300. ];
  301. $this->assertTags($result, $expected);
  302. $data['escape'] = false;
  303. $result = $select->render($data);
  304. $expected = [
  305. 'select' => [
  306. 'name' => 'Birds[name]',
  307. ],
  308. ['optgroup' => ['label' => '>XSS<']],
  309. ['option' => ['value' => '1']],
  310. 'One>',
  311. '/option',
  312. '/optgroup',
  313. '/select'
  314. ];
  315. $this->assertTags($result, $expected);
  316. }
  317. /**
  318. * test rendering with option groups
  319. *
  320. * @return void
  321. */
  322. public function testRenderOptionGroupsWithAttributes() {
  323. $select = new SelectBox($this->templates);
  324. $data = [
  325. 'name' => 'Birds[name]',
  326. 'options' => [
  327. [
  328. 'text' => 'Mammal',
  329. 'data-foo' => 'bar',
  330. 'options' => [
  331. 'beaver' => 'Beaver',
  332. 'elk' => 'Elk',
  333. ]
  334. ]
  335. ]
  336. ];
  337. $result = $select->render($data);
  338. $expected = [
  339. 'select' => [
  340. 'name' => 'Birds[name]',
  341. ],
  342. ['optgroup' => ['data-foo' => 'bar', 'label' => 'Mammal']],
  343. ['option' => ['value' => 'beaver']],
  344. 'Beaver',
  345. '/option',
  346. ['option' => ['value' => 'elk']],
  347. 'Elk',
  348. '/option',
  349. '/optgroup',
  350. '/select'
  351. ];
  352. $this->assertTags($result, $expected);
  353. }
  354. /**
  355. * test rendering with option groups with traversable nodes
  356. *
  357. * @return void
  358. */
  359. public function testRenderOptionGroupsTraversable() {
  360. $select = new SelectBox($this->templates);
  361. $mammals = new \ArrayObject(['beaver' => 'Beaver', 'elk' => 'Elk']);
  362. $data = [
  363. 'name' => 'Birds[name]',
  364. 'options' => [
  365. 'Mammal' => $mammals,
  366. 'Bird' => [
  367. 'budgie' => 'Budgie',
  368. 'eagle' => 'Eagle',
  369. ]
  370. ]
  371. ];
  372. $result = $select->render($data);
  373. $expected = [
  374. 'select' => [
  375. 'name' => 'Birds[name]',
  376. ],
  377. ['optgroup' => ['label' => 'Mammal']],
  378. ['option' => ['value' => 'beaver']],
  379. 'Beaver',
  380. '/option',
  381. ['option' => ['value' => 'elk']],
  382. 'Elk',
  383. '/option',
  384. '/optgroup',
  385. ['optgroup' => ['label' => 'Bird']],
  386. ['option' => ['value' => 'budgie']],
  387. 'Budgie',
  388. '/option',
  389. ['option' => ['value' => 'eagle']],
  390. 'Eagle',
  391. '/option',
  392. '/optgroup',
  393. '/select'
  394. ];
  395. $this->assertTags($result, $expected);
  396. }
  397. /**
  398. * test rendering option groups and selected values
  399. *
  400. * @return void
  401. */
  402. public function testRenderOptionGroupsSelectedAndDisabled() {
  403. $select = new SelectBox($this->templates);
  404. $data = [
  405. 'name' => 'Birds[name]',
  406. 'val' => ['1', '2', 'burp'],
  407. 'disabled' => ['1x', '2x', 'nope'],
  408. 'options' => [
  409. 'ones' => [
  410. 1 => 'one',
  411. '1x' => 'one x',
  412. ],
  413. 'twos' => [
  414. '2' => 'two',
  415. '2x' => 'two x',
  416. ]
  417. ]
  418. ];
  419. $result = $select->render($data);
  420. $expected = [
  421. 'select' => [
  422. 'name' => 'Birds[name]',
  423. ],
  424. ['optgroup' => ['label' => 'ones']],
  425. ['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
  426. ['option' => ['value' => '1x', 'disabled' => 'disabled']], 'one x', '/option',
  427. '/optgroup',
  428. ['optgroup' => ['label' => 'twos']],
  429. ['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
  430. ['option' => ['value' => '2x', 'disabled' => 'disabled']], 'two x', '/option',
  431. '/optgroup',
  432. '/select'
  433. ];
  434. $this->assertTags($result, $expected);
  435. }
  436. /**
  437. * test rendering a totally disabled element
  438. *
  439. * @return void
  440. */
  441. public function testRenderDisabled() {
  442. $select = new SelectBox($this->templates);
  443. $data = [
  444. 'disabled' => true,
  445. 'name' => 'Birds[name]',
  446. 'options' => ['a' => 'Albatross', 'b' => 'Budgie'],
  447. 'val' => 'a',
  448. ];
  449. $result = $select->render($data);
  450. $expected = [
  451. 'select' => [
  452. 'name' => 'Birds[name]',
  453. 'disabled' => 'disabled',
  454. ],
  455. ['option' => ['value' => 'a', 'selected' => 'selected']], 'Albatross', '/option',
  456. ['option' => ['value' => 'b']], 'Budgie', '/option',
  457. '/select'
  458. ];
  459. $this->assertTags($result, $expected);
  460. }
  461. /**
  462. * test rendering a disabled element
  463. *
  464. * @return void
  465. */
  466. public function testRenderDisabledMultiple() {
  467. $select = new SelectBox($this->templates);
  468. $data = [
  469. 'disabled' => ['a', 'c'],
  470. 'val' => 'a',
  471. 'name' => 'Birds[name]',
  472. 'options' => [
  473. 'a' => 'Albatross',
  474. 'b' => 'Budgie',
  475. 'c' => 'Canary',
  476. ]
  477. ];
  478. $result = $select->render($data);
  479. $expected = [
  480. 'select' => [
  481. 'name' => 'Birds[name]',
  482. ],
  483. ['option' => ['value' => 'a', 'selected' => 'selected', 'disabled' => 'disabled']],
  484. 'Albatross',
  485. '/option',
  486. ['option' => ['value' => 'b']],
  487. 'Budgie',
  488. '/option',
  489. ['option' => ['value' => 'c', 'disabled' => 'disabled']],
  490. 'Canary',
  491. '/option',
  492. '/select'
  493. ];
  494. $this->assertTags($result, $expected);
  495. }
  496. /**
  497. * test rendering with an empty value
  498. *
  499. * @return void
  500. */
  501. public function testRenderEmptyOption() {
  502. $select = new SelectBox($this->templates);
  503. $data = [
  504. 'id' => 'BirdName',
  505. 'name' => 'Birds[name]',
  506. 'empty' => true,
  507. 'options' => ['a' => 'Albatross', 'b' => 'Budgie']
  508. ];
  509. $result = $select->render($data);
  510. $expected = [
  511. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  512. ['option' => ['value' => '']], '/option',
  513. ['option' => ['value' => 'a']], 'Albatross', '/option',
  514. ['option' => ['value' => 'b']], 'Budgie', '/option',
  515. '/select'
  516. ];
  517. $this->assertTags($result, $expected);
  518. $data['empty'] = 'empty';
  519. $result = $select->render($data);
  520. $expected = [
  521. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  522. ['option' => ['value' => '']], 'empty', '/option',
  523. ['option' => ['value' => 'a']], 'Albatross', '/option',
  524. ['option' => ['value' => 'b']], 'Budgie', '/option',
  525. '/select'
  526. ];
  527. $this->assertTags($result, $expected);
  528. $data['empty'] = 'empty';
  529. $data['val'] = '';
  530. $result = $select->render($data);
  531. $expected = [
  532. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  533. ['option' => ['value' => '', 'selected' => 'selected']], 'empty', '/option',
  534. ['option' => ['value' => 'a']], 'Albatross', '/option',
  535. ['option' => ['value' => 'b']], 'Budgie', '/option',
  536. '/select'
  537. ];
  538. $this->assertTags($result, $expected);
  539. $data['val'] = false;
  540. $result = $select->render($data);
  541. $this->assertTags($result, $expected);
  542. }
  543. /**
  544. * Test rendering with disabling escaping.
  545. *
  546. * @return void
  547. */
  548. public function testRenderEscapingOption() {
  549. $select = new SelectBox($this->templates);
  550. $data = [
  551. 'name' => 'Birds[name]',
  552. 'options' => [
  553. 'a' => '>Albatross',
  554. 'b' => '>Budgie',
  555. 'c' => '>Canary',
  556. ]
  557. ];
  558. $result = $select->render($data);
  559. $expected = [
  560. 'select' => [
  561. 'name' => 'Birds[name]',
  562. ],
  563. ['option' => ['value' => 'a']],
  564. '&gt;Albatross',
  565. '/option',
  566. ['option' => ['value' => 'b']],
  567. '&gt;Budgie',
  568. '/option',
  569. ['option' => ['value' => 'c']],
  570. '&gt;Canary',
  571. '/option',
  572. '/select'
  573. ];
  574. $this->assertTags($result, $expected);
  575. $data = [
  576. 'escape' => false,
  577. 'name' => 'Birds[name]',
  578. 'options' => [
  579. '>a' => '>Albatross',
  580. ]
  581. ];
  582. $result = $select->render($data);
  583. $expected = [
  584. 'select' => [
  585. 'name' => 'Birds[name]',
  586. ],
  587. ['option' => ['value' => '>a']],
  588. '>Albatross',
  589. '/option',
  590. '/select'
  591. ];
  592. $this->assertTags($result, $expected);
  593. }
  594. }