Merge branch 'v15/dev' into contrib

This commit is contained in:
Sebastiaan Janssen
2024-09-27 09:09:18 +02:00
339 changed files with 14071 additions and 1046 deletions

View File

@@ -80,7 +80,9 @@ public class ComponentTests
Options.Create(new ContentSettings()));
var eventAggregator = Mock.Of<IEventAggregator>();
var scopeProvider = new ScopeProvider(
new AmbientScopeStack(), new AmbientScopeContextStack(),Mock.Of<IDistributedLockingMechanismFactory>(),
new AmbientScopeStack(),
new AmbientScopeContextStack(),
Mock.Of<IDistributedLockingMechanismFactory>(),
f,
fs,
new TestOptionsMonitor<CoreDebugSettings>(coreDebug),
@@ -113,7 +115,7 @@ public class ComponentTests
Mock.Of<IProfiler>());
[Test]
public void Boot1A()
public async Task Boot1A()
{
var register = MockRegister();
var composition = new UmbracoBuilder(register, Mock.Of<IConfiguration>(), TestHelper.GetMockedTypeLoader());
@@ -176,9 +178,9 @@ public class ComponentTests
var components = builder.CreateCollection(factory);
Assert.IsEmpty(components);
components.Initialize();
await components.InitializeAsync(false, default);
Assert.IsEmpty(Initialized);
components.Terminate();
await components.TerminateAsync(false, default);
Assert.IsEmpty(Terminated);
}
@@ -277,7 +279,7 @@ public class ComponentTests
}
[Test]
public void Initialize()
public async Task Initialize()
{
Composed.Clear();
Initialized.Clear();
@@ -347,11 +349,11 @@ public class ComponentTests
var components = builder.CreateCollection(factory);
Assert.IsEmpty(Initialized);
components.Initialize();
await components.InitializeAsync(false, default);
AssertTypeArray(TypeArray<Component5, Component5a>(), Initialized);
Assert.IsEmpty(Terminated);
components.Terminate();
await components.TerminateAsync(false, default);
AssertTypeArray(TypeArray<Component5a, Component5>(), Terminated);
}

View File

