Updated unit test coverage for html helpers

- updated HtmlHelperLabelExtensionsTest
 - Added tests for HtmlHelperInputExtensions
 - Added tests for ValidationMessage and ValidationSummary extensions
This commit is contained in:
Ajay Bhargav Baaskaran 2016-01-11 16:15:19 -08:00
parent 357f4a00b7
commit 726ebb7a05
9 changed files with 982 additions and 0 deletions

View File

@ -649,6 +649,73 @@ namespace Microsoft.AspNetCore.Mvc.Rendering
Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(html));
}
[Fact]
public void Checkbox_UsesSpecifiedExpression()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var checkboxResult = helper.CheckBox("Property1");
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[checkbox]]\" value=\"HtmlEncode[[true]]\" />" +
"<input name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[hidden]]\" value=\"HtmlEncode[[false]]\" />",
HtmlContentUtilities.HtmlContentToString(checkboxResult));
}
[Fact]
public void Checkbox_UsesSpecifiedIsChecked()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var checkboxResult = helper.CheckBox("Property1", isChecked: true);
// Assert
Assert.Equal(
"<input checked=\"HtmlEncode[[checked]]\" id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[checkbox]]\" value=\"HtmlEncode[[true]]\" />" +
"<input name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[hidden]]\" value=\"HtmlEncode[[false]]\" />",
HtmlContentUtilities.HtmlContentToString(checkboxResult));
}
[Fact]
public void Checkbox_UsesSpecifiedIsCheckedRegardlessOfExpressionValue()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = true };
// Act
var checkboxResult = helper.CheckBox("Property1", isChecked: false);
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[checkbox]]\" value=\"HtmlEncode[[true]]\" />" +
"<input name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[hidden]]\" value=\"HtmlEncode[[false]]\" />",
HtmlContentUtilities.HtmlContentToString(checkboxResult));
}
[Fact]
public void Checkbox_UsesSpecifiedHtmlAttributes()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var checkboxResult = helper.CheckBox("Property1", htmlAttributes: new { attr = "value" });
// Assert
Assert.Equal(
"<input attr=\"HtmlEncode[[value]]\" id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[checkbox]]\" value=\"HtmlEncode[[true]]\" />" +
"<input name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[hidden]]\" value=\"HtmlEncode[[false]]\" />",
HtmlContentUtilities.HtmlContentToString(checkboxResult));
}
private static ViewDataDictionary<TestModel> GetTestModelViewData()
{
return new ViewDataDictionary<TestModel>(new EmptyModelMetadataProvider())

View File

@ -835,6 +835,60 @@ namespace Microsoft.AspNetCore.Mvc.Rendering
Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result));
}
[Fact]
public void Hidden_UsesSpecifiedExpression()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var hiddenResult = helper.Hidden("Property1");
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[hidden]]\" value=\"HtmlEncode[[propValue]]\" />",
HtmlContentUtilities.HtmlContentToString(hiddenResult));
}
[Fact]
public void HiddenFor_UsesSpecifiedExpression()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var hiddenForResult = helper.HiddenFor(m => m.Property1);
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[hidden]]\" value=\"HtmlEncode[[propValue]]\" />",
HtmlContentUtilities.HtmlContentToString(hiddenForResult));
}
[Fact]
public void Hidden_UsesSpecifiedValue()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var hiddenResult = helper.Hidden("Property1", value: "myvalue");
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[hidden]]\" value=\"HtmlEncode[[myvalue]]\" />",
HtmlContentUtilities.HtmlContentToString(hiddenResult));
}
private static ViewDataDictionary<HiddenModel> GetViewDataWithNullModelAndNonNullViewData()
{
return new ViewDataDictionary<HiddenModel>(new EmptyModelMetadataProvider())
@ -899,5 +953,10 @@ namespace Microsoft.AspNetCore.Mvc.Rendering
public List<string> Property6 { get; } = new List<string>();
}
private class TestModel
{
public string Property1 { get; set; }
}
}
}

View File

