Update the existing date/time property editor to provide a kind of unspecified (#19727)
* Update the existing date/time property editor to provide a Kind of Unspecified * Added unit tests
This commit is contained in:
@@ -20,19 +20,16 @@ public class DatePickerValueConverter : PropertyValueConverterBase
|
||||
|
||||
internal static DateTime ParseDateTimeValue(object? source)
|
||||
{
|
||||
if (source is null)
|
||||
{
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
|
||||
if (source is DateTime dateTimeValue)
|
||||
{
|
||||
return dateTimeValue;
|
||||
return DateTime.SpecifyKind(dateTimeValue, DateTimeKind.Unspecified);
|
||||
}
|
||||
|
||||
Attempt<DateTime> attempt = source.TryConvertTo<DateTime>();
|
||||
return attempt.Success
|
||||
? attempt.Result
|
||||
: DateTime.MinValue;
|
||||
if (source.TryConvertTo<DateTime>() is { Success: true } attempt)
|
||||
{
|
||||
return DateTime.SpecifyKind(attempt.Result, DateTimeKind.Unspecified);
|
||||
}
|
||||
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Core.PropertyEditors.ValueConverters;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.PropertyEditors.ValueConverters;
|
||||
|
||||
[TestFixture]
|
||||
public class DatePickerValueConverterTests
|
||||
{
|
||||
private static object[] _parseDateTimeValueCases =
|
||||
[
|
||||
new object[] { null, DateTime.MinValue },
|
||||
new object[] { DateTime.MinValue, DateTime.MinValue },
|
||||
new object[] { new DateTime(2021, 01, 20, 9, 0, 36), new DateTime(2021, 01, 20, 9, 0, 36) },
|
||||
new object[] { "2021-01-20T09:00:36", new DateTime(2021, 01, 20, 9, 0, 36) },
|
||||
new object[] { "test", DateTime.MinValue },
|
||||
];
|
||||
|
||||
[TestCaseSource(nameof(_parseDateTimeValueCases))]
|
||||
public void Can_Parse_DateTime_Value(object? input, DateTime expected)
|
||||
{
|
||||
var result = DatePickerValueConverter.ParseDateTimeValue(input);
|
||||
Assert.AreEqual(expected, result);
|
||||
Assert.AreEqual(DateTimeKind.Unspecified, result.Kind);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user