2020-12-19 08:17:35 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2020-12-08 10:42:26 +11:00
|
|
|
using System;
|
2020-03-13 12:08:25 +11:00
|
|
|
using System.IO;
|
2020-03-13 14:43:41 +11:00
|
|
|
using System.Reflection;
|
2020-10-27 10:53:01 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-09-18 08:39:38 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
2020-03-13 12:08:25 +11:00
|
|
|
using Moq;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
using Umbraco.Cms.Core.Cache;
|
|
|
|
|
using Umbraco.Cms.Core.Composing;
|
|
|
|
|
using Umbraco.Cms.Core.Configuration;
|
|
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Diagnostics;
|
|
|
|
|
using Umbraco.Cms.Core.Hosting;
|
|
|
|
|
using Umbraco.Cms.Core.IO;
|
|
|
|
|
using Umbraco.Cms.Core.Logging;
|
|
|
|
|
using Umbraco.Cms.Core.Models.PublishedContent;
|
|
|
|
|
using Umbraco.Cms.Core.Net;
|
|
|
|
|
using Umbraco.Cms.Core.Routing;
|
|
|
|
|
using Umbraco.Cms.Core.Runtime;
|
|
|
|
|
using Umbraco.Cms.Core.Serialization;
|
|
|
|
|
using Umbraco.Cms.Core.Strings;
|
2021-02-10 14:45:44 +01:00
|
|
|
using Umbraco.Cms.Tests.Common.TestHelpers;
|
2020-03-13 12:08:25 +11:00
|
|
|
using Umbraco.Core.Persistence;
|
|
|
|
|
using Umbraco.Core.Serialization;
|
2021-02-09 11:26:22 +01:00
|
|
|
using Umbraco.Extensions;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Constants = Umbraco.Cms.Core.Constants;
|
2020-03-13 12:08:25 +11:00
|
|
|
|
2021-02-10 14:45:44 +01:00
|
|
|
namespace Umbraco.Cms.Tests.Common
|
2020-03-13 12:08:25 +11:00
|
|
|
{
|
|
|
|
|
/// <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-31 18:01:27 +11:00
|
|
|
private string _workingDir;
|
2020-03-13 14:43:41 +11:00
|
|
|
|
2020-03-25 15:06:22 +11:00
|
|
|
protected TestHelperBase(Assembly entryAssembly)
|
2020-03-13 12:08:25 +11:00
|
|
|
{
|
2020-03-13 14:43:41 +11:00
|
|
|
MainDom = new SimpleMainDom();
|
2020-09-18 08:39:38 +02:00
|
|
|
_typeFinder = new TypeFinder(NullLoggerFactory.Instance.CreateLogger<TypeFinder>(), new DefaultUmbracoAssemblyProvider(entryAssembly), new VaryingRuntimeHash());
|
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
|
|
|
|
2020-12-19 08:17:35 +01:00
|
|
|
public TypeLoader GetMockedTypeLoader() =>
|
|
|
|
|
new TypeLoader(Mock.Of<ITypeFinder>(), Mock.Of<IAppPolicyCache>(), new DirectoryInfo(GetHostingEnvironment().MapPathContentRoot(Constants.SystemDirectories.TempData)), Mock.Of<ILogger<TypeLoader>>(), Mock.Of<IProfilingLogger>());
|
2020-03-13 12:08:25 +11:00
|
|
|
|
2020-12-19 08:17:35 +01:00
|
|
|
//// public Configs GetConfigs() => GetConfigsFactory().Create();
|
2020-03-23 16:39:27 +11:00
|
|
|
|
2020-03-13 12:08:25 +11:00
|
|
|
public abstract IBackOfficeInfo GetBackOfficeInfo();
|
|
|
|
|
|
2020-12-19 08:17:35 +01: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 18:01:27 +11:00
|
|
|
public virtual string WorkingDirectory
|
2020-03-13 12:08:25 +11:00
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-12-19 08:17:35 +01:00
|
|
|
if (_workingDir != null)
|
|
|
|
|
{
|
|
|
|
|
return _workingDir;
|
|
|
|
|
}
|
2020-03-31 18:01:27 +11:00
|
|
|
|
|
|
|
|
var dir = Path.Combine(Assembly.GetExecutingAssembly().GetRootDirectorySafe(), "TEMP");
|
|
|
|
|
|
2020-03-31 17:27:51 +11:00
|
|
|
if (!Directory.Exists(dir))
|
2020-12-19 08:17:35 +01:00
|
|
|
{
|
2020-03-31 17:27:51 +11:00
|
|
|
Directory.CreateDirectory(dir);
|
2020-12-19 08:17:35 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-31 18:01:27 +11:00
|
|
|
_workingDir = dir;
|
|
|
|
|
return _workingDir;
|
2020-03-13 12:08:25 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IShortStringHelper ShortStringHelper { get; } = new DefaultShortStringHelper(new DefaultShortStringHelperConfig());
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2020-03-13 12:08:25 +11:00
|
|
|
public IJsonSerializer JsonSerializer { get; } = new JsonNetSerializer();
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2020-03-13 12:08:25 +11:00
|
|
|
public IVariationContextAccessor VariationContextAccessor { get; } = new TestVariationContextAccessor();
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2020-03-13 12:08:25 +11:00
|
|
|
public abstract IDbProviderFactoryCreator DbProviderFactoryCreator { get; }
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2020-03-13 12:08:25 +11:00
|
|
|
public abstract IBulkSqlInsertProvider BulkSqlInsertProvider { get; }
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2020-03-13 12:08:25 +11:00
|
|
|
public abstract IMarchal Marchal { get; }
|
2020-12-19 08:17:35 +01:00
|
|
|
|
|
|
|
|
public CoreDebugSettings 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-11-18 04:49:03 -08:00
|
|
|
{
|
2020-12-19 08:17:35 +01:00
|
|
|
IHostingEnvironment hostingEnvironment = GetHostingEnvironment();
|
2020-11-30 08:51:12 +01:00
|
|
|
|
|
|
|
|
if (TestEnvironment.IsWindows)
|
|
|
|
|
{
|
|
|
|
|
_ioHelper = new IOHelperWindows(hostingEnvironment);
|
|
|
|
|
}
|
|
|
|
|
else if (TestEnvironment.IsLinux)
|
|
|
|
|
{
|
|
|
|
|
_ioHelper = new IOHelperLinux(hostingEnvironment);
|
|
|
|
|
}
|
|
|
|
|
else if (TestEnvironment.IsOSX)
|
|
|
|
|
{
|
|
|
|
|
_ioHelper = new IOHelperOSX(hostingEnvironment);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException("Unexpected OS");
|
|
|
|
|
}
|
2020-11-18 04:49:03 -08:00
|
|
|
}
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2020-03-13 14:43:41 +11:00
|
|
|
return _ioHelper;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-13 12:08:25 +11:00
|
|
|
|
|
|
|
|
public IMainDom MainDom { get; }
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2020-03-13 14:43:41 +11:00
|
|
|
public UriUtility UriUtility
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_uriUtility == null)
|
2020-12-19 08:17:35 +01:00
|
|
|
{
|
2020-03-13 14:43:41 +11:00
|
|
|
_uriUtility = new UriUtility(GetHostingEnvironment());
|
2020-12-19 08:17:35 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-13 14:43:41 +11:00
|
|
|
return _uriUtility;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2020-03-13 12:08:25 +11:00
|
|
|
/// <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-04-14 16:55:54 +01:00
|
|
|
public virtual string MapPathForTestFiles(string relativePath)
|
2020-03-13 12:08:25 +11:00
|
|
|
{
|
|
|
|
|
if (!relativePath.StartsWith("~/"))
|
2020-12-19 08:17:35 +01:00
|
|
|
{
|
2020-03-31 17:27:51 +11:00
|
|
|
throw new ArgumentException("relativePath must start with '~/'", nameof(relativePath));
|
2020-12-19 08:17:35 +01:00
|
|
|
}
|
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-09-03 11:36:57 +02:00
|
|
|
public IUmbracoVersion GetUmbracoVersion() => new UmbracoVersion();
|
2020-03-13 12:08:25 +11:00
|
|
|
|
2020-12-19 08:17:35 +01:00
|
|
|
public IServiceCollection GetRegister() => new ServiceCollection();
|
2020-03-13 12:08:25 +11:00
|
|
|
|
|
|
|
|
public abstract IHostingEnvironment GetHostingEnvironment();
|
2020-12-19 08:17:35 +01:00
|
|
|
|
2020-03-26 15:39:20 +11:00
|
|
|
public abstract IApplicationShutdownRegistry GetHostingEnvironmentLifetime();
|
2020-03-13 12:08:25 +11:00
|
|
|
|
|
|
|
|
public abstract IIpResolver GetIpResolver();
|
|
|
|
|
|
2020-12-19 08:17:35 +01:00
|
|
|
public IRequestCache GetRequestCache() => new DictionaryAppCache();
|
2020-03-13 12:08:25 +11:00
|
|
|
|
|
|
|
|
public IPublishedUrlProvider GetPublishedUrlProvider()
|
|
|
|
|
{
|
|
|
|
|
var mock = new Mock<IPublishedUrlProvider>();
|
|
|
|
|
|
|
|
|
|
return mock.Object;
|
|
|
|
|
}
|
2020-04-22 14:23:56 +10:00
|
|
|
|
|
|
|
|
public ILoggingConfiguration GetLoggingConfiguration(IHostingEnvironment hostingEnv = null)
|
|
|
|
|
{
|
|
|
|
|
hostingEnv = hostingEnv ?? GetHostingEnvironment();
|
|
|
|
|
return new LoggingConfiguration(
|
2020-12-19 08:17:35 +01:00
|
|
|
Path.Combine(hostingEnv.ApplicationPhysicalPath, "umbraco", "logs"));
|
2020-04-22 14:23:56 +10:00
|
|
|
}
|
2020-03-13 12:08:25 +11:00
|
|
|
}
|
|
|
|
|
}
|