@ -252,6 +252,71 @@ namespace Microsoft.AspNetCore.Mvc.Core
Assert.Equal("<label for=\"HtmlEncode[[unknownKey]]\">HtmlEncode[[unknownKey]]</label>", HtmlContentUtilities.HtmlContentToString(result));
}
[Fact]
public void Label_UsesSpecifiedLabelText()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var labelResult = helper.Label("Property1", labelText: "Hello");
// Assert
Assert.Equal("<label for=\"HtmlEncode[[Property1]]\">HtmlEncode[[Hello]]</label>", HtmlContentUtilities.HtmlContentToString(labelResult));
}
[Fact]
public void LabelFor_UsesSpecifiedLabelText()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var labelForResult = helper.LabelFor(m => m.Property1, labelText: "Hello");
// Assert
Assert.Equal("<label for=\"HtmlEncode[[Property1]]\">HtmlEncode[[Hello]]</label>", HtmlContentUtilities.HtmlContentToString(labelForResult));
}
[Fact]
public void LabelForModel_UsesSpecifiedLabelText()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var labelForModelResult = helper.LabelForModel(labelText: "Hello");
// Assert
Assert.Equal("<label for=\"\">HtmlEncode[[Hello]]</label>", HtmlContentUtilities.HtmlContentToString(labelForModelResult));
}
[Fact]
public void LabelFor_DisplaysSpecifiedHtmlAttributes()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var labelForResult = helper.LabelFor(m => m.Property1, htmlAttributes: new { attr="value" });
// Assert
Assert.Equal("<label attr=\"HtmlEncode[[value]]\" for=\"HtmlEncode[[Property1]]\">HtmlEncode[[Property1]]</label>", HtmlContentUtilities.HtmlContentToString(labelForResult));
}
[Fact]
public void LabelForModel_DisplaysSpecifiedHtmlAttributes()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var labelForModelResult = helper.LabelForModel(labelText: "Hello", htmlAttributes: new { attr = "value" });
// Assert
Assert.Equal("<label attr=\"HtmlEncode[[value]]\" for=\"\">HtmlEncode[[Hello]]</label>", HtmlContentUtilities.HtmlContentToString(labelForModelResult));
}
private sealed class InnerClass
{
public int Id { get; set; }

View File

@ -345,6 +345,60 @@ namespace Microsoft.AspNetCore.Mvc.Rendering
Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result));
}
[Fact]
public void Password_UsesSpecifiedExpressionForNames_IgnoresExpressionValue()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var passwordResult = helper.Password("Property1");
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[password]]\" />",
HtmlContentUtilities.HtmlContentToString(passwordResult));
}
[Fact]
public void PasswordFor_UsesSpecifiedExpressionForNames_IgnoresExpressionValue()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var passwordForResult = helper.PasswordFor(m => m.Property1);
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[password]]\" />",
HtmlContentUtilities.HtmlContentToString(passwordForResult));
}
[Fact]
public void Password_UsesSpecifiedValue()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var passwordResult = helper.Password("Property1", value: "myvalue");
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[password]]\" value=\"HtmlEncode[[myvalue]]\" />",
HtmlContentUtilities.HtmlContentToString(passwordResult));
}
private static ViewDataDictionary<PasswordModel> GetViewDataWithNullModelAndNonEmptyViewData()
{
return new ViewDataDictionary<PasswordModel>(new EmptyModelMetadataProvider())
@ -397,5 +451,10 @@ namespace Microsoft.AspNetCore.Mvc.Rendering
public List<string> Property6 { get; } = new List<string>();
}
private class TestModel
{
public string Property1 { get; set; }
}
}
}

View File

