Files
Umbraco-CMS/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs

204 lines
7.5 KiB
C#
Raw Normal View History

2017-07-20 11:21:28 +02:00
using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Moq;
using NUnit.Framework;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Dictionary;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Services;
using Umbraco.Tests.TestHelpers;
2016-11-07 19:12:09 +01:00
using Umbraco.Tests.TestHelpers.Stubs;
using Umbraco.Tests.Testing;
using Umbraco.Web;
2017-05-30 18:13:11 +02:00
using Umbraco.Web.Composing;
using Umbraco.Web.Mvc;
using Umbraco.Web.PublishedCache;
using Umbraco.Web.Routing;
using Umbraco.Web.Security;
namespace Umbraco.Tests.Web.Mvc
{
[TestFixture]
[UmbracoTest(WithApplication = true)]
public class SurfaceControllerTests : UmbracoTestBase
{
2016-10-13 21:08:07 +02:00
public override void SetUp()
2016-06-09 11:57:13 +02:00
{
2016-10-13 21:08:07 +02:00
base.SetUp();
Current.UmbracoContextAccessor = new TestUmbracoContextAccessor();
2016-06-09 11:57:13 +02:00
}
[Test]
public void Can_Construct_And_Get_Result()
{
2016-10-13 21:08:07 +02:00
var umbracoContext = UmbracoContext.EnsureContext(
2016-10-18 17:09:26 +02:00
Current.UmbracoContextAccessor,
new Mock<HttpContextBase>().Object,
2017-10-31 12:48:24 +01:00
Mock.Of<IPublishedSnapshotService>(),
new Mock<WebSecurity>(null, null).Object,
2016-10-13 21:08:07 +02:00
TestObjects.GetUmbracoSettings(),
Enumerable.Empty<IUrlProvider>(),
true);
2016-10-13 21:08:07 +02:00
var ctrl = new TestSurfaceController { UmbracoContext = umbracoContext };
var result = ctrl.Index();
Assert.IsNotNull(result);
}
[Test]
public void Umbraco_Context_Not_Null()
{
var umbCtx = UmbracoContext.EnsureContext(
2016-10-18 17:09:26 +02:00
Current.UmbracoContextAccessor,
new Mock<HttpContextBase>().Object,
2017-10-31 12:48:24 +01:00
Mock.Of<IPublishedSnapshotService>(),
new Mock<WebSecurity>(null, null).Object,
2016-10-13 21:08:07 +02:00
TestObjects.GetUmbracoSettings(),
Enumerable.Empty<IUrlProvider>(),
true);
var ctrl = new TestSurfaceController { UmbracoContext = umbCtx };
Assert.IsNotNull(ctrl.UmbracoContext);
}
[Test]
public void Umbraco_Helper_Not_Null()
{
2016-10-13 21:08:07 +02:00
var umbracoContext = UmbracoContext.EnsureContext(
2016-10-18 17:09:26 +02:00
Current.UmbracoContextAccessor,
new Mock<HttpContextBase>().Object,
2017-10-31 12:48:24 +01:00
Mock.Of<IPublishedSnapshotService>(),
new Mock<WebSecurity>(null, null).Object,
2016-10-13 21:08:07 +02:00
TestObjects.GetUmbracoSettings(),
Enumerable.Empty<IUrlProvider>(),
true);
2016-10-13 21:08:07 +02:00
var controller = new TestSurfaceController { UmbracoContext = umbracoContext };
Container.Register(_ => umbracoContext);
Container.InjectProperties(controller);
2016-10-13 21:08:07 +02:00
Assert.IsNotNull(controller.Umbraco);
}
[Test]
public void Can_Lookup_Content()
{
2017-10-31 12:48:24 +01:00
var publishedSnapshot = new Mock<IPublishedShapshot>();
publishedSnapshot.Setup(x => x.MemberCache).Returns(Mock.Of<IPublishedMemberCache>());
var publishedSnapshotService = new Mock<IPublishedSnapshotService>();
publishedSnapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny<string>())).Returns(publishedSnapshot.Object);
2016-05-26 19:20:33 +02:00
2016-10-13 21:08:07 +02:00
var umbracoContext = UmbracoContext.EnsureContext(
2016-10-18 17:09:26 +02:00
Current.UmbracoContextAccessor,
new Mock<HttpContextBase>().Object,
2017-10-31 12:48:24 +01:00
publishedSnapshotService.Object,
new Mock<WebSecurity>(null, null).Object,
Mock.Of<IUmbracoSettingsSection>(section => section.WebRouting == Mock.Of<IWebRoutingSection>(routingSection => routingSection.UrlProviderMode == "AutoLegacy")),
Enumerable.Empty<IUrlProvider>(),
true);
var helper = new UmbracoHelper(
2016-10-13 21:08:07 +02:00
umbracoContext,
Mock.Of<IPublishedContent>(),
2016-06-30 18:35:43 +02:00
Mock.Of<IPublishedContentQuery>(query => query.Content(It.IsAny<int>()) ==
//return mock of IPublishedContent for any call to GetById
Mock.Of<IPublishedContent>(content => content.Id == 2)),
Mock.Of<ITagQuery>(),
Mock.Of<IDataTypeService>(),
Mock.Of<ICultureDictionary>(),
Mock.Of<IUmbracoComponentRenderer>(),
2016-10-13 21:08:07 +02:00
new MembershipHelper(umbracoContext, Mock.Of<MembershipProvider>(), Mock.Of<RoleProvider>()),
new ServiceContext(),
CacheHelper.CreateDisabledCacheHelper());
2016-10-13 21:08:07 +02:00
var ctrl = new TestSurfaceController { UmbracoContext = umbracoContext, Umbraco = helper };
var result = ctrl.GetContent(2) as PublishedContentResult;
Assert.IsNotNull(result);
Assert.AreEqual(2, result.Content.Id);
}
[Test]
public void Mock_Current_Page()
{
var webRoutingSettings = Mock.Of<IWebRoutingSection>(section => section.UrlProviderMode == "AutoLegacy");
2016-10-13 21:08:07 +02:00
var umbracoContext = UmbracoContext.EnsureContext(
2016-10-18 17:09:26 +02:00
Current.UmbracoContextAccessor,
new Mock<HttpContextBase>().Object,
2017-10-31 12:48:24 +01:00
Mock.Of<IPublishedSnapshotService>(),
new Mock<WebSecurity>(null, null).Object,
Mock.Of<IUmbracoSettingsSection>(section => section.WebRouting == webRoutingSettings),
Enumerable.Empty<IUrlProvider>(),
true);
var content = Mock.Of<IPublishedContent>(publishedContent => publishedContent.Id == 12345);
2016-10-13 21:08:07 +02:00
var contextBase = umbracoContext.HttpContext;
2017-10-31 12:48:24 +01:00
var publishedRouter = BaseWebTest.CreatePublishedRouter(TestObjects.GetUmbracoSettings().WebRouting);
var frequest = publishedRouter.CreateRequest(umbracoContext, new Uri("http://localhost/test"));
frequest.PublishedContent = content;
var routeDefinition = new RouteDefinition
{
PublishedContentRequest = frequest
};
var routeData = new RouteData();
routeData.DataTokens.Add(Core.Constants.Web.UmbracoRouteDefinitionDataToken, routeDefinition);
2016-10-13 21:08:07 +02:00
var ctrl = new TestSurfaceController { UmbracoContext = umbracoContext, Umbraco = new UmbracoHelper() };
ctrl.ControllerContext = new ControllerContext(contextBase, routeData, ctrl);
var result = ctrl.GetContentFromCurrentPage() as PublishedContentResult;
Assert.AreEqual(12345, result.Content.Id);
}
public class TestSurfaceController : SurfaceController
{
public ActionResult Index()
{
return View();
}
public ActionResult GetContent(int id)
{
2016-06-30 18:35:43 +02:00
var content = Umbraco.Content(id);
return new PublishedContentResult(content);
}
public ActionResult GetContentFromCurrentPage()
{
var content = CurrentPage;
return new PublishedContentResult(content);
}
}
public class PublishedContentResult : ActionResult
{
public IPublishedContent Content { get; set; }
public PublishedContentResult(IPublishedContent content)
{
Content = content;
}
public override void ExecuteResult(ControllerContext context)
{
}
}
}
2017-07-20 11:21:28 +02:00
}