From 30b0f142eb6e564d6a450c0cb4586d9ff072d287 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Mon, 5 Oct 2020 21:45:33 +0200 Subject: [PATCH] Migrated various service tests into the new integration or unit tests projects as appropriate. --- .../Services/ContentServiceEventTests.cs | 111 +++++++----------- .../Services/SectionServiceTests.cs | 21 ++-- .../Services/AmbiguousEventTests.cs | 78 ------------ src/Umbraco.Tests/Umbraco.Tests.csproj | 2 - .../UmbracoContext/UmbracoContextFactory.cs | 1 - 5 files changed, 53 insertions(+), 160 deletions(-) rename src/{Umbraco.Tests => Umbraco.Tests.Integration}/Services/ContentServiceEventTests.cs (75%) delete mode 100644 src/Umbraco.Tests/Services/AmbiguousEventTests.cs diff --git a/src/Umbraco.Tests/Services/ContentServiceEventTests.cs b/src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs similarity index 75% rename from src/Umbraco.Tests/Services/ContentServiceEventTests.cs rename to src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs index c22679a820..8fcb41305b 100644 --- a/src/Umbraco.Tests/Services/ContentServiceEventTests.cs +++ b/src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs @@ -1,4 +1,5 @@ using System.Linq; +using System.Threading.Tasks; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Configuration.Models; @@ -7,7 +8,7 @@ using Umbraco.Core.Models; using Umbraco.Core.Persistence.Repositories.Implement; using Umbraco.Core.Services; using Umbraco.Core.Services.Implement; -using Umbraco.Tests.Common.Builders; +using Umbraco.Tests.Integration.Testing; using Umbraco.Tests.TestHelpers.Entities; using Umbraco.Tests.Testing; @@ -18,13 +19,18 @@ namespace Umbraco.Tests.Services PublishedRepositoryEvents = true, WithApplication = true, Logger = UmbracoTestOptions.Logger.Console)] - public class ContentServiceEventTests : TestWithSomeContentBase + public class ContentServiceEventTests : UmbracoIntegrationTest { + private IContentTypeService _contentTypeService => GetRequiredService(); + private IContentService _contentService => GetRequiredService(); + private ILocalizationService _localizationService => GetRequiredService(); + private IFileService _fileService => GetRequiredService(); + private GlobalSettings _globalSettings; - public override void SetUp() + public override async Task Setup() { - base.SetUp(); + await base.Setup(); ContentRepositoryBase.ThrowOnWarning = true; _globalSettings = new GlobalSettings(); } @@ -38,28 +44,22 @@ namespace Umbraco.Tests.Services [Test] public void Saving_Culture() { - var languageService = ServiceContext.LocalizationService; - - languageService.Save(new Language(_globalSettings, "fr-FR")); - - var contentTypeService = ServiceContext.ContentTypeService; + _localizationService.Save(new Language(_globalSettings, "fr-FR")); var contentType = MockedContentTypes.CreateTextPageContentType(); - ServiceContext.FileService.SaveTemplate(contentType.DefaultTemplate); + _fileService.SaveTemplate(contentType.DefaultTemplate); contentType.Variations = ContentVariation.Culture; foreach (var propertyType in contentType.PropertyTypes) propertyType.Variations = ContentVariation.Culture; - contentTypeService.Save(contentType); - - var contentService = ServiceContext.ContentService; + _contentTypeService.Save(contentType); IContent document = new Content("content", -1, contentType); document.SetCultureName("hello", "en-US"); document.SetCultureName("bonjour", "fr-FR"); - contentService.Save(document); + _contentService.Save(document); //re-get - dirty properties need resetting - document = contentService.GetById(document.Id); + document = _contentService.GetById(document.Id); // properties: title, bodyText, keywords, description document.SetValue("title", "title-en", "en-US"); @@ -88,7 +88,7 @@ namespace Umbraco.Tests.Services ContentService.Saved += OnSaved; try { - contentService.Save(document); + _contentService.Save(document); } finally { @@ -100,13 +100,9 @@ namespace Umbraco.Tests.Services [Test] public void Saving_Set_Value() { - var contentTypeService = ServiceContext.ContentTypeService; - var contentType = MockedContentTypes.CreateTextPageContentType(); - ServiceContext.FileService.SaveTemplate(contentType.DefaultTemplate); - contentTypeService.Save(contentType); - - var contentService = ServiceContext.ContentService; + _fileService.SaveTemplate(contentType.DefaultTemplate); + _contentTypeService.Save(contentType); IContent document = new Content("content", -1, contentType); @@ -136,44 +132,37 @@ namespace Umbraco.Tests.Services ContentService.Saved += OnSaved; try { - contentService.Save(document); + _contentService.Save(document); } finally { ContentService.Saving -= OnSaving; ContentService.Saved -= OnSaved; } - } [Test] public void Publishing_Culture() { - var languageService = ServiceContext.LocalizationService; - - languageService.Save(new Language(_globalSettings, "fr-FR")); - - var contentTypeService = ServiceContext.ContentTypeService; + _localizationService.Save(new Language(_globalSettings, "fr-FR")); var contentType = MockedContentTypes.CreateTextPageContentType(); - ServiceContext.FileService.SaveTemplate(contentType.DefaultTemplate); + _fileService.SaveTemplate(contentType.DefaultTemplate); contentType.Variations = ContentVariation.Culture; foreach (var propertyType in contentType.PropertyTypes) propertyType.Variations = ContentVariation.Culture; - contentTypeService.Save(contentType); - - var contentService = ServiceContext.ContentService; + _contentTypeService.Save(contentType); IContent document = new Content("content", -1, contentType); document.SetCultureName("hello", "en-US"); document.SetCultureName("bonjour", "fr-FR"); - contentService.Save(document); + _contentService.Save(document); Assert.IsFalse(document.IsCulturePublished("fr-FR")); Assert.IsFalse(document.IsCulturePublished("en-US")); //re-get - dirty properties need resetting - document = contentService.GetById(document.Id); + document = _contentService.GetById(document.Id); void OnPublishing(IContentService sender, ContentPublishingEventArgs e) { @@ -199,7 +188,7 @@ namespace Umbraco.Tests.Services ContentService.Published += OnPublished; try { - contentService.SaveAndPublish(document, "fr-FR"); + _contentService.SaveAndPublish(document, "fr-FR"); } finally { @@ -207,7 +196,7 @@ namespace Umbraco.Tests.Services ContentService.Published -= OnPublished; } - document = contentService.GetById(document.Id); + document = _contentService.GetById(document.Id); // ensure it works and does not throw Assert.IsTrue(document.IsCulturePublished("fr-FR")); @@ -217,13 +206,9 @@ namespace Umbraco.Tests.Services [Test] public void Publishing_Set_Value() { - var contentTypeService = ServiceContext.ContentTypeService; - var contentType = MockedContentTypes.CreateTextPageContentType(); - ServiceContext.FileService.SaveTemplate(contentType.DefaultTemplate); - contentTypeService.Save(contentType); - - var contentService = ServiceContext.ContentService; + _fileService.SaveTemplate(contentType.DefaultTemplate); + _contentTypeService.Save(contentType); IContent document = new Content("content", -1, contentType); @@ -242,21 +227,21 @@ namespace Umbraco.Tests.Services Assert.AreSame("title", document.GetValue("title")); - //we're only dealing with invariant here + // We're only dealing with invariant here. var propValue = saved.Properties["title"].Values.First(x => x.Culture == null && x.Segment == null); Assert.AreEqual("title", propValue.EditedValue); Assert.AreEqual("title", propValue.PublishedValue); } - //We are binding to Saving (not Publishing), because the Publishing event is really just used for cancelling, it should not be - //used for setting values and it won't actually work! This is because the Publishing event is raised AFTER the values on the model - //are published, but Saving is raised BEFORE. + // We are binding to Saving (not Publishing), because the Publishing event is really just used for cancelling, it should not be + // used for setting values and it won't actually work! This is because the Publishing event is raised AFTER the values on the model + // are published, but Saving is raised BEFORE. ContentService.Saving += OnSaving; ContentService.Saved += OnSaved; try { - contentService.SaveAndPublish(document); + _contentService.SaveAndPublish(document); } finally { @@ -268,19 +253,15 @@ namespace Umbraco.Tests.Services [Test] public void Publishing_Set_Mandatory_Value() { - var contentTypeService = ServiceContext.ContentTypeService; - var contentType = MockedContentTypes.CreateTextPageContentType(); var titleProperty = contentType.PropertyTypes.First(x => x.Alias == "title"); titleProperty.Mandatory = true; // make this required! - ServiceContext.FileService.SaveTemplate(contentType.DefaultTemplate); - contentTypeService.Save(contentType); - - var contentService = ServiceContext.ContentService; + _fileService.SaveTemplate(contentType.DefaultTemplate); + _contentTypeService.Save(contentType); IContent document = new Content("content", -1, contentType); - var result = contentService.SaveAndPublish(document); + var result = _contentService.SaveAndPublish(document); Assert.IsFalse(result.Success); Assert.AreEqual("title", result.InvalidProperties.First().Alias); @@ -297,13 +278,13 @@ namespace Umbraco.Tests.Services saved.SetValue("title", "title"); } - //We are binding to Saving (not Publishing), because the Publishing event is really just used for cancelling, it should not be - //used for setting values and it won't actually work! This is because the Publishing event is raised AFTER the values on the model - //are published, but Saving is raised BEFORE. + // We are binding to Saving (not Publishing), because the Publishing event is really just used for cancelling, it should not be + // used for setting values and it won't actually work! This is because the Publishing event is raised AFTER the values on the model + // are published, but Saving is raised BEFORE. ContentService.Saving += OnSaving; try { - result = contentService.SaveAndPublish(document); + result = _contentService.SaveAndPublish(document); Assert.IsTrue(result.Success); //will succeed now because we were able to specify the required value in the Saving event } finally @@ -315,20 +296,16 @@ namespace Umbraco.Tests.Services [Test] public void Unpublishing_Culture() { - var languageService = ServiceContext.LocalizationService; - - languageService.Save(new Language(_globalSettings, "fr-FR")); - - var contentTypeService = ServiceContext.ContentTypeService; + _localizationService.Save(new Language(_globalSettings, "fr-FR")); var contentType = MockedContentTypes.CreateTextPageContentType(); - ServiceContext.FileService.SaveTemplate(contentType.DefaultTemplate); + _fileService.SaveTemplate(contentType.DefaultTemplate); contentType.Variations = ContentVariation.Culture; foreach (var propertyType in contentType.PropertyTypes) propertyType.Variations = ContentVariation.Culture; - contentTypeService.Save(contentType); + _contentTypeService.Save(contentType); - var contentService = (ContentService)ServiceContext.ContentService; + var contentService = (ContentService)_contentService; IContent document = new Content("content", -1, contentType); document.SetCultureName("hello", "en-US"); diff --git a/src/Umbraco.Tests.Integration/Services/SectionServiceTests.cs b/src/Umbraco.Tests.Integration/Services/SectionServiceTests.cs index 533a66ad96..a54a1c1c59 100644 --- a/src/Umbraco.Tests.Integration/Services/SectionServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/SectionServiceTests.cs @@ -1,12 +1,9 @@ -using NUnit.Framework; -using System.Linq; +using System.Linq; using System.Threading; -using Umbraco.Core; -using Umbraco.Core.Composing; +using NUnit.Framework; using Umbraco.Core.Configuration.Models; using Umbraco.Core.Models.Membership; using Umbraco.Core.Services; -using Umbraco.Tests.Common.Builders; using Umbraco.Tests.Integration.Testing; using Umbraco.Tests.Testing; using Umbraco.Web.Services; @@ -21,8 +18,8 @@ namespace Umbraco.Tests.Services [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] public class SectionServiceTests : UmbracoIntegrationTest { - private ISectionService SectionService => GetRequiredService(); - private IUserService UserService => GetRequiredService(); + private ISectionService _sectionService => GetRequiredService(); + private IUserService _userService => GetRequiredService(); [Test] public void SectionService_Can_Get_Allowed_Sections_For_User() @@ -31,7 +28,7 @@ namespace Umbraco.Tests.Services var user = CreateTestUser(); // Act - var result = SectionService.GetAllowedSections(user.Id).ToList(); + var result = _sectionService.GetAllowedSections(user.Id).ToList(); // Assert Assert.AreEqual(3, result.Count); @@ -46,7 +43,7 @@ namespace Umbraco.Tests.Services Username = "testUser", Email = "testuser@test.com", }; - UserService.Save(user, false); + _userService.Save(user, false); var userGroupA = new UserGroup(ShortStringHelper) { @@ -56,7 +53,7 @@ namespace Umbraco.Tests.Services userGroupA.AddAllowedSection("media"); userGroupA.AddAllowedSection("settings"); // TODO: This is failing the test - UserService.Save(userGroupA, new[] { user.Id }, false); + _userService.Save(userGroupA, new[] { user.Id }, false); var userGroupB = new UserGroup(ShortStringHelper) { @@ -65,9 +62,9 @@ namespace Umbraco.Tests.Services }; userGroupB.AddAllowedSection("settings"); userGroupB.AddAllowedSection("member"); - UserService.Save(userGroupB, new[] { user.Id }, false); + _userService.Save(userGroupB, new[] { user.Id }, false); - return UserService.GetUserById(user.Id); + return _userService.GetUserById(user.Id); } } } diff --git a/src/Umbraco.Tests/Services/AmbiguousEventTests.cs b/src/Umbraco.Tests/Services/AmbiguousEventTests.cs deleted file mode 100644 index e137da1188..0000000000 --- a/src/Umbraco.Tests/Services/AmbiguousEventTests.cs +++ /dev/null @@ -1,78 +0,0 @@ -using System; -using System.Reflection; -using System.Text; -using NUnit.Framework; -using Umbraco.Core.Events; -using Umbraco.Core.Services.Implement; - -namespace Umbraco.Tests.Services -{ - [TestFixture] - public class AmbiguousEventTests - { - [Explicit] - [TestCase(typeof(ContentService))] - [TestCase(typeof(MediaService))] - public void ListAmbiguousEvents(Type serviceType) - { - var typedEventHandler = typeof(TypedEventHandler<,>); - - // get all events - var events = serviceType.GetEvents(BindingFlags.Static | BindingFlags.Public); - - string TypeName(Type type) - { - if (!type.IsGenericType) - return type.Name; - var sb = new StringBuilder(); - TypeNameSb(type, sb); - return sb.ToString(); - } - - void TypeNameSb(Type type, StringBuilder sb) - { - var name = type.Name; - var pos = name.IndexOf('`'); - name = pos > 0 ? name.Substring(0, pos) : name; - sb.Append(name); - if (!type.IsGenericType) - return; - sb.Append("<"); - var first = true; - foreach (var arg in type.GetGenericArguments()) - { - if (first) first = false; - else sb.Append(", "); - TypeNameSb(arg, sb); - } - sb.Append(">"); - } - - foreach (var e in events) - { - // only continue if this is a TypedEventHandler - if (!e.EventHandlerType.IsGenericType) continue; - var typeDef = e.EventHandlerType.GetGenericTypeDefinition(); - if (typedEventHandler != typeDef) continue; - - // get the event args type - var eventArgsType = e.EventHandlerType.GenericTypeArguments[1]; - - // try to find the event back, based upon sender type + args type - // exclude -ing (eg Saving) events, we don't deal with them in EventDefinitionBase (they always trigger) - var found = EventNameExtractor.FindEvents(serviceType, eventArgsType, EventNameExtractor.MatchIngNames); - - if (found.Length == 1) continue; - - if (found.Length == 0) - { - Console.WriteLine($"{typeof(ContentService).Name} {e.Name} {TypeName(eventArgsType)} NotFound"); - continue; - } - - Console.WriteLine($"{typeof(ContentService).Name} {e.Name} {TypeName(eventArgsType)} Ambiguous"); - Console.WriteLine("\t" + string.Join(", ", found)); - } - } - } -} \ No newline at end of file diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 197e928e1f..b65cc2ff74 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -154,8 +154,6 @@ - - diff --git a/src/Umbraco.Web.Common/UmbracoContext/UmbracoContextFactory.cs b/src/Umbraco.Web.Common/UmbracoContext/UmbracoContextFactory.cs index a31ef614cf..f50a2e2ba8 100644 --- a/src/Umbraco.Web.Common/UmbracoContext/UmbracoContextFactory.cs +++ b/src/Umbraco.Web.Common/UmbracoContext/UmbracoContextFactory.cs @@ -96,7 +96,6 @@ namespace Umbraco.Web if (currentUmbracoContext != null) return new UmbracoContextReference(currentUmbracoContext, false, _umbracoContextAccessor); - var umbracoContext = CreateUmbracoContext(); _umbracoContextAccessor.UmbracoContext = umbracoContext;