From 65393ff3d9f23391dacdb107e01c2fc4a201d3ec Mon Sep 17 00:00:00 2001 From: Pantelis Date: Tue, 30 Sep 2025 08:18:26 +0300 Subject: [PATCH] Models: `PropertyType` constructor sets the `DataTypeKey` if `IDataType` has identity (#20301) * PropertyType constructor sets the DataTypeKey if passed IDataType has identity * Updated unit tests to verify behaviour. --------- Co-authored-by: Andy Butland --- src/Umbraco.Core/Models/IPropertyType.cs | 3 ++ src/Umbraco.Core/Models/PropertyType.cs | 2 ++ .../Builders/PropertyTypeBuilder.cs | 10 +++++- .../Umbraco.Core/Models/PropertyTypeTests.cs | 31 ++++++++++++++++--- .../Builders/PropertyTypeBuilderTests.cs | 3 ++ 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/Umbraco.Core/Models/IPropertyType.cs b/src/Umbraco.Core/Models/IPropertyType.cs index a48f8e01ae..994a174b8e 100644 --- a/src/Umbraco.Core/Models/IPropertyType.cs +++ b/src/Umbraco.Core/Models/IPropertyType.cs @@ -24,6 +24,9 @@ public interface IPropertyType : IEntity, IRememberBeingDirty /// int DataTypeId { get; set; } + /// + /// Gets or sets the Guid unique identifier of the datatype for this property type. + /// Guid DataTypeKey { get; set; } /// diff --git a/src/Umbraco.Core/Models/PropertyType.cs b/src/Umbraco.Core/Models/PropertyType.cs index 8fe28f7751..4943cb94ce 100644 --- a/src/Umbraco.Core/Models/PropertyType.cs +++ b/src/Umbraco.Core/Models/PropertyType.cs @@ -47,6 +47,7 @@ public class PropertyType : EntityBase, IPropertyType, IEquatable if (dataType.HasIdentity) { _dataTypeId = dataType.Id; + _dataTypeKey = dataType.Key; } _propertyEditorAlias = dataType.EditorAlias; @@ -159,6 +160,7 @@ public class PropertyType : EntityBase, IPropertyType, IEquatable set => SetPropertyValueAndDetectChanges(value, ref _dataTypeId, nameof(DataTypeId)); } + /// [DataMember] public Guid DataTypeKey { diff --git a/tests/Umbraco.Tests.Common/Builders/PropertyTypeBuilder.cs b/tests/Umbraco.Tests.Common/Builders/PropertyTypeBuilder.cs index 9b7fce3a9d..b1882d4ab5 100644 --- a/tests/Umbraco.Tests.Common/Builders/PropertyTypeBuilder.cs +++ b/tests/Umbraco.Tests.Common/Builders/PropertyTypeBuilder.cs @@ -1,7 +1,6 @@ // Copyright (c) Umbraco. // See LICENSE for more details. -using System; using Umbraco.Cms.Core; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Strings; @@ -38,6 +37,7 @@ public class PropertyTypeBuilder private string _alias; private DateTime? _createDate; private int? _dataTypeId; + private Guid? _dataTypeKey; private string _description; private int? _id; private Guid? _key; @@ -132,6 +132,12 @@ public class PropertyTypeBuilder return this; } + public PropertyTypeBuilder WithDataTypeKey(Guid dataTypeKey) + { + _dataTypeKey = dataTypeKey; + return this; + } + public PropertyTypeBuilder WithPropertyGroupId(int propertyGroupId) { _propertyGroupId = new Lazy(() => propertyGroupId); @@ -176,6 +182,7 @@ public class PropertyTypeBuilder var updateDate = _updateDate ?? DateTime.Now; var sortOrder = _sortOrder ?? 0; var dataTypeId = _dataTypeId ?? -88; + var dataTypeKey = _dataTypeKey ?? Guid.Empty; var description = _description ?? string.Empty; var propertyGroupId = _propertyGroupId; var mandatory = _mandatory ?? false; @@ -196,6 +203,7 @@ public class PropertyTypeBuilder Name = name, SortOrder = sortOrder, DataTypeId = dataTypeId, + DataTypeKey = dataTypeKey, Description = description, CreateDate = createDate, UpdateDate = updateDate, diff --git a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Models/PropertyTypeTests.cs b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Models/PropertyTypeTests.cs index 41a6573c2d..4411a02ad0 100644 --- a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Models/PropertyTypeTests.cs +++ b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Models/PropertyTypeTests.cs @@ -6,6 +6,7 @@ using System.Reflection; using System.Text.Json; using NUnit.Framework; using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Strings; using Umbraco.Cms.Tests.Common.Builders; using Umbraco.Cms.Tests.Common.Builders.Extensions; @@ -15,9 +16,27 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Models; public class PropertyTypeTests { [SetUp] - public void SetUp() => _builder = new PropertyTypeBuilder(); + public void SetUp() + { + _propertyTypeBuilder = new PropertyTypeBuilder(); + _dataTypeBuilder = new DataTypeBuilder(); + } - private PropertyTypeBuilder _builder; + private PropertyTypeBuilder _propertyTypeBuilder; + private DataTypeBuilder _dataTypeBuilder; + + [Test] + public void Can_Create_From_DataType() + { + var shortStringHelper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()); + var dt = BuildDataType(); + var pt = new PropertyType(shortStringHelper, dt); + + Assert.AreEqual(dt.Id, pt.DataTypeId); + Assert.AreEqual(dt.Key, pt.DataTypeKey); + Assert.AreEqual(dt.EditorAlias, pt.PropertyEditorAlias); + Assert.AreEqual(dt.DatabaseType, pt.ValueStorageType); + } [Test] public void Can_Deep_Clone() @@ -32,7 +51,7 @@ public class PropertyTypeTests Assert.AreEqual(clone.Alias, pt.Alias); Assert.AreEqual(clone.CreateDate, pt.CreateDate); Assert.AreEqual(clone.DataTypeId, pt.DataTypeId); - Assert.AreEqual(clone.DataTypeId, pt.DataTypeId); + Assert.AreEqual(clone.DataTypeKey, pt.DataTypeKey); Assert.AreEqual(clone.Description, pt.Description); Assert.AreEqual(clone.Key, pt.Key); Assert.AreEqual(clone.Mandatory, pt.Mandatory); @@ -69,7 +88,7 @@ public class PropertyTypeTests } private PropertyType BuildPropertyType() => - _builder + _propertyTypeBuilder .WithId(3) .WithPropertyEditorAlias("TestPropertyEditor") .WithAlias("test") @@ -81,4 +100,8 @@ public class PropertyTypeTests .WithMandatory(true) .WithValidationRegExp("xxxx") .Build(); + + private DataType BuildDataType() => + _dataTypeBuilder + .Build(); } diff --git a/tests/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/PropertyTypeBuilderTests.cs b/tests/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/PropertyTypeBuilderTests.cs index 3b4c41bce4..0ce7bc92f1 100644 --- a/tests/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/PropertyTypeBuilderTests.cs +++ b/tests/Umbraco.Tests.UnitTests/Umbraco.Tests.Common/Builders/PropertyTypeBuilderTests.cs @@ -23,6 +23,7 @@ public class PropertyTypeBuilderTests const string testName = "Test"; const int testSortOrder = 9; const int testDataTypeId = 5; + var testDataTypeKey = Guid.NewGuid(); var testCreateDate = DateTime.Now.AddHours(-1); var testUpdateDate = DateTime.Now; const string testDescription = "testing"; @@ -43,6 +44,7 @@ public class PropertyTypeBuilderTests .WithName(testName) .WithSortOrder(testSortOrder) .WithDataTypeId(testDataTypeId) + .WithDataTypeKey(testDataTypeKey) .WithCreateDate(testCreateDate) .WithUpdateDate(testUpdateDate) .WithDescription(testDescription) @@ -60,6 +62,7 @@ public class PropertyTypeBuilderTests Assert.AreEqual(testName, propertyType.Name); Assert.AreEqual(testSortOrder, propertyType.SortOrder); Assert.AreEqual(testDataTypeId, propertyType.DataTypeId); + Assert.AreEqual(testDataTypeKey, propertyType.DataTypeKey); Assert.AreEqual(testCreateDate, propertyType.CreateDate); Assert.AreEqual(testUpdateDate, propertyType.UpdateDate); Assert.AreEqual(testDescription, propertyType.Description);