Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/PropertyEditors/DateTimePropertyEditorTests.cs
Nicklas Kramer fa39908899 Converting DateTime.MinValue to sqlDateTime's minimum value (#20019)
* Converting DateTime.MinValue to sqlDateTime's minimum value

* Changing code to be a bit less hacky

* Changing hard coded value to a variable based on SqlDateTime

* Removing unused code

* Moving date converter logic to DateTimePropertyEditor.cs

* Replacing tests with proper version

* Removing unused import

* Removing unused imports again

* Creating new logic, to ensure formatting is more precise.

* Rewriting tests to be more precise and include testing on odd format separators

* Used parsing to determine timeonly date picker data type configuration format.
Fixed casing on key for data type configuration format.

---------

Co-authored-by: Andy Butland <abutland73@gmail.com>
2025-09-09 09:47:49 +02:00

42 lines
2.0 KiB
C#

using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models.Editors;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Strings;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.PropertyEditors;
public class DateTimePropertyEditorTests
{
// Various time formats with years below the minimum, so we expect to increase the date to the minimum supported by SQL Server.
[TestCase("01/01/0001 10:00", "01/01/1753 10:00", "hh:mm")]
[TestCase("01/01/0001 10:00", "01/01/1753 10:00", "HH:mm")]
[TestCase("01/01/0001 10:00", "01/01/1753 10:00", "hh mm")]
[TestCase("10/10/1000 10:00", "10/10/1753 10:00", "hh:mm:ss")]
[TestCase("10/10/1000 10:00", "10/10/1753 10:00", "hh-mm-ss")]
// Time format with year above the minimum, so we expect to not convert.
[TestCase("01/01/2000 10:00", "01/01/2000 10:00", "HH:mm")]
// Date formats, so we don't convert even if the year is below the minimum.
[TestCase("01/01/0001 10:00", "01/01/0001 10:00", "dd-MM-yyyy hh:mm")]
[TestCase("01/01/0001 10:00", "01/01/0001 10:00", "dd-MM-yyyy")]
[TestCase("01/01/0001 10:00", "01/01/0001 10:00", "yyyy-MM-d")]
public void Time_Only_Format_Ensures_DateTime_Can_Be_Persisted(DateTime actualDateTime, DateTime expectedDateTime, string format)
{
var dateTimePropertyEditor = new DateTimePropertyEditor.DateTimePropertyValueEditor(
Mock.Of<IShortStringHelper>(),
Mock.Of<IJsonSerializer>(),
Mock.Of<IIOHelper>(),
new DataEditorAttribute("Alias") { ValueType = ValueTypes.DateTime.ToString() });
Dictionary<string, object> dictionary = new Dictionary<string, object> { { DateTimePropertyEditor.DateTimePropertyValueEditor.DateTypeConfigurationFormatKey, format } };
ContentPropertyData propertyData = new ContentPropertyData(actualDateTime, dictionary);
var value = (DateTime)dateTimePropertyEditor.FromEditor(propertyData, null);
Assert.AreEqual(expectedDateTime, value);
}
}