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 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.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/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..5afc045231 100644 --- a/src/Umbraco.Tests/Services/ContentServiceEventTests.cs +++ b/src/Umbraco.Tests.Integration/Services/ContentServiceEventTests.cs @@ -7,24 +7,29 @@ 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; -namespace Umbraco.Tests.Services +namespace Umbraco.Tests.Integration.Services { [TestFixture] [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, PublishedRepositoryEvents = true, WithApplication = true, Logger = UmbracoTestOptions.Logger.Console)] - public class ContentServiceEventTests : TestWithSomeContentBase + public class ContentServiceEventTests : UmbracoIntegrationTest { + 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(); } @@ -38,28 +43,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 +87,7 @@ namespace Umbraco.Tests.Services ContentService.Saved += OnSaved; try { - contentService.Save(document); + ContentService.Save(document); } finally { @@ -100,13 +99,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 +131,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 +187,7 @@ namespace Umbraco.Tests.Services ContentService.Published += OnPublished; try { - contentService.SaveAndPublish(document, "fr-FR"); + ContentService.SaveAndPublish(document, "fr-FR"); } finally { @@ -207,7 +195,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 +205,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 +226,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 +252,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 +277,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 +295,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/MacroServiceTests.cs b/src/Umbraco.Tests.Integration/Services/MacroServiceTests.cs index 0c38f77a9e..75eaa85467 100644 --- a/src/Umbraco.Tests.Integration/Services/MacroServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/MacroServiceTests.cs @@ -9,10 +9,12 @@ using Umbraco.Core.Models; using Umbraco.Core.Persistence.Repositories.Implement; using Umbraco.Core.Scoping; using Umbraco.Core.Services; +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)] @@ -20,19 +22,13 @@ namespace Umbraco.Tests.Services public class MacroServiceTests : UmbracoIntegrationTest { private IMacroService MacroService => GetRequiredService(); - [SetUp] - public void SetupTestData() + public void SetupTest() { - CreateTestData(); - } - - public void CreateTestData() - { - var provider = ScopeProvider; - using (var scope = provider.CreateScope()) + 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")); @@ -41,19 +37,13 @@ namespace Umbraco.Tests.Services } } - [TearDown] - public override void TearDown() - { - base.TearDown(); - } - [Test] public void Can_Get_By_Alias() { // Act var macro = MacroService.GetByAlias("test1"); - //assert + // Assert Assert.IsNotNull(macro); Assert.AreEqual("Test1", macro.Name); } @@ -64,7 +54,7 @@ namespace Umbraco.Tests.Services // Act var result = MacroService.GetAll(); - //assert + // Assert Assert.AreEqual(3, result.Count()); } @@ -72,10 +62,12 @@ namespace Umbraco.Tests.Services public void Can_Create() { // Act - var macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); + var macro = CreateMacro(); MacroService.Save(macro); - //assert + MacroService.Save(macro); + + // Assert Assert.IsTrue(macro.HasIdentity); Assert.Greater(macro.Id, 0); Assert.AreNotEqual(Guid.Empty, macro.Key); @@ -95,13 +87,14 @@ namespace Umbraco.Tests.Services [Test] public void Can_Delete() { + // Arrange var macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); MacroService.Save(macro); // Act MacroService.Delete(macro); - //assert + // Assert var result = MacroService.GetById(macro.Id); Assert.IsNull(result); @@ -112,7 +105,8 @@ namespace Umbraco.Tests.Services [Test] public void Can_Update() { - IMacro macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); + // Arrange + var macro = CreateMacro(); MacroService.Save(macro); // Act @@ -121,10 +115,9 @@ namespace Umbraco.Tests.Services macro.Alias = "NewAlias"; MacroService.Save(macro); - macro = MacroService.GetById(macro.Id); - //assert + // Assert Assert.AreEqual("New name", macro.Name); Assert.AreEqual("NewAlias", macro.Alias); Assert.AreEqual(currKey, macro.Key); @@ -134,7 +127,8 @@ namespace Umbraco.Tests.Services [Test] public void Can_Update_Property() { - IMacro macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); + // Arrange + var macro = CreateMacro(); macro.Properties.Add(new MacroProperty("blah", "Blah", 0, "blah")); MacroService.Save(macro); @@ -150,7 +144,7 @@ namespace Umbraco.Tests.Services 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); @@ -163,7 +157,8 @@ namespace Umbraco.Tests.Services [Test] public void Can_Update_Remove_Property() { - IMacro macro = new Macro(ShortStringHelper, "test", "Test", "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); + // Arrange + 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")); @@ -190,7 +185,7 @@ namespace Umbraco.Tests.Services 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); @@ -200,15 +195,14 @@ namespace Umbraco.Tests.Services { Assert.AreEqual(propKey.Key, macro.Properties[propKey.Alias].Key); } - } [Test] public void Can_Add_And_Remove_Properties() { - 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")); @@ -218,19 +212,18 @@ namespace Umbraco.Tests.Services 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); - //assert - - //re-get + // Assert result1 = MacroService.GetById(result1.Id); Assert.AreEqual(2, result1.Properties.Values.Count()); @@ -239,21 +232,21 @@ namespace Umbraco.Tests.Services [Test] public void Cannot_Save_Macro_With_Empty_Name() { - var macro = new Macro(ShortStringHelper, "test", string.Empty, "~/Views/MacroPartials/Test.cshtml", cacheDuration: 1234); + // Arrange + var macro = CreateMacro(name: string.Empty); // Act & Assert Assert.Throws(() => MacroService.Save(macro)); } - //[Test] - //public void Can_Get_Many_By_Alias() - //{ - // // 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/Services/SectionServiceTests.cs b/src/Umbraco.Tests.Integration/Services/SectionServiceTests.cs index 533a66ad96..f34ea32cff 100644 --- a/src/Umbraco.Tests.Integration/Services/SectionServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/SectionServiceTests.cs @@ -1,17 +1,14 @@ -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; -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..585c1319a4 100644 --- a/src/Umbraco.Tests.Integration/Services/TagServiceTests.cs +++ b/src/Umbraco.Tests.Integration/Services/TagServiceTests.cs @@ -26,7 +26,7 @@ namespace Umbraco.Tests.Integration.Services private IContentTypeService ContentTypeService => GetRequiredService(); private ITagService TagService => GetRequiredService(); private IDataTypeService DataTypeService => GetRequiredService(); - public PropertyEditorCollection PropertyEditorCollection => GetRequiredService(); + private PropertyEditorCollection PropertyEditorCollection => GetRequiredService(); [Test] public void TagApiConsistencyTest() diff --git a/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs b/src/Umbraco.Tests.Integration/Services/UserServiceTests.cs index 64bd89c8f8..fbe51da837 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 { @@ -495,10 +496,10 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Find_By_Email_Starts_With() { - var users = MockedUser.CreateMulipleUsers(10); + 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); @@ -510,10 +511,10 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Find_By_Email_Ends_With() { - var users = MockedUser.CreateMulipleUsers(10); + var users = CreateMulipleUsers(10); UserService.Save(users); //include this - var customUser = MockedUser.CreateUser(); + var customUser = CreateUser(); customUser.Email = "hello@test.com"; UserService.Save(customUser); @@ -525,10 +526,10 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Find_By_Email_Contains() { - var users = MockedUser.CreateMulipleUsers(10); + var users = CreateMulipleUsers(10); UserService.Save(users); //include this - var customUser = MockedUser.CreateUser(); + var customUser = CreateUser(); customUser.Email = "hello@test.com"; UserService.Save(customUser); @@ -540,10 +541,10 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Find_By_Email_Exact() { - var users = MockedUser.CreateMulipleUsers(10); + var users = CreateMulipleUsers(10); UserService.Save(users); //include this - var customUser = MockedUser.CreateUser(); + var customUser = CreateUser(); customUser.Email = "hello@test.com"; UserService.Save(customUser); @@ -555,7 +556,7 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Get_All_Paged_Users() { - var users = MockedUser.CreateMulipleUsers(10); + var users = CreateMulipleUsers(10); UserService.Save(users); var found = UserService.GetAll(0, 2, out var totalRecs); @@ -570,7 +571,7 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Get_All_Paged_Users_With_Filter() { - var users = MockedUser.CreateMulipleUsers(10).ToArray(); + var users = CreateMulipleUsers(10).ToArray(); UserService.Save(users); var found = UserService.GetAll(0, 2, out var totalRecs, "username", Direction.Ascending, filter: "test"); @@ -587,7 +588,7 @@ namespace Umbraco.Tests.Integration.Services var userGroup = MockedUserGroup.CreateUserGroup(); 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()); @@ -610,7 +611,7 @@ namespace Umbraco.Tests.Integration.Services var userGroup = MockedUserGroup.CreateUserGroup(); 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()); @@ -635,9 +636,9 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Count_All_Users() { - var users = MockedUser.CreateMulipleUsers(10); + var users = CreateMulipleUsers(10); UserService.Save(users); - var customUser = MockedUser.CreateUser(); + var customUser = CreateUser(); UserService.Save(customUser); var found = UserService.GetCount(MemberCountType.All); @@ -650,20 +651,20 @@ 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)); + 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); + 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); @@ -675,10 +676,10 @@ namespace Umbraco.Tests.Integration.Services [Test] public void Count_All_Approved_Users() { - var users = MockedUser.CreateMulipleUsers(10, (i, member) => member.IsApproved = i % 2 == 0); + 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); @@ -896,7 +897,7 @@ 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); @@ -908,7 +909,7 @@ 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); @@ -917,7 +918,7 @@ namespace Umbraco.Tests.Integration.Services } [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); @@ -983,6 +984,39 @@ namespace Umbraco.Tests.Integration.Services 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(); @@ -1018,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.Infrastructure/Editors/UserEditorAuthorizationHelperTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Editors/UserEditorAuthorizationHelperTests.cs index 187e3cccd2..2fd0c7b8ea 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,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.TestHelpers.Entities; +using Umbraco.Tests.Common.Builders; +using Umbraco.Tests.Common.Builders.Extensions; using Umbraco.Web.Editors; namespace Umbraco.Tests.Web.Controllers @@ -22,8 +19,8 @@ namespace Umbraco.Tests.Web.Controllers [Test] public void Admin_Is_Authorized() { - var currentUser = GetAdminUser(); - var savingUser = MockedUser.GetUserMock(); + var currentUser = CreateAdminUser(); + var savingUser = CreateUser(); var contentService = new Mock(); var mediaService = new Mock(); @@ -36,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); } @@ -44,8 +41,8 @@ namespace Umbraco.Tests.Web.Controllers [Test] public void Non_Admin_Cannot_Save_Admin() { - var currentUser = MockedUser.GetUserMock(); - var savingUser = GetAdminUser(); + var currentUser = CreateUser(); + var savingUser = CreateAdminUser(); var contentService = new Mock(); var mediaService = new Mock(); @@ -58,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); } @@ -66,12 +63,8 @@ namespace Umbraco.Tests.Web.Controllers [Test] public void Cannot_Grant_Group_Membership_Without_Being_A_Member() { - var currentUser = MockedUser.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 currentUser = CreateUser(withGroup: true); + var savingUser = CreateUser(); var contentService = new Mock(); var mediaService = new Mock(); @@ -84,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); } @@ -92,12 +85,8 @@ namespace Umbraco.Tests.Web.Controllers [Test] public void Can_Grant_Group_Membership_With_Being_A_Member() { - var currentUser = MockedUser.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 currentUser = CreateUser(withGroup: true); + var savingUser = CreateUser(); var contentService = new Mock(); var mediaService = new Mock(); @@ -110,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); } @@ -126,10 +115,8 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = MockedUser.GetUserMock(); - currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.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())) @@ -150,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); } @@ -166,10 +153,8 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = MockedUser.GetUserMock(); - currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.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())) @@ -190,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); } @@ -206,9 +191,8 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = MockedUser.GetUserMock(); - currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.GetUserMock(); + var currentUser = CreateUser(startContentIds: new[] { 9876 }); + var savingUser = CreateUser(); var contentService = new Mock(); contentService.Setup(x => x.GetById(It.IsAny())) @@ -229,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); } @@ -245,9 +229,8 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = MockedUser.GetUserMock(); - currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.GetUserMock(); + var currentUser = CreateUser(startContentIds: new[] { 9876 }); + var savingUser = CreateUser(); var contentService = new Mock(); contentService.Setup(x => x.GetById(It.IsAny())) @@ -268,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); } @@ -285,9 +268,8 @@ namespace Umbraco.Tests.Web.Controllers }; - var currentUser = MockedUser.GetUserMock(); - currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.GetUserMock(); + var currentUser = CreateUser(startMediaIds: new[] { 9876 }); + var savingUser = CreateUser(); var contentService = new Mock(); var mediaService = new Mock(); @@ -308,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); } @@ -324,9 +306,8 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = MockedUser.GetUserMock(); - currentUser.Setup(x => x.StartMediaIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.GetUserMock(); + var currentUser = CreateUser(startMediaIds: new[] { 9876 }); + var savingUser = CreateUser(); var contentService = new Mock(); var mediaService = new Mock(); @@ -347,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); } @@ -363,10 +344,8 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = MockedUser.GetUserMock(); - currentUser.Setup(x => x.StartMediaIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.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(); @@ -387,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); } @@ -403,10 +382,8 @@ namespace Umbraco.Tests.Web.Controllers {4567, "-1,4567"}, }; - var currentUser = MockedUser.GetUserMock(); - currentUser.Setup(x => x.StartMediaIds).Returns(new[] { 9876 }); - var savingUser = MockedUser.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(); @@ -427,19 +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); } - private IUser GetAdminUser() + private static IUser CreateUser(bool withGroup = false, int[] startContentIds = null, int[] startMediaIds = null) { - var admin = MockedUser.GetUserMock(); - admin.Setup(x => x.Groups).Returns(new[] + var builder = new UserBuilder() + .WithStartContentIds(startContentIds != null ? startContentIds : new int[0]) + .WithStartMediaIds(startMediaIds != null ? startMediaIds : new int[0]); + if (withGroup) { - new ReadOnlyUserGroup(1, "Admin", "icon-user", null, null, Constants.Security.AdminGroupAlias, new string[0], new string[0]) - }); - return admin.Object; + builder = (UserBuilder)builder + .AddUserGroup() + .WithName("Test") + .WithAlias("test") + .Done(); + } + + return builder.Build(); + } + + private static IUser CreateAdminUser() + { + return new UserBuilder() + .AddUserGroup() + .WithId(1) + .WithName("Admin") + .WithAlias(Constants.Security.AdminGroupAlias) + .Done() + .Build(); } } } diff --git a/src/Umbraco.Tests/Services/AmbiguousEventTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Services/AmbiguousEventTests.cs similarity index 97% rename from src/Umbraco.Tests/Services/AmbiguousEventTests.cs rename to src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Services/AmbiguousEventTests.cs index e137da1188..6eebf5d237 100644 --- a/src/Umbraco.Tests/Services/AmbiguousEventTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Services/AmbiguousEventTests.cs @@ -5,7 +5,7 @@ using NUnit.Framework; using Umbraco.Core.Events; using Umbraco.Core.Services.Implement; -namespace Umbraco.Tests.Services +namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Services { [TestFixture] public class AmbiguousEventTests @@ -75,4 +75,4 @@ namespace Umbraco.Tests.Services } } } -} \ No newline at end of file +} 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..2f76db452a 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 = null, bool withUserGroup = true) + { + var builder = new UserBuilder() + .WithId(id) + .WithStartContentIds(startContentId.HasValue ? new[] { startContentId.Value } : new int[0]); + 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..91305e463c 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 = null) + { + return new UserBuilder() + .WithId(id) + .WithStartMediaIds(startMediaId.HasValue ? new[] { startMediaId.Value } : new int[0]) + .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..5c186c890c 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,14 @@ namespace Umbraco.Tests.Web.Controllers Assert.AreEqual(3, list.ElementAt(2).Id); } + private IUser CreateUser(int id = 0, int? startContentId = null) + { + return new UserBuilder() + .WithId(id) + .WithStartContentIds(startContentId.HasValue ? new[] { startContentId.Value } : new int[0]) + .Build(); + } + private class MyTestClass { public IEnumerable MyList { get; set; } diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 621737a14f..7f5a93cede 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -140,6 +140,8 @@ + + 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;