Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Umbraco.Core/Models/IPropertyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 2 additions & 0 deletions src/Umbraco.Core/Models/PropertyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public PropertyType(IShortStringHelper shortStringHelper, IDataType dataType)
if (dataType.HasIdentity)
{
_dataTypeId = dataType.Id;
_dataTypeKey = dataType.Key;
}

_propertyEditorAlias = dataType.EditorAlias;
Expand Down Expand Up @@ -159,6 +160,7 @@ public int DataTypeId
set => SetPropertyValueAndDetectChanges(value, ref _dataTypeId, nameof(DataTypeId));
}

/// <inheritdoc />
[DataMember]
public Guid DataTypeKey
{
Expand Down
10 changes: 9 additions & 1 deletion tests/Umbraco.Tests.Common/Builders/PropertyTypeBuilder.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -38,6 +37,7 @@
private string _alias;
private DateTime? _createDate;
private int? _dataTypeId;
private Guid? _dataTypeKey;
private string _description;
private int? _id;
private Guid? _key;
Expand Down Expand Up @@ -132,6 +132,12 @@
return this;
}

public PropertyTypeBuilder<TParent> WithDataTypeKey(Guid dataTypeKey)
{
_dataTypeKey = dataTypeKey;
return this;
}

public PropertyTypeBuilder<TParent> WithPropertyGroupId(int propertyGroupId)
{
_propertyGroupId = new Lazy<int>(() => propertyGroupId);
Expand Down Expand Up @@ -176,26 +182,28 @@
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;
var mandatoryMessage = _mandatoryMessage ?? string.Empty;
var validationRegExp = _validationRegExp ?? string.Empty;
var validationRegExpMessage = _validationRegExpMessage ?? string.Empty;
var supportsPublishing = _supportsPublishing ?? false;
var labelOnTop = _labelOnTop ?? false;
var variations = _variations ?? ContentVariation.Nothing;

var shortStringHelper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig());

var propertyType = new PropertyType(shortStringHelper, propertyEditorAlias, valueStorageType)
{
Id = id,
Key = key,
Alias = alias,
Name = name,
SortOrder = sortOrder,
DataTypeId = dataTypeId,
DataTypeKey = dataTypeKey,

Check warning on line 206 in tests/Umbraco.Tests.Common/Builders/PropertyTypeBuilder.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

❌ Getting worse: Complex Method

Build increases in cyclomatic complexity from 19 to 20, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
Description = description,
CreateDate = createDate,
UpdateDate = updateDate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;

Expand All @@ -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 _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);

private PropertyTypeBuilder _builder;
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()
Expand All @@ -32,7 +51,7 @@ public void Can_Deep_Clone()
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);
Expand Down Expand Up @@ -69,7 +88,7 @@ public void Can_Serialize_Without_Error()
}

private PropertyType BuildPropertyType() =>
_builder
_propertyTypeBuilder
.WithId(3)
.WithPropertyEditorAlias("TestPropertyEditor")
.WithAlias("test")
Expand All @@ -81,4 +100,8 @@ private PropertyType BuildPropertyType() =>
.WithMandatory(true)
.WithValidationRegExp("xxxx")
.Build();

private DataType BuildDataType() =>
_dataTypeBuilder
.Build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void Is_Built_Correctly()
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";
Expand All @@ -43,6 +44,7 @@ public void Is_Built_Correctly()
.WithName(testName)
.WithSortOrder(testSortOrder)
.WithDataTypeId(testDataTypeId)
.WithDataTypeKey(testDataTypeKey)
.WithCreateDate(testCreateDate)
.WithUpdateDate(testUpdateDate)
.WithDescription(testDescription)
Expand All @@ -60,6 +62,7 @@ public void Is_Built_Correctly()
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);
Expand Down