Beispiel #1
0
        public void ValueHelpersWithErrorsGetValueFromModelState()
        {
            // Arrange
            ViewDataDictionary <FooBarModel> viewDataWithErrors = new ViewDataDictionary <FooBarModel> {
                { "foo", "ViewDataFoo" }
            };

            viewDataWithErrors.Model = new FooBarModel()
            {
                foo = "ViewItemFoo", bar = "ViewItemBar"
            };
            viewDataWithErrors.TemplateInfo.HtmlFieldPrefix = "FieldPrefix";

            ModelState modelStateFoo = new ModelState();

            modelStateFoo.Value = HtmlHelperTest.GetValueProviderResult(new string[] { "AttemptedValueFoo" }, "AttemptedValueFoo");
            viewDataWithErrors.ModelState["FieldPrefix.foo"] = modelStateFoo;

            ModelState modelStateFooBar = new ModelState();

            modelStateFooBar.Value = HtmlHelperTest.GetValueProviderResult(new string[] { "AttemptedValueFooBar" }, "AttemptedValueFooBar");
            viewDataWithErrors.ModelState["FieldPrefix"] = modelStateFooBar;

            HtmlHelper <FooBarModel> helper = MvcHelper.GetHtmlHelper(viewDataWithErrors);

            // Act & Assert
            Assert.Equal("AttemptedValueFoo", helper.Value("foo").ToHtmlString());
            Assert.Equal("AttemptedValueFoo", helper.ValueFor(m => m.foo).ToHtmlString());
            Assert.Equal("AttemptedValueFooBar", helper.ValueForModel().ToHtmlString());
        }
Beispiel #2
0
        public void SelectHelpersUseCurrentCultureToConvertValues()
        {
            // Arrange
            HtmlHelper defaultValueHelper = HtmlHelperTest.GetHtmlHelper(new ViewDataDictionary {
                { "foo", new [] { new DateTime(1900, 1, 1, 0, 0, 1) } },
                { "bar", new DateTime(1900, 1, 1, 0, 0, 1) }
            });
            HtmlHelper helper     = HtmlHelperTest.GetHtmlHelper();
            SelectList selectList = new SelectList(GetSampleCultureAnonymousObjects(), "Date", "FullWord", new DateTime(1900, 1, 1, 0, 0, 0));

            var tests = new[] {
                // DropDownList(name, selectList, optionLabel)
                new {
                    Html   = @"<select id=""foo"" name=""foo""><option selected=""selected"" value=""1900/01/01 12:00:00 AM"">Alpha</option>
<option value=""1900/01/01 12:00:01 AM"">Bravo</option>
<option value=""1900/01/01 12:00:02 AM"">Charlie</option>
</select>",
                    Action = new GenericDelegate <string>(() => helper.DropDownList("foo", selectList, (string)null))
                },
                // DropDownList(name, selectList, optionLabel) (With default value selected from ViewData)
                new {
                    Html   = @"<select id=""bar"" name=""bar""><option value=""1900/01/01 12:00:00 AM"">Alpha</option>
<option selected=""selected"" value=""1900/01/01 12:00:01 AM"">Bravo</option>
<option value=""1900/01/01 12:00:02 AM"">Charlie</option>
</select>",
                    Action = new GenericDelegate <string>(() => defaultValueHelper.DropDownList("bar", selectList, (string)null))
                },

                // ListBox(name, selectList)
                new {
                    Html   = @"<select id=""foo"" multiple=""multiple"" name=""foo""><option selected=""selected"" value=""1900/01/01 12:00:00 AM"">Alpha</option>
<option value=""1900/01/01 12:00:01 AM"">Bravo</option>
<option value=""1900/01/01 12:00:02 AM"">Charlie</option>
</select>",
                    Action = new GenericDelegate <string>(() => helper.ListBox("foo", selectList))
                },

                // ListBox(name, selectList) (With default value selected from ViewData)
                new {
                    Html   = @"<select id=""foo"" multiple=""multiple"" name=""foo""><option value=""1900/01/01 12:00:00 AM"">Alpha</option>
<option selected=""selected"" value=""1900/01/01 12:00:01 AM"">Bravo</option>
<option value=""1900/01/01 12:00:02 AM"">Charlie</option>
</select>",
                    Action = new GenericDelegate <string>(() => defaultValueHelper.ListBox("foo", selectList))
                }
            };

            // Act && Assert
            using (HtmlHelperTest.ReplaceCulture("en-ZA", "en-US")) {
                foreach (var test in tests)
                {
                    Assert.AreEqual(test.Html, test.Action());
                }
            }
        }
Beispiel #3
0
        public void ActionLinkParametersNeedEscaping()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper();

            // Act
            string html = htmlHelper.ActionLink("linktext<&>\"", "new action<&>\"");

            // Assert
            Assert.AreEqual <string>(@"<a href=""" + AppPathModifier + @"/app/home/new%20action%3C&amp;%3E%22"">linktext&lt;&amp;&gt;&quot;</a>", html);
        }
