Exemplo n.º 1
0
        public void TimeInputTemplateTests()
        {
            var type = "time";

            Assert.Equal(
                GetExpectedInputTag(type, "Value"),
                DefaultEditorTemplates.TimeInputTemplate(MakeHtmlHelper <string>("Value")));

            Assert.Equal(
                GetExpectedInputTag(type, "&lt;script>alert(&#39;XSS!&#39;)&lt;/script>"),
                DefaultEditorTemplates.TimeInputTemplate(MakeHtmlHelper <string>("<script>alert('XSS!')</script>")));

            var epocInLocalTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();
            var helper          = MakeHtmlHelper <DateTime>(epocInLocalTime);

            Assert.Equal(
                GetExpectedInputTag(type, epocInLocalTime.ToString(CultureInfo.CurrentCulture)),
                DefaultEditorTemplates.TimeInputTemplate(helper));

            helper.Html5DateRenderingMode = Html5DateRenderingMode.Rfc3339;

            Assert.Equal(
                GetExpectedInputTag(type, epocInLocalTime.ToString("HH:mm:ss.fff")),
                DefaultEditorTemplates.TimeInputTemplate(helper));
        }
Exemplo n.º 2
0
        public void HiddenInputTemplateTests()
        {
            Assert.AreEqual(
                @"Hidden Value<input id=""FieldPrefix"" name=""FieldPrefix"" type=""hidden"" value=""Hidden Value"" />",
                DefaultEditorTemplates.HiddenInputTemplate(MakeHtmlHelper <string>("Hidden Value")));

            Assert.AreEqual(
                @"&lt;script&gt;alert('XSS!')&lt;/script&gt;<input id=""FieldPrefix"" name=""FieldPrefix"" type=""hidden"" value=""&lt;script>alert('XSS!')&lt;/script>"" />",
                DefaultEditorTemplates.HiddenInputTemplate(MakeHtmlHelper <string>("<script>alert('XSS!')</script>")));

            var helperWithInvisibleHtml = MakeHtmlHelper <string>("<script>alert('XSS!')</script>", "<b>Encode me!</b>");

            helperWithInvisibleHtml.ViewData.ModelMetadata.HideSurroundingHtml = true;
            Assert.AreEqual(
                @"<input id=""FieldPrefix"" name=""FieldPrefix"" type=""hidden"" value=""&lt;script>alert('XSS!')&lt;/script>"" />",
                DefaultEditorTemplates.HiddenInputTemplate(helperWithInvisibleHtml));

            byte[] byteValues = { 1, 2, 3, 4, 5 };

            Assert.AreEqual(
                @"&quot;AQIDBAU=&quot;<input id=""FieldPrefix"" name=""FieldPrefix"" type=""hidden"" value=""AQIDBAU="" />",
                DefaultEditorTemplates.HiddenInputTemplate(MakeHtmlHelper <Binary>(new Binary(byteValues))));

            Assert.AreEqual(
                @"System.Byte[]<input id=""FieldPrefix"" name=""FieldPrefix"" type=""hidden"" value=""AQIDBAU="" />",
                DefaultEditorTemplates.HiddenInputTemplate(MakeHtmlHelper <byte[]>(byteValues)));
        }
Exemplo n.º 3
0
        public void ObjectTemplate_IgnoresPropertiesWith_ScaffoldColumnFalse()
        {
            // Arrange
            var expected =
                @"<div class=""editor-label""><label for=""Property1"">Property1</label></div>" +
                Environment.NewLine +
                @"<div class=""editor-field""><input class=""text-box single-line"" id=""Property1"" name=""Property1"" type=""text"" value="""" /> </div>" +
                Environment.NewLine +
                @"<div class=""editor-label""><label for=""Property3"">Property3</label></div>" +
                Environment.NewLine +
                @"<div class=""editor-field""><input class=""text-box single-line"" id=""Property3"" name=""Property3"" type=""text"" value="""" /> </div>" +
                Environment.NewLine;

            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 = DefaultEditorTemplates.ObjectTemplate(htmlHelper);

            // Assert
            Assert.Equal(expected, result);
        }
