public void DisplayNameHelpers_ReturnDisplayNameForProperty_IfNonNull(string displayName)
        {
            // Arrange
            var provider = new TestModelMetadataProvider();

            provider
            .ForProperty <DefaultTemplatesUtilities.ObjectTemplateModel>("Property1")
            .DisplayDetails(dd => dd.DisplayName = () => displayName);

            var helper           = DefaultTemplatesUtilities.GetHtmlHelper(provider: provider);
            var enumerableHelper = DefaultTemplatesUtilities.GetHtmlHelperForEnumerable(provider: provider);

            // Act
            var displayNameResult              = helper.DisplayName("Property1");
            var displayNameForResult           = helper.DisplayNameFor(m => m.Property1);
            var displayNameForEnumerableResult = enumerableHelper.DisplayNameFor(m => m.Property1);

            // Assert
            Assert.Equal(displayName, displayNameResult);
            Assert.Equal(displayName, displayNameForResult);
            Assert.Equal(displayName, displayNameForEnumerableResult);
        }
Exemplo n.º 2
0
        public void CheckBoxNotInTemplate_GetsValueFromPropertyOfViewDataEntry()
        {
            // Arrange
            var expected =
                @"<input checked=""HtmlEncode[[checked]]"" id=""HtmlEncode[[Prefix_Property1]]"" " +
                @"name=""HtmlEncode[[Prefix.Property1]]"" type=""HtmlEncode[[checkbox]]"" value=""HtmlEncode[[true]]"" />" +
                @"<input name=""HtmlEncode[[Prefix.Property1]]"" type=""HtmlEncode[[hidden]]"" value=""HtmlEncode[[false]]"" />";
            var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetTestModelViewData());

            helper.ViewContext.ClientValidationEnabled = false;
            helper.ViewData.Remove(nameof(TestModel.Property1));
            helper.ViewData["Prefix"] = new TestModel {
                Property1 = true
            };
            helper.ViewData.Model = new TestModel();

            // Act
            var html = helper.CheckBox("Prefix.Property1", isChecked: null, htmlAttributes: null);

            // Assert
            Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(html));
        }
Exemplo n.º 3
0
        public void CheckBoxInTemplate_GetsModelValue_IfModelStateAndViewDataEmpty()
        {
            // Arrange
            var expected =
                @"<input checked=""HtmlEncode[[checked]]"" id=""HtmlEncode[[Prefix_Property1]]"" " +
                @"name=""HtmlEncode[[Prefix.Property1]]"" type=""HtmlEncode[[checkbox]]"" value=""HtmlEncode[[true]]"" />" +
                @"<input name=""HtmlEncode[[Prefix.Property1]]"" type=""HtmlEncode[[hidden]]"" value=""HtmlEncode[[false]]"" />";
            var metadataProvider = new EmptyModelMetadataProvider();
            var helper           = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary <TestModel>(metadataProvider));

            helper.ViewContext.ClientValidationEnabled = false;
            helper.ViewData.Model = new TestModel {
                Property1 = true
            };
            helper.ViewData.TemplateInfo.HtmlFieldPrefix = "Prefix";

            // Act
            var html = helper.CheckBox("Property1", isChecked: null, htmlAttributes: null);

            // Assert
            Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(html));
        }
Exemplo n.º 4
0
        public void HiddenForInTemplate_UsesModelStateValueForComplexExpressions(
            Expression <Func <HiddenModel, string> > expression,
            string expected)
        {
            // Arrange
            var viewData = GetViewDataWithNullModelAndNonNullViewData();

            viewData.ModelState.Add("pre.Property3[key]", GetModelStateEntry("Prop3Val"));
            viewData.ModelState.Add("pre.Property4.Property5", GetModelStateEntry("Prop5Val"));
            viewData.ModelState.Add("pre.Property4.Property6[0]", GetModelStateEntry("Prop6Val"));

            var helper = DefaultTemplatesUtilities.GetHtmlHelper(viewData);

            viewData.TemplateInfo.HtmlFieldPrefix = "pre";
            var attributes = new { data_val = "true", value = "attr-val" };

            // Act
            var result = helper.HiddenFor(expression, attributes);

            // Assert
            Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result));
        }
