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

342 lines
13 KiB
C#
Raw Normal View History

2017-07-20 11:21:28 +02:00
using System;
using System.Collections.Generic;
2016-04-14 18:17:18 +02:00
using System.Data;
using System.Data.Common;
2016-10-13 21:08:07 +02:00
using System.Linq;
2018-07-20 09:49:05 +02:00
using System.Linq.Expressions;
2016-10-13 21:08:07 +02:00
using System.Web;
2016-04-14 18:17:18 +02:00
using Moq;
using Umbraco.Core;
2018-07-20 15:45:01 +02:00
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
2016-10-13 21:08:07 +02:00
using Umbraco.Core.Configuration.UmbracoSettings;
2018-07-20 09:49:05 +02:00
using Umbraco.Core.IO;
2016-04-22 12:03:30 +02:00
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
2019-04-16 20:03:07 +02:00
using Umbraco.Core.Models.PublishedContent;
2016-04-14 18:17:18 +02:00
using Umbraco.Core.Persistence;
2019-03-29 08:30:51 +01:00
using Umbraco.Core.Persistence.SqlSyntax;
2016-04-14 18:17:18 +02:00
using Umbraco.Core.Services;
using Umbraco.Tests.Testing.Objects.Accessors;
2016-10-13 21:08:07 +02:00
using Umbraco.Web;
using Umbraco.Web.PublishedCache;
using Umbraco.Web.Routing;
using Umbraco.Web.Security;
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
{
/// <summary>
2016-12-16 14:18:37 +01:00
/// Gets a mocked IUmbracoDatabaseFactory.
2016-04-14 18:17:18 +02:00
/// </summary>
2016-12-16 14:18:37 +01:00
/// <returns>An IUmbracoDatabaseFactory.</returns>
2016-04-14 18:17:18 +02:00
/// <param name="configured">A value indicating whether the factory is configured.</param>
/// <param name="canConnect">A value indicating whether the factory can connect to the database.</param>
/// <remarks>This is just a void factory that has no actual database.</remarks>
2016-12-16 14:18:37 +01:00
public IUmbracoDatabaseFactory GetDatabaseFactoryMock(bool configured = true, bool canConnect = true)
2016-04-14 18:17:18 +02:00
{
2019-03-29 08:30:51 +01:00
var sqlSyntax = new SqlCeSyntaxProvider();
var sqlContext = Mock.Of<ISqlContext>();
Mock.Get(sqlContext).Setup(x => x.SqlSyntax).Returns(sqlSyntax);
2016-12-16 14:18:37 +01:00
var databaseFactoryMock = new Mock<IUmbracoDatabaseFactory>();
2016-04-14 18:17:18 +02:00
databaseFactoryMock.Setup(x => x.Configured).Returns(configured);
databaseFactoryMock.Setup(x => x.CanConnect).Returns(canConnect);
2019-03-29 08:30:51 +01:00
databaseFactoryMock.Setup(x => x.SqlContext).Returns(sqlContext);
2016-04-22 12:03:30 +02:00
2017-05-12 14:49:44 +02:00
// can create a database - but don't try to use it!
2016-04-22 12:03:30 +02:00
if (configured && canConnect)
2017-05-12 14:49:44 +02:00
databaseFactoryMock.Setup(x => x.CreateDatabase()).Returns(GetUmbracoSqlCeDatabase(Mock.Of<ILogger>()));
2016-04-22 12:03:30 +02:00
2016-04-14 18:17:18 +02:00
return databaseFactoryMock.Object;
}
/// <summary>
/// Gets a mocked service context built with mocked services.
/// </summary>
/// <returns>A ServiceContext.</returns>
2018-11-28 11:05:41 +01:00
public ServiceContext GetServiceContextMock(IFactory container = null)
2016-04-14 18:17:18 +02:00
{
// FIXME: else some tests break - figure it out
2018-11-28 11:05:41 +01:00
container = null;
2019-01-04 16:02:10 +01:00
return ServiceContext.CreatePartial(
2018-11-28 11:05:41 +01:00
MockService<IContentService>(container),
MockService<IMediaService>(container),
MockService<IContentTypeService>(container),
MockService<IMediaTypeService>(container),
MockService<IDataTypeService>(container),
MockService<IFileService>(container),
MockService<ILocalizationService>(container),
MockService<IPackagingService>(container),
MockService<IEntityService>(container),
MockService<IRelationService>(container),
MockService<IMemberGroupService>(container),
MockService<IMemberTypeService>(container),
MockService<IMemberService>(container),
MockService<IUserService>(container),
MockService<ITagService>(container),
MockService<INotificationService>(container),
MockService<ILocalizedTextService>(container),
MockService<IAuditService>(container),
MockService<IDomainService>(container),
MockService<IMacroService>(container));
2016-10-13 21:08:07 +02:00
}
2018-11-28 11:05:41 +01:00
private T MockService<T>(IFactory container = null)
2016-10-13 21:08:07 +02:00
where T : class
{
return container?.TryGetInstance<T>() ?? new Mock<T>().Object;
2016-04-14 18:17:18 +02:00
}
/// <summary>
/// Gets an opened database connection that can begin a transaction.
/// </summary>
/// <returns>A DbConnection.</returns>
/// <remarks>This is because NPoco wants a DbConnection, NOT an IDbConnection,
/// and DbConnection is hard to mock so we create our own class here.</remarks>
2016-12-14 18:40:16 +01:00
public DbConnection GetDbConnection()
2016-04-14 18:17:18 +02:00
{
return new MockDbConnection();
}
2016-10-13 21:08:07 +02:00
/// <summary>
/// Gets an Umbraco context.
/// </summary>
/// <returns>An Umbraco context.</returns>
/// <remarks>This should be the minimum Umbraco context.</remarks>
2016-12-14 18:40:16 +01:00
public UmbracoContext GetUmbracoContextMock(IUmbracoContextAccessor accessor = null)
2016-10-13 21:08:07 +02:00
{
var httpContext = Mock.Of<HttpContextBase>();
2018-04-27 11:38:50 +10:00
var publishedSnapshotMock = new Mock<IPublishedSnapshot>();
2017-10-31 12:50:30 +01:00
publishedSnapshotMock.Setup(x => x.Members).Returns(Mock.Of<IPublishedMemberCache>());
2017-10-31 12:48:24 +01:00
var publishedSnapshot = publishedSnapshotMock.Object;
var publishedSnapshotServiceMock = new Mock<IPublishedSnapshotService>();
publishedSnapshotServiceMock.Setup(x => x.CreatePublishedSnapshot(It.IsAny<string>())).Returns(publishedSnapshot);
var publishedSnapshotService = publishedSnapshotServiceMock.Object;
2016-10-13 21:08:07 +02:00
var umbracoSettings = GetUmbracoSettings();
var globalSettings = GetGlobalSettings();
var urlProviders = new UrlProviderCollection(Enumerable.Empty<IUrlProvider>());
2019-04-15 15:57:35 +02:00
var mediaUrlProviders = new MediaUrlProviderCollection(Enumerable.Empty<IMediaUrlProvider>());
2016-10-13 21:08:07 +02:00
2016-10-18 17:09:26 +02:00
if (accessor == null) accessor = new TestUmbracoContextAccessor();
2019-02-14 12:11:06 +01:00
var umbracoContextFactory = new UmbracoContextFactory(
accessor,
publishedSnapshotService,
new TestVariationContextAccessor(),
new TestDefaultCultureAccessor(),
umbracoSettings,
globalSettings,
urlProviders,
2019-04-15 15:57:35 +02:00
mediaUrlProviders,
2019-02-14 12:11:06 +01:00
Mock.Of<IUserService>());
return umbracoContextFactory.EnsureUmbracoContext(httpContext).UmbracoContext;
2016-10-13 21:08:07 +02:00
}
2016-12-14 18:40:16 +01:00
public IUmbracoSettingsSection GetUmbracoSettings()
2016-10-13 21:08:07 +02:00
{
// FIXME: Why not use the SettingsForTest.GenerateMock ... ?
// FIXME: Shouldn't we use the default ones so they are the same instance for each test?
2016-10-13 21:08:07 +02:00
var umbracoSettingsMock = new Mock<IUmbracoSettingsSection>();
var webRoutingSectionMock = new Mock<IWebRoutingSection>();
2019-04-16 20:03:07 +02:00
webRoutingSectionMock.Setup(x => x.UrlProviderMode).Returns(UrlMode.Auto.ToString());
2016-10-13 21:08:07 +02:00
umbracoSettingsMock.Setup(x => x.WebRouting).Returns(webRoutingSectionMock.Object);
return umbracoSettingsMock.Object;
}
public IGlobalSettings GetGlobalSettings()
{
return SettingsForTests.GetDefaultGlobalSettings();
}
2018-07-20 09:49:05 +02:00
public IFileSystems GetFileSystemsMock()
{
var fileSystems = Mock.Of<IFileSystems>();
2019-02-14 12:11:06 +01:00
2018-07-20 09:49:05 +02:00
MockFs(fileSystems, x => x.MacroPartialsFileSystem);
MockFs(fileSystems, x => x.MvcViewsFileSystem);
MockFs(fileSystems, x => x.PartialViewsFileSystem);
MockFs(fileSystems, x => x.ScriptsFileSystem);
MockFs(fileSystems, x => x.StylesheetsFileSystem);
2018-07-20 09:49:05 +02:00
return fileSystems;
}
private void MockFs(IFileSystems fileSystems, Expression<Func<IFileSystems, IFileSystem>> fileSystem)
{
var fs = Mock.Of<IFileSystem>();
Mock.Get(fileSystems).Setup(fileSystem).Returns(fs);
}
2016-04-14 18:17:18 +02:00
#region Inner classes
private class MockDbConnection : DbConnection
{
protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
{
return Mock.Of<DbTransaction>(); // enough here
}
public override void Close()
{
throw new NotImplementedException();
}
public override void ChangeDatabase(string databaseName)
{
throw new NotImplementedException();
}
public override void Open()
{
throw new NotImplementedException();
}
public override string ConnectionString { get; set; }
protected override DbCommand CreateDbCommand()
{
throw new NotImplementedException();
}
public override string Database { get; }
public override string DataSource { get; }
public override string ServerVersion { get; }
public override ConnectionState State => ConnectionState.Open; // else NPoco reopens
}
2017-05-12 14:49:44 +02:00
public class TestDataTypeService : IDataTypeService
{
public TestDataTypeService()
{
DataTypes = new Dictionary<int, IDataType>();
}
public TestDataTypeService(params IDataType[] dataTypes)
{
DataTypes = dataTypes.ToDictionary(x => x.Id, x => x);
}
public TestDataTypeService(IEnumerable<IDataType> dataTypes)
{
DataTypes = dataTypes.ToDictionary(x => x.Id, x => x);
}
public Dictionary<int, IDataType> DataTypes { get; }
2018-05-31 16:52:38 +10:00
public Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentId, string name, int userId = -1)
{
throw new NotImplementedException();
}
2018-05-31 16:52:38 +10:00
public Attempt<OperationResult> SaveContainer(EntityContainer container, int userId = -1)
{
throw new NotImplementedException();
}
public EntityContainer GetContainer(int containerId)
{
throw new NotImplementedException();
}
public EntityContainer GetContainer(Guid containerId)
{
throw new NotImplementedException();
}
public IEnumerable<EntityContainer> GetContainers(string folderName, int level)
{
throw new NotImplementedException();
}
public IEnumerable<EntityContainer> GetContainers(IDataType dataType)
{
throw new NotImplementedException();
}
public IEnumerable<EntityContainer> GetContainers(int[] containerIds)
{
throw new NotImplementedException();
}
2018-05-31 16:52:38 +10:00
public Attempt<OperationResult> DeleteContainer(int containerId, int userId = -1)
{
throw new NotImplementedException();
}
2018-05-31 16:52:38 +10:00
public Attempt<OperationResult<OperationResultType, EntityContainer>> RenameContainer(int id, string name, int userId = -1)
{
throw new NotImplementedException();
}
public IDataType GetDataType(string name)
{
throw new NotImplementedException();
}
public IDataType GetDataType(int id)
{
DataTypes.TryGetValue(id, out var dataType);
return dataType;
}
public IDataType GetDataType(Guid id)
{
throw new NotImplementedException();
}
public IEnumerable<IDataType> GetAll(params int[] ids)
{
if (ids.Length == 0) return DataTypes.Values;
return ids.Select(x => DataTypes.TryGetValue(x, out var dataType) ? dataType : null).WhereNotNull();
}
2018-05-31 16:52:38 +10:00
public void Save(IDataType dataType, int userId = -1)
{
throw new NotImplementedException();
}
2018-05-31 16:52:38 +10:00
public void Save(IEnumerable<IDataType> dataTypeDefinitions, int userId = -1)
{
throw new NotImplementedException();
}
public void Save(IEnumerable<IDataType> dataTypeDefinitions, int userId, bool raiseEvents)
{
throw new NotImplementedException();
}
2018-05-31 16:52:38 +10:00
public void Delete(IDataType dataType, int userId = -1)
{
throw new NotImplementedException();
}
public IEnumerable<IDataType> GetByEditorAlias(string propertyEditorAlias)
{
throw new NotImplementedException();
}
public Attempt<OperationResult<MoveOperationStatusType>> Move(IDataType toMove, int parentId)
{
throw new NotImplementedException();
}
2019-10-08 23:09:45 +11:00
public IReadOnlyDictionary<Udi, IEnumerable<string>> GetReferences(int id)
2019-10-08 23:09:45 +11:00
{
throw new NotImplementedException();
}
}
2016-04-14 18:17:18 +02:00
#endregion
}
2017-07-20 11:21:28 +02:00
}