public async Task ProcessAsync_DoesNotGenerateValidationSummaryWhenNone_IgnoresCase(string validationSummary)
        {
            // Arrange
            var validationSummaryTagHelper = new ValidationSummaryTagHelper
            {
                ValidationSummaryValue = validationSummary
            };
            var output = new TagHelperOutput(
                "div",
                attributes: new Dictionary <string, string>(),
                content: "Content of validation message");
            var tagBuilder = new TagBuilder("span2")
            {
                InnerHtml = "New HTML"
            };

            var generator = new Mock <IHtmlGenerator>(MockBehavior.Strict);

            // Act
            await validationSummaryTagHelper.ProcessAsync(context : null, output : output);

            // Assert
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal("Content of validation message", output.Content);
        }
        public async Task ProcessAsync_DoesNothingIfValidationSummaryNone()
        {
            // Arrange
            var validationSummaryTagHelper = new ValidationSummaryTagHelper
            {
                ValidationSummary = ValidationSummary.None,
            };
            var expectedPreContent  = "original pre-content";
            var expectedContent     = "original content";
            var expectedPostContent = "original post-content";
            var output = new TagHelperOutput(
                "div",
                attributes: new Dictionary <string, object>());

            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);

            var generator   = new Mock <IHtmlGenerator>(MockBehavior.Strict);
            var viewContext = CreateViewContext();

            validationSummaryTagHelper.ViewContext = viewContext;
            validationSummaryTagHelper.Generator   = generator.Object;

            // Act
            await validationSummaryTagHelper.ProcessAsync(context : null, output : output);

            // Assert
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
        }
        public async Task ProcessAsync_CallsIntoGenerateValidationSummaryWithExpectedParameters()
        {
            // Arrange
            var validationSummaryTagHelper = new ValidationSummaryTagHelper
            {
                ValidationSummaryValue = "ModelOnly",
            };
            var output = new TagHelperOutput(
                "div",
                attributes: new Dictionary <string, string>(),
                content: "Content of validation summary");
            var expectedViewContext = CreateViewContext();
            var generator           = new Mock <IHtmlGenerator>();

            generator
            .Setup(mock => mock.GenerateValidationSummary(expectedViewContext, true, null, null, null))
            .Returns(new TagBuilder("div"))
            .Verifiable();
            validationSummaryTagHelper.ViewContext = expectedViewContext;
            validationSummaryTagHelper.Generator   = generator.Object;

            // Act & Assert
            await validationSummaryTagHelper.ProcessAsync(context : null, output : output);

            generator.Verify();
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal("Content of validation summary", output.Content);
        }
        public async Task ProcessAsync_DoesNothingIfNullOrEmptyValidationSummaryValue(string validationSummaryValue)
        {
            // Arrange
            var validationSummaryTagHelper = new ValidationSummaryTagHelper
            {
                ValidationSummaryValue = validationSummaryValue
            };
            var output = new TagHelperOutput(
                "div",
                attributes: new Dictionary <string, string>(),
                content: "Content of validation message");

            var generator   = new Mock <IHtmlGenerator>(MockBehavior.Strict);
            var viewContext = CreateViewContext();

            validationSummaryTagHelper.ViewContext = viewContext;
            validationSummaryTagHelper.Generator   = generator.Object;

            // Act
            await validationSummaryTagHelper.ProcessAsync(context : null, output : output);

            // Assert
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal("Content of validation message", output.Content);
        }
Esempio n. 5
0
        public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationSummary()
        {
            // Arrange
            var tagBuilder = new TagBuilder("span2", new HtmlEncoder())
            {
                InnerHtml = "New HTML"
            };

            tagBuilder.Attributes.Add("data-foo", "bar");
            tagBuilder.Attributes.Add("data-hello", "world");
            tagBuilder.Attributes.Add("anything", "something");

            var generator = new Mock <IHtmlGenerator>(MockBehavior.Strict);

            generator
            .Setup(mock => mock.GenerateValidationSummary(
                       It.IsAny <ViewContext>(),
                       It.IsAny <bool>(),
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <object>()))
            .Returns(tagBuilder);

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator.Object)
            {
                ValidationSummary = ValidationSummary.ModelOnly,
            };

            var expectedPreContent = "original pre-content";
            var expectedContent    = "original content";
            var output             = new TagHelperOutput(
                "div",
                attributes: new TagHelperAttributeList());

            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent("Content of validation summary");

            var viewContext = CreateViewContext();

            validationSummaryTagHelper.ViewContext = viewContext;

            // Act
            await validationSummaryTagHelper.ProcessAsync(context : null, output : output);

            // Assert
            Assert.Equal("div", output.TagName);
            Assert.Equal(3, output.Attributes.Count);
            var attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-foo"));

            Assert.Equal("bar", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-hello"));
            Assert.Equal("world", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("anything"));
            Assert.Equal("something", attribute.Value);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal("Content of validation summaryNew HTML", output.PostContent.GetContent());
        }
