Browse Source

line ending fix.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
74d4e04d95
  1. 896
      tests/unit/framework/util/HtmlTest.php

896
tests/unit/framework/util/HtmlTest.php

@ -1,448 +1,448 @@
<?php <?php
namespace yiiunit\framework\util; namespace yiiunit\framework\util;
use Yii; use Yii;
use yii\helpers\Html; use yii\helpers\Html;
use yii\web\Application; use yii\web\Application;
class HtmlTest extends \yii\test\TestCase class HtmlTest extends \yii\test\TestCase
{ {
public function setUp() public function setUp()
{ {
new Application('test', '@yiiunit/runtime', array( new Application('test', '@yiiunit/runtime', array(
'components' => array( 'components' => array(
'request' => array( 'request' => array(
'class' => 'yii\web\Request', 'class' => 'yii\web\Request',
'url' => '/test', 'url' => '/test',
), ),
), ),
)); ));
} }
public function tearDown() public function tearDown()
{ {
Yii::$app = null; Yii::$app = null;
} }
public function testEncode() public function testEncode()
{ {
$this->assertEquals("a&lt;&gt;&amp;&quot;&#039;", Html::encode("a<>&\"'")); $this->assertEquals("a&lt;&gt;&amp;&quot;&#039;", Html::encode("a<>&\"'"));
} }
public function testDecode() public function testDecode()
{ {
$this->assertEquals("a<>&\"'", Html::decode("a&lt;&gt;&amp;&quot;&#039;")); $this->assertEquals("a<>&\"'", Html::decode("a&lt;&gt;&amp;&quot;&#039;"));
} }
public function testTag() public function testTag()
{ {
$this->assertEquals('<br />', Html::tag('br')); $this->assertEquals('<br />', Html::tag('br'));
$this->assertEquals('<span></span>', Html::tag('span')); $this->assertEquals('<span></span>', Html::tag('span'));
$this->assertEquals('<div>content</div>', Html::tag('div', 'content')); $this->assertEquals('<div>content</div>', Html::tag('div', 'content'));
$this->assertEquals('<input type="text" name="test" value="&lt;&gt;" />', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>'))); $this->assertEquals('<input type="text" name="test" value="&lt;&gt;" />', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>')));
Html::$closeVoidElements = false; Html::$closeVoidElements = false;
$this->assertEquals('<br>', Html::tag('br')); $this->assertEquals('<br>', Html::tag('br'));
$this->assertEquals('<span></span>', Html::tag('span')); $this->assertEquals('<span></span>', Html::tag('span'));
$this->assertEquals('<div>content</div>', Html::tag('div', 'content')); $this->assertEquals('<div>content</div>', Html::tag('div', 'content'));
$this->assertEquals('<input type="text" name="test" value="&lt;&gt;">', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>'))); $this->assertEquals('<input type="text" name="test" value="&lt;&gt;">', Html::tag('input', '', array('type' => 'text', 'name' => 'test', 'value' => '<>')));
Html::$closeVoidElements = true; Html::$closeVoidElements = true;
$this->assertEquals('<span disabled="disabled"></span>', Html::tag('span', '', array('disabled' => true))); $this->assertEquals('<span disabled="disabled"></span>', Html::tag('span', '', array('disabled' => true)));
Html::$showBooleanAttributeValues = false; Html::$showBooleanAttributeValues = false;
$this->assertEquals('<span disabled></span>', Html::tag('span', '', array('disabled' => true))); $this->assertEquals('<span disabled></span>', Html::tag('span', '', array('disabled' => true)));
Html::$showBooleanAttributeValues = true; Html::$showBooleanAttributeValues = true;
} }
public function testBeginTag() public function testBeginTag()
{ {
$this->assertEquals('<br>', Html::beginTag('br')); $this->assertEquals('<br>', Html::beginTag('br'));
$this->assertEquals('<span id="test" class="title">', Html::beginTag('span', array('id' => 'test', 'class' => 'title'))); $this->assertEquals('<span id="test" class="title">', Html::beginTag('span', array('id' => 'test', 'class' => 'title')));
} }
public function testEndTag() public function testEndTag()
{ {
$this->assertEquals('</br>', Html::endTag('br')); $this->assertEquals('</br>', Html::endTag('br'));
$this->assertEquals('</span>', Html::endTag('span')); $this->assertEquals('</span>', Html::endTag('span'));
} }
public function testCdata() public function testCdata()
{ {
$data = 'test<>'; $data = 'test<>';
$this->assertEquals('<![CDATA[' . $data . ']]>', Html::cdata($data)); $this->assertEquals('<![CDATA[' . $data . ']]>', Html::cdata($data));
} }
public function testStyle() public function testStyle()
{ {
$content = 'a <>'; $content = 'a <>';
$this->assertEquals("<style type=\"text/css\">/*<![CDATA[*/\n{$content}\n/*]]>*/</style>", Html::style($content)); $this->assertEquals("<style type=\"text/css\">/*<![CDATA[*/\n{$content}\n/*]]>*/</style>", Html::style($content));
$this->assertEquals("<style type=\"text/less\">/*<![CDATA[*/\n{$content}\n/*]]>*/</style>", Html::style($content, array('type' => 'text/less'))); $this->assertEquals("<style type=\"text/less\">/*<![CDATA[*/\n{$content}\n/*]]>*/</style>", Html::style($content, array('type' => 'text/less')));
} }
public function testScript() public function testScript()
{ {
$content = 'a <>'; $content = 'a <>';
$this->assertEquals("<script type=\"text/javascript\">/*<![CDATA[*/\n{$content}\n/*]]>*/</script>", Html::script($content)); $this->assertEquals("<script type=\"text/javascript\">/*<![CDATA[*/\n{$content}\n/*]]>*/</script>", Html::script($content));
$this->assertEquals("<script type=\"text/js\">/*<![CDATA[*/\n{$content}\n/*]]>*/</script>", Html::script($content, array('type' => 'text/js'))); $this->assertEquals("<script type=\"text/js\">/*<![CDATA[*/\n{$content}\n/*]]>*/</script>", Html::script($content, array('type' => 'text/js')));
} }
public function testCssFile() public function testCssFile()
{ {
$this->assertEquals('<link type="text/css" href="http://example.com" rel="stylesheet" />', Html::cssFile('http://example.com')); $this->assertEquals('<link type="text/css" href="http://example.com" rel="stylesheet" />', Html::cssFile('http://example.com'));
$this->assertEquals('<link type="text/css" href="/test" rel="stylesheet" />', Html::cssFile('')); $this->assertEquals('<link type="text/css" href="/test" rel="stylesheet" />', Html::cssFile(''));
} }
public function testJsFile() public function testJsFile()
{ {
$this->assertEquals('<script type="text/javascript" src="http://example.com"></script>', Html::jsFile('http://example.com')); $this->assertEquals('<script type="text/javascript" src="http://example.com"></script>', Html::jsFile('http://example.com'));
$this->assertEquals('<script type="text/javascript" src="/test"></script>', Html::jsFile('')); $this->assertEquals('<script type="text/javascript" src="/test"></script>', Html::jsFile(''));
} }
public function testBeginForm() public function testBeginForm()
{ {
$this->assertEquals('<form action="/test" method="post">', Html::beginForm()); $this->assertEquals('<form action="/test" method="post">', Html::beginForm());
$this->assertEquals('<form action="/example" method="get">', Html::beginForm('/example', 'get')); $this->assertEquals('<form action="/example" method="get">', Html::beginForm('/example', 'get'));
$hiddens = array( $hiddens = array(
'<input type="hidden" name="id" value="1" />', '<input type="hidden" name="id" value="1" />',
'<input type="hidden" name="title" value="&lt;" />', '<input type="hidden" name="title" value="&lt;" />',
); );
$this->assertEquals('<form action="/example" method="get">' . "\n" . implode("\n", $hiddens), Html::beginForm('/example?id=1&title=%3C', 'get')); $this->assertEquals('<form action="/example" method="get">' . "\n" . implode("\n", $hiddens), Html::beginForm('/example?id=1&title=%3C', 'get'));
} }
public function testEndForm() public function testEndForm()
{ {
$this->assertEquals('</form>', Html::endForm()); $this->assertEquals('</form>', Html::endForm());
} }
public function testA() public function testA()
{ {
$this->assertEquals('<a>something<></a>', Html::a('something<>')); $this->assertEquals('<a>something<></a>', Html::a('something<>'));
$this->assertEquals('<a href="/example">something</a>', Html::a('something', '/example')); $this->assertEquals('<a href="/example">something</a>', Html::a('something', '/example'));
$this->assertEquals('<a href="/test">something</a>', Html::a('something', '')); $this->assertEquals('<a href="/test">something</a>', Html::a('something', ''));
} }
public function testMailto() public function testMailto()
{ {
$this->assertEquals('<a href="mailto:test&lt;&gt;">test<></a>', Html::mailto('test<>')); $this->assertEquals('<a href="mailto:test&lt;&gt;">test<></a>', Html::mailto('test<>'));
$this->assertEquals('<a href="mailto:test&gt;">test<></a>', Html::mailto('test<>', 'test>')); $this->assertEquals('<a href="mailto:test&gt;">test<></a>', Html::mailto('test<>', 'test>'));
} }
public function testImg() public function testImg()
{ {
$this->assertEquals('<img src="/example" alt="" />', Html::img('/example')); $this->assertEquals('<img src="/example" alt="" />', Html::img('/example'));
$this->assertEquals('<img src="/test" alt="" />', Html::img('')); $this->assertEquals('<img src="/test" alt="" />', Html::img(''));
$this->assertEquals('<img src="/example" width="10" alt="something" />', Html::img('/example', array('alt' => 'something', 'width' => 10))); $this->assertEquals('<img src="/example" width="10" alt="something" />', Html::img('/example', array('alt' => 'something', 'width' => 10)));
} }
public function testLabel() public function testLabel()
{ {
$this->assertEquals('<label>something<></label>', Html::label('something<>')); $this->assertEquals('<label>something<></label>', Html::label('something<>'));
$this->assertEquals('<label for="a">something<></label>', Html::label('something<>', 'a')); $this->assertEquals('<label for="a">something<></label>', Html::label('something<>', 'a'));
$this->assertEquals('<label class="test" for="a">something<></label>', Html::label('something<>', 'a', array('class' => 'test'))); $this->assertEquals('<label class="test" for="a">something<></label>', Html::label('something<>', 'a', array('class' => 'test')));
} }
public function testButton() public function testButton()
{ {
$this->assertEquals('<button type="button">Button</button>', Html::button()); $this->assertEquals('<button type="button">Button</button>', Html::button());
$this->assertEquals('<button type="button" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>')); $this->assertEquals('<button type="button" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>'));
$this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>', array('type' => 'submit', 'class' => "t"))); $this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>', array('type' => 'submit', 'class' => "t")));
} }
public function testSubmitButton() public function testSubmitButton()
{ {
$this->assertEquals('<button type="submit">Submit</button>', Html::submitButton()); $this->assertEquals('<button type="submit">Submit</button>', Html::submitButton());
$this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::submitButton('test', 'value', 'content<>', array('class' => 't'))); $this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::submitButton('test', 'value', 'content<>', array('class' => 't')));
} }
public function testResetButton() public function testResetButton()
{ {
$this->assertEquals('<button type="reset">Reset</button>', Html::resetButton()); $this->assertEquals('<button type="reset">Reset</button>', Html::resetButton());
$this->assertEquals('<button type="reset" class="t" name="test" value="value">content<></button>', Html::resetButton('test', 'value', 'content<>', array('class' => 't'))); $this->assertEquals('<button type="reset" class="t" name="test" value="value">content<></button>', Html::resetButton('test', 'value', 'content<>', array('class' => 't')));
} }
public function testInput() public function testInput()
{ {
$this->assertEquals('<input type="text" />', Html::input('text')); $this->assertEquals('<input type="text" />', Html::input('text'));
$this->assertEquals('<input type="text" class="t" name="test" value="value" />', Html::input('text', 'test', 'value', array('class' => 't'))); $this->assertEquals('<input type="text" class="t" name="test" value="value" />', Html::input('text', 'test', 'value', array('class' => 't')));
} }
public function testButtonInput() public function testButtonInput()
{ {
$this->assertEquals('<input type="button" name="test" value="Button" />', Html::buttonInput('test')); $this->assertEquals('<input type="button" name="test" value="Button" />', Html::buttonInput('test'));
$this->assertEquals('<input type="button" class="a" name="test" value="text" />', Html::buttonInput('test', 'text', array('class' => 'a'))); $this->assertEquals('<input type="button" class="a" name="test" value="text" />', Html::buttonInput('test', 'text', array('class' => 'a')));
} }
public function testSubmitInput() public function testSubmitInput()
{ {
$this->assertEquals('<input type="submit" value="Submit" />', Html::submitInput()); $this->assertEquals('<input type="submit" value="Submit" />', Html::submitInput());
$this->assertEquals('<input type="submit" class="a" name="test" value="text" />', Html::submitInput('test', 'text', array('class' => 'a'))); $this->assertEquals('<input type="submit" class="a" name="test" value="text" />', Html::submitInput('test', 'text', array('class' => 'a')));
} }
public function testResetInput() public function testResetInput()
{ {
$this->assertEquals('<input type="reset" value="Reset" />', Html::resetInput()); $this->assertEquals('<input type="reset" value="Reset" />', Html::resetInput());
$this->assertEquals('<input type="reset" class="a" name="test" value="text" />', Html::resetInput('test', 'text', array('class' => 'a'))); $this->assertEquals('<input type="reset" class="a" name="test" value="text" />', Html::resetInput('test', 'text', array('class' => 'a')));
} }
public function testTextInput() public function testTextInput()
{ {
$this->assertEquals('<input type="text" name="test" />', Html::textInput('test')); $this->assertEquals('<input type="text" name="test" />', Html::textInput('test'));
$this->assertEquals('<input type="text" class="t" name="test" value="value" />', Html::textInput('test', 'value', array('class' => 't'))); $this->assertEquals('<input type="text" class="t" name="test" value="value" />', Html::textInput('test', 'value', array('class' => 't')));
} }
public function testHiddenInput() public function testHiddenInput()
{ {
$this->assertEquals('<input type="hidden" name="test" />', Html::hiddenInput('test')); $this->assertEquals('<input type="hidden" name="test" />', Html::hiddenInput('test'));
$this->assertEquals('<input type="hidden" class="t" name="test" value="value" />', Html::hiddenInput('test', 'value', array('class' => 't'))); $this->assertEquals('<input type="hidden" class="t" name="test" value="value" />', Html::hiddenInput('test', 'value', array('class' => 't')));
} }
public function testPasswordInput() public function testPasswordInput()
{ {
$this->assertEquals('<input type="password" name="test" />', Html::passwordInput('test')); $this->assertEquals('<input type="password" name="test" />', Html::passwordInput('test'));
$this->assertEquals('<input type="password" class="t" name="test" value="value" />', Html::passwordInput('test', 'value', array('class' => 't'))); $this->assertEquals('<input type="password" class="t" name="test" value="value" />', Html::passwordInput('test', 'value', array('class' => 't')));
} }
public function testFileInput() public function testFileInput()
{ {
$this->assertEquals('<input type="file" name="test" />', Html::fileInput('test')); $this->assertEquals('<input type="file" name="test" />', Html::fileInput('test'));
$this->assertEquals('<input type="file" class="t" name="test" value="value" />', Html::fileInput('test', 'value', array('class' => 't'))); $this->assertEquals('<input type="file" class="t" name="test" value="value" />', Html::fileInput('test', 'value', array('class' => 't')));
} }
public function testTextarea() public function testTextarea()
{ {
$this->assertEquals('<textarea name="test"></textarea>', Html::textarea('test')); $this->assertEquals('<textarea name="test"></textarea>', Html::textarea('test'));
$this->assertEquals('<textarea class="t" name="test">value&lt;&gt;</textarea>', Html::textarea('test', 'value<>', array('class' => 't'))); $this->assertEquals('<textarea class="t" name="test">value&lt;&gt;</textarea>', Html::textarea('test', 'value<>', array('class' => 't')));
} }
public function testRadio() public function testRadio()
{ {
$this->assertEquals('<input type="radio" name="test" value="1" />', Html::radio('test')); $this->assertEquals('<input type="radio" name="test" value="1" />', Html::radio('test'));
$this->assertEquals('<input type="radio" class="a" name="test" checked="checked" />', Html::radio('test', true, null, array('class' => 'a'))); $this->assertEquals('<input type="radio" class="a" name="test" checked="checked" />', Html::radio('test', true, null, array('class' => 'a')));
$this->assertEquals('<input type="hidden" name="test" value="0" /><input type="radio" class="a" name="test" value="2" checked="checked" />', Html::radio('test', true, 2, array('class' => 'a' , 'uncheck' => '0'))); $this->assertEquals('<input type="hidden" name="test" value="0" /><input type="radio" class="a" name="test" value="2" checked="checked" />', Html::radio('test', true, 2, array('class' => 'a' , 'uncheck' => '0')));
} }
public function testCheckbox() public function testCheckbox()
{ {
$this->assertEquals('<input type="checkbox" name="test" value="1" />', Html::checkbox('test')); $this->assertEquals('<input type="checkbox" name="test" value="1" />', Html::checkbox('test'));
$this->assertEquals('<input type="checkbox" class="a" name="test" checked="checked" />', Html::checkbox('test', true, null, array('class' => 'a'))); $this->assertEquals('<input type="checkbox" class="a" name="test" checked="checked" />', Html::checkbox('test', true, null, array('class' => 'a')));
$this->assertEquals('<input type="hidden" name="test" value="0" /><input type="checkbox" class="a" name="test" value="2" checked="checked" />', Html::checkbox('test', true, 2, array('class' => 'a', 'uncheck' => '0'))); $this->assertEquals('<input type="hidden" name="test" value="0" /><input type="checkbox" class="a" name="test" value="2" checked="checked" />', Html::checkbox('test', true, 2, array('class' => 'a', 'uncheck' => '0')));
} }
public function testDropDownList() public function testDropDownList()
{ {
$expected = <<<EOD $expected = <<<EOD
<select name="test"> <select name="test">
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::dropDownList('test')); $this->assertEquals($expected, Html::dropDownList('test'));
$expected = <<<EOD $expected = <<<EOD
<select name="test"> <select name="test">
<option value="value1">text1</option> <option value="value1">text1</option>
<option value="value2">text2</option> <option value="value2">text2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::dropDownList('test', null, $this->getDataItems())); $this->assertEquals($expected, Html::dropDownList('test', null, $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<select name="test"> <select name="test">
<option value="value1">text1</option> <option value="value1">text1</option>
<option value="value2" selected="selected">text2</option> <option value="value2" selected="selected">text2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::dropDownList('test', 'value2', $this->getDataItems())); $this->assertEquals($expected, Html::dropDownList('test', 'value2', $this->getDataItems()));
} }
public function testListBox() public function testListBox()
{ {
$expected = <<<EOD $expected = <<<EOD
<select name="test" size="4"> <select name="test" size="4">
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test')); $this->assertEquals($expected, Html::listBox('test'));
$expected = <<<EOD $expected = <<<EOD
<select name="test" size="5"> <select name="test" size="5">
<option value="value1">text1</option> <option value="value1">text1</option>
<option value="value2">text2</option> <option value="value2">text2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems(), array('size' => 5))); $this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems(), array('size' => 5)));
$expected = <<<EOD $expected = <<<EOD
<select name="test" size="4"> <select name="test" size="4">
<option value="value1&lt;&gt;">text1&lt;&gt;</option> <option value="value1&lt;&gt;">text1&lt;&gt;</option>
<option value="value 2">text&nbsp;&nbsp;2</option> <option value="value 2">text&nbsp;&nbsp;2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems2())); $this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems2()));
$expected = <<<EOD $expected = <<<EOD
<select name="test" size="4"> <select name="test" size="4">
<option value="value1">text1</option> <option value="value1">text1</option>
<option value="value2" selected="selected">text2</option> <option value="value2" selected="selected">text2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', 'value2', $this->getDataItems())); $this->assertEquals($expected, Html::listBox('test', 'value2', $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<select name="test" size="4"> <select name="test" size="4">
<option value="value1" selected="selected">text1</option> <option value="value1" selected="selected">text1</option>
<option value="value2" selected="selected">text2</option> <option value="value2" selected="selected">text2</option>
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', array('value1', 'value2'), $this->getDataItems())); $this->assertEquals($expected, Html::listBox('test', array('value1', 'value2'), $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<select name="test[]" multiple="multiple" size="4"> <select name="test[]" multiple="multiple" size="4">
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', null, array(), array('multiple' => true))); $this->assertEquals($expected, Html::listBox('test', null, array(), array('multiple' => true)));
$expected = <<<EOD $expected = <<<EOD
<input type="hidden" name="test" value="0" /><select name="test" size="4"> <input type="hidden" name="test" value="0" /><select name="test" size="4">
</select> </select>
EOD; EOD;
$this->assertEquals($expected, Html::listBox('test', '', array(), array('unselect' => '0'))); $this->assertEquals($expected, Html::listBox('test', '', array(), array('unselect' => '0')));
} }
public function testCheckboxList() public function testCheckboxList()
{ {
$this->assertEquals('', Html::checkboxList('test')); $this->assertEquals('', Html::checkboxList('test'));
$expected = <<<EOD $expected = <<<EOD
<label><input type="checkbox" name="test[]" value="value1" /> text1</label> <label><input type="checkbox" name="test[]" value="value1" /> text1</label>
<label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label> <label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label>
EOD; EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems())); $this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<label><input type="checkbox" name="test[]" value="value1&lt;&gt;" /> text1<></label> <label><input type="checkbox" name="test[]" value="value1&lt;&gt;" /> text1<></label>
<label><input type="checkbox" name="test[]" value="value 2" /> text 2</label> <label><input type="checkbox" name="test[]" value="value 2" /> text 2</label>
EOD; EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems2())); $this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems2()));
$expected = <<<EOD $expected = <<<EOD
<input type="hidden" name="test" value="0" /><label><input type="checkbox" name="test[]" value="value1" /> text1</label><br /> <input type="hidden" name="test" value="0" /><label><input type="checkbox" name="test[]" value="value1" /> text1</label><br />
<label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label> <label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label>
EOD; EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array( $this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array(
'separator' => "<br />\n", 'separator' => "<br />\n",
'unselect' => '0', 'unselect' => '0',
))); )));
$expected = <<<EOD $expected = <<<EOD
0<label>text1 <input type="checkbox" name="test[]" value="value1" /></label> 0<label>text1 <input type="checkbox" name="test[]" value="value1" /></label>
1<label>text2 <input type="checkbox" name="test[]" value="value2" checked="checked" /></label> 1<label>text2 <input type="checkbox" name="test[]" value="value2" checked="checked" /></label>
EOD; EOD;
$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array( $this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array(
'item' => function ($index, $label, $name, $checked, $value) { 'item' => function ($index, $label, $name, $checked, $value) {
return $index . Html::label($label . ' ' . Html::checkbox($name, $checked, $value)); return $index . Html::label($label . ' ' . Html::checkbox($name, $checked, $value));
} }
))); )));
} }
public function testRadioList() public function testRadioList()
{ {
$this->assertEquals('', Html::radioList('test')); $this->assertEquals('', Html::radioList('test'));
$expected = <<<EOD $expected = <<<EOD
<label><input type="radio" name="test" value="value1" /> text1</label> <label><input type="radio" name="test" value="value1" /> text1</label>
<label><input type="radio" name="test" value="value2" checked="checked" /> text2</label> <label><input type="radio" name="test" value="value2" checked="checked" /> text2</label>
EOD; EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems())); $this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems()));
$expected = <<<EOD $expected = <<<EOD
<label><input type="radio" name="test" value="value1&lt;&gt;" /> text1<></label> <label><input type="radio" name="test" value="value1&lt;&gt;" /> text1<></label>
<label><input type="radio" name="test" value="value 2" /> text 2</label> <label><input type="radio" name="test" value="value 2" /> text 2</label>
EOD; EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems2())); $this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems2()));
$expected = <<<EOD $expected = <<<EOD
<input type="hidden" name="test" value="0" /><label><input type="radio" name="test" value="value1" /> text1</label><br /> <input type="hidden" name="test" value="0" /><label><input type="radio" name="test" value="value1" /> text1</label><br />
<label><input type="radio" name="test" value="value2" checked="checked" /> text2</label> <label><input type="radio" name="test" value="value2" checked="checked" /> text2</label>
EOD; EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array( $this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array(
'separator' => "<br />\n", 'separator' => "<br />\n",
'unselect' => '0', 'unselect' => '0',
))); )));
$expected = <<<EOD $expected = <<<EOD
0<label>text1 <input type="radio" name="test" value="value1" /></label> 0<label>text1 <input type="radio" name="test" value="value1" /></label>
1<label>text2 <input type="radio" name="test" value="value2" checked="checked" /></label> 1<label>text2 <input type="radio" name="test" value="value2" checked="checked" /></label>
EOD; EOD;
$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array( $this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array(
'item' => function ($index, $label, $name, $checked, $value) { 'item' => function ($index, $label, $name, $checked, $value) {
return $index . Html::label($label . ' ' . Html::radio($name, $checked, $value)); return $index . Html::label($label . ' ' . Html::radio($name, $checked, $value));
} }
))); )));
} }
public function testRenderOptions() public function testRenderOptions()
{ {
$data = array( $data = array(
'value1' => 'label1', 'value1' => 'label1',
'group1' => array( 'group1' => array(
'value11' => 'label11', 'value11' => 'label11',
'group11' => array( 'group11' => array(
'value111' => 'label111', 'value111' => 'label111',
), ),
'group12' => array(), 'group12' => array(),
), ),
'value2' => 'label2', 'value2' => 'label2',
'group2' => array(), 'group2' => array(),
); );
$expected = <<<EOD $expected = <<<EOD
<option value="">please&nbsp;select&lt;&gt;</option> <option value="">please&nbsp;select&lt;&gt;</option>
<option value="value1" selected="selected">label1</option> <option value="value1" selected="selected">label1</option>
<optgroup label="group1"> <optgroup label="group1">
<option value="value11">label11</option> <option value="value11">label11</option>
<optgroup label="group11"> <optgroup label="group11">
<option class="option" value="value111" selected="selected">label111</option> <option class="option" value="value111" selected="selected">label111</option>
</optgroup> </optgroup>
<optgroup class="group" label="group12"> <optgroup class="group" label="group12">
</optgroup> </optgroup>
</optgroup> </optgroup>
<option value="value2">label2</option> <option value="value2">label2</option>
<optgroup label="group2"> <optgroup label="group2">
</optgroup> </optgroup>
EOD; EOD;
$attributes = array( $attributes = array(
'prompt' => 'please select<>', 'prompt' => 'please select<>',
'options' => array( 'options' => array(
'value111' => array('class' => 'option'), 'value111' => array('class' => 'option'),
), ),
'groups' => array( 'groups' => array(
'group12' => array('class' => 'group'), 'group12' => array('class' => 'group'),
), ),
); );
$this->assertEquals($expected, Html::renderSelectOptions(array('value111', 'value1'), $data, $attributes)); $this->assertEquals($expected, Html::renderSelectOptions(array('value111', 'value1'), $data, $attributes));
} }
public function testRenderAttributes() public function testRenderAttributes()
{ {
$this->assertEquals('', Html::renderTagAttributes(array())); $this->assertEquals('', Html::renderTagAttributes(array()));
$this->assertEquals(' name="test" value="1&lt;&gt;"', Html::renderTagAttributes(array('name' => 'test', 'empty' => null, 'value' => '1<>'))); $this->assertEquals(' name="test" value="1&lt;&gt;"', Html::renderTagAttributes(array('name' => 'test', 'empty' => null, 'value' => '1<>')));
Html::$showBooleanAttributeValues = false; Html::$showBooleanAttributeValues = false;
$this->assertEquals(' checked disabled', Html::renderTagAttributes(array('checked' => 'checked', 'disabled' => true, 'hidden' => false))); $this->assertEquals(' checked disabled', Html::renderTagAttributes(array('checked' => 'checked', 'disabled' => true, 'hidden' => false)));
Html::$showBooleanAttributeValues = true; Html::$showBooleanAttributeValues = true;
} }
protected function getDataItems() protected function getDataItems()
{ {
return array( return array(
'value1' => 'text1', 'value1' => 'text1',
'value2' => 'text2', 'value2' => 'text2',
); );
} }
protected function getDataItems2() protected function getDataItems2()
{ {
return array( return array(
'value1<>' => 'text1<>', 'value1<>' => 'text1<>',
'value 2' => 'text 2', 'value 2' => 'text 2',
); );
} }
} }

Loading…
Cancel
Save