| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\View\Widget;
- use Cake\TestSuite\TestCase;
- use Cake\View\StringTemplate;
- use Cake\View\Widget\LabelWidget;
- use Cake\View\Widget\MultiCheckboxWidget;
- use Cake\View\Widget\NestingLabelWidget;
- /**
- * MultiCheckbox test case.
- */
- class MultiCheckboxWidgetTest extends TestCase
- {
- /**
- * setup method.
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $templates = [
- 'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
- 'label' => '<label{{attrs}}>{{text}}</label>',
- 'nestingLabel' => '<label{{attrs}}>{{input}}{{text}}</label>',
- 'checkboxWrapper' => '<div class="checkbox">{{input}}{{label}}</div>',
- 'multicheckboxWrapper' => '<fieldset{{attrs}}>{{content}}</fieldset>',
- 'multicheckboxTitle' => '<legend>{{text}}</legend>',
- 'selectedClass' => 'selected',
- ];
- $this->templates = new StringTemplate($templates);
- $this->context = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
- }
- /**
- * Test render simple option sets.
- *
- * @return void
- */
- public function testRenderSimple()
- {
- $label = new LabelWidget($this->templates);
- $input = new MultiCheckboxWidget($this->templates, $label);
- $data = [
- 'name' => 'Tags[id]',
- 'options' => [
- 1 => 'CakePHP',
- 2 => 'Development',
- ],
- ];
- $result = $input->render($data, $this->context);
- $expected = [
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => 1,
- 'id' => 'tags-id-1',
- ]],
- ['label' => ['for' => 'tags-id-1']],
- 'CakePHP',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => 2,
- 'id' => 'tags-id-2',
- ]],
- ['label' => ['for' => 'tags-id-2']],
- 'Development',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test render complex and additional attributes.
- *
- * @return void
- */
- public function testRenderComplex()
- {
- $label = new LabelWidget($this->templates);
- $input = new MultiCheckboxWidget($this->templates, $label);
- $data = [
- 'name' => 'Tags[id]',
- 'val' => 2,
- 'disabled' => ['1'],
- 'options' => [
- ['value' => '1', 'text' => 'CakePHP', 'data-test' => 'val'],
- ['value' => '2', 'text' => 'Development', 'class' => 'custom'],
- ],
- ];
- $result = $input->render($data, $this->context);
- $expected = [
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'disabled' => 'disabled',
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => 1,
- 'id' => 'tags-id-1',
- 'data-test' => 'val',
- ]],
- ['label' => ['for' => 'tags-id-1']],
- 'CakePHP',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'checked' => 'checked',
- 'name' => 'Tags[id][]',
- 'value' => 2,
- 'id' => 'tags-id-2',
- 'class' => 'custom',
- ]],
- ['label' => ['class' => 'selected', 'for' => 'tags-id-2']],
- 'Development',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test render escpaing options.
- *
- * @return void
- */
- public function testRenderEscaping()
- {
- $label = new LabelWidget($this->templates);
- $input = new MultiCheckboxWidget($this->templates, $label);
- $data = [
- 'name' => 'Tags[id]',
- 'options' => [
- '>' => '>>',
- ],
- ];
- $result = $input->render($data, $this->context);
- $expected = [
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => '>',
- 'id' => 'tags-id',
- ]],
- ['label' => ['for' => 'tags-id']],
- '>>',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test render selected checkboxes.
- *
- * @return void
- */
- public function testRenderSelected()
- {
- $label = new LabelWidget($this->templates);
- $input = new MultiCheckboxWidget($this->templates, $label);
- $data = [
- 'name' => 'Tags[id]',
- 'options' => [
- 1 => 'CakePHP',
- '1x' => 'Development',
- ],
- 'val' => [1],
- 'disabled' => false,
- ];
- $result = $input->render($data, $this->context);
- $expected = [
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => 1,
- 'id' => 'tags-id-1',
- 'checked' => 'checked',
- ]],
- ['label' => ['class' => 'selected', 'for' => 'tags-id-1']],
- 'CakePHP',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => '1x',
- 'id' => 'tags-id-1x',
- ]],
- ['label' => ['for' => 'tags-id-1x']],
- 'Development',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $data['val'] = 1;
- $result = $input->render($data, $this->context);
- $this->assertHtml($expected, $result);
- $data['val'] = '1';
- $result = $input->render($data, $this->context);
- $this->assertHtml($expected, $result);
- }
- /**
- * Test render disabled checkboxes.
- *
- * @return void
- */
- public function testRenderDisabled()
- {
- $label = new LabelWidget($this->templates);
- $input = new MultiCheckboxWidget($this->templates, $label);
- $data = [
- 'name' => 'Tags[id]',
- 'options' => [
- 1 => 'CakePHP',
- '1x' => 'Development',
- ],
- 'disabled' => true,
- ];
- $result = $input->render($data, $this->context);
- $expected = [
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => 1,
- 'id' => 'tags-id-1',
- 'disabled' => 'disabled',
- ]],
- ['label' => ['for' => 'tags-id-1']],
- 'CakePHP',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => '1x',
- 'id' => 'tags-id-1x',
- 'disabled' => 'disabled',
- ]],
- ['label' => ['for' => 'tags-id-1x']],
- 'Development',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $data['disabled'] = 'a string';
- $result = $input->render($data, $this->context);
- $this->assertHtml($expected, $result);
- $data['disabled'] = ['1', '1x'];
- $this->assertHtml($expected, $result);
- $data = [
- 'name' => 'Tags[id]',
- 'options' => [
- 1 => 'CakePHP',
- '1x' => 'Development',
- ],
- 'disabled' => [1],
- ];
- $result = $input->render($data, $this->context);
- $expected = [
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => 1,
- 'id' => 'tags-id-1',
- 'disabled' => 'disabled',
- ]],
- ['label' => ['for' => 'tags-id-1']],
- 'CakePHP',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => '1x',
- 'id' => 'tags-id-1x',
- ]],
- ['label' => ['for' => 'tags-id-1x']],
- 'Development',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test render templateVars
- *
- * @return void
- */
- public function testRenderTemplateVars()
- {
- $templates = [
- 'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}" data-var="{{inputVar}}" {{attrs}}>',
- 'label' => '<label{{attrs}}>{{text}} {{inputVar}}</label>',
- 'checkboxWrapper' => '<div class="checkbox" data-wrap="{{wrapVar}}">{{input}}{{label}}</div>',
- ];
- $this->templates->add($templates);
- $label = new LabelWidget($this->templates);
- $input = new MultiCheckboxWidget($this->templates, $label);
- $data = [
- 'name' => 'Tags[id]',
- 'options' => [
- ['value' => '1', 'text' => 'CakePHP', 'templateVars' => ['inputVar' => 'i-var']],
- '1x' => 'Development',
- ],
- 'templateVars' => ['inputVar' => 'default', 'wrapVar' => 'val'],
- ];
- $result = $input->render($data, $this->context);
- $expected = [
- ['div' => ['class' => 'checkbox', 'data-wrap' => 'val']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => 1,
- 'id' => 'tags-id-1',
- 'data-var' => 'i-var',
- ]],
- ['label' => ['for' => 'tags-id-1']],
- 'CakePHP i-var',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox', 'data-wrap' => 'val']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => '1x',
- 'id' => 'tags-id-1x',
- 'data-var' => 'default',
- ]],
- ['label' => ['for' => 'tags-id-1x']],
- 'Development default',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test label = false with checkboxWrapper option.
- *
- * @return void
- */
- public function testNoLabelWithCheckboxWrapperOption()
- {
- $data = [
- 'label' => false,
- 'name' => 'test',
- 'options' => [
- 1 => 'A',
- 2 => 'B',
- ],
- ];
- $label = new LabelWidget($this->templates);
- $input = new MultiCheckboxWidget($this->templates, $label);
- $result = $input->render($data, $this->context);
- $expected = [
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'test[]',
- 'value' => 1,
- 'id' => 'test-1',
- ]],
- ['label' => ['for' => 'test-1']],
- 'A',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'test[]',
- 'value' => '2',
- 'id' => 'test-2',
- ]],
- ['label' => ['for' => 'test-2']],
- 'B',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $templates = [
- 'checkboxWrapper' => '<div class="checkbox">{{label}}</div>',
- ];
- $this->templates->add($templates);
- $result = $input->render($data, $this->context);
- $expected = [
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'test[]',
- 'value' => 1,
- 'id' => 'test-1',
- ]],
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'test[]',
- 'value' => '2',
- 'id' => 'test-2',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test rendering without input nesting inspite of using NestingLabelWidget
- *
- * @return void
- */
- public function testRenderNestingLabelWidgetWithoutInputNesting()
- {
- $label = new NestingLabelWidget($this->templates);
- $input = new MultiCheckboxWidget($this->templates, $label);
- $data = [
- 'name' => 'tags',
- 'label' => [
- 'input' => false,
- ],
- 'options' => [
- 1 => 'CakePHP',
- ],
- ];
- $result = $input->render($data, $this->context);
- $expected = [
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'tags[]',
- 'value' => 1,
- 'id' => 'tags-1',
- ]],
- ['label' => ['for' => 'tags-1']],
- 'CakePHP',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test render with groupings.
- *
- * @return void
- */
- public function testRenderGrouped()
- {
- $label = new LabelWidget($this->templates);
- $input = new MultiCheckboxWidget($this->templates, $label);
- $data = [
- 'name' => 'Tags[id]',
- 'options' => [
- 'Group 1' => [
- 1 => 'CakePHP',
- ],
- 'Group 2' => [
- 2 => 'Development',
- ],
- ],
- ];
- $result = $input->render($data, $this->context);
- $expected = [
- '<fieldset',
- '<legend', 'Group 1', '/legend',
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => 1,
- 'id' => 'tags-id-1',
- ]],
- ['label' => ['for' => 'tags-id-1']],
- 'CakePHP',
- '/label',
- '/div',
- '/fieldset',
- '<fieldset',
- '<legend', 'Group 2', '/legend',
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => 2,
- 'id' => 'tags-id-2',
- ]],
- ['label' => ['for' => 'tags-id-2']],
- 'Development',
- '/label',
- '/div',
- '/fieldset',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test render with partial groupings.
- *
- * @return void
- */
- public function testRenderPartialGrouped()
- {
- $label = new LabelWidget($this->templates);
- $input = new MultiCheckboxWidget($this->templates, $label);
- $data = [
- 'name' => 'Tags[id]',
- 'options' => [
- 1 => 'PHP',
- 'Group 1' => [
- 2 => 'CakePHP',
- ],
- 3 => 'Development',
- ],
- ];
- $result = $input->render($data, $this->context);
- $expected = [
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => 1,
- 'id' => 'tags-id-1',
- ]],
- ['label' => ['for' => 'tags-id-1']],
- 'PHP',
- '/label',
- '/div',
- '<fieldset',
- '<legend', 'Group 1', '/legend',
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => 2,
- 'id' => 'tags-id-2',
- ]],
- ['label' => ['for' => 'tags-id-2']],
- 'CakePHP',
- '/label',
- '/div',
- '/fieldset',
- ['div' => ['class' => 'checkbox']],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Tags[id][]',
- 'value' => 3,
- 'id' => 'tags-id-3',
- ]],
- ['label' => ['for' => 'tags-id-3']],
- 'Development',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testRenderCustomAttributes method
- *
- * Test render with custom attributes
- *
- * @return void
- */
- public function testRenderCustomAttributes()
- {
- $label = new LabelWidget($this->templates);
- $input = new MultiCheckboxWidget($this->templates, $label);
- $result = $input->render([
- 'name' => 'category',
- 'options' => ['1', '2'],
- 'class' => 'my-class',
- 'data-ref' => 'custom-attr',
- ], $this->context);
- $expected = [
- ['div' => ['class' => 'checkbox']],
- [
- 'input' => [
- 'type' => 'checkbox',
- 'name' => 'category[]',
- 'value' => '0',
- 'id' => 'category-0',
- 'class' => 'my-class',
- 'data-ref' => 'custom-attr',
- ],
- ],
- ['label' => ['for' => 'category-0']],
- '1',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- [
- 'input' => [
- 'type' => 'checkbox',
- 'name' => 'category[]',
- 'value' => '1',
- 'id' => 'category-1',
- 'class' => 'my-class',
- 'data-ref' => 'custom-attr',
- ],
- ],
- ['label' => ['for' => 'category-1']],
- '2',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testRenderExplicitId method
- *
- * Test that the id passed is actually used
- * Issue: https://github.com/cakephp/cakephp/issues/13342
- *
- * @return void
- */
- public function testRenderExplicitId()
- {
- $label = new LabelWidget($this->templates);
- $input = new MultiCheckboxWidget($this->templates, $label);
- $data = [
- 'name' => 'field',
- 'options' => ['value1', 'value2'],
- 'id' => 'alternative-id',
- 'idPrefix' => 'willBeIgnored',
- ];
- $result = $input->render($data, $this->context);
- $expected = [
- [
- 'div' => ['class' => 'checkbox'],
- 'input' => ['type' => 'checkbox', 'name' => 'field[]', 'value' => '0', 'id' => 'alternative-id-0'],
- 'label' => ['for' => 'alternative-id-0'],
- ],
- 'value1',
- '/label',
- '/div',
- [
- 'div' => ['class' => 'checkbox'],
- 'input' => ['type' => 'checkbox', 'name' => 'field[]', 'value' => '1', 'id' => 'alternative-id-1'],
- 'label' => ['for' => 'alternative-id-1'],
- ],
- 'value2',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $data = [
- 'name' => 'field',
- 'options' => ['value1', 'value2'],
- 'idPrefix' => 'formprefix',
- ];
- $result = $input->render($data, $this->context);
- $expected = [
- [
- 'div' => ['class' => 'checkbox'],
- 'input' => ['type' => 'checkbox', 'name' => 'field[]', 'value' => '0', 'id' => 'formprefix-field-0'],
- 'label' => ['for' => 'formprefix-field-0'],
- ],
- 'value1',
- '/label',
- '/div',
- [
- 'div' => ['class' => 'checkbox'],
- 'input' => ['type' => 'checkbox', 'name' => 'field[]', 'value' => '1', 'id' => 'formprefix-field-1'],
- 'label' => ['for' => 'formprefix-field-1'],
- ],
- 'value2',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testRenderSelectedClass method
- *
- * Test that the custom selected class is passed to label
- * Issue: https://github.com/cakephp/cakephp/issues/11249
- *
- * @return void
- */
- public function testRenderSelectedClass()
- {
- $this->templates->add(['selectedClass' => 'active']);
- $label = new LabelWidget($this->templates);
- $input = new MultiCheckboxWidget($this->templates, $label);
- $data = [
- 'name' => 'field',
- 'options' => ['value1', 'value2'],
- 'id' => 'alternative-id',
- 'idPrefix' => 'willBeIgnored',
- ];
- $result = $input->render($data, $this->context);
- $data = [
- 'name' => 'field',
- 'options' => [1 => 'value1', 2 => 'value2'],
- 'val' => 1,
- 'label' => ['title' => 'my label'],
- ];
- $result = $input->render($data, $this->context);
- $expected = [
- [
- 'div' => ['class' => 'checkbox'],
- 'input' => ['type' => 'checkbox', 'name' => 'field[]', 'value' => '1', 'checked' => 'checked', 'id' => 'field-1'],
- 'label' => ['title' => 'my label', 'for' => 'field-1', 'class' => 'active'],
- ],
- 'value1',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- }
- }
|