<commit_before>// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.MonoRail.Framework.Tests.Helpers
{
	using System;
	using System.Collections;
	using System.Collections.Generic;
	using System.Collections.Specialized;
	using System.Globalization;
	using System.IO;
	using System.Reflection;
	using System.Threading;
	using Castle.DynamicProxy;
	using Castle.MonoRail.Framework.Helpers;
	using Castle.MonoRail.Framework.Tests.Controllers;
	using NUnit.Framework;

	[TestFixture]
	public class FormHelperTestCase
	{
		private FormHelper helper;
		private Product product;
		private SimpleUser user;
		private Subscription subscription;
		private Month[] months;

		[SetUp]
		public void Init()
		{
			CultureInfo en = CultureInfo.CreateSpecificCulture("en");

			Thread.CurrentThread.CurrentCulture = en;
			Thread.CurrentThread.CurrentUICulture = en;

			helper = new FormHelper();

			subscription = new Subscription();
			months = new Month[] {new Month(1, "January"), new Month(1, "February")};
			product = new Product("memory card", 10, (decimal) 12.30);

			user = new SimpleUser();

			HomeController controller = new HomeController();
			ControllerContext context = new ControllerContext();

			context.PropertyBag.Add("product", product);
			context.PropertyBag.Add("user", user);
			context.PropertyBag.Add("roles", new Role[] { new Role(1, "a"), new Role(2, "b"), new Role(3, "c") });
			context.PropertyBag.Add("sendemail", true);
			context.PropertyBag.Add("sendemailstringtrue", "true");
			context.PropertyBag.Add("sendemailstringfalse", "false"); 
			context.PropertyBag.Add("confirmation", "abc");
			context.PropertyBag.Add("fileaccess", FileAccess.Read);
			context.PropertyBag.Add("subscription", subscription);
			context.PropertyBag.Add("months", months);

			helper.SetController(controller, context);
		}

		[Test]
		public void FormTagDoesNotMixUrlParametersWithFormElementParameters()
		{
			// No solution here. Id is ambiguous
			// helper.FormTag(DictHelper.Create("noaction=true"));
		}

		[Test]
		public void OverridingElementId()
		{
			Assert.AreEqual("<input type=\"text\" id=\"something\" name=\"product.name\" value=\"memory card\" />",
			                helper.TextField("product.name", DictHelper.Create("id=something")));

			Assert.AreEqual("<input type=\"password\" id=\"something\" name=\"product.name\" value=\"memory card\" />",
			                helper.PasswordField("product.name", DictHelper.Create("id=something")));

			Assert.AreEqual("<input type=\"hidden\" id=\"something\" name=\"product.name\" value=\"memory card\" />",
			                helper.HiddenField("product.name", DictHelper.Create("id=something")));

			product.IsAvailable = false;

			Assert.AreEqual("<input type=\"checkbox\" id=\"something\" name=\"product.isavailable\" value=\"true\" />" +
			                "<input type=\"hidden\" id=\"somethingH\" name=\"product.isavailable\" value=\"false\" />",
			                helper.CheckboxField("product.isavailable", DictHelper.Create("id=something")));

			user.IsActive = true;

			Assert.AreEqual(
				"<input type=\"radio\" id=\"something\" name=\"user.isactive\" value=\"True\" checked=\"checked\" />",
				helper.RadioField("user.isactive", true, DictHelper.Create("id=something")));

			Assert.AreEqual("<label for=\"something\">Name:</label>",
			                helper.LabelFor("product.name", "Name:", DictHelper.Create("id=something")));

			ArrayList list = new ArrayList();

			list.Add("cat1");
			list.Add("cat2");

			Assert.AreEqual("<select id=\"something\" name=\"product.category.id\" >" + Environment.NewLine +
			                "<option value=\"cat1\">cat1</option>" + Environment.NewLine + "<option value=\"cat2\">cat2</option>" +
			                Environment.NewLine + "</select>",
			                helper.Select("product.category.id", list, DictHelper.Create("id=something")));
		}

		[Test]
		public void TextField()
		{
			Assert.AreEqual("<input type=\"text\" id=\"product_name\" name=\"product.name\" value=\"memory card\" />",
			                helper.TextField("product.name"));
			Assert.AreEqual("<input type=\"text\" id=\"product_quantity\" name=\"product.quantity\" value=\"10\" />",
			                helper.TextField("product.quantity"));
		}

		[Test]
		public void TextFieldWithAutoCompletion()
		{
			Assert.AreEqual("<input type=\"text\" id=\"product_name\" name=\"product.name\" value=\"memory card\" autocomplete=\"off\" /><div id=\"product_nameautocomplete\" class=\"auto_complete\"></div><script type=\"text/javascript\">new Ajax.Autocompleter('product_name', 'product_nameautocomplete', 'someurl',{})</script>",
							helper.TextFieldAutoComplete("product.name", "someurl", DictHelper.Create(), DictHelper.Create()));
			Assert.AreEqual("<input type=\"text\" id=\"product_name\" name=\"product.name\" value=\"memory card\" autocomplete=\"on\" /><div id=\"product_nameautocomplete\" class=\"auto_complete\"></div><script type=\"text/javascript\">new Ajax.Autocompleter('product_name', 'product_nameautocomplete', 'someurl',{})</script>",
							helper.TextFieldAutoComplete("product.name", "someurl", DictHelper.Create("autocomplete=on"), DictHelper.Create()));
			Assert.IsTrue(helper.TextFieldAutoComplete("product.name", "someurl", DictHelper.Create(), DictHelper.Create()).Contains(helper.TextField("product.name", DictHelper.Create("autocomplete=off"))));
		}

		[Test]
		public void NumberField()
		{
			Assert.AreEqual(
				"<input type=\"text\" id=\"product_quantity\" name=\"product.quantity\" value=\"10\" onKeyPress=\"return monorail_formhelper_numberonly(event, [], []);\" />",
				helper.NumberField("product.quantity"));
			Assert.AreEqual(
				"<input type=\"text\" id=\"product_quantity\" name=\"product.quantity\" value=\"10\" onKeyPress=\"return monorail_formhelper_numberonly(event, [1], []);\" />",
				helper.NumberField("product.quantity", DictHelper.Create("exceptions=1")));
			Assert.AreEqual(
				"<input type=\"text\" id=\"product_quantity\" name=\"product.quantity\" value=\"10\" onKeyPress=\"return monorail_formhelper_numberonly(event, [1,2], []);\" />",
				helper.NumberField("product.quantity", DictHelper.Create("exceptions=1,2")));
		}

		[Test]
		public void MaskedNumberField()
		{
			Assert.AreEqual(
				"<input type=\"text\" id=\"product_quantity\" name=\"product.quantity\" value=\"10\" onKeyPress=\"return monorail_formhelper_numberonly(event, [], []);\" " +
				"onBlur=\"javascript:void(0);return monorail_formhelper_mask(event,this,'2,5','-');\" onKeyUp=\"javascript:void(0);return monorail_formhelper_mask(event,this,'2,5','-');\" />",
				helper.NumberField("product.quantity", DictHelper.Create("mask=2,5")));
		}

		[Test]
		public void PasswordField()
		{<commit_msg>Fixed casing on test case<commit_after>// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.MonoRail.Framework.Tests.Helpers
{
	using System;
	using System.Collections;
	using System.Collections.Generic;
	using System.Collections.Specialized;
	using System.Globalization;
	using System.IO;
	using System.Reflection;
	using System.Threading;
	using Castle.DynamicProxy;
	using Castle.MonoRail.Framework.Helpers;
	using Castle.MonoRail.Framework.Tests.Controllers;
	using NUnit.Framework;

	[TestFixture]
	public class FormHelperTestCase
	{
		private FormHelper helper;
		private Product product;
		private SimpleUser user;
		private Subscription subscription;
		private Month[] months;

		[SetUp]
		public void Init()
		{
			CultureInfo en = CultureInfo.CreateSpecificCulture("en");

			Thread.CurrentThread.CurrentCulture = en;
			Thread.CurrentThread.CurrentUICulture = en;

			helper = new FormHelper();

			subscription = new Subscription();
			months = new Month[] {new Month(1, "January"), new Month(1, "February")};
			product = new Product("memory card", 10, (decimal) 12.30);

			user = new SimpleUser();

			HomeController controller = new HomeController();
			ControllerContext context = new ControllerContext();

			context.PropertyBag.Add("product", product);
			context.PropertyBag.Add("user", user);
			context.PropertyBag.Add("roles", new Role[] { new Role(1, "a"), new Role(2, "b"), new Role(3, "c") });
			context.PropertyBag.Add("sendemail", true);
			context.PropertyBag.Add("sendemailstringtrue", "true");
			context.PropertyBag.Add("sendemailstringfalse", "false"); 
			context.PropertyBag.Add("confirmation", "abc");
			context.PropertyBag.Add("fileaccess", FileAccess.Read);
			context.PropertyBag.Add("subscription", subscription);
			context.PropertyBag.Add("months", months);

			helper.SetController(controller, context);
		}

		[Test]
		public void FormTagDoesNotMixUrlParametersWithFormElementParameters()
		{
			// No solution here. Id is ambiguous
			// helper.FormTag(DictHelper.Create("noaction=true"));
		}

		[Test]
		public void OverridingElementId()
		{
			Assert.AreEqual("<input type=\"text\" id=\"something\" name=\"product.name\" value=\"memory card\" />",
			                helper.TextField("product.name", DictHelper.Create("id=something")));

			Assert.AreEqual("<input type=\"password\" id=\"something\" name=\"product.name\" value=\"memory card\" />",
			                helper.PasswordField("product.name", DictHelper.Create("id=something")));

			Assert.AreEqual("<input type=\"hidden\" id=\"something\" name=\"product.name\" value=\"memory card\" />",
			                helper.HiddenField("product.name", DictHelper.Create("id=something")));

			product.IsAvailable = false;

			Assert.AreEqual("<input type=\"checkbox\" id=\"something\" name=\"product.isavailable\" value=\"true\" />" +
			                "<input type=\"hidden\" id=\"somethingH\" name=\"product.isavailable\" value=\"false\" />",
			                helper.CheckboxField("product.isavailable", DictHelper.Create("id=something")));

			user.IsActive = true;

			Assert.AreEqual(
				"<input type=\"radio\" id=\"something\" name=\"user.isactive\" value=\"True\" checked=\"checked\" />",
				helper.RadioField("user.isactive", true, DictHelper.Create("id=something")));

			Assert.AreEqual("<label for=\"something\">Name:</label>",
			                helper.LabelFor("product.name", "Name:", DictHelper.Create("id=something")));

			ArrayList list = new ArrayList();

			list.Add("cat1");
			list.Add("cat2");

			Assert.AreEqual("<select id=\"something\" name=\"product.category.id\" >" + Environment.NewLine +
			                "<option value=\"cat1\">cat1</option>" + Environment.NewLine + "<option value=\"cat2\">cat2</option>" +
			                Environment.NewLine + "</select>",
			                helper.Select("product.category.id", list, DictHelper.Create("id=something")));
		}

		[Test]
		public void TextField()
		{
			Assert.AreEqual("<input type=\"text\" id=\"product_name\" name=\"product.name\" value=\"memory card\" />",
			                helper.TextField("product.name"));
			Assert.AreEqual("<input type=\"text\" id=\"product_quantity\" name=\"product.quantity\" value=\"10\" />",
			                helper.TextField("product.quantity"));
		}

		[Test]
		public void TextFieldWithAutoCompletion()
		{
			Assert.AreEqual("<input type=\"text\" id=\"product_name\" name=\"product.name\" value=\"memory card\" autocomplete=\"off\" /><div id=\"product_nameautocomplete\" class=\"auto_complete\"></div><script type=\"text/javascript\">new Ajax.Autocompleter('product_name', 'product_nameautocomplete', 'someurl',{})</script>",
							helper.TextFieldAutoComplete("product.name", "someurl", DictHelper.Create(), DictHelper.Create()));
			Assert.AreEqual("<input type=\"text\" id=\"product_name\" name=\"product.name\" value=\"memory card\" autocomplete=\"on\" /><div id=\"product_nameautocomplete\" class=\"auto_complete\"></div><script type=\"text/javascript\">new Ajax.Autocompleter('product_name', 'product_nameautocomplete', 'someurl',{})</script>",
							helper.TextFieldAutoComplete("product.name", "someurl", DictHelper.Create("autocomplete=on"), DictHelper.Create()));
			Assert.IsTrue(helper.TextFieldAutoComplete("product.name", "someurl", DictHelper.Create(), DictHelper.Create()).Contains(helper.TextField("product.name", DictHelper.Create("autocomplete=off"))));
		}

		[Test]
		public void NumberField()
		{
			Assert.AreEqual(
				"<input type=\"text\" id=\"product_quantity\" name=\"product.quantity\" value=\"10\" onkeypress=\"return monorail_formhelper_numberonly(event, [], []);\" />",
				helper.NumberField("product.quantity"));
			Assert.AreEqual(
				"<input type=\"text\" id=\"product_quantity\" name=\"product.quantity\" value=\"10\" onkeypress=\"return monorail_formhelper_numberonly(event, [1], []);\" />",
				helper.NumberField("product.quantity", DictHelper.Create("exceptions=1")));
			Assert.AreEqual(
				"<input type=\"text\" id=\"product_quantity\" name=\"product.quantity\" value=\"10\" onkeypress=\"return monorail_formhelper_numberonly(event, [1,2], []);\" />",
				helper.NumberField("product.quantity", DictHelper.Create("exceptions=1,2")));
		}

		[Test]
		public void MaskedNumberField()
		{
			Assert.AreEqual(
				"<input type=\"text\" id=\"product_quantity\" name=\"product.quantity\" value=\"10\" onkeypress=\"return monorail_formhelper_numberonly(event, [], []);\" " +
				"onblur=\"javascript:void(0);return monorail_formhelper_mask(event,this,'2,5','-');\" onkeyup=\"javascript:void(0);return monorail_formhelper_mask(event,this,'2,5','-');\" />",
				helper.NumberField("product.quantity", DictHelper.Create("mask=2,5")));
		}

		[Test]
		public void PasswordField()
		{
{'fraction_non_alphanumeric': 0.17715141612200436, 'fraction_numerical': 0.00721677559912854, 'mean_word_length': 4.300974377481054, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6744824', 'n_tokens_mistral': 4684, 'n_tokens_neox': 4135, 'n_words': 832}