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

319 lines
12 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;
2016-04-14 18:17:18 +02:00
using Umbraco.Core.Persistence;
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
{
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);
2017-09-22 18:48:58 +02:00
databaseFactoryMock.Setup(x => x.SqlContext).Returns(Mock.Of<ISqlContext>());
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-07-20 15:45:01 +02:00
public ServiceContext GetServiceContextMock(IContainer container = null)
2016-04-14 18:17:18 +02:00
{
return new ServiceContext(
2016-10-13 21:08:07 +02:00
MockService<IContentService>(),
MockService<IMediaService>(),
MockService<IContentTypeService>(),
MockService<IMediaTypeService>(),
MockService<IDataTypeService>(),
MockService<IFileService>(),
MockService<ILocalizationService>(),
MockService<IPackagingService>(),
MockService<IEntityService>(),
MockService<IRelationService>(),
MockService<IMemberGroupService>(),
MockService<IMemberTypeService>(),
MockService<IMemberService>(),
MockService<IUserService>(),
MockService<ISectionService>(),
MockService<IApplicationTreeService>(),
MockService<ITagService>(),
MockService<INotificationService>(),
MockService<ILocalizedTextService>(),
MockService<IAuditService>(),
MockService<IDomainService>(),
MockService<IMacroService>());
}
2018-07-20 15:45:01 +02:00
private T MockService<T>(IContainer 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 webSecurity = new Mock<WebSecurity>(null, null, globalSettings).Object;
2016-10-13 21:08:07 +02:00
var urlProviders = Enumerable.Empty<IUrlProvider>();
2016-10-18 17:09:26 +02:00
if (accessor == null) accessor = new TestUmbracoContextAccessor();
2018-04-30 21:29:49 +02:00
return UmbracoContext.EnsureContext(accessor, httpContext, publishedSnapshotService, webSecurity, umbracoSettings, urlProviders, globalSettings, new TestVariationContextAccessor(), true);
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>();
webRoutingSectionMock.Setup(x => x.UrlProviderMode).Returns(UrlProviderMode.Auto.ToString());
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>();
2018-07-20 09:49:05 +02:00
MockFs(fileSystems, x => x.MasterPagesFileSystem);
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();
}
}
2016-04-14 18:17:18 +02:00
#endregion
}
2017-07-20 11:21:28 +02:00
}