From 03f22e93626eee865562b50aa63e0c94caf43253 Mon Sep 17 00:00:00 2001 From: Shannon Date: Mon, 21 Dec 2020 16:44:50 +1100 Subject: [PATCH] Fixing tests after merge --- .../Runtime/CoreRuntime.cs | 2 - .../PublishedSnapshotServiceEventHandler.cs | 10 +++++ .../Testing/UmbracoIntegrationTest.cs | 31 +++++++------- .../ContentTypeServiceVariantsTests.cs | 41 +++++++++++-------- .../ApplicationBuilderExtensions.cs | 3 +- 5 files changed, 53 insertions(+), 34 deletions(-) diff --git a/src/Umbraco.Infrastructure/Runtime/CoreRuntime.cs b/src/Umbraco.Infrastructure/Runtime/CoreRuntime.cs index 55fa3457ed..35b7443338 100644 --- a/src/Umbraco.Infrastructure/Runtime/CoreRuntime.cs +++ b/src/Umbraco.Infrastructure/Runtime/CoreRuntime.cs @@ -43,8 +43,6 @@ namespace Umbraco.Infrastructure.Runtime _databaseFactory = databaseFactory; _eventAggregator = eventAggregator; _hostingEnvironment = hostingEnvironment; - - _logger = _loggerFactory.CreateLogger(); } diff --git a/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotServiceEventHandler.cs b/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotServiceEventHandler.cs index 31bc9b3d63..084ed569ca 100644 --- a/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotServiceEventHandler.cs +++ b/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotServiceEventHandler.cs @@ -11,6 +11,9 @@ using Umbraco.Infrastructure.PublishedCache.Persistence; namespace Umbraco.Web.PublishedCache.NuCache { + /// + /// Subscribes to Umbraco events to ensure nucache remains consistent with the source data + /// public class PublishedSnapshotServiceEventHandler : IDisposable { private readonly IRuntimeState _runtime; @@ -18,6 +21,9 @@ namespace Umbraco.Web.PublishedCache.NuCache private readonly IPublishedSnapshotService _publishedSnapshotService; private readonly INuCacheContentService _publishedContentService; + /// + /// Initializes a new instance of the class. + /// public PublishedSnapshotServiceEventHandler( IRuntimeState runtime, IPublishedSnapshotService publishedSnapshotService, @@ -28,6 +34,10 @@ namespace Umbraco.Web.PublishedCache.NuCache _publishedContentService = publishedContentService; } + /// + /// Binds to the Umbraco events + /// + /// Returns true if binding occurred public bool Start() { // however, the cache is NOT available until we are configured, because loading diff --git a/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs b/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs index d3abd76d7b..7aea7f37a5 100644 --- a/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs +++ b/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs @@ -50,7 +50,10 @@ namespace Umbraco.Tests.Integration.Testing public void OnTestTearDown(Action tearDown) { if (_testTeardown == null) + { _testTeardown = new List(); + } + _testTeardown.Add(tearDown); } @@ -60,7 +63,9 @@ namespace Umbraco.Tests.Integration.Testing public void FixtureTearDown() { foreach (var a in _fixtureTeardown) + { a(); + } } [TearDown] @@ -69,7 +74,9 @@ namespace Umbraco.Tests.Integration.Testing if (_testTeardown != null) { foreach (var a in _testTeardown) + { a(); + } } _testTeardown = null; @@ -107,7 +114,7 @@ namespace Umbraco.Tests.Integration.Testing Configure(app); } - protected void BeforeHostStart(IHost host) + protected virtual void BeforeHostStart(IHost host) { Services = host.Services; UseTestDatabase(Services); @@ -149,7 +156,6 @@ namespace Umbraco.Tests.Integration.Testing /// /// Create the Generic Host and execute startup ConfigureServices/Configure calls /// - /// public virtual IHostBuilder CreateHostBuilder() { var hostBuilder = Host.CreateDefaultBuilder() @@ -183,8 +189,6 @@ namespace Umbraco.Tests.Integration.Testing #endregion - #region IStartup - public virtual void ConfigureServices(IServiceCollection services) { services.AddSingleton(TestHelper.DbProviderFactoryCreator); @@ -245,8 +249,6 @@ namespace Umbraco.Tests.Integration.Testing app.UseUmbracoCore(); // This no longer starts CoreRuntime, it's very fast } - #endregion - #region LocalDb private static readonly object _dbLocker = new object(); @@ -387,8 +389,6 @@ namespace Umbraco.Tests.Integration.Testing #endregion - #region Common services - protected UmbracoTestAttribute TestOptions => TestOptionAttributeBase.GetTestOptions(); protected virtual T GetRequiredService() => Services.GetRequiredService(); @@ -420,13 +420,16 @@ namespace Umbraco.Tests.Integration.Testing /// Returns the /// protected ILoggerFactory LoggerFactory => Services.GetRequiredService(); - protected AppCaches AppCaches => Services.GetRequiredService(); - protected IIOHelper IOHelper => Services.GetRequiredService(); - protected IShortStringHelper ShortStringHelper => Services.GetRequiredService(); - protected GlobalSettings GlobalSettings => Services.GetRequiredService>().Value; - protected IMapperCollection Mappers => Services.GetRequiredService(); - #endregion + protected AppCaches AppCaches => Services.GetRequiredService(); + + protected IIOHelper IOHelper => Services.GetRequiredService(); + + protected IShortStringHelper ShortStringHelper => Services.GetRequiredService(); + + protected GlobalSettings GlobalSettings => Services.GetRequiredService>().Value; + + protected IMapperCollection Mappers => Services.GetRequiredService(); #region Builders diff --git a/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentTypeServiceVariantsTests.cs b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentTypeServiceVariantsTests.cs index e9ec0cbae0..1fbfb00a1b 100644 --- a/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentTypeServiceVariantsTests.cs +++ b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentTypeServiceVariantsTests.cs @@ -2,6 +2,8 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using NUnit.Framework; using Umbraco.Core.Cache; using Umbraco.Core.Configuration.Models; @@ -25,11 +27,33 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Services public class ContentTypeServiceVariantsTests : UmbracoIntegrationTest { private ISqlContext SqlContext => GetRequiredService(); + private IContentService ContentService => GetRequiredService(); + private IContentTypeService ContentTypeService => GetRequiredService(); + private IRedirectUrlService RedirectUrlService => GetRequiredService(); + private ILocalizationService LocalizationService => GetRequiredService(); + protected override void BeforeHostStart(IHost host) + { + base.BeforeHostStart(host); + + // Ensure that the events are bound on each test + PublishedSnapshotServiceEventHandler eventBinder = host.Services.GetRequiredService(); + eventBinder.Start(); + } + + public override void TearDown() + { + // Ensure this is dipsosed + // TODO: How come MSDI doesn't automatically dispose all of these at the end of each test? + // How can we automatically dispose of all IDisposables at the end of the tests as if it were an aspnet app? + Services.GetRequiredService().Dispose(); + base.TearDown(); + } + private void AssertJsonStartsWith(int id, string expected) { var json = GetJson(id).Replace('"', '\''); @@ -512,9 +536,6 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Services { // one simple content type, variant, with both variant and invariant properties // can change it to invariant and back - - //hack to ensure events are initialized - (GetRequiredService() as PublishedSnapshotService)?.OnApplicationInit(null, null); CreateFrenchAndEnglishLangs(); var contentType = CreateContentType(ContentVariation.Culture); @@ -603,10 +624,6 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Services // one simple content type, invariant // can change it to variant and back // can then switch one property to variant - - //hack to ensure events are initialized - (GetRequiredService() as PublishedSnapshotService)?.OnApplicationInit(null, null); - var globalSettings = new GlobalSettings(); var languageEn = new Language(globalSettings, "en") { IsDefault = true }; @@ -696,9 +713,6 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Services { // one simple content type, variant, with both variant and invariant properties // can change an invariant property to variant and back - - //hack to ensure events are initialized - (GetRequiredService() as PublishedSnapshotService)?.OnApplicationInit(null, null); CreateFrenchAndEnglishLangs(); var contentType = CreateContentType(ContentVariation.Culture); @@ -893,7 +907,6 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Services { // one simple content type, variant, with both variant and invariant properties // can change an invariant property to variant and back - CreateFrenchAndEnglishLangs(); var contentType = CreateContentType(ContentVariation.Culture | ContentVariation.Segment); @@ -974,9 +987,6 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Services // one composed content type, variant, with both variant and invariant properties // can change the composing content type to invariant and back // can change the composed content type to invariant and back - - //hack to ensure events are initialized - (GetRequiredService() as PublishedSnapshotService)?.OnApplicationInit(null, null); CreateFrenchAndEnglishLangs(); var composing = CreateContentType(ContentVariation.Culture, "composing"); @@ -1071,9 +1081,6 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Services // one composed content type, invariant // can change the composing content type to invariant and back // can change the variant composed content type to invariant and back - - //hack to ensure events are initialized - (GetRequiredService() as PublishedSnapshotService)?.OnApplicationInit(null, null); CreateFrenchAndEnglishLangs(); var composing = CreateContentType(ContentVariation.Culture, "composing"); diff --git a/src/Umbraco.Web.Common/Extensions/ApplicationBuilderExtensions.cs b/src/Umbraco.Web.Common/Extensions/ApplicationBuilderExtensions.cs index a118fa746c..0f2d1ffcdd 100644 --- a/src/Umbraco.Web.Common/Extensions/ApplicationBuilderExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/ApplicationBuilderExtensions.cs @@ -10,6 +10,7 @@ using Smidge.Nuglify; using StackExchange.Profiling; using Umbraco.Core; using Umbraco.Core.Configuration.Models; +using Umbraco.Core.Hosting; using Umbraco.Infrastructure.Logging.Serilog.Enrichers; using Umbraco.Web.Common.Middleware; using Umbraco.Web.Common.Plugins; @@ -79,7 +80,7 @@ namespace Umbraco.Extensions } /// - /// Start Umbraco + /// Enables core Umbraco functionality /// public static IApplicationBuilder UseUmbracoCore(this IApplicationBuilder app) {