@@ -15,7 +15,7 @@ public class ContentPickerValueConverterTests : PropertyValueConverterTests
{
private ContentPickerValueConverter CreateValueConverter(IApiContentNameProvider? nameProvider = null)
=> new ContentPickerValueConverter(
PublishedSnapshotAccessor,
PublishedContentCacheMock.Object,
new ApiContentBuilder(
nameProvider ?? new ApiContentNameProvider(),
CreateContentRouteBuilder(ApiContentPathProvider, CreateGlobalSettings()),

View File

@@ -10,7 +10,6 @@ using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.PropertyEditors.ValueConverters;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Templates;
using Umbraco.Cms.Core.Web;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.DeliveryApi;
@@ -24,7 +23,7 @@ public class MarkdownEditorValueConverterTests : PropertyValueConverterTests
[TestCase(123, "")]
public void MarkdownEditorValueConverter_ConvertsValueToMarkdownString(object inter, string expected)
{
var linkParser = new HtmlLocalLinkParser(Mock.Of<IUmbracoContextAccessor>(), Mock.Of<IPublishedUrlProvider>());
var linkParser = new HtmlLocalLinkParser(Mock.Of<IPublishedUrlProvider>());
var urlParser = new HtmlUrlParser(Mock.Of<IOptionsMonitor<ContentSettings>>(), Mock.Of<ILogger<HtmlUrlParser>>(), Mock.Of<IProfilingLogger>(), Mock.Of<IIOHelper>());
var valueConverter = new MarkdownEditorValueConverter(linkParser, urlParser);

View File

@@ -375,7 +375,7 @@ public abstract class OutputExpansionStrategyTestBase : PropertyValueConverterTe
internal PublishedElementPropertyBase CreateContentPickerProperty(IPublishedElement parent, Guid pickedContentKey, string propertyTypeAlias, IApiContentBuilder contentBuilder)
{
ContentPickerValueConverter contentPickerValueConverter = new ContentPickerValueConverter(PublishedSnapshotAccessor, contentBuilder);
ContentPickerValueConverter contentPickerValueConverter = new ContentPickerValueConverter(PublishedContentCacheMock.Object, contentBuilder);
var contentPickerPropertyType = SetupPublishedPropertyType(contentPickerValueConverter, propertyTypeAlias, Constants.PropertyEditors.Aliases.ContentPicker);
return new PublishedElementPropertyBase(contentPickerPropertyType, parent, false, PropertyCacheLevel.None, new GuidUdi(Constants.UdiEntityType.Document, pickedContentKey).ToString());

View File

@@ -135,7 +135,7 @@ public class RichTextPropertyEditorHelperTests
{
const string input = """
{
"markup": "<p>this is some markup</p><umb-rte-block data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf02\"><!--Umbraco-Block--></umb-rte-block>",
"markup": "<p>this is some markup</p><umb-rte-block data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf02\"></umb-rte-block>",
"blocks": {
"layout": {
"Umbraco.TinyMCE": [{
@@ -157,7 +157,7 @@ public class RichTextPropertyEditorHelperTests
var result = RichTextPropertyEditorHelper.TryParseRichTextEditorValue(input, JsonSerializer(), Logger(), out RichTextEditorValue? value);
Assert.IsTrue(result);
Assert.IsNotNull(value);
Assert.AreEqual("<p>this is some markup</p><umb-rte-block data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf02\"><!--Umbraco-Block--></umb-rte-block>", value.Markup);
Assert.AreEqual("<p>this is some markup</p><umb-rte-block data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf02\"></umb-rte-block>", value.Markup);
Assert.IsNotNull(value.Blocks);
@@ -172,6 +172,57 @@ public class RichTextPropertyEditorHelperTests
Assert.AreEqual(0, value.Blocks.SettingsData.Count);
}
[Test]
public void Can_Parse_Mixed_Blocks_And_Inline_Blocks()
{
const string input = """
{
"markup": "<p>this is <umb-rte-block-inline data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf03\"></umb-rte-block-inline> some markup</p><umb-rte-block data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf02\"></umb-rte-block>",
"blocks": {
"layout": {
"Umbraco.TinyMCE": [{
"contentUdi": "umb://element/36cc710ad8a645d0a07f7bbd8742cf02"
}, {
"contentUdi": "umb://element/36cc710ad8a645d0a07f7bbd8742cf03"
}
]
},
"contentData": [{
"contentTypeKey": "b2f0806c-d231-4c78-88b2-3c97d26e1123",
"udi": "umb://element/36cc710ad8a645d0a07f7bbd8742cf02",
"contentPropertyAlias": "A content property value"
}, {
"contentTypeKey": "b2f0806c-d231-4c78-88b2-3c97d26e1124",
"udi": "umb://element/36cc710ad8a645d0a07f7bbd8742cf03",
"contentPropertyAlias": "A content property value"
}
],
"settingsData": []
}
}
""";
var result = RichTextPropertyEditorHelper.TryParseRichTextEditorValue(input, JsonSerializer(), Logger(), out RichTextEditorValue? value);
Assert.IsTrue(result);
Assert.IsNotNull(value);
Assert.AreEqual("<p>this is <umb-rte-block-inline data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf03\"></umb-rte-block-inline> some markup</p><umb-rte-block data-content-udi=\"umb://element/36cc710ad8a645d0a07f7bbd8742cf02\"></umb-rte-block>", value.Markup);
Assert.IsNotNull(value.Blocks);
Guid[] contentTypeGuids = [Guid.Parse("b2f0806c-d231-4c78-88b2-3c97d26e1123"), Guid.Parse("b2f0806c-d231-4c78-88b2-3c97d26e1124")];
Guid[] itemGuids = [Guid.Parse("36cc710a-d8a6-45d0-a07f-7bbd8742cf02"), Guid.Parse("36cc710a-d8a6-45d0-a07f-7bbd8742cf03")];
Assert.AreEqual(2, value.Blocks.ContentData.Count);
for (var i = 0; i < value.Blocks.ContentData.Count; i++) {
var item = value.Blocks.ContentData[i];
Assert.AreEqual(contentTypeGuids[i], item.ContentTypeKey);
Assert.AreEqual(new GuidUdi(Constants.UdiEntityType.Element, itemGuids[i]), item.Udi);
Assert.AreEqual(itemGuids[i], item.Key);
}
Assert.AreEqual(0, value.Blocks.SettingsData.Count);
}
private IJsonSerializer JsonSerializer() => new SystemTextJsonSerializer();
private ILogger Logger() => Mock.Of<ILogger>();

View File

@@ -0,0 +1,996 @@
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services.Navigation;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Services;
[TestFixture]
public class ContentNavigationServiceBaseTests
{
private TestContentNavigationService _navigationService;
private Guid Root { get; set; }
private Guid Child1 { get; set; }
private Guid Grandchild1 { get; set; }
private Guid Grandchild2 { get; set; }
private Guid Child2 { get; set; }
private Guid Grandchild3 { get; set; }
private Guid GreatGrandchild1 { get; set; }
private Guid Child3 { get; set; }
private Guid Grandchild4 { get; set; }
[SetUp]
public void Setup()
{
// Root
// - Child 1
// - Grandchild 1
// - Grandchild 2
// - Child 2
// - Grandchild 3
// - Great-grandchild 1
// - Child 3
// - Grandchild 4
_navigationService = new TestContentNavigationService(
Mock.Of<ICoreScopeProvider>(),
Mock.Of<INavigationRepository>());
Root = new Guid("E48DD82A-7059-418E-9B82-CDD5205796CF");
_navigationService.Add(Root);
Child1 = new Guid("C6173927-0C59-4778-825D-D7B9F45D8DDE");
_navigationService.Add(Child1, Root);
Grandchild1 = new Guid("E856AC03-C23E-4F63-9AA9-681B42A58573");
_navigationService.Add(Grandchild1, Child1);
Grandchild2 = new Guid("A1B1B217-B02F-4307-862C-A5E22DB729EB");
_navigationService.Add(Grandchild2, Child1);
Child2 = new Guid("60E0E5C4-084E-4144-A560-7393BEAD2E96");
_navigationService.Add(Child2, Root);
Grandchild3 = new Guid("D63C1621-C74A-4106-8587-817DEE5FB732");
_navigationService.Add(Grandchild3, Child2);
GreatGrandchild1 = new Guid("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7");
_navigationService.Add(GreatGrandchild1, Grandchild3);
Child3 = new Guid("B606E3FF-E070-4D46-8CB9-D31352029FDF");
_navigationService.Add(Child3, Root);
Grandchild4 = new Guid("F381906C-223C-4466-80F7-B63B4EE073F8");
_navigationService.Add(Grandchild4, Child3);
}
[Test]
public void Cannot_Get_Parent_From_Non_Existing_Content_Key()
{
// Arrange
var nonExistingKey = Guid.NewGuid();
// Act
var result = _navigationService.TryGetParentKey(nonExistingKey, out Guid? parentKey);
// Assert
Assert.Multiple(() =>
{
Assert.IsFalse(result);
Assert.IsNull(parentKey);
});
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF", null)] // Root
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE", "E48DD82A-7059-418E-9B82-CDD5205796CF")] // Child 1
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573", "C6173927-0C59-4778-825D-D7B9F45D8DDE")] // Grandchild 1
[TestCase("A1B1B217-B02F-4307-862C-A5E22DB729EB", "C6173927-0C59-4778-825D-D7B9F45D8DDE")] // Grandchild 2
[TestCase("60E0E5C4-084E-4144-A560-7393BEAD2E96", "E48DD82A-7059-418E-9B82-CDD5205796CF")] // Child 2
[TestCase("D63C1621-C74A-4106-8587-817DEE5FB732", "60E0E5C4-084E-4144-A560-7393BEAD2E96")] // Grandchild 3
[TestCase("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7", "D63C1621-C74A-4106-8587-817DEE5FB732")] // Great-grandchild 1
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF", "E48DD82A-7059-418E-9B82-CDD5205796CF")] // Child 3
[TestCase("F381906C-223C-4466-80F7-B63B4EE073F8", "B606E3FF-E070-4D46-8CB9-D31352029FDF")] // Grandchild 4
public void Can_Get_Parent_From_Existing_Content_Key(Guid childKey, Guid? expectedParentKey)
{
// Act
var result = _navigationService.TryGetParentKey(childKey, out Guid? parentKey);
// Assert
Assert.Multiple(() =>
{
Assert.IsTrue(result);
if (expectedParentKey is null)
{
Assert.IsNull(parentKey);
}
else
{
Assert.IsNotNull(parentKey);
Assert.AreEqual(expectedParentKey, parentKey);
}
});
}
[Test]
public void Cannot_Get_Children_From_Non_Existing_Content_Key()
{
// Arrange
var nonExistingKey = Guid.NewGuid();
// Act
var result = _navigationService.TryGetChildrenKeys(nonExistingKey, out IEnumerable<Guid> childrenKeys);
// Assert
Assert.Multiple(() =>
{
Assert.IsFalse(result);
Assert.IsEmpty(childrenKeys);
});
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF", 3)] // Root - Child 1, Child 2, Child 3
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE", 2)] // Child 1 - Grandchild 1, Grandchild 2
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573", 0)] // Grandchild 1
[TestCase("A1B1B217-B02F-4307-862C-A5E22DB729EB", 0)] // Grandchild 2
[TestCase("60E0E5C4-084E-4144-A560-7393BEAD2E96", 1)] // Child 2 - Grandchild 3
[TestCase("D63C1621-C74A-4106-8587-817DEE5FB732", 1)] // Grandchild 3 - Great-grandchild 1
[TestCase("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7", 0)] // Great-grandchild 1
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF", 1)] // Child 3 - Grandchild 4
[TestCase("F381906C-223C-4466-80F7-B63B4EE073F8", 0)] // Grandchild 4
public void Can_Get_Children_From_Existing_Content_Key(Guid parentKey, int childrenCount)
{
// Act
var result = _navigationService.TryGetChildrenKeys(parentKey, out IEnumerable<Guid> childrenKeys);
// Assert
Assert.Multiple(() =>
{
Assert.IsTrue(result);
Assert.AreEqual(childrenCount, childrenKeys.Count());
});
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF",
new[]
{
"C6173927-0C59-4778-825D-D7B9F45D8DDE", "60E0E5C4-084E-4144-A560-7393BEAD2E96",
"B606E3FF-E070-4D46-8CB9-D31352029FDF"
})] // Root
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE",
new[] { "E856AC03-C23E-4F63-9AA9-681B42A58573", "A1B1B217-B02F-4307-862C-A5E22DB729EB" })] // Child 1
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573", new string[0])] // Grandchild 1
[TestCase("60E0E5C4-084E-4144-A560-7393BEAD2E96", new[] { "D63C1621-C74A-4106-8587-817DEE5FB732" })] // Child 2
[TestCase("D63C1621-C74A-4106-8587-817DEE5FB732", new[] { "56E29EA9-E224-4210-A59F-7C2C5C0C5CC7" })] // Grandchild 3
[TestCase("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7", new string[0])] // Great-grandchild 1
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF", new[] { "F381906C-223C-4466-80F7-B63B4EE073F8" })] // Child 3
public void Can_Get_Children_From_Existing_Content_Key_In_Correct_Order(Guid parentKey, string[] children)
{
// Arrange
Guid[] expectedChildren = Array.ConvertAll(children, Guid.Parse);
// Act
_navigationService.TryGetChildrenKeys(parentKey, out IEnumerable<Guid> childrenKeys);
List<Guid> childrenList = childrenKeys.ToList();
// Assert
for (var i = 0; i < expectedChildren.Length; i++)
{
Assert.AreEqual(expectedChildren[i], childrenList.ElementAt(i));
}
}
[Test]
public void Cannot_Get_Descendants_From_Non_Existing_Content_Key()
{
// Arrange
var nonExistingKey = Guid.NewGuid();
// Act
var result = _navigationService.TryGetDescendantsKeys(nonExistingKey, out IEnumerable<Guid> descendantsKeys);
// Assert
Assert.Multiple(() =>
{
Assert.IsFalse(result);
Assert.IsEmpty(descendantsKeys);
});
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF",
8)] // Root - Child 1, Grandchild 1, Grandchild 2, Child 2, Grandchild 3, Great-grandchild 1, Child 3, Grandchild 4
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE", 2)] // Child 1 - Grandchild 1, Grandchild 2
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573", 0)] // Grandchild 1
[TestCase("A1B1B217-B02F-4307-862C-A5E22DB729EB", 0)] // Grandchild 2
[TestCase("60E0E5C4-084E-4144-A560-7393BEAD2E96", 2)] // Child 2 - Grandchild 3, Great-grandchild 1
[TestCase("D63C1621-C74A-4106-8587-817DEE5FB732", 1)] // Grandchild 3 - Great-grandchild 1
[TestCase("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7", 0)] // Great-grandchild 1
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF", 1)] // Child 3 - Grandchild 4
[TestCase("F381906C-223C-4466-80F7-B63B4EE073F8", 0)] // Grandchild 4
public void Can_Get_Descendants_From_Existing_Content_Key(Guid parentKey, int descendantsCount)
{
// Act
var result = _navigationService.TryGetDescendantsKeys(parentKey, out IEnumerable<Guid> descendantsKeys);
// Assert
Assert.Multiple(() =>
{
Assert.IsTrue(result);
Assert.AreEqual(descendantsCount, descendantsKeys.Count());
});
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF",
new[]
{
"C6173927-0C59-4778-825D-D7B9F45D8DDE", "E856AC03-C23E-4F63-9AA9-681B42A58573",
"A1B1B217-B02F-4307-862C-A5E22DB729EB", "60E0E5C4-084E-4144-A560-7393BEAD2E96",
"D63C1621-C74A-4106-8587-817DEE5FB732", "56E29EA9-E224-4210-A59F-7C2C5C0C5CC7",
"B606E3FF-E070-4D46-8CB9-D31352029FDF", "F381906C-223C-4466-80F7-B63B4EE073F8"
})] // Root
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE",
new[] { "E856AC03-C23E-4F63-9AA9-681B42A58573", "A1B1B217-B02F-4307-862C-A5E22DB729EB" })] // Child 1
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573", new string[0])] // Grandchild 1
[TestCase("60E0E5C4-084E-4144-A560-7393BEAD2E96",
new[] { "D63C1621-C74A-4106-8587-817DEE5FB732", "56E29EA9-E224-4210-A59F-7C2C5C0C5CC7" })] // Child 2
[TestCase("D63C1621-C74A-4106-8587-817DEE5FB732", new[] { "56E29EA9-E224-4210-A59F-7C2C5C0C5CC7" })] // Grandchild 3
[TestCase("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7", new string[0])] // Great-grandchild 1
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF", new[] { "F381906C-223C-4466-80F7-B63B4EE073F8" })] // Child 3
public void Can_Get_Descendants_From_Existing_Content_Key_In_Correct_Order(Guid parentKey, string[] descendants)
{
// Arrange
Guid[] expectedDescendants = Array.ConvertAll(descendants, Guid.Parse);
// Act
_navigationService.TryGetDescendantsKeys(parentKey, out IEnumerable<Guid> descendantsKeys);
List<Guid> descendantsList = descendantsKeys.ToList();
// Assert
for (var i = 0; i < expectedDescendants.Length; i++)
{
Assert.AreEqual(expectedDescendants[i], descendantsList.ElementAt(i));
}
}
[Test]
public void Cannot_Get_Ancestors_From_Non_Existing_Content_Key()
{
// Arrange
var nonExistingKey = Guid.NewGuid();
// Act
var result = _navigationService.TryGetAncestorsKeys(nonExistingKey, out IEnumerable<Guid> ancestorsKeys);
// Assert
Assert.Multiple(() =>
{
Assert.IsFalse(result);
Assert.IsEmpty(ancestorsKeys);
});
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF", 0)] // Root
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE", 1)] // Child 1 - Root
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573", 2)] // Grandchild 1 - Child 1, Root
[TestCase("A1B1B217-B02F-4307-862C-A5E22DB729EB", 2)] // Grandchild 2 - Child 1, Root
[TestCase("60E0E5C4-084E-4144-A560-7393BEAD2E96", 1)] // Child 2 - Root
[TestCase("D63C1621-C74A-4106-8587-817DEE5FB732", 2)] // Grandchild 3 - Child 2, Root
[TestCase("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7", 3)] // Great-grandchild 1 - Grandchild 3, Child 2, Root
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF", 1)] // Child 3 - Root
[TestCase("F381906C-223C-4466-80F7-B63B4EE073F8", 2)] // Grandchild 4 - Child 3, Root
public void Can_Get_Ancestors_From_Existing_Content_Key(Guid childKey, int ancestorsCount)
{
// Act
var result = _navigationService.TryGetAncestorsKeys(childKey, out IEnumerable<Guid> ancestorsKeys);
// Assert
Assert.Multiple(() =>
{
Assert.IsTrue(result);
Assert.AreEqual(ancestorsCount, ancestorsKeys.Count());
});
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF", new string[0])] // Root
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE", new[] { "E48DD82A-7059-418E-9B82-CDD5205796CF" })] // Child 1
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573",
new[] { "C6173927-0C59-4778-825D-D7B9F45D8DDE", "E48DD82A-7059-418E-9B82-CDD5205796CF" })] // Grandchild 1
[TestCase("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7",
new[]
{
"D63C1621-C74A-4106-8587-817DEE5FB732", "60E0E5C4-084E-4144-A560-7393BEAD2E96",
"E48DD82A-7059-418E-9B82-CDD5205796CF"
})] // Great-grandchild 1
public void Can_Get_Ancestors_From_Existing_Content_Key_In_Correct_Order(Guid childKey, string[] ancestors)
{
// Arrange
Guid[] expectedAncestors = Array.ConvertAll(ancestors, Guid.Parse);
// Act
_navigationService.TryGetAncestorsKeys(childKey, out IEnumerable<Guid> ancestorsKeys);
List<Guid> ancestorsList = ancestorsKeys.ToList();
// Assert
for (var i = 0; i < expectedAncestors.Length; i++)
{
Assert.AreEqual(expectedAncestors[i], ancestorsList.ElementAt(i));
}
}
[Test]
public void Cannot_Get_Siblings_Of_Non_Existing_Content_Key()
{
// Arrange
var nonExistingKey = Guid.NewGuid();
// Act
var result = _navigationService.TryGetSiblingsKeys(nonExistingKey, out IEnumerable<Guid> siblingsKeys);
// Assert
Assert.Multiple(() =>
{
Assert.IsFalse(result);
Assert.IsEmpty(siblingsKeys);
});
}
[Test]
public void Can_Get_Siblings_Of_Existing_Content_Key_Without_Self()
{
// Arrange
Guid nodeKey = Child1;
// Act
var result = _navigationService.TryGetSiblingsKeys(nodeKey, out IEnumerable<Guid> siblingsKeys);
List<Guid> siblingsList = siblingsKeys.ToList();
// Assert
Assert.Multiple(() =>
{
Assert.IsTrue(result);
Assert.IsNotEmpty(siblingsList);
Assert.IsFalse(siblingsList.Contains(nodeKey));
});
}
[Test]
public void Can_Get_Siblings_Of_Existing_Content_Key_At_Content_Root()
{
// Arrange
Guid anotherRoot = new Guid("716380B9-DAA9-4930-A461-95EF39EBAB41");
_navigationService.Add(anotherRoot);
// Act
_navigationService.TryGetSiblingsKeys(anotherRoot, out IEnumerable<Guid> siblingsKeys);
List<Guid> siblingsList = siblingsKeys.ToList();
// Assert
Assert.Multiple(() =>
{
Assert.IsNotEmpty(siblingsList);
Assert.AreEqual(1, siblingsList.Count);
Assert.AreEqual(Root, siblingsList.First());
});
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF", 0)] // Root
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE", 2)] // Child 1 - Child 2, Child 3
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573", 1)] // Grandchild 1 - Grandchild 2
[TestCase("A1B1B217-B02F-4307-862C-A5E22DB729EB", 1)] // Grandchild 2 - Grandchild 1
[TestCase("60E0E5C4-084E-4144-A560-7393BEAD2E96", 2)] // Child 2 - Child 1, Child 3
[TestCase("D63C1621-C74A-4106-8587-817DEE5FB732", 0)] // Grandchild 3
[TestCase("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7", 0)] // Great-grandchild 1
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF", 2)] // Child 3 - Child 1, Child 2
[TestCase("F381906C-223C-4466-80F7-B63B4EE073F8", 0)] // Grandchild 4
public void Can_Get_Siblings_Of_Existing_Content_Key(Guid key, int siblingsCount)
{
// Act
var result = _navigationService.TryGetSiblingsKeys(key, out IEnumerable<Guid> siblingsKeys);
// Assert
Assert.Multiple(() =>
{
Assert.IsTrue(result);
Assert.AreEqual(siblingsCount, siblingsKeys.Count());
});
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF", new string[0])] // Root
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE", new[] { "60E0E5C4-084E-4144-A560-7393BEAD2E96", "B606E3FF-E070-4D46-8CB9-D31352029FDF" })] // Child 1 - Child 2, Child 3
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573", new[] { "A1B1B217-B02F-4307-862C-A5E22DB729EB" })] // Grandchild 1 - Grandchild 2
public void Can_Get_Siblings_Of_Existing_Content_Key_In_Correct_Order(Guid childKey, string[] siblings)
{
// Arrange
Guid[] expectedSiblings = Array.ConvertAll(siblings, Guid.Parse);
// Act
_navigationService.TryGetSiblingsKeys(childKey, out IEnumerable<Guid> siblingsKeys);
List<Guid> siblingsList = siblingsKeys.ToList();
// Assert
for (var i = 0; i < expectedSiblings.Length; i++)
{
Assert.AreEqual(expectedSiblings[i], siblingsList.ElementAt(i));
}
}
[Test]
public void Cannot_Move_Node_To_Bin_When_Non_Existing_Content_Key()
{
// Arrange
var nonExistingKey = Guid.NewGuid();
// Act
var result = _navigationService.MoveToBin(nonExistingKey);
// Assert
Assert.IsFalse(result);
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF")] // Root
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE")] // Child 1
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573")] // Grandchild 1
[TestCase("A1B1B217-B02F-4307-862C-A5E22DB729EB")] // Grandchild 2
[TestCase("60E0E5C4-084E-4144-A560-7393BEAD2E96")] // Child 2
[TestCase("D63C1621-C74A-4106-8587-817DEE5FB732")] // Grandchild 3
[TestCase("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7")] // Great-grandchild 1
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF")] // Child 3
[TestCase("F381906C-223C-4466-80F7-B63B4EE073F8")] // Grandchild 4
public void Can_Move_Node_To_Bin(Guid keyOfNodeToRemove)
{
// Act
var result = _navigationService.MoveToBin(keyOfNodeToRemove);
// Assert
Assert.IsTrue(result);
var nodeExists = _navigationService.TryGetParentKey(keyOfNodeToRemove, out Guid? parentKey);
Assert.Multiple(() =>
{
Assert.IsFalse(nodeExists);
Assert.IsNull(parentKey);
});
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF")] // Root
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE")] // Child 1
[TestCase("F381906C-223C-4466-80F7-B63B4EE073F8")] // Grandchild 4
public void Moving_Node_To_Bin_Removes_Its_Descendants_As_Well(Guid keyOfNodeToRemove)
{
// Arrange
_navigationService.TryGetDescendantsKeys(keyOfNodeToRemove, out IEnumerable<Guid> initialDescendantsKeys);
// Act
var result = _navigationService.MoveToBin(keyOfNodeToRemove);
// Assert
Assert.IsTrue(result);
_navigationService.TryGetDescendantsKeys(keyOfNodeToRemove, out IEnumerable<Guid> descendantsKeys);
Assert.Multiple(() =>
{
Assert.AreEqual(0, descendantsKeys.Count());
foreach (Guid descendant in initialDescendantsKeys)
{
var descendantExists = _navigationService.TryGetParentKey(descendant, out _);
Assert.IsFalse(descendantExists);
}
});
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF")] // Root
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE")] // Child 1
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573")] // Grandchild 1
[TestCase("A1B1B217-B02F-4307-862C-A5E22DB729EB")] // Grandchild 2
[TestCase("60E0E5C4-084E-4144-A560-7393BEAD2E96")] // Child 2
[TestCase("D63C1621-C74A-4106-8587-817DEE5FB732")] // Grandchild 3
[TestCase("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7")] // Great-grandchild 1
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF")] // Child 3
[TestCase("F381906C-223C-4466-80F7-B63B4EE073F8")] // Grandchild 4
public void Moving_Node_To_Bin_Adds_It_To_Recycle_Bin_Root(Guid keyOfNodeToRemove)
{
// Act
_navigationService.MoveToBin(keyOfNodeToRemove);
// Assert
var nodeExistsInBin = _navigationService.TryGetParentKeyInBin(keyOfNodeToRemove, out Guid? parentKeyInBin);
Assert.Multiple(() =>
{
Assert.IsTrue(nodeExistsInBin);
Assert.IsNull(parentKeyInBin);
});
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF")] // Root
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE")] // Child 1
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF")] // Child 3
[TestCase("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7")] // Great-grandchild 1
public void Moving_Node_To_Bin_Adds_Its_Descendants_To_Recycle_Bin_As_Well(Guid keyOfNodeToRemove)
{
// Arrange
_navigationService.TryGetDescendantsKeys(keyOfNodeToRemove, out IEnumerable<Guid> initialDescendantsKeys);
List<Guid> initialDescendantsList = initialDescendantsKeys.ToList();
// Act
_navigationService.MoveToBin(keyOfNodeToRemove);
// Assert
var nodeExistsInBin = _navigationService.TryGetDescendantsKeysInBin(keyOfNodeToRemove, out IEnumerable<Guid> descendantsKeysInBin);
Assert.Multiple(() =>
{
Assert.IsTrue(nodeExistsInBin);
CollectionAssert.AreEqual(initialDescendantsList, descendantsKeysInBin);
foreach (Guid descendant in initialDescendantsList)
{
_navigationService.TryGetParentKeyInBin(descendant, out Guid? parentKeyInBin);
Assert.IsNotNull(parentKeyInBin); // The descendant kept its initial parent
}
});
}
[Test]
public void Cannot_Add_Node_When_Parent_Does_Not_Exist()
{
// Arrange
var newNodeKey = Guid.NewGuid();
var nonExistentParentKey = Guid.NewGuid();
// Act
var result = _navigationService.Add(newNodeKey, nonExistentParentKey);
// Assert
Assert.IsFalse(result);
}
[Test]
public void Cannot_Add_When_Node_With_The_Same_Key_Already_Exists()
{
// Act
var result = _navigationService.Add(Child1);
// Assert
Assert.IsFalse(result);
}
[Test]
public void Can_Add_Node_To_Content_Root()
{
// Arrange
var newNodeKey = Guid.NewGuid();
// Act
var result = _navigationService.Add(newNodeKey); // parentKey is null
// Assert
Assert.IsTrue(result);
var nodeExists = _navigationService.TryGetParentKey(newNodeKey, out Guid? parentKey);
Assert.Multiple(() =>
{
Assert.IsTrue(nodeExists);
Assert.IsNull(parentKey);
});
}
[Test]
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE")] // Child 1
[TestCase("F381906C-223C-4466-80F7-B63B4EE073F8")] // Grandchild 4
[TestCase("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7")] // Great-grandchild 1
public void Can_Add_Node_To_Parent(Guid parentKey)
{
// Arrange
var newNodeKey = Guid.NewGuid();
_navigationService.TryGetChildrenKeys(parentKey, out IEnumerable<Guid> currentChildrenKeys);
var currentChildrenCount = currentChildrenKeys.Count();
// Act
var result = _navigationService.Add(newNodeKey, parentKey);
// Assert
Assert.IsTrue(result);
_navigationService.TryGetChildrenKeys(parentKey, out IEnumerable<Guid> newChildrenKeys);
var newChildrenList = newChildrenKeys.ToList();
Assert.Multiple(() =>
{
Assert.AreEqual(currentChildrenCount + 1, newChildrenList.Count);
Assert.IsTrue(newChildrenList.Any(childKey => childKey == newNodeKey));
});
}
[Test]
public void Cannot_Move_Node_When_Target_Parent_Does_Not_Exist()
{
// Arrange
Guid nodeToMove = Child1;
var nonExistentTargetParentKey = Guid.NewGuid();
// Act
var result = _navigationService.Move(nodeToMove, nonExistentTargetParentKey);
// Assert
Assert.IsFalse(result);
}
[Test]
public void Cannot_Move_Node_That_Does_Not_Exist()
{
// Arrange
var nonExistentNodeKey = Guid.NewGuid();
Guid targetParentKey = Child1;
// Act
var result = _navigationService.Move(nonExistentNodeKey, targetParentKey);
// Assert
Assert.IsFalse(result);
}
[Test]
public void Cannot_Move_Node_To_Itself()
{
// Arrange
Guid nodeToMove = Child1;
// Act
var result = _navigationService.Move(nodeToMove, nodeToMove);
// Assert
Assert.IsFalse(result);
}
[Test]
public void Can_Move_Node_To_Content_Root()
{
// Arrange
Guid nodeToMove = Child1;
// Act
var result = _navigationService.Move(nodeToMove); // parentKey is null
// Assert
Assert.IsTrue(result);
// Verify the node's new parent is null (moved to content root)
_navigationService.TryGetParentKey(nodeToMove, out Guid? newParentKey);
Assert.IsNull(newParentKey);
}
[Test]
public void Can_Move_Node_To_Existing_Target_Parent()
{
// Arrange
Guid nodeToMove = Grandchild4;
Guid targetParentKey = Child1;
// Act
var result = _navigationService.Move(nodeToMove, targetParentKey);
// Assert
Assert.IsTrue(result);
// Verify the node's new parent is updated
_navigationService.TryGetParentKey(nodeToMove, out Guid? newParentKey);
Assert.Multiple(() =>
{
Assert.IsNotNull(newParentKey);
Assert.AreEqual(targetParentKey, newParentKey);
});
}
[Test]
public void Moved_Node_Has_Updated_Parent()
{
// Arrange
Guid nodeToMove = Grandchild1;
Guid targetParentKey = Child2;
_navigationService.TryGetParentKey(nodeToMove, out Guid? oldParentKey);
// Act
var result = _navigationService.Move(nodeToMove, targetParentKey);
// Assert
Assert.IsTrue(result);
// Verify the node's new parent is updated
_navigationService.TryGetParentKey(nodeToMove, out Guid? newParentKey);
Assert.Multiple(() =>
{
Assert.IsNotNull(newParentKey);
Assert.AreEqual(targetParentKey, newParentKey);
// Verify that the new parent is different from the old one
Assert.AreNotEqual(oldParentKey, targetParentKey);
});
}
[Test]
public void Moved_Node_Is_Removed_From_Its_Current_Parent()
{
// Arrange
Guid nodeToMove = Grandchild3;
Guid targetParentKey = Child3;
_navigationService.TryGetParentKey(nodeToMove, out Guid? oldParentKey);
_navigationService.TryGetChildrenKeys(oldParentKey!.Value, out IEnumerable<Guid> oldParentChildrenKeys);
var oldParentChildrenCount = oldParentChildrenKeys.Count();
// Act
var result = _navigationService.Move(nodeToMove, targetParentKey);
// Assert
Assert.IsTrue(result);
// Verify the node is removed from its old parent's children list
_navigationService.TryGetChildrenKeys(oldParentKey.Value, out IEnumerable<Guid> childrenKeys);
List<Guid> childrenList = childrenKeys.ToList();
Assert.Multiple(() =>
{
CollectionAssert.DoesNotContain(childrenList, nodeToMove);
Assert.AreEqual(oldParentChildrenCount - 1, childrenList.Count);
});
}
[Test]
public void Moved_Node_Is_Added_To_Its_New_Parent()
{
// Arrange
Guid nodeToMove = Grandchild2;
Guid targetParentKey = Child2;
_navigationService.TryGetChildrenKeys(targetParentKey, out IEnumerable<Guid> targetParentChildrenKeys);
var targetParentChildrenCount = targetParentChildrenKeys.Count();
// Act
var result = _navigationService.Move(nodeToMove, targetParentKey);
// Assert
Assert.IsTrue(result);
// Verify the node is added to its new parent's children list
_navigationService.TryGetChildrenKeys(targetParentKey, out IEnumerable<Guid> childrenKeys);
List<Guid> childrenList = childrenKeys.ToList();
Assert.Multiple(() =>
{
CollectionAssert.Contains(childrenList, nodeToMove);
Assert.AreEqual(targetParentChildrenCount + 1, childrenList.Count);
});
}
[Test]
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573", "60E0E5C4-084E-4144-A560-7393BEAD2E96", 0)] // Grandchild 1 to Child 2
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF", null, 1)] // Child 3 to content root
[TestCase("60E0E5C4-084E-4144-A560-7393BEAD2E96", "C6173927-0C59-4778-825D-D7B9F45D8DDE", 2)] // Child 2 to Child 1
public void Moved_Node_Has_The_Same_Amount_Of_Descendants(Guid nodeToMove, Guid? targetParentKey, int initialDescendantsCount)
{
// Act
var result = _navigationService.Move(nodeToMove, targetParentKey);
// Assert
Assert.IsTrue(result);
// Verify that the number of descendants remain the same after moving the node
_navigationService.TryGetDescendantsKeys(nodeToMove, out IEnumerable<Guid> descendantsKeys);
var descendantsCountAfterMove = descendantsKeys.Count();
Assert.AreEqual(initialDescendantsCount, descendantsCountAfterMove);
}
[Test]
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF", "A1B1B217-B02F-4307-862C-A5E22DB729EB", 0)] // Child 3 to Grandchild 2
[TestCase("60E0E5C4-084E-4144-A560-7393BEAD2E96", "B606E3FF-E070-4D46-8CB9-D31352029FDF", 1)] // Child 2 to Child 3
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573", "60E0E5C4-084E-4144-A560-7393BEAD2E96", 2)] // Grandchild 1 to Child 2
public void Number_Of_Target_Parent_Descendants_Updates_When_Moving_Node_With_Descendants(Guid nodeToMove, Guid targetParentKey, int initialDescendantsCountOfTargetParent)
{
// Arrange
// Get the number of descendants of the node to move
_navigationService.TryGetDescendantsKeys(nodeToMove, out IEnumerable<Guid> descendantsKeys);
var descendantsCountOfNodeToMove = descendantsKeys.Count();
// Act
var result = _navigationService.Move(nodeToMove, targetParentKey);
// Assert
Assert.IsTrue(result);
_navigationService.TryGetDescendantsKeys(targetParentKey, out IEnumerable<Guid> updatedTargetParentDescendantsKeys);
var updatedDescendantsCountOfTargetParent = updatedTargetParentDescendantsKeys.Count();
// Verify the number of descendants of the target parent has increased by the number of descendants of the moved node plus the node itself
Assert.AreEqual(initialDescendantsCountOfTargetParent + descendantsCountOfNodeToMove + 1, updatedDescendantsCountOfTargetParent);
}
[Test]
public void Cannot_Restore_Node_When_Target_Parent_Does_Not_Exist()
{
// Arrange
Guid nodeToRestore = Grandchild1;
var nonExistentTargetParentKey = Guid.NewGuid();
_navigationService.MoveToBin(nodeToRestore);
// Act
var result = _navigationService.RestoreFromBin(nodeToRestore, nonExistentTargetParentKey);
// Assert
Assert.IsFalse(result);
}
[Test]
public void Cannot_Restore_Node_That_Does_Not_Exist()
{
// Arrange
Guid notDeletedNodeKey = Grandchild4;
Guid targetParentKey = Child3;
// Act
var result = _navigationService.RestoreFromBin(notDeletedNodeKey, targetParentKey);
// Assert
Assert.IsFalse(result);
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF", null)] // Root
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE", "E48DD82A-7059-418E-9B82-CDD5205796CF")] // Child 1
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573", "C6173927-0C59-4778-825D-D7B9F45D8DDE")] // Grandchild 1
[TestCase("A1B1B217-B02F-4307-862C-A5E22DB729EB", "C6173927-0C59-4778-825D-D7B9F45D8DDE")] // Grandchild 2
[TestCase("60E0E5C4-084E-4144-A560-7393BEAD2E96", "E48DD82A-7059-418E-9B82-CDD5205796CF")] // Child 2
[TestCase("D63C1621-C74A-4106-8587-817DEE5FB732", "60E0E5C4-084E-4144-A560-7393BEAD2E96")] // Grandchild 3
[TestCase("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7", "D63C1621-C74A-4106-8587-817DEE5FB732")] // Great-grandchild 1
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF", "E48DD82A-7059-418E-9B82-CDD5205796CF")] // Child 3
[TestCase("F381906C-223C-4466-80F7-B63B4EE073F8", "B606E3FF-E070-4D46-8CB9-D31352029FDF")] // Grandchild 4
public void Can_Restore_Node_To_Existing_Target_Parent(Guid nodeToRestore, Guid? targetParentKey)
{
// Arrange
_navigationService.MoveToBin(nodeToRestore);
// Act
var result = _navigationService.RestoreFromBin(nodeToRestore, targetParentKey);
// Assert
Assert.IsTrue(result);
// Verify the node's new parent is updated
_navigationService.TryGetParentKey(nodeToRestore, out Guid? parentKeyAfterRestore);
Assert.Multiple(() =>
{
if (targetParentKey is null)
{
Assert.IsNull(parentKeyAfterRestore);
}
else
{
Assert.IsNotNull(parentKeyAfterRestore);
}
Assert.AreEqual(targetParentKey, parentKeyAfterRestore);
});
}
[Test]
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573", "C6173927-0C59-4778-825D-D7B9F45D8DDE")] // Grandchild 1 to Child 1
[TestCase("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7", "60E0E5C4-084E-4144-A560-7393BEAD2E96")] // Great-grandchild 1 to Child 2
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF", "E48DD82A-7059-418E-9B82-CDD5205796CF")] // Child 3 to Root
public void Restored_Node_Is_Added_To_Its_Target_Parent(Guid nodeToRestore, Guid targetParentKey)
{
// Arrange
_navigationService.MoveToBin(nodeToRestore);
_navigationService.TryGetChildrenKeys(targetParentKey, out IEnumerable<Guid> targetParentChildrenKeys);
var targetParentChildrenCount = targetParentChildrenKeys.Count();
// Act
var result = _navigationService.RestoreFromBin(nodeToRestore, targetParentKey);
// Assert
Assert.IsTrue(result);
// Verify the node is added to its target parent's children list
_navigationService.TryGetChildrenKeys(targetParentKey, out IEnumerable<Guid> childrenKeys);
List<Guid> childrenList = childrenKeys.ToList();
Assert.Multiple(() =>
{
CollectionAssert.Contains(childrenList, nodeToRestore);
Assert.AreEqual(targetParentChildrenCount + 1, childrenList.Count);
});
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF")] // Root
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE")] // Child 1
[TestCase("D63C1621-C74A-4106-8587-817DEE5FB732")] // Grandchild 3
[TestCase("F381906C-223C-4466-80F7-B63B4EE073F8")] // Grandchild 4
public void Restored_Node_And_Its_Descendants_Are_Removed_From_Bin(Guid nodeToRestore)
{
// Arrange
_navigationService.MoveToBin(nodeToRestore);
_navigationService.TryGetDescendantsKeysInBin(nodeToRestore, out IEnumerable<Guid> descendantsKeysInBin);
// Act
_navigationService.RestoreFromBin(nodeToRestore);
// Assert
var nodeExistsInBin = _navigationService.TryGetParentKeyInBin(nodeToRestore, out Guid? parentKeyInBinAfterRestore);
Assert.Multiple(() =>
{
Assert.IsFalse(nodeExistsInBin);
Assert.IsNull(parentKeyInBinAfterRestore);
foreach (Guid descendant in descendantsKeysInBin)
{
var descendantExistsInBin = _navigationService.TryGetParentKeyInBin(descendant, out _);
Assert.IsFalse(descendantExistsInBin);
}
});
}
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF", null, 8)] // Root to content root
[TestCase("C6173927-0C59-4778-825D-D7B9F45D8DDE", "56E29EA9-E224-4210-A59F-7C2C5C0C5CC7", 2)] // Child 1 to Great-grandchild 1
[TestCase("F381906C-223C-4466-80F7-B63B4EE073F8", "60E0E5C4-084E-4144-A560-7393BEAD2E96", 0)] // Grandchild 4 to Child 2
public void Restored_Node_Has_The_Same_Amount_Of_Descendants(Guid nodeToRestore, Guid? targetParentKey, int initialDescendantsCount)
{
// Arrange
_navigationService.MoveToBin(nodeToRestore);
// Act
_navigationService.RestoreFromBin(nodeToRestore, targetParentKey);
// Assert
// Verify that the number of descendants remain the same after restoring the node
_navigationService.TryGetDescendantsKeys(nodeToRestore, out IEnumerable<Guid> restoredDescendantsKeys);
var descendantsCountAfterRestore = restoredDescendantsKeys.Count();
Assert.AreEqual(initialDescendantsCount, descendantsCountAfterRestore);
}
}
internal class TestContentNavigationService : ContentNavigationServiceBase
{
public TestContentNavigationService(ICoreScopeProvider coreScopeProvider, INavigationRepository navigationRepository)
: base(coreScopeProvider, navigationRepository)
{
}
// Not needed for testing here
public override Task RebuildAsync() => Task.CompletedTask;
// Not needed for testing here
public override Task RebuildBinAsync() => Task.CompletedTask;
}

View File

@@ -88,7 +88,7 @@ public class StringExtensionsTests
[TestCase("Hello this is my string string", "Hello this is my string string", "")]
public void TrimEnd(string input, string forTrimming, string shouldBe)
{
var trimmed = input.TrimEnd(forTrimming);
var trimmed = input.TrimEndExact(forTrimming);
Assert.AreEqual(shouldBe, trimmed);
}
@@ -98,7 +98,7 @@ public class StringExtensionsTests
[TestCase("Hello this is my string", "Hello this is my string", "")]
public void TrimStart(string input, string forTrimming, string shouldBe)
{
var trimmed = input.TrimStart(forTrimming);
var trimmed = input.TrimStartExact(forTrimming);
Assert.AreEqual(shouldBe, trimmed);
}

View File

@@ -29,9 +29,8 @@ public class HtmlLocalLinkParserTests
</p><p><img src='/media/234234.jpg' data-udi=""umb://media-type/B726D735E4C446D58F703F3FBCFC97A5"" />
<a type=""media"" href=""/{localLink:7e21a725-b905-4c5f-86dc-8c41ec116e39}"" title=""media"">media</a>
</p>";
var umbracoContextAccessor = new TestUmbracoContextAccessor();
var parser = new HtmlLocalLinkParser(umbracoContextAccessor, Mock.Of<IPublishedUrlProvider>());
var parser = new HtmlLocalLinkParser(Mock.Of<IPublishedUrlProvider>());
var result = parser.FindUdisFromLocalLinks(input).ToList();
@@ -56,8 +55,7 @@ public class HtmlLocalLinkParserTests
<a href=""{locallink:umb://document-type/2D692FCB070B4CDA92FB6883FDBFD6E2}"">hello</a>
</p>";
var umbracoContextAccessor = new TestUmbracoContextAccessor();
var parser = new HtmlLocalLinkParser(umbracoContextAccessor, Mock.Of<IPublishedUrlProvider>());
var parser = new HtmlLocalLinkParser(Mock.Of<IPublishedUrlProvider>());
var result = parser.FindUdisFromLocalLinks(input).ToList();
@@ -90,8 +88,7 @@ public class HtmlLocalLinkParserTests
<a type=""media"" href=""/{localLink:7e21a725-b905-4c5f-86dc-8c41ec116e39}"" title=""media"">media</a>
</p>";
var umbracoContextAccessor = new TestUmbracoContextAccessor();
var parser = new HtmlLocalLinkParser(umbracoContextAccessor, Mock.Of<IPublishedUrlProvider>());
var parser = new HtmlLocalLinkParser(Mock.Of<IPublishedUrlProvider>());
var result = parser.FindUdisFromLocalLinks(input).ToList();
@@ -204,7 +201,7 @@ public class HtmlLocalLinkParserTests
mediaCache.Setup(x => x.GetById(It.IsAny<int>())).Returns(media.Object);
mediaCache.Setup(x => x.GetById(It.IsAny<Guid>())).Returns(media.Object);
var linkParser = new HtmlLocalLinkParser(umbracoContextAccessor, publishedUrlProvider);
var linkParser = new HtmlLocalLinkParser(publishedUrlProvider);
var output = linkParser.EnsureInternalLinks(input);

View File

@@ -203,7 +203,7 @@ public class NPocoSqlTemplateTests
.Append("AND whatever=@0", SqlTemplate.Arg("k")));
sql = template.Sql(new { j = new[] { 1, 2, 3 }, k = "oops" });
Assert.AreEqual(sqlBase.TrimEnd("WHERE ") + "AND whatever=@0,@1,@2 AND whatever=@3", sql.SQL.NoCrLf());
Assert.AreEqual(sqlBase.TrimEndExact("WHERE ") + "AND whatever=@0,@1,@2 AND whatever=@3", sql.SQL.NoCrLf());
Assert.AreEqual(4, sql.Arguments.Length);
Assert.AreEqual(1, sql.Arguments[0]);
Assert.AreEqual(2, sql.Arguments[1]);

View File

@@ -0,0 +1,117 @@
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services.Navigation;
using Umbraco.Cms.Infrastructure.HybridCache.SeedKeyProviders.Document;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.PublishedCache.HybridCache;
[TestFixture]
public class DocumentBreadthFirstKeyProviderTests
{
[Test]
public void ZeroSeedCountReturnsZeroKeys()
{
// The structure here doesn't matter greatly, it just matters that there is something.
var navigationQueryService = new Mock<IDocumentNavigationQueryService>();
var rootKey = Guid.NewGuid();
IEnumerable<Guid> rootKeyList = new List<Guid> { rootKey };
IEnumerable<Guid> rootChildren = new List<Guid> { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };
navigationQueryService.Setup(x => x.TryGetRootKeys(out rootKeyList)).Returns(true);
navigationQueryService.Setup(x => x.TryGetChildrenKeys(It.IsAny<Guid>(), out rootChildren)).Returns(true);
var cacheSettings = new CacheSettings { DocumentBreadthFirstSeedCount = 0 };
var sut = new DocumentBreadthFirstKeyProvider(navigationQueryService.Object, Options.Create(cacheSettings));
var result = sut.GetSeedKeys();
Assert.Zero(result.Count);
}
[Test]
public void OnlyReturnsKeysUpToSeedCount()
{
// Structure
// Root
// - Child1
// - Child2
// - Child3
var navigationQueryService = new Mock<IDocumentNavigationQueryService>();
var rootKey = Guid.NewGuid();
IEnumerable<Guid> rootKeyList = new List<Guid> { rootKey };
IEnumerable<Guid> rootChildren = new List<Guid> { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };
navigationQueryService.Setup(x => x.TryGetRootKeys(out rootKeyList)).Returns(true);
navigationQueryService.Setup(x => x.TryGetChildrenKeys(rootKey, out rootChildren)).Returns(true);
var expected = 3;
var cacheSettings = new CacheSettings { DocumentBreadthFirstSeedCount = expected };
var sut = new DocumentBreadthFirstKeyProvider(navigationQueryService.Object, Options.Create(cacheSettings));
var result = sut.GetSeedKeys();
Assert.That(result.Count, Is.EqualTo(expected));
}
[Test]
public void IsBreadthFirst()
{
// Structure
// Root
// - Child1
// - GrandChild
// - Child2
// - Child3
var navigationQueryService = new Mock<IDocumentNavigationQueryService>();
var rootKey = Guid.NewGuid();
var child1Key = Guid.NewGuid();
var grandChildKey = Guid.NewGuid();
IEnumerable<Guid> rootKeyList = new List<Guid> { rootKey };
IEnumerable<Guid> rootChildren = new List<Guid> { child1Key, Guid.NewGuid(), Guid.NewGuid() };
IEnumerable<Guid> grandChildren = new List<Guid> { grandChildKey };
navigationQueryService.Setup(x => x.TryGetRootKeys(out rootKeyList)).Returns(true);
navigationQueryService.Setup(x => x.TryGetChildrenKeys(rootKey, out rootChildren)).Returns(true);
navigationQueryService.Setup(x => x.TryGetChildrenKeys(child1Key, out grandChildren)).Returns(true);
// This'll get all children but no grandchildren
var cacheSettings = new CacheSettings { DocumentBreadthFirstSeedCount = 4 };
var sut = new DocumentBreadthFirstKeyProvider(navigationQueryService.Object, Options.Create(cacheSettings));
var result = sut.GetSeedKeys();
Assert.That(result.Contains(grandChildKey), Is.False);
}
[Test]
public void CanGetAll()
{
var navigationQueryService = new Mock<IDocumentNavigationQueryService>();
var rootKey = Guid.NewGuid();
IEnumerable<Guid> rootKeyList = new List<Guid> { rootKey };
var childrenCount = 300;
List<Guid> rootChildren = new List<Guid> ();
for (int i = 0; i < childrenCount; i++)
{
rootChildren.Add(Guid.NewGuid());
}
IEnumerable<Guid> childrenEnumerable = rootChildren;
navigationQueryService.Setup(x => x.TryGetRootKeys(out rootKeyList)).Returns(true);
navigationQueryService.Setup(x => x.TryGetChildrenKeys(rootKey, out childrenEnumerable)).Returns(true);
var settings = new CacheSettings { DocumentBreadthFirstSeedCount = int.MaxValue };
var sut = new DocumentBreadthFirstKeyProvider(navigationQueryService.Object, Options.Create(settings));
var result = sut.GetSeedKeys();
var expected = childrenCount + 1; // Root + children
Assert.That(result.Count, Is.EqualTo(expected));
}
}