// 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 System.Web.Mvc.Test; using System.Web.Routing; using System.Web.WebPages.Scope; using Microsoft.TestCommon; using Microsoft.Web.UnitTestUtil; namespace System.Web.Mvc.Html.Test { [Xunit.Collection("Uses ScopeStorage or ViewEngines.Engines")] public class TextAreaExtensionsTest : IDisposable { private static readonly RouteValueDictionary _textAreaAttributesDictionary = new RouteValueDictionary(new { rows = "15", cols = "12" }); private static readonly object _textAreaAttributesObjectDictionary = new { rows = "15", cols = "12" }; private static readonly object _textAreaAttributesObjectUnderscoresDictionary = new { rows = "15", cols = "12", foo_bar = "baz" }; private class TextAreaModel { public string foo { get; set; } public string bar { get; set; } } private static ViewDataDictionary GetTextAreaViewData() { ViewDataDictionary viewData = new ViewDataDictionary { { "foo", "ViewDataFoo" } }; viewData.Model = new TextAreaModel { foo = "ViewItemFoo", bar = "ViewItemBar" }; return viewData; } private static ViewDataDictionary GetTextAreaViewDataWithErrors() { ViewDataDictionary viewData = new ViewDataDictionary { { "foo", "ViewDataFoo" } }; viewData.Model = new TextAreaModel { foo = "ViewItemFoo", bar = "ViewItemBar" }; ModelState modelStateFoo = new ModelState(); modelStateFoo.Errors.Add(new ModelError("foo error 1")); modelStateFoo.Errors.Add(new ModelError("foo error 2")); viewData.ModelState["foo"] = modelStateFoo; modelStateFoo.Value = HtmlHelperTest.GetValueProviderResult(new string[] { "AttemptedValueFoo" }, "AttemptedValueFoo"); return viewData; } // TextArea [Fact] public void TextAreaParameterDictionaryMerging() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); // Act MvcHtmlString html = helper.TextArea("foo", new { rows = "30" }); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaParameterDictionaryMerging_Unobtrusive() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); helper.ViewContext.ClientValidationEnabled = true; helper.ViewContext.UnobtrusiveJavaScriptEnabled = true; helper.ViewContext.FormContext = new FormContext(); helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } }; // Act MvcHtmlString html = helper.TextArea("foo", new { rows = "30" }); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaParameterDictionaryMergingExplicitParameters() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); // Act MvcHtmlString html = helper.TextArea("foo", "bar", 10, 25, new { rows = "30" }); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaParameterDictionaryMergingExplicitParametersWithUnderscores() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); // Act MvcHtmlString html = helper.TextArea("foo", "bar", 10, 25, new { rows = "30", foo_bar = "baz" }); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaWithEmptyNameThrows() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); // Act & Assert Assert.ThrowsArgumentNullOrEmpty( delegate { helper.TextArea(String.Empty); }, "name"); } [Fact] public void TextAreaWithOutOfRangeColsThrows() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); // Act & Assert Assert.ThrowsArgumentOutOfRange( delegate { helper.TextArea("Foo", null /* value */, 0, -1, null /* htmlAttributes */); }, "columns", "The value must be greater than or equal to zero."); } [Fact] public void TextAreaWithOutOfRangeRowsThrows() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); // Act & Assert Assert.ThrowsArgumentOutOfRange( delegate { helper.TextArea("Foo", null /* value */, -1, 0, null /* htmlAttributes */); }, "rows", "The value must be greater than or equal to zero."); } [Theory] [PropertyData("AttributeEncodedData", PropertyType = typeof(EncodedDataSets))] public void TextArea_AttributeEncodes_Name(string text, bool htmlEncode, string encodedText) { // Arrange var viewData = new ViewDataDictionary(model: null); viewData.ModelMetadata.HtmlEncode = htmlEncode; var helper = MvcHelper.GetHtmlHelper(viewData); // Act // htmlAttributes included only to avoid special-cased renaming done for id attribute. var result = helper.TextArea(name: text, htmlAttributes: new { id = "id", }).ToHtmlString(); // Assert Assert.Equal( "", result); } [Theory] [PropertyData("AttributeEncodedData", PropertyType = typeof(EncodedDataSets))] public void TextArea_AttributeEncodes_Prefix(string text, bool htmlEncode, string encodedText) { // Arrange var viewData = new ViewDataDictionary(model: null); viewData.ModelMetadata.HtmlEncode = htmlEncode; viewData.TemplateInfo.HtmlFieldPrefix = text; var helper = MvcHelper.GetHtmlHelper(viewData); // Act // htmlAttributes included only to avoid special-cased renaming done for id attribute. var result = helper.TextArea(name: String.Empty, htmlAttributes: new { id = "id", }).ToHtmlString(); // Assert Assert.Equal( "", result); } [Theory] [PropertyData("HtmlEncodedData", PropertyType = typeof(EncodedDataSets))] public void TextArea_HtmlEncodes_Value(string text, bool htmlEncode, string encodedText) { // Arrange var viewData = new ViewDataDictionary(text); viewData.ModelMetadata.HtmlEncode = htmlEncode; viewData.TemplateInfo.HtmlFieldPrefix = "Prefix"; var helper = MvcHelper.GetHtmlHelper(viewData); // Act var result = helper.TextArea("").ToHtmlString(); // Assert Assert.Equal( "", result); } [Fact] public void TextAreaWithExplicitValue() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); // Act MvcHtmlString html = helper.TextArea("foo", "bar"); // Assert Assert.Equal( "", html.ToHtmlString()); } [Theory] [PropertyData("HtmlEncodedData", PropertyType = typeof(EncodedDataSets))] public void TextAreaWithExplicitValue_HtmlEncodes_Value(string text, bool htmlEncode, string encodedText) { // Arrange var viewData = new ViewDataDictionary(model: null); viewData.ModelMetadata.HtmlEncode = htmlEncode; var helper = MvcHelper.GetHtmlHelper(viewData); // Act var result = helper.TextArea("name", text).ToHtmlString(); // Assert Assert.Equal( "", result); } [Fact] public void TextAreaWithDefaultAttributes() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextArea("foo"); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaWithZeroRowsAndColumns() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextArea("foo", null, 0, 0, null); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaWithDotReplacementForId() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextArea("foo.bar.baz"); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaWithObjectAttributes() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextArea("foo", _textAreaAttributesObjectDictionary); // Assert Assert.Equal( "", html.ToHtmlString()); } [Theory] [PropertyData("AttributeEncodedData", PropertyType = typeof(EncodedDataSets))] public void TextAreaWithObjectAttributes_AttributeEncodes_AddedHtmlAttributes( string text, bool htmlEncode, string encodedText) { // Arrange var viewData = new ViewDataDictionary(model: null); viewData.ModelMetadata.HtmlEncode = htmlEncode; viewData.TemplateInfo.HtmlFieldPrefix = "Prefix"; var helper = MvcHelper.GetHtmlHelper(viewData); // Act var result = helper.TextArea(name: "", htmlAttributes: new { attribute = text, }).ToHtmlString(); // Assert Assert.Equal( "", result); } [Fact] public void TextAreaWithObjectAttributesWithUnderscores() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextArea("foo", _textAreaAttributesObjectUnderscoresDictionary); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaWithDictionaryAttributes() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextArea("foo", _textAreaAttributesDictionary); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaWithExplicitValueAndObjectAttributes() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextArea("foo", "Hello World", _textAreaAttributesObjectDictionary); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaWithExplicitValueAndObjectAttributesWithUnderscores() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextArea("foo", "Hello World", _textAreaAttributesObjectUnderscoresDictionary); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaWithExplicitValueAndDictionaryAttributes() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextArea("foo", "", _textAreaAttributesDictionary); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaWithNoValueAndObjectAttributes() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextArea("baz", _textAreaAttributesObjectDictionary); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaWithNullValue() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextArea("foo", null, null); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaWithViewDataErrors() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewDataWithErrors()); // Act MvcHtmlString html = helper.TextArea("foo", _textAreaAttributesObjectDictionary); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaWithViewDataErrorsAndCustomClass() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewDataWithErrors()); // Act MvcHtmlString html = helper.TextArea("foo", new { @class = "foo-class" }); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaWithPrefix() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.TextArea("foo", "bar"); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaWithPrefixAndEmptyName() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.TextArea("", "bar"); // Assert Assert.Equal( "", html.ToHtmlString()); } // TextAreaFor [Fact] public void TextAreaForWithNullExpression() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act & Assert Assert.ThrowsArgumentNull( () => helper.TextAreaFor(null), "expression" ); } [Fact] public void TextAreaForWithOutOfRangeColsThrows() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act & Assert Assert.ThrowsArgumentOutOfRange( () => helper.TextAreaFor(m => m.foo, 0, -1, null /* htmlAttributes */), "columns", "The value must be greater than or equal to zero." ); } [Fact] public void TextAreaForWithOutOfRangeRowsThrows() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act & Assert Assert.ThrowsArgumentOutOfRange( () => helper.TextAreaFor(m => m.foo, -1, 0, null /* htmlAttributes */), "rows", "The value must be greater than or equal to zero." ); } [Fact] public void TextAreaForParameterDictionaryMerging() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo, new { rows = "30" }); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaForParameterDictionaryMerging_Unobtrusive() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); helper.ViewContext.ClientValidationEnabled = true; helper.ViewContext.UnobtrusiveJavaScriptEnabled = true; helper.ViewContext.FormContext = new FormContext(); ModelMetadata modelMetadata = null; helper.ClientValidationRuleFactory = (name, metadata) => { modelMetadata = metadata; return new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } }; }; // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo, new { rows = "30" }); // Assert Assert.NotNull(modelMetadata); Assert.Equal("foo", modelMetadata.PropertyName); Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaForWithDefaultAttributes() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaForWithZeroRowsAndColumns() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo, 0, 0, null); // Assert Assert.Equal( "", html.ToHtmlString()); } [Theory] [PropertyData("AttributeEncodedData", PropertyType = typeof(EncodedDataSets))] public void TextAreaFor_AttributeEncodes_Prefix(string text, bool htmlEncode, string encodedText) { // Arrange var viewData = new ViewDataDictionary(model: null); viewData.ModelMetadata.HtmlEncode = htmlEncode; viewData.TemplateInfo.HtmlFieldPrefix = text; var helper = MvcHelper.GetHtmlHelper(viewData); // Act // htmlAttributes included only to avoid special-cased renaming done for id attribute. var result = helper.TextAreaFor(m => m, htmlAttributes: new { id = "id", }).ToHtmlString(); // Assert Assert.Equal( "", result); } [Theory] [PropertyData("HtmlEncodedData", PropertyType = typeof(EncodedDataSets))] public void TextAreaFor_HtmlEncodes_Value(string text, bool htmlEncode, string encodedText) { // Arrange var viewData = new ViewDataDictionary(text); viewData.ModelMetadata.HtmlEncode = htmlEncode; var helper = MvcHelper.GetHtmlHelper(viewData); // Act var result = helper.TextAreaFor(m => text).ToHtmlString(); // Assert Assert.Equal( "", result); } [Fact] public void TextAreaForWithObjectAttributes() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo, _textAreaAttributesObjectDictionary); // Assert Assert.Equal( "", html.ToHtmlString()); } [Theory] [PropertyData("AttributeEncodedData", PropertyType = typeof(EncodedDataSets))] public void TextAreaForWithObjectAttributes_AttributeEncodes_AddedHtmlAttributes( string text, bool htmlEncode, string encodedText) { // Arrange var viewData = new ViewDataDictionary(model: null); viewData.ModelMetadata.HtmlEncode = htmlEncode; viewData.TemplateInfo.HtmlFieldPrefix = "Prefix"; var helper = MvcHelper.GetHtmlHelper(viewData); // Act var result = helper.TextAreaFor(m => m, htmlAttributes: new { attribute = text, }).ToHtmlString(); // Assert Assert.Equal( "", result); } [Fact] public void TextAreaForWithObjectAttributesWithUnderscores() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo, _textAreaAttributesObjectUnderscoresDictionary); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaForWithDictionaryAttributes() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo, _textAreaAttributesDictionary); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaForWithViewDataErrors() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewDataWithErrors()); // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo, _textAreaAttributesObjectDictionary); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaForWithViewDataErrorsAndCustomClass() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewDataWithErrors()); // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo, new { @class = "foo-class" }); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaForWithPrefix() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaForWithPrefixAndEmptyName() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.TextAreaFor(m => m); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaForParameterDictionaryMergingWithObjectValues() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo, 10, 25, new { rows = "30" }); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaForParameterDictionaryMergingWithObjectValuesWithUnderscores() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo, 10, 25, new { rows = "30", foo_bar = "baz" }); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaForParameterDictionaryMergingWithDictionaryValues() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo, 10, 25, new RouteValueDictionary(new { rows = "30" })); // Assert Assert.Equal( "", html.ToHtmlString()); } [Fact] public void TextAreaHelperDoesNotEncodeInnerHtmlPrefix() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); ModelMetadata metadata = ModelMetadata.FromStringExpression("foo", helper.ViewData); metadata.Model = ""; // Act MvcHtmlString html = TextAreaExtensions.TextAreaHelper(helper, metadata, "testEncoding", rowsAndColumns: null, htmlAttributes: null, innerHtmlPrefix: ""); // Assert Assert.Equal("", html.ToHtmlString()); } public void Dispose() { // Reset ScopeStorage (written via e.g. ViewContext.ClientValidationEnabled) between tests to avoid unexpected interactions. ScopeStorage.CurrentProvider = new StaticScopeStorageProvider(); ScopeStorage.GlobalScope.Clear(); } } }