@ -0,0 +1,157 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TestCommon;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Core
{
/// <summary>
/// Test the RadioButton extensions in <see cref="HtmlHelperInputExtensions" /> class.
/// </summary>
public class HtmlHelperRadioButtonExtensionsTest
{
[Fact]
public void RadioButton_UsesSpecifiedExpression()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var radioButtonResult = helper.RadioButton("Property1", value: "myvalue");
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[radio]]\" value=\"HtmlEncode[[myvalue]]\" />",
HtmlContentUtilities.HtmlContentToString(radioButtonResult));
}
[Fact]
public void RadioButtonFor_UsesSpecifiedExpression()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var radioButtonForResult = helper.RadioButtonFor(m => m.Property1, value: "myvalue");
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[radio]]\" value=\"HtmlEncode[[myvalue]]\" />",
HtmlContentUtilities.HtmlContentToString(radioButtonForResult));
}
[Theory]
[InlineData("MyValue")]
[InlineData("myvalue")]
public void RadioButton_CheckedWhenValueMatchesSpecifiedExpression(string value)
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = value };
// Act
var radioButtonResult = helper.RadioButton("Property1", value: "myvalue");
// Assert
Assert.Equal(
"<input checked=\"HtmlEncode[[checked]]\" id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[radio]]\" value=\"HtmlEncode[[myvalue]]\" />",
HtmlContentUtilities.HtmlContentToString(radioButtonResult));
}
[Theory]
[InlineData("MyValue")]
[InlineData("myvalue")]
public void RadioButtonFor_CheckedWhenValueMatchesSpecifiedExpression(string value)
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = value };
// Act
var radioButtonForResult = helper.RadioButtonFor(m => m.Property1, value: "myvalue");
// Assert
Assert.Equal(
"<input checked=\"HtmlEncode[[checked]]\" id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[radio]]\" value=\"HtmlEncode[[myvalue]]\" />",
HtmlContentUtilities.HtmlContentToString(radioButtonForResult));
}
[Fact]
public void RadioButtonHelpers_UsesSpecifiedIsChecked()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var radioButtonResult = helper.RadioButton("Property1", value: "myvalue", isChecked: true);
// Assert
Assert.Equal(
"<input checked=\"HtmlEncode[[checked]]\" id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[radio]]\" value=\"HtmlEncode[[myvalue]]\" />",
HtmlContentUtilities.HtmlContentToString(radioButtonResult));
}
[Fact]
public void RadioButtonHelpers_UsesSpecifiedIsCheckedRegardlessOfValue()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property2 = true };
// Act
var radioButtonResult = helper.RadioButton("Property2", value: "myvalue", isChecked: false);
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property2]]\" name=\"HtmlEncode[[Property2]]\" type=\"HtmlEncode[[radio]]\" value=\"HtmlEncode[[myvalue]]\" />",
HtmlContentUtilities.HtmlContentToString(radioButtonResult));
}
[Fact]
public void RadioButton_UsesSpecifiedHtmlAttributes()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var radioButtonResult = helper.RadioButton("Property1", value: "myvalue", htmlAttributes: new { attr = "value" });
// Assert
Assert.Equal(
"<input attr=\"HtmlEncode[[value]]\" id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[radio]]\" value=\"HtmlEncode[[myvalue]]\" />",
HtmlContentUtilities.HtmlContentToString(radioButtonResult));
}
[Fact]
public void RadioButtonFor_UsesSpecifiedHtmlAttributes()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var radioButtonForResult = helper.RadioButtonFor(m => m.Property1, value: "myvalue", htmlAttributes: new { attr = "value" });
// Assert
Assert.Equal(
"<input attr=\"HtmlEncode[[value]]\" id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[radio]]\" value=\"HtmlEncode[[myvalue]]\" />",
HtmlContentUtilities.HtmlContentToString(radioButtonForResult));
}
private class TestModel
{
public string Property1 { get; set; }
public bool Property2 { get; set; }
}
}
}

View File