Exemplo n.º 5
0
        public void HiddenForInTemplate_UsesPrefixWhenLookingUpModelStateValues()
        {
            // Arrange
            var expected = @"<input id=""HtmlEncode[[MyPrefix$Property1]]"" name=""HtmlEncode[[MyPrefix.Property1]]"" type=""HtmlEncode[[hidden]]"" " +
                           @"value=""HtmlEncode[[modelstate-with-prefix]]"" />";
            var helper = DefaultTemplatesUtilities.GetHtmlHelper(
                GetViewDataWithModelStateAndModelAndViewDataValues(),
                "$");

            helper.ViewData.Model.Property1 = "propValue";
            helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
            helper.ViewData.ModelState.Clear();
            helper.ViewData.ModelState.Add("Property1", GetModelStateEntry("modelstate-without-prefix"));
            helper.ViewData.ModelState.Add("MyPrefix.Property1", GetModelStateEntry("modelstate-with-prefix"));
            helper.ViewData.ModelState.Add("MyPrefix$Property1", GetModelStateEntry("modelstate-with-iddotreplacement"));

            // Act
            var result = helper.HiddenFor(m => m.Property1, htmlAttributes: null);

            // Assert
            Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result));
        }
Exemplo n.º 6
0
        public void HiddenInTemplate_UsesPrefixNameToLookupPropertyValueInViewData()
        {
            // Arrange
            var expected = @"<input id=""HtmlEncode[[MyPrefix$Property1]]"" name=""HtmlEncode[[MyPrefix.Property1]]"" type=""HtmlEncode[[hidden]]"" " +
                           @"value=""HtmlEncode[[vdd-with-prefix]]"" />";
            var helper = DefaultTemplatesUtilities.GetHtmlHelper(
                GetViewDataWithModelStateAndModelAndViewDataValues(),
                idAttributeDotReplacement: "$");

            helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
            helper.ViewData.ModelState.Clear();
            helper.ViewData.Clear();
            helper.ViewData.Add("Property1", "vdd-without-prefix");
            helper.ViewData.Add("MyPrefix.Property1", "vdd-with-prefix");
            helper.ViewData.Add("MyPrefix$Property1", "vdd-with-iddotreplacement");

            // Act
            var result = helper.Hidden("Property1", value: null, htmlAttributes: null);

            // Assert
            Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result));
        }
Exemplo n.º 7
0
        public void CheckBoxInTemplate_WithEmptyExpression_GeneratesExpectedValue()
        {
            // Arrange
            // Mono issue - https://github.com/aspnet/External/issues/19
            var expected = PlatformNormalizer.NormalizeContent(
                @"<input data-val=""HtmlEncode[[true]]"" data-val-required=""HtmlEncode[[The Boolean field is required.]]"" " +
                @"id=""HtmlEncode[[MyPrefix]]"" name=""HtmlEncode[[MyPrefix]]"" Property3=""HtmlEncode[[Property3Value]]"" " +
                @"type=""HtmlEncode[[checkbox]]"" value=""HtmlEncode[[true]]"" /><input name=""HtmlEncode[[MyPrefix]]"" type=""HtmlEncode[[hidden]]"" " +
                @"value=""HtmlEncode[[false]]"" />");
            var helper     = DefaultTemplatesUtilities.GetHtmlHelper(model: false);
            var attributes = new Dictionary <string, object> {
                { "Property3", "Property3Value" }
            };

            helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";

            // Act
            var html = helper.CheckBox(string.Empty, isChecked: false, htmlAttributes: attributes);

            // Assert
            Assert.Equal(expected, html.ToString());
        }
Exemplo n.º 8
0
        public void CheckBoxForInTemplate_GeneratesExpectedValue()
        {
            // Arrange
            // Mono issue - https://github.com/aspnet/External/issues/19
            var expected = PlatformNormalizer.NormalizeContent(
                @"<input data-val=""HtmlEncode[[true]]"" data-val-required=""HtmlEncode[[The Property1 field is required.]]"" " +
                @"id=""HtmlEncode[[MyPrefix_Property1]]"" name=""HtmlEncode[[MyPrefix.Property1]]"" Property3=""HtmlEncode[[PropValue]]"" " +
                @"type=""HtmlEncode[[checkbox]]"" value=""HtmlEncode[[true]]"" /><input name=""HtmlEncode[[MyPrefix.Property1]]"" type=""HtmlEncode[[hidden]]"" " +
                @"value=""HtmlEncode[[false]]"" />");
            var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetTestModelViewData());

            helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix";
            var attributes = new Dictionary <string, object> {
                { "Property3", "PropValue" }
            };

            // Act
            var html = helper.CheckBoxFor(m => m.Property1, attributes);

            // Assert
            Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(html));
        }
