Skip to content

Commit

Permalink
Add test coverage of yii\helpers\Html (#14220)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyar authored and samdark committed May 28, 2017
1 parent 0d9cd0c commit e2218cb
Showing 1 changed file with 210 additions and 0 deletions.
210 changes: 210 additions & 0 deletions tests/framework/helpers/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function testCssFile()
$this->assertEquals('<link href="/test" rel="stylesheet">', Html::cssFile(''));
$this->assertEquals("<!--[if IE 9]>\n" . '<link href="http://example.com" rel="stylesheet">' . "\n<![endif]-->", Html::cssFile('http://example.com', ['condition' => 'IE 9']));
$this->assertEquals("<!--[if (gte IE 9)|(!IE)]><!-->\n" . '<link href="http://example.com" rel="stylesheet">' . "\n<!--<![endif]-->", Html::cssFile('http://example.com', ['condition' => '(gte IE 9)|(!IE)']));
$this->assertEquals('<noscript><link href="http://example.com" rel="stylesheet"></noscript>', Html::cssFile('http://example.com', ['noscript' => true]));
}

public function testJsFile()
Expand All @@ -100,6 +101,76 @@ public function testJsFile()
$this->assertEquals("<!--[if (gte IE 9)|(!IE)]><!-->\n" . '<script src="http://example.com"></script>' . "\n<!--<![endif]-->", Html::jsFile('http://example.com', ['condition' => '(gte IE 9)|(!IE)']));
}

public function testCsrfMetaTagsDisableCsrfValidation()
{
$this->mockApplication([
'components' => [
'request' => [
'class' => 'yii\web\Request',
'enableCsrfValidation' => false,
],
],
]);
$this->assertEquals('', Html::csrfMetaTags());
}

public function testCsrfMetaTagsEnableCsrfValidation()
{
$this->mockApplication([
'components' => [
'request' => [
'class' => 'yii\web\Request',
'enableCsrfValidation' => true,
'cookieValidationKey' => 'key',
],
'response' => [
'class' => 'yii\web\Response',
],
],
]);
$pattern = '<meta name="csrf-param" content="_csrf">%A<meta name="csrf-token" content="%s">';
$actual = Html::csrfMetaTags();
$this->assertStringMatchesFormat($pattern, $actual);
}

public function testCsrfMetaTagsEnableCsrfValidationWithoutCookieValidationKey()
{
$request = $this->getMock('yii\\web\\Request');
$request->method('enableCsrfValidation')->willReturn(true);
Yii::$app->set('request', $request);
$pattern = '<meta name="csrf-param" content="_csrf">%A<meta name="csrf-token">';
$actual = Html::csrfMetaTags();
$this->assertStringMatchesFormat($pattern, $actual);
}

/**
* @dataProvider dataProviderBeginFormSimulateViaPost
*
* @param string $expected
* @param string $method
*/
public function testBeginFormSimulateViaPost($expected, $method)
{
$actual = Html::beginForm('/foo', $method);
$this->assertStringMatchesFormat($expected, $actual);
}

/**
* Data provider for [[testBeginFormSimulateViaPost()]]
* @return array test data
*/
public function dataProviderBeginFormSimulateViaPost()
{
return [
['<form action="/foo" method="GET">', 'GET'],
['<form action="/foo" method="POST">', 'POST'],
['<form action="/foo" method="post">%A<input type="hidden" name="_method" value="DELETE">', 'DELETE'],
['<form action="/foo" method="post">%A<input type="hidden" name="_method" value="GETFOO">', 'GETFOO'],
['<form action="/foo" method="post">%A<input type="hidden" name="_method" value="POSTFOO">', 'POSTFOO'],
['<form action="/foo" method="post">%A<input type="hidden" name="_method" value="POSTFOOPOST">', 'POSTFOOPOST'],
];
}

public function testBeginForm()
{
$this->assertEquals('<form action="/test" method="post">', Html::beginForm());
Expand All @@ -109,6 +180,10 @@ public function testBeginForm()
'<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'));

$expected = '<form action="/foo" method="GET">%A<input type="hidden" name="p" value="">';
$actual = Html::beginForm('/foo?p', 'GET');
$this->assertStringMatchesFormat($expected, $actual);
}

public function testEndForm()
Expand Down Expand Up @@ -421,6 +496,13 @@ public function testDropDownList()
'value2' => ['selected' => true]
],
]));

$expected = <<<EOD
<select name="test[]" multiple="true" size="4">
</select>
EOD;
$this->assertEqualsWithoutLE($expected, Html::dropDownList('test', null, [], ['multiple' => 'true']));
}

public function testListBox()
Expand Down Expand Up @@ -644,6 +726,8 @@ public function testUl()
]));

$this->assertEquals('<ul class="test"></ul>', Html::ul([], ['class' => 'test']));

