From f16b8e705aac6d23ef123b7466d61b179aed3dc5 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 8 Dec 2020 10:20:03 +1100 Subject: [PATCH] reduce dependencies in PublishedSnapshotService, more linting --- .../IPublishedSnapshotService.cs | 3 +- .../PublishedSnapshotService.cs | 7 +- .../Repositories/RelationRepositoryTest.cs | 158 +++++++++--------- .../Objects/TestUmbracoContextFactory.cs | 4 +- .../Controllers/SurfaceControllerTests.cs | 14 +- .../UmbracoContext/UmbracoContext.cs | 10 +- .../UmbracoContext/UmbracoContextFactory.cs | 16 +- 7 files changed, 101 insertions(+), 111 deletions(-) diff --git a/src/Umbraco.Core/PublishedCache/IPublishedSnapshotService.cs b/src/Umbraco.Core/PublishedCache/IPublishedSnapshotService.cs index b23c8ae10f..68b2367ce0 100644 --- a/src/Umbraco.Core/PublishedCache/IPublishedSnapshotService.cs +++ b/src/Umbraco.Core/PublishedCache/IPublishedSnapshotService.cs @@ -1,10 +1,11 @@ -using System; +using System; using System.Collections.Generic; using Umbraco.Core.Models.Membership; using Umbraco.Web.Cache; namespace Umbraco.Web.PublishedCache { + /// /// Creates and manages instances. /// diff --git a/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotService.cs b/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotService.cs index 2b80b32b71..97e3df16a6 100644 --- a/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotService.cs +++ b/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotService.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -81,8 +81,6 @@ namespace Umbraco.Web.PublishedCache.NuCache #region Constructors - //private static int _singletonCheck; - public PublishedSnapshotService( PublishedSnapshotServiceOptions options, IMainDom mainDom, @@ -110,9 +108,6 @@ namespace Umbraco.Web.PublishedCache.NuCache IOptions config) : base(publishedSnapshotAccessor, variationContextAccessor) { - //if (Interlocked.Increment(ref _singletonCheck) > 1) - // throw new Exception("Singleton must be instantiated only once!"); - _options = options; _mainDom = mainDom; _lifeTime = lifeTime; diff --git a/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/RelationRepositoryTest.cs b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/RelationRepositoryTest.cs index 56bacb93a9..26efdf325a 100644 --- a/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/RelationRepositoryTest.cs +++ b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/RelationRepositoryTest.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using Microsoft.Extensions.Logging; @@ -30,15 +30,21 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor private Content _subpage2; private Relation _relation; private Relation _relation2; + private IContentTypeService ContentTypeService => GetRequiredService(); + private IContentService ContentService => GetRequiredService(); private IMediaTypeService MediaTypeService => GetRequiredService(); + private IMediaService MediaService => GetRequiredService(); private IMemberTypeService MemberTypeService => GetRequiredService(); - private IMemberService MemberService => GetRequiredService(); + + private IMemberService GetMemberService() => GetRequiredService(); + private IRelationService RelationService => GetRequiredService(); + private IFileService FileService => GetRequiredService(); [SetUp] @@ -57,13 +63,12 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor public void Can_Perform_Add_On_RelationRepository() { // Arrange - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { - RelationTypeRepository repositoryType; - var repository = CreateRepository(ScopeProvider, out repositoryType); + RelationRepository repository = CreateRepository(ScopeProvider, out RelationTypeRepository repositoryType); // Act - var relationType = repositoryType.Get(1); + IRelationType relationType = repositoryType.Get(1); var relation = new Relation(_textpage.Id, _subpage.Id, relationType); repository.Save(relation); @@ -78,18 +83,17 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor public void Can_Perform_Update_On_RelationRepository() { // Arrange - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { - RelationTypeRepository repositoryType; - var repository = CreateRepository(ScopeProvider, out repositoryType); + RelationRepository repository = CreateRepository(ScopeProvider, out RelationTypeRepository repositoryType); // Act - var relation = repository.Get(1); + IRelation relation = repository.Get(1); relation.Comment = "This relation has been updated"; repository.Save(relation); - var relationUpdated = repository.Get(1); + IRelation relationUpdated = repository.Get(1); // Assert Assert.That(relationUpdated, Is.Not.Null); @@ -102,13 +106,12 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor public void Can_Perform_Delete_On_RelationRepository() { // Arrange - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { - RelationTypeRepository repositoryType; - var repository = CreateRepository(ScopeProvider, out repositoryType); + RelationRepository repository = CreateRepository(ScopeProvider, out RelationTypeRepository repositoryType); // Act - var relation = repository.Get(2); + IRelation relation = repository.Get(2); repository.Delete(relation); @@ -123,13 +126,12 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor public void Can_Perform_Get_On_RelationRepository() { // Arrange - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { - RelationTypeRepository repositoryType; - var repository = CreateRepository(ScopeProvider, out repositoryType); + RelationRepository repository = CreateRepository(ScopeProvider, out RelationTypeRepository repositoryType); // Act - var relation = repository.Get(1); + IRelation relation = repository.Get(1); // Assert Assert.That(relation, Is.Not.Null); @@ -144,13 +146,12 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor public void Can_Perform_GetAll_On_RelationRepository() { // Arrange - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { - RelationTypeRepository repositoryType; - var repository = CreateRepository(ScopeProvider, out repositoryType); + RelationRepository repository = CreateRepository(ScopeProvider, out RelationTypeRepository repositoryType); // Act - var relations = repository.GetMany(); + IEnumerable relations = repository.GetMany(); // Assert Assert.That(relations, Is.Not.Null); @@ -164,13 +165,12 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor public void Can_Perform_GetAll_With_Params_On_RelationRepository() { // Arrange - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { - RelationTypeRepository repositoryType; - var repository = CreateRepository(ScopeProvider, out repositoryType); + RelationRepository repository = CreateRepository(ScopeProvider, out RelationTypeRepository repositoryType); // Act - var relations = repository.GetMany(1, 2); + IEnumerable relations = repository.GetMany(1, 2); // Assert Assert.That(relations, Is.Not.Null); @@ -183,18 +183,18 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor [Test] public void Get_Paged_Parent_Entities_By_Child_Id() { - CreateTestDataForPagingTests(out var createdContent, out var createdMembers, out var createdMedia); + CreateTestDataForPagingTests(out List createdContent, out List createdMembers, out List createdMedia); - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { - var repository = CreateRepository(ScopeProvider, out var relationTypeRepository); + RelationRepository repository = CreateRepository(ScopeProvider, out var relationTypeRepository); // Get parent entities for child id var parents = repository.GetPagedParentEntitiesByChildId(createdMedia[0].Id, 0, 11, out var totalRecords).ToList(); Assert.AreEqual(6, totalRecords); Assert.AreEqual(6, parents.Count); - //add the next page + // add the next page parents.AddRange(repository.GetPagedParentEntitiesByChildId(createdMedia[0].Id, 1, 11, out totalRecords)); Assert.AreEqual(6, totalRecords); Assert.AreEqual(6, parents.Count); @@ -207,7 +207,7 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor Assert.AreEqual(0, mediaEntities.Count); Assert.AreEqual(3, memberEntities.Count); - //only of a certain type + // only of a certain type parents.AddRange(repository.GetPagedParentEntitiesByChildId(createdMedia[0].Id, 0, 100, out totalRecords, UmbracoObjectTypes.Document.GetGuid())); Assert.AreEqual(3, totalRecords); @@ -222,17 +222,17 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor [Test] public void Get_Paged_Parent_Child_Entities_With_Same_Entity_Relation() { - //Create a media item and create a relationship between itself (parent -> child) - var imageType = MediaTypeBuilder.CreateImageMediaType("myImage"); + // Create a media item and create a relationship between itself (parent -> child) + MediaType imageType = MediaTypeBuilder.CreateImageMediaType("myImage"); MediaTypeService.Save(imageType); - var media = MediaBuilder.CreateMediaImage(imageType, -1); + Media media = MediaBuilder.CreateMediaImage(imageType, -1); MediaService.Save(media); - var relType = RelationService.GetRelationTypeByAlias(Constants.Conventions.RelationTypes.RelatedMediaAlias); + IRelationType relType = RelationService.GetRelationTypeByAlias(Constants.Conventions.RelationTypes.RelatedMediaAlias); RelationService.Relate(media.Id, media.Id, relType); - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { - var repository = CreateRepository(ScopeProvider, out var relationTypeRepository); + RelationRepository repository = CreateRepository(ScopeProvider, out var relationTypeRepository); // Get parent entities for child id var parents = repository.GetPagedParentEntitiesByChildId(media.Id, 0, 10, out var totalRecords).ToList(); @@ -251,16 +251,16 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor { CreateTestDataForPagingTests(out var createdContent, out var createdMembers, out _); - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { - var repository = CreateRepository(ScopeProvider, out _); + RelationRepository repository = CreateRepository(ScopeProvider, out _); // Get parent entities for child id var parents = repository.GetPagedChildEntitiesByParentId(createdContent[0].Id, 0, 6, out var totalRecords).ToList(); Assert.AreEqual(3, totalRecords); Assert.AreEqual(3, parents.Count); - //add the next page + // add the next page parents.AddRange(repository.GetPagedChildEntitiesByParentId(createdContent[0].Id, 1, 6, out totalRecords)); Assert.AreEqual(3, totalRecords); Assert.AreEqual(3, parents.Count); @@ -273,7 +273,7 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor Assert.AreEqual(3, mediaEntities.Count); Assert.AreEqual(0, memberEntities.Count); - //only of a certain type + // only of a certain type parents.AddRange(repository.GetPagedChildEntitiesByParentId(createdContent[0].Id, 0, 100, out totalRecords, UmbracoObjectTypes.Media.GetGuid())); Assert.AreEqual(3, totalRecords); @@ -287,9 +287,9 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor private void CreateTestDataForPagingTests(out List createdContent, out List createdMembers, out List createdMedia) { - //Create content + // Create content createdContent = new List(); - var contentType = ContentTypeBuilder.CreateBasicContentType("blah"); + ContentType contentType = ContentTypeBuilder.CreateBasicContentType("blah"); ContentTypeService.Save(contentType); for (int i = 0; i < 3; i++) { @@ -298,33 +298,42 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor createdContent.Add(c1); } - //Create media + // Create media createdMedia = new List(); - var imageType = MediaTypeBuilder.CreateImageMediaType("myImage"); + MediaType imageType = MediaTypeBuilder.CreateImageMediaType("myImage"); MediaTypeService.Save(imageType); for (int i = 0; i < 3; i++) { - var c1 = MediaBuilder.CreateMediaImage(imageType, -1); + Media c1 = MediaBuilder.CreateMediaImage(imageType, -1); MediaService.Save(c1); createdMedia.Add(c1); } // Create members - var memberType = MemberTypeBuilder.CreateSimpleMemberType("simple"); + MemberType memberType = MemberTypeBuilder.CreateSimpleMemberType("simple"); MemberTypeService.Save(memberType); createdMembers = MemberBuilder.CreateSimpleMembers(memberType, 3).ToList(); - MemberService.Save(createdMembers); + GetMemberService().Save(createdMembers); - var relType = RelationService.GetRelationTypeByAlias(Constants.Conventions.RelationTypes.RelatedMediaAlias); + IRelationType relType = RelationService.GetRelationTypeByAlias(Constants.Conventions.RelationTypes.RelatedMediaAlias); // Relate content to media - foreach (var content in createdContent) - foreach (var media in createdMedia) + foreach (IContent content in createdContent) + { + foreach (IMedia media in createdMedia) + { RelationService.Relate(content.Id, media.Id, relType); + } + } + // Relate members to media - foreach (var member in createdMembers) - foreach (var media in createdMedia) + foreach (IMember member in createdMembers) + { + foreach (IMedia media in createdMedia) + { RelationService.Relate(member.Id, media.Id, relType); + } + } } [Test] @@ -333,8 +342,7 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor // Arrange using (var scope = ScopeProvider.CreateScope()) { - RelationTypeRepository repositoryType; - var repository = CreateRepository(ScopeProvider, out repositoryType); + var repository = CreateRepository(ScopeProvider, out RelationTypeRepository repositoryType); // Act var exists = repository.Exists(2); @@ -352,8 +360,7 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor // Arrange using (var scope = ScopeProvider.CreateScope()) { - RelationTypeRepository repositoryType; - var repository = CreateRepository(ScopeProvider, out repositoryType); + var repository = CreateRepository(ScopeProvider, out RelationTypeRepository repositoryType); // Act var query = scope.SqlContext.Query().Where(x => x.ParentId == _textpage.Id); @@ -368,14 +375,13 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor public void Can_Perform_GetByQuery_On_RelationRepository() { // Arrange - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { - RelationTypeRepository repositoryType; - var repository = CreateRepository(ScopeProvider, out repositoryType); + RelationRepository repository = CreateRepository(ScopeProvider, out RelationTypeRepository repositoryType); // Act - var query = scope.SqlContext.Query().Where(x => x.RelationTypeId == _relateContent.Id); - var relations = repository.Get(query); + global::Umbraco.Core.Persistence.Querying.IQuery query = scope.SqlContext.Query().Where(x => x.RelationTypeId == _relateContent.Id); + IEnumerable relations = repository.Get(query); // Assert Assert.That(relations, Is.Not.Null); @@ -389,12 +395,11 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor public void Can_Delete_Content_And_Verify_Relation_Is_Removed() { // Arrange - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { - RelationTypeRepository repositoryType; - var repository = CreateRepository(ScopeProvider, out repositoryType); + RelationRepository repository = CreateRepository(ScopeProvider, out RelationTypeRepository repositoryType); - var content = ContentService.GetById(_subpage.Id); + IContent content = ContentService.GetById(_subpage.Id); ContentService.Delete(content, 0); // Act @@ -410,17 +415,20 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor public void CreateTestData() { _relateContent = new RelationType( - "Relate Content on Copy", "relateContentOnCopy", true, + "Relate Content on Copy", + "relateContentOnCopy", + true, Constants.ObjectTypes.Document, new Guid("C66BA18E-EAF3-4CFF-8A22-41B16D66A972")); - _relateContentType = new RelationType("Relate ContentType on Copy", + _relateContentType = new RelationType( + "Relate ContentType on Copy", "relateContentTypeOnCopy", true, Constants.ObjectTypes.DocumentType, new Guid("A2CB7800-F571-4787-9638-BC48539A0EFB")); - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { var accessor = (IScopeAccessor)ScopeProvider; var relationTypeRepository = new RelationTypeRepository(accessor, AppCaches.Disabled, Mock.Of>()); @@ -430,22 +438,22 @@ namespace Umbraco.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositor relationTypeRepository.Save(_relateContent); relationTypeRepository.Save(_relateContentType); - var template = TemplateBuilder.CreateTextPageTemplate(); + Template template = TemplateBuilder.CreateTextPageTemplate(); FileService.SaveTemplate(template); - //Create and Save ContentType "umbTextpage" -> (NodeDto.NodeIdSeed) + // Create and Save ContentType "umbTextpage" -> (NodeDto.NodeIdSeed) _contentType = ContentTypeBuilder.CreateSimpleContentType("umbTextpage", "Textpage", defaultTemplateId: template.Id); ContentTypeService.Save(_contentType); - //Create and Save Content "Homepage" based on "umbTextpage" -> (NodeDto.NodeIdSeed + 1) + // Create and Save Content "Homepage" based on "umbTextpage" -> (NodeDto.NodeIdSeed + 1) _textpage = ContentBuilder.CreateSimpleContent(_contentType); ContentService.Save(_textpage, 0); - //Create and Save Content "Text Page 1" based on "umbTextpage" -> (NodeDto.NodeIdSeed + 2) + // Create and Save Content "Text Page 1" based on "umbTextpage" -> (NodeDto.NodeIdSeed + 2) _subpage = ContentBuilder.CreateSimpleContent(_contentType, "Text Page 1", _textpage.Id); ContentService.Save(_subpage, 0); - //Create and Save Content "Text Page 1" based on "umbTextpage" -> (NodeDto.NodeIdSeed + 3) + // Create and Save Content "Text Page 1" based on "umbTextpage" -> (NodeDto.NodeIdSeed + 3) _subpage2 = ContentBuilder.CreateSimpleContent(_contentType, "Text Page 2", _textpage.Id); ContentService.Save(_subpage2, 0); diff --git a/src/Umbraco.Tests.UnitTests/TestHelpers/Objects/TestUmbracoContextFactory.cs b/src/Umbraco.Tests.UnitTests/TestHelpers/Objects/TestUmbracoContextFactory.cs index 6853a02314..7889d49192 100644 --- a/src/Umbraco.Tests.UnitTests/TestHelpers/Objects/TestUmbracoContextFactory.cs +++ b/src/Umbraco.Tests.UnitTests/TestHelpers/Objects/TestUmbracoContextFactory.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using Moq; using Umbraco.Core.Configuration.Models; @@ -48,10 +48,8 @@ namespace Umbraco.Tests.UnitTests.TestHelpers.Objects new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), Options.Create(globalSettings), - Mock.Of(), hostingEnvironment, new UriUtility(hostingEnvironment), - httpContextAccessor, new AspNetCoreCookieManager(httpContextAccessor), Mock.Of(), backofficeSecurityAccessorMock.Object diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Web.Website/Controllers/SurfaceControllerTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Web.Website/Controllers/SurfaceControllerTests.cs index e9fef92a3c..82bd6719d4 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Web.Website/Controllers/SurfaceControllerTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Web.Website/Controllers/SurfaceControllerTests.cs @@ -1,4 +1,4 @@ -using System.Threading.Tasks; +using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Routing; @@ -38,7 +38,6 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Web.Website.Controllers [Test] public void Can_Construct_And_Get_Result() { - var httpContextAccessor = Mock.Of(); var hostingEnvironment = Mock.Of(); var backofficeSecurityAccessor = Mock.Of(); Mock.Get(backofficeSecurityAccessor).Setup(x => x.BackOfficeSecurity).Returns(Mock.Of()); @@ -50,10 +49,8 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Web.Website.Controllers new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), Options.Create(globalSettings), - Mock.Of(), hostingEnvironment, new UriUtility(hostingEnvironment), - httpContextAccessor, Mock.Of(), Mock.Of(), backofficeSecurityAccessor); @@ -74,7 +71,6 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Web.Website.Controllers public void Umbraco_Context_Not_Null() { var globalSettings = new GlobalSettings(); - var httpContextAccessor = Mock.Of(); var hostingEnvironment = Mock.Of(); var backofficeSecurityAccessor = Mock.Of(); Mock.Get(backofficeSecurityAccessor).Setup(x => x.BackOfficeSecurity).Returns(Mock.Of()); @@ -84,10 +80,8 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Web.Website.Controllers new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), Options.Create(globalSettings), - Mock.Of(), hostingEnvironment, new UriUtility(hostingEnvironment), - httpContextAccessor, Mock.Of(), Mock.Of(), backofficeSecurityAccessor); @@ -112,7 +106,6 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Web.Website.Controllers var backofficeSecurityAccessor = Mock.Of(); Mock.Get(backofficeSecurityAccessor).Setup(x => x.BackOfficeSecurity).Returns(Mock.Of()); var publishedSnapshotService = new Mock(); - var httpContextAccessor = Mock.Of(); var hostingEnvironment = Mock.Of(); var globalSettings = new GlobalSettings(); @@ -122,10 +115,8 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Web.Website.Controllers new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), Options.Create(globalSettings), - Mock.Of(), hostingEnvironment, new UriUtility(hostingEnvironment), - httpContextAccessor, Mock.Of(), Mock.Of(), backofficeSecurityAccessor); @@ -150,7 +141,6 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Web.Website.Controllers public void Mock_Current_Page() { var globalSettings = new GlobalSettings(); - var httpContextAccessor = Mock.Of(); var hostingEnvironment = Mock.Of(); var backofficeSecurityAccessor = Mock.Of(); Mock.Get(backofficeSecurityAccessor).Setup(x => x.BackOfficeSecurity).Returns(Mock.Of()); @@ -160,10 +150,8 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Web.Website.Controllers new TestVariationContextAccessor(), new TestDefaultCultureAccessor(), Options.Create(globalSettings), - Mock.Of(), hostingEnvironment, new UriUtility(hostingEnvironment), - httpContextAccessor, Mock.Of(), Mock.Of(), backofficeSecurityAccessor); diff --git a/src/Umbraco.Web.Common/UmbracoContext/UmbracoContext.cs b/src/Umbraco.Web.Common/UmbracoContext/UmbracoContext.cs index 56dd4b054e..97bb9ac7c4 100644 --- a/src/Umbraco.Web.Common/UmbracoContext/UmbracoContext.cs +++ b/src/Umbraco.Web.Common/UmbracoContext/UmbracoContext.cs @@ -1,4 +1,4 @@ -using System; +using System; using Umbraco.Core; using Umbraco.Core.Configuration.Models; using Umbraco.Core.Hosting; @@ -21,7 +21,7 @@ namespace Umbraco.Web private readonly Lazy _publishedSnapshot; private string _previewToken; private bool? _previewing; - private IBackOfficeSecurity _backofficeSecurity; + private readonly IBackOfficeSecurity _backofficeSecurity; // initializes a new instance of the UmbracoContext class // internal for unit tests @@ -37,7 +37,11 @@ namespace Umbraco.Web ICookieManager cookieManager, IRequestAccessor requestAccessor) { - if (publishedSnapshotService == null) throw new ArgumentNullException(nameof(publishedSnapshotService)); + if (publishedSnapshotService == null) + { + throw new ArgumentNullException(nameof(publishedSnapshotService)); + } + VariationContextAccessor = variationContextAccessor ?? throw new ArgumentNullException(nameof(variationContextAccessor)); _globalSettings = globalSettings ?? throw new ArgumentNullException(nameof(globalSettings)); diff --git a/src/Umbraco.Web.Common/UmbracoContext/UmbracoContextFactory.cs b/src/Umbraco.Web.Common/UmbracoContext/UmbracoContextFactory.cs index 553ea07a90..9dd4939c3d 100644 --- a/src/Umbraco.Web.Common/UmbracoContext/UmbracoContextFactory.cs +++ b/src/Umbraco.Web.Common/UmbracoContext/UmbracoContextFactory.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Text; using Microsoft.AspNetCore.Http; @@ -26,9 +26,7 @@ namespace Umbraco.Web private readonly IDefaultCultureAccessor _defaultCultureAccessor; private readonly GlobalSettings _globalSettings; - private readonly IUserService _userService; private readonly IHostingEnvironment _hostingEnvironment; - private readonly IHttpContextAccessor _httpContextAccessor; private readonly ICookieManager _cookieManager; private readonly IRequestAccessor _requestAccessor; private readonly IBackOfficeSecurityAccessor _backofficeSecurityAccessor; @@ -43,23 +41,19 @@ namespace Umbraco.Web IVariationContextAccessor variationContextAccessor, IDefaultCultureAccessor defaultCultureAccessor, IOptions globalSettings, - IUserService userService, IHostingEnvironment hostingEnvironment, UriUtility uriUtility, - IHttpContextAccessor httpContextAccessor, ICookieManager cookieManager, IRequestAccessor requestAccessor, - IBackOfficeSecurityAccessor backofficeSecurityAccessor) + IBackOfficeSecurityAccessor backofficeSecurityAccessor) { _umbracoContextAccessor = umbracoContextAccessor ?? throw new ArgumentNullException(nameof(umbracoContextAccessor)); _publishedSnapshotService = publishedSnapshotService ?? throw new ArgumentNullException(nameof(publishedSnapshotService)); _variationContextAccessor = variationContextAccessor ?? throw new ArgumentNullException(nameof(variationContextAccessor)); _defaultCultureAccessor = defaultCultureAccessor ?? throw new ArgumentNullException(nameof(defaultCultureAccessor)); _globalSettings = globalSettings.Value ?? throw new ArgumentNullException(nameof(globalSettings)); - _userService = userService ?? throw new ArgumentNullException(nameof(userService)); _hostingEnvironment = hostingEnvironment ?? throw new ArgumentNullException(nameof(hostingEnvironment)); _uriUtility = uriUtility ?? throw new ArgumentNullException(nameof(uriUtility)); - _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); _cookieManager = cookieManager ?? throw new ArgumentNullException(nameof(cookieManager)); _requestAccessor = requestAccessor ?? throw new ArgumentNullException(nameof(requestAccessor)); _backofficeSecurityAccessor = backofficeSecurityAccessor ?? throw new ArgumentNullException(nameof(backofficeSecurityAccessor)); @@ -92,11 +86,13 @@ namespace Umbraco.Web /// public UmbracoContextReference EnsureUmbracoContext() { - var currentUmbracoContext = _umbracoContextAccessor.UmbracoContext; + IUmbracoContext currentUmbracoContext = _umbracoContextAccessor.UmbracoContext; if (currentUmbracoContext != null) + { return new UmbracoContextReference(currentUmbracoContext, false, _umbracoContextAccessor); + } - var umbracoContext = CreateUmbracoContext(); + IUmbracoContext umbracoContext = CreateUmbracoContext(); _umbracoContextAccessor.UmbracoContext = umbracoContext; return new UmbracoContextReference(umbracoContext, true, _umbracoContextAccessor);