Exemplo n.º 9
0
        public void CheckBoxForWithNonNullContainer_UsesPropertyValue(bool value, string expectedChecked)
        {
            // Arrange
            var expected = @"<input {0}id=""Property1"" name=""Property1"" type=""checkbox"" value=""true"" />" +
                           @"<input name=""Property1"" type=""hidden"" value=""false"" />";

            expected = string.Format(expected, expectedChecked);

            var viewData = GetTestModelViewData();

            viewData.Model = new TestModel
            {
                Property1 = value,
            };

            var helper = DefaultTemplatesUtilities.GetHtmlHelper(viewData);

            // Act
            var html = helper.CheckBoxFor(m => m.Property1);

            // Assert
            Assert.Equal(expected, html.ToString());
        }
Exemplo n.º 10
0
        public void CheckBoxFor_UsesModelStateAttemptedValue(string attemptedValue, string expectedChecked)
        {
            // Arrange
            var expected =
                @"<input {0}data-val=""HtmlEncode[[true]]"" data-val-required=""HtmlEncode[[The Property1 field is required.]]"" " +
                @"id=""HtmlEncode[[Property1]]"" name=""HtmlEncode[[Property1]]"" type=""HtmlEncode[[checkbox]]"" value=""HtmlEncode[[true]]"" />" +
                @"<input name=""HtmlEncode[[Property1]]"" type=""HtmlEncode[[hidden]]"" value=""HtmlEncode[[false]]"" />";

            expected = string.Format(expected, expectedChecked);

            var viewData            = GetTestModelViewData();
            var helper              = DefaultTemplatesUtilities.GetHtmlHelper(viewData);
            var valueProviderResult =
                new ValueProviderResult(attemptedValue, attemptedValue, CultureInfo.InvariantCulture);

            viewData.ModelState.SetModelValue("Property1", valueProviderResult);

            // Act
            var html = helper.CheckBoxFor(m => m.Property1);

            // Assert
            Assert.Equal(expected, html.ToString());
        }
Exemplo n.º 11
0
        public void DisplayTextFor_ReturnsSimpleDisplayText_IfSetAndValueNonNull()
        {
            // Arrange
            var model = new OverriddenToStringModel("Ignored text")
            {
                SimpleDisplay = "Simple display text",
            };

            var provider = new TestModelMetadataProvider();

            provider.ForType <OverriddenToStringModel>().DisplayDetails(dd =>
            {
                dd.SimpleDisplayProperty = nameof(OverriddenToStringModel.SimpleDisplay);
            });

            var helper = DefaultTemplatesUtilities.GetHtmlHelper <OverriddenToStringModel>(model: model, provider: provider);

            // Act
            var result = helper.DisplayTextFor(m => m);

            // Assert
            Assert.Equal("Simple display text", result);
        }
Exemplo n.º 12
0
        public void ValidationSummary_OneInvalidProperty_ReturnsExpectedDiv(
            bool excludePropertyErrors,
            bool clientValidationEnabled,
            string ignored,
            string expected)
        {
            // Arrange
            var model = new ValidationModel();
            var html  = DefaultTemplatesUtilities.GetHtmlHelper(model);

            html.ViewContext.ClientValidationEnabled = clientValidationEnabled;
            html.ViewData.ModelState.AddModelError("Property1", "This is my validation message");

            // Act
            var result = html.ValidationSummary(
                excludePropertyErrors,
                message: null,
                htmlAttributes: null,
                tag: null);

            // Assert
            Assert.Equal(expected, result.ToString());
        }
Exemplo n.º 13
0
        public void BeginFormWithActionNameContollerNameMethodAndHtmlAttributesParameters_CallsHtmlGeneratorWithExpectedValues(
            string actionName,
            string controllerName,
            FormMethod method,
            object htmlAttributes)
        {
            // Arrange
            var tagBuilder    = new TagBuilder(tagName: "form");
            var htmlGenerator = new Mock <IHtmlGenerator>(MockBehavior.Strict);
            var htmlHelper    = DefaultTemplatesUtilities.GetHtmlHelper(htmlGenerator.Object);

            htmlGenerator
            .Setup(realHelper => realHelper.GenerateForm(
                       htmlHelper.ViewContext,
                       actionName,
                       controllerName,
                       null, // routeValues
                       method.ToString().ToLowerInvariant(),
                       htmlAttributes))
            .Returns(tagBuilder)
            .Verifiable();

            // Guards
            Assert.NotNull(htmlHelper.ViewContext);
            var writer  = Assert.IsAssignableFrom <StringWriter>(htmlHelper.ViewContext.Writer);
            var builder = writer.GetStringBuilder();

            Assert.NotNull(builder);

            // Act
            var mvcForm = htmlHelper.BeginForm(actionName, controllerName, method, htmlAttributes);

            // Assert
            Assert.NotNull(mvcForm);
            Assert.Equal("<form>", builder.ToString());
            htmlGenerator.Verify();
        }
