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 <abutland73@gmail.com>
This commit is contained in:
Pantelis
2025-09-30 08:18:26 +03:00
committed by GitHub
parent 82fc41a459
commit 65393ff3d9
5 changed files with 44 additions and 5 deletions

View File

@@ -24,6 +24,9 @@ public interface IPropertyType : IEntity, IRememberBeingDirty
/// </summary>
int DataTypeId { get; set; }
/// <summary>
/// Gets or sets the Guid unique identifier of the datatype for this property type.
/// </summary>
Guid DataTypeKey { get; set; }
/// <summary>

View File

@@ -47,6 +47,7 @@ public class PropertyType : EntityBase, IPropertyType, IEquatable<PropertyType>
if (dataType.HasIdentity)
{
_dataTypeId = dataType.Id;
_dataTypeKey = dataType.Key;
}
_propertyEditorAlias = dataType.EditorAlias;
@@ -159,6 +160,7 @@ public class PropertyType : EntityBase, IPropertyType, IEquatable<PropertyType>
set => SetPropertyValueAndDetectChanges(value, ref _dataTypeId, nameof(DataTypeId));
}
/// <inheritdoc />
[DataMember]
public Guid DataTypeKey
{

View File

@@ -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<TParent>
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<TParent>
return this;
}
public PropertyTypeBuilder<TParent> WithDataTypeKey(Guid dataTypeKey)
{
_dataTypeKey = dataTypeKey;
return this;
}
public PropertyTypeBuilder<TParent> WithPropertyGroupId(int propertyGroupId)
{
_propertyGroupId = new Lazy<int>(() => propertyGroupId);
@@ -176,6 +182,7 @@ public class PropertyTypeBuilder<TParent>
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<TParent>
Name = name,
SortOrder = sortOrder,
DataTypeId = dataTypeId,
DataTypeKey = dataTypeKey,
Description = description,
CreateDate = createDate,
UpdateDate = updateDate,

View File

@@ -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();
}

View File

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