| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\View\Widget;
- use Cake\Collection\Collection;
- use Cake\TestSuite\TestCase;
- use Cake\View\StringTemplate;
- use Cake\View\Widget\SelectBoxWidget;
- /**
- * SelectBox test case
- */
- class SelectBoxWidgetTest extends TestCase
- {
- /**
- * setup method.
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $templates = [
- 'select' => '<select name="{{name}}"{{attrs}}>{{content}}</select>',
- 'selectMultiple' => '<select name="{{name}}[]" multiple="multiple"{{attrs}}>{{content}}</select>',
- 'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>',
- 'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
- ];
- $this->context = $this->getMock('Cake\View\Form\ContextInterface');
- $this->templates = new StringTemplate($templates);
- }
- /**
- * test render no options
- *
- * @return void
- */
- public function testRenderNoOptions()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'id' => 'BirdName',
- 'name' => 'Birds[name]',
- 'options' => []
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test simple rendering
- *
- * @return void
- */
- public function testRenderSimple()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'id' => 'BirdName',
- 'name' => 'Birds[name]',
- 'options' => ['a' => 'Albatross', 'b' => 'Budgie']
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
- ['option' => ['value' => 'a']], 'Albatross', '/option',
- ['option' => ['value' => 'b']], 'Budgie', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test simple iterator rendering
- *
- * @return void
- */
- public function testRenderSimpleIterator()
- {
- $select = new SelectBoxWidget($this->templates);
- $options = new \ArrayObject(['a' => 'Albatross', 'b' => 'Budgie']);
- $data = [
- 'name' => 'Birds[name]',
- 'options' => $options,
- 'empty' => true
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => ['name' => 'Birds[name]'],
- ['option' => ['value' => '']], '/option',
- ['option' => ['value' => 'a']], 'Albatross', '/option',
- ['option' => ['value' => 'b']], 'Budgie', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test simple iterator rendering with empty option
- *
- * @return void
- */
- public function testRenderSimpleIteratorWithEmpty()
- {
- $select = new SelectBoxWidget($this->templates);
- $options = new Collection(['a' => 'Albatross', 'b' => 'Budgie']);
- $data = [
- 'name' => 'Birds[name]',
- 'options' => $options,
- 'empty' => 'Pick one'
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => ['name' => 'Birds[name]'],
- ['option' => ['value' => '']], 'Pick one', '/option',
- ['option' => ['value' => 'a']], 'Albatross', '/option',
- ['option' => ['value' => 'b']], 'Budgie', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test complex option rendering
- *
- * @return void
- */
- public function testRenderComplex()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'id' => 'BirdName',
- 'name' => 'Birds[name]',
- 'options' => [
- ['value' => 'a', 'text' => 'Albatross'],
- ['value' => 'b', 'text' => 'Budgie', 'data-foo' => 'bar'],
- ]
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
- ['option' => ['value' => 'a']],
- 'Albatross',
- '/option',
- ['option' => ['value' => 'b', 'data-foo' => 'bar']],
- 'Budgie',
- '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test rendering with a selected value
- *
- * @return void
- */
- public function testRenderSelected()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'id' => 'BirdName',
- 'name' => 'Birds[name]',
- 'val' => '1',
- 'options' => [
- 1 => 'one',
- '1x' => 'one x',
- '2' => 'two',
- '2x' => 'two x',
- ]
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
- ['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
- ['option' => ['value' => '1x']], 'one x', '/option',
- ['option' => ['value' => '2']], 'two', '/option',
- ['option' => ['value' => '2x']], 'two x', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $data['val'] = 2;
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
- ['option' => ['value' => '1']], 'one', '/option',
- ['option' => ['value' => '1x']], 'one x', '/option',
- ['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
- ['option' => ['value' => '2x']], 'two x', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test complex option rendering with a selected value
- *
- * @return void
- */
- public function testRenderComplexSelected()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'id' => 'BirdName',
- 'name' => 'Birds[name]',
- 'val' => 'a',
- 'options' => [
- ['value' => 'a', 'text' => 'Albatross'],
- ['value' => 'b', 'text' => 'Budgie', 'data-foo' => 'bar'],
- ]
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
- ['option' => ['value' => 'a', 'selected' => 'selected']],
- 'Albatross',
- '/option',
- ['option' => ['value' => 'b', 'data-foo' => 'bar']],
- 'Budgie',
- '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test rendering a multi select
- *
- * @return void
- */
- public function testRenderMultipleSelect()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'id' => 'BirdName',
- 'name' => 'Birds[name]',
- 'multiple' => true,
- 'options' => ['a' => 'Albatross', 'b' => 'Budgie']
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => [
- 'name' => 'Birds[name][]',
- 'id' => 'BirdName',
- 'multiple' => 'multiple',
- ],
- ['option' => ['value' => 'a']], 'Albatross', '/option',
- ['option' => ['value' => 'b']], 'Budgie', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test rendering multi select & selected values
- *
- * @return void
- */
- public function testRenderMultipleSelected()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'multiple' => true,
- 'id' => 'BirdName',
- 'name' => 'Birds[name]',
- 'val' => ['1', '2', 'burp'],
- 'options' => [
- 1 => 'one',
- '1x' => 'one x',
- '2' => 'two',
- '2x' => 'two x',
- ]
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => [
- 'name' => 'Birds[name][]',
- 'multiple' => 'multiple',
- 'id' => 'BirdName'
- ],
- ['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
- ['option' => ['value' => '1x']], 'one x', '/option',
- ['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
- ['option' => ['value' => '2x']], 'two x', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test rendering with option groups
- *
- * @return void
- */
- public function testRenderOptionGroups()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'name' => 'Birds[name]',
- 'options' => [
- 'Mammal' => [
- 'beaver' => 'Beaver',
- 'elk' => 'Elk',
- ],
- 'Bird' => [
- 'budgie' => 'Budgie',
- 'eagle' => 'Eagle',
- ]
- ]
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => [
- 'name' => 'Birds[name]',
- ],
- ['optgroup' => ['label' => 'Mammal']],
- ['option' => ['value' => 'beaver']],
- 'Beaver',
- '/option',
- ['option' => ['value' => 'elk']],
- 'Elk',
- '/option',
- '/optgroup',
- ['optgroup' => ['label' => 'Bird']],
- ['option' => ['value' => 'budgie']],
- 'Budgie',
- '/option',
- ['option' => ['value' => 'eagle']],
- 'Eagle',
- '/option',
- '/optgroup',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test rendering with option groups and escaping
- *
- * @return void
- */
- public function testRenderOptionGroupsEscape()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'name' => 'Birds[name]',
- 'options' => [
- '>XSS<' => [
- '1' => 'One>',
- ],
- ]
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => [
- 'name' => 'Birds[name]',
- ],
- ['optgroup' => ['label' => '>XSS<']],
- ['option' => ['value' => '1']],
- 'One>',
- '/option',
- '/optgroup',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $data['escape'] = false;
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => [
- 'name' => 'Birds[name]',
- ],
- ['optgroup' => ['label' => '>XSS<']],
- ['option' => ['value' => '1']],
- 'One>',
- '/option',
- '/optgroup',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test rendering with option groups
- *
- * @return void
- */
- public function testRenderOptionGroupsWithAttributes()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'name' => 'Birds[name]',
- 'options' => [
- [
- 'text' => 'Mammal',
- 'data-foo' => 'bar',
- 'options' => [
- 'beaver' => 'Beaver',
- 'elk' => 'Elk',
- ]
- ]
- ]
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => [
- 'name' => 'Birds[name]',
- ],
- ['optgroup' => ['data-foo' => 'bar', 'label' => 'Mammal']],
- ['option' => ['value' => 'beaver']],
- 'Beaver',
- '/option',
- ['option' => ['value' => 'elk']],
- 'Elk',
- '/option',
- '/optgroup',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test rendering with option groups with traversable nodes
- *
- * @return void
- */
- public function testRenderOptionGroupsTraversable()
- {
- $select = new SelectBoxWidget($this->templates);
- $mammals = new \ArrayObject(['beaver' => 'Beaver', 'elk' => 'Elk']);
- $data = [
- 'name' => 'Birds[name]',
- 'options' => [
- 'Mammal' => $mammals,
- 'Bird' => [
- 'budgie' => 'Budgie',
- 'eagle' => 'Eagle',
- ]
- ]
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => [
- 'name' => 'Birds[name]',
- ],
- ['optgroup' => ['label' => 'Mammal']],
- ['option' => ['value' => 'beaver']],
- 'Beaver',
- '/option',
- ['option' => ['value' => 'elk']],
- 'Elk',
- '/option',
- '/optgroup',
- ['optgroup' => ['label' => 'Bird']],
- ['option' => ['value' => 'budgie']],
- 'Budgie',
- '/option',
- ['option' => ['value' => 'eagle']],
- 'Eagle',
- '/option',
- '/optgroup',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test rendering option groups and selected values
- *
- * @return void
- */
- public function testRenderOptionGroupsSelectedAndDisabled()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'name' => 'Birds[name]',
- 'val' => ['1', '2', 'burp'],
- 'disabled' => ['1x', '2x', 'nope'],
- 'options' => [
- 'ones' => [
- 1 => 'one',
- '1x' => 'one x',
- ],
- 'twos' => [
- '2' => 'two',
- '2x' => 'two x',
- ]
- ]
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => [
- 'name' => 'Birds[name]',
- ],
- ['optgroup' => ['label' => 'ones']],
- ['option' => ['value' => '1', 'selected' => 'selected']], 'one', '/option',
- ['option' => ['value' => '1x', 'disabled' => 'disabled']], 'one x', '/option',
- '/optgroup',
- ['optgroup' => ['label' => 'twos']],
- ['option' => ['value' => '2', 'selected' => 'selected']], 'two', '/option',
- ['option' => ['value' => '2x', 'disabled' => 'disabled']], 'two x', '/option',
- '/optgroup',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test rendering a totally disabled element
- *
- * @return void
- */
- public function testRenderDisabled()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'disabled' => true,
- 'name' => 'Birds[name]',
- 'options' => ['a' => 'Albatross', 'b' => 'Budgie'],
- 'val' => 'a',
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => [
- 'name' => 'Birds[name]',
- 'disabled' => 'disabled',
- ],
- ['option' => ['value' => 'a', 'selected' => 'selected']], 'Albatross', '/option',
- ['option' => ['value' => 'b']], 'Budgie', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'disabled' => [1],
- 'name' => 'numbers',
- 'options' => ['1' => 'One', '2' => 'Two'],
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => [
- 'name' => 'numbers',
- ],
- ['option' => ['value' => '1', 'disabled' => 'disabled']], 'One', '/option',
- ['option' => ['value' => '2']], 'Two', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test rendering a disabled element
- *
- * @return void
- */
- public function testRenderDisabledMultiple()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'disabled' => ['a', 'c'],
- 'val' => 'a',
- 'name' => 'Birds[name]',
- 'options' => [
- 'a' => 'Albatross',
- 'b' => 'Budgie',
- 'c' => 'Canary',
- ]
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => [
- 'name' => 'Birds[name]',
- ],
- ['option' => ['value' => 'a', 'selected' => 'selected', 'disabled' => 'disabled']],
- 'Albatross',
- '/option',
- ['option' => ['value' => 'b']],
- 'Budgie',
- '/option',
- ['option' => ['value' => 'c', 'disabled' => 'disabled']],
- 'Canary',
- '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test complex option rendering with a disabled element
- *
- * @return void
- */
- public function testRenderComplexDisabled()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'disabled' => ['b'],
- 'id' => 'BirdName',
- 'name' => 'Birds[name]',
- 'options' => [
- ['value' => 'a', 'text' => 'Albatross'],
- ['value' => 'b', 'text' => 'Budgie', 'data-foo' => 'bar'],
- ]
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
- ['option' => ['value' => 'a']],
- 'Albatross',
- '/option',
- ['option' => ['value' => 'b', 'data-foo' => 'bar', 'disabled' => 'disabled']],
- 'Budgie',
- '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test rendering with an empty value
- *
- * @return void
- */
- public function testRenderEmptyOption()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'id' => 'BirdName',
- 'name' => 'Birds[name]',
- 'empty' => true,
- 'options' => ['a' => 'Albatross', 'b' => 'Budgie']
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
- ['option' => ['value' => '']], '/option',
- ['option' => ['value' => 'a']], 'Albatross', '/option',
- ['option' => ['value' => 'b']], 'Budgie', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $data['empty'] = 'empty';
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
- ['option' => ['value' => '']], 'empty', '/option',
- ['option' => ['value' => 'a']], 'Albatross', '/option',
- ['option' => ['value' => 'b']], 'Budgie', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $data['empty'] = 'empty';
- $data['val'] = '';
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
- ['option' => ['value' => '', 'selected' => 'selected']], 'empty', '/option',
- ['option' => ['value' => 'a']], 'Albatross', '/option',
- ['option' => ['value' => 'b']], 'Budgie', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $data['val'] = false;
- $result = $select->render($data, $this->context);
- $this->assertHtml($expected, $result);
- }
- /**
- * Test rendering with disabling escaping.
- *
- * @return void
- */
- public function testRenderEscapingOption()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'name' => 'Birds[name]',
- 'options' => [
- 'a' => '>Albatross',
- 'b' => '>Budgie',
- 'c' => '>Canary',
- ]
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => [
- 'name' => 'Birds[name]',
- ],
- ['option' => ['value' => 'a']],
- '>Albatross',
- '/option',
- ['option' => ['value' => 'b']],
- '>Budgie',
- '/option',
- ['option' => ['value' => 'c']],
- '>Canary',
- '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $data = [
- 'escape' => false,
- 'name' => 'Birds[name]',
- 'options' => [
- '>a' => '>Albatross',
- ]
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => [
- 'name' => 'Birds[name]',
- ],
- ['option' => ['value' => '>a']],
- '>Albatross',
- '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test render with null options
- *
- * @return void
- */
- public function testRenderNullOptions()
- {
- $select = new SelectBoxWidget($this->templates);
- $data = [
- 'id' => 'BirdName',
- 'name' => 'Birds[name]',
- 'options' => null
- ];
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $data['empty'] = true;
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
- ['option' => ['value' => '']], '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- $data['empty'] = 'empty';
- $result = $select->render($data, $this->context);
- $expected = [
- 'select' => ['name' => 'Birds[name]', 'id' => 'BirdName'],
- ['option' => ['value' => '']], 'empty', '/option',
- '/select'
- ];
- $this->assertHtml($expected, $result);
- }
- }
|