@ -0,0 +1,142 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TestCommon;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Core
{
/// <summary>
/// Test the TextArea extensions in <see cref="HtmlHelperInputExtensions" /> class.
/// </summary>
public class HtmlHelperTextAreaExtensionsTest
{
[Fact]
public void TextArea_UsesSpecifiedExpression()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var textAreaResult = helper.TextArea("Property1");
// Assert
Assert.Equal(
"<textarea id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\">\r\nHtmlEncode[[propValue]]</textarea>",
HtmlContentUtilities.HtmlContentToString(textAreaResult));
}
[Fact]
public void TextAreaFor_UsesSpecifiedExpression()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var textAreaForResult = helper.TextAreaFor(m => m.Property1);
// Assert
Assert.Equal(
"<textarea id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\">\r\nHtmlEncode[[propValue]]</textarea>",
HtmlContentUtilities.HtmlContentToString(textAreaForResult));
}
[Fact]
public void TextAreaHelpers_UsesSpecifiedValue()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var textAreaResult = helper.TextArea("Property1", value: "myvalue");
// Assert
Assert.Equal(
"<textarea id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\">\r\nHtmlEncode[[myvalue]]</textarea>",
HtmlContentUtilities.HtmlContentToString(textAreaResult));
}
[Fact]
public void TextArea_UsesSpecifiedHtmlAttributes()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var textAreaResult = helper.TextArea("Property1", htmlAttributes: new { attr = "value" });
// Assert
Assert.Equal(
"<textarea attr=\"HtmlEncode[[value]]\" id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\">\r\nHtmlEncode[[propValue]]</textarea>",
HtmlContentUtilities.HtmlContentToString(textAreaResult));
}
[Fact]
public void TextAreaFor_UsesSpecifiedHtmlAttributes()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var textAreaForResult = helper.TextAreaFor(m => m.Property1, htmlAttributes: new { attr = "value" });
// Assert
Assert.Equal(
"<textarea attr=\"HtmlEncode[[value]]\" id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\">\r\nHtmlEncode[[propValue]]</textarea>",
HtmlContentUtilities.HtmlContentToString(textAreaForResult));
}
[Fact]
public void TextArea_UsesSpecifiedRowsAndColumns()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var textAreaResult = helper.TextArea("Property1", value: "myvalue", rows: 1, columns: 2, htmlAttributes: new { attr = "value" });
// Assert
Assert.Equal(
"<textarea attr=\"HtmlEncode[[value]]\" columns=\"HtmlEncode[[2]]\" id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" rows=\"HtmlEncode[[1]]\">\r\nHtmlEncode[[myvalue]]</textarea>",
HtmlContentUtilities.HtmlContentToString(textAreaResult));
}
[Fact]
public void TextAreaFor_UsesSpecifiedRowsAndColumns()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var textAreaForResult = helper.TextAreaFor(m => m.Property1, rows: 1, columns: 2, htmlAttributes: new { attr = "value" });
// Assert
Assert.Equal(
"<textarea attr=\"HtmlEncode[[value]]\" columns=\"HtmlEncode[[2]]\" id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" rows=\"HtmlEncode[[1]]\">\r\n</textarea>",
HtmlContentUtilities.HtmlContentToString(textAreaForResult));
}
private class TestModel
{
public string Property1 { get; set; }
}
}
}

View File

@ -0,0 +1,148 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TestCommon;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Core
{
/// <summary>
/// Test the TextBox extensions in <see cref="HtmlHelperInputExtensions" /> class.
/// </summary>
public class HtmlHelperTextBoxExtensionsTest
{
[Fact]
public void TextBox_UsesSpecifiedExpression()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var textBoxResult = helper.TextBox("Property1");
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[text]]\" value=\"HtmlEncode[[propValue]]\" />",
HtmlContentUtilities.HtmlContentToString(textBoxResult));
}
[Fact]
public void TextBoxFor_UsesSpecifiedExpression()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var textBoxForResult = helper.TextBoxFor(m => m.Property1);
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[text]]\" value=\"HtmlEncode[[propValue]]\" />",
HtmlContentUtilities.HtmlContentToString(textBoxForResult));
}
[Fact]
public void TextBox_UsesSpecifiedValue()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var textBoxResult = helper.TextBox("Property1", value: "myvalue");
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[text]]\" value=\"HtmlEncode[[myvalue]]\" />",
HtmlContentUtilities.HtmlContentToString(textBoxResult));
}
[Fact]
public void TextBox_UsesSpecifiedFormat()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var textBoxResult = helper.TextBox("Property1", value: null, format: "prefix: {0}");
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[text]]\" value=\"HtmlEncode[[prefix: propValue]]\" />",
HtmlContentUtilities.HtmlContentToString(textBoxResult));
}
[Fact]
public void TextBox_UsesSpecifiedFormatOverridesPropertyValue()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var textBoxResult = helper.TextBox("Property1", value: "myvalue", format: "prefix: {0}");
// Assert
Assert.Equal(
"<input id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[text]]\" value=\"HtmlEncode[[prefix: myvalue]]\" />",
HtmlContentUtilities.HtmlContentToString(textBoxResult));
}
[Fact]
public void TextBox_UsesSpecifiedHtmlAttributes()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var textBoxResult = helper.TextBox("Property1", value: "myvalue", htmlAttributes: new { attr = "value" });
// Assert
Assert.Equal(
"<input attr=\"HtmlEncode[[value]]\" id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[text]]\" value=\"HtmlEncode[[myvalue]]\" />",
HtmlContentUtilities.HtmlContentToString(textBoxResult));
}
[Fact]
public void TextBoxFor_UsesSpecifiedHtmlAttributes()
{
// Arrange
var metadataProvider = new EmptyModelMetadataProvider();
var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary<TestModel>(metadataProvider));
helper.ViewContext.ClientValidationEnabled = false;
helper.ViewData.Model = new TestModel { Property1 = "propValue" };
// Act
var textBoxForResult = helper.TextBoxFor(m => m.Property1, htmlAttributes: new { attr = "value" });
// Assert
Assert.Equal(
"<input attr=\"HtmlEncode[[value]]\" id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[text]]\" value=\"HtmlEncode[[propValue]]\" />",
HtmlContentUtilities.HtmlContentToString(textBoxForResult));
}
private class TestModel
{
public string Property1 { get; set; }
}
}
}

