Esempio n. 1
0
        public void GetUnobtrusiveValidationAttributesReturnsEmptySetWhenClientValidationIsNotEnabled()
        {
            // Arrange
            var formContext = new FormContext();

            formContext.RenderedField("foobar", true);
            var viewContext = new Mock <ViewContext>();

            viewContext.SetupGet(vc => vc.FormContext).Returns(formContext);
            var viewDataContainer = MvcHelper.GetViewDataContainer(new ViewDataDictionary());
            var htmlHelper        = new HtmlHelper(viewContext.Object, viewDataContainer);

            // Act
            IDictionary <string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar");

            // Assert
            Assert.AreEqual(0, result.Count);
        }
Esempio n. 2
0
        public void ConstructorGuardClauses()
        {
            // Arrange
            var viewContext       = new Mock <ViewContext>().Object;
            var viewDataContainer = MvcHelper.GetViewDataContainer(null);

            // Act & Assert
            ExceptionHelper.ExpectArgumentNullException(
                () => new HtmlHelper(null, viewDataContainer),
                "viewContext"
                );
            ExceptionHelper.ExpectArgumentNullException(
                () => new HtmlHelper(viewContext, null),
                "viewDataContainer"
                );
            ExceptionHelper.ExpectArgumentNullException(
                () => new HtmlHelper(viewContext, viewDataContainer, null),
                "routeCollection"
                );
        }
Esempio n. 3
0
        public void GetUnobtrusiveValidationAttributesMarksRenderedFieldsWithFullName()
        {
            // Arrange
            var formContext = new FormContext();
            var viewContext = new Mock <ViewContext>();
            var viewData    = new ViewDataDictionary();

            viewContext.SetupGet(vc => vc.FormContext).Returns(formContext);
            viewContext.SetupGet(vc => vc.ClientValidationEnabled).Returns(true);
            viewContext.SetupGet(vc => vc.UnobtrusiveJavaScriptEnabled).Returns(true);
            viewData.TemplateInfo.HtmlFieldPrefix = "Prefix";
            var viewDataContainer = MvcHelper.GetViewDataContainer(viewData);
            var htmlHelper        = new HtmlHelper(viewContext.Object, viewDataContainer);

            // Act
            htmlHelper.GetUnobtrusiveValidationAttributes("foobar");

            // Assert
            Assert.IsFalse(formContext.RenderedField("foobar"));
            Assert.IsTrue(formContext.RenderedField("Prefix.foobar"));
        }
Esempio n. 4
0
        public void GetUnobtrusiveValidationAttributesReturnsEmptySetAndSetsFieldAsRenderedForFieldWithNoClientRules()
        {
            // Arrange
            var formContext = new FormContext();
            var viewContext = new Mock <ViewContext>();

            viewContext.SetupGet(vc => vc.FormContext).Returns(formContext);
            viewContext.SetupGet(vc => vc.ClientValidationEnabled).Returns(true);
            viewContext.SetupGet(vc => vc.UnobtrusiveJavaScriptEnabled).Returns(true);
            var viewDataContainer = MvcHelper.GetViewDataContainer(new ViewDataDictionary());
            var htmlHelper        = new HtmlHelper(viewContext.Object, viewDataContainer);

            htmlHelper.ClientValidationRuleFactory = delegate { return(Enumerable.Empty <ModelClientValidationRule>()); };

            // Act
            IDictionary <string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar");

            // Assert
            Assert.AreEqual(0, result.Count);
            Assert.IsTrue(formContext.RenderedField("foobar"));
        }
Esempio n. 5
0
        public void GetUnobtrusiveValidationAttributeUsesViewDataForModelMetadataLookup()
        {
            // Arrange
            var formContext = new FormContext();
            var viewContext = new Mock <ViewContext>();
            var viewData    = new ViewDataDictionary <MyModel>();

            viewContext.SetupGet(vc => vc.FormContext).Returns(formContext);
            viewContext.SetupGet(vc => vc.ClientValidationEnabled).Returns(true);
            viewContext.SetupGet(vc => vc.UnobtrusiveJavaScriptEnabled).Returns(true);
            viewData.TemplateInfo.HtmlFieldPrefix = "Prefix";
            var viewDataContainer = MvcHelper.GetViewDataContainer(viewData);
            var htmlHelper        = new HtmlHelper(viewContext.Object, viewDataContainer);

            // Act
            IDictionary <string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("MyProperty");

            // Assert
            Assert.AreEqual(2, result.Count);
            Assert.AreEqual("true", result["data-val"]);
            Assert.AreEqual("My required message", result["data-val-required"]);
        }
