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-05-12 16:11:11 +02:00
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
using Umbraco.Cms.Core.Cache;
|
|
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Models.PublishedContent;
|
|
|
|
|
using Umbraco.Cms.Core.PublishedCache;
|
|
|
|
|
using Umbraco.Cms.Core.Routing;
|
|
|
|
|
using Umbraco.Cms.Core.Security;
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Core.Web;
|
2021-02-10 14:45:44 +01:00
|
|
|
using Umbraco.Cms.Tests.Common;
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Testing;
|
2021-02-10 14:58:22 +01:00
|
|
|
using Umbraco.Cms.Tests.UnitTests.TestHelpers.Objects;
|
2021-02-10 11:42:04 +01:00
|
|
|
using Umbraco.Cms.Web.Common.Routing;
|
2021-02-10 14:21:48 +01:00
|
|
|
using Umbraco.Cms.Web.Website.Controllers;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Website.Controllers;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
[UmbracoTest(WithApplication = true)]
|
|
|
|
|
public class SurfaceControllerTests
|
2020-05-12 16:11:11 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp() => _umbracoContextAccessor = new TestUmbracoContextAccessor();
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
private IUmbracoContextAccessor _umbracoContextAccessor;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Can_Construct_And_Get_Result()
|
|
|
|
|
{
|
|
|
|
|
var backofficeSecurityAccessor = Mock.Of<IBackOfficeSecurityAccessor>();
|
|
|
|
|
Mock.Get(backofficeSecurityAccessor).Setup(x => x.BackOfficeSecurity).Returns(Mock.Of<IBackOfficeSecurity>());
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2025-03-03 07:38:30 +01:00
|
|
|
var umbracoContextFactory = TestUmbracoContextFactory.Create(_umbracoContextAccessor);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
|
|
|
|
|
var umbracoContext = umbracoContextReference.UmbracoContext;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var ctrl = new TestSurfaceController(umbracoContextAccessor, Mock.Of<IPublishedContentQuery>(), Mock.Of<IPublishedUrlProvider>());
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var result = ctrl.Index();
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsNotNull(result);
|
|
|
|
|
}
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Umbraco_Context_Not_Null()
|
|
|
|
|
{
|
|
|
|
|
var backofficeSecurityAccessor = Mock.Of<IBackOfficeSecurityAccessor>();
|
|
|
|
|
Mock.Get(backofficeSecurityAccessor).Setup(x => x.BackOfficeSecurity).Returns(Mock.Of<IBackOfficeSecurity>());
|
2025-03-03 07:38:30 +01:00
|
|
|
var umbracoContextFactory = TestUmbracoContextFactory.Create(_umbracoContextAccessor);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
|
|
|
|
|
var umbCtx = umbracoContextReference.UmbracoContext;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbCtx);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var ctrl = new TestSurfaceController(umbracoContextAccessor, Mock.Of<IPublishedContentQuery>(), Mock.Of<IPublishedUrlProvider>());
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsNotNull(ctrl.UmbracoContext);
|
|
|
|
|
}
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Can_Lookup_Content()
|
|
|
|
|
{
|
|
|
|
|
var content = new Mock<IPublishedContent>();
|
|
|
|
|
content.Setup(x => x.Id).Returns(2);
|
|
|
|
|
var backofficeSecurityAccessor = Mock.Of<IBackOfficeSecurityAccessor>();
|
|
|
|
|
Mock.Get(backofficeSecurityAccessor).Setup(x => x.BackOfficeSecurity).Returns(Mock.Of<IBackOfficeSecurity>());
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2025-03-03 07:38:30 +01:00
|
|
|
var umbracoContextFactory = TestUmbracoContextFactory.Create(_umbracoContextAccessor);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
|
|
|
|
|
var umbracoContext = umbracoContextReference.UmbracoContext;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var publishedContentQuery = Mock.Of<IPublishedContentQuery>(query => query.Content(2) == content.Object);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var ctrl = new TestSurfaceController(umbracoContextAccessor, publishedContentQuery, Mock.Of<IPublishedUrlProvider>());
|
|
|
|
|
var result = ctrl.GetContent(2) as PublishedContentResult;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsNotNull(result);
|
|
|
|
|
Assert.IsNotNull(result.Content);
|
|
|
|
|
Assert.AreEqual(2, result.Content.Id);
|
|
|
|
|
}
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Mock_Current_Page()
|
|
|
|
|
{
|
|
|
|
|
var backofficeSecurityAccessor = Mock.Of<IBackOfficeSecurityAccessor>();
|
|
|
|
|
Mock.Get(backofficeSecurityAccessor).Setup(x => x.BackOfficeSecurity).Returns(Mock.Of<IBackOfficeSecurity>());
|
2025-03-03 07:38:30 +01:00
|
|
|
var umbracoContextFactory = TestUmbracoContextFactory.Create(_umbracoContextAccessor);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext();
|
|
|
|
|
var umbracoContext = umbracoContextReference.UmbracoContext;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var content = Mock.Of<IPublishedContent>(publishedContent => publishedContent.Id == 12345);
|
|
|
|
|
var builder = new PublishedRequestBuilder(umbracoContext.CleanedUmbracoUrl, Mock.Of<IFileService>());
|
|
|
|
|
builder.SetPublishedContent(content);
|
|
|
|
|
var publishedRequest = builder.Build();
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var routeDefinition = new UmbracoRouteValues(publishedRequest, null);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var httpContext = new DefaultHttpContext();
|
|
|
|
|
httpContext.Features.Set(routeDefinition);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var ctrl =
|
|
|
|
|
new TestSurfaceController(umbracoContextAccessor, Mock.Of<IPublishedContentQuery>(), Mock.Of<IPublishedUrlProvider>())
|
2020-05-12 16:11:11 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
ControllerContext = new ControllerContext { HttpContext = httpContext, RouteData = new RouteData() },
|
2020-05-12 16:11:11 +02:00
|
|
|
};
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var result = ctrl.GetContentFromCurrentPage() as PublishedContentResult;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual(12345, result.Content.Id);
|
|
|
|
|
}
|
2020-05-15 10:05:29 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public class TestSurfaceController : SurfaceController
|
|
|
|
|
{
|
|
|
|
|
private readonly IPublishedContentQuery _publishedContentQuery;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public TestSurfaceController(
|
|
|
|
|
IUmbracoContextAccessor umbracoContextAccessor,
|
|
|
|
|
IPublishedContentQuery publishedContentQuery,
|
|
|
|
|
IPublishedUrlProvider publishedUrlProvider)
|
|
|
|
|
: base(umbracoContextAccessor, null, ServiceContext.CreatePartial(), AppCaches.Disabled, null, publishedUrlProvider) =>
|
|
|
|
|
_publishedContentQuery = publishedContentQuery;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IActionResult Index() =>
|
2020-12-20 08:36:11 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// ReSharper disable once Mvc.ViewNotResolved
|
|
|
|
|
View();
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IActionResult GetContent(int id)
|
|
|
|
|
{
|
|
|
|
|
var content = _publishedContentQuery.Content(id);
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
return new PublishedContentResult(content);
|
|
|
|
|
}
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IActionResult GetContentFromCurrentPage()
|
|
|
|
|
{
|
|
|
|
|
var content = CurrentPage;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
return new PublishedContentResult(content);
|
2020-05-12 16:11:11 +02:00
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public class PublishedContentResult : IActionResult
|
|
|
|
|
{
|
|
|
|
|
public PublishedContentResult(IPublishedContent content) => Content = content;
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IPublishedContent Content { get; set; }
|
2020-05-12 16:11:11 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public Task ExecuteResultAsync(ActionContext context) => Task.CompletedTask;
|
2020-05-12 16:11:11 +02:00
|
|
|
}
|
|
|
|
|
}
|