HtmlTest.php 21.1 KB
Newer Older
Qiang Xue committed
1 2
<?php

3
namespace yiiunit\framework\helpers;
Qiang Xue committed
4 5 6

use Yii;
use yii\helpers\Html;
7
use yiiunit\TestCase;
Qiang Xue committed
8

9 10 11
/**
 * @group helpers
 */
12
class HtmlTest extends TestCase
Qiang Xue committed
13
{
Carsten Brandt committed
14
	protected function setUp()
Qiang Xue committed
15
	{
Carsten Brandt committed
16
		parent::setUp();
Alexander Makarov committed
17 18 19
		$this->mockApplication([
			'components' => [
				'request' => [
Qiang Xue committed
20 21
					'class' => 'yii\web\Request',
					'url' => '/test',
Qiang Xue committed
22
					'enableCsrfValidation' => false,
Alexander Makarov committed
23 24
				],
				'response' => [
25
					'class' => 'yii\web\Response',
Alexander Makarov committed
26 27 28
				],
			],
		]);
Qiang Xue committed
29 30
	}

31 32 33 34 35 36 37 38
	public function assertEqualsWithoutLE($expected, $actual)
	{
		$expected = str_replace("\r\n", "\n", $expected);
		$actual = str_replace("\r\n", "\n", $actual);

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

Qiang Xue committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	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'));
Alexander Makarov committed
54 55
		$this->assertEquals('<input type="text" name="test" value="&lt;&gt;">', Html::tag('input', '', ['type' => 'text', 'name' => 'test', 'value' => '<>']));
		$this->assertEquals('<span disabled></span>', Html::tag('span', '', ['disabled' => true]));
Qiang Xue committed
56 57 58 59 60
	}

	public function testBeginTag()
	{
		$this->assertEquals('<br>', Html::beginTag('br'));
Alexander Makarov committed
61
		$this->assertEquals('<span id="test" class="title">', Html::beginTag('span', ['id' => 'test', 'class' => 'title']));
Qiang Xue committed
62 63 64 65 66 67 68 69 70 71 72
	}

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

	public function testStyle()
	{
		$content = 'a <>';
73
		$this->assertEquals("<style>$content</style>", Html::style($content));
Alexander Makarov committed
74
		$this->assertEquals("<style type=\"text/less\">$content</style>", Html::style($content, ['type' => 'text/less']));
Qiang Xue committed
75 76 77 78 79
	}

	public function testScript()
	{
		$content = 'a <>';
80
		$this->assertEquals("<script>$content</script>", Html::script($content));
Alexander Makarov committed
81
		$this->assertEquals("<script type=\"text/js\">$content</script>", Html::script($content, ['type' => 'text/js']));
Qiang Xue committed
82 83 84 85
	}

	public function testCssFile()
	{
86 87
		$this->assertEquals('<link href="http://example.com" rel="stylesheet">', Html::cssFile('http://example.com'));
		$this->assertEquals('<link href="/test" rel="stylesheet">', Html::cssFile(''));
Qiang Xue committed
88 89 90 91
	}

	public function testJsFile()
	{
92 93
		$this->assertEquals('<script src="http://example.com"></script>', Html::jsFile('http://example.com'));
		$this->assertEquals('<script src="/test"></script>', Html::jsFile(''));
Qiang Xue committed
94 95 96 97 98 99
	}

	public function testBeginForm()
	{
		$this->assertEquals('<form action="/test" method="post">', Html::beginForm());
		$this->assertEquals('<form action="/example" method="get">', Html::beginForm('/example', 'get'));
Alexander Makarov committed
100
		$hiddens = [
101 102
			'<input type="hidden" name="id" value="1">',
			'<input type="hidden" name="title" value="&lt;">',
Alexander Makarov committed
103
		];
Qiang Xue committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
		$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()
	{
127 128
		$this->assertEquals('<img src="/example" alt="">', Html::img('/example'));
		$this->assertEquals('<img src="/test" alt="">', Html::img(''));
Alexander Makarov committed
129
		$this->assertEquals('<img src="/example" width="10" alt="something">', Html::img('/example', ['alt' => 'something', 'width' => 10]));
Qiang Xue committed
130 131 132 133 134 135
	}

	public function testLabel()
	{
		$this->assertEquals('<label>something<></label>', Html::label('something<>'));
		$this->assertEquals('<label for="a">something<></label>', Html::label('something<>', 'a'));
Alexander Makarov committed
136
		$this->assertEquals('<label class="test" for="a">something<></label>', Html::label('something<>', 'a', ['class' => 'test']));
Qiang Xue committed
137 138 139 140
	}

	public function testButton()
	{
141
		$this->assertEquals('<button>Button</button>', Html::button());
Alexander Makarov committed
142 143
		$this->assertEquals('<button name="test" value="value">content<></button>', Html::button('content<>', ['name' => 'test', 'value' => 'value']));
		$this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::button('content<>', ['type' => 'submit', 'name' => 'test', 'value' => 'value', 'class' => "t"]));
Qiang Xue committed
144 145 146 147 148
	}

	public function testSubmitButton()
	{
		$this->assertEquals('<button type="submit">Submit</button>', Html::submitButton());
Alexander Makarov committed
149
		$this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::submitButton('content<>', ['name' => 'test', 'value' => 'value', 'class' => 't']));
Qiang Xue committed
150 151 152 153 154
	}

	public function testResetButton()
	{
		$this->assertEquals('<button type="reset">Reset</button>', Html::resetButton());
Alexander Makarov committed
155
		$this->assertEquals('<button type="reset" class="t" name="test" value="value">content<></button>', Html::resetButton('content<>', ['name' => 'test', 'value' => 'value', 'class' => 't']));
Qiang Xue committed
156 157 158 159
	}

	public function testInput()
	{
160
		$this->assertEquals('<input type="text">', Html::input('text'));
Alexander Makarov committed
161
		$this->assertEquals('<input type="text" class="t" name="test" value="value">', Html::input('text', 'test', 'value', ['class' => 't']));
Qiang Xue committed
162 163 164 165
	}

	public function testButtonInput()
	{
166
		$this->assertEquals('<input type="button" value="Button">', Html::buttonInput());
Alexander Makarov committed
167
		$this->assertEquals('<input type="button" class="a" name="test" value="text">', Html::buttonInput('text', ['name' => 'test', 'class' => 'a']));
Qiang Xue committed
168 169 170 171
	}

	public function testSubmitInput()
	{
172
		$this->assertEquals('<input type="submit" value="Submit">', Html::submitInput());
Alexander Makarov committed
173
		$this->assertEquals('<input type="submit" class="a" name="test" value="text">', Html::submitInput('text', ['name' => 'test', 'class' => 'a']));
Qiang Xue committed
174 175 176 177
	}

	public function testResetInput()
	{
178
		$this->assertEquals('<input type="reset" value="Reset">', Html::resetInput());
Alexander Makarov committed
179
		$this->assertEquals('<input type="reset" class="a" name="test" value="text">', Html::resetInput('text', ['name' => 'test', 'class' => 'a']));
Qiang Xue committed
180 181 182 183
	}

	public function testTextInput()
	{
184
		$this->assertEquals('<input type="text" name="test">', Html::textInput('test'));
Alexander Makarov committed
185
		$this->assertEquals('<input type="text" class="t" name="test" value="value">', Html::textInput('test', 'value', ['class' => 't']));
Qiang Xue committed
186 187 188 189
	}

	public function testHiddenInput()
	{
190
		$this->assertEquals('<input type="hidden" name="test">', Html::hiddenInput('test'));
Alexander Makarov committed
191
		$this->assertEquals('<input type="hidden" class="t" name="test" value="value">', Html::hiddenInput('test', 'value', ['class' => 't']));
Qiang Xue committed
192 193 194 195
	}

	public function testPasswordInput()
	{
196
		$this->assertEquals('<input type="password" name="test">', Html::passwordInput('test'));
Alexander Makarov committed
197
		$this->assertEquals('<input type="password" class="t" name="test" value="value">', Html::passwordInput('test', 'value', ['class' => 't']));
Qiang Xue committed
198 199 200 201
	}

	public function testFileInput()
	{
202
		$this->assertEquals('<input type="file" name="test">', Html::fileInput('test'));
Alexander Makarov committed
203
		$this->assertEquals('<input type="file" class="t" name="test" value="value">', Html::fileInput('test', 'value', ['class' => 't']));
Qiang Xue committed
204 205 206 207 208
	}

	public function testTextarea()
	{
		$this->assertEquals('<textarea name="test"></textarea>', Html::textarea('test'));
Alexander Makarov committed
209
		$this->assertEquals('<textarea class="t" name="test">value&lt;&gt;</textarea>', Html::textarea('test', 'value<>', ['class' => 't']));
Qiang Xue committed
210 211 212 213
	}

	public function testRadio()
	{
214
		$this->assertEquals('<input type="radio" name="test" value="1">', Html::radio('test'));
Alexander Makarov committed
215 216
		$this->assertEquals('<input type="radio" class="a" name="test" checked>', Html::radio('test', true, ['class' => 'a', 'value' => null]));
		$this->assertEquals('<input type="hidden" name="test" value="0"><input type="radio" class="a" name="test" value="2" checked>', Html::radio('test', true, ['class' => 'a' , 'uncheck' => '0', 'value' => 2]));
Qiang Xue committed
217

Alexander Makarov committed
218
		$this->assertEquals('<div class="radio"><label class="bbb"><input type="radio" class="a" name="test" checked> ccc</label></div>', Html::radio('test', true, [
Qiang Xue committed
219 220 221
			'class' => 'a',
			'value' => null,
			'label' => 'ccc',
Alexander Makarov committed
222 223 224
			'labelOptions' => ['class' =>'bbb'],
		]));
		$this->assertEquals('<input type="hidden" name="test" value="0"><div class="radio"><label><input type="radio" class="a" name="test" value="2" checked> ccc</label></div>', Html::radio('test', true, [
Qiang Xue committed
225 226 227 228
			'class' => 'a',
			'uncheck' => '0',
			'label' => 'ccc',
			'value' => 2,
Alexander Makarov committed
229
		]));
Qiang Xue committed
230 231 232 233
	}

	public function testCheckbox()
	{
234
		$this->assertEquals('<input type="checkbox" name="test" value="1">', Html::checkbox('test'));
Alexander Makarov committed
235 236
		$this->assertEquals('<input type="checkbox" class="a" name="test" checked>', Html::checkbox('test', true, ['class' => 'a', 'value' => null]));
		$this->assertEquals('<input type="hidden" name="test" value="0"><input type="checkbox" class="a" name="test" value="2" checked>', Html::checkbox('test', true, ['class' => 'a', 'uncheck' => '0', 'value' => 2]));
Qiang Xue committed
237

Alexander Makarov committed
238
		$this->assertEquals('<div class="checkbox"><label class="bbb"><input type="checkbox" class="a" name="test" checked> ccc</label></div>', Html::checkbox('test', true, [
Qiang Xue committed
239 240 241
			'class' => 'a',
			'value' => null,
			'label' => 'ccc',
Alexander Makarov committed
242 243 244
			'labelOptions' => ['class' =>'bbb'],
		]));
		$this->assertEquals('<input type="hidden" name="test" value="0"><div class="checkbox"><label><input type="checkbox" class="a" name="test" value="2" checked> ccc</label></div>', Html::checkbox('test', true, [
Qiang Xue committed
245 246 247 248
			'class' => 'a',
			'uncheck' => '0',
			'label' => 'ccc',
			'value' => 2,
Alexander Makarov committed
249
		]));
Qiang Xue committed
250 251 252 253 254 255 256 257 258
	}

	public function testDropDownList()
	{
		$expected = <<<EOD
<select name="test">

</select>
EOD;
259
		$this->assertEqualsWithoutLE($expected, Html::dropDownList('test'));
Qiang Xue committed
260 261 262 263 264 265
		$expected = <<<EOD
<select name="test">
<option value="value1">text1</option>
<option value="value2">text2</option>
</select>
EOD;
266
		$this->assertEqualsWithoutLE($expected, Html::dropDownList('test', null, $this->getDataItems()));
Qiang Xue committed
267 268 269
		$expected = <<<EOD
<select name="test">
<option value="value1">text1</option>
270
<option value="value2" selected>text2</option>
Qiang Xue committed
271 272
</select>
EOD;
273
		$this->assertEqualsWithoutLE($expected, Html::dropDownList('test', 'value2', $this->getDataItems()));
Qiang Xue committed
274 275 276 277 278 279 280 281 282
	}

	public function testListBox()
	{
		$expected = <<<EOD
<select name="test" size="4">

</select>
EOD;
283
		$this->assertEqualsWithoutLE($expected, Html::listBox('test'));
Qiang Xue committed
284 285 286 287 288 289
		$expected = <<<EOD
<select name="test" size="5">
<option value="value1">text1</option>
<option value="value2">text2</option>
</select>
EOD;
Alexander Makarov committed
290
		$this->assertEqualsWithoutLE($expected, Html::listBox('test', null, $this->getDataItems(), ['size' => 5]));
Qiang Xue committed
291 292 293 294 295 296
		$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;
297
		$this->assertEqualsWithoutLE($expected, Html::listBox('test', null, $this->getDataItems2()));
Qiang Xue committed
298 299 300
		$expected = <<<EOD
<select name="test" size="4">
<option value="value1">text1</option>
301
<option value="value2" selected>text2</option>
Qiang Xue committed
302 303
</select>
EOD;
304
		$this->assertEqualsWithoutLE($expected, Html::listBox('test', 'value2', $this->getDataItems()));
Qiang Xue committed
305 306
		$expected = <<<EOD
<select name="test" size="4">
307 308
<option value="value1" selected>text1</option>
<option value="value2" selected>text2</option>
Qiang Xue committed
309 310
</select>
EOD;
Alexander Makarov committed
311
		$this->assertEqualsWithoutLE($expected, Html::listBox('test', ['value1', 'value2'], $this->getDataItems()));
Qiang Xue committed
312 313

		$expected = <<<EOD
314
<select name="test[]" multiple size="4">
Qiang Xue committed
315 316 317

</select>
EOD;
Alexander Makarov committed
318
		$this->assertEqualsWithoutLE($expected, Html::listBox('test', null, [], ['multiple' => true]));
Qiang Xue committed
319
		$expected = <<<EOD
320
<input type="hidden" name="test" value="0"><select name="test" size="4">
Qiang Xue committed
321 322 323

</select>
EOD;
Alexander Makarov committed
324
		$this->assertEqualsWithoutLE($expected, Html::listBox('test', '', [], ['unselect' => '0']));
Qiang Xue committed
325 326 327 328
	}

	public function testCheckboxList()
	{
Qiang Xue committed
329
		$this->assertEquals('<div></div>', Html::checkboxList('test'));
Qiang Xue committed
330 331

		$expected = <<<EOD
Qiang Xue committed
332 333
<div><div class="checkbox"><label><input type="checkbox" name="test[]" value="value1"> text1</label></div>
<div class="checkbox"><label><input type="checkbox" name="test[]" value="value2" checked> text2</label></div></div>
Qiang Xue committed
334
EOD;
Alexander Makarov committed
335
		$this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $this->getDataItems()));
