Ejemplo n.º 1
0
        public void LabelHelpers_ReturnEmptyForProperty_IfDisplayNameEmpty()
        {
            // Arrange
            var metadataHelper = new MetadataHelper();  // All properties will use the same metadata.

            metadataHelper.Metadata
            .Setup(metadata => metadata.DisplayName)
            .Returns(string.Empty);
            var helper = DefaultTemplatesUtilities.GetHtmlHelper(metadataHelper.MetadataProvider.Object);

            // Act
            var labelResult         = helper.Label(expression: string.Empty);
            var labelNullResult     = helper.Label(expression: null); // null is another alias for current model
            var labelForResult      = helper.LabelFor(m => m);
            var labelForModelResult = helper.LabelForModel();

            // Assert
            Assert.Empty(labelResult.ToString());
            Assert.Empty(labelNullResult.ToString());
            Assert.Empty(labelForResult.ToString());
            Assert.Empty(labelForModelResult.ToString());
        }
        public void ObjectTemplateDisplaysNullDisplayTextWithNullModelAndTemplateDepthGreaterThanOne()
        {
            // Arrange
            var provider = new TestModelMetadataProvider();

            provider.ForType <DefaultTemplatesUtilities.ObjectTemplateModel>().DisplayDetails(dd =>
            {
                dd.NullDisplayText       = "Null Display Text";
                dd.SimpleDisplayProperty = "Property1";
            });

            var html = DefaultTemplatesUtilities.GetHtmlHelper(provider: provider);

            html.ViewData.TemplateInfo.AddVisited("foo");
            html.ViewData.TemplateInfo.AddVisited("bar");

            // Act
            var result = DefaultEditorTemplates.ObjectTemplate(html);

            // Assert
            Assert.Equal(html.ViewData.ModelMetadata.NullDisplayText, result);
        }
Ejemplo n.º 3
0
        public void ObjectTemplateDisplaysSimplePropertiesOnObjectByDefault()
        {
            var expected =
                "<div class=\"display-label\">Property1</div>" + Environment.NewLine
                + "<div class=\"display-field\">Model = p1, ModelType = System.String, PropertyName = Property1," +
                " SimpleDisplayText = p1</div>" + Environment.NewLine
                + "<div class=\"display-label\">Property2</div>" + Environment.NewLine
                + "<div class=\"display-field\">Model = (null), ModelType = System.String, PropertyName = Property2," +
                " SimpleDisplayText = (null)</div>" + Environment.NewLine;

            // Arrange
            var model = new DefaultTemplatesUtilities.ObjectTemplateModel {
                Property1 = "p1", Property2 = null
            };
            var html = DefaultTemplatesUtilities.GetHtmlHelper(model);

            // Act
            var result = DefaultDisplayTemplates.ObjectTemplate(html);

            // Assert
            Assert.Equal(expected, result);
        }
Ejemplo n.º 4
0
        public void HiddenInputTemplate_HonoursHideSurroundingHtml()
        {
            // Arrange
            var expected = "<input id=\"FieldPrefix\" name=\"FieldPrefix\" type=\"hidden\" value=\"Model string\" />";

            var model    = "Model string";
            var html     = DefaultTemplatesUtilities.GetHtmlHelper(model);
            var viewData = html.ViewData;

            viewData.ModelMetadata.HideSurroundingHtml = true;

            var templateInfo = viewData.TemplateInfo;

            templateInfo.HtmlFieldPrefix     = "FieldPrefix";
            templateInfo.FormattedModelValue = "Formatted string";

            // Act
            var result = DefaultEditorTemplates.HiddenInputTemplate(html);

            // Assert
            Assert.Equal(expected, result);
        }
        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);
        }
        public void EditorFor_CallsExpectedHtmlHelper_TemplateHint(string templateName, string expectedResult)
        {
            // Arrange
            var model = new DefaultTemplatesUtilities.ObjectTemplateModel {
                Property1 = "True"
            };
            var viewEngine = new Mock <ICompositeViewEngine>();

            viewEngine
            .Setup(v => v.FindPartialView(It.IsAny <ActionContext>(), It.IsAny <string>()))
            .Returns(ViewEngineResult.NotFound("", Enumerable.Empty <string>()));

            var provider = new TestModelMetadataProvider();

            provider.ForProperty <DefaultTemplatesUtilities.ObjectTemplateModel>("Property1").DisplayDetails(dd =>
            {
                dd.TemplateHint = templateName;
            });

            var helper = DefaultTemplatesUtilities.GetHtmlHelper(
                model,
                null,
                viewEngine.Object,
                provider,
                innerHelper => new StubbyHtmlHelper(innerHelper));

            // TemplateBuilder sets FormattedModelValue before calling TemplateRenderer and it's used in most templates.
            helper.ViewData.TemplateInfo.FormattedModelValue = "Formatted string";

            // Act
            var result = helper.EditorFor(
                anotherModel => anotherModel.Property1,
                templateName,
                htmlFieldName: null,
                additionalViewData: null);

            // Assert
            Assert.Equal(expectedResult, result.ToString());
        }
