ActiveFormTest.php 2.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/**
 * @author Carsten Brandt <mail@cebe.cc>
 */

namespace yiiunit\framework\widgets;

use yii\base\DynamicModel;
use yii\widgets\ActiveForm;

/**
 * @group widgets
 */
class ActiveFormTest extends \yiiunit\TestCase
{
16 17 18 19 20
    protected function setUp()
    {
        $this->mockApplication();
    }

Alexander Makarov committed
21 22 23 24 25 26 27 28
    public function assertEqualsWithoutLE($expected, $actual)
    {
        $expected = str_replace("\r\n", "\n", $expected);
        $actual = str_replace("\r\n", "\n", $actual);

        $this->assertEquals($expected, $actual);
    }

29 30 31 32 33 34
    public function testBooleanAttributes()
    {
        $o = ['template' => '{input}'];

        $model = new DynamicModel(['name']);
        ob_start();
Qiang Xue committed
35
        $form = new ActiveForm(['action' => '/something']);
36 37
        ob_end_clean();

Alexander Makarov committed
38
        $this->assertEqualsWithoutLE(<<<EOF
39 40 41 42 43 44
<div class="form-group field-dynamicmodel-name">
<input type="email" id="dynamicmodel-name" class="form-control" name="DynamicModel[name]" required>
</div>
EOF
, (string) $form->field($model, 'name', $o)->input('email', ['required' => true]));

Alexander Makarov committed
45
        $this->assertEqualsWithoutLE(<<<EOF
46 47 48 49
<div class="form-group field-dynamicmodel-name">
<input type="email" id="dynamicmodel-name" class="form-control" name="DynamicModel[name]">
</div>
EOF
50
            , (string) $form->field($model, 'name', $o)->input('email', ['required' => false]));
51 52


Alexander Makarov committed
53
        $this->assertEqualsWithoutLE(<<<EOF
54
<div class="form-group field-dynamicmodel-name">
55
<input type="email" id="dynamicmodel-name" class="form-control" name="DynamicModel[name]" required="test">
56 57
</div>
EOF
58
            , (string) $form->field($model, 'name', $o)->input('email', ['required' => 'test']));
59

60
    }
61 62 63

    public function testIssue5356()
    {
Alexander Makarov committed
64 65
        $o = ['template' => '{input}'];

66 67 68 69 70 71 72
        $model = new DynamicModel(['categories']);
        $model->categories = 1;
        ob_start();
        $form = new ActiveForm(['action' => '/something']);
        ob_end_clean();

        // https://github.com/yiisoft/yii2/issues/5356
Alexander Makarov committed
73 74 75 76 77 78 79
        $this->assertEqualsWithoutLE(<<<EOF
<div class="form-group field-dynamicmodel-categories">
<input type="hidden" name="DynamicModel[categories]" value=""><select id="dynamicmodel-categories" class="form-control" name="DynamicModel[categories][]" multiple size="4">
<option value="0">apple</option>
<option value="1" selected>banana</option>
<option value="2">avocado</option>
</select>
80 81
</div>
EOF
Alexander Makarov committed
82
             , (string) $form->field($model, 'categories', $o)->listBox(['apple', 'banana', 'avocado'], ['multiple' => true]));
83
    }
84
}