Files
Umbraco-CMS/src/Umbraco.Tests/TestHelpers/TestObjects.cs

268 lines
18 KiB
C#
Raw Normal View History

2016-04-14 18:17:18 +02:00
using System;
2016-04-22 09:58:02 +02:00
using System.IO;
using System.Linq;
2016-05-23 12:53:29 +02:00
using Moq;
2016-04-14 18:17:18 +02:00
using NPoco;
2016-11-30 19:23:20 +01:00
using Umbraco.Core;
using Umbraco.Core.Cache;
2017-05-30 15:46:25 +02:00
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
2016-04-22 09:58:02 +02:00
using Umbraco.Core.Events;
using Umbraco.Core.Hosting;
2016-04-22 09:58:02 +02:00
using Umbraco.Core.IO;
2016-04-14 18:17:18 +02:00
using Umbraco.Core.Logging;
using Umbraco.Core.Packaging;
2016-04-14 18:17:18 +02:00
using Umbraco.Core.Persistence;
2016-05-23 12:53:29 +02:00
using Umbraco.Core.Persistence.Mappers;
2017-12-15 11:19:03 +01:00
using Umbraco.Core.Persistence.Repositories;
2016-04-14 18:17:18 +02:00
using Umbraco.Core.Persistence.SqlSyntax;
2018-01-26 17:55:20 +01:00
using Umbraco.Core.PropertyEditors;
2017-05-12 14:49:44 +02:00
using Umbraco.Core.Scoping;
2016-04-22 09:58:02 +02:00
using Umbraco.Core.Services;
2017-12-28 09:18:09 +01:00
using Umbraco.Core.Services.Implement;
2016-04-22 09:58:02 +02:00
using Umbraco.Core.Strings;
using Umbraco.Persistance.SqlCe;
using Umbraco.Tests.TestHelpers.Stubs;
using Current = Umbraco.Web.Composing.Current;
2016-04-14 18:17:18 +02:00
namespace Umbraco.Tests.TestHelpers
{
/// <summary>
/// Provides objects for tests.
/// </summary>
2016-12-14 18:40:16 +01:00
internal partial class TestObjects
2016-04-14 18:17:18 +02:00
{
2018-11-28 12:59:40 +01:00
private readonly IRegister _register;
2016-12-14 18:40:16 +01:00
2018-11-28 12:59:40 +01:00
public TestObjects(IRegister register)
2016-12-14 18:40:16 +01:00
{
2018-11-28 12:59:40 +01:00
_register = register;
2016-12-14 18:40:16 +01:00
}
2016-04-14 18:17:18 +02:00
/// <summary>
/// Gets an UmbracoDatabase.
/// </summary>
/// <param name="logger">A logger.</param>
/// <returns>An UmbracoDatabase.</returns>
/// <remarks>This is just a void database that has no actual database but pretends to have an open connection
/// that can begin a transaction.</remarks>
2016-12-14 18:40:16 +01:00
public UmbracoDatabase GetUmbracoSqlCeDatabase(ILogger logger)
2016-04-14 18:17:18 +02:00
{
var syntax = new SqlCeSyntaxProvider();
2016-10-13 21:08:07 +02:00
var connection = GetDbConnection();
2017-09-22 18:28:21 +02:00
var sqlContext = new SqlContext(syntax, DatabaseType.SQLCe, Mock.Of<IPocoDataFactory>());
return new UmbracoDatabase(connection, sqlContext, logger, TestHelper.BulkSqlInsertProvider);
2016-04-14 18:17:18 +02:00
}
/// <summary>
/// Gets an UmbracoDatabase.
/// </summary>
/// <param name="logger">A logger.</param>
/// <returns>An UmbracoDatabase.</returns>
/// <remarks>This is just a void database that has no actual database but pretends to have an open connection
/// that can begin a transaction.</remarks>
2016-12-14 18:40:16 +01:00
public UmbracoDatabase GetUmbracoSqlServerDatabase(ILogger logger)
2016-04-14 18:17:18 +02:00
{
var syntax = new SqlServerSyntaxProvider(); // do NOT try to get the server's version!
2016-10-13 21:08:07 +02:00
var connection = GetDbConnection();
2017-09-22 18:28:21 +02:00
var sqlContext = new SqlContext(syntax, DatabaseType.SqlServer2008, Mock.Of<IPocoDataFactory>());
return new UmbracoDatabase(connection, sqlContext, logger, TestHelper.BulkSqlInsertProvider);
2016-04-14 18:17:18 +02:00
}
2016-04-22 09:58:02 +02:00
2018-11-28 12:59:40 +01:00
public void RegisterServices(IRegister register)
2016-10-13 21:08:07 +02:00
{ }
2016-04-22 09:58:02 +02:00
/// <summary>
/// Gets a ServiceContext.
/// </summary>
/// <param name="scopeProvider"></param>
/// <param name="scopeAccessor"></param>
2016-04-22 09:58:02 +02:00
/// <param name="cache">A cache.</param>
/// <param name="logger">A logger.</param>
2019-11-18 19:46:32 +01:00
/// <param name="ioHelper">An io helper.</param>
/// <param name="globalSettings"></param>
/// <param name="contentSettings"></param>
2016-04-22 09:58:02 +02:00
/// <param name="eventMessagesFactory">An event messages factory.</param>
/// <param name="urlSegmentProviders">Some url segment providers.</param>
/// <param name="umbracoVersion">An Umbraco Version.</param>
/// <param name="factory">A container.</param>
2016-04-22 09:58:02 +02:00
/// <returns>A ServiceContext.</returns>
/// <remarks>Should be used sparingly for integration tests only - for unit tests
/// just mock the services to be passed to the ctor of the ServiceContext.</remarks>
public ServiceContext GetServiceContext(IScopeProvider scopeProvider, IScopeAccessor scopeAccessor,
2019-01-17 08:34:29 +01:00
AppCaches cache,
2016-04-22 09:58:02 +02:00
ILogger logger,
2019-11-18 19:46:32 +01:00
IIOHelper ioHelper,
IGlobalSettings globalSettings,
2020-03-12 15:30:22 +01:00
IContentSettings contentSettings,
2016-04-22 09:58:02 +02:00
IEventMessagesFactory eventMessagesFactory,
UrlSegmentProviderCollection urlSegmentProviders,
2019-11-19 22:37:14 +00:00
IUmbracoVersion umbracoVersion,
IHostingEnvironment hostingEnvironment,
2018-11-28 12:59:40 +01:00
IFactory factory = null)
2016-04-22 09:58:02 +02:00
{
2017-12-15 11:19:03 +01:00
if (scopeProvider == null) throw new ArgumentNullException(nameof(scopeProvider));
if (scopeAccessor == null) throw new ArgumentNullException(nameof(scopeAccessor));
2016-04-22 09:58:02 +02:00
if (cache == null) throw new ArgumentNullException(nameof(cache));
if (logger == null) throw new ArgumentNullException(nameof(logger));
if (eventMessagesFactory == null) throw new ArgumentNullException(nameof(eventMessagesFactory));
2018-11-24 15:38:00 +01:00
var scheme = Mock.Of<IMediaPathScheme>();
2019-12-05 10:41:58 +01:00
var shortStringHelper = Mock.Of<IShortStringHelper>();
var mediaFileSystem = new MediaFileSystem(Mock.Of<IFileSystem>(), scheme, logger, shortStringHelper);
2016-04-22 09:58:02 +02:00
2018-11-28 12:59:40 +01:00
var externalLoginService = GetLazyService<IExternalLoginService>(factory, c => new ExternalLoginService(scopeProvider, logger, eventMessagesFactory, GetRepo<IExternalLoginRepository>(c)));
var publicAccessService = GetLazyService<IPublicAccessService>(factory, c => new PublicAccessService(scopeProvider, logger, eventMessagesFactory, GetRepo<IPublicAccessRepository>(c)));
var domainService = GetLazyService<IDomainService>(factory, c => new DomainService(scopeProvider, logger, eventMessagesFactory, GetRepo<IDomainRepository>(c)));
var auditService = GetLazyService<IAuditService>(factory, c => new AuditService(scopeProvider, logger, eventMessagesFactory, GetRepo<IAuditRepository>(c), GetRepo<IAuditEntryRepository>(c)));
2016-04-22 09:58:02 +02:00
2018-11-28 12:59:40 +01:00
var localizedTextService = GetLazyService<ILocalizedTextService>(factory, c => new LocalizedTextService(
2016-04-22 09:58:02 +02:00
new Lazy<LocalizedTextServiceFileSources>(() =>
{
2019-11-18 20:43:38 +01:00
var mainLangFolder = new DirectoryInfo(ioHelper.MapPath(Current.Configs.Global().UmbracoPath + "/config/lang/"));
var appPlugins = new DirectoryInfo(ioHelper.MapPath(Constants.SystemDirectories.AppPlugins));
var configLangFolder = new DirectoryInfo(ioHelper.MapPath(Constants.SystemDirectories.Config + "/lang/"));
2016-04-22 09:58:02 +02:00
var pluginLangFolders = appPlugins.Exists == false
? Enumerable.Empty<LocalizedTextServiceSupplementaryFileSource>()
: 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<LocalizedTextServiceSupplementaryFileSource>()
: 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,
2016-04-22 09:58:02 +02:00
mainLangFolder,
pluginLangFolders.Concat(userLangFolders));
}),
logger));
2017-09-14 19:29:12 +02:00
var runtimeState = Mock.Of<IRuntimeState>();
var idkMap = new IdKeyMap(scopeProvider);
2017-12-15 11:19:03 +01:00
var propertyEditorCollection = new PropertyEditorCollection(new DataEditorCollection(Enumerable.Empty<DataEditor>()));
2018-11-28 12:59:40 +01:00
var localizationService = GetLazyService<ILocalizationService>(factory, c => new LocalizationService(scopeProvider, logger, eventMessagesFactory, GetRepo<IDictionaryRepository>(c), GetRepo<IAuditRepository>(c), GetRepo<ILanguageRepository>(c)));
var userService = GetLazyService<IUserService>(factory, c => new UserService(scopeProvider, logger, eventMessagesFactory, runtimeState, GetRepo<IUserRepository>(c), GetRepo<IUserGroupRepository>(c),globalSettings));
var dataTypeService = GetLazyService<IDataTypeService>(factory, c => new DataTypeService(scopeProvider, logger, eventMessagesFactory, GetRepo<IDataTypeRepository>(c), GetRepo<IDataTypeContainerRepository>(c), GetRepo<IAuditRepository>(c), GetRepo<IEntityRepository>(c), GetRepo<IContentTypeRepository>(c), ioHelper, localizedTextService.Value, localizationService.Value, TestHelper.ShortStringHelper));
2020-08-06 12:59:21 +02:00
var propertyValidationService = new Lazy<IPropertyValidationService>(() => new PropertyValidationService(propertyEditorCollection, dataTypeService.Value, localizedTextService.Value));
var contentService = GetLazyService<IContentService>(factory, c => new ContentService(scopeProvider, logger, eventMessagesFactory, GetRepo<IDocumentRepository>(c), GetRepo<IEntityRepository>(c), GetRepo<IAuditRepository>(c), GetRepo<IContentTypeRepository>(c), GetRepo<IDocumentBlueprintRepository>(c), GetRepo<ILanguageRepository>(c), propertyValidationService, TestHelper.ShortStringHelper));
2020-08-21 08:42:14 +02:00
var notificationService = GetLazyService<INotificationService>(factory, c => new NotificationService(scopeProvider, userService.Value, contentService.Value, localizationService.Value, logger, ioHelper, GetRepo<INotificationsRepository>(c), globalSettings, contentSettings, TestHelper.EmailSender));
2019-12-18 10:32:22 +01:00
var serverRegistrationService = GetLazyService<IServerRegistrationService>(factory, c => new ServerRegistrationService(scopeProvider, logger, eventMessagesFactory, GetRepo<IServerRegistrationRepository>(c), TestHelper.GetHostingEnvironment()));
2018-11-28 12:59:40 +01:00
var memberGroupService = GetLazyService<IMemberGroupService>(factory, c => new MemberGroupService(scopeProvider, logger, eventMessagesFactory, GetRepo<IMemberGroupRepository>(c)));
2019-11-12 14:16:50 +11:00
var memberService = GetLazyService<IMemberService>(factory, c => new MemberService(scopeProvider, logger, eventMessagesFactory, memberGroupService.Value, GetRepo<IMemberRepository>(c), GetRepo<IMemberTypeRepository>(c), GetRepo<IMemberGroupRepository>(c), GetRepo<IAuditRepository>(c)));
var mediaService = GetLazyService<IMediaService>(factory, c => new MediaService(scopeProvider, mediaFileSystem, logger, eventMessagesFactory, GetRepo<IMediaRepository>(c), GetRepo<IAuditRepository>(c), GetRepo<IMediaTypeRepository>(c), GetRepo<IEntityRepository>(c), TestHelper.ShortStringHelper));
2018-11-28 12:59:40 +01:00
var contentTypeService = GetLazyService<IContentTypeService>(factory, c => new ContentTypeService(scopeProvider, logger, eventMessagesFactory, contentService.Value, GetRepo<IContentTypeRepository>(c), GetRepo<IAuditRepository>(c), GetRepo<IDocumentTypeContainerRepository>(c), GetRepo<IEntityRepository>(c)));
var mediaTypeService = GetLazyService<IMediaTypeService>(factory, c => new MediaTypeService(scopeProvider, logger, eventMessagesFactory, mediaService.Value, GetRepo<IMediaTypeRepository>(c), GetRepo<IAuditRepository>(c), GetRepo<IMediaTypeContainerRepository>(c), GetRepo<IEntityRepository>(c)));
var fileService = GetLazyService<IFileService>(factory, c => new FileService(scopeProvider, logger, eventMessagesFactory, GetRepo<IStylesheetRepository>(c), GetRepo<IScriptRepository>(c), GetRepo<ITemplateRepository>(c), GetRepo<IPartialViewRepository>(c), GetRepo<IPartialViewMacroRepository>(c), GetRepo<IAuditRepository>(c), TestHelper.ShortStringHelper, globalSettings, hostingEnvironment));
2018-11-28 12:59:40 +01:00
var memberTypeService = GetLazyService<IMemberTypeService>(factory, c => new MemberTypeService(scopeProvider, logger, eventMessagesFactory, memberService.Value, GetRepo<IMemberTypeRepository>(c), GetRepo<IAuditRepository>(c), GetRepo<IEntityRepository>(c)));
var entityService = GetLazyService<IEntityService>(factory, c => new EntityService(scopeProvider, logger, eventMessagesFactory, idkMap, GetRepo<IEntityRepository>(c)));
2016-04-22 09:58:02 +02:00
2018-11-28 12:59:40 +01:00
var macroService = GetLazyService<IMacroService>(factory, c => new MacroService(scopeProvider, logger, eventMessagesFactory, GetRepo<IMacroRepository>(c), GetRepo<IAuditRepository>(c)));
var packagingService = GetLazyService<IPackagingService>(factory, c =>
{
2019-11-19 13:03:58 +01:00
var compiledPackageXmlParser = new CompiledPackageXmlParser(new ConflictingPackageData(macroService.Value, fileService.Value), globalSettings);
return new PackagingService(
auditService.Value,
new PackagesRepository(contentService.Value, contentTypeService.Value, dataTypeService.Value, fileService.Value, macroService.Value, localizationService.Value, hostingEnvironment,
2019-12-18 10:32:22 +01:00
new EntityXmlSerializer(contentService.Value, mediaService.Value, dataTypeService.Value, userService.Value, localizationService.Value, contentTypeService.Value, urlSegmentProviders, TestHelper.ShortStringHelper, propertyEditorCollection), logger, umbracoVersion, globalSettings, "createdPackages.config"),
new PackagesRepository(contentService.Value, contentTypeService.Value, dataTypeService.Value, fileService.Value, macroService.Value, localizationService.Value, hostingEnvironment,
2019-12-18 10:32:22 +01:00
new EntityXmlSerializer(contentService.Value, mediaService.Value, dataTypeService.Value, userService.Value, localizationService.Value, contentTypeService.Value, urlSegmentProviders, TestHelper.ShortStringHelper, propertyEditorCollection), logger, umbracoVersion, globalSettings, "installedPackages.config"),
new PackageInstallation(
Merge remote-tracking branch 'origin/netcore/dev' into netcore/feature/move-files # Conflicts: # src/Umbraco.Core/Persistence/Repositories/Implement/ContentRepositoryBase.cs # src/Umbraco.Core/Persistence/Repositories/Implement/DocumentBlueprintRepository.cs # src/Umbraco.Core/Persistence/Repositories/Implement/DocumentRepository.cs # src/Umbraco.Core/Persistence/Repositories/Implement/MediaRepository.cs # src/Umbraco.Core/Persistence/Repositories/Implement/MemberRepository.cs # src/Umbraco.Tests/Persistence/Repositories/ContentTypeRepositoryTest.cs # src/Umbraco.Tests/Persistence/Repositories/DocumentRepositoryTest.cs # src/Umbraco.Tests/Persistence/Repositories/DomainRepositoryTest.cs # src/Umbraco.Tests/Persistence/Repositories/MediaRepositoryTest.cs # src/Umbraco.Tests/Persistence/Repositories/MemberRepositoryTest.cs # src/Umbraco.Tests/Persistence/Repositories/PublicAccessRepositoryTest.cs # src/Umbraco.Tests/Persistence/Repositories/TagRepositoryTest.cs # src/Umbraco.Tests/Persistence/Repositories/TemplateRepositoryTest.cs # src/Umbraco.Tests/Persistence/Repositories/UserRepositoryTest.cs # src/Umbraco.Tests/Published/NestedContentTests.cs # src/Umbraco.Tests/PublishedContent/PublishedContentTestBase.cs # src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs # src/Umbraco.Tests/Services/ContentServicePerformanceTest.cs # src/Umbraco.Tests/Services/ContentServiceTests.cs # src/Umbraco.Tests/Web/TemplateUtilitiesTests.cs # src/Umbraco.Web/PropertyEditors/ContentPickerPropertyEditor.cs # src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs # src/Umbraco.Web/PropertyEditors/MediaPickerPropertyEditor.cs # src/Umbraco.Web/PropertyEditors/MultiNodeTreePickerPropertyEditor.cs # src/Umbraco.Web/PropertyEditors/MultiUrlPickerPropertyEditor.cs # src/Umbraco.Web/PropertyEditors/NestedContentPropertyEditor.cs # src/Umbraco.Web/PropertyEditors/RichTextPropertyEditor.cs
2019-12-11 08:13:51 +01:00
new PackageDataInstallation(logger, fileService.Value, macroService.Value, localizationService.Value, dataTypeService.Value, entityService.Value, contentTypeService.Value, contentService.Value, propertyEditorCollection, scopeProvider, shortStringHelper, GetGlobalSettings(), localizedTextService.Value),
2019-11-13 21:00:54 +01:00
new PackageFileInstallation(compiledPackageXmlParser, ioHelper, new ProfilingLogger(logger, new TestProfiler())),
compiledPackageXmlParser, Mock.Of<IPackageActionRunner>(),
Mock.Of<IHostingEnvironment>(x => x.ApplicationPhysicalPath == ioHelper.MapPath("~"))),
ioHelper);
});
2018-11-28 12:59:40 +01:00
var relationService = GetLazyService<IRelationService>(factory, c => new RelationService(scopeProvider, logger, eventMessagesFactory, entityService.Value, GetRepo<IRelationRepository>(c), GetRepo<IRelationTypeRepository>(c)));
var tagService = GetLazyService<ITagService>(factory, c => new TagService(scopeProvider, logger, eventMessagesFactory, GetRepo<ITagRepository>(c)));
2018-11-28 12:59:40 +01:00
var redirectUrlService = GetLazyService<IRedirectUrlService>(factory, c => new RedirectUrlService(scopeProvider, logger, eventMessagesFactory, GetRepo<IRedirectUrlRepository>(c)));
var consentService = GetLazyService<IConsentService>(factory, c => new ConsentService(scopeProvider, logger, eventMessagesFactory, GetRepo<IConsentRepository>(c)));
var keyValueService = GetLazyService<IKeyValueService>(factory, c => new KeyValueService(scopeProvider, GetRepo<IKeyValueRepository>(c)));
2019-02-01 17:16:50 +01:00
var contentTypeServiceBaseFactory = GetLazyService<IContentTypeBaseServiceProvider>(factory, c => new ContentTypeBaseServiceProvider(factory.GetInstance<IContentTypeService>(),factory.GetInstance<IMediaTypeService>(),factory.GetInstance<IMemberTypeService>()));
2016-04-22 09:58:02 +02:00
return new ServiceContext(
publicAccessService,
domainService,
auditService,
localizedTextService,
tagService,
contentService,
userService,
memberService,
mediaService,
contentTypeService,
mediaTypeService,
2016-04-22 09:58:02 +02:00
dataTypeService,
fileService,
localizationService,
packagingService,
serverRegistrationService,
entityService,
relationService,
macroService,
memberTypeService,
memberGroupService,
notificationService,
2016-07-08 17:58:01 +02:00
externalLoginService,
2018-03-27 17:59:53 +02:00
redirectUrlService,
consentService,
keyValueService,
contentTypeServiceBaseFactory);
2016-04-22 09:58:02 +02:00
}
2016-05-23 12:53:29 +02:00
2018-11-28 11:05:41 +01:00
private Lazy<T> GetLazyService<T>(IFactory container, Func<IFactory, T> ctor)
2016-10-13 21:08:07 +02:00
where T : class
2016-05-23 12:53:29 +02:00
{
2017-12-15 11:19:03 +01:00
return new Lazy<T>(() => container?.TryGetInstance<T>() ?? ctor(container));
}
2018-11-28 11:05:41 +01:00
private T GetRepo<T>(IFactory container)
2017-12-15 11:19:03 +01:00
where T : class, IRepository
{
return container?.TryGetInstance<T>() ?? Mock.Of<T>();
2016-10-13 21:08:07 +02:00
}
public IScopeProvider GetScopeProvider(ILogger logger, ITypeFinder typeFinder = null, FileSystems fileSystems = null, IUmbracoDatabaseFactory databaseFactory = null)
2016-10-13 21:08:07 +02:00
{
if (databaseFactory == null)
{
// var mappersBuilder = new MapperCollectionBuilder(Current.Container); // FIXME:
// mappersBuilder.AddCore();
// var mappers = mappersBuilder.CreateCollection();
2018-11-28 11:05:41 +01:00
var mappers = Current.Factory.GetInstance<IMapperCollection>();
databaseFactory = new UmbracoDatabaseFactory(logger,
2020-04-03 17:05:50 +11:00
SettingsForTests.DefaultGlobalSettings,
2020-03-13 20:37:10 +01:00
new ConnectionStrings(),
Constants.System.UmbracoConnectionName,
2020-03-13 20:37:10 +01:00
new Lazy<IMapperCollection>(() => mappers),
TestHelper.DbProviderFactoryCreator);
2017-05-12 14:49:44 +02:00
}
typeFinder ??= new TypeFinder(logger, new DefaultUmbracoAssemblyProvider(GetType().Assembly), new VaryingRuntimeHash());
fileSystems ??= new FileSystems(Current.Factory, logger, TestHelper.IOHelper, SettingsForTests.GenerateMockGlobalSettings(), TestHelper.GetHostingEnvironment());
2020-03-16 14:02:08 +01:00
var coreDebug = TestHelper.CoreDebugSettings;
var mediaFileSystem = Mock.Of<IMediaFileSystem>();
var scopeProvider = new ScopeProvider(databaseFactory, fileSystems, coreDebug, mediaFileSystem, logger, typeFinder, NoAppCache.Instance);
2017-05-12 14:49:44 +02:00
return scopeProvider;
}
2016-04-14 18:17:18 +02:00
}
}