2020-05-08 13:23:44 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Abstractions;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
|
|
using Moq;
|
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
|
|
|
|
|
using Umbraco.Web.Common.ModelBinders;
|
|
|
|
|
|
using Umbraco.Web.Models;
|
|
|
|
|
|
|
2020-05-16 09:41:54 +02:00
|
|
|
|
namespace Umbraco.Tests.UnitTests.Umbraco.Web.Common.ModelBinders
|
2020-05-08 13:23:44 +02:00
|
|
|
|
{
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
|
public class ContentModelBinderTests
|
|
|
|
|
|
{
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Does_Not_Bind_Model_When_UmbracoDataToken_Not_In_Route_Data()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
var bindingContext = CreateBindingContext(typeof(ContentModel), withUmbracoDataToken: false);
|
|
|
|
|
|
var binder = new ContentModelBinder();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
binder.BindModelAsync(bindingContext);
|
|
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
Assert.False(bindingContext.Result.IsModelSet);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Does_Not_Bind_Model_When_Source_Not_Of_Expected_Type()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
var bindingContext = CreateBindingContext(typeof(ContentModel), source: new NonContentModel());
|
|
|
|
|
|
var binder = new ContentModelBinder();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
binder.BindModelAsync(bindingContext);
|
|
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
Assert.False(bindingContext.Result.IsModelSet);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
2020-10-19 14:34:08 +02:00
|
|
|
|
public void BindModel_Returns_If_Same_Type()
|
2020-05-08 13:23:44 +02:00
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
2020-10-19 14:34:08 +02:00
|
|
|
|
var content = new ContentModel(CreatePublishedContent());
|
|
|
|
|
|
var bindingContext = CreateBindingContext(typeof(ContentModel), source: content);
|
2020-05-08 13:23:44 +02:00
|
|
|
|
var binder = new ContentModelBinder();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
binder.BindModelAsync(bindingContext);
|
|
|
|
|
|
|
|
|
|
|
|
// Assert
|
2020-10-19 14:34:08 +02:00
|
|
|
|
Assert.AreSame(content, bindingContext.Result.Model);
|
2020-05-08 13:23:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Binds_From_IPublishedContent_To_Content_Model()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
var bindingContext = CreateBindingContext(typeof(ContentModel), source: CreatePublishedContent());
|
|
|
|
|
|
var binder = new ContentModelBinder();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
binder.BindModelAsync(bindingContext);
|
|
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
Assert.True(bindingContext.Result.IsModelSet);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Binds_From_IPublishedContent_To_Content_Model_Of_T()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
var bindingContext = CreateBindingContext(typeof(ContentModel<ContentType1>), source: new ContentModel<ContentType2>(new ContentType2(CreatePublishedContent())));
|
|
|
|
|
|
var binder = new ContentModelBinder();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
binder.BindModelAsync(bindingContext);
|
|
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
Assert.True(bindingContext.Result.IsModelSet);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private ModelBindingContext CreateBindingContext(Type modelType, bool withUmbracoDataToken = true, object source = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var httpContext = new DefaultHttpContext();
|
|
|
|
|
|
var routeData = new RouteData();
|
|
|
|
|
|
if (withUmbracoDataToken)
|
|
|
|
|
|
routeData.DataTokens.Add(Constants.Web.UmbracoDataToken, source);
|
|
|
|
|
|
|
|
|
|
|
|
var actionContext = new ActionContext(httpContext, routeData, new ActionDescriptor());
|
|
|
|
|
|
var metadataProvider = new EmptyModelMetadataProvider();
|
|
|
|
|
|
var routeValueDictionary = new RouteValueDictionary();
|
|
|
|
|
|
var valueProvider = new RouteValueProvider(BindingSource.Path, routeValueDictionary);
|
|
|
|
|
|
return new DefaultModelBindingContext
|
|
|
|
|
|
{
|
|
|
|
|
|
ActionContext = actionContext,
|
|
|
|
|
|
ModelMetadata = metadataProvider.GetMetadataForType(modelType),
|
|
|
|
|
|
ModelName = modelType.Name,
|
|
|
|
|
|
ValueProvider = valueProvider,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private class NonContentModel
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IPublishedContent CreatePublishedContent()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new ContentType2(new Mock<IPublishedContent>().Object);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class ContentType1 : PublishedContentWrapped
|
|
|
|
|
|
{
|
|
|
|
|
|
public ContentType1(IPublishedContent content) : base(content) { }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class ContentType2 : ContentType1
|
|
|
|
|
|
{
|
|
|
|
|
|
public ContentType2(IPublishedContent content) : base(content) { }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|