2020-12-03 13:32:04 +00:00
|
|
|
using System;
|
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.Options;
|
|
|
|
|
using Moq;
|
2020-03-30 20:55:13 +11:00
|
|
|
using NUnit.Framework;
|
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.Mappers;
|
2022-01-13 17:44:11 +00:00
|
|
|
using Umbraco.Cms.Infrastructure.Scoping;
|
2021-02-10 14:45:44 +01:00
|
|
|
using Umbraco.Cms.Tests.Common.Builders;
|
2021-02-11 08:30:27 +01:00
|
|
|
using Umbraco.Cms.Tests.Integration.DependencyInjection;
|
|
|
|
|
using Umbraco.Cms.Tests.Integration.Extensions;
|
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>
|
2022-02-11 20:37:56 +00:00
|
|
|
public abstract class UmbracoIntegrationTest : UmbracoIntegrationTestBase
|
2020-03-30 20:55:13 +11:00
|
|
|
{
|
2022-02-11 20:37:56 +00:00
|
|
|
private IHost _host;
|
2020-09-02 18:10:29 +10:00
|
|
|
|
2022-02-11 20:37:56 +00:00
|
|
|
protected IServiceProvider Services => _host.Services;
|
2020-09-02 18:10:29 +10:00
|
|
|
|
2020-10-27 14:10:19 +01:00
|
|
|
[SetUp]
|
2022-02-11 20:37:56 +00:00
|
|
|
public 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
|
|
|
|
2022-02-11 20:37:56 +00:00
|
|
|
_host = hostBuilder.Build();
|
|
|
|
|
UseTestDatabase(_host.Services);
|
|
|
|
|
_host.Start();
|
2020-12-17 11:15:58 +00:00
|
|
|
|
2022-02-11 20:37:56 +00:00
|
|
|
if (TestOptions.Boot)
|
2020-10-06 18:43:07 +02:00
|
|
|
{
|
2022-02-11 20:37:56 +00:00
|
|
|
Services.GetRequiredService<IUmbracoContextFactory>().EnsureUmbracoContext();
|
2020-10-06 18:43:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-11-19 20:05:28 +00:00
|
|
|
|
2022-02-11 20:37:56 +00:00
|
|
|
[TearDown]
|
|
|
|
|
public void TearDownAsync() => _host.StopAsync();
|
|
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Create the Generic Host and execute startup ConfigureServices/Configure calls
|
|
|
|
|
/// </summary>
|
2022-02-11 20:37:56 +00:00
|
|
|
private IHostBuilder CreateHostBuilder()
|
2020-09-02 18:10:29 +10:00
|
|
|
{
|
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.
|
|
|
|
|
.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
|
|
|
})
|
2022-02-11 14:24:45 +00:00
|
|
|
.ConfigureServices((_, services) =>
|
2020-09-02 18:10:29 +10:00
|
|
|
{
|
|
|
|
|
ConfigureServices(services);
|
2022-02-12 11:57:28 +00:00
|
|
|
ConfigureTestServices(services);
|
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
|
|
|
});
|
2022-02-11 14:24:45 +00:00
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
return hostBuilder;
|
|
|
|
|
}
|
2020-12-03 13:32:04 +00:00
|
|
|
|
2022-02-12 11:57:28 +00:00
|
|
|
protected void ConfigureServices(IServiceCollection services)
|
2020-09-02 18:10:29 +10:00
|
|
|
{
|
2022-02-11 20:37:56 +00:00
|
|
|
services.AddUnique(CreateLoggerFactory());
|
2020-09-02 18:10:29 +10:00
|
|
|
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()
|
2022-02-11 20:37:56 +00:00
|
|
|
.AddTestServices(TestHelper);
|
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
|
|
|
}
|
|
|
|
|
|
2022-02-12 11:57:28 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Hook for altering UmbracoBuilder setup
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Can also be used for registering test doubles.
|
|
|
|
|
/// </remarks>
|
2022-02-11 20:37:56 +00:00
|
|
|
protected virtual void CustomTestSetup(IUmbracoBuilder builder)
|
2020-03-30 20:55:13 +11:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 11:57:28 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Hook for registering test doubles.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void ConfigureTestServices(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 18:10:29 +10:00
|
|
|
protected virtual T GetRequiredService<T>() => Services.GetRequiredService<T>();
|
|
|
|
|
|
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
|
|
|
|
2022-02-11 20:37:56 +00:00
|
|
|
protected UserBuilder UserBuilderInstance { get; } = new ();
|
2020-03-30 21:53:30 +11:00
|
|
|
|
2022-02-11 20:37:56 +00:00
|
|
|
protected UserGroupBuilder UserGroupBuilderInstance { get; } = new ();
|
2020-03-30 20:55:13 +11:00
|
|
|
}
|
|
|
|
|
}
|