HtmlTest.php 16.4 KB
Newer Older
Qiang Xue committed
1 2 3 4 5
<?php

namespace yiiunit\framework\util;

use Yii;
Qiang Xue committed
6
use yii\helpers\Html;
Qiang Xue committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
use yii\web\Application;

class HtmlTest extends \yii\test\TestCase
{
	public function setUp()
	{
		new Application('test', '@yiiunit/runtime', array(
			'components' => array(
				'request' => array(
					'class' => 'yii\web\Request',
					'url' => '/test',
				),
			),
		));
	}

	public function tearDown()
	{
		Yii::$app = null;
	}

	public function testEncode()
	{
		$this->assertEquals("a&lt;&gt;&amp;&quot;&#039;", Html::encode("a<>&\"'"));
	}

	public function testDecode()
	{
		$this->assertEquals("a<>&\"'", Html::decode("a&lt;&gt;&amp;&quot;&#039;"));
	}

	public function testTag()
	{
		$this->assertEquals('<br />', Html::tag('br'));
		$this->assertEquals('<span></span>', Html::tag('span'));
		$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' => '<>')));

		Html::$closeVoidElements = false;

		$this->assertEquals('<br>', Html::tag('br'));
		$this->assertEquals('<span></span>', Html::tag('span'));
		$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' => '<>')));

		Html::$closeVoidElements = true;
Qiang Xue committed
53 54 55 56 57

		$this->assertEquals('<span disabled="disabled"></span>', Html::tag('span', '', array('disabled' => true)));
		Html::$showBooleanAttributeValues = false;
		$this->assertEquals('<span disabled></span>', Html::tag('span', '', array('disabled' => true)));
		Html::$showBooleanAttributeValues = true;
Qiang Xue committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	}

	public function testBeginTag()
	{
		$this->assertEquals('<br>', Html::beginTag('br'));
		$this->assertEquals('<span id="test" class="title">', Html::beginTag('span', array('id' => 'test', 'class' => 'title')));
	}

	public function testEndTag()
	{
		$this->assertEquals('</br>', Html::endTag('br'));
		$this->assertEquals('</span>', Html::endTag('span'));
	}

	public function testCdata()
	{
		$data = 'test<>';
		$this->assertEquals('<![CDATA[' . $data . ']]>', Html::cdata($data));
	}

	public function testStyle()
	{
		$content = 'a <>';
		$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')));
	}

	public function testScript()
	{
		$content = 'a <>';
		$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')));
	}

	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="/test" rel="stylesheet" />', Html::cssFile(''));
	}

	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="/test"></script>', Html::jsFile(''));
	}

	public function testBeginForm()
	{
		$this->assertEquals('<form action="/test" method="post">', Html::beginForm());
		$this->assertEquals('<form action="/example" method="get">', Html::beginForm('/example', 'get'));
		$hiddens = array(
			'<input type="hidden" name="id" value="1" />',
			'<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'));
	}

	public function testEndForm()
	{
		$this->assertEquals('</form>', Html::endForm());
	}

	public function testA()
	{
		$this->assertEquals('<a>something<></a>', Html::a('something<>'));
		$this->assertEquals('<a href="/example">something</a>', Html::a('something', '/example'));
		$this->assertEquals('<a href="/test">something</a>', Html::a('something', ''));
	}

	public function testMailto()
	{
		$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>'));
	}

	public function testImg()
	{
		$this->assertEquals('<img src="/example" alt="" />', Html::img('/example'));
		$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)));
	}

	public function testLabel()
	{
		$this->assertEquals('<label>something<></label>', Html::label('something<>'));
		$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')));
	}

	public function testButton()
	{
		$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="submit" class="t" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>', array('type' => 'submit', 'class' => "t")));
	}

	public function testSubmitButton()
	{
		$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')));
	}

	public function testResetButton()
	{
		$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')));
	}

	public function testInput()
	{
		$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')));
	}

	public function testButtonInput()
	{
Qiang Xue committed
174 175
		$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')));
Qiang Xue committed
176 177 178 179
	}

	public function testSubmitInput()
	{
Qiang Xue committed
180 181
		$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')));
Qiang Xue committed
182 183 184 185
	}

	public function testResetInput()
	{
Qiang Xue committed
186 187
		$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')));
Qiang Xue committed
188 189 190 191
	}

	public function testTextInput()
	{
Qiang Xue committed
192 193
		$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')));
Qiang Xue committed
194 195 196 197
	}

	public function testHiddenInput()
	{
Qiang Xue committed
198 199
		$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')));
Qiang Xue committed
200 201 202 203
	}

	public function testPasswordInput()
	{
Qiang Xue committed
204 205
		$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')));
Qiang Xue committed
206 207 208 209
	}

	public function testFileInput()
	{
Qiang Xue committed
210 211
		$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')));
Qiang Xue committed
212 213 214 215
	}

	public function testTextarea()
	{
Qiang Xue committed
216 217
		$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')));
Qiang Xue committed
218 219 220 221
	}

	public function testRadio()
	{
Qiang Xue committed
222
		$this->assertEquals('<input type="radio" name="test" value="1" />', Html::radio('test'));
Qiang Xue committed
223 224
		$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')));
Qiang Xue committed
225 226 227 228
	}

	public function testCheckbox()
	{
Qiang Xue committed
229
		$this->assertEquals('<input type="checkbox" name="test" value="1" />', Html::checkbox('test'));
Qiang Xue committed
230 231
		$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')));
Qiang Xue committed
232 233 234 235
	}

	public function testDropDownList()
	{
Qiang Xue committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
		$expected = <<<EOD
<select name="test">

</select>
EOD;
		$this->assertEquals($expected, Html::dropDownList('test'));
		$expected = <<<EOD
<select name="test">
<option value="value1">text1</option>
<option value="value2">text2</option>
</select>
EOD;
		$this->assertEquals($expected, Html::dropDownList('test', null, $this->getDataItems()));
		$expected = <<<EOD
<select name="test">
<option value="value1">text1</option>
<option value="value2" selected="selected">text2</option>
</select>
EOD;
		$this->assertEquals($expected, Html::dropDownList('test', 'value2', $this->getDataItems()));
Qiang Xue committed
256 257 258 259
	}

	public function testListBox()
	{
Qiang Xue committed
260 261 262 263 264 265 266 267 268 269 270 271
		$expected = <<<EOD
<select name="test" size="4">

</select>
EOD;
		$this->assertEquals($expected, Html::listBox('test'));
		$expected = <<<EOD
<select name="test" size="5">
<option value="value1">text1</option>
<option value="value2">text2</option>
</select>
EOD;
Qiang Xue committed
272
		$this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems(), array('size' => 5)));
