mockApplication([ 'components' => [ 'request' => [ 'class' => 'yii\web\Request', 'url' => '/test', 'enableCsrfValidation' => false, ], 'response' => [ 'class' => 'yii\web\Response', ], ], ]); } public function assertEqualsWithoutLE($expected, $actual) { $expected = str_replace("\r\n", "\n", $expected); $actual = str_replace("\r\n", "\n", $actual); $this->assertEquals($expected, $actual); } public function testEncode() { $this->assertEquals("a<>&"'", Html::encode("a<>&\"'")); } public function testDecode() { $this->assertEquals("a<>&\"'", Html::decode("a<>&"'")); } public function testTag() { $this->assertEquals('
', Html::tag('br')); $this->assertEquals('', Html::tag('span')); $this->assertEquals('
content
', Html::tag('div', 'content')); $this->assertEquals('', Html::tag('input', '', ['type' => 'text', 'name' => 'test', 'value' => '<>'])); $this->assertEquals('', Html::tag('span', '', ['disabled' => true])); } public function testBeginTag() { $this->assertEquals('
', Html::beginTag('br')); $this->assertEquals('', Html::beginTag('span', ['id' => 'test', 'class' => 'title'])); } public function testEndTag() { $this->assertEquals('
', Html::endTag('br')); $this->assertEquals('
', Html::endTag('span')); } public function testStyle() { $content = 'a <>'; $this->assertEquals("", Html::style($content)); $this->assertEquals("", Html::style($content, ['type' => 'text/less'])); } public function testScript() { $content = 'a <>'; $this->assertEquals("", Html::script($content)); $this->assertEquals("", Html::script($content, ['type' => 'text/js'])); } public function testCssFile() { $this->assertEquals('', Html::cssFile('http://example.com')); $this->assertEquals('', Html::cssFile('')); } public function testJsFile() { $this->assertEquals('', Html::jsFile('http://example.com')); $this->assertEquals('', Html::jsFile('')); } public function testBeginForm() { $this->assertEquals('
', Html::beginForm()); $this->assertEquals('', Html::beginForm('/example', 'get')); $hiddens = [ '', '', ]; $this->assertEquals('' . "\n" . implode("\n", $hiddens), Html::beginForm('/example?id=1&title=%3C', 'get')); } public function testEndForm() { $this->assertEquals('
', Html::endForm()); } public function testA() { $this->assertEquals('something<>', Html::a('something<>')); $this->assertEquals('something', Html::a('something', '/example')); $this->assertEquals('something', Html::a('something', '')); } public function testMailto() { $this->assertEquals('test<>', Html::mailto('test<>')); $this->assertEquals('test<>', Html::mailto('test<>', 'test>')); } public function testImg() { $this->assertEquals('', Html::img('/example')); $this->assertEquals('', Html::img('')); $this->assertEquals('something', Html::img('/example', ['alt' => 'something', 'width' => 10])); } public function testLabel() { $this->assertEquals('', Html::label('something<>')); $this->assertEquals('', Html::label('something<>', 'a')); $this->assertEquals('', Html::label('something<>', 'a', ['class' => 'test'])); } public function testButton() { $this->assertEquals('', Html::button()); $this->assertEquals('', Html::button('content<>', ['name' => 'test', 'value' => 'value'])); $this->assertEquals('', Html::button('content<>', ['type' => 'submit', 'name' => 'test', 'value' => 'value', 'class' => "t"])); } public function testSubmitButton() { $this->assertEquals('', Html::submitButton()); $this->assertEquals('', Html::submitButton('content<>', ['name' => 'test', 'value' => 'value', 'class' => 't'])); } public function testResetButton() { $this->assertEquals('', Html::resetButton()); $this->assertEquals('', Html::resetButton('content<>', ['name' => 'test', 'value' => 'value', 'class' => 't'])); } public function testInput() { $this->assertEquals('', Html::input('text')); $this->assertEquals('', Html::input('text', 'test', 'value', ['class' => 't'])); } public function testButtonInput() { $this->assertEquals('', Html::buttonInput()); $this->assertEquals('', Html::buttonInput('text', ['name' => 'test', 'class' => 'a'])); } public function testSubmitInput() { $this->assertEquals('', Html::submitInput()); $this->assertEquals('', Html::submitInput('text', ['name' => 'test', 'class' => 'a'])); } public function testResetInput() { $this->assertEquals('', Html::resetInput()); $this->assertEquals('', Html::resetInput('text', ['name' => 'test', 'class' => 'a'])); } public function testTextInput() { $this->assertEquals('', Html::textInput('test')); $this->assertEquals('', Html::textInput('test', 'value', ['class' => 't'])); } public function testHiddenInput() { $this->assertEquals('', Html::hiddenInput('test')); $this->assertEquals('', Html::hiddenInput('test', 'value', ['class' => 't'])); } public function testPasswordInput() { $this->assertEquals('', Html::passwordInput('test')); $this->assertEquals('', Html::passwordInput('test', 'value', ['class' => 't'])); } public function testFileInput() { $this->assertEquals('', Html::fileInput('test')); $this->assertEquals('', Html::fileInput('test', 'value', ['class' => 't'])); } public function testTextarea() { $this->assertEquals('', Html::textarea('test')); $this->assertEquals('', Html::textarea('test', 'value<>', ['class' => 't'])); } public function testRadio() { $this->assertEquals('', Html::radio('test')); $this->assertEquals('', Html::radio('test', true, ['class' => 'a', 'value' => null])); $this->assertEquals('', Html::radio('test', true, ['class' => 'a' , 'uncheck' => '0', 'value' => 2])); $this->assertEquals('
', Html::radio('test', true, [ 'class' => 'a', 'value' => null, 'label' => 'ccc', 'labelOptions' => ['class' =>'bbb'], ])); $this->assertEquals('
', Html::radio('test', true, [ 'class' => 'a', 'uncheck' => '0', 'label' => 'ccc', 'value' => 2, ])); } public function testCheckbox() { $this->assertEquals('', Html::checkbox('test')); $this->assertEquals('', Html::checkbox('test', true, ['class' => 'a', 'value' => null])); $this->assertEquals('', Html::checkbox('test', true, ['class' => 'a', 'uncheck' => '0', 'value' => 2])); $this->assertEquals('
', Html::checkbox('test', true, [ 'class' => 'a', 'value' => null, 'label' => 'ccc', 'labelOptions' => ['class' =>'bbb'], ])); $this->assertEquals('
', Html::checkbox('test', true, [ 'class' => 'a', 'uncheck' => '0', 'label' => 'ccc', 'value' => 2, ])); } public function testDropDownList() { $expected = << EOD; $this->assertEqualsWithoutLE($expected, Html::dropDownList('test')); $expected = << EOD; $this->assertEqualsWithoutLE($expected, Html::dropDownList('test', null, $this->getDataItems())); $expected = << EOD; $this->assertEqualsWithoutLE($expected, Html::dropDownList('test', 'value2', $this->getDataItems())); } public function testListBox() { $expected = << EOD; $this->assertEqualsWithoutLE($expected, Html::listBox('test')); $expected = << EOD; $this->assertEqualsWithoutLE($expected, Html::listBox('test', null, $this->getDataItems(), ['size' => 5])); $expected = << EOD; $this->assertEqualsWithoutLE($expected, Html::listBox('test', null, $this->getDataItems2())); $expected = << EOD; $this->assertEqualsWithoutLE($expected, Html::listBox('test', 'value2', $this->getDataItems())); $expected = << EOD; $this->assertEqualsWithoutLE($expected, Html::listBox('test', ['value1', 'value2'], $this->getDataItems())); $expected = << EOD; $this->assertEqualsWithoutLE($expected, Html::listBox('test', null, [], ['multiple' => true])); $expected = << EOD; $this->assertEqualsWithoutLE($expected, Html::listBox('test', '', [], ['unselect' => '0'])); } public function testCheckboxList() { $this->assertEquals('
', Html::checkboxList('test')); $expected = <<
EOD; $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $this->getDataItems())); $expected = <<
EOD; $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $this->getDataItems2())); $expected = <<