Esempio n. 6
0
        public async Task ProcessAsync_GeneratesExpectedOutput()
        {
            // Arrange
            var expectedTagName  = "not-div";
            var metadataProvider = new TestModelMetadataProvider();
            var htmlGenerator    = new TestableHtmlGenerator(metadataProvider);

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(htmlGenerator)
            {
                ValidationSummary = ValidationSummary.All,
            };

            var expectedPreContent = "original pre-content";
            var expectedContent    = "original content";
            var tagHelperContext   = new TagHelperContext(
                allAttributes: new ReadOnlyTagHelperAttributeList <IReadOnlyTagHelperAttribute>(
                    Enumerable.Empty <IReadOnlyTagHelperAttribute>()),
                items: new Dictionary <object, object>(),
                uniqueId: "test",
                getChildContentAsync: () =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("Something");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });
            var output = new TagHelperOutput(
                expectedTagName,
                attributes: new TagHelperAttributeList
            {
                { "class", "form-control" }
            });

            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent("Custom Content");

            Model model       = null;
            var   viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider);

            validationSummaryTagHelper.ViewContext = viewContext;

            // Act
            await validationSummaryTagHelper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal(2, output.Attributes.Count);
            var attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("class"));

            Assert.Equal("form-control validation-summary-valid", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-valmsg-summary"));
            Assert.Equal("true", attribute.Value);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal("Custom Content<ul><li style=\"display:none\"></li>" + Environment.NewLine + "</ul>",
                         output.PostContent.GetContent());
            Assert.Equal(expectedTagName, output.TagName);
        }
Esempio n. 7
0
        public async Task ProcessAsync_GeneratesValidationSummaryWhenNotNone(ValidationSummary validationSummary)
        {
            // Arrange
            var tagBuilder = new TagBuilder("span2");

            tagBuilder.InnerHtml.SetHtmlContent("New HTML");

            var generator = new Mock <IHtmlGenerator>();

            generator
            .Setup(mock => mock.GenerateValidationSummary(
                       It.IsAny <ViewContext>(),
                       It.IsAny <bool>(),
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <object>()))
            .Returns(tagBuilder)
            .Verifiable();

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator.Object)
            {
                ValidationSummary = validationSummary,
            };

            var expectedPreContent = "original pre-content";
            var expectedContent    = "original content";
            var output             = new TagHelperOutput(
                tagName: "div",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (_) => Task.FromResult <TagHelperContent>(new DefaultTagHelperContent()));

            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent("Content of validation message");

            var viewContext = CreateViewContext();

            validationSummaryTagHelper.ViewContext = viewContext;

            var context = new TagHelperContext(
                allAttributes: new ReadOnlyTagHelperAttributeList <IReadOnlyTagHelperAttribute>(
                    Enumerable.Empty <IReadOnlyTagHelperAttribute>()),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            // Act
            await validationSummaryTagHelper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal("Content of validation messageNew HTML", output.PostContent.GetContent());
            generator.Verify();
        }
        public void Concstructor_IntializesProperties_AsExpected()
        {
            // Arrange & Act
            var validationSummaryTagHelper = new ValidationSummaryTagHelper();

            // Assert
            Assert.Null(validationSummaryTagHelper.Generator);
            Assert.Null(validationSummaryTagHelper.ViewContext);
            Assert.Equal(ValidationSummary.None, validationSummaryTagHelper.ValidationSummary);
        }
        public void Concstructor_IntializesProperties_AsExpected()
        {
            // Arrange & Act
            var validationSummaryTagHelper = new ValidationSummaryTagHelper();

            // Assert
            Assert.Null(validationSummaryTagHelper.Generator);
            Assert.Null(validationSummaryTagHelper.ViewContext);
            Assert.Equal(ValidationSummary.None, validationSummaryTagHelper.ValidationSummary);
        }
