235 lines
8.9 KiB
C#
235 lines
8.9 KiB
C#
|
|
using System.Threading.Tasks;
|
|||
|
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
using Moq;
|
|||
|
|
using NUnit.Framework;
|
|||
|
|
using Umbraco.Core.Cache;
|
|||
|
|
using Umbraco.Core.Models.PublishedContent;
|
|||
|
|
using Umbraco.Core.Services;
|
|||
|
|
using Umbraco.Tests.Common;
|
|||
|
|
using Umbraco.Tests.Integration.Implementations;
|
|||
|
|
using Umbraco.Tests.Integration.Testing;
|
|||
|
|
using Umbraco.Tests.Testing;
|
|||
|
|
using Umbraco.Web;
|
|||
|
|
using Umbraco.Web.PublishedCache;
|
|||
|
|
using Umbraco.Web.Routing;
|
|||
|
|
using Umbraco.Web.Website.Controllers;
|
|||
|
|
|
|||
|
|
namespace Umbraco.Tests.Integration
|
|||
|
|
{
|
|||
|
|
[TestFixture]
|
|||
|
|
[UmbracoTest(WithApplication = true)]
|
|||
|
|
public class SurfaceControllerTests : UmbracoIntegrationTest
|
|||
|
|
{
|
|||
|
|
private IUmbracoContextAccessor _umbracoContextAccessor;
|
|||
|
|
|
|||
|
|
[SetUp]
|
|||
|
|
public void SetUp()
|
|||
|
|
{
|
|||
|
|
_umbracoContextAccessor = new TestUmbracoContextAccessor();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[Test]
|
|||
|
|
public void Can_Construct_And_Get_Result()
|
|||
|
|
{
|
|||
|
|
var testHelper = new TestHelper();
|
|||
|
|
var httpContextAccessor = testHelper.GetHttpContextAccessor();
|
|||
|
|
var hostingEnvironment = testHelper.GetHostingEnvironment();
|
|||
|
|
|
|||
|
|
var umbracoContextFactory = new UmbracoContextFactory(
|
|||
|
|
_umbracoContextAccessor,
|
|||
|
|
Mock.Of<IPublishedSnapshotService>(),
|
|||
|
|
new TestVariationContextAccessor(),
|
|||
|
|
new TestDefaultCultureAccessor(),
|
|||
|
|
GlobalSettings,
|
|||
|
|
Mock.Of<IUserService>(),
|
|||
|
|
hostingEnvironment,
|
|||
|
|
new UriUtility(hostingEnvironment),
|
|||
|
|
httpContextAccessor,
|
|||
|
|
Mock.Of<ICookieManager>(),
|
|||
|
|
Mock.Of<IRequestAccessor>());
|
|||
|
|
|
|||
|
|
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
|
|||
|
|
var umbracoContext = umbracoContextReference.UmbracoContext;
|
|||
|
|
|
|||
|
|
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext);
|
|||
|
|
|
|||
|
|
var ctrl = new TestSurfaceController(umbracoContextAccessor, Mock.Of<IPublishedContentQuery>(), Mock.Of<IPublishedUrlProvider>());
|
|||
|
|
|
|||
|
|
var result = ctrl.Index();
|
|||
|
|
|
|||
|
|
Assert.IsNotNull(result);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[Test]
|
|||
|
|
public void Umbraco_Context_Not_Null()
|
|||
|
|
{
|
|||
|
|
var testHelper = new TestHelper();
|
|||
|
|
var httpContextAccessor = testHelper.GetHttpContextAccessor();
|
|||
|
|
var hostingEnvironment = testHelper.GetHostingEnvironment();
|
|||
|
|
|
|||
|
|
var umbracoContextFactory = new UmbracoContextFactory(
|
|||
|
|
_umbracoContextAccessor,
|
|||
|
|
Mock.Of<IPublishedSnapshotService>(),
|
|||
|
|
new TestVariationContextAccessor(),
|
|||
|
|
new TestDefaultCultureAccessor(),
|
|||
|
|
GlobalSettings,
|
|||
|
|
Mock.Of<IUserService>(),
|
|||
|
|
hostingEnvironment,
|
|||
|
|
new UriUtility(hostingEnvironment),
|
|||
|
|
httpContextAccessor,
|
|||
|
|
Mock.Of<ICookieManager>(),
|
|||
|
|
Mock.Of<IRequestAccessor>());
|
|||
|
|
|
|||
|
|
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
|
|||
|
|
var umbCtx = umbracoContextReference.UmbracoContext;
|
|||
|
|
|
|||
|
|
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbCtx);
|
|||
|
|
|
|||
|
|
var ctrl = new TestSurfaceController(umbracoContextAccessor, Mock.Of<IPublishedContentQuery>(), Mock.Of<IPublishedUrlProvider>());
|
|||
|
|
|
|||
|
|
Assert.IsNotNull(ctrl.UmbracoContext);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[Test]
|
|||
|
|
public void Can_Lookup_Content()
|
|||
|
|
{
|
|||
|
|
var publishedSnapshot = new Mock<IPublishedSnapshot>();
|
|||
|
|
publishedSnapshot.Setup(x => x.Members).Returns(Mock.Of<IPublishedMemberCache>());
|
|||
|
|
var content = new Mock<IPublishedContent>();
|
|||
|
|
content.Setup(x => x.Id).Returns(2);
|
|||
|
|
|
|||
|
|
var publishedSnapshotService = new Mock<IPublishedSnapshotService>();
|
|||
|
|
var testHelper = new TestHelper();
|
|||
|
|
var httpContextAccessor = testHelper.GetHttpContextAccessor();
|
|||
|
|
var hostingEnvironment = testHelper.GetHostingEnvironment();
|
|||
|
|
|
|||
|
|
var umbracoContextFactory = new UmbracoContextFactory(
|
|||
|
|
_umbracoContextAccessor,
|
|||
|
|
publishedSnapshotService.Object,
|
|||
|
|
new TestVariationContextAccessor(),
|
|||
|
|
new TestDefaultCultureAccessor(),
|
|||
|
|
GlobalSettings,
|
|||
|
|
Mock.Of<IUserService>(),
|
|||
|
|
hostingEnvironment,
|
|||
|
|
new UriUtility(hostingEnvironment),
|
|||
|
|
httpContextAccessor,
|
|||
|
|
Mock.Of<ICookieManager>(),
|
|||
|
|
Mock.Of<IRequestAccessor>());
|
|||
|
|
|
|||
|
|
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
|
|||
|
|
var umbracoContext = umbracoContextReference.UmbracoContext;
|
|||
|
|
|
|||
|
|
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext);
|
|||
|
|
|
|||
|
|
var publishedContentQuery = Mock.Of<IPublishedContentQuery>(query => query.Content(2) == content.Object);
|
|||
|
|
|
|||
|
|
var ctrl = new TestSurfaceController(umbracoContextAccessor, publishedContentQuery, Mock.Of<IPublishedUrlProvider>());
|
|||
|
|
var result = ctrl.GetContent(2) as PublishedContentResult;
|
|||
|
|
|
|||
|
|
Assert.IsNotNull(result);
|
|||
|
|
Assert.IsNotNull(result.Content);
|
|||
|
|
Assert.AreEqual(2, result.Content.Id);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/*
|
|||
|
|
[Test]
|
|||
|
|
public void Mock_Current_Page()
|
|||
|
|
{
|
|||
|
|
var testHelper = new TestHelper();
|
|||
|
|
var httpContextAccessor = testHelper.GetHttpContextAccessor();
|
|||
|
|
var hostingEnvironment = testHelper.GetHostingEnvironment();
|
|||
|
|
|
|||
|
|
var umbracoContextFactory = new UmbracoContextFactory(
|
|||
|
|
_umbracoContextAccessor,
|
|||
|
|
Mock.Of<IPublishedSnapshotService>(),
|
|||
|
|
new TestVariationContextAccessor(),
|
|||
|
|
new TestDefaultCultureAccessor(),
|
|||
|
|
GlobalSettings,
|
|||
|
|
Mock.Of<IUserService>(),
|
|||
|
|
hostingEnvironment,
|
|||
|
|
new UriUtility(hostingEnvironment),
|
|||
|
|
httpContextAccessor,
|
|||
|
|
Mock.Of<ICookieManager>(),
|
|||
|
|
Mock.Of<IRequestAccessor>());
|
|||
|
|
|
|||
|
|
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
|
|||
|
|
var umbracoContext = umbracoContextReference.UmbracoContext;
|
|||
|
|
|
|||
|
|
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext);
|
|||
|
|
|
|||
|
|
var content = Mock.Of<IPublishedContent>(publishedContent => publishedContent.Id == 12345);
|
|||
|
|
|
|||
|
|
// TODO: Figure out how to create published router
|
|||
|
|
var publishedRouter = BaseWebTest.CreatePublishedRouter(TestHelpers.SettingsForTests.GenerateMockWebRoutingSettings());
|
|||
|
|
var frequest = publishedRouter.CreateRequest(umbracoContext, new Uri("http://localhost/test"));
|
|||
|
|
frequest.PublishedContent = content;
|
|||
|
|
|
|||
|
|
var routeDefinition = new RouteDefinition
|
|||
|
|
{
|
|||
|
|
PublishedRequest = frequest
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
var routeData = new RouteData();
|
|||
|
|
routeData.DataTokens.Add(Core.Constants.Web.UmbracoRouteDefinitionDataToken, routeDefinition);
|
|||
|
|
|
|||
|
|
var ctrl = new TestSurfaceController(umbracoContextAccessor, Mock.Of<IPublishedContentQuery>(), Mock.Of<IPublishedUrlProvider>());
|
|||
|
|
ctrl.ControllerContext = new ControllerContext()
|
|||
|
|
{
|
|||
|
|
HttpContext = Mock.Of<HttpContext>(),
|
|||
|
|
RouteData = routeData
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
var result = ctrl.GetContentFromCurrentPage() as PublishedContentResult;
|
|||
|
|
|
|||
|
|
Assert.AreEqual(12345, result.Content.Id);
|
|||
|
|
}
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
public class TestSurfaceController : SurfaceController
|
|||
|
|
{
|
|||
|
|
private readonly IPublishedContentQuery _publishedContentQuery;
|
|||
|
|
|
|||
|
|
public TestSurfaceController(IUmbracoContextAccessor umbracoContextAccessor, IPublishedContentQuery publishedContentQuery, IPublishedUrlProvider publishedUrlProvider)
|
|||
|
|
: base(umbracoContextAccessor, null, ServiceContext.CreatePartial(), AppCaches.Disabled, null, null, publishedUrlProvider)
|
|||
|
|
{
|
|||
|
|
_publishedContentQuery = publishedContentQuery;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public IActionResult Index()
|
|||
|
|
{
|
|||
|
|
// ReSharper disable once Mvc.ViewNotResolved
|
|||
|
|
return View();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public IActionResult GetContent(int id)
|
|||
|
|
{
|
|||
|
|
var content = _publishedContentQuery.Content(id);
|
|||
|
|
|
|||
|
|
return new PublishedContentResult(content);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public IActionResult GetContentFromCurrentPage()
|
|||
|
|
{
|
|||
|
|
var content = CurrentPage;
|
|||
|
|
|
|||
|
|
return new PublishedContentResult(content);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public class PublishedContentResult : IActionResult
|
|||
|
|
{
|
|||
|
|
public IPublishedContent Content { get; set; }
|
|||
|
|
|
|||
|
|
public PublishedContentResult(IPublishedContent content)
|
|||
|
|
{
|
|||
|
|
Content = content;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public Task ExecuteResultAsync(ActionContext context)
|
|||
|
|
{
|
|||
|
|
return Task.CompletedTask;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|