2020-12-23 11:35:49 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2020-12-03 13:32:04 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
using System.IO;
|
2020-12-22 10:43:07 +00:00
|
|
|
using System.Threading.Tasks;
|
2020-03-30 20:55:13 +11:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
2020-12-24 08:55:21 +01:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2020-12-03 13:32:04 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2020-03-30 20:55:13 +11:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2020-12-03 13:32:04 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Moq;
|
2020-03-30 20:55:13 +11:00
|
|
|
using NUnit.Framework;
|
2020-12-03 13:32:04 +00:00
|
|
|
using Serilog;
|
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.Models;
|
|
|
|
|
using Umbraco.Cms.Core.DependencyInjection;
|
|
|
|
|
using Umbraco.Cms.Core.IO;
|
2021-02-15 11:41:12 +01:00
|
|
|
using Umbraco.Cms.Core.Scoping;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Core.Strings;
|
|
|
|
|
using Umbraco.Cms.Core.Web;
|
2021-02-12 10:41:07 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.DependencyInjection;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Mappers;
|
2021-02-10 14:45:44 +01:00
|
|
|
using Umbraco.Cms.Tests.Common.Builders;
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Testing;
|
2021-02-11 08:30:27 +01:00
|
|
|
using Umbraco.Cms.Tests.Integration.DependencyInjection;
|
|
|
|
|
using Umbraco.Cms.Tests.Integration.Extensions;
|
|
|
|
|
using Umbraco.Cms.Tests.Integration.Implementations;
|
2020-12-03 13:32:04 +00:00
|
|
|
using Umbraco.Extensions;
|
2020-03-30 20:55:13 +11:00
|
|
|
|
2021-02-11 08:30:27 +01:00
|
|
|
namespace Umbraco.Cms.Tests.Integration.Testing
|
2020-03-30 20:55:13 +11:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Abstract class for integration tests
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This will use a Host Builder to boot and install Umbraco ready for use
|
|
|
|
|
/// </remarks>
|
|
|
|
|
[SingleThreaded]
|
|
|
|
|
[NonParallelizable]
|
|
|
|
|
public abstract class UmbracoIntegrationTest
|
|
|
|
|
{
|
2020-09-04 00:27:43 +10:00
|
|
|
private List<Action> _testTeardown = null;
|
2020-12-23 11:35:49 +01:00
|
|
|
private readonly List<Action> _fixtureTeardown = new List<Action>();
|
2020-09-02 18:10:29 +10:00
|
|
|
|
2020-09-04 00:27:43 +10:00
|
|
|
public void OnTestTearDown(Action tearDown)
|
|
|
|
|
{
|
|
|
|
|
if (_testTeardown == null)
|
2020-12-21 16:44:50 +11:00
|
|
|
{
|
2020-09-04 00:27:43 +10:00
|
|
|
_testTeardown = new List<Action>();
|
2020-12-21 16:44:50 +11:00
|
|
|
}
|
|
|
|
|
|
2020-09-04 00:27:43 +10:00
|
|
|
_testTeardown.Add(tearDown);
|
|
|
|
|
}
|
2020-09-02 18:10:29 +10:00
|
|
|
|
2020-09-04 00:27:43 +10:00
|
|
|
public void OnFixtureTearDown(Action tearDown) => _fixtureTeardown.Add(tearDown);
|
2020-09-02 18:10:29 +10:00
|
|
|
|
|
|
|
|
[OneTimeTearDown]
|
2020-09-04 00:27:43 +10:00
|
|
|
public void FixtureTearDown()
|
|
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
foreach (Action a in _fixtureTeardown)
|
2020-12-21 16:44:50 +11:00
|
|
|
{
|
2020-12-03 13:32:04 +00:00
|
|
|
a();
|
2020-12-21 16:44:50 +11:00
|
|
|
}
|
2020-09-04 00:27:43 +10:00
|
|
|
}
|
2020-09-02 18:10:29 +10:00
|
|
|
|
|
|
|
|
[TearDown]
|
2020-12-22 10:43:07 +00:00
|
|
|
public async Task TearDownAsync()
|
2020-09-04 00:27:43 +10:00
|
|
|
{
|
2020-11-10 08:50:47 +00:00
|
|
|
if (_testTeardown != null)
|
|
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
foreach (Action a in _testTeardown)
|
2020-12-21 16:44:50 +11:00
|
|
|
{
|
2020-12-03 13:32:04 +00:00
|
|
|
a();
|
2020-12-21 16:44:50 +11:00
|
|
|
}
|
2020-11-10 08:50:47 +00:00
|
|
|
}
|
2020-12-15 15:20:36 +00:00
|
|
|
|
2020-09-04 00:27:43 +10:00
|
|
|
_testTeardown = null;
|
2021-03-04 16:18:57 +11:00
|
|
|
_firstTestInFixture = false;
|
|
|
|
|
s_firstTestInSession = false;
|
2020-12-15 15:20:36 +00:00
|
|
|
|
|
|
|
|
// Ensure CoreRuntime stopped (now it's a HostedService)
|
2021-04-27 07:35:47 +02:00
|
|
|
IHost host = Services?.GetService<IHost>();
|
|
|
|
|
if (host is not null)
|
|
|
|
|
{
|
|
|
|
|
await host.StopAsync();
|
|
|
|
|
host.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 00:27:43 +10:00
|
|
|
}
|
2020-09-02 18:10:29 +10:00
|
|
|
|
2020-10-27 14:10:19 +01:00
|
|
|
[TearDown]
|
2020-12-23 11:35:49 +01:00
|
|
|
public virtual void TearDown_Logging() =>
|
2020-10-16 10:45:22 +02:00
|
|
|
TestContext.Progress.Write($" {TestContext.CurrentContext.Result.Outcome.Status}");
|
2020-09-02 18:10:29 +10:00
|
|
|
|
2020-10-27 14:10:19 +01:00
|
|
|
[SetUp]
|
2020-12-23 11:35:49 +01:00
|
|
|
public virtual void SetUp_Logging() =>
|
2021-03-04 16:18:57 +11:00
|
|
|
TestContext.Progress.Write($"Start test {s_testCount++}: {TestContext.CurrentContext.Test.Name}");
|
2020-11-10 08:50:47 +00:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
[SetUp]
|
2020-10-05 10:02:11 +02:00
|
|
|
public virtual void Setup()
|
2020-09-02 18:10:29 +10:00
|
|
|
{
|
2021-03-07 19:20:16 +01:00
|
|
|
InMemoryConfiguration[Constants.Configuration.ConfigUnattended + ":" + nameof(UnattendedSettings.InstallUnattended)] = "true";
|
2020-12-23 11:35:49 +01:00
|
|
|
IHostBuilder hostBuilder = CreateHostBuilder();
|
2020-10-16 08:38:25 +02:00
|
|
|
|
2020-12-17 11:15:58 +00:00
|
|
|
IHost host = hostBuilder.Build();
|
2020-12-17 12:18:28 +00:00
|
|
|
BeforeHostStart(host);
|
2020-12-17 11:15:58 +00:00
|
|
|
host.Start();
|
|
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
var app = new ApplicationBuilder(host.Services);
|
2020-10-16 10:45:22 +02:00
|
|
|
Configure(app);
|
2020-09-02 18:10:29 +10:00
|
|
|
}
|
|
|
|
|
|
2020-12-21 16:44:50 +11:00
|
|
|
protected virtual void BeforeHostStart(IHost host)
|
2020-12-17 12:18:28 +00:00
|
|
|
{
|
|
|
|
|
Services = host.Services;
|
|
|
|
|
UseTestDatabase(Services);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-06 18:43:07 +02:00
|
|
|
private ILoggerFactory CreateLoggerFactory()
|
|
|
|
|
{
|
2020-11-19 20:05:28 +00:00
|
|
|
try
|
|
|
|
|
{
|
2020-12-16 01:54:49 +00:00
|
|
|
switch (TestOptions.Logger)
|
2020-11-19 20:05:28 +00:00
|
|
|
{
|
|
|
|
|
case UmbracoTestOptions.Logger.Mock:
|
|
|
|
|
return NullLoggerFactory.Instance;
|
|
|
|
|
case UmbracoTestOptions.Logger.Serilog:
|
2020-12-17 11:15:58 +00:00
|
|
|
return Microsoft.Extensions.Logging.LoggerFactory.Create(builder =>
|
|
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
string path = Path.Combine(TestHelper.WorkingDirectory, "logs", "umbraco_integration_tests_.txt");
|
2020-12-17 11:15:58 +00:00
|
|
|
|
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
|
|
|
.WriteTo.File(path, rollingInterval: RollingInterval.Day)
|
Examine 2.0 integration (#10241)
* Init commit for examine 2.0 work, most old umb examine tests working, probably a lot that doesn't
* Gets Umbraco Examine tests passing and makes some sense out of them, fixes some underlying issues.
* Large refactor, remove TaskHelper, rename Notifications to be consistent, Gets all examine/lucene indexes building and startup ordered in the correct way, removes old files, creates new IUmbracoIndexingHandler for abstracting out all index operations for umbraco data, abstracts out IIndexRebuilder, Fixes Stack overflow with LiveModelsProvider and loading assemblies, ports some changes from v8 for startup handling with cold boots, refactors out LastSyncedFileManager
* fix up issues with rebuilding and management dashboard.
* removes old files, removes NetworkHelper, fixes LastSyncedFileManager implementation to ensure the machine name is used, fix up logging with cold boot state.
* Makes MainDom safer to use and makes PublishedSnapshotService lazily register with MainDom
* lazily acquire application id (fix unit tests)
* Fixes resource casing and missing test file
* Ensures caches when requiring internal services for PublishedSnapshotService, UseNuCache is a separate call, shouldn't be buried in AddWebComponents, was also causing issues in integration tests since nucache was being used for the Id2Key service.
* For UmbracoTestServerTestBase enable nucache services
* Fixing tests
* Fix another test
* Fixes tests, use TestHostingEnvironment, make Tests.Common use net5, remove old Lucene.Net.Contrib ref.
* Fixes up some review notes
* Fixes issue with doubly registering PublishedSnapshotService meanig there could be 2x instances of it
* Checks for parseexception when executing the query
* Use application root instead of duplicating functionality.
* Added Examine project to netcore only solution file
* Fixed casing issue with LazyLoad, that is not lowercase.
* uses cancellationToken instead of bool flag, fixes always reading lastId from the LastSyncedFileManager, fixes RecurringHostedServiceBase so that there isn't an overlapping thread for the same task type
* Fix tests
* remove legacy test project from solution file
* Fix test
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-05-18 18:31:38 +10:00
|
|
|
.MinimumLevel.Debug()
|
2020-12-17 11:15:58 +00:00
|
|
|
.CreateLogger();
|
|
|
|
|
|
|
|
|
|
builder.AddSerilog(Log.Logger);
|
|
|
|
|
});
|
2020-11-19 20:05:28 +00:00
|
|
|
case UmbracoTestOptions.Logger.Console:
|
Examine 2.0 integration (#10241)
* Init commit for examine 2.0 work, most old umb examine tests working, probably a lot that doesn't
* Gets Umbraco Examine tests passing and makes some sense out of them, fixes some underlying issues.
* Large refactor, remove TaskHelper, rename Notifications to be consistent, Gets all examine/lucene indexes building and startup ordered in the correct way, removes old files, creates new IUmbracoIndexingHandler for abstracting out all index operations for umbraco data, abstracts out IIndexRebuilder, Fixes Stack overflow with LiveModelsProvider and loading assemblies, ports some changes from v8 for startup handling with cold boots, refactors out LastSyncedFileManager
* fix up issues with rebuilding and management dashboard.
* removes old files, removes NetworkHelper, fixes LastSyncedFileManager implementation to ensure the machine name is used, fix up logging with cold boot state.
* Makes MainDom safer to use and makes PublishedSnapshotService lazily register with MainDom
* lazily acquire application id (fix unit tests)
* Fixes resource casing and missing test file
* Ensures caches when requiring internal services for PublishedSnapshotService, UseNuCache is a separate call, shouldn't be buried in AddWebComponents, was also causing issues in integration tests since nucache was being used for the Id2Key service.
* For UmbracoTestServerTestBase enable nucache services
* Fixing tests
* Fix another test
* Fixes tests, use TestHostingEnvironment, make Tests.Common use net5, remove old Lucene.Net.Contrib ref.
* Fixes up some review notes
* Fixes issue with doubly registering PublishedSnapshotService meanig there could be 2x instances of it
* Checks for parseexception when executing the query
* Use application root instead of duplicating functionality.
* Added Examine project to netcore only solution file
* Fixed casing issue with LazyLoad, that is not lowercase.
* uses cancellationToken instead of bool flag, fixes always reading lastId from the LastSyncedFileManager, fixes RecurringHostedServiceBase so that there isn't an overlapping thread for the same task type
* Fix tests
* remove legacy test project from solution file
* Fix test
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-05-18 18:31:38 +10:00
|
|
|
return Microsoft.Extensions.Logging.LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Debug));
|
2020-11-19 20:05:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
2020-10-06 18:43:07 +02:00
|
|
|
{
|
2020-11-19 20:05:28 +00:00
|
|
|
// ignored
|
2020-10-06 18:43:07 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-19 20:05:28 +00:00
|
|
|
return NullLoggerFactory.Instance;
|
2020-10-06 18:43:07 +02:00
|
|
|
}
|
2020-11-19 20:05:28 +00:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Create the Generic Host and execute startup ConfigureServices/Configure calls
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual IHostBuilder CreateHostBuilder()
|
|
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
IHostBuilder hostBuilder = Host.CreateDefaultBuilder()
|
|
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
// IMPORTANT: We Cannot use UseStartup, there's all sorts of threads about this with testing. Although this can work
|
|
|
|
|
// if you want to setup your tests this way, it is a bit annoying to do that as the WebApplicationFactory will
|
|
|
|
|
// create separate Host instances. So instead of UseStartup, we just call ConfigureServices/Configure ourselves,
|
|
|
|
|
// and in the case of the UmbracoTestServerTestBase it will use the ConfigureWebHost to Configure the IApplicationBuilder directly.
|
2020-12-23 11:35:49 +01:00
|
|
|
// .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(GetType()); })
|
2020-09-02 18:10:29 +10:00
|
|
|
.ConfigureAppConfiguration((context, configBuilder) =>
|
|
|
|
|
{
|
|
|
|
|
context.HostingEnvironment = TestHelper.GetWebHostEnvironment();
|
2020-10-28 14:54:16 +01:00
|
|
|
configBuilder.Sources.Clear();
|
2020-09-02 18:10:29 +10:00
|
|
|
configBuilder.AddInMemoryCollection(InMemoryConfiguration);
|
2020-10-28 14:54:16 +01:00
|
|
|
|
|
|
|
|
Configuration = configBuilder.Build();
|
2020-09-02 18:10:29 +10:00
|
|
|
})
|
|
|
|
|
.ConfigureServices((hostContext, services) =>
|
|
|
|
|
{
|
|
|
|
|
ConfigureServices(services);
|
2020-12-16 01:54:49 +00:00
|
|
|
services.AddUnique(CreateLoggerFactory());
|
2020-12-15 15:20:36 +00:00
|
|
|
|
2020-12-16 01:54:49 +00:00
|
|
|
if (!TestOptions.Boot)
|
2020-12-15 15:20:36 +00:00
|
|
|
{
|
|
|
|
|
// If boot is false, we don't want the CoreRuntime hosted service to start
|
2020-12-16 01:54:49 +00:00
|
|
|
// So we replace it with a Mock
|
2020-12-15 15:20:36 +00:00
|
|
|
services.AddUnique(Mock.Of<IRuntime>());
|
|
|
|
|
}
|
2020-09-02 18:10:29 +10:00
|
|
|
});
|
|
|
|
|
return hostBuilder;
|
|
|
|
|
}
|
2020-12-03 13:32:04 +00:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
public virtual void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton(TestHelper.DbProviderFactoryCreator);
|
2020-12-11 18:20:07 +00:00
|
|
|
services.AddTransient<TestUmbracoDatabaseFactoryProvider>();
|
2020-12-24 08:55:21 +01:00
|
|
|
IWebHostEnvironment webHostEnvironment = TestHelper.GetWebHostEnvironment();
|
2020-09-02 18:10:29 +10:00
|
|
|
services.AddRequiredNetCoreServices(TestHelper, webHostEnvironment);
|
|
|
|
|
|
2021-12-22 13:03:38 +01:00
|
|
|
// We register this service because we need it for IRuntimeState, if we don't this breaks 900 tests
|
|
|
|
|
services.AddSingleton<IConflictingRouteService, TestConflictingRouteService>();
|
|
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
// Add it!
|
2021-07-09 15:31:01 -06:00
|
|
|
Core.Hosting.IHostingEnvironment hostingEnvironment = TestHelper.GetHostingEnvironment();
|
2021-02-09 10:22:42 +01:00
|
|
|
TypeLoader typeLoader = services.AddTypeLoader(
|
2020-11-24 09:22:38 +00:00
|
|
|
GetType().Assembly,
|
2021-07-09 15:31:01 -06:00
|
|
|
hostingEnvironment,
|
2020-11-24 09:22:38 +00:00
|
|
|
TestHelper.ConsoleLoggerFactory,
|
|
|
|
|
AppCaches.NoCache,
|
|
|
|
|
Configuration,
|
|
|
|
|
TestHelper.Profiler);
|
2021-07-09 15:31:01 -06:00
|
|
|
var builder = new UmbracoBuilder(services, Configuration, typeLoader, TestHelper.ConsoleLoggerFactory, TestHelper.Profiler, AppCaches.NoCache, hostingEnvironment);
|
2020-11-20 11:48:32 +00:00
|
|
|
|
2021-08-19 09:08:12 +02:00
|
|
|
builder.Services.AddLogger(hostingEnvironment, TestHelper.GetLoggingConfiguration(), Configuration);
|
2020-11-20 12:24:16 +00:00
|
|
|
|
2020-11-19 09:06:04 +00:00
|
|
|
builder.AddConfiguration()
|
2020-12-24 18:11:16 +11:00
|
|
|
.AddUmbracoCore()
|
|
|
|
|
.AddWebComponents()
|
2021-03-16 19:19:03 +11:00
|
|
|
.AddRuntimeMinifier()
|
2020-12-24 16:35:59 +11:00
|
|
|
.AddBackOfficeAuthentication()
|
2020-12-24 18:11:16 +11:00
|
|
|
.AddBackOfficeIdentity()
|
2021-02-12 17:06:29 +00:00
|
|
|
.AddMembersIdentity()
|
2021-03-12 21:48:24 +01:00
|
|
|
.AddExamine()
|
2020-12-24 18:11:16 +11:00
|
|
|
.AddTestServices(TestHelper, GetAppCaches());
|
2021-01-04 12:22:03 +11:00
|
|
|
|
|
|
|
|
if (TestOptions.Mapper)
|
|
|
|
|
{
|
|
|
|
|
// TODO: Should these just be called from within AddUmbracoCore/AddWebComponents?
|
|
|
|
|
builder
|
|
|
|
|
.AddCoreMappingProfiles()
|
|
|
|
|
.AddWebMappingProfiles();
|
|
|
|
|
}
|
2020-11-20 11:11:52 +00:00
|
|
|
|
2020-09-30 14:27:18 +02:00
|
|
|
services.AddSignalR();
|
2020-09-02 18:10:29 +10:00
|
|
|
services.AddMvc();
|
|
|
|
|
|
2020-12-23 13:57:41 +11:00
|
|
|
CustomTestSetup(builder);
|
2020-12-24 18:11:16 +11:00
|
|
|
|
|
|
|
|
builder.Build();
|
2020-09-02 18:10:29 +10:00
|
|
|
}
|
|
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
protected virtual AppCaches GetAppCaches() =>
|
|
|
|
|
|
2020-10-29 14:08:01 +01:00
|
|
|
// Disable caches for integration tests
|
2020-12-23 11:35:49 +01:00
|
|
|
AppCaches.NoCache;
|
2020-10-29 14:08:01 +01:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
public virtual void Configure(IApplicationBuilder app)
|
|
|
|
|
{
|
2020-12-16 01:54:49 +00:00
|
|
|
if (TestOptions.Boot)
|
|
|
|
|
{
|
|
|
|
|
Services.GetRequiredService<IUmbracoContextFactory>().EnsureUmbracoContext();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.UseUmbracoCore(); // This no longer starts CoreRuntime, it's very fast
|
2020-11-10 08:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
private static readonly object s_dbLocker = new object();
|
|
|
|
|
private static ITestDatabase s_dbInstance;
|
|
|
|
|
private static TestDbMeta s_fixtureDbMeta;
|
2020-09-02 18:10:29 +10:00
|
|
|
|
2020-12-16 01:54:49 +00:00
|
|
|
protected void UseTestDatabase(IServiceProvider serviceProvider)
|
2020-09-02 18:10:29 +10:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
IRuntimeState state = serviceProvider.GetRequiredService<IRuntimeState>();
|
|
|
|
|
TestUmbracoDatabaseFactoryProvider testDatabaseFactoryProvider = serviceProvider.GetRequiredService<TestUmbracoDatabaseFactoryProvider>();
|
|
|
|
|
IUmbracoDatabaseFactory databaseFactory = serviceProvider.GetRequiredService<IUmbracoDatabaseFactory>();
|
|
|
|
|
ILoggerFactory loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
|
2020-11-19 20:05:28 +00:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
// This will create a db, install the schema and ensure the app is configured to run
|
2020-12-16 01:54:49 +00:00
|
|
|
SetupTestDatabase(testDatabaseFactoryProvider, databaseFactory, loggerFactory, state, TestHelper.WorkingDirectory);
|
2020-09-02 18:10:29 +10:00
|
|
|
}
|
|
|
|
|
|
2020-03-30 20:55:13 +11:00
|
|
|
/// <summary>
|
2020-12-16 01:54:49 +00:00
|
|
|
/// Get or create an instance of <see cref="ITestDatabase"/>
|
2020-03-30 20:55:13 +11:00
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// There must only be ONE instance shared between all tests in a session
|
|
|
|
|
/// </remarks>
|
2020-12-11 18:20:07 +00:00
|
|
|
private static ITestDatabase GetOrCreateDatabase(string filesPath, ILoggerFactory loggerFactory, TestUmbracoDatabaseFactoryProvider dbFactory)
|
2020-03-30 20:55:13 +11:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
lock (s_dbLocker)
|
2020-03-30 20:55:13 +11:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
if (s_dbInstance != null)
|
2020-12-16 01:54:49 +00:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
return s_dbInstance;
|
2020-12-16 01:54:49 +00:00
|
|
|
}
|
2020-03-30 20:55:13 +11:00
|
|
|
|
2020-12-19 11:49:37 +00:00
|
|
|
// TODO: pull from IConfiguration
|
|
|
|
|
var settings = new TestDatabaseSettings
|
|
|
|
|
{
|
|
|
|
|
PrepareThreadCount = 4,
|
|
|
|
|
EmptyDatabasesCount = 2,
|
|
|
|
|
SchemaDatabaseCount = 4
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
s_dbInstance = TestDatabaseFactory.Create(settings, filesPath, loggerFactory, dbFactory);
|
2020-12-16 01:54:49 +00:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
return s_dbInstance;
|
2020-03-30 20:55:13 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a LocalDb instance to use for the test
|
|
|
|
|
/// </summary>
|
2020-12-16 01:54:49 +00:00
|
|
|
private void SetupTestDatabase(
|
2020-12-11 18:20:07 +00:00
|
|
|
TestUmbracoDatabaseFactoryProvider testUmbracoDatabaseFactoryProvider,
|
|
|
|
|
IUmbracoDatabaseFactory databaseFactory,
|
|
|
|
|
ILoggerFactory loggerFactory,
|
|
|
|
|
IRuntimeState runtimeState,
|
|
|
|
|
string workingDirectory)
|
2020-09-02 18:10:29 +10:00
|
|
|
{
|
2020-12-16 01:54:49 +00:00
|
|
|
if (TestOptions.Database == UmbracoTestOptions.Database.None)
|
|
|
|
|
{
|
2020-09-02 18:10:29 +10:00
|
|
|
return;
|
2020-12-16 01:54:49 +00:00
|
|
|
}
|
2020-03-30 20:55:13 +11:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
// need to manually register this factory
|
|
|
|
|
DbProviderFactories.RegisterFactory(Constants.DbProviderNames.SqlServer, SqlClientFactory.Instance);
|
2020-03-30 20:55:13 +11:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
string dbFilePath = Path.Combine(workingDirectory, "LocalDb");
|
2020-03-30 20:55:13 +11:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
ITestDatabase db = GetOrCreateDatabase(dbFilePath, loggerFactory, testUmbracoDatabaseFactoryProvider);
|
2020-03-30 20:55:13 +11:00
|
|
|
|
2020-12-16 01:54:49 +00:00
|
|
|
switch (TestOptions.Database)
|
2020-09-02 18:10:29 +10:00
|
|
|
{
|
|
|
|
|
case UmbracoTestOptions.Database.NewSchemaPerTest:
|
2020-03-30 20:55:13 +11:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
// New DB + Schema
|
2020-12-17 11:15:58 +00:00
|
|
|
TestDbMeta newSchemaDbMeta = db.AttachSchema();
|
2020-03-30 20:55:13 +11:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
// Add teardown callback
|
2020-12-12 11:33:57 +00:00
|
|
|
OnTestTearDown(() => db.Detach(newSchemaDbMeta));
|
2020-06-09 07:49:26 +02:00
|
|
|
|
2020-12-17 11:15:58 +00:00
|
|
|
ConfigureTestDatabaseFactory(newSchemaDbMeta, databaseFactory, runtimeState);
|
2020-06-30 20:11:39 +02:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
Assert.AreEqual(RuntimeLevel.Run, runtimeState.Level);
|
2020-06-30 20:11:39 +02:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
break;
|
|
|
|
|
case UmbracoTestOptions.Database.NewEmptyPerTest:
|
2020-12-17 11:15:58 +00:00
|
|
|
TestDbMeta newEmptyDbMeta = db.AttachEmpty();
|
2020-03-30 20:55:13 +11:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
// Add teardown callback
|
2020-12-12 11:33:57 +00:00
|
|
|
OnTestTearDown(() => db.Detach(newEmptyDbMeta));
|
2020-03-30 20:55:13 +11:00
|
|
|
|
2020-12-17 11:15:58 +00:00
|
|
|
ConfigureTestDatabaseFactory(newEmptyDbMeta, databaseFactory, runtimeState);
|
2020-10-28 14:54:16 +01:00
|
|
|
|
|
|
|
|
Assert.AreEqual(RuntimeLevel.Install, runtimeState.Level);
|
2020-06-09 07:49:26 +02:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
break;
|
|
|
|
|
case UmbracoTestOptions.Database.NewSchemaPerFixture:
|
2020-10-06 14:16:29 +02:00
|
|
|
// Only attach schema once per fixture
|
|
|
|
|
// Doing it more than once will block the process since the old db hasn't been detached
|
|
|
|
|
// and it would be the same as NewSchemaPerTest even if it didn't block
|
2021-03-04 16:18:57 +11:00
|
|
|
if (_firstTestInFixture)
|
2020-10-02 14:16:36 +02:00
|
|
|
{
|
2020-10-02 14:35:24 +02:00
|
|
|
// New DB + Schema
|
2020-12-17 11:15:58 +00:00
|
|
|
TestDbMeta newSchemaFixtureDbMeta = db.AttachSchema();
|
2020-12-23 11:35:49 +01:00
|
|
|
s_fixtureDbMeta = newSchemaFixtureDbMeta;
|
2020-09-02 18:10:29 +10:00
|
|
|
|
2020-10-02 14:35:24 +02:00
|
|
|
// Add teardown callback
|
2020-12-12 11:33:57 +00:00
|
|
|
OnFixtureTearDown(() => db.Detach(newSchemaFixtureDbMeta));
|
2020-10-02 14:16:36 +02:00
|
|
|
}
|
2020-09-02 18:10:29 +10:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
ConfigureTestDatabaseFactory(s_fixtureDbMeta, databaseFactory, runtimeState);
|
2020-09-02 18:10:29 +10:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case UmbracoTestOptions.Database.NewEmptyPerFixture:
|
2020-10-28 14:54:16 +01:00
|
|
|
// Only attach schema once per fixture
|
|
|
|
|
// Doing it more than once will block the process since the old db hasn't been detached
|
|
|
|
|
// and it would be the same as NewSchemaPerTest even if it didn't block
|
2021-03-04 16:18:57 +11:00
|
|
|
if (_firstTestInFixture)
|
2020-10-28 14:54:16 +01:00
|
|
|
{
|
|
|
|
|
// New DB + Schema
|
2020-12-17 11:15:58 +00:00
|
|
|
TestDbMeta newEmptyFixtureDbMeta = db.AttachEmpty();
|
2020-12-23 11:35:49 +01:00
|
|
|
s_fixtureDbMeta = newEmptyFixtureDbMeta;
|
2020-09-02 18:10:29 +10:00
|
|
|
|
2020-10-28 14:54:16 +01:00
|
|
|
// Add teardown callback
|
2020-12-12 11:33:57 +00:00
|
|
|
OnFixtureTearDown(() => db.Detach(newEmptyFixtureDbMeta));
|
2020-10-28 14:54:16 +01:00
|
|
|
}
|
2020-09-02 18:10:29 +10:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
ConfigureTestDatabaseFactory(s_fixtureDbMeta, databaseFactory, runtimeState);
|
2020-09-02 18:10:29 +10:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2020-12-16 01:54:49 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(TestOptions), TestOptions, null);
|
2020-09-02 18:10:29 +10:00
|
|
|
}
|
2020-03-30 20:55:13 +11:00
|
|
|
}
|
|
|
|
|
|
2020-12-17 11:15:58 +00:00
|
|
|
private void ConfigureTestDatabaseFactory(TestDbMeta meta, IUmbracoDatabaseFactory factory, IRuntimeState state)
|
|
|
|
|
{
|
|
|
|
|
ILogger<UmbracoIntegrationTest> log = Services.GetRequiredService<ILogger<UmbracoIntegrationTest>>();
|
|
|
|
|
log.LogInformation($"ConfigureTestDatabaseFactory - Using test database: [{meta.Name}] - IsEmpty: [{meta.IsEmpty}]");
|
|
|
|
|
|
|
|
|
|
// It's just been pulled from container and wasn't used to create test database
|
|
|
|
|
Assert.IsFalse(factory.Configured);
|
|
|
|
|
|
|
|
|
|
factory.Configure(meta.ConnectionString, Constants.DatabaseProviders.SqlServer);
|
|
|
|
|
state.DetermineRuntimeLevel();
|
|
|
|
|
log.LogInformation($"ConfigureTestDatabaseFactory - Determined RuntimeLevel: [{state.Level}]");
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 01:54:49 +00:00
|
|
|
protected UmbracoTestAttribute TestOptions => TestOptionAttributeBase.GetTestOptions<UmbracoTestAttribute>();
|
|
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
protected virtual T GetRequiredService<T>() => Services.GetRequiredService<T>();
|
|
|
|
|
|
|
|
|
|
public Dictionary<string, string> InMemoryConfiguration { get; } = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
public IConfiguration Configuration { get; protected set; }
|
|
|
|
|
|
2020-12-23 12:02:01 +11:00
|
|
|
public TestHelper TestHelper { get; } = new TestHelper();
|
2020-09-02 18:10:29 +10:00
|
|
|
|
2020-12-23 13:57:41 +11:00
|
|
|
protected virtual void CustomTestSetup(IUmbracoBuilder builder) { }
|
2020-05-17 10:39:30 +01:00
|
|
|
|
2020-03-30 20:55:13 +11:00
|
|
|
/// <summary>
|
2020-12-23 11:35:49 +01:00
|
|
|
/// Gets or sets the DI container.
|
2020-03-30 20:55:13 +11:00
|
|
|
/// </summary>
|
2020-09-02 18:10:29 +10:00
|
|
|
protected IServiceProvider Services { get; set; }
|
2020-03-30 20:55:13 +11:00
|
|
|
|
|
|
|
|
/// <summary>
|
2020-12-23 11:35:49 +01:00
|
|
|
/// Gets the <see cref="IScopeProvider"/>
|
2020-03-30 20:55:13 +11:00
|
|
|
/// </summary>
|
|
|
|
|
protected IScopeProvider ScopeProvider => Services.GetRequiredService<IScopeProvider>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-12-23 11:35:49 +01:00
|
|
|
/// Gets the <see cref="IScopeAccessor"/>
|
2020-03-30 20:55:13 +11:00
|
|
|
/// </summary>
|
|
|
|
|
protected IScopeAccessor ScopeAccessor => Services.GetRequiredService<IScopeAccessor>();
|
|
|
|
|
|
2020-09-17 10:35:11 +02:00
|
|
|
/// <summary>
|
2020-12-23 11:35:49 +01:00
|
|
|
/// Gets the <see cref="ILoggerFactory"/>
|
2020-09-17 10:35:11 +02:00
|
|
|
/// </summary>
|
2020-10-06 18:43:07 +02:00
|
|
|
protected ILoggerFactory LoggerFactory => Services.GetRequiredService<ILoggerFactory>();
|
2020-12-21 16:44:50 +11:00
|
|
|
|
2020-03-30 21:53:30 +11:00
|
|
|
protected AppCaches AppCaches => Services.GetRequiredService<AppCaches>();
|
2020-12-21 16:44:50 +11:00
|
|
|
|
2020-03-30 21:53:30 +11:00
|
|
|
protected IIOHelper IOHelper => Services.GetRequiredService<IIOHelper>();
|
2020-12-21 16:44:50 +11:00
|
|
|
|
2020-03-30 21:53:30 +11:00
|
|
|
protected IShortStringHelper ShortStringHelper => Services.GetRequiredService<IShortStringHelper>();
|
2020-12-21 16:44:50 +11:00
|
|
|
|
2020-08-24 12:34:37 +02:00
|
|
|
protected GlobalSettings GlobalSettings => Services.GetRequiredService<IOptions<GlobalSettings>>().Value;
|
2020-03-30 21:53:30 +11:00
|
|
|
|
2020-12-21 16:44:50 +11:00
|
|
|
protected IMapperCollection Mappers => Services.GetRequiredService<IMapperCollection>();
|
2020-03-30 21:53:30 +11:00
|
|
|
|
2021-03-04 16:18:57 +11:00
|
|
|
protected UserBuilder UserBuilderInstance { get; } = new UserBuilder();
|
|
|
|
|
protected UserGroupBuilder UserGroupBuilderInstance { get; } = new UserGroupBuilder();
|
2020-03-30 21:53:30 +11:00
|
|
|
|
2021-03-04 16:18:57 +11:00
|
|
|
private static bool s_firstTestInSession = true;
|
|
|
|
|
private bool _firstTestInFixture = true;
|
|
|
|
|
private static int s_testCount = 1;
|
2020-03-30 20:55:13 +11:00
|
|
|
}
|
|
|
|
|
}
|