Esempio n. 10
0
        public async Task ProcessAsync_CallsIntoGenerateValidationSummaryWithExpectedParameters(
            ValidationSummary validationSummary,
            bool expectedExcludePropertyErrors)
        {
            // Arrange
            var expectedViewContext = CreateViewContext();

            var generator = new Mock <IHtmlGenerator>();

            generator
            .Setup(mock => mock.GenerateValidationSummary(
                       expectedViewContext,
                       expectedExcludePropertyErrors,
                       null,  // message
                       null,  // headerTag
                       null)) // htmlAttributes
            .Returns(new TagBuilder("div"))
            .Verifiable();

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator.Object)
            {
                ValidationSummary = validationSummary,
            };

            var expectedPreContent  = "original pre-content";
            var expectedContent     = "original content";
            var expectedPostContent = "original post-content";
            var output = new TagHelperOutput(
                tagName: "div",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (_) => Task.FromResult <TagHelperContent>(new DefaultTagHelperContent()));

            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);

            validationSummaryTagHelper.ViewContext = expectedViewContext;

            var context = new TagHelperContext(
                allAttributes: new ReadOnlyTagHelperAttributeList <IReadOnlyTagHelperAttribute>(
                    Enumerable.Empty <IReadOnlyTagHelperAttribute>()),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            // Act & Assert
            await validationSummaryTagHelper.ProcessAsync(context, output);

            generator.Verify();
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
        }
        public async Task ProcessAsync_GeneratesExpectedOutput()
        {
            // Arrange
            var expectedTagName = "not-div";
            var metadataProvider = new TestModelMetadataProvider();
            var htmlGenerator = new TestableHtmlGenerator(metadataProvider);

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(htmlGenerator)
            {
                ValidationSummary = ValidationSummary.All,
            };

            var expectedPreContent = "original pre-content";
            var expectedContent = "original content";
            var tagHelperContext = new TagHelperContext(
                allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
                    Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
                items: new Dictionary<object, object>(),
                uniqueId: "test",
                getChildContentAsync: useCachedResult =>
                {
                    var tagHelperContent = new DefaultTagHelperContent();
                    tagHelperContent.SetContent("Something");
                    return Task.FromResult<TagHelperContent>(tagHelperContent);
                });
            var output = new TagHelperOutput(
                expectedTagName,
                attributes: new TagHelperAttributeList
                {
                    { "class", "form-control" }
                });
            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent("Custom Content");

            Model model = null;
            var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider);
            validationSummaryTagHelper.ViewContext = viewContext;

            // Act
            await validationSummaryTagHelper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal(2, output.Attributes.Count);
            var attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("class"));
            Assert.Equal("form-control validation-summary-valid", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-valmsg-summary"));
            Assert.Equal("true", attribute.Value);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal("Custom Content<ul><li style=\"display:none\"></li>" + Environment.NewLine + "</ul>",
                         output.PostContent.GetContent());
            Assert.Equal(expectedTagName, output.TagName);
        }
Esempio n. 12
0
        public async Task ProcessAsync_GeneratesExpectedOutput()
        {
            // Arrange
            var expectedTagName            = "not-div";
            var metadataProvider           = new DataAnnotationsModelMetadataProvider();
            var validationSummaryTagHelper = new ValidationSummaryTagHelper
            {
                ValidationSummary = ValidationSummary.All,
            };

            var expectedPreContent = "original pre-content";
            var expectedContent    = "original content";
            var tagHelperContext   = new TagHelperContext(
                allAttributes: new Dictionary <string, object>(),
                uniqueId: "test",
                getChildContentAsync: () => Task.FromResult("Something"));
            var output = new TagHelperOutput(
                expectedTagName,
                attributes: new Dictionary <string, string>
            {
                { "class", "form-control" }
            })
            {
                PreContent  = expectedPreContent,
                Content     = expectedContent,
                PostContent = "Custom Content",
            };

            var   htmlGenerator = new TestableHtmlGenerator(metadataProvider);
            Model model         = null;
            var   viewContext   = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider);

            validationSummaryTagHelper.ViewContext = viewContext;
            validationSummaryTagHelper.Generator   = htmlGenerator;

            // Act
            await validationSummaryTagHelper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal(2, output.Attributes.Count);
            var attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("class"));

            Assert.Equal("form-control validation-summary-valid", attribute.Value);
            attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("data-valmsg-summary"));
            Assert.Equal("true", attribute.Value);
            Assert.Equal(expectedPreContent, output.PreContent);
            Assert.Equal(expectedContent, output.Content);
            Assert.Equal("Custom Content<ul><li style=\"display:none\"></li>" + Environment.NewLine + "</ul>",
                         output.PostContent);
            Assert.Equal(expectedTagName, output.TagName);
        }