Qiang Xue committed
273 274 275 276 277 278
		$expected = <<<EOD
<select name="test" size="4">
<option value="value1&lt;&gt;">text1&lt;&gt;</option>
<option value="value  2">text&nbsp;&nbsp;2</option>
</select>
EOD;
Qiang Xue committed
279
		$this->assertEquals($expected, Html::listBox('test', null, $this->getDataItems2()));
Qiang Xue committed
280 281 282 283 284 285
		$expected = <<<EOD
<select name="test" size="4">
<option value="value1">text1</option>
<option value="value2" selected="selected">text2</option>
</select>
EOD;
Qiang Xue committed
286
		$this->assertEquals($expected, Html::listBox('test', 'value2', $this->getDataItems()));
Qiang Xue committed
287 288 289 290 291 292
		$expected = <<<EOD
<select name="test" size="4">
<option value="value1" selected="selected">text1</option>
<option value="value2" selected="selected">text2</option>
</select>
EOD;
Qiang Xue committed
293
		$this->assertEquals($expected, Html::listBox('test', array('value1', 'value2'), $this->getDataItems()));
Qiang Xue committed
294 295 296 297 298 299

		$expected = <<<EOD
<select name="test[]" multiple="multiple" size="4">

</select>
EOD;
Qiang Xue committed
300
		$this->assertEquals($expected, Html::listBox('test', null, array(), array('multiple' => true)));
Qiang Xue committed
301 302 303 304 305
		$expected = <<<EOD
<input type="hidden" name="test" value="0" /><select name="test" size="4">

</select>
EOD;
Qiang Xue committed
306
		$this->assertEquals($expected, Html::listBox('test', '', array(), array('unselect' => '0')));
Qiang Xue committed
307 308 309 310
	}

	public function testCheckboxList()
	{
Qiang Xue committed
311 312 313 314 315 316
		$this->assertEquals('', Html::checkboxList('test'));

		$expected = <<<EOD
<label><input type="checkbox" name="test[]" value="value1" /> text1</label>
<label><input type="checkbox" name="test[]" value="value2" checked="checked" /> text2</label>
EOD;
Qiang Xue committed
317
		$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems()));
Qiang Xue committed
318 319 320 321 322

		$expected = <<<EOD
<label><input type="checkbox" name="test[]" value="value1&lt;&gt;" /> text1<></label>
<label><input type="checkbox" name="test[]" value="value  2" /> text  2</label>
EOD;
Qiang Xue committed
323
		$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems2()));
Qiang Xue committed
324 325 326 327 328

		$expected = <<<EOD
<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>
EOD;
Qiang Xue committed
329
		$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array(
Qiang Xue committed
330 331 332 333 334
			'separator' => "<br />\n",
			'unselect' => '0',
		)));

		$expected = <<<EOD
