From 30b0f142eb6e564d6a450c0cb4586d9ff072d287 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Mon, 5 Oct 2020 21:45:33 +0200 Subject: [PATCH 1/8] 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; From 6736860f4bdfcc1a1a8b3e02d3332e5b3185c7de Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Mon, 5 Oct 2020 21:45:51 +0200 Subject: [PATCH 2/8] Removed duplicate depenency registration. --- .../Runtime/CoreInitialComposer.cs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/Umbraco.Infrastructure/Runtime/CoreInitialComposer.cs b/src/Umbraco.Infrastructure/Runtime/CoreInitialComposer.cs index fdd358e64a..7981d8c964 100644 --- a/src/Umbraco.Infrastructure/Runtime/CoreInitialComposer.cs +++ b/src/Umbraco.Infrastructure/Runtime/CoreInitialComposer.cs @@ -1,11 +1,12 @@ using System; using Examine; +using Microsoft.Extensions.Options; using Umbraco.Core.Cache; using Umbraco.Core.Composing; using Umbraco.Core.Composing.CompositionExtensions; using Umbraco.Core.Configuration; using Umbraco.Core.Configuration.Grid; -using Umbraco.Core.Configuration.UmbracoSettings; +using Umbraco.Core.Configuration.Models; using Umbraco.Core.Dashboards; using Umbraco.Core.Dictionary; using Umbraco.Core.Events; @@ -18,7 +19,6 @@ using Umbraco.Core.Migrations; using Umbraco.Core.Migrations.Install; using Umbraco.Core.Migrations.PostMigrations; using Umbraco.Core.Models.PublishedContent; -using Umbraco.Core.Templates; using Umbraco.Core.Persistence; using Umbraco.Core.PropertyEditors; using Umbraco.Core.PropertyEditors.Validators; @@ -29,6 +29,7 @@ using Umbraco.Core.Services; using Umbraco.Core.Services.Implement; using Umbraco.Core.Strings; using Umbraco.Core.Sync; +using Umbraco.Core.Templates; using Umbraco.Examine; using Umbraco.Infrastructure.Examine; using Umbraco.Infrastructure.Media; @@ -41,6 +42,7 @@ using Umbraco.Web.Features; using Umbraco.Web.HealthCheck; using Umbraco.Web.HealthCheck.NotificationMethods; using Umbraco.Web.Install; +using Umbraco.Web.Media; using Umbraco.Web.Media.EmbedProviders; using Umbraco.Web.Migrations.PostMigrations; using Umbraco.Web.Models.PublishedContent; @@ -55,9 +57,6 @@ using Umbraco.Web.Templates; using Umbraco.Web.Trees; using IntegerValidator = Umbraco.Core.PropertyEditors.Validators.IntegerValidator; using TextStringValueConverter = Umbraco.Core.PropertyEditors.ValueConverters.TextStringValueConverter; -using Umbraco.Core.Configuration.Models; -using Microsoft.Extensions.Options; -using Umbraco.Web.Media; namespace Umbraco.Core.Runtime { @@ -204,13 +203,6 @@ namespace Umbraco.Core.Runtime // Config manipulator composition.RegisterUnique(); - - // register the http context and umbraco context accessors - // we *should* use the HttpContextUmbracoContextAccessor, however there are cases when - // we have no http context, eg when booting Umbraco or in background threads, so instead - // let's use an hybrid accessor that can fall back to a ThreadStatic context. - composition.RegisterUnique(); - // register the umbraco context factory // composition.RegisterUnique(); composition.RegisterUnique(); @@ -353,7 +345,6 @@ namespace Umbraco.Core.Runtime return new PublishedContentQuery(umbCtx.UmbracoContext.PublishedSnapshot, factory.GetInstance(), factory.GetInstance()); }, Lifetime.Request); - composition.RegisterUnique(); // register the http context and umbraco context accessors From 30e49ac548b4cbfaf5604457368089948dfab8c6 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Tue, 6 Oct 2020 08:28:02 +0200 Subject: [PATCH 3/8] Fix after rebase. --- .../Services/ContentServiceEventTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs b/src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs index 8fcb41305b..3222c1b052 100644 --- a/src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs +++ b/src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs @@ -28,9 +28,9 @@ namespace Umbraco.Tests.Services private GlobalSettings _globalSettings; - public override async Task Setup() + public override void Setup() { - await base.Setup(); + base.Setup(); ContentRepositoryBase.ThrowOnWarning = true; _globalSettings = new GlobalSettings(); } From 1f83c610439c83435d2c5e5aa1f730ea7ef82db2 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Tue, 6 Oct 2020 09:23:15 +0200 Subject: [PATCH 4/8] Aligned service integration tests with naming convention for private variables and use of test model builders. --- .../TestHelpers/Entities/MockedUser.cs | 62 --- .../Services/AuditServiceTests.cs | 2 +- .../Services/ContentServiceEventTests.cs | 3 +- .../Services/LocalizationServiceTests.cs | 130 +++--- .../Services/MediaServiceTests.cs | 82 ++-- .../Services/PublicAccessServiceTests.cs | 54 +-- .../Services/SectionServiceTests.cs | 2 +- .../Services/TagServiceTests.cs | 50 +-- .../Services/UserServiceTests.cs | 404 ++++++++++-------- .../UserEditorAuthorizationHelperTests.cs | 67 +-- .../Services/AmbiguousEventTests.cs | 78 ++++ src/Umbraco.Tests/Umbraco.Tests.csproj | 2 +- 12 files changed, 497 insertions(+), 439 deletions(-) delete mode 100644 src/Umbraco.Tests.Common/TestHelpers/Entities/MockedUser.cs create mode 100644 src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Services/AmbiguousEventTests.cs diff --git a/src/Umbraco.Tests.Common/TestHelpers/Entities/MockedUser.cs b/src/Umbraco.Tests.Common/TestHelpers/Entities/MockedUser.cs deleted file mode 100644 index 5b77909fec..0000000000 --- a/src/Umbraco.Tests.Common/TestHelpers/Entities/MockedUser.cs +++ /dev/null @@ -1,62 +0,0 @@ -using Moq; -using System; -using System.Collections.Generic; -using Umbraco.Core.Configuration.Models; -using Umbraco.Core.Models.Membership; - -namespace Umbraco.Tests.Common.TestHelpers.Entities -{ - public static class MockedUser - { - /// - /// Returns a and ensures that the ToUserCache and FromUserCache methods are mapped correctly for - /// dealing with start node caches - /// - /// - public static Mock GetUserMock() - { - var userCache = new Dictionary(); - var userMock = new Mock(); - userMock.Setup(x => x.FromUserCache(It.IsAny())).Returns((string key) => userCache.TryGetValue(key, out var val) ? val is int[] iVal ? iVal : null : null); - userMock.Setup(x => x.ToUserCache(It.IsAny(), It.IsAny())).Callback((string key, int[] val) => userCache[key] = val); - return userMock; - } - - public static User CreateUser(string suffix = "") - { - var globalSettings = new GlobalSettings(); - var user = new User(globalSettings) - { - Language = "en", - IsApproved = true, - Name = "TestUser" + suffix, - RawPasswordValue = "testing", - IsLockedOut = false, - Email = "test" + suffix + "@test.com", - Username = "TestUser" + suffix - }; - - return user; - } - - public static IEnumerable CreateMulipleUsers(int amount, Action onCreating = null) - { - var list = new List(); - - var globalSettings = new GlobalSettings(); - for (var i = 0; i < amount; i++) - { - var name = "Member No-" + i; - var user = new User(globalSettings, name, "test" + i + "@test.com", "test" + i, "test" + i); - - onCreating?.Invoke(i, user); - - user.ResetDirtyProperties(false); - - list.Add(user); - } - - return list; - } - } -} diff --git a/src/Umbraco.Tests.Integration/Services/AuditServiceTests.cs b/src/Umbraco.Tests.Integration/Services/AuditServiceTests.cs index 9a0ba54082..474229372c 100644 --- a/src/Umbraco.Tests.Integration/Services/AuditServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/AuditServiceTests.cs @@ -9,7 +9,7 @@ using Umbraco.Tests.Common.Builders; using Umbraco.Tests.Integration.Testing; using Umbraco.Tests.Testing; -namespace Umbraco.Tests.Services +namespace Umbraco.Tests.Integration.Services { [TestFixture] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] diff --git a/src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs b/src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs index 3222c1b052..e2b6d20d82 100644 --- a/src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs +++ b/src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs @@ -1,5 +1,4 @@ using System.Linq; -using System.Threading.Tasks; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Configuration.Models; @@ -12,7 +11,7 @@ using Umbraco.Tests.Integration.Testing; using Umbraco.Tests.TestHelpers.Entities; using Umbraco.Tests.Testing; -namespace Umbraco.Tests.Services +namespace Umbraco.Tests.Integration.Services { [TestFixture] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, diff --git a/src/Umbraco.Tests.Integration/Services/LocalizationServiceTests.cs b/src/Umbraco.Tests.Integration/Services/LocalizationServiceTests.cs index 83d3476324..5c17666619 100644 --- a/src/Umbraco.Tests.Integration/Services/LocalizationServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/LocalizationServiceTests.cs @@ -31,7 +31,7 @@ namespace Umbraco.Tests.Integration.Services private int _englishLangId; private GlobalSettings _globalSettings; - private ILocalizationService LocalizationService => GetRequiredService(); + private ILocalizationService _localizationService => GetRequiredService(); [SetUp] public void SetUp() @@ -43,7 +43,7 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_Get_Root_Dictionary_Items() { - var rootItems = LocalizationService.GetRootDictionaryItems(); + var rootItems = _localizationService.GetRootDictionaryItems(); Assert.NotNull(rootItems); Assert.IsTrue(rootItems.Any()); @@ -52,14 +52,14 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_Determint_If_DictionaryItem_Exists() { - var exists = LocalizationService.DictionaryItemExists("Parent"); + var exists = _localizationService.DictionaryItemExists("Parent"); Assert.IsTrue(exists); } [Test] public void Can_Get_All_Languages() { - var languages = LocalizationService.GetAllLanguages(); + var languages = _localizationService.GetAllLanguages(); Assert.NotNull(languages); Assert.IsTrue(languages.Any()); Assert.That(languages.Count(), Is.EqualTo(3)); @@ -68,37 +68,37 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_Get_Dictionary_Item_By_Int_Id() { - var parentItem = LocalizationService.GetDictionaryItemById(_parentItemIntId); + var parentItem = _localizationService.GetDictionaryItemById(_parentItemIntId); Assert.NotNull(parentItem); - var childItem = LocalizationService.GetDictionaryItemById(_childItemIntId); + var childItem = _localizationService.GetDictionaryItemById(_childItemIntId); Assert.NotNull(childItem); } [Test] public void Can_Get_Dictionary_Item_By_Guid_Id() { - var parentItem = LocalizationService.GetDictionaryItemById(_parentItemGuidId); + var parentItem = _localizationService.GetDictionaryItemById(_parentItemGuidId); Assert.NotNull(parentItem); - var childItem = LocalizationService.GetDictionaryItemById(_childItemGuidId); + var childItem = _localizationService.GetDictionaryItemById(_childItemGuidId); Assert.NotNull(childItem); } [Test] public void Can_Get_Dictionary_Item_By_Key() { - var parentItem = LocalizationService.GetDictionaryItemByKey("Parent"); + var parentItem = _localizationService.GetDictionaryItemByKey("Parent"); Assert.NotNull(parentItem); - var childItem = LocalizationService.GetDictionaryItemByKey("Child"); + var childItem = _localizationService.GetDictionaryItemByKey("Child"); Assert.NotNull(childItem); } [Test] public void Can_Get_Dictionary_Item_Children() { - var item = LocalizationService.GetDictionaryItemChildren(_parentItemGuidId); + var item = _localizationService.GetDictionaryItemChildren(_parentItemGuidId); Assert.NotNull(item); Assert.That(item.Count(), Is.EqualTo(1)); @@ -114,8 +114,8 @@ namespace Umbraco.Tests.Integration.Services { using (var scope = ScopeProvider.CreateScope()) { - var en = LocalizationService.GetLanguageById(_englishLangId); - var dk = LocalizationService.GetLanguageById(_danishLangId); + var en = _localizationService.GetLanguageById(_englishLangId); + var dk = _localizationService.GetLanguageById(_danishLangId); var currParentId = _childItemGuidId; for (var i = 0; i < 25; i++) @@ -137,8 +137,8 @@ namespace Umbraco.Tests.Integration.Services new DictionaryTranslation(dk, "BørnVærdi2 " + i) } }; - LocalizationService.Save(desc1); - LocalizationService.Save(desc2); + _localizationService.Save(desc1); + _localizationService.Save(desc2); currParentId = desc1.Key; } @@ -146,7 +146,7 @@ namespace Umbraco.Tests.Integration.Services scope.Database.AsUmbracoDatabase().EnableSqlTrace = true; scope.Database.AsUmbracoDatabase().EnableSqlCount = true; - var items = LocalizationService.GetDictionaryItemDescendants(_parentItemGuidId).ToArray(); + var items = _localizationService.GetDictionaryItemDescendants(_parentItemGuidId).ToArray(); Debug.WriteLine("SQL CALLS: " + scope.Database.AsUmbracoDatabase().SqlCount); @@ -159,8 +159,8 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_GetLanguageById() { - var danish = LocalizationService.GetLanguageById(_danishLangId); - var english = LocalizationService.GetLanguageById(_englishLangId); + var danish = _localizationService.GetLanguageById(_danishLangId); + var english = _localizationService.GetLanguageById(_englishLangId); Assert.NotNull(danish); Assert.NotNull(english); } @@ -168,8 +168,8 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_GetLanguageByIsoCode() { - var danish = LocalizationService.GetLanguageByIsoCode("da-DK"); - var english = LocalizationService.GetLanguageByIsoCode("en-GB"); + var danish = _localizationService.GetLanguageByIsoCode("da-DK"); + var english = _localizationService.GetLanguageByIsoCode("en-GB"); Assert.NotNull(danish); Assert.NotNull(english); } @@ -177,14 +177,14 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Does_Not_Fail_When_Language_Doesnt_Exist() { - var language = LocalizationService.GetLanguageByIsoCode("sv-SE"); + var language = _localizationService.GetLanguageByIsoCode("sv-SE"); Assert.Null(language); } [Test] public void Does_Not_Fail_When_DictionaryItem_Doesnt_Exist() { - var item = LocalizationService.GetDictionaryItemByKey("RandomKey"); + var item = _localizationService.GetDictionaryItemByKey("RandomKey"); Assert.Null(item); } @@ -192,34 +192,34 @@ namespace Umbraco.Tests.Integration.Services public void Can_Delete_Language() { var norwegian = new Language(_globalSettings, "nb-NO") { CultureName = "Norwegian" }; - LocalizationService.Save(norwegian, 0); + _localizationService.Save(norwegian, 0); Assert.That(norwegian.HasIdentity, Is.True); var languageId = norwegian.Id; - LocalizationService.Delete(norwegian); + _localizationService.Delete(norwegian); - var language = LocalizationService.GetLanguageById(languageId); + var language = _localizationService.GetLanguageById(languageId); Assert.Null(language); } [Test] public void Can_Delete_Language_Used_As_Fallback() { - var danish = LocalizationService.GetLanguageByIsoCode("da-DK"); + var danish = _localizationService.GetLanguageByIsoCode("da-DK"); var norwegian = new Language(_globalSettings, "nb-NO") { CultureName = "Norwegian", FallbackLanguageId = danish.Id }; - LocalizationService.Save(norwegian, 0); + _localizationService.Save(norwegian, 0); var languageId = danish.Id; - LocalizationService.Delete(danish); + _localizationService.Delete(danish); - var language = LocalizationService.GetLanguageById(languageId); + var language = _localizationService.GetLanguageById(languageId); Assert.Null(language); } [Test] public void Can_Create_DictionaryItem_At_Root() { - var english = LocalizationService.GetLanguageByIsoCode("en-US"); + var english = _localizationService.GetLanguageByIsoCode("en-US"); var item = (IDictionaryItem)new DictionaryItem("Testing123") { @@ -228,10 +228,10 @@ namespace Umbraco.Tests.Integration.Services new DictionaryTranslation(english, "Hello world") } }; - LocalizationService.Save(item); + _localizationService.Save(item); //re-get - item = LocalizationService.GetDictionaryItemById(item.Id); + item = _localizationService.GetDictionaryItemById(item.Id); Assert.Greater(item.Id, 0); Assert.IsTrue(item.HasIdentity); @@ -243,18 +243,18 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_Create_DictionaryItem_At_Root_With_Identity() { - var item = LocalizationService.CreateDictionaryItemWithIdentity( + var item = _localizationService.CreateDictionaryItemWithIdentity( "Testing12345", null, "Hellooooo"); //re-get - item = LocalizationService.GetDictionaryItemById(item.Id); + item = _localizationService.GetDictionaryItemById(item.Id); Assert.IsNotNull(item); Assert.Greater(item.Id, 0); Assert.IsTrue(item.HasIdentity); Assert.IsFalse(item.ParentId.HasValue); Assert.AreEqual("Testing12345", item.ItemKey); - var allLangs = LocalizationService.GetAllLanguages(); + var allLangs = _localizationService.GetAllLanguages(); Assert.Greater(allLangs.Count(), 0); foreach (var language in allLangs) { @@ -265,20 +265,20 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_Add_Translation_To_Existing_Dictionary_Item() { - var english = LocalizationService.GetLanguageByIsoCode("en-US"); + var english = _localizationService.GetLanguageByIsoCode("en-US"); var item = (IDictionaryItem) new DictionaryItem("Testing123"); - LocalizationService.Save(item); + _localizationService.Save(item); //re-get - item = LocalizationService.GetDictionaryItemById(item.Id); + item = _localizationService.GetDictionaryItemById(item.Id); item.Translations = new List { new DictionaryTranslation(english, "Hello world") }; - LocalizationService.Save(item); + _localizationService.Save(item); Assert.AreEqual(1, item.Translations.Count()); foreach (var translation in item.Translations) @@ -289,14 +289,14 @@ namespace Umbraco.Tests.Integration.Services item.Translations = new List(item.Translations) { new DictionaryTranslation( - LocalizationService.GetLanguageByIsoCode("en-GB"), + _localizationService.GetLanguageByIsoCode("en-GB"), "My new value") }; - LocalizationService.Save(item); + _localizationService.Save(item); //re-get - item = LocalizationService.GetDictionaryItemById(item.Id); + item = _localizationService.GetDictionaryItemById(item.Id); Assert.AreEqual(2, item.Translations.Count()); Assert.AreEqual("Hello world", item.Translations.First().Value); @@ -306,27 +306,27 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_Delete_DictionaryItem() { - var item = LocalizationService.GetDictionaryItemByKey("Child"); + var item = _localizationService.GetDictionaryItemByKey("Child"); Assert.NotNull(item); - LocalizationService.Delete(item); + _localizationService.Delete(item); - var deletedItem = LocalizationService.GetDictionaryItemByKey("Child"); + var deletedItem = _localizationService.GetDictionaryItemByKey("Child"); Assert.Null(deletedItem); } [Test] public void Can_Update_Existing_DictionaryItem() { - var item = LocalizationService.GetDictionaryItemByKey("Child"); + var item = _localizationService.GetDictionaryItemByKey("Child"); foreach (var translation in item.Translations) { translation.Value = translation.Value + "UPDATED"; } - LocalizationService.Save(item); + _localizationService.Save(item); - var updatedItem = LocalizationService.GetDictionaryItemByKey("Child"); + var updatedItem = _localizationService.GetDictionaryItemByKey("Child"); Assert.NotNull(updatedItem); foreach (var translation in updatedItem.Translations) @@ -339,7 +339,7 @@ namespace Umbraco.Tests.Integration.Services public void Find_BaseData_Language() { // Act - var languages = LocalizationService.GetAllLanguages(); + var languages = _localizationService.GetAllLanguages(); // Assert Assert.That(3, Is.EqualTo(languages.Count())); @@ -353,8 +353,8 @@ namespace Umbraco.Tests.Integration.Services var language = new Core.Models.Language(_globalSettings, isoCode); // Act - LocalizationService.Save(language); - var result = LocalizationService.GetLanguageByIsoCode(isoCode); + _localizationService.Save(language); + var result = _localizationService.GetLanguageByIsoCode(isoCode); // Assert Assert.NotNull(result); @@ -367,8 +367,8 @@ namespace Umbraco.Tests.Integration.Services var language = new Core.Models.Language(_globalSettings, isoCode); // Act - LocalizationService.Save(language); - var result = LocalizationService.GetLanguageById(language.Id); + _localizationService.Save(language); + var result = _localizationService.GetLanguageById(language.Id); // Assert Assert.NotNull(result); @@ -378,16 +378,16 @@ namespace Umbraco.Tests.Integration.Services public void Set_Default_Language() { var language = new Language(_globalSettings, "en-AU") {IsDefault = true}; - LocalizationService.Save(language); - var result = LocalizationService.GetLanguageById(language.Id); + _localizationService.Save(language); + var result = _localizationService.GetLanguageById(language.Id); Assert.IsTrue(result.IsDefault); var language2 = new Language(_globalSettings, "en-NZ") {IsDefault = true}; - LocalizationService.Save(language2); - var result2 = LocalizationService.GetLanguageById(language2.Id); + _localizationService.Save(language2); + var result2 = _localizationService.GetLanguageById(language2.Id); //re-get - result = LocalizationService.GetLanguageById(language.Id); + result = _localizationService.GetLanguageById(language.Id); Assert.IsTrue(result2.IsDefault); Assert.IsFalse(result.IsDefault); @@ -398,11 +398,11 @@ namespace Umbraco.Tests.Integration.Services { var isoCode = "en-AU"; var language = new Core.Models.Language(_globalSettings, isoCode); - LocalizationService.Save(language); + _localizationService.Save(language); // Act - LocalizationService.Delete(language); - var result = LocalizationService.GetLanguageByIsoCode(isoCode); + _localizationService.Delete(language); + var result = _localizationService.GetLanguageByIsoCode(isoCode); // Assert Assert.Null(result); @@ -412,8 +412,8 @@ namespace Umbraco.Tests.Integration.Services { var danish = new Language(_globalSettings, "da-DK") { CultureName = "Danish" }; var english = new Language(_globalSettings, "en-GB") { CultureName = "English" }; - LocalizationService.Save(danish, 0); - LocalizationService.Save(english, 0); + _localizationService.Save(danish, 0); + _localizationService.Save(english, 0); _danishLangId = danish.Id; _englishLangId = english.Id; @@ -425,7 +425,7 @@ namespace Umbraco.Tests.Integration.Services new DictionaryTranslation(danish, "ForældreVærdi") } }; - LocalizationService.Save(parentItem); + _localizationService.Save(parentItem); _parentItemGuidId = parentItem.Key; _parentItemIntId = parentItem.Id; @@ -437,7 +437,7 @@ namespace Umbraco.Tests.Integration.Services new DictionaryTranslation(danish, "BørnVærdi") } }; - LocalizationService.Save(childItem); + _localizationService.Save(childItem); _childItemGuidId = childItem.Key; _childItemIntId = childItem.Id; } diff --git a/src/Umbraco.Tests.Integration/Services/MediaServiceTests.cs b/src/Umbraco.Tests.Integration/Services/MediaServiceTests.cs index a43f21d061..89c01b54d3 100644 --- a/src/Umbraco.Tests.Integration/Services/MediaServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/MediaServiceTests.cs @@ -19,8 +19,8 @@ namespace Umbraco.Tests.Integration.Services [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, PublishedRepositoryEvents = true)] public class MediaServiceTests : UmbracoIntegrationTest { - private IMediaService MediaService => GetRequiredService(); - private IMediaTypeService MediaTypeService => GetRequiredService(); + private IMediaService _mediaService => GetRequiredService(); + private IMediaTypeService _mediaTypeService => GetRequiredService(); /// /// Used to list out all ambiguous events that will require dispatching with a name @@ -28,7 +28,7 @@ namespace Umbraco.Tests.Integration.Services [Test, Explicit] public void List_Ambiguous_Events() { - var events = MediaService.GetType().GetEvents(BindingFlags.Static | BindingFlags.Public); + var events = _mediaService.GetType().GetEvents(BindingFlags.Static | BindingFlags.Public); var typedEventHandler = typeof(TypedEventHandler<,>); foreach (var e in events) { @@ -52,30 +52,30 @@ namespace Umbraco.Tests.Integration.Services public void Get_Paged_Children_With_Media_Type_Filter() { var mediaType1 = MockedContentTypes.CreateImageMediaType("Image2"); - MediaTypeService.Save(mediaType1); + _mediaTypeService.Save(mediaType1); var mediaType2 = MockedContentTypes.CreateImageMediaType("Image3"); - MediaTypeService.Save(mediaType2); + _mediaTypeService.Save(mediaType2); for (var i = 0; i < 10; i++) { var m1 = MockedMedia.CreateMediaImage(mediaType1, -1); - MediaService.Save(m1); + _mediaService.Save(m1); var m2 = MockedMedia.CreateMediaImage(mediaType2, -1); - MediaService.Save(m2); + _mediaService.Save(m2); } long total; var provider = ScopeProvider; using (provider.CreateScope()) { - var result = MediaService.GetPagedChildren(-1, 0, 11, out total, + var result = _mediaService.GetPagedChildren(-1, 0, 11, out total, provider.SqlContext.Query() .Where(x => new[] { mediaType1.Id, mediaType2.Id }.Contains(x.ContentTypeId)), Ordering.By("SortOrder", Direction.Ascending)); Assert.AreEqual(11, result.Count()); Assert.AreEqual(20, total); - result = MediaService.GetPagedChildren(-1, 1, 11, out total, + result = _mediaService.GetPagedChildren(-1, 1, 11, out total, provider.SqlContext.Query() .Where(x => new[] { mediaType1.Id, mediaType2.Id }.Contains(x.ContentTypeId)), Ordering.By("SortOrder", Direction.Ascending)); @@ -89,10 +89,10 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var mediaItems = CreateTrashedTestMedia(); - var media = MediaService.GetById(mediaItems.Item3.Id); + var media = _mediaService.GetById(mediaItems.Item3.Id); // Act - MediaService.Move(media, mediaItems.Item2.Id); + _mediaService.Move(media, mediaItems.Item2.Id); // Assert Assert.That(media.ParentId, Is.EqualTo(mediaItems.Item2.Id)); @@ -104,10 +104,10 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var mediaItems = CreateTrashedTestMedia(); - var media = MediaService.GetById(mediaItems.Item1.Id); + var media = _mediaService.GetById(mediaItems.Item1.Id); // Act - MediaService.MoveToRecycleBin(media); + _mediaService.MoveToRecycleBin(media); // Assert Assert.That(media.ParentId, Is.EqualTo(-21)); @@ -119,11 +119,11 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var mediaItems = CreateTrashedTestMedia(); - var media = MediaService.GetById(mediaItems.Item4.Id); + var media = _mediaService.GetById(mediaItems.Item4.Id); // Act - moving out of recycle bin - MediaService.Move(media, mediaItems.Item1.Id); - var mediaChild = MediaService.GetById(mediaItems.Item5.Id); + _mediaService.Move(media, mediaItems.Item1.Id); + var mediaChild = _mediaService.GetById(mediaItems.Item5.Id); // Assert Assert.That(media.ParentId, Is.EqualTo(mediaItems.Item1.Id)); @@ -137,11 +137,11 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var mediaType = MockedContentTypes.CreateVideoMediaType(); - MediaTypeService.Save(mediaType); - var media = MediaService.CreateMedia(string.Empty, -1, "video"); + _mediaTypeService.Save(mediaType); + var media = _mediaService.CreateMedia(string.Empty, -1, "video"); // Act & Assert - Assert.Throws(() => MediaService.Save(media)); + Assert.Throws(() => _mediaService.Save(media)); } /* [Test] @@ -163,13 +163,13 @@ namespace Umbraco.Tests.Integration.Services public void Can_Get_Media_By_Path() { var mediaType = MockedContentTypes.CreateImageMediaType("Image2"); - MediaTypeService.Save(mediaType); + _mediaTypeService.Save(mediaType); var media = MockedMedia.CreateMediaImage(mediaType, -1); - MediaService.Save(media); + _mediaService.Save(media); var mediaPath = "/media/test-image.png"; - var resolvedMedia = MediaService.GetMediaByPath(mediaPath); + var resolvedMedia = _mediaService.GetMediaByPath(mediaPath); Assert.IsNotNull(resolvedMedia); Assert.That(resolvedMedia.GetValue(Constants.Conventions.Media.File).ToString() == mediaPath); @@ -179,13 +179,13 @@ namespace Umbraco.Tests.Integration.Services public void Can_Get_Media_With_Crop_By_Path() { var mediaType = MockedContentTypes.CreateImageMediaTypeWithCrop("Image2"); - MediaTypeService.Save(mediaType); + _mediaTypeService.Save(mediaType); var media = MockedMedia.CreateMediaImageWithCrop(mediaType, -1); - MediaService.Save(media); + _mediaService.Save(media); var mediaPath = "/media/test-image.png"; - var resolvedMedia = MediaService.GetMediaByPath(mediaPath); + var resolvedMedia = _mediaService.GetMediaByPath(mediaPath); Assert.IsNotNull(resolvedMedia); Assert.That(resolvedMedia.GetValue(Constants.Conventions.Media.File).ToString().Contains(mediaPath)); @@ -195,14 +195,14 @@ namespace Umbraco.Tests.Integration.Services public void Can_Get_Paged_Children() { var mediaType = MockedContentTypes.CreateImageMediaType("Image2"); - MediaTypeService.Save(mediaType); + _mediaTypeService.Save(mediaType); for (var i = 0; i < 10; i++) { var c1 = MockedMedia.CreateMediaImage(mediaType, -1); - MediaService.Save(c1); + _mediaService.Save(c1); } - var service = MediaService; + var service = _mediaService; long total; var entities = service.GetPagedChildren(-1, 0, 6, out total).ToArray(); @@ -217,25 +217,25 @@ namespace Umbraco.Tests.Integration.Services public void Can_Get_Paged_Children_Dont_Get_Descendants() { var mediaType = MockedContentTypes.CreateImageMediaType("Image2"); - MediaTypeService.Save(mediaType); + _mediaTypeService.Save(mediaType); // only add 9 as we also add a folder with children for (var i = 0; i < 9; i++) { var m1 = MockedMedia.CreateMediaImage(mediaType, -1); - MediaService.Save(m1); + _mediaService.Save(m1); } var mediaTypeForFolder = MockedContentTypes.CreateImageMediaType("Folder2"); - MediaTypeService.Save(mediaTypeForFolder); + _mediaTypeService.Save(mediaTypeForFolder); var mediaFolder = MockedMedia.CreateMediaFolder(mediaTypeForFolder, -1); - MediaService.Save(mediaFolder); + _mediaService.Save(mediaFolder); for (var i = 0; i < 10; i++) { var m1 = MockedMedia.CreateMediaImage(mediaType, mediaFolder.Id); - MediaService.Save(m1); + _mediaService.Save(m1); } - var service = MediaService; + var service = _mediaService; long total; // children in root including the folder - not the descendants in the folder @@ -258,28 +258,28 @@ namespace Umbraco.Tests.Integration.Services private Tuple CreateTrashedTestMedia() { //Create and Save folder-Media -> 1050 - var folderMediaType = MediaTypeService.Get(1031); + var folderMediaType = _mediaTypeService.Get(1031); var folder = MockedMedia.CreateMediaFolder(folderMediaType, -1); - MediaService.Save(folder); + _mediaService.Save(folder); //Create and Save folder-Media -> 1051 var folder2 = MockedMedia.CreateMediaFolder(folderMediaType, -1); - MediaService.Save(folder2); + _mediaService.Save(folder2); //Create and Save image-Media -> 1052 - var imageMediaType = MediaTypeService.Get(1032); + var imageMediaType = _mediaTypeService.Get(1032); var image = (Media)MockedMedia.CreateMediaImage(imageMediaType, 1050); - MediaService.Save(image); + _mediaService.Save(image); //Create and Save folder-Media that is trashed -> 1053 var folderTrashed = (Media)MockedMedia.CreateMediaFolder(folderMediaType, -21); folderTrashed.Trashed = true; - MediaService.Save(folderTrashed); + _mediaService.Save(folderTrashed); //Create and Save image-Media child of folderTrashed -> 1054 var imageTrashed = (Media)MockedMedia.CreateMediaImage(imageMediaType, folderTrashed.Id); imageTrashed.Trashed = true; - MediaService.Save(imageTrashed); + _mediaService.Save(imageTrashed); return new Tuple(folder, folder2, image, folderTrashed, imageTrashed); } diff --git a/src/Umbraco.Tests.Integration/Services/PublicAccessServiceTests.cs b/src/Umbraco.Tests.Integration/Services/PublicAccessServiceTests.cs index 5294e8015b..42419565a2 100644 --- a/src/Umbraco.Tests.Integration/Services/PublicAccessServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/PublicAccessServiceTests.cs @@ -15,20 +15,20 @@ namespace Umbraco.Tests.Integration.Services [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] public class PublicAccessServiceTests : UmbracoIntegrationTest { - private IContentService ContentService => GetRequiredService(); - private IContentTypeService ContentTypeService => GetRequiredService(); - private IFileService FileService => GetRequiredService(); - private IPublicAccessService PublicAccessService => GetRequiredService(); + private IContentService _contentService => GetRequiredService(); + private IContentTypeService _contentTypeService => GetRequiredService(); + private IFileService _fileService => GetRequiredService(); + private IPublicAccessService _publicAccessService => GetRequiredService(); [Test] public void Can_Add_New_Entry() { // Arrange var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah"); - FileService.SaveTemplate(ct.DefaultTemplate); - ContentTypeService.Save(ct); + _fileService.SaveTemplate(ct.DefaultTemplate); + _contentTypeService.Save(ct); var c = MockedContent.CreateSimpleContent(ct, "Test", -1); - ContentService.Save(c); + _contentService.Save(c); // Act var entry = new PublicAccessEntry(c, c, c, new[] @@ -39,7 +39,7 @@ namespace Umbraco.Tests.Integration.Services RuleValue = "TestVal" }, }); - var result = PublicAccessService.Save(entry); + var result = _publicAccessService.Save(entry); // Assert Assert.IsTrue(result.Success); @@ -56,10 +56,10 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah"); - FileService.SaveTemplate(ct.DefaultTemplate); - ContentTypeService.Save(ct); + _fileService.SaveTemplate(ct.DefaultTemplate); + _contentTypeService.Save(ct); var c = MockedContent.CreateSimpleContent(ct, "Test", -1); - ContentService.Save(c); + _contentService.Save(c); var entry = new PublicAccessEntry(c, c, c, new[] { new PublicAccessRule() @@ -68,12 +68,12 @@ namespace Umbraco.Tests.Integration.Services RuleValue = "TestVal" }, }); - PublicAccessService.Save(entry); + _publicAccessService.Save(entry); // Act - var updated = PublicAccessService.AddRule(c, "TestType2", "AnotherVal"); + var updated = _publicAccessService.AddRule(c, "TestType2", "AnotherVal"); //re-get - entry = PublicAccessService.GetEntryForContent(c); + entry = _publicAccessService.GetEntryForContent(c); // Assert Assert.IsTrue(updated.Success); @@ -86,10 +86,10 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah"); - FileService.SaveTemplate(ct.DefaultTemplate); - ContentTypeService.Save(ct); + _fileService.SaveTemplate(ct.DefaultTemplate); + _contentTypeService.Save(ct); var c = MockedContent.CreateSimpleContent(ct, "Test", -1); - ContentService.Save(c); + _contentService.Save(c); var entry = new PublicAccessEntry(c, c, c, new[] { new PublicAccessRule() @@ -98,14 +98,14 @@ namespace Umbraco.Tests.Integration.Services RuleValue = "TestVal" }, }); - PublicAccessService.Save(entry); + _publicAccessService.Save(entry); // Act - var updated1 = PublicAccessService.AddRule(c, "TestType", "AnotherVal1"); - var updated2 = PublicAccessService.AddRule(c, "TestType", "AnotherVal2"); + var updated1 = _publicAccessService.AddRule(c, "TestType", "AnotherVal1"); + var updated2 = _publicAccessService.AddRule(c, "TestType", "AnotherVal2"); //re-get - entry = PublicAccessService.GetEntryForContent(c); + entry = _publicAccessService.GetEntryForContent(c); // Assert Assert.IsTrue(updated1.Success); @@ -120,10 +120,10 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah"); - FileService.SaveTemplate(ct.DefaultTemplate); - ContentTypeService.Save(ct); + _fileService.SaveTemplate(ct.DefaultTemplate); + _contentTypeService.Save(ct); var c = MockedContent.CreateSimpleContent(ct, "Test", -1); - ContentService.Save(c); + _contentService.Save(c); var entry = new PublicAccessEntry(c, c, c, new[] { new PublicAccessRule() @@ -137,12 +137,12 @@ namespace Umbraco.Tests.Integration.Services RuleValue = "TestValue2" }, }); - PublicAccessService.Save(entry); + _publicAccessService.Save(entry); // Act - var removed = PublicAccessService.RemoveRule(c, "TestType", "TestValue1"); + var removed = _publicAccessService.RemoveRule(c, "TestType", "TestValue1"); //re-get - entry = PublicAccessService.GetEntryForContent(c); + entry = _publicAccessService.GetEntryForContent(c); // Assert Assert.IsTrue(removed.Success); diff --git a/src/Umbraco.Tests.Integration/Services/SectionServiceTests.cs b/src/Umbraco.Tests.Integration/Services/SectionServiceTests.cs index a54a1c1c59..ef8301a315 100644 --- a/src/Umbraco.Tests.Integration/Services/SectionServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/SectionServiceTests.cs @@ -8,7 +8,7 @@ using Umbraco.Tests.Integration.Testing; using Umbraco.Tests.Testing; using Umbraco.Web.Services; -namespace Umbraco.Tests.Services +namespace Umbraco.Tests.Integration.Services { /// /// Tests covering the SectionService diff --git a/src/Umbraco.Tests.Integration/Services/TagServiceTests.cs b/src/Umbraco.Tests.Integration/Services/TagServiceTests.cs index db2c8196e7..a3ee817a28 100644 --- a/src/Umbraco.Tests.Integration/Services/TagServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/TagServiceTests.cs @@ -22,11 +22,11 @@ namespace Umbraco.Tests.Integration.Services [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] public class TagServiceTests : UmbracoIntegrationTest { - private IContentService ContentService => GetRequiredService(); - private IContentTypeService ContentTypeService => GetRequiredService(); - private ITagService TagService => GetRequiredService(); - private IDataTypeService DataTypeService => GetRequiredService(); - public PropertyEditorCollection PropertyEditorCollection => GetRequiredService(); + private IContentService _contentService => GetRequiredService(); + private IContentTypeService _contentTypeService => GetRequiredService(); + private ITagService _tagService => GetRequiredService(); + private IDataTypeService _dataTypeService => GetRequiredService(); + private PropertyEditorCollection _propertyEditorCollection => GetRequiredService(); [Test] public void TagApiConsistencyTest() @@ -37,25 +37,25 @@ namespace Umbraco.Tests.Integration.Services { DataTypeId = 1041 }); - ContentTypeService.Save(contentType); + _contentTypeService.Save(contentType); IContent content1 = MockedContent.CreateSimpleContent(contentType, "Tagged content 1", -1); - content1.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow", "pig", "goat" }); - ContentService.SaveAndPublish(content1); + content1.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "cow", "pig", "goat" }); + _contentService.SaveAndPublish(content1); // change - content1.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "elephant" }, true); - content1.RemoveTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow" }); - ContentService.SaveAndPublish(content1); + content1.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "elephant" }, true); + content1.RemoveTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "cow" }); + _contentService.SaveAndPublish(content1); // more changes - content1.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "mouse" }, true); - ContentService.SaveAndPublish(content1); - content1.RemoveTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "mouse" }); - ContentService.SaveAndPublish(content1); + content1.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "mouse" }, true); + _contentService.SaveAndPublish(content1); + content1.RemoveTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "mouse" }); + _contentService.SaveAndPublish(content1); // get it back - content1 = ContentService.GetById(content1.Id); + content1 = _contentService.GetById(content1.Id); var tagsValue = content1.GetValue("tags").ToString(); var tagsValues = JsonConvert.DeserializeObject(tagsValue); Assert.AreEqual(3, tagsValues.Length); @@ -63,7 +63,7 @@ namespace Umbraco.Tests.Integration.Services Assert.Contains("goat", tagsValues); Assert.Contains("elephant", tagsValues); - var tags = TagService.GetTagsForProperty(content1.Id, "tags").ToArray(); + var tags = _tagService.GetTagsForProperty(content1.Id, "tags").ToArray(); Assert.IsTrue(tags.All(x => x.Group == "default")); tagsValues = tags.Select(x => x.Text).ToArray(); @@ -82,22 +82,22 @@ namespace Umbraco.Tests.Integration.Services { DataTypeId = Constants.DataTypes.Tags }); - ContentTypeService.Save(contentType); + _contentTypeService.Save(contentType); var content1 = MockedContent.CreateSimpleContent(contentType, "Tagged content 1", -1); - content1.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow", "pig", "goat" }); - ContentService.SaveAndPublish(content1); + content1.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "cow", "pig", "goat" }); + _contentService.SaveAndPublish(content1); var content2 = MockedContent.CreateSimpleContent(contentType, "Tagged content 2", -1); - content2.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow", "pig" }); - ContentService.SaveAndPublish(content2); + content2.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "cow", "pig" }); + _contentService.SaveAndPublish(content2); var content3 = MockedContent.CreateSimpleContent(contentType, "Tagged content 3", -1); - content3.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow" }); - ContentService.SaveAndPublish(content3); + content3.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "cow" }); + _contentService.SaveAndPublish(content3); // Act - var tags = TagService.GetAllContentTags() + var tags = _tagService.GetAllContentTags() .OrderByDescending(x => x.NodeCount) .ToList(); diff --git a/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs b/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs index 64bd89c8f8..ba7dfae0f5 100644 --- a/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs @@ -12,11 +12,12 @@ using Umbraco.Core.Models.Membership; using Umbraco.Core.Persistence.Querying; using Umbraco.Core.Services; using Umbraco.Core.Services.Implement; +using Umbraco.Tests.Common.Builders; +using Umbraco.Tests.Common.Builders.Extensions; using Umbraco.Tests.Integration.Testing; using Umbraco.Tests.TestHelpers.Entities; using Umbraco.Tests.Testing; using Umbraco.Web.Actions; -using MockedUser = Umbraco.Tests.Common.TestHelpers.Entities.MockedUser; namespace Umbraco.Tests.Integration.Services { @@ -28,9 +29,9 @@ namespace Umbraco.Tests.Integration.Services [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] public class UserServiceTests : UmbracoIntegrationTest { - private UserService UserService => (UserService) GetRequiredService(); - private IContentTypeService ContentTypeService => GetRequiredService(); - private IContentService ContentService => GetRequiredService(); + private UserService _userService => (UserService) GetRequiredService(); + private IContentTypeService _contentTypeService => GetRequiredService(); + private IContentService _contentService => GetRequiredService(); [Test] public void Get_User_Permissions_For_Unassigned_Permission_Nodes() @@ -38,17 +39,17 @@ namespace Umbraco.Tests.Integration.Services // Arrange var user = CreateTestUser(out _); var contentType = MockedContentTypes.CreateSimpleContentType(); - ContentTypeService.Save(contentType); + _contentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - ContentService.Save(content); + _contentService.Save(content); // Act - var permissions = UserService.GetPermissions(user, content[0].Id, content[1].Id, content[2].Id).ToArray(); + var permissions = _userService.GetPermissions(user, content[0].Id, content[1].Id, content[2].Id).ToArray(); // Assert Assert.AreEqual(3, permissions.Length); @@ -64,23 +65,23 @@ namespace Umbraco.Tests.Integration.Services var user = CreateTestUser(out var userGroup); var contentType = MockedContentTypes.CreateSimpleContentType(); - ContentTypeService.Save(contentType); + _contentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - ContentService.Save(content); - ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content[2], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + _contentService.Save(content); + _contentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content[2], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); // Act - var permissions = UserService.GetPermissions(user, content[0].Id, content[1].Id, content[2].Id).ToArray(); + var permissions = _userService.GetPermissions(user, content[0].Id, content[1].Id, content[2].Id).ToArray(); // Assert Assert.AreEqual(3, permissions.Length); @@ -96,23 +97,23 @@ namespace Umbraco.Tests.Integration.Services var userGroup = CreateTestUserGroup(); var contentType = MockedContentTypes.CreateSimpleContentType(); - ContentTypeService.Save(contentType); + _contentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - ContentService.Save(content); - ContentService.SetPermission(content.ElementAt(0), ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content.ElementAt(0), ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content.ElementAt(0), ActionMove.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content.ElementAt(1), ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content.ElementAt(1), ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content.ElementAt(2), ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + _contentService.Save(content); + _contentService.SetPermission(content.ElementAt(0), ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content.ElementAt(0), ActionDelete.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content.ElementAt(0), ActionMove.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content.ElementAt(1), ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content.ElementAt(1), ActionDelete.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content.ElementAt(2), ActionBrowse.ActionLetter, new int[] { userGroup.Id }); // Act - var permissions = UserService.GetPermissions(userGroup, false, content[0].Id, content[1].Id, content[2].Id).ToArray(); + var permissions = _userService.GetPermissions(userGroup, false, content[0].Id, content[1].Id, content[2].Id).ToArray(); // Assert Assert.AreEqual(3, permissions.Length); @@ -128,22 +129,22 @@ namespace Umbraco.Tests.Integration.Services var userGroup = CreateTestUserGroup(); var contentType = MockedContentTypes.CreateSimpleContentType(); - ContentTypeService.Save(contentType); + _contentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - ContentService.Save(content); - ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id }); + _contentService.Save(content); + _contentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id }); // Act - var permissions = UserService.GetPermissions(userGroup, true, content[0].Id, content[1].Id, content[2].Id) + var permissions = _userService.GetPermissions(userGroup, true, content[0].Id, content[1].Id, content[2].Id) .ToArray(); // Assert @@ -160,35 +161,35 @@ namespace Umbraco.Tests.Integration.Services var userGroup1 = CreateTestUserGroup(); var userGroup2 = CreateTestUserGroup("test2", "Test 2"); var userGroup3 = CreateTestUserGroup("test3", "Test 3"); - var user = UserService.CreateUserWithIdentity("John Doe", "john@umbraco.io"); + var user = _userService.CreateUserWithIdentity("John Doe", "john@umbraco.io"); var defaultPermissionCount = userGroup3.Permissions.Count(); user.AddGroup(userGroup1); user.AddGroup(userGroup2); user.AddGroup(userGroup3); - UserService.Save(user); + _userService.Save(user); var contentType = MockedContentTypes.CreateSimpleContentType(); - ContentTypeService.Save(contentType); + _contentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - ContentService.Save(content); + _contentService.Save(content); //assign permissions - we aren't assigning anything explicit for group3 and nothing explicit for content[2] /w group2 - ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup1.Id }); - ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup1.Id }); - ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup2.Id }); - ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup1.Id }); - ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup2.Id }); - ContentService.SetPermission(content[2], ActionDelete.ActionLetter, new int[] { userGroup1.Id }); + _contentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup1.Id }); + _contentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup1.Id }); + _contentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup2.Id }); + _contentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup1.Id }); + _contentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup2.Id }); + _contentService.SetPermission(content[2], ActionDelete.ActionLetter, new int[] { userGroup1.Id }); // Act //we don't pass in any nodes so it will return all of them - var result = UserService.GetPermissions(user).ToArray(); + var result = _userService.GetPermissions(user).ToArray(); var permissions = result .GroupBy(x => x.EntityId) .ToDictionary(x => x.Key, x => x.GroupBy(a => a.UserGroupId).ToDictionary(a => a.Key, a => a.ToArray())); @@ -239,24 +240,24 @@ namespace Umbraco.Tests.Integration.Services var userGroup = CreateTestUserGroup(); var contentType = MockedContentTypes.CreateSimpleContentType(); - ContentTypeService.Save(contentType); + _contentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - ContentService.Save(content); - ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(content[2], ActionDelete.ActionLetter, new int[] { userGroup.Id }); + _contentService.Save(content); + _contentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(content[2], ActionDelete.ActionLetter, new int[] { userGroup.Id }); // Act //we don't pass in any nodes so it will return all of them - var permissions = UserService.GetPermissions(userGroup, true) + var permissions = _userService.GetPermissions(userGroup, true) .GroupBy(x => x.EntityId) .ToDictionary(x => x.Key, x => x); @@ -400,22 +401,22 @@ namespace Umbraco.Tests.Integration.Services var userGroup = CreateTestUserGroup(); var contentType = MockedContentTypes.CreateSimpleContentType(); - ContentTypeService.Save(contentType); + _contentTypeService.Save(contentType); var parent = MockedContent.CreateSimpleContent(contentType); - ContentService.Save(parent); + _contentService.Save(parent); var child1 = MockedContent.CreateSimpleContent(contentType, "child1", parent); - ContentService.Save(child1); + _contentService.Save(child1); var child2 = MockedContent.CreateSimpleContent(contentType, "child2", child1); - ContentService.Save(child2); + _contentService.Save(child2); - ContentService.SetPermission(parent, ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(parent, ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(parent, ActionMove.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(parent, ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ContentService.SetPermission(parent, ActionDelete.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(parent, ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(parent, ActionDelete.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(parent, ActionMove.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(parent, ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + _contentService.SetPermission(parent, ActionDelete.ActionLetter, new int[] { userGroup.Id }); // Act - var permissions = UserService.GetPermissionsForPath(userGroup, child2.Path); + var permissions = _userService.GetPermissionsForPath(userGroup, child2.Path); // Assert var allPermissions = permissions.GetAllPermissions().ToArray(); @@ -425,10 +426,10 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_Delete_User() { - var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - UserService.Delete(user, true); - var deleted = UserService.GetUserById(user.Id); + _userService.Delete(user, true); + var deleted = _userService.GetUserById(user.Id); // Assert Assert.That(deleted, Is.Null); @@ -437,10 +438,10 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Disables_User_Instead_Of_Deleting_If_Flag_Not_Set() { - var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - UserService.Delete(user); - var deleted = UserService.GetUserById(user.Id); + _userService.Delete(user); + var deleted = _userService.GetUserById(user.Id); // Assert Assert.That(deleted, Is.Not.Null); @@ -449,60 +450,60 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Exists_By_Username() { - var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - var user2 = UserService.CreateUserWithIdentity("john2@umbraco.io", "john2@umbraco.io"); - Assert.IsTrue(UserService.Exists("JohnDoe")); - Assert.IsFalse(UserService.Exists("notFound")); - Assert.IsTrue(UserService.Exists("john2@umbraco.io")); + var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user2 = _userService.CreateUserWithIdentity("john2@umbraco.io", "john2@umbraco.io"); + Assert.IsTrue(_userService.Exists("JohnDoe")); + Assert.IsFalse(_userService.Exists("notFound")); + Assert.IsTrue(_userService.Exists("john2@umbraco.io")); } [Test] public void Get_By_Email() { - var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - Assert.IsNotNull(UserService.GetByEmail(user.Email)); - Assert.IsNull(UserService.GetByEmail("do@not.find")); + Assert.IsNotNull(_userService.GetByEmail(user.Email)); + Assert.IsNull(_userService.GetByEmail("do@not.find")); } [Test] public void Get_By_Username() { - var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - Assert.IsNotNull(UserService.GetByUsername(user.Username)); - Assert.IsNull(UserService.GetByUsername("notFound")); + Assert.IsNotNull(_userService.GetByUsername(user.Username)); + Assert.IsNull(_userService.GetByUsername("notFound")); } [Test] public void Get_By_Username_With_Backslash() { - var user = UserService.CreateUserWithIdentity("mydomain\\JohnDoe", "john@umbraco.io"); + var user = _userService.CreateUserWithIdentity("mydomain\\JohnDoe", "john@umbraco.io"); - Assert.IsNotNull(UserService.GetByUsername(user.Username)); - Assert.IsNull(UserService.GetByUsername("notFound")); + Assert.IsNotNull(_userService.GetByUsername(user.Username)); + Assert.IsNull(_userService.GetByUsername("notFound")); } [Test] public void Get_By_Object_Id() { - var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - Assert.IsNotNull(UserService.GetUserById(user.Id)); - Assert.IsNull(UserService.GetUserById(9876)); + Assert.IsNotNull(_userService.GetUserById(user.Id)); + Assert.IsNull(_userService.GetUserById(9876)); } [Test] public void Find_By_Email_Starts_With() { - var users = MockedUser.CreateMulipleUsers(10); - UserService.Save(users); + var users = CreateMulipleUsers(10); + _userService.Save(users); //don't find this - var customUser = MockedUser.CreateUser(); + var customUser = CreateUser(); customUser.Email = "hello@hello.com"; - UserService.Save(customUser); + _userService.Save(customUser); - var found = UserService.FindByEmail("tes", 0, 100, out _, StringPropertyMatchType.StartsWith); + var found = _userService.FindByEmail("tes", 0, 100, out _, StringPropertyMatchType.StartsWith); Assert.AreEqual(10, found.Count()); } @@ -510,14 +511,14 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Find_By_Email_Ends_With() { - var users = MockedUser.CreateMulipleUsers(10); - UserService.Save(users); + var users = CreateMulipleUsers(10); + _userService.Save(users); //include this - var customUser = MockedUser.CreateUser(); + var customUser = CreateUser(); customUser.Email = "hello@test.com"; - UserService.Save(customUser); + _userService.Save(customUser); - var found = UserService.FindByEmail("test.com", 0, 100, out _, StringPropertyMatchType.EndsWith); + var found = _userService.FindByEmail("test.com", 0, 100, out _, StringPropertyMatchType.EndsWith); Assert.AreEqual(11, found.Count()); } @@ -525,14 +526,14 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Find_By_Email_Contains() { - var users = MockedUser.CreateMulipleUsers(10); - UserService.Save(users); + var users = CreateMulipleUsers(10); + _userService.Save(users); //include this - var customUser = MockedUser.CreateUser(); + var customUser = CreateUser(); customUser.Email = "hello@test.com"; - UserService.Save(customUser); + _userService.Save(customUser); - var found = UserService.FindByEmail("test", 0, 100, out _, StringPropertyMatchType.Contains); + var found = _userService.FindByEmail("test", 0, 100, out _, StringPropertyMatchType.Contains); Assert.AreEqual(11, found.Count()); } @@ -540,14 +541,14 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Find_By_Email_Exact() { - var users = MockedUser.CreateMulipleUsers(10); - UserService.Save(users); + var users = CreateMulipleUsers(10); + _userService.Save(users); //include this - var customUser = MockedUser.CreateUser(); + var customUser = CreateUser(); customUser.Email = "hello@test.com"; - UserService.Save(customUser); + _userService.Save(customUser); - var found = UserService.FindByEmail("hello@test.com", 0, 100, out _, StringPropertyMatchType.Exact); + var found = _userService.FindByEmail("hello@test.com", 0, 100, out _, StringPropertyMatchType.Exact); Assert.AreEqual(1, found.Count()); } @@ -555,10 +556,10 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Get_All_Paged_Users() { - var users = MockedUser.CreateMulipleUsers(10); - UserService.Save(users); + var users = CreateMulipleUsers(10); + _userService.Save(users); - var found = UserService.GetAll(0, 2, out var totalRecs); + var found = _userService.GetAll(0, 2, out var totalRecs); Assert.AreEqual(2, found.Count()); // + 1 because of the built in admin user @@ -570,10 +571,10 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Get_All_Paged_Users_With_Filter() { - var users = MockedUser.CreateMulipleUsers(10).ToArray(); - UserService.Save(users); + var users = CreateMulipleUsers(10).ToArray(); + _userService.Save(users); - var found = UserService.GetAll(0, 2, out var totalRecs, "username", Direction.Ascending, filter: "test"); + var found = _userService.GetAll(0, 2, out var totalRecs, "username", Direction.Ascending, filter: "test"); Assert.AreEqual(2, found.Count()); Assert.AreEqual(10, totalRecs); @@ -585,18 +586,18 @@ namespace Umbraco.Tests.Integration.Services public void Get_All_Paged_Users_For_Group() { var userGroup = MockedUserGroup.CreateUserGroup(); - UserService.Save(userGroup); + _userService.Save(userGroup); - var users = MockedUser.CreateMulipleUsers(10).ToArray(); + var users = CreateMulipleUsers(10).ToArray(); for (var i = 0; i < 10;) { users[i].AddGroup(userGroup.ToReadOnlyGroup()); i = i + 2; } - UserService.Save(users); + _userService.Save(users); long totalRecs; - var found = UserService.GetAll(0, 2, out totalRecs, "username", Direction.Ascending, includeUserGroups: new[] { userGroup.Alias }); + var found = _userService.GetAll(0, 2, out totalRecs, "username", Direction.Ascending, includeUserGroups: new[] { userGroup.Alias }); Assert.AreEqual(2, found.Count()); Assert.AreEqual(5, totalRecs); @@ -608,9 +609,9 @@ namespace Umbraco.Tests.Integration.Services public void Get_All_Paged_Users_For_Group_With_Filter() { var userGroup = MockedUserGroup.CreateUserGroup(); - UserService.Save(userGroup); + _userService.Save(userGroup); - var users = MockedUser.CreateMulipleUsers(10).ToArray(); + var users = CreateMulipleUsers(10).ToArray(); for (var i = 0; i < 10;) { users[i].AddGroup(userGroup.ToReadOnlyGroup()); @@ -621,10 +622,10 @@ namespace Umbraco.Tests.Integration.Services users[i].Name = "blah" + users[i].Name; i = i + 3; } - UserService.Save(users); + _userService.Save(users); long totalRecs; - var found = UserService.GetAll(0, 2, out totalRecs, "username", Direction.Ascending, userGroups: new[] { userGroup.Alias }, filter: "blah"); + var found = _userService.GetAll(0, 2, out totalRecs, "username", Direction.Ascending, userGroups: new[] { userGroup.Alias }, filter: "blah"); Assert.AreEqual(2, found.Count()); Assert.AreEqual(2, totalRecs); @@ -635,12 +636,12 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Count_All_Users() { - var users = MockedUser.CreateMulipleUsers(10); - UserService.Save(users); - var customUser = MockedUser.CreateUser(); - UserService.Save(customUser); + var users = CreateMulipleUsers(10); + _userService.Save(users); + var customUser = CreateUser(); + _userService.Save(customUser); - var found = UserService.GetCount(MemberCountType.All); + var found = _userService.GetCount(MemberCountType.All); // + 1 because of the built in admin user Assert.AreEqual(12, found); @@ -650,24 +651,24 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Count_All_Online_Users() { - var users = MockedUser.CreateMulipleUsers(10, (i, member) => member.LastLoginDate = DateTime.Now.AddMinutes(i * -2)); - UserService.Save(users); + var users = CreateMulipleUsers(10, (i, member) => member.LastLoginDate = DateTime.Now.AddMinutes(i * -2)); + _userService.Save(users); - var customUser = MockedUser.CreateUser(); + var customUser = CreateUser(); throw new NotImplementedException(); } [Test] public void Count_All_Locked_Users() { - var users = MockedUser.CreateMulipleUsers(10, (i, member) => member.IsLockedOut = i % 2 == 0); - UserService.Save(users); + var users = CreateMulipleUsers(10, (i, member) => member.IsLockedOut = i % 2 == 0); + _userService.Save(users); - var customUser = MockedUser.CreateUser(); + var customUser = CreateUser(); customUser.IsLockedOut = true; - UserService.Save(customUser); + _userService.Save(customUser); - var found = UserService.GetCount(MemberCountType.LockedOut); + var found = _userService.GetCount(MemberCountType.LockedOut); Assert.AreEqual(6, found); } @@ -675,14 +676,14 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Count_All_Approved_Users() { - var users = MockedUser.CreateMulipleUsers(10, (i, member) => member.IsApproved = i % 2 == 0); - UserService.Save(users); + var users = CreateMulipleUsers(10, (i, member) => member.IsApproved = i % 2 == 0); + _userService.Save(users); - var customUser = MockedUser.CreateUser(); + var customUser = CreateUser(); customUser.IsApproved = false; - UserService.Save(customUser); + _userService.Save(customUser); - var found = UserService.GetCount(MemberCountType.Approved); + var found = _userService.GetCount(MemberCountType.Approved); // + 1 because of the built in admin user Assert.AreEqual(6, found); @@ -692,7 +693,7 @@ namespace Umbraco.Tests.Integration.Services public void Can_Persist_New_User() { // Act - var membershipUser = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var membershipUser = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); // Assert Assert.That(membershipUser.HasIdentity, Is.True); @@ -712,7 +713,7 @@ namespace Umbraco.Tests.Integration.Services var encodedPassword = Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password))); var globalSettings = new GlobalSettings(); var membershipUser = new User(globalSettings, "JohnDoe", "john@umbraco.io", encodedPassword, encodedPassword); - UserService.Save(membershipUser); + _userService.Save(membershipUser); // Assert Assert.That(membershipUser.HasIdentity, Is.True); @@ -732,9 +733,9 @@ namespace Umbraco.Tests.Integration.Services }; userGroup.AddAllowedSection("content"); userGroup.AddAllowedSection("mediat"); - UserService.Save(userGroup); + _userService.Save(userGroup); - var result1 = UserService.GetUserGroupById(userGroup.Id); + var result1 = _userService.GetUserGroupById(userGroup.Id); Assert.AreEqual(2, result1.AllowedSections.Count()); @@ -743,9 +744,9 @@ namespace Umbraco.Tests.Integration.Services userGroup.AddAllowedSection("test2"); userGroup.AddAllowedSection("test3"); userGroup.AddAllowedSection("test4"); - UserService.Save(userGroup); + _userService.Save(userGroup); - result1 = UserService.GetUserGroupById(userGroup.Id); + result1 = _userService.GetUserGroupById(userGroup.Id); Assert.AreEqual(6, result1.AllowedSections.Count()); @@ -758,11 +759,11 @@ namespace Umbraco.Tests.Integration.Services //now just re-add a couple result1.AddAllowedSection("test3"); result1.AddAllowedSection("test4"); - UserService.Save(result1); + _userService.Save(result1); // Assert //re-get - result1 = UserService.GetUserGroupById(userGroup.Id); + result1 = _userService.GetUserGroupById(userGroup.Id); Assert.AreEqual(2, result1.AllowedSections.Count()); } @@ -779,21 +780,21 @@ namespace Umbraco.Tests.Integration.Services Alias = "Group2", Name = "Group 2" }; - UserService.Save(userGroup1); - UserService.Save(userGroup2); + _userService.Save(userGroup1); + _userService.Save(userGroup2); //adds some allowed sections userGroup1.AddAllowedSection("test"); userGroup2.AddAllowedSection("test"); - UserService.Save(userGroup1); - UserService.Save(userGroup2); + _userService.Save(userGroup1); + _userService.Save(userGroup2); //now clear the section from all users - UserService.DeleteSectionFromAllUserGroups("test"); + _userService.DeleteSectionFromAllUserGroups("test"); // Assert - var result1 = UserService.GetUserGroupById(userGroup1.Id); - var result2 = UserService.GetUserGroupById(userGroup2.Id); + var result1 = _userService.GetUserGroupById(userGroup1.Id); + var result2 = _userService.GetUserGroupById(userGroup2.Id); Assert.IsFalse(result1.AllowedSections.Contains("test")); Assert.IsFalse(result2.AllowedSections.Contains("test")); } @@ -820,14 +821,14 @@ namespace Umbraco.Tests.Integration.Services Alias = "Group3", Name = "Group 3" }; - UserService.Save(userGroup1); - UserService.Save(userGroup2); - UserService.Save(userGroup3); + _userService.Save(userGroup1); + _userService.Save(userGroup2); + _userService.Save(userGroup3); // Assert - var result1 = UserService.GetUserGroupById(userGroup1.Id); - var result2 = UserService.GetUserGroupById(userGroup2.Id); - var result3 = UserService.GetUserGroupById(userGroup3.Id); + var result1 = _userService.GetUserGroupById(userGroup1.Id); + var result2 = _userService.GetUserGroupById(userGroup2.Id); + var result3 = _userService.GetUserGroupById(userGroup3.Id); Assert.IsTrue(result1.AllowedSections.Contains("test")); Assert.IsTrue(result2.AllowedSections.Contains("test")); Assert.IsFalse(result3.AllowedSections.Contains("test")); @@ -836,13 +837,13 @@ namespace Umbraco.Tests.Integration.Services foreach (var userGroup in new[] { userGroup1, userGroup2, userGroup3 }) { userGroup.AddAllowedSection("test"); - UserService.Save(userGroup); + _userService.Save(userGroup); } // Assert - result1 = UserService.GetUserGroupById(userGroup1.Id); - result2 = UserService.GetUserGroupById(userGroup2.Id); - result3 = UserService.GetUserGroupById(userGroup3.Id); + result1 = _userService.GetUserGroupById(userGroup1.Id); + result2 = _userService.GetUserGroupById(userGroup2.Id); + result3 = _userService.GetUserGroupById(userGroup3.Id); Assert.IsTrue(result1.AllowedSections.Contains("test")); Assert.IsTrue(result2.AllowedSections.Contains("test")); Assert.IsTrue(result3.AllowedSections.Contains("test")); @@ -852,39 +853,39 @@ namespace Umbraco.Tests.Integration.Services public void Cannot_Create_User_With_Empty_Username() { // Act & Assert - Assert.Throws(() => UserService.CreateUserWithIdentity(string.Empty, "john@umbraco.io")); + Assert.Throws(() => _userService.CreateUserWithIdentity(string.Empty, "john@umbraco.io")); } [Test] public void Cannot_Save_User_With_Empty_Username() { // Arrange - var user = UserService.CreateUserWithIdentity("John Doe", "john@umbraco.io"); + var user = _userService.CreateUserWithIdentity("John Doe", "john@umbraco.io"); user.Username = string.Empty; // Act & Assert - Assert.Throws(() => UserService.Save(user)); + Assert.Throws(() => _userService.Save(user)); } [Test] public void Cannot_Save_User_With_Empty_Name() { // Arrange - var user = UserService.CreateUserWithIdentity("John Doe", "john@umbraco.io"); + var user = _userService.CreateUserWithIdentity("John Doe", "john@umbraco.io"); user.Name = string.Empty; // Act & Assert - Assert.Throws(() => UserService.Save(user)); + Assert.Throws(() => _userService.Save(user)); } [Test] public void Get_By_Profile_Username() { // Arrange - var user = UserService.CreateUserWithIdentity("test1", "test1@test.com"); + var user = _userService.CreateUserWithIdentity("test1", "test1@test.com"); // Act - var profile = UserService.GetProfileByUserName(user.Username); + var profile = _userService.GetProfileByUserName(user.Username); // Assert Assert.IsNotNull(profile); @@ -896,10 +897,10 @@ namespace Umbraco.Tests.Integration.Services public void Get_By_Profile_Id() { // Arrange - var user = (IUser)UserService.CreateUserWithIdentity("test1", "test1@test.com"); + var user = _userService.CreateUserWithIdentity("test1", "test1@test.com"); // Act - var profile = UserService.GetProfileById((int)user.Id); + var profile = _userService.GetProfileById((int)user.Id); // Assert Assert.IsNotNull(profile); @@ -908,18 +909,18 @@ namespace Umbraco.Tests.Integration.Services } [Test] - public void Get_By_Profile_Id_Must_return_null_if_user_not_exists() + public void Get_By_Profile_Id_Must_Return_Null_If_User_Does_Not_Exist() { - var profile = UserService.GetProfileById(42); + var profile = _userService.GetProfileById(42); // Assert Assert.IsNull(profile); } [Test] - public void GetProfilesById_Must_empty_if_users_not_exists() + public void GetProfilesById_Must_Return_Empty_If_User_Does_Not_Exist() { - var profiles = UserService.GetProfilesById(42); + var profiles = _userService.GetProfilesById(42); // Assert CollectionAssert.IsEmpty(profiles); @@ -933,7 +934,7 @@ namespace Umbraco.Tests.Integration.Services // Act - var updatedItem = (User)UserService.GetByUsername(originalUser.Username); + var updatedItem = (User)_userService.GetByUsername(originalUser.Username); // Assert Assert.IsNotNull(updatedItem); @@ -961,7 +962,7 @@ namespace Umbraco.Tests.Integration.Services CreateTestUsers(startContentItems.Select(x => x.Id).ToArray(), testUserGroup, 3); - var usersInGroup = UserService.GetAllInGroup(userGroupId); + var usersInGroup = _userService.GetAllInGroup(userGroupId); foreach (var user in usersInGroup) Assert.AreEqual(user.StartContentIds.Length, startContentItems.Length); @@ -971,27 +972,60 @@ namespace Umbraco.Tests.Integration.Services { var contentType = MockedContentTypes.CreateSimpleContentType(); - ContentTypeService.Save(contentType); + _contentTypeService.Save(contentType); var startContentItems = new List(); for (var i = 0; i < numberToCreate; i++) startContentItems.Add(MockedContent.CreateSimpleContent(contentType)); - ContentService.Save(startContentItems); + _contentService.Save(startContentItems); return startContentItems.ToArray(); } + private static IEnumerable CreateMulipleUsers(int amount, Action onCreating = null) + { + var list = new List(); + + for (var i = 0; i < amount; i++) + { + var name = "User No-" + i; + var user = new UserBuilder() + .WithName(name) + .WithEmail("test" + i + "@test.com") + .WithLogin("test" + i, "test" + i) + .Build(); + + onCreating?.Invoke(i, user); + + user.ResetDirtyProperties(false); + + list.Add(user); + } + + return list; + } + + private static User CreateUser(string suffix = "") + { + return new UserBuilder() + .WithIsApproved(true) + .WithName("TestUser" + suffix) + .WithLogin("TestUser" + suffix, "testing") + .WithEmail("test" + suffix + "@test.com") + .Build(); + } + private IUser CreateTestUser(out IUserGroup userGroup) { userGroup = CreateTestUserGroup(); - var user = UserService.CreateUserWithIdentity("test1", "test1@test.com"); + var user = _userService.CreateUserWithIdentity("test1", "test1@test.com"); user.AddGroup(userGroup.ToReadOnlyGroup()); - UserService.Save(user); + _userService.Save(user); return user; } @@ -1002,13 +1036,13 @@ namespace Umbraco.Tests.Integration.Services for (var i = 0; i < numberToCreate; i++) { - var user = UserService.CreateUserWithIdentity($"test{i}", $"test{i}@test.com"); + var user = _userService.CreateUserWithIdentity($"test{i}", $"test{i}@test.com"); user.AddGroup(userGroup.ToReadOnlyGroup()); var updateable = (User)user; updateable.StartContentIds = startContentIds; - UserService.Save(user); + _userService.Save(user); users.Add(user); } @@ -1028,7 +1062,7 @@ namespace Umbraco.Tests.Integration.Services userGroup.AddAllowedSection("content"); userGroup.AddAllowedSection("media"); - UserService.Save(userGroup); + _userService.Save(userGroup); return userGroup; } diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Editors/UserEditorAuthorizationHelperTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Editors/UserEditorAuthorizationHelperTests.cs index 187e3cccd2..bbb0f1b3e3 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Editors/UserEditorAuthorizationHelperTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Editors/UserEditorAuthorizationHelperTests.cs @@ -1,8 +1,5 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using Moq; using NUnit.Framework; using Umbraco.Core; @@ -10,8 +7,6 @@ using Umbraco.Core.Models; using Umbraco.Core.Models.Entities; using Umbraco.Core.Models.Membership; using Umbraco.Core.Services; -using Umbraco.Tests.Common.TestHelpers.Entities; -using Umbraco.Tests.TestHelpers.Entities; using Umbraco.Web.Editors; namespace Umbraco.Tests.Web.Controllers @@ -23,7 +18,7 @@ namespace Umbraco.Tests.Web.Controllers public void Admin_Is_Authorized() { var currentUser = GetAdminUser(); - var savingUser = MockedUser.GetUserMock(); + var savingUser = GetUserMock(); var contentService = new Mock(); var mediaService = new Mock(); @@ -44,7 +39,7 @@ namespace Umbraco.Tests.Web.Controllers [Test] public void Non_Admin_Cannot_Save_Admin() { - var currentUser = MockedUser.GetUserMock(); + var currentUser = GetUserMock(); var savingUser = GetAdminUser(); var contentService = new Mock(); @@ -66,12 +61,12 @@ namespace Umbraco.Tests.Web.Controllers [Test] public void Cannot_Grant_Group_Membership_Without_Being_A_Member() { - var currentUser = MockedUser.GetUserMock(); + var currentUser = GetUserMock(); currentUser.Setup(x => x.Groups).Returns(new[] { new ReadOnlyUserGroup(1, "Test", "icon-user", null, null, "test", new string[0], new string[0]) }); - var savingUser = MockedUser.GetUserMock(); + var savingUser = GetUserMock(); var contentService = new Mock(); var mediaService = new Mock(); @@ -92,12 +87,12 @@ namespace Umbraco.Tests.Web.Controllers [Test] public void Can_Grant_Group_Membership_With_Being_A_Member() { - var currentUser = MockedUser.GetUserMock(); + var currentUser = GetUserMock(); currentUser.Setup(x => x.Groups).Returns(new[] { new ReadOnlyUserGroup(1, "Test", "icon-user", null, null, "test", new string[0], new string[0]) }); - var savingUser = MockedUser.GetUserMock(); + var savingUser = GetUserMock(); var contentService = new Mock(); var mediaService = new Mock(); @@ -126,9 +121,9 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = MockedUser.GetUserMock(); + var currentUser = GetUserMock(); currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.GetUserMock(); + var savingUser = GetUserMock(); savingUser.Setup(x => x.StartContentIds).Returns(new[] { 1234 }); var contentService = new Mock(); @@ -166,9 +161,9 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = MockedUser.GetUserMock(); + var currentUser = GetUserMock(); currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.GetUserMock(); + var savingUser = GetUserMock(); savingUser.Setup(x => x.StartContentIds).Returns(new[] { 1234, 4567 }); var contentService = new Mock(); @@ -206,9 +201,9 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = MockedUser.GetUserMock(); + var currentUser = GetUserMock(); currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.GetUserMock(); + var savingUser = GetUserMock(); var contentService = new Mock(); contentService.Setup(x => x.GetById(It.IsAny())) @@ -245,9 +240,9 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = MockedUser.GetUserMock(); + var currentUser = GetUserMock(); currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.GetUserMock(); + var savingUser = GetUserMock(); var contentService = new Mock(); contentService.Setup(x => x.GetById(It.IsAny())) @@ -285,9 +280,9 @@ namespace Umbraco.Tests.Web.Controllers }; - var currentUser = MockedUser.GetUserMock(); + var currentUser = GetUserMock(); currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.GetUserMock(); + var savingUser = GetUserMock(); var contentService = new Mock(); var mediaService = new Mock(); @@ -324,9 +319,9 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = MockedUser.GetUserMock(); + var currentUser = GetUserMock(); currentUser.Setup(x => x.StartMediaIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.GetUserMock(); + var savingUser = GetUserMock(); var contentService = new Mock(); var mediaService = new Mock(); @@ -363,9 +358,9 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = MockedUser.GetUserMock(); + var currentUser = GetUserMock(); currentUser.Setup(x => x.StartMediaIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.GetUserMock(); + var savingUser = GetUserMock(); savingUser.Setup(x => x.StartMediaIds).Returns(new[] { 1234 }); var contentService = new Mock(); @@ -403,9 +398,9 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = MockedUser.GetUserMock(); + var currentUser = GetUserMock(); currentUser.Setup(x => x.StartMediaIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.GetUserMock(); + var savingUser = GetUserMock(); savingUser.Setup(x => x.StartMediaIds).Returns(new[] { 1234, 4567 }); var contentService = new Mock(); @@ -432,9 +427,23 @@ namespace Umbraco.Tests.Web.Controllers Assert.IsTrue(result.Success); } + /// + /// Returns a and ensures that the ToUserCache and FromUserCache methods are mapped correctly for + /// dealing with start node caches + /// + /// + private static Mock GetUserMock() + { + var userCache = new Dictionary(); + var userMock = new Mock(); + userMock.Setup(x => x.FromUserCache(It.IsAny())).Returns((string key) => userCache.TryGetValue(key, out var val) ? val is int[] iVal ? iVal : null : null); + userMock.Setup(x => x.ToUserCache(It.IsAny(), It.IsAny())).Callback((string key, int[] val) => userCache[key] = val); + return userMock; + } + private IUser GetAdminUser() { - var admin = MockedUser.GetUserMock(); + var admin = GetUserMock(); admin.Setup(x => x.Groups).Returns(new[] { new ReadOnlyUserGroup(1, "Admin", "icon-user", null, null, Constants.Security.AdminGroupAlias, new string[0], new string[0]) diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Services/AmbiguousEventTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Services/AmbiguousEventTests.cs new file mode 100644 index 0000000000..6eebf5d237 --- /dev/null +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Services/AmbiguousEventTests.cs @@ -0,0 +1,78 @@ +using System; +using System.Reflection; +using System.Text; +using NUnit.Framework; +using Umbraco.Core.Events; +using Umbraco.Core.Services.Implement; + +namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.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)); + } + } + } +} diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index b65cc2ff74..846b7b9fd2 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -140,6 +140,7 @@ + @@ -154,7 +155,6 @@ - From 7461a10c78024c254fb4c00d308baafcc216f345 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Tue, 6 Oct 2020 09:54:10 +0200 Subject: [PATCH 5/8] Migrated macro service tests to new integration tests project. --- .../Services/MacroServiceTests.cs | 149 ++++++++---------- .../Testing/UmbracoIntegrationTest.cs | 3 +- src/Umbraco.Tests/Umbraco.Tests.csproj | 1 - 3 files changed, 68 insertions(+), 85 deletions(-) rename src/{Umbraco.Tests => Umbraco.Tests.Integration}/Services/MacroServiceTests.cs (64%) diff --git a/src/Umbraco.Tests/Services/MacroServiceTests.cs b/src/Umbraco.Tests.Integration/Services/MacroServiceTests.cs similarity index 64% rename from src/Umbraco.Tests/Services/MacroServiceTests.cs rename to src/Umbraco.Tests.Integration/Services/MacroServiceTests.cs index ceecb72156..1909503308 100644 --- a/src/Umbraco.Tests/Services/MacroServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/MacroServiceTests.cs @@ -6,27 +6,33 @@ using NUnit.Framework; using Umbraco.Core.Cache; using Umbraco.Core.Logging; using Umbraco.Core.Models; - -using Umbraco.Core.Persistence.Repositories; using Umbraco.Core.Persistence.Repositories.Implement; using Umbraco.Core.Scoping; +using Umbraco.Core.Services; +using Umbraco.Core.Services.Implement; +using Umbraco.Tests.Common.Builders; +using Umbraco.Tests.Common.Builders.Extensions; +using Umbraco.Tests.Integration.Testing; using Umbraco.Tests.Testing; -namespace Umbraco.Tests.Services +namespace Umbraco.Tests.Integration.Services { [TestFixture] [Apartment(ApartmentState.STA)] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] - public class MacroServiceTests : TestWithSomeContentBase + public class MacroServiceTests : UmbracoIntegrationTest { - public override void CreateTestData() - { - base.CreateTestData(); + private MacroService _macroService => (MacroService)GetRequiredService(); - var provider = TestObjects.GetScopeProvider(Logger); - using (var scope = provider.CreateScope()) + [SetUp] + public override void Setup() + { + base.Setup(); + + var sp = ScopeProvider; + using (var scope = sp.CreateScope()) { - var repository = new MacroRepository((IScopeAccessor) provider, AppCaches.Disabled, Mock.Of(), ShortStringHelper); + var repository = new MacroRepository((IScopeAccessor) sp, AppCaches.Disabled, Mock.Of(), ShortStringHelper); repository.Save(new Macro(ShortStringHelper, "test1", "Test1", "~/views/macropartials/test1.cshtml")); repository.Save(new Macro(ShortStringHelper, "test2", "Test2", "~/views/macropartials/test2.cshtml")); @@ -35,22 +41,15 @@ namespace Umbraco.Tests.Services } } - [TearDown] - public override void TearDown() - { - base.TearDown(); - } - [Test] public void Can_Get_By_Alias() { // Arrange - var macroService = ServiceContext.MacroService; // Act - var macro = macroService.GetByAlias("test1"); + var macro = _macroService.GetByAlias("test1"); - //assert + // Assert Assert.IsNotNull(macro); Assert.AreEqual("Test1", macro.Name); } @@ -59,12 +58,11 @@ namespace Umbraco.Tests.Services public void Can_Get_All() { // Arrange - var macroService = ServiceContext.MacroService; // Act - var result = macroService.GetAll(); + var result = _macroService.GetAll(); - //assert + // Assert Assert.AreEqual(3, result.Count()); } @@ -72,23 +70,23 @@ namespace Umbraco.Tests.Services public void Can_Create() { // Arrange - var macroService = ServiceContext.MacroService; // Act - var macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); - macroService.Save(macro); + var macro = CreateMacro(); - //assert + _macroService.Save(macro); + + // Assert Assert.IsTrue(macro.HasIdentity); Assert.Greater(macro.Id, 0); Assert.AreNotEqual(Guid.Empty, macro.Key); - var result = macroService.GetById(macro.Id); + var result = _macroService.GetById(macro.Id); Assert.AreEqual("test", result.Alias); Assert.AreEqual("Test", result.Name); Assert.AreEqual("~/Views/MacroPartials/Test.cshtml", result.MacroSource); Assert.AreEqual(1234, result.CacheDuration); - result = macroService.GetById(macro.Key); + result = _macroService.GetById(macro.Key); Assert.AreEqual("test", result.Alias); Assert.AreEqual("Test", result.Name); Assert.AreEqual("~/Views/MacroPartials/Test.cshtml", result.MacroSource); @@ -99,18 +97,17 @@ namespace Umbraco.Tests.Services public void Can_Delete() { // Arrange - var macroService = ServiceContext.MacroService; var macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); - macroService.Save(macro); + _macroService.Save(macro); // Act - macroService.Delete(macro); + _macroService.Delete(macro); - //assert - var result = macroService.GetById(macro.Id); + // Assert + var result = _macroService.GetById(macro.Id); Assert.IsNull(result); - result = macroService.GetById(macro.Key); + result = _macroService.GetById(macro.Key); Assert.IsNull(result); } @@ -118,20 +115,18 @@ namespace Umbraco.Tests.Services public void Can_Update() { // Arrange - var macroService = ServiceContext.MacroService; - IMacro macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); - macroService.Save(macro); + var macro = CreateMacro(); + _macroService.Save(macro); // Act var currKey = macro.Key; macro.Name = "New name"; macro.Alias = "NewAlias"; - macroService.Save(macro); + _macroService.Save(macro); + macro = _macroService.GetById(macro.Id); - macro = macroService.GetById(macro.Id); - - //assert + // Assert Assert.AreEqual("New name", macro.Name); Assert.AreEqual("NewAlias", macro.Alias); Assert.AreEqual(currKey, macro.Key); @@ -142,10 +137,9 @@ namespace Umbraco.Tests.Services public void Can_Update_Property() { // Arrange - var macroService = ServiceContext.MacroService; - IMacro macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); + var macro = CreateMacro(); macro.Properties.Add(new MacroProperty("blah", "Blah", 0, "blah")); - macroService.Save(macro); + _macroService.Save(macro); Assert.AreNotEqual(Guid.Empty, macro.Properties[0].Key); @@ -155,11 +149,11 @@ namespace Umbraco.Tests.Services macro.Properties[0].Name = "new Name"; macro.Properties[0].SortOrder = 1; macro.Properties[0].EditorAlias = "new"; - macroService.Save(macro); + _macroService.Save(macro); - macro = macroService.GetById(macro.Id); + macro = _macroService.GetById(macro.Id); - //assert + // Assert Assert.AreEqual(1, macro.Properties.Count); Assert.AreEqual(currPropKey, macro.Properties[0].Key); Assert.AreEqual("new Alias", macro.Properties[0].Alias); @@ -173,12 +167,11 @@ namespace Umbraco.Tests.Services public void Can_Update_Remove_Property() { // Arrange - var macroService = ServiceContext.MacroService; - IMacro macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); + var macro = CreateMacro(); macro.Properties.Add(new MacroProperty("blah1", "Blah1", 0, "blah1")); macro.Properties.Add(new MacroProperty("blah2", "Blah2", 1, "blah2")); macro.Properties.Add(new MacroProperty("blah3", "Blah3", 2, "blah3")); - macroService.Save(macro); + _macroService.Save(macro); var lastKey = macro.Properties[0].Key; for (var i = 1; i < macro.Properties.Count; i++) @@ -197,11 +190,11 @@ namespace Umbraco.Tests.Services var allPropKeys = macro.Properties.Values.Select(x => new { x.Alias, x.Key }).ToArray(); - macroService.Save(macro); + _macroService.Save(macro); - macro = macroService.GetById(macro.Id); + macro = _macroService.GetById(macro.Id); - //assert + // Assert Assert.AreEqual(2, macro.Properties.Count); Assert.AreEqual("newAlias", macro.Properties["newAlias"].Alias); Assert.AreEqual("new Name", macro.Properties["newAlias"].Name); @@ -211,39 +204,36 @@ namespace Umbraco.Tests.Services { Assert.AreEqual(propKey.Key, macro.Properties[propKey.Alias].Key); } - } [Test] public void Can_Add_And_Remove_Properties() { - var macroService = ServiceContext.MacroService; - var macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); + var macro = CreateMacro(); - //adds some properties + // Adds some properties macro.Properties.Add(new MacroProperty("blah1", "Blah1", 0, "blah1")); macro.Properties.Add(new MacroProperty("blah2", "Blah2", 0, "blah2")); macro.Properties.Add(new MacroProperty("blah3", "Blah3", 0, "blah3")); macro.Properties.Add(new MacroProperty("blah4", "Blah4", 0, "blah4")); - macroService.Save(macro); + _macroService.Save(macro); - var result1 = macroService.GetById(macro.Id); + var result1 = _macroService.GetById(macro.Id); Assert.AreEqual(4, result1.Properties.Values.Count()); - //simulate clearing the sections + // Simulate clearing the sections foreach (var s in result1.Properties.Values.ToArray()) { result1.Properties.Remove(s.Alias); } - //now just re-add a couple + + // Now just re-add a couple result1.Properties.Add(new MacroProperty("blah3", "Blah3", 0, "blah3")); result1.Properties.Add(new MacroProperty("blah4", "Blah4", 0, "blah4")); - macroService.Save(result1); + _macroService.Save(result1); - //assert - - //re-get - result1 = macroService.GetById(result1.Id); + // Assert + result1 = _macroService.GetById(result1.Id); Assert.AreEqual(2, result1.Properties.Values.Count()); } @@ -252,25 +242,20 @@ namespace Umbraco.Tests.Services public void Cannot_Save_Macro_With_Empty_Name() { // Arrange - var macroService = ServiceContext.MacroService; - var macro = new Macro(ShortStringHelper, "test", string.Empty, "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); + var macro = CreateMacro(name: string.Empty); // Act & Assert - Assert.Throws(() => macroService.Save(macro)); + Assert.Throws(() => _macroService.Save(macro)); } - //[Test] - //public void Can_Get_Many_By_Alias() - //{ - // // Arrange - // var macroService = ServiceContext.MacroService; - - // // Act - // var result = macroService.GetAll("test1", "test2"); - - // //assert - // Assert.AreEqual(2, result.Count()); - //} - + private static IMacro CreateMacro(string name = "Test") + { + return new MacroBuilder() + .WithAlias("test") + .WithName(name) + .WithSource("~/Views/MacroPartials/Test.cshtml") + .WithCacheDuration(1234) + .Build(); + } } } diff --git a/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs b/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs index 4448308a2e..01871641ac 100644 --- a/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs +++ b/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs @@ -45,8 +45,7 @@ namespace Umbraco.Tests.Integration.Testing [SingleThreaded] [NonParallelizable] public abstract class UmbracoIntegrationTest - { - + { public static LightInjectContainer CreateUmbracoContainer(out UmbracoServiceProviderFactory serviceProviderFactory) { var container = UmbracoServiceProviderFactory.CreateServiceContainer(); diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 846b7b9fd2..8863b8ccb5 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -271,7 +271,6 @@ - From 11dc3459bc1c4ddb08c4a9c495e8535d8377c9bf Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Tue, 6 Oct 2020 12:02:29 +0200 Subject: [PATCH 6/8] Further update to user service and controller tests to use test builders. --- .../Builders/UserGroupBuilder.cs | 14 ++- .../Services/UserServiceTests.cs | 17 ++-- .../Builders/UserGroupBuilderTests.cs | 4 +- .../Controllers/ContentControllerUnitTests.cs | 88 ++++++++----------- .../Controllers/MediaControllerUnitTests.cs | 50 +++++------ ...terAllowedOutgoingContentAttributeTests.cs | 28 +++--- 6 files changed, 93 insertions(+), 108 deletions(-) diff --git a/src/Umbraco.Tests.Common/Builders/UserGroupBuilder.cs b/src/Umbraco.Tests.Common/Builders/UserGroupBuilder.cs index 6bdc766ee2..a5a96786ce 100644 --- a/src/Umbraco.Tests.Common/Builders/UserGroupBuilder.cs +++ b/src/Umbraco.Tests.Common/Builders/UserGroupBuilder.cs @@ -26,7 +26,7 @@ namespace Umbraco.Tests.Common.Builders private string _icon; private string _name; private IEnumerable _permissions = Enumerable.Empty(); - private IEnumerable _sectionCollection = Enumerable.Empty(); + private IEnumerable _allowedSections = Enumerable.Empty(); private string _suffix; private int? _startContentId; private int? _startMediaId; @@ -55,7 +55,7 @@ namespace Umbraco.Tests.Common.Builders public UserGroupBuilder WithPermissions(string permissions) { - _permissions = permissions.Split(); + _permissions = permissions.ToCharArray().Select(x => x.ToString()); return this; } @@ -65,6 +65,12 @@ namespace Umbraco.Tests.Common.Builders return this; } + public UserGroupBuilder WithAllowedSections(IList allowedSections) + { + _allowedSections = allowedSections; + return this; + } + public UserGroupBuilder WithStartContentId(int startContentId) { _startContentId = startContentId; @@ -107,9 +113,9 @@ namespace Umbraco.Tests.Common.Builders userGroup.StartContentId = startContentId; userGroup.StartMediaId = startMediaId; - foreach (var item in _sectionCollection) + foreach (var section in _allowedSections) { - userGroup.AddAllowedSection(item); + userGroup.AddAllowedSection(section); } return userGroup; diff --git a/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs b/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs index ba7dfae0f5..e0f2e423e1 100644 --- a/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs @@ -1052,19 +1052,16 @@ namespace Umbraco.Tests.Integration.Services private UserGroup CreateTestUserGroup(string alias = "testGroup", string name = "Test Group") { - var userGroup = new UserGroup(ShortStringHelper) - { - Alias = alias, - Name = name, - Permissions = "ABCDEFGHIJ1234567".ToCharArray().Select(x => x.ToString()) - }; - - userGroup.AddAllowedSection("content"); - userGroup.AddAllowedSection("media"); + var userGroup = new UserGroupBuilder() + .WithAlias(alias) + .WithName(name) + .WithPermissions("ABCDEFGHIJ1234567") + .WithAllowedSections(new[] { "content", "media" }) + .Build(); _userService.Save(userGroup); - return userGroup; + return (UserGroup)userGroup; } } } diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/UserGroupBuilderTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/UserGroupBuilderTests.cs index 2cd2887878..0cfa0649ff 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/UserGroupBuilderTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/UserGroupBuilderTests.cs @@ -1,4 +1,4 @@ -using System; +using System.Linq; using NUnit.Framework; using Umbraco.Tests.Common.Builders; using Umbraco.Tests.Common.Builders.Extensions; @@ -41,7 +41,7 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Tests.Common.Builders Assert.AreEqual(testName, userGroup.Name); Assert.AreEqual(testUserCount, userGroup.UserCount); Assert.AreEqual(testIcon, userGroup.Icon); - Assert.AreEqual(testPermissions, string.Join(string.Empty, userGroup.Permissions)); + Assert.AreEqual(testPermissions.Length, userGroup.Permissions.Count()); Assert.AreEqual(testStartContentId, userGroup.StartContentId); Assert.AreEqual(testStartMediaId, userGroup.StartMediaId); } diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/ContentControllerUnitTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/ContentControllerUnitTests.cs index 56f52215a2..14d888470f 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/ContentControllerUnitTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/ContentControllerUnitTests.cs @@ -1,27 +1,23 @@ -using System.Collections.Generic; -using Moq; +using Moq; using NUnit.Framework; using Umbraco.Core.Models; using Umbraco.Core.Models.Entities; using Umbraco.Core.Models.Membership; using Umbraco.Core.Security; using Umbraco.Core.Services; -using Umbraco.Tests.Common.TestHelpers.Entities; +using Umbraco.Tests.Common.Builders; +using Umbraco.Tests.Common.Builders.Extensions; namespace Umbraco.Tests.Web.Controllers { [TestFixture] public class ContentControllerUnitTests { - [Test] public void Access_Allowed_By_Path() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(9); - userMock.Setup(u => u.Groups).Returns(new[] { new ReadOnlyUserGroup(1, "admin", "", -1, -1, "admin", new string[0], new List()) }); - var user = userMock.Object; + var user = CreateUser(id: 9); var contentMock = new Mock(); contentMock.Setup(c => c.Path).Returns("-1,1234,5678"); var content = contentMock.Object; @@ -44,9 +40,7 @@ namespace Umbraco.Tests.Web.Controllers public void No_Content_Found() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(9); - var user = userMock.Object; + var user = CreateUser(id: 9); var contentMock = new Mock(); contentMock.Setup(c => c.Path).Returns("-1,1234,5678"); var content = contentMock.Object; @@ -72,10 +66,7 @@ namespace Umbraco.Tests.Web.Controllers public void No_Access_By_Path() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(9); - userMock.Setup(u => u.StartContentIds).Returns(new[] { 9876 }); - var user = userMock.Object; + var user = CreateUser(id: 9, startContentId: 9876); var contentMock = new Mock(); contentMock.Setup(c => c.Path).Returns("-1,1234,5678"); var content = contentMock.Object; @@ -103,9 +94,7 @@ namespace Umbraco.Tests.Web.Controllers public void No_Access_By_Permission() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(9); - var user = userMock.Object; + var user = CreateUser(id: 9); var contentMock = new Mock(); contentMock.Setup(c => c.Path).Returns("-1,1234,5678"); var content = contentMock.Object; @@ -134,10 +123,7 @@ namespace Umbraco.Tests.Web.Controllers public void Access_Allowed_By_Permission() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(9); - userMock.Setup(u => u.Groups).Returns(new[] { new ReadOnlyUserGroup(1, "admin", "", -1, -1, "admin", new string[0], new List()) }); - var user = userMock.Object; + var user = CreateUser(id: 9); var contentMock = new Mock(); contentMock.Setup(c => c.Path).Returns("-1,1234,5678"); var content = contentMock.Object; @@ -166,10 +152,7 @@ namespace Umbraco.Tests.Web.Controllers public void Access_To_Root_By_Path() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(0); - userMock.Setup(u => u.Groups).Returns(new[] { new ReadOnlyUserGroup(1, "admin", "", -1, -1, "admin", new string[0], new List()) }); - var user = userMock.Object; + var user = CreateUser(); var contentServiceMock = new Mock(); var contentService = contentServiceMock.Object; var userServiceMock = new Mock(); @@ -188,10 +171,7 @@ namespace Umbraco.Tests.Web.Controllers public void Access_To_Recycle_Bin_By_Path() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(0); - userMock.Setup(u => u.Groups).Returns(new[] { new ReadOnlyUserGroup(1, "admin", "", -1, -1, "admin", new string[0], new List()) }); - var user = userMock.Object; + var user = CreateUser(); var contentServiceMock = new Mock(); var contentService = contentServiceMock.Object; var userServiceMock = new Mock(); @@ -210,10 +190,7 @@ namespace Umbraco.Tests.Web.Controllers public void No_Access_To_Recycle_Bin_By_Path() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(0); - userMock.Setup(u => u.StartContentIds).Returns(new[] { 1234 }); - var user = userMock.Object; + var user = CreateUser(startContentId: 1234); var contentServiceMock = new Mock(); var contentService = contentServiceMock.Object; var userServiceMock = new Mock(); @@ -234,10 +211,8 @@ namespace Umbraco.Tests.Web.Controllers public void No_Access_To_Root_By_Path() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(0); - userMock.Setup(u => u.StartContentIds).Returns(new[] { 1234 }); - var user = userMock.Object; + var user = CreateUser(startContentId: 1234); + var contentServiceMock = new Mock(); var contentService = contentServiceMock.Object; var userServiceMock = new Mock(); @@ -258,10 +233,7 @@ namespace Umbraco.Tests.Web.Controllers public void Access_To_Root_By_Permission() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(0); - userMock.Setup(u => u.Groups).Returns(new[] { new ReadOnlyUserGroup(1, "admin", "", -1, -1, "admin", new string[0], new List()) }); - var user = userMock.Object; + var user = CreateUser(); var userServiceMock = new Mock(); var permissions = new EntityPermissionCollection @@ -276,7 +248,6 @@ namespace Umbraco.Tests.Web.Controllers var entityServiceMock = new Mock(); var entityService = entityServiceMock.Object; - //act var result = ContentPermissionsHelper.CheckPermissions(-1, user, userService, contentService, entityService, out var foundContent, new[] { 'A' }); @@ -288,9 +259,7 @@ namespace Umbraco.Tests.Web.Controllers public void No_Access_To_Root_By_Permission() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(0); - var user = userMock.Object; + var user = CreateUser(withUserGroup: false); var userServiceMock = new Mock(); var permissions = new EntityPermissionCollection @@ -316,10 +285,7 @@ namespace Umbraco.Tests.Web.Controllers public void Access_To_Recycle_Bin_By_Permission() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(0); - userMock.Setup(u => u.Groups).Returns(new[] { new ReadOnlyUserGroup(1, "admin", "", -1, -1, "admin", new string[0], new List()) }); - var user = userMock.Object; + var user = CreateUser(); var userServiceMock = new Mock(); var permissions = new EntityPermissionCollection @@ -346,9 +312,7 @@ namespace Umbraco.Tests.Web.Controllers public void No_Access_To_Recycle_Bin_By_Permission() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(0); - var user = userMock.Object; + var user = CreateUser(withUserGroup: false); var userServiceMock = new Mock(); var permissions = new EntityPermissionCollection @@ -369,6 +333,24 @@ namespace Umbraco.Tests.Web.Controllers //assert Assert.AreEqual(ContentPermissionsHelper.ContentAccess.Denied, result); } + + private IUser CreateUser(int id = 0, int startContentId = -1, bool withUserGroup = true) + { + var builder = new UserBuilder() + .WithId(id) + .WithStartContentIds(startContentId == -1 ? new int[0] : new[] { startContentId }); + if (withUserGroup) + { + builder = builder + .AddUserGroup() + .WithId(1) + .WithName("admin") + .WithAlias("admin") + .Done(); + } + + return builder.Build(); + } } //NOTE: The below self hosted stuff does work so need to get some tests written. Some are not possible atm because diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/MediaControllerUnitTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/MediaControllerUnitTests.cs index aef08cec5a..124c934b2f 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/MediaControllerUnitTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/MediaControllerUnitTests.cs @@ -5,7 +5,8 @@ using Umbraco.Core.Models; using Umbraco.Core.Models.Entities; using Umbraco.Core.Models.Membership; using Umbraco.Core.Services; -using Umbraco.Tests.Common.TestHelpers.Entities; +using Umbraco.Tests.Common.Builders; +using Umbraco.Tests.Common.Builders.Extensions; using Umbraco.Web.BackOffice.Controllers; using Umbraco.Web.Common.Exceptions; @@ -18,10 +19,7 @@ namespace Umbraco.Tests.Web.Controllers public void Access_Allowed_By_Path() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(9); - userMock.Setup(u => u.Groups).Returns(new[] { new ReadOnlyUserGroup(1, "admin", "", -1, -1, "admin", new string[0], new List()) }); - var user = userMock.Object; + var user = CreateUser(id: 9); var mediaMock = new Mock(); mediaMock.Setup(m => m.Path).Returns("-1,1234,5678"); var media = mediaMock.Object; @@ -42,9 +40,7 @@ namespace Umbraco.Tests.Web.Controllers public void Throws_Exception_When_No_Media_Found() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(9); - var user = userMock.Object; + var user = CreateUser(id: 9); var mediaMock = new Mock(); mediaMock.Setup(m => m.Path).Returns("-1,1234,5678"); var media = mediaMock.Object; @@ -62,10 +58,7 @@ namespace Umbraco.Tests.Web.Controllers public void No_Access_By_Path() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(9); - userMock.Setup(u => u.StartMediaIds).Returns(new[] { 9876 }); - var user = userMock.Object; + var user = CreateUser(id: 9, startMediaId: 9876); var mediaMock = new Mock(); mediaMock.Setup(m => m.Path).Returns("-1,1234,5678"); var media = mediaMock.Object; @@ -88,10 +81,7 @@ namespace Umbraco.Tests.Web.Controllers public void Access_To_Root_By_Path() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(0); - userMock.Setup(u => u.Groups).Returns(new[] { new ReadOnlyUserGroup(1, "admin", "", -1, -1, "admin", new string[0], new List()) }); - var user = userMock.Object; + var user = CreateUser(); var mediaServiceMock = new Mock(); var mediaService = mediaServiceMock.Object; var entityServiceMock = new Mock(); @@ -108,10 +98,7 @@ namespace Umbraco.Tests.Web.Controllers public void No_Access_To_Root_By_Path() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(0); - userMock.Setup(u => u.StartMediaIds).Returns(new[] { 1234 }); - var user = userMock.Object; + var user = CreateUser(startMediaId: 1234); var mediaServiceMock = new Mock(); var mediaService = mediaServiceMock.Object; var entityServiceMock = new Mock(); @@ -130,10 +117,7 @@ namespace Umbraco.Tests.Web.Controllers public void Access_To_Recycle_Bin_By_Path() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(0); - userMock.Setup(u => u.Groups).Returns(new[] { new ReadOnlyUserGroup(1, "admin", "", -1, -1, "admin", new string[0], new List()) }); - var user = userMock.Object; + var user = CreateUser(); var mediaServiceMock = new Mock(); var mediaService = mediaServiceMock.Object; var entityServiceMock = new Mock(); @@ -150,10 +134,7 @@ namespace Umbraco.Tests.Web.Controllers public void No_Access_To_Recycle_Bin_By_Path() { //arrange - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(0); - userMock.Setup(u => u.StartMediaIds).Returns(new[] { 1234 }); - var user = userMock.Object; + var user = CreateUser(startMediaId: 1234); var mediaServiceMock = new Mock(); var mediaService = mediaServiceMock.Object; var entityServiceMock = new Mock(); @@ -167,5 +148,18 @@ namespace Umbraco.Tests.Web.Controllers //assert Assert.IsFalse(result); } + + private IUser CreateUser(int id = 0, int startMediaId = -1) + { + return new UserBuilder() + .WithId(id) + .WithStartMediaIds(new[] { startMediaId }) + .AddUserGroup() + .WithId(1) + .WithName("admin") + .WithAlias("admin") + .Done() + .Build(); + } } } diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Filters/FilterAllowedOutgoingContentAttributeTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Filters/FilterAllowedOutgoingContentAttributeTests.cs index f6e551dc27..6717b0e046 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Filters/FilterAllowedOutgoingContentAttributeTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Filters/FilterAllowedOutgoingContentAttributeTests.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using System.Linq; -using System.Net.Http.Headers; using Microsoft.AspNetCore.Mvc; using Moq; using NUnit.Framework; @@ -10,11 +9,11 @@ using Umbraco.Core.Models.Entities; using Umbraco.Core.Models.Membership; using Umbraco.Core.Security; using Umbraco.Core.Services; -using Umbraco.Tests.Common.TestHelpers.Entities; +using Umbraco.Tests.Common.Builders; +using Umbraco.Tests.Common.Builders.Extensions; using Umbraco.Web.Actions; using Umbraco.Web.BackOffice.Filters; using Umbraco.Web.Models.ContentEditing; -using Umbraco.Web.Security; namespace Umbraco.Tests.Web.Controllers { @@ -78,10 +77,7 @@ namespace Umbraco.Tests.Web.Controllers [Test] public void Filter_On_Start_Node() { - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(9); - userMock.Setup(u => u.StartContentIds).Returns(new[] { 5 }); - var user = userMock.Object; + var user = CreateUser(id: 9, startContentId: 5); var userServiceMock = new Mock(); var userService = userServiceMock.Object; var entityServiceMock = new Mock(); @@ -124,10 +120,7 @@ namespace Umbraco.Tests.Web.Controllers } var ids = list.Select(x => (int)x.Id).ToArray(); - var userMock = MockedUser.GetUserMock(); - userMock.Setup(u => u.Id).Returns(9); - userMock.Setup(u => u.StartContentIds).Returns(new int[0]); - var user = userMock.Object; + var user = CreateUser(id: 9, startContentId: 0); var userServiceMock = new Mock(); //we're only assigning 3 nodes browse permissions so that is what we expect as a result @@ -155,6 +148,19 @@ namespace Umbraco.Tests.Web.Controllers Assert.AreEqual(3, list.ElementAt(2).Id); } + private IUser CreateUser(int id = 0, int startContentId = -1) + { + return new UserBuilder() + .WithId(id) + .WithStartContentIds(new[] { startContentId }) + .AddUserGroup() + .WithId(1) + .WithName("admin") + .WithAlias("admin") + .Done() + .Build(); + } + private class MyTestClass { public IEnumerable MyList { get; set; } From 0e5c013c36bb4982b7a3e584ff8470c3529034e4 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Tue, 6 Oct 2020 12:43:24 +0200 Subject: [PATCH 7/8] Updated from PR review. --- .../Services/ContentServiceEventTests.cs | 66 ++-- .../Services/LocalizationServiceTests.cs | 130 +++---- .../Services/MacroServiceTests.cs | 48 +-- .../Services/MediaServiceTests.cs | 82 ++--- .../Services/PublicAccessServiceTests.cs | 54 +-- .../Services/SectionServiceTests.cs | 14 +- .../Services/TagServiceTests.cs | 50 +-- .../Services/UserServiceTests.cs | 324 +++++++++--------- 8 files changed, 384 insertions(+), 384 deletions(-) diff --git a/src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs b/src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs index e2b6d20d82..5afc045231 100644 --- a/src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs +++ b/src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs @@ -20,16 +20,16 @@ namespace Umbraco.Tests.Integration.Services Logger = UmbracoTestOptions.Logger.Console)] public class ContentServiceEventTests : UmbracoIntegrationTest { - private IContentTypeService _contentTypeService => GetRequiredService(); - private IContentService _contentService => GetRequiredService(); - private ILocalizationService _localizationService => GetRequiredService(); - private IFileService _fileService => GetRequiredService(); + private IContentTypeService ContentTypeService => GetRequiredService(); + private ContentService ContentService => (ContentService)GetRequiredService(); + private ILocalizationService LocalizationService => GetRequiredService(); + private IFileService FileService => GetRequiredService(); private GlobalSettings _globalSettings; - public override void Setup() + [SetUp] + public void SetupTest() { - base.Setup(); ContentRepositoryBase.ThrowOnWarning = true; _globalSettings = new GlobalSettings(); } @@ -43,22 +43,22 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Saving_Culture() { - _localizationService.Save(new Language(_globalSettings, "fr-FR")); + LocalizationService.Save(new Language(_globalSettings, "fr-FR")); var contentType = MockedContentTypes.CreateTextPageContentType(); - _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); 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"); @@ -87,7 +87,7 @@ namespace Umbraco.Tests.Integration.Services ContentService.Saved += OnSaved; try { - _contentService.Save(document); + ContentService.Save(document); } finally { @@ -100,8 +100,8 @@ namespace Umbraco.Tests.Integration.Services public void Saving_Set_Value() { var contentType = MockedContentTypes.CreateTextPageContentType(); - _fileService.SaveTemplate(contentType.DefaultTemplate); - _contentTypeService.Save(contentType); + FileService.SaveTemplate(contentType.DefaultTemplate); + ContentTypeService.Save(contentType); IContent document = new Content("content", -1, contentType); @@ -131,7 +131,7 @@ namespace Umbraco.Tests.Integration.Services ContentService.Saved += OnSaved; try { - _contentService.Save(document); + ContentService.Save(document); } finally { @@ -143,25 +143,25 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Publishing_Culture() { - _localizationService.Save(new Language(_globalSettings, "fr-FR")); + LocalizationService.Save(new Language(_globalSettings, "fr-FR")); var contentType = MockedContentTypes.CreateTextPageContentType(); - _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); 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) { @@ -187,7 +187,7 @@ namespace Umbraco.Tests.Integration.Services ContentService.Published += OnPublished; try { - _contentService.SaveAndPublish(document, "fr-FR"); + ContentService.SaveAndPublish(document, "fr-FR"); } finally { @@ -195,7 +195,7 @@ namespace Umbraco.Tests.Integration.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")); @@ -206,8 +206,8 @@ namespace Umbraco.Tests.Integration.Services public void Publishing_Set_Value() { var contentType = MockedContentTypes.CreateTextPageContentType(); - _fileService.SaveTemplate(contentType.DefaultTemplate); - _contentTypeService.Save(contentType); + FileService.SaveTemplate(contentType.DefaultTemplate); + ContentTypeService.Save(contentType); IContent document = new Content("content", -1, contentType); @@ -240,7 +240,7 @@ namespace Umbraco.Tests.Integration.Services ContentService.Saved += OnSaved; try { - _contentService.SaveAndPublish(document); + ContentService.SaveAndPublish(document); } finally { @@ -255,12 +255,12 @@ namespace Umbraco.Tests.Integration.Services var contentType = MockedContentTypes.CreateTextPageContentType(); var titleProperty = contentType.PropertyTypes.First(x => x.Alias == "title"); titleProperty.Mandatory = true; // make this required! - _fileService.SaveTemplate(contentType.DefaultTemplate); - _contentTypeService.Save(contentType); + 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); @@ -283,7 +283,7 @@ namespace Umbraco.Tests.Integration.Services 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 @@ -295,16 +295,16 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Unpublishing_Culture() { - _localizationService.Save(new Language(_globalSettings, "fr-FR")); + LocalizationService.Save(new Language(_globalSettings, "fr-FR")); var contentType = MockedContentTypes.CreateTextPageContentType(); - _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)_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/LocalizationServiceTests.cs b/src/Umbraco.Tests.Integration/Services/LocalizationServiceTests.cs index 5c17666619..83d3476324 100644 --- a/src/Umbraco.Tests.Integration/Services/LocalizationServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/LocalizationServiceTests.cs @@ -31,7 +31,7 @@ namespace Umbraco.Tests.Integration.Services private int _englishLangId; private GlobalSettings _globalSettings; - private ILocalizationService _localizationService => GetRequiredService(); + private ILocalizationService LocalizationService => GetRequiredService(); [SetUp] public void SetUp() @@ -43,7 +43,7 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_Get_Root_Dictionary_Items() { - var rootItems = _localizationService.GetRootDictionaryItems(); + var rootItems = LocalizationService.GetRootDictionaryItems(); Assert.NotNull(rootItems); Assert.IsTrue(rootItems.Any()); @@ -52,14 +52,14 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_Determint_If_DictionaryItem_Exists() { - var exists = _localizationService.DictionaryItemExists("Parent"); + var exists = LocalizationService.DictionaryItemExists("Parent"); Assert.IsTrue(exists); } [Test] public void Can_Get_All_Languages() { - var languages = _localizationService.GetAllLanguages(); + var languages = LocalizationService.GetAllLanguages(); Assert.NotNull(languages); Assert.IsTrue(languages.Any()); Assert.That(languages.Count(), Is.EqualTo(3)); @@ -68,37 +68,37 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_Get_Dictionary_Item_By_Int_Id() { - var parentItem = _localizationService.GetDictionaryItemById(_parentItemIntId); + var parentItem = LocalizationService.GetDictionaryItemById(_parentItemIntId); Assert.NotNull(parentItem); - var childItem = _localizationService.GetDictionaryItemById(_childItemIntId); + var childItem = LocalizationService.GetDictionaryItemById(_childItemIntId); Assert.NotNull(childItem); } [Test] public void Can_Get_Dictionary_Item_By_Guid_Id() { - var parentItem = _localizationService.GetDictionaryItemById(_parentItemGuidId); + var parentItem = LocalizationService.GetDictionaryItemById(_parentItemGuidId); Assert.NotNull(parentItem); - var childItem = _localizationService.GetDictionaryItemById(_childItemGuidId); + var childItem = LocalizationService.GetDictionaryItemById(_childItemGuidId); Assert.NotNull(childItem); } [Test] public void Can_Get_Dictionary_Item_By_Key() { - var parentItem = _localizationService.GetDictionaryItemByKey("Parent"); + var parentItem = LocalizationService.GetDictionaryItemByKey("Parent"); Assert.NotNull(parentItem); - var childItem = _localizationService.GetDictionaryItemByKey("Child"); + var childItem = LocalizationService.GetDictionaryItemByKey("Child"); Assert.NotNull(childItem); } [Test] public void Can_Get_Dictionary_Item_Children() { - var item = _localizationService.GetDictionaryItemChildren(_parentItemGuidId); + var item = LocalizationService.GetDictionaryItemChildren(_parentItemGuidId); Assert.NotNull(item); Assert.That(item.Count(), Is.EqualTo(1)); @@ -114,8 +114,8 @@ namespace Umbraco.Tests.Integration.Services { using (var scope = ScopeProvider.CreateScope()) { - var en = _localizationService.GetLanguageById(_englishLangId); - var dk = _localizationService.GetLanguageById(_danishLangId); + var en = LocalizationService.GetLanguageById(_englishLangId); + var dk = LocalizationService.GetLanguageById(_danishLangId); var currParentId = _childItemGuidId; for (var i = 0; i < 25; i++) @@ -137,8 +137,8 @@ namespace Umbraco.Tests.Integration.Services new DictionaryTranslation(dk, "BørnVærdi2 " + i) } }; - _localizationService.Save(desc1); - _localizationService.Save(desc2); + LocalizationService.Save(desc1); + LocalizationService.Save(desc2); currParentId = desc1.Key; } @@ -146,7 +146,7 @@ namespace Umbraco.Tests.Integration.Services scope.Database.AsUmbracoDatabase().EnableSqlTrace = true; scope.Database.AsUmbracoDatabase().EnableSqlCount = true; - var items = _localizationService.GetDictionaryItemDescendants(_parentItemGuidId).ToArray(); + var items = LocalizationService.GetDictionaryItemDescendants(_parentItemGuidId).ToArray(); Debug.WriteLine("SQL CALLS: " + scope.Database.AsUmbracoDatabase().SqlCount); @@ -159,8 +159,8 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_GetLanguageById() { - var danish = _localizationService.GetLanguageById(_danishLangId); - var english = _localizationService.GetLanguageById(_englishLangId); + var danish = LocalizationService.GetLanguageById(_danishLangId); + var english = LocalizationService.GetLanguageById(_englishLangId); Assert.NotNull(danish); Assert.NotNull(english); } @@ -168,8 +168,8 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_GetLanguageByIsoCode() { - var danish = _localizationService.GetLanguageByIsoCode("da-DK"); - var english = _localizationService.GetLanguageByIsoCode("en-GB"); + var danish = LocalizationService.GetLanguageByIsoCode("da-DK"); + var english = LocalizationService.GetLanguageByIsoCode("en-GB"); Assert.NotNull(danish); Assert.NotNull(english); } @@ -177,14 +177,14 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Does_Not_Fail_When_Language_Doesnt_Exist() { - var language = _localizationService.GetLanguageByIsoCode("sv-SE"); + var language = LocalizationService.GetLanguageByIsoCode("sv-SE"); Assert.Null(language); } [Test] public void Does_Not_Fail_When_DictionaryItem_Doesnt_Exist() { - var item = _localizationService.GetDictionaryItemByKey("RandomKey"); + var item = LocalizationService.GetDictionaryItemByKey("RandomKey"); Assert.Null(item); } @@ -192,34 +192,34 @@ namespace Umbraco.Tests.Integration.Services public void Can_Delete_Language() { var norwegian = new Language(_globalSettings, "nb-NO") { CultureName = "Norwegian" }; - _localizationService.Save(norwegian, 0); + LocalizationService.Save(norwegian, 0); Assert.That(norwegian.HasIdentity, Is.True); var languageId = norwegian.Id; - _localizationService.Delete(norwegian); + LocalizationService.Delete(norwegian); - var language = _localizationService.GetLanguageById(languageId); + var language = LocalizationService.GetLanguageById(languageId); Assert.Null(language); } [Test] public void Can_Delete_Language_Used_As_Fallback() { - var danish = _localizationService.GetLanguageByIsoCode("da-DK"); + var danish = LocalizationService.GetLanguageByIsoCode("da-DK"); var norwegian = new Language(_globalSettings, "nb-NO") { CultureName = "Norwegian", FallbackLanguageId = danish.Id }; - _localizationService.Save(norwegian, 0); + LocalizationService.Save(norwegian, 0); var languageId = danish.Id; - _localizationService.Delete(danish); + LocalizationService.Delete(danish); - var language = _localizationService.GetLanguageById(languageId); + var language = LocalizationService.GetLanguageById(languageId); Assert.Null(language); } [Test] public void Can_Create_DictionaryItem_At_Root() { - var english = _localizationService.GetLanguageByIsoCode("en-US"); + var english = LocalizationService.GetLanguageByIsoCode("en-US"); var item = (IDictionaryItem)new DictionaryItem("Testing123") { @@ -228,10 +228,10 @@ namespace Umbraco.Tests.Integration.Services new DictionaryTranslation(english, "Hello world") } }; - _localizationService.Save(item); + LocalizationService.Save(item); //re-get - item = _localizationService.GetDictionaryItemById(item.Id); + item = LocalizationService.GetDictionaryItemById(item.Id); Assert.Greater(item.Id, 0); Assert.IsTrue(item.HasIdentity); @@ -243,18 +243,18 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_Create_DictionaryItem_At_Root_With_Identity() { - var item = _localizationService.CreateDictionaryItemWithIdentity( + var item = LocalizationService.CreateDictionaryItemWithIdentity( "Testing12345", null, "Hellooooo"); //re-get - item = _localizationService.GetDictionaryItemById(item.Id); + item = LocalizationService.GetDictionaryItemById(item.Id); Assert.IsNotNull(item); Assert.Greater(item.Id, 0); Assert.IsTrue(item.HasIdentity); Assert.IsFalse(item.ParentId.HasValue); Assert.AreEqual("Testing12345", item.ItemKey); - var allLangs = _localizationService.GetAllLanguages(); + var allLangs = LocalizationService.GetAllLanguages(); Assert.Greater(allLangs.Count(), 0); foreach (var language in allLangs) { @@ -265,20 +265,20 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_Add_Translation_To_Existing_Dictionary_Item() { - var english = _localizationService.GetLanguageByIsoCode("en-US"); + var english = LocalizationService.GetLanguageByIsoCode("en-US"); var item = (IDictionaryItem) new DictionaryItem("Testing123"); - _localizationService.Save(item); + LocalizationService.Save(item); //re-get - item = _localizationService.GetDictionaryItemById(item.Id); + item = LocalizationService.GetDictionaryItemById(item.Id); item.Translations = new List { new DictionaryTranslation(english, "Hello world") }; - _localizationService.Save(item); + LocalizationService.Save(item); Assert.AreEqual(1, item.Translations.Count()); foreach (var translation in item.Translations) @@ -289,14 +289,14 @@ namespace Umbraco.Tests.Integration.Services item.Translations = new List(item.Translations) { new DictionaryTranslation( - _localizationService.GetLanguageByIsoCode("en-GB"), + LocalizationService.GetLanguageByIsoCode("en-GB"), "My new value") }; - _localizationService.Save(item); + LocalizationService.Save(item); //re-get - item = _localizationService.GetDictionaryItemById(item.Id); + item = LocalizationService.GetDictionaryItemById(item.Id); Assert.AreEqual(2, item.Translations.Count()); Assert.AreEqual("Hello world", item.Translations.First().Value); @@ -306,27 +306,27 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_Delete_DictionaryItem() { - var item = _localizationService.GetDictionaryItemByKey("Child"); + var item = LocalizationService.GetDictionaryItemByKey("Child"); Assert.NotNull(item); - _localizationService.Delete(item); + LocalizationService.Delete(item); - var deletedItem = _localizationService.GetDictionaryItemByKey("Child"); + var deletedItem = LocalizationService.GetDictionaryItemByKey("Child"); Assert.Null(deletedItem); } [Test] public void Can_Update_Existing_DictionaryItem() { - var item = _localizationService.GetDictionaryItemByKey("Child"); + var item = LocalizationService.GetDictionaryItemByKey("Child"); foreach (var translation in item.Translations) { translation.Value = translation.Value + "UPDATED"; } - _localizationService.Save(item); + LocalizationService.Save(item); - var updatedItem = _localizationService.GetDictionaryItemByKey("Child"); + var updatedItem = LocalizationService.GetDictionaryItemByKey("Child"); Assert.NotNull(updatedItem); foreach (var translation in updatedItem.Translations) @@ -339,7 +339,7 @@ namespace Umbraco.Tests.Integration.Services public void Find_BaseData_Language() { // Act - var languages = _localizationService.GetAllLanguages(); + var languages = LocalizationService.GetAllLanguages(); // Assert Assert.That(3, Is.EqualTo(languages.Count())); @@ -353,8 +353,8 @@ namespace Umbraco.Tests.Integration.Services var language = new Core.Models.Language(_globalSettings, isoCode); // Act - _localizationService.Save(language); - var result = _localizationService.GetLanguageByIsoCode(isoCode); + LocalizationService.Save(language); + var result = LocalizationService.GetLanguageByIsoCode(isoCode); // Assert Assert.NotNull(result); @@ -367,8 +367,8 @@ namespace Umbraco.Tests.Integration.Services var language = new Core.Models.Language(_globalSettings, isoCode); // Act - _localizationService.Save(language); - var result = _localizationService.GetLanguageById(language.Id); + LocalizationService.Save(language); + var result = LocalizationService.GetLanguageById(language.Id); // Assert Assert.NotNull(result); @@ -378,16 +378,16 @@ namespace Umbraco.Tests.Integration.Services public void Set_Default_Language() { var language = new Language(_globalSettings, "en-AU") {IsDefault = true}; - _localizationService.Save(language); - var result = _localizationService.GetLanguageById(language.Id); + LocalizationService.Save(language); + var result = LocalizationService.GetLanguageById(language.Id); Assert.IsTrue(result.IsDefault); var language2 = new Language(_globalSettings, "en-NZ") {IsDefault = true}; - _localizationService.Save(language2); - var result2 = _localizationService.GetLanguageById(language2.Id); + LocalizationService.Save(language2); + var result2 = LocalizationService.GetLanguageById(language2.Id); //re-get - result = _localizationService.GetLanguageById(language.Id); + result = LocalizationService.GetLanguageById(language.Id); Assert.IsTrue(result2.IsDefault); Assert.IsFalse(result.IsDefault); @@ -398,11 +398,11 @@ namespace Umbraco.Tests.Integration.Services { var isoCode = "en-AU"; var language = new Core.Models.Language(_globalSettings, isoCode); - _localizationService.Save(language); + LocalizationService.Save(language); // Act - _localizationService.Delete(language); - var result = _localizationService.GetLanguageByIsoCode(isoCode); + LocalizationService.Delete(language); + var result = LocalizationService.GetLanguageByIsoCode(isoCode); // Assert Assert.Null(result); @@ -412,8 +412,8 @@ namespace Umbraco.Tests.Integration.Services { var danish = new Language(_globalSettings, "da-DK") { CultureName = "Danish" }; var english = new Language(_globalSettings, "en-GB") { CultureName = "English" }; - _localizationService.Save(danish, 0); - _localizationService.Save(english, 0); + LocalizationService.Save(danish, 0); + LocalizationService.Save(english, 0); _danishLangId = danish.Id; _englishLangId = english.Id; @@ -425,7 +425,7 @@ namespace Umbraco.Tests.Integration.Services new DictionaryTranslation(danish, "ForældreVærdi") } }; - _localizationService.Save(parentItem); + LocalizationService.Save(parentItem); _parentItemGuidId = parentItem.Key; _parentItemIntId = parentItem.Id; @@ -437,7 +437,7 @@ namespace Umbraco.Tests.Integration.Services new DictionaryTranslation(danish, "BørnVærdi") } }; - _localizationService.Save(childItem); + LocalizationService.Save(childItem); _childItemGuidId = childItem.Key; _childItemIntId = childItem.Id; } diff --git a/src/Umbraco.Tests.Integration/Services/MacroServiceTests.cs b/src/Umbraco.Tests.Integration/Services/MacroServiceTests.cs index 1909503308..2064365cbb 100644 --- a/src/Umbraco.Tests.Integration/Services/MacroServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/MacroServiceTests.cs @@ -22,7 +22,7 @@ namespace Umbraco.Tests.Integration.Services [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] public class MacroServiceTests : UmbracoIntegrationTest { - private MacroService _macroService => (MacroService)GetRequiredService(); + private MacroService MacroService => (MacroService)GetRequiredService(); [SetUp] public override void Setup() @@ -47,7 +47,7 @@ namespace Umbraco.Tests.Integration.Services // Arrange // Act - var macro = _macroService.GetByAlias("test1"); + var macro = MacroService.GetByAlias("test1"); // Assert Assert.IsNotNull(macro); @@ -60,7 +60,7 @@ namespace Umbraco.Tests.Integration.Services // Arrange // Act - var result = _macroService.GetAll(); + var result = MacroService.GetAll(); // Assert Assert.AreEqual(3, result.Count()); @@ -74,19 +74,19 @@ namespace Umbraco.Tests.Integration.Services // Act var macro = CreateMacro(); - _macroService.Save(macro); + MacroService.Save(macro); // Assert Assert.IsTrue(macro.HasIdentity); Assert.Greater(macro.Id, 0); Assert.AreNotEqual(Guid.Empty, macro.Key); - var result = _macroService.GetById(macro.Id); + var result = MacroService.GetById(macro.Id); Assert.AreEqual("test", result.Alias); Assert.AreEqual("Test", result.Name); Assert.AreEqual("~/Views/MacroPartials/Test.cshtml", result.MacroSource); Assert.AreEqual(1234, result.CacheDuration); - result = _macroService.GetById(macro.Key); + result = MacroService.GetById(macro.Key); Assert.AreEqual("test", result.Alias); Assert.AreEqual("Test", result.Name); Assert.AreEqual("~/Views/MacroPartials/Test.cshtml", result.MacroSource); @@ -98,16 +98,16 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); - _macroService.Save(macro); + MacroService.Save(macro); // Act - _macroService.Delete(macro); + MacroService.Delete(macro); // Assert - var result = _macroService.GetById(macro.Id); + var result = MacroService.GetById(macro.Id); Assert.IsNull(result); - result = _macroService.GetById(macro.Key); + result = MacroService.GetById(macro.Key); Assert.IsNull(result); } @@ -116,15 +116,15 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var macro = CreateMacro(); - _macroService.Save(macro); + MacroService.Save(macro); // Act var currKey = macro.Key; macro.Name = "New name"; macro.Alias = "NewAlias"; - _macroService.Save(macro); + MacroService.Save(macro); - macro = _macroService.GetById(macro.Id); + macro = MacroService.GetById(macro.Id); // Assert Assert.AreEqual("New name", macro.Name); @@ -139,7 +139,7 @@ namespace Umbraco.Tests.Integration.Services // Arrange var macro = CreateMacro(); macro.Properties.Add(new MacroProperty("blah", "Blah", 0, "blah")); - _macroService.Save(macro); + MacroService.Save(macro); Assert.AreNotEqual(Guid.Empty, macro.Properties[0].Key); @@ -149,9 +149,9 @@ namespace Umbraco.Tests.Integration.Services macro.Properties[0].Name = "new Name"; macro.Properties[0].SortOrder = 1; macro.Properties[0].EditorAlias = "new"; - _macroService.Save(macro); + MacroService.Save(macro); - macro = _macroService.GetById(macro.Id); + macro = MacroService.GetById(macro.Id); // Assert Assert.AreEqual(1, macro.Properties.Count); @@ -171,7 +171,7 @@ namespace Umbraco.Tests.Integration.Services macro.Properties.Add(new MacroProperty("blah1", "Blah1", 0, "blah1")); macro.Properties.Add(new MacroProperty("blah2", "Blah2", 1, "blah2")); macro.Properties.Add(new MacroProperty("blah3", "Blah3", 2, "blah3")); - _macroService.Save(macro); + MacroService.Save(macro); var lastKey = macro.Properties[0].Key; for (var i = 1; i < macro.Properties.Count; i++) @@ -190,9 +190,9 @@ namespace Umbraco.Tests.Integration.Services var allPropKeys = macro.Properties.Values.Select(x => new { x.Alias, x.Key }).ToArray(); - _macroService.Save(macro); + MacroService.Save(macro); - macro = _macroService.GetById(macro.Id); + macro = MacroService.GetById(macro.Id); // Assert Assert.AreEqual(2, macro.Properties.Count); @@ -216,9 +216,9 @@ namespace Umbraco.Tests.Integration.Services macro.Properties.Add(new MacroProperty("blah2", "Blah2", 0, "blah2")); macro.Properties.Add(new MacroProperty("blah3", "Blah3", 0, "blah3")); macro.Properties.Add(new MacroProperty("blah4", "Blah4", 0, "blah4")); - _macroService.Save(macro); + MacroService.Save(macro); - var result1 = _macroService.GetById(macro.Id); + var result1 = MacroService.GetById(macro.Id); Assert.AreEqual(4, result1.Properties.Values.Count()); // Simulate clearing the sections @@ -230,10 +230,10 @@ namespace Umbraco.Tests.Integration.Services // Now just re-add a couple result1.Properties.Add(new MacroProperty("blah3", "Blah3", 0, "blah3")); result1.Properties.Add(new MacroProperty("blah4", "Blah4", 0, "blah4")); - _macroService.Save(result1); + MacroService.Save(result1); // Assert - result1 = _macroService.GetById(result1.Id); + result1 = MacroService.GetById(result1.Id); Assert.AreEqual(2, result1.Properties.Values.Count()); } @@ -245,7 +245,7 @@ namespace Umbraco.Tests.Integration.Services var macro = CreateMacro(name: string.Empty); // Act & Assert - Assert.Throws(() => _macroService.Save(macro)); + Assert.Throws(() => MacroService.Save(macro)); } private static IMacro CreateMacro(string name = "Test") diff --git a/src/Umbraco.Tests.Integration/Services/MediaServiceTests.cs b/src/Umbraco.Tests.Integration/Services/MediaServiceTests.cs index 89c01b54d3..a43f21d061 100644 --- a/src/Umbraco.Tests.Integration/Services/MediaServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/MediaServiceTests.cs @@ -19,8 +19,8 @@ namespace Umbraco.Tests.Integration.Services [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, PublishedRepositoryEvents = true)] public class MediaServiceTests : UmbracoIntegrationTest { - private IMediaService _mediaService => GetRequiredService(); - private IMediaTypeService _mediaTypeService => GetRequiredService(); + private IMediaService MediaService => GetRequiredService(); + private IMediaTypeService MediaTypeService => GetRequiredService(); /// /// Used to list out all ambiguous events that will require dispatching with a name @@ -28,7 +28,7 @@ namespace Umbraco.Tests.Integration.Services [Test, Explicit] public void List_Ambiguous_Events() { - var events = _mediaService.GetType().GetEvents(BindingFlags.Static | BindingFlags.Public); + var events = MediaService.GetType().GetEvents(BindingFlags.Static | BindingFlags.Public); var typedEventHandler = typeof(TypedEventHandler<,>); foreach (var e in events) { @@ -52,30 +52,30 @@ namespace Umbraco.Tests.Integration.Services public void Get_Paged_Children_With_Media_Type_Filter() { var mediaType1 = MockedContentTypes.CreateImageMediaType("Image2"); - _mediaTypeService.Save(mediaType1); + MediaTypeService.Save(mediaType1); var mediaType2 = MockedContentTypes.CreateImageMediaType("Image3"); - _mediaTypeService.Save(mediaType2); + MediaTypeService.Save(mediaType2); for (var i = 0; i < 10; i++) { var m1 = MockedMedia.CreateMediaImage(mediaType1, -1); - _mediaService.Save(m1); + MediaService.Save(m1); var m2 = MockedMedia.CreateMediaImage(mediaType2, -1); - _mediaService.Save(m2); + MediaService.Save(m2); } long total; var provider = ScopeProvider; using (provider.CreateScope()) { - var result = _mediaService.GetPagedChildren(-1, 0, 11, out total, + var result = MediaService.GetPagedChildren(-1, 0, 11, out total, provider.SqlContext.Query() .Where(x => new[] { mediaType1.Id, mediaType2.Id }.Contains(x.ContentTypeId)), Ordering.By("SortOrder", Direction.Ascending)); Assert.AreEqual(11, result.Count()); Assert.AreEqual(20, total); - result = _mediaService.GetPagedChildren(-1, 1, 11, out total, + result = MediaService.GetPagedChildren(-1, 1, 11, out total, provider.SqlContext.Query() .Where(x => new[] { mediaType1.Id, mediaType2.Id }.Contains(x.ContentTypeId)), Ordering.By("SortOrder", Direction.Ascending)); @@ -89,10 +89,10 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var mediaItems = CreateTrashedTestMedia(); - var media = _mediaService.GetById(mediaItems.Item3.Id); + var media = MediaService.GetById(mediaItems.Item3.Id); // Act - _mediaService.Move(media, mediaItems.Item2.Id); + MediaService.Move(media, mediaItems.Item2.Id); // Assert Assert.That(media.ParentId, Is.EqualTo(mediaItems.Item2.Id)); @@ -104,10 +104,10 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var mediaItems = CreateTrashedTestMedia(); - var media = _mediaService.GetById(mediaItems.Item1.Id); + var media = MediaService.GetById(mediaItems.Item1.Id); // Act - _mediaService.MoveToRecycleBin(media); + MediaService.MoveToRecycleBin(media); // Assert Assert.That(media.ParentId, Is.EqualTo(-21)); @@ -119,11 +119,11 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var mediaItems = CreateTrashedTestMedia(); - var media = _mediaService.GetById(mediaItems.Item4.Id); + var media = MediaService.GetById(mediaItems.Item4.Id); // Act - moving out of recycle bin - _mediaService.Move(media, mediaItems.Item1.Id); - var mediaChild = _mediaService.GetById(mediaItems.Item5.Id); + MediaService.Move(media, mediaItems.Item1.Id); + var mediaChild = MediaService.GetById(mediaItems.Item5.Id); // Assert Assert.That(media.ParentId, Is.EqualTo(mediaItems.Item1.Id)); @@ -137,11 +137,11 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var mediaType = MockedContentTypes.CreateVideoMediaType(); - _mediaTypeService.Save(mediaType); - var media = _mediaService.CreateMedia(string.Empty, -1, "video"); + MediaTypeService.Save(mediaType); + var media = MediaService.CreateMedia(string.Empty, -1, "video"); // Act & Assert - Assert.Throws(() => _mediaService.Save(media)); + Assert.Throws(() => MediaService.Save(media)); } /* [Test] @@ -163,13 +163,13 @@ namespace Umbraco.Tests.Integration.Services public void Can_Get_Media_By_Path() { var mediaType = MockedContentTypes.CreateImageMediaType("Image2"); - _mediaTypeService.Save(mediaType); + MediaTypeService.Save(mediaType); var media = MockedMedia.CreateMediaImage(mediaType, -1); - _mediaService.Save(media); + MediaService.Save(media); var mediaPath = "/media/test-image.png"; - var resolvedMedia = _mediaService.GetMediaByPath(mediaPath); + var resolvedMedia = MediaService.GetMediaByPath(mediaPath); Assert.IsNotNull(resolvedMedia); Assert.That(resolvedMedia.GetValue(Constants.Conventions.Media.File).ToString() == mediaPath); @@ -179,13 +179,13 @@ namespace Umbraco.Tests.Integration.Services public void Can_Get_Media_With_Crop_By_Path() { var mediaType = MockedContentTypes.CreateImageMediaTypeWithCrop("Image2"); - _mediaTypeService.Save(mediaType); + MediaTypeService.Save(mediaType); var media = MockedMedia.CreateMediaImageWithCrop(mediaType, -1); - _mediaService.Save(media); + MediaService.Save(media); var mediaPath = "/media/test-image.png"; - var resolvedMedia = _mediaService.GetMediaByPath(mediaPath); + var resolvedMedia = MediaService.GetMediaByPath(mediaPath); Assert.IsNotNull(resolvedMedia); Assert.That(resolvedMedia.GetValue(Constants.Conventions.Media.File).ToString().Contains(mediaPath)); @@ -195,14 +195,14 @@ namespace Umbraco.Tests.Integration.Services public void Can_Get_Paged_Children() { var mediaType = MockedContentTypes.CreateImageMediaType("Image2"); - _mediaTypeService.Save(mediaType); + MediaTypeService.Save(mediaType); for (var i = 0; i < 10; i++) { var c1 = MockedMedia.CreateMediaImage(mediaType, -1); - _mediaService.Save(c1); + MediaService.Save(c1); } - var service = _mediaService; + var service = MediaService; long total; var entities = service.GetPagedChildren(-1, 0, 6, out total).ToArray(); @@ -217,25 +217,25 @@ namespace Umbraco.Tests.Integration.Services public void Can_Get_Paged_Children_Dont_Get_Descendants() { var mediaType = MockedContentTypes.CreateImageMediaType("Image2"); - _mediaTypeService.Save(mediaType); + MediaTypeService.Save(mediaType); // only add 9 as we also add a folder with children for (var i = 0; i < 9; i++) { var m1 = MockedMedia.CreateMediaImage(mediaType, -1); - _mediaService.Save(m1); + MediaService.Save(m1); } var mediaTypeForFolder = MockedContentTypes.CreateImageMediaType("Folder2"); - _mediaTypeService.Save(mediaTypeForFolder); + MediaTypeService.Save(mediaTypeForFolder); var mediaFolder = MockedMedia.CreateMediaFolder(mediaTypeForFolder, -1); - _mediaService.Save(mediaFolder); + MediaService.Save(mediaFolder); for (var i = 0; i < 10; i++) { var m1 = MockedMedia.CreateMediaImage(mediaType, mediaFolder.Id); - _mediaService.Save(m1); + MediaService.Save(m1); } - var service = _mediaService; + var service = MediaService; long total; // children in root including the folder - not the descendants in the folder @@ -258,28 +258,28 @@ namespace Umbraco.Tests.Integration.Services private Tuple CreateTrashedTestMedia() { //Create and Save folder-Media -> 1050 - var folderMediaType = _mediaTypeService.Get(1031); + var folderMediaType = MediaTypeService.Get(1031); var folder = MockedMedia.CreateMediaFolder(folderMediaType, -1); - _mediaService.Save(folder); + MediaService.Save(folder); //Create and Save folder-Media -> 1051 var folder2 = MockedMedia.CreateMediaFolder(folderMediaType, -1); - _mediaService.Save(folder2); + MediaService.Save(folder2); //Create and Save image-Media -> 1052 - var imageMediaType = _mediaTypeService.Get(1032); + var imageMediaType = MediaTypeService.Get(1032); var image = (Media)MockedMedia.CreateMediaImage(imageMediaType, 1050); - _mediaService.Save(image); + MediaService.Save(image); //Create and Save folder-Media that is trashed -> 1053 var folderTrashed = (Media)MockedMedia.CreateMediaFolder(folderMediaType, -21); folderTrashed.Trashed = true; - _mediaService.Save(folderTrashed); + MediaService.Save(folderTrashed); //Create and Save image-Media child of folderTrashed -> 1054 var imageTrashed = (Media)MockedMedia.CreateMediaImage(imageMediaType, folderTrashed.Id); imageTrashed.Trashed = true; - _mediaService.Save(imageTrashed); + MediaService.Save(imageTrashed); return new Tuple(folder, folder2, image, folderTrashed, imageTrashed); } diff --git a/src/Umbraco.Tests.Integration/Services/PublicAccessServiceTests.cs b/src/Umbraco.Tests.Integration/Services/PublicAccessServiceTests.cs index 42419565a2..5294e8015b 100644 --- a/src/Umbraco.Tests.Integration/Services/PublicAccessServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/PublicAccessServiceTests.cs @@ -15,20 +15,20 @@ namespace Umbraco.Tests.Integration.Services [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] public class PublicAccessServiceTests : UmbracoIntegrationTest { - private IContentService _contentService => GetRequiredService(); - private IContentTypeService _contentTypeService => GetRequiredService(); - private IFileService _fileService => GetRequiredService(); - private IPublicAccessService _publicAccessService => GetRequiredService(); + private IContentService ContentService => GetRequiredService(); + private IContentTypeService ContentTypeService => GetRequiredService(); + private IFileService FileService => GetRequiredService(); + private IPublicAccessService PublicAccessService => GetRequiredService(); [Test] public void Can_Add_New_Entry() { // Arrange var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah"); - _fileService.SaveTemplate(ct.DefaultTemplate); - _contentTypeService.Save(ct); + FileService.SaveTemplate(ct.DefaultTemplate); + ContentTypeService.Save(ct); var c = MockedContent.CreateSimpleContent(ct, "Test", -1); - _contentService.Save(c); + ContentService.Save(c); // Act var entry = new PublicAccessEntry(c, c, c, new[] @@ -39,7 +39,7 @@ namespace Umbraco.Tests.Integration.Services RuleValue = "TestVal" }, }); - var result = _publicAccessService.Save(entry); + var result = PublicAccessService.Save(entry); // Assert Assert.IsTrue(result.Success); @@ -56,10 +56,10 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah"); - _fileService.SaveTemplate(ct.DefaultTemplate); - _contentTypeService.Save(ct); + FileService.SaveTemplate(ct.DefaultTemplate); + ContentTypeService.Save(ct); var c = MockedContent.CreateSimpleContent(ct, "Test", -1); - _contentService.Save(c); + ContentService.Save(c); var entry = new PublicAccessEntry(c, c, c, new[] { new PublicAccessRule() @@ -68,12 +68,12 @@ namespace Umbraco.Tests.Integration.Services RuleValue = "TestVal" }, }); - _publicAccessService.Save(entry); + PublicAccessService.Save(entry); // Act - var updated = _publicAccessService.AddRule(c, "TestType2", "AnotherVal"); + var updated = PublicAccessService.AddRule(c, "TestType2", "AnotherVal"); //re-get - entry = _publicAccessService.GetEntryForContent(c); + entry = PublicAccessService.GetEntryForContent(c); // Assert Assert.IsTrue(updated.Success); @@ -86,10 +86,10 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah"); - _fileService.SaveTemplate(ct.DefaultTemplate); - _contentTypeService.Save(ct); + FileService.SaveTemplate(ct.DefaultTemplate); + ContentTypeService.Save(ct); var c = MockedContent.CreateSimpleContent(ct, "Test", -1); - _contentService.Save(c); + ContentService.Save(c); var entry = new PublicAccessEntry(c, c, c, new[] { new PublicAccessRule() @@ -98,14 +98,14 @@ namespace Umbraco.Tests.Integration.Services RuleValue = "TestVal" }, }); - _publicAccessService.Save(entry); + PublicAccessService.Save(entry); // Act - var updated1 = _publicAccessService.AddRule(c, "TestType", "AnotherVal1"); - var updated2 = _publicAccessService.AddRule(c, "TestType", "AnotherVal2"); + var updated1 = PublicAccessService.AddRule(c, "TestType", "AnotherVal1"); + var updated2 = PublicAccessService.AddRule(c, "TestType", "AnotherVal2"); //re-get - entry = _publicAccessService.GetEntryForContent(c); + entry = PublicAccessService.GetEntryForContent(c); // Assert Assert.IsTrue(updated1.Success); @@ -120,10 +120,10 @@ namespace Umbraco.Tests.Integration.Services { // Arrange var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah"); - _fileService.SaveTemplate(ct.DefaultTemplate); - _contentTypeService.Save(ct); + FileService.SaveTemplate(ct.DefaultTemplate); + ContentTypeService.Save(ct); var c = MockedContent.CreateSimpleContent(ct, "Test", -1); - _contentService.Save(c); + ContentService.Save(c); var entry = new PublicAccessEntry(c, c, c, new[] { new PublicAccessRule() @@ -137,12 +137,12 @@ namespace Umbraco.Tests.Integration.Services RuleValue = "TestValue2" }, }); - _publicAccessService.Save(entry); + PublicAccessService.Save(entry); // Act - var removed = _publicAccessService.RemoveRule(c, "TestType", "TestValue1"); + var removed = PublicAccessService.RemoveRule(c, "TestType", "TestValue1"); //re-get - entry = _publicAccessService.GetEntryForContent(c); + entry = PublicAccessService.GetEntryForContent(c); // Assert Assert.IsTrue(removed.Success); diff --git a/src/Umbraco.Tests.Integration/Services/SectionServiceTests.cs b/src/Umbraco.Tests.Integration/Services/SectionServiceTests.cs index ef8301a315..f34ea32cff 100644 --- a/src/Umbraco.Tests.Integration/Services/SectionServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/SectionServiceTests.cs @@ -18,8 +18,8 @@ namespace Umbraco.Tests.Integration.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() @@ -28,7 +28,7 @@ namespace Umbraco.Tests.Integration.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); @@ -43,7 +43,7 @@ namespace Umbraco.Tests.Integration.Services Username = "testUser", Email = "testuser@test.com", }; - _userService.Save(user, false); + UserService.Save(user, false); var userGroupA = new UserGroup(ShortStringHelper) { @@ -53,7 +53,7 @@ namespace Umbraco.Tests.Integration.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) { @@ -62,9 +62,9 @@ namespace Umbraco.Tests.Integration.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.Integration/Services/TagServiceTests.cs b/src/Umbraco.Tests.Integration/Services/TagServiceTests.cs index a3ee817a28..585c1319a4 100644 --- a/src/Umbraco.Tests.Integration/Services/TagServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/TagServiceTests.cs @@ -22,11 +22,11 @@ namespace Umbraco.Tests.Integration.Services [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] public class TagServiceTests : UmbracoIntegrationTest { - private IContentService _contentService => GetRequiredService(); - private IContentTypeService _contentTypeService => GetRequiredService(); - private ITagService _tagService => GetRequiredService(); - private IDataTypeService _dataTypeService => GetRequiredService(); - private PropertyEditorCollection _propertyEditorCollection => GetRequiredService(); + private IContentService ContentService => GetRequiredService(); + private IContentTypeService ContentTypeService => GetRequiredService(); + private ITagService TagService => GetRequiredService(); + private IDataTypeService DataTypeService => GetRequiredService(); + private PropertyEditorCollection PropertyEditorCollection => GetRequiredService(); [Test] public void TagApiConsistencyTest() @@ -37,25 +37,25 @@ namespace Umbraco.Tests.Integration.Services { DataTypeId = 1041 }); - _contentTypeService.Save(contentType); + ContentTypeService.Save(contentType); IContent content1 = MockedContent.CreateSimpleContent(contentType, "Tagged content 1", -1); - content1.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "cow", "pig", "goat" }); - _contentService.SaveAndPublish(content1); + content1.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow", "pig", "goat" }); + ContentService.SaveAndPublish(content1); // change - content1.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "elephant" }, true); - content1.RemoveTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "cow" }); - _contentService.SaveAndPublish(content1); + content1.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "elephant" }, true); + content1.RemoveTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow" }); + ContentService.SaveAndPublish(content1); // more changes - content1.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "mouse" }, true); - _contentService.SaveAndPublish(content1); - content1.RemoveTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "mouse" }); - _contentService.SaveAndPublish(content1); + content1.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "mouse" }, true); + ContentService.SaveAndPublish(content1); + content1.RemoveTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "mouse" }); + ContentService.SaveAndPublish(content1); // get it back - content1 = _contentService.GetById(content1.Id); + content1 = ContentService.GetById(content1.Id); var tagsValue = content1.GetValue("tags").ToString(); var tagsValues = JsonConvert.DeserializeObject(tagsValue); Assert.AreEqual(3, tagsValues.Length); @@ -63,7 +63,7 @@ namespace Umbraco.Tests.Integration.Services Assert.Contains("goat", tagsValues); Assert.Contains("elephant", tagsValues); - var tags = _tagService.GetTagsForProperty(content1.Id, "tags").ToArray(); + var tags = TagService.GetTagsForProperty(content1.Id, "tags").ToArray(); Assert.IsTrue(tags.All(x => x.Group == "default")); tagsValues = tags.Select(x => x.Text).ToArray(); @@ -82,22 +82,22 @@ namespace Umbraco.Tests.Integration.Services { DataTypeId = Constants.DataTypes.Tags }); - _contentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var content1 = MockedContent.CreateSimpleContent(contentType, "Tagged content 1", -1); - content1.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "cow", "pig", "goat" }); - _contentService.SaveAndPublish(content1); + content1.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow", "pig", "goat" }); + ContentService.SaveAndPublish(content1); var content2 = MockedContent.CreateSimpleContent(contentType, "Tagged content 2", -1); - content2.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "cow", "pig" }); - _contentService.SaveAndPublish(content2); + content2.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow", "pig" }); + ContentService.SaveAndPublish(content2); var content3 = MockedContent.CreateSimpleContent(contentType, "Tagged content 3", -1); - content3.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "cow" }); - _contentService.SaveAndPublish(content3); + content3.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow" }); + ContentService.SaveAndPublish(content3); // Act - var tags = _tagService.GetAllContentTags() + var tags = TagService.GetAllContentTags() .OrderByDescending(x => x.NodeCount) .ToList(); diff --git a/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs b/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs index e0f2e423e1..fbe51da837 100644 --- a/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs @@ -29,9 +29,9 @@ namespace Umbraco.Tests.Integration.Services [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] public class UserServiceTests : UmbracoIntegrationTest { - private UserService _userService => (UserService) GetRequiredService(); - private IContentTypeService _contentTypeService => GetRequiredService(); - private IContentService _contentService => GetRequiredService(); + private UserService UserService => (UserService) GetRequiredService(); + private IContentTypeService ContentTypeService => GetRequiredService(); + private IContentService ContentService => GetRequiredService(); [Test] public void Get_User_Permissions_For_Unassigned_Permission_Nodes() @@ -39,17 +39,17 @@ namespace Umbraco.Tests.Integration.Services // Arrange var user = CreateTestUser(out _); var contentType = MockedContentTypes.CreateSimpleContentType(); - _contentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - _contentService.Save(content); + ContentService.Save(content); // Act - var permissions = _userService.GetPermissions(user, content[0].Id, content[1].Id, content[2].Id).ToArray(); + var permissions = UserService.GetPermissions(user, content[0].Id, content[1].Id, content[2].Id).ToArray(); // Assert Assert.AreEqual(3, permissions.Length); @@ -65,23 +65,23 @@ namespace Umbraco.Tests.Integration.Services var user = CreateTestUser(out var userGroup); var contentType = MockedContentTypes.CreateSimpleContentType(); - _contentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - _contentService.Save(content); - _contentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content[2], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + ContentService.Save(content); + ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content[2], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); // Act - var permissions = _userService.GetPermissions(user, content[0].Id, content[1].Id, content[2].Id).ToArray(); + var permissions = UserService.GetPermissions(user, content[0].Id, content[1].Id, content[2].Id).ToArray(); // Assert Assert.AreEqual(3, permissions.Length); @@ -97,23 +97,23 @@ namespace Umbraco.Tests.Integration.Services var userGroup = CreateTestUserGroup(); var contentType = MockedContentTypes.CreateSimpleContentType(); - _contentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - _contentService.Save(content); - _contentService.SetPermission(content.ElementAt(0), ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content.ElementAt(0), ActionDelete.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content.ElementAt(0), ActionMove.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content.ElementAt(1), ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content.ElementAt(1), ActionDelete.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content.ElementAt(2), ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + ContentService.Save(content); + ContentService.SetPermission(content.ElementAt(0), ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content.ElementAt(0), ActionDelete.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content.ElementAt(0), ActionMove.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content.ElementAt(1), ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content.ElementAt(1), ActionDelete.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content.ElementAt(2), ActionBrowse.ActionLetter, new int[] { userGroup.Id }); // Act - var permissions = _userService.GetPermissions(userGroup, false, content[0].Id, content[1].Id, content[2].Id).ToArray(); + var permissions = UserService.GetPermissions(userGroup, false, content[0].Id, content[1].Id, content[2].Id).ToArray(); // Assert Assert.AreEqual(3, permissions.Length); @@ -129,22 +129,22 @@ namespace Umbraco.Tests.Integration.Services var userGroup = CreateTestUserGroup(); var contentType = MockedContentTypes.CreateSimpleContentType(); - _contentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - _contentService.Save(content); - _contentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id }); + ContentService.Save(content); + ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id }); // Act - var permissions = _userService.GetPermissions(userGroup, true, content[0].Id, content[1].Id, content[2].Id) + var permissions = UserService.GetPermissions(userGroup, true, content[0].Id, content[1].Id, content[2].Id) .ToArray(); // Assert @@ -161,35 +161,35 @@ namespace Umbraco.Tests.Integration.Services var userGroup1 = CreateTestUserGroup(); var userGroup2 = CreateTestUserGroup("test2", "Test 2"); var userGroup3 = CreateTestUserGroup("test3", "Test 3"); - var user = _userService.CreateUserWithIdentity("John Doe", "john@umbraco.io"); + var user = UserService.CreateUserWithIdentity("John Doe", "john@umbraco.io"); var defaultPermissionCount = userGroup3.Permissions.Count(); user.AddGroup(userGroup1); user.AddGroup(userGroup2); user.AddGroup(userGroup3); - _userService.Save(user); + UserService.Save(user); var contentType = MockedContentTypes.CreateSimpleContentType(); - _contentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - _contentService.Save(content); + ContentService.Save(content); //assign permissions - we aren't assigning anything explicit for group3 and nothing explicit for content[2] /w group2 - _contentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup1.Id }); - _contentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup1.Id }); - _contentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup2.Id }); - _contentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup1.Id }); - _contentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup2.Id }); - _contentService.SetPermission(content[2], ActionDelete.ActionLetter, new int[] { userGroup1.Id }); + ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup1.Id }); + ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup1.Id }); + ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup2.Id }); + ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup1.Id }); + ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup2.Id }); + ContentService.SetPermission(content[2], ActionDelete.ActionLetter, new int[] { userGroup1.Id }); // Act //we don't pass in any nodes so it will return all of them - var result = _userService.GetPermissions(user).ToArray(); + var result = UserService.GetPermissions(user).ToArray(); var permissions = result .GroupBy(x => x.EntityId) .ToDictionary(x => x.Key, x => x.GroupBy(a => a.UserGroupId).ToDictionary(a => a.Key, a => a.ToArray())); @@ -240,24 +240,24 @@ namespace Umbraco.Tests.Integration.Services var userGroup = CreateTestUserGroup(); var contentType = MockedContentTypes.CreateSimpleContentType(); - _contentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - _contentService.Save(content); - _contentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(content[2], ActionDelete.ActionLetter, new int[] { userGroup.Id }); + ContentService.Save(content); + ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(content[2], ActionDelete.ActionLetter, new int[] { userGroup.Id }); // Act //we don't pass in any nodes so it will return all of them - var permissions = _userService.GetPermissions(userGroup, true) + var permissions = UserService.GetPermissions(userGroup, true) .GroupBy(x => x.EntityId) .ToDictionary(x => x.Key, x => x); @@ -401,22 +401,22 @@ namespace Umbraco.Tests.Integration.Services var userGroup = CreateTestUserGroup(); var contentType = MockedContentTypes.CreateSimpleContentType(); - _contentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var parent = MockedContent.CreateSimpleContent(contentType); - _contentService.Save(parent); + ContentService.Save(parent); var child1 = MockedContent.CreateSimpleContent(contentType, "child1", parent); - _contentService.Save(child1); + ContentService.Save(child1); var child2 = MockedContent.CreateSimpleContent(contentType, "child2", child1); - _contentService.Save(child2); + ContentService.Save(child2); - _contentService.SetPermission(parent, ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(parent, ActionDelete.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(parent, ActionMove.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(parent, ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - _contentService.SetPermission(parent, ActionDelete.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(parent, ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(parent, ActionDelete.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(parent, ActionMove.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(parent, ActionBrowse.ActionLetter, new int[] { userGroup.Id }); + ContentService.SetPermission(parent, ActionDelete.ActionLetter, new int[] { userGroup.Id }); // Act - var permissions = _userService.GetPermissionsForPath(userGroup, child2.Path); + var permissions = UserService.GetPermissionsForPath(userGroup, child2.Path); // Assert var allPermissions = permissions.GetAllPermissions().ToArray(); @@ -426,10 +426,10 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Can_Delete_User() { - var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - _userService.Delete(user, true); - var deleted = _userService.GetUserById(user.Id); + UserService.Delete(user, true); + var deleted = UserService.GetUserById(user.Id); // Assert Assert.That(deleted, Is.Null); @@ -438,10 +438,10 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Disables_User_Instead_Of_Deleting_If_Flag_Not_Set() { - var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - _userService.Delete(user); - var deleted = _userService.GetUserById(user.Id); + UserService.Delete(user); + var deleted = UserService.GetUserById(user.Id); // Assert Assert.That(deleted, Is.Not.Null); @@ -450,60 +450,60 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Exists_By_Username() { - var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - var user2 = _userService.CreateUserWithIdentity("john2@umbraco.io", "john2@umbraco.io"); - Assert.IsTrue(_userService.Exists("JohnDoe")); - Assert.IsFalse(_userService.Exists("notFound")); - Assert.IsTrue(_userService.Exists("john2@umbraco.io")); + var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user2 = UserService.CreateUserWithIdentity("john2@umbraco.io", "john2@umbraco.io"); + Assert.IsTrue(UserService.Exists("JohnDoe")); + Assert.IsFalse(UserService.Exists("notFound")); + Assert.IsTrue(UserService.Exists("john2@umbraco.io")); } [Test] public void Get_By_Email() { - var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - Assert.IsNotNull(_userService.GetByEmail(user.Email)); - Assert.IsNull(_userService.GetByEmail("do@not.find")); + Assert.IsNotNull(UserService.GetByEmail(user.Email)); + Assert.IsNull(UserService.GetByEmail("do@not.find")); } [Test] public void Get_By_Username() { - var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - Assert.IsNotNull(_userService.GetByUsername(user.Username)); - Assert.IsNull(_userService.GetByUsername("notFound")); + Assert.IsNotNull(UserService.GetByUsername(user.Username)); + Assert.IsNull(UserService.GetByUsername("notFound")); } [Test] public void Get_By_Username_With_Backslash() { - var user = _userService.CreateUserWithIdentity("mydomain\\JohnDoe", "john@umbraco.io"); + var user = UserService.CreateUserWithIdentity("mydomain\\JohnDoe", "john@umbraco.io"); - Assert.IsNotNull(_userService.GetByUsername(user.Username)); - Assert.IsNull(_userService.GetByUsername("notFound")); + Assert.IsNotNull(UserService.GetByUsername(user.Username)); + Assert.IsNull(UserService.GetByUsername("notFound")); } [Test] public void Get_By_Object_Id() { - var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - Assert.IsNotNull(_userService.GetUserById(user.Id)); - Assert.IsNull(_userService.GetUserById(9876)); + Assert.IsNotNull(UserService.GetUserById(user.Id)); + Assert.IsNull(UserService.GetUserById(9876)); } [Test] public void Find_By_Email_Starts_With() { var users = CreateMulipleUsers(10); - _userService.Save(users); + UserService.Save(users); //don't find this var customUser = CreateUser(); customUser.Email = "hello@hello.com"; - _userService.Save(customUser); + UserService.Save(customUser); - var found = _userService.FindByEmail("tes", 0, 100, out _, StringPropertyMatchType.StartsWith); + var found = UserService.FindByEmail("tes", 0, 100, out _, StringPropertyMatchType.StartsWith); Assert.AreEqual(10, found.Count()); } @@ -512,13 +512,13 @@ namespace Umbraco.Tests.Integration.Services public void Find_By_Email_Ends_With() { var users = CreateMulipleUsers(10); - _userService.Save(users); + UserService.Save(users); //include this var customUser = CreateUser(); customUser.Email = "hello@test.com"; - _userService.Save(customUser); + UserService.Save(customUser); - var found = _userService.FindByEmail("test.com", 0, 100, out _, StringPropertyMatchType.EndsWith); + var found = UserService.FindByEmail("test.com", 0, 100, out _, StringPropertyMatchType.EndsWith); Assert.AreEqual(11, found.Count()); } @@ -527,13 +527,13 @@ namespace Umbraco.Tests.Integration.Services public void Find_By_Email_Contains() { var users = CreateMulipleUsers(10); - _userService.Save(users); + UserService.Save(users); //include this var customUser = CreateUser(); customUser.Email = "hello@test.com"; - _userService.Save(customUser); + UserService.Save(customUser); - var found = _userService.FindByEmail("test", 0, 100, out _, StringPropertyMatchType.Contains); + var found = UserService.FindByEmail("test", 0, 100, out _, StringPropertyMatchType.Contains); Assert.AreEqual(11, found.Count()); } @@ -542,13 +542,13 @@ namespace Umbraco.Tests.Integration.Services public void Find_By_Email_Exact() { var users = CreateMulipleUsers(10); - _userService.Save(users); + UserService.Save(users); //include this var customUser = CreateUser(); customUser.Email = "hello@test.com"; - _userService.Save(customUser); + UserService.Save(customUser); - var found = _userService.FindByEmail("hello@test.com", 0, 100, out _, StringPropertyMatchType.Exact); + var found = UserService.FindByEmail("hello@test.com", 0, 100, out _, StringPropertyMatchType.Exact); Assert.AreEqual(1, found.Count()); } @@ -557,9 +557,9 @@ namespace Umbraco.Tests.Integration.Services public void Get_All_Paged_Users() { var users = CreateMulipleUsers(10); - _userService.Save(users); + UserService.Save(users); - var found = _userService.GetAll(0, 2, out var totalRecs); + var found = UserService.GetAll(0, 2, out var totalRecs); Assert.AreEqual(2, found.Count()); // + 1 because of the built in admin user @@ -572,9 +572,9 @@ namespace Umbraco.Tests.Integration.Services public void Get_All_Paged_Users_With_Filter() { var users = CreateMulipleUsers(10).ToArray(); - _userService.Save(users); + UserService.Save(users); - var found = _userService.GetAll(0, 2, out var totalRecs, "username", Direction.Ascending, filter: "test"); + var found = UserService.GetAll(0, 2, out var totalRecs, "username", Direction.Ascending, filter: "test"); Assert.AreEqual(2, found.Count()); Assert.AreEqual(10, totalRecs); @@ -586,7 +586,7 @@ namespace Umbraco.Tests.Integration.Services public void Get_All_Paged_Users_For_Group() { var userGroup = MockedUserGroup.CreateUserGroup(); - _userService.Save(userGroup); + UserService.Save(userGroup); var users = CreateMulipleUsers(10).ToArray(); for (var i = 0; i < 10;) @@ -594,10 +594,10 @@ namespace Umbraco.Tests.Integration.Services users[i].AddGroup(userGroup.ToReadOnlyGroup()); i = i + 2; } - _userService.Save(users); + UserService.Save(users); long totalRecs; - var found = _userService.GetAll(0, 2, out totalRecs, "username", Direction.Ascending, includeUserGroups: new[] { userGroup.Alias }); + var found = UserService.GetAll(0, 2, out totalRecs, "username", Direction.Ascending, includeUserGroups: new[] { userGroup.Alias }); Assert.AreEqual(2, found.Count()); Assert.AreEqual(5, totalRecs); @@ -609,7 +609,7 @@ namespace Umbraco.Tests.Integration.Services public void Get_All_Paged_Users_For_Group_With_Filter() { var userGroup = MockedUserGroup.CreateUserGroup(); - _userService.Save(userGroup); + UserService.Save(userGroup); var users = CreateMulipleUsers(10).ToArray(); for (var i = 0; i < 10;) @@ -622,10 +622,10 @@ namespace Umbraco.Tests.Integration.Services users[i].Name = "blah" + users[i].Name; i = i + 3; } - _userService.Save(users); + UserService.Save(users); long totalRecs; - var found = _userService.GetAll(0, 2, out totalRecs, "username", Direction.Ascending, userGroups: new[] { userGroup.Alias }, filter: "blah"); + var found = UserService.GetAll(0, 2, out totalRecs, "username", Direction.Ascending, userGroups: new[] { userGroup.Alias }, filter: "blah"); Assert.AreEqual(2, found.Count()); Assert.AreEqual(2, totalRecs); @@ -637,11 +637,11 @@ namespace Umbraco.Tests.Integration.Services public void Count_All_Users() { var users = CreateMulipleUsers(10); - _userService.Save(users); + UserService.Save(users); var customUser = CreateUser(); - _userService.Save(customUser); + UserService.Save(customUser); - var found = _userService.GetCount(MemberCountType.All); + var found = UserService.GetCount(MemberCountType.All); // + 1 because of the built in admin user Assert.AreEqual(12, found); @@ -652,7 +652,7 @@ namespace Umbraco.Tests.Integration.Services public void Count_All_Online_Users() { var users = CreateMulipleUsers(10, (i, member) => member.LastLoginDate = DateTime.Now.AddMinutes(i * -2)); - _userService.Save(users); + UserService.Save(users); var customUser = CreateUser(); throw new NotImplementedException(); @@ -662,13 +662,13 @@ namespace Umbraco.Tests.Integration.Services public void Count_All_Locked_Users() { var users = CreateMulipleUsers(10, (i, member) => member.IsLockedOut = i % 2 == 0); - _userService.Save(users); + UserService.Save(users); var customUser = CreateUser(); customUser.IsLockedOut = true; - _userService.Save(customUser); + UserService.Save(customUser); - var found = _userService.GetCount(MemberCountType.LockedOut); + var found = UserService.GetCount(MemberCountType.LockedOut); Assert.AreEqual(6, found); } @@ -677,13 +677,13 @@ namespace Umbraco.Tests.Integration.Services public void Count_All_Approved_Users() { var users = CreateMulipleUsers(10, (i, member) => member.IsApproved = i % 2 == 0); - _userService.Save(users); + UserService.Save(users); var customUser = CreateUser(); customUser.IsApproved = false; - _userService.Save(customUser); + UserService.Save(customUser); - var found = _userService.GetCount(MemberCountType.Approved); + var found = UserService.GetCount(MemberCountType.Approved); // + 1 because of the built in admin user Assert.AreEqual(6, found); @@ -693,7 +693,7 @@ namespace Umbraco.Tests.Integration.Services public void Can_Persist_New_User() { // Act - var membershipUser = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var membershipUser = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); // Assert Assert.That(membershipUser.HasIdentity, Is.True); @@ -713,7 +713,7 @@ namespace Umbraco.Tests.Integration.Services var encodedPassword = Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password))); var globalSettings = new GlobalSettings(); var membershipUser = new User(globalSettings, "JohnDoe", "john@umbraco.io", encodedPassword, encodedPassword); - _userService.Save(membershipUser); + UserService.Save(membershipUser); // Assert Assert.That(membershipUser.HasIdentity, Is.True); @@ -733,9 +733,9 @@ namespace Umbraco.Tests.Integration.Services }; userGroup.AddAllowedSection("content"); userGroup.AddAllowedSection("mediat"); - _userService.Save(userGroup); + UserService.Save(userGroup); - var result1 = _userService.GetUserGroupById(userGroup.Id); + var result1 = UserService.GetUserGroupById(userGroup.Id); Assert.AreEqual(2, result1.AllowedSections.Count()); @@ -744,9 +744,9 @@ namespace Umbraco.Tests.Integration.Services userGroup.AddAllowedSection("test2"); userGroup.AddAllowedSection("test3"); userGroup.AddAllowedSection("test4"); - _userService.Save(userGroup); + UserService.Save(userGroup); - result1 = _userService.GetUserGroupById(userGroup.Id); + result1 = UserService.GetUserGroupById(userGroup.Id); Assert.AreEqual(6, result1.AllowedSections.Count()); @@ -759,11 +759,11 @@ namespace Umbraco.Tests.Integration.Services //now just re-add a couple result1.AddAllowedSection("test3"); result1.AddAllowedSection("test4"); - _userService.Save(result1); + UserService.Save(result1); // Assert //re-get - result1 = _userService.GetUserGroupById(userGroup.Id); + result1 = UserService.GetUserGroupById(userGroup.Id); Assert.AreEqual(2, result1.AllowedSections.Count()); } @@ -780,21 +780,21 @@ namespace Umbraco.Tests.Integration.Services Alias = "Group2", Name = "Group 2" }; - _userService.Save(userGroup1); - _userService.Save(userGroup2); + UserService.Save(userGroup1); + UserService.Save(userGroup2); //adds some allowed sections userGroup1.AddAllowedSection("test"); userGroup2.AddAllowedSection("test"); - _userService.Save(userGroup1); - _userService.Save(userGroup2); + UserService.Save(userGroup1); + UserService.Save(userGroup2); //now clear the section from all users - _userService.DeleteSectionFromAllUserGroups("test"); + UserService.DeleteSectionFromAllUserGroups("test"); // Assert - var result1 = _userService.GetUserGroupById(userGroup1.Id); - var result2 = _userService.GetUserGroupById(userGroup2.Id); + var result1 = UserService.GetUserGroupById(userGroup1.Id); + var result2 = UserService.GetUserGroupById(userGroup2.Id); Assert.IsFalse(result1.AllowedSections.Contains("test")); Assert.IsFalse(result2.AllowedSections.Contains("test")); } @@ -821,14 +821,14 @@ namespace Umbraco.Tests.Integration.Services Alias = "Group3", Name = "Group 3" }; - _userService.Save(userGroup1); - _userService.Save(userGroup2); - _userService.Save(userGroup3); + UserService.Save(userGroup1); + UserService.Save(userGroup2); + UserService.Save(userGroup3); // Assert - var result1 = _userService.GetUserGroupById(userGroup1.Id); - var result2 = _userService.GetUserGroupById(userGroup2.Id); - var result3 = _userService.GetUserGroupById(userGroup3.Id); + var result1 = UserService.GetUserGroupById(userGroup1.Id); + var result2 = UserService.GetUserGroupById(userGroup2.Id); + var result3 = UserService.GetUserGroupById(userGroup3.Id); Assert.IsTrue(result1.AllowedSections.Contains("test")); Assert.IsTrue(result2.AllowedSections.Contains("test")); Assert.IsFalse(result3.AllowedSections.Contains("test")); @@ -837,13 +837,13 @@ namespace Umbraco.Tests.Integration.Services foreach (var userGroup in new[] { userGroup1, userGroup2, userGroup3 }) { userGroup.AddAllowedSection("test"); - _userService.Save(userGroup); + UserService.Save(userGroup); } // Assert - result1 = _userService.GetUserGroupById(userGroup1.Id); - result2 = _userService.GetUserGroupById(userGroup2.Id); - result3 = _userService.GetUserGroupById(userGroup3.Id); + result1 = UserService.GetUserGroupById(userGroup1.Id); + result2 = UserService.GetUserGroupById(userGroup2.Id); + result3 = UserService.GetUserGroupById(userGroup3.Id); Assert.IsTrue(result1.AllowedSections.Contains("test")); Assert.IsTrue(result2.AllowedSections.Contains("test")); Assert.IsTrue(result3.AllowedSections.Contains("test")); @@ -853,39 +853,39 @@ namespace Umbraco.Tests.Integration.Services public void Cannot_Create_User_With_Empty_Username() { // Act & Assert - Assert.Throws(() => _userService.CreateUserWithIdentity(string.Empty, "john@umbraco.io")); + Assert.Throws(() => UserService.CreateUserWithIdentity(string.Empty, "john@umbraco.io")); } [Test] public void Cannot_Save_User_With_Empty_Username() { // Arrange - var user = _userService.CreateUserWithIdentity("John Doe", "john@umbraco.io"); + var user = UserService.CreateUserWithIdentity("John Doe", "john@umbraco.io"); user.Username = string.Empty; // Act & Assert - Assert.Throws(() => _userService.Save(user)); + Assert.Throws(() => UserService.Save(user)); } [Test] public void Cannot_Save_User_With_Empty_Name() { // Arrange - var user = _userService.CreateUserWithIdentity("John Doe", "john@umbraco.io"); + var user = UserService.CreateUserWithIdentity("John Doe", "john@umbraco.io"); user.Name = string.Empty; // Act & Assert - Assert.Throws(() => _userService.Save(user)); + Assert.Throws(() => UserService.Save(user)); } [Test] public void Get_By_Profile_Username() { // Arrange - var user = _userService.CreateUserWithIdentity("test1", "test1@test.com"); + var user = UserService.CreateUserWithIdentity("test1", "test1@test.com"); // Act - var profile = _userService.GetProfileByUserName(user.Username); + var profile = UserService.GetProfileByUserName(user.Username); // Assert Assert.IsNotNull(profile); @@ -897,10 +897,10 @@ namespace Umbraco.Tests.Integration.Services public void Get_By_Profile_Id() { // Arrange - var user = _userService.CreateUserWithIdentity("test1", "test1@test.com"); + var user = UserService.CreateUserWithIdentity("test1", "test1@test.com"); // Act - var profile = _userService.GetProfileById((int)user.Id); + var profile = UserService.GetProfileById((int)user.Id); // Assert Assert.IsNotNull(profile); @@ -911,7 +911,7 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Get_By_Profile_Id_Must_Return_Null_If_User_Does_Not_Exist() { - var profile = _userService.GetProfileById(42); + var profile = UserService.GetProfileById(42); // Assert Assert.IsNull(profile); @@ -920,7 +920,7 @@ namespace Umbraco.Tests.Integration.Services [Test] public void GetProfilesById_Must_Return_Empty_If_User_Does_Not_Exist() { - var profiles = _userService.GetProfilesById(42); + var profiles = UserService.GetProfilesById(42); // Assert CollectionAssert.IsEmpty(profiles); @@ -934,7 +934,7 @@ namespace Umbraco.Tests.Integration.Services // Act - var updatedItem = (User)_userService.GetByUsername(originalUser.Username); + var updatedItem = (User)UserService.GetByUsername(originalUser.Username); // Assert Assert.IsNotNull(updatedItem); @@ -962,7 +962,7 @@ namespace Umbraco.Tests.Integration.Services CreateTestUsers(startContentItems.Select(x => x.Id).ToArray(), testUserGroup, 3); - var usersInGroup = _userService.GetAllInGroup(userGroupId); + var usersInGroup = UserService.GetAllInGroup(userGroupId); foreach (var user in usersInGroup) Assert.AreEqual(user.StartContentIds.Length, startContentItems.Length); @@ -972,14 +972,14 @@ namespace Umbraco.Tests.Integration.Services { var contentType = MockedContentTypes.CreateSimpleContentType(); - _contentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var startContentItems = new List(); for (var i = 0; i < numberToCreate; i++) startContentItems.Add(MockedContent.CreateSimpleContent(contentType)); - _contentService.Save(startContentItems); + ContentService.Save(startContentItems); return startContentItems.ToArray(); } @@ -1021,11 +1021,11 @@ namespace Umbraco.Tests.Integration.Services { userGroup = CreateTestUserGroup(); - var user = _userService.CreateUserWithIdentity("test1", "test1@test.com"); + var user = UserService.CreateUserWithIdentity("test1", "test1@test.com"); user.AddGroup(userGroup.ToReadOnlyGroup()); - _userService.Save(user); + UserService.Save(user); return user; } @@ -1036,13 +1036,13 @@ namespace Umbraco.Tests.Integration.Services for (var i = 0; i < numberToCreate; i++) { - var user = _userService.CreateUserWithIdentity($"test{i}", $"test{i}@test.com"); + var user = UserService.CreateUserWithIdentity($"test{i}", $"test{i}@test.com"); user.AddGroup(userGroup.ToReadOnlyGroup()); var updateable = (User)user; updateable.StartContentIds = startContentIds; - _userService.Save(user); + UserService.Save(user); users.Add(user); } @@ -1059,7 +1059,7 @@ namespace Umbraco.Tests.Integration.Services .WithAllowedSections(new[] { "content", "media" }) .Build(); - _userService.Save(userGroup); + UserService.Save(userGroup); return (UserGroup)userGroup; } From 8e48f2cc3179e18d156417bce88940af995b1c72 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Tue, 6 Oct 2020 13:24:32 +0200 Subject: [PATCH 8/8] Further updates from PR comments and additional use of test model builders. --- .../Services/MacroServiceTests.cs | 4 +- .../UserEditorAuthorizationHelperTests.cs | 134 ++++++++---------- .../Controllers/ContentControllerUnitTests.cs | 4 +- .../Controllers/MediaControllerUnitTests.cs | 4 +- ...terAllowedOutgoingContentAttributeTests.cs | 9 +- 5 files changed, 67 insertions(+), 88 deletions(-) diff --git a/src/Umbraco.Tests.Integration/Services/MacroServiceTests.cs b/src/Umbraco.Tests.Integration/Services/MacroServiceTests.cs index 2064365cbb..4f533df188 100644 --- a/src/Umbraco.Tests.Integration/Services/MacroServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/MacroServiceTests.cs @@ -25,10 +25,8 @@ namespace Umbraco.Tests.Integration.Services private MacroService MacroService => (MacroService)GetRequiredService(); [SetUp] - public override void Setup() + public void SetupTest() { - base.Setup(); - var sp = ScopeProvider; using (var scope = sp.CreateScope()) { diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Editors/UserEditorAuthorizationHelperTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Editors/UserEditorAuthorizationHelperTests.cs index bbb0f1b3e3..2fd0c7b8ea 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Editors/UserEditorAuthorizationHelperTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Editors/UserEditorAuthorizationHelperTests.cs @@ -7,6 +7,8 @@ using Umbraco.Core.Models; using Umbraco.Core.Models.Entities; using Umbraco.Core.Models.Membership; using Umbraco.Core.Services; +using Umbraco.Tests.Common.Builders; +using Umbraco.Tests.Common.Builders.Extensions; using Umbraco.Web.Editors; namespace Umbraco.Tests.Web.Controllers @@ -17,8 +19,8 @@ namespace Umbraco.Tests.Web.Controllers [Test] public void Admin_Is_Authorized() { - var currentUser = GetAdminUser(); - var savingUser = GetUserMock(); + var currentUser = CreateAdminUser(); + var savingUser = CreateUser(); var contentService = new Mock(); var mediaService = new Mock(); @@ -31,7 +33,7 @@ namespace Umbraco.Tests.Web.Controllers userService.Object, entityService.Object); - var result = authHelper.IsAuthorized(currentUser, savingUser.Object, new int[0], new int[0], new string[0]); + var result = authHelper.IsAuthorized(currentUser, savingUser, new int[0], new int[0], new string[0]); Assert.IsTrue(result.Success); } @@ -39,8 +41,8 @@ namespace Umbraco.Tests.Web.Controllers [Test] public void Non_Admin_Cannot_Save_Admin() { - var currentUser = GetUserMock(); - var savingUser = GetAdminUser(); + var currentUser = CreateUser(); + var savingUser = CreateAdminUser(); var contentService = new Mock(); var mediaService = new Mock(); @@ -53,7 +55,7 @@ namespace Umbraco.Tests.Web.Controllers userService.Object, entityService.Object); - var result = authHelper.IsAuthorized(currentUser.Object, savingUser, new int[0], new int[0], new string[0]); + var result = authHelper.IsAuthorized(currentUser, savingUser, new int[0], new int[0], new string[0]); Assert.IsFalse(result.Success); } @@ -61,12 +63,8 @@ namespace Umbraco.Tests.Web.Controllers [Test] public void Cannot_Grant_Group_Membership_Without_Being_A_Member() { - var currentUser = GetUserMock(); - currentUser.Setup(x => x.Groups).Returns(new[] - { - new ReadOnlyUserGroup(1, "Test", "icon-user", null, null, "test", new string[0], new string[0]) - }); - var savingUser = GetUserMock(); + var currentUser = CreateUser(withGroup: true); + var savingUser = CreateUser(); var contentService = new Mock(); var mediaService = new Mock(); @@ -79,7 +77,7 @@ namespace Umbraco.Tests.Web.Controllers userService.Object, entityService.Object); - var result = authHelper.IsAuthorized(currentUser.Object, savingUser.Object, new int[0], new int[0], new[] {"FunGroup"}); + var result = authHelper.IsAuthorized(currentUser, savingUser, new int[0], new int[0], new[] {"FunGroup"}); Assert.IsFalse(result.Success); } @@ -87,12 +85,8 @@ namespace Umbraco.Tests.Web.Controllers [Test] public void Can_Grant_Group_Membership_With_Being_A_Member() { - var currentUser = GetUserMock(); - currentUser.Setup(x => x.Groups).Returns(new[] - { - new ReadOnlyUserGroup(1, "Test", "icon-user", null, null, "test", new string[0], new string[0]) - }); - var savingUser = GetUserMock(); + var currentUser = CreateUser(withGroup: true); + var savingUser = CreateUser(); var contentService = new Mock(); var mediaService = new Mock(); @@ -105,7 +99,7 @@ namespace Umbraco.Tests.Web.Controllers userService.Object, entityService.Object); - var result = authHelper.IsAuthorized(currentUser.Object, savingUser.Object, new int[0], new int[0], new[] { "test" }); + var result = authHelper.IsAuthorized(currentUser, savingUser, new int[0], new int[0], new[] { "test" }); Assert.IsTrue(result.Success); } @@ -121,10 +115,8 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = GetUserMock(); - currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 }); - var savingUser = GetUserMock(); - savingUser.Setup(x => x.StartContentIds).Returns(new[] { 1234 }); + var currentUser = CreateUser(startContentIds: new[] { 9876 }); + var savingUser = CreateUser(startContentIds: new[] { 1234 }); var contentService = new Mock(); contentService.Setup(x => x.GetById(It.IsAny())) @@ -145,7 +137,7 @@ namespace Umbraco.Tests.Web.Controllers entityService.Object); //adding 5555 which currentUser has access to since it's a child of 9876 ... adding is still ok even though currentUser doesn't have access to 1234 - var result = authHelper.IsAuthorized(currentUser.Object, savingUser.Object, new[] { 1234, 5555 }, new int[0], new string[0]); + var result = authHelper.IsAuthorized(currentUser, savingUser, new[] { 1234, 5555 }, new int[0], new string[0]); Assert.IsTrue(result.Success); } @@ -161,10 +153,8 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = GetUserMock(); - currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 }); - var savingUser = GetUserMock(); - savingUser.Setup(x => x.StartContentIds).Returns(new[] { 1234, 4567 }); + var currentUser = CreateUser(startContentIds: new[] { 9876 }); + var savingUser = CreateUser(startContentIds: new[] { 1234, 4567 }); var contentService = new Mock(); contentService.Setup(x => x.GetById(It.IsAny())) @@ -185,7 +175,7 @@ namespace Umbraco.Tests.Web.Controllers entityService.Object); //removing 4567 start node even though currentUser doesn't have acces to it ... removing is ok - var result = authHelper.IsAuthorized(currentUser.Object, savingUser.Object, new[] { 1234 }, new int[0], new string[0]); + var result = authHelper.IsAuthorized(currentUser, savingUser, new[] { 1234 }, new int[0], new string[0]); Assert.IsTrue(result.Success); } @@ -201,9 +191,8 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = GetUserMock(); - currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 }); - var savingUser = GetUserMock(); + var currentUser = CreateUser(startContentIds: new[] { 9876 }); + var savingUser = CreateUser(); var contentService = new Mock(); contentService.Setup(x => x.GetById(It.IsAny())) @@ -224,7 +213,7 @@ namespace Umbraco.Tests.Web.Controllers entityService.Object); //adding 1234 but currentUser doesn't have access to it ... nope - var result = authHelper.IsAuthorized(currentUser.Object, savingUser.Object, new []{1234}, new int[0], new string[0]); + var result = authHelper.IsAuthorized(currentUser, savingUser, new []{1234}, new int[0], new string[0]); Assert.IsFalse(result.Success); } @@ -240,9 +229,8 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = GetUserMock(); - currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 }); - var savingUser = GetUserMock(); + var currentUser = CreateUser(startContentIds: new[] { 9876 }); + var savingUser = CreateUser(); var contentService = new Mock(); contentService.Setup(x => x.GetById(It.IsAny())) @@ -263,7 +251,7 @@ namespace Umbraco.Tests.Web.Controllers entityService.Object); //adding 5555 which currentUser has access to since it's a child of 9876 ... ok - var result = authHelper.IsAuthorized(currentUser.Object, savingUser.Object, new[] { 5555 }, new int[0], new string[0]); + var result = authHelper.IsAuthorized(currentUser, savingUser, new[] { 5555 }, new int[0], new string[0]); Assert.IsTrue(result.Success); } @@ -280,9 +268,8 @@ namespace Umbraco.Tests.Web.Controllers }; - var currentUser = GetUserMock(); - currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 }); - var savingUser = GetUserMock(); + var currentUser = CreateUser(startMediaIds: new[] { 9876 }); + var savingUser = CreateUser(); var contentService = new Mock(); var mediaService = new Mock(); @@ -303,7 +290,7 @@ namespace Umbraco.Tests.Web.Controllers entityService.Object); //adding 1234 but currentUser doesn't have access to it ... nope - var result = authHelper.IsAuthorized(currentUser.Object, savingUser.Object, new int[0], new[] {1234}, new string[0]); + var result = authHelper.IsAuthorized(currentUser, savingUser, new int[0], new[] {1234}, new string[0]); Assert.IsFalse(result.Success); } @@ -319,9 +306,8 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = GetUserMock(); - currentUser.Setup(x => x.StartMediaIds).Returns(new[] { 9876 }); - var savingUser = GetUserMock(); + var currentUser = CreateUser(startMediaIds: new[] { 9876 }); + var savingUser = CreateUser(); var contentService = new Mock(); var mediaService = new Mock(); @@ -342,7 +328,7 @@ namespace Umbraco.Tests.Web.Controllers entityService.Object); //adding 5555 which currentUser has access to since it's a child of 9876 ... ok - var result = authHelper.IsAuthorized(currentUser.Object, savingUser.Object, new int[0], new[] { 5555 }, new string[0]); + var result = authHelper.IsAuthorized(currentUser, savingUser, new int[0], new[] { 5555 }, new string[0]); Assert.IsTrue(result.Success); } @@ -358,10 +344,8 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = GetUserMock(); - currentUser.Setup(x => x.StartMediaIds).Returns(new[] { 9876 }); - var savingUser = GetUserMock(); - savingUser.Setup(x => x.StartMediaIds).Returns(new[] { 1234 }); + var currentUser = CreateUser(startMediaIds: new[] { 9876 }); + var savingUser = CreateUser(startMediaIds: new[] { 1234 }); var contentService = new Mock(); var mediaService = new Mock(); @@ -382,7 +366,7 @@ namespace Umbraco.Tests.Web.Controllers entityService.Object); //adding 5555 which currentUser has access to since it's a child of 9876 ... adding is still ok even though currentUser doesn't have access to 1234 - var result = authHelper.IsAuthorized(currentUser.Object, savingUser.Object, new int[0], new[] { 1234, 5555 }, new string[0]); + var result = authHelper.IsAuthorized(currentUser, savingUser, new int[0], new[] { 1234, 5555 }, new string[0]); Assert.IsTrue(result.Success); } @@ -398,10 +382,8 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = GetUserMock(); - currentUser.Setup(x => x.StartMediaIds).Returns(new[] { 9876 }); - var savingUser = GetUserMock(); - savingUser.Setup(x => x.StartMediaIds).Returns(new[] { 1234, 4567 }); + var currentUser = CreateUser(startMediaIds: new[] { 9876 }); + var savingUser = CreateUser(startMediaIds: new[] { 1234, 4567 }); var contentService = new Mock(); var mediaService = new Mock(); @@ -422,33 +404,37 @@ namespace Umbraco.Tests.Web.Controllers entityService.Object); //removing 4567 start node even though currentUser doesn't have acces to it ... removing is ok - var result = authHelper.IsAuthorized(currentUser.Object, savingUser.Object, new int[0], new[] { 1234 }, new string[0]); + var result = authHelper.IsAuthorized(currentUser, savingUser, new int[0], new[] { 1234 }, new string[0]); Assert.IsTrue(result.Success); } - /// - /// Returns a and ensures that the ToUserCache and FromUserCache methods are mapped correctly for - /// dealing with start node caches - /// - /// - private static Mock GetUserMock() + private static IUser CreateUser(bool withGroup = false, int[] startContentIds = null, int[] startMediaIds = null) { - var userCache = new Dictionary(); - var userMock = new Mock(); - userMock.Setup(x => x.FromUserCache(It.IsAny())).Returns((string key) => userCache.TryGetValue(key, out var val) ? val is int[] iVal ? iVal : null : null); - userMock.Setup(x => x.ToUserCache(It.IsAny(), It.IsAny())).Callback((string key, int[] val) => userCache[key] = val); - return userMock; + var builder = new UserBuilder() + .WithStartContentIds(startContentIds != null ? startContentIds : new int[0]) + .WithStartMediaIds(startMediaIds != null ? startMediaIds : new int[0]); + if (withGroup) + { + builder = (UserBuilder)builder + .AddUserGroup() + .WithName("Test") + .WithAlias("test") + .Done(); + } + + return builder.Build(); } - private IUser GetAdminUser() + private static IUser CreateAdminUser() { - var admin = GetUserMock(); - admin.Setup(x => x.Groups).Returns(new[] - { - new ReadOnlyUserGroup(1, "Admin", "icon-user", null, null, Constants.Security.AdminGroupAlias, new string[0], new string[0]) - }); - return admin.Object; + return new UserBuilder() + .AddUserGroup() + .WithId(1) + .WithName("Admin") + .WithAlias(Constants.Security.AdminGroupAlias) + .Done() + .Build(); } } } diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/ContentControllerUnitTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/ContentControllerUnitTests.cs index 14d888470f..2f76db452a 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/ContentControllerUnitTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/ContentControllerUnitTests.cs @@ -334,11 +334,11 @@ namespace Umbraco.Tests.Web.Controllers Assert.AreEqual(ContentPermissionsHelper.ContentAccess.Denied, result); } - private IUser CreateUser(int id = 0, int startContentId = -1, bool withUserGroup = true) + private IUser CreateUser(int id = 0, int? startContentId = null, bool withUserGroup = true) { var builder = new UserBuilder() .WithId(id) - .WithStartContentIds(startContentId == -1 ? new int[0] : new[] { startContentId }); + .WithStartContentIds(startContentId.HasValue ? new[] { startContentId.Value } : new int[0]); if (withUserGroup) { builder = builder diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/MediaControllerUnitTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/MediaControllerUnitTests.cs index 124c934b2f..91305e463c 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/MediaControllerUnitTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Controllers/MediaControllerUnitTests.cs @@ -149,11 +149,11 @@ namespace Umbraco.Tests.Web.Controllers Assert.IsFalse(result); } - private IUser CreateUser(int id = 0, int startMediaId = -1) + private IUser CreateUser(int id = 0, int? startMediaId = null) { return new UserBuilder() .WithId(id) - .WithStartMediaIds(new[] { startMediaId }) + .WithStartMediaIds(startMediaId.HasValue ? new[] { startMediaId.Value } : new int[0]) .AddUserGroup() .WithId(1) .WithName("admin") diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Filters/FilterAllowedOutgoingContentAttributeTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Filters/FilterAllowedOutgoingContentAttributeTests.cs index 6717b0e046..5c186c890c 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Filters/FilterAllowedOutgoingContentAttributeTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Filters/FilterAllowedOutgoingContentAttributeTests.cs @@ -148,16 +148,11 @@ namespace Umbraco.Tests.Web.Controllers Assert.AreEqual(3, list.ElementAt(2).Id); } - private IUser CreateUser(int id = 0, int startContentId = -1) + private IUser CreateUser(int id = 0, int? startContentId = null) { return new UserBuilder() .WithId(id) - .WithStartContentIds(new[] { startContentId }) - .AddUserGroup() - .WithId(1) - .WithName("admin") - .WithAlias("admin") - .Done() + .WithStartContentIds(startContentId.HasValue ? new[] { startContentId.Value } : new int[0]) .Build(); }