2020-12-20 08:36:11 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2020-12-08 10:20:03 +11:00
|
|
|
using System.Threading.Tasks;
|
2020-05-15 10:05:29 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-05-12 16:11:11 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-05-15 10:05:29 +02:00
|
|
|
using Microsoft.AspNetCore.Routing;
|
2020-08-24 10:52:48 +02:00
|
|
|
using Microsoft.Extensions.Options;
|
2020-05-12 16:11:11 +02:00
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Umbraco.Core.Cache;
|
2020-09-21 21:06:24 +02:00
|
|
|
using Umbraco.Core.Configuration.Models;
|
2020-05-15 10:05:29 +02:00
|
|
|
using Umbraco.Core.Hosting;
|
2020-05-12 16:11:11 +02:00
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
2021-01-08 17:21:35 +11:00
|
|
|
using Umbraco.Core.Routing;
|
2020-09-22 10:01:00 +02:00
|
|
|
using Umbraco.Core.Security;
|
2020-05-12 16:11:11 +02:00
|
|
|
using Umbraco.Core.Services;
|
|
|
|
|
using Umbraco.Tests.Common;
|
|
|
|
|
using Umbraco.Tests.Testing;
|
|
|
|
|
using Umbraco.Web;
|
2020-12-10 18:09:32 +11:00
|
|
|
using Umbraco.Web.Common.Routing;
|
2020-05-12 16:11:11 +02:00
|
|
|
using Umbraco.Web.PublishedCache;
|
|
|
|
|
using Umbraco.Web.Routing;
|
2020-05-15 10:05:29 +02:00
|
|
|
using Umbraco.Web.Website;
|
2020-05-12 16:11:11 +02:00
|
|
|
using Umbraco.Web.Website.Controllers;
|
2020-12-08 16:33:50 +11:00
|
|
|
using Umbraco.Web.Website.Routing;
|
2020-10-26 13:34:08 +01:00
|
|
|
using CoreConstants = Umbraco.Core.Constants;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2020-10-26 13:34:08 +01:00
|
|
|
namespace Umbraco.Tests.UnitTests.Umbraco.Web.Website.Controllers
|
2020-05-12 16:11:11 +02:00
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
[UmbracoTest(WithApplication = true)]
|
2020-05-15 10:05:29 +02:00
|
|
|
public class SurfaceControllerTests
|
2020-05-12 16:11:11 +02:00
|
|
|
{
|
|
|
|
|
private IUmbracoContextAccessor _umbracoContextAccessor;
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
2020-12-20 08:36:11 +01:00
|
|
|
public void SetUp() => _umbracoContextAccessor = new TestUmbracoContextAccessor();
|
2020-05-12 16:11:11 +02:00
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Can_Construct_And_Get_Result()
|
|
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
IHostingEnvironment hostingEnvironment = Mock.Of<IHostingEnvironment>();
|
|
|
|
|
IBackOfficeSecurityAccessor backofficeSecurityAccessor = Mock.Of<IBackOfficeSecurityAccessor>();
|
2020-10-21 16:51:00 +11:00
|
|
|
Mock.Get(backofficeSecurityAccessor).Setup(x => x.BackOfficeSecurity).Returns(Mock.Of<IBackOfficeSecurity>());
|
2020-09-21 21:06:24 +02:00
|
|
|
var globalSettings = new GlobalSettings();
|
2020-05-12 16:11:11 +02:00
|
|
|
|
|
|
|
|
var umbracoContextFactory = new UmbracoContextFactory(
|
|
|
|
|
_umbracoContextAccessor,
|
|
|
|
|
Mock.Of<IPublishedSnapshotService>(),
|
|
|
|
|
new TestVariationContextAccessor(),
|
|
|
|
|
new TestDefaultCultureAccessor(),
|
2021-01-08 17:21:35 +11:00
|
|
|
new UmbracoRequestPaths(Options.Create(globalSettings), hostingEnvironment),
|
2020-05-12 16:11:11 +02:00
|
|
|
hostingEnvironment,
|
|
|
|
|
new UriUtility(hostingEnvironment),
|
|
|
|
|
Mock.Of<ICookieManager>(),
|
2020-06-09 07:49:26 +02:00
|
|
|
Mock.Of<IRequestAccessor>(),
|
2020-09-22 10:01:00 +02:00
|
|
|
backofficeSecurityAccessor);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2020-12-20 08:36:11 +01:00
|
|
|
UmbracoContextReference umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
|
|
|
|
|
IUmbracoContext umbracoContext = umbracoContextReference.UmbracoContext;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
|
|
|
|
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext);
|
|
|
|
|
|
|
|
|
|
var ctrl = new TestSurfaceController(umbracoContextAccessor, Mock.Of<IPublishedContentQuery>(), Mock.Of<IPublishedUrlProvider>());
|
|
|
|
|
|
2020-12-20 08:36:11 +01:00
|
|
|
IActionResult result = ctrl.Index();
|
2020-05-12 16:11:11 +02:00
|
|
|
|
|
|
|
|
Assert.IsNotNull(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Umbraco_Context_Not_Null()
|
|
|
|
|
{
|
2020-09-21 21:06:24 +02:00
|
|
|
var globalSettings = new GlobalSettings();
|
2020-12-20 08:36:11 +01:00
|
|
|
IHostingEnvironment hostingEnvironment = Mock.Of<IHostingEnvironment>();
|
|
|
|
|
IBackOfficeSecurityAccessor backofficeSecurityAccessor = Mock.Of<IBackOfficeSecurityAccessor>();
|
2020-10-21 16:51:00 +11:00
|
|
|
Mock.Get(backofficeSecurityAccessor).Setup(x => x.BackOfficeSecurity).Returns(Mock.Of<IBackOfficeSecurity>());
|
2020-05-12 16:11:11 +02:00
|
|
|
var umbracoContextFactory = new UmbracoContextFactory(
|
|
|
|
|
_umbracoContextAccessor,
|
|
|
|
|
Mock.Of<IPublishedSnapshotService>(),
|
|
|
|
|
new TestVariationContextAccessor(),
|
|
|
|
|
new TestDefaultCultureAccessor(),
|
2021-01-08 17:21:35 +11:00
|
|
|
new UmbracoRequestPaths(Options.Create(globalSettings), hostingEnvironment),
|
2020-05-12 16:11:11 +02:00
|
|
|
hostingEnvironment,
|
|
|
|
|
new UriUtility(hostingEnvironment),
|
|
|
|
|
Mock.Of<ICookieManager>(),
|
2020-06-09 07:49:26 +02:00
|
|
|
Mock.Of<IRequestAccessor>(),
|
2020-09-22 10:01:00 +02:00
|
|
|
backofficeSecurityAccessor);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2020-12-20 08:36:11 +01:00
|
|
|
UmbracoContextReference umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
|
|
|
|
|
IUmbracoContext umbCtx = umbracoContextReference.UmbracoContext;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
|
|
|
|
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);
|
2020-12-20 08:36:11 +01:00
|
|
|
IBackOfficeSecurityAccessor backofficeSecurityAccessor = Mock.Of<IBackOfficeSecurityAccessor>();
|
2020-10-21 16:51:00 +11:00
|
|
|
Mock.Get(backofficeSecurityAccessor).Setup(x => x.BackOfficeSecurity).Returns(Mock.Of<IBackOfficeSecurity>());
|
2020-05-12 16:11:11 +02:00
|
|
|
var publishedSnapshotService = new Mock<IPublishedSnapshotService>();
|
2020-12-20 08:36:11 +01:00
|
|
|
IHostingEnvironment hostingEnvironment = Mock.Of<IHostingEnvironment>();
|
2020-09-21 21:06:24 +02:00
|
|
|
var globalSettings = new GlobalSettings();
|
2020-05-12 16:11:11 +02:00
|
|
|
|
|
|
|
|
var umbracoContextFactory = new UmbracoContextFactory(
|
|
|
|
|
_umbracoContextAccessor,
|
|
|
|
|
publishedSnapshotService.Object,
|
|
|
|
|
new TestVariationContextAccessor(),
|
|
|
|
|
new TestDefaultCultureAccessor(),
|
2021-01-08 17:21:35 +11:00
|
|
|
new UmbracoRequestPaths(Options.Create(globalSettings), hostingEnvironment),
|
2020-05-12 16:11:11 +02:00
|
|
|
hostingEnvironment,
|
|
|
|
|
new UriUtility(hostingEnvironment),
|
|
|
|
|
Mock.Of<ICookieManager>(),
|
2020-06-09 07:49:26 +02:00
|
|
|
Mock.Of<IRequestAccessor>(),
|
2020-09-22 10:01:00 +02:00
|
|
|
backofficeSecurityAccessor);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2020-12-20 08:36:11 +01:00
|
|
|
UmbracoContextReference umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
|
|
|
|
|
IUmbracoContext umbracoContext = umbracoContextReference.UmbracoContext;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
|
|
|
|
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext);
|
|
|
|
|
|
2020-12-20 08:36:11 +01:00
|
|
|
IPublishedContentQuery publishedContentQuery = Mock.Of<IPublishedContentQuery>(query => query.Content(2) == content.Object);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2020-09-21 21:06:24 +02:00
|
|
|
var globalSettings = new GlobalSettings();
|
2020-12-20 08:36:11 +01:00
|
|
|
IHostingEnvironment hostingEnvironment = Mock.Of<IHostingEnvironment>();
|
|
|
|
|
IBackOfficeSecurityAccessor backofficeSecurityAccessor = Mock.Of<IBackOfficeSecurityAccessor>();
|
2020-10-21 16:51:00 +11:00
|
|
|
Mock.Get(backofficeSecurityAccessor).Setup(x => x.BackOfficeSecurity).Returns(Mock.Of<IBackOfficeSecurity>());
|
2020-05-12 16:11:11 +02:00
|
|
|
var umbracoContextFactory = new UmbracoContextFactory(
|
|
|
|
|
_umbracoContextAccessor,
|
|
|
|
|
Mock.Of<IPublishedSnapshotService>(),
|
|
|
|
|
new TestVariationContextAccessor(),
|
|
|
|
|
new TestDefaultCultureAccessor(),
|
2021-01-08 17:21:35 +11:00
|
|
|
new UmbracoRequestPaths(Options.Create(globalSettings), hostingEnvironment),
|
2020-05-12 16:11:11 +02:00
|
|
|
hostingEnvironment,
|
|
|
|
|
new UriUtility(hostingEnvironment),
|
|
|
|
|
Mock.Of<ICookieManager>(),
|
2020-06-09 07:49:26 +02:00
|
|
|
Mock.Of<IRequestAccessor>(),
|
2020-09-22 10:01:00 +02:00
|
|
|
backofficeSecurityAccessor);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2020-12-20 08:36:11 +01:00
|
|
|
UmbracoContextReference umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
|
|
|
|
|
IUmbracoContext umbracoContext = umbracoContextReference.UmbracoContext;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
|
|
|
|
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext);
|
|
|
|
|
|
2020-12-20 08:36:11 +01:00
|
|
|
IPublishedContent content = Mock.Of<IPublishedContent>(publishedContent => publishedContent.Id == 12345);
|
2021-01-06 20:03:49 +11:00
|
|
|
var builder = new PublishedRequestBuilder(umbracoContext.CleanedUmbracoUrl, Mock.Of<IFileService>());
|
|
|
|
|
builder.SetPublishedContent(content);
|
|
|
|
|
IPublishedRequest publishedRequest = builder.Build();
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2021-01-06 20:03:49 +11:00
|
|
|
var routeDefinition = new UmbracoRouteValues(publishedRequest);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
|
|
|
|
var routeData = new RouteData();
|
2020-12-10 18:09:32 +11:00
|
|
|
routeData.Values.Add(CoreConstants.Web.UmbracoRouteDefinitionDataToken, routeDefinition);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2020-12-20 08:36:11 +01:00
|
|
|
var ctrl = new TestSurfaceController(umbracoContextAccessor, Mock.Of<IPublishedContentQuery>(), Mock.Of<IPublishedUrlProvider>())
|
2020-05-12 16:11:11 +02:00
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
ControllerContext = new ControllerContext()
|
|
|
|
|
{
|
|
|
|
|
HttpContext = Mock.Of<HttpContext>(),
|
|
|
|
|
RouteData = routeData
|
|
|
|
|
}
|
2020-05-12 16:11:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = ctrl.GetContentFromCurrentPage() as PublishedContentResult;
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(12345, result.Content.Id);
|
|
|
|
|
}
|
2020-05-15 10:05:29 +02:00
|
|
|
|
2020-05-12 16:11:11 +02:00
|
|
|
public class TestSurfaceController : SurfaceController
|
|
|
|
|
{
|
|
|
|
|
private readonly IPublishedContentQuery _publishedContentQuery;
|
|
|
|
|
|
|
|
|
|
public TestSurfaceController(IUmbracoContextAccessor umbracoContextAccessor, IPublishedContentQuery publishedContentQuery, IPublishedUrlProvider publishedUrlProvider)
|
2020-12-20 08:36:11 +01:00
|
|
|
: base(umbracoContextAccessor, null, ServiceContext.CreatePartial(), AppCaches.Disabled, null, publishedUrlProvider) =>
|
2020-05-12 16:11:11 +02:00
|
|
|
_publishedContentQuery = publishedContentQuery;
|
|
|
|
|
|
2020-12-20 08:36:11 +01:00
|
|
|
public IActionResult Index() =>
|
|
|
|
|
|
2020-05-12 16:11:11 +02:00
|
|
|
// ReSharper disable once Mvc.ViewNotResolved
|
2020-12-20 08:36:11 +01:00
|
|
|
View();
|
2020-05-12 16:11:11 +02:00
|
|
|
|
|
|
|
|
public IActionResult GetContent(int id)
|
|
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
IPublishedContent content = _publishedContentQuery.Content(id);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
|
|
|
|
return new PublishedContentResult(content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IActionResult GetContentFromCurrentPage()
|
|
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
IPublishedContent content = CurrentPage;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
|
|
|
|
return new PublishedContentResult(content);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class PublishedContentResult : IActionResult
|
|
|
|
|
{
|
|
|
|
|
public IPublishedContent Content { get; set; }
|
|
|
|
|
|
2020-12-20 08:36:11 +01:00
|
|
|
public PublishedContentResult(IPublishedContent content) => Content = content;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2020-12-20 08:36:11 +01:00
|
|
|
public Task ExecuteResultAsync(ActionContext context) => Task.CompletedTask;
|
2020-05-12 16:11:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|