Qiang Xue committed
336 337

		$expected = <<<EOD
Qiang Xue committed
338 339
<div><div class="checkbox"><label><input type="checkbox" name="test[]" value="value1&lt;&gt;"> text1&lt;&gt;</label></div>
<div class="checkbox"><label><input type="checkbox" name="test[]" value="value  2"> text  2</label></div></div>
Qiang Xue committed
340
EOD;
Alexander Makarov committed
341
		$this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $this->getDataItems2()));
Qiang Xue committed
342 343

		$expected = <<<EOD
Qiang Xue committed
344 345
<input type="hidden" name="test" value="0"><div><div class="checkbox"><label><input type="checkbox" name="test[]" value="value1"> text1</label></div><br>
<div class="checkbox"><label><input type="checkbox" name="test[]" value="value2" checked> text2</label></div></div>
Qiang Xue committed
346
EOD;
Alexander Makarov committed
347
		$this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $this->getDataItems(), [
348
			'separator' => "<br>\n",
Qiang Xue committed
349
			'unselect' => '0',
Alexander Makarov committed
350
		]));
Qiang Xue committed
351 352

		$expected = <<<EOD
Qiang Xue committed
353 354
<div>0<label>text1 <input type="checkbox" name="test[]" value="value1"></label>
1<label>text2 <input type="checkbox" name="test[]" value="value2" checked></label></div>
Qiang Xue committed
355
EOD;
Alexander Makarov committed
356
		$this->assertEqualsWithoutLE($expected, Html::checkboxList('test', ['value2'], $this->getDataItems(), [
Qiang Xue committed
357
			'item' => function ($index, $label, $name, $checked, $value) {
Alexander Makarov committed
358
				return $index . Html::label($label . ' ' . Html::checkbox($name, $checked, ['value' => $value]));
Qiang Xue committed
359
			}
Alexander Makarov committed
360
		]));
