using System;
using System.IO;
using System.Linq;
using Moq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.SqlSyntax;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Strings;
using Umbraco.Core.DI;
using Umbraco.Core.Persistence.Mappers;
using Umbraco.Core.Events;
using Umbraco.Core.Manifest;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Persistence.Querying;
using Umbraco.Core.Plugins;
using Umbraco.Core.Services;
using Umbraco.Tests.TestHelpers.Stubs;
using Umbraco.Web.Services;
using UmbracoExamine;
namespace Umbraco.Tests.TestHelpers
{
///
/// Provides a base class for all Umbraco tests that require the Umbraco application.
///
///
/// Sets the Umbraco application DI container.
/// Defines the Compose method for DI composition.
/// Sets all sorts of things such as logging, plugin manager, base services, database factory & context...
/// Does *not* create a database.
///
[TestFixture]
[UmbracoTest(AutoMapper = true, ResetPluginManager = false)]
public abstract class TestWithApplicationBase : TestWithSettingsBase
{
protected ILogger Logger => Container.GetInstance();
protected IProfiler Profiler => Container.GetInstance();
protected ProfilingLogger ProfilingLogger => Container.GetInstance();
protected CacheHelper CacheHelper => Container.GetInstance();
protected virtual ISqlSyntaxProvider SqlSyntax => new SqlCeSyntaxProvider();
protected IMapperCollection Mappers => Container.GetInstance();
protected IQueryFactory QueryFactory => Container.GetInstance().QueryFactory;
///
/// Gets a value indicating whether the plugin manager should be resetted before and after each test.
///
///
/// False by default, so the plugin manager does not need to re-scan all of the assemblies and tests run faster.
/// Can be overriden if the plugin manager does need to reset, usually when SetupPluginManager has been overriden.
///
protected virtual bool PluginManagerResetRequired => false;
public override void SetUp()
{
base.SetUp();
TestHelper.InitializeContentDirectories();
// initialize legacy mapings for core editors
// create the legacy prop-eds mapping
if (LegacyPropertyEditorIdToAliasConverter.Count() == 0)
LegacyPropertyEditorIdToAliasConverter.CreateMappingsForCoreEditors();
}
public override void TearDown()
{
base.TearDown();
TestHelper.CleanContentDirectories();
TestHelper.CleanUmbracoSettingsConfig();
}
protected override void Compose()
{
base.Compose();
var settings = SettingsForTests.GetDefault();
// default Datalayer/Repositories/SQL/Database/etc...
Container.RegisterFrom();
// register basic stuff that might need to be there for some container resolvers to work
Container.RegisterSingleton(factory => SettingsForTests.GetDefault());
Container.RegisterSingleton(factory => settings.Content);
Container.RegisterSingleton(factory => settings.Templates);
Container.Register();
Container.Register(factory => new MediaFileSystem(Mock.Of()));
Container.RegisterSingleton();
// replace some stuff
Container.RegisterSingleton(factory => Mock.Of(), "ScriptFileSystem");
Container.RegisterSingleton(factory => Mock.Of(), "PartialViewFileSystem");
Container.RegisterSingleton(factory => Mock.Of(), "PartialViewMacroFileSystem");
Container.RegisterSingleton(factory => Mock.Of(), "StylesheetFileSystem");
// need real file systems here as templates content is on-disk only
//Container.RegisterSingleton(factory => Mock.Of(), "MasterpageFileSystem");
//Container.RegisterSingleton(factory => Mock.Of(), "ViewFileSystem");
Container.RegisterSingleton(factory => new PhysicalFileSystem("Views", "/views"), "ViewFileSystem");
Container.RegisterSingleton(factory => new PhysicalFileSystem("MasterPages", "/masterpages"), "MasterpageFileSystem");
// no factory (noop)
Container.RegisterSingleton();
// register application stuff (database factory & context, services...)
Container.RegisterCollectionBuilder()
.AddCore();
Container.RegisterSingleton(_ => new TransientEventMessagesFactory());
Container.RegisterSingleton();
var sqlSyntaxProviders = TestObjects.GetDefaultSqlSyntaxProviders(Logger);
Container.RegisterSingleton(_ => sqlSyntaxProviders.OfType().First());
Container.RegisterSingleton(f => new UmbracoDatabaseFactory(
Core.Configuration.GlobalSettings.UmbracoConnectionName,
sqlSyntaxProviders,
Logger, f.GetInstance(),
Mock.Of()));
Container.RegisterSingleton(f => new DatabaseContext(f.GetInstance()));
Container.RegisterCollectionBuilder(); // empty
Container.Register(factory
=> TestObjects.GetDatabaseUnitOfWorkProvider(factory.GetInstance(), factory.TryGetInstance(), factory.TryGetInstance()));
Container.RegisterFrom();
// composition root is doing weird things, fix
Container.RegisterSingleton();
Container.RegisterSingleton();
// somehow property editor ends up wanting this
Container.RegisterSingleton(f => new ManifestBuilder(
f.GetInstance(),
new ManifestParser(f.GetInstance(), new DirectoryInfo(IOHelper.MapPath("~/App_Plugins")), f.GetInstance())
));
// note - don't register collections, use builders
Container.RegisterCollectionBuilder();
}
}
}