| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- <?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\CheckboxWidget;
- /**
- * Checkbox test case
- */
- class CheckboxWidgetTest extends TestCase
- {
- /**
- * setup method.
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $templates = [
- 'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
- ];
- $this->templates = new StringTemplate($templates);
- $this->context = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
- }
- /**
- * Test rendering simple checkboxes.
- *
- * @return void
- */
- public function testRenderSimple()
- {
- $checkbox = new CheckboxWidget($this->templates);
- $data = [
- 'name' => 'Comment[spam]',
- ];
- $result = $checkbox->render($data, $this->context);
- $expected = [
- 'input' => [
- 'type' => 'checkbox',
- 'name' => 'Comment[spam]',
- 'value' => 1,
- ],
- ];
- $this->assertHtml($expected, $result);
- $data = [
- 'name' => 'Comment[spam]',
- 'value' => 99,
- ];
- $result = $checkbox->render($data, $this->context);
- $expected = [
- 'input' => [
- 'type' => 'checkbox',
- 'name' => 'Comment[spam]',
- 'value' => 99,
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test rendering disabled checkboxes.
- *
- * @return void
- */
- public function testRenderDisabled()
- {
- $checkbox = new CheckboxWidget($this->templates);
- $data = [
- 'name' => 'Comment[spam]',
- 'disabled' => true,
- ];
- $result = $checkbox->render($data, $this->context);
- $expected = [
- 'input' => [
- 'type' => 'checkbox',
- 'name' => 'Comment[spam]',
- 'value' => 1,
- 'disabled' => 'disabled',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test rendering checked checkboxes.
- *
- * @return void
- */
- public function testRenderChecked()
- {
- $checkbox = new CheckboxWidget($this->templates);
- $data = [
- 'name' => 'Comment[spam]',
- 'value' => 1,
- 'checked' => 1,
- ];
- $result = $checkbox->render($data, $this->context);
- $expected = [
- 'input' => [
- 'type' => 'checkbox',
- 'name' => 'Comment[spam]',
- 'value' => 1,
- 'checked' => 'checked',
- ],
- ];
- $this->assertHtml($expected, $result);
- $data = [
- 'name' => 'Comment[spam]',
- 'value' => 1,
- 'val' => 1,
- ];
- $result = $checkbox->render($data, $this->context);
- $this->assertHtml($expected, $result);
- $data['val'] = '1';
- $result = $checkbox->render($data, $this->context);
- $this->assertHtml($expected, $result);
- $data = [
- 'name' => 'Comment[spam]',
- 'value' => 1,
- 'val' => '1x',
- ];
- $result = $checkbox->render($data, $this->context);
- $expected = [
- 'input' => [
- 'type' => 'checkbox',
- 'name' => 'Comment[spam]',
- 'value' => 1,
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Data provider for checkbox values
- *
- * @return array
- */
- public static function checkedProvider()
- {
- return [
- ['checked'],
- ['1'],
- [1],
- [true],
- ];
- }
- /**
- * Test rendering checked checkboxes with value.
- *
- * @dataProvider checkedProvider
- * @return void
- */
- public function testRenderCheckedValue($checked)
- {
- $checkbox = new CheckboxWidget($this->templates);
- $data = [
- 'name' => 'Comment[spam]',
- 'value' => 1,
- 'checked' => $checked,
- ];
- $result = $checkbox->render($data, $this->context);
- $expected = [
- 'input' => [
- 'type' => 'checkbox',
- 'name' => 'Comment[spam]',
- 'value' => 1,
- 'checked' => 'checked',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Data provider for checkbox values
- *
- * @return array
- */
- public static function uncheckedProvider()
- {
- return [
- [''],
- ['0'],
- [0],
- [false],
- [null],
- ];
- }
- /**
- * Test rendering unchecked checkboxes
- *
- * @dataProvider uncheckedProvider
- * @return void
- */
- public function testRenderUnCheckedValue($checked)
- {
- $checkbox = new CheckboxWidget($this->templates);
- $data = [
- 'name' => 'Comment[spam]',
- 'value' => 1,
- 'val' => 1,
- 'checked' => $checked,
- ];
- $result = $checkbox->render($data, $this->context);
- $expected = [
- 'input' => [
- 'type' => 'checkbox',
- 'name' => 'Comment[spam]',
- 'value' => 1,
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Ensure templateVars option is hooked up.
- *
- * @return void
- */
- public function testRenderTemplateVars()
- {
- $this->templates->add([
- 'checkbox' => '<input type="checkbox" custom="{{custom}}" name="{{name}}" value="{{value}}"{{attrs}}>',
- ]);
- $checkbox = new CheckboxWidget($this->templates);
- $data = [
- 'templateVars' => ['custom' => 'value'],
- 'name' => 'Comment[spam]',
- 'value' => 1,
- ];
- $result = $checkbox->render($data, $this->context);
- $expected = [
- 'input' => [
- 'type' => 'checkbox',
- 'custom' => 'value',
- 'name' => 'Comment[spam]',
- 'value' => 1,
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testRenderCustomAttributes method
- *
- * Test render with custom attributes.
- *
- * @return void
- */
- public function testRenderCustomAttributes()
- {
- $checkbox = new CheckboxWidget($this->templates);
- $result = $checkbox->render([
- 'name' => 'Model[field]',
- 'class' => 'my-class',
- 'data-ref' => 'custom-attr',
- 'value' => 1,
- ], $this->context);
- $expected = [
- 'input' => [
- 'type' => 'checkbox',
- 'name' => 'Model[field]',
- 'value' => '1',
- 'class' => 'my-class',
- 'data-ref' => 'custom-attr',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- }
|