Ejemplo n.º 7
0
        public void ObjectTemplate_IgnoresPropertiesWith_ScaffoldColumnFalse()
        {
            // Arrange
            var expected =
                @"<div class=""display-label"">Property1</div>
<div class=""display-field""></div>
<div class=""display-label"">Property3</div>
<div class=""display-field""></div>
";
            var model      = new DefaultTemplatesUtilities.ObjectWithScaffoldColumn();
            var viewEngine = new Mock <ICompositeViewEngine>();

            viewEngine.Setup(v => v.FindPartialView(It.IsAny <ActionContext>(), It.IsAny <string>()))
            .Returns(ViewEngineResult.NotFound("", Enumerable.Empty <string>()));
            var htmlHelper = DefaultTemplatesUtilities.GetHtmlHelper(model, viewEngine.Object);

            // Act
            var result = DefaultDisplayTemplates.ObjectTemplate(htmlHelper);

            // Assert
            Assert.Equal(expected, result);
        }
Ejemplo n.º 8
0
        public void LabelFor_ConsultsMetadataProviderForMetadataAboutProperty()
        {
            // Arrange
            var modelType      = typeof(DefaultTemplatesUtilities.ObjectTemplateModel);
            var metadataHelper = new MetadataHelper();

            metadataHelper.MetadataProvider
            .Setup(p => p.GetMetadataForProperty(It.IsAny <Func <object> >(), modelType, "Property1"))
            .Returns(metadataHelper.Metadata.Object)
            .Verifiable();

            var helper = DefaultTemplatesUtilities.GetHtmlHelper(metadataHelper.MetadataProvider.Object);

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

            // Assert
            metadataHelper.MetadataProvider.Verify();

            // LabelFor() falls back to expression name when DisplayName and PropertyName are null.
            Assert.Equal("<label for=\"Property1\">Property1</label>", result.ToString());
        }
Ejemplo n.º 9
0
        public void LabelHelpers_DisplayMetadataPropertyNameForProperty_IfOverridden(string propertyName)
        {
            // Arrange
            var metadataHelper = new MetadataHelper();
            var metadata       = new ModelMetadata(
                metadataHelper.MetadataProvider.Object,
                containerType: null,
                modelAccessor: null,
                modelType: typeof(object),
                propertyName: propertyName);

            metadataHelper.MetadataProvider
            .Setup(provider => provider.GetMetadataForProperty(It.IsAny <Func <object> >(), It.IsAny <Type>(), "Property1"))
            .Returns(metadata);

            var helper = DefaultTemplatesUtilities.GetHtmlHelper(metadataHelper.MetadataProvider.Object);

            // Act
            var labelForResult = helper.LabelFor(m => m.Property1);

            // Assert
            Assert.Equal("<label for=\"Property1\">" + propertyName + "</label>", labelForResult.ToString());
        }
        public void DisplayNameHelpers_ReturnDisplayName_IfNonNull(string displayName)
        {
            // Arrange
            var helper = DefaultTemplatesUtilities.GetHtmlHelper();

            helper.ViewData.ModelMetadata.DisplayName = displayName;
            var enumerableHelper =
                DefaultTemplatesUtilities.GetHtmlHelper <IEnumerable <DefaultTemplatesUtilities.ObjectTemplateModel> >(model: null);

            enumerableHelper.ViewData.ModelMetadata.DisplayName = displayName;

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

            // Assert
            Assert.Equal(displayName, displayNameResult);
            Assert.Equal(displayName, displayNameForResult);
            Assert.Equal(displayName, displayNameForEnumerableResult);
            Assert.Equal(displayName, displayNameForModelResult);
        }