Esempio n. 6
0
        public void HttpMethodOverrideGuardClauses()
        {
            // Arrange
            var viewContext       = new Mock <ViewContext>().Object;
            var viewDataContainer = MvcHelper.GetViewDataContainer(null);
            var htmlHelper        = new HtmlHelper(viewContext, viewDataContainer);

            // Act & Assert
            ExceptionHelper.ExpectArgumentExceptionNullOrEmpty(
                () => htmlHelper.HttpMethodOverride(null),
                "httpMethod"
                );
            ExceptionHelper.ExpectArgumentException(
                () => htmlHelper.HttpMethodOverride((HttpVerbs)10000),
                @"The specified HttpVerbs value is not supported. The supported values are Delete, Head, and Put.
Parameter name: httpVerb"
                );
            ExceptionHelper.ExpectArgumentException(
                () => htmlHelper.HttpMethodOverride(HttpVerbs.Get),
                @"The specified HttpVerbs value is not supported. The supported values are Delete, Head, and Put.
Parameter name: httpVerb"
                );
            ExceptionHelper.ExpectArgumentException(
                () => htmlHelper.HttpMethodOverride(HttpVerbs.Post),
                @"The specified HttpVerbs value is not supported. The supported values are Delete, Head, and Put.
Parameter name: httpVerb"
                );
            ExceptionHelper.ExpectArgumentException(
                () => htmlHelper.HttpMethodOverride("gEt"),
                @"The GET and POST HTTP methods are not supported.
Parameter name: httpMethod"
                );
            ExceptionHelper.ExpectArgumentException(
                () => htmlHelper.HttpMethodOverride("pOsT"),
                @"The GET and POST HTTP methods are not supported.
Parameter name: httpMethod"
                );
        }
Esempio n. 7
0
        public void GetUnobtrusiveValidationAttributesMessageIsHtmlEncoded()
        {
            // Arrange
            var formContext = new FormContext();
            var viewContext = new Mock <ViewContext>();

            viewContext.SetupGet(vc => vc.FormContext).Returns(formContext);
            viewContext.SetupGet(vc => vc.ClientValidationEnabled).Returns(true);
            viewContext.SetupGet(vc => vc.UnobtrusiveJavaScriptEnabled).Returns(true);
            var viewDataContainer = MvcHelper.GetViewDataContainer(new ViewDataDictionary());
            var htmlHelper        = new HtmlHelper(viewContext.Object, viewDataContainer);

            htmlHelper.ClientValidationRuleFactory = delegate {
                return(new[] { new ModelClientValidationRule {
                                   ValidationType = "type", ErrorMessage = "<script>alert('xss')</script>"
                               } });
            };

            // Act
            IDictionary <string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar");

            // Assert
            Assert.AreEqual("&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;", result["data-val-type"]);
        }
Esempio n. 8
0
        public void GetUnobtrusiveValidationAttributesIncludesDataValTrueWithNonEmptyClientRuleList()
        {
            // Arrange
            var formContext = new FormContext();
            var viewContext = new Mock <ViewContext>();

            viewContext.SetupGet(vc => vc.FormContext).Returns(formContext);
            viewContext.SetupGet(vc => vc.ClientValidationEnabled).Returns(true);
            viewContext.SetupGet(vc => vc.UnobtrusiveJavaScriptEnabled).Returns(true);
            var viewDataContainer = MvcHelper.GetViewDataContainer(new ViewDataDictionary());
            var htmlHelper        = new HtmlHelper(viewContext.Object, viewDataContainer);

            htmlHelper.ClientValidationRuleFactory = delegate {
                return(new[] { new ModelClientValidationRule {
                                   ValidationType = "type"
                               } });
            };

            // Act
            IDictionary <string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar");

            // Assert
            Assert.AreEqual("true", result["data-val"]);
        }