// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. using System.Collections.Generic; using System.Data.Linq; using System.Web.WebPages.Html; using Moq; using Xunit; using Assert = Microsoft.TestCommon.AssertEx; namespace System.Web.WebPages.Test { public class InputHelperTest { private static readonly IDictionary _attributesDictionary = new Dictionary { { "baz", "BazValue" } }; private static readonly object _attributesObject = new { baz = "BazValue" }; [Fact] public void HiddenWithBinaryArrayValueRendersBase64EncodedValue() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var result = helper.Hidden("ProductName", new Binary(new byte[] { 23, 43, 53 })); // Assert Assert.Equal("", result.ToHtmlString()); } [Fact] public void HiddenWithEmptyNameThrows() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act & Assert Assert.ThrowsArgumentNullOrEmptyString(() => helper.Hidden(String.Empty), "name"); Assert.ThrowsArgumentNullOrEmptyString(() => helper.Hidden(null), "name"); } [Fact] public void HiddenWithExplicitValue() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Hidden("foo", "DefaultFoo"); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void HiddenWithExplicitValueAndAttributesDictionary() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Hidden("foo", "DefaultFoo", new Dictionary { { "attr", "attr-val" } }); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void HiddenWithExplicitValueAndObjectDictionary() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Hidden("foo", "DefaultFoo", new { attr = "attr-val" }); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void HiddenWithExplicitValueNull() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Hidden("foo", value: null); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void HiddenWithModelValue() { // Arrange var model = new ModelStateDictionary(); model.SetModelValue("foo", "bar"); HtmlHelper helper = HtmlHelperFactory.Create(model); // Act var html = helper.Hidden("foo"); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void HiddenWithModelValueAndAttributesDictionary() { // Arrange var model = new ModelStateDictionary(); model.SetModelValue("foo", "bar"); HtmlHelper helper = HtmlHelperFactory.Create(model); // Act var html = helper.Hidden("foo", null, new Dictionary { { "attr", "attr-val" } }); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void HiddenWithImplicitValueAndAttributesObject() { // Arrange var model = new ModelStateDictionary(); model.SetModelValue("foo", "bar"); HtmlHelper helper = HtmlHelperFactory.Create(model); // Act var html = helper.Hidden("foo", null, new { attr = "attr-val" }); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void HiddenWithNameAndValue() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Hidden("foo", "fooValue"); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void HiddenWithExplicitOverwritesAttributeValue() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Hidden("foo", "fooValue", new { value = "barValue" }); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void HiddenWithModelValueOverwritesAttributeValue() { // Arrange var model = new ModelStateDictionary(); model.SetModelValue("foo", "fooValue"); HtmlHelper helper = HtmlHelperFactory.Create(model); // Act var html = helper.Hidden("foo", null, new { value = "barValue" }); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void HiddenAddsUnobtrusiveValidationAttributes() { // Arrange const string fieldName = "name"; var modelStateDictionary = new ModelStateDictionary(); var validationHelper = new ValidationHelper(new Mock().Object, modelStateDictionary); HtmlHelper helper = HtmlHelperFactory.Create(modelStateDictionary, validationHelper); // Act validationHelper.RequireField(fieldName, "Please specify a valid Name."); validationHelper.Add(fieldName, Validator.StringLength(30, errorMessage: "Name cannot exceed {0} characters")); var html = helper.Hidden(fieldName, value: null, htmlAttributes: new Dictionary { { "data-some-val", "5" } }); // Assert Assert.Equal(@"", html.ToString()); } // Password [Fact] public void PasswordWithEmptyNameThrows() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act & Assert Assert.ThrowsArgumentNullOrEmptyString(() => helper.Password(String.Empty), "name"); Assert.ThrowsArgumentNullOrEmptyString(() => helper.Password(null), "name"); } [Fact] public void PasswordDictionaryOverridesImplicitParameters() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Password("foo", "Some Value", new { type = "fooType" }); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void PasswordExplicitParametersOverrideDictionary() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Password("foo", "Some Value", new { value = "Another Value", name = "bar" }); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void PasswordWithExplicitValue() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Password("foo", "DefaultFoo", (object)null); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void PasswordWithExplicitValueAndAttributesDictionary() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Password("foo", "DefaultFoo", new { baz = "BazValue" }); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void PasswordWithExplicitValueAndAttributesObject() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Password("foo", "DefaultFoo", new Dictionary { { "baz", "BazValue" } }); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void PasswordWithExplicitValueNull() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Password("foo", value: (string)null); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void PasswordWithImplicitValue() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Password("foo"); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void PasswordWithImplicitValueAndAttributesDictionary() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Password("foo", null, _attributesDictionary); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void PasswordWithImplicitValueAndAttributesDictionaryReturnsEmptyValueIfNotFound() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Password("keyNotFound", null, _attributesDictionary); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void PasswordWithImplicitValueAndAttributesObject() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Password("foo", null, _attributesObject); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void PasswordWithNameAndValue() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.Password("foo", "fooValue"); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void PasswordWithNullNameThrows() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act & Assert Assert.ThrowsArgumentNullOrEmptyString(() => helper.Password(null), "name"); Assert.ThrowsArgumentNullOrEmptyString(() => helper.Password(String.Empty), "name"); } [Fact] public void PasswordAddsUnobtrusiveValidationAttributes() { // Arrange const string fieldName = "name"; var modelStateDictionary = new ModelStateDictionary(); var validationHelper = new ValidationHelper(new Mock().Object, modelStateDictionary); HtmlHelper helper = HtmlHelperFactory.Create(modelStateDictionary, validationHelper); // Act validationHelper.RequireField(fieldName, "Please specify a valid Name."); validationHelper.Add(fieldName, Validator.StringLength(30, errorMessage: "Name cannot exceed {0} characters")); var html = helper.Password(fieldName, value: null, htmlAttributes: new Dictionary { { "data-some-val", "5" } }); // Assert Assert.Equal(@"", html.ToString()); } //Input [Fact] public void TextBoxDictionaryOverridesImplicitValues() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.TextBox("foo", "DefaultFoo", new { type = "fooType" }); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void TextBoxExplicitParametersOverrideDictionaryValues() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.TextBox("foo", "DefaultFoo", new { value = "Some other value" }); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void TextBoxWithDotReplacementForId() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.TextBox("foo.bar.baz", null); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void TextBoxWithEmptyNameThrows() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act & Assert Assert.ThrowsArgumentNullOrEmptyString(() => helper.TextBox(null), "name"); Assert.ThrowsArgumentNullOrEmptyString(() => helper.TextBox(String.Empty), "name"); } [Fact] public void TextBoxWithExplicitValue() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.TextBox("foo", "DefaultFoo", (object)null); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void TextBoxWithExplicitValueAndAttributesDictionary() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.TextBox("foo", "DefaultFoo", _attributesDictionary); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void TextBoxWithExplicitValueAndAttributesObject() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.TextBox("foo", "DefaultFoo", _attributesObject); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void TextBoxWithExplicitValueNull() { // Arrange var modelState = new ModelStateDictionary(); modelState.SetModelValue("foo", "fooModelValue"); HtmlHelper helper = HtmlHelperFactory.Create(modelState); // Act var html = helper.TextBox("foo", (string)null /* value */, (object)null); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void TextBoxWithImplicitValue() { // Arrange var modelState = new ModelStateDictionary(); modelState.SetModelValue("foo", "fooModelValue"); HtmlHelper helper = HtmlHelperFactory.Create(modelState); // Act var html = helper.TextBox("foo"); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void TextBoxWithImplicitValueAndAttributesDictionary() { // Arrange var modelState = new ModelStateDictionary(); modelState.SetModelValue("foo", "fooModelValue"); HtmlHelper helper = HtmlHelperFactory.Create(modelState); // Act var html = helper.TextBox("foo", null, _attributesDictionary); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void TextBoxWithImplicitValueAndAttributesDictionaryReturnsEmptyValueIfNotFound() { // Arrange var modelState = new ModelStateDictionary(); modelState.SetModelValue("foo", "fooModelValue"); HtmlHelper helper = HtmlHelperFactory.Create(modelState); // Act var html = helper.TextBox("keyNotFound", null, _attributesDictionary); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void TextBoxWithImplicitValueAndAttributesObject() { // Arrange var modelState = new ModelStateDictionary(); modelState.SetModelValue("foo", "fooModelValue"); HtmlHelper helper = HtmlHelperFactory.Create(modelState); // Act var html = helper.TextBox("foo", null, _attributesObject); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void TextBoxWithNameAndValue() { // Arrange HtmlHelper helper = HtmlHelperFactory.Create(); // Act var html = helper.TextBox("foo", "fooValue"); // Assert Assert.Equal(@"", html.ToHtmlString()); } [Fact] public void TextBoxAddsUnobtrusiveValidationAttributes() { // Arrange const string fieldName = "name"; var modelStateDictionary = new ModelStateDictionary(); var validationHelper = new ValidationHelper(new Mock().Object, modelStateDictionary); HtmlHelper helper = HtmlHelperFactory.Create(modelStateDictionary, validationHelper); // Act validationHelper.RequireField(fieldName, "Please specify a valid Name."); validationHelper.Add(fieldName, Validator.StringLength(30, errorMessage: "Name cannot exceed {0} characters")); var html = helper.TextBox(fieldName, value: null, htmlAttributes: new Dictionary { { "data-some-val", "5" } }); // Assert Assert.Equal(@"", html.ToString()); } } }