2020-03-13 12:08:25 +11:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
2020-03-13 14:43:41 +11:00
|
|
|
|
using System.Reflection;
|
2020-03-13 12:08:25 +11:00
|
|
|
|
using Moq;
|
|
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Cache;
|
|
|
|
|
|
using Umbraco.Core.Composing;
|
|
|
|
|
|
using Umbraco.Core.Configuration;
|
|
|
|
|
|
using Umbraco.Core.Configuration.UmbracoSettings;
|
|
|
|
|
|
using Umbraco.Core.Diagnostics;
|
|
|
|
|
|
using Umbraco.Core.Hosting;
|
|
|
|
|
|
using Umbraco.Core.IO;
|
|
|
|
|
|
using Umbraco.Core.Logging;
|
|
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
2020-03-25 16:52:42 +11:00
|
|
|
|
using Umbraco.Net;
|
2020-03-13 12:08:25 +11:00
|
|
|
|
using Umbraco.Core.Persistence;
|
|
|
|
|
|
using Umbraco.Core.Serialization;
|
|
|
|
|
|
using Umbraco.Core.Strings;
|
|
|
|
|
|
using Umbraco.Core.Sync;
|
|
|
|
|
|
using Umbraco.Web;
|
|
|
|
|
|
using Umbraco.Web.Routing;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.Common
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Common helper properties and methods useful to testing
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public abstract class TestHelperBase
|
|
|
|
|
|
{
|
2020-03-13 14:43:41 +11:00
|
|
|
|
private readonly ITypeFinder _typeFinder;
|
|
|
|
|
|
private UriUtility _uriUtility;
|
|
|
|
|
|
private IIOHelper _ioHelper;
|
|
|
|
|
|
|
2020-03-25 15:06:22 +11:00
|
|
|
|
protected TestHelperBase(Assembly entryAssembly)
|
2020-03-13 12:08:25 +11:00
|
|
|
|
{
|
2020-03-23 16:39:27 +11:00
|
|
|
|
SettingsForTests = new SettingsForTests();
|
2020-03-13 14:43:41 +11:00
|
|
|
|
MainDom = new SimpleMainDom();
|
|
|
|
|
|
_typeFinder = new TypeFinder(Mock.Of<ILogger>(), new DefaultUmbracoAssemblyProvider(entryAssembly));
|
2020-03-13 12:08:25 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-13 14:43:41 +11:00
|
|
|
|
public ITypeFinder GetTypeFinder() => _typeFinder;
|
2020-03-13 12:08:25 +11:00
|
|
|
|
|
|
|
|
|
|
public TypeLoader GetMockedTypeLoader()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new TypeLoader(IOHelper, Mock.Of<ITypeFinder>(), Mock.Of<IAppPolicyCache>(), new DirectoryInfo(IOHelper.MapPath("~/App_Data/TEMP")), Mock.Of<IProfilingLogger>());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-23 17:15:32 +11:00
|
|
|
|
public Configs GetConfigs() => GetConfigsFactory().Create();
|
2020-03-23 16:39:27 +11:00
|
|
|
|
|
2020-03-13 12:08:25 +11:00
|
|
|
|
public IRuntimeState GetRuntimeState()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new RuntimeState(
|
|
|
|
|
|
Mock.Of<ILogger>(),
|
|
|
|
|
|
Mock.Of<IGlobalSettings>(),
|
2020-03-23 17:15:32 +11:00
|
|
|
|
GetUmbracoVersion(),
|
2020-03-25 15:06:22 +11:00
|
|
|
|
GetBackOfficeInfo());
|
2020-03-13 12:08:25 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public abstract IBackOfficeInfo GetBackOfficeInfo();
|
|
|
|
|
|
|
2020-03-23 17:15:32 +11:00
|
|
|
|
public IConfigsFactory GetConfigsFactory() => new ConfigsFactory();
|
2020-03-13 12:08:25 +11:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-03-31 17:27:51 +11:00
|
|
|
|
/// Gets the working directory of the test project.
|
2020-03-13 12:08:25 +11:00
|
|
|
|
/// </summary>
|
2020-03-31 17:27:51 +11:00
|
|
|
|
public string WorkingDirectory
|
2020-03-13 12:08:25 +11:00
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2020-03-31 17:27:51 +11:00
|
|
|
|
var dir = Path.Combine(IOHelper.MapPath("~"), "TEMP");
|
|
|
|
|
|
if (!Directory.Exists(dir))
|
|
|
|
|
|
Directory.CreateDirectory(dir);
|
|
|
|
|
|
return dir;
|
2020-03-13 12:08:25 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IShortStringHelper ShortStringHelper { get; } = new DefaultShortStringHelper(new DefaultShortStringHelperConfig());
|
|
|
|
|
|
public IJsonSerializer JsonSerializer { get; } = new JsonNetSerializer();
|
|
|
|
|
|
public IVariationContextAccessor VariationContextAccessor { get; } = new TestVariationContextAccessor();
|
|
|
|
|
|
public abstract IDbProviderFactoryCreator DbProviderFactoryCreator { get; }
|
|
|
|
|
|
public abstract IBulkSqlInsertProvider BulkSqlInsertProvider { get; }
|
|
|
|
|
|
public abstract IMarchal Marchal { get; }
|
2020-03-16 14:02:08 +01:00
|
|
|
|
public ICoreDebugSettings CoreDebugSettings { get; } = new CoreDebugSettings();
|
2020-03-13 12:08:25 +11:00
|
|
|
|
|
2020-03-13 14:43:41 +11:00
|
|
|
|
public IIOHelper IOHelper
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_ioHelper == null)
|
2020-03-23 15:50:01 +11:00
|
|
|
|
_ioHelper = new IOHelper(GetHostingEnvironment(), SettingsForTests.GenerateMockGlobalSettings());
|
2020-03-13 14:43:41 +11:00
|
|
|
|
return _ioHelper;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-03-13 12:08:25 +11:00
|
|
|
|
|
|
|
|
|
|
public IMainDom MainDom { get; }
|
2020-03-13 14:43:41 +11:00
|
|
|
|
public UriUtility UriUtility
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_uriUtility == null)
|
|
|
|
|
|
_uriUtility = new UriUtility(GetHostingEnvironment());
|
|
|
|
|
|
return _uriUtility;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-13 12:08:25 +11:00
|
|
|
|
public SettingsForTests SettingsForTests { get; }
|
|
|
|
|
|
public IWebRoutingSettings WebRoutingSettings => SettingsForTests.GenerateMockWebRoutingSettings();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-03-31 17:27:51 +11:00
|
|
|
|
/// Some test files are copied to the /bin (/bin/debug) on build, this is a utility to return their physical path based on a virtual path name
|
2020-03-13 12:08:25 +11:00
|
|
|
|
/// </summary>
|
2020-03-31 17:27:51 +11:00
|
|
|
|
/// <param name="relativePath"></param>
|
2020-03-13 12:08:25 +11:00
|
|
|
|
/// <returns></returns>
|
2020-03-31 17:27:51 +11:00
|
|
|
|
public string MapPathForTestFiles(string relativePath)
|
2020-03-13 12:08:25 +11:00
|
|
|
|
{
|
|
|
|
|
|
if (!relativePath.StartsWith("~/"))
|
2020-03-31 17:27:51 +11:00
|
|
|
|
throw new ArgumentException("relativePath must start with '~/'", nameof(relativePath));
|
2020-03-13 12:08:25 +11:00
|
|
|
|
|
2020-03-31 17:27:51 +11:00
|
|
|
|
var codeBase = typeof(TestHelperBase).Assembly.CodeBase;
|
|
|
|
|
|
var uri = new Uri(codeBase);
|
|
|
|
|
|
var path = uri.LocalPath;
|
|
|
|
|
|
var bin = Path.GetDirectoryName(path);
|
|
|
|
|
|
|
|
|
|
|
|
return relativePath.Replace("~/", bin + "/");
|
2020-03-13 12:08:25 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-23 17:16:33 +11:00
|
|
|
|
public IUmbracoVersion GetUmbracoVersion() => new UmbracoVersion(GetConfigs().Global());
|
2020-03-13 12:08:25 +11:00
|
|
|
|
|
|
|
|
|
|
public IRegister GetRegister()
|
|
|
|
|
|
{
|
2020-03-23 17:15:32 +11:00
|
|
|
|
return RegisterFactory.Create(GetConfigs().Global());
|
2020-03-13 12:08:25 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public abstract IHostingEnvironment GetHostingEnvironment();
|
2020-03-26 15:39:20 +11:00
|
|
|
|
public abstract IApplicationShutdownRegistry GetHostingEnvironmentLifetime();
|
2020-03-13 12:08:25 +11:00
|
|
|
|
|
|
|
|
|
|
public abstract IIpResolver GetIpResolver();
|
|
|
|
|
|
|
|
|
|
|
|
public IRequestCache GetRequestCache()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new DictionaryAppCache();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedUrlProvider GetPublishedUrlProvider()
|
|
|
|
|
|
{
|
|
|
|
|
|
var mock = new Mock<IPublishedUrlProvider>();
|
|
|
|
|
|
|
|
|
|
|
|
return mock.Object;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|