SelectBoxTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 CakePHP(tm) v3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Input;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\View\Input\SelectBox;
  18. use Cake\View\StringTemplate;
  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
  276. *
  277. * @return void
  278. */
  279. public function testRenderOptionGroupsWithAttributes() {
  280. $select = new SelectBox($this->templates);
  281. $data = [
  282. 'name' => 'Birds[name]',
  283. 'options' => [
  284. [
  285. 'text' => 'Mammal',
  286. 'data-foo' => 'bar',
  287. 'options' => [
  288. 'beaver' => 'Beaver',
  289. 'elk' => 'Elk',
  290. ]
  291. ]
  292. ]
  293. ];
  294. $result = $select->render($data);
  295. $expected = [
  296. 'select' => [
  297. 'name' => 'Birds[name]',
  298. ],
  299. ['optgroup' => ['data-foo' => 'bar', 'label' => 'Mammal']],
  300. ['option' => ['value' => 'beaver']],
  301. 'Beaver',
  302. '/option',
  303. ['option' => ['value' => 'elk']],
  304. 'Elk',
  305. '/option',
  306. '/optgroup',
  307. '/select'
  308. ];
  309. $this->assertTags($result, $expected);
  310. }
  311. /**
  312. * test rendering with option groups with traversable nodes
  313. *
  314. * @return void
  315. */
  316. public function testRenderOptionGroupsTraversable() {
  317. $select = new SelectBox($this->templates);
  318. $mammals = new \ArrayObject(['beaver' => 'Beaver', 'elk' => 'Elk']);
  319. $data = [
  320. 'name' => 'Birds[name]',
  321. 'options' => [
  322. 'Mammal' => $mammals,
  323. 'Bird' => [
  324. 'budgie' => 'Budgie',
  325. 'eagle' => 'Eagle',
  326. ]
  327. ]
  328. ];
  329. $result = $select->render($data);
  330. $expected = [
  331. 'select' => [
  332. 'name' => 'Birds[name]',
  333. ],
  334. ['optgroup' => ['label' => 'Mammal']],
  335. ['option' => ['value' => 'beaver']],
  336. 'Beaver',
  337. '/option',
  338. ['option' => ['value' => 'elk']],
  339. 'Elk',
  340. '/option',
  341. '/optgroup',
  342. ['optgroup' => ['label' => 'Bird']],
  343. ['option' => ['value' => 'budgie']],
  344. 'Budgie',
  345. '/option',
  346. ['option' => ['value' => 'eagle']],
  347. 'Eagle',
  348. '/option',
  349. '/optgroup',
  350. '/select'
  351. ];
  352. $this->assertTags($result, $expected);
  353. }
  354. /**
  355. * test rendering option groups and selected values
  356. *
  357. * @return void
  358. */
  359. public function testRenderOptionGroupsSelectedAndDisabled() {
  360. $select = new SelectBox($this->templates);
  361. $data = [
  362. 'name' => 'Birds[name]',
  363. 'val' => ['1', '2', 'burp'],
  364. 'disabled' => ['1x', '2x', 'nope'],
  365. 'options' => [
  366. 'ones' => [
  367. 1 => 'one',
  368. '1x' => 'one x',
  369. ],
  370. 'twos' => [
  371. '2' => 'two',
  372. '2x' => 'two x',
  373. ]
  374. ]
  375. ];
  376. $result = $select->render($data);
  377. $expected = [
  378. 'select' => [
  379. 'name' => 'Birds[name]',
  380. ],
  381. ['optgroup' => ['label' => 'ones']],
  382. ['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
  383. ['option' => ['value' => '1x', 'disabled' => 'disabled']], 'one x', '/option',
  384. '/optgroup',
  385. ['optgroup' => ['label' => 'twos']],
  386. ['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
  387. ['option' => ['value' => '2x', 'disabled' => 'disabled']], 'two x', '/option',
  388. '/optgroup',
  389. '/select'
  390. ];
  391. $this->assertTags($result, $expected);
  392. }
  393. /**
  394. * test rendering a totally disabled element
  395. *
  396. * @return void
  397. */
  398. public function testRenderDisabled() {
  399. $select = new SelectBox($this->templates);
  400. $data = [
  401. 'disabled' => true,
  402. 'name' => 'Birds[name]',
  403. 'options' => ['a' => 'Albatross', 'b' => 'Budgie'],
  404. 'val' => 'a',
  405. ];
  406. $result = $select->render($data);
  407. $expected = [
  408. 'select' => [
  409. 'name' => 'Birds[name]',
  410. 'disabled' => 'disabled',
  411. ],
  412. ['option' => ['value' => 'a', 'selected' => 'selected']], 'Albatross', '/option',
  413. ['option' => ['value' => 'b']], 'Budgie', '/option',
  414. '/select'
  415. ];
  416. $this->assertTags($result, $expected);
  417. }
  418. /**
  419. * test rendering a disabled element
  420. *
  421. * @return void
  422. */
  423. public function testRenderDisabledMultiple() {
  424. $select = new SelectBox($this->templates);
  425. $data = [
  426. 'disabled' => ['a', 'c'],
  427. 'val' => 'a',
  428. 'name' => 'Birds[name]',
  429. 'options' => [
  430. 'a' => 'Albatross',
  431. 'b' => 'Budgie',
  432. 'c' => 'Canary',
  433. ]
  434. ];
  435. $result = $select->render($data);
  436. $expected = [
  437. 'select' => [
  438. 'name' => 'Birds[name]',
  439. ],
  440. ['option' => ['value' => 'a', 'selected' => 'selected', 'disabled' => 'disabled']],
  441. 'Albatross',
  442. '/option',
  443. ['option' => ['value' => 'b']],
  444. 'Budgie',
  445. '/option',
  446. ['option' => ['value' => 'c', 'disabled' => 'disabled']],
  447. 'Canary',
  448. '/option',
  449. '/select'
  450. ];
  451. $this->assertTags($result, $expected);
  452. }
  453. /**
  454. * test rendering with an empty value
  455. *
  456. * @return void
  457. */
  458. public function testRenderEmptyOption() {
  459. $select = new SelectBox($this->templates);
  460. $data = [
  461. 'id' => 'BirdName',
  462. 'name' => 'Birds[name]',
  463. 'empty' => true,
  464. 'options' => ['a' => 'Albatross', 'b' => 'Budgie']
  465. ];
  466. $result = $select->render($data);
  467. $expected = [
  468. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  469. ['option' => ['value' => '']], '/option',
  470. ['option' => ['value' => 'a']], 'Albatross', '/option',
  471. ['option' => ['value' => 'b']], 'Budgie', '/option',
  472. '/select'
  473. ];
  474. $this->assertTags($result, $expected);
  475. $data['empty'] = 'empty';
  476. $result = $select->render($data);
  477. $expected = [
  478. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  479. ['option' => ['value' => '']], 'empty', '/option',
  480. ['option' => ['value' => 'a']], 'Albatross', '/option',
  481. ['option' => ['value' => 'b']], 'Budgie', '/option',
  482. '/select'
  483. ];
  484. $this->assertTags($result, $expected);
  485. $data['empty'] = 'empty';
  486. $data['val'] = '';
  487. $result = $select->render($data);
  488. $expected = [
  489. 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
  490. ['option' => ['value' => '', 'selected' => 'selected']], 'empty', '/option',
  491. ['option' => ['value' => 'a']], 'Albatross', '/option',
  492. ['option' => ['value' => 'b']], 'Budgie', '/option',
  493. '/select'
  494. ];
  495. $this->assertTags($result, $expected);
  496. $data['val'] = false;
  497. $result = $select->render($data);
  498. $this->assertTags($result, $expected);
  499. }
  500. /**
  501. * Test rendering with disabling escaping.
  502. *
  503. * @return void
  504. */
  505. public function testRenderEscapingOption() {
  506. $select = new SelectBox($this->templates);
  507. $data = [
  508. 'name' => 'Birds[name]',
  509. 'options' => [
  510. 'a' => '>Albatross',
  511. 'b' => '>Budgie',
  512. 'c' => '>Canary',
  513. ]
  514. ];
  515. $result = $select->render($data);
  516. $expected = [
  517. 'select' => [
  518. 'name' => 'Birds[name]',
  519. ],
  520. ['option' => ['value' => 'a']],
  521. '&gt;Albatross',
  522. '/option',
  523. ['option' => ['value' => 'b']],
  524. '&gt;Budgie',
  525. '/option',
  526. ['option' => ['value' => 'c']],
  527. '&gt;Canary',
  528. '/option',
  529. '/select'
  530. ];
  531. $this->assertTags($result, $expected);
  532. $data = [
  533. 'escape' => false,
  534. 'name' => 'Birds[name]',
  535. 'options' => [
  536. '>a' => '>Albatross',
  537. ]
  538. ];
  539. $result = $select->render($data);
  540. $expected = [
  541. 'select' => [
  542. 'name' => 'Birds[name]',
  543. ],
  544. ['option' => ['value' => '>a']],
  545. '>Albatross',
  546. '/option',
  547. '/select'
  548. ];
  549. $this->assertTags($result, $expected);
  550. }
  551. }