| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- <?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\TestSuite\TestCase;
- use Cake\View\StringTemplate;
- use Cake\View\Widget\LabelWidget;
- use Cake\View\Widget\MultiCheckboxWidget;
- /**
- * 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>',
- 'checkboxWrapper' => '<div class="checkbox">{{input}}{{label}}</div>',
- ];
- $this->templates = new StringTemplate($templates);
- $this->context = $this->getMock('Cake\View\Form\ContextInterface');
- }
- /**
- * 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);
- }
- }
|