'',
];
$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' => '',
]);
$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);
}
}