Esempio n. 13
0
        public async Task ProcessAsync_GeneratesValidationSummaryWhenNotNone(ValidationSummary validationSummary)
        {
            // Arrange
            var tagBuilder = new TagBuilder("span2", new HtmlEncoder())
            {
                InnerHtml = "New HTML"
            };

            var generator = new Mock <IHtmlGenerator>();

            generator
            .Setup(mock => mock.GenerateValidationSummary(
                       It.IsAny <ViewContext>(),
                       It.IsAny <bool>(),
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <object>()))
            .Returns(tagBuilder)
            .Verifiable();

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator.Object)
            {
                ValidationSummary = validationSummary,
            };

            var expectedPreContent = "original pre-content";
            var expectedContent    = "original content";
            var output             = new TagHelperOutput(
                "div",
                attributes: new TagHelperAttributeList());

            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent("Content of validation message");

            var viewContext = CreateViewContext();

            validationSummaryTagHelper.ViewContext = viewContext;

            // Act
            await validationSummaryTagHelper.ProcessAsync(context : null, output : output);

            // Assert
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal("Content of validation messageNew HTML", output.PostContent.GetContent());
            generator.Verify();
        }
        public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationSummary()
        {
            // Arrange
            var validationSummaryTagHelper = new ValidationSummaryTagHelper
            {
                ValidationSummaryValue = "ModelOnly"
            };
            var output = new TagHelperOutput(
                "div",
                attributes: new Dictionary <string, string>(),
                content: "Content of validation summary");
            var tagBuilder = new TagBuilder("span2")
            {
                InnerHtml = "New HTML"
            };

            tagBuilder.Attributes.Add("data-foo", "bar");
            tagBuilder.Attributes.Add("data-hello", "world");
            tagBuilder.Attributes.Add("anything", "something");

            var generator = new Mock <IHtmlGenerator>(MockBehavior.Strict);

            generator
            .Setup(mock => mock.GenerateValidationSummary(
                       It.IsAny <ViewContext>(),
                       It.IsAny <bool>(),
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <object>()))
            .Returns(tagBuilder);
            var viewContext = CreateViewContext();

            validationSummaryTagHelper.ViewContext = viewContext;
            validationSummaryTagHelper.Generator   = generator.Object;

            // Act
            await validationSummaryTagHelper.ProcessAsync(context : null, output : output);

            // Assert
            Assert.Equal(output.TagName, "div");
            Assert.Equal(3, output.Attributes.Count);
            var attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("data-foo"));

            Assert.Equal("bar", attribute.Value);
            attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("data-hello"));
            Assert.Equal("world", attribute.Value);
            attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("anything"));
            Assert.Equal("something", attribute.Value);
            Assert.Equal("Content of validation summaryNew HTML", output.Content);
        }
