public async Task ProcessAsync_NoContentAndNoAspForThrowsInvalidOperationException()
        {
            // Arrange
            var errorSummaryContext = new ErrorSummaryContext();

            var context = new TagHelperContext(
                tagName: "govuk-error-summary-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(ErrorSummaryContext), errorSummaryContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-error-summary-item",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("An error message");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            output.TagMode = TagMode.SelfClosing;

            var tagHelper = new ErrorSummaryItemTagHelper(new DefaultGovUkHtmlGenerator(), new DefaultModelHelper());

            // Act & Assert
            var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => tagHelper.ProcessAsync(context, output));

            Assert.Equal("Content is required when the 'asp-for' attribute is not specified.", ex.Message);
        }
        public async Task ProcessAsync_AddsDescriptionToContext()
        {
            // Arrange
            var errorSummaryContext = new ErrorSummaryContext();

            var context = new TagHelperContext(
                tagName: "govuk-error-summary-description",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(ErrorSummaryContext), errorSummaryContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-error-summary-description",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("Some description");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new ErrorSummaryDescriptionTagHelper();

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

            // Assert
            Assert.Equal("Some description", errorSummaryContext.Description?.content.AsString());
        }
        public async Task ProcessAsync_AlreadyHaveDescriptionThrowsInvalidOperationException()
        {
            // Arrange
            var errorSummaryContext = new ErrorSummaryContext();

            errorSummaryContext.TrySetDescription(attributes: null, new HtmlString("Existing description"));

            var context = new TagHelperContext(
                tagName: "govuk-error-summary-description",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(ErrorSummaryContext), errorSummaryContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-error-summary-description",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("Some description");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new ErrorSummaryDescriptionTagHelper();

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

            Assert.Equal("Cannot render <govuk-error-summary-description> here.", ex.Message);
        }
        public async Task ProcessAsync_AspForSpecifiedGeneratesExpectedContent()
        {
            // Arrange
            var errorSummaryContext = new ErrorSummaryContext();

            var context = new TagHelperContext(
                tagName: "govuk-error-summary-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(ErrorSummaryContext), errorSummaryContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-error-summary-item",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            output.TagMode = TagMode.SelfClosing;

            var modelHelperMock = new Mock <IModelHelper>();

            modelHelperMock
            .Setup(mock => mock.GetFullHtmlFieldName(
                       /*viewContext: */ It.IsAny <ViewContext>(),
                       /*expression: */ It.IsAny <string>()))
            .Returns("Foo");

            modelHelperMock
            .Setup(mock => mock.GetValidationMessage(
                       /*viewContext: */ It.IsAny <ViewContext>(),
                       /*modelExplorer: */ It.IsAny <ModelExplorer>(),
                       /*expression: */ It.IsAny <string>()))
            .Returns("Generated error");

            var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), "Foo");

            var tagHelper = new ErrorSummaryItemTagHelper(new DefaultGovUkHtmlGenerator(), modelHelperMock.Object)
            {
                AspFor      = new ModelExpression("Foo", modelExplorer),
                ViewContext = new ViewContext()
            };

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

            // Assert
            Assert.Equal(1, errorSummaryContext.Items.Count);

            var firstItem = errorSummaryContext.Items.First();

            Assert.Equal("<a href=\"#Foo\">Generated error</a>", firstItem.Content.AsString());
        }
Esempio n. 5
0
        public async Task ProcessAsync_BothHrefAndAspForSpecified_UsesHrefAttribute()
        {
            // Arrange
            var errorSummaryContext = new ErrorSummaryContext();

            var context = new TagHelperContext(
                tagName: "govuk-error-summary-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(ErrorSummaryContext), errorSummaryContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-error-summary-item",
                attributes: new TagHelperAttributeList()
            {
                { "href", "#SomeHref" }
            },
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            output.TagMode = TagMode.SelfClosing;

            var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), new Model())
                                .GetExplorerForProperty(nameof(Model.Field));

            var viewContext = new ViewContext();

            var options = Options.Create(new GovUkFrontendAspNetCoreOptions());
            var dateInputParseErrorsProvider = new DateInputParseErrorsProvider();

            viewContext.ModelState.AddModelError(nameof(Model.Field), "ModelState error message");

            var tagHelper = new ErrorSummaryItemTagHelper(options, dateInputParseErrorsProvider)
            {
                AspFor      = new ModelExpression(nameof(Model.Field), modelExplorer),
                ViewContext = viewContext
            };

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

            // Assert
            Assert.Collection(
                errorSummaryContext.Items,
                item =>
            {
                Assert.Equal("#SomeHref", item.Href);
            });
        }
