Fixing tests

This commit is contained in:
Shannon
2020-04-03 17:05:50 +11:00
parent 4920268ec8
commit d711796c59
18 changed files with 45 additions and 40 deletions

View File

@@ -1,4 +1,5 @@
using Moq;
using System.Collections.Generic;
using Moq;
using Semver;
using Umbraco.Core;
using Umbraco.Core.Configuration;
@@ -100,31 +101,26 @@ namespace Umbraco.Tests.Common
/// </summary>
private void ResetSettings()
{
_defaultGlobalSettings = null;
_defaultGlobalSettings.Clear();
_defaultHostingSettings = null;
}
private IGlobalSettings _defaultGlobalSettings;
private readonly Dictionary<SemVersion, IGlobalSettings> _defaultGlobalSettings = new Dictionary<SemVersion, IGlobalSettings>();
private IHostingSettings _defaultHostingSettings;
public IGlobalSettings GetDefaultGlobalSettings(IUmbracoVersion umbVersion)
{
if (_defaultGlobalSettings == null)
{
_defaultGlobalSettings = GenerateMockGlobalSettings(umbVersion);
}
return _defaultGlobalSettings;
if (_defaultGlobalSettings.TryGetValue(umbVersion.SemanticVersion, out var settings))
return settings;
settings = GenerateMockGlobalSettings(umbVersion);
_defaultGlobalSettings[umbVersion.SemanticVersion] = settings;
return settings;
}
public IHostingSettings GetDefaultHostingSettings()
{
if (_defaultHostingSettings == null)
{
_defaultHostingSettings = GenerateMockHostingSettings();
}
return _defaultHostingSettings;
}
public IHostingSettings DefaultHostingSettings => _defaultHostingSettings ?? (_defaultHostingSettings = GenerateMockHostingSettings());
private IHostingSettings GenerateMockHostingSettings()
public IHostingSettings GenerateMockHostingSettings()
{
var config = Mock.Of<IHostingSettings>(
settings =>

View File

@@ -106,7 +106,7 @@ namespace Umbraco.Tests.Integration.Implementations
public override IHostingEnvironment GetHostingEnvironment()
=> _hostingEnvironment ??= new TestHostingEnvironment(
SettingsForTests.GetDefaultHostingSettings(),
SettingsForTests.DefaultHostingSettings,
_hostEnvironment);
public override IApplicationShutdownRegistry GetHostingEnvironmentLifetime() => _hostingLifetime;

View File

@@ -33,7 +33,7 @@ namespace Umbraco.Tests.Components
var logger = Mock.Of<ILogger>();
var typeFinder = TestHelper.GetTypeFinder();
var f = new UmbracoDatabaseFactory(logger, SettingsForTests.GetDefaultGlobalSettings(), Mock.Of<IConnectionStrings>(), new Lazy<IMapperCollection>(() => new MapperCollection(Enumerable.Empty<BaseMapper>())), TestHelper.DbProviderFactoryCreator);
var f = new UmbracoDatabaseFactory(logger, SettingsForTests.DefaultGlobalSettings, Mock.Of<IConnectionStrings>(), new Lazy<IMapperCollection>(() => new MapperCollection(Enumerable.Empty<BaseMapper>())), TestHelper.DbProviderFactoryCreator);
var fs = new FileSystems(mock.Object, logger, TestHelper.IOHelper, SettingsForTests.GenerateMockGlobalSettings());
var coreDebug = Mock.Of<ICoreDebugSettings>();
var mediaFileSystem = Mock.Of<IMediaFileSystem>();

View File

@@ -20,7 +20,7 @@ namespace Umbraco.Tests.Configurations
{
var globalSettings = SettingsForTests.GenerateMockGlobalSettings();
var mockHostingSettings = Mock.Get(SettingsForTests.GetDefaultHostingSettings());
var mockHostingSettings = Mock.Get(SettingsForTests.GenerateMockHostingSettings());
mockHostingSettings.Setup(x => x.ApplicationVirtualPath).Returns(rootPath);
var hostingEnvironment = new AspNetHostingEnvironment(mockHostingSettings.Object);

View File

@@ -35,7 +35,7 @@ namespace Umbraco.Tests.CoreThings
public void Is_Back_Office_Request(string input, string virtualPath, bool expected)
{
var globalSettings = SettingsForTests.GenerateMockGlobalSettings();
var mockHostingSettings = Mock.Get(SettingsForTests.GetDefaultHostingSettings());
var mockHostingSettings = Mock.Get(SettingsForTests.GenerateMockHostingSettings());
mockHostingSettings.Setup(x => x.ApplicationVirtualPath).Returns(virtualPath);
var hostingEnvironment = new AspNetHostingEnvironment(mockHostingSettings.Object);

View File

@@ -40,7 +40,7 @@ namespace Umbraco.Tests.IO
composition.RegisterUnique<IMediaPathScheme, UniqueMediaPathScheme>();
composition.RegisterUnique(TestHelper.IOHelper);
composition.Configs.Add(SettingsForTests.GetDefaultGlobalSettings);
composition.Configs.Add(() => SettingsForTests.DefaultGlobalSettings);
composition.Configs.Add(SettingsForTests.GenerateMockContentSettings);
composition.ComposeFileSystems();

View File

@@ -14,7 +14,7 @@ namespace Umbraco.Tests.Models
[TestFixture]
public class UserTests
{
private IGlobalSettings GlobalSettings { get; } = SettingsForTests.GetDefaultGlobalSettings();
private IGlobalSettings GlobalSettings { get; } = SettingsForTests.DefaultGlobalSettings;
[Test]
public void Can_Deep_Clone()

View File

@@ -34,7 +34,7 @@ namespace Umbraco.Tests.Models
Current.Reset();
var configs = TestHelper.GetConfigs();
configs.Add(SettingsForTests.GetDefaultGlobalSettings);
configs.Add(() => SettingsForTests.DefaultGlobalSettings);
configs.Add(SettingsForTests.GenerateMockContentSettings);
_factory = Mock.Of<IFactory>();

View File

@@ -51,6 +51,7 @@ namespace Umbraco.Tests.Routing
public void Is_Reserved_By_Route(string url, bool shouldMatch)
{
//reset the app config, we only want to test routes not the hard coded paths
// TODO: Why are we using and modifying the global IGlobalSettings and not just a custom one?
var globalSettingsMock = Mock.Get(Factory.GetInstance<IGlobalSettings>()); //this will modify the IGlobalSettings instance stored in the container
globalSettingsMock.Setup(x => x.ReservedPaths).Returns("");
globalSettingsMock.Setup(x => x.ReservedUrls).Returns("");

View File

@@ -90,8 +90,8 @@ namespace Umbraco.Tests.Runtimes
private static readonly IIOHelper _ioHelper = TestHelper.IOHelper;
private static readonly IProfiler _profiler = new TestProfiler();
private static readonly Configs _configs = GetConfigs();
private static readonly IGlobalSettings _globalSettings = SettingsForTests.GetDefaultGlobalSettings();
private static readonly IHostingSettings _hostingSettings = SettingsForTests.GetDefaultHostingSettings();
private static readonly IGlobalSettings _globalSettings = SettingsForTests.DefaultGlobalSettings;
private static readonly IHostingSettings _hostingSettings = SettingsForTests.DefaultHostingSettings;
private static readonly IContentSettings _contentSettings = SettingsForTests.GenerateMockContentSettings();
private static readonly IWebRoutingSettings _settings = _configs.WebRouting();

View File

@@ -124,7 +124,7 @@ namespace Umbraco.Tests.Runtimes
.Append<DistributedCacheBinderComponent>();
// configure
composition.Configs.Add(TestHelpers.SettingsForTests.GetDefaultGlobalSettings);
composition.Configs.Add(() => TestHelpers.SettingsForTests.DefaultGlobalSettings);
composition.Configs.Add(TestHelpers.SettingsForTests.GenerateMockContentSettings);
// create and register the factory
@@ -163,7 +163,7 @@ namespace Umbraco.Tests.Runtimes
var scopeProvider = factory.GetInstance<IScopeProvider>();
using (var scope = scopeProvider.CreateScope())
{
var creator = new DatabaseSchemaCreator(scope.Database, logger, umbracoVersion, TestHelpers.SettingsForTests.GetDefaultGlobalSettings());
var creator = new DatabaseSchemaCreator(scope.Database, logger, umbracoVersion, TestHelpers.SettingsForTests.DefaultGlobalSettings);
creator.InitializeDatabaseSchema();
scope.Complete();
}

View File

@@ -41,7 +41,7 @@ namespace Umbraco.Tests.Scoping
composition.RegisterUnique(factory => new FileSystems(factory, factory.TryGetInstance<ILogger>(), TestHelper.IOHelper, SettingsForTests.GenerateMockGlobalSettings()));
composition.WithCollectionBuilder<MapperCollectionBuilder>();
composition.Configs.Add(SettingsForTests.GetDefaultGlobalSettings);
composition.Configs.Add(() => SettingsForTests.DefaultGlobalSettings);
composition.Configs.Add(SettingsForTests.GenerateMockContentSettings);
Current.Reset();

View File

@@ -45,9 +45,11 @@ namespace Umbraco.Tests.TestHelpers
public static void Reset() => _settingsForTests.Reset();
internal static IGlobalSettings GetDefaultGlobalSettings() => _settingsForTests.GetDefaultGlobalSettings(TestHelper.GetUmbracoVersion());
internal static IGlobalSettings DefaultGlobalSettings => _settingsForTests.GetDefaultGlobalSettings(TestHelper.GetUmbracoVersion());
internal static IHostingSettings GetDefaultHostingSettings() => _settingsForTests.GetDefaultHostingSettings();
internal static IHostingSettings DefaultHostingSettings => _settingsForTests.DefaultHostingSettings;
public static IHostingSettings GenerateMockHostingSettings() => _settingsForTests.GenerateMockHostingSettings();
public static IWebRoutingSettings GenerateMockWebRoutingSettings() => _settingsForTests.GenerateMockWebRoutingSettings();

View File

@@ -60,7 +60,7 @@ namespace Umbraco.Tests.TestHelpers
TestHelper.IOHelper, Mock.Of<ILogger>(), SettingsForTests.GenerateMockWebRoutingSettings());
public override IHostingEnvironment GetHostingEnvironment()
=> new AspNetHostingEnvironment(SettingsForTests.GetDefaultHostingSettings());
=> new AspNetHostingEnvironment(SettingsForTests.DefaultHostingSettings);
public override IApplicationShutdownRegistry GetHostingEnvironmentLifetime()
=> new AspNetApplicationShutdownRegistry();

View File

@@ -137,7 +137,7 @@ namespace Umbraco.Tests.TestHelpers
public IGlobalSettings GetGlobalSettings()
{
return SettingsForTests.GetDefaultGlobalSettings();
return SettingsForTests.DefaultGlobalSettings;
}
public IFileSystems GetFileSystemsMock()
{

View File

@@ -246,7 +246,7 @@ namespace Umbraco.Tests.TestHelpers
// var mappers = mappersBuilder.CreateCollection();
var mappers = Current.Factory.GetInstance<IMapperCollection>();
databaseFactory = new UmbracoDatabaseFactory(logger,
SettingsForTests.GetDefaultGlobalSettings(),
SettingsForTests.DefaultGlobalSettings,
new ConnectionStrings(),
Constants.System.UmbracoConnectionName,
new Lazy<IMapperCollection>(() => mappers),

View File

@@ -139,7 +139,7 @@ namespace Umbraco.Tests.Testing
protected virtual IProfilingLogger ProfilingLogger => Factory.GetInstance<IProfilingLogger>();
protected IHostingEnvironment HostingEnvironment { get; } = new AspNetHostingEnvironment(TestHelpers.SettingsForTests.GetDefaultHostingSettings());
protected IHostingEnvironment HostingEnvironment { get; } = new AspNetHostingEnvironment(TestHelpers.SettingsForTests.DefaultHostingSettings);
protected IApplicationShutdownRegistry HostingLifetime { get; } = new AspNetApplicationShutdownRegistry();
protected IIpResolver IpResolver => Factory.GetInstance<IIpResolver>();
protected IBackOfficeInfo BackOfficeInfo => Factory.GetInstance<IBackOfficeInfo>();
@@ -175,7 +175,7 @@ namespace Umbraco.Tests.Testing
TypeFinder = new TypeFinder(logger, new DefaultUmbracoAssemblyProvider(GetType().Assembly), new VaryingRuntimeHash());
var appCaches = GetAppCaches();
var globalSettings = TestHelpers.SettingsForTests.GetDefaultGlobalSettings();
var globalSettings = TestHelpers.SettingsForTests.DefaultGlobalSettings;
var settings = TestHelpers.SettingsForTests.GenerateMockWebRoutingSettings();
IBackOfficeInfo backOfficeInfo = new AspNetBackOfficeInfo(globalSettings, IOHelper, logger, settings);
@@ -413,8 +413,8 @@ namespace Umbraco.Tests.Testing
protected virtual void ComposeSettings()
{
Composition.Configs.Add(TestHelpers.SettingsForTests.GetDefaultGlobalSettings);
Composition.Configs.Add(TestHelpers.SettingsForTests.GetDefaultHostingSettings);
Composition.Configs.Add(() => TestHelpers.SettingsForTests.DefaultGlobalSettings);
Composition.Configs.Add(() => TestHelpers.SettingsForTests.DefaultHostingSettings);
Composition.Configs.Add(TestHelpers.SettingsForTests.GenerateMockRequestHandlerSettings);
Composition.Configs.Add(TestHelpers.SettingsForTests.GenerateMockWebRoutingSettings);
Composition.Configs.Add(TestHelpers.SettingsForTests.GenerateMockSecuritySettings);

View File

@@ -21,7 +21,9 @@ namespace Umbraco.Web.Hosting
ApplicationId = HostingEnvironment.ApplicationID;
// when we are not hosted (i.e. unit test or otherwise) we'll need to get the root path from the executing assembly
ApplicationPhysicalPath = HostingEnvironment.ApplicationPhysicalPath ?? Assembly.GetExecutingAssembly().GetRootDirectorySafe();
ApplicationVirtualPath = hostingSettings.ApplicationVirtualPath ?? HostingEnvironment.ApplicationVirtualPath?.EnsureStartsWith("/") ?? "/";
ApplicationVirtualPath = hostingSettings.ApplicationVirtualPath?.EnsureStartsWith('/')
?? HostingEnvironment.ApplicationVirtualPath?.EnsureStartsWith("/")
?? "/";
IISVersion = HttpRuntime.IISVersion;
}
@@ -38,7 +40,11 @@ namespace Umbraco.Web.Hosting
public string MapPath(string path)
{
return HostingEnvironment.MapPath(path);
if (HostingEnvironment.IsHosted)
return HostingEnvironment.MapPath(path);
// this will be the case in unit tests, we'll manually map the path
return ApplicationPhysicalPath + path.TrimStart("~").EnsureStartsWith("/");
}
public string ToAbsolute(string virtualPath) => VirtualPathUtility.ToAbsolute(virtualPath, ApplicationVirtualPath);