Skip to content

Commit 29b6c2d

Browse files
authored
Assert dates in content editing integration tests to millisecond only (#19513)
* Assert dates in content editing integration tests to millisecond only. * Add date time extension unit tests and refactor to switch statement. * Removed whitespace.
1 parent c223d93 commit 29b6c2d

File tree

3 files changed

+70
-32
lines changed

3 files changed

+70
-32
lines changed

src/Umbraco.Core/Extensions/DateTimeExtensions.cs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace Umbraco.Extensions;
77

8+
/// <summary>
9+
/// Provides Extensions for <see cref="DateTime"/>.
10+
/// </summary>
811
public static class DateTimeExtensions
912
{
1013
/// <summary>
@@ -18,6 +21,7 @@ public enum DateTruncate
1821
Hour,
1922
Minute,
2023
Second,
24+
Millisecond,
2125
}
2226

2327
/// <summary>
@@ -35,32 +39,15 @@ public static string ToIsoString(this DateTime dt) =>
3539
/// <param name="truncateTo">The level to truncate the date to.</param>
3640
/// <returns>The truncated date.</returns>
3741
public static DateTime TruncateTo(this DateTime dt, DateTruncate truncateTo)
38-
{
39-
if (truncateTo == DateTruncate.Year)
40-
{
41-
return new DateTime(dt.Year, 1, 1, 0, 0, 0, dt.Kind);
42-
}
43-
44-
if (truncateTo == DateTruncate.Month)
42+
=> truncateTo switch
4543
{
46-
return new DateTime(dt.Year, dt.Month, 1, 0, 0, 0, dt.Kind);
47-
}
48-
49-
if (truncateTo == DateTruncate.Day)
50-
{
51-
return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, dt.Kind);
52-
}
53-
54-
if (truncateTo == DateTruncate.Hour)
55-
{
56-
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0, dt.Kind);
57-
}
58-
59-
if (truncateTo == DateTruncate.Minute)
60-
{
61-
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0, dt.Kind);
62-
}
63-
64-
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Kind);
65-
}
44+
DateTruncate.Year => new DateTime(dt.Year, 1, 1, 0, 0, 0, dt.Kind),
45+
DateTruncate.Month => new DateTime(dt.Year, dt.Month, 1, 0, 0, 0, dt.Kind),
46+
DateTruncate.Day => new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, dt.Kind),
47+
DateTruncate.Hour => new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0, dt.Kind),
48+
DateTruncate.Minute => new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0, dt.Kind),
49+
DateTruncate.Second => new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Kind),
50+
DateTruncate.Millisecond => new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond, dt.Kind),
51+
_ => throw new ArgumentOutOfRangeException(nameof(truncateTo), truncateTo, "Invalid truncation level"),
52+
};
6653
}

tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentEditingServiceTests.Update.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ public async Task Cannot_Update_Variant_Readonly_Property_Value()
569569
}
570570

571571
[Test]
572-
public async Task Updating_Single_Variant_Name_Does_Not_Change_Update_Dates_Of_Other_Vaiants()
572+
public async Task Updating_Single_Variant_Name_Does_Not_Change_Update_Dates_Of_Other_Variants()
573573
{
574574
var contentType = await CreateVariantContentType(variantTitleAsMandatory: false);
575575

@@ -614,11 +614,11 @@ public async Task Updating_Single_Variant_Name_Does_Not_Change_Update_Dates_Of_O
614614
void VerifyUpdate(IContent? updatedContent)
615615
{
616616
Assert.IsNotNull(updatedContent);
617-
Assert.AreEqual(firstUpdateDateDa, updatedContent.GetUpdateDate("da-DK"));
617+
Assert.AreEqual(firstUpdateDateDa?.TruncateTo(DateTimeExtensions.DateTruncate.Millisecond), updatedContent.GetUpdateDate("da-DK")?.TruncateTo(DateTimeExtensions.DateTruncate.Millisecond));
618618

619619
var lastUpdateDateEn = updatedContent.GetUpdateDate("en-US")
620620
?? throw new InvalidOperationException("Expected a publish date for EN");
621-
Assert.Greater(lastUpdateDateEn, firstUpdateDateEn);
621+
Assert.Greater(lastUpdateDateEn.TruncateTo(DateTimeExtensions.DateTruncate.Millisecond), firstUpdateDateEn?.TruncateTo(DateTimeExtensions.DateTruncate.Millisecond));
622622
}
623623
}
624624

