From 43c10a7fc0e1e34c8dae8992abaa5560eb49ef4c Mon Sep 17 00:00:00 2001 From: kjac Date: Sat, 4 Feb 2023 12:02:58 +0100 Subject: [PATCH] Expose content type key on ContentTypeSort --- src/Umbraco.Core/Models/ContentTypeSort.cs | 9 ++++++++- .../Models/Mapping/EntityMapDefinition.cs | 1 + .../Packaging/PackageDataInstallation.cs | 2 +- .../Implement/ContentTypeCommonRepository.cs | 8 ++++++-- tests/Umbraco.TestData/LoadTestController.cs | 2 +- .../Builders/ContentTypeSortBuilder.cs | 3 ++- .../Repositories/ContentTypeRepositoryTest.cs | 4 ++-- .../Services/ContentServicePerformanceTest.cs | 12 ++++++------ .../Services/ContentServiceTagsTests.cs | 2 +- .../Services/ContentServiceTests.cs | 4 ++-- 10 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/Umbraco.Core/Models/ContentTypeSort.cs b/src/Umbraco.Core/Models/ContentTypeSort.cs index e10d650cac..91728c2b8a 100644 --- a/src/Umbraco.Core/Models/ContentTypeSort.cs +++ b/src/Umbraco.Core/Models/ContentTypeSort.cs @@ -21,16 +21,18 @@ public class ContentTypeSort : IValueObject, IDeepCloneable SortOrder = sortOrder; } - public ContentTypeSort(Lazy id, int sortOrder, string alias) + public ContentTypeSort(Lazy id, int sortOrder, string alias, Guid key) { Id = id; SortOrder = sortOrder; Alias = alias; + Key = key; } /// /// Gets or sets the Id of the ContentType /// + // FIXME: remove this in favor of Key (Id should only be used at repository level) public Lazy Id { get; set; } = new(() => 0); /// @@ -43,6 +45,11 @@ public class ContentTypeSort : IValueObject, IDeepCloneable /// public string Alias { get; set; } = string.Empty; + /// + /// Gets or sets the unique Key of the ContentType + /// + public Guid Key { get; set; } + public object DeepClone() { var clone = (ContentTypeSort)MemberwiseClone(); diff --git a/src/Umbraco.Infrastructure/Models/Mapping/EntityMapDefinition.cs b/src/Umbraco.Infrastructure/Models/Mapping/EntityMapDefinition.cs index 9e582b6520..2c0ef3f383 100644 --- a/src/Umbraco.Infrastructure/Models/Mapping/EntityMapDefinition.cs +++ b/src/Umbraco.Infrastructure/Models/Mapping/EntityMapDefinition.cs @@ -129,6 +129,7 @@ public class EntityMapDefinition : IMapDefinition private static void Map(EntityBasic source, ContentTypeSort target, MapperContext context) { target.Alias = source.Alias; + target.Key = source.Key; target.Id = new Lazy(() => Convert.ToInt32(source.Id)); } diff --git a/src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs b/src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs index 76e20a48e8..4ee900e9df 100644 --- a/src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs +++ b/src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs @@ -1177,7 +1177,7 @@ namespace Umbraco.Cms.Infrastructure.Packaging } allowedChildren?.Add(new ContentTypeSort(new Lazy(() => allowedChild.Id), sortOrder, - allowedChild.Alias)); + allowedChild.Alias, allowedChild.Key)); sortOrder++; } diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentTypeCommonRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentTypeCommonRepository.cs index 72ebd3a79a..21fb9ccb15 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentTypeCommonRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentTypeCommonRepository.cs @@ -88,6 +88,7 @@ internal class ContentTypeCommonRepository : IContentTypeCommonRepository // prepare // note: same alias could be used for media, content... but always different ids = ok var aliases = contentTypeDtos.ToDictionary(x => x.NodeId, x => x.Alias); + var keys = contentTypeDtos.ToDictionary(x => x.NodeId, x => x.NodeDto.UniqueId); // create var allowedDtoIx = 0; @@ -120,14 +121,17 @@ internal class ContentTypeCommonRepository : IContentTypeCommonRepository while (allowedDtoIx < allowedDtos?.Count && allowedDtos[allowedDtoIx].Id == contentTypeDto.NodeId) { ContentTypeAllowedContentTypeDto allowedDto = allowedDtos[allowedDtoIx]; - if (!aliases.TryGetValue(allowedDto.AllowedId, out var alias)) + if (!aliases.TryGetValue(allowedDto.AllowedId, out var alias) + || !keys.TryGetValue(allowedDto.AllowedId, out Guid key)) { continue; } allowedContentTypes.Add(new ContentTypeSort( new Lazy(() => allowedDto.AllowedId), - allowedDto.SortOrder, alias!)); + allowedDto.SortOrder, + alias!, + key)); allowedDtoIx++; } diff --git a/tests/Umbraco.TestData/LoadTestController.cs b/tests/Umbraco.TestData/LoadTestController.cs index 741fe4e94b..15d69221ac 100644 --- a/tests/Umbraco.TestData/LoadTestController.cs +++ b/tests/Umbraco.TestData/LoadTestController.cs @@ -217,7 +217,7 @@ public class LoadTestController : Controller }; containerType.AllowedContentTypes = containerType.AllowedContentTypes.Union(new[] { - new ContentTypeSort(new Lazy(() => contentType.Id), 0, contentType.Alias) + new ContentTypeSort(new Lazy(() => contentType.Id), 0, contentType.Alias, contentType.Key) }); containerType.AllowedTemplates = containerType.AllowedTemplates.Union(new[] { containerTemplate }); containerType.SetDefaultTemplate(containerTemplate); diff --git a/tests/Umbraco.Tests.Common/Builders/ContentTypeSortBuilder.cs b/tests/Umbraco.Tests.Common/Builders/ContentTypeSortBuilder.cs index 560e27defd..eaeb6900dc 100644 --- a/tests/Umbraco.Tests.Common/Builders/ContentTypeSortBuilder.cs +++ b/tests/Umbraco.Tests.Common/Builders/ContentTypeSortBuilder.cs @@ -51,7 +51,8 @@ public class ContentTypeSortBuilder var id = _id ?? 1; var alias = _alias ?? Guid.NewGuid().ToString().ToCamelCase(); var sortOrder = _sortOrder ?? 0; + var key = Guid.NewGuid(); - return new ContentTypeSort(new Lazy(() => id), sortOrder, alias); + return new ContentTypeSort(new Lazy(() => id), sortOrder, alias, key); } } diff --git a/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/ContentTypeRepositoryTest.cs b/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/ContentTypeRepositoryTest.cs index aae69e2f61..39f9f558f6 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/ContentTypeRepositoryTest.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/ContentTypeRepositoryTest.cs @@ -809,8 +809,8 @@ public class ContentTypeRepositoryTest : UmbracoIntegrationTest var contentType = repository.Get(_simpleContentType.Id); contentType.AllowedContentTypes = new List { - new(new Lazy(() => subpageContentType.Id), 0, subpageContentType.Alias), - new(new Lazy(() => simpleSubpageContentType.Id), 1, simpleSubpageContentType.Alias) + new(new Lazy(() => subpageContentType.Id), 0, subpageContentType.Alias, subpageContentType.Key), + new(new Lazy(() => simpleSubpageContentType.Id), 1, simpleSubpageContentType.Alias, simpleSubpageContentType.Key) }; repository.Save(contentType); diff --git a/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServicePerformanceTest.cs b/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServicePerformanceTest.cs index bafd0aa138..67437dd4c1 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServicePerformanceTest.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServicePerformanceTest.cs @@ -70,18 +70,18 @@ public class ContentServicePerformanceTest : UmbracoIntegrationTest ContentTypeService.Save(new[] { contentType1, contentType2, contentType3 }); contentType1.AllowedContentTypes = new[] { - new ContentTypeSort(new Lazy(() => contentType2.Id), 0, contentType2.Alias), - new ContentTypeSort(new Lazy(() => contentType3.Id), 1, contentType3.Alias) + new ContentTypeSort(new Lazy(() => contentType2.Id), 0, contentType2.Alias, contentType2.Key), + new ContentTypeSort(new Lazy(() => contentType3.Id), 1, contentType3.Alias, contentType3.Key) }; contentType2.AllowedContentTypes = new[] { - new ContentTypeSort(new Lazy(() => contentType1.Id), 0, contentType1.Alias), - new ContentTypeSort(new Lazy(() => contentType3.Id), 1, contentType3.Alias) + new ContentTypeSort(new Lazy(() => contentType1.Id), 0, contentType1.Alias, contentType1.Key), + new ContentTypeSort(new Lazy(() => contentType3.Id), 1, contentType3.Alias, contentType3.Key) }; contentType3.AllowedContentTypes = new[] { - new ContentTypeSort(new Lazy(() => contentType1.Id), 0, contentType1.Alias), - new ContentTypeSort(new Lazy(() => contentType2.Id), 1, contentType2.Alias) + new ContentTypeSort(new Lazy(() => contentType1.Id), 0, contentType1.Alias, contentType1.Key), + new ContentTypeSort(new Lazy(() => contentType2.Id), 1, contentType2.Alias, contentType2.Key) }; ContentTypeService.Save(new[] { contentType1, contentType2, contentType3 }); diff --git a/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServiceTagsTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServiceTagsTests.cs index 550fedcb41..d21de88807 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServiceTagsTests.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServiceTagsTests.cs @@ -652,7 +652,7 @@ public class ContentServiceTagsTests : UmbracoIntegrationTest CreateAndAddTagsPropertyType(contentType); ContentTypeService.Save(contentType); contentType.AllowedContentTypes = - new[] { new ContentTypeSort(new Lazy(() => contentType.Id), 0, contentType.Alias) }; + new[] { new ContentTypeSort(new Lazy(() => contentType.Id), 0, contentType.Alias, contentType.Key) }; var content = ContentBuilder.CreateSimpleContent(contentType, "Tagged content"); content.AssignTags(PropertyEditorCollection, DataTypeService, Serializer, "tags", diff --git a/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServiceTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServiceTests.cs index 9d1242d5c7..34d2e123e7 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServiceTests.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServiceTests.cs @@ -1843,7 +1843,7 @@ public class ContentServiceTests : UmbracoIntegrationTestWithContent ContentTypeBuilder.CreateSimpleContentType("umbTextpage1", "Textpage", defaultTemplateId: template.Id); contentType.AllowedContentTypes = new List { - new(new Lazy(() => contentType.Id), 0, contentType.Alias) + new(new Lazy(() => contentType.Id), 0, contentType.Alias, contentType.Key) }; ContentTypeService.Save(contentType); @@ -1883,7 +1883,7 @@ public class ContentServiceTests : UmbracoIntegrationTestWithContent ContentTypeBuilder.CreateSimpleContentType("umbTextpage1", "Textpage", defaultTemplateId: template.Id); contentType.AllowedContentTypes = new List { - new(new Lazy(() => contentType.Id), 0, contentType.Alias) + new(new Lazy(() => contentType.Id), 0, contentType.Alias, contentType.Key) }; ContentTypeService.Save(contentType);