diff --git a/src/Umbraco.Tests.Common/SettingsForTests.cs b/src/Umbraco.Tests.Common/SettingsForTests.cs index c3ba47e61b..0f7836c788 100644 --- a/src/Umbraco.Tests.Common/SettingsForTests.cs +++ b/src/Umbraco.Tests.Common/SettingsForTests.cs @@ -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 /// private void ResetSettings() { - _defaultGlobalSettings = null; + _defaultGlobalSettings.Clear(); + _defaultHostingSettings = null; } - private IGlobalSettings _defaultGlobalSettings; + private readonly Dictionary _defaultGlobalSettings = new Dictionary(); 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( settings => diff --git a/src/Umbraco.Tests.Integration/Implementations/TestHelper.cs b/src/Umbraco.Tests.Integration/Implementations/TestHelper.cs index d5b2d1a196..eb96ca01a8 100644 --- a/src/Umbraco.Tests.Integration/Implementations/TestHelper.cs +++ b/src/Umbraco.Tests.Integration/Implementations/TestHelper.cs @@ -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; diff --git a/src/Umbraco.Tests/Components/ComponentTests.cs b/src/Umbraco.Tests/Components/ComponentTests.cs index 807c12973c..8438ec0109 100644 --- a/src/Umbraco.Tests/Components/ComponentTests.cs +++ b/src/Umbraco.Tests/Components/ComponentTests.cs @@ -33,7 +33,7 @@ namespace Umbraco.Tests.Components var logger = Mock.Of(); var typeFinder = TestHelper.GetTypeFinder(); - var f = new UmbracoDatabaseFactory(logger, SettingsForTests.GetDefaultGlobalSettings(), Mock.Of(), new Lazy(() => new MapperCollection(Enumerable.Empty())), TestHelper.DbProviderFactoryCreator); + var f = new UmbracoDatabaseFactory(logger, SettingsForTests.DefaultGlobalSettings, Mock.Of(), new Lazy(() => new MapperCollection(Enumerable.Empty())), TestHelper.DbProviderFactoryCreator); var fs = new FileSystems(mock.Object, logger, TestHelper.IOHelper, SettingsForTests.GenerateMockGlobalSettings()); var coreDebug = Mock.Of(); var mediaFileSystem = Mock.Of(); diff --git a/src/Umbraco.Tests/Configurations/GlobalSettingsTests.cs b/src/Umbraco.Tests/Configurations/GlobalSettingsTests.cs index 327c801f5b..56237f562c 100644 --- a/src/Umbraco.Tests/Configurations/GlobalSettingsTests.cs +++ b/src/Umbraco.Tests/Configurations/GlobalSettingsTests.cs @@ -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); diff --git a/src/Umbraco.Tests/CoreThings/UriExtensionsTests.cs b/src/Umbraco.Tests/CoreThings/UriExtensionsTests.cs index 1fe3a50d0c..6fdf9d9385 100644 --- a/src/Umbraco.Tests/CoreThings/UriExtensionsTests.cs +++ b/src/Umbraco.Tests/CoreThings/UriExtensionsTests.cs @@ -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); diff --git a/src/Umbraco.Tests/IO/FileSystemsTests.cs b/src/Umbraco.Tests/IO/FileSystemsTests.cs index bf726c6fd8..bbd306ad6d 100644 --- a/src/Umbraco.Tests/IO/FileSystemsTests.cs +++ b/src/Umbraco.Tests/IO/FileSystemsTests.cs @@ -40,7 +40,7 @@ namespace Umbraco.Tests.IO composition.RegisterUnique(); composition.RegisterUnique(TestHelper.IOHelper); - composition.Configs.Add(SettingsForTests.GetDefaultGlobalSettings); + composition.Configs.Add(() => SettingsForTests.DefaultGlobalSettings); composition.Configs.Add(SettingsForTests.GenerateMockContentSettings); composition.ComposeFileSystems(); diff --git a/src/Umbraco.Tests/Models/UserTests.cs b/src/Umbraco.Tests/Models/UserTests.cs index 531112e4b6..0bd6b17cf1 100644 --- a/src/Umbraco.Tests/Models/UserTests.cs +++ b/src/Umbraco.Tests/Models/UserTests.cs @@ -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() diff --git a/src/Umbraco.Tests/Models/VariationTests.cs b/src/Umbraco.Tests/Models/VariationTests.cs index f353ee3c8a..774c1cb612 100644 --- a/src/Umbraco.Tests/Models/VariationTests.cs +++ b/src/Umbraco.Tests/Models/VariationTests.cs @@ -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(); diff --git a/src/Umbraco.Tests/Routing/RoutableDocumentFilterTests.cs b/src/Umbraco.Tests/Routing/RoutableDocumentFilterTests.cs index bedd8a8744..a78459fa39 100644 --- a/src/Umbraco.Tests/Routing/RoutableDocumentFilterTests.cs +++ b/src/Umbraco.Tests/Routing/RoutableDocumentFilterTests.cs @@ -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()); //this will modify the IGlobalSettings instance stored in the container globalSettingsMock.Setup(x => x.ReservedPaths).Returns(""); globalSettingsMock.Setup(x => x.ReservedUrls).Returns(""); diff --git a/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs b/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs index 6ccb2c3b35..488a4f6dad 100644 --- a/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs +++ b/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs @@ -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(); diff --git a/src/Umbraco.Tests/Runtimes/StandaloneTests.cs b/src/Umbraco.Tests/Runtimes/StandaloneTests.cs index 1b6526c24b..1a4c7f2040 100644 --- a/src/Umbraco.Tests/Runtimes/StandaloneTests.cs +++ b/src/Umbraco.Tests/Runtimes/StandaloneTests.cs @@ -124,7 +124,7 @@ namespace Umbraco.Tests.Runtimes .Append(); // 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(); 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(); } diff --git a/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs b/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs index fd37192da2..40e0f60fe6 100644 --- a/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs +++ b/src/Umbraco.Tests/Scoping/ScopeEventDispatcherTests.cs @@ -41,7 +41,7 @@ namespace Umbraco.Tests.Scoping composition.RegisterUnique(factory => new FileSystems(factory, factory.TryGetInstance(), TestHelper.IOHelper, SettingsForTests.GenerateMockGlobalSettings())); composition.WithCollectionBuilder(); - composition.Configs.Add(SettingsForTests.GetDefaultGlobalSettings); + composition.Configs.Add(() => SettingsForTests.DefaultGlobalSettings); composition.Configs.Add(SettingsForTests.GenerateMockContentSettings); Current.Reset(); diff --git a/src/Umbraco.Tests/TestHelpers/SettingsForTests.cs b/src/Umbraco.Tests/TestHelpers/SettingsForTests.cs index fc3d3f6b52..b58301287b 100644 --- a/src/Umbraco.Tests/TestHelpers/SettingsForTests.cs +++ b/src/Umbraco.Tests/TestHelpers/SettingsForTests.cs @@ -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(); diff --git a/src/Umbraco.Tests/TestHelpers/TestHelper.cs b/src/Umbraco.Tests/TestHelpers/TestHelper.cs index aab0903f1d..7eca49183d 100644 --- a/src/Umbraco.Tests/TestHelpers/TestHelper.cs +++ b/src/Umbraco.Tests/TestHelpers/TestHelper.cs @@ -60,7 +60,7 @@ namespace Umbraco.Tests.TestHelpers TestHelper.IOHelper, Mock.Of(), SettingsForTests.GenerateMockWebRoutingSettings()); public override IHostingEnvironment GetHostingEnvironment() - => new AspNetHostingEnvironment(SettingsForTests.GetDefaultHostingSettings()); + => new AspNetHostingEnvironment(SettingsForTests.DefaultHostingSettings); public override IApplicationShutdownRegistry GetHostingEnvironmentLifetime() => new AspNetApplicationShutdownRegistry(); diff --git a/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs b/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs index 585ad6cc9e..76c8173ad0 100644 --- a/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs +++ b/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs @@ -137,7 +137,7 @@ namespace Umbraco.Tests.TestHelpers public IGlobalSettings GetGlobalSettings() { - return SettingsForTests.GetDefaultGlobalSettings(); + return SettingsForTests.DefaultGlobalSettings; } public IFileSystems GetFileSystemsMock() { diff --git a/src/Umbraco.Tests/TestHelpers/TestObjects.cs b/src/Umbraco.Tests/TestHelpers/TestObjects.cs index cc61c84abd..c27bd046c8 100644 --- a/src/Umbraco.Tests/TestHelpers/TestObjects.cs +++ b/src/Umbraco.Tests/TestHelpers/TestObjects.cs @@ -246,7 +246,7 @@ namespace Umbraco.Tests.TestHelpers // var mappers = mappersBuilder.CreateCollection(); var mappers = Current.Factory.GetInstance(); databaseFactory = new UmbracoDatabaseFactory(logger, - SettingsForTests.GetDefaultGlobalSettings(), + SettingsForTests.DefaultGlobalSettings, new ConnectionStrings(), Constants.System.UmbracoConnectionName, new Lazy(() => mappers), diff --git a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs index 56f0534cf4..b635e63e94 100644 --- a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs +++ b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs @@ -139,7 +139,7 @@ namespace Umbraco.Tests.Testing protected virtual IProfilingLogger ProfilingLogger => Factory.GetInstance(); - 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(); protected IBackOfficeInfo BackOfficeInfo => Factory.GetInstance(); @@ -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); diff --git a/src/Umbraco.Web/AspNet/AspNetHostingEnvironment.cs b/src/Umbraco.Web/AspNet/AspNetHostingEnvironment.cs index 59271bf88d..03c12c36f8 100644 --- a/src/Umbraco.Web/AspNet/AspNetHostingEnvironment.cs +++ b/src/Umbraco.Web/AspNet/AspNetHostingEnvironment.cs @@ -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);