View File

@ -0,0 +1,135 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TestCommon;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Core
{
/// <summary>
/// Test the ValidationMessage extensions in <see cref="HtmlHelperValidationExtensions" /> class.
/// </summary>
public class HtmlHelperValidationMessageExtensionsTest
{
[Fact]
public void ValidationMessage_UsesSpecifiedExpression()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var validationMessageResult = helper.ValidationMessage("Property1");
// Assert
Assert.Equal(
"<span class=\"HtmlEncode[[field-validation-valid]]\" data-valmsg-for=\"HtmlEncode[[Property1]]\" data-valmsg-replace=\"HtmlEncode[[true]]\"></span>",
HtmlContentUtilities.HtmlContentToString(validationMessageResult));
}
[Fact]
public void ValidationMessageFor_UsesSpecifiedExpression()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var validationMessageForResult = helper.ValidationMessageFor(m => m.Property1);
// Assert
Assert.Equal(
"<span class=\"HtmlEncode[[field-validation-valid]]\" data-valmsg-for=\"HtmlEncode[[Property1]]\" data-valmsg-replace=\"HtmlEncode[[true]]\"></span>",
HtmlContentUtilities.HtmlContentToString(validationMessageForResult));
}
[Fact]
public void ValidationMessage_UsesSpecifiedMessage()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var validationMessageResult = helper.ValidationMessage("Property1", message: "Custom Message");
// Assert
Assert.Equal(
"<span class=\"HtmlEncode[[field-validation-valid]]\" data-valmsg-for=\"HtmlEncode[[Property1]]\" data-valmsg-replace=\"HtmlEncode[[false]]\">HtmlEncode[[Custom Message]]</span>",
HtmlContentUtilities.HtmlContentToString(validationMessageResult));
}
[Fact]
public void ValidationMessageFor_UsesSpecifiedMessage()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var validationMessageForResult = helper.ValidationMessageFor(m => m.Property1, message: "Custom Message");
// Assert
Assert.Equal(
"<span class=\"HtmlEncode[[field-validation-valid]]\" data-valmsg-for=\"HtmlEncode[[Property1]]\" data-valmsg-replace=\"HtmlEncode[[false]]\">HtmlEncode[[Custom Message]]</span>",
HtmlContentUtilities.HtmlContentToString(validationMessageForResult));
}
[Fact]
public void ValidationMessage_UsesSpecifiedHtmlAttributes()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var validationMessageResult = helper.ValidationMessage("Property1", message: "Custom Message", htmlAttributes: new { attr="value" });
// Assert
Assert.Equal(
"<span attr=\"HtmlEncode[[value]]\" class=\"HtmlEncode[[field-validation-valid]]\" data-valmsg-for=\"HtmlEncode[[Property1]]\" data-valmsg-replace=\"HtmlEncode[[false]]\">HtmlEncode[[Custom Message]]</span>",
HtmlContentUtilities.HtmlContentToString(validationMessageResult));
}
[Fact]
public void ValidationMessageFor_UsesSpecifiedHtmlAttributes()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var validationMessageForResult = helper.ValidationMessageFor(m => m.Property1, message: "Custom Message", htmlAttributes: new { attr = "value" });
// Assert
Assert.Equal(
"<span attr=\"HtmlEncode[[value]]\" class=\"HtmlEncode[[field-validation-valid]]\" data-valmsg-for=\"HtmlEncode[[Property1]]\" data-valmsg-replace=\"HtmlEncode[[false]]\">HtmlEncode[[Custom Message]]</span>",
HtmlContentUtilities.HtmlContentToString(validationMessageForResult));
}
[Fact]
public void ValidationMessage_UsesSpecifiedTag()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var validationMessageResult = helper.ValidationMessage("Property1", message: "Custom Message", tag: "div");
// Assert
Assert.Equal(
"<div class=\"HtmlEncode[[field-validation-valid]]\" data-valmsg-for=\"HtmlEncode[[Property1]]\" data-valmsg-replace=\"HtmlEncode[[false]]\">HtmlEncode[[Custom Message]]</div>",
HtmlContentUtilities.HtmlContentToString(validationMessageResult));
}
[Fact]
public void ValidationMessageFor_UsesSpecifiedTag()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
// Act
var validationMessageForResult = helper.ValidationMessageFor(m => m.Property1, message: "Custom Message", tag: "div");
// Assert
Assert.Equal(
"<div class=\"HtmlEncode[[field-validation-valid]]\" data-valmsg-for=\"HtmlEncode[[Property1]]\" data-valmsg-replace=\"HtmlEncode[[false]]\">HtmlEncode[[Custom Message]]</div>",
HtmlContentUtilities.HtmlContentToString(validationMessageForResult));
}
}
}