Ejemplo n.º 11
0
        public void LabelHelpers_ReturnEmptyForModel_IfDisplayNameEmpty()
        {
            // Arrange
            var provider = new TestModelMetadataProvider();

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

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

            // Act
            var labelResult         = helper.Label(expression: string.Empty);
            var labelNullResult     = helper.Label(expression: null); // null is another alias for current model
            var labelForResult      = helper.LabelFor(m => m);
            var labelForModelResult = helper.LabelForModel();

            // Assert
            Assert.Empty(labelResult.ToString());
            Assert.Empty(labelNullResult.ToString());
            Assert.Empty(labelForResult.ToString());
            Assert.Empty(labelForModelResult.ToString());
        }
        public void DisplayNameHelpers_ReturnDisplayNameForProperty_IfNonNull(string displayName)
        {
            // Arrange
            var metadataHelper = new MetadataHelper();  // All properties will use the same metadata.
            metadataHelper.Metadata
                .Setup(metadata => metadata.DisplayName)
                .Returns(displayName);
            var helper = DefaultTemplatesUtilities.GetHtmlHelper(metadataHelper.MetadataProvider.Object);
            var enumerableHelper =
                DefaultTemplatesUtilities.GetHtmlHelper<IEnumerable<DefaultTemplatesUtilities.ObjectTemplateModel>>(
                    model: null,
                    provider: metadataHelper.MetadataProvider.Object);

            // 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);
        }
Ejemplo n.º 13
0
        public void IdAndNameHelpers_ReturnPrefixForModel(string prefix)
        {
            // Arrange
            var helper = DefaultTemplatesUtilities.GetHtmlHelper();

            helper.ViewData.TemplateInfo.HtmlFieldPrefix = prefix;

            // Act
            var idResult           = helper.Id(name: string.Empty);
            var idForResult        = helper.IdFor(m => m);
            var idForModelResult   = helper.IdForModel();
            var nameResult         = helper.Name(name: string.Empty);
            var nameForResult      = helper.NameFor(m => m);
            var nameForModelResult = helper.NameForModel();

            // Assert
            Assert.Equal(prefix, idResult);
            Assert.Equal(prefix, idForResult);
            Assert.Equal(prefix, idForModelResult);
            Assert.Equal(prefix, nameResult);
            Assert.Equal(prefix, nameForResult);
            Assert.Equal(prefix, nameForModelResult);
        }
Ejemplo n.º 14
0
        public void EditorFor_FindsModel_EvenIfNullOrEmpty(string propertyValue)
        {
            // Arrange
            var model = new DefaultTemplatesUtilities.ObjectTemplateModel {
                Property1 = propertyValue,
            };
            var viewEngine = new Mock <ICompositeViewEngine>();

            viewEngine
            .Setup(v => v.FindPartialView(It.IsAny <ActionContext>(), It.IsAny <string>()))
            .Returns(ViewEngineResult.NotFound("", Enumerable.Empty <string>()));
            var helper = DefaultTemplatesUtilities.GetHtmlHelper(model, viewEngine.Object);

            helper.ViewData["Property1"] = "ViewData string";

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

            // Assert
            Assert.Equal(
                "<input class=\"text-box single-line\" id=\"Property1\" name=\"Property1\" type=\"text\" value=\"\" />",
                result.ToString());
        }
