// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using Moq; using Xunit; using Assert = Microsoft.TestCommon.AssertEx; namespace System.Web.Mvc.Test { public class AssociatedValidatorProviderTest { [Fact] public void GetValidatorsGuardClauses() { // Arrange ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(object)); Mock provider = new Mock { CallBase = true }; // Act & Assert Assert.ThrowsArgumentNull( () => provider.Object.GetValidators(null, new ControllerContext()), "metadata"); Assert.ThrowsArgumentNull( () => provider.Object.GetValidators(metadata, null), "context"); } [Fact] public void GetValidatorsForPropertyWithLocalAttributes() { // Arrange IEnumerable callbackAttributes = null; ControllerContext context = new ControllerContext(); ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(PropertyModel), "LocalAttributes"); Mock provider = new Mock { CallBase = true }; provider.Setup(p => p.AbstractGetValidators(metadata, context, It.IsAny>())) .Callback>((m, c, attributes) => callbackAttributes = attributes) .Returns(() => null) .Verifiable(); // Act provider.Object.GetValidators(metadata, context); // Assert provider.Verify(); Assert.True(callbackAttributes.Any(a => a is RequiredAttribute)); } [Fact] public void GetValidatorsForPropertyWithMetadataAttributes() { // Arrange IEnumerable callbackAttributes = null; ControllerContext context = new ControllerContext(); ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(PropertyModel), "MetadataAttributes"); Mock provider = new Mock { CallBase = true }; provider.Setup(p => p.AbstractGetValidators(metadata, context, It.IsAny>())) .Callback>((m, c, attributes) => callbackAttributes = attributes) .Returns(() => null) .Verifiable(); // Act provider.Object.GetValidators(metadata, context); // Assert provider.Verify(); Assert.True(callbackAttributes.Any(a => a is RangeAttribute)); } [Fact] public void GetValidatorsForPropertyWithMixedAttributes() { // Arrange IEnumerable callbackAttributes = null; ControllerContext context = new ControllerContext(); ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(PropertyModel), "MixedAttributes"); Mock provider = new Mock { CallBase = true }; provider.Setup(p => p.AbstractGetValidators(metadata, context, It.IsAny>())) .Callback>((m, c, attributes) => callbackAttributes = attributes) .Returns(() => null) .Verifiable(); // Act provider.Object.GetValidators(metadata, context); // Assert provider.Verify(); Assert.True(callbackAttributes.Any(a => a is RangeAttribute)); Assert.True(callbackAttributes.Any(a => a is RequiredAttribute)); } [MetadataType(typeof(Metadata))] private class PropertyModel { [Required] public int LocalAttributes { get; set; } public string MetadataAttributes { get; set; } [Required] public double MixedAttributes { get; set; } private class Metadata { [Range(10, 100)] public object MetadataAttributes { get; set; } [Range(10, 100)] public object MixedAttributes { get; set; } } } public abstract class TestableAssociatedValidatorProvider : AssociatedValidatorProvider { protected override IEnumerable GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable attributes) { return AbstractGetValidators(metadata, context, attributes); } // Hoist access public abstract IEnumerable AbstractGetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable attributes); } } }