diff --git a/src/Umbraco.Core/Scoping/CallContext.cs b/src/Umbraco.Core/Scoping/CallContext.cs index 2937990eab..cc8bf7cc7e 100644 --- a/src/Umbraco.Core/Scoping/CallContext.cs +++ b/src/Umbraco.Core/Scoping/CallContext.cs @@ -20,7 +20,15 @@ namespace Umbraco.Core.Scoping /// The name with which to associate the new item in the call context. /// The object to store in the call context. public static void SetData(string name, T data) => _state.GetOrAdd(name, _ => new AsyncLocal()).Value = data; - + + //Replace the SetData with the following when you need to debug AsyncLocal. The args.ThreadContextChanged can be usefull + //public static void SetData(string name, T data) => _state.GetOrAdd(name, _ => new AsyncLocal(OnValueChanged)).Value = data; + // public static void OnValueChanged(AsyncLocalValueChangedArgs args) + // { + // var typeName = typeof(T).ToString(); + // Console.WriteLine($"OnValueChanged!, Type: {typeName} Prev: #{args.PreviousValue} Current: #{args.CurrentValue}"); + // } + /// /// Retrieves an object with the specified name from the . /// diff --git a/src/Umbraco.Infrastructure/Services/Implement/UserService.cs b/src/Umbraco.Infrastructure/Services/Implement/UserService.cs index 65fb235054..5ad543dcba 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/UserService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/UserService.cs @@ -948,7 +948,7 @@ namespace Umbraco.Core.Services.Implement { var nodeIds = path.GetIdsFromPathReversed(); - if (nodeIds.Length == 0) + if (nodeIds.Length == 0 || user is null) return EntityPermissionSet.Empty(); //collect all permissions structures for all nodes for all groups belonging to the user diff --git a/src/Umbraco.Tests.Common/TestHelpers/Entities/MockedUser.cs b/src/Umbraco.Tests.Common/TestHelpers/Entities/MockedUser.cs index 7488fc1a88..5b77909fec 100644 --- a/src/Umbraco.Tests.Common/TestHelpers/Entities/MockedUser.cs +++ b/src/Umbraco.Tests.Common/TestHelpers/Entities/MockedUser.cs @@ -1,6 +1,7 @@ using Moq; using System; using System.Collections.Generic; +using Umbraco.Core.Configuration.Models; using Umbraco.Core.Models.Membership; namespace Umbraco.Tests.Common.TestHelpers.Entities @@ -21,5 +22,41 @@ namespace Umbraco.Tests.Common.TestHelpers.Entities 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/Services/LocalizationServiceTests.cs b/src/Umbraco.Tests.Integration/Services/LocalizationServiceTests.cs similarity index 64% rename from src/Umbraco.Tests/Services/LocalizationServiceTests.cs rename to src/Umbraco.Tests.Integration/Services/LocalizationServiceTests.cs index 4d3c5fdddc..83d3476324 100644 --- a/src/Umbraco.Tests/Services/LocalizationServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/LocalizationServiceTests.cs @@ -4,15 +4,14 @@ using System.Diagnostics; using System.Linq; using System.Threading; using NUnit.Framework; -using Umbraco.Core; -using Umbraco.Core.Models; -using Umbraco.Tests.TestHelpers; -using Umbraco.Tests.Testing; -using Umbraco.Core.Persistence; using Umbraco.Core.Configuration.Models; -using Umbraco.Tests.Common.Builders; +using Umbraco.Core.Models; +using Umbraco.Core.Persistence; +using Umbraco.Core.Services; +using Umbraco.Tests.Integration.Testing; +using Umbraco.Tests.Testing; -namespace Umbraco.Tests.Services +namespace Umbraco.Tests.Integration.Services { /// /// Tests covering all methods in the LocalizationService class. @@ -22,7 +21,7 @@ namespace Umbraco.Tests.Services [TestFixture] [Apartment(ApartmentState.STA)] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] - public class LocalizationServiceTests : TestWithSomeContentBase + public class LocalizationServiceTests : UmbracoIntegrationTest { private Guid _parentItemGuidId; private int _parentItemIntId; @@ -32,17 +31,19 @@ namespace Umbraco.Tests.Services private int _englishLangId; private GlobalSettings _globalSettings; + private ILocalizationService LocalizationService => GetRequiredService(); - public override void SetUp() + [SetUp] + public void SetUp() { - base.SetUp(); _globalSettings = new GlobalSettings(); + CreateTestData(); } [Test] public void Can_Get_Root_Dictionary_Items() { - var rootItems = ServiceContext.LocalizationService.GetRootDictionaryItems(); + var rootItems = LocalizationService.GetRootDictionaryItems(); Assert.NotNull(rootItems); Assert.IsTrue(rootItems.Any()); @@ -51,14 +52,14 @@ namespace Umbraco.Tests.Services [Test] public void Can_Determint_If_DictionaryItem_Exists() { - var exists = ServiceContext.LocalizationService.DictionaryItemExists("Parent"); + var exists = LocalizationService.DictionaryItemExists("Parent"); Assert.IsTrue(exists); } [Test] public void Can_Get_All_Languages() { - var languages = ServiceContext.LocalizationService.GetAllLanguages(); + var languages = LocalizationService.GetAllLanguages(); Assert.NotNull(languages); Assert.IsTrue(languages.Any()); Assert.That(languages.Count(), Is.EqualTo(3)); @@ -67,37 +68,37 @@ namespace Umbraco.Tests.Services [Test] public void Can_Get_Dictionary_Item_By_Int_Id() { - var parentItem = ServiceContext.LocalizationService.GetDictionaryItemById(_parentItemIntId); + var parentItem = LocalizationService.GetDictionaryItemById(_parentItemIntId); Assert.NotNull(parentItem); - var childItem = ServiceContext.LocalizationService.GetDictionaryItemById(_childItemIntId); + var childItem = LocalizationService.GetDictionaryItemById(_childItemIntId); Assert.NotNull(childItem); } [Test] public void Can_Get_Dictionary_Item_By_Guid_Id() { - var parentItem = ServiceContext.LocalizationService.GetDictionaryItemById(_parentItemGuidId); + var parentItem = LocalizationService.GetDictionaryItemById(_parentItemGuidId); Assert.NotNull(parentItem); - var childItem = ServiceContext.LocalizationService.GetDictionaryItemById(_childItemGuidId); + var childItem = LocalizationService.GetDictionaryItemById(_childItemGuidId); Assert.NotNull(childItem); } [Test] public void Can_Get_Dictionary_Item_By_Key() { - var parentItem = ServiceContext.LocalizationService.GetDictionaryItemByKey("Parent"); + var parentItem = LocalizationService.GetDictionaryItemByKey("Parent"); Assert.NotNull(parentItem); - var childItem = ServiceContext.LocalizationService.GetDictionaryItemByKey("Child"); + var childItem = LocalizationService.GetDictionaryItemByKey("Child"); Assert.NotNull(childItem); } [Test] public void Can_Get_Dictionary_Item_Children() { - var item = ServiceContext.LocalizationService.GetDictionaryItemChildren(_parentItemGuidId); + var item = LocalizationService.GetDictionaryItemChildren(_parentItemGuidId); Assert.NotNull(item); Assert.That(item.Count(), Is.EqualTo(1)); @@ -113,11 +114,11 @@ namespace Umbraco.Tests.Services { using (var scope = ScopeProvider.CreateScope()) { - var en = ServiceContext.LocalizationService.GetLanguageById(_englishLangId); - var dk = ServiceContext.LocalizationService.GetLanguageById(_danishLangId); + var en = LocalizationService.GetLanguageById(_englishLangId); + var dk = LocalizationService.GetLanguageById(_danishLangId); var currParentId = _childItemGuidId; - for (int i = 0; i < 25; i++) + for (var i = 0; i < 25; i++) { //Create 2 per level var desc1 = new DictionaryItem(currParentId, "D1" + i) @@ -136,8 +137,8 @@ namespace Umbraco.Tests.Services new DictionaryTranslation(dk, "BørnVærdi2 " + i) } }; - ServiceContext.LocalizationService.Save(desc1); - ServiceContext.LocalizationService.Save(desc2); + LocalizationService.Save(desc1); + LocalizationService.Save(desc2); currParentId = desc1.Key; } @@ -145,8 +146,7 @@ namespace Umbraco.Tests.Services scope.Database.AsUmbracoDatabase().EnableSqlTrace = true; scope.Database.AsUmbracoDatabase().EnableSqlCount = true; - var items = ServiceContext.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.Services [Test] public void Can_GetLanguageById() { - var danish = ServiceContext.LocalizationService.GetLanguageById(_danishLangId); - var english = ServiceContext.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.Services [Test] public void Can_GetLanguageByIsoCode() { - var danish = ServiceContext.LocalizationService.GetLanguageByIsoCode("da-DK"); - var english = ServiceContext.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.Services [Test] public void Does_Not_Fail_When_Language_Doesnt_Exist() { - var language = ServiceContext.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 = ServiceContext.LocalizationService.GetDictionaryItemByKey("RandomKey"); + var item = LocalizationService.GetDictionaryItemByKey("RandomKey"); Assert.Null(item); } @@ -192,34 +192,34 @@ namespace Umbraco.Tests.Services public void Can_Delete_Language() { var norwegian = new Language(_globalSettings, "nb-NO") { CultureName = "Norwegian" }; - ServiceContext.LocalizationService.Save(norwegian, 0); + LocalizationService.Save(norwegian, 0); Assert.That(norwegian.HasIdentity, Is.True); var languageId = norwegian.Id; - ServiceContext.LocalizationService.Delete(norwegian); + LocalizationService.Delete(norwegian); - var language = ServiceContext.LocalizationService.GetLanguageById(languageId); + var language = LocalizationService.GetLanguageById(languageId); Assert.Null(language); } [Test] public void Can_Delete_Language_Used_As_Fallback() { - var danish = ServiceContext.LocalizationService.GetLanguageByIsoCode("da-DK"); + var danish = LocalizationService.GetLanguageByIsoCode("da-DK"); var norwegian = new Language(_globalSettings, "nb-NO") { CultureName = "Norwegian", FallbackLanguageId = danish.Id }; - ServiceContext.LocalizationService.Save(norwegian, 0); + LocalizationService.Save(norwegian, 0); var languageId = danish.Id; - ServiceContext.LocalizationService.Delete(danish); + LocalizationService.Delete(danish); - var language = ServiceContext.LocalizationService.GetLanguageById(languageId); + var language = LocalizationService.GetLanguageById(languageId); Assert.Null(language); } [Test] public void Can_Create_DictionaryItem_At_Root() { - var english = ServiceContext.LocalizationService.GetLanguageByIsoCode("en-US"); + var english = LocalizationService.GetLanguageByIsoCode("en-US"); var item = (IDictionaryItem)new DictionaryItem("Testing123") { @@ -228,10 +228,10 @@ namespace Umbraco.Tests.Services new DictionaryTranslation(english, "Hello world") } }; - ServiceContext.LocalizationService.Save(item); + LocalizationService.Save(item); //re-get - item = ServiceContext.LocalizationService.GetDictionaryItemById(item.Id); + item = LocalizationService.GetDictionaryItemById(item.Id); Assert.Greater(item.Id, 0); Assert.IsTrue(item.HasIdentity); @@ -243,44 +243,42 @@ namespace Umbraco.Tests.Services [Test] public void Can_Create_DictionaryItem_At_Root_With_Identity() { - - var item = ServiceContext.LocalizationService.CreateDictionaryItemWithIdentity( + var item = LocalizationService.CreateDictionaryItemWithIdentity( "Testing12345", null, "Hellooooo"); //re-get - item = ServiceContext.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 = ServiceContext.LocalizationService.GetAllLanguages(); + var allLangs = LocalizationService.GetAllLanguages(); Assert.Greater(allLangs.Count(), 0); foreach (var language in allLangs) { Assert.AreEqual("Hellooooo", item.Translations.Single(x => x.Language.CultureName == language.CultureName).Value); } - } [Test] public void Can_Add_Translation_To_Existing_Dictionary_Item() { - var english = ServiceContext.LocalizationService.GetLanguageByIsoCode("en-US"); + var english = LocalizationService.GetLanguageByIsoCode("en-US"); var item = (IDictionaryItem) new DictionaryItem("Testing123"); - ServiceContext.LocalizationService.Save(item); + LocalizationService.Save(item); //re-get - item = ServiceContext.LocalizationService.GetDictionaryItemById(item.Id); + item = LocalizationService.GetDictionaryItemById(item.Id); item.Translations = new List { new DictionaryTranslation(english, "Hello world") }; - ServiceContext.LocalizationService.Save(item); + LocalizationService.Save(item); Assert.AreEqual(1, item.Translations.Count()); foreach (var translation in item.Translations) @@ -291,14 +289,14 @@ namespace Umbraco.Tests.Services item.Translations = new List(item.Translations) { new DictionaryTranslation( - ServiceContext.LocalizationService.GetLanguageByIsoCode("en-GB"), + LocalizationService.GetLanguageByIsoCode("en-GB"), "My new value") }; - ServiceContext.LocalizationService.Save(item); + LocalizationService.Save(item); //re-get - item = ServiceContext.LocalizationService.GetDictionaryItemById(item.Id); + item = LocalizationService.GetDictionaryItemById(item.Id); Assert.AreEqual(2, item.Translations.Count()); Assert.AreEqual("Hello world", item.Translations.First().Value); @@ -308,27 +306,27 @@ namespace Umbraco.Tests.Services [Test] public void Can_Delete_DictionaryItem() { - var item = ServiceContext.LocalizationService.GetDictionaryItemByKey("Child"); + var item = LocalizationService.GetDictionaryItemByKey("Child"); Assert.NotNull(item); - ServiceContext.LocalizationService.Delete(item); + LocalizationService.Delete(item); - var deletedItem = ServiceContext.LocalizationService.GetDictionaryItemByKey("Child"); + var deletedItem = LocalizationService.GetDictionaryItemByKey("Child"); Assert.Null(deletedItem); } [Test] public void Can_Update_Existing_DictionaryItem() { - var item = ServiceContext.LocalizationService.GetDictionaryItemByKey("Child"); + var item = LocalizationService.GetDictionaryItemByKey("Child"); foreach (var translation in item.Translations) { translation.Value = translation.Value + "UPDATED"; } - ServiceContext.LocalizationService.Save(item); + LocalizationService.Save(item); - var updatedItem = ServiceContext.LocalizationService.GetDictionaryItemByKey("Child"); + var updatedItem = LocalizationService.GetDictionaryItemByKey("Child"); Assert.NotNull(updatedItem); foreach (var translation in updatedItem.Translations) @@ -340,11 +338,8 @@ namespace Umbraco.Tests.Services [Test] public void Find_BaseData_Language() { - // Arrange - var localizationService = ServiceContext.LocalizationService; - // Act - var languages = localizationService.GetAllLanguages(); + var languages = LocalizationService.GetAllLanguages(); // Assert Assert.That(3, Is.EqualTo(languages.Count())); @@ -354,13 +349,12 @@ namespace Umbraco.Tests.Services public void Save_Language_And_GetLanguageByIsoCode() { // Arrange - var localizationService = ServiceContext.LocalizationService; var isoCode = "en-AU"; 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); @@ -369,13 +363,12 @@ namespace Umbraco.Tests.Services [Test] public void Save_Language_And_GetLanguageById() { - var localizationService = ServiceContext.LocalizationService; var isoCode = "en-AU"; 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); @@ -384,20 +377,17 @@ namespace Umbraco.Tests.Services [Test] public void Set_Default_Language() { - var localizationService = ServiceContext.LocalizationService; - var language = new Core.Models.Language(_globalSettings, "en-AU"); - language.IsDefault = true; - localizationService.Save(language); - var result = localizationService.GetLanguageById(language.Id); + var language = new Language(_globalSettings, "en-AU") {IsDefault = true}; + LocalizationService.Save(language); + var result = LocalizationService.GetLanguageById(language.Id); Assert.IsTrue(result.IsDefault); - var language2 = new Core.Models.Language(_globalSettings, "en-NZ"); - language2.IsDefault = true; - localizationService.Save(language2); - var result2 = localizationService.GetLanguageById(language2.Id); + var language2 = new Language(_globalSettings, "en-NZ") {IsDefault = true}; + 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); @@ -406,25 +396,24 @@ namespace Umbraco.Tests.Services [Test] public void Deleted_Language_Should_Not_Exist() { - var localizationService = ServiceContext.LocalizationService; 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); } - public override void CreateTestData() + public void CreateTestData() { var danish = new Language(_globalSettings, "da-DK") { CultureName = "Danish" }; var english = new Language(_globalSettings, "en-GB") { CultureName = "English" }; - ServiceContext.LocalizationService.Save(danish, 0); - ServiceContext.LocalizationService.Save(english, 0); + LocalizationService.Save(danish, 0); + LocalizationService.Save(english, 0); _danishLangId = danish.Id; _englishLangId = english.Id; @@ -436,7 +425,7 @@ namespace Umbraco.Tests.Services new DictionaryTranslation(danish, "ForældreVærdi") } }; - ServiceContext.LocalizationService.Save(parentItem); + LocalizationService.Save(parentItem); _parentItemGuidId = parentItem.Key; _parentItemIntId = parentItem.Id; @@ -448,10 +437,9 @@ namespace Umbraco.Tests.Services new DictionaryTranslation(danish, "BørnVærdi") } }; - ServiceContext.LocalizationService.Save(childItem); + LocalizationService.Save(childItem); _childItemGuidId = childItem.Key; _childItemIntId = childItem.Id; } - } } diff --git a/src/Umbraco.Tests/Services/MediaServiceTests.cs b/src/Umbraco.Tests.Integration/Services/MediaServiceTests.cs similarity index 66% rename from src/Umbraco.Tests/Services/MediaServiceTests.cs rename to src/Umbraco.Tests.Integration/Services/MediaServiceTests.cs index b3dc274c5e..a43f21d061 100644 --- a/src/Umbraco.Tests/Services/MediaServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/MediaServiceTests.cs @@ -1,38 +1,34 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Text; using System.Threading; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Events; using Umbraco.Core.Models; -using Umbraco.Core.Persistence; -using Umbraco.Core.Persistence.DatabaseModelDefinitions; -using Umbraco.Core.Persistence.Dtos; -using Umbraco.Core.Persistence.Repositories; using Umbraco.Core.Services; using Umbraco.Core.Services.Implement; -using Umbraco.Tests.LegacyXmlPublishedCache; -using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.Integration.Testing; using Umbraco.Tests.TestHelpers.Entities; using Umbraco.Tests.Testing; -namespace Umbraco.Tests.Services +namespace Umbraco.Tests.Integration.Services { [TestFixture] [Apartment(ApartmentState.STA)] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, PublishedRepositoryEvents = true)] - public class MediaServiceTests : TestWithSomeContentBase + public class MediaServiceTests : UmbracoIntegrationTest { + private IMediaService MediaService => GetRequiredService(); + private IMediaTypeService MediaTypeService => GetRequiredService(); + /// /// Used to list out all ambiguous events that will require dispatching with a name /// [Test, Explicit] public void List_Ambiguous_Events() { - var events = ServiceContext.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) { @@ -55,32 +51,37 @@ namespace Umbraco.Tests.Services [Test] public void Get_Paged_Children_With_Media_Type_Filter() { - var mediaService = ServiceContext.MediaService; var mediaType1 = MockedContentTypes.CreateImageMediaType("Image2"); - ServiceContext.MediaTypeService.Save(mediaType1); + MediaTypeService.Save(mediaType1); var mediaType2 = MockedContentTypes.CreateImageMediaType("Image3"); - ServiceContext.MediaTypeService.Save(mediaType2); + MediaTypeService.Save(mediaType2); - for (int i = 0; i < 10; i++) + 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 result = ServiceContext.MediaService.GetPagedChildren(-1, 0, 11, out total, - 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); + var provider = ScopeProvider; + using (provider.CreateScope()) + { + 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 = ServiceContext.MediaService.GetPagedChildren(-1, 1, 11, out total, - SqlContext.Query().Where(x => new[] { mediaType1.Id, mediaType2.Id }.Contains(x.ContentTypeId)), - Ordering.By("SortOrder", Direction.Ascending)); - Assert.AreEqual(9, result.Count()); - Assert.AreEqual(20, 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)); + Assert.AreEqual(9, result.Count()); + Assert.AreEqual(20, total); + } } [Test] @@ -88,11 +89,10 @@ namespace Umbraco.Tests.Services { // Arrange var mediaItems = CreateTrashedTestMedia(); - var mediaService = ServiceContext.MediaService; - 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,11 +104,10 @@ namespace Umbraco.Tests.Services { // Arrange var mediaItems = CreateTrashedTestMedia(); - var mediaService = ServiceContext.MediaService; - 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)); @@ -120,12 +119,11 @@ namespace Umbraco.Tests.Services { // Arrange var mediaItems = CreateTrashedTestMedia(); - var mediaService = ServiceContext.MediaService; - 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)); @@ -138,43 +136,40 @@ namespace Umbraco.Tests.Services public void Cannot_Save_Media_With_Empty_Name() { // Arrange - var mediaService = ServiceContext.MediaService; var mediaType = MockedContentTypes.CreateVideoMediaType(); - ServiceContext.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] public void Ensure_Content_Xml_Created() { - var mediaService = ServiceContext.MediaService; var mediaType = MockedContentTypes.CreateVideoMediaType(); - ServiceContext.MediaTypeService.Save(mediaType); - var media = mediaService.CreateMedia("Test", -1, "video"); + MediaTypeService.Save(mediaType); + var media = MediaService.CreateMedia("Test", -1, "video"); - mediaService.Save(media); + MediaService.Save(media); using (var scope = ScopeProvider.CreateScope()) { Assert.IsTrue(scope.Database.Exists(media.Id)); } - } + }*/ [Test] public void Can_Get_Media_By_Path() { - var mediaService = ServiceContext.MediaService; var mediaType = MockedContentTypes.CreateImageMediaType("Image2"); - ServiceContext.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); @@ -183,15 +178,14 @@ namespace Umbraco.Tests.Services [Test] public void Can_Get_Media_With_Crop_By_Path() { - var mediaService = ServiceContext.MediaService; var mediaType = MockedContentTypes.CreateImageMediaTypeWithCrop("Image2"); - ServiceContext.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)); @@ -201,14 +195,14 @@ namespace Umbraco.Tests.Services public void Can_Get_Paged_Children() { var mediaType = MockedContentTypes.CreateImageMediaType("Image2"); - ServiceContext.MediaTypeService.Save(mediaType); - for (int i = 0; i < 10; i++) + MediaTypeService.Save(mediaType); + for (var i = 0; i < 10; i++) { var c1 = MockedMedia.CreateMediaImage(mediaType, -1); - ServiceContext.MediaService.Save(c1); + MediaService.Save(c1); } - var service = ServiceContext.MediaService; + var service = MediaService; long total; var entities = service.GetPagedChildren(-1, 0, 6, out total).ToArray(); @@ -223,25 +217,25 @@ namespace Umbraco.Tests.Services public void Can_Get_Paged_Children_Dont_Get_Descendants() { var mediaType = MockedContentTypes.CreateImageMediaType("Image2"); - ServiceContext.MediaTypeService.Save(mediaType); + MediaTypeService.Save(mediaType); // only add 9 as we also add a folder with children - for (int i = 0; i < 9; i++) + for (var i = 0; i < 9; i++) { var m1 = MockedMedia.CreateMediaImage(mediaType, -1); - ServiceContext.MediaService.Save(m1); + MediaService.Save(m1); } var mediaTypeForFolder = MockedContentTypes.CreateImageMediaType("Folder2"); - ServiceContext.MediaTypeService.Save(mediaTypeForFolder); + MediaTypeService.Save(mediaTypeForFolder); var mediaFolder = MockedMedia.CreateMediaFolder(mediaTypeForFolder, -1); - ServiceContext.MediaService.Save(mediaFolder); - for (int i = 0; i < 10; i++) + MediaService.Save(mediaFolder); + for (var i = 0; i < 10; i++) { var m1 = MockedMedia.CreateMediaImage(mediaType, mediaFolder.Id); - ServiceContext.MediaService.Save(m1); + MediaService.Save(m1); } - var service = ServiceContext.MediaService; + var service = MediaService; long total; // children in root including the folder - not the descendants in the folder @@ -264,29 +258,28 @@ namespace Umbraco.Tests.Services private Tuple CreateTrashedTestMedia() { //Create and Save folder-Media -> 1050 - var folderMediaType = ServiceContext.MediaTypeService.Get(1031); + var folderMediaType = MediaTypeService.Get(1031); var folder = MockedMedia.CreateMediaFolder(folderMediaType, -1); - ServiceContext.MediaService.Save(folder); + MediaService.Save(folder); //Create and Save folder-Media -> 1051 var folder2 = MockedMedia.CreateMediaFolder(folderMediaType, -1); - ServiceContext.MediaService.Save(folder2); + MediaService.Save(folder2); //Create and Save image-Media -> 1052 - var imageMediaType = ServiceContext.MediaTypeService.Get(1032); + var imageMediaType = MediaTypeService.Get(1032); var image = (Media)MockedMedia.CreateMediaImage(imageMediaType, 1050); - ServiceContext.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; - ServiceContext.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; - ServiceContext.MediaService.Save(imageTrashed); - + MediaService.Save(imageTrashed); return new Tuple(folder, folder2, image, folderTrashed, imageTrashed); } diff --git a/src/Umbraco.Tests/Services/PublicAccessServiceTests.cs b/src/Umbraco.Tests.Integration/Services/PublicAccessServiceTests.cs similarity index 63% rename from src/Umbraco.Tests/Services/PublicAccessServiceTests.cs rename to src/Umbraco.Tests.Integration/Services/PublicAccessServiceTests.cs index 8434aad332..5294e8015b 100644 --- a/src/Umbraco.Tests/Services/PublicAccessServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/PublicAccessServiceTests.cs @@ -4,41 +4,42 @@ using System.Threading; using NUnit.Framework; using Umbraco.Core.Models; using Umbraco.Core.Services; -using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.Integration.Testing; using Umbraco.Tests.TestHelpers.Entities; using Umbraco.Tests.Testing; -namespace Umbraco.Tests.Services +namespace Umbraco.Tests.Integration.Services { [TestFixture] [Apartment(ApartmentState.STA)] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] - public class PublicAccessServiceTests : TestWithSomeContentBase + public class PublicAccessServiceTests : UmbracoIntegrationTest { + 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 contentService = ServiceContext.ContentService; - var contentTypeService = ServiceContext.ContentTypeService; var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah"); - ServiceContext.FileService.SaveTemplate(ct.DefaultTemplate); - contentTypeService.Save(ct); + FileService.SaveTemplate(ct.DefaultTemplate); + ContentTypeService.Save(ct); var c = MockedContent.CreateSimpleContent(ct, "Test", -1); - contentService.Save(c); - var publicAccessService = ServiceContext.PublicAccessService; - + ContentService.Save(c); // Act var entry = new PublicAccessEntry(c, c, c, new[] - { + { new PublicAccessRule() { RuleType = "TestType", RuleValue = "TestVal" }, }); - var result = publicAccessService.Save(entry); + var result = PublicAccessService.Save(entry); // Assert Assert.IsTrue(result.Success); @@ -54,28 +55,25 @@ namespace Umbraco.Tests.Services public void Can_Add_Rule() { // Arrange - var contentService = ServiceContext.ContentService; - var contentTypeService = ServiceContext.ContentTypeService; var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah"); - ServiceContext.FileService.SaveTemplate(ct.DefaultTemplate); - contentTypeService.Save(ct); + FileService.SaveTemplate(ct.DefaultTemplate); + ContentTypeService.Save(ct); var c = MockedContent.CreateSimpleContent(ct, "Test", -1); - contentService.Save(c); - var publicAccessService = ServiceContext.PublicAccessService; + ContentService.Save(c); var entry = new PublicAccessEntry(c, c, c, new[] - { + { new PublicAccessRule() { RuleType = "TestType", 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); @@ -87,30 +85,27 @@ namespace Umbraco.Tests.Services public void Can_Add_Multiple_Value_For_Same_Rule_Type() { // Arrange - var contentService = ServiceContext.ContentService; - var contentTypeService = ServiceContext.ContentTypeService; var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah"); - ServiceContext.FileService.SaveTemplate(ct.DefaultTemplate); - contentTypeService.Save(ct); + FileService.SaveTemplate(ct.DefaultTemplate); + ContentTypeService.Save(ct); var c = MockedContent.CreateSimpleContent(ct, "Test", -1); - contentService.Save(c); - var publicAccessService = ServiceContext.PublicAccessService; + ContentService.Save(c); var entry = new PublicAccessEntry(c, c, c, new[] - { + { new PublicAccessRule() { RuleType = "TestType", 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); @@ -124,16 +119,13 @@ namespace Umbraco.Tests.Services public void Can_Remove_Rule() { // Arrange - var contentService = ServiceContext.ContentService; - var contentTypeService = ServiceContext.ContentTypeService; var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah"); - ServiceContext.FileService.SaveTemplate(ct.DefaultTemplate); - contentTypeService.Save(ct); + FileService.SaveTemplate(ct.DefaultTemplate); + ContentTypeService.Save(ct); var c = MockedContent.CreateSimpleContent(ct, "Test", -1); - contentService.Save(c); - var publicAccessService = ServiceContext.PublicAccessService; + ContentService.Save(c); var entry = new PublicAccessEntry(c, c, c, new[] - { + { new PublicAccessRule() { RuleType = "TestType", @@ -145,12 +137,12 @@ namespace Umbraco.Tests.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); @@ -158,6 +150,5 @@ namespace Umbraco.Tests.Services Assert.AreEqual(1, entry.Rules.Count()); Assert.AreEqual("TestValue2", entry.Rules.ElementAt(0).RuleValue); } - } } diff --git a/src/Umbraco.Tests/Services/TagServiceTests.cs b/src/Umbraco.Tests.Integration/Services/TagServiceTests.cs similarity index 76% rename from src/Umbraco.Tests/Services/TagServiceTests.cs rename to src/Umbraco.Tests.Integration/Services/TagServiceTests.cs index f4916168d4..db2c8196e7 100644 --- a/src/Umbraco.Tests/Services/TagServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/TagServiceTests.cs @@ -2,16 +2,15 @@ using System.Threading; using Newtonsoft.Json; using NUnit.Framework; -using NUnit.Framework.Internal; using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.PropertyEditors; using Umbraco.Core.Services; -using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.Integration.Testing; using Umbraco.Tests.TestHelpers.Entities; using Umbraco.Tests.Testing; -namespace Umbraco.Tests.Services +namespace Umbraco.Tests.Integration.Services { /// /// Tests covering methods in the TagService class. @@ -21,41 +20,42 @@ namespace Umbraco.Tests.Services [TestFixture] [Apartment(ApartmentState.STA)] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] - public class TagServiceTests : TestWithSomeContentBase + public class TagServiceTests : UmbracoIntegrationTest { - public PropertyEditorCollection PropertyEditorCollection => Factory.GetInstance(); + private IContentService ContentService => GetRequiredService(); + private IContentTypeService ContentTypeService => GetRequiredService(); + private ITagService TagService => GetRequiredService(); + private IDataTypeService DataTypeService => GetRequiredService(); + public PropertyEditorCollection PropertyEditorCollection => GetRequiredService(); [Test] public void TagApiConsistencyTest() { - var contentService = ServiceContext.ContentService; - var contentTypeService = ServiceContext.ContentTypeService; - var tagService = ServiceContext.TagService; var contentType = MockedContentTypes.CreateSimpleContentType("umbMandatory", "Mandatory Doc Type", true); contentType.PropertyGroups.First().PropertyTypes.Add( new PropertyType(ShortStringHelper, "test", ValueStorageType.Ntext, "tags") { 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); + ContentService.SaveAndPublish(content1); // change content1.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "elephant" }, true); content1.RemoveTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow" }); - contentService.SaveAndPublish(content1); + ContentService.SaveAndPublish(content1); // more changes content1.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "mouse" }, true); - contentService.SaveAndPublish(content1); + ContentService.SaveAndPublish(content1); content1.RemoveTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "mouse" }); - contentService.SaveAndPublish(content1); + 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.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(); @@ -76,31 +76,28 @@ namespace Umbraco.Tests.Services [Test] public void TagList_Contains_NodeCount() { - var contentService = ServiceContext.ContentService; - var contentTypeService = ServiceContext.ContentTypeService; - var tagService = ServiceContext.TagService; var contentType = MockedContentTypes.CreateSimpleContentType("umbMandatory", "Mandatory Doc Type", true); contentType.PropertyGroups.First().PropertyTypes.Add( new PropertyType(ShortStringHelper, Constants.PropertyEditors.Aliases.Tags, ValueStorageType.Ntext, "tags") { 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); + ContentService.SaveAndPublish(content1); var content2 = MockedContent.CreateSimpleContent(contentType, "Tagged content 2", -1); content2.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow", "pig" }); - contentService.SaveAndPublish(content2); + ContentService.SaveAndPublish(content2); var content3 = MockedContent.CreateSimpleContent(contentType, "Tagged content 3", -1); content3.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow" }); - contentService.SaveAndPublish(content3); + ContentService.SaveAndPublish(content3); // Act - var tags = tagService.GetAllContentTags() + var tags = TagService.GetAllContentTags() .OrderByDescending(x => x.NodeCount) .ToList(); diff --git a/src/Umbraco.Tests/Services/UserServiceTests.cs b/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs similarity index 67% rename from src/Umbraco.Tests/Services/UserServiceTests.cs rename to src/Umbraco.Tests.Integration/Services/UserServiceTests.cs index 76eda054bc..64bd89c8f8 100644 --- a/src/Umbraco.Tests/Services/UserServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs @@ -12,43 +12,45 @@ 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.Integration.Testing; using Umbraco.Tests.TestHelpers.Entities; using Umbraco.Tests.Testing; using Umbraco.Web.Actions; -using MockedUser = Umbraco.Tests.TestHelpers.Entities.MockedUser; +using MockedUser = Umbraco.Tests.Common.TestHelpers.Entities.MockedUser; -namespace Umbraco.Tests.Services +namespace Umbraco.Tests.Integration.Services { /// /// Tests covering the UserService /// [TestFixture] [Apartment(ApartmentState.STA)] - [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, WithApplication = true)] - public class UserServiceTests : TestWithSomeContentBase + [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] + public class UserServiceTests : UmbracoIntegrationTest { + private UserService UserService => (UserService) GetRequiredService(); + private IContentTypeService ContentTypeService => GetRequiredService(); + private IContentService ContentService => GetRequiredService(); + [Test] public void Get_User_Permissions_For_Unassigned_Permission_Nodes() { // Arrange - var userService = ServiceContext.UserService; - var user = CreateTestUser(out var userGroup); + var user = CreateTestUser(out _); var contentType = MockedContentTypes.CreateSimpleContentType(); - ServiceContext.ContentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - ServiceContext.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 Assert.AreEqual(3, permissions.Length); Assert.AreEqual(17, permissions[0].AssignedPermissions.Length); Assert.AreEqual(17, permissions[1].AssignedPermissions.Length); @@ -59,30 +61,28 @@ namespace Umbraco.Tests.Services public void Get_User_Permissions_For_Assigned_Permission_Nodes() { // Arrange - var userService = ServiceContext.UserService; - IUserGroup userGroup; - var user = CreateTestUser(out userGroup); + var user = CreateTestUser(out var userGroup); var contentType = MockedContentTypes.CreateSimpleContentType(); - ServiceContext.ContentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - ServiceContext.ContentService.Save(content); - ServiceContext.ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.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 Assert.AreEqual(3, permissions.Length); Assert.AreEqual(3, permissions[0].AssignedPermissions.Length); Assert.AreEqual(2, permissions[1].AssignedPermissions.Length); @@ -93,29 +93,28 @@ namespace Umbraco.Tests.Services public void Get_UserGroup_Assigned_Permissions() { // Arrange - var userService = ServiceContext.UserService; var userGroup = CreateTestUserGroup(); var contentType = MockedContentTypes.CreateSimpleContentType(); - ServiceContext.ContentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - ServiceContext.ContentService.Save(content); - ServiceContext.ContentService.SetPermission(content.ElementAt(0), ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(content.ElementAt(0), ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(content.ElementAt(0), ActionMove.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(content.ElementAt(1), ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(content.ElementAt(1), ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.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 Assert.AreEqual(3, permissions.Length); Assert.AreEqual(3, permissions[0].AssignedPermissions.Length); Assert.AreEqual(2, permissions[1].AssignedPermissions.Length); @@ -126,29 +125,28 @@ namespace Umbraco.Tests.Services public void Get_UserGroup_Assigned_And_Default_Permissions() { // Arrange - var userService = ServiceContext.UserService; var userGroup = CreateTestUserGroup(); var contentType = MockedContentTypes.CreateSimpleContentType(); - ServiceContext.ContentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - ServiceContext.ContentService.Save(content); - ServiceContext.ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.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 + // Assert Assert.AreEqual(3, permissions.Length); Assert.AreEqual(3, permissions[0].AssignedPermissions.Length); Assert.AreEqual(2, permissions[1].AssignedPermissions.Length); @@ -159,44 +157,43 @@ namespace Umbraco.Tests.Services public void Get_All_User_Permissions_For_All_Nodes_With_Explicit_Permission() { // Arrange - var userService = ServiceContext.UserService; 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(); - ServiceContext.ContentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - ServiceContext.ContentService.Save(content); + ContentService.Save(content); //assign permissions - we aren't assigning anything explicit for group3 and nothing explicit for content[2] /w group2 - ServiceContext.ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup1.Id }); - ServiceContext.ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup1.Id }); - ServiceContext.ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup2.Id }); - ServiceContext.ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup1.Id }); - ServiceContext.ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup2.Id }); - ServiceContext.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())); - //assert + // Assert //there will be 3 since that is how many content items there are Assert.AreEqual(3, permissions.Count); @@ -239,32 +236,31 @@ namespace Umbraco.Tests.Services public void Get_All_User_Group_Permissions_For_All_Nodes() { // Arrange - var userService = ServiceContext.UserService; var userGroup = CreateTestUserGroup(); var contentType = MockedContentTypes.CreateSimpleContentType(); - ServiceContext.ContentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var content = new[] { MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType), MockedContent.CreateSimpleContent(contentType) }; - ServiceContext.ContentService.Save(content); - ServiceContext.ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.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); - //assert + // Assert Assert.AreEqual(3, permissions.Count); Assert.IsTrue(permissions.ContainsKey(content[0].Id)); Assert.AreEqual(3, permissions[content[0].Id].SelectMany(x => x.AssignedPermissions).Count()); @@ -339,7 +335,6 @@ namespace Umbraco.Tests.Services allPermissions = result.GetAllPermissions().ToArray(); Assert.AreEqual(5, allPermissions.Length, string.Join(",", allPermissions)); Assert.IsTrue(allPermissions.ContainsAll(new[] { "S", "D", "F", "G", "K" })); - } [Test] @@ -402,28 +397,27 @@ namespace Umbraco.Tests.Services public void Get_User_Implicit_Permissions() { // Arrange - var userService = ServiceContext.UserService; var userGroup = CreateTestUserGroup(); var contentType = MockedContentTypes.CreateSimpleContentType(); - ServiceContext.ContentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var parent = MockedContent.CreateSimpleContent(contentType); - ServiceContext.ContentService.Save(parent); + ContentService.Save(parent); var child1 = MockedContent.CreateSimpleContent(contentType, "child1", parent); - ServiceContext.ContentService.Save(child1); + ContentService.Save(child1); var child2 = MockedContent.CreateSimpleContent(contentType, "child2", child1); - ServiceContext.ContentService.Save(child2); + ContentService.Save(child2); - ServiceContext.ContentService.SetPermission(parent, ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(parent, ActionDelete.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(parent, ActionMove.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.ContentService.SetPermission(parent, ActionBrowse.ActionLetter, new int[] { userGroup.Id }); - ServiceContext.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 + // Assert var allPermissions = permissions.GetAllPermissions().ToArray(); Assert.AreEqual(3, allPermissions.Length); } @@ -431,10 +425,10 @@ namespace Umbraco.Tests.Services [Test] public void Can_Delete_User() { - var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - ServiceContext.UserService.Delete(user, true); - var deleted = ServiceContext.UserService.GetUserById(user.Id); + UserService.Delete(user, true); + var deleted = UserService.GetUserById(user.Id); // Assert Assert.That(deleted, Is.Null); @@ -443,10 +437,10 @@ namespace Umbraco.Tests.Services [Test] public void Disables_User_Instead_Of_Deleting_If_Flag_Not_Set() { - var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - ServiceContext.UserService.Delete(user); - var deleted = ServiceContext.UserService.GetUserById(user.Id); + UserService.Delete(user); + var deleted = UserService.GetUserById(user.Id); // Assert Assert.That(deleted, Is.Not.Null); @@ -455,60 +449,60 @@ namespace Umbraco.Tests.Services [Test] public void Exists_By_Username() { - var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - var user2 = ServiceContext.UserService.CreateUserWithIdentity("john2@umbraco.io", "john2@umbraco.io"); - Assert.IsTrue(ServiceContext.UserService.Exists("JohnDoe")); - Assert.IsFalse(ServiceContext.UserService.Exists("notFound")); - Assert.IsTrue(ServiceContext.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 = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - Assert.IsNotNull(ServiceContext.UserService.GetByEmail(user.Email)); - Assert.IsNull(ServiceContext.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 = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - Assert.IsNotNull(ServiceContext.UserService.GetByUsername(user.Username)); - Assert.IsNull(ServiceContext.UserService.GetByUsername("notFound")); + Assert.IsNotNull(UserService.GetByUsername(user.Username)); + Assert.IsNull(UserService.GetByUsername("notFound")); } [Test] public void Get_By_Username_With_Backslash() { - var user = ServiceContext.UserService.CreateUserWithIdentity("mydomain\\JohnDoe", "john@umbraco.io"); + var user = UserService.CreateUserWithIdentity("mydomain\\JohnDoe", "john@umbraco.io"); - Assert.IsNotNull(ServiceContext.UserService.GetByUsername(user.Username)); - Assert.IsNull(ServiceContext.UserService.GetByUsername("notFound")); + Assert.IsNotNull(UserService.GetByUsername(user.Username)); + Assert.IsNull(UserService.GetByUsername("notFound")); } [Test] public void Get_By_Object_Id() { - var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); + var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io"); - Assert.IsNotNull(ServiceContext.UserService.GetUserById(user.Id)); - Assert.IsNull(ServiceContext.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); - ServiceContext.UserService.Save(users); + UserService.Save(users); //don't find this var customUser = MockedUser.CreateUser(); customUser.Email = "hello@hello.com"; - ServiceContext.UserService.Save(customUser); + UserService.Save(customUser); - var found = ServiceContext.UserService.FindByEmail("tes", 0, 100, out _, StringPropertyMatchType.StartsWith); + var found = UserService.FindByEmail("tes", 0, 100, out _, StringPropertyMatchType.StartsWith); Assert.AreEqual(10, found.Count()); } @@ -517,13 +511,13 @@ namespace Umbraco.Tests.Services public void Find_By_Email_Ends_With() { var users = MockedUser.CreateMulipleUsers(10); - ServiceContext.UserService.Save(users); + UserService.Save(users); //include this var customUser = MockedUser.CreateUser(); customUser.Email = "hello@test.com"; - ServiceContext.UserService.Save(customUser); + UserService.Save(customUser); - var found = ServiceContext.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()); } @@ -532,13 +526,13 @@ namespace Umbraco.Tests.Services public void Find_By_Email_Contains() { var users = MockedUser.CreateMulipleUsers(10); - ServiceContext.UserService.Save(users); + UserService.Save(users); //include this var customUser = MockedUser.CreateUser(); customUser.Email = "hello@test.com"; - ServiceContext.UserService.Save(customUser); + UserService.Save(customUser); - var found = ServiceContext.UserService.FindByEmail("test", 0, 100, out _, StringPropertyMatchType.Contains); + var found = UserService.FindByEmail("test", 0, 100, out _, StringPropertyMatchType.Contains); Assert.AreEqual(11, found.Count()); } @@ -547,13 +541,13 @@ namespace Umbraco.Tests.Services public void Find_By_Email_Exact() { var users = MockedUser.CreateMulipleUsers(10); - ServiceContext.UserService.Save(users); + UserService.Save(users); //include this var customUser = MockedUser.CreateUser(); customUser.Email = "hello@test.com"; - ServiceContext.UserService.Save(customUser); + UserService.Save(customUser); - var found = ServiceContext.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()); } @@ -562,9 +556,9 @@ namespace Umbraco.Tests.Services public void Get_All_Paged_Users() { var users = MockedUser.CreateMulipleUsers(10); - ServiceContext.UserService.Save(users); + UserService.Save(users); - var found = ServiceContext.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 @@ -577,9 +571,9 @@ namespace Umbraco.Tests.Services public void Get_All_Paged_Users_With_Filter() { var users = MockedUser.CreateMulipleUsers(10).ToArray(); - ServiceContext.UserService.Save(users); + UserService.Save(users); - var found = ServiceContext.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); @@ -591,7 +585,7 @@ namespace Umbraco.Tests.Services public void Get_All_Paged_Users_For_Group() { var userGroup = MockedUserGroup.CreateUserGroup(); - ServiceContext.UserService.Save(userGroup); + UserService.Save(userGroup); var users = MockedUser.CreateMulipleUsers(10).ToArray(); for (var i = 0; i < 10;) @@ -599,10 +593,10 @@ namespace Umbraco.Tests.Services users[i].AddGroup(userGroup.ToReadOnlyGroup()); i = i + 2; } - ServiceContext.UserService.Save(users); + UserService.Save(users); long totalRecs; - var found = ServiceContext.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); @@ -614,7 +608,7 @@ namespace Umbraco.Tests.Services public void Get_All_Paged_Users_For_Group_With_Filter() { var userGroup = MockedUserGroup.CreateUserGroup(); - ServiceContext.UserService.Save(userGroup); + UserService.Save(userGroup); var users = MockedUser.CreateMulipleUsers(10).ToArray(); for (var i = 0; i < 10;) @@ -627,10 +621,10 @@ namespace Umbraco.Tests.Services users[i].Name = "blah" + users[i].Name; i = i + 3; } - ServiceContext.UserService.Save(users); + UserService.Save(users); long totalRecs; - var found = ServiceContext.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); @@ -642,11 +636,11 @@ namespace Umbraco.Tests.Services public void Count_All_Users() { var users = MockedUser.CreateMulipleUsers(10); - ServiceContext.UserService.Save(users); + UserService.Save(users); var customUser = MockedUser.CreateUser(); - ServiceContext.UserService.Save(customUser); + UserService.Save(customUser); - var found = ServiceContext.UserService.GetCount(MemberCountType.All); + var found = UserService.GetCount(MemberCountType.All); // + 1 because of the built in admin user Assert.AreEqual(12, found); @@ -657,7 +651,7 @@ namespace Umbraco.Tests.Services public void Count_All_Online_Users() { var users = MockedUser.CreateMulipleUsers(10, (i, member) => member.LastLoginDate = DateTime.Now.AddMinutes(i * -2)); - ServiceContext.UserService.Save(users); + UserService.Save(users); var customUser = MockedUser.CreateUser(); throw new NotImplementedException(); @@ -667,13 +661,13 @@ namespace Umbraco.Tests.Services public void Count_All_Locked_Users() { var users = MockedUser.CreateMulipleUsers(10, (i, member) => member.IsLockedOut = i % 2 == 0); - ServiceContext.UserService.Save(users); + UserService.Save(users); var customUser = MockedUser.CreateUser(); customUser.IsLockedOut = true; - ServiceContext.UserService.Save(customUser); + UserService.Save(customUser); - var found = ServiceContext.UserService.GetCount(MemberCountType.LockedOut); + var found = UserService.GetCount(MemberCountType.LockedOut); Assert.AreEqual(6, found); } @@ -682,13 +676,13 @@ namespace Umbraco.Tests.Services public void Count_All_Approved_Users() { var users = MockedUser.CreateMulipleUsers(10, (i, member) => member.IsApproved = i % 2 == 0); - ServiceContext.UserService.Save(users); + UserService.Save(users); var customUser = MockedUser.CreateUser(); customUser.IsApproved = false; - ServiceContext.UserService.Save(customUser); + UserService.Save(customUser); - var found = ServiceContext.UserService.GetCount(MemberCountType.Approved); + var found = UserService.GetCount(MemberCountType.Approved); // + 1 because of the built in admin user Assert.AreEqual(6, found); @@ -697,11 +691,8 @@ namespace Umbraco.Tests.Services [Test] public void Can_Persist_New_User() { - // Arrange - var userService = ServiceContext.UserService; - // 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,9 +704,6 @@ namespace Umbraco.Tests.Services [Test] public void Can_Persist_New_User_With_Hashed_Password() { - // Arrange - var userService = ServiceContext.UserService; - // Act // NOTE: Normally the hash'ing would be handled in the membership provider, so the service just saves the password var password = "123456"; @@ -724,7 +712,7 @@ namespace Umbraco.Tests.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); @@ -744,9 +732,9 @@ namespace Umbraco.Tests.Services }; userGroup.AddAllowedSection("content"); userGroup.AddAllowedSection("mediat"); - ServiceContext.UserService.Save(userGroup); + UserService.Save(userGroup); - var result1 = ServiceContext.UserService.GetUserGroupById(userGroup.Id); + var result1 = UserService.GetUserGroupById(userGroup.Id); Assert.AreEqual(2, result1.AllowedSections.Count()); @@ -755,9 +743,9 @@ namespace Umbraco.Tests.Services userGroup.AddAllowedSection("test2"); userGroup.AddAllowedSection("test3"); userGroup.AddAllowedSection("test4"); - ServiceContext.UserService.Save(userGroup); + UserService.Save(userGroup); - result1 = ServiceContext.UserService.GetUserGroupById(userGroup.Id); + result1 = UserService.GetUserGroupById(userGroup.Id); Assert.AreEqual(6, result1.AllowedSections.Count()); @@ -770,11 +758,11 @@ namespace Umbraco.Tests.Services //now just re-add a couple result1.AddAllowedSection("test3"); result1.AddAllowedSection("test4"); - ServiceContext.UserService.Save(result1); + UserService.Save(result1); - //assert + // Assert //re-get - result1 = ServiceContext.UserService.GetUserGroupById(userGroup.Id); + result1 = UserService.GetUserGroupById(userGroup.Id); Assert.AreEqual(2, result1.AllowedSections.Count()); } @@ -791,21 +779,21 @@ namespace Umbraco.Tests.Services Alias = "Group2", Name = "Group 2" }; - ServiceContext.UserService.Save(userGroup1); - ServiceContext.UserService.Save(userGroup2); + UserService.Save(userGroup1); + UserService.Save(userGroup2); //adds some allowed sections userGroup1.AddAllowedSection("test"); userGroup2.AddAllowedSection("test"); - ServiceContext.UserService.Save(userGroup1); - ServiceContext.UserService.Save(userGroup2); + UserService.Save(userGroup1); + UserService.Save(userGroup2); //now clear the section from all users - ServiceContext.UserService.DeleteSectionFromAllUserGroups("test"); + UserService.DeleteSectionFromAllUserGroups("test"); - //assert - var result1 = ServiceContext.UserService.GetUserGroupById(userGroup1.Id); - var result2 = ServiceContext.UserService.GetUserGroupById(userGroup2.Id); + // Assert + var result1 = UserService.GetUserGroupById(userGroup1.Id); + var result2 = UserService.GetUserGroupById(userGroup2.Id); Assert.IsFalse(result1.AllowedSections.Contains("test")); Assert.IsFalse(result2.AllowedSections.Contains("test")); } @@ -832,14 +820,14 @@ namespace Umbraco.Tests.Services Alias = "Group3", Name = "Group 3" }; - ServiceContext.UserService.Save(userGroup1); - ServiceContext.UserService.Save(userGroup2); - ServiceContext.UserService.Save(userGroup3); + UserService.Save(userGroup1); + UserService.Save(userGroup2); + UserService.Save(userGroup3); - //assert - var result1 = ServiceContext.UserService.GetUserGroupById(userGroup1.Id); - var result2 = ServiceContext.UserService.GetUserGroupById(userGroup2.Id); - var result3 = ServiceContext.UserService.GetUserGroupById(userGroup3.Id); + // Assert + 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")); @@ -848,13 +836,13 @@ namespace Umbraco.Tests.Services foreach (var userGroup in new[] { userGroup1, userGroup2, userGroup3 }) { userGroup.AddAllowedSection("test"); - ServiceContext.UserService.Save(userGroup); + UserService.Save(userGroup); } - //assert - result1 = ServiceContext.UserService.GetUserGroupById(userGroup1.Id); - result2 = ServiceContext.UserService.GetUserGroupById(userGroup2.Id); - result3 = ServiceContext.UserService.GetUserGroupById(userGroup3.Id); + // Assert + 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")); @@ -863,46 +851,40 @@ namespace Umbraco.Tests.Services [Test] public void Cannot_Create_User_With_Empty_Username() { - // Arrange - var userService = ServiceContext.UserService; - // 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 userService = ServiceContext.UserService; - 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 userService = ServiceContext.UserService; - 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 = ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com"); + var user = UserService.CreateUserWithIdentity("test1", "test1@test.com"); // Act - - var profile = ServiceContext.UserService.GetProfileByUserName(user.Username); + var profile = UserService.GetProfileByUserName(user.Username); // Assert Assert.IsNotNull(profile); @@ -914,11 +896,10 @@ namespace Umbraco.Tests.Services public void Get_By_Profile_Id() { // Arrange - var user = (IUser)ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com"); + var user = (IUser)UserService.CreateUserWithIdentity("test1", "test1@test.com"); // Act - - var profile = ServiceContext.UserService.GetProfileById((int)user.Id); + var profile = UserService.GetProfileById((int)user.Id); // Assert Assert.IsNotNull(profile); @@ -929,7 +910,7 @@ namespace Umbraco.Tests.Services [Test] public void Get_By_Profile_Id_Must_return_null_if_user_not_exists() { - var profile = ServiceContext.UserService.GetProfileById(42); + var profile = UserService.GetProfileById(42); // Assert Assert.IsNull(profile); @@ -938,7 +919,7 @@ namespace Umbraco.Tests.Services [Test] public void GetProfilesById_Must_empty_if_users_not_exists() { - var profiles = ServiceContext.UserService.GetProfilesById(42); + var profiles = UserService.GetProfilesById(42); // Assert CollectionAssert.IsEmpty(profiles); @@ -948,12 +929,11 @@ namespace Umbraco.Tests.Services public void Get_User_By_Username() { // Arrange - IUserGroup userGroup; - var originalUser = CreateTestUser(out userGroup); + var originalUser = CreateTestUser(out _); // Act - var updatedItem = (User) ServiceContext.UserService.GetByUsername(originalUser.Username); + var updatedItem = (User)UserService.GetByUsername(originalUser.Username); // Assert Assert.IsNotNull(updatedItem); @@ -981,7 +961,7 @@ namespace Umbraco.Tests.Services CreateTestUsers(startContentItems.Select(x => x.Id).ToArray(), testUserGroup, 3); - var usersInGroup = ServiceContext.UserService.GetAllInGroup(userGroupId); + var usersInGroup = UserService.GetAllInGroup(userGroupId); foreach (var user in usersInGroup) Assert.AreEqual(user.StartContentIds.Length, startContentItems.Length); @@ -991,14 +971,14 @@ namespace Umbraco.Tests.Services { var contentType = MockedContentTypes.CreateSimpleContentType(); - ServiceContext.ContentTypeService.Save(contentType); + ContentTypeService.Save(contentType); var startContentItems = new List(); for (var i = 0; i < numberToCreate; i++) startContentItems.Add(MockedContent.CreateSimpleContent(contentType)); - ServiceContext.ContentService.Save(startContentItems); + ContentService.Save(startContentItems); return startContentItems.ToArray(); } @@ -1007,11 +987,11 @@ namespace Umbraco.Tests.Services { userGroup = CreateTestUserGroup(); - var user = ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com"); + var user = UserService.CreateUserWithIdentity("test1", "test1@test.com"); user.AddGroup(userGroup.ToReadOnlyGroup()); - ServiceContext.UserService.Save(user); + UserService.Save(user); return user; } @@ -1022,13 +1002,13 @@ namespace Umbraco.Tests.Services for (var i = 0; i < numberToCreate; i++) { - var user = ServiceContext.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; - ServiceContext.UserService.Save(user); + UserService.Save(user); users.Add(user); } @@ -1048,7 +1028,7 @@ namespace Umbraco.Tests.Services userGroup.AddAllowedSection("content"); userGroup.AddAllowedSection("media"); - ServiceContext.UserService.Save(userGroup); + UserService.Save(userGroup); return userGroup; } diff --git a/src/Umbraco.Tests.Integration/TestServerTest/UmbracoTestServerTestBase.cs b/src/Umbraco.Tests.Integration/TestServerTest/UmbracoTestServerTestBase.cs index 9f10ce968e..2aacd7b8fb 100644 --- a/src/Umbraco.Tests.Integration/TestServerTest/UmbracoTestServerTestBase.cs +++ b/src/Umbraco.Tests.Integration/TestServerTest/UmbracoTestServerTestBase.cs @@ -31,7 +31,7 @@ namespace Umbraco.Tests.Integration.TestServerTest public abstract class UmbracoTestServerTestBase : UmbracoIntegrationTest { [SetUp] - public override Task Setup() + public override void Setup() { InMemoryConfiguration["ConnectionStrings:" + Constants.System.UmbracoConnectionName] = null; InMemoryConfiguration["Umbraco:CMS:Hosting:Debug"] = "true"; @@ -55,8 +55,6 @@ namespace Umbraco.Tests.Integration.TestServerTest }); LinkGenerator = Factory.Services.GetRequiredService(); - - return Task.CompletedTask; } public override IHostBuilder CreateHostBuilder() diff --git a/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs b/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs index 1dae786d01..4448308a2e 100644 --- a/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs +++ b/src/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs @@ -46,6 +46,7 @@ namespace Umbraco.Tests.Integration.Testing [NonParallelizable] public abstract class UmbracoIntegrationTest { + public static LightInjectContainer CreateUmbracoContainer(out UmbracoServiceProviderFactory serviceProviderFactory) { var container = UmbracoServiceProviderFactory.CreateServiceContainer(); @@ -80,10 +81,10 @@ namespace Umbraco.Tests.Integration.Testing } [SetUp] - public virtual async Task Setup() + public virtual void Setup() { var hostBuilder = CreateHostBuilder(); - var host = await hostBuilder.StartAsync(); + var host = hostBuilder.StartAsync().GetAwaiter().GetResult(); Services = host.Services; var app = new ApplicationBuilder(host.Services); Configure(app); diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 2395542d62..197e928e1f 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -230,7 +230,6 @@ - @@ -268,17 +267,13 @@ - - - - diff --git a/src/Umbraco.Web.Common/Security/BackofficeSecurity.cs b/src/Umbraco.Web.Common/Security/BackofficeSecurity.cs index 6587501be7..c16071b68a 100644 --- a/src/Umbraco.Web.Common/Security/BackofficeSecurity.cs +++ b/src/Umbraco.Web.Common/Security/BackofficeSecurity.cs @@ -66,7 +66,7 @@ namespace Umbraco.Web.Common.Security /// public Attempt GetUserId() { - var identity = _httpContextAccessor.GetRequiredHttpContext().GetCurrentIdentity(); + var identity = _httpContextAccessor.HttpContext?.GetCurrentIdentity(); return identity == null ? Attempt.Fail() : Attempt.Succeed(identity.Id); }