Esempio n. 15
0
        public async Task ProcessAsync_CallsIntoGenerateValidationSummaryWithExpectedParameters(
            ValidationSummary validationSummary,
            bool expectedExcludePropertyErrors)
        {
            // Arrange
            var expectedViewContext = CreateViewContext();

            var generator = new Mock <IHtmlGenerator>();

            generator
            .Setup(mock => mock.GenerateValidationSummary(
                       expectedViewContext,
                       expectedExcludePropertyErrors,
                       null,  // message
                       null,  // headerTag
                       null)) // htmlAttributes
            .Returns(new TagBuilder("div", new HtmlEncoder()))
            .Verifiable();

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator.Object)
            {
                ValidationSummary = validationSummary,
            };

            var expectedPreContent  = "original pre-content";
            var expectedContent     = "original content";
            var expectedPostContent = "original post-content";
            var output = new TagHelperOutput(
                "div",
                attributes: new TagHelperAttributeList());

            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);


            validationSummaryTagHelper.ViewContext = expectedViewContext;

            // Act & Assert
            await validationSummaryTagHelper.ProcessAsync(context : null, output : output);

            generator.Verify();
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
        }
        public void ValidationSummaryProperty_ThrowsWhenSetToInvalidValidationSummaryValue(
            ValidationSummary validationSummary)
        {
            // Arrange
            var validationSummaryTagHelper = new ValidationSummaryTagHelper();
            var expectedMessage            = string.Format(
                @"The value of argument 'value' ({0}) is invalid for Enum type 'Microsoft.AspNet.Mvc.ValidationSummary'.
Parameter name: value",
                validationSummary);

            // Act & Assert
            var ex = Assert.Throws <ArgumentException>(
                "value",
                () => { validationSummaryTagHelper.ValidationSummary = validationSummary; });

            Assert.Equal(expectedMessage, ex.Message);
        }
Esempio n. 17
0
        public void ValidationSummaryProperty_ThrowsWhenSetToInvalidValidationSummaryValue(
            ValidationSummary validationSummary)
        {
            // Arrange
            var generator = new TestableHtmlGenerator(new EmptyModelMetadataProvider());

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator);
            var validationTypeName         = typeof(ValidationSummary).FullName;
            var expectedMessage            =
                $@"The value of argument 'value' ({validationSummary}) is invalid for Enum type '{validationTypeName}'.
Parameter name: value";

            // Act & Assert
            var ex = Assert.Throws <ArgumentException>(
                "value",
                () => { validationSummaryTagHelper.ValidationSummary = validationSummary; });

            Assert.Equal(expectedMessage, ex.Message);
        }