Qiang Xue committed
361 362 363 364
	}

	public function testRadioList()
	{
Qiang Xue committed
365
		$this->assertEquals('<div></div>', Html::radioList('test'));
Qiang Xue committed
366 367

		$expected = <<<EOD
Qiang Xue committed
368 369
<div><div class="radio"><label><input type="radio" name="test" value="value1"> text1</label></div>
<div class="radio"><label><input type="radio" name="test" value="value2" checked> text2</label></div></div>
Qiang Xue committed
370
EOD;
Alexander Makarov committed
371
		$this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $this->getDataItems()));
Qiang Xue committed
372 373

		$expected = <<<EOD
Qiang Xue committed
374 375
<div><div class="radio"><label><input type="radio" name="test" value="value1&lt;&gt;"> text1&lt;&gt;</label></div>
<div class="radio"><label><input type="radio" name="test" value="value  2"> text  2</label></div></div>
Qiang Xue committed
376
EOD;
Alexander Makarov committed
377
		$this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $this->getDataItems2()));
Qiang Xue committed
378 379

		$expected = <<<EOD
Qiang Xue committed
380 381
<input type="hidden" name="test" value="0"><div><div class="radio"><label><input type="radio" name="test" value="value1"> text1</label></div><br>
<div class="radio"><label><input type="radio" name="test" value="value2" checked> text2</label></div></div>
Qiang Xue committed
382
EOD;
Alexander Makarov committed
383
		$this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $this->getDataItems(), [
384
			'separator' => "<br>\n",
Qiang Xue committed
385
			'unselect' => '0',
Alexander Makarov committed
386
		]));
