using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using Moq;
using NPoco;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.SqlSyntax;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Publishing;
using Umbraco.Core.Services;
using Umbraco.Core.Strings;
using Umbraco.Web.Services;
namespace Umbraco.Tests.TestHelpers
{
///
/// Provides objects for tests.
///
static partial class TestObjects
{
///
/// Gets the default ISqlSyntaxProvider objects.
///
/// A logger.
/// A (lazy) database factory.
/// The default ISqlSyntaxProvider objects.
public static IEnumerable GetDefaultSqlSyntaxProviders(ILogger logger, Lazy lazyFactory = null)
{
return new ISqlSyntaxProvider[]
{
new MySqlSyntaxProvider(logger),
new SqlCeSyntaxProvider(),
new SqlServerSyntaxProvider(lazyFactory ?? new Lazy(() => null))
};
}
///
/// Gets an UmbracoDatabase.
///
/// A logger.
/// An UmbracoDatabase.
/// This is just a void database that has no actual database but pretends to have an open connection
/// that can begin a transaction.
public static UmbracoDatabase GetUmbracoSqlCeDatabase(ILogger logger)
{
var syntax = new SqlCeSyntaxProvider();
var dbProviderFactory = DbProviderFactories.GetFactory(Constants.DbProviderNames.SqlCe);
var connection = TestObjects.GetDbConnection();
return new UmbracoDatabase(connection, syntax, DatabaseType.SQLCe, dbProviderFactory, logger);
}
///
/// Gets an UmbracoDatabase.
///
/// A logger.
/// An UmbracoDatabase.
/// This is just a void database that has no actual database but pretends to have an open connection
/// that can begin a transaction.
public static UmbracoDatabase GetUmbracoSqlServerDatabase(ILogger logger)
{
var syntax = new SqlServerSyntaxProvider(new Lazy(() => null)); // do NOT try to get the server's version!
var dbProviderFactory = DbProviderFactories.GetFactory(Constants.DbProviderNames.SqlServer);
var connection = TestObjects.GetDbConnection();
return new UmbracoDatabase(connection, syntax, DatabaseType.SqlServer2008, dbProviderFactory, logger);
}
///
/// Gets a ServiceContext.
///
/// A repository factory.
/// A database unit of work provider.
/// A file unit of work provider.
/// A publishing strategy.
/// A cache.
/// A logger.
/// An event messages factory.
/// Some url segment providers.
/// A ServiceContext.
/// Should be used sparingly for integration tests only - for unit tests
/// just mock the services to be passed to the ctor of the ServiceContext.
public static ServiceContext GetServiceContext(RepositoryFactory repositoryFactory,
IDatabaseUnitOfWorkProvider dbUnitOfWorkProvider,
IUnitOfWorkProvider fileUnitOfWorkProvider,
CacheHelper cache,
ILogger logger,
IEventMessagesFactory eventMessagesFactory,
IEnumerable urlSegmentProviders)
{
if (repositoryFactory == null) throw new ArgumentNullException(nameof(repositoryFactory));
if (dbUnitOfWorkProvider == null) throw new ArgumentNullException(nameof(dbUnitOfWorkProvider));
if (fileUnitOfWorkProvider == null) throw new ArgumentNullException(nameof(fileUnitOfWorkProvider));
if (cache == null) throw new ArgumentNullException(nameof(cache));
if (logger == null) throw new ArgumentNullException(nameof(logger));
if (eventMessagesFactory == null) throw new ArgumentNullException(nameof(eventMessagesFactory));
var provider = dbUnitOfWorkProvider;
var fileProvider = fileUnitOfWorkProvider;
var migrationEntryService = new Lazy(() => new MigrationEntryService(provider, logger, eventMessagesFactory));
var externalLoginService = new Lazy(() => new ExternalLoginService(provider, logger, eventMessagesFactory));
var publicAccessService = new Lazy(() => new PublicAccessService(provider, logger, eventMessagesFactory));
var taskService = new Lazy(() => new TaskService(provider, logger, eventMessagesFactory));
var domainService = new Lazy(() => new DomainService(provider, logger, eventMessagesFactory));
var auditService = new Lazy(() => new AuditService(provider, logger, eventMessagesFactory));
var localizedTextService = new Lazy(() => new LocalizedTextService(
new Lazy(() =>
{
var mainLangFolder = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Umbraco + "/config/lang/"));
var appPlugins = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.AppPlugins));
var configLangFolder = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Config + "/lang/"));
var pluginLangFolders = appPlugins.Exists == false
? Enumerable.Empty()
: appPlugins.GetDirectories()
.SelectMany(x => x.GetDirectories("Lang"))
.SelectMany(x => x.GetFiles("*.xml", SearchOption.TopDirectoryOnly))
.Where(x => Path.GetFileNameWithoutExtension(x.FullName).Length == 5)
.Select(x => new LocalizedTextServiceSupplementaryFileSource(x, false));
//user defined langs that overwrite the default, these should not be used by plugin creators
var userLangFolders = configLangFolder.Exists == false
? Enumerable.Empty()
: configLangFolder
.GetFiles("*.user.xml", SearchOption.TopDirectoryOnly)
.Where(x => Path.GetFileNameWithoutExtension(x.FullName).Length == 10)
.Select(x => new LocalizedTextServiceSupplementaryFileSource(x, true));
return new LocalizedTextServiceFileSources(
logger,
cache.RuntimeCache,
mainLangFolder,
pluginLangFolders.Concat(userLangFolders));
}),
logger));
var userService = new Lazy(() => new UserService(provider, logger, eventMessagesFactory));
var dataTypeService = new Lazy(() => new DataTypeService(provider, logger, eventMessagesFactory));
var contentService = new Lazy(() => new ContentService(provider, logger, eventMessagesFactory, dataTypeService.Value, userService.Value, urlSegmentProviders));
var notificationService = new Lazy(() => new NotificationService(provider, userService.Value, contentService.Value, repositoryFactory, logger));
var serverRegistrationService = new Lazy(() => new ServerRegistrationService(provider, logger, eventMessagesFactory));
var memberGroupService = new Lazy(() => new MemberGroupService(provider, logger, eventMessagesFactory));
var memberService = new Lazy(() => new MemberService(provider, logger, eventMessagesFactory, memberGroupService.Value, dataTypeService.Value));
var mediaService = new Lazy(() => new MediaService(provider, logger, eventMessagesFactory, dataTypeService.Value, userService.Value, urlSegmentProviders));
var contentTypeService = new Lazy(() => new ContentTypeService(provider, logger, eventMessagesFactory, contentService.Value));
var mediaTypeService = new Lazy(() => new MediaTypeService(provider, logger, eventMessagesFactory, mediaService.Value));
var fileService = new Lazy(() => new FileService(fileProvider, provider, logger, eventMessagesFactory));
var localizationService = new Lazy(() => new LocalizationService(provider, logger, eventMessagesFactory));
var memberTypeService = new Lazy(() => new MemberTypeService(provider, logger, eventMessagesFactory, memberService.Value));
var entityService = new Lazy(() => new EntityService(
provider, logger, eventMessagesFactory,
contentService.Value, contentTypeService.Value, mediaService.Value, mediaTypeService.Value, dataTypeService.Value, memberService.Value, memberTypeService.Value,
//TODO: Consider making this an isolated cache instead of using the global one
cache.RuntimeCache));
var macroService = new Lazy(() => new MacroService(provider, logger, eventMessagesFactory));
var packagingService = new Lazy(() => new PackagingService(logger, contentService.Value, contentTypeService.Value, mediaService.Value, macroService.Value, dataTypeService.Value, fileService.Value, localizationService.Value, entityService.Value, userService.Value, repositoryFactory, provider, urlSegmentProviders));
var relationService = new Lazy(() => new RelationService(provider, logger, eventMessagesFactory, entityService.Value));
var treeService = new Lazy(() => new ApplicationTreeService(logger, cache));
var tagService = new Lazy(() => new TagService(provider, logger, eventMessagesFactory));
var sectionService = new Lazy(() => new SectionService(userService.Value, treeService.Value, provider, cache));
return new ServiceContext(
migrationEntryService,
publicAccessService,
taskService,
domainService,
auditService,
localizedTextService,
tagService,
contentService,
userService,
memberService,
mediaService,
contentTypeService,
mediaTypeService,
dataTypeService,
fileService,
localizationService,
packagingService,
serverRegistrationService,
entityService,
relationService,
treeService,
sectionService,
macroService,
memberTypeService,
memberGroupService,
notificationService,
externalLoginService);
}
}
}