Merge branch 'netcore/netcore' into feature/clean-up-umbraco-tests-unittests

# Conflicts:
#	src/Umbraco.Tests.UnitTests/Umbraco.Core/Components/ComponentTests.cs
#	src/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/BlockListPropertyValueConverterTests.cs
#	src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/ModelBinders/ContentModelBinderTests.cs
#	src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/ModelBinders/RenderModelBinderTests.cs
#	src/Umbraco.Tests.UnitTests/Umbraco.Web.Common/Views/UmbracoViewPageTests.cs
#	src/Umbraco.Tests.UnitTests/Umbraco.Web.Website/Controllers/RenderIndexActionSelectorAttributeTests.cs
#	src/Umbraco.Tests.UnitTests/Umbraco.Web.Website/Controllers/SurfaceControllerTests.cs
This commit is contained in:
Andy Butland
2020-12-22 11:33:39 +01:00
129 changed files with 3287 additions and 3444 deletions

View File

@@ -2,6 +2,7 @@
// See LICENSE for more details.
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
@@ -12,6 +13,7 @@ using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web.Common.ModelBinders;
using Umbraco.Web.Common.Routing;
using Umbraco.Web.Models;
namespace Umbraco.Tests.UnitTests.Umbraco.Web.Common.ModelBinders
@@ -19,58 +21,100 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Web.Common.ModelBinders
[TestFixture]
public class ContentModelBinderTests
{
private ContentModelBinder _contentModelBinder;
[SetUp]
public void SetUp() => _contentModelBinder = new ContentModelBinder();
[Test]
public void Does_Not_Bind_Model_When_UmbracoDataToken_Not_In_Route_Data()
[TestCase(typeof(IPublishedContent), false)]
[TestCase(typeof(ContentModel), false)]
[TestCase(typeof(ContentType1), false)]
[TestCase(typeof(ContentModel<ContentType1>), false)]
[TestCase(typeof(NonContentModel), true)]
[TestCase(typeof(MyCustomContentModel), true)]
[TestCase(typeof(IContentModel), true)]
public void Returns_Binder_For_IPublishedContent_And_IRenderModel(Type testType, bool expectNull)
{
var binderProvider = new ContentModelBinderProvider();
var contextMock = new Mock<ModelBinderProviderContext>();
contextMock.Setup(x => x.Metadata).Returns(new EmptyModelMetadataProvider().GetMetadataForType(testType));
IModelBinder found = binderProvider.GetBinder(contextMock.Object);
if (expectNull)
{
Assert.IsNull(found);
}
else
{
Assert.IsNotNull(found);
}
}
[Test]
public async Task Does_Not_Bind_Model_When_UmbracoToken_Not_In_Route_Values()
{
// Arrange
ModelBindingContext bindingContext = CreateBindingContext(typeof(ContentModel), withUmbracoDataToken: false);
var binder = new ContentModelBinder();
IPublishedContent pc = CreatePublishedContent();
ModelBindingContext bindingContext = CreateBindingContextForUmbracoRequest(typeof(ContentModel), pc);
bindingContext.ActionContext.RouteData.Values.Remove(Constants.Web.UmbracoRouteDefinitionDataToken);
// Act
binder.BindModelAsync(bindingContext);
await _contentModelBinder.BindModelAsync(bindingContext);
// Assert
Assert.False(bindingContext.Result.IsModelSet);
}
[Test]
public void Does_Not_Bind_Model_When_Source_Not_Of_Expected_Type()
public async Task Does_Not_Bind_Model_When_UmbracoToken_Has_Incorrect_Model()
{
// Arrange
ModelBindingContext bindingContext = CreateBindingContext(typeof(ContentModel), source: new NonContentModel());
var binder = new ContentModelBinder();
IPublishedContent pc = CreatePublishedContent();
ModelBindingContext bindingContext = CreateBindingContextForUmbracoRequest(typeof(ContentModel), pc);
bindingContext.ActionContext.RouteData.Values[Constants.Web.UmbracoRouteDefinitionDataToken] = new NonContentModel();
// Act
binder.BindModelAsync(bindingContext);
await _contentModelBinder.BindModelAsync(bindingContext);
// Assert
Assert.False(bindingContext.Result.IsModelSet);
}
[Test]
public void BindModel_Returns_If_Same_Type()
public async Task Bind_Model_When_UmbracoToken_Is_In_Route_Values()
{
// Arrange
var content = new ContentModel(CreatePublishedContent());
ModelBindingContext bindingContext = CreateBindingContext(typeof(ContentModel), source: content);
var binder = new ContentModelBinder();
IPublishedContent pc = CreatePublishedContent();
ModelBindingContext bindingContext = CreateBindingContextForUmbracoRequest(typeof(ContentModel), pc);
// Act
binder.BindModelAsync(bindingContext);
await _contentModelBinder.BindModelAsync(bindingContext);
// Assert
Assert.AreSame(content, bindingContext.Result.Model);
Assert.True(bindingContext.Result.IsModelSet);
}
[Test]
public void Throws_When_Source_Not_Of_Expected_Type()
{
// Arrange
IPublishedContent pc = CreatePublishedContent();
var bindingContext = new DefaultModelBindingContext();
// Act/Assert
Assert.Throws<ModelBindingException>(() => _contentModelBinder.BindModel(bindingContext, new NonContentModel(), typeof(ContentModel)));
}
[Test]
public void Binds_From_IPublishedContent_To_Content_Model()
{
// Arrange
ModelBindingContext bindingContext = CreateBindingContext(typeof(ContentModel), source: CreatePublishedContent());
var binder = new ContentModelBinder();
IPublishedContent pc = CreatePublishedContent();
ModelBindingContext bindingContext = new DefaultModelBindingContext();
// Act
binder.BindModelAsync(bindingContext);
_contentModelBinder.BindModel(bindingContext, pc, typeof(ContentModel));
// Assert
Assert.True(bindingContext.Result.IsModelSet);
@@ -80,23 +124,97 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Web.Common.ModelBinders
public void Binds_From_IPublishedContent_To_Content_Model_Of_T()
{
// Arrange
ModelBindingContext bindingContext = CreateBindingContext(typeof(ContentModel<ContentType1>), source: new ContentModel<ContentType2>(new ContentType2(CreatePublishedContent())));
var binder = new ContentModelBinder();
IPublishedContent pc = CreatePublishedContent();
ModelBindingContext bindingContext = new DefaultModelBindingContext();
// Act
binder.BindModelAsync(bindingContext);
_contentModelBinder.BindModel(bindingContext, new ContentModel<ContentType2>(new ContentType2(pc)), typeof(ContentModel<ContentType1>));
// Assert
Assert.True(bindingContext.Result.IsModelSet);
}
private ModelBindingContext CreateBindingContext(Type modelType, bool withUmbracoDataToken = true, object source = null)
[Test]
public void BindModel_Null_Source_Returns_Null()
{
var bindingContext = new DefaultModelBindingContext();
_contentModelBinder.BindModel(bindingContext, null, typeof(ContentType1));
Assert.IsNull(bindingContext.Result.Model);
}
[Test]
public void BindModel_Returns_If_Same_Type()
{
var content = new ContentType1(Mock.Of<IPublishedContent>());
var bindingContext = new DefaultModelBindingContext();
_contentModelBinder.BindModel(bindingContext, content, typeof(ContentType1));
Assert.AreSame(content, bindingContext.Result.Model);
}
[Test]
public void BindModel_RenderModel_To_IPublishedContent()
{
var content = new ContentType1(Mock.Of<IPublishedContent>());
var renderModel = new ContentModel(content);
var bindingContext = new DefaultModelBindingContext();
_contentModelBinder.BindModel(bindingContext, renderModel, typeof(IPublishedContent));
Assert.AreSame(content, bindingContext.Result.Model);
}
[Test]
public void BindModel_IPublishedContent_To_RenderModel()
{
var content = new ContentType1(Mock.Of<IPublishedContent>());
var bindingContext = new DefaultModelBindingContext();
_contentModelBinder.BindModel(bindingContext, content, typeof(ContentModel));
var bound = (IContentModel)bindingContext.Result.Model;
Assert.AreSame(content, bound.Content);
}
[Test]
public void BindModel_IPublishedContent_To_Generic_RenderModel()
{
var content = new ContentType1(Mock.Of<IPublishedContent>());
var bindingContext = new DefaultModelBindingContext();
_contentModelBinder.BindModel(bindingContext, content, typeof(ContentModel<ContentType1>));
var bound = (IContentModel)bindingContext.Result.Model;
Assert.AreSame(content, bound.Content);
}
[Test]
public void Null_Model_Binds_To_Null()
{
IPublishedContent pc = Mock.Of<IPublishedContent>();
var bindingContext = new DefaultModelBindingContext();
_contentModelBinder.BindModel(bindingContext, null, typeof(ContentModel));
Assert.IsNull(bindingContext.Result.Model);
}
[Test]
public void Invalid_Model_Type_Throws_Exception()
{
IPublishedContent pc = Mock.Of<IPublishedContent>();
var bindingContext = new DefaultModelBindingContext();
Assert.Throws<ModelBindingException>(() => _contentModelBinder.BindModel(bindingContext, "Hello", typeof(IPublishedContent)));
}
/// <summary>
/// Creates a binding context with the route values populated to similute an Umbraco dynamically routed request
/// </summary>
private ModelBindingContext CreateBindingContextForUmbracoRequest(Type modelType, IPublishedContent publishedContent)
{
var httpContext = new DefaultHttpContext();
var routeData = new RouteData();
if (withUmbracoDataToken)
routeData.Values.Add(Constants.Web.UmbracoRouteDefinitionDataToken, new UmbracoRouteValues(publishedContent));
{
routeData.DataTokens.Add(Constants.Web.UmbracoDataToken, source);
}
var actionContext = new ActionContext(httpContext, routeData, new ActionDescriptor());
@@ -133,5 +251,12 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Web.Common.ModelBinders
{
}
}
public class MyCustomContentModel : ContentModel
{
public MyCustomContentModel(IPublishedContent content)
: base(content)
{ }
}
}
}

