2017-07-20 11:21:28 +02:00
|
|
|
|
using System;
|
2015-02-18 17:14:55 +01:00
|
|
|
|
using System.Linq;
|
2014-06-17 18:53:23 +10:00
|
|
|
|
using System.Web;
|
|
|
|
|
|
using System.Web.Mvc;
|
2015-02-18 18:05:37 +01:00
|
|
|
|
using System.Web.Routing;
|
2015-02-18 17:14:55 +01:00
|
|
|
|
using System.Web.Security;
|
2014-06-17 18:53:23 +10:00
|
|
|
|
using Moq;
|
|
|
|
|
|
using NUnit.Framework;
|
2016-05-18 23:34:56 +02:00
|
|
|
|
using Umbraco.Core.Cache;
|
2018-06-19 19:59:12 +02:00
|
|
|
|
using Umbraco.Core.Composing;
|
2015-02-18 17:14:55 +01:00
|
|
|
|
using Umbraco.Core.Configuration.UmbracoSettings;
|
|
|
|
|
|
using Umbraco.Core.Dictionary;
|
2018-06-28 23:28:22 +02:00
|
|
|
|
using Umbraco.Core.Logging;
|
2016-06-10 16:37:28 +02:00
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
2015-02-18 17:14:55 +01:00
|
|
|
|
using Umbraco.Core.Services;
|
2014-06-17 18:53:23 +10:00
|
|
|
|
using Umbraco.Tests.TestHelpers;
|
2016-11-07 19:12:09 +01:00
|
|
|
|
using Umbraco.Tests.TestHelpers.Stubs;
|
2016-12-16 11:23:01 +01:00
|
|
|
|
using Umbraco.Tests.Testing;
|
2018-04-18 19:46:47 +02:00
|
|
|
|
using Umbraco.Tests.Testing.Objects.Accessors;
|
2014-06-17 18:53:23 +10:00
|
|
|
|
using Umbraco.Web;
|
|
|
|
|
|
using Umbraco.Web.Mvc;
|
2016-05-26 18:48:35 +02:00
|
|
|
|
using Umbraco.Web.PublishedCache;
|
2015-02-18 17:14:55 +01:00
|
|
|
|
using Umbraco.Web.Routing;
|
|
|
|
|
|
using Umbraco.Web.Security;
|
2018-06-19 19:59:12 +02:00
|
|
|
|
using Current = Umbraco.Web.Composing.Current;
|
2014-06-17 18:53:23 +10:00
|
|
|
|
|
2016-02-17 10:59:48 +01:00
|
|
|
|
namespace Umbraco.Tests.Web.Mvc
|
2014-06-17 18:53:23 +10:00
|
|
|
|
{
|
|
|
|
|
|
[TestFixture]
|
2016-12-16 11:23:01 +01:00
|
|
|
|
[UmbracoTest(WithApplication = true)]
|
|
|
|
|
|
public class SurfaceControllerTests : UmbracoTestBase
|
2014-06-17 18:53:23 +10:00
|
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2014-06-17 18:53:23 +10:00
|
|
|
|
[Test]
|
|
|
|
|
|
public void Can_Construct_And_Get_Result()
|
|
|
|
|
|
{
|
2018-04-06 13:51:54 +10:00
|
|
|
|
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(),
|
2018-04-06 13:51:54 +10:00
|
|
|
|
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;
|
2014-06-17 18:53:23 +10:00
|
|
|
|
|
2018-06-19 19:59:12 +02:00
|
|
|
|
var ctrl = new TestSurfaceController(umbracoContext);
|
2014-06-17 18:53:23 +10:00
|
|
|
|
|
|
|
|
|
|
var result = ctrl.Index();
|
|
|
|
|
|
|
|
|
|
|
|
Assert.IsNotNull(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Umbraco_Context_Not_Null()
|
|
|
|
|
|
{
|
2018-04-06 13:51:54 +10:00
|
|
|
|
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(),
|
2018-04-06 13:51:54 +10:00
|
|
|
|
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;
|
2014-06-17 18:53:23 +10:00
|
|
|
|
|
2018-06-19 19:59:12 +02:00
|
|
|
|
var ctrl = new TestSurfaceController(umbCtx);
|
2014-06-17 18:53:23 +10:00
|
|
|
|
|
|
|
|
|
|
Assert.IsNotNull(ctrl.UmbracoContext);
|
|
|
|
|
|
}
|
2019-02-14 12:11:06 +01:00
|
|
|
|
|
2014-06-17 18:53:23 +10: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>();
|
2018-04-06 13:51:54 +10:00
|
|
|
|
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")),
|
2018-04-06 13:51:54 +10:00
|
|
|
|
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;
|
2014-06-17 18:53:23 +10:00
|
|
|
|
|
2015-02-18 17:14:55 +01:00
|
|
|
|
var helper = new UmbracoHelper(
|
2016-10-13 21:08:07 +02:00
|
|
|
|
umbracoContext,
|
2015-02-18 17:14:55 +01:00
|
|
|
|
Mock.Of<ITagQuery>(),
|
2019-01-31 15:09:31 +11:00
|
|
|
|
Mock.Of<ICultureDictionaryFactory>(),
|
2015-02-18 17:14:55 +01:00
|
|
|
|
Mock.Of<IUmbracoComponentRenderer>(),
|
2019-02-01 15:30:38 +11:00
|
|
|
|
Mock.Of<IPublishedContentQuery>(query => query.Content(2) == content.Object),
|
2019-02-01 11:33:13 +11:00
|
|
|
|
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>()));
|
2015-02-18 18:05:37 +01:00
|
|
|
|
|
2018-06-19 19:59:12 +02:00
|
|
|
|
var ctrl = new TestSurfaceController(umbracoContext, helper);
|
2015-02-18 17:14:55 +01:00
|
|
|
|
var result = ctrl.GetContent(2) as PublishedContentResult;
|
2014-06-17 18:53:23 +10:00
|
|
|
|
|
2015-02-18 17:14:55 +01:00
|
|
|
|
Assert.IsNotNull(result);
|
2019-02-01 15:30:38 +11:00
|
|
|
|
Assert.IsNotNull(result.Content);
|
2015-02-18 18:05:37 +01:00
|
|
|
|
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");
|
2018-04-06 13:51:54 +10:00
|
|
|
|
var globalSettings = TestObjects.GetGlobalSettings();
|
2015-02-18 18:05:37 +01: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
|
|
|
|
Mock.Of<IPublishedSnapshotService>(),
|
2019-02-14 12:11:06 +01:00
|
|
|
|
new TestVariationContextAccessor(),
|
|
|
|
|
|
new TestDefaultCultureAccessor(),
|
2015-02-18 18:05:37 +01:00
|
|
|
|
Mock.Of<IUmbracoSettingsSection>(section => section.WebRouting == webRoutingSettings),
|
2018-04-06 13:51:54 +10:00
|
|
|
|
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;
|
2015-02-18 18:05:37 +01:00
|
|
|
|
|
|
|
|
|
|
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"));
|
2016-10-11 18:52:01 +02:00
|
|
|
|
frequest.PublishedContent = content;
|
2015-02-18 18:05:37 +01:00
|
|
|
|
|
|
|
|
|
|
var routeDefinition = new RouteDefinition
|
|
|
|
|
|
{
|
2017-10-31 12:50:30 +01:00
|
|
|
|
PublishedRequest = frequest
|
2015-02-18 18:05:37 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var routeData = new RouteData();
|
2016-09-01 19:06:08 +02:00
|
|
|
|
routeData.DataTokens.Add(Core.Constants.Web.UmbracoRouteDefinitionDataToken, routeDefinition);
|
2015-02-18 18:05:37 +01:00
|
|
|
|
|
2018-06-19 19:59:12 +02:00
|
|
|
|
var ctrl = new TestSurfaceController(umbracoContext, new UmbracoHelper());
|
2015-02-18 18:05:37 +01:00
|
|
|
|
ctrl.ControllerContext = new ControllerContext(contextBase, routeData, ctrl);
|
|
|
|
|
|
|
|
|
|
|
|
var result = ctrl.GetContentFromCurrentPage() as PublishedContentResult;
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(12345, result.Content.Id);
|
2014-06-17 18:53:23 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class TestSurfaceController : SurfaceController
|
|
|
|
|
|
{
|
2018-06-19 19:59:12 +02:00
|
|
|
|
public TestSurfaceController(UmbracoContext ctx, UmbracoHelper helper = null)
|
2019-01-31 15:09:31 +11:00
|
|
|
|
: base(ctx, null, ServiceContext.CreatePartial(), Mock.Of<AppCaches>(), null, null, helper)
|
2018-06-19 19:59:12 +02:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-06-17 18:53:23 +10:00
|
|
|
|
public ActionResult Index()
|
|
|
|
|
|
{
|
|
|
|
|
|
return View();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ActionResult GetContent(int id)
|
|
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
var content = Umbraco.Content(id);
|
2014-06-17 18:53:23 +10:00
|
|
|
|
|
|
|
|
|
|
return new PublishedContentResult(content);
|
|
|
|
|
|
}
|
2015-02-18 18:05:37 +01:00
|
|
|
|
|
|
|
|
|
|
public ActionResult GetContentFromCurrentPage()
|
|
|
|
|
|
{
|
|
|
|
|
|
var content = CurrentPage;
|
|
|
|
|
|
|
|
|
|
|
|
return new PublishedContentResult(content);
|
|
|
|
|
|
}
|
2014-06-17 18:53:23 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|