Esempio n. 18
0
        public async Task ProcessAsync_DoesNothingIfValidationSummaryNone()
        {
            // Arrange
            var generator = new Mock <IHtmlGenerator>(MockBehavior.Strict);

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator.Object)
            {
                ValidationSummary = ValidationSummary.None,
            };

            var expectedPreContent  = "original pre-content";
            var expectedContent     = "original content";
            var expectedPostContent = "original post-content";
            var output = new TagHelperOutput(
                tagName: "div",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (_) => Task.FromResult <TagHelperContent>(new DefaultTagHelperContent()));

            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);

            var viewContext = CreateViewContext();

            validationSummaryTagHelper.ViewContext = viewContext;

            var context = new TagHelperContext(
                allAttributes: new ReadOnlyTagHelperAttributeList <IReadOnlyTagHelperAttribute>(
                    Enumerable.Empty <IReadOnlyTagHelperAttribute>()),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            // Act
            await validationSummaryTagHelper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
        }
        public async Task ProcessAsync_GeneratesValidationSummaryWhenNotNone_IgnoresCase(string validationSummary)
        {
            // Arrange
            var validationSummaryTagHelper = new ValidationSummaryTagHelper
            {
                ValidationSummaryValue = validationSummary
            };
            var output = new TagHelperOutput(
                "div",
                attributes: new Dictionary <string, string>(),
                content: "Content of validation message");
            var tagBuilder = new TagBuilder("span2")
            {
                InnerHtml = "New HTML"
            };

            var generator = new Mock <IHtmlGenerator>();

            generator
            .Setup(mock => mock.GenerateValidationSummary(
                       It.IsAny <ViewContext>(),
                       It.IsAny <bool>(),
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <object>()))
            .Returns(tagBuilder)
            .Verifiable();
            var viewContext = CreateViewContext();

            validationSummaryTagHelper.ViewContext = viewContext;
            validationSummaryTagHelper.Generator   = generator.Object;

            // Act
            await validationSummaryTagHelper.ProcessAsync(context : null, output : output);

            // Assert
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal("Content of validation messageNew HTML", output.Content);
            generator.Verify();
        }
        public async Task ProcessAsync_GeneratesExpectedOutput()
        {
            // Arrange
            var expectedTagName            = "not-div";
            var metadataProvider           = new DataAnnotationsModelMetadataProvider();
            var validationSummaryTagHelper = new ValidationSummaryTagHelper
            {
                ValidationSummaryValue = "All"
            };

            var tagHelperContext = new TagHelperContext(new Dictionary <string, object>());
            var output           = new TagHelperOutput(
                expectedTagName,
                attributes: new Dictionary <string, string>
            {
                { "class", "form-control" }
            },
                content: "Custom Content");

            var   htmlGenerator = new TestableHtmlGenerator(metadataProvider);
            Model model         = null;
            var   viewContext   = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider);

            validationSummaryTagHelper.ViewContext = viewContext;
            validationSummaryTagHelper.Generator   = htmlGenerator;

            // Act
            await validationSummaryTagHelper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal(2, output.Attributes.Count);
            var attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("class"));

            Assert.Equal("form-control validation-summary-valid", attribute.Value);
            attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("data-valmsg-summary"));
            Assert.Equal("true", attribute.Value);
            Assert.Equal("Custom Content<ul><li style=\"display:none\"></li>" + Environment.NewLine + "</ul>",
                         output.Content);
            Assert.Equal(expectedTagName, output.TagName);
        }
        public async Task ProcessAsync_ThrowsWhenInvalidValidationSummaryValue()
        {
            // Arrange
            var validationSummaryTagHelper = new ValidationSummaryTagHelper
            {
                ValidationSummaryValue = "Hello World"
            };
            var output = new TagHelperOutput(
                "div",
                attributes: new Dictionary <string, string>(),
                content: "Content of validation message");
            var expectedViewContext = CreateViewContext();
            var expectedMessage     = "Cannot parse 'validation-summary' value 'Hello World' for <div>. Acceptable values " +
                                      "are 'All', 'ModelOnly' and 'None'.";

            // Act
            var ex = await Assert.ThrowsAsync <InvalidOperationException>(
                () => validationSummaryTagHelper.ProcessAsync(context: null, output: output));

            // Assert
            Assert.Equal(expectedMessage, ex.Message);
        }
        public async Task ProcessAsync_CallsIntoGenerateValidationSummaryWithExpectedParameters(
            ValidationSummary validationSummary,
            bool expectedExcludePropertyErrors)
        {
            // Arrange
            var expectedViewContext = CreateViewContext();

            var generator = new Mock<IHtmlGenerator>();
            generator
                .Setup(mock => mock.GenerateValidationSummary(
                    expectedViewContext,
                    expectedExcludePropertyErrors,
                    null,   // message
                    null,   // headerTag
                    null))  // htmlAttributes
                .Returns(new TagBuilder("div"))
                .Verifiable();

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator.Object)
            {
                ValidationSummary = validationSummary,
            };

            var expectedPreContent = "original pre-content";
            var expectedContent = "original content";
            var expectedPostContent = "original post-content";
            var output = new TagHelperOutput(
                "div",
                attributes: new TagHelperAttributeList());
            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);

            
            validationSummaryTagHelper.ViewContext = expectedViewContext;

            // Act & Assert
            await validationSummaryTagHelper.ProcessAsync(context: null, output: output);

            generator.Verify();
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
        }
        public void ValidationSummaryProperty_ThrowsWhenSetToInvalidValidationSummaryValue(
            ValidationSummary validationSummary)
        {
            // Arrange
            var generator = new TestableHtmlGenerator(new EmptyModelMetadataProvider());

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator);
            var validationTypeName = typeof(ValidationSummary).FullName;
            var expectedMessage =
                $@"The value of argument 'value' ({validationSummary}) is invalid for Enum type '{validationTypeName}'.
Parameter name: value";

            // Act & Assert
            var ex = Assert.Throws<ArgumentException>(
                "value",
                () => { validationSummaryTagHelper.ValidationSummary = validationSummary; });
            Assert.Equal(expectedMessage, ex.Message);
        }