Qiang Xue committed
335 336
0<label>text1 <input type="checkbox" name="test[]" value="value1" /></label>
1<label>text2 <input type="checkbox" name="test[]" value="value2" checked="checked" /></label>
Qiang Xue committed
337
EOD;
Qiang Xue committed
338 339 340
		$this->assertEquals($expected, Html::checkboxList('test', array('value2'), $this->getDataItems(), array(
			'item' => function ($index, $label, $name, $checked, $value) {
				return $index . Html::label($label . ' ' . Html::checkbox($name, $checked, $value));
Qiang Xue committed
341 342
			}
		)));
Qiang Xue committed
343 344 345 346
	}

	public function testRadioList()
	{
Qiang Xue committed
347 348 349 350 351 352
		$this->assertEquals('', Html::radioList('test'));

		$expected = <<<EOD
<label><input type="radio" name="test" value="value1" /> text1</label>
<label><input type="radio" name="test" value="value2" checked="checked" /> text2</label>
EOD;
Qiang Xue committed
353
		$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems()));
Qiang Xue committed
354 355 356 357 358

		$expected = <<<EOD
<label><input type="radio" name="test" value="value1&lt;&gt;" /> text1<></label>
<label><input type="radio" name="test" value="value  2" /> text  2</label>
EOD;
Qiang Xue committed
359
		$this->assertEquals($expected, Html::radioList('test',  array('value2'), $this->getDataItems2()));
Qiang Xue committed
360 361 362 363 364

		$expected = <<<EOD
<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>
EOD;
Qiang Xue committed
365
		$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array(
Qiang Xue committed
366 367 368 369 370
			'separator' => "<br />\n",
			'unselect' => '0',
		)));

		$expected = <<<EOD
Qiang Xue committed
371 372
0<label>text1 <input type="radio" name="test" value="value1" /></label>
1<label>text2 <input type="radio" name="test" value="value2" checked="checked" /></label>
Qiang Xue committed
373
EOD;
Qiang Xue committed
374 375 376
		$this->assertEquals($expected, Html::radioList('test', array('value2'), $this->getDataItems(), array(
			'item' => function ($index, $label, $name, $checked, $value) {
				return $index . Html::label($label . ' ' . Html::radio($name, $checked, $value));
Qiang Xue committed
377 378
			}
		)));
Qiang Xue committed
379 380 381 382
	}

	public function testRenderOptions()
	{
Qiang Xue committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
		$data = array(
			'value1' => 'label1',
			'group1' => array(
				'value11' => 'label11',
				'group11' => array(
					'value111' => 'label111',
				),
				'group12' => array(),
			),
			'value2' => 'label2',
			'group2' => array(),
		);
		$expected = <<<EOD
<option value="">please&nbsp;select&lt;&gt;</option>
<option value="value1" selected="selected">label1</option>
<optgroup label="group1">
<option value="value11">label11</option>
<optgroup label="group11">
<option class="option" value="value111" selected="selected">label111</option>
</optgroup>
<optgroup class="group" label="group12">

</optgroup>
</optgroup>
<option value="value2">label2</option>
<optgroup label="group2">

</optgroup>
EOD;
		$attributes = array(
			'prompt' => 'please select<>',
			'options' => array(
				'value111' => array('class' => 'option'),
			),
			'groups' => array(
				'group12' => array('class' => 'group'),
			),
		);
Qiang Xue committed
421
		$this->assertEquals($expected, Html::renderSelectOptions(array('value111', 'value1'), $data, $attributes));
Qiang Xue committed
422 423 424 425
	}

	public function testRenderAttributes()
	{
Qiang Xue committed
426 427
		$this->assertEquals('', Html::renderTagAttributes(array()));
		$this->assertEquals(' name="test" value="1&lt;&gt;"', Html::renderTagAttributes(array('name' => 'test', 'empty' => null, 'value' => '1<>')));
Qiang Xue committed
428
		Html::$showBooleanAttributeValues = false;
Qiang Xue committed
429
		$this->assertEquals(' checked disabled', Html::renderTagAttributes(array('checked' => 'checked', 'disabled' => true, 'hidden' => false)));
Qiang Xue committed
430 431 432 433 434 435 436 437 438
		Html::$showBooleanAttributeValues = true;
	}

	protected function getDataItems()
	{
		return array(
			'value1' => 'text1',
			'value2' => 'text2',
		);
Qiang Xue committed
439 440
	}

Qiang Xue committed
441
	protected function getDataItems2()
Qiang Xue committed
442
	{
Qiang Xue committed
443 444 445 446
		return array(
			'value1<>' => 'text1<>',
			'value  2' => 'text  2',
		);
Qiang Xue committed
447 448
	}
}