Esempio n. 6
0
        public void SetTitle_SetsTitleOnContext()
        {
            // Arrange
            var context = new ErrorSummaryContext();

            // Act
            context.SetTitle(attributes: null, content: new HtmlString("Title"));

            // Assert
            Assert.Equal("Title", context.Title?.Content?.RenderToString());
        }
Esempio n. 7
0
        public void SetDescription_SetsDescriptionOnContext()
        {
            // Arrange
            var context = new ErrorSummaryContext();

            // Act
            context.SetDescription(attributes: null, content: new HtmlString("Description"));

            // Assert
            Assert.Equal("Description", context.Description?.Content?.RenderToString());
        }
Esempio n. 8
0
        public void SetTitle_AlreadyGotTitle_ThrowsInvalidOperationException()
        {
            // Arrange
            var context = new ErrorSummaryContext();

            context.SetTitle(attributes: null, content: new HtmlString("Existing title"));

            // Act
            var ex = Record.Exception(() => context.SetTitle(attributes: null, content: new HtmlString("Title")));

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("Only one <govuk-error-summary-title> element is permitted within each <govuk-error-summary>.", ex.Message);
        }
Esempio n. 9
0
        public async Task ProcessAsync_HrefAttributeSpecifiedOrDerived_SetsHrefOnItem()
        {
            // Arrange
            var errorSummaryContext = new ErrorSummaryContext();

            var context = new TagHelperContext(
                tagName: "govuk-error-summary-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(ErrorSummaryContext), errorSummaryContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-error-summary-item",
                attributes: new TagHelperAttributeList()
            {
                { "href", "#TheField" }
            },
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("Error message");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var options = Options.Create(new GovUkFrontendAspNetCoreOptions());
            var dateInputParseErrorsProvider = new DateInputParseErrorsProvider();

            var tagHelper = new ErrorSummaryItemTagHelper(options, dateInputParseErrorsProvider);

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

            // Assert
            Assert.Collection(
                errorSummaryContext.Items,
                item =>
            {
                Assert.Equal("#TheField", item.Href);
            });
        }
        public async Task ProcessAsync_LinkAttributesSpecifiedGeneratesExpectedContent()
        {
            // Arrange
            var errorSummaryContext = new ErrorSummaryContext();

            var context = new TagHelperContext(
                tagName: "govuk-error-summary-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(ErrorSummaryContext), errorSummaryContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-error-summary-item",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("An error message");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new ErrorSummaryItemTagHelper(new DefaultGovUkHtmlGenerator(), new DefaultModelHelper())
            {
                For = "field"
            };

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

            // Assert
            Assert.Equal(1, errorSummaryContext.Items.Count);

            var firstItem = errorSummaryContext.Items.First();

            Assert.Equal("<a href=\"#field\">An error message</a>", firstItem.Content.AsString());
        }
Esempio n. 11
0
        public void AddItem_AddsItemToItems()
        {
            // Arrange
            var context = new ErrorSummaryContext();

            var item = new ErrorSummaryItem()
            {
                Content = new HtmlString("An error message"),
                Href    = "#TheField"
            };

            // Act
            context.AddItem(item);

            // Assert
            Assert.Collection(
                context.Items,
                item =>
            {
                Assert.Equal("An error message", item.Content.RenderToString());
                Assert.Equal("#TheField", item.Href);
            });
        }
Esempio n. 12
0
        public async Task ProcessAsync_NoContentOrAspNetFor_ThrowsInvalidOperationException()
        {
            // Arrange
            var errorSummaryContext = new ErrorSummaryContext();

            var context = new TagHelperContext(
                tagName: "govuk-error-summary-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(ErrorSummaryContext), errorSummaryContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-error-summary-item",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            output.TagMode = TagMode.SelfClosing;

            var options = Options.Create(new GovUkFrontendAspNetCoreOptions());
            var dateInputParseErrorsProvider = new DateInputParseErrorsProvider();

            var tagHelper = new ErrorSummaryItemTagHelper(options, dateInputParseErrorsProvider);

            // Act
            var ex = await Record.ExceptionAsync(() => tagHelper.ProcessAsync(context, output));

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("Content is required when the 'asp-for' attribute is not specified.", ex.Message);
        }