Ejemplo n.º 1
0
        public void AddValidation_WithErrorMessageLocalizerFactoryLocalizerProviderAndDisplayName_SetsAttributesAsExpected()
        {
            // Arrange
            var expected = "Error about 'Length' from localizer.";
            var url      = "/Controller/Action";

            var metadataProvider = new TestModelMetadataProvider();

            metadataProvider
            .ForProperty(typeof(string), nameof(string.Length))
            .DisplayDetails(d => d.DisplayName = () => "Display Length");
            var localizerFactory = new Mock <IStringLocalizerFactory>(MockBehavior.Strict).Object;
            var context          = GetValidationContext(localizerFactory, metadataProvider);

            var attribute = new TestableRemoteAttributeBase(dummyGetUrlReturnValue: url)
            {
                HttpMethod   = "POST",
                ErrorMessage = "Error about '{0}' from override.",
            };

            var localizedString = new LocalizedString("Fred", expected);
            var localizer       = new Mock <IStringLocalizer>(MockBehavior.Strict);

            localizer
            .Setup(l => l["Error about '{0}' from override.", "Display Length"])
            .Returns(localizedString)
            .Verifiable();
            var options = context.ActionContext.HttpContext.RequestServices
                          .GetRequiredService <IOptions <MvcDataAnnotationsLocalizationOptions> >();

            options.Value.DataAnnotationLocalizerProvider = (type, factory) => localizer.Object;

            // Act
            attribute.AddValidation(context);

            // Assert
            localizer.VerifyAll();

            Assert.Collection(
                context.Attributes,
                kvp => { Assert.Equal("data-val", kvp.Key); Assert.Equal("true", kvp.Value); },
                kvp =>
            {
                Assert.Equal("data-val-remote", kvp.Key);
                Assert.Equal(expected, kvp.Value);
            },
                kvp =>
            {
                Assert.Equal("data-val-remote-additionalfields", kvp.Key);
                Assert.Equal("*.Length", kvp.Value);
            },
                kvp => { Assert.Equal("data-val-remote-type", kvp.Key); Assert.Equal("POST", kvp.Value); },
                kvp => { Assert.Equal("data-val-remote-url", kvp.Key); Assert.Equal(url, kvp.Value); });
        }
Ejemplo n.º 2
0
        public void AddValidation_WithErrorResourcesLocalizerFactoryAndLocalizerProvider_SetsAttributesAsExpected()
        {
            // Arrange
            var expected  = "Error about 'Length' from resources.";
            var url       = "/Controller/Action";
            var attribute = new TestableRemoteAttributeBase(dummyGetUrlReturnValue: url)
            {
                HttpMethod = "POST",
                ErrorMessageResourceName = nameof(Resources.RemoteAttribute_Error),
                ErrorMessageResourceType = typeof(Resources),
            };

            var localizerFactory = new Mock <IStringLocalizerFactory>(MockBehavior.Strict).Object;
            var context          = GetValidationContext(localizerFactory);

            var localizer = new Mock <IStringLocalizer>(MockBehavior.Strict);
            var options   = context.ActionContext.HttpContext.RequestServices
                            .GetRequiredService <IOptions <MvcDataAnnotationsLocalizationOptions> >();

            options.Value.DataAnnotationLocalizerProvider = (type, factory) => localizer.Object;

            // Act
            attribute.AddValidation(context);

            // Assert
            Assert.Collection(
                context.Attributes,
                kvp => { Assert.Equal("data-val", kvp.Key); Assert.Equal("true", kvp.Value); },
                kvp =>
            {
                // Configuring the attribute using ErrorMessageResource* trumps available IStringLocalizer etc.
                Assert.Equal("data-val-remote", kvp.Key);
                Assert.Equal(expected, kvp.Value);
            },
                kvp =>
            {
                Assert.Equal("data-val-remote-additionalfields", kvp.Key);
                Assert.Equal("*.Length", kvp.Value);
            },
                kvp => { Assert.Equal("data-val-remote-type", kvp.Key); Assert.Equal("POST", kvp.Value); },
                kvp => { Assert.Equal("data-val-remote-url", kvp.Key); Assert.Equal(url, kvp.Value); });
        }
Ejemplo n.º 3
0
        public void AddValidation_WithErrorMessageAndDisplayName_SetsAttributesAsExpected()
        {
            // Arrange
            var expected = "Error about 'Display Length' from override.";
            var url      = "/Controller/Action";

            var metadataProvider = new TestModelMetadataProvider();

            metadataProvider
            .ForProperty(typeof(string), nameof(string.Length))
            .DisplayDetails(d => d.DisplayName = () => "Display Length");
            var context = GetValidationContext(localizerFactory: null, metadataProvider: metadataProvider);

            var attribute = new TestableRemoteAttributeBase(dummyGetUrlReturnValue: url)
            {
                HttpMethod   = "POST",
                ErrorMessage = "Error about '{0}' from override.",
            };

            // Act
            attribute.AddValidation(context);

            // Assert
            Assert.Collection(
                context.Attributes,
                kvp => { Assert.Equal("data-val", kvp.Key); Assert.Equal("true", kvp.Value); },
                kvp =>
            {
                Assert.Equal("data-val-remote", kvp.Key);
                Assert.Equal(expected, kvp.Value);
            },
                kvp =>
            {
                Assert.Equal("data-val-remote-additionalfields", kvp.Key);
                Assert.Equal("*.Length", kvp.Value);
            },
                kvp => { Assert.Equal("data-val-remote-type", kvp.Key); Assert.Equal("POST", kvp.Value); },
                kvp => { Assert.Equal("data-val-remote-url", kvp.Key); Assert.Equal(url, kvp.Value); });
        }
Ejemplo n.º 4
0
        public void AddValidation_WithErrorMessageAndLocalizerProvider_SetsAttributesAsExpected()
        {
            // Arrange
            var expected  = "Error about 'Length' from override.";
            var url       = "/Controller/Action";
            var context   = GetValidationContext();
            var attribute = new TestableRemoteAttributeBase(url)
            {
                HttpMethod   = "POST",
                ErrorMessage = "Error about '{0}' from override.",
            };

            var options = context.ActionContext.HttpContext.RequestServices
                          .GetRequiredService <IOptions <MvcDataAnnotationsLocalizationOptions> >();
            var localizer = new Mock <IStringLocalizer>(MockBehavior.Strict);

            options.Value.DataAnnotationLocalizerProvider = (type, factory) => localizer.Object;

            // Act
            attribute.AddValidation(context);

            // Assert
            Assert.Collection(
                context.Attributes,
                kvp => { Assert.Equal("data-val", kvp.Key); Assert.Equal("true", kvp.Value); },
                kvp =>
            {
                // Non-null DataAnnotationLocalizerProvider alone is insufficient to change error message.
                Assert.Equal("data-val-remote", kvp.Key);
                Assert.Equal(expected, kvp.Value);
            },
                kvp =>
            {
                Assert.Equal("data-val-remote-additionalfields", kvp.Key);
                Assert.Equal("*.Length", kvp.Value);
            },
                kvp => { Assert.Equal("data-val-remote-type", kvp.Key); Assert.Equal("POST", kvp.Value); },
                kvp => { Assert.Equal("data-val-remote-url", kvp.Key); Assert.Equal(url, kvp.Value); });
        }