public void ValidationSummaryWithObjectAttributesWithUnderscores() { // Arrange HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(GetViewDataWithModelErrors()); // Act MvcHtmlString html = htmlHelper.ValidationSummary(null /* message */, new { foo_baz = "baz" }); // Assert Assert.Equal( "<div class=\"validation-summary-errors\" foo-baz=\"baz\"><ul><li>foo error <1></li>" + Environment.NewLine + "<li>foo error 2</li>" + Environment.NewLine + "<li>bar error <1></li>" + Environment.NewLine + "<li>bar error 2</li>" + Environment.NewLine + "</ul></div>", html.ToHtmlString()); }
public void ValidationSummaryWithNoErrors_EmptyUlIfClientValidationEnabled() { // Arrange HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(new ViewDataDictionary()); htmlHelper.ViewContext.ClientValidationEnabled = true; htmlHelper.ViewContext.FormContext = new FormContext(); // Act MvcHtmlString html = htmlHelper.ValidationSummary(); // Assert Assert.Equal( "<div class=\"validation-summary-valid\" id=\"validationSummary\"><ul><li style=\"display:none\"></li>" + Environment.NewLine + "</ul></div>", html.ToHtmlString()); }
public void ValidationSummaryWithObjectAttributesAndMessage() { // Arrange HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(GetViewDataWithModelErrors()); // Act MvcHtmlString html = htmlHelper.ValidationSummary("This is my message.", new { baz = "baz" }); // Assert Assert.Equal(@"<div baz=""baz"" class=""validation-summary-errors""><span>This is my message.</span> <ul><li>foo error <1></li> <li>foo error 2</li> <li>bar error <1></li> <li>bar error 2</li> </ul></div>", html.ToHtmlString()); }
public void DisplayNameForModelUsesModelMetadata() { // Arrange ViewDataDictionary viewData = new ViewDataDictionary(); Mock <ModelMetadata> metadata = new MetadataHelper().Metadata; metadata.Setup(m => m.DisplayName).Returns("Custom display name from metadata"); viewData.ModelMetadata = metadata.Object; viewData.TemplateInfo.HtmlFieldPrefix = "Prefix"; // Act MvcHtmlString result = MvcHelper.GetHtmlHelper(viewData).DisplayNameForModel(); // Assert Assert.Equal("Custom display name from metadata", result.ToHtmlString()); }
public void RadioButtonListWithDictionaryAttributesWithInvalidSelectListFromViewData() { // Arrange ViewDataDictionary viewData = new ViewDataDictionary(); viewData["FooList"] = "FOOBAR3"; HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(viewData); // Act / Assert Assert.Throws <InvalidOperationException>( () => htmlHelper.RadioButtonList( "FooList", new RouteValueDictionary(new { attr1 = "value1" }) ) ); }
public void ValidationErrorOrdering() { // Arrange HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(GetViewDataWithModelWithDisplayOrderErrors()); // Act MvcHtmlString html = htmlHelper.ValidationSummary(); // Assert Assert.Equal( "<div class=\"validation-summary-errors\"><ul><li>Error 1</li>" + Environment.NewLine + "<li>Error 2</li>" + Environment.NewLine + "<li>Error 3</li>" + Environment.NewLine + "<li>Error 4</li>" + Environment.NewLine + "</ul></div>", html.ToHtmlString()); }
public void DisplayNameUsesMetadataForDisplayText() { // Arrange MetadataHelper metadataHelper = new MetadataHelper(); metadataHelper.Metadata .Setup(m => m.DisplayName) .Returns("Custom display name from metadata"); // Act MvcHtmlString result = MvcHelper .GetHtmlHelper() .DisplayNameInternal("PropertyName", metadataHelper.MetadataProvider.Object); // Assert Assert.Equal("Custom display name from metadata", result.ToHtmlString()); }
public void HtmlBuilder_SelfCloseTagStart_SingleAttribute_DashesQuotes() { // Arrange var viewModel = new EquipmentViewModel { Item = new EquipmentModel(), CategoriesList = Enumerable.Empty <CategoryModel>() }; var viewData = new ViewDataDictionary(viewModel); var html = MvcHelper.GetHtmlHelper(viewModel, new EquipmentController(null, null), true); // Act var element = html.BeginElementSelfClose("input", new { @class = "justice", data_characters_allowed = "\"'" }); // Assert Assert.AreEqual("<input class='justice' data-characters-allowed='\"\\''/>", element.ToString()); }
public void HtmlBuilder_PlainTagStart() { // Arrange var viewModel = new EquipmentViewModel { Item = new EquipmentModel(), CategoriesList = Enumerable.Empty <CategoryModel>() }; var viewData = new ViewDataDictionary(viewModel); var html = MvcHelper.GetHtmlHelper(viewModel, new EquipmentController(null, null), true); // Act var element = html.BeginElement("div"); // Assert Assert.AreEqual("<div>", element.ToString()); }
public void StronglyTypedWithNoPrefix() { // Arrange HtmlHelper <OuterClass> html = MvcHelper.GetHtmlHelper( new ViewDataDictionary <OuterClass>() ); // Act & Assert Assert.Equal("IntValue", html.IdFor(m => m.IntValue).ToHtmlString()); Assert.Equal("Inner_StringValue", html.IdFor(m => m.Inner.StringValue).ToHtmlString()); Assert.Equal("IntValue", html.NameFor(m => m.IntValue).ToHtmlString()); Assert.Equal( "Inner.StringValue", html.NameFor(m => m.Inner.StringValue).ToHtmlString() ); }
public void HtmlBuilder_PlainTagStart_SingleAttribute_DashesNoQuotes() { // Arrange var viewModel = new EquipmentViewModel { Item = new EquipmentModel(), CategoriesList = Enumerable.Empty <CategoryModel>() }; var viewData = new ViewDataDictionary(viewModel); var html = MvcHelper.GetHtmlHelper(viewModel, new EquipmentController(null, null), true); // Act var element = html.BeginElement("div", new { @class = "justice", data_model = "template" }); // Assert Assert.AreEqual("<div class='justice' data-model='template'>", element.ToString()); }
public void ActionLinkWithControllerNameAndObjectProperties_AttributeEncodes_AddedHtmlAttributes( string text, bool htmlEncode, string expectedText) { // Arrange var helper = MvcHelper.GetHtmlHelper(); // Act var result = helper.ActionLink("text", "action", "controller", routeValues: null, htmlAttributes: new { attribute = text }); // Assert Assert.Equal( @"<a attribute=""" + expectedText + @""" href=""" + AppPathModifier + @"/app/controller/action"">text</a>", result.ToHtmlString()); }
public void ValidationSummary() { // Arrange HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(GetViewDataWithModelErrors()); // Act MvcHtmlString html = htmlHelper.ValidationSummary(); // Assert Assert.Equal( "<div class=\"validation-summary-errors\"><ul><li>foo error <1></li>" + Environment.NewLine + "<li>foo error 2</li>" + Environment.NewLine + "<li>bar error <1></li>" + Environment.NewLine + "<li>bar error 2</li>" + Environment.NewLine + "</ul></div>", html.ToHtmlString()); }
public void ActionLinkWithActionNameAndValueObject() { // Arrange HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(); // Act MvcHtmlString html = htmlHelper.ActionLink( "linktext", "newaction", new { controller = "home2" } ); // Assert Assert.Equal( @"<a href=""" + AppPathModifier + @"/app/home2/newaction"">linktext</a>", html.ToHtmlString() ); }
public void RouteLinkWithRouteNameAndDefaults() { // Arrange HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(); // Act MvcHtmlString html = htmlHelper.RouteLink( "linktext", "namedroute", new { Action = "newaction" } ); // Assert Assert.Equal( @"<a href=""" + AppPathModifier + @"/app/named/home/newaction"">linktext</a>", html.ToHtmlString() ); }
public void TextAreaForWithOutOfRangeRowsThrows() { // Arrange HtmlHelper <TextAreaModel> helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); // Act & Assert Assert.ThrowsArgumentOutOfRange( () => helper.TextAreaFor( m => m.foo, -1, 0, null /* htmlAttributes */ ), "rows", "The value must be greater than or equal to zero." ); }
public void TextAreaForWithPrefixAndEmptyName() { // Arrange HtmlHelper <TextAreaModel> helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.TextAreaFor(m => m); // Assert Assert.Equal( "<textarea cols=\"20\" id=\"MyPrefix\" name=\"MyPrefix\" rows=\"2\">" + Environment.NewLine + "System.Web.Mvc.Html.Test.TextAreaExtensionsTest+TextAreaModel</textarea>", html.ToHtmlString() ); }
public void DisplayText_HonoursHtmlEncode_ForProperty(string text, bool htmlEncode, string expectedResult) { // Arrange var model = new DontHtmlEncodeModel { Encoded = text, NotEncoded = text, }; var viewData = new ViewDataDictionary <DontHtmlEncodeModel>(model); var helper = MvcHelper.GetHtmlHelper(viewData); var propertyName = htmlEncode ? "Encoded" : "NotEncoded"; // Act MvcHtmlString result = helper.DisplayText(propertyName); // Assert Assert.Equal(expectedResult, result.ToHtmlString()); }
public void MenuItemExtension_ReturnedCorrectHtml() { string actionName = "View"; string controllerName = "Profile"; int id = 12; string linkText = "Профиль"; StringBuilder exceptedHtml = new StringBuilder(); exceptedHtml.Append("<li class=''>"); exceptedHtml.Append($@"<a href=""{AppPathModifier}/{controllerName}/{actionName}/{id}"">"); exceptedHtml.Append($"{linkText}</a></li>"); var htmlHelper = MvcHelper.GetHtmlHelper(); var actualHtml = htmlHelper.MenuItem(linkText, actionName, controllerName, new { Id = id }, null); Assert.AreEqual(exceptedHtml.ToString(), actualHtml.ToHtmlString(), "Html is not correct"); }
public void NameHelpers_AttributeEncode_Prefix(string text, string expectedText) { // Arrange var viewData = new ViewDataDictionary <string>(model: null); viewData.TemplateInfo.HtmlFieldPrefix = text; var helper = MvcHelper.GetHtmlHelper <string>(viewData); // Act var nameResult = helper.Name(""); var nameForResult = helper.NameFor(m => m); var nameForModelResult = helper.NameForModel(); // Assert Assert.Equal(expectedText, nameResult.ToHtmlString()); Assert.Equal(expectedText, nameForResult.ToHtmlString()); Assert.Equal(expectedText, nameForModelResult.ToHtmlString()); }
public void TextAreaForWithViewDataErrorsAndCustomClass() { // Arrange HtmlHelper <TextAreaModel> helper = MvcHelper.GetHtmlHelper( GetTextAreaViewDataWithErrors() ); // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo, new { @class = "foo-class" }); // Assert Assert.Equal( "<textarea class=\"input-validation-error foo-class\" cols=\"20\" id=\"foo\" name=\"foo\" rows=\"2\">" + Environment.NewLine + "AttemptedValueFoo</textarea>", html.ToHtmlString() ); }
public void LinkGenerationDoesNotChangeProvidedDictionary() { // Arrange HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(); RouteValueDictionary valuesDictionary = new RouteValueDictionary(); // Act htmlHelper.ActionLink( "linkText", "actionName", valuesDictionary, new RouteValueDictionary() ); // Assert Assert.Empty(valuesDictionary); Assert.False(valuesDictionary.ContainsKey("action")); }
public void TextAreaForWithPrefix() { // Arrange HtmlHelper <TextAreaModel> helper = MvcHelper.GetHtmlHelper(GetTextAreaViewData()); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.TextAreaFor(m => m.foo); // Assert Assert.Equal( "<textarea cols=\"20\" id=\"MyPrefix_foo\" name=\"MyPrefix.foo\" rows=\"2\">" + Environment.NewLine + "ViewItemFoo</textarea>", html.ToHtmlString() ); }
public void TextAreaWithPrefixAndEmptyName() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act MvcHtmlString html = helper.TextArea("", "bar"); // Assert Assert.Equal( "<textarea cols=\"20\" id=\"MyPrefix\" name=\"MyPrefix\" rows=\"2\">" + Environment.NewLine + "bar</textarea>", html.ToHtmlString() ); }
public void ValueHelpers_AttributeEncode_Value(string text, bool htmlEncode, string encodedText) { // Arrange var viewData = new ViewDataDictionary <string>(text); viewData.ModelMetadata.HtmlEncode = htmlEncode; var helper = MvcHelper.GetHtmlHelper(viewData); // Act var valueResult = helper.Value("").ToHtmlString(); var valueForResult = helper.ValueFor(m => m).ToHtmlString(); var valueForModelResult = helper.ValueForModel().ToHtmlString(); // Assert Assert.Equal(encodedText, valueResult); Assert.Equal(encodedText, valueForResult); Assert.Equal(encodedText, valueForModelResult); }
public void CollectionTemplateWrappingObjectTemplate_DoesNotEncodeNullDisplayText_IfNull( string text, bool htmlEncode, string unusedText) { // Arrange var model = new[] { (ObjectTemplateModel)null, }; var viewData = new ViewDataDictionary <ObjectTemplateModel[]>(model); var html = MvcHelper.GetHtmlHelper(viewData); // GetHtmlHelper does not mock enough of the ViewContext for TemplateHelpers use. var viewContext = Mock.Get(html.ViewContext); viewContext.Setup(c => c.TempData).Returns(new TempDataDictionary()); viewContext.Setup(c => c.View).Returns(new DummyView()); viewContext.Setup(c => c.Writer).Returns(TextWriter.Null); // Developers might need to do something similar (including MetadataOverrideScope or another approach // replacing ModelMetadataProviders.Current) since for example [DisplayFormat] cannot be applied to a class. var metadata = ModelMetadataProviders.Current.GetMetadataForType(() => null, typeof(ObjectTemplateModel)); metadata.HtmlEncode = htmlEncode; metadata.NullDisplayText = text; string editorResult; string editorForResult; string editorForModelResult; using (new TemplateHelpersSafeScope()) { using (new MetadataOverrideScope(metadata)) { // Act editorResult = html.Editor("").ToHtmlString(); editorForResult = html.EditorFor(m => m).ToHtmlString(); editorForModelResult = html.EditorForModel().ToHtmlString(); } } // Assert Assert.Equal(text, editorResult); Assert.Equal(text, editorForResult); Assert.Equal(text, editorForModelResult); }
public void TextAreaParameterDictionaryMerging_Unobtrusive() { // Arrange HtmlHelper helper = MvcHelper.GetHtmlHelper(); helper.ViewContext.ClientValidationEnabled = true; helper.ViewContext.UnobtrusiveJavaScriptEnabled = true; helper.ViewContext.FormContext = new FormContext(); helper.ClientValidationRuleFactory = (name, metadata) => new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" } }; // Act MvcHtmlString html = helper.TextArea("foo", new { rows = "30" }); // Assert Assert.Equal(@"<textarea cols=""20"" data-val=""true"" data-val-type=""error"" id=""foo"" name=""foo"" rows=""30""> </textarea>", html.ToHtmlString()); }
public void ValidationSummaryWithDictionary() { // Arrange HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(GetViewDataWithModelErrors()); RouteValueDictionary htmlAttributes = new RouteValueDictionary(); htmlAttributes["class"] = "my-class"; // Act MvcHtmlString html = htmlHelper.ValidationSummary(null /* message */, htmlAttributes); // Assert Assert.AreEqual(@"<div class=""validation-summary-errors my-class""><ul><li>foo error <1></li> <li>foo error 2</li> <li>bar error <1></li> <li>bar error 2</li> </ul></div>" , html.ToHtmlString()); }
public void ActionLinkExplictValuesOverrideDictionary() { // Arrange HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(); // Act MvcHtmlString html = htmlHelper.ActionLink( "linktext", "explicitAction", new { action = "dictionaryAction" }, null ); // Assert Assert.Equal( @"<a href=""" + AppPathModifier + @"/app/home/explicitAction"">linktext</a>", html.ToHtmlString() ); }
public void DisplayNameForNonMemberExpressionThrows() { // Act & Assert Assert.Throws <InvalidOperationException>( () => MvcHelper.GetHtmlHelper().DisplayNameFor(model => new { foo = "Bar" }), "Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions." ); Assert.Throws <InvalidOperationException>( () => GetEnumerableHtmlHelper() .DisplayNameFor( (Expression <Func <IEnumerable <Foo>, object> >)( model => new { foo = "Bar" } ) ), "Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions." ); }