using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Umbraco.Core;
using Umbraco.Core.DependencyInjection;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.Hosting;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Services;
using Umbraco.Core.Services.Implement;
using Umbraco.Core.Sync;
using Umbraco.Core.WebAssets;
using Umbraco.Examine;
using Umbraco.Tests.TestHelpers.Stubs;
using Umbraco.Web.PublishedCache.NuCache;
using Umbraco.Web.Scheduling;
using Umbraco.Web.Search;
using Umbraco.Infrastructure.Cache;
namespace Umbraco.Tests.Integration.Testing
{
///
/// This is used to replace certain services that are normally registered from our Core / Infrastructure that
/// we do not want active within integration tests
///
///
/// This is a IUserComposer so that it runs after all core composers
///
public class IntegrationTestComposer : ComponentComposer
{
// TODO: Kill this and only enable using ext methods what we need (first we need to kill composers)
public override void Compose(IUmbracoBuilder builder)
{
base.Compose(builder);
builder.Services.AddUnique();
builder.Services.AddUnique(factory => Mock.Of());
// we don't want persisted nucache files in tests
builder.Services.AddTransient(factory => new PublishedSnapshotServiceOptions { IgnoreLocalDb = true });
#if IS_WINDOWS
// ensure all lucene indexes are using RAM directory (no file system)
builder.Services.AddUnique();
#endif
// replace this service so that it can lookup the correct file locations
builder.Services.AddUnique(GetLocalizedTextService);
builder.Services.AddUnique();
builder.Services.AddUnique();
}
///
/// Used to register a replacement for where the file sources are the ones within the netcore project so
/// we don't need to copy files
///
private ILocalizedTextService GetLocalizedTextService(IServiceProvider factory)
{
var globalSettings = factory.GetRequiredService>();
var loggerFactory = factory.GetRequiredService();
var appCaches = factory.GetRequiredService();
var localizedTextService = new LocalizedTextService(
new Lazy(() =>
{
// get the src folder
var currFolder = new DirectoryInfo(TestContext.CurrentContext.TestDirectory);
while(!currFolder.Name.Equals("src", StringComparison.InvariantCultureIgnoreCase))
{
currFolder = currFolder.Parent;
}
var netcoreUI = currFolder.GetDirectories("Umbraco.Web.UI.NetCore", SearchOption.TopDirectoryOnly).First();
var mainLangFolder = new DirectoryInfo(Path.Combine(netcoreUI.FullName, globalSettings.Value.UmbracoPath.TrimStart("~/"), "config", "lang"));
return new LocalizedTextServiceFileSources(
loggerFactory.CreateLogger(),
appCaches,
mainLangFolder);
}),
loggerFactory.CreateLogger());
return localizedTextService;
}
// replace the default so there is no background index rebuilder
private class TestBackgroundIndexRebuilder : BackgroundIndexRebuilder
{
public TestBackgroundIndexRebuilder(IMainDom mainDom, IProfilingLogger profilingLogger , ILoggerFactory loggerFactory, IApplicationShutdownRegistry hostingEnvironment, IndexRebuilder indexRebuilder)
: base(mainDom, profilingLogger , loggerFactory, hostingEnvironment, indexRebuilder)
{
}
public override void RebuildIndexes(bool onlyEmptyIndexes, int waitMilliseconds = 0)
{
// noop
}
}
private class NoopServerMessenger : IServerMessenger
{
public NoopServerMessenger()
{ }
public void QueueRefresh(ICacheRefresher refresher, TPayload[] payload)
{
}
public void QueueRefresh(ICacheRefresher refresher, Func getNumericId, params T[] instances)
{
}
public void QueueRefresh(ICacheRefresher refresher, Func getGuidId, params T[] instances)
{
}
public void QueueRemove(ICacheRefresher refresher, Func getNumericId, params T[] instances)
{
}
public void QueueRemove(ICacheRefresher refresher, params int[] numericIds)
{
}
public void QueueRefresh(ICacheRefresher refresher, params int[] numericIds)
{
}
public void QueueRefresh(ICacheRefresher refresher, params Guid[] guidIds)
{
}
public void QueueRefreshAll(ICacheRefresher refresher)
{
}
public void Sync() { }
public void SendMessages() { }
}
}
}