2020-03-30 20:55:13 +11:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
using NUnit.Framework;
|
2020-03-30 21:53:30 +11:00
|
|
|
|
using Umbraco.Core.Cache;
|
2020-03-30 20:55:13 +11:00
|
|
|
|
using Umbraco.Core.Composing;
|
|
|
|
|
|
using Umbraco.Core.Composing.LightInject;
|
|
|
|
|
|
using Umbraco.Core.Configuration;
|
2020-03-30 21:53:30 +11:00
|
|
|
|
using Umbraco.Core.IO;
|
2020-03-30 20:55:13 +11:00
|
|
|
|
using Umbraco.Core.Logging;
|
|
|
|
|
|
using Umbraco.Core.Persistence;
|
2020-03-30 21:53:30 +11:00
|
|
|
|
using Umbraco.Core.Persistence.Mappers;
|
2020-03-30 20:55:13 +11:00
|
|
|
|
using Umbraco.Core.Scoping;
|
2020-03-30 21:53:30 +11:00
|
|
|
|
using Umbraco.Core.Strings;
|
|
|
|
|
|
using Umbraco.Tests.Common.Builders;
|
2020-03-30 20:55:13 +11:00
|
|
|
|
using Umbraco.Tests.Integration.Extensions;
|
|
|
|
|
|
using Umbraco.Tests.Integration.Implementations;
|
|
|
|
|
|
using Umbraco.Web.BackOffice.AspNetCore;
|
2020-03-31 12:22:11 +02:00
|
|
|
|
using Umbraco.Web.Common.Extensions;
|
2020-03-30 20:55:13 +11:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.Integration.Testing
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <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
|
|
|
|
|
|
{
|
|
|
|
|
|
public static LightInjectContainer GetUmbracoContainer(out UmbracoServiceProviderFactory serviceProviderFactory)
|
|
|
|
|
|
{
|
|
|
|
|
|
var container = UmbracoServiceProviderFactory.CreateServiceContainer();
|
|
|
|
|
|
serviceProviderFactory = new UmbracoServiceProviderFactory(container);
|
|
|
|
|
|
var umbracoContainer = serviceProviderFactory.GetContainer();
|
|
|
|
|
|
return umbracoContainer;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get or create an instance of <see cref="LocalDbTestDatabase"/>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filesPath"></param>
|
|
|
|
|
|
/// <param name="logger"></param>
|
|
|
|
|
|
/// <param name="globalSettings"></param>
|
|
|
|
|
|
/// <param name="dbFactory"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// There must only be ONE instance shared between all tests in a session
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public static LocalDbTestDatabase GetOrCreate(string filesPath, ILogger logger, IGlobalSettings globalSettings, IUmbracoDatabaseFactory dbFactory)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock (_dbLocker)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_dbInstance != null) return _dbInstance;
|
|
|
|
|
|
|
|
|
|
|
|
var localDb = new LocalDb();
|
|
|
|
|
|
if (localDb.IsAvailable == false)
|
|
|
|
|
|
throw new InvalidOperationException("LocalDB is not available.");
|
|
|
|
|
|
_dbInstance = new LocalDbTestDatabase(logger, globalSettings, localDb, filesPath, dbFactory);
|
|
|
|
|
|
return _dbInstance;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static readonly object _dbLocker = new object();
|
|
|
|
|
|
private static LocalDbTestDatabase _dbInstance;
|
|
|
|
|
|
|
2020-04-03 13:54:28 +11:00
|
|
|
|
private Action _testTeardown = null;
|
|
|
|
|
|
private Action _fixtureTeardown = null;
|
2020-03-30 20:55:13 +11:00
|
|
|
|
|
|
|
|
|
|
public void OnTestTearDown(Action tearDown)
|
|
|
|
|
|
{
|
2020-04-03 13:54:28 +11:00
|
|
|
|
_testTeardown = tearDown;
|
2020-03-30 20:55:13 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void OnFixtureTearDown(Action tearDown)
|
|
|
|
|
|
{
|
2020-04-03 13:54:28 +11:00
|
|
|
|
_fixtureTeardown = tearDown;
|
2020-03-30 20:55:13 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[OneTimeTearDown]
|
|
|
|
|
|
public void FixtureTearDown()
|
|
|
|
|
|
{
|
2020-04-03 13:54:28 +11:00
|
|
|
|
_fixtureTeardown?.Invoke();
|
2020-03-30 20:55:13 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
|
|
public void TearDown()
|
|
|
|
|
|
{
|
2020-04-03 13:54:28 +11:00
|
|
|
|
_testTeardown?.Invoke();
|
2020-03-30 20:55:13 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
|
public async Task Setup()
|
|
|
|
|
|
{
|
|
|
|
|
|
var umbracoContainer = GetUmbracoContainer(out var serviceProviderFactory);
|
|
|
|
|
|
var testHelper = new TestHelper();
|
|
|
|
|
|
|
|
|
|
|
|
var hostBuilder = new HostBuilder()
|
|
|
|
|
|
.UseUmbraco(serviceProviderFactory)
|
|
|
|
|
|
.ConfigureServices((hostContext, services) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var webHostEnvironment = testHelper.GetWebHostEnvironment();
|
|
|
|
|
|
services.AddRequiredNetCoreServices(testHelper, webHostEnvironment);
|
|
|
|
|
|
|
|
|
|
|
|
// Add it!
|
|
|
|
|
|
services.AddUmbracoConfiguration(hostContext.Configuration);
|
2020-04-22 14:23:56 +10:00
|
|
|
|
services.AddUmbracoCore(webHostEnvironment, umbracoContainer, GetType().Assembly, testHelper.GetLoggingConfiguration(), out _);
|
2020-03-30 20:55:13 +11:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var host = await hostBuilder.StartAsync();
|
|
|
|
|
|
var app = new ApplicationBuilder(host.Services);
|
|
|
|
|
|
Services = app.ApplicationServices;
|
|
|
|
|
|
|
|
|
|
|
|
// This will create a db, install the schema and ensure the app is configured to run
|
2020-04-03 13:16:01 +11:00
|
|
|
|
app.UseTestLocalDb(testHelper.WorkingDirectory, this);
|
2020-03-30 20:55:13 +11:00
|
|
|
|
|
|
|
|
|
|
app.UseUmbracoCore();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-30 21:53:30 +11:00
|
|
|
|
#region Common services
|
|
|
|
|
|
|
2020-03-30 20:55:13 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns the DI container
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected IServiceProvider Services { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns the <see cref="IScopeProvider"/>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected IScopeProvider ScopeProvider => Services.GetRequiredService<IScopeProvider>();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns the <see cref="IScopeAccessor"/>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected IScopeAccessor ScopeAccessor => Services.GetRequiredService<IScopeAccessor>();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns the <see cref="ILogger"/>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected ILogger Logger => Services.GetRequiredService<ILogger>();
|
2020-03-30 21:53:30 +11:00
|
|
|
|
|
|
|
|
|
|
protected AppCaches AppCaches => Services.GetRequiredService<AppCaches>();
|
|
|
|
|
|
protected IIOHelper IOHelper => Services.GetRequiredService<IIOHelper>();
|
|
|
|
|
|
protected IShortStringHelper ShortStringHelper => Services.GetRequiredService<IShortStringHelper>();
|
|
|
|
|
|
protected IGlobalSettings GlobalSettings => Services.GetRequiredService<IGlobalSettings>();
|
|
|
|
|
|
protected IMapperCollection Mappers => Services.GetRequiredService<IMapperCollection>();
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Builders
|
|
|
|
|
|
|
|
|
|
|
|
protected GlobalSettingsBuilder GlobalSettingsBuilder = new GlobalSettingsBuilder();
|
|
|
|
|
|
protected UserBuilder UserBuilder = new UserBuilder();
|
|
|
|
|
|
protected UserGroupBuilder UserGroupBuilder = new UserGroupBuilder();
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
2020-03-30 20:55:13 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|