@@ -671,11 +671,11 @@ public async Task Updating_Single_Variant_Property_Does_Not_Change_Update_Dates_
671671
void VerifyUpdate(IContent? updatedContent)
672672
{
673673
Assert.IsNotNull(updatedContent);
674-
Assert.AreEqual(firstUpdateDateEn, updatedContent.GetUpdateDate("en-US"));
674+
Assert.AreEqual(firstUpdateDateEn.TruncateTo(DateTimeExtensions.DateTruncate.Millisecond), updatedContent.GetUpdateDate("en-US")?.TruncateTo(DateTimeExtensions.DateTruncate.Millisecond));
675675

676676
var lastUpdateDateDa = updatedContent.GetUpdateDate("da-DK")
677677
?? throw new InvalidOperationException("Expected an update date for DA");
678-
Assert.Greater(lastUpdateDateDa, firstUpdateDateDa);
678+
Assert.Greater(lastUpdateDateDa.TruncateTo(DateTimeExtensions.DateTruncate.Millisecond), firstUpdateDateDa.TruncateTo(DateTimeExtensions.DateTruncate.Millisecond));
679679
}
680680
}
681681
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Umbraco.
2+
// See LICENSE for more details.
3+
4+
using NUnit.Framework;
5+
using Umbraco.Extensions;
6+
7+
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Extensions;
8+
9+
[TestFixture]
10+
public class DateTimeExtensionsTests
11+
{
12+
[Test]
13+
public void ToIsoString_ReturnsCorrectFormat()
14+
{
15+
var date = new DateTime(2025, 6, 9, 14, 30, 45, DateTimeKind.Utc);
16+
var result = date.ToIsoString();
17+
Assert.AreEqual("2025-06-09 14:30:45", result);
18+
}
19+
20+
[TestCase(2023, 5, 15, 14, 30, 45, 123, DateTimeExtensions.DateTruncate.Year, 2023, 1, 1, 0, 0, 0, 0)]
21+
[TestCase(2023, 5, 15, 14, 30, 45, 123, DateTimeExtensions.DateTruncate.Month, 2023, 5, 1, 0, 0, 0, 0)]
22+
[TestCase(2023, 5, 15, 14, 30, 45, 123, DateTimeExtensions.DateTruncate.Day, 2023, 5, 15, 0, 0, 0, 0)]
23+
[TestCase(2023, 5, 15, 14, 30, 45, 123, DateTimeExtensions.DateTruncate.Hour, 2023, 5, 15, 14, 0, 0, 0)]
24+
[TestCase(2023, 5, 15, 14, 30, 45, 123, DateTimeExtensions.DateTruncate.Minute, 2023, 5, 15, 14, 30, 0, 0)]
25+
[TestCase(2023, 5, 15, 14, 30, 45, 123, DateTimeExtensions.DateTruncate.Second, 2023, 5, 15, 14, 30, 45, 0)]
26+
[TestCase(2023, 5, 15, 14, 30, 45, 123, DateTimeExtensions.DateTruncate.Millisecond, 2023, 5, 15, 14, 30, 45, 123)]
27+
public void TruncateTo_TruncatesCorrectly(
28+
int year,
29+
int month,
30+
int day,
31+
int hour,
32+
int minute,
33+
int second,
34+
int millisecond,
35+
DateTimeExtensions.DateTruncate truncateTo,
36+
int expectedYear,
37+
int expectedMonth,
38+
int expectedDay,
39+
int expectedHour,
40+
int expectedMinute,
41+
int expectedSecond,
42+
int expectedMillisecond)
43+
{
44+
var date = new DateTime(year, month, day, hour, minute, second, millisecond, DateTimeKind.Utc);
45+
var expected = new DateTime(expectedYear, expectedMonth, expectedDay, expectedHour, expectedMinute, expectedSecond, expectedMillisecond, DateTimeKind.Utc);
46+
47+
var result = date.TruncateTo(truncateTo);
48+
49+
Assert.AreEqual(expected, result);
50+
}
51+
}

0 commit comments

Comments
 (0)