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 Microsoft.AspNetCore.Http;
|
2020-09-25 09:39:01 +02:00
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Moq;
|
2020-09-08 13:03:43 +02:00
|
|
|
using Umbraco.Core.Configuration.Models;
|
2020-09-25 09:39:01 +02:00
|
|
|
using Umbraco.Core.Hosting;
|
2021-01-08 17:21:35 +11:00
|
|
|
using Umbraco.Core.Routing;
|
2020-09-25 09:39:01 +02:00
|
|
|
using Umbraco.Core.Security;
|
2020-03-13 12:08:25 +11:00
|
|
|
using Umbraco.Tests.Common;
|
2019-10-22 00:53:52 +11:00
|
|
|
using Umbraco.Web;
|
2020-09-25 09:39:01 +02:00
|
|
|
using Umbraco.Web.Common.AspNetCore;
|
2019-10-22 00:53:52 +11:00
|
|
|
using Umbraco.Web.PublishedCache;
|
|
|
|
|
using Umbraco.Web.Routing;
|
|
|
|
|
|
2020-09-25 09:39:01 +02:00
|
|
|
namespace Umbraco.Tests.UnitTests.TestHelpers.Objects
|
2019-10-22 00:53:52 +11:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Simplify creating test UmbracoContext's
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class TestUmbracoContextFactory
|
|
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
public static IUmbracoContextFactory Create(
|
|
|
|
|
GlobalSettings globalSettings = null,
|
2020-02-13 13:57:39 +01:00
|
|
|
IUmbracoContextAccessor umbracoContextAccessor = null,
|
2020-02-14 13:04:49 +01:00
|
|
|
IHttpContextAccessor httpContextAccessor = null,
|
|
|
|
|
IPublishedUrlProvider publishedUrlProvider = null)
|
2019-10-22 00:53:52 +11:00
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
if (globalSettings == null)
|
|
|
|
|
{
|
|
|
|
|
globalSettings = new GlobalSettings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (umbracoContextAccessor == null)
|
|
|
|
|
{
|
|
|
|
|
umbracoContextAccessor = new TestUmbracoContextAccessor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (httpContextAccessor == null)
|
|
|
|
|
{
|
|
|
|
|
httpContextAccessor = Mock.Of<IHttpContextAccessor>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (publishedUrlProvider == null)
|
|
|
|
|
{
|
|
|
|
|
publishedUrlProvider = Mock.Of<IPublishedUrlProvider>();
|
|
|
|
|
}
|
2019-10-22 00:53:52 +11:00
|
|
|
|
|
|
|
|
var contentCache = new Mock<IPublishedContentCache>();
|
2019-12-11 08:13:51 +01:00
|
|
|
var mediaCache = new Mock<IPublishedMediaCache>();
|
2019-10-22 00:53:52 +11:00
|
|
|
var snapshot = new Mock<IPublishedSnapshot>();
|
|
|
|
|
snapshot.Setup(x => x.Content).Returns(contentCache.Object);
|
|
|
|
|
snapshot.Setup(x => x.Media).Returns(mediaCache.Object);
|
|
|
|
|
var snapshotService = new Mock<IPublishedSnapshotService>();
|
2019-12-11 08:13:51 +01:00
|
|
|
snapshotService.Setup(x => x.CreatePublishedSnapshot(It.IsAny<string>())).Returns(snapshot.Object);
|
|
|
|
|
|
2020-12-20 08:36:11 +01:00
|
|
|
IHostingEnvironment hostingEnvironment = Mock.Of<IHostingEnvironment>();
|
2020-10-21 16:51:00 +11:00
|
|
|
var backofficeSecurityAccessorMock = new Mock<IBackOfficeSecurityAccessor>();
|
|
|
|
|
backofficeSecurityAccessorMock.Setup(x => x.BackOfficeSecurity).Returns(Mock.Of<IBackOfficeSecurity>());
|
2020-12-20 08:36:11 +01:00
|
|
|
|
2019-10-22 00:53:52 +11:00
|
|
|
var umbracoContextFactory = new UmbracoContextFactory(
|
|
|
|
|
umbracoContextAccessor,
|
|
|
|
|
snapshotService.Object,
|
|
|
|
|
new TestVariationContextAccessor(),
|
|
|
|
|
new TestDefaultCultureAccessor(),
|
2021-01-08 17:21:35 +11:00
|
|
|
new UmbracoRequestPaths(Options.Create(globalSettings), hostingEnvironment),
|
2020-09-25 09:39:01 +02:00
|
|
|
hostingEnvironment,
|
|
|
|
|
new UriUtility(hostingEnvironment),
|
|
|
|
|
new AspNetCoreCookieManager(httpContextAccessor),
|
|
|
|
|
Mock.Of<IRequestAccessor>(),
|
2020-12-20 08:36:11 +01:00
|
|
|
backofficeSecurityAccessorMock.Object);
|
2019-10-22 00:53:52 +11:00
|
|
|
|
|
|
|
|
return umbracoContextFactory;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|