Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test coverage of yii\helpers\Html #14220

Merged
merged 1 commit into from
May 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method name SimulateViaPost doesn't match data from data provider since there are other methods as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method testBeginFormSimulateViaPost and data providerBeginFormSimulateViaPost testing conition framework/helpers/BaseHtml.php:336 neme for method i based on comment from framework/helpers/BaseHtml.php:337:

simulate PUT, DELETE, etc. via POST

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unclear what exactly needs to be corrected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method name.

{
$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