Exemplo n.º 14
0
        public void DisplayText_ReturnsPropertyValue_IfNameFound()
        {
            // Arrange
            var model = new OverriddenToStringModel("Ignored text")
            {
                Name          = "Property value",
                SimpleDisplay = "Simple display text",
            };

            var provider = new TestModelMetadataProvider();

            provider.ForType <OverriddenToStringModel>().DisplayDetails(dd =>
            {
                dd.SimpleDisplayProperty = nameof(OverriddenToStringModel.SimpleDisplay);
            });

            var helper = DefaultTemplatesUtilities.GetHtmlHelper <OverriddenToStringModel>(model: model, provider: provider);

            // Act
            var result = helper.DisplayText("Name");

            // Assert
            Assert.Equal("Property value", result);
        }
Exemplo n.º 15
0
        public void DisplayNameHelpers_ReturnDisplayName_IfNonNull(string displayName)
        {
            // Arrange
            var provider = new TestModelMetadataProvider();

            provider
            .ForType <DefaultTemplatesUtilities.ObjectTemplateModel>()
            .DisplayDetails(dd => dd.DisplayName = () => displayName);

            var helper           = DefaultTemplatesUtilities.GetHtmlHelper(provider: provider);
            var enumerableHelper = DefaultTemplatesUtilities.GetHtmlHelperForEnumerable(provider: provider);

            // Act
            var displayNameResult              = helper.DisplayName(expression: string.Empty);
            var displayNameForResult           = helper.DisplayNameFor(m => m);
            var displayNameForEnumerableResult = enumerableHelper.DisplayNameFor((DefaultTemplatesUtilities.ObjectTemplateModel m) => m);
            var displayNameForModelResult      = helper.DisplayNameForModel();

            // Assert
            Assert.Equal(displayName, displayNameResult);
            Assert.Equal(displayName, displayNameForResult);
            Assert.Equal(displayName, displayNameForEnumerableResult);
            Assert.Equal(displayName, displayNameForModelResult);
        }
        public void ValidationSummary_InvalidModelWithPrefix_ReturnsExpectedDiv(
            bool excludePropertyErrors,
            bool clientValidationEnabled,
            string expected,
            string ignored)
        {
            // Arrange
            var model = new ValidationModel();
            var html  = DefaultTemplatesUtilities.GetHtmlHelper(model);

            html.ViewContext.ClientValidationEnabled   = clientValidationEnabled;
            html.ViewData.TemplateInfo.HtmlFieldPrefix = "this.is.my.prefix";
            html.ViewData.ModelState.AddModelError("this.is.my.prefix", "This is my validation message");

            // Act
            var result = html.ValidationSummary(
                excludePropertyErrors,
                message: null,
                htmlAttributes: null,
                tag: null);

            // Assert
            Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result));
        }
Exemplo n.º 17
0
        public void CheckBoxFor_UsesModelStateAttemptedValue(string attemptedValue, string expectedChecked)
        {
            // Arrange
            // Mono issue - https://github.com/aspnet/External/issues/19
            var expected = PlatformNormalizer.NormalizeContent(
                @"<input {0}data-val=""HtmlEncode[[true]]"" data-val-required=""HtmlEncode[[The Property1 field is required.]]"" " +
                @"id=""HtmlEncode[[Property1]]"" name=""HtmlEncode[[Property1]]"" type=""HtmlEncode[[checkbox]]"" value=""HtmlEncode[[true]]"" />" +
                @"<input name=""HtmlEncode[[Property1]]"" type=""HtmlEncode[[hidden]]"" value=""HtmlEncode[[false]]"" />");

            expected = string.Format(expected, expectedChecked);

            var viewData            = GetTestModelViewData();
            var helper              = DefaultTemplatesUtilities.GetHtmlHelper(viewData);
            var valueProviderResult =
                new ValueProviderResult(attemptedValue, attemptedValue, CultureInfo.InvariantCulture);

            viewData.ModelState.SetModelValue("Property1", valueProviderResult);

            // Act
            var html = helper.CheckBoxFor(m => m.Property1, htmlAttributes: null);

            // Assert
            Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(html));
        }