Skip to content

Commit

Permalink
Tested and remediated Page View Models
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Einhorn committed Feb 20, 2015
1 parent 174f1ee commit 3537c14
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 3 deletions.
1 change: 0 additions & 1 deletion DVM4T.Core/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ public bool IsMatch(IViewModelData data, IViewModelKeyProvider provider)
}
}

//Consider adding abstract classes for common Fields? Could I use Dependency Injection to add the concrete implementations?
public class PageViewModelAttribute : Attribute, IPageModelAttribute
{
public PageViewModelAttribute(string[] viewModelKeys)
Expand Down
9 changes: 8 additions & 1 deletion DVM4T.Core/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public WebConfigViewModelKeyProvider(string webConfigKey)
}
}

public class PageViewModelData : IViewModelData
public class PageViewModelData : IPageViewModelData
{

public PageViewModelData(IPageData pageData, IViewModelBuilder builder)
Expand All @@ -86,6 +86,7 @@ public PageViewModelData(IPageData pageData, IViewModelBuilder builder)
Metadata = pageData.Metadata;
PublicationId = pageData.PublicationId;
Template = pageData.PageTemplate;
Page = pageData;
BaseData = pageData;
}

Expand Down Expand Up @@ -119,6 +120,12 @@ public ITemplateData Template
get;
private set;
}

public IPageData Page
{
get;
private set;
}
}
public class ComponentPresentationViewModelData : ContentViewModelData, IComponentPresentationViewModelData
{
Expand Down
2 changes: 1 addition & 1 deletion DVM4T.Core/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public ViewModelTypeNotFoundException(IContentViewModelData data)
{ }

public ViewModelTypeNotFoundException(IViewModelData data)
: base(String.Format("Could not find view model for item with Template {0} and Publication ID {1}", data.Template.Title, data.PublicationId))
: base(String.Format("Could not find view model for item with Template '{0}' and Publication ID '{1}'", data.Template.Title, data.PublicationId))
{ }
}

Expand Down
31 changes: 31 additions & 0 deletions DVM4T.DD4T/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -555,5 +555,36 @@ public override Type ExpectedReturnType
}
}

public class PresentationsByViewAttribute : ComponentPresentationsAttributeBase
{
public override System.Collections.IEnumerable GetPresentationValues(IList<IComponentPresentationData> cps, Type propertyType, IViewModelBuilder builder = null)
{
IList<IViewModel> result = ReflectionUtility.ReflectionCache.CreateInstance(propertyType) as IList<IViewModel>;
string view = null;
foreach (var cp in cps)
{
if (cp.ComponentTemplate != null && cp.ComponentTemplate.Metadata != null && cp.ComponentTemplate.Metadata.ContainsKey("view"))
{
view = cp.ComponentTemplate.Metadata["view"].Values.Cast<string>().FirstOrDefault(); //DD4T Convention for view name
if (view != null && view.StartsWith(View))
{
result.Add(builder.BuildCPViewModel(cp));
}
}
}
return result;
}
private readonly string byView;
public PresentationsByViewAttribute(string byView)
{
this.byView = byView;
}

public string View { get { return byView; } }
public override Type ExpectedReturnType
{
get { return typeof(IList<IViewModel>); }
}
}

}
10 changes: 10 additions & 0 deletions DVM4T.Tests/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,14 @@ public class BrokenViewModel : ComponentPresentationViewModelBase
public double NumberFieldExample { get; set; }

}

[PageViewModel(new string[] { "Homepage" })]
public class Homepage : ViewModelBase
{
[PresentationsByView("Carousel")]
public ViewModelList<GeneralContentViewModel> Carousels { get; set; }

[TextField("javascript", IsMetadata = true)]
public String Javascript { get; set; }
}
}
79 changes: 79 additions & 0 deletions DVM4T.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,28 @@ public void TestViewModelKey()
Assert.IsInstanceOfType(model, typeof(TitleViewModel));
}

[TestMethod]
[ExpectedException(typeof(ViewModelTypeNotFoundException))]
public void TestViewModelTypeNotFoundException()
{
string viewModelKey = "Non-existent View Model Key";
ViewModelDefaults.Builder.LoadViewModels(typeof(GeneralContentViewModel).Assembly);
var cp = GetMockCp(GetMockModel());
((Dynamic.ComponentTemplate)cp.ComponentTemplate).MetadataFields = new Dynamic.FieldSet
{
{
"viewModelKey",
new Dynamic.Field { Values = new List<string> { viewModelKey }}
}
};
((Dynamic.Component)cp.Component).Title = "test title";

//exercise
var model = ViewModelDefaults.Builder.BuildCPViewModel(new ComponentPresentation(cp));

//test
//Assert.IsInstanceOfType(model, typeof(TitleViewModel));
}
[TestMethod]
public void TestCustomKeyProvider()
{
Expand Down Expand Up @@ -323,6 +345,63 @@ public void TestOOXPM()
Assert.IsNotNull(markup);
}

[TestMethod]
public void TestBuildPageModel()
{
string expectedString = autoMocker.Create<string>();
var cp = GetCPMockup<MockModels.GeneralContentViewModel, MvcHtmlString>(x => x.Body, new MvcHtmlString(expectedString)) as Dynamic.ComponentPresentation;
cp.ComponentTemplate = new Dynamic.ComponentTemplate
{
MetadataFields = new Dynamic.FieldSet
{
{
"viewModelKey",
new Dynamic.Field { Values = new List<string> { "BasicGeneralContent"} }
},
{
"view",
new Dynamic.Field { Values = new List<string> { "CarouselGeneralContent"} }
}
},
Title = "Testing CT",
Id = "tcm:1-123-32"
};
var page = new Dynamic.Page
{
PageTemplate = new Dynamic.PageTemplate
{
MetadataFields = new Dynamic.FieldSet
{
{ "viewModelKey",
new Dynamic.Field
{
Values = new List<string> { "Homepage" },
Name = "viewModelKey",
FieldType = Dynamic.FieldType.Text,
}
}
}
},
MetadataFields = new Dynamic.FieldSet
{
{ "javascript",
new Dynamic.Field
{
Values = new List<string> { "some javascript here" },
Name = "javascript",
FieldType = Dynamic.FieldType.Text,
}
}
},
ComponentPresentations = new List<Dynamic.ComponentPresentation>
{
cp
}
};
ViewModelDefaults.Builder.LoadViewModels(typeof(Homepage).Assembly);
var pageModel = ViewModelDefaults.Builder.BuildPageViewModel(new Page(page));
Assert.IsNotNull(pageModel);
}

private TModel GetCPModelMockup<TModel, TProp>(Expression<Func<TModel, TProp>> propLambda, TProp value)
where TModel : MockContracts.IComponentPresentationViewModel
Expand Down

0 comments on commit 3537c14

Please sign in to comment.