Qiang Xue committed
387 388

		$expected = <<<EOD
Qiang Xue committed
389 390
<div>0<label>text1 <input type="radio" name="test" value="value1"></label>
1<label>text2 <input type="radio" name="test" value="value2" checked></label></div>
Qiang Xue committed
391
EOD;
Alexander Makarov committed
392
		$this->assertEqualsWithoutLE($expected, Html::radioList('test', ['value2'], $this->getDataItems(), [
Qiang Xue committed
393
			'item' => function ($index, $label, $name, $checked, $value) {
Alexander Makarov committed
394
				return $index . Html::label($label . ' ' . Html::radio($name, $checked, ['value' => $value]));
Qiang Xue committed
395
			}
Alexander Makarov committed
396
		]));
Qiang Xue committed
397 398
	}

399 400
	public function testUl()
	{
Alexander Makarov committed
401
		$data = [
402
			1, 'abc', '<>',
Alexander Makarov committed
403
		];
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
		$expected = <<<EOD
<ul>
<li>1</li>
<li>abc</li>
<li>&lt;&gt;</li>
</ul>
EOD;
		$this->assertEqualsWithoutLE($expected, Html::ul($data));
		$expected = <<<EOD
<ul class="test">
<li class="item-0">1</li>
<li class="item-1">abc</li>
<li class="item-2"><></li>
</ul>
EOD;
Alexander Makarov committed
419
		$this->assertEqualsWithoutLE($expected, Html::ul($data, [
420
			'class' => 'test',
Alexander Makarov committed
421
			'item' => function ($item, $index) {
422 423
				return "<li class=\"item-$index\">$item</li>";
			}
Alexander Makarov committed
424
		]));
425 426 427 428
	}

	public function testOl()
	{
Alexander Makarov committed
429
		$data = [
430
			1, 'abc', '<>',
Alexander Makarov committed
431
		];
432 433
		$expected = <<<EOD
<ol>
434 435 436
<li class="ti">1</li>
<li class="ti">abc</li>
<li class="ti">&lt;&gt;</li>
437 438
</ol>
EOD;
Alexander Makarov committed
439 440 441
		$this->assertEqualsWithoutLE($expected, Html::ol($data, [
			'itemOptions' => ['class' => 'ti'],
		]));
442 443 444 445 446 447 448
		$expected = <<<EOD
<ol class="test">
<li class="item-0">1</li>
<li class="item-1">abc</li>
<li class="item-2"><></li>
</ol>
EOD;
Alexander Makarov committed
449
		$this->assertEqualsWithoutLE($expected, Html::ol($data, [
450
			'class' => 'test',
Alexander Makarov committed
451
			'item' => function ($item, $index) {
452 453
				return "<li class=\"item-$index\">$item</li>";
			}
Alexander Makarov committed
454
		]));
455 456
	}

