Use data type configuration to determine default value for empty toggle and slider property values (#17854)

* Use data type configuration to determine default value for empty toggle property values.

* Added/updated unit tests.

* Fixed failing integration tests.

* Applied similar default value display for the slider property editor and aligned implementation of true/false with this.

* Fixed unit tests.

* Removed "duplicate" JsonPropertyName attributes and added a custom TypeInfoResolver for data type configuration so we can re-use the existing ConfigurationField attributes.

* Minor cleanup

---------

Co-authored-by: nikolajlauridsen <nikolajlauridsen@protonmail.ch>
This commit is contained in:
Andy Butland
2025-01-08 11:42:13 +01:00
committed by GitHub
parent b7f424756c
commit 413398afc6
9 changed files with 192 additions and 41 deletions

View File

@@ -9,6 +9,7 @@ using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.PropertyEditors.ValueConverters;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Infrastructure.Serialization;
using Umbraco.Cms.Tests.Common.Builders;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.PropertyEditors;
@@ -46,30 +47,54 @@ public class PropertyEditorValueConverterTests
}
}
[TestCase("TRUE", true)]
[TestCase("True", true)]
[TestCase("true", true)]
[TestCase("1", true)]
[TestCase(1, true)]
[TestCase(true, true)]
[TestCase("FALSE", false)]
[TestCase("False", false)]
[TestCase("false", false)]
[TestCase("0", false)]
[TestCase(0, false)]
[TestCase(false, false)]
[TestCase("", false)]
[TestCase(null, false)]
[TestCase("blah", false)]
public void CanConvertYesNoPropertyEditor(object value, bool expected)
[TestCase("TRUE", null, true)]
[TestCase("True", null, true)]
[TestCase("true", null, true)]
[TestCase("1", null, true)]
[TestCase(1, null, true)]
[TestCase(true, null, true)]
[TestCase("FALSE", null, false)]
[TestCase("False", null, false)]
[TestCase("false", null, false)]
[TestCase("0", null, false)]
[TestCase(0, null, false)]
[TestCase(false, null, false)]
[TestCase("", null, false)]
[TestCase("blah", null, false)]
[TestCase(null, false, false)]
[TestCase(null, true, true)]
public void CanConvertTrueFalsePropertyEditor(object value, bool initialStateConfigurationValue, bool expected)
{
var publishedDataType = CreatePublishedDataType(initialStateConfigurationValue);
var publishedPropertyTypeMock = new Mock<IPublishedPropertyType>();
publishedPropertyTypeMock
.SetupGet(p => p.DataType)
.Returns(publishedDataType);
var converter = new YesNoValueConverter();
var result =
converter.ConvertSourceToIntermediate(null, null, value, false); // does not use type for conversion
var intermediateResult = converter.ConvertSourceToIntermediate(null, publishedPropertyTypeMock.Object, value, false);
var result = converter.ConvertIntermediateToObject(null, publishedPropertyTypeMock.Object, PropertyCacheLevel.Element, intermediateResult, false);
Assert.AreEqual(expected, result);
}
private static PublishedDataType CreatePublishedDataType(bool initialStateConfigurationValue)
{
var dataTypeConfiguration = new TrueFalseConfiguration
{
InitialState = initialStateConfigurationValue
};
var dateTypeMock = new Mock<IDataType>();
dateTypeMock.SetupGet(x => x.Id).Returns(1000);
dateTypeMock.SetupGet(x => x.EditorAlias).Returns(global::Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.Boolean);
dateTypeMock.SetupGet(x => x.EditorUiAlias).Returns("Umb.PropertyEditorUi.Toggle");
dateTypeMock.SetupGet(x => x.ConfigurationObject).Returns(dataTypeConfiguration);
return new PublishedDataType(dateTypeMock.Object.Id, dateTypeMock.Object.EditorAlias, dateTypeMock.Object.EditorUiAlias, new Lazy<object>(() => dataTypeConfiguration));
}
[TestCase("[\"apples\"]", new[] { "apples" })]
[TestCase("[\"apples\",\"oranges\"]", new[] { "apples", "oranges" })]
[TestCase("[\"apples\",\"oranges\",\"pears\"]", new[] { "apples", "oranges", "pears" })]