SelectBoxTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. $select = new SelectBox($this->templates);
  461. $data = [
  462. 'disabled' => [1],
  463. 'name' => 'numbers',
  464. 'options' => ['1' => 'One', '2' => 'Two'],
  465. ];
  466. $result = $select->render($data);
  467. $expected = [
  468. 'select' => [
  469. 'name' => 'numbers',
  470. ],
  471. ['option' => ['value' => '1', 'disabled' => 'disabled']], 'One', '/option',
  472. ['option' => ['value' => '2']], 'Two', '/option',
  473. '/select'
  474. ];
  475. $this->assertTags($result, $expected);
  476. }
  477. /**
  478. * test rendering a disabled element
  479. *
  480. * @return void
  481. */
  482. public function testRenderDisabledMultiple() {
  483. $select = new SelectBox($this->templates);
  484. $data = [
  485. 'disabled' => ['a', 'c'],
  486. 'val' => 'a',
  487. 'name' => 'Birds[name]',
  488. 'options' => [
  489. 'a' => 'Albatross',
  490. 'b' => 'Budgie',
  491. 'c' => 'Canary',
  492. ]
  493. ];
  494. $result = $select->render($data);
  495. $expected = [
  496. 'select' => [
  497. 'name' => 'Birds[name]',
  498. ],
  499. ['option' => ['value' => 'a', 'selected' => 'selected', 'disabled' => 'disabled']],
  500. 'Albatross',
  501. '/option',
  502. ['option' => ['value' => 'b']],
  503. 'Budgie',
  504. '/option',
  505. ['option' => ['value' => 'c', 'disabled' => 'disabled']],
  506. 'Canary',
  507. '/option',
  508. '/select'
  509. ];
  510. $this->assertTags($result, $expected);
  511. }
  512. /**
  513. * test rendering with an empty value
  514. *
  515. * @return void
  516. */
  517. public function testRenderEmptyOption() {
  518. $select = new SelectBox($this->templates);
  519. $data = [
  520. 'id' => 'BirdName',
  521. 'name' => 'Birds[name]',
  522. 'empty' => true,
  523. 'options' => ['a' => 'Albatross', 'b' => 'Budgie']
  524. ];
  525. $result = $select->render($data);
  526. $expected = [
  527. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  528. ['option' => ['value' => '']], '/option',
  529. ['option' => ['value' => 'a']], 'Albatross', '/option',
  530. ['option' => ['value' => 'b']], 'Budgie', '/option',
  531. '/select'
  532. ];
  533. $this->assertTags($result, $expected);
  534. $data['empty'] = 'empty';
  535. $result = $select->render($data);
  536. $expected = [
  537. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  538. ['option' => ['value' => '']], 'empty', '/option',
  539. ['option' => ['value' => 'a']], 'Albatross', '/option',
  540. ['option' => ['value' => 'b']], 'Budgie', '/option',
  541. '/select'
  542. ];
  543. $this->assertTags($result, $expected);
  544. $data['empty'] = 'empty';
  545. $data['val'] = '';
  546. $result = $select->render($data);
  547. $expected = [
  548. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  549. ['option' => ['value' => '', 'selected' => 'selected']], 'empty', '/option',
  550. ['option' => ['value' => 'a']], 'Albatross', '/option',
  551. ['option' => ['value' => 'b']], 'Budgie', '/option',
  552. '/select'
  553. ];
  554. $this->assertTags($result, $expected);
  555. $data['val'] = false;
  556. $result = $select->render($data);
  557. $this->assertTags($result, $expected);
  558. }
  559. /**
  560. * Test rendering with disabling escaping.
  561. *
  562. * @return void
  563. */
  564. public function testRenderEscapingOption() {
  565. $select = new SelectBox($this->templates);
  566. $data = [
  567. 'name' => 'Birds[name]',
  568. 'options' => [
  569. 'a' => '>Albatross',
  570. 'b' => '>Budgie',
  571. 'c' => '>Canary',
  572. ]
  573. ];
  574. $result = $select->render($data);
  575. $expected = [
  576. 'select' => [
  577. 'name' => 'Birds[name]',
  578. ],
  579. ['option' => ['value' => 'a']],
  580. '&gt;Albatross',
  581. '/option',
  582. ['option' => ['value' => 'b']],
  583. '&gt;Budgie',
  584. '/option',
  585. ['option' => ['value' => 'c']],
  586. '&gt;Canary',
  587. '/option',
  588. '/select'
  589. ];
  590. $this->assertTags($result, $expected);
  591. $data = [
  592. 'escape' => false,
  593. 'name' => 'Birds[name]',
  594. 'options' => [
  595. '>a' => '>Albatross',
  596. ]
  597. ];
  598. $result = $select->render($data);
  599. $expected = [
  600. 'select' => [
  601. 'name' => 'Birds[name]',
  602. ],
  603. ['option' => ['value' => '>a']],
  604. '>Albatross',
  605. '/option',
  606. '/select'
  607. ];
  608. $this->assertTags($result, $expected);
  609. }
  610. }