Skip to content

Commit

Permalink
Added/updated unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyButland committed Dec 20, 2024
1 parent 4ed06e4 commit bf1e165
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
11 changes: 10 additions & 1 deletion tests/Umbraco.Tests.Common/Builders/DataTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class DataTypeBuilder
private int? _sortOrder;
private bool? _trashed;
private DateTime? _updateDate;
private Dictionary<string, object> _configurationData = [];

public DataTypeBuilder() => _dataEditorBuilder = new DataEditorBuilder<DataTypeBuilder>(this);

Expand Down Expand Up @@ -118,6 +119,12 @@ public DataTypeBuilder WithDatabaseType(ValueStorageType databaseType)
return this;
}

public DataTypeBuilder WithConfigurationData(Dictionary<string, object> configurationData)
{
_configurationData = configurationData;
return this;
}

public DataEditorBuilder<DataTypeBuilder> AddEditor() => _dataEditorBuilder;

public override DataType Build()
Expand All @@ -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)
{
Expand All @@ -150,7 +158,8 @@ public override DataType Build()
Path = path,
CreatorId = creatorId,
DatabaseType = databaseType,
SortOrder = sortOrder
SortOrder = sortOrder,
ConfigurationData = configurationData,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<string, object>();
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<object>(() => dataTypeConfiguration));

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
converter.ConvertSourceToIntermediate(null, publishedPropertyTypeMock.Object, value, false); // does not use type for conversion

Assert.AreEqual(expected, result);
}
Expand Down

0 comments on commit bf1e165

Please sign in to comment.