Ejemplo n.º 1
0
        public void TryValidateModel_TestAJAXLikeValidation()
        {
            TestWidget                  model            = new TestWidget();
            Type                        modelType        = model.GetType();
            List <string>               errors           = null;
            WFModelMetaData             metadata         = new WFModelMetaData();
            WFModelMetaData             metadataExpected = new WFModelMetaData();
            bool                        actual;
            Dictionary <string, string> values = new Dictionary <string, string>();

            values.Add("model_RequiredName", "reqname");
            values.Add("model_MaxLengthName", "toolongofaname");
            values.Add("model_Sprockets", "4");    //bad sprocket value
            values.Add("model_Email", "bademail"); //bad email
            values.Add("model_Price", "999");      //bad price
            // NoErrorMessage property is NOT added and should NOT be present

            actual = WFUtilities.TryValidateModel(model, "model_", new WFDictionaryValueProvider(values), metadata, new WFTypeRuleProvider(modelType));
            errors = metadata.Errors;
            Assert.AreEqual(errors.Count, 4);

            Assert.AreEqual(errors[0], "Max length 10 characters");
            Assert.AreEqual(errors[1], "Invalid number of sprockets.");
            Assert.AreEqual(errors[2], "Widget must have valid e-mail");
            //Generated error message on custom annotation
            Assert.AreEqual(errors[3], "Invalid price");

            Assert.AreEqual(actual, false);
        }
Ejemplo n.º 2
0
        public void TryValidateModel_TestAgainstModelSuccess()
        {
            TestWidget      model            = new TestWidget();
            Type            modelType        = model.GetType();
            WFModelMetaData metadata         = new WFModelMetaData();
            WFModelMetaData metadataExpected = new WFModelMetaData();
            bool            actual;

            model.Email          = "*****@*****.**";
            model.MaxLengthName  = "goodname";
            model.RequiredName   = "reqname"; //Required
            model.Sprockets      = 6;         //Good sprocket count
            model.Price          = 0.99d;     //Good price
            model.NoErrorMessage = "reqmsg";  //Required - no error message defined

            actual = WFUtilities.TryValidateModel(model, "", new WFObjectValueProvider(model, ""), metadata, new WFTypeRuleProvider(modelType));

            //No Errors
            Assert.AreEqual(metadata.Errors.Count, 0);

            //Properties are not collected when an error does not exist
            //The reason is because there is no page to collect them for
            Assert.AreEqual(metadata.Properties.Count, 0);

            Assert.AreEqual(actual, true);
        }
Ejemplo n.º 3
0
        public void TryValidateModel_TestAgainstModelClassAttribute()
        {
            ClassAttributeTestWidget model   = new ClassAttributeTestWidget();
            Type            modelType        = model.GetType();
            WFModelMetaData metadata         = new WFModelMetaData();
            WFModelMetaData metadataExpected = new WFModelMetaData();
            bool            actual;

            model.Email           = "*****@*****.**";
            model.MaxLengthName   = "goodname";
            model.RequiredName    = "reqname"; //Required
            model.Sprockets       = 6;         //Good sprocket count
            model.Price           = 0.99d;     //Good price
            model.NoErrorMessage  = "reqmsg";  //Required - no error message defined
            model.Password        = "******";
            model.ConfirmPassword = "******";
            actual = WFUtilities.TryValidateModel(model, "", new WFObjectValueProvider(model, ""), metadata, new WFTypeRuleProvider(modelType));

            //There should be one error from the model attribute
            Assert.AreEqual(metadata.Errors.Count, 1);

            //Properties are not collected when an error does not exist
            //The reason is because there is no page to collect them for
            Assert.AreEqual(metadata.Properties.Count, 0);

            Assert.AreEqual(actual, false);
        }
Ejemplo n.º 4
0
        public void RegisterXMLValidationConfiguration_TestClassLevelValidators()
        {
            WFUtilities.RegisterXMLValidationConfiguration(Environment.CurrentDirectory + "\\Validator3.config");
            TestParticipantClass tpc = new TestParticipantClass();

            tpc.Password        = "******";
            tpc.ConfirmPassword = "******";
            WFModelMetaData metadata = new WFModelMetaData();

            WFUtilities.TryValidateModel(tpc, "", new WFObjectValueProvider(tpc, ""), metadata, new WFXmlRuleSetRuleProvider("ClassLevelAttributes"));

            Assert.AreEqual("Password fields must match.", metadata.Errors[0]);
        }
Ejemplo n.º 5
0
        public void TryValidateModel_TestProxyClassValidation()
        {
            FriendlyClass fc = new FriendlyClass();

            fc.LastName = "onereallybiglongstringthatkeepsgoing"; // break validation
            List <string>   errors      = null;
            WFModelMetaData metadata    = new WFModelMetaData();
            bool            didValidate = WFUtilities.TryValidateModel(fc, "", new WFObjectValueProvider(fc, ""), metadata, new WFTypeRuleProvider(typeof(FriendlyClass)));

            errors = metadata.Errors;
            Assert.AreEqual(false, didValidate);
            Assert.AreEqual(2, errors.Count);
            Assert.AreEqual("The FirstName field is required.", errors[0]);
            Assert.AreEqual("The field LastName must be a string with a maximum length of 10.", errors[1]);
            Assert.AreEqual(2, metadata.Properties.Count);
        }