Exemplo n.º 4
0
        public void ObjectTemplatePreventsRecursionOnModelTypeForNullModelValues()
        {
            string expected = @"<div class=""editor-label""><label for=""FieldPrefix_Property2"">Property2</label></div>
<div class=""editor-field"">Model = propValue2, ModelType = System.String, PropertyName = Property2, HtmlFieldName = Property2, TemplateName = (null), Mode = Edit, AdditionalViewData = (null) </div>
";

            // Arrange
            ObjectTemplateModel          model         = new ObjectTemplateModel();
            HtmlHelper                   html          = MakeHtmlHelper <ObjectTemplateModel>(model);
            Mock <ModelMetadataProvider> provider      = new Mock <ModelMetadataProvider>();
            Func <object>                accessor      = () => model;
            Mock <ModelMetadata>         metadata      = new Mock <ModelMetadata>(provider.Object, null, accessor, typeof(ObjectTemplateModel), null);
            ModelMetadata                prop1Metadata = new ModelMetadata(provider.Object, typeof(ObjectTemplateModel), null, typeof(string), "Property1");
            ModelMetadata                prop2Metadata = new ModelMetadata(provider.Object, typeof(ObjectTemplateModel), () => "propValue2", typeof(string), "Property2");

            html.ViewData.ModelMetadata = metadata.Object;
            metadata.Expect(p => p.Properties).Returns(() => new[] { prop1Metadata, prop2Metadata });
            html.ViewData.TemplateInfo.VisitedObjects.Add(typeof(string));

            // Act
            string result = DefaultEditorTemplates.ObjectTemplate(html, SpyCallback);

            // Assert
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 5
0
        public void ObjectTemplateWithShowForEditorMetadata()
        {
            string expected = @"<div class=""editor-label""><label for=""FieldPrefix_Property1"">Property1</label></div>
<div class=""editor-field"">Model = (null), ModelType = System.String, PropertyName = Property1, HtmlFieldName = Property1, TemplateName = (null), Mode = Edit, AdditionalViewData = (null) </div>
";

            // Arrange
            ObjectTemplateModel          model         = new ObjectTemplateModel();
            HtmlHelper                   html          = MakeHtmlHelper <ObjectTemplateModel>(model);
            Mock <ModelMetadataProvider> provider      = new Mock <ModelMetadataProvider>();
            Func <object>                accessor      = () => model;
            Mock <ModelMetadata>         metadata      = new Mock <ModelMetadata>(provider.Object, null, accessor, typeof(ObjectTemplateModel), null);
            ModelMetadata                prop1Metadata = new ModelMetadata(provider.Object, typeof(ObjectTemplateModel), null, typeof(string), "Property1")
            {
                ShowForEdit = true
            };
            ModelMetadata prop2Metadata = new ModelMetadata(provider.Object, typeof(ObjectTemplateModel), null, typeof(string), "Property2")
            {
                ShowForEdit = false
            };

            html.ViewData.ModelMetadata = metadata.Object;
            metadata.Expect(p => p.Properties).Returns(() => new[] { prop1Metadata, prop2Metadata });

            // Act
            string result = DefaultEditorTemplates.ObjectTemplate(html, SpyCallback);

            // Assert
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 6
0
        public void ObjectTemplate_HonoursHideSurroundingHtml()
        {
            // Arrange
            var expected =
                "Model = p1, ModelType = System.String, PropertyName = Property1, SimpleDisplayText = p1" +
                "<div class=\"editor-label\"><label for=\"Property2\">Property2</label></div>" +
                Environment.NewLine +
                "<div class=\"editor-field\">" +
                "Model = (null), ModelType = System.String, PropertyName = Property2, SimpleDisplayText = (null) " +
                "<span class=\"field-validation-valid\" data-valmsg-for=\"Property2\" data-valmsg-replace=\"true\">" +
                "</span></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 = DefaultEditorTemplates.ObjectTemplate(html);

            // Assert
            Assert.Equal(expected, result);
        }
Exemplo n.º 7
0
        public void ObjectTemplateWithDisplayNameMetadata()
        {
            string expected =
                "<div class=\"editor-field\">Model = (null), ModelType = System.String, PropertyName = Property1, HtmlFieldName = Property1, TemplateName = (null), Mode = Edit, AdditionalViewData = (null) </div>" + Environment.NewLine
                + "<div class=\"editor-label\"><label for=\"FieldPrefix_Property2\">Custom display name</label></div>" + Environment.NewLine
                + "<div class=\"editor-field\">Model = (null), ModelType = System.String, PropertyName = Property2, HtmlFieldName = Property2, TemplateName = (null), Mode = Edit, AdditionalViewData = (null) </div>" + Environment.NewLine;

            // Arrange
            ObjectTemplateModel          model         = new ObjectTemplateModel();
            HtmlHelper                   html          = MakeHtmlHelper <ObjectTemplateModel>(model);
            Mock <ModelMetadataProvider> provider      = new Mock <ModelMetadataProvider>();
            Func <object>                accessor      = () => model;
            Mock <ModelMetadata>         metadata      = new Mock <ModelMetadata>(provider.Object, null, accessor, typeof(ObjectTemplateModel), null);
            ModelMetadata                prop1Metadata = new ModelMetadata(provider.Object, typeof(ObjectTemplateModel), null, typeof(string), "Property1")
            {
                DisplayName = String.Empty
            };
            ModelMetadata prop2Metadata = new ModelMetadata(provider.Object, typeof(ObjectTemplateModel), null, typeof(string), "Property2")
            {
                DisplayName = "Custom display name"
            };

            html.ViewData.ModelMetadata = metadata.Object;
            metadata.Setup(p => p.Properties).Returns(() => new[] { prop1Metadata, prop2Metadata });

            // Act
            string result = DefaultEditorTemplates.ObjectTemplate(html, SpyCallback);

            // Assert
            Assert.Equal(expected, result);
        }
Exemplo n.º 8
0
        public void ObjectTemplateEditsSimplePropertiesOnObjectByDefault()
        {
            var expected =
                "<div class=\"editor-label\"><label for=\"Property1\">Property1</label></div>" + Environment.NewLine
                + "<div class=\"editor-field\">Model = p1, ModelType = System.String, PropertyName = Property1," +
                " SimpleDisplayText = p1 " +
                "<span class=\"field-validation-valid\" data-valmsg-for=\"Property1\" data-valmsg-replace=\"true\">" +
                "</span></div>" + Environment.NewLine
                + "<div class=\"editor-label\"><label for=\"Property2\">Property2</label></div>" + Environment.NewLine
                + "<div class=\"editor-field\">Model = (null), ModelType = System.String, PropertyName = Property2," +
                " SimpleDisplayText = (null) " +
                "<span class=\"field-validation-valid\" data-valmsg-for=\"Property2\" data-valmsg-replace=\"true\">" +
                "</span></div>" + Environment.NewLine;

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

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

            // Assert
            Assert.Equal(expected, result);
        }
        public void HiddenInputTemplate_HonoursHideSurroundingHtml()
        {
            // Arrange
            var expected = "<input id=\"HtmlEncode[[FieldPrefix]]\" name=\"HtmlEncode[[FieldPrefix]]\" type=\"HtmlEncode[[hidden]]\" value=\"HtmlEncode[[Model string]]\" />";

            var model = "Model string";

            var provider = new TestModelMetadataProvider();

            provider.ForType <string>().DisplayDetails(dd =>
            {
                dd.HideSurroundingHtml = true;
            });

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

            var templateInfo = html.ViewData.TemplateInfo;

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

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

            // Assert
            Assert.Equal(expected, result);
        }
Exemplo n.º 10
0
        public void ObjectTemplateDisplaysSimpleDisplayTextWithNonNullModelTemplateDepthGreaterThanOne(
            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.NullDisplayText    = "Null Display Text";
            metadata.SimpleDisplayText  = simpleDisplayText;
            html.ViewData.ModelMetadata = metadata;
            html.ViewData.TemplateInfo.AddVisited("foo");
            html.ViewData.TemplateInfo.AddVisited("bar");

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

            // Assert
            Assert.Equal(expectedResult, result);
        }
        public void ObjectTemplate_HonoursHideSurroundingHtml()
        {
            // Arrange
            var expected =
                "Model = p1, ModelType = System.String, PropertyName = Property1, SimpleDisplayText = p1" +
                "<div class=\"HtmlEncode[[editor-label]]\"><label for=\"HtmlEncode[[Property2]]\">HtmlEncode[[Property2]]</label></div>" +
                Environment.NewLine +
                "<div class=\"HtmlEncode[[editor-field]]\">" +
                "Model = (null), ModelType = System.String, PropertyName = Property2, SimpleDisplayText = (null) " +
                "<span class=\"HtmlEncode[[field-validation-valid]]\" data-valmsg-for=\"HtmlEncode[[Property2]]\" data-valmsg-replace=\"HtmlEncode[[true]]\">" +
                "</span></div>" +
                Environment.NewLine;

            var provider = new TestModelMetadataProvider();

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

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

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

            // Assert
            Assert.Equal(expected, result);
        }
        public void ObjectTemplateDisplaysSimpleDisplayTextWithNonNullModelTemplateDepthGreaterThanOne(
            string simpleDisplayText,
            bool htmlEncode,
            string expectedResult)
        {
            // Arrange
            var model = new DefaultTemplatesUtilities.ObjectTemplateModel()
            {
                Property1 = simpleDisplayText,
            };

            var provider = new TestModelMetadataProvider();

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

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

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

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

            // Assert
            Assert.Equal(expectedResult, result);
        }
Exemplo n.º 13
0
        public void StringTemplateTests()
        {
            Assert.AreEqual(
                @"<input class=""text-box single-line"" id=""FieldPrefix"" name=""FieldPrefix"" type=""text"" value=""Value"" />",
                DefaultEditorTemplates.StringTemplate(MakeHtmlHelper <string>("Value")));

            Assert.AreEqual(
                @"<input class=""text-box single-line"" id=""FieldPrefix"" name=""FieldPrefix"" type=""text"" value=""&lt;script>alert('XSS!')&lt;/script>"" />",
                DefaultEditorTemplates.StringTemplate(MakeHtmlHelper <string>("<script>alert('XSS!')</script>")));
        }
Exemplo n.º 14
0
        public void EmailAddressTemplateTests()
        {
            Assert.Equal(
                @"<input class=""text-box single-line"" id=""FieldPrefix"" name=""FieldPrefix"" type=""email"" value=""Value"" />",
                DefaultEditorTemplates.EmailAddressInputTemplate(MakeHtmlHelper <string>("Value")));

            Assert.Equal(
                @"<input class=""text-box single-line"" id=""FieldPrefix"" name=""FieldPrefix"" type=""email"" value=""&lt;script>alert(&#39;XSS!&#39;)&lt;/script>"" />",
                DefaultEditorTemplates.EmailAddressInputTemplate(MakeHtmlHelper <string>("<script>alert('XSS!')</script>")));
        }
Exemplo n.º 15
0
        public void NumberInputTemplateTests()
        {
            Assert.Equal(
                "<input class=\"text-box single-line\" id=\"FieldPrefix\" name=\"FieldPrefix\" type=\"number\" value=\"Value\" />",
                DefaultEditorTemplates.NumberInputTemplate(MakeHtmlHelper <string>("Value")));

            Assert.Equal(
                "<input class=\"text-box single-line\" id=\"FieldPrefix\" name=\"FieldPrefix\" type=\"number\" value=\"&lt;script>alert(&#39;XSS!&#39;)&lt;/script>\" />",
                DefaultEditorTemplates.NumberInputTemplate(MakeHtmlHelper <string>("<script>alert('XSS!')</script>")));
        }
Exemplo n.º 16
0
        public void ObjectTemplateAllPropertiesFromEntityObjectAreHidden()
        {
            // Arrange
            HtmlHelper html = MakeHtmlHelper <ObjectTemplateModel>(new MyEntityObject());

            // Act
            string result = DefaultEditorTemplates.ObjectTemplate(html, SpyCallback);

            // Assert
            Assert.AreEqual(String.Empty, result);
        }
Exemplo n.º 17
0
        public void CollectionTemplateWithNullModel()
        {
            // Arrange
            HtmlHelper html = MakeHtmlHelper <object>(null);

            // Act
            string result = DefaultEditorTemplates.CollectionTemplate(html, CollectionSpyCallback);

            // Assert
            Assert.AreEqual(String.Empty, result);
        }
Exemplo n.º 18
0
        public void CollectionTemplateNonEnumerableModelThrows()
        {
            // Arrange
            HtmlHelper html = MakeHtmlHelper <object>(new object());

            // Act & Assert
            ExceptionHelper.ExpectInvalidOperationException(
                () => DefaultEditorTemplates.CollectionTemplate(html, CollectionSpyCallback),
                "The Collection template was used with an object of type 'System.Object', which does not implement System.IEnumerable."
                );
        }
Exemplo n.º 19
0
        public void DecimalTemplateTests()
        {
            Assert.AreEqual(
                @"<input class=""text-box single-line"" id=""FieldPrefix"" name=""FieldPrefix"" type=""text"" value=""12.35"" />",
                DefaultEditorTemplates.DecimalTemplate(MakeHtmlHelper <decimal>(12.3456M)));

            Assert.AreEqual(
                @"<input class=""text-box single-line"" id=""FieldPrefix"" name=""FieldPrefix"" type=""text"" value=""Formatted Value"" />",
                DefaultEditorTemplates.DecimalTemplate(MakeHtmlHelper <decimal>(12.3456M, "Formatted Value")));

            Assert.AreEqual(
                @"<input class=""text-box single-line"" id=""FieldPrefix"" name=""FieldPrefix"" type=""text"" value=""&lt;script>alert('XSS!')&lt;/script>"" />",
                DefaultEditorTemplates.DecimalTemplate(MakeHtmlHelper <decimal>(12.3456M, "<script>alert('XSS!')</script>")));
        }
Exemplo n.º 20
0
        public void MultilineTextTemplateTests()
        {
            Assert.Equal(
                "<textarea class=\"text-box multi-line\" id=\"FieldPrefix\" name=\"FieldPrefix\">" + Environment.NewLine
                + "Multiple" + Environment.NewLine
                + "Line" + Environment.NewLine
                + "Value!</textarea>",
                DefaultEditorTemplates.MultilineTextTemplate(MakeHtmlHelper <string>("", "Multiple" + Environment.NewLine + "Line" + Environment.NewLine + "Value!")));

            Assert.Equal(
                "<textarea class=\"text-box multi-line\" id=\"FieldPrefix\" name=\"FieldPrefix\">" + Environment.NewLine
                + "&lt;script&gt;alert(&#39;XSS!&#39;)&lt;/script&gt;</textarea>",
                DefaultEditorTemplates.MultilineTextTemplate(MakeHtmlHelper <string>("", "<script>alert('XSS!')</script>")));
        }
        public void ColorInputTemplateTests()
        {
            Assert.Equal(
                @"<input class=""text-box single-line"" id=""FieldPrefix"" name=""FieldPrefix"" type=""color"" value=""#33F4CC"" />",
                DefaultEditorTemplates.ColorInputTemplate(MakeHtmlHelper <string>("#33F4CC")));

            var color = Color.FromArgb(0x33, 0xf4, 0xcc);

            Assert.Equal(
                @"<input class=""text-box single-line"" id=""FieldPrefix"" name=""FieldPrefix"" type=""color"" value=""#33F4CC"" />",
                DefaultEditorTemplates.ColorInputTemplate(MakeHtmlHelper <Color>(color)));

            Assert.Equal(
                @"<input class=""text-box single-line"" id=""FieldPrefix"" name=""FieldPrefix"" type=""color"" value=""&lt;script>alert(&#39;XSS!&#39;)&lt;/script>"" />",
                DefaultEditorTemplates.ColorInputTemplate(MakeHtmlHelper <string>("<script>alert('XSS!')</script>")));
        }
Exemplo n.º 22
0
        public void MultilineTextTemplateTests()
        {
            Assert.AreEqual(
                @"<textarea class=""text-box multi-line"" id=""FieldPrefix"" name=""FieldPrefix"">
Multiple
Line
Value!</textarea>",
                DefaultEditorTemplates.MultilineTextTemplate(MakeHtmlHelper <string>("", @"Multiple
Line
Value!")));

            Assert.AreEqual(
                @"<textarea class=""text-box multi-line"" id=""FieldPrefix"" name=""FieldPrefix"">
&lt;script&gt;alert('XSS!')&lt;/script&gt;</textarea>",
                DefaultEditorTemplates.MultilineTextTemplate(MakeHtmlHelper <string>("", "<script>alert('XSS!')</script>")));
        }
Exemplo n.º 23
0
        public void CollectionTemplateNullITemInWeaklyTypedCollectionUsesModelTypeOfString()
        {
            // Arrange
            HtmlHelper html = MakeHtmlHelper <ArrayList>(new ArrayList {
                null
            });

            html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = null;

            // Act
            string result = DefaultEditorTemplates.CollectionTemplate(html, CollectionSpyCallback);

            // Assert
            Assert.AreEqual(@"
Model = (null), ModelType = System.String, PropertyName = (null), HtmlFieldName = [0], TemplateName = (null), Mode = Edit, TemplateInfo.HtmlFieldPrefix = , AdditionalViewData = (null)",
                            result);
        }
        public void DecimalTemplateTests()
        {
            Assert.Equal(
                String.Format(
                    CultureInfo.CurrentCulture,
                    @"<input class=""text-box single-line"" id=""FieldPrefix"" name=""FieldPrefix"" type=""text"" value=""{0:0.00}"" />",
                    12.35M),
                DefaultEditorTemplates.DecimalTemplate(MakeHtmlHelper <decimal>(12.3456M)));

            Assert.Equal(
                @"<input class=""text-box single-line"" id=""FieldPrefix"" name=""FieldPrefix"" type=""text"" value=""Formatted Value"" />",
                DefaultEditorTemplates.DecimalTemplate(MakeHtmlHelper <decimal>(12.3456M, "Formatted Value")));

            Assert.Equal(
                @"<input class=""text-box single-line"" id=""FieldPrefix"" name=""FieldPrefix"" type=""text"" value=""&lt;script>alert(&#39;XSS!&#39;)&lt;/script>"" />",
                DefaultEditorTemplates.DecimalTemplate(MakeHtmlHelper <decimal>(12.3456M, "<script>alert('XSS!')</script>")));
        }
Exemplo n.º 25
0
        public void CollectionTemplateWithSingleItemCollectionWithPrefix()
        {
            // Arrange
            HtmlHelper html = MakeHtmlHelper <List <string> >(new List <string> {
                "foo"
            });

            html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "ModelProperty";

            // Act
            string result = DefaultEditorTemplates.CollectionTemplate(html, CollectionSpyCallback);

            // Assert
            Assert.AreEqual(@"
Model = foo, ModelType = System.String, PropertyName = (null), HtmlFieldName = ModelProperty[0], TemplateName = (null), Mode = Edit, TemplateInfo.HtmlFieldPrefix = , AdditionalViewData = (null)",
                            result);
        }
Exemplo n.º 26
0
        public void CollectionTemplateNullItemInStronglyTypedCollectionUsesModelTypeFromIEnumerable()
        {
            // Arrange
            HtmlHelper html = MakeHtmlHelper <List <IHttpHandler> >(new List <IHttpHandler> {
                null
            });

            html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = null;

            // Act
            string result = DefaultEditorTemplates.CollectionTemplate(html, CollectionSpyCallback);

            // Assert
            Assert.AreEqual(@"
Model = (null), ModelType = System.Web.IHttpHandler, PropertyName = (null), HtmlFieldName = [0], TemplateName = (null), Mode = Edit, TemplateInfo.HtmlFieldPrefix = , AdditionalViewData = (null)",
                            result);
        }
Exemplo n.º 27
0
        public void ObjectTemplateDisplaysNullDisplayTextWithNullModelAndTemplateDepthGreaterThanOne()
        {
            // Arrange
            HtmlHelper    html     = MakeHtmlHelper <ObjectTemplateModel>(null);
            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(ObjectTemplateModel));

            metadata.NullDisplayText    = "Null Display Text";
            metadata.SimpleDisplayText  = "Simple Display Text";
            html.ViewData.ModelMetadata = metadata;
            html.ViewData.TemplateInfo.VisitedObjects.Add("foo");
            html.ViewData.TemplateInfo.VisitedObjects.Add("bar");

            // Act
            string result = DefaultEditorTemplates.ObjectTemplate(html, SpyCallback);

            // Assert
            Assert.AreEqual(metadata.NullDisplayText, result);
        }
        public void ObjectTemplate_OrdersProperties_AsExpected()
        {
            // Arrange
            var model = new OrderedModel();
            var html  = DefaultTemplatesUtilities.GetHtmlHelper(model);
            var expectedProperties = new List <string>
            {
                "OrderedProperty3",
                "OrderedProperty2",
                "OrderedProperty1",
                "Property3",
                "Property1",
                "Property2",
                "LastProperty",
            };

            var stringBuilder = new StringBuilder();

            foreach (var property in expectedProperties)
            {
                var label = string.Format(
                    CultureInfo.InvariantCulture,
                    "<div class=\"HtmlEncode[[editor-label]]\"><label for=\"HtmlEncode[[{0}]]\">HtmlEncode[[{0}]]</label></div>",
                    property);
                stringBuilder.AppendLine(label);

                var value = string.Format(
                    CultureInfo.InvariantCulture,
                    "<div class=\"HtmlEncode[[editor-field]]\">Model = (null), ModelType = System.String, PropertyName = {0}, " +
                    "SimpleDisplayText = (null) " +
                    "<span class=\"HtmlEncode[[field-validation-valid]]\" data-valmsg-for=\"HtmlEncode[[{0}]]\" data-valmsg-replace=\"HtmlEncode[[true]]\">" +
                    "</span></div>",
                    property);
                stringBuilder.AppendLine(value);
            }
            var expected = stringBuilder.ToString();

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

            // Assert
            Assert.Equal(expected, result);
        }
Exemplo n.º 29
0
        public void CollectionTemplateUsesRealObjectTypes()
        {
            // Arrange
            HtmlHelper html = MakeHtmlHelper <List <Object> >(new List <Object> {
                1, 2.3, "Hello World"
            });

            html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = null;

            // Act
            string result = DefaultEditorTemplates.CollectionTemplate(html, CollectionSpyCallback);

            // Assert
            Assert.AreEqual(@"
Model = 1, ModelType = System.Int32, PropertyName = (null), HtmlFieldName = [0], TemplateName = (null), Mode = Edit, TemplateInfo.HtmlFieldPrefix = , AdditionalViewData = (null)
Model = 2.3, ModelType = System.Double, PropertyName = (null), HtmlFieldName = [1], TemplateName = (null), Mode = Edit, TemplateInfo.HtmlFieldPrefix = , AdditionalViewData = (null)
Model = Hello World, ModelType = System.String, PropertyName = (null), HtmlFieldName = [2], TemplateName = (null), Mode = Edit, TemplateInfo.HtmlFieldPrefix = , AdditionalViewData = (null)",
                            result);
        }
Exemplo n.º 30
0
        public void CollectionTemplateNullItemInCollectionOfNullableValueTypesDoesNotDiscardNullable()
        {
            // Arrange
            HtmlHelper html = MakeHtmlHelper <List <int?> >(new List <int?> {
                1, null, 2
            });

            html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = null;

            // Act
            string result = DefaultEditorTemplates.CollectionTemplate(html, CollectionSpyCallback);

            // Assert
            Assert.AreEqual(@"
Model = 1, ModelType = System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], PropertyName = (null), HtmlFieldName = [0], TemplateName = (null), Mode = Edit, TemplateInfo.HtmlFieldPrefix = , AdditionalViewData = (null)
Model = (null), ModelType = System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], PropertyName = (null), HtmlFieldName = [1], TemplateName = (null), Mode = Edit, TemplateInfo.HtmlFieldPrefix = , AdditionalViewData = (null)
Model = 2, ModelType = System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], PropertyName = (null), HtmlFieldName = [2], TemplateName = (null), Mode = Edit, TemplateInfo.HtmlFieldPrefix = , AdditionalViewData = (null)",
                            result);
        }