View File

@ -335,6 +335,156 @@ namespace Microsoft.AspNetCore.Mvc.Rendering
Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result));
}
[Fact]
public void ValidationSummary_UsesValuesFromModelState()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
helper.ViewData.ModelState.AddModelError("Property1", "Error for Property1");
// Act
var validationSummaryResult = helper.ValidationSummary();
// Assert
Assert.Equal(
"<div class=\"HtmlEncode[[validation-summary-errors]]\" data-valmsg-summary=\"HtmlEncode[[true]]\">" +
"<ul><li>HtmlEncode[[Error for Property1]]</li>\r\n</ul></div>",
HtmlContentUtilities.HtmlContentToString(validationSummaryResult));
}
[Fact]
public void ValidationSummary_ExcludesPropertyErrors()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
helper.ViewData.ModelState.AddModelError("Property1", "Error for Property1");
// Act
var validationSummaryResult = helper.ValidationSummary(excludePropertyErrors: true);
// Assert
Assert.Equal(
"<div class=\"HtmlEncode[[validation-summary-errors]]\"><ul><li style=\"display:none\"></li>\r\n</ul></div>",
HtmlContentUtilities.HtmlContentToString(validationSummaryResult));
}
[Fact]
public void ValidationSummary_UsesSpecifiedMessage()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
helper.ViewData.ModelState.AddModelError("Property1", "Error for Property1");
// Act
var validationSummaryResult = helper.ValidationSummary(message: "Custom Message");
// Assert
Assert.Equal(
"<div class=\"HtmlEncode[[validation-summary-errors]]\" data-valmsg-summary=\"HtmlEncode[[true]]\">" +
"<span>HtmlEncode[[Custom Message]]</span>\r\n<ul><li>HtmlEncode[[Error for Property1]]</li>\r\n</ul></div>",
HtmlContentUtilities.HtmlContentToString(validationSummaryResult));
}
[Fact]
public void ValidationSummary_UsesSpecifiedTag()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
helper.ViewData.ModelState.AddModelError("Property1", "Error for Property1");
// Act
var validationSummaryResult = helper.ValidationSummary(message: "Custom Message", tag: "div");
// Assert
Assert.Equal(
"<div class=\"HtmlEncode[[validation-summary-errors]]\" data-valmsg-summary=\"HtmlEncode[[true]]\">" +
"<div>HtmlEncode[[Custom Message]]</div>\r\n<ul><li>HtmlEncode[[Error for Property1]]</li>\r\n</ul></div>",
HtmlContentUtilities.HtmlContentToString(validationSummaryResult));
}
[Fact]
public void ValidationSummary_UsesSpecifiedMessageAndExcludesPropertyErrors()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
helper.ViewData.ModelState.AddModelError("Property1", "Error for Property1");
// Act
var validationSummaryResult = helper.ValidationSummary(excludePropertyErrors: true, message: "Custom Message");
// Assert
Assert.Equal(
"<div class=\"HtmlEncode[[validation-summary-errors]]\"><span>HtmlEncode[[Custom Message]]</span>\r\n<ul><li style=\"display:none\"></li>\r\n</ul></div>",
HtmlContentUtilities.HtmlContentToString(validationSummaryResult));
}
[Fact]
public void ValidationSummary_UsesSpecifiedHtmlAttributes()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
helper.ViewData.ModelState.AddModelError("Property1", "Error for Property1");
// Act
var validationSummaryResult = helper.ValidationSummary(message: "Custom Message", htmlAttributes: new { attr="value" });
// Assert
Assert.Equal(
"<div attr=\"HtmlEncode[[value]]\" class=\"HtmlEncode[[validation-summary-errors]]\" data-valmsg-summary=\"HtmlEncode[[true]]\">" +
"<span>HtmlEncode[[Custom Message]]</span>\r\n<ul><li>HtmlEncode[[Error for Property1]]</li>\r\n</ul></div>",
HtmlContentUtilities.HtmlContentToString(validationSummaryResult));
}
[Fact]
public void ValidationSummary_UsesSpecifiedHtmlAttributesAndTag()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
helper.ViewData.ModelState.AddModelError("Property1", "Error for Property1");
// Act
var validationSummaryResult = helper.ValidationSummary(message: "Custom Message", htmlAttributes: new { attr = "value" }, tag: "div");
// Assert
Assert.Equal(
"<div attr=\"HtmlEncode[[value]]\" class=\"HtmlEncode[[validation-summary-errors]]\" data-valmsg-summary=\"HtmlEncode[[true]]\">" +
"<div>HtmlEncode[[Custom Message]]</div>\r\n<ul><li>HtmlEncode[[Error for Property1]]</li>\r\n</ul></div>",
HtmlContentUtilities.HtmlContentToString(validationSummaryResult));
}
[Fact]
public void ValidationSummary_UsesSpecifiedUsesSpecifiedTagAndExcludesPropertyErrors()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
helper.ViewData.ModelState.AddModelError("Property1", "Error for Property1");
// Act
var validationSummaryResult = helper.ValidationSummary(excludePropertyErrors: true, message: "Custom Message", tag: "div");
// Assert
Assert.Equal(
"<div class=\"HtmlEncode[[validation-summary-errors]]\"><div>HtmlEncode[[Custom Message]]</div>\r\n<ul><li style=\"display:none\"></li>\r\n</ul></div>",
HtmlContentUtilities.HtmlContentToString(validationSummaryResult));
}
[Fact]
public void ValidationSummary_UsesSpecifiedUsesSpecifiedHtmlAttributesAndExcludesPropertyErrors()
{
// Arrange
var helper = DefaultTemplatesUtilities.GetHtmlHelper();
helper.ViewData.ModelState.AddModelError("Property1", "Error for Property1");
// Act
var validationSummaryResult = helper.ValidationSummary(excludePropertyErrors: true, message: "Custom Message", htmlAttributes: new { attr = "value" });
// Assert
Assert.Equal(
"<div attr=\"HtmlEncode[[value]]\" class=\"HtmlEncode[[validation-summary-errors]]\"><span>HtmlEncode[[Custom Message]]</span>\r\n" +
"<ul><li style=\"display:none\"></li>\r\n</ul></div>",
HtmlContentUtilities.HtmlContentToString(validationSummaryResult));
}
// Adds errors for various parts of the model, including the root.
private void AddMultipleErrors(ModelStateDictionary modelState)
{