Ejemplo n.º 6
0
        public void ValidationMessageFor_LambdaTest()
        {
            TestParticipantClass tpc = GetTestParticipant();

            string vmfOK = Html.ValidationMessageFor(m => m.FirstName);

            tpc.FirstName = "";
            Html.MetaData = new WFModelMetaData(); //Reset metadata
            bool   didValidate = WFUtilities.TryValidateModel(tpc, "", new WFObjectValueProvider(tpc, ""), Html.MetaData, new WFTypeRuleProvider(typeof(ProxyMessageFromValidator)));
            string vmfError    = Html.ValidationMessageFor(m => m.FirstName);

            Assert.AreEqual("<span id = \"FirstName_validationMessage\" name = \"FirstName_validationMessage\" class = \"field-validation-valid\"></span>\r\n", vmfOK);
            Assert.AreEqual("<span id = \"FirstName_validationMessage\" name = \"FirstName_validationMessage\" class = \"field-validation-error\">The FirstName field is required.</span>\r\n", vmfError);

            Html.MetaData = new WFModelMetaData(); //Reset metadata
        }
Ejemplo n.º 7
0
        public void TryValidateModel_ErrorMessageTests()
        {
            string resourceErrorMessage = Resources.FirstName_ErrorMessage_Test1;

            TestParticipantClass tpc = new TestParticipantClass();

            WFModelMetaData metadata = new WFModelMetaData();

            WFUtilities.TryValidateModel(tpc, "", new WFObjectValueProvider(tpc, ""), metadata, new WFTypeRuleProvider(typeof(ProxyMessageFromValidator)));
            Assert.AreEqual("The FirstName field is required.", metadata.Errors[0]);
            metadata = new WFModelMetaData();
            WFUtilities.TryValidateModel(tpc, "", new WFObjectValueProvider(tpc, ""), metadata, new WFTypeRuleProvider(typeof(ProxyMessageFromConstant)));
            Assert.AreEqual("This is a constant error.", metadata.Errors[0]);
            metadata = new WFModelMetaData();
            WFUtilities.TryValidateModel(tpc, "", new WFObjectValueProvider(tpc, ""), metadata, new WFTypeRuleProvider(typeof(ProxyMessageFromResource)));
            Assert.AreEqual("This value from resource.", metadata.Errors[0]);
            metadata = new WFModelMetaData();
        }
Ejemplo n.º 8
0
        public void TryValidateModel_TestAgainstModel()
        {
            TestWidget      model            = new TestWidget();
            Type            modelType        = model.GetType();
            List <string>   errors           = null;
            List <string>   errorsExpected   = new List <string>();
            WFModelMetaData metadata         = new WFModelMetaData();
            WFModelMetaData metadataExpected = new WFModelMetaData();
            bool            expected         = false;
            bool            actual;

            model.Email          = "bademail";
            model.MaxLengthName  = "toolongofaname";
            model.RequiredName   = "";  //Required
            model.Sprockets      = 4;   //Invalid sprocket count
            model.Price          = 999; //Invalid price
            model.NoErrorMessage = "";  //Required - no error message defined

            actual = WFUtilities.TryValidateModel(model, "", new WFObjectValueProvider(model, ""), metadata, new WFTypeRuleProvider(modelType));
            errors = metadata.Errors;

            Assert.AreEqual(errors.Count, 6);
            //Generated error messages on standard annotations
            Assert.AreEqual(errors[0], "Widget name required");
            Assert.AreEqual(errors[1], "Max length 10 characters");
            Assert.AreEqual(errors[2], "Invalid number of sprockets.");
            Assert.AreEqual(errors[3], "Widget must have valid e-mail");
            //Generated error message on custom annotation
            Assert.AreEqual(errors[4], "Invalid price");
            //Auto-generated error message
            Assert.AreEqual(errors[5], "The NoErrorMessage field is required.");

            Assert.AreEqual(metadata.Properties.Count, 6);
            Assert.AreEqual(metadata.Properties[0].PropertyName, "RequiredName");
            Assert.AreEqual(metadata.Properties[1].PropertyName, "MaxLengthName");
            Assert.AreEqual(metadata.Properties[2].PropertyName, "Sprockets");
            Assert.AreEqual(metadata.Properties[3].PropertyName, "Email");
            Assert.AreEqual(metadata.Properties[4].PropertyName, "Price");
            Assert.AreEqual(metadata.Properties[5].PropertyName, "NoErrorMessage");

            Assert.AreEqual(actual, false);
        }
