Introduce INavigationService for in-memory navigation data (#16818)

* Tests

* Remove props and use local vars

* Adding preliminary navigation service and content implementation

* Adding preliminary unit tests

* Change from async methods

* Refactor GetParentKey to TryGetParentKey

* Refactor GetChildrenKeys to TryGetChildrenKeys

* Refactor GetDescendantsKeys to TryGetDescendantsKeys

* Refactor GetAncestorsKeys to TryGetAncestorsKeys

* Refactor GetSiblingsKeys to TryGetSiblingsKeys

* Refactor TryGetChildrenKeys

* Initial integration tests

* Use ContentEditingService instead of ContentService

* Remove INavigationService.Copy implementation and unit tests

* Rename var

* Adding clarification

* Initial ContentNavigationRepository

* Initial NavigationFactory

* Remove filtering from factory

* NavigationRepository and implementation

* InitializationService responsible for seeding the in-memory structure

* Register repository and service

* Adding NavigationDto and NavigationNode

* Adding INavigationService dependency and Enlist updating navigation structure actions

* Documentation

* Adding tests for removing descendants as well

* Changed to ConcurrentDictionary

* Remove keys comments for tests

* Adding documentation

* Forgotten ConcurrentDictionary change

* Isolating the operations on the model

* Splitting the INavigationService to separate the querying from the managing functionality

* Introducing specific navigation services for document, document recycle bin, media and media recycle bin

* Making ContentNavigationService into a base as the functionality will be shared between the document, document recycle bin, media and media recycle bin services

* Adding the implementations of document, document recycle bin, media and media recycle bin navigation services

* Fixing comments

* Initializing all 4 collections

* Adapting the navigation unit tests to the base now

* Adapting integration tests to specific navigation service

* Adding test for rebuilding the structure

* Adding implementation for Adding and Getting a node - needed for moving to and restoring from the recycle bin + tests

* Updating the document navigation structure from the ContentService

* Fix typo

* Adding trashed items implementation in base - currently managing 2 structures

* Removing no longer relevant GetNavigationNode and AddNavigationNode

* Fix removing parent when child is removed supporting methods

* Added restoring functionality

* Adding Bin functionality to DocumentNavigationService

* Removing Move signature from IDocumentNavigationService

* Adding RecycleBin query and management services

* Re-adding Move and removing GetNavigationNode and AddNavigationNode signatures from interface

* Rebuilding bin structure using _documentNavigationService, instead of _documentRecycleBinNavigationService

* Fixing test name

* Adding more tests for remove

* Adding tests for restore and removing ones for GetNavigationNode and AddNavigationNode

* Remove comments

* Removing document and media RecycleBinNavigationService and their interfaces

* Adding media rebuild bin

* Fixing initialization with correct interfaces

* Removing RecycleBinNavigationServices' registration

* Remove IDocumentRecycleBinNavigationService dependency

* Updating in-memory nav structure when content updates happen

* Adding the rest of the integration tests

* Clean up IMediaNavigationService

* Fix comments

* Remove CustomTestSetup in integration tests as the structure is updated when content updates happen

* Adding and fixing comments

* Making RebuildBinAsync abstract as well

* Adding DocumentNavigationServiceTestsBase

* Splitting DocumentNavigationServiceTests into partial test classes

* Cleaning up DocumentNavigationServiceTests since tests have been moved to specific partial classes

* Reuse a method for creating content in tests

* Change type in test base

* Adding navigation structure updates in media service

* Adding MediaNavigationServiceTestsBase

* Adding integration tests for media nav str

* Remove services as we will have more concrete ones

* Add document and media IXNavigationQueryService and IXNavigationManagementService

* Inject ManagementService in ContentService.cs and MediaService.cs

* Change implementation to implement the new services + registration

* Make classes sealed

* Inject correct services in InitializationService

* Using the right services in integration tests

* Adding comments

* Removing bin interfaces from main navigation ones

* Rename Remove to MoveToBin

* V14 QA added block list editor tests (#16862)

* Added tests for blocklistEditor

* Added more tets

* Removed faker

* Added blockTest

* Updates

* Added tests

* Removed dependencies

* Fixes

* Clean up

* Fixed naming

* Cleaned up

* Bumped version

* Added missing semicolons

* Added tags

* Only runs the new tests

* Updates

* Bumped version

* Fixed tests

* Cleaned up

* Updated version

* Fixes, not done

* Fixed tests

* Bumped helpers

* Bumped helpers

* Fixed conflict

* Fixed comment

* Reverted to run smokeTests

* Updated helpers

* improve missingProperties data returned for missing propertie values (#16910)

Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>

* update backoffice submodule

* Rename initialization service to initialization hosted service

* Refactor repository to return a collection

* Add interface for the NavigationDto

* Add constants to bind property names between DTOs

* Move factory and fix input type

* Use constants for column names

* Use factory from base

---------

Co-authored-by: Andreas Zerbst <73799582+andr317c@users.noreply.github.com>
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
Co-authored-by: Sven Geusens <sge@umbraco.dk>
Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>
Co-authored-by: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com>
This commit is contained in:
Elitsa Marinovska
2024-09-04 11:18:08 +02:00
committed by GitHub
parent eff520c7eb
commit 5a7d563b8a
54 changed files with 3669 additions and 770 deletions

View File

@@ -0,0 +1,46 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
// TODO: test that it is added to its new parent - check parent's children
// TODO: test that it has the same amount of descendants - depending on value of includeDescendants param
// TODO: test that the number of target parent descendants updates when copying node with descendants
// TODO: test that copied node descendants have different keys than source node descendants
public partial class DocumentNavigationServiceTests
{
[Test]
[TestCase("A1B1B217-B02F-4307-862C-A5E22DB729EB", "A1B1B217-B02F-4307-862C-A5E22DB729EB")] // Grandchild 2 to itself
[TestCase("60E0E5C4-084E-4144-A560-7393BEAD2E96", null)] // Child 2 to content root
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF", "C6173927-0C59-4778-825D-D7B9F45D8DDE")] // Child 3 to Child 1
public async Task Structure_Updates_When_Copying_Content(Guid nodeToCopy, Guid? targetParentKey)
{
// Arrange
DocumentNavigationQueryService.TryGetParentKey(nodeToCopy, out Guid? sourceParentKey);
// Act
var copyAttempt = await ContentEditingService.CopyAsync(nodeToCopy, targetParentKey, false, false, Constants.Security.SuperUserKey);
Guid copiedItemKey = copyAttempt.Result.Key;
// Assert
Assert.AreNotEqual(nodeToCopy, copiedItemKey);
DocumentNavigationQueryService.TryGetParentKey(copiedItemKey, out Guid? copiedItemParentKey);
Assert.Multiple(() =>
{
if (targetParentKey is null)
{
// Verify the copied node's parent is null (it's been copied to content root)
Assert.IsNull(copiedItemParentKey);
}
else
{
Assert.IsNotNull(copiedItemParentKey);
}
Assert.AreEqual(targetParentKey, copiedItemParentKey);
Assert.AreNotEqual(sourceParentKey, copiedItemParentKey);
});
}
}

View File

@@ -0,0 +1,39 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models.ContentEditing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
public partial class DocumentNavigationServiceTests
{
[Test]
public async Task Structure_Updates_When_Creating_Content()
{
// Arrange
DocumentNavigationQueryService.TryGetSiblingsKeys(Root.Key, out IEnumerable<Guid> initialSiblingsKeys);
var initialRootNodeSiblingsCount = initialSiblingsKeys.Count();
var createModel = new ContentCreateModel
{
ContentTypeKey = ContentType.Key,
ParentKey = Constants.System.RootKey, // Create node at content root
InvariantName = "Root 2",
};
// Act
var createAttempt = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
Guid createdItemKey = createAttempt.Result.Content!.Key;
// Verify that the structure has updated by checking the siblings list of the Root once again
DocumentNavigationQueryService.TryGetSiblingsKeys(Root.Key, out IEnumerable<Guid> updatedSiblingsKeys);
List<Guid> siblingsList = updatedSiblingsKeys.ToList();
// Assert
Assert.Multiple(() =>
{
Assert.IsNotEmpty(siblingsList);
Assert.AreEqual(initialRootNodeSiblingsCount + 1, siblingsList.Count);
Assert.AreEqual(createdItemKey, siblingsList.First());
});
}
}

View File

@@ -0,0 +1,43 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
// TODO: Test that the descendants of the node have also been removed from both structures
public partial class DocumentNavigationServiceTests
{
[Test]
[TestCase("E48DD82A-7059-418E-9B82-CDD5205796CF")] // Root
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF")] // Child 3
[TestCase("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7")] // Great-grandchild 1
public async Task Structure_Updates_When_Deleting_Content(Guid nodeToDelete)
{
// Arrange
DocumentNavigationQueryService.TryGetDescendantsKeys(nodeToDelete, out IEnumerable<Guid> initialDescendantsKeys);
// Act
// Deletes the item whether it is in the recycle bin or not
var deleteAttempt = await ContentEditingService.DeleteAsync(nodeToDelete, Constants.Security.SuperUserKey);
Guid deletedItemKey = deleteAttempt.Result.Key;
// Assert
var nodeExists = DocumentNavigationQueryService.TryGetDescendantsKeys(deletedItemKey, out _);
var nodeExistsInRecycleBin = DocumentNavigationQueryService.TryGetDescendantsKeysInBin(nodeToDelete, out _);
Assert.Multiple(() =>
{
Assert.AreEqual(nodeToDelete, deletedItemKey);
Assert.IsFalse(nodeExists);
Assert.IsFalse(nodeExistsInRecycleBin);
foreach (Guid descendant in initialDescendantsKeys)
{
var descendantExists = DocumentNavigationQueryService.TryGetParentKey(descendant, out _);
Assert.IsFalse(descendantExists);
var descendantExistsInRecycleBin = DocumentNavigationQueryService.TryGetParentKeyInBin(descendant, out _);
Assert.IsFalse(descendantExistsInRecycleBin);
}
});
}
}

View File

@@ -0,0 +1,30 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
// TODO: check that the descendants have also been removed from both structures - navigation and trash
public partial class DocumentNavigationServiceTests
{
[Test]
public async Task Structure_Updates_When_Deleting_From_Recycle_Bin()
{
// Arrange
Guid nodeToDelete = Child1.Key;
Guid nodeInRecycleBin = Grandchild4.Key;
// Move nodes to recycle bin
await ContentEditingService.MoveToRecycleBinAsync(nodeInRecycleBin, Constants.Security.SuperUserKey); // Make sure we have an item already in the recycle bin to act as a sibling
await ContentEditingService.MoveToRecycleBinAsync(nodeToDelete, Constants.Security.SuperUserKey); // Make sure the item is in the recycle bin
DocumentNavigationQueryService.TryGetSiblingsKeysInBin(nodeInRecycleBin, out IEnumerable<Guid> initialSiblingsKeys);
// Act
await ContentEditingService.DeleteFromRecycleBinAsync(nodeToDelete, Constants.Security.SuperUserKey);
// Assert
DocumentNavigationQueryService.TryGetSiblingsKeysInBin(nodeInRecycleBin, out IEnumerable<Guid> updatedSiblingsKeys);
// Verify siblings count has decreased by one
Assert.AreEqual(initialSiblingsKeys.Count() - 1, updatedSiblingsKeys.Count());
}
}

View File

@@ -0,0 +1,39 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
public partial class DocumentNavigationServiceTests
{
[Test]
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573", "60E0E5C4-084E-4144-A560-7393BEAD2E96")] // Grandchild 1 to Child 2
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF", null)] // Child 3 to content root
[TestCase("60E0E5C4-084E-4144-A560-7393BEAD2E96", "C6173927-0C59-4778-825D-D7B9F45D8DDE")] // Child 2 to Child 1
public async Task Structure_Updates_When_Moving_Content(Guid nodeToMove, Guid? targetParentKey)
{
// Arrange
DocumentNavigationQueryService.TryGetParentKey(nodeToMove, out Guid? originalParentKey);
// Act
var moveAttempt = await ContentEditingService.MoveAsync(nodeToMove, targetParentKey, Constants.Security.SuperUserKey);
// Verify the node's new parent is updated
DocumentNavigationQueryService.TryGetParentKey(moveAttempt.Result!.Key, out Guid? updatedParentKey);
// Assert
Assert.Multiple(() =>
{
if (targetParentKey is null)
{
Assert.IsNull(updatedParentKey);
}
else
{
Assert.IsNotNull(updatedParentKey);
}
Assert.AreNotEqual(originalParentKey, updatedParentKey);
Assert.AreEqual(targetParentKey, updatedParentKey);
});
}
}

View File

@@ -0,0 +1,32 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
// TODO: also check that initial siblings count's decreased
// TODO: and that descendants are still the same (i.e. they've also been moved to recycle bin)
public partial class DocumentNavigationServiceTests
{
[Test]
public async Task Structure_Updates_When_Moving_Content_To_Recycle_Bin()
{
// Arrange
Guid nodeToMoveToRecycleBin = Child3.Key;
DocumentNavigationQueryService.TryGetParentKey(nodeToMoveToRecycleBin, out Guid? originalParentKey);
// Act
await ContentEditingService.MoveToRecycleBinAsync(nodeToMoveToRecycleBin, Constants.Security.SuperUserKey);
// Assert
var nodeExists = DocumentNavigationQueryService.TryGetParentKey(nodeToMoveToRecycleBin, out _); // Verify that the item is no longer in the document structure
var nodeExistsInRecycleBin = DocumentNavigationQueryService.TryGetParentKeyInBin(nodeToMoveToRecycleBin, out Guid? updatedParentKeyInRecycleBin);
Assert.Multiple(() =>
{
Assert.IsFalse(nodeExists);
Assert.IsTrue(nodeExistsInRecycleBin);
Assert.AreNotEqual(originalParentKey, updatedParentKeyInRecycleBin);
Assert.IsNull(updatedParentKeyInRecycleBin); // Verify the node's parent is now located at the root of the recycle bin (null)
});
}
}

View File

@@ -0,0 +1,59 @@
using NUnit.Framework;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services.Navigation;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
public partial class DocumentNavigationServiceTests
{
[Test]
public async Task Structure_Can_Rebuild()
{
// Arrange
Guid nodeKey = Root.Key;
// Capture original built state of DocumentNavigationService
DocumentNavigationQueryService.TryGetParentKey(nodeKey, out Guid? originalParentKey);
DocumentNavigationQueryService.TryGetChildrenKeys(nodeKey, out IEnumerable<Guid> originalChildrenKeys);
DocumentNavigationQueryService.TryGetDescendantsKeys(nodeKey, out IEnumerable<Guid> originalDescendantsKeys);
DocumentNavigationQueryService.TryGetAncestorsKeys(nodeKey, out IEnumerable<Guid> originalAncestorsKeys);
DocumentNavigationQueryService.TryGetSiblingsKeys(nodeKey, out IEnumerable<Guid> originalSiblingsKeys);
// Im-memory navigation structure is empty here
var newDocumentNavigationService = new DocumentNavigationService(GetRequiredService<ICoreScopeProvider>(), GetRequiredService<INavigationRepository>());
var initialNodeExists = newDocumentNavigationService.TryGetParentKey(nodeKey, out _);
// Act
await newDocumentNavigationService.RebuildAsync();
// Capture rebuilt state
var nodeExists = newDocumentNavigationService.TryGetParentKey(nodeKey, out Guid? parentKeyFromRebuild);
newDocumentNavigationService.TryGetChildrenKeys(nodeKey, out IEnumerable<Guid> childrenKeysFromRebuild);
newDocumentNavigationService.TryGetDescendantsKeys(nodeKey, out IEnumerable<Guid> descendantsKeysFromRebuild);
newDocumentNavigationService.TryGetAncestorsKeys(nodeKey, out IEnumerable<Guid> ancestorsKeysFromRebuild);
newDocumentNavigationService.TryGetSiblingsKeys(nodeKey, out IEnumerable<Guid> siblingsKeysFromRebuild);
// Assert
Assert.Multiple(() =>
{
Assert.IsFalse(initialNodeExists);
// Verify that the item is present in the navigation structure after a rebuild
Assert.IsTrue(nodeExists);
// Verify that we have the same items as in the original built state of DocumentNavigationService
Assert.AreEqual(originalParentKey, parentKeyFromRebuild);
CollectionAssert.AreEquivalent(originalChildrenKeys, childrenKeysFromRebuild);
CollectionAssert.AreEquivalent(originalDescendantsKeys, descendantsKeysFromRebuild);
CollectionAssert.AreEquivalent(originalAncestorsKeys, ancestorsKeysFromRebuild);
CollectionAssert.AreEquivalent(originalSiblingsKeys, siblingsKeysFromRebuild);
});
}
[Test]
// TODO: Test that you can rebuild bin structure as well
public async Task Bin_Structure_Can_Rebuild()
{
}
}

View File

@@ -0,0 +1,50 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
// TODO: test that descendants are also restored in the right place
public partial class DocumentNavigationServiceTests
{
[Test]
[TestCase("E856AC03-C23E-4F63-9AA9-681B42A58573", "C6173927-0C59-4778-825D-D7B9F45D8DDE")] // Grandchild 1 to Child 1
[TestCase("A1B1B217-B02F-4307-862C-A5E22DB729EB", "D63C1621-C74A-4106-8587-817DEE5FB732")] // Grandchild 2 to Grandchild 3
[TestCase("B606E3FF-E070-4D46-8CB9-D31352029FDF", null)] // Child 3 to content root
public async Task Structure_Updates_When_Restoring_Content(Guid nodeToRestore, Guid? targetParentKey)
{
// Arrange
Guid nodeInRecycleBin = GreatGrandchild1.Key;
// Move nodes to recycle bin
await ContentEditingService.MoveToRecycleBinAsync(nodeInRecycleBin, Constants.Security.SuperUserKey); // Make sure we have an item already in the recycle bin to act as a sibling
await ContentEditingService.MoveToRecycleBinAsync(nodeToRestore, Constants.Security.SuperUserKey); // Make sure the item is in the recycle bin
DocumentNavigationQueryService.TryGetParentKeyInBin(nodeToRestore, out Guid? initialParentKey);
DocumentNavigationQueryService.TryGetSiblingsKeysInBin(nodeInRecycleBin, out IEnumerable<Guid> initialSiblingsKeys);
// Act
var restoreAttempt = await ContentEditingService.RestoreAsync(nodeToRestore, targetParentKey, Constants.Security.SuperUserKey);
Guid restoredItemKey = restoreAttempt.Result.Key;
// Assert
DocumentNavigationQueryService.TryGetParentKey(restoredItemKey, out Guid? restoredItemParentKey);
DocumentNavigationQueryService.TryGetSiblingsKeysInBin(nodeInRecycleBin, out IEnumerable<Guid> updatedSiblingsKeys);
Assert.Multiple(() =>
{
// Verify siblings count has decreased by one
Assert.AreEqual(initialSiblingsKeys.Count() - 1, updatedSiblingsKeys.Count());
if (targetParentKey is null)
{
Assert.IsNull(restoredItemParentKey);
}
else
{
Assert.IsNotNull(restoredItemParentKey);
Assert.AreNotEqual(initialParentKey, restoredItemParentKey);
}
Assert.AreEqual(targetParentKey, restoredItemParentKey);
});
}
}

View File

@@ -0,0 +1,54 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models.ContentEditing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
public partial class DocumentNavigationServiceTests
{
[Test]
public async Task Structure_Does_Not_Update_When_Updating_Content()
{
// Arrange
Guid nodeToUpdate = Root.Key;
// Capture initial state
DocumentNavigationQueryService.TryGetParentKey(nodeToUpdate, out Guid? initialParentKey);
DocumentNavigationQueryService.TryGetChildrenKeys(nodeToUpdate, out IEnumerable<Guid> initialChildrenKeys);
DocumentNavigationQueryService.TryGetDescendantsKeys(nodeToUpdate, out IEnumerable<Guid> initialDescendantsKeys);
DocumentNavigationQueryService.TryGetAncestorsKeys(nodeToUpdate, out IEnumerable<Guid> initialAncestorsKeys);
DocumentNavigationQueryService.TryGetSiblingsKeys(nodeToUpdate, out IEnumerable<Guid> initialSiblingsKeys);
var updateModel = new ContentUpdateModel
{
InvariantName = "Updated Root",
};
// Act
var updateAttempt = await ContentEditingService.UpdateAsync(nodeToUpdate, updateModel, Constants.Security.SuperUserKey);
Guid updatedItemKey = updateAttempt.Result.Content!.Key;
// Capture updated state
var nodeExists = DocumentNavigationQueryService.TryGetParentKey(updatedItemKey, out Guid? updatedParentKey);
DocumentNavigationQueryService.TryGetChildrenKeys(updatedItemKey, out IEnumerable<Guid> childrenKeysAfterUpdate);
DocumentNavigationQueryService.TryGetDescendantsKeys(updatedItemKey, out IEnumerable<Guid> descendantsKeysAfterUpdate);
DocumentNavigationQueryService.TryGetAncestorsKeys(updatedItemKey, out IEnumerable<Guid> ancestorsKeysAfterUpdate);
DocumentNavigationQueryService.TryGetSiblingsKeys(updatedItemKey, out IEnumerable<Guid> siblingsKeysAfterUpdate);
// Assert
Assert.Multiple(() =>
{
// Verify that the item is still present in the navigation structure
Assert.IsTrue(nodeExists);
Assert.AreEqual(nodeToUpdate, updatedItemKey);
// Verify that nothing's changed
Assert.AreEqual(initialParentKey, updatedParentKey);
CollectionAssert.AreEquivalent(initialChildrenKeys, childrenKeysAfterUpdate);
CollectionAssert.AreEquivalent(initialDescendantsKeys, descendantsKeysAfterUpdate);
CollectionAssert.AreEquivalent(initialAncestorsKeys, ancestorsKeysAfterUpdate);
CollectionAssert.AreEquivalent(initialSiblingsKeys, siblingsKeysAfterUpdate);
});
}
}

View File

@@ -0,0 +1,90 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Tests.Common.Builders;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
public partial class DocumentNavigationServiceTests : DocumentNavigationServiceTestsBase
{
[SetUp]
public async Task Setup()
{
// Root
// - Child 1
// - Grandchild 1
// - Grandchild 2
// - Child 2
// - Grandchild 3
// - Great-grandchild 1
// - Child 3
// - Grandchild 4
// Doc Type
ContentType = ContentTypeBuilder.CreateSimpleContentType("page", "Page");
ContentType.Key = new Guid("DD72B8A6-2CE3-47F0-887E-B695A1A5D086");
ContentType.AllowedAsRoot = true;
ContentType.AllowedTemplates = null;
ContentType.AllowedContentTypes = new[] { new ContentTypeSort(ContentType.Key, 0, ContentType.Alias) };
await ContentTypeService.CreateAsync(ContentType, Constants.Security.SuperUserKey);
// Content
var rootModel = CreateContentCreateModel("Root", new Guid("E48DD82A-7059-418E-9B82-CDD5205796CF"));
var rootCreateAttempt = await ContentEditingService.CreateAsync(rootModel, Constants.Security.SuperUserKey);
Root = rootCreateAttempt.Result.Content!;
var child1Model = CreateContentCreateModel("Child 1", new Guid("C6173927-0C59-4778-825D-D7B9F45D8DDE"), Root.Key);
var child1CreateAttempt = await ContentEditingService.CreateAsync(child1Model, Constants.Security.SuperUserKey);
Child1 = child1CreateAttempt.Result.Content!;
var grandchild1Model = CreateContentCreateModel("Grandchild 1", new Guid("E856AC03-C23E-4F63-9AA9-681B42A58573"), Child1.Key);
var grandchild1CreateAttempt = await ContentEditingService.CreateAsync(grandchild1Model, Constants.Security.SuperUserKey);
Grandchild1 = grandchild1CreateAttempt.Result.Content!;
var grandchild2Model = CreateContentCreateModel("Grandchild 2", new Guid("A1B1B217-B02F-4307-862C-A5E22DB729EB"), Child1.Key);
var grandchild2CreateAttempt = await ContentEditingService.CreateAsync(grandchild2Model, Constants.Security.SuperUserKey);
Grandchild2 = grandchild2CreateAttempt.Result.Content!;
var child2Model = CreateContentCreateModel("Child 2", new Guid("60E0E5C4-084E-4144-A560-7393BEAD2E96"), Root.Key);
var child2CreateAttempt = await ContentEditingService.CreateAsync(child2Model, Constants.Security.SuperUserKey);
Child2 = child2CreateAttempt.Result.Content!;
var grandchild3Model = CreateContentCreateModel("Grandchild 3", new Guid("D63C1621-C74A-4106-8587-817DEE5FB732"), Child2.Key);
var grandchild3CreateAttempt = await ContentEditingService.CreateAsync(grandchild3Model, Constants.Security.SuperUserKey);
Grandchild3 = grandchild3CreateAttempt.Result.Content!;
var greatGrandchild1Model = CreateContentCreateModel("Great-grandchild 1", new Guid("56E29EA9-E224-4210-A59F-7C2C5C0C5CC7"), Grandchild3.Key);
var greatGrandchild1CreateAttempt = await ContentEditingService.CreateAsync(greatGrandchild1Model, Constants.Security.SuperUserKey);
GreatGrandchild1 = greatGrandchild1CreateAttempt.Result.Content!;
var child3Model = CreateContentCreateModel("Child 3", new Guid("B606E3FF-E070-4D46-8CB9-D31352029FDF"), Root.Key);
var child3CreateAttempt = await ContentEditingService.CreateAsync(child3Model, Constants.Security.SuperUserKey);
Child3 = child3CreateAttempt.Result.Content!;
var grandchild4Model = CreateContentCreateModel("Grandchild 4", new Guid("F381906C-223C-4466-80F7-B63B4EE073F8"), Child3.Key);
var grandchild4CreateAttempt = await ContentEditingService.CreateAsync(grandchild4Model, Constants.Security.SuperUserKey);
Grandchild4 = grandchild4CreateAttempt.Result.Content!;
}
[Test]
public async Task Structure_Does_Not_Update_When_Scope_Is_Not_Completed()
{
// Arrange
Guid notCreatedRootKey = new Guid("516927E5-8574-497B-B45B-E27EFAB47DE4");
// Create node at content root
var createModel = CreateContentCreateModel("Root 2", notCreatedRootKey);
using (ICoreScope scope = ScopeProvider.CreateCoreScope())
{
await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
}
// Act
var nodeExists = DocumentNavigationQueryService.TryGetParentKey(notCreatedRootKey, out _);
// Assert
Assert.IsFalse(nodeExists);
}
}

View File

@@ -0,0 +1,51 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.Navigation;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public abstract class DocumentNavigationServiceTestsBase : UmbracoIntegrationTest
{
protected IContentTypeService ContentTypeService => GetRequiredService<IContentTypeService>();
// Testing with IContentEditingService as it calls IContentService underneath
protected IContentEditingService ContentEditingService => GetRequiredService<IContentEditingService>();
protected IDocumentNavigationQueryService DocumentNavigationQueryService => GetRequiredService<IDocumentNavigationQueryService>();
protected IContentType ContentType { get; set; }
protected IContent Root { get; set; }
protected IContent Child1 { get; set; }
protected IContent Grandchild1 { get; set; }
protected IContent Grandchild2 { get; set; }
protected IContent Child2 { get; set; }
protected IContent Grandchild3 { get; set; }
protected IContent GreatGrandchild1 { get; set; }
protected IContent Child3 { get; set; }
protected IContent Grandchild4 { get; set; }
protected ContentCreateModel CreateContentCreateModel(string name, Guid key, Guid? parentKey = null)
=> new()
{
ContentTypeKey = ContentType.Key,
ParentKey = parentKey ?? Constants.System.RootKey,
InvariantName = name,
Key = key,
};
}

View File

@@ -0,0 +1,9 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
public partial class MediaNavigationServiceTests
{
}

View File

@@ -0,0 +1,9 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
public partial class MediaNavigationServiceTests
{
}

View File

@@ -0,0 +1,9 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
public partial class MediaNavigationServiceTests
{
}

View File

@@ -0,0 +1,9 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
public partial class MediaNavigationServiceTests
{
}

View File

@@ -0,0 +1,9 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
public partial class MediaNavigationServiceTests
{
}

View File

@@ -0,0 +1,59 @@
using NUnit.Framework;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services.Navigation;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
public partial class MediaNavigationServiceTests
{
[Test]
public async Task Structure_Can_Rebuild()
{
// Arrange
Guid nodeKey = Album.Key;
// Capture original built state of MediaNavigationService
MediaNavigationQueryService.TryGetParentKey(nodeKey, out Guid? originalParentKey);
MediaNavigationQueryService.TryGetChildrenKeys(nodeKey, out IEnumerable<Guid> originalChildrenKeys);
MediaNavigationQueryService.TryGetDescendantsKeys(nodeKey, out IEnumerable<Guid> originalDescendantsKeys);
MediaNavigationQueryService.TryGetAncestorsKeys(nodeKey, out IEnumerable<Guid> originalAncestorsKeys);
MediaNavigationQueryService.TryGetSiblingsKeys(nodeKey, out IEnumerable<Guid> originalSiblingsKeys);
// Im-memory navigation structure is empty here
var newMediaNavigationService = new MediaNavigationService(GetRequiredService<ICoreScopeProvider>(), GetRequiredService<INavigationRepository>());
var initialNodeExists = newMediaNavigationService.TryGetParentKey(nodeKey, out _);
// Act
await newMediaNavigationService.RebuildAsync();
// Capture rebuilt state
var nodeExists = newMediaNavigationService.TryGetParentKey(nodeKey, out Guid? parentKeyFromRebuild);
newMediaNavigationService.TryGetChildrenKeys(nodeKey, out IEnumerable<Guid> childrenKeysFromRebuild);
newMediaNavigationService.TryGetDescendantsKeys(nodeKey, out IEnumerable<Guid> descendantsKeysFromRebuild);
newMediaNavigationService.TryGetAncestorsKeys(nodeKey, out IEnumerable<Guid> ancestorsKeysFromRebuild);
newMediaNavigationService.TryGetSiblingsKeys(nodeKey, out IEnumerable<Guid> siblingsKeysFromRebuild);
// Assert
Assert.Multiple(() =>
{
Assert.IsFalse(initialNodeExists);
// Verify that the item is present in the navigation structure after a rebuild
Assert.IsTrue(nodeExists);
// Verify that we have the same items as in the original built state of MediaNavigationService
Assert.AreEqual(originalParentKey, parentKeyFromRebuild);
CollectionAssert.AreEquivalent(originalChildrenKeys, childrenKeysFromRebuild);
CollectionAssert.AreEquivalent(originalDescendantsKeys, descendantsKeysFromRebuild);
CollectionAssert.AreEquivalent(originalAncestorsKeys, ancestorsKeysFromRebuild);
CollectionAssert.AreEquivalent(originalSiblingsKeys, siblingsKeysFromRebuild);
});
}
[Test]
// TODO: Test that you can rebuild bin structure as well
public async Task Bin_Structure_Can_Rebuild()
{
}
}

View File

@@ -0,0 +1,9 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
public partial class MediaNavigationServiceTests
{
}

View File

@@ -0,0 +1,9 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
public partial class MediaNavigationServiceTests
{
}

View File

@@ -0,0 +1,79 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Scoping;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
public partial class MediaNavigationServiceTests : MediaNavigationServiceTestsBase
{
[SetUp]
public async Task Setup()
{
// Album
// - Image 1
// - Sub-album 1
// - Image 2
// - Image 3
// - Sub-album 2
// - Sub-sub-album 1
// - Image 4
// Media Types
FolderMediaType = MediaTypeService.Get(Constants.Conventions.MediaTypes.Folder);
ImageMediaType = MediaTypeService.Get(Constants.Conventions.MediaTypes.Image);
// Media
var albumModel = CreateMediaCreateModel("Album", new Guid("1CD97C02-8534-4B72-AE9E-AE52EC94CF31"), FolderMediaType.Key);
var albumCreateAttempt = await MediaEditingService.CreateAsync(albumModel, Constants.Security.SuperUserKey);
Album = albumCreateAttempt.Result.Content!;
var image1Model = CreateMediaCreateModel("Image 1", new Guid("03976EBE-A942-4F24-9885-9186E99AEF7C"), ImageMediaType.Key, Album.Key);
var image1CreateAttempt = await MediaEditingService.CreateAsync(image1Model, Constants.Security.SuperUserKey);
Image1 = image1CreateAttempt.Result.Content!;
var subAlbum1Model = CreateMediaCreateModel("Sub-album 1", new Guid("139DC977-E50F-4382-9728-B278C4B7AC6A"), FolderMediaType.Key, Album.Key);
var subAlbum1CreateAttempt = await MediaEditingService.CreateAsync(subAlbum1Model, Constants.Security.SuperUserKey);
SubAlbum1 = subAlbum1CreateAttempt.Result.Content!;
var image2Model = CreateMediaCreateModel("Image 2", new Guid("3E489C32-9315-42DA-95CE-823D154B09C8"), ImageMediaType.Key, SubAlbum1.Key);
var image2CreateAttempt = await MediaEditingService.CreateAsync(image2Model, Constants.Security.SuperUserKey);
Image2 = image2CreateAttempt.Result.Content!;
var image3Model = CreateMediaCreateModel("Image 3", new Guid("6176BD70-2CD2-4AEE-A045-084C94E4AFF2"), ImageMediaType.Key, SubAlbum1.Key);
var image3CreateAttempt = await MediaEditingService.CreateAsync(image3Model, Constants.Security.SuperUserKey);
Image3 = image3CreateAttempt.Result.Content!;
var subAlbum2Model = CreateMediaCreateModel("Sub-album 2", new Guid("DBCAFF2F-BFA4-4744-A948-C290C432D564"), FolderMediaType.Key, Album.Key);
var subAlbum2CreateAttempt = await MediaEditingService.CreateAsync(subAlbum2Model, Constants.Security.SuperUserKey);
SubAlbum2 = subAlbum2CreateAttempt.Result.Content!;
var subSubAlbum1Model = CreateMediaCreateModel("Sub-sub-album 1", new Guid("E0B23D56-9A0E-4FC4-BD42-834B73B4C7AB"), FolderMediaType.Key, SubAlbum2.Key);
var subSubAlbum1CreateAttempt = await MediaEditingService.CreateAsync(subSubAlbum1Model, Constants.Security.SuperUserKey);
SubSubAlbum1 = subSubAlbum1CreateAttempt.Result.Content!;
var image4Model = CreateMediaCreateModel("Image 4", new Guid("62BCE72F-8C18-420E-BCAC-112B5ECC95FD"), ImageMediaType.Key, SubSubAlbum1.Key);
var image4CreateAttempt = await MediaEditingService.CreateAsync(image4Model, Constants.Security.SuperUserKey);
Image4 = image4CreateAttempt.Result.Content!;
}
[Test]
public async Task Structure_Does_Not_Update_When_Scope_Is_Not_Completed()
{
// Arrange
Guid notCreatedAlbumKey = new Guid("860EE748-BC7E-4A13-A1D9-C9160B25AD6E");
// Create node at media root
var createModel = CreateMediaCreateModel("Album 2", notCreatedAlbumKey, FolderMediaType.Key);
using (ICoreScope scope = ScopeProvider.CreateCoreScope())
{
await MediaEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey);
}
// Act
var nodeExists = MediaNavigationQueryService.TryGetParentKey(notCreatedAlbumKey, out _);
// Assert
Assert.IsFalse(nodeExists);
}
}

View File

@@ -0,0 +1,51 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.Navigation;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public abstract class MediaNavigationServiceTestsBase : UmbracoIntegrationTest
{
protected IMediaTypeService MediaTypeService => GetRequiredService<IMediaTypeService>();
// Testing with IMediaEditingService as it calls IMediaService underneath
protected IMediaEditingService MediaEditingService => GetRequiredService<IMediaEditingService>();
protected IMediaNavigationQueryService MediaNavigationQueryService => GetRequiredService<IMediaNavigationQueryService>();
protected IMediaType FolderMediaType { get; set; }
protected IMediaType ImageMediaType { get; set; }
protected IMedia Album { get; set; }
protected IMedia Image1 { get; set; }
protected IMedia SubAlbum1 { get; set; }
protected IMedia Image2 { get; set; }
protected IMedia Image3 { get; set; }
protected IMedia SubAlbum2 { get; set; }
protected IMedia SubSubAlbum1 { get; set; }
protected IMedia Image4 { get; set; }
protected MediaCreateModel CreateMediaCreateModel(string name, Guid key, Guid mediaTypeKey, Guid? parentKey = null)
=> new()
{
ContentTypeKey = mediaTypeKey,
ParentKey = parentKey ?? Constants.System.RootKey,
InvariantName = name,
Key = key,
};
}