public void AddThrowsIfFieldIsEmpty() { // Arrange RequestFieldValidatorBase.IgnoreUseUnvalidatedValues = true; ValidationHelper validationHelper = GetValidationHelper(GetContext()); // Act and Assert Assert.ThrowsArgumentNullOrEmptyString(() => validationHelper.Add(field: null), "field"); Assert.ThrowsArgumentNullOrEmptyString(() => validationHelper.Add(field: String.Empty), "field"); }
public void AddThrowsIfValidatorsIsNullOrAnyValidatorIsNull() { // Arrange RequestFieldValidatorBase.IgnoreUseUnvalidatedValues = true; ValidationHelper validationHelper = GetValidationHelper(GetContext()); // Act and Assert Assert.ThrowsArgumentNull(() => validationHelper.Add(field: "foo", validators: null), "validators"); Assert.ThrowsArgumentNull(() => validationHelper.Add(field: "foo", validators: new[] { Validator.DateTime(), null }), "validators"); }
public void AddWorksForCustomValidator() { // Arrange RequestFieldValidatorBase.IgnoreUseUnvalidatedValues = true; string message = "Foo is not an odd number."; var oddValidator = new Mock <IValidator>(); oddValidator.Setup(c => c.Validate(It.IsAny <ValidationContext>())).Returns <ValidationContext>(v => { Assert.IsAssignableFrom <HttpContextBase>(v.ObjectInstance); var context = (HttpContextBase)v.ObjectInstance; var value = Int32.Parse(context.Request.Form["foo"]); if (value % 2 != 0) { return(ValidationResult.Success); } return(new ValidationResult(message)); }).Verifiable(); ValidationHelper validationHelper = GetValidationHelper(GetContext(new { foo = "6" })); // Act validationHelper.Add("foo", oddValidator.Object); var result = validationHelper.Validate(); // Assert Assert.Equal(1, result.Count()); Assert.Equal(message, result.First().ErrorMessage); oddValidator.Verify(); }
public void GetValidationHtmlReturnsAttributesForRegisteredValidators() { // Arrange RequestFieldValidatorBase.IgnoreUseUnvalidatedValues = true; var validator = new Mock <IValidator>(); var clientRules = new ModelClientValidationRule { ValidationType = "foo", ErrorMessage = "Foo error." }; clientRules.ValidationParameters["qux"] = "some data"; validator.Setup(c => c.ClientValidationRule).Returns(clientRules).Verifiable(); var expected = @"data-val-required=""This field is required."" data-val-foo=""Foo error."" data-val-foo-qux=""some data"" data-val=""true"""; ValidationHelper validationHelper = GetValidationHelper(GetContext()); // Act validationHelper.RequireField("foo"); validationHelper.Add("foo", validator.Object); var validationAttributes = validationHelper.For("foo"); // Assert Assert.Equal(expected, validationAttributes.ToString()); }
public void IsValidValidatesSpecifiedFields() { // Arrange RequestFieldValidatorBase.IgnoreUseUnvalidatedValues = true; string message = "Foo is not an odd number."; var oddValidator = new Mock <IValidator>(); oddValidator.Setup(c => c.Validate(It.IsAny <ValidationContext>())).Returns <ValidationContext>(v => { Assert.IsAssignableFrom <HttpContextBase>(v.ObjectInstance); var context = (HttpContextBase)v.ObjectInstance; int value; if (!Int32.TryParse(context.Request.Form["foo"], out value)) { return(ValidationResult.Success); } if (value % 2 != 0) { return(ValidationResult.Success); } return(new ValidationResult(message)); }).Verifiable(); ValidationHelper validationHelper = GetValidationHelper(GetContext(new { foo = "3", bar = "" })); // Act validationHelper.Add(new[] { "foo", "bar" }, oddValidator.Object); validationHelper.RequireFields(new[] { "foo", "bar" }); var result = validationHelper.IsValid("foo"); // Assert Assert.True(result); }
public void CheckboxAddsUnobtrusiveValidationAttributes() { // Arrange const string fieldName = "name"; var modelStateDictionary = new ModelStateDictionary(); var validationHelper = new ValidationHelper( new Mock <HttpContextBase>().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.CheckBox( fieldName, new Dictionary <string, object> { { "data-some-val", "5" } } ); // Assert Assert.Equal( @"<input data-some-val=""5"" data-val=""true"" data-val-length=""Name cannot exceed 30 characters"" data-val-length-max=""30"" data-val-required=""Please specify a valid Name."" id=""name"" name=""name"" type=""checkbox"" />", html.ToString() ); }
public void IsValidReturnsTrueIfAllValuesPassValidation() { // Arrange RequestFieldValidatorBase.IgnoreUseUnvalidatedValues = true; string message = "Foo is not an odd number."; var oddValidator = new Mock <IValidator>(); oddValidator.Setup(c => c.Validate(It.IsAny <ValidationContext>())).Returns <ValidationContext>(v => { var context = Assert.IsAssignableFrom <HttpContextBase>(v.ObjectInstance); if (context.Request.Form["foo"].IsEmpty()) { return(ValidationResult.Success); } int value = context.Request.Form["foo"].AsInt(); if (value % 2 != 0) { return(ValidationResult.Success); } return(new ValidationResult(message)); }).Verifiable(); ValidationHelper validationHelper = GetValidationHelper(GetContext(new { foo = "5", bar = "2" })); // Act validationHelper.Add(new[] { "foo", "bar" }, oddValidator.Object); validationHelper.RequireField("foo"); var result = validationHelper.IsValid(); // Assert Assert.True(result); }
public void ValidateRunsForSpecifiedFields() { // Arrange RequestFieldValidatorBase.IgnoreUseUnvalidatedValues = true; string message = "Foo is not an odd number."; var oddValidator = new Mock <IValidator>(); oddValidator.Setup(c => c.Validate(It.IsAny <ValidationContext>())).Returns <ValidationContext>(v => { var context = Assert.IsAssignableFrom <HttpContextBase>(v.ObjectInstance); if (context.Request.Form["foo"].IsEmpty()) { return(ValidationResult.Success); } int value = context.Request.Form["foo"].AsInt(); if (value % 2 != 0) { return(ValidationResult.Success); } return(new ValidationResult(message)); }).Verifiable(); ValidationHelper validationHelper = GetValidationHelper(GetContext(new { foo = "", bar = "" })); // Act validationHelper.Add(new[] { "foo", "bar" }, oddValidator.Object); validationHelper.RequireField("foo"); var results = validationHelper.Validate("foo"); // Assert ValidationResult result = Assert.Single(results); Assert.Equal("This field is required.", result.ErrorMessage); }
public void GetValidationHtmlHtmlEncodesAttributeValues() { // Arrange RequestFieldValidatorBase.IgnoreUseUnvalidatedValues = true; var validator = new Mock <IValidator>(); var clientRules = new ModelClientValidationRule { ValidationType = "biz", ErrorMessage = "<Biz error.>" }; clientRules.ValidationParameters["qux"] = "<some ' data>"; validator.Setup(c => c.ClientValidationRule).Returns(clientRules).Verifiable(); var expected = @"data-val-required=""This field is required."" data-val-biz=""<Biz error.>"" data-val-biz-qux=""<some ' data>"" data-val=""true"""; // Act ValidationHelper validationHelper = GetValidationHelper(GetContext()); // Assert validationHelper.RequireField("foo"); validationHelper.Add("foo", validator.Object); var validationAttributes = validationHelper.For("foo"); // Assert Assert.Equal(expected, validationAttributes.ToString()); }
public void GetErrorsReturnsAllErrorsIfNoParametersAreSpecified() { // Arrange RequestFieldValidatorBase.IgnoreUseUnvalidatedValues = true; string message = "Foo is not an odd number."; var oddValidator = new Mock <IValidator>(); oddValidator.Setup(c => c.Validate(It.IsAny <ValidationContext>())).Returns <ValidationContext>(v => { Assert.IsAssignableFrom <HttpContextBase>(v.ObjectInstance); var context = (HttpContextBase)v.ObjectInstance; if (context.Request.Form["foo"].IsEmpty()) { return(ValidationResult.Success); } int value = context.Request.Form["foo"].AsInt(); if (value % 2 != 0) { return(ValidationResult.Success); } return(new ValidationResult(message)); }).Verifiable(); ValidationHelper validationHelper = GetValidationHelper(GetContext(new { foo = "4", bar = "" })); // Act validationHelper.Add("foo", oddValidator.Object); validationHelper.RequireFields(new[] { "bar", "foo" }); var result = validationHelper.GetErrors(); // Assert Assert.Equal(2, result.Count()); Assert.Equal("Foo is not an odd number.", result.First()); Assert.Equal("This field is required.", result.Last()); }
public void AddThrowsIfValidatorsParamsArrayIsNull() { // Arrange RequestFieldValidatorBase.IgnoreUseUnvalidatedValues = true; ValidationHelper validationHelper = GetValidationHelper(GetContext()); // Act and Assert Assert.ThrowsArgumentNull(() => validationHelper.Add("foo", null), "validators"); }
public void AddThrowsIfValidatorsAreNull() { // Arrange RequestFieldValidatorBase.IgnoreUseUnvalidatedValues = true; ValidationHelper validationHelper = GetValidationHelper(GetContext()); // Act and Assert Assert.ThrowsArgumentNull(() => validationHelper.Add("foo", Validator.Required(), null, Validator.Range(0, 10)), "validators"); }
public void ClassForReturnsInvalidClassNameIfFieldHasErrors() { // Arrange ValidationHelper helper = GetPostValidationHelper(); helper.Add("foo", new AutoFailValidator()); // Act/Assert Assert.Equal(ValidationHelper.InvalidCssClass, helper.ClassFor("foo").ToHtmlString()); }
public void ListBoxAddsUnobtrusiveValidationAttributes() { // Arrange const string fieldName = "name"; var modelStateDictionary = new ModelStateDictionary(); var validationHelper = new ValidationHelper(new Mock <HttpContextBase>().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.ListBox(fieldName, GetSelectList(), htmlAttributes: new Dictionary <string, object> { { "data-some-val", "5" } }); // Assert Assert.Equal(@"<select data-some-val=""5"" data-val=""true"" data-val-length=""Name cannot exceed 30 characters"" data-val-length-max=""30"" data-val-required=""Please specify a valid Name."" id=""name"" name=""name""> <option value=""A"">Alpha</option> <option value=""B"">Bravo</option> <option value=""C"">Charlie</option> </select>", html.ToString()); }
public void DropDownAddsUnobtrusiveValidationAttributes() { // Arrange const string fieldName = "name"; var modelStateDictionary = new ModelStateDictionary(); var validationHelper = new ValidationHelper( new Mock <HttpContextBase>().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.DropDownList( fieldName, GetSelectList(), htmlAttributes: new Dictionary <string, object> { { "data-some-val", "5" } } ); // Assert Assert.Equal( "<select data-some-val=\"5\" data-val=\"true\" data-val-length=\"Name cannot exceed 30 characters\" data-val-length-max=\"30\" data-val-required=\"Please specify a valid Name.\" id=\"name\" name=\"name\">" + Environment.NewLine + "<option value=\"A\">Alpha</option>" + Environment.NewLine + "<option value=\"B\">Bravo</option>" + Environment.NewLine + "<option value=\"C\">Charlie</option>" + Environment.NewLine + "</select>", html.ToString() ); }
public void DropDownAddsUnobtrusiveValidationAttributes() { // Arrange const string fieldName = "name"; var modelStateDictionary = new ModelStateDictionary(); var validationHelper = new ValidationHelper(new Mock<HttpContextBase>().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.DropDownList(fieldName, GetSelectList(), htmlAttributes: new Dictionary<string, object> { { "data-some-val", "5" } }); // Assert Assert.Equal( "<select data-some-val=\"5\" data-val=\"true\" data-val-length=\"Name cannot exceed 30 characters\" data-val-length-max=\"30\" data-val-required=\"Please specify a valid Name.\" id=\"name\" name=\"name\">" + Environment.NewLine + "<option value=\"A\">Alpha</option>" + Environment.NewLine + "<option value=\"B\">Bravo</option>" + Environment.NewLine + "<option value=\"C\">Charlie</option>" + Environment.NewLine + "</select>", html.ToString()); }
public void CheckboxAddsUnobtrusiveValidationAttributes() { // Arrange const string fieldName = "name"; var modelStateDictionary = new ModelStateDictionary(); var validationHelper = new ValidationHelper(new Mock<HttpContextBase>().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.CheckBox(fieldName, new Dictionary<string, object> { { "data-some-val", "5" } }); // Assert Assert.Equal(@"<input data-some-val=""5"" data-val=""true"" data-val-length=""Name cannot exceed 30 characters"" data-val-length-max=""30"" data-val-required=""Please specify a valid Name."" id=""name"" name=""name"" type=""checkbox"" />", html.ToString()); }
public void ListBoxAddsUnobtrusiveValidationAttributes() { // Arrange const string fieldName = "name"; var modelStateDictionary = new ModelStateDictionary(); var validationHelper = new ValidationHelper(new Mock<HttpContextBase>().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.ListBox(fieldName, GetSelectList(), htmlAttributes: new Dictionary<string, object> { { "data-some-val", "5" } }); // Assert Assert.Equal(@"<select data-some-val=""5"" data-val=""true"" data-val-length=""Name cannot exceed 30 characters"" data-val-length-max=""30"" data-val-required=""Please specify a valid Name."" id=""name"" name=""name""> <option value=""A"">Alpha</option> <option value=""B"">Bravo</option> <option value=""C"">Charlie</option> </select>", html.ToString()); }