// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. using System.Web.Mvc.Test; using System.Web.Routing; using Microsoft.Web.UnitTestUtil; using Xunit; using Assert = Microsoft.TestCommon.AssertEx; namespace System.Web.Mvc.Html.Test { public class TextAreaExtensionsTest { 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."); } [Fact] public void TextAreaWithExplicitValue() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); // Act MvcHtmlString html = helper.TextArea("foo", "bar"); // Assert Assert.Equal(@"", html.ToHtmlString()); } [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()); } [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(); helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } }; // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo, new { rows = "30" }); // Assert 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()); } [Fact] public void TextAreaForWithObjectAttributes() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo, _textAreaAttributesObjectDictionary); // Assert Assert.Equal(@"", html.ToHtmlString()); } [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()); } } }