Esempio n. 24
0
        public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationSummary()
        {
            // Arrange
            var tagBuilder = new TagBuilder("span2");

            tagBuilder.InnerHtml.SetHtmlContent("New HTML");
            tagBuilder.Attributes.Add("data-foo", "bar");
            tagBuilder.Attributes.Add("data-hello", "world");
            tagBuilder.Attributes.Add("anything", "something");

            var generator = new Mock <IHtmlGenerator>(MockBehavior.Strict);

            generator
            .Setup(mock => mock.GenerateValidationSummary(
                       It.IsAny <ViewContext>(),
                       It.IsAny <bool>(),
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <object>()))
            .Returns(tagBuilder);

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator.Object)
            {
                ValidationSummary = ValidationSummary.ModelOnly,
            };

            var expectedPreContent = "original pre-content";
            var expectedContent    = "original content";
            var output             = new TagHelperOutput(
                tagName: "div",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (_) => Task.FromResult <TagHelperContent>(new DefaultTagHelperContent()));

            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent("Content of validation summary");

            var viewContext = CreateViewContext();

            validationSummaryTagHelper.ViewContext = viewContext;

            var context = new TagHelperContext(
                allAttributes: new ReadOnlyTagHelperAttributeList <IReadOnlyTagHelperAttribute>(
                    Enumerable.Empty <IReadOnlyTagHelperAttribute>()),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            // Act
            await validationSummaryTagHelper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("div", output.TagName);
            Assert.Equal(3, output.Attributes.Count);
            var attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-foo"));

            Assert.Equal("bar", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-hello"));
            Assert.Equal("world", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("anything"));
            Assert.Equal("something", attribute.Value);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal("Content of validation summaryNew HTML", output.PostContent.GetContent());
        }
        public async Task ProcessAsync_GeneratesValidationSummaryWhenNotNone(ValidationSummary validationSummary)
        {
            // Arrange
            var tagBuilder = new TagBuilder("span2");
            tagBuilder.InnerHtml.SetContentEncoded("New HTML");

            var generator = new Mock<IHtmlGenerator>();
            generator
                .Setup(mock => mock.GenerateValidationSummary(
                    It.IsAny<ViewContext>(),
                    It.IsAny<bool>(),
                    It.IsAny<string>(),
                    It.IsAny<string>(),
                    It.IsAny<object>()))
                .Returns(tagBuilder)
                .Verifiable();

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator.Object)
            {
                ValidationSummary = validationSummary,
            };

            var expectedPreContent = "original pre-content";
            var expectedContent = "original content";
            var output = new TagHelperOutput(
                "div",
                attributes: new TagHelperAttributeList());
            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent("Content of validation message");

            var viewContext = CreateViewContext();
            validationSummaryTagHelper.ViewContext = viewContext;

            // Act
            await validationSummaryTagHelper.ProcessAsync(context: null, output: output);

            // Assert
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal("Content of validation messageNew HTML", output.PostContent.GetContent());
            generator.Verify();
        }
        public async Task ProcessAsync_DoesNothingIfValidationSummaryNone()
        {
            // Arrange
            var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator.Object)
            {
                ValidationSummary = ValidationSummary.None,
            };

            var expectedPreContent = "original pre-content";
            var expectedContent = "original content";
            var expectedPostContent = "original post-content";
            var output = new TagHelperOutput(
                "div",
                attributes: new TagHelperAttributeList());
            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);

            var viewContext = CreateViewContext();
            validationSummaryTagHelper.ViewContext = viewContext;

            // Act
            await validationSummaryTagHelper.ProcessAsync(context: null, output: output);

            // Assert
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
        }
        public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationSummary()
        {
            // Arrange
            var tagBuilder = new TagBuilder("span2");
            tagBuilder.InnerHtml.SetContentEncoded("New HTML");
            tagBuilder.Attributes.Add("data-foo", "bar");
            tagBuilder.Attributes.Add("data-hello", "world");
            tagBuilder.Attributes.Add("anything", "something");

            var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
            generator
                .Setup(mock => mock.GenerateValidationSummary(
                    It.IsAny<ViewContext>(),
                    It.IsAny<bool>(),
                    It.IsAny<string>(),
                    It.IsAny<string>(),
                    It.IsAny<object>()))
                .Returns(tagBuilder);

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator.Object)
            {
                ValidationSummary = ValidationSummary.ModelOnly,
            };

            var expectedPreContent = "original pre-content";
            var expectedContent = "original content";
            var output = new TagHelperOutput(
                "div",
                attributes: new TagHelperAttributeList());
            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent("Content of validation summary");

            var viewContext = CreateViewContext();
            validationSummaryTagHelper.ViewContext = viewContext;

            // Act
            await validationSummaryTagHelper.ProcessAsync(context: null, output: output);

            // Assert
            Assert.Equal("div", output.TagName);
            Assert.Equal(3, output.Attributes.Count);
            var attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-foo"));
            Assert.Equal("bar", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-hello"));
            Assert.Equal("world", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("anything"));
            Assert.Equal("something", attribute.Value);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal("Content of validation summaryNew HTML", output.PostContent.GetContent());
        }
        public async Task ProcessAsync_CallsIntoGenerateValidationSummaryWithExpectedParameters(
            ValidationSummary validationSummary,
            bool expectedExcludePropertyErrors)
        {
            // Arrange
            var expectedViewContext = CreateViewContext();

            var generator = new Mock<IHtmlGenerator>();
            generator
                .Setup(mock => mock.GenerateValidationSummary(
                    expectedViewContext,
                    expectedExcludePropertyErrors,
                    null,   // message
                    null,   // headerTag
                    null))  // htmlAttributes
                .Returns(new TagBuilder("div"))
                .Verifiable();

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator.Object)
            {
                ValidationSummary = validationSummary,
            };

            var expectedPreContent = "original pre-content";
            var expectedContent = "original content";
            var expectedPostContent = "original post-content";
            var output = new TagHelperOutput(
                tagName: "div",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);

            validationSummaryTagHelper.ViewContext = expectedViewContext;

            var context = new TagHelperContext(
                allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
                    Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
                items: new Dictionary<object, object>(),
                uniqueId: "test");

            // Act & Assert
            await validationSummaryTagHelper.ProcessAsync(context, output);

            generator.Verify();
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
        }
        public async Task ProcessAsync_GeneratesValidationSummaryWhenNotNone(ValidationSummary validationSummary)
        {
            // Arrange
            var tagBuilder = new TagBuilder("span2");
            tagBuilder.InnerHtml.SetHtmlContent("New HTML");

            var generator = new Mock<IHtmlGenerator>();
            generator
                .Setup(mock => mock.GenerateValidationSummary(
                    It.IsAny<ViewContext>(),
                    It.IsAny<bool>(),
                    It.IsAny<string>(),
                    It.IsAny<string>(),
                    It.IsAny<object>()))
                .Returns(tagBuilder)
                .Verifiable();

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator.Object)
            {
                ValidationSummary = validationSummary,
            };

            var expectedPreContent = "original pre-content";
            var expectedContent = "original content";
            var output = new TagHelperOutput(
                tagName: "div",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent("Content of validation message");

            var viewContext = CreateViewContext();
            validationSummaryTagHelper.ViewContext = viewContext;

            var context = new TagHelperContext(
                allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
                    Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
                items: new Dictionary<object, object>(),
                uniqueId: "test");

            // Act
            await validationSummaryTagHelper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal("Content of validation messageNew HTML", output.PostContent.GetContent());
            generator.Verify();
        }
        public async Task ProcessAsync_DoesNothingIfValidationSummaryNone()
        {
            // Arrange
            var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator.Object)
            {
                ValidationSummary = ValidationSummary.None,
            };

            var expectedPreContent = "original pre-content";
            var expectedContent = "original content";
            var expectedPostContent = "original post-content";
            var output = new TagHelperOutput(
                tagName: "div",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);

            var viewContext = CreateViewContext();
            validationSummaryTagHelper.ViewContext = viewContext;

            var context = new TagHelperContext(
                allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
                    Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
                items: new Dictionary<object, object>(),
                uniqueId: "test");

            // Act
            await validationSummaryTagHelper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("div", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
        }
        public void ValidationSummaryProperty_ThrowsWhenSetToInvalidValidationSummaryValue(
            ValidationSummary validationSummary)
        {
            // Arrange
            var generator = new TestableHtmlGenerator(new EmptyModelMetadataProvider());

            var validationSummaryTagHelper = new ValidationSummaryTagHelper(generator);
            var expectedMessage = string.Format(
                @"The value of argument 'value' ({0}) is invalid for Enum type 'Microsoft.AspNet.Mvc.ValidationSummary'.
Parameter name: value",
                validationSummary);

            // Act & Assert
            var ex = Assert.Throws<ArgumentException>(
                "value",
                () => { validationSummaryTagHelper.ValidationSummary = validationSummary; });
            Assert.Equal(expectedMessage, ex.Message);
        }