'', 'label' => '{{text}}', 'checkboxWrapper' => '
{{input}}{{label}}
', 'multicheckboxWrapper' => '{{content}}', 'multicheckboxTitle' => '{{text}}', ]; $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' => '', 'label' => '{{text}} {{inputVar}}', 'checkboxWrapper' => '
{{input}}{{label}}
', ]; $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' => '
{{label}}
', ]; $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 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 = [ ' ['class' => 'checkbox']], ['input' => [ 'type' => 'checkbox', 'name' => 'Tags[id][]', 'value' => 1, 'id' => 'tags-id-1', ]], ['label' => ['for' => 'tags-id-1']], 'CakePHP', '/label', '/div', '/fieldset', ' ['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', ' ['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); } }