EOD; $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $this->getDataItems(), [ 'separator' => "
\n", 'unselect' => '0', ])); $expected = <<0 1 EOD; $this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $this->getDataItems(), [ 'item' => function ($index, $label, $name, $checked, $value) { return $index . Html::label($label . ' ' . Html::checkbox($name, $checked, ['value' => $value])); } ])); } public function testRadioList() { $this->assertEquals('
', Html::radioList('test')); $expected = <<
EOD; $this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $this->getDataItems())); $expected = <<
EOD; $this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $this->getDataItems2())); $expected = <<

EOD; $this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $this->getDataItems(), [ 'separator' => "
\n", 'unselect' => '0', ])); $expected = <<0 1 EOD; $this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $this->getDataItems(), [ 'item' => function ($index, $label, $name, $checked, $value) { return $index . Html::label($label . ' ' . Html::radio($name, $checked, ['value' => $value])); } ])); } public function testUl() { $data = [ 1, 'abc', '<>', ]; $expected = <<
  • 1
  • abc
  • <>
  • EOD; $this->assertEqualsWithoutLE($expected, Html::ul($data)); $expected = <<
  • 1
  • abc
  • <>
  • EOD; $this->assertEqualsWithoutLE($expected, Html::ul($data, [ 'class' => 'test', 'item' => function ($item, $index) { return "
  • $item
  • "; } ])); } public function testOl() { $data = [ 1, 'abc', '<>', ]; $expected = <<
  • 1
  • abc
  • <>
  • EOD; $this->assertEqualsWithoutLE($expected, Html::ol($data, [ 'itemOptions' => ['class' => 'ti'], ])); $expected = <<
  • 1
  • abc
  • <>
  • EOD; $this->assertEqualsWithoutLE($expected, Html::ol($data, [ 'class' => 'test', 'item' => function ($item, $index) { return "
  • $item
  • "; } ])); } public function testRenderOptions() { $data = [ 'value1' => 'label1', 'group1' => [ 'value11' => 'label11', 'group11' => [ 'value111' => 'label111', ], 'group12' => [], ], 'value2' => 'label2', 'group2' => [], ]; $expected = <<please select<> EOD; $attributes = [ 'prompt' => 'please select<>', 'options' => [ 'value111' => ['class' => 'option'], ], 'groups' => [ 'group12' => ['class' => 'group'], ], ]; $this->assertEqualsWithoutLE($expected, Html::renderSelectOptions(['value111', 'value1'], $data, $attributes)); } public function testRenderAttributes() { $this->assertEquals('', Html::renderTagAttributes([])); $this->assertEquals(' name="test" value="1<>"', Html::renderTagAttributes(['name' => 'test', 'empty' => null, 'value' => '1<>'])); $this->assertEquals(' checked disabled', Html::renderTagAttributes(['checked' => true, 'disabled' => true, 'hidden' => false])); } public function testAddCssClass() { $options = []; Html::addCssClass($options, 'test'); $this->assertEquals(['class' => 'test'], $options); Html::addCssClass($options, 'test'); $this->assertEquals(['class' => 'test'], $options); Html::addCssClass($options, 'test2'); $this->assertEquals(['class' => 'test test2'], $options); Html::addCssClass($options, 'test'); $this->assertEquals(['class' => 'test test2'], $options); Html::addCssClass($options, 'test2'); $this->assertEquals(['class' => 'test test2'], $options); Html::addCssClass($options, 'test3'); $this->assertEquals(['class' => 'test test2 test3'], $options); Html::addCssClass($options, 'test2'); $this->assertEquals(['class' => 'test test2 test3'], $options); } public function testRemoveCssClass() { $options = ['class' => 'test test2 test3']; Html::removeCssClass($options, 'test2'); $this->assertEquals(['class' => 'test test3'], $options); Html::removeCssClass($options, 'test2'); $this->assertEquals(['class' => 'test test3'], $options); Html::removeCssClass($options, 'test'); $this->assertEquals(['class' => 'test3'], $options); Html::removeCssClass($options, 'test3'); $this->assertEquals([], $options); } protected function getDataItems() { return [ 'value1' => 'text1', 'value2' => 'text2', ]; } protected function getDataItems2() { return [ 'value1<>' => 'text1<>', 'value 2' => 'text 2', ]; } }