diff --git a/src/Umbraco.Core/ApplicationContext.cs b/src/Umbraco.Core/ApplicationContext.cs index e73bdb9e46..3ac680d4ab 100644 --- a/src/Umbraco.Core/ApplicationContext.cs +++ b/src/Umbraco.Core/ApplicationContext.cs @@ -28,12 +28,11 @@ namespace Umbraco.Core /// /// /// - internal ApplicationContext(DatabaseContext dbContext, ServiceContext serviceContext, CacheHelper cache) + public ApplicationContext(DatabaseContext dbContext, ServiceContext serviceContext, CacheHelper cache) { if (dbContext == null) throw new ArgumentNullException("dbContext"); if (serviceContext == null) throw new ArgumentNullException("serviceContext"); if (cache == null) throw new ArgumentNullException("cache"); - _databaseContext = dbContext; _services = serviceContext; ApplicationCache = cache; @@ -43,12 +42,60 @@ namespace Umbraco.Core /// Creates a basic app context /// /// - internal ApplicationContext(CacheHelper cache) + public ApplicationContext(CacheHelper cache) { ApplicationCache = cache; } - /// + /// + /// A method used to set and/or ensure that a global ApplicationContext singleton is created. + /// + /// + /// The instance to set on the global application singleton + /// + /// If set to true and the singleton is already set, it will be replaced + /// + /// + /// This is NOT thread safe + /// + public static ApplicationContext EnsureContext(ApplicationContext appContext, bool replaceContext) + { + if (ApplicationContext.Current != null) + { + if (!replaceContext) + return ApplicationContext.Current; + } + ApplicationContext.Current = appContext; + return ApplicationContext.Current; + } + + /// + /// A method used to create and ensure that a global ApplicationContext singleton is created. + /// + /// + /// + /// If set to true will replace the current singleton instance - This should only be used for unit tests or on app + /// startup if for some reason the boot manager is not the umbraco boot manager. + /// + /// + /// + /// + /// + /// This is NOT thread safe + /// + public static ApplicationContext EnsureContext(DatabaseContext dbContext, ServiceContext serviceContext, bool enableCache, bool replaceContext) + { + if (ApplicationContext.Current != null) + { + if (!replaceContext) + return ApplicationContext.Current; + } + var ctx = new ApplicationContext(dbContext, serviceContext, enableCache); + ApplicationContext.Current = ctx; + return ApplicationContext.Current; + } + + /// /// Singleton accessor /// public static ApplicationContext Current { get; internal set; } diff --git a/src/Umbraco.Core/DatabaseContext.cs b/src/Umbraco.Core/DatabaseContext.cs index 4062e2a52f..64a40f32e4 100644 --- a/src/Umbraco.Core/DatabaseContext.cs +++ b/src/Umbraco.Core/DatabaseContext.cs @@ -29,7 +29,7 @@ namespace Umbraco.Core private string _providerName; private DatabaseSchemaResult _result; - internal DatabaseContext(IDatabaseFactory factory) + public DatabaseContext(IDatabaseFactory factory) { _factory = factory; } diff --git a/src/Umbraco.Core/Models/ContentBase.cs b/src/Umbraco.Core/Models/ContentBase.cs index c63c8eba1e..f90e847289 100644 --- a/src/Umbraco.Core/Models/ContentBase.cs +++ b/src/Umbraco.Core/Models/ContentBase.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Collections.Specialized; +using System.ComponentModel; using System.Diagnostics; using System.Globalization; using System.Linq; @@ -50,7 +51,7 @@ namespace Umbraco.Core.Models _contentTypeId = int.Parse(contentType.Id.ToString(CultureInfo.InvariantCulture)); _properties = properties; _properties.EnsurePropertyTypes(PropertyTypes); - AdditionalData = new Dictionary(); + _additionalData = new Dictionary(); } /// @@ -74,7 +75,7 @@ namespace Umbraco.Core.Models _contentTypeId = int.Parse(contentType.Id.ToString(CultureInfo.InvariantCulture)); _properties = properties; _properties.EnsurePropertyTypes(PropertyTypes); - AdditionalData = new Dictionary(); + _additionalData = new Dictionary(); } private static readonly PropertyInfo NameSelector = ExpressionHelper.GetPropertyInfo(x => x.Name); @@ -255,6 +256,16 @@ namespace Umbraco.Core.Models } } + private readonly IDictionary _additionalData; + /// + /// Some entities may expose additional data that other's might not, this custom data will be available in this collection + /// + [EditorBrowsable(EditorBrowsableState.Never)] + IDictionary IUmbracoEntity.AdditionalData + { + get { return _additionalData; } + } + /// /// Some entities may expose additional data that other's might not, this custom data will be available in this collection /// diff --git a/src/Umbraco.Core/Models/ContentTypeBase.cs b/src/Umbraco.Core/Models/ContentTypeBase.cs index 0457c113b7..87acf07d45 100644 --- a/src/Umbraco.Core/Models/ContentTypeBase.cs +++ b/src/Umbraco.Core/Models/ContentTypeBase.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Collections.Specialized; +using System.ComponentModel; using System.Linq; using System.Reflection; using System.Runtime.Serialization; @@ -42,7 +43,7 @@ namespace Umbraco.Core.Models _allowedContentTypes = new List(); _propertyGroups = new PropertyGroupCollection(); _propertyTypes = new PropertyTypeCollection(); - AdditionalData = new Dictionary(); + _additionalData = new Dictionary(); } protected ContentTypeBase(IContentTypeBase parent) @@ -53,7 +54,7 @@ namespace Umbraco.Core.Models _allowedContentTypes = new List(); _propertyGroups = new PropertyGroupCollection(); _propertyTypes = new PropertyTypeCollection(); - AdditionalData = new Dictionary(); + _additionalData = new Dictionary(); } private static readonly PropertyInfo NameSelector = ExpressionHelper.GetPropertyInfo(x => x.Name); @@ -316,6 +317,16 @@ namespace Umbraco.Core.Models } } + private readonly IDictionary _additionalData; + /// + /// Some entities may expose additional data that other's might not, this custom data will be available in this collection + /// + [EditorBrowsable(EditorBrowsableState.Never)] + IDictionary IUmbracoEntity.AdditionalData + { + get { return _additionalData; } + } + /// /// Some entities may expose additional data that other's might not, this custom data will be available in this collection /// diff --git a/src/Umbraco.Core/Models/DataTypeDefinition.cs b/src/Umbraco.Core/Models/DataTypeDefinition.cs index a930bf94c4..8fcc8b6a13 100644 --- a/src/Umbraco.Core/Models/DataTypeDefinition.cs +++ b/src/Umbraco.Core/Models/DataTypeDefinition.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Reflection; using System.Runtime.Serialization; using Umbraco.Core.Models.EntityBase; @@ -34,14 +35,14 @@ namespace Umbraco.Core.Models { _parentId = parentId; _propertyEditorAlias = LegacyPropertyEditorIdToAliasConverter.GetAliasFromLegacyId(controlId, true); - AdditionalData = new Dictionary(); + + _additionalData = new Dictionary(); } - public DataTypeDefinition(int parentId, string propertyEditorAlias) { _parentId = parentId; _propertyEditorAlias = propertyEditorAlias; - AdditionalData = new Dictionary(); + _additionalData = new Dictionary(); } private static readonly PropertyInfo NameSelector = ExpressionHelper.GetPropertyInfo(x => x.Name); @@ -173,9 +174,11 @@ namespace Umbraco.Core.Models _trashed = value; return _trashed; }, _trashed, TrashedSelector); + //This is a custom property that is not exposed in IUmbracoEntity so add it to the additional data + _additionalData["Trashed"] = value; } } - + [DataMember] public string PropertyEditorAlias { @@ -209,7 +212,7 @@ namespace Umbraco.Core.Models var alias = LegacyPropertyEditorIdToAliasConverter.GetAliasFromLegacyId(value, true); PropertyEditorAlias = alias; //This is a custom property that is not exposed in IUmbracoEntity so add it to the additional data - AdditionalData["DatabaseType"] = value; + _additionalData["ControlId"] = value; } } @@ -229,10 +232,20 @@ namespace Umbraco.Core.Models }, _databaseType, DatabaseTypeSelector); //This is a custom property that is not exposed in IUmbracoEntity so add it to the additional data - AdditionalData["DatabaseType"] = value; + _additionalData["DatabaseType"] = value; } } + private readonly IDictionary _additionalData; + /// + /// Some entities may expose additional data that other's might not, this custom data will be available in this collection + /// + [EditorBrowsable(EditorBrowsableState.Never)] + IDictionary IUmbracoEntity.AdditionalData + { + get { return _additionalData; } + } + /// /// Some entities may expose additional data that other's might not, this custom data will be available in this collection /// diff --git a/src/Umbraco.Core/Persistence/Factories/IEntityFactory.cs b/src/Umbraco.Core/Persistence/Factories/IEntityFactory.cs index 6053090178..a0d8823d68 100644 --- a/src/Umbraco.Core/Persistence/Factories/IEntityFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/IEntityFactory.cs @@ -1,5 +1,7 @@ namespace Umbraco.Core.Persistence.Factories { + //TODO: Not sure why we need this interface as it's never referenced in code. + internal interface IEntityFactory where TEntity : class where TDto : class diff --git a/src/Umbraco.Core/Persistence/IDatabaseFactory.cs b/src/Umbraco.Core/Persistence/IDatabaseFactory.cs index 87c146fa0f..b0efb7f94a 100644 --- a/src/Umbraco.Core/Persistence/IDatabaseFactory.cs +++ b/src/Umbraco.Core/Persistence/IDatabaseFactory.cs @@ -5,7 +5,7 @@ namespace Umbraco.Core.Persistence /// /// Used to create the UmbracoDatabase for use in the DatabaseContext /// - internal interface IDatabaseFactory : IDisposable + public interface IDatabaseFactory : IDisposable { UmbracoDatabase CreateDatabase(); } diff --git a/src/Umbraco.Core/Persistence/RepositoryFactory.cs b/src/Umbraco.Core/Persistence/RepositoryFactory.cs index 89d04bf4e7..f502e28d32 100644 --- a/src/Umbraco.Core/Persistence/RepositoryFactory.cs +++ b/src/Umbraco.Core/Persistence/RepositoryFactory.cs @@ -20,7 +20,7 @@ namespace Umbraco.Core.Persistence } - internal RepositoryFactory(bool disableAllCache) + public RepositoryFactory(bool disableAllCache) : this(disableAllCache, UmbracoConfig.For.UmbracoSettings()) { diff --git a/src/Umbraco.Core/Services/EntityService.cs b/src/Umbraco.Core/Services/EntityService.cs index 882aa86b4e..7e103c687b 100644 --- a/src/Umbraco.Core/Services/EntityService.cs +++ b/src/Umbraco.Core/Services/EntityService.cs @@ -10,7 +10,7 @@ using Umbraco.Core.Persistence.UnitOfWork; namespace Umbraco.Core.Services { - public class EntityService : IService + public class EntityService : IService, IEntityService { private readonly IDatabaseUnitOfWorkProvider _uowProvider; private readonly RepositoryFactory _repositoryFactory; diff --git a/src/Umbraco.Core/Services/IEntityService.cs b/src/Umbraco.Core/Services/IEntityService.cs new file mode 100644 index 0000000000..c32d755c3f --- /dev/null +++ b/src/Umbraco.Core/Services/IEntityService.cs @@ -0,0 +1,146 @@ +using System; +using System.Collections.Generic; +using Umbraco.Core.Models; +using Umbraco.Core.Models.EntityBase; + +namespace Umbraco.Core.Services +{ + public interface IEntityService + { + /// + /// Gets an UmbracoEntity by its Id, and optionally loads the complete object graph. + /// + /// + /// By default this will load the base type with a minimum set of properties. + /// + /// Id of the object to retrieve + /// Optional bool to load the complete object graph when set to False. + /// An + IUmbracoEntity Get(int id, bool loadBaseType = true); + + /// + /// Gets an UmbracoEntity by its Id and UmbracoObjectType, and optionally loads the complete object graph. + /// + /// + /// By default this will load the base type with a minimum set of properties. + /// + /// Id of the object to retrieve + /// UmbracoObjectType of the entity to retrieve + /// Optional bool to load the complete object graph when set to False. + /// An + IUmbracoEntity Get(int id, UmbracoObjectTypes umbracoObjectType, bool loadBaseType = true); + + /// + /// Gets an UmbracoEntity by its Id and specified Type. Optionally loads the complete object graph. + /// + /// + /// By default this will load the base type with a minimum set of properties. + /// + /// Type of the model to retrieve. Must be based on an + /// Id of the object to retrieve + /// Optional bool to load the complete object graph when set to False. + /// An + IUmbracoEntity Get(int id, bool loadBaseType = true) where T : IUmbracoEntity; + + /// + /// Gets the parent of entity by its id + /// + /// Id of the entity to retrieve the Parent for + /// An + IUmbracoEntity GetParent(int id); + + /// + /// Gets the parent of entity by its id and UmbracoObjectType + /// + /// Id of the entity to retrieve the Parent for + /// UmbracoObjectType of the parent to retrieve + /// An + IUmbracoEntity GetParent(int id, UmbracoObjectTypes umbracoObjectType); + + /// + /// Gets a collection of children by the parents Id + /// + /// Id of the parent to retrieve children for + /// An enumerable list of objects + IEnumerable GetChildren(int parentId); + + /// + /// Gets a collection of children by the parents Id and UmbracoObjectType + /// + /// Id of the parent to retrieve children for + /// UmbracoObjectType of the children to retrieve + /// An enumerable list of objects + IEnumerable GetChildren(int parentId, UmbracoObjectTypes umbracoObjectType); + + /// + /// Gets a collection of descendents by the parents Id + /// + /// Id of entity to retrieve descendents for + /// An enumerable list of objects + IEnumerable GetDescendents(int id); + + /// + /// Gets a collection of descendents by the parents Id + /// + /// Id of entity to retrieve descendents for + /// UmbracoObjectType of the descendents to retrieve + /// An enumerable list of objects + IEnumerable GetDescendents(int id, UmbracoObjectTypes umbracoObjectType); + + /// + /// Gets a collection of the entities at the root, which corresponds to the entities with a Parent Id of -1. + /// + /// UmbracoObjectType of the root entities to retrieve + /// An enumerable list of objects + IEnumerable GetRootEntities(UmbracoObjectTypes umbracoObjectType); + + /// + /// Gets a collection of all of a given type. + /// + /// Type of the entities to retrieve + /// An enumerable list of objects + IEnumerable GetAll() where T : IUmbracoEntity; + + /// + /// Gets a collection of all of a given type. + /// + /// UmbracoObjectType of the entities to return + /// An enumerable list of objects + IEnumerable GetAll(UmbracoObjectTypes umbracoObjectType); + + /// + /// Gets a collection of + /// + /// Guid id of the UmbracoObjectType + /// An enumerable list of objects + IEnumerable GetAll(Guid objectTypeId); + + /// + /// Gets the UmbracoObjectType from the integer id of an IUmbracoEntity. + /// + /// Id of the entity + /// + UmbracoObjectTypes GetObjectType(int id); + + /// + /// Gets the UmbracoObjectType from an IUmbracoEntity. + /// + /// + /// + UmbracoObjectTypes GetObjectType(IUmbracoEntity entity); + + /// + /// Gets the Type of an entity by its Id + /// + /// Id of the entity + /// Type of the entity + Type GetEntityType(int id); + + /// + /// Gets the Type of an entity by its + /// + /// + /// Type of the entity + Type GetEntityType(UmbracoObjectTypes umbracoObjectType); + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Services/RelationService.cs b/src/Umbraco.Core/Services/RelationService.cs index 83d09023ab..29a42da115 100644 --- a/src/Umbraco.Core/Services/RelationService.cs +++ b/src/Umbraco.Core/Services/RelationService.cs @@ -13,10 +13,10 @@ namespace Umbraco.Core.Services { private readonly IDatabaseUnitOfWorkProvider _uowProvider; private readonly RepositoryFactory _repositoryFactory; - private readonly EntityService _entityService; + private readonly IEntityService _entityService; public RelationService(IDatabaseUnitOfWorkProvider uowProvider, RepositoryFactory repositoryFactory, - EntityService entityService) + IEntityService entityService) { _uowProvider = uowProvider; _repositoryFactory = repositoryFactory; diff --git a/src/Umbraco.Core/Services/ServiceContext.cs b/src/Umbraco.Core/Services/ServiceContext.cs index ce5a974de8..00f5e5d6c5 100644 --- a/src/Umbraco.Core/Services/ServiceContext.cs +++ b/src/Umbraco.Core/Services/ServiceContext.cs @@ -12,46 +12,71 @@ namespace Umbraco.Core.Services /// public class ServiceContext { - private Lazy _contentService; - private Lazy _userService; - private Lazy _memberService; - private Lazy _mediaService; - private Lazy _contentTypeService; - private Lazy _dataTypeService; - private Lazy _fileService; - private Lazy _localizationService; + private Lazy _contentService; + private Lazy _userService; + private Lazy _memberService; + private Lazy _mediaService; + private Lazy _contentTypeService; + private Lazy _dataTypeService; + private Lazy _fileService; + private Lazy _localizationService; private Lazy _packagingService; private Lazy _serverRegistrationService; - private Lazy _entityService; + private Lazy _entityService; private Lazy _relationService; private Lazy _treeService; private Lazy _sectionService; - private Lazy _macroService; - private Lazy _memberTypeService; + private Lazy _macroService; + private Lazy _memberTypeService; - /// - /// Constructor - /// - /// - /// - /// + /// + /// public ctor - will generally just be used for unit testing + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public ServiceContext(IContentService contentService, IMediaService mediaService, IContentTypeService contentTypeService, IDataTypeService dataTypeService, IFileService fileService, ILocalizationService localizationService, PackagingService packagingService, IEntityService entityService, RelationService relationService) + { + _contentService = new Lazy(() => contentService); + _mediaService = new Lazy(() => mediaService); + _contentTypeService = new Lazy(() => contentTypeService); + _dataTypeService = new Lazy(() => dataTypeService); + _fileService = new Lazy(() => fileService); + _localizationService = new Lazy(() => localizationService); + _packagingService = new Lazy(() => packagingService); + _entityService = new Lazy(() => entityService); + _relationService = new Lazy(() => relationService); + } + + /// + /// Constructor used to instantiate the core services + /// + /// + /// + /// internal ServiceContext(IDatabaseUnitOfWorkProvider dbUnitOfWorkProvider, IUnitOfWorkProvider fileUnitOfWorkProvider, BasePublishingStrategy publishingStrategy, CacheHelper cache) - { + { BuildServiceCache(dbUnitOfWorkProvider, fileUnitOfWorkProvider, publishingStrategy, cache, - //this needs to be lazy because when we create the service context it's generally before the - //resolvers have been initialized! - new Lazy(() => RepositoryResolver.Current.Factory)); - } + //this needs to be lazy because when we create the service context it's generally before the + //resolvers have been initialized! + new Lazy(() => RepositoryResolver.Current.Factory)); + } /// /// Builds the various services /// - private void BuildServiceCache( - IDatabaseUnitOfWorkProvider dbUnitOfWorkProvider, - IUnitOfWorkProvider fileUnitOfWorkProvider, - BasePublishingStrategy publishingStrategy, + private void BuildServiceCache( + IDatabaseUnitOfWorkProvider dbUnitOfWorkProvider, + IUnitOfWorkProvider fileUnitOfWorkProvider, + BasePublishingStrategy publishingStrategy, CacheHelper cache, - Lazy repositoryFactory) + Lazy repositoryFactory) { var provider = dbUnitOfWorkProvider; var fileProvider = fileUnitOfWorkProvider; @@ -59,37 +84,37 @@ namespace Umbraco.Core.Services if (_serverRegistrationService == null) _serverRegistrationService = new Lazy(() => new ServerRegistrationService(provider, repositoryFactory.Value)); - if (_userService == null) - _userService = new Lazy(() => new UserService(provider, repositoryFactory.Value)); + if (_userService == null) + _userService = new Lazy(() => new UserService(provider, repositoryFactory.Value)); if (_memberService == null) - _memberService = new Lazy(() => new MemberService(provider, repositoryFactory.Value)); + _memberService = new Lazy(() => new MemberService(provider, repositoryFactory.Value)); if (_contentService == null) - _contentService = new Lazy(() => new ContentService(provider, repositoryFactory.Value, publishingStrategy)); + _contentService = new Lazy(() => new ContentService(provider, repositoryFactory.Value, publishingStrategy)); - if(_mediaService == null) - _mediaService = new Lazy(() => new MediaService(provider, repositoryFactory.Value)); + if (_mediaService == null) + _mediaService = new Lazy(() => new MediaService(provider, repositoryFactory.Value)); - if(_contentTypeService == null) - _contentTypeService = new Lazy(() => new ContentTypeService(provider, repositoryFactory.Value, _contentService.Value, _mediaService.Value)); + if (_contentTypeService == null) + _contentTypeService = new Lazy(() => new ContentTypeService(provider, repositoryFactory.Value, _contentService.Value, _mediaService.Value)); - if(_dataTypeService == null) - _dataTypeService = new Lazy(() => new DataTypeService(provider, repositoryFactory.Value)); + if (_dataTypeService == null) + _dataTypeService = new Lazy(() => new DataTypeService(provider, repositoryFactory.Value)); - if(_fileService == null) - _fileService = new Lazy(() => new FileService(fileProvider, provider, repositoryFactory.Value)); + if (_fileService == null) + _fileService = new Lazy(() => new FileService(fileProvider, provider, repositoryFactory.Value)); - if(_localizationService == null) - _localizationService = new Lazy(() => new LocalizationService(provider, repositoryFactory.Value)); + if (_localizationService == null) + _localizationService = new Lazy(() => new LocalizationService(provider, repositoryFactory.Value)); - if(_packagingService == null) + if (_packagingService == null) _packagingService = new Lazy(() => new PackagingService(_contentService.Value, _contentTypeService.Value, _mediaService.Value, _dataTypeService.Value, _fileService.Value, _localizationService.Value, repositoryFactory.Value, provider)); if (_entityService == null) - _entityService = new Lazy(() => new EntityService(provider, repositoryFactory.Value, _contentService.Value, _contentTypeService.Value, _mediaService.Value, _dataTypeService.Value)); + _entityService = new Lazy(() => new EntityService(provider, repositoryFactory.Value, _contentService.Value, _contentTypeService.Value, _mediaService.Value, _dataTypeService.Value)); - if(_relationService == null) + if (_relationService == null) _relationService = new Lazy(() => new RelationService(provider, repositoryFactory.Value, _entityService.Value)); if (_treeService == null) @@ -116,7 +141,7 @@ namespace Umbraco.Core.Services /// /// Gets the /// - internal MacroService MacroService + internal IMacroService MacroService { get { return _macroService.Value; } } @@ -124,7 +149,7 @@ namespace Umbraco.Core.Services /// /// Gets the /// - public EntityService EntityService + public IEntityService EntityService { get { return _entityService.Value; } } @@ -150,7 +175,7 @@ namespace Umbraco.Core.Services /// public IContentTypeService ContentTypeService { - get { return _contentTypeService.Value; } + get { return _contentTypeService.Value; } } /// @@ -158,7 +183,7 @@ namespace Umbraco.Core.Services /// public IDataTypeService DataTypeService { - get { return _dataTypeService.Value; } + get { return _dataTypeService.Value; } } /// @@ -166,7 +191,7 @@ namespace Umbraco.Core.Services /// public IFileService FileService { - get { return _fileService.Value; } + get { return _fileService.Value; } } /// @@ -174,7 +199,7 @@ namespace Umbraco.Core.Services /// public ILocalizationService LocalizationService { - get { return _localizationService.Value; } + get { return _localizationService.Value; } } /// @@ -182,7 +207,7 @@ namespace Umbraco.Core.Services /// public IMediaService MediaService { - get { return _mediaService.Value; } + get { return _mediaService.Value; } } /// @@ -198,7 +223,7 @@ namespace Umbraco.Core.Services /// internal IUserService UserService { - get { return _userService.Value; } + get { return _userService.Value; } } /// @@ -232,5 +257,14 @@ namespace Umbraco.Core.Services { get { return _memberTypeService.Value; } } + + /// + /// Gets the MemberTypeService + /// + internal IMemberTypeService MemberTypeService + { + get { return _memberTypeService.Value; } + } + } } \ No newline at end of file diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index ef58c0a1bf..1e6eef6584 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -922,6 +922,7 @@ + diff --git a/src/Umbraco.Tests/MockTests.cs b/src/Umbraco.Tests/MockTests.cs new file mode 100644 index 0000000000..be31d2dbf4 --- /dev/null +++ b/src/Umbraco.Tests/MockTests.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Web; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.UnitOfWork; +using Umbraco.Core.Services; +using Moq; +using Umbraco.Web; + +namespace Umbraco.Tests +{ + [TestFixture] + public class MockTests + { + + [Test] + public void Can_Create_Empty_App_Context() + { + var appCtx = new ApplicationContext(false); + Assert.Pass(); + } + + [Test] + public void Can_Create_Service_Context() + { + var svcCtx = new ServiceContext( + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new PackagingService( + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new RepositoryFactory(true), + new Mock().Object), + new Mock().Object, + new RelationService( + new Mock().Object, + new RepositoryFactory(true), + new Mock().Object)); + Assert.Pass(); + } + + [Test] + public void Can_Create_Db_Context() + { + var dbCtx = new DatabaseContext(new Mock().Object); + Assert.Pass(); + } + + [Test] + public void Can_Create_App_Context_With_Services() + { + var appCtx = new ApplicationContext( + new DatabaseContext(new Mock().Object), + new ServiceContext( + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new PackagingService( + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new Mock().Object, + new RepositoryFactory(true), + new Mock().Object), + new Mock().Object, + new RelationService( + new Mock().Object, + new RepositoryFactory(true), + new Mock().Object)), + false); + Assert.Pass(); + } + + [Test] + public void Can_Assign_App_Context_Singleton() + { + var appCtx = new ApplicationContext(false); + var result = ApplicationContext.EnsureContext(appCtx, true); + Assert.AreEqual(appCtx, result); + } + + [Test] + public void Does_Not_Overwrite_App_Context_Singleton() + { + ApplicationContext.EnsureContext(new ApplicationContext(false), true); + var appCtx = new ApplicationContext(false); + var result = ApplicationContext.EnsureContext(appCtx, false); + Assert.AreNotEqual(appCtx, result); + } + + [NUnit.Framework.Ignore("Need to fix more stuff up, this is ignore because an exception occurs because it wants to ensure we have a resolver initialized - need to make that process better for testability")] + [Test] + public void Can_Get_Umbraco_Context() + { + var appCtx = new ApplicationContext(false); + ApplicationContext.EnsureContext(appCtx, true); + + var umbCtx = UmbracoContext.EnsureContext( + new Mock().Object, + appCtx, + true); + + Assert.AreEqual(umbCtx, UmbracoContext.Current); + } + + } +} diff --git a/src/Umbraco.Tests/Auditing/AuditTests.cs b/src/Umbraco.Tests/Persistence/Auditing/AuditTests.cs similarity index 92% rename from src/Umbraco.Tests/Auditing/AuditTests.cs rename to src/Umbraco.Tests/Persistence/Auditing/AuditTests.cs index ff5e6bb164..68e7bcb003 100644 --- a/src/Umbraco.Tests/Auditing/AuditTests.cs +++ b/src/Umbraco.Tests/Persistence/Auditing/AuditTests.cs @@ -1,35 +1,35 @@ -using System.Linq; -using NUnit.Framework; -using Umbraco.Core.Auditing; -using Umbraco.Core.Models.Rdbms; -using Umbraco.Tests.TestHelpers; - -namespace Umbraco.Tests.Auditing -{ - [TestFixture] - public class AuditTests : BaseDatabaseFactoryTest - { - [SetUp] - public override void Initialize() - { - base.Initialize(); - } - - [Test] - public void Can_Add_Audit_Entry() - { - Audit.Add(AuditTypes.System, "This is a System audit trail", 0, -1); - - var dtos = DatabaseContext.Database.Fetch("WHERE id > -1"); - - Assert.That(dtos.Any(), Is.True); - Assert.That(dtos.First().Comment, Is.EqualTo("This is a System audit trail")); - } - - [TearDown] - public override void TearDown() - { - base.TearDown(); - } - } +using System.Linq; +using NUnit.Framework; +using Umbraco.Core.Auditing; +using Umbraco.Core.Models.Rdbms; +using Umbraco.Tests.TestHelpers; + +namespace Umbraco.Tests.Persistence.Auditing +{ + [TestFixture] + public class AuditTests : BaseDatabaseFactoryTest + { + [SetUp] + public override void Initialize() + { + base.Initialize(); + } + + [Test] + public void Can_Add_Audit_Entry() + { + Audit.Add(AuditTypes.System, "This is a System audit trail", 0, -1); + + var dtos = DatabaseContext.Database.Fetch("WHERE id > -1"); + + Assert.That(dtos.Any(), Is.True); + Assert.That(dtos.First().Comment, Is.EqualTo("This is a System audit trail")); + } + + [TearDown] + public override void TearDown() + { + base.TearDown(); + } + } } \ No newline at end of file diff --git a/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs b/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs index 2b00ea75cf..4b13acf7ee 100644 --- a/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs +++ b/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs @@ -3,8 +3,8 @@ using System.Web.Routing; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Models; -using Umbraco.Tests.Stubs; using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Web; using Umbraco.Web.Models; using Umbraco.Web.Mvc; diff --git a/src/Umbraco.Tests/Routing/uQueryGetNodeIdByUrlTests.cs b/src/Umbraco.Tests/Routing/uQueryGetNodeIdByUrlTests.cs index 0798da0770..375964c00d 100644 --- a/src/Umbraco.Tests/Routing/uQueryGetNodeIdByUrlTests.cs +++ b/src/Umbraco.Tests/Routing/uQueryGetNodeIdByUrlTests.cs @@ -1,7 +1,7 @@ using Moq; using NUnit.Framework; -using Umbraco.Tests.Stubs; using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Web.Routing; using umbraco.BusinessLogic; using umbraco.cms.businesslogic.template; diff --git a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs index 1a259e4424..6379649d3d 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseDatabaseFactoryTest.cs @@ -21,7 +21,6 @@ using Umbraco.Core.Persistence.UnitOfWork; using Umbraco.Core.PropertyEditors; using Umbraco.Core.Publishing; using Umbraco.Core.Services; -using Umbraco.Tests.Stubs; using Umbraco.Web; using Umbraco.Web.PublishedCache; using Umbraco.Web.PublishedCache.XmlPublishedCache; diff --git a/src/Umbraco.Tests/TestHelpers/BaseRoutingTest.cs b/src/Umbraco.Tests/TestHelpers/BaseRoutingTest.cs index ab787bf105..d55a437976 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseRoutingTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseRoutingTest.cs @@ -4,7 +4,7 @@ using System.Web.Routing; using NUnit.Framework; using Umbraco.Core.Configuration; using Umbraco.Core.Models; -using Umbraco.Tests.Stubs; +using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Web; using Umbraco.Web.PublishedCache.XmlPublishedCache; using Umbraco.Web.Routing; diff --git a/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs b/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs index 95e89468c3..a635a42269 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs @@ -12,7 +12,6 @@ using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.UnitOfWork; using Umbraco.Core.Publishing; using Umbraco.Core.Services; -using Umbraco.Tests.Stubs; using Umbraco.Web; using Umbraco.Web.Routing; using umbraco.BusinessLogic; diff --git a/src/Umbraco.Tests/Stubs/FakeLastChanceFinder.cs b/src/Umbraco.Tests/TestHelpers/Stubs/FakeLastChanceFinder.cs similarity index 81% rename from src/Umbraco.Tests/Stubs/FakeLastChanceFinder.cs rename to src/Umbraco.Tests/TestHelpers/Stubs/FakeLastChanceFinder.cs index 9090909baa..c9f5dbe024 100644 --- a/src/Umbraco.Tests/Stubs/FakeLastChanceFinder.cs +++ b/src/Umbraco.Tests/TestHelpers/Stubs/FakeLastChanceFinder.cs @@ -1,12 +1,12 @@ -using Umbraco.Web.Routing; - -namespace Umbraco.Tests.Stubs -{ - internal class FakeLastChanceFinder : IContentFinder - { - public bool TryFindContent(PublishedContentRequest docRequest) - { - return false; - } - } +using Umbraco.Web.Routing; + +namespace Umbraco.Tests.TestHelpers.Stubs +{ + internal class FakeLastChanceFinder : IContentFinder + { + public bool TryFindContent(PublishedContentRequest docRequest) + { + return false; + } + } } \ No newline at end of file diff --git a/src/Umbraco.Tests/Stubs/TestControllerFactory.cs b/src/Umbraco.Tests/TestHelpers/Stubs/TestControllerFactory.cs similarity index 94% rename from src/Umbraco.Tests/Stubs/TestControllerFactory.cs rename to src/Umbraco.Tests/TestHelpers/Stubs/TestControllerFactory.cs index 872093f896..5796ee5bc3 100644 --- a/src/Umbraco.Tests/Stubs/TestControllerFactory.cs +++ b/src/Umbraco.Tests/TestHelpers/Stubs/TestControllerFactory.cs @@ -1,40 +1,40 @@ -using System; -using System.Linq; -using System.Reflection; -using System.Web.Mvc; -using System.Web.Routing; -using System.Web.SessionState; -using Umbraco.Core; - -namespace Umbraco.Tests.Stubs -{ - /// - /// Used in place of the UmbracoControllerFactory which relies on BuildManager which throws exceptions in a unit test context - /// - internal class TestControllerFactory : IControllerFactory - { - - public IController CreateController(RequestContext requestContext, string controllerName) - { - var types = TypeFinder.FindClassesOfType(new[] { Assembly.GetExecutingAssembly() }); - - var controllerTypes = types.Where(x => x.Name.Equals(controllerName + "Controller", StringComparison.InvariantCultureIgnoreCase)); - var t = controllerTypes.SingleOrDefault(); - - if (t == null) - return null; - - return Activator.CreateInstance(t) as IController; - } - - public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName) - { - return SessionStateBehavior.Disabled; - } - - public void ReleaseController(IController controller) - { - controller.DisposeIfDisposable(); - } - } +using System; +using System.Linq; +using System.Reflection; +using System.Web.Mvc; +using System.Web.Routing; +using System.Web.SessionState; +using Umbraco.Core; + +namespace Umbraco.Tests.TestHelpers.Stubs +{ + /// + /// Used in place of the UmbracoControllerFactory which relies on BuildManager which throws exceptions in a unit test context + /// + internal class TestControllerFactory : IControllerFactory + { + + public IController CreateController(RequestContext requestContext, string controllerName) + { + var types = TypeFinder.FindClassesOfType(new[] { Assembly.GetExecutingAssembly() }); + + var controllerTypes = types.Where(x => x.Name.Equals(controllerName + "Controller", StringComparison.InvariantCultureIgnoreCase)); + var t = controllerTypes.SingleOrDefault(); + + if (t == null) + return null; + + return Activator.CreateInstance(t) as IController; + } + + public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName) + { + return SessionStateBehavior.Disabled; + } + + public void ReleaseController(IController controller) + { + controller.DisposeIfDisposable(); + } + } } \ No newline at end of file diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index a2df401ad3..96f5dfe61f 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -164,7 +164,8 @@ - + + @@ -422,7 +423,7 @@ - + @@ -459,7 +460,7 @@ - +