diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperCheckboxTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperCheckboxTest.cs index 2ba4eea499..71a527ec72 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperCheckboxTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperCheckboxTest.cs @@ -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( + "" + + "", + HtmlContentUtilities.HtmlContentToString(checkboxResult)); + } + + [Fact] + public void Checkbox_UsesSpecifiedIsChecked() + { + // Arrange + var helper = DefaultTemplatesUtilities.GetHtmlHelper(); + + // Act + var checkboxResult = helper.CheckBox("Property1", isChecked: true); + + // Assert + Assert.Equal( + "" + + "", + HtmlContentUtilities.HtmlContentToString(checkboxResult)); + } + + [Fact] + public void Checkbox_UsesSpecifiedIsCheckedRegardlessOfExpressionValue() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property1 = true }; + + // Act + var checkboxResult = helper.CheckBox("Property1", isChecked: false); + + // Assert + Assert.Equal( + "" + + "", + 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( + "" + + "", + HtmlContentUtilities.HtmlContentToString(checkboxResult)); + } + private static ViewDataDictionary GetTestModelViewData() { return new ViewDataDictionary(new EmptyModelMetadataProvider()) diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperHiddenTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperHiddenTest.cs index 9ecb2472b9..51312a93ef 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperHiddenTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperHiddenTest.cs @@ -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(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property1 = "propValue" }; + + // Act + var hiddenResult = helper.Hidden("Property1"); + + // Assert + Assert.Equal( + "", + HtmlContentUtilities.HtmlContentToString(hiddenResult)); + } + + [Fact] + public void HiddenFor_UsesSpecifiedExpression() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property1 = "propValue" }; + + // Act + var hiddenForResult = helper.HiddenFor(m => m.Property1); + + // Assert + Assert.Equal( + "", + HtmlContentUtilities.HtmlContentToString(hiddenForResult)); + } + + [Fact] + public void Hidden_UsesSpecifiedValue() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property1 = "propValue" }; + + // Act + var hiddenResult = helper.Hidden("Property1", value: "myvalue"); + + // Assert + Assert.Equal( + "", + HtmlContentUtilities.HtmlContentToString(hiddenResult)); + } + private static ViewDataDictionary GetViewDataWithNullModelAndNonNullViewData() { return new ViewDataDictionary(new EmptyModelMetadataProvider()) @@ -899,5 +953,10 @@ namespace Microsoft.AspNetCore.Mvc.Rendering public List Property6 { get; } = new List(); } + + private class TestModel + { + public string Property1 { get; set; } + } } } diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperLabelExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperLabelExtensionsTest.cs index 6e73ba0d55..f4177af1d7 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperLabelExtensionsTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperLabelExtensionsTest.cs @@ -252,6 +252,71 @@ namespace Microsoft.AspNetCore.Mvc.Core Assert.Equal("", HtmlContentUtilities.HtmlContentToString(result)); } + [Fact] + public void Label_UsesSpecifiedLabelText() + { + // Arrange + var helper = DefaultTemplatesUtilities.GetHtmlHelper(); + + // Act + var labelResult = helper.Label("Property1", labelText: "Hello"); + + // Assert + Assert.Equal("", 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("", HtmlContentUtilities.HtmlContentToString(labelForResult)); + } + + [Fact] + public void LabelForModel_UsesSpecifiedLabelText() + { + // Arrange + var helper = DefaultTemplatesUtilities.GetHtmlHelper(); + + // Act + var labelForModelResult = helper.LabelForModel(labelText: "Hello"); + + // Assert + Assert.Equal("", 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("", 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("", HtmlContentUtilities.HtmlContentToString(labelForModelResult)); + } + private sealed class InnerClass { public int Id { get; set; } diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperPasswordTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperPasswordTest.cs index 76cc0acc5c..1bb0db369a 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperPasswordTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperPasswordTest.cs @@ -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(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property1 = "propValue" }; + + // Act + var passwordResult = helper.Password("Property1"); + + // Assert + Assert.Equal( + "", + HtmlContentUtilities.HtmlContentToString(passwordResult)); + } + + [Fact] + public void PasswordFor_UsesSpecifiedExpressionForNames_IgnoresExpressionValue() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property1 = "propValue" }; + + // Act + var passwordForResult = helper.PasswordFor(m => m.Property1); + + // Assert + Assert.Equal( + "", + HtmlContentUtilities.HtmlContentToString(passwordForResult)); + } + + [Fact] + public void Password_UsesSpecifiedValue() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property1 = "propValue" }; + + // Act + var passwordResult = helper.Password("Property1", value: "myvalue"); + + // Assert + Assert.Equal( + "", + HtmlContentUtilities.HtmlContentToString(passwordResult)); + } + private static ViewDataDictionary GetViewDataWithNullModelAndNonEmptyViewData() { return new ViewDataDictionary(new EmptyModelMetadataProvider()) @@ -397,5 +451,10 @@ namespace Microsoft.AspNetCore.Mvc.Rendering public List Property6 { get; } = new List(); } + + private class TestModel + { + public string Property1 { get; set; } + } } } diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperRadioButtonExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperRadioButtonExtensionsTest.cs new file mode 100644 index 0000000000..911845668a --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperRadioButtonExtensionsTest.cs @@ -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 +{ + /// + /// Test the RadioButton extensions in class. + /// + public class HtmlHelperRadioButtonExtensionsTest + { + [Fact] + public void RadioButton_UsesSpecifiedExpression() + { + // Arrange + var helper = DefaultTemplatesUtilities.GetHtmlHelper(); + + // Act + var radioButtonResult = helper.RadioButton("Property1", value: "myvalue"); + + // Assert + Assert.Equal( + "", + 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( + "", + 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(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property1 = value }; + + // Act + var radioButtonResult = helper.RadioButton("Property1", value: "myvalue"); + + // Assert + Assert.Equal( + "", + 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(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( + "", + 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( + "", + HtmlContentUtilities.HtmlContentToString(radioButtonResult)); + } + + [Fact] + public void RadioButtonHelpers_UsesSpecifiedIsCheckedRegardlessOfValue() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(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( + "", + 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( + "", + 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( + "", + HtmlContentUtilities.HtmlContentToString(radioButtonForResult)); + } + + private class TestModel + { + public string Property1 { get; set; } + + public bool Property2 { get; set; } + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperTextAreaExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperTextAreaExtensionsTest.cs new file mode 100644 index 0000000000..1a9aef4391 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperTextAreaExtensionsTest.cs @@ -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 +{ + /// + /// Test the TextArea extensions in class. + /// + public class HtmlHelperTextAreaExtensionsTest + { + [Fact] + public void TextArea_UsesSpecifiedExpression() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property1 = "propValue" }; + + // Act + var textAreaResult = helper.TextArea("Property1"); + + // Assert + Assert.Equal( + "", + HtmlContentUtilities.HtmlContentToString(textAreaResult)); + } + + [Fact] + public void TextAreaFor_UsesSpecifiedExpression() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property1 = "propValue" }; + + // Act + var textAreaForResult = helper.TextAreaFor(m => m.Property1); + + // Assert + Assert.Equal( + "", + HtmlContentUtilities.HtmlContentToString(textAreaForResult)); + } + + [Fact] + public void TextAreaHelpers_UsesSpecifiedValue() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property1 = "propValue" }; + + // Act + var textAreaResult = helper.TextArea("Property1", value: "myvalue"); + + // Assert + Assert.Equal( + "", + HtmlContentUtilities.HtmlContentToString(textAreaResult)); + } + + [Fact] + public void TextArea_UsesSpecifiedHtmlAttributes() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(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( + "", + HtmlContentUtilities.HtmlContentToString(textAreaResult)); + } + + [Fact] + public void TextAreaFor_UsesSpecifiedHtmlAttributes() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(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( + "", + 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( + "", + 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( + "", + HtmlContentUtilities.HtmlContentToString(textAreaForResult)); + } + + private class TestModel + { + public string Property1 { get; set; } + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperTextBoxExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperTextBoxExtensionsTest.cs new file mode 100644 index 0000000000..9a21f91fb0 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperTextBoxExtensionsTest.cs @@ -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 +{ + /// + /// Test the TextBox extensions in class. + /// + public class HtmlHelperTextBoxExtensionsTest + { + [Fact] + public void TextBox_UsesSpecifiedExpression() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property1 = "propValue" }; + + // Act + var textBoxResult = helper.TextBox("Property1"); + + // Assert + Assert.Equal( + "", + HtmlContentUtilities.HtmlContentToString(textBoxResult)); + } + + [Fact] + public void TextBoxFor_UsesSpecifiedExpression() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property1 = "propValue" }; + + // Act + var textBoxForResult = helper.TextBoxFor(m => m.Property1); + + // Assert + Assert.Equal( + "", + HtmlContentUtilities.HtmlContentToString(textBoxForResult)); + } + + [Fact] + public void TextBox_UsesSpecifiedValue() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); + helper.ViewContext.ClientValidationEnabled = false; + helper.ViewData.Model = new TestModel { Property1 = "propValue" }; + + // Act + var textBoxResult = helper.TextBox("Property1", value: "myvalue"); + + // Assert + Assert.Equal( + "", + HtmlContentUtilities.HtmlContentToString(textBoxResult)); + } + + [Fact] + public void TextBox_UsesSpecifiedFormat() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(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( + "", + HtmlContentUtilities.HtmlContentToString(textBoxResult)); + } + + [Fact] + public void TextBox_UsesSpecifiedFormatOverridesPropertyValue() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(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( + "", + HtmlContentUtilities.HtmlContentToString(textBoxResult)); + } + + [Fact] + public void TextBox_UsesSpecifiedHtmlAttributes() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(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( + "", + HtmlContentUtilities.HtmlContentToString(textBoxResult)); + } + + [Fact] + public void TextBoxFor_UsesSpecifiedHtmlAttributes() + { + // Arrange + var metadataProvider = new EmptyModelMetadataProvider(); + var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(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( + "", + HtmlContentUtilities.HtmlContentToString(textBoxForResult)); + } + + private class TestModel + { + public string Property1 { get; set; } + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperValidationMessageExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperValidationMessageExtensionsTest.cs new file mode 100644 index 0000000000..b9e1f5fc4f --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperValidationMessageExtensionsTest.cs @@ -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 +{ + /// + /// Test the ValidationMessage extensions in class. + /// + public class HtmlHelperValidationMessageExtensionsTest + { + [Fact] + public void ValidationMessage_UsesSpecifiedExpression() + { + // Arrange + var helper = DefaultTemplatesUtilities.GetHtmlHelper(); + + // Act + var validationMessageResult = helper.ValidationMessage("Property1"); + + // Assert + Assert.Equal( + "", + HtmlContentUtilities.HtmlContentToString(validationMessageResult)); + } + + [Fact] + public void ValidationMessageFor_UsesSpecifiedExpression() + { + // Arrange + var helper = DefaultTemplatesUtilities.GetHtmlHelper(); + + // Act + var validationMessageForResult = helper.ValidationMessageFor(m => m.Property1); + + // Assert + Assert.Equal( + "", + 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( + "HtmlEncode[[Custom Message]]", + 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( + "HtmlEncode[[Custom Message]]", + 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( + "HtmlEncode[[Custom Message]]", + 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( + "HtmlEncode[[Custom Message]]", + 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( + "
HtmlEncode[[Custom Message]]
", + 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( + "
HtmlEncode[[Custom Message]]
", + HtmlContentUtilities.HtmlContentToString(validationMessageForResult)); + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperValidationSummaryTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperValidationSummaryTest.cs index 2d4377c5de..d29a01891a 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperValidationSummaryTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperValidationSummaryTest.cs @@ -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( + "
" + + "
  • HtmlEncode[[Error for Property1]]
  • \r\n
", + 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( + "
  • \r\n
", + 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( + "
" + + "HtmlEncode[[Custom Message]]\r\n
  • HtmlEncode[[Error for Property1]]
  • \r\n
", + 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( + "
" + + "
HtmlEncode[[Custom Message]]
\r\n
  • HtmlEncode[[Error for Property1]]
  • \r\n
", + 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( + "
HtmlEncode[[Custom Message]]\r\n
  • \r\n
", + 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( + "
" + + "HtmlEncode[[Custom Message]]\r\n
  • HtmlEncode[[Error for Property1]]
  • \r\n
", + 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( + "
" + + "
HtmlEncode[[Custom Message]]
\r\n
  • HtmlEncode[[Error for Property1]]
  • \r\n
", + 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( + "
HtmlEncode[[Custom Message]]
\r\n
  • \r\n
", + 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( + "
HtmlEncode[[Custom Message]]\r\n" + + "
  • \r\n
", + HtmlContentUtilities.HtmlContentToString(validationSummaryResult)); + } + // Adds errors for various parts of the model, including the root. private void AddMultipleErrors(ModelStateDictionary modelState) {