Beispiel #4
0
        public void ValidationMessageReturnsWithCustomMessage()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors());

            // Act
            string html = htmlHelper.ValidationMessage("foo", "bar error");

            // Assert
            Assert.AreEqual(@"<span class=""field-validation-error"">bar error</span>", html);
        }
Beispiel #5
0
        public void TextBoxWithImplicitValue()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetTextBoxViewData());

            // Act
            string html = helper.TextBox("foo");

            // Assert
            Assert.AreEqual(@"<input id=""foo"" name=""foo"" type=""text"" value=""ViewDataFoo"" />", html);
        }
Beispiel #6
0
        public void ValidationMessageReturnsGenericMessageInsteadOfExceptionText()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors());

            // Act
            string html = htmlHelper.ValidationMessage("quux");

            // Assert
            Assert.AreEqual(@"<span class=""field-validation-error"">The value 'quuxValue' is invalid.</span>", html);
        }
Beispiel #7
0
        public void ValidationMessageReturnsWithObjectAttributes()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors());

            // Act
            string html = htmlHelper.ValidationMessage("foo", new { bar = "bar" });

            // Assert
            Assert.AreEqual(@"<span bar=""bar"" class=""field-validation-error"">foo error &lt;1&gt;</span>", html);
        }
Beispiel #8
0
        public void ValidationMessageThrowsIfModelNameIsNull()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper();

            // Act & Assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                htmlHelper.ValidationMessage(null);
            }, "modelName");
        }
Beispiel #9
0
        public void ValidationSummaryWithNoErrorsReturnsNull()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(new ViewDataDictionary());

            // Act
            string html = htmlHelper.ValidationSummary();

            // Assert
            Assert.IsNull(html, "html should be null if there are no errors to report.");
        }
Beispiel #10
0
        public void ActionLinkWithProtocol()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper();

            // Act
            string html = htmlHelper.ActionLink("linktext", "newaction", "home2", "https", "foo.bar.com", null /* fragment */, new { id = "someid" }, new { baz = "baz" });

            // Assert
            Assert.AreEqual <string>(@"<a baz=""baz"" href=""https://foo.bar.com" + AppPathModifier + @"/app/home2/newaction/someid"">linktext</a>", html);
        }
Beispiel #11
0
        public void ActionLinkDictionaryOverridesImplicitValues()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper();

            // Act
            string html = htmlHelper.ActionLink("linktext", "newaction", null, new { href = "http://foo.com" });

            // Assert
            Assert.AreEqual <string>(@"<a href=""http://foo.com"">linktext</a>", html);
        }
Beispiel #12
0
        public void ActionLinkWithNullHostname()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper();

            // Act
            string html = htmlHelper.ActionLink("linktext", "newaction", "home2", "https", null /* hostName */, "foo", new { id = "someid" }, new { baz = "baz" });

            // Assert
            Assert.AreEqual <string>(@"<a baz=""baz"" href=""https://localhost" + AppPathModifier + @"/app/home2/newaction/someid#foo"">linktext</a>", html);
        }
Beispiel #13
0
        public void ActionLinkWithNullProtocolNullHostNameAndNullFragment()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper();

            // Act
            string html = htmlHelper.ActionLink("linktext", "newaction", "home2", null /* protocol */, null /* hostName */, null /* fragment */, new { id = "someid" }, new { baz = "baz" });

            // Assert
            Assert.AreEqual <string>(@"<a baz=""baz"" href=""" + AppPathModifier + @"/app/home2/newaction/someid"">linktext</a>", html);
        }
Beispiel #14
0
        public void ActionLinkWithDictionary()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper();

            // Act
            string html = htmlHelper.ActionLink("linktext", "newaction", new RouteValueDictionary(new { Controller = "home2", id = "someid" }), new RouteValueDictionary(new { baz = "baz" }));

            // Assert
            Assert.AreEqual <string>(@"<a baz=""baz"" href=""" + AppPathModifier + @"/app/home2/newaction/someid"">linktext</a>", html);
        }
Beispiel #15
0
        public void TextBoxWithImplicitValueAndAttributesObject()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetTextBoxViewData());

            // Act
            string html = helper.TextBox("foo", null, _attributesObjectDictionary);

            // Assert
            Assert.AreEqual(@"<input baz=""BazObjValue"" id=""foo"" name=""foo"" type=""text"" value=""ViewDataFoo"" />", html);
        }
Beispiel #16
0
        public void TextBoxWithImplicitValueAndAttributesDictionaryReturnsEmptyValueIfNotFound()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetTextBoxViewData());

            // Act
            string html = helper.TextBox("keyNotFound", null, _attributesDictionary);

            // Assert
            Assert.AreEqual(@"<input baz=""BazValue"" id=""keyNotFound"" name=""keyNotFound"" type=""text"" value="""" />", html);
        }
Beispiel #17
0
        public void ActionLinkWithActionNameAndValueDictionary()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper();

            // Act
            string html = htmlHelper.ActionLink("linktext", "newaction", new RouteValueDictionary(new { controller = "home2" }));

            // Assert
            Assert.AreEqual <string>(@"<a href=""" + AppPathModifier + @"/app/home2/newaction"">linktext</a>", html);
        }
