Aligned service integration tests with naming convention for private variables and use of test model builders.

This commit is contained in:
Andy Butland
2020-10-06 09:23:15 +02:00
parent 30e49ac548
commit 1f83c61043
12 changed files with 497 additions and 439 deletions

View File

@@ -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
{
/// <summary>
/// Returns a <see cref="Mock{IUser}"/> and ensures that the ToUserCache and FromUserCache methods are mapped correctly for
/// dealing with start node caches
/// </summary>
/// <returns></returns>
public static Mock<IUser> GetUserMock()
{
var userCache = new Dictionary<string, object>();
var userMock = new Mock<IUser>();
userMock.Setup(x => x.FromUserCache<int[]>(It.IsAny<string>())).Returns((string key) => userCache.TryGetValue(key, out var val) ? val is int[] iVal ? iVal : null : null);
userMock.Setup(x => x.ToUserCache<int[]>(It.IsAny<string>(), It.IsAny<int[]>())).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<IUser> CreateMulipleUsers(int amount, Action<int, IUser> onCreating = null)
{
var list = new List<IUser>();
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;
}
}
}

View File

@@ -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)]

View File

@@ -1,5 +1,4 @@
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Configuration.Models;
@@ -12,7 +11,7 @@ using Umbraco.Tests.Integration.Testing;
using Umbraco.Tests.TestHelpers.Entities;
using Umbraco.Tests.Testing;
namespace Umbraco.Tests.Services
namespace Umbraco.Tests.Integration.Services
{
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest,

View File

@@ -31,7 +31,7 @@ namespace Umbraco.Tests.Integration.Services
private int _englishLangId;
private GlobalSettings _globalSettings;
private ILocalizationService LocalizationService => GetRequiredService<ILocalizationService>();
private ILocalizationService _localizationService => GetRequiredService<ILocalizationService>();
[SetUp]
public void SetUp()
@@ -43,7 +43,7 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Can_Get_Root_Dictionary_Items()
{
var rootItems = LocalizationService.GetRootDictionaryItems();
var rootItems = _localizationService.GetRootDictionaryItems();
Assert.NotNull(rootItems);
Assert.IsTrue(rootItems.Any());
@@ -52,14 +52,14 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Can_Determint_If_DictionaryItem_Exists()
{
var exists = LocalizationService.DictionaryItemExists("Parent");
var exists = _localizationService.DictionaryItemExists("Parent");
Assert.IsTrue(exists);
}
[Test]
public void Can_Get_All_Languages()
{
var languages = LocalizationService.GetAllLanguages();
var languages = _localizationService.GetAllLanguages();
Assert.NotNull(languages);
Assert.IsTrue(languages.Any());
Assert.That(languages.Count(), Is.EqualTo(3));
@@ -68,37 +68,37 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Can_Get_Dictionary_Item_By_Int_Id()
{
var parentItem = LocalizationService.GetDictionaryItemById(_parentItemIntId);
var parentItem = _localizationService.GetDictionaryItemById(_parentItemIntId);
Assert.NotNull(parentItem);
var childItem = LocalizationService.GetDictionaryItemById(_childItemIntId);
var childItem = _localizationService.GetDictionaryItemById(_childItemIntId);
Assert.NotNull(childItem);
}
[Test]
public void Can_Get_Dictionary_Item_By_Guid_Id()
{
var parentItem = LocalizationService.GetDictionaryItemById(_parentItemGuidId);
var parentItem = _localizationService.GetDictionaryItemById(_parentItemGuidId);
Assert.NotNull(parentItem);
var childItem = LocalizationService.GetDictionaryItemById(_childItemGuidId);
var childItem = _localizationService.GetDictionaryItemById(_childItemGuidId);
Assert.NotNull(childItem);
}
[Test]
public void Can_Get_Dictionary_Item_By_Key()
{
var parentItem = LocalizationService.GetDictionaryItemByKey("Parent");
var parentItem = _localizationService.GetDictionaryItemByKey("Parent");
Assert.NotNull(parentItem);
var childItem = LocalizationService.GetDictionaryItemByKey("Child");
var childItem = _localizationService.GetDictionaryItemByKey("Child");
Assert.NotNull(childItem);
}
[Test]
public void Can_Get_Dictionary_Item_Children()
{
var item = LocalizationService.GetDictionaryItemChildren(_parentItemGuidId);
var item = _localizationService.GetDictionaryItemChildren(_parentItemGuidId);
Assert.NotNull(item);
Assert.That(item.Count(), Is.EqualTo(1));
@@ -114,8 +114,8 @@ namespace Umbraco.Tests.Integration.Services
{
using (var scope = ScopeProvider.CreateScope())
{
var en = LocalizationService.GetLanguageById(_englishLangId);
var dk = LocalizationService.GetLanguageById(_danishLangId);
var en = _localizationService.GetLanguageById(_englishLangId);
var dk = _localizationService.GetLanguageById(_danishLangId);
var currParentId = _childItemGuidId;
for (var i = 0; i < 25; i++)
@@ -137,8 +137,8 @@ namespace Umbraco.Tests.Integration.Services
new DictionaryTranslation(dk, "BørnVærdi2 " + i)
}
};
LocalizationService.Save(desc1);
LocalizationService.Save(desc2);
_localizationService.Save(desc1);
_localizationService.Save(desc2);
currParentId = desc1.Key;
}
@@ -146,7 +146,7 @@ namespace Umbraco.Tests.Integration.Services
scope.Database.AsUmbracoDatabase().EnableSqlTrace = true;
scope.Database.AsUmbracoDatabase().EnableSqlCount = true;
var items = LocalizationService.GetDictionaryItemDescendants(_parentItemGuidId).ToArray();
var items = _localizationService.GetDictionaryItemDescendants(_parentItemGuidId).ToArray();
Debug.WriteLine("SQL CALLS: " + scope.Database.AsUmbracoDatabase().SqlCount);
@@ -159,8 +159,8 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Can_GetLanguageById()
{
var danish = LocalizationService.GetLanguageById(_danishLangId);
var english = LocalizationService.GetLanguageById(_englishLangId);
var danish = _localizationService.GetLanguageById(_danishLangId);
var english = _localizationService.GetLanguageById(_englishLangId);
Assert.NotNull(danish);
Assert.NotNull(english);
}
@@ -168,8 +168,8 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Can_GetLanguageByIsoCode()
{
var danish = LocalizationService.GetLanguageByIsoCode("da-DK");
var english = LocalizationService.GetLanguageByIsoCode("en-GB");
var danish = _localizationService.GetLanguageByIsoCode("da-DK");
var english = _localizationService.GetLanguageByIsoCode("en-GB");
Assert.NotNull(danish);
Assert.NotNull(english);
}
@@ -177,14 +177,14 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Does_Not_Fail_When_Language_Doesnt_Exist()
{
var language = LocalizationService.GetLanguageByIsoCode("sv-SE");
var language = _localizationService.GetLanguageByIsoCode("sv-SE");
Assert.Null(language);
}
[Test]
public void Does_Not_Fail_When_DictionaryItem_Doesnt_Exist()
{
var item = LocalizationService.GetDictionaryItemByKey("RandomKey");
var item = _localizationService.GetDictionaryItemByKey("RandomKey");
Assert.Null(item);
}
@@ -192,34 +192,34 @@ namespace Umbraco.Tests.Integration.Services
public void Can_Delete_Language()
{
var norwegian = new Language(_globalSettings, "nb-NO") { CultureName = "Norwegian" };
LocalizationService.Save(norwegian, 0);
_localizationService.Save(norwegian, 0);
Assert.That(norwegian.HasIdentity, Is.True);
var languageId = norwegian.Id;
LocalizationService.Delete(norwegian);
_localizationService.Delete(norwegian);
var language = LocalizationService.GetLanguageById(languageId);
var language = _localizationService.GetLanguageById(languageId);
Assert.Null(language);
}
[Test]
public void Can_Delete_Language_Used_As_Fallback()
{
var danish = LocalizationService.GetLanguageByIsoCode("da-DK");
var danish = _localizationService.GetLanguageByIsoCode("da-DK");
var norwegian = new Language(_globalSettings, "nb-NO") { CultureName = "Norwegian", FallbackLanguageId = danish.Id };
LocalizationService.Save(norwegian, 0);
_localizationService.Save(norwegian, 0);
var languageId = danish.Id;
LocalizationService.Delete(danish);
_localizationService.Delete(danish);
var language = LocalizationService.GetLanguageById(languageId);
var language = _localizationService.GetLanguageById(languageId);
Assert.Null(language);
}
[Test]
public void Can_Create_DictionaryItem_At_Root()
{
var english = LocalizationService.GetLanguageByIsoCode("en-US");
var english = _localizationService.GetLanguageByIsoCode("en-US");
var item = (IDictionaryItem)new DictionaryItem("Testing123")
{
@@ -228,10 +228,10 @@ namespace Umbraco.Tests.Integration.Services
new DictionaryTranslation(english, "Hello world")
}
};
LocalizationService.Save(item);
_localizationService.Save(item);
//re-get
item = LocalizationService.GetDictionaryItemById(item.Id);
item = _localizationService.GetDictionaryItemById(item.Id);
Assert.Greater(item.Id, 0);
Assert.IsTrue(item.HasIdentity);
@@ -243,18 +243,18 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Can_Create_DictionaryItem_At_Root_With_Identity()
{
var item = LocalizationService.CreateDictionaryItemWithIdentity(
var item = _localizationService.CreateDictionaryItemWithIdentity(
"Testing12345", null, "Hellooooo");
//re-get
item = LocalizationService.GetDictionaryItemById(item.Id);
item = _localizationService.GetDictionaryItemById(item.Id);
Assert.IsNotNull(item);
Assert.Greater(item.Id, 0);
Assert.IsTrue(item.HasIdentity);
Assert.IsFalse(item.ParentId.HasValue);
Assert.AreEqual("Testing12345", item.ItemKey);
var allLangs = LocalizationService.GetAllLanguages();
var allLangs = _localizationService.GetAllLanguages();
Assert.Greater(allLangs.Count(), 0);
foreach (var language in allLangs)
{
@@ -265,20 +265,20 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Can_Add_Translation_To_Existing_Dictionary_Item()
{
var english = LocalizationService.GetLanguageByIsoCode("en-US");
var english = _localizationService.GetLanguageByIsoCode("en-US");
var item = (IDictionaryItem) new DictionaryItem("Testing123");
LocalizationService.Save(item);
_localizationService.Save(item);
//re-get
item = LocalizationService.GetDictionaryItemById(item.Id);
item = _localizationService.GetDictionaryItemById(item.Id);
item.Translations = new List<IDictionaryTranslation>
{
new DictionaryTranslation(english, "Hello world")
};
LocalizationService.Save(item);
_localizationService.Save(item);
Assert.AreEqual(1, item.Translations.Count());
foreach (var translation in item.Translations)
@@ -289,14 +289,14 @@ namespace Umbraco.Tests.Integration.Services
item.Translations = new List<IDictionaryTranslation>(item.Translations)
{
new DictionaryTranslation(
LocalizationService.GetLanguageByIsoCode("en-GB"),
_localizationService.GetLanguageByIsoCode("en-GB"),
"My new value")
};
LocalizationService.Save(item);
_localizationService.Save(item);
//re-get
item = LocalizationService.GetDictionaryItemById(item.Id);
item = _localizationService.GetDictionaryItemById(item.Id);
Assert.AreEqual(2, item.Translations.Count());
Assert.AreEqual("Hello world", item.Translations.First().Value);
@@ -306,27 +306,27 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Can_Delete_DictionaryItem()
{
var item = LocalizationService.GetDictionaryItemByKey("Child");
var item = _localizationService.GetDictionaryItemByKey("Child");
Assert.NotNull(item);
LocalizationService.Delete(item);
_localizationService.Delete(item);
var deletedItem = LocalizationService.GetDictionaryItemByKey("Child");
var deletedItem = _localizationService.GetDictionaryItemByKey("Child");
Assert.Null(deletedItem);
}
[Test]
public void Can_Update_Existing_DictionaryItem()
{
var item = LocalizationService.GetDictionaryItemByKey("Child");
var item = _localizationService.GetDictionaryItemByKey("Child");
foreach (var translation in item.Translations)
{
translation.Value = translation.Value + "UPDATED";
}
LocalizationService.Save(item);
_localizationService.Save(item);
var updatedItem = LocalizationService.GetDictionaryItemByKey("Child");
var updatedItem = _localizationService.GetDictionaryItemByKey("Child");
Assert.NotNull(updatedItem);
foreach (var translation in updatedItem.Translations)
@@ -339,7 +339,7 @@ namespace Umbraco.Tests.Integration.Services
public void Find_BaseData_Language()
{
// Act
var languages = LocalizationService.GetAllLanguages();
var languages = _localizationService.GetAllLanguages();
// Assert
Assert.That(3, Is.EqualTo(languages.Count()));
@@ -353,8 +353,8 @@ namespace Umbraco.Tests.Integration.Services
var language = new Core.Models.Language(_globalSettings, isoCode);
// Act
LocalizationService.Save(language);
var result = LocalizationService.GetLanguageByIsoCode(isoCode);
_localizationService.Save(language);
var result = _localizationService.GetLanguageByIsoCode(isoCode);
// Assert
Assert.NotNull(result);
@@ -367,8 +367,8 @@ namespace Umbraco.Tests.Integration.Services
var language = new Core.Models.Language(_globalSettings, isoCode);
// Act
LocalizationService.Save(language);
var result = LocalizationService.GetLanguageById(language.Id);
_localizationService.Save(language);
var result = _localizationService.GetLanguageById(language.Id);
// Assert
Assert.NotNull(result);
@@ -378,16 +378,16 @@ namespace Umbraco.Tests.Integration.Services
public void Set_Default_Language()
{
var language = new Language(_globalSettings, "en-AU") {IsDefault = true};
LocalizationService.Save(language);
var result = LocalizationService.GetLanguageById(language.Id);
_localizationService.Save(language);
var result = _localizationService.GetLanguageById(language.Id);
Assert.IsTrue(result.IsDefault);
var language2 = new Language(_globalSettings, "en-NZ") {IsDefault = true};
LocalizationService.Save(language2);
var result2 = LocalizationService.GetLanguageById(language2.Id);
_localizationService.Save(language2);
var result2 = _localizationService.GetLanguageById(language2.Id);
//re-get
result = LocalizationService.GetLanguageById(language.Id);
result = _localizationService.GetLanguageById(language.Id);
Assert.IsTrue(result2.IsDefault);
Assert.IsFalse(result.IsDefault);
@@ -398,11 +398,11 @@ namespace Umbraco.Tests.Integration.Services
{
var isoCode = "en-AU";
var language = new Core.Models.Language(_globalSettings, isoCode);
LocalizationService.Save(language);
_localizationService.Save(language);
// Act
LocalizationService.Delete(language);
var result = LocalizationService.GetLanguageByIsoCode(isoCode);
_localizationService.Delete(language);
var result = _localizationService.GetLanguageByIsoCode(isoCode);
// Assert
Assert.Null(result);
@@ -412,8 +412,8 @@ namespace Umbraco.Tests.Integration.Services
{
var danish = new Language(_globalSettings, "da-DK") { CultureName = "Danish" };
var english = new Language(_globalSettings, "en-GB") { CultureName = "English" };
LocalizationService.Save(danish, 0);
LocalizationService.Save(english, 0);
_localizationService.Save(danish, 0);
_localizationService.Save(english, 0);
_danishLangId = danish.Id;
_englishLangId = english.Id;
@@ -425,7 +425,7 @@ namespace Umbraco.Tests.Integration.Services
new DictionaryTranslation(danish, "ForældreVærdi")
}
};
LocalizationService.Save(parentItem);
_localizationService.Save(parentItem);
_parentItemGuidId = parentItem.Key;
_parentItemIntId = parentItem.Id;
@@ -437,7 +437,7 @@ namespace Umbraco.Tests.Integration.Services
new DictionaryTranslation(danish, "BørnVærdi")
}
};
LocalizationService.Save(childItem);
_localizationService.Save(childItem);
_childItemGuidId = childItem.Key;
_childItemIntId = childItem.Id;
}

View File

@@ -19,8 +19,8 @@ namespace Umbraco.Tests.Integration.Services
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, PublishedRepositoryEvents = true)]
public class MediaServiceTests : UmbracoIntegrationTest
{
private IMediaService MediaService => GetRequiredService<IMediaService>();
private IMediaTypeService MediaTypeService => GetRequiredService<IMediaTypeService>();
private IMediaService _mediaService => GetRequiredService<IMediaService>();
private IMediaTypeService _mediaTypeService => GetRequiredService<IMediaTypeService>();
/// <summary>
/// Used to list out all ambiguous events that will require dispatching with a name
@@ -28,7 +28,7 @@ namespace Umbraco.Tests.Integration.Services
[Test, Explicit]
public void List_Ambiguous_Events()
{
var events = MediaService.GetType().GetEvents(BindingFlags.Static | BindingFlags.Public);
var events = _mediaService.GetType().GetEvents(BindingFlags.Static | BindingFlags.Public);
var typedEventHandler = typeof(TypedEventHandler<,>);
foreach (var e in events)
{
@@ -52,30 +52,30 @@ namespace Umbraco.Tests.Integration.Services
public void Get_Paged_Children_With_Media_Type_Filter()
{
var mediaType1 = MockedContentTypes.CreateImageMediaType("Image2");
MediaTypeService.Save(mediaType1);
_mediaTypeService.Save(mediaType1);
var mediaType2 = MockedContentTypes.CreateImageMediaType("Image3");
MediaTypeService.Save(mediaType2);
_mediaTypeService.Save(mediaType2);
for (var i = 0; i < 10; i++)
{
var m1 = MockedMedia.CreateMediaImage(mediaType1, -1);
MediaService.Save(m1);
_mediaService.Save(m1);
var m2 = MockedMedia.CreateMediaImage(mediaType2, -1);
MediaService.Save(m2);
_mediaService.Save(m2);
}
long total;
var provider = ScopeProvider;
using (provider.CreateScope())
{
var result = MediaService.GetPagedChildren(-1, 0, 11, out total,
var result = _mediaService.GetPagedChildren(-1, 0, 11, out total,
provider.SqlContext.Query<IMedia>()
.Where(x => new[] { mediaType1.Id, mediaType2.Id }.Contains(x.ContentTypeId)),
Ordering.By("SortOrder", Direction.Ascending));
Assert.AreEqual(11, result.Count());
Assert.AreEqual(20, total);
result = MediaService.GetPagedChildren(-1, 1, 11, out total,
result = _mediaService.GetPagedChildren(-1, 1, 11, out total,
provider.SqlContext.Query<IMedia>()
.Where(x => new[] { mediaType1.Id, mediaType2.Id }.Contains(x.ContentTypeId)),
Ordering.By("SortOrder", Direction.Ascending));
@@ -89,10 +89,10 @@ namespace Umbraco.Tests.Integration.Services
{
// Arrange
var mediaItems = CreateTrashedTestMedia();
var media = MediaService.GetById(mediaItems.Item3.Id);
var media = _mediaService.GetById(mediaItems.Item3.Id);
// Act
MediaService.Move(media, mediaItems.Item2.Id);
_mediaService.Move(media, mediaItems.Item2.Id);
// Assert
Assert.That(media.ParentId, Is.EqualTo(mediaItems.Item2.Id));
@@ -104,10 +104,10 @@ namespace Umbraco.Tests.Integration.Services
{
// Arrange
var mediaItems = CreateTrashedTestMedia();
var media = MediaService.GetById(mediaItems.Item1.Id);
var media = _mediaService.GetById(mediaItems.Item1.Id);
// Act
MediaService.MoveToRecycleBin(media);
_mediaService.MoveToRecycleBin(media);
// Assert
Assert.That(media.ParentId, Is.EqualTo(-21));
@@ -119,11 +119,11 @@ namespace Umbraco.Tests.Integration.Services
{
// Arrange
var mediaItems = CreateTrashedTestMedia();
var media = MediaService.GetById(mediaItems.Item4.Id);
var media = _mediaService.GetById(mediaItems.Item4.Id);
// Act - moving out of recycle bin
MediaService.Move(media, mediaItems.Item1.Id);
var mediaChild = MediaService.GetById(mediaItems.Item5.Id);
_mediaService.Move(media, mediaItems.Item1.Id);
var mediaChild = _mediaService.GetById(mediaItems.Item5.Id);
// Assert
Assert.That(media.ParentId, Is.EqualTo(mediaItems.Item1.Id));
@@ -137,11 +137,11 @@ namespace Umbraco.Tests.Integration.Services
{
// Arrange
var mediaType = MockedContentTypes.CreateVideoMediaType();
MediaTypeService.Save(mediaType);
var media = MediaService.CreateMedia(string.Empty, -1, "video");
_mediaTypeService.Save(mediaType);
var media = _mediaService.CreateMedia(string.Empty, -1, "video");
// Act & Assert
Assert.Throws<ArgumentException>(() => MediaService.Save(media));
Assert.Throws<ArgumentException>(() => _mediaService.Save(media));
}
/*
[Test]
@@ -163,13 +163,13 @@ namespace Umbraco.Tests.Integration.Services
public void Can_Get_Media_By_Path()
{
var mediaType = MockedContentTypes.CreateImageMediaType("Image2");
MediaTypeService.Save(mediaType);
_mediaTypeService.Save(mediaType);
var media = MockedMedia.CreateMediaImage(mediaType, -1);
MediaService.Save(media);
_mediaService.Save(media);
var mediaPath = "/media/test-image.png";
var resolvedMedia = MediaService.GetMediaByPath(mediaPath);
var resolvedMedia = _mediaService.GetMediaByPath(mediaPath);
Assert.IsNotNull(resolvedMedia);
Assert.That(resolvedMedia.GetValue(Constants.Conventions.Media.File).ToString() == mediaPath);
@@ -179,13 +179,13 @@ namespace Umbraco.Tests.Integration.Services
public void Can_Get_Media_With_Crop_By_Path()
{
var mediaType = MockedContentTypes.CreateImageMediaTypeWithCrop("Image2");
MediaTypeService.Save(mediaType);
_mediaTypeService.Save(mediaType);
var media = MockedMedia.CreateMediaImageWithCrop(mediaType, -1);
MediaService.Save(media);
_mediaService.Save(media);
var mediaPath = "/media/test-image.png";
var resolvedMedia = MediaService.GetMediaByPath(mediaPath);
var resolvedMedia = _mediaService.GetMediaByPath(mediaPath);
Assert.IsNotNull(resolvedMedia);
Assert.That(resolvedMedia.GetValue(Constants.Conventions.Media.File).ToString().Contains(mediaPath));
@@ -195,14 +195,14 @@ namespace Umbraco.Tests.Integration.Services
public void Can_Get_Paged_Children()
{
var mediaType = MockedContentTypes.CreateImageMediaType("Image2");
MediaTypeService.Save(mediaType);
_mediaTypeService.Save(mediaType);
for (var i = 0; i < 10; i++)
{
var c1 = MockedMedia.CreateMediaImage(mediaType, -1);
MediaService.Save(c1);
_mediaService.Save(c1);
}
var service = MediaService;
var service = _mediaService;
long total;
var entities = service.GetPagedChildren(-1, 0, 6, out total).ToArray();
@@ -217,25 +217,25 @@ namespace Umbraco.Tests.Integration.Services
public void Can_Get_Paged_Children_Dont_Get_Descendants()
{
var mediaType = MockedContentTypes.CreateImageMediaType("Image2");
MediaTypeService.Save(mediaType);
_mediaTypeService.Save(mediaType);
// only add 9 as we also add a folder with children
for (var i = 0; i < 9; i++)
{
var m1 = MockedMedia.CreateMediaImage(mediaType, -1);
MediaService.Save(m1);
_mediaService.Save(m1);
}
var mediaTypeForFolder = MockedContentTypes.CreateImageMediaType("Folder2");
MediaTypeService.Save(mediaTypeForFolder);
_mediaTypeService.Save(mediaTypeForFolder);
var mediaFolder = MockedMedia.CreateMediaFolder(mediaTypeForFolder, -1);
MediaService.Save(mediaFolder);
_mediaService.Save(mediaFolder);
for (var i = 0; i < 10; i++)
{
var m1 = MockedMedia.CreateMediaImage(mediaType, mediaFolder.Id);
MediaService.Save(m1);
_mediaService.Save(m1);
}
var service = MediaService;
var service = _mediaService;
long total;
// children in root including the folder - not the descendants in the folder
@@ -258,28 +258,28 @@ namespace Umbraco.Tests.Integration.Services
private Tuple<IMedia, IMedia, IMedia, IMedia, IMedia> CreateTrashedTestMedia()
{
//Create and Save folder-Media -> 1050
var folderMediaType = MediaTypeService.Get(1031);
var folderMediaType = _mediaTypeService.Get(1031);
var folder = MockedMedia.CreateMediaFolder(folderMediaType, -1);
MediaService.Save(folder);
_mediaService.Save(folder);
//Create and Save folder-Media -> 1051
var folder2 = MockedMedia.CreateMediaFolder(folderMediaType, -1);
MediaService.Save(folder2);
_mediaService.Save(folder2);
//Create and Save image-Media -> 1052
var imageMediaType = MediaTypeService.Get(1032);
var imageMediaType = _mediaTypeService.Get(1032);
var image = (Media)MockedMedia.CreateMediaImage(imageMediaType, 1050);
MediaService.Save(image);
_mediaService.Save(image);
//Create and Save folder-Media that is trashed -> 1053
var folderTrashed = (Media)MockedMedia.CreateMediaFolder(folderMediaType, -21);
folderTrashed.Trashed = true;
MediaService.Save(folderTrashed);
_mediaService.Save(folderTrashed);
//Create and Save image-Media child of folderTrashed -> 1054
var imageTrashed = (Media)MockedMedia.CreateMediaImage(imageMediaType, folderTrashed.Id);
imageTrashed.Trashed = true;
MediaService.Save(imageTrashed);
_mediaService.Save(imageTrashed);
return new Tuple<IMedia, IMedia, IMedia, IMedia, IMedia>(folder, folder2, image, folderTrashed, imageTrashed);
}

View File

@@ -15,20 +15,20 @@ namespace Umbraco.Tests.Integration.Services
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class PublicAccessServiceTests : UmbracoIntegrationTest
{
private IContentService ContentService => GetRequiredService<IContentService>();
private IContentTypeService ContentTypeService => GetRequiredService<IContentTypeService>();
private IFileService FileService => GetRequiredService<IFileService>();
private IPublicAccessService PublicAccessService => GetRequiredService<IPublicAccessService>();
private IContentService _contentService => GetRequiredService<IContentService>();
private IContentTypeService _contentTypeService => GetRequiredService<IContentTypeService>();
private IFileService _fileService => GetRequiredService<IFileService>();
private IPublicAccessService _publicAccessService => GetRequiredService<IPublicAccessService>();
[Test]
public void Can_Add_New_Entry()
{
// Arrange
var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah");
FileService.SaveTemplate(ct.DefaultTemplate);
ContentTypeService.Save(ct);
_fileService.SaveTemplate(ct.DefaultTemplate);
_contentTypeService.Save(ct);
var c = MockedContent.CreateSimpleContent(ct, "Test", -1);
ContentService.Save(c);
_contentService.Save(c);
// Act
var entry = new PublicAccessEntry(c, c, c, new[]
@@ -39,7 +39,7 @@ namespace Umbraco.Tests.Integration.Services
RuleValue = "TestVal"
},
});
var result = PublicAccessService.Save(entry);
var result = _publicAccessService.Save(entry);
// Assert
Assert.IsTrue(result.Success);
@@ -56,10 +56,10 @@ namespace Umbraco.Tests.Integration.Services
{
// Arrange
var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah");
FileService.SaveTemplate(ct.DefaultTemplate);
ContentTypeService.Save(ct);
_fileService.SaveTemplate(ct.DefaultTemplate);
_contentTypeService.Save(ct);
var c = MockedContent.CreateSimpleContent(ct, "Test", -1);
ContentService.Save(c);
_contentService.Save(c);
var entry = new PublicAccessEntry(c, c, c, new[]
{
new PublicAccessRule()
@@ -68,12 +68,12 @@ namespace Umbraco.Tests.Integration.Services
RuleValue = "TestVal"
},
});
PublicAccessService.Save(entry);
_publicAccessService.Save(entry);
// Act
var updated = PublicAccessService.AddRule(c, "TestType2", "AnotherVal");
var updated = _publicAccessService.AddRule(c, "TestType2", "AnotherVal");
//re-get
entry = PublicAccessService.GetEntryForContent(c);
entry = _publicAccessService.GetEntryForContent(c);
// Assert
Assert.IsTrue(updated.Success);
@@ -86,10 +86,10 @@ namespace Umbraco.Tests.Integration.Services
{
// Arrange
var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah");
FileService.SaveTemplate(ct.DefaultTemplate);
ContentTypeService.Save(ct);
_fileService.SaveTemplate(ct.DefaultTemplate);
_contentTypeService.Save(ct);
var c = MockedContent.CreateSimpleContent(ct, "Test", -1);
ContentService.Save(c);
_contentService.Save(c);
var entry = new PublicAccessEntry(c, c, c, new[]
{
new PublicAccessRule()
@@ -98,14 +98,14 @@ namespace Umbraco.Tests.Integration.Services
RuleValue = "TestVal"
},
});
PublicAccessService.Save(entry);
_publicAccessService.Save(entry);
// Act
var updated1 = PublicAccessService.AddRule(c, "TestType", "AnotherVal1");
var updated2 = PublicAccessService.AddRule(c, "TestType", "AnotherVal2");
var updated1 = _publicAccessService.AddRule(c, "TestType", "AnotherVal1");
var updated2 = _publicAccessService.AddRule(c, "TestType", "AnotherVal2");
//re-get
entry = PublicAccessService.GetEntryForContent(c);
entry = _publicAccessService.GetEntryForContent(c);
// Assert
Assert.IsTrue(updated1.Success);
@@ -120,10 +120,10 @@ namespace Umbraco.Tests.Integration.Services
{
// Arrange
var ct = MockedContentTypes.CreateSimpleContentType("blah", "Blah");
FileService.SaveTemplate(ct.DefaultTemplate);
ContentTypeService.Save(ct);
_fileService.SaveTemplate(ct.DefaultTemplate);
_contentTypeService.Save(ct);
var c = MockedContent.CreateSimpleContent(ct, "Test", -1);
ContentService.Save(c);
_contentService.Save(c);
var entry = new PublicAccessEntry(c, c, c, new[]
{
new PublicAccessRule()
@@ -137,12 +137,12 @@ namespace Umbraco.Tests.Integration.Services
RuleValue = "TestValue2"
},
});
PublicAccessService.Save(entry);
_publicAccessService.Save(entry);
// Act
var removed = PublicAccessService.RemoveRule(c, "TestType", "TestValue1");
var removed = _publicAccessService.RemoveRule(c, "TestType", "TestValue1");
//re-get
entry = PublicAccessService.GetEntryForContent(c);
entry = _publicAccessService.GetEntryForContent(c);
// Assert
Assert.IsTrue(removed.Success);

View File

@@ -8,7 +8,7 @@ using Umbraco.Tests.Integration.Testing;
using Umbraco.Tests.Testing;
using Umbraco.Web.Services;
namespace Umbraco.Tests.Services
namespace Umbraco.Tests.Integration.Services
{
/// <summary>
/// Tests covering the SectionService

View File

@@ -22,11 +22,11 @@ namespace Umbraco.Tests.Integration.Services
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class TagServiceTests : UmbracoIntegrationTest
{
private IContentService ContentService => GetRequiredService<IContentService>();
private IContentTypeService ContentTypeService => GetRequiredService<IContentTypeService>();
private ITagService TagService => GetRequiredService<ITagService>();
private IDataTypeService DataTypeService => GetRequiredService<IDataTypeService>();
public PropertyEditorCollection PropertyEditorCollection => GetRequiredService<PropertyEditorCollection>();
private IContentService _contentService => GetRequiredService<IContentService>();
private IContentTypeService _contentTypeService => GetRequiredService<IContentTypeService>();
private ITagService _tagService => GetRequiredService<ITagService>();
private IDataTypeService _dataTypeService => GetRequiredService<IDataTypeService>();
private PropertyEditorCollection _propertyEditorCollection => GetRequiredService<PropertyEditorCollection>();
[Test]
public void TagApiConsistencyTest()
@@ -37,25 +37,25 @@ namespace Umbraco.Tests.Integration.Services
{
DataTypeId = 1041
});
ContentTypeService.Save(contentType);
_contentTypeService.Save(contentType);
IContent content1 = MockedContent.CreateSimpleContent(contentType, "Tagged content 1", -1);
content1.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow", "pig", "goat" });
ContentService.SaveAndPublish(content1);
content1.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "cow", "pig", "goat" });
_contentService.SaveAndPublish(content1);
// change
content1.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "elephant" }, true);
content1.RemoveTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow" });
ContentService.SaveAndPublish(content1);
content1.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "elephant" }, true);
content1.RemoveTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "cow" });
_contentService.SaveAndPublish(content1);
// more changes
content1.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "mouse" }, true);
ContentService.SaveAndPublish(content1);
content1.RemoveTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "mouse" });
ContentService.SaveAndPublish(content1);
content1.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "mouse" }, true);
_contentService.SaveAndPublish(content1);
content1.RemoveTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "mouse" });
_contentService.SaveAndPublish(content1);
// get it back
content1 = ContentService.GetById(content1.Id);
content1 = _contentService.GetById(content1.Id);
var tagsValue = content1.GetValue("tags").ToString();
var tagsValues = JsonConvert.DeserializeObject<string[]>(tagsValue);
Assert.AreEqual(3, tagsValues.Length);
@@ -63,7 +63,7 @@ namespace Umbraco.Tests.Integration.Services
Assert.Contains("goat", tagsValues);
Assert.Contains("elephant", tagsValues);
var tags = TagService.GetTagsForProperty(content1.Id, "tags").ToArray();
var tags = _tagService.GetTagsForProperty(content1.Id, "tags").ToArray();
Assert.IsTrue(tags.All(x => x.Group == "default"));
tagsValues = tags.Select(x => x.Text).ToArray();
@@ -82,22 +82,22 @@ namespace Umbraco.Tests.Integration.Services
{
DataTypeId = Constants.DataTypes.Tags
});
ContentTypeService.Save(contentType);
_contentTypeService.Save(contentType);
var content1 = MockedContent.CreateSimpleContent(contentType, "Tagged content 1", -1);
content1.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow", "pig", "goat" });
ContentService.SaveAndPublish(content1);
content1.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "cow", "pig", "goat" });
_contentService.SaveAndPublish(content1);
var content2 = MockedContent.CreateSimpleContent(contentType, "Tagged content 2", -1);
content2.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow", "pig" });
ContentService.SaveAndPublish(content2);
content2.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "cow", "pig" });
_contentService.SaveAndPublish(content2);
var content3 = MockedContent.CreateSimpleContent(contentType, "Tagged content 3", -1);
content3.AssignTags(PropertyEditorCollection, DataTypeService, "tags", new[] { "cow" });
ContentService.SaveAndPublish(content3);
content3.AssignTags(_propertyEditorCollection, _dataTypeService, "tags", new[] { "cow" });
_contentService.SaveAndPublish(content3);
// Act
var tags = TagService.GetAllContentTags()
var tags = _tagService.GetAllContentTags()
.OrderByDescending(x => x.NodeCount)
.ToList();

View File

@@ -12,11 +12,12 @@ using Umbraco.Core.Models.Membership;
using Umbraco.Core.Persistence.Querying;
using Umbraco.Core.Services;
using Umbraco.Core.Services.Implement;
using Umbraco.Tests.Common.Builders;
using Umbraco.Tests.Common.Builders.Extensions;
using Umbraco.Tests.Integration.Testing;
using Umbraco.Tests.TestHelpers.Entities;
using Umbraco.Tests.Testing;
using Umbraco.Web.Actions;
using MockedUser = Umbraco.Tests.Common.TestHelpers.Entities.MockedUser;
namespace Umbraco.Tests.Integration.Services
{
@@ -28,9 +29,9 @@ namespace Umbraco.Tests.Integration.Services
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class UserServiceTests : UmbracoIntegrationTest
{
private UserService UserService => (UserService) GetRequiredService<IUserService>();
private IContentTypeService ContentTypeService => GetRequiredService<IContentTypeService>();
private IContentService ContentService => GetRequiredService<IContentService>();
private UserService _userService => (UserService) GetRequiredService<IUserService>();
private IContentTypeService _contentTypeService => GetRequiredService<IContentTypeService>();
private IContentService _contentService => GetRequiredService<IContentService>();
[Test]
public void Get_User_Permissions_For_Unassigned_Permission_Nodes()
@@ -38,17 +39,17 @@ namespace Umbraco.Tests.Integration.Services
// Arrange
var user = CreateTestUser(out _);
var contentType = MockedContentTypes.CreateSimpleContentType();
ContentTypeService.Save(contentType);
_contentTypeService.Save(contentType);
var content = new[]
{
MockedContent.CreateSimpleContent(contentType),
MockedContent.CreateSimpleContent(contentType),
MockedContent.CreateSimpleContent(contentType)
};
ContentService.Save(content);
_contentService.Save(content);
// Act
var permissions = UserService.GetPermissions(user, content[0].Id, content[1].Id, content[2].Id).ToArray();
var permissions = _userService.GetPermissions(user, content[0].Id, content[1].Id, content[2].Id).ToArray();
// Assert
Assert.AreEqual(3, permissions.Length);
@@ -64,23 +65,23 @@ namespace Umbraco.Tests.Integration.Services
var user = CreateTestUser(out var userGroup);
var contentType = MockedContentTypes.CreateSimpleContentType();
ContentTypeService.Save(contentType);
_contentTypeService.Save(contentType);
var content = new[]
{
MockedContent.CreateSimpleContent(contentType),
MockedContent.CreateSimpleContent(contentType),
MockedContent.CreateSimpleContent(contentType)
};
ContentService.Save(content);
ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content[2], ActionBrowse.ActionLetter, new int[] { userGroup.Id });
_contentService.Save(content);
_contentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content[2], ActionBrowse.ActionLetter, new int[] { userGroup.Id });
// Act
var permissions = UserService.GetPermissions(user, content[0].Id, content[1].Id, content[2].Id).ToArray();
var permissions = _userService.GetPermissions(user, content[0].Id, content[1].Id, content[2].Id).ToArray();
// Assert
Assert.AreEqual(3, permissions.Length);
@@ -96,23 +97,23 @@ namespace Umbraco.Tests.Integration.Services
var userGroup = CreateTestUserGroup();
var contentType = MockedContentTypes.CreateSimpleContentType();
ContentTypeService.Save(contentType);
_contentTypeService.Save(contentType);
var content = new[]
{
MockedContent.CreateSimpleContent(contentType),
MockedContent.CreateSimpleContent(contentType),
MockedContent.CreateSimpleContent(contentType)
};
ContentService.Save(content);
ContentService.SetPermission(content.ElementAt(0), ActionBrowse.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content.ElementAt(0), ActionDelete.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content.ElementAt(0), ActionMove.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content.ElementAt(1), ActionBrowse.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content.ElementAt(1), ActionDelete.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content.ElementAt(2), ActionBrowse.ActionLetter, new int[] { userGroup.Id });
_contentService.Save(content);
_contentService.SetPermission(content.ElementAt(0), ActionBrowse.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content.ElementAt(0), ActionDelete.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content.ElementAt(0), ActionMove.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content.ElementAt(1), ActionBrowse.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content.ElementAt(1), ActionDelete.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content.ElementAt(2), ActionBrowse.ActionLetter, new int[] { userGroup.Id });
// Act
var permissions = UserService.GetPermissions(userGroup, false, content[0].Id, content[1].Id, content[2].Id).ToArray();
var permissions = _userService.GetPermissions(userGroup, false, content[0].Id, content[1].Id, content[2].Id).ToArray();
// Assert
Assert.AreEqual(3, permissions.Length);
@@ -128,22 +129,22 @@ namespace Umbraco.Tests.Integration.Services
var userGroup = CreateTestUserGroup();
var contentType = MockedContentTypes.CreateSimpleContentType();
ContentTypeService.Save(contentType);
_contentTypeService.Save(contentType);
var content = new[]
{
MockedContent.CreateSimpleContent(contentType),
MockedContent.CreateSimpleContent(contentType),
MockedContent.CreateSimpleContent(contentType)
};
ContentService.Save(content);
ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id });
_contentService.Save(content);
_contentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id });
// Act
var permissions = UserService.GetPermissions(userGroup, true, content[0].Id, content[1].Id, content[2].Id)
var permissions = _userService.GetPermissions(userGroup, true, content[0].Id, content[1].Id, content[2].Id)
.ToArray();
// Assert
@@ -160,35 +161,35 @@ namespace Umbraco.Tests.Integration.Services
var userGroup1 = CreateTestUserGroup();
var userGroup2 = CreateTestUserGroup("test2", "Test 2");
var userGroup3 = CreateTestUserGroup("test3", "Test 3");
var user = UserService.CreateUserWithIdentity("John Doe", "john@umbraco.io");
var user = _userService.CreateUserWithIdentity("John Doe", "john@umbraco.io");
var defaultPermissionCount = userGroup3.Permissions.Count();
user.AddGroup(userGroup1);
user.AddGroup(userGroup2);
user.AddGroup(userGroup3);
UserService.Save(user);
_userService.Save(user);
var contentType = MockedContentTypes.CreateSimpleContentType();
ContentTypeService.Save(contentType);
_contentTypeService.Save(contentType);
var content = new[]
{
MockedContent.CreateSimpleContent(contentType),
MockedContent.CreateSimpleContent(contentType),
MockedContent.CreateSimpleContent(contentType)
};
ContentService.Save(content);
_contentService.Save(content);
//assign permissions - we aren't assigning anything explicit for group3 and nothing explicit for content[2] /w group2
ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup1.Id });
ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup1.Id });
ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup2.Id });
ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup1.Id });
ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup2.Id });
ContentService.SetPermission(content[2], ActionDelete.ActionLetter, new int[] { userGroup1.Id });
_contentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup1.Id });
_contentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup1.Id });
_contentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup2.Id });
_contentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup1.Id });
_contentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup2.Id });
_contentService.SetPermission(content[2], ActionDelete.ActionLetter, new int[] { userGroup1.Id });
// Act
//we don't pass in any nodes so it will return all of them
var result = UserService.GetPermissions(user).ToArray();
var result = _userService.GetPermissions(user).ToArray();
var permissions = result
.GroupBy(x => x.EntityId)
.ToDictionary(x => x.Key, x => x.GroupBy(a => a.UserGroupId).ToDictionary(a => a.Key, a => a.ToArray()));
@@ -239,24 +240,24 @@ namespace Umbraco.Tests.Integration.Services
var userGroup = CreateTestUserGroup();
var contentType = MockedContentTypes.CreateSimpleContentType();
ContentTypeService.Save(contentType);
_contentTypeService.Save(contentType);
var content = new[]
{
MockedContent.CreateSimpleContent(contentType),
MockedContent.CreateSimpleContent(contentType),
MockedContent.CreateSimpleContent(contentType)
};
ContentService.Save(content);
ContentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(content[2], ActionDelete.ActionLetter, new int[] { userGroup.Id });
_contentService.Save(content);
_contentService.SetPermission(content[0], ActionBrowse.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content[0], ActionDelete.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content[0], ActionMove.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content[1], ActionBrowse.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content[1], ActionDelete.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(content[2], ActionDelete.ActionLetter, new int[] { userGroup.Id });
// Act
//we don't pass in any nodes so it will return all of them
var permissions = UserService.GetPermissions(userGroup, true)
var permissions = _userService.GetPermissions(userGroup, true)
.GroupBy(x => x.EntityId)
.ToDictionary(x => x.Key, x => x);
@@ -400,22 +401,22 @@ namespace Umbraco.Tests.Integration.Services
var userGroup = CreateTestUserGroup();
var contentType = MockedContentTypes.CreateSimpleContentType();
ContentTypeService.Save(contentType);
_contentTypeService.Save(contentType);
var parent = MockedContent.CreateSimpleContent(contentType);
ContentService.Save(parent);
_contentService.Save(parent);
var child1 = MockedContent.CreateSimpleContent(contentType, "child1", parent);
ContentService.Save(child1);
_contentService.Save(child1);
var child2 = MockedContent.CreateSimpleContent(contentType, "child2", child1);
ContentService.Save(child2);
_contentService.Save(child2);
ContentService.SetPermission(parent, ActionBrowse.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(parent, ActionDelete.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(parent, ActionMove.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(parent, ActionBrowse.ActionLetter, new int[] { userGroup.Id });
ContentService.SetPermission(parent, ActionDelete.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(parent, ActionBrowse.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(parent, ActionDelete.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(parent, ActionMove.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(parent, ActionBrowse.ActionLetter, new int[] { userGroup.Id });
_contentService.SetPermission(parent, ActionDelete.ActionLetter, new int[] { userGroup.Id });
// Act
var permissions = UserService.GetPermissionsForPath(userGroup, child2.Path);
var permissions = _userService.GetPermissionsForPath(userGroup, child2.Path);
// Assert
var allPermissions = permissions.GetAllPermissions().ToArray();
@@ -425,10 +426,10 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Can_Delete_User()
{
var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io");
var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io");
UserService.Delete(user, true);
var deleted = UserService.GetUserById(user.Id);
_userService.Delete(user, true);
var deleted = _userService.GetUserById(user.Id);
// Assert
Assert.That(deleted, Is.Null);
@@ -437,10 +438,10 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Disables_User_Instead_Of_Deleting_If_Flag_Not_Set()
{
var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io");
var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io");
UserService.Delete(user);
var deleted = UserService.GetUserById(user.Id);
_userService.Delete(user);
var deleted = _userService.GetUserById(user.Id);
// Assert
Assert.That(deleted, Is.Not.Null);
@@ -449,60 +450,60 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Exists_By_Username()
{
var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io");
var user2 = UserService.CreateUserWithIdentity("john2@umbraco.io", "john2@umbraco.io");
Assert.IsTrue(UserService.Exists("JohnDoe"));
Assert.IsFalse(UserService.Exists("notFound"));
Assert.IsTrue(UserService.Exists("john2@umbraco.io"));
var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io");
var user2 = _userService.CreateUserWithIdentity("john2@umbraco.io", "john2@umbraco.io");
Assert.IsTrue(_userService.Exists("JohnDoe"));
Assert.IsFalse(_userService.Exists("notFound"));
Assert.IsTrue(_userService.Exists("john2@umbraco.io"));
}
[Test]
public void Get_By_Email()
{
var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io");
var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io");
Assert.IsNotNull(UserService.GetByEmail(user.Email));
Assert.IsNull(UserService.GetByEmail("do@not.find"));
Assert.IsNotNull(_userService.GetByEmail(user.Email));
Assert.IsNull(_userService.GetByEmail("do@not.find"));
}
[Test]
public void Get_By_Username()
{
var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io");
var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io");
Assert.IsNotNull(UserService.GetByUsername(user.Username));
Assert.IsNull(UserService.GetByUsername("notFound"));
Assert.IsNotNull(_userService.GetByUsername(user.Username));
Assert.IsNull(_userService.GetByUsername("notFound"));
}
[Test]
public void Get_By_Username_With_Backslash()
{
var user = UserService.CreateUserWithIdentity("mydomain\\JohnDoe", "john@umbraco.io");
var user = _userService.CreateUserWithIdentity("mydomain\\JohnDoe", "john@umbraco.io");
Assert.IsNotNull(UserService.GetByUsername(user.Username));
Assert.IsNull(UserService.GetByUsername("notFound"));
Assert.IsNotNull(_userService.GetByUsername(user.Username));
Assert.IsNull(_userService.GetByUsername("notFound"));
}
[Test]
public void Get_By_Object_Id()
{
var user = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io");
var user = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io");
Assert.IsNotNull(UserService.GetUserById(user.Id));
Assert.IsNull(UserService.GetUserById(9876));
Assert.IsNotNull(_userService.GetUserById(user.Id));
Assert.IsNull(_userService.GetUserById(9876));
}
[Test]
public void Find_By_Email_Starts_With()
{
var users = MockedUser.CreateMulipleUsers(10);
UserService.Save(users);
var users = CreateMulipleUsers(10);
_userService.Save(users);
//don't find this
var customUser = MockedUser.CreateUser();
var customUser = CreateUser();
customUser.Email = "hello@hello.com";
UserService.Save(customUser);
_userService.Save(customUser);
var found = UserService.FindByEmail("tes", 0, 100, out _, StringPropertyMatchType.StartsWith);
var found = _userService.FindByEmail("tes", 0, 100, out _, StringPropertyMatchType.StartsWith);
Assert.AreEqual(10, found.Count());
}
@@ -510,14 +511,14 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Find_By_Email_Ends_With()
{
var users = MockedUser.CreateMulipleUsers(10);
UserService.Save(users);
var users = CreateMulipleUsers(10);
_userService.Save(users);
//include this
var customUser = MockedUser.CreateUser();
var customUser = CreateUser();
customUser.Email = "hello@test.com";
UserService.Save(customUser);
_userService.Save(customUser);
var found = UserService.FindByEmail("test.com", 0, 100, out _, StringPropertyMatchType.EndsWith);
var found = _userService.FindByEmail("test.com", 0, 100, out _, StringPropertyMatchType.EndsWith);
Assert.AreEqual(11, found.Count());
}
@@ -525,14 +526,14 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Find_By_Email_Contains()
{
var users = MockedUser.CreateMulipleUsers(10);
UserService.Save(users);
var users = CreateMulipleUsers(10);
_userService.Save(users);
//include this
var customUser = MockedUser.CreateUser();
var customUser = CreateUser();
customUser.Email = "hello@test.com";
UserService.Save(customUser);
_userService.Save(customUser);
var found = UserService.FindByEmail("test", 0, 100, out _, StringPropertyMatchType.Contains);
var found = _userService.FindByEmail("test", 0, 100, out _, StringPropertyMatchType.Contains);
Assert.AreEqual(11, found.Count());
}
@@ -540,14 +541,14 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Find_By_Email_Exact()
{
var users = MockedUser.CreateMulipleUsers(10);
UserService.Save(users);
var users = CreateMulipleUsers(10);
_userService.Save(users);
//include this
var customUser = MockedUser.CreateUser();
var customUser = CreateUser();
customUser.Email = "hello@test.com";
UserService.Save(customUser);
_userService.Save(customUser);
var found = UserService.FindByEmail("hello@test.com", 0, 100, out _, StringPropertyMatchType.Exact);
var found = _userService.FindByEmail("hello@test.com", 0, 100, out _, StringPropertyMatchType.Exact);
Assert.AreEqual(1, found.Count());
}
@@ -555,10 +556,10 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Get_All_Paged_Users()
{
var users = MockedUser.CreateMulipleUsers(10);
UserService.Save(users);
var users = CreateMulipleUsers(10);
_userService.Save(users);
var found = UserService.GetAll(0, 2, out var totalRecs);
var found = _userService.GetAll(0, 2, out var totalRecs);
Assert.AreEqual(2, found.Count());
// + 1 because of the built in admin user
@@ -570,10 +571,10 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Get_All_Paged_Users_With_Filter()
{
var users = MockedUser.CreateMulipleUsers(10).ToArray();
UserService.Save(users);
var users = CreateMulipleUsers(10).ToArray();
_userService.Save(users);
var found = UserService.GetAll(0, 2, out var totalRecs, "username", Direction.Ascending, filter: "test");
var found = _userService.GetAll(0, 2, out var totalRecs, "username", Direction.Ascending, filter: "test");
Assert.AreEqual(2, found.Count());
Assert.AreEqual(10, totalRecs);
@@ -585,18 +586,18 @@ namespace Umbraco.Tests.Integration.Services
public void Get_All_Paged_Users_For_Group()
{
var userGroup = MockedUserGroup.CreateUserGroup();
UserService.Save(userGroup);
_userService.Save(userGroup);
var users = MockedUser.CreateMulipleUsers(10).ToArray();
var users = CreateMulipleUsers(10).ToArray();
for (var i = 0; i < 10;)
{
users[i].AddGroup(userGroup.ToReadOnlyGroup());
i = i + 2;
}
UserService.Save(users);
_userService.Save(users);
long totalRecs;
var found = UserService.GetAll(0, 2, out totalRecs, "username", Direction.Ascending, includeUserGroups: new[] { userGroup.Alias });
var found = _userService.GetAll(0, 2, out totalRecs, "username", Direction.Ascending, includeUserGroups: new[] { userGroup.Alias });
Assert.AreEqual(2, found.Count());
Assert.AreEqual(5, totalRecs);
@@ -608,9 +609,9 @@ namespace Umbraco.Tests.Integration.Services
public void Get_All_Paged_Users_For_Group_With_Filter()
{
var userGroup = MockedUserGroup.CreateUserGroup();
UserService.Save(userGroup);
_userService.Save(userGroup);
var users = MockedUser.CreateMulipleUsers(10).ToArray();
var users = CreateMulipleUsers(10).ToArray();
for (var i = 0; i < 10;)
{
users[i].AddGroup(userGroup.ToReadOnlyGroup());
@@ -621,10 +622,10 @@ namespace Umbraco.Tests.Integration.Services
users[i].Name = "blah" + users[i].Name;
i = i + 3;
}
UserService.Save(users);
_userService.Save(users);
long totalRecs;
var found = UserService.GetAll(0, 2, out totalRecs, "username", Direction.Ascending, userGroups: new[] { userGroup.Alias }, filter: "blah");
var found = _userService.GetAll(0, 2, out totalRecs, "username", Direction.Ascending, userGroups: new[] { userGroup.Alias }, filter: "blah");
Assert.AreEqual(2, found.Count());
Assert.AreEqual(2, totalRecs);
@@ -635,12 +636,12 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Count_All_Users()
{
var users = MockedUser.CreateMulipleUsers(10);
UserService.Save(users);
var customUser = MockedUser.CreateUser();
UserService.Save(customUser);
var users = CreateMulipleUsers(10);
_userService.Save(users);
var customUser = CreateUser();
_userService.Save(customUser);
var found = UserService.GetCount(MemberCountType.All);
var found = _userService.GetCount(MemberCountType.All);
// + 1 because of the built in admin user
Assert.AreEqual(12, found);
@@ -650,24 +651,24 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Count_All_Online_Users()
{
var users = MockedUser.CreateMulipleUsers(10, (i, member) => member.LastLoginDate = DateTime.Now.AddMinutes(i * -2));
UserService.Save(users);
var users = CreateMulipleUsers(10, (i, member) => member.LastLoginDate = DateTime.Now.AddMinutes(i * -2));
_userService.Save(users);
var customUser = MockedUser.CreateUser();
var customUser = CreateUser();
throw new NotImplementedException();
}
[Test]
public void Count_All_Locked_Users()
{
var users = MockedUser.CreateMulipleUsers(10, (i, member) => member.IsLockedOut = i % 2 == 0);
UserService.Save(users);
var users = CreateMulipleUsers(10, (i, member) => member.IsLockedOut = i % 2 == 0);
_userService.Save(users);
var customUser = MockedUser.CreateUser();
var customUser = CreateUser();
customUser.IsLockedOut = true;
UserService.Save(customUser);
_userService.Save(customUser);
var found = UserService.GetCount(MemberCountType.LockedOut);
var found = _userService.GetCount(MemberCountType.LockedOut);
Assert.AreEqual(6, found);
}
@@ -675,14 +676,14 @@ namespace Umbraco.Tests.Integration.Services
[Test]
public void Count_All_Approved_Users()
{
var users = MockedUser.CreateMulipleUsers(10, (i, member) => member.IsApproved = i % 2 == 0);
UserService.Save(users);
var users = CreateMulipleUsers(10, (i, member) => member.IsApproved = i % 2 == 0);
_userService.Save(users);
var customUser = MockedUser.CreateUser();
var customUser = CreateUser();
customUser.IsApproved = false;
UserService.Save(customUser);
_userService.Save(customUser);
var found = UserService.GetCount(MemberCountType.Approved);
var found = _userService.GetCount(MemberCountType.Approved);
// + 1 because of the built in admin user
Assert.AreEqual(6, found);
@@ -692,7 +693,7 @@ namespace Umbraco.Tests.Integration.Services
public void Can_Persist_New_User()
{
// Act
var membershipUser = UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io");
var membershipUser = _userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io");
// Assert
Assert.That(membershipUser.HasIdentity, Is.True);
@@ -712,7 +713,7 @@ namespace Umbraco.Tests.Integration.Services
var encodedPassword = Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
var globalSettings = new GlobalSettings();
var membershipUser = new User(globalSettings, "JohnDoe", "john@umbraco.io", encodedPassword, encodedPassword);
UserService.Save(membershipUser);
_userService.Save(membershipUser);
// Assert
Assert.That(membershipUser.HasIdentity, Is.True);
@@ -732,9 +733,9 @@ namespace Umbraco.Tests.Integration.Services
};
userGroup.AddAllowedSection("content");
userGroup.AddAllowedSection("mediat");
UserService.Save(userGroup);
_userService.Save(userGroup);
var result1 = UserService.GetUserGroupById(userGroup.Id);
var result1 = _userService.GetUserGroupById(userGroup.Id);
Assert.AreEqual(2, result1.AllowedSections.Count());
@@ -743,9 +744,9 @@ namespace Umbraco.Tests.Integration.Services
userGroup.AddAllowedSection("test2");
userGroup.AddAllowedSection("test3");
userGroup.AddAllowedSection("test4");
UserService.Save(userGroup);
_userService.Save(userGroup);
result1 = UserService.GetUserGroupById(userGroup.Id);
result1 = _userService.GetUserGroupById(userGroup.Id);
Assert.AreEqual(6, result1.AllowedSections.Count());
@@ -758,11 +759,11 @@ namespace Umbraco.Tests.Integration.Services
//now just re-add a couple
result1.AddAllowedSection("test3");
result1.AddAllowedSection("test4");
UserService.Save(result1);
_userService.Save(result1);
// Assert
//re-get
result1 = UserService.GetUserGroupById(userGroup.Id);
result1 = _userService.GetUserGroupById(userGroup.Id);
Assert.AreEqual(2, result1.AllowedSections.Count());
}
@@ -779,21 +780,21 @@ namespace Umbraco.Tests.Integration.Services
Alias = "Group2",
Name = "Group 2"
};
UserService.Save(userGroup1);
UserService.Save(userGroup2);
_userService.Save(userGroup1);
_userService.Save(userGroup2);
//adds some allowed sections
userGroup1.AddAllowedSection("test");
userGroup2.AddAllowedSection("test");
UserService.Save(userGroup1);
UserService.Save(userGroup2);
_userService.Save(userGroup1);
_userService.Save(userGroup2);
//now clear the section from all users
UserService.DeleteSectionFromAllUserGroups("test");
_userService.DeleteSectionFromAllUserGroups("test");
// Assert
var result1 = UserService.GetUserGroupById(userGroup1.Id);
var result2 = UserService.GetUserGroupById(userGroup2.Id);
var result1 = _userService.GetUserGroupById(userGroup1.Id);
var result2 = _userService.GetUserGroupById(userGroup2.Id);
Assert.IsFalse(result1.AllowedSections.Contains("test"));
Assert.IsFalse(result2.AllowedSections.Contains("test"));
}
@@ -820,14 +821,14 @@ namespace Umbraco.Tests.Integration.Services
Alias = "Group3",
Name = "Group 3"
};
UserService.Save(userGroup1);
UserService.Save(userGroup2);
UserService.Save(userGroup3);
_userService.Save(userGroup1);
_userService.Save(userGroup2);
_userService.Save(userGroup3);
// Assert
var result1 = UserService.GetUserGroupById(userGroup1.Id);
var result2 = UserService.GetUserGroupById(userGroup2.Id);
var result3 = UserService.GetUserGroupById(userGroup3.Id);
var result1 = _userService.GetUserGroupById(userGroup1.Id);
var result2 = _userService.GetUserGroupById(userGroup2.Id);
var result3 = _userService.GetUserGroupById(userGroup3.Id);
Assert.IsTrue(result1.AllowedSections.Contains("test"));
Assert.IsTrue(result2.AllowedSections.Contains("test"));
Assert.IsFalse(result3.AllowedSections.Contains("test"));
@@ -836,13 +837,13 @@ namespace Umbraco.Tests.Integration.Services
foreach (var userGroup in new[] { userGroup1, userGroup2, userGroup3 })
{
userGroup.AddAllowedSection("test");
UserService.Save(userGroup);
_userService.Save(userGroup);
}
// Assert
result1 = UserService.GetUserGroupById(userGroup1.Id);
result2 = UserService.GetUserGroupById(userGroup2.Id);
result3 = UserService.GetUserGroupById(userGroup3.Id);
result1 = _userService.GetUserGroupById(userGroup1.Id);
result2 = _userService.GetUserGroupById(userGroup2.Id);
result3 = _userService.GetUserGroupById(userGroup3.Id);
Assert.IsTrue(result1.AllowedSections.Contains("test"));
Assert.IsTrue(result2.AllowedSections.Contains("test"));
Assert.IsTrue(result3.AllowedSections.Contains("test"));
@@ -852,39 +853,39 @@ namespace Umbraco.Tests.Integration.Services
public void Cannot_Create_User_With_Empty_Username()
{
// Act & Assert
Assert.Throws<ArgumentException>(() => UserService.CreateUserWithIdentity(string.Empty, "john@umbraco.io"));
Assert.Throws<ArgumentException>(() => _userService.CreateUserWithIdentity(string.Empty, "john@umbraco.io"));
}
[Test]
public void Cannot_Save_User_With_Empty_Username()
{
// Arrange
var user = UserService.CreateUserWithIdentity("John Doe", "john@umbraco.io");
var user = _userService.CreateUserWithIdentity("John Doe", "john@umbraco.io");
user.Username = string.Empty;
// Act & Assert
Assert.Throws<ArgumentException>(() => UserService.Save(user));
Assert.Throws<ArgumentException>(() => _userService.Save(user));
}
[Test]
public void Cannot_Save_User_With_Empty_Name()
{
// Arrange
var user = UserService.CreateUserWithIdentity("John Doe", "john@umbraco.io");
var user = _userService.CreateUserWithIdentity("John Doe", "john@umbraco.io");
user.Name = string.Empty;
// Act & Assert
Assert.Throws<ArgumentException>(() => UserService.Save(user));
Assert.Throws<ArgumentException>(() => _userService.Save(user));
}
[Test]
public void Get_By_Profile_Username()
{
// Arrange
var user = UserService.CreateUserWithIdentity("test1", "test1@test.com");
var user = _userService.CreateUserWithIdentity("test1", "test1@test.com");
// Act
var profile = UserService.GetProfileByUserName(user.Username);
var profile = _userService.GetProfileByUserName(user.Username);
// Assert
Assert.IsNotNull(profile);
@@ -896,10 +897,10 @@ namespace Umbraco.Tests.Integration.Services
public void Get_By_Profile_Id()
{
// Arrange
var user = (IUser)UserService.CreateUserWithIdentity("test1", "test1@test.com");
var user = _userService.CreateUserWithIdentity("test1", "test1@test.com");
// Act
var profile = UserService.GetProfileById((int)user.Id);
var profile = _userService.GetProfileById((int)user.Id);
// Assert
Assert.IsNotNull(profile);
@@ -908,18 +909,18 @@ namespace Umbraco.Tests.Integration.Services
}
[Test]
public void Get_By_Profile_Id_Must_return_null_if_user_not_exists()
public void Get_By_Profile_Id_Must_Return_Null_If_User_Does_Not_Exist()
{
var profile = UserService.GetProfileById(42);
var profile = _userService.GetProfileById(42);
// Assert
Assert.IsNull(profile);
}
[Test]
public void GetProfilesById_Must_empty_if_users_not_exists()
public void GetProfilesById_Must_Return_Empty_If_User_Does_Not_Exist()
{
var profiles = UserService.GetProfilesById(42);
var profiles = _userService.GetProfilesById(42);
// Assert
CollectionAssert.IsEmpty(profiles);
@@ -933,7 +934,7 @@ namespace Umbraco.Tests.Integration.Services
// Act
var updatedItem = (User)UserService.GetByUsername(originalUser.Username);
var updatedItem = (User)_userService.GetByUsername(originalUser.Username);
// Assert
Assert.IsNotNull(updatedItem);
@@ -961,7 +962,7 @@ namespace Umbraco.Tests.Integration.Services
CreateTestUsers(startContentItems.Select(x => x.Id).ToArray(), testUserGroup, 3);
var usersInGroup = UserService.GetAllInGroup(userGroupId);
var usersInGroup = _userService.GetAllInGroup(userGroupId);
foreach (var user in usersInGroup)
Assert.AreEqual(user.StartContentIds.Length, startContentItems.Length);
@@ -971,27 +972,60 @@ namespace Umbraco.Tests.Integration.Services
{
var contentType = MockedContentTypes.CreateSimpleContentType();
ContentTypeService.Save(contentType);
_contentTypeService.Save(contentType);
var startContentItems = new List<Content>();
for (var i = 0; i < numberToCreate; i++)
startContentItems.Add(MockedContent.CreateSimpleContent(contentType));
ContentService.Save(startContentItems);
_contentService.Save(startContentItems);
return startContentItems.ToArray();
}
private static IEnumerable<IUser> CreateMulipleUsers(int amount, Action<int, IUser> onCreating = null)
{
var list = new List<IUser>();
for (var i = 0; i < amount; i++)
{
var name = "User No-" + i;
var user = new UserBuilder()
.WithName(name)
.WithEmail("test" + i + "@test.com")
.WithLogin("test" + i, "test" + i)
.Build();
onCreating?.Invoke(i, user);
user.ResetDirtyProperties(false);
list.Add(user);
}
return list;
}
private static User CreateUser(string suffix = "")
{
return new UserBuilder()
.WithIsApproved(true)
.WithName("TestUser" + suffix)
.WithLogin("TestUser" + suffix, "testing")
.WithEmail("test" + suffix + "@test.com")
.Build();
}
private IUser CreateTestUser(out IUserGroup userGroup)
{
userGroup = CreateTestUserGroup();
var user = UserService.CreateUserWithIdentity("test1", "test1@test.com");
var user = _userService.CreateUserWithIdentity("test1", "test1@test.com");
user.AddGroup(userGroup.ToReadOnlyGroup());
UserService.Save(user);
_userService.Save(user);
return user;
}
@@ -1002,13 +1036,13 @@ namespace Umbraco.Tests.Integration.Services
for (var i = 0; i < numberToCreate; i++)
{
var user = UserService.CreateUserWithIdentity($"test{i}", $"test{i}@test.com");
var user = _userService.CreateUserWithIdentity($"test{i}", $"test{i}@test.com");
user.AddGroup(userGroup.ToReadOnlyGroup());
var updateable = (User)user;
updateable.StartContentIds = startContentIds;
UserService.Save(user);
_userService.Save(user);
users.Add(user);
}
@@ -1028,7 +1062,7 @@ namespace Umbraco.Tests.Integration.Services
userGroup.AddAllowedSection("content");
userGroup.AddAllowedSection("media");
UserService.Save(userGroup);
_userService.Save(userGroup);
return userGroup;
}

View File

@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using Umbraco.Core;
@@ -10,8 +7,6 @@ using Umbraco.Core.Models;
using Umbraco.Core.Models.Entities;
using Umbraco.Core.Models.Membership;
using Umbraco.Core.Services;
using Umbraco.Tests.Common.TestHelpers.Entities;
using Umbraco.Tests.TestHelpers.Entities;
using Umbraco.Web.Editors;
namespace Umbraco.Tests.Web.Controllers
@@ -23,7 +18,7 @@ namespace Umbraco.Tests.Web.Controllers
public void Admin_Is_Authorized()
{
var currentUser = GetAdminUser();
var savingUser = MockedUser.GetUserMock();
var savingUser = GetUserMock();
var contentService = new Mock<IContentService>();
var mediaService = new Mock<IMediaService>();
@@ -44,7 +39,7 @@ namespace Umbraco.Tests.Web.Controllers
[Test]
public void Non_Admin_Cannot_Save_Admin()
{
var currentUser = MockedUser.GetUserMock();
var currentUser = GetUserMock();
var savingUser = GetAdminUser();
var contentService = new Mock<IContentService>();
@@ -66,12 +61,12 @@ namespace Umbraco.Tests.Web.Controllers
[Test]
public void Cannot_Grant_Group_Membership_Without_Being_A_Member()
{
var currentUser = MockedUser.GetUserMock();
var currentUser = GetUserMock();
currentUser.Setup(x => x.Groups).Returns(new[]
{
new ReadOnlyUserGroup(1, "Test", "icon-user", null, null, "test", new string[0], new string[0])
});
var savingUser = MockedUser.GetUserMock();
var savingUser = GetUserMock();
var contentService = new Mock<IContentService>();
var mediaService = new Mock<IMediaService>();
@@ -92,12 +87,12 @@ namespace Umbraco.Tests.Web.Controllers
[Test]
public void Can_Grant_Group_Membership_With_Being_A_Member()
{
var currentUser = MockedUser.GetUserMock();
var currentUser = GetUserMock();
currentUser.Setup(x => x.Groups).Returns(new[]
{
new ReadOnlyUserGroup(1, "Test", "icon-user", null, null, "test", new string[0], new string[0])
});
var savingUser = MockedUser.GetUserMock();
var savingUser = GetUserMock();
var contentService = new Mock<IContentService>();
var mediaService = new Mock<IMediaService>();
@@ -126,9 +121,9 @@ namespace Umbraco.Tests.Web.Controllers
{4567, "-1,4567"},
};
var currentUser = MockedUser.GetUserMock();
var currentUser = GetUserMock();
currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 });
var savingUser = MockedUser.GetUserMock();
var savingUser = GetUserMock();
savingUser.Setup(x => x.StartContentIds).Returns(new[] { 1234 });
var contentService = new Mock<IContentService>();
@@ -166,9 +161,9 @@ namespace Umbraco.Tests.Web.Controllers
{4567, "-1,4567"},
};
var currentUser = MockedUser.GetUserMock();
var currentUser = GetUserMock();
currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 });
var savingUser = MockedUser.GetUserMock();
var savingUser = GetUserMock();
savingUser.Setup(x => x.StartContentIds).Returns(new[] { 1234, 4567 });
var contentService = new Mock<IContentService>();
@@ -206,9 +201,9 @@ namespace Umbraco.Tests.Web.Controllers
{4567, "-1,4567"},
};
var currentUser = MockedUser.GetUserMock();
var currentUser = GetUserMock();
currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 });
var savingUser = MockedUser.GetUserMock();
var savingUser = GetUserMock();
var contentService = new Mock<IContentService>();
contentService.Setup(x => x.GetById(It.IsAny<int>()))
@@ -245,9 +240,9 @@ namespace Umbraco.Tests.Web.Controllers
{4567, "-1,4567"},
};
var currentUser = MockedUser.GetUserMock();
var currentUser = GetUserMock();
currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 });
var savingUser = MockedUser.GetUserMock();
var savingUser = GetUserMock();
var contentService = new Mock<IContentService>();
contentService.Setup(x => x.GetById(It.IsAny<int>()))
@@ -285,9 +280,9 @@ namespace Umbraco.Tests.Web.Controllers
};
var currentUser = MockedUser.GetUserMock();
var currentUser = GetUserMock();
currentUser.Setup(x => x.StartContentIds).Returns(new[] { 9876 });
var savingUser = MockedUser.GetUserMock();
var savingUser = GetUserMock();
var contentService = new Mock<IContentService>();
var mediaService = new Mock<IMediaService>();
@@ -324,9 +319,9 @@ namespace Umbraco.Tests.Web.Controllers
{4567, "-1,4567"},
};
var currentUser = MockedUser.GetUserMock();
var currentUser = GetUserMock();
currentUser.Setup(x => x.StartMediaIds).Returns(new[] { 9876 });
var savingUser = MockedUser.GetUserMock();
var savingUser = GetUserMock();
var contentService = new Mock<IContentService>();
var mediaService = new Mock<IMediaService>();
@@ -363,9 +358,9 @@ namespace Umbraco.Tests.Web.Controllers
{4567, "-1,4567"},
};
var currentUser = MockedUser.GetUserMock();
var currentUser = GetUserMock();
currentUser.Setup(x => x.StartMediaIds).Returns(new[] { 9876 });
var savingUser = MockedUser.GetUserMock();
var savingUser = GetUserMock();
savingUser.Setup(x => x.StartMediaIds).Returns(new[] { 1234 });
var contentService = new Mock<IContentService>();
@@ -403,9 +398,9 @@ namespace Umbraco.Tests.Web.Controllers
{4567, "-1,4567"},
};
var currentUser = MockedUser.GetUserMock();
var currentUser = GetUserMock();
currentUser.Setup(x => x.StartMediaIds).Returns(new[] { 9876 });
var savingUser = MockedUser.GetUserMock();
var savingUser = GetUserMock();
savingUser.Setup(x => x.StartMediaIds).Returns(new[] { 1234, 4567 });
var contentService = new Mock<IContentService>();
@@ -432,9 +427,23 @@ namespace Umbraco.Tests.Web.Controllers
Assert.IsTrue(result.Success);
}
/// <summary>
/// Returns a <see cref="Mock{IUser}"/> and ensures that the ToUserCache and FromUserCache methods are mapped correctly for
/// dealing with start node caches
/// </summary>
/// <returns></returns>
private static Mock<IUser> GetUserMock()
{
var userCache = new Dictionary<string, object>();
var userMock = new Mock<IUser>();
userMock.Setup(x => x.FromUserCache<int[]>(It.IsAny<string>())).Returns((string key) => userCache.TryGetValue(key, out var val) ? val is int[] iVal ? iVal : null : null);
userMock.Setup(x => x.ToUserCache<int[]>(It.IsAny<string>(), It.IsAny<int[]>())).Callback((string key, int[] val) => userCache[key] = val);
return userMock;
}
private IUser GetAdminUser()
{
var admin = MockedUser.GetUserMock();
var admin = GetUserMock();
admin.Setup(x => x.Groups).Returns(new[]
{
new ReadOnlyUserGroup(1, "Admin", "icon-user", null, null, Constants.Security.AdminGroupAlias, new string[0], new string[0])

View File

@@ -0,0 +1,78 @@
using System;
using System.Reflection;
using System.Text;
using NUnit.Framework;
using Umbraco.Core.Events;
using Umbraco.Core.Services.Implement;
namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.Services
{
[TestFixture]
public class AmbiguousEventTests
{
[Explicit]
[TestCase(typeof(ContentService))]
[TestCase(typeof(MediaService))]
public void ListAmbiguousEvents(Type serviceType)
{
var typedEventHandler = typeof(TypedEventHandler<,>);
// get all events
var events = serviceType.GetEvents(BindingFlags.Static | BindingFlags.Public);
string TypeName(Type type)
{
if (!type.IsGenericType)
return type.Name;
var sb = new StringBuilder();
TypeNameSb(type, sb);
return sb.ToString();
}
void TypeNameSb(Type type, StringBuilder sb)
{
var name = type.Name;
var pos = name.IndexOf('`');
name = pos > 0 ? name.Substring(0, pos) : name;
sb.Append(name);
if (!type.IsGenericType)
return;
sb.Append("<");
var first = true;
foreach (var arg in type.GetGenericArguments())
{
if (first) first = false;
else sb.Append(", ");
TypeNameSb(arg, sb);
}
sb.Append(">");
}
foreach (var e in events)
{
// only continue if this is a TypedEventHandler
if (!e.EventHandlerType.IsGenericType) continue;
var typeDef = e.EventHandlerType.GetGenericTypeDefinition();
if (typedEventHandler != typeDef) continue;
// get the event args type
var eventArgsType = e.EventHandlerType.GenericTypeArguments[1];
// try to find the event back, based upon sender type + args type
// exclude -ing (eg Saving) events, we don't deal with them in EventDefinitionBase (they always trigger)
var found = EventNameExtractor.FindEvents(serviceType, eventArgsType, EventNameExtractor.MatchIngNames);
if (found.Length == 1) continue;
if (found.Length == 0)
{
Console.WriteLine($"{typeof(ContentService).Name} {e.Name} {TypeName(eventArgsType)} NotFound");
continue;
}
Console.WriteLine($"{typeof(ContentService).Name} {e.Name} {TypeName(eventArgsType)} Ambiguous");
Console.WriteLine("\t" + string.Join(", ", found));
}
}
}
}

View File

@@ -140,6 +140,7 @@
<Compile Include="Scoping\ScopeEventDispatcherTests.cs" />
<Compile Include="Security\BackOfficeOwinUserManagerTests.cs" />
<Compile Include="Security\OwinDataProtectorTokenProviderTests.cs" />
<Compile Include="Services\ContentServicePublishBranchTests.cs" />
<Compile Include="Services\KeyValueServiceTests.cs" />
<Compile Include="Persistence\Repositories\UserRepositoryTest.cs" />
<Compile Include="TestHelpers\Entities\MockedEntity.cs" />
@@ -154,7 +155,6 @@
<Compile Include="Routing\RoutableDocumentFilterTests.cs" />
<Compile Include="Runtimes\StandaloneTests.cs" />
<Compile Include="Routing\GetContentUrlsTests.cs" />
<Compile Include="Services\ContentServicePublishBranchTests.cs" />
<Compile Include="Services\ContentServiceTagsTests.cs" />
<Compile Include="Services\ContentTypeServiceVariantsTests.cs" />
<Compile Include="Services\EntityXmlSerializerTests.cs" />