Ejemplo n.º 9
0
        public void TextBoxFor_LambdaTest()
        {
            TestParticipantClass tpc = GetTestParticipant();
            string lambdaStr         = Html.TextBoxFor(p => p.FirstName);

            Assert.AreEqual("<input name = \"FirstName\" id = \"FirstName\" type = \"text\" value = \"Michael\" />", lambdaStr);
            lambdaStr = Html.TextBoxFor(p => p.FirstName, new { Class = "clsTxt" });
            Assert.AreEqual("<input name = \"FirstName\" id = \"FirstName\" type = \"text\" value = \"Michael\" class = \"clsTxt\" />", lambdaStr);

            //With an error
            tpc.FirstName = "";
            Html.MetaData = new WFModelMetaData(); //Reset metadata
            bool   didValidate = WFUtilities.TryValidateModel(tpc, "", new WFObjectValueProvider(tpc, ""), Html.MetaData, new WFTypeRuleProvider(typeof(ProxyMessageFromValidator)));
            string lambdaError = Html.TextBoxFor(p => p.FirstName);

            Assert.AreEqual("<input name = \"FirstName\" id = \"FirstName\" type = \"text\" value = \"\" class = \"input-validation-error\" />", lambdaError);

            Html.MetaData = new WFModelMetaData(); //Reset metadata

            tpc.FirstName = "&Michael";
            lambdaStr     = Html.TextBoxFor(p => p.FirstName, new { Class = "clsTxt" });
            Assert.AreEqual("<input name = \"FirstName\" id = \"FirstName\" type = \"text\" value = \"&amp;Michael\" class = \"clsTxt\" />", lambdaStr);
        }
Ejemplo n.º 10
0
        public void RegisterXMLValidationConfiguration_TestXMLErrorMessage()
        {
            WFUtilities.RegisterXMLValidationConfiguration(Environment.CurrentDirectory + "\\Validator2.config");

            string resourceErrorMessage = Resources.FirstName_ErrorMessage_Test1;

            TestParticipantClass tpc = new TestParticipantClass();

            WFModelMetaData metadata = new WFModelMetaData();


            //============================= WEB FORMS SERVER CONTROL VALIDATION =================================
            Page p = new Page();

            p.Controls.Add(new TextBox()
            {
                ID = "FirstName"
            });
            p.Controls.Add(new DataAnnotationValidatorControl()
            {
                PropertyName      = "FirstName",
                ControlToValidate = "FirstName",
                XmlRuleSetName    = "MessageFromValidator",
                ErrorMessage      = "This message from control itself."
            });

            p.Validators.Add(p.Controls[1] as IValidator);


            (p.Controls[1] as BaseValidator).Validate();
            Assert.AreEqual("This message from control itself.", (p.Controls[1] as DataAnnotationValidatorControl).ErrorMessage);

            p = new Page();
            p.Controls.Add(new TextBox()
            {
                ID = "FirstName"
            });
            p.Controls.Add(new DataAnnotationValidatorControl()
            {
                PropertyName      = "FirstName",
                ControlToValidate = "FirstName",
                XmlRuleSetName    = "MessageFromXML",
            });

            p.Validators.Add(p.Controls[1] as IValidator);


            (p.Controls[1] as BaseValidator).Validate();
            Assert.AreEqual("The First Name field cannot be empty.", (p.Controls[1] as DataAnnotationValidatorControl).ErrorMessage);

            p = new Page();
            p.Controls.Add(new TextBox()
            {
                ID = "FirstName"
            });
            p.Controls.Add(new DataAnnotationValidatorControl()
            {
                PropertyName      = "FirstName",
                ControlToValidate = "FirstName",
                XmlRuleSetName    = "MessageFromResource",
            });

            p.Validators.Add(p.Controls[1] as IValidator);


            (p.Controls[1] as BaseValidator).Validate();
            Assert.AreEqual("This value from resource.", (p.Controls[1] as DataAnnotationValidatorControl).ErrorMessage);

            //====================== TryValidateModel ==============================

            WFUtilities.TryValidateModel(tpc, "", new WFObjectValueProvider(tpc, ""), metadata, new WFXmlRuleSetRuleProvider("MessageFromValidator"));
            Assert.AreEqual("The FirstName field is required.", metadata.Errors[0]);
            metadata = new WFModelMetaData();
            WFUtilities.TryValidateModel(tpc, "", new WFObjectValueProvider(tpc, ""), metadata, new WFXmlRuleSetRuleProvider("MessageFromXML"));
            Assert.AreEqual("The First Name field cannot be empty.", metadata.Errors[0]);
            metadata = new WFModelMetaData();
            WFUtilities.TryValidateModel(tpc, "", new WFObjectValueProvider(tpc, ""), metadata, new WFXmlRuleSetRuleProvider("MessageFromResource"));
            Assert.AreEqual("This value from resource.", metadata.Errors[0]);
            metadata = new WFModelMetaData();
        }