Ejemplo n.º 15
0
        public void MultilineTextTemplate_ReturnsTextArea()
        {
            // Arrange
            var expected =
                "<textarea class=\"text-box multi-line\" id=\"FieldPrefix\" name=\"FieldPrefix\">" +
                Environment.NewLine +
                "Formatted string</textarea>";

            var model        = "Model string";
            var html         = DefaultTemplatesUtilities.GetHtmlHelper(model);
            var templateInfo = html.ViewData.TemplateInfo;

            templateInfo.HtmlFieldPrefix = "FieldPrefix";

            // TemplateBuilder sets FormattedModelValue before calling TemplateRenderer and it's used below.
            templateInfo.FormattedModelValue = "Formatted string";

            // Act
            var result = DefaultEditorTemplates.MultilineTemplate(html);

            // Assert
            Assert.Equal(expected, result);
        }
        public void Editor_FindsViewDataMember()
        {
            // Arrange
            var model = new DefaultTemplatesUtilities.ObjectTemplateModel {
                Property1 = "Model string"
            };
            var viewEngine = new Mock <ICompositeViewEngine>();

            viewEngine
            .Setup(v => v.FindPartialView(It.IsAny <ActionContext>(), It.IsAny <string>()))
            .Returns(ViewEngineResult.NotFound("", Enumerable.Empty <string>()));
            var helper = DefaultTemplatesUtilities.GetHtmlHelper(model, viewEngine.Object);

            helper.ViewData["Property1"] = "ViewData string";

            // Act
            var result = helper.Editor("Property1");

            // Assert
            Assert.Equal(
                "<input class=\"HtmlEncode[[text-box single-line]]\" id=\"HtmlEncode[[Property1]]\" name=\"HtmlEncode[[Property1]]\" type=\"HtmlEncode[[text]]\" value=\"HtmlEncode[[ViewData string]]\" />",
                result.ToString());
        }
Ejemplo n.º 17
0
        public void ObjectTemplateDisplaysSimpleDisplayTextWhenTemplateDepthGreaterThanOne(
            string simpleDisplayText,
            bool htmlEncode,
            string expectedResult)
        {
            // Arrange
            var model    = new DefaultTemplatesUtilities.ObjectTemplateModel();
            var html     = DefaultTemplatesUtilities.GetHtmlHelper(model);
            var metadata =
                new EmptyModelMetadataProvider()
                .GetMetadataForType(() => model, typeof(DefaultTemplatesUtilities.ObjectTemplateModel));

            metadata.HtmlEncode         = htmlEncode;
            metadata.SimpleDisplayText  = simpleDisplayText;
            html.ViewData.ModelMetadata = metadata;
            html.ViewData.TemplateInfo.AddVisited("foo");
            html.ViewData.TemplateInfo.AddVisited("bar");

            // Act
            var result = DefaultDisplayTemplates.ObjectTemplate(html);

            // Assert
            Assert.Equal(expectedResult, result);
        }
Ejemplo n.º 18
0
        public void ObjectTemplate_HonoursHideSurroundingHtml()
        {
            // Arrange
            var expected =
                "Model = p1, ModelType = System.String, PropertyName = Property1, SimpleDisplayText = p1" +
                "<div class=\"display-label\">Property2</div>" + Environment.NewLine +
                "<div class=\"display-field\">Model = (null), ModelType = System.String, PropertyName = Property2," +
                " SimpleDisplayText = (null)</div>" + Environment.NewLine;

            var model = new DefaultTemplatesUtilities.ObjectTemplateModel {
                Property1 = "p1", Property2 = null
            };
            var html     = DefaultTemplatesUtilities.GetHtmlHelper(model);
            var metadata =
                html.ViewData.ModelMetadata.Properties.First(m => string.Equals(m.PropertyName, "Property1"));

            metadata.HideSurroundingHtml = true;

            // Act
            var result = DefaultDisplayTemplates.ObjectTemplate(html);

            // Assert
            Assert.Equal(expected, result);
        }
Ejemplo n.º 19
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);
        }
Ejemplo n.º 20
0
        public void LabelHelpers_DisplayMetadataPropertyName_IfOverridden(string propertyName)
        {
            // Arrange
            var metadata = new ModelMetadata(
                new DataAnnotationsModelMetadataProvider(),
                containerType: null,
                modelAccessor: null,
                modelType: typeof(object),
                propertyName: propertyName);

            var helper = DefaultTemplatesUtilities.GetHtmlHelper();

            helper.ViewData.ModelMetadata = metadata;

            // Act
            var labelResult         = helper.Label(expression: string.Empty);
            var labelForResult      = helper.LabelFor(m => m);
            var labelForModelResult = helper.LabelForModel();

            // Assert
            Assert.Equal("<label for=\"\">" + propertyName + "</label>", labelResult.ToString());
            Assert.Equal("<label for=\"\">" + propertyName + "</label>", labelForResult.ToString());
            Assert.Equal("<label for=\"\">" + propertyName + "</label>", labelForModelResult.ToString());
        }