Minor changes to improve testability of helpers
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Helpers / Validations / FormValidationTestCase.cs
blob71333d220be3019c54587aa467ef6f5ef7cc07b3
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Tests.Helpers.Validations
17 using System.Globalization;
18 using System.Threading;
19 using Castle.Components.Validator;
20 using Castle.MonoRail.Framework.Helpers;
21 using Castle.MonoRail.Framework.Tests.Controllers;
22 using Castle.MonoRail.TestSupport;
23 using NUnit.Framework;
25 [TestFixture]
26 public class FormValidationTestCase : BaseControllerTest
28 private FormHelper helper;
29 private ModelWithValidation model;
31 [SetUp]
32 public void Init()
34 CultureInfo en = CultureInfo.CreateSpecificCulture("en");
36 Thread.CurrentThread.CurrentCulture = en;
37 Thread.CurrentThread.CurrentUICulture = en;
39 helper = new FormHelper();
40 model = new ModelWithValidation();
42 HomeController controller = new HomeController();
43 PrepareController(controller, "", "Home", "Index");
45 controller.PropertyBag.Add("model", model);
47 helper.SetController(controller);
50 [Test]
51 public void ValidationIsGeneratedForModel()
53 helper.FormTag(DictHelper.Create("noaction=true"));
55 Assert.AreEqual("<input type=\"text\" id=\"model_nonemptyfield\" " +
56 "name=\"model.nonemptyfield\" value=\"\" class=\"required\" " +
57 "title=\"This is a required field.\" />", helper.TextField("model.nonemptyfield"));
59 Assert.AreEqual("<input type=\"text\" id=\"model_emailfield\" " +
60 "name=\"model.emailfield\" value=\"\" class=\"validate-email\" " +
61 "title=\"Email doesnt look right.\" />", helper.TextField("model.emailfield"));
63 // Attribute order cannot be guaranted, so this test may fail ocasionally
64 // Assert.AreEqual("<input type=\"text\" id=\"model_nonemptyemailfield\" " +
65 // "name=\"model.nonemptyemailfield\" value=\"\" class=\"validate-email required\" " +
66 // "title=\"Please enter a valid email address. For example fred@domain.com, This is a required field\" />", helper.TextField("model.nonemptyemailfield"));
68 helper.EndFormTag();
71 [Test]
72 public void UsingScopes()
74 helper.FormTag(DictHelper.Create("noaction=true"));
75 helper.Push("model");
77 Assert.AreEqual("<input type=\"text\" id=\"model_nonemptyfield\" " +
78 "name=\"model.nonemptyfield\" value=\"\" class=\"required\" " +
79 "title=\"This is a required field.\" />", helper.TextField("nonemptyfield"));
81 Assert.AreEqual("<input type=\"text\" id=\"model_emailfield\" " +
82 "name=\"model.emailfield\" value=\"\" class=\"validate-email\" " +
83 "title=\"Email doesnt look right.\" />", helper.TextField("emailfield"));
85 // Attribute order cannot be guaranted, so this test may fail ocasionally
86 // Assert.AreEqual("<input type=\"text\" id=\"model_nonemptyemailfield\" " +
87 // "name=\"model.nonemptyemailfield\" value=\"\" class=\"validate-email required\" " +
88 // "title=\"Please enter a valid email address. For example fred@domain.com, This is a required field\" />", helper.TextField("nonemptyemailfield"));
90 helper.Pop();
91 helper.EndFormTag();
94 [Test]
95 public void ValidationForSelects()
97 helper.FormTag(DictHelper.Create("noaction=true"));
99 Assert.AreEqual("<select id=\"model_city\" " +
100 "name=\"model.city\" class=\"validate-selection\" " +
101 "title=\"This is a required field.\" >\r\n" +
102 "<option value=\"0\">---</option>\r\n" +
103 "<option value=\"Sao Paulo\">Sao Paulo</option>\r\n" +
104 "<option value=\"Sao Carlos\">Sao Carlos</option>\r\n" +
105 "</select>",
106 helper.Select("model.city",
107 new string[] { "Sao Paulo", "Sao Carlos" }, DictHelper.Create("firstoption=---")));
109 helper.EndFormTag();
112 [Test]
113 public void ValidationAreInherited()
115 helper.FormTag(DictHelper.Create("noaction=true"));
117 Assert.AreEqual("<select id=\"model_city_id\" " +
118 "name=\"model.city.id\" class=\"validate-selection\" " +
119 "title=\"This is a required field.\" >\r\n" +
120 "<option value=\"0\">---</option>\r\n" +
121 "<option value=\"1\">1</option>\r\n" +
122 "<option value=\"2\">2</option>\r\n" +
123 "</select>",
124 helper.Select("model.city.id",
125 new string[] { "1", "2" }, DictHelper.Create("firstoption=---")));
127 helper.EndFormTag();
131 public class ModelWithValidation
133 private string nonEmptyField;
134 private string emailField;
135 private string nonEmptyEmailField;
136 private string city;
137 private Country country;
139 [ValidateNonEmpty]
140 public Country Country
142 get { return country; }
143 set { country = value; }
146 [ValidateNonEmpty]
147 public string City
149 get { return city; }
150 set { city = value; }
153 [ValidateNonEmpty]
154 public string NonEmptyField
156 get { return nonEmptyField; }
157 set { nonEmptyField = value; }
160 [ValidateEmail("Email doesnt look right")]
161 public string EmailField
163 get { return emailField; }
164 set { emailField = value; }
167 [ValidateNonEmpty, ValidateEmail]
168 public string NonEmptyEmailField
170 get { return nonEmptyEmailField; }
171 set { nonEmptyEmailField = value; }
175 public class Country
177 private int id;
179 public int Id
181 get { return id; }
182 set { id = value; }