View File

@@ -1,179 +0,0 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
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;
namespace Umbraco.Tests.UnitTests.Umbraco.Web.Common.ModelBinders
{
[TestFixture]
public class RenderModelBinderTests
{
private ContentModelBinder _contentModelBinder;
[SetUp]
public void SetUp() => _contentModelBinder = new ContentModelBinder();
[Test]
[TestCase(typeof(IPublishedContent), false)]
[TestCase(typeof(ContentModel), false)]
[TestCase(typeof(MyContent), false)]
[TestCase(typeof(ContentModel<MyContent>), false)]
[TestCase(typeof(MyOtherContent), true)]
[TestCase(typeof(MyCustomContentModel), true)]
[TestCase(typeof(IContentModel), true)]
public void Returns_Binder_For_IPublishedContent_And_IRenderModel(Type testType, bool expectNull)
{
var binderProvider = new ContentModelBinderProvider();
var contextMock = new Mock<ModelBinderProviderContext>();
contextMock.Setup(x => x.Metadata).Returns(new EmptyModelMetadataProvider().GetMetadataForType(testType));
IModelBinder found = binderProvider.GetBinder(contextMock.Object);
if (expectNull)
{
Assert.IsNull(found);
}
else
{
Assert.IsNotNull(found);
}
}
[Test]
public void BindModel_Null_Source_Returns_Null()
{
var bindingContext = new DefaultModelBindingContext();
_contentModelBinder.BindModelAsync(bindingContext, null, typeof(MyContent));
Assert.IsNull(bindingContext.Result.Model);
}
[Test]
public void BindModel_Returns_If_Same_Type()
{
var content = new MyContent(Mock.Of<IPublishedContent>());
var bindingContext = new DefaultModelBindingContext();
_contentModelBinder.BindModelAsync(bindingContext, content, typeof(MyContent));
Assert.AreSame(content, bindingContext.Result.Model);
}
[Test]
public void BindModel_RenderModel_To_IPublishedContent()
{
var content = new MyContent(Mock.Of<IPublishedContent>());
var renderModel = new ContentModel(content);
var bindingContext = new DefaultModelBindingContext();
_contentModelBinder.BindModelAsync(bindingContext, renderModel, typeof(IPublishedContent));
Assert.AreSame(content, bindingContext.Result.Model);
}
[Test]
public void BindModel_IPublishedContent_To_RenderModel()
{
var content = new MyContent(Mock.Of<IPublishedContent>());
var bindingContext = new DefaultModelBindingContext();
_contentModelBinder.BindModelAsync(bindingContext, content, typeof(ContentModel));
var bound = (IContentModel)bindingContext.Result.Model;
Assert.AreSame(content, bound.Content);
}
[Test]
public void BindModel_IPublishedContent_To_Generic_RenderModel()
{
var content = new MyContent(Mock.Of<IPublishedContent>());
var bindingContext = new DefaultModelBindingContext();
_contentModelBinder.BindModelAsync(bindingContext, content, typeof(ContentModel<MyContent>));
var bound = (IContentModel)bindingContext.Result.Model;
Assert.AreSame(content, bound.Content);
}
[Test]
public void No_DataToken_Returns_Null()
{
var content = new MyContent(Mock.Of<IPublishedContent>());
ModelBindingContext bindingContext = CreateBindingContext(typeof(ContentModel), false, content);
_contentModelBinder.BindModelAsync(bindingContext);
Assert.IsNull(bindingContext.Result.Model);
}
[Test]
public void Invalid_DataToken_Model_Type_Returns_Null()
{
ModelBindingContext bindingContext = CreateBindingContext(typeof(IPublishedContent), source: "Hello");
_contentModelBinder.BindModelAsync(bindingContext);
Assert.IsNull(bindingContext.Result.Model);
}
[Test]
public void IPublishedContent_DataToken_Model_Type_Uses_DefaultImplementation()
{
var content = new MyContent(Mock.Of<IPublishedContent>());
ModelBindingContext bindingContext = CreateBindingContext(typeof(MyContent), source: content);
_contentModelBinder.BindModelAsync(bindingContext);
Assert.AreEqual(content, bindingContext.Result.Model);
}
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,
};
}
public class MyCustomContentModel : ContentModel
{
public MyCustomContentModel(IPublishedContent content)
: base(content)
{
}
}
public class MyOtherContent
{
}
public class MyContent : PublishedContentWrapped
{
public MyContent(IPublishedContent content)
: base(content)
{
}
}
}
}