Qiang Xue committed
457 458
	public function testRenderOptions()
	{
Alexander Makarov committed
459
		$data = [
Qiang Xue committed
460
			'value1' => 'label1',
Alexander Makarov committed
461
			'group1' => [
Qiang Xue committed
462
				'value11' => 'label11',
Alexander Makarov committed
463
				'group11' => [
Qiang Xue committed
464
					'value111' => 'label111',
Alexander Makarov committed
465 466 467
				],
				'group12' => [],
			],
Qiang Xue committed
468
			'value2' => 'label2',
Alexander Makarov committed
469 470
			'group2' => [],
		];
Qiang Xue committed
471 472
		$expected = <<<EOD
<option value="">please&nbsp;select&lt;&gt;</option>
473
<option value="value1" selected>label1</option>
Qiang Xue committed
474 475 476
<optgroup label="group1">
<option value="value11">label11</option>
<optgroup label="group11">
477
<option class="option" value="value111" selected>label111</option>
Qiang Xue committed
478 479 480 481 482 483 484 485 486 487
</optgroup>
<optgroup class="group" label="group12">

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

</optgroup>
EOD;
Alexander Makarov committed
488
		$attributes = [
Qiang Xue committed
489
			'prompt' => 'please select<>',
Alexander Makarov committed
490 491 492 493 494 495 496 497
			'options' => [
				'value111' => ['class' => 'option'],
			],
			'groups' => [
				'group12' => ['class' => 'group'],
			],
		];
		$this->assertEqualsWithoutLE($expected, Html::renderSelectOptions(['value111', 'value1'], $data, $attributes));
Qiang Xue committed
498 499 500 501
	}

	public function testRenderAttributes()
	{
Alexander Makarov committed
502 503 504
		$this->assertEquals('', Html::renderTagAttributes([]));
		$this->assertEquals(' name="test" value="1&lt;&gt;"', Html::renderTagAttributes(['name' => 'test', 'empty' => null, 'value' => '1<>']));
		$this->assertEquals(' checked disabled', Html::renderTagAttributes(['checked' => true, 'disabled' => true, 'hidden' => false]));
Qiang Xue committed
505 506
	}

