2021-07-09 11:53:24 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace yiiunit\extensions\bootstrap5;
|
|
|
|
|
|
|
|
use yii\base\Model;
|
|
|
|
use yii\bootstrap5\Html;
|
|
|
|
use yii\bootstrap5\ToggleButtonGroup;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group bootstrap5
|
|
|
|
*/
|
|
|
|
class ToggleButtonGroupTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testCheckbox()
|
|
|
|
{
|
|
|
|
Html::$counter = 0;
|
|
|
|
$html = ToggleButtonGroup::widget([
|
|
|
|
'type' => ToggleButtonGroup::TYPE_CHECKBOX,
|
|
|
|
'model' => new ToggleButtonGroupTestModel(),
|
|
|
|
'attribute' => 'value',
|
|
|
|
'items' => [
|
|
|
|
'1' => 'item 1',
|
|
|
|
'2' => 'item 2',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$expectedHtml = <<<HTML
|
2021-08-04 08:51:53 +02:00
|
|
|
<input type="hidden" name="ToggleButtonGroupTestModel[value]" value=""><div id="togglebuttongrouptestmodel-value" class="btn-group" role="group"><input type="checkbox" id="i0" class="btn-check" name="ToggleButtonGroupTestModel[value][]" value="1" autocomplete="off">
|
|
|
|
<label class="btn btn-outline-secondary" for="i0">item 1</label>
|
|
|
|
<input type="checkbox" id="i1" class="btn-check" name="ToggleButtonGroupTestModel[value][]" value="2" autocomplete="off">
|
|
|
|
<label class="btn btn-outline-secondary" for="i1">item 2</label></div>
|
2021-07-09 11:53:24 +02:00
|
|
|
HTML;
|
|
|
|
$this->assertEqualsWithoutLE($expectedHtml, $html);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
2021-08-04 08:21:51 +02:00
|
|
|
public function testCheckboxChecked()
|
|
|
|
{
|
2021-07-09 11:53:24 +02:00
|
|
|
Html::$counter = 0;
|
|
|
|
$html = ToggleButtonGroup::widget([
|
|
|
|
'type' => ToggleButtonGroup::TYPE_CHECKBOX,
|
|
|
|
'model' => new ToggleButtonGroupTestModel(['value' => '2']),
|
|
|
|
'attribute' => 'value',
|
|
|
|
'items' => [
|
|
|
|
'1' => 'item 1',
|
|
|
|
'2' => 'item 2',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2021-08-04 08:51:53 +02:00
|
|
|
$this->assertContains('<input type="checkbox" id="i1" class="btn-check" name="ToggleButtonGroupTestModel[value][]" value="2" checked autocomplete="off">', $html);
|
2021-07-09 11:53:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRadio()
|
|
|
|
{
|
|
|
|
Html::$counter = 0;
|
|
|
|
$html = ToggleButtonGroup::widget([
|
|
|
|
'type' => ToggleButtonGroup::TYPE_RADIO,
|
|
|
|
'model' => new ToggleButtonGroupTestModel(),
|
|
|
|
'attribute' => 'value',
|
|
|
|
'items' => [
|
|
|
|
'1' => 'item 1',
|
|
|
|
'2' => 'item 2',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$expectedHtml = <<<HTML
|
2021-08-04 08:51:53 +02:00
|
|
|
<input type="hidden" name="ToggleButtonGroupTestModel[value]" value=""><div id="togglebuttongrouptestmodel-value" class="btn-group" role="group"><input type="radio" id="i0" class="btn-check" name="ToggleButtonGroupTestModel[value]" value="1" autocomplete="off">
|
|
|
|
<label class="btn btn-outline-secondary" for="i0">item 1</label>
|
|
|
|
<input type="radio" id="i1" class="btn-check" name="ToggleButtonGroupTestModel[value]" value="2" autocomplete="off">
|
|
|
|
<label class="btn btn-outline-secondary" for="i1">item 2</label></div>
|
2021-07-09 11:53:24 +02:00
|
|
|
HTML;
|
|
|
|
$this->assertEqualsWithoutLE($expectedHtml, $html);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
2021-08-04 08:21:51 +02:00
|
|
|
public function testRadioChecked()
|
|
|
|
{
|
2021-07-09 11:53:24 +02:00
|
|
|
Html::$counter = 0;
|
|
|
|
$html = ToggleButtonGroup::widget([
|
|
|
|
'type' => ToggleButtonGroup::TYPE_RADIO,
|
|
|
|
'model' => new ToggleButtonGroupTestModel(['value' => '2']),
|
|
|
|
'attribute' => 'value',
|
|
|
|
'items' => [
|
|
|
|
'1' => 'item 1',
|
|
|
|
'2' => 'item 2',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2021-08-04 08:51:53 +02:00
|
|
|
$this->assertContains('<input type="radio" id="i1" class="btn-check" name="ToggleButtonGroupTestModel[value]" value="2" checked autocomplete="off">', $html);
|
2021-07-09 11:53:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ToggleButtonGroupTestModel extends Model
|
|
|
|
{
|
|
|
|
public $value;
|
|
|
|
}
|