$this->assertStringMatchesFormat('<foo>%A</foo>', Html::ul([], ['tag' => 'foo']));
}

public function testOl()
Expand Down Expand Up @@ -760,6 +844,13 @@ public function testRenderAttributes()
$this->assertEquals('', Html::renderTagAttributes(['class' => []]));
$this->assertEquals(' style="width: 100px; height: 200px;"', Html::renderTagAttributes(['style' => ['width' => '100px', 'height' => '200px']]));
$this->assertEquals('', Html::renderTagAttributes(['style' => []]));

$attributes = [
'data' => [
'foo' => [],
],
];
$this->assertEquals(' data-foo=\'[]\'', Html::renderTagAttributes($attributes));
}

public function testAddCssClass()
Expand Down Expand Up @@ -1326,6 +1417,125 @@ public function testAttributeNameException($name)
$this->expectException('yii\base\InvalidParamException');
Html::getAttributeName($name);
}

public function testActiveFileInput()
{
$expected = '<input type="hidden" name="foo" value=""><input type="file" id="htmltestmodel-types" name="foo">';
$model = new HtmlTestModel();
$actual = Html::activeFileInput($model, 'types', ['name' => 'foo']);
$this->assertEqualsWithoutLE($expected, $actual);
}

/**
* @expectedException \yii\base\InvalidParamException
* @expectedExceptionMessage Attribute name must contain word characters only.
*/
public function testGetAttributeValueInvalidParamException()
{
$model = new HtmlTestModel();
Html::getAttributeValue($model, '-');
}

public function testGetAttributeValue()
{
$model = new HtmlTestModel();

$expected = null;
$actual = Html::getAttributeValue($model, 'types');
$this->assertSame($expected, $actual);

$activeRecord = $this->getMock('yii\\db\\ActiveRecordInterface');
$activeRecord->method('getPrimaryKey')->willReturn(1);
$model->types = $activeRecord;

$expected = 1;
$actual = Html::getAttributeValue($model, 'types');
$this->assertSame($expected, $actual);

$model->types = [
$activeRecord,
];

$expected = [1];
$actual = Html::getAttributeValue($model, 'types');
$this->assertSame($expected, $actual);
}

/**
* @expectedException \yii\base\InvalidParamException
* @expectedExceptionMessage Attribute name must contain word characters only.
*/
public function testGetInputNameInvalidParamExceptionAttribute()
{
$model = new HtmlTestModel();
Html::getInputName($model, '-');
}

/**
* @expectedException \yii\base\InvalidParamException
* @expectedExceptionMessageRegExp /(.*)formName\(\) cannot be empty for tabular inputs.$/
*/
public function testGetInputNameInvalidParamExceptionFormName()
{
$model = $this->getMock('yii\\base\\Model');
$model->method('formName')->willReturn('');
Html::getInputName($model, '[foo]bar');
}

public function testGetInputName()
{
$model = $this->getMock('yii\\base\\Model');
$model->method('formName')->willReturn('');
$expected = 'types';
$actual = Html::getInputName($model, 'types');
$this->assertSame($expected, $actual);
}


public function testEscapeJsRegularExpression()
{
$expected = '/[a-z0-9-]+/';
$actual = Html::escapeJsRegularExpression('([a-z0-9-]+)');
$this->assertSame($expected, $actual);

$expected = '/([a-z0-9-]+)/gim';
$actual = Html::escapeJsRegularExpression('/([a-z0-9-]+)/Ugimex');
$this->assertSame($expected, $actual);
}

public function testActiveDropDownList()
{
$expected = <<<HTML
<input type="hidden" name="HtmlTestModel[types]" value=""><select id="htmltestmodel-types" name="HtmlTestModel[types][]" multiple="true" size="4">
</select>
HTML;
$model = new HtmlTestModel();
$actual = Html::activeDropDownList($model, 'types', [], ['multiple' => 'true']);
$this->assertEqualsWithoutLE($expected, $actual);
}

public function testActiveCheckboxList()
{
$model = new HtmlTestModel();

$expected = <<<HTML
<input type="hidden" name="HtmlTestModel[types]" value=""><div id="htmltestmodel-types"><label><input type="radio" name="HtmlTestModel[types]" value="0"> foo</label></div>
HTML;
$actual = Html::activeRadioList($model, 'types', ['foo']);
$this->assertEqualsWithoutLE($expected, $actual);
}

public function testActiveRadioList()
{
$model = new HtmlTestModel();

$expected = <<<HTML
<input type="hidden" name="HtmlTestModel[types]" value=""><div id="htmltestmodel-types"><label><input type="checkbox" name="HtmlTestModel[types][]" value="0"> foo</label></div>
HTML;
$actual = Html::activeCheckboxList($model, 'types', ['foo']);
$this->assertEqualsWithoutLE($expected, $actual);
}
}

/**
Expand Down

0 comments on commit e2218cb

Please sign in to comment.