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

211 lines
8.0 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;
2018-06-19 19:59:12 +02:00
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Dictionary;
using Umbraco.Core.Logging;
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.Tests.Testing.Objects.Accessors;
using Umbraco.Web;
using Umbraco.Web.Mvc;
using Umbraco.Web.PublishedCache;
using Umbraco.Web.Routing;
using Umbraco.Web.Security;
2018-06-19 19:59:12 +02:00
using Current = Umbraco.Web.Composing.Current;
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()
{
var globalSettings = TestObjects.GetGlobalSettings();
2019-02-14 12:11:06 +01:00
var umbracoContextFactory = new UmbracoContextFactory(
2016-10-18 17:09:26 +02:00
Current.UmbracoContextAccessor,
2017-10-31 12:48:24 +01:00
Mock.Of<IPublishedSnapshotService>(),
2019-02-14 12:11:06 +01:00
new TestVariationContextAccessor(),
new TestDefaultCultureAccessor(),
2016-10-13 21:08:07 +02:00
TestObjects.GetUmbracoSettings(),
globalSettings,
2019-02-14 12:11:06 +01:00
Enumerable.Empty<IUrlProvider>(),
Mock.Of<IUserService>());
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of<HttpContextBase>());
var umbracoContext = umbracoContextReference.UmbracoContext;
2018-06-19 19:59:12 +02:00
var ctrl = new TestSurfaceController(umbracoContext);
var result = ctrl.Index();
Assert.IsNotNull(result);
}
[Test]
public void Umbraco_Context_Not_Null()
{
var globalSettings = TestObjects.GetGlobalSettings();
2019-02-14 12:11:06 +01:00
var umbracoContextFactory = new UmbracoContextFactory(
2016-10-18 17:09:26 +02:00
Current.UmbracoContextAccessor,
2017-10-31 12:48:24 +01:00
Mock.Of<IPublishedSnapshotService>(),
2019-02-14 12:11:06 +01:00
new TestVariationContextAccessor(),
new TestDefaultCultureAccessor(),
2016-10-13 21:08:07 +02:00
TestObjects.GetUmbracoSettings(),
globalSettings,
2019-02-14 12:11:06 +01:00
Enumerable.Empty<IUrlProvider>(),
Mock.Of<IUserService>());
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of<HttpContextBase>());
var umbCtx = umbracoContextReference.UmbracoContext;
2018-06-19 19:59:12 +02:00
var ctrl = new TestSurfaceController(umbCtx);
Assert.IsNotNull(ctrl.UmbracoContext);
}
2019-02-14 12:11:06 +01:00
[Test]
public void Can_Lookup_Content()
{
2018-04-27 11:38:50 +10:00
var publishedSnapshot = new Mock<IPublishedSnapshot>();
2017-10-31 12:50:30 +01:00
publishedSnapshot.Setup(x => x.Members).Returns(Mock.Of<IPublishedMemberCache>());
2018-12-07 12:44:54 +01:00
var content = new Mock<IPublishedContent>();
content.Setup(x => x.Id).Returns(2);
2017-10-31 12:48:24 +01:00
var publishedSnapshotService = new Mock<IPublishedSnapshotService>();
var globalSettings = TestObjects.GetGlobalSettings();
2016-05-26 19:20:33 +02:00
2019-02-14 12:11:06 +01:00
var umbracoContextFactory = new UmbracoContextFactory(
2016-10-18 17:09:26 +02:00
Current.UmbracoContextAccessor,
2017-10-31 12:48:24 +01:00
publishedSnapshotService.Object,
2019-02-14 12:11:06 +01:00
new TestVariationContextAccessor(),
new TestDefaultCultureAccessor(),
2019-01-31 00:57:10 +11:00
Mock.Of<IUmbracoSettingsSection>(section => section.WebRouting == Mock.Of<IWebRoutingSection>(routingSection => routingSection.UrlProviderMode == "Auto")),
globalSettings,
2019-02-14 12:11:06 +01:00
Enumerable.Empty<IUrlProvider>(),
Mock.Of<IUserService>());
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of<HttpContextBase>());
var umbracoContext = umbracoContextReference.UmbracoContext;
var helper = new UmbracoHelper(
2016-10-13 21:08:07 +02:00
umbracoContext,
Mock.Of<ITagQuery>(),
Mock.Of<ICultureDictionaryFactory>(),
Mock.Of<IUmbracoComponentRenderer>(),
2019-02-01 15:30:38 +11:00
Mock.Of<IPublishedContentQuery>(query => query.Content(2) == content.Object),
new MembershipHelper(new TestUmbracoContextAccessor(umbracoContext), Mock.Of<MembershipProvider>(), Mock.Of<RoleProvider>(), Mock.Of<IMemberService>(), Mock.Of<IMemberTypeService>(), Mock.Of<IUserService>(), Mock.Of<IPublicAccessService>(), Mock.Of<AppCaches>(), Mock.Of<ILogger>()));
2018-06-19 19:59:12 +02:00
var ctrl = new TestSurfaceController(umbracoContext, helper);
var result = ctrl.GetContent(2) as PublishedContentResult;
Assert.IsNotNull(result);
2019-02-01 15:30:38 +11:00
Assert.IsNotNull(result.Content);
Assert.AreEqual(2, result.Content.Id);
}
[Test]
public void Mock_Current_Page()
{
2019-01-31 00:57:10 +11:00
var webRoutingSettings = Mock.Of<IWebRoutingSection>(section => section.UrlProviderMode == "Auto");
var globalSettings = TestObjects.GetGlobalSettings();
2019-02-14 12:11:06 +01:00
var umbracoContextFactory = new UmbracoContextFactory(
2016-10-18 17:09:26 +02:00
Current.UmbracoContextAccessor,
2017-10-31 12:48:24 +01:00
Mock.Of<IPublishedSnapshotService>(),
2019-02-14 12:11:06 +01:00
new TestVariationContextAccessor(),
new TestDefaultCultureAccessor(),
Mock.Of<IUmbracoSettingsSection>(section => section.WebRouting == webRoutingSettings),
globalSettings,
2019-02-14 12:11:06 +01:00
Enumerable.Empty<IUrlProvider>(),
Mock.Of<IUserService>());
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(Mock.Of<HttpContextBase>());
var umbracoContext = umbracoContextReference.UmbracoContext;
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
{
2017-10-31 12:50:30 +01:00
PublishedRequest = frequest
};
var routeData = new RouteData();
routeData.DataTokens.Add(Core.Constants.Web.UmbracoRouteDefinitionDataToken, routeDefinition);
2018-06-19 19:59:12 +02:00
var ctrl = new TestSurfaceController(umbracoContext, 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
{
2018-06-19 19:59:12 +02:00
public TestSurfaceController(UmbracoContext ctx, UmbracoHelper helper = null)
: base(ctx, null, ServiceContext.CreatePartial(), Mock.Of<AppCaches>(), null, null, helper)
2018-06-19 19:59:12 +02:00
{
}
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
}