Files
Umbraco-CMS/src/Umbraco.Tests/Testing/UmbracoTestBase.cs

435 lines
17 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Linq;
2016-11-05 19:23:55 +01:00
using System.Reflection;
using AutoMapper;
using Examine;
2016-11-05 19:23:55 +01:00
using Moq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Components;
2017-05-30 15:46:25 +02:00
using Umbraco.Core.Composing;
2018-07-20 09:49:05 +02:00
using Umbraco.Core.Composing.Composers;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Events;
using Umbraco.Core.IO;
2018-06-29 14:25:48 +02:00
using Umbraco.Core.IO.MediaPathSchemes;
2016-11-05 19:23:55 +01:00
using Umbraco.Core.Logging;
2018-08-30 19:08:55 +02:00
using Umbraco.Core.Logging.Serilog;
using Umbraco.Core.Manifest;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Mappers;
2018-04-28 09:55:36 +02:00
using Umbraco.Core.Persistence.Repositories.Implement;
using Umbraco.Core.Persistence.SqlSyntax;
using Umbraco.Core.PropertyEditors;
2017-06-23 18:54:42 +02:00
using Umbraco.Core.Scoping;
using Umbraco.Core.Services;
2018-04-28 09:55:36 +02:00
using Umbraco.Core.Services.Implement;
using Umbraco.Core.Strings;
using Umbraco.Tests.Components;
2016-12-16 10:40:14 +01:00
using Umbraco.Tests.TestHelpers;
using Umbraco.Tests.TestHelpers.Stubs;
2016-11-07 20:50:14 +01:00
using Umbraco.Web;
using Umbraco.Web.Services;
using Umbraco.Tests.Testing.Objects.Accessors;
using Umbraco.Web.Actions;
2018-07-20 09:49:05 +02:00
using Umbraco.Web.Composing.Composers;
2018-09-20 18:51:46 +02:00
using Umbraco.Web.ContentApps;
2017-05-30 15:46:25 +02:00
using Current = Umbraco.Core.Composing.Current;
using Umbraco.Web.Routing;
2016-11-05 19:23:55 +01:00
2016-12-16 10:40:14 +01:00
namespace Umbraco.Tests.Testing
2016-11-05 19:23:55 +01:00
{
/// <summary>
/// Provides the top-level base class for all Umbraco integration tests.
/// </summary>
/// <remarks>
/// True unit tests do not need to inherit from this class, but most of Umbraco tests
/// are not true unit tests but integration tests requiring services, databases, etc. This class
/// provides all the necessary environment, through DI. Yes, DI is bad in tests - unit tests.
/// But it is OK in integration tests.
/// </remarks>
public abstract class UmbracoTestBase
{
// this class
// ensures that Current is properly resetted
// ensures that a service container is properly initialized and disposed
// compose the required dependencies according to test options (UmbracoTestAttribute)
//
// everything is virtual (because, why not?)
// starting a test runs like this:
// - SetUp() // when overriding, call base.SetUp() *first* then setup your own stuff
// --- Compose() // when overriding, call base.Commpose() *first* then compose your own stuff
// --- Initialize() // same
2016-11-05 19:23:55 +01:00
// - test runs
// - TearDown() // when overriding, clear you own stuff *then* call base.TearDown()
//
// about attributes
//
// this class defines the SetUp and TearDown methods, with proper attributes, and
// these attributes are *inherited* so classes inheriting from this class should *not*
// add the attributes to SetUp nor TearDown again
//
// this class is *not* marked with the TestFeature attribute because it is *not* a
// test feature, and no test "base" class should be. only actual test feature classes
// should be marked with that attribute.
protected Composition Composition { get; private set; }
2018-11-28 12:59:40 +01:00
protected IFactory Factory { get; private set; }
2016-11-05 19:23:55 +01:00
protected UmbracoTestAttribute Options { get; private set; }
2016-12-16 10:40:14 +01:00
protected static bool FirstTestInSession = true;
protected bool FirstTestInFixture = true;
2016-12-14 18:40:16 +01:00
internal TestObjects TestObjects { get; private set; }
2017-05-30 15:33:13 +02:00
private static TypeLoader _commonTypeLoader;
private TypeLoader _featureTypeLoader;
2016-11-05 19:23:55 +01:00
#region Accessors
2018-11-28 12:59:40 +01:00
protected ILogger Logger => Factory.GetInstance<ILogger>();
2018-11-28 12:59:40 +01:00
protected IProfiler Profiler => Factory.GetInstance<IProfiler>();
2018-11-28 12:59:40 +01:00
protected virtual IProfilingLogger ProfilingLogger => Factory.GetInstance<IProfilingLogger>();
2019-01-17 08:34:29 +01:00
protected AppCaches AppCaches => Factory.GetInstance<AppCaches>();
2018-11-28 12:59:40 +01:00
protected virtual ISqlSyntaxProvider SqlSyntax => Factory.GetInstance<ISqlSyntaxProvider>();
2018-11-28 12:59:40 +01:00
protected IMapperCollection Mappers => Factory.GetInstance<IMapperCollection>();
#endregion
#region Setup
2016-11-05 19:23:55 +01:00
[SetUp]
public virtual void SetUp()
{
// should not need this if all other tests were clean
// but hey, never know, better avoid garbage-in
Reset();
// get/merge the attributes marking the method and/or the classes
2017-09-19 18:47:07 +02:00
Options = TestOptionAttributeBase.GetTestOptions<UmbracoTestAttribute>();
2016-11-05 19:23:55 +01:00
2018-11-28 16:57:01 +01:00
// fixme - align to runtimes & components - don't redo everything here
2018-11-28 11:05:41 +01:00
var (logger, profiler) = GetLoggers(Options.Logger);
var proflogger = new ProfilingLogger(logger, profiler);
var cacheHelper = GetCacheHelper();
var globalSettings = SettingsForTests.GetDefaultGlobalSettings();
var typeLoader = GetTypeLoader(cacheHelper.RuntimeCache, globalSettings, proflogger, Options.TypeLoader);
2018-11-28 12:59:40 +01:00
var register = RegisterFactory.Create();
2018-11-28 11:05:41 +01:00
Composition = new Composition(register, typeLoader, proflogger, ComponentTests.MockRuntimeState(RuntimeLevel.Run));
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique(typeLoader);
Composition.RegisterUnique(logger);
Composition.RegisterUnique(profiler);
Composition.RegisterUnique<IProfilingLogger>(proflogger);
Composition.RegisterUnique(cacheHelper);
Composition.RegisterUnique(cacheHelper.RuntimeCache);
2018-11-28 12:59:40 +01:00
TestObjects = new TestObjects(register);
2016-11-05 19:23:55 +01:00
Compose();
2018-11-29 10:35:16 +01:00
Current.Factory = Factory = Composition.CreateFactory();
2016-11-05 19:23:55 +01:00
Initialize();
}
protected virtual void Compose()
{
ComposeAutoMapper(Options.AutoMapper);
ComposeDatabase(Options.Database);
ComposeApplication(Options.WithApplication);
2017-09-20 20:06:46 +02:00
2016-11-05 19:23:55 +01:00
// etc
ComposeWeb();
2016-12-16 10:40:14 +01:00
ComposeWtf();
2016-11-05 19:23:55 +01:00
// not sure really
2018-11-28 11:05:41 +01:00
Compose(Composition);
2016-11-05 19:23:55 +01:00
}
protected virtual void Compose(Composition composition)
{ }
protected virtual void Initialize()
{
InitializeAutoMapper(Options.AutoMapper);
InitializeApplication(Options.WithApplication);
2016-11-05 19:23:55 +01:00
}
#endregion
#region Compose
2016-11-05 19:23:55 +01:00
2018-11-28 11:05:41 +01:00
protected virtual (ILogger, IProfiler) GetLoggers(UmbracoTestOptions.Logger option)
2016-11-05 19:23:55 +01:00
{
2018-11-28 11:05:41 +01:00
ILogger logger;
IProfiler profiler;
switch (option)
2018-10-02 15:19:01 +02:00
{
2018-11-28 11:05:41 +01:00
case UmbracoTestOptions.Logger.Mock:
logger = Mock.Of<ILogger>();
profiler = Mock.Of<IProfiler>();
break;
case UmbracoTestOptions.Logger.Serilog:
logger = new SerilogLogger(new FileInfo(TestHelper.MapPathForTest("~/unit-test.config")));
profiler = new LogProfiler(logger);
break;
case UmbracoTestOptions.Logger.Console:
logger = new ConsoleLogger();
profiler = new LogProfiler(logger);
break;
default:
throw new NotSupportedException($"Logger option {option} is not supported.");
2018-10-02 15:19:01 +02:00
}
2016-11-05 19:23:55 +01:00
2018-11-28 11:05:41 +01:00
return (logger, profiler);
}
2019-01-17 08:34:29 +01:00
protected virtual AppCaches GetCacheHelper()
2018-11-28 11:05:41 +01:00
{
2019-01-17 08:34:29 +01:00
return AppCaches.Disabled;
2016-11-05 19:23:55 +01:00
}
protected virtual void ComposeWeb()
2016-12-16 10:40:14 +01:00
{
// imported from TestWithSettingsBase
// which was inherited by TestWithApplicationBase so pretty much used everywhere
2017-05-30 18:13:11 +02:00
Umbraco.Web.Composing.Current.UmbracoContextAccessor = new TestUmbracoContextAccessor();
2017-09-20 20:06:46 +02:00
2018-07-18 10:32:50 +02:00
// web
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique(_ => Umbraco.Web.Composing.Current.UmbracoContextAccessor);
Composition.RegisterUnique<PublishedRouter>();
2018-11-28 17:35:12 +01:00
Composition.WithCollectionBuilder<ContentFinderCollectionBuilder>();
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique<IContentLastChanceFinder, TestLastChanceFinder>();
Composition.RegisterUnique<IVariationContextAccessor, TestVariationContextAccessor>();
}
protected virtual void ComposeWtf()
2018-07-18 10:32:50 +02:00
{
2017-09-20 20:06:46 +02:00
// what else?
var runtimeStateMock = new Mock<IRuntimeState>();
runtimeStateMock.Setup(x => x.Level).Returns(RuntimeLevel.Run);
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique(f => runtimeStateMock.Object);
2017-09-20 20:06:46 +02:00
// ah...
2018-11-28 17:35:12 +01:00
Composition.WithCollectionBuilder<ActionCollectionBuilder>();
Composition.WithCollectionBuilder<PropertyValueConverterCollectionBuilder>();
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique<IPublishedContentTypeFactory, PublishedContentTypeFactory>();
2018-06-29 14:25:48 +02:00
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique<IMediaPathScheme, OriginalMediaPathScheme>();
2018-09-20 18:51:46 +02:00
// register empty content apps collection
2018-12-07 16:12:08 +01:00
Composition.WithCollectionBuilder<ContentAppFactoryCollectionBuilder>();
2016-12-16 10:40:14 +01:00
}
2016-11-05 19:23:55 +01:00
protected virtual void ComposeAutoMapper(bool configure)
{
if (configure == false) return;
2018-11-27 10:37:33 +01:00
Composition
.ComposeCoreMappingProfiles()
.ComposeWebMappingProfiles();
2016-11-05 19:23:55 +01:00
}
2019-01-17 11:01:23 +01:00
protected virtual TypeLoader GetTypeLoader(IAppPolicedCache runtimeCache, IGlobalSettings globalSettings, IProfilingLogger logger, UmbracoTestOptions.TypeLoader option)
2016-11-05 19:23:55 +01:00
{
2018-11-28 11:05:41 +01:00
switch (option)
2016-11-05 19:23:55 +01:00
{
2018-11-28 11:05:41 +01:00
case UmbracoTestOptions.TypeLoader.Default:
return _commonTypeLoader ?? (_commonTypeLoader = CreateCommonTypeLoader(runtimeCache, globalSettings, logger));
case UmbracoTestOptions.TypeLoader.PerFixture:
return _featureTypeLoader ?? (_featureTypeLoader = CreateTypeLoader(runtimeCache, globalSettings, logger));
case UmbracoTestOptions.TypeLoader.PerTest:
return CreateTypeLoader(runtimeCache, globalSettings, logger);
default:
throw new ArgumentOutOfRangeException(nameof(option));
}
2016-11-05 19:23:55 +01:00
}
2019-01-17 11:01:23 +01:00
protected virtual TypeLoader CreateTypeLoader(IAppPolicedCache runtimeCache, IGlobalSettings globalSettings, IProfilingLogger logger)
2017-05-30 15:33:13 +02:00
{
2018-11-28 11:05:41 +01:00
return CreateCommonTypeLoader(runtimeCache, globalSettings, logger);
2017-05-30 15:33:13 +02:00
}
2018-11-28 11:05:41 +01:00
// common to all tests = cannot be overriden
2019-01-17 11:01:23 +01:00
private static TypeLoader CreateCommonTypeLoader(IAppPolicedCache runtimeCache, IGlobalSettings globalSettings, IProfilingLogger logger)
2017-05-30 15:33:13 +02:00
{
return new TypeLoader(runtimeCache, globalSettings.LocalTempStorageLocation, logger, false)
2017-05-30 15:33:13 +02:00
{
AssembliesToScan = new[]
{
Assembly.Load("Umbraco.Core"),
2017-06-27 16:35:29 +02:00
Assembly.Load("Umbraco.Web"),
2017-06-23 18:54:42 +02:00
Assembly.Load("Umbraco.Tests")
2017-05-30 15:33:13 +02:00
}
};
}
2016-11-05 19:23:55 +01:00
protected virtual void ComposeDatabase(UmbracoTestOptions.Database option)
{
if (option == UmbracoTestOptions.Database.None) return;
// create the file
// create the schema
}
2019-01-07 10:43:28 +01:00
protected virtual void ComposeSettings()
{
Composition.Configs.Add(SettingsForTests.GetDefaultUmbracoSettings);
Composition.Configs.Add(SettingsForTests.GetDefaultGlobalSettings);
}
protected virtual void ComposeApplication(bool withApplication)
{
2019-01-07 10:43:28 +01:00
ComposeSettings();
2019-01-07 10:43:28 +01:00
if (withApplication == false) return;
// default Datalayer/Repositories/SQL/Database/etc...
2018-11-27 10:37:33 +01:00
Composition.ComposeRepositories();
// register basic stuff that might need to be there for some container resolvers to work
2019-01-07 10:43:28 +01:00
Composition.RegisterUnique(factory => factory.GetInstance<IUmbracoSettingsSection>().Content);
Composition.RegisterUnique(factory => factory.GetInstance<IUmbracoSettingsSection>().Templates);
Composition.RegisterUnique(factory => factory.GetInstance<IUmbracoSettingsSection>().WebRouting);
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique<IExamineManager>(factory => ExamineManager.Instance);
// register filesystems
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique(factory => TestObjects.GetFileSystemsMock());
2018-11-24 15:38:00 +01:00
var logger = Mock.Of<ILogger>();
var scheme = Mock.Of<IMediaPathScheme>();
var config = Mock.Of<IContentSection>();
var mediaFileSystem = new MediaFileSystem(Mock.Of<IFileSystem>(), config, scheme, logger);
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique<IMediaFileSystem>(factory => mediaFileSystem);
// no factory (noop)
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique<IPublishedModelFactory, NoopPublishedModelFactory>();
// register application stuff (database factory & context, services...)
2018-11-28 17:35:12 +01:00
Composition.WithCollectionBuilder<MapperCollectionBuilder>()
.AddCoreMappers();
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique<IEventMessagesFactory>(_ => new TransientEventMessagesFactory());
Composition.RegisterUnique<IUmbracoDatabaseFactory>(f => new UmbracoDatabaseFactory(
2017-05-12 14:49:44 +02:00
Constants.System.UmbracoConnectionName,
Logger,
2018-11-28 16:57:01 +01:00
new Lazy<IMapperCollection>(Mock.Of<IMapperCollection>)));
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique(f => f.TryGetInstance<IUmbracoDatabaseFactory>().SqlContext);
2018-11-28 17:35:12 +01:00
Composition.WithCollectionBuilder<UrlSegmentProviderCollectionBuilder>(); // empty
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique(factory
2017-06-23 18:54:42 +02:00
=> TestObjects.GetScopeProvider(factory.TryGetInstance<ILogger>(), factory.TryGetInstance<FileSystems>(), factory.TryGetInstance<IUmbracoDatabaseFactory>()));
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique(factory => (IScopeAccessor) factory.GetInstance<IScopeProvider>());
2018-11-27 10:37:33 +01:00
Composition.ComposeServices();
2018-07-20 09:49:05 +02:00
// composition root is doing weird things, fix
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique<IApplicationTreeService, ApplicationTreeService>();
Composition.RegisterUnique<ISectionService, SectionService>();
// somehow property editor ends up wanting this
2018-11-28 17:35:12 +01:00
Composition.WithCollectionBuilder<ManifestValueValidatorCollectionBuilder>();
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique<ManifestParser>();
// note - don't register collections, use builders
2018-11-28 17:35:12 +01:00
Composition.WithCollectionBuilder<DataEditorCollectionBuilder>();
2018-11-29 10:35:16 +01:00
Composition.RegisterUnique<PropertyEditorCollection>();
Composition.RegisterUnique<ParameterEditorCollection>();
}
2016-11-05 19:23:55 +01:00
#endregion
#region Initialize
protected virtual void InitializeAutoMapper(bool configure)
{
if (configure == false) return;
Mapper.Initialize(configuration =>
{
2018-11-28 12:59:40 +01:00
var profiles = Factory.GetAllInstances<Profile>();
2017-07-19 13:42:47 +02:00
foreach (var profile in profiles)
configuration.AddProfile(profile);
2016-11-05 19:23:55 +01:00
});
}
protected virtual void InitializeApplication(bool withApplication)
{
if (withApplication == false) return;
TestHelper.InitializeContentDirectories();
}
2016-11-05 19:23:55 +01:00
#endregion
#region TearDown and Reset
[TearDown]
public virtual void TearDown()
{
2016-12-16 10:40:14 +01:00
FirstTestInFixture = false;
FirstTestInSession = false;
2016-11-05 19:23:55 +01:00
Reset();
if (Options.WithApplication)
{
TestHelper.CleanContentDirectories();
TestHelper.CleanUmbracoSettingsConfig();
}
2016-11-05 19:23:55 +01:00
}
protected virtual void Reset()
{
2017-06-23 18:54:42 +02:00
// reset and dispose scopes
// ensures we don't leak an opened database connection
// which would lock eg SqlCe .sdf files
2018-11-28 12:59:40 +01:00
if (Factory?.TryGetInstance<IScopeProvider>() is ScopeProvider scopeProvider)
2017-06-23 18:54:42 +02:00
{
2018-11-28 16:57:01 +01:00
Scope scope;
2017-06-23 18:54:42 +02:00
while ((scope = scopeProvider.AmbientScope) != null)
{
scope.Reset();
scope.Dispose();
}
}
2018-11-28 16:57:01 +01:00
Current.Reset(); // disposes the factory
2016-11-07 20:50:14 +01:00
// reset all other static things that should not be static ;(
UriUtility.ResetAppDomainAppVirtualPath();
2016-12-16 10:40:14 +01:00
SettingsForTests.Reset(); // fixme - should it be optional?
2018-03-30 19:31:42 +02:00
Mapper.Reset();
2018-04-28 09:55:36 +02:00
// clear static events
DocumentRepository.ClearScopeEvents();
MediaRepository.ClearScopeEvents();
MemberRepository.ClearScopeEvents();
ContentTypeService.ClearScopeEvents();
MediaTypeService.ClearScopeEvents();
MemberTypeService.ClearScopeEvents();
2016-11-05 19:23:55 +01:00
}
#endregion
}
}