507 508
	public function testAddCssClass()
	{
Alexander Makarov committed
509
		$options = [];
510
		Html::addCssClass($options, 'test');
Alexander Makarov committed
511
		$this->assertEquals(['class' => 'test'], $options);
512
		Html::addCssClass($options, 'test');
Alexander Makarov committed
513
		$this->assertEquals(['class' => 'test'], $options);
514
		Html::addCssClass($options, 'test2');
Alexander Makarov committed
515
		$this->assertEquals(['class' => 'test test2'], $options);
516
		Html::addCssClass($options, 'test');
Alexander Makarov committed
517
		$this->assertEquals(['class' => 'test test2'], $options);
518
		Html::addCssClass($options, 'test2');
Alexander Makarov committed
519
		$this->assertEquals(['class' => 'test test2'], $options);
520
		Html::addCssClass($options, 'test3');
Alexander Makarov committed
521
		$this->assertEquals(['class' => 'test test2 test3'], $options);
522
		Html::addCssClass($options, 'test2');
Alexander Makarov committed
523
		$this->assertEquals(['class' => 'test test2 test3'], $options);
524 525 526 527
	}

	public function testRemoveCssClass()
	{
Alexander Makarov committed
528
		$options = ['class' => 'test test2 test3'];
529
		Html::removeCssClass($options, 'test2');
Alexander Makarov committed
530
		$this->assertEquals(['class' => 'test test3'], $options);
531
		Html::removeCssClass($options, 'test2');
Alexander Makarov committed
532
		$this->assertEquals(['class' => 'test test3'], $options);
533
		Html::removeCssClass($options, 'test');
Alexander Makarov committed
534
		$this->assertEquals(['class' => 'test3'], $options);
535
		Html::removeCssClass($options, 'test3');
Alexander Makarov committed
536
		$this->assertEquals([], $options);
537 538
	}

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
	public function testCssStyleFromArray()
	{
		$this->assertEquals('width: 100px; height: 200px;', Html::cssStyleFromArray([
			'width' => '100px',
			'height' => '200px',
		]));
		$this->assertNull(Html::cssStyleFromArray([]));
	}

	public function testCssStyleToArray()
	{
		$this->assertEquals([
			'width' => '100px',
			'height' => '200px',
		], Html::cssStyleToArray('width: 100px; height: 200px;'));
		$this->assertEquals([], Html::cssStyleToArray('  '));
	}

	public function testAddCssStyle()
	{
		$options = ['style' => 'width: 100px; height: 200px;'];
		Html::addCssStyle($options, 'width: 110px; color: red;');
		$this->assertEquals('width: 110px; height: 200px; color: red;', $options['style']);

		$options = ['style' => 'width: 100px; height: 200px;'];
		Html::addCssStyle($options, ['width' => '110px', 'color' => 'red']);
		$this->assertEquals('width: 110px; height: 200px; color: red;', $options['style']);

		$options = ['style' => 'width: 100px; height: 200px;'];
		Html::addCssStyle($options, 'width: 110px; color: red;', false);
		$this->assertEquals('width: 100px; height: 200px; color: red;', $options['style']);

		$options = [];
		Html::addCssStyle($options, 'width: 110px; color: red;');
		$this->assertEquals('width: 110px; color: red;', $options['style']);

		$options = [];
		Html::addCssStyle($options, 'width: 110px; color: red;', false);
		$this->assertEquals('width: 110px; color: red;', $options['style']);
	}

	public function testRemoveCssStyle()
	{
		$options = ['style' => 'width: 110px; height: 200px; color: red;'];
		Html::removeCssStyle($options, 'width');
		$this->assertEquals('height: 200px; color: red;', $options['style']);
		Html::removeCssStyle($options, ['height']);
		$this->assertEquals('color: red;', $options['style']);
		Html::removeCssStyle($options, ['color', 'background']);
		$this->assertNull($options['style']);

		$options = [];
		Html::removeCssStyle($options, ['color', 'background']);
		$this->assertTrue(!array_key_exists('style', $options));
	}

Qiang Xue committed
595 596
	protected function getDataItems()
	{
Alexander Makarov committed
597
		return [
Qiang Xue committed
598 599
			'value1' => 'text1',
			'value2' => 'text2',
Alexander Makarov committed
600
		];
Qiang Xue committed
601 602 603 604
	}

	protected function getDataItems2()
	{
Alexander Makarov committed
605
		return [
Qiang Xue committed
606 607
			'value1<>' => 'text1<>',
			'value  2' => 'text  2',
Alexander Makarov committed
608
		];
Qiang Xue committed
609 610
	}
}