Beispiel #18
0
        public void ActionLinkWithNonDefaultPortAndSameProtocol()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(Uri.UriSchemeHttp, 32768);

            // Act
            string html = htmlHelper.ActionLink("linktext", "newaction", "home2", "http", "foo.bar.com", "foo", new { id = "someid" }, new { baz = "baz" });

            // Assert
            Assert.AreEqual <string>(@"<a baz=""baz"" href=""http://foo.bar.com:32768" + AppPathModifier + @"/app/home2/newaction/someid#foo"">linktext</a>", html);
        }
Beispiel #19
0
        public void ActionLinkWithControllerName()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper();

            // Act
            string html = htmlHelper.ActionLink("linktext", "newaction", "home2");

            // Assert
            Assert.AreEqual <string>(@"<a href=""" + AppPathModifier + @"/app/home2/newaction"">linktext</a>", html);
        }
Beispiel #20
0
        public void ActionLinkExplictValuesOverrideDictionary()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper();

            // Act
            string html = htmlHelper.ActionLink("linktext", "explicitAction", new { action = "dictionaryAction" }, null);

            // Assert
            Assert.AreEqual <string>(@"<a href=""" + AppPathModifier + @"/app/home/explicitAction"">linktext</a>", html);
        }
Beispiel #21
0
        public void ValidationMessageWithModelStateAndNoErrors()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors());

            // Act
            string html = htmlHelper.ValidationMessage("baz");

            // Assert
            Assert.IsNull(html, "html should be null if there are no errors");
        }
Beispiel #22
0
        public void RouteLinkWithProtocolAndFragment()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper();

            // Act
            string html = htmlHelper.RouteLink("linktext", "namedroute", "https", "foo.bar.com", "foo", new { Action = "newaction", Controller = "home2", id = "someid" }, new { baz = "baz" });

            // Assert
            Assert.AreEqual <string>(@"<a baz=""baz"" href=""https://foo.bar.com" + AppPathModifier + @"/app/named/home2/newaction/someid#foo"">linktext</a>", html);
        }
Beispiel #23
0
        public void ValidationMessageReturnsFirstError()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors());

            // Act
            string html = htmlHelper.ValidationMessage("foo");

            // Assert
            Assert.AreEqual(@"<span class=""field-validation-error"">foo error &lt;1&gt;</span>", html);
        }
Beispiel #24
0
        public void RouteLinkWithRouteNameAndDefaults()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper();

            // Act
            string html = htmlHelper.RouteLink("linktext", "namedroute", new { Action = "newaction" });

            // Assert
            Assert.AreEqual <string>(@"<a href=""" + AppPathModifier + @"/app/named/home/newaction"">linktext</a>", html);
        }
Beispiel #25
0
        public void ValidationMessageReturnsNullForInvalidName()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors());

            // Act
            string html = htmlHelper.ValidationMessage("boo");

            // Assert
            Assert.IsNull(html, "html should be null if name is invalid.");
        }
Beispiel #26
0
        public void TextBoxWithDotReplacementForId()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetTextBoxViewData());

            // Act
            string html = helper.TextBox("foo.bar.baz", null);

            // Assert
            Assert.AreEqual(@"<input id=""foo_bar_baz"" name=""foo.bar.baz"" type=""text"" value="""" />", html);
        }
Beispiel #27
0
        public void ValidationMessageReturnsWithCustomClassOverridesDefault()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors());

            // Act
            string html = htmlHelper.ValidationMessage("foo", new { @class = "my-custom-css-class" });

            // Assert
            Assert.AreEqual(@"<span class=""my-custom-css-class"">foo error &lt;1&gt;</span>", html);
        }
Beispiel #28
0
        public void RouteLinkWithRouteNameAndObjectProperties()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper();

            // Act
            string html = htmlHelper.RouteLink("linktext", "namedroute", new { Action = "newaction", Controller = "home2", id = "someid" }, new { baz = "baz" });

            // Assert
            Assert.AreEqual <string>(@"<a baz=""baz"" href=""" + AppPathModifier + @"/app/named/home2/newaction/someid"">linktext</a>", html);
        }
Beispiel #29
0
        public void RouteLinkWithRouteNameAndDictionary()
        {
            // Arrange
            HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper();

            // Act
            string html = htmlHelper.RouteLink("linktext", "namedroute", new RouteValueDictionary(new { Action = "newaction", Controller = "home2", id = "someid" }), new RouteValueDictionary());

            // Assert
            Assert.AreEqual <string>(@"<a href=""" + AppPathModifier + @"/app/named/home2/newaction/someid"">linktext</a>", html);
        }
Beispiel #30
0
        public void TextBoxWithExplicitValueNull()
        {
            // Arrange
            HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetTextBoxViewData());

            // Act
            string html = helper.TextBox("foo", (string)null /* value */, (object)null);

            // Assert
            Assert.AreEqual(@"<input id=""foo"" name=""foo"" type=""text"" value=""ViewDataFoo"" />", html);
        }