From bf1e1651f4e6ce545eca4d45898b639966d6bedc Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Fri, 20 Dec 2024 17:31:56 +0100 Subject: [PATCH] Added/updated unit tests. --- .../Builders/DataTypeBuilder.cs | 11 +++- .../PropertyEditorValueConverterTests.cs | 54 +++++++++++++------ 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/tests/Umbraco.Tests.Common/Builders/DataTypeBuilder.cs b/tests/Umbraco.Tests.Common/Builders/DataTypeBuilder.cs index 9df232a5fdef..d91bd1e2724f 100644 --- a/tests/Umbraco.Tests.Common/Builders/DataTypeBuilder.cs +++ b/tests/Umbraco.Tests.Common/Builders/DataTypeBuilder.cs @@ -37,6 +37,7 @@ public class DataTypeBuilder private int? _sortOrder; private bool? _trashed; private DateTime? _updateDate; + private Dictionary _configurationData = []; public DataTypeBuilder() => _dataEditorBuilder = new DataEditorBuilder(this); @@ -118,6 +119,12 @@ public DataTypeBuilder WithDatabaseType(ValueStorageType databaseType) return this; } + public DataTypeBuilder WithConfigurationData(Dictionary configurationData) + { + _configurationData = configurationData; + return this; + } + public DataEditorBuilder AddEditor() => _dataEditorBuilder; public override DataType Build() @@ -136,6 +143,7 @@ public override DataType Build() var databaseType = _databaseType ?? ValueStorageType.Ntext; var sortOrder = _sortOrder ?? 0; var serializer = new SystemTextConfigurationEditorJsonSerializer(); + var configurationData = _configurationData; return new DataType(editor, serializer, parentId) { @@ -150,7 +158,8 @@ public override DataType Build() Path = path, CreatorId = creatorId, DatabaseType = databaseType, - SortOrder = sortOrder + SortOrder = sortOrder, + ConfigurationData = configurationData, }; } } diff --git a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/PropertyEditorValueConverterTests.cs b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/PropertyEditorValueConverterTests.cs index 461edeef699e..2a8afb7d86c4 100644 --- a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/PropertyEditorValueConverterTests.cs +++ b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/PropertyEditorValueConverterTests.cs @@ -9,6 +9,7 @@ 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,26 +47,45 @@ public void CanConvertDatePickerPropertyEditor(string date, bool expected) } } - [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, null, false)] + [TestCase(null, false, false)] + [TestCase(null, true, true)] + public void CanConvertYesNoPropertyEditor(object value, bool? initialStateConfigurationValue, bool expected) { + var dataTypeConfiguration = new Dictionary(); + if (initialStateConfigurationValue.HasValue) + { + dataTypeConfiguration.Add("default", initialStateConfigurationValue.Value); + } + + var dataType = new DataTypeBuilder() + .WithConfigurationData(dataTypeConfiguration) + .Build(); + + var publishedDataType = new PublishedDataType(dataType.Id, dataType.EditorAlias, dataType.EditorUiAlias, new Lazy(() => dataTypeConfiguration)); + + var publishedPropertyTypeMock = new Mock(); + 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 + converter.ConvertSourceToIntermediate(null, publishedPropertyTypeMock.Object, value, false); // does not use type for conversion Assert.AreEqual(expected, result); }