1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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 using System
.Text
.RegularExpressions
;
17 namespace Castle
.MonoRail
.Framework
.Tests
.Helpers
20 using System
.Globalization
;
22 using System
.Threading
;
24 using Castle
.MonoRail
.Framework
.Helpers
;
25 using Castle
.MonoRail
.Framework
.Tests
.Controllers
;
27 using NUnit
.Framework
;
30 public class FormHelperCheckboxFieldListTestCase
38 get { return values; }
39 set { values = value; }
43 private FormHelper helper
;
44 private Product product
;
45 private SimpleUser user
;
46 private SimpleUser
[] users
;
47 private Subscription subscription
;
48 private Month
[] months
;
49 private MockClass mock
;
54 CultureInfo en
= CultureInfo
.CreateSpecificCulture("en");
56 Thread
.CurrentThread
.CurrentCulture
= en
;
57 Thread
.CurrentThread
.CurrentUICulture
= en
;
59 helper
= new FormHelper();
61 subscription
= new Subscription();
62 mock
= new MockClass();
63 months
= new Month
[] {new Month(1, "January"), new Month(1, "February")}
;
64 product
= new Product("memory card", 10, (decimal) 12.30);
65 user
= new SimpleUser();
66 users
= new SimpleUser
[] { new SimpleUser(1, false), new SimpleUser(2, true), new SimpleUser(3, false), new SimpleUser(4, true) }
;
67 mock
.Values
= new int[] { 2, 3 }
;
69 HomeController controller
= new HomeController();
70 ControllerContext context
= new ControllerContext();
72 context
.PropertyBag
.Add("product", product
);
73 context
.PropertyBag
.Add("user", user
);
74 context
.PropertyBag
.Add("users", users
);
75 context
.PropertyBag
.Add("roles", new Role
[] { new Role(1, "a"), new Role(2, "b"), new Role(3, "c") }
);
76 context
.PropertyBag
.Add("sendemail", true);
77 context
.PropertyBag
.Add("confirmation", "abc");
78 context
.PropertyBag
.Add("fileaccess", FileAccess
.Read
);
79 context
.PropertyBag
.Add("subscription", subscription
);
80 context
.PropertyBag
.Add("months", months
);
81 context
.PropertyBag
.Add("mock", mock
);
83 helper
.SetController(controller
, context
);
87 /// Code posted on the mailing list
90 public void BugReport1()
92 FormHelper
.CheckboxList list
=
93 helper
.CreateCheckboxList("mock.Values", new int[] {1,2,3,4}
);
95 Assert
.IsNotNull(list
);
99 foreach(Object item
in list
)
101 String content
= list
.Item();
102 if (index
== 1 || index
== 2)
104 Assert
.AreEqual("<input type=\"checkbox\" id=\"mock_Values_" + index
+
105 "_\" name=\"mock.Values[" + index
+ "]\" value=\"" + item
+ "\" checked=\"checked\" />", content
);
109 Assert
.AreEqual("<input type=\"checkbox\" id=\"mock_Values_" + index
+
110 "_\" name=\"mock.Values[" + index
+ "]\" value=\"" + item
+ "\" />", content
);
118 /// Tests the subscription.Months as null and an int array as source
121 public void CheckboxFieldList1()
123 subscription
.Months
= null;
125 FormHelper
.CheckboxList list
=
126 helper
.CreateCheckboxList("subscription.Months", new int[] {1,2,3,4,5,6}
);
128 Assert
.IsNotNull(list
);
132 foreach(Object item
in list
)
134 String content
= list
.Item();
135 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months_" + index
+ "_\" name=\"subscription.Months[" + index
+ "]\" value=\"" + item
+ "\" />", content
);
141 /// Tests the subscription.Months as non null and an int array as source
144 public void CheckboxFieldList2()
146 subscription
.Months
= new int[] { 1, 2, 3, 4, 5 }
;
148 FormHelper
.CheckboxList list
=
149 helper
.CreateCheckboxList("subscription.Months", new int[] {1,2,3,4,5}
);
151 Assert
.IsNotNull(list
);
155 foreach(Object item
in list
)
157 String content
= list
.Item();
158 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months_" + index
+ "_\" " +
159 "name=\"subscription.Months[" + index
+ "]\" value=\"" + item
+ "\" checked=\"checked\" />", content
);
165 /// Tests the subscription.Months2 as null and using a <c>Month</c> array as data source
168 public void CheckboxFieldList3()
170 FormHelper
.CheckboxList list
=
171 helper
.CreateCheckboxList("subscription.Months2", months
, DictHelper
.Create("value=id"));
173 Assert
.IsNotNull(list
);
177 foreach(Month item
in list
)
179 String content
= list
.Item();
180 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index
+
181 "_\" name=\"subscription.Months2[" + index
+ "].id\" value=\"" + item
.Id
+ "\" />", content
);
187 /// Tests the subscription.Months2 as null and using a <c>Month</c> array as data source
190 public void CheckboxFieldList3a()
192 subscription
.Months3
= null;
194 FormHelper
.CheckboxList list
=
195 helper
.CreateCheckboxList("subscription.Months3", months
, DictHelper
.Create("value=id"));
197 Assert
.IsNotNull(list
);
201 foreach(Month item
in list
)
203 String content
= list
.Item();
204 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months3_" + index
+
205 "_\" name=\"subscription.Months3[" + index
+ "].id\" value=\"" + item
.Id
+ "\" />", content
);
211 /// Tests the subscription.Months2 as non null and using a <c>Month</c> array as data source
214 public void CheckboxFieldList4()
216 subscription
.Months2
= new Month
[] {new Month(3, "March")}
;
218 FormHelper
.CheckboxList list
=
219 helper
.CreateCheckboxList("subscription.Months2", months
, DictHelper
.Create("value=id"));
221 Assert
.IsNotNull(list
);
225 foreach(Month item
in list
)
227 String content
= list
.Item();
228 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index
+
229 "_\" name=\"subscription.Months2[" + index
+ "].Id\" value=\"" + item
.Id
+ "\" />", content
);
235 /// Tests the subscription.Months2 as non null and using a <c>Month</c> array as data source
236 /// but with selection
239 public void CheckboxFieldList5()
241 subscription
.Months2
= new Month
[] {new Month(1, "January"), new Month(2, "Feb") }
;
243 FormHelper
.CheckboxList list
=
244 helper
.CreateCheckboxList("subscription.Months2", months
, DictHelper
.Create("value=id"));
246 Assert
.IsNotNull(list
);
250 foreach(Month item
in list
)
252 String content
= list
.Item();
253 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months2_" + index
+
254 "_\" name=\"subscription.Months2[" + index
+ "].Id\" value=\"" + item
.Id
+ "\" checked=\"checked\" />", content
);
260 public void UsingIdPerElement()
262 subscription
.Months
= new int[] { 1, 2, 3, 4, 5 }
;
264 FormHelper
.CheckboxList list
=
265 helper
.CreateCheckboxList("subscription.Months", new int[] { 1, 2 }
);
267 Assert
.IsNotNull(list
);
271 foreach(Object item
in list
)
273 string content
= list
.Item("menu" + index
);
277 Assert
.AreEqual("<input type=\"checkbox\" id=\"menu" + index
+ "\" name=\"subscription.Months[" + index
+ "]\" value=\"" + item
+ "\" checked=\"checked\" />", content
);
281 Assert
.AreEqual("<input type=\"checkbox\" name=\"subscription.Months[" + index
+ "]\" value=\"" + item
+ "\" id=\"menu" + index
+ "\" />", content
);
288 public void CheckboxFieldListInDotNet2()
290 FormHelper
.CheckboxList list
=
291 helper
.CreateCheckboxList("subscription.Months4", months
, DictHelper
.Create("value=id"));
293 Assert
.IsNotNull(list
);
297 foreach(Month item
in list
)
299 String content
= list
.Item();
300 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months4_" + index
+ "_\" name=\"subscription.Months4[" + index
+ "].id\" value=\"" + item
.Id
+ "\" />", content
);
307 public void CheckboxFieldListInDotNet2_WithSelection()
309 subscription
.Months4
.Add(new Month(1, "January"));
310 subscription
.Months4
.Add(new Month(2, "Feb"));
312 FormHelper
.CheckboxList list
=
313 helper
.CreateCheckboxList("subscription.Months4", months
, DictHelper
.Create("value=id"));
315 Assert
.IsNotNull(list
);
319 foreach(Month item
in list
)
321 String content
= list
.Item();
322 Assert
.AreEqual("<input type=\"checkbox\" id=\"subscription_Months4_" + index
+ "_\" name=\"subscription.Months4[" + index
+ "].Id\" value=\"" + item
.Id
+ "\" checked=\"checked\" />", content
);
328 /// Test a simple checkbox list with LabelFor values
331 public void CheckBoxFieldListWithLabelFor()
333 subscription
.Months
= new int[] { 1, 2, 3, 4, 5 }
;
335 FormHelper
.CheckboxList list
=
336 helper
.CreateCheckboxList("subscription.Months", new int[] { 1, 2, 3, 4, 5 }
);
338 Assert
.IsNotNull(list
);
340 foreach (Object item
in list
)
342 string content
= list
.Item();
343 string label
= list
.LabelFor(item
.ToString());
345 Match checkboxIdMatch
= new Regex("\\s+id=\"(?<value>[^\"]+)\"(?:\\s+|>)", RegexOptions
.IgnoreCase
).Match(content
);
346 Match labelIdMatch
= new Regex("\\s+for=\"(?<value>[^\"]+)\"(?:\\s+|>)", RegexOptions
.IgnoreCase
).Match(label
);
347 Match labelContentMatch
= new Regex("<label[^>]*>(?<value>[^<]*)</label>", RegexOptions
.IgnoreCase
).Match(label
);
349 Assert
.AreEqual(checkboxIdMatch
.Groups
["value"].Value
, labelIdMatch
.Groups
["value"].Value
);
350 Assert
.AreEqual(item
.ToString(), labelContentMatch
.Groups
["value"].Value
);
355 /// Test a simple checkbox list with LabelFor values
358 public void CheckBoxFieldListWithLabelForUsingCustomLabel()
360 subscription
.Months
= new int[] { 1, 2, 3, 4, 5 }
;
362 FormHelper
.CheckboxList list
=
363 helper
.CreateCheckboxList("subscription.Months", new int[] { 1, 2, 3, 4, 5 }
);
365 Assert
.IsNotNull(list
);
367 foreach (Object item
in list
)
369 string content
= list
.Item();
370 string label
= list
.LabelFor("Item "+item
);
372 Match checkboxIdMatch
= new Regex("\\s+id=\"(?<value>[^\"]+)\"(?:\\s+|>)", RegexOptions
.IgnoreCase
).Match(content
);
373 Match labelIdMatch
= new Regex("\\s+for=\"(?<value>[^\"]+)\"(?:\\s+|>)", RegexOptions
.IgnoreCase
).Match(label
);
374 Match labelContentMatch
= new Regex("<label[^>]*>(?<value>[^<]*)</label>", RegexOptions
.IgnoreCase
).Match(label
);
376 Assert
.AreEqual(checkboxIdMatch
.Groups
["value"].Value
, labelIdMatch
.Groups
["value"].Value
);
377 Assert
.AreEqual("Item " + item
, labelContentMatch
.Groups
["value"].Value
);