Skip to content

Commit c1b531f

Browse files
fix: Update date formatting tests to handle culture-specific formats - Convert DateTagHelper unit tests to use InlineData pattern with multiple cultures
1 parent acaaf29 commit c1b531f

File tree

5 files changed

+85
-35
lines changed

5 files changed

+85
-35
lines changed

tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/ViewFieldsBindingFixture.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net;
1+
using System.Globalization;
2+
using System.Net;
23
using FluentAssertions;
34
using HtmlAgilityPack;
45
using Microsoft.AspNetCore.TestHost;
@@ -75,7 +76,8 @@ public async Task SitecoreLayoutModelBinders_BindDataCorrectly()
7576
sectionNode.ChildNodes.First(n => n.Name.Equals("p", StringComparison.OrdinalIgnoreCase)).InnerText
7677
.Should().BeEmpty();
7778

78-
sectionNode.ChildNodes.First(n => n.Name.Equals("textarea", StringComparison.OrdinalIgnoreCase)).InnerText.Should().Contain("12/12/2019");
79+
DateTime expectedDate = DateTime.Parse("12.12.19", CultureInfo.InvariantCulture);
80+
sectionNode.ChildNodes.First(n => n.Name.Equals("textarea", StringComparison.OrdinalIgnoreCase)).InnerText.Should().Contain(expectedDate.ToString("MM/dd/yyyy", CultureInfo.CurrentCulture));
7981

8082
sectionNode.ChildNodes.First(n => n.Name.Equals("span", StringComparison.OrdinalIgnoreCase)).InnerHtml
8183
.Should().Be(TestConstants.TestMultilineFieldValue.Replace(Environment.NewLine, "<br>", StringComparison.OrdinalIgnoreCase));

tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/TagHelpers/AllFieldTagHelpersFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public async Task ComponentWithAllFieldTypes_RendersFieldsCorrectly()
8282
sectionNode.ChildNodes.First(n => n.Name.Equals("div", StringComparison.OrdinalIgnoreCase) && n.Id.Equals("div5", StringComparison.OrdinalIgnoreCase)).InnerHtml
8383
.Should().Be(TestConstants.AllFieldsImageValue);
8484
sectionNode.ChildNodes.First(n => n.Name.Equals("div", StringComparison.OrdinalIgnoreCase) && n.Id.Equals("div6", StringComparison.OrdinalIgnoreCase)).InnerHtml
85-
.Should().Be(TestConstants.DateFieldValue);
85+
.Should().Be(TestConstants.DateTimeValue.ToString("MM/dd/yyyy", CultureInfo.CurrentCulture));
8686
sectionNode.ChildNodes.First(n => n.Name.Equals("div", StringComparison.OrdinalIgnoreCase) && n.Id.Equals("div7", StringComparison.OrdinalIgnoreCase)).InnerHtml
8787
.Should().Be(TestConstants.MediaLibraryItemImageFieldValue);
8888
sectionNode.ChildNodes.First(n => n.Name.Equals("div", StringComparison.OrdinalIgnoreCase) && n.Id.Equals("div8", StringComparison.OrdinalIgnoreCase)).InnerHtml

tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/TagHelpers/DateFieldTagHelperFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public async Task DateTagHelper_GeneratesProperDate()
9393
HtmlNode? sectionNode = doc.DocumentNode.ChildNodes.First(n => n.HasClass("component-with-dates"));
9494

9595
// Assert
96-
sectionNode.ChildNodes[1].InnerHtml.Should().Be("05/04/2012");
97-
sectionNode.ChildNodes[3].InnerHtml.Should().Be("05/04/2012 00:00:00");
96+
sectionNode.ChildNodes[1].InnerHtml.Should().Be(TestConstants.DateTimeValue.ToString("MM/dd/yyyy", CultureInfo.CurrentCulture));
97+
sectionNode.ChildNodes[3].InnerHtml.Should().Be(TestConstants.DateTimeValue.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.CurrentCulture));
9898
sectionNode.ChildNodes[5].InnerHtml.Should().Be(TestConstants.DateTimeValue.ToString(CultureInfo.CurrentCulture));
9999
sectionNode.ChildNodes[9].InnerHtml.Should().Contain("04.05.2012");
100100
}

tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/TagHelpers/RichTextFieldTagHelperFixture.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net;
1+
using System.Globalization;
2+
using System.Net;
23
using System.Text.Encodings.Web;
34
using FluentAssertions;
45
using HtmlAgilityPack;
@@ -70,7 +71,8 @@ public async Task RichTextFieldTagHelper_DoesNotResetOtherTagHelperOutput()
7071

7172
// Assert
7273
// check scenario that RichTextTagHelper does not reset values of another helpers.
73-
sectionNode.ChildNodes.First(n => n.Name.Equals("textarea", StringComparison.OrdinalIgnoreCase)).InnerText.Should().Contain("12/12/2019");
74+
DateTime expectedDate = DateTime.Parse("12.12.19", CultureInfo.InvariantCulture);
75+
sectionNode.ChildNodes.First(n => n.Name.Equals("textarea", StringComparison.OrdinalIgnoreCase)).InnerText.Should().Contain(expectedDate.ToString("MM/dd/yyyy", CultureInfo.CurrentCulture));
7476
}
7577

7678
[Fact]

tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/Fields/DateTagHelperFixture.cs

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,46 @@ public void Process_ScDateTagWithEmptyValueInForAttribute_GeneratesEmptyOutput(
123123
}
124124

125125
[Theory]
126-
[AutoNSubstituteData]
127-
public void Process_ScDateTagWithCustomFormat_GeneratesCustomDateFormatOutput(
128-
DateTagHelper sut,
129-
TagHelperContext tagHelperContext,
130-
TagHelperOutput tagHelperOutput)
126+
[InlineData("en-US")]
127+
[InlineData("da-DK")]
128+
[InlineData("uk-UA")]
129+
public void Process_ScDateTagWithCustomFormat_GeneratesCustomDateFormatOutput(string cultureName)
131130
{
132131
// Arrange
133-
const string dateFormat = "MM/dd/yyyy H:mm";
134-
tagHelperOutput.TagName = RenderingEngineConstants.SitecoreTagHelpers.DateHtmlTag;
135-
sut.DateFormat = dateFormat;
136-
sut.For = GetModelExpression(new DateField(_date));
132+
CultureInfo originalCulture = CultureInfo.CurrentCulture;
133+
CultureInfo originalUiCulture = CultureInfo.CurrentUICulture;
134+
CultureInfo testCulture = new CultureInfo(cultureName);
137135

138-
// Act
139-
sut.Process(tagHelperContext, tagHelperOutput);
140-
141-
// Assert
142-
tagHelperOutput.Content.GetContent().Should().Be(_date.ToString(dateFormat, CultureInfo.InvariantCulture));
136+
try
137+
{
138+
CultureInfo.CurrentCulture = testCulture;
139+
CultureInfo.CurrentUICulture = testCulture;
140+
141+
const string dateFormat = "MM/dd/yyyy H:mm";
142+
DateTagHelper sut = new DateTagHelper(new EditableChromeRenderer());
143+
TagHelperContext tagHelperContext = new TagHelperContext([], new Dictionary<object, object>(), Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture));
144+
TagHelperOutput tagHelperOutput = new TagHelperOutput(string.Empty, [], (_, _) =>
145+
{
146+
DefaultTagHelperContent tagHelperContent = new();
147+
return Task.FromResult<TagHelperContent>(tagHelperContent);
148+
});
149+
150+
tagHelperOutput.TagName = RenderingEngineConstants.SitecoreTagHelpers.DateHtmlTag;
151+
sut.DateFormat = dateFormat;
152+
sut.For = GetModelExpression(new DateField(_date));
153+
154+
// Act
155+
sut.Process(tagHelperContext, tagHelperOutput);
156+
157+
// Assert - Expect culture-specific formatting based on current culture
158+
string expected = _date.ToString(dateFormat, testCulture);
159+
tagHelperOutput.Content.GetContent().Should().Be(expected);
160+
}
161+
finally
162+
{
163+
CultureInfo.CurrentCulture = originalCulture;
164+
CultureInfo.CurrentUICulture = originalUiCulture;
165+
}
143166
}
144167

145168
[Theory]
@@ -258,23 +281,46 @@ public void Process_ScDateTagWithAspDataAttributeWithEmptyValueInForAttribute_Ge
258281
}
259282

260283
[Theory]
261-
[AutoNSubstituteData]
262-
public void Process_ScDateTagWithAspDataAttributeWithCustomFormat_GeneratesCustomDateFormatOutput(
263-
DateTagHelper sut,
264-
TagHelperContext tagHelperContext,
265-
TagHelperOutput tagHelperOutput)
284+
[InlineData("en-US")]
285+
[InlineData("da-DK")]
286+
[InlineData("uk-UA")]
287+
public void Process_ScDateTagWithAspDataAttributeWithCustomFormat_GeneratesCustomDateFormatOutput(string cultureName)
266288
{
267289
// Arrange
268-
string dateFormat = "MM/dd/yyyy H:mm";
269-
tagHelperOutput.TagName = RenderingEngineConstants.SitecoreTagHelpers.DateHtmlTag;
270-
sut.DateFormat = dateFormat;
271-
sut.DateModel = new DateField(_date);
290+
CultureInfo originalCulture = CultureInfo.CurrentCulture;
291+
CultureInfo originalUiCulture = CultureInfo.CurrentUICulture;
292+
CultureInfo testCulture = new CultureInfo(cultureName);
272293

273-
// Act
274-
sut.Process(tagHelperContext, tagHelperOutput);
275-
276-
// Assert
277-
tagHelperOutput.Content.GetContent().Should().Be(_date.ToString(dateFormat, CultureInfo.InvariantCulture));
294+
try
295+
{
296+
CultureInfo.CurrentCulture = testCulture;
297+
CultureInfo.CurrentUICulture = testCulture;
298+
299+
string dateFormat = "MM/dd/yyyy H:mm";
300+
DateTagHelper sut = new DateTagHelper(new EditableChromeRenderer());
301+
TagHelperContext tagHelperContext = new TagHelperContext([], new Dictionary<object, object>(), Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture));
302+
TagHelperOutput tagHelperOutput = new TagHelperOutput(string.Empty, [], (_, _) =>
303+
{
304+
DefaultTagHelperContent tagHelperContent = new();
305+
return Task.FromResult<TagHelperContent>(tagHelperContent);
306+
});
307+
308+
tagHelperOutput.TagName = RenderingEngineConstants.SitecoreTagHelpers.DateHtmlTag;
309+
sut.DateFormat = dateFormat;
310+
sut.DateModel = new DateField(_date);
311+
312+
// Act
313+
sut.Process(tagHelperContext, tagHelperOutput);
314+
315+
// Assert - Expect culture-specific formatting based on current culture
316+
string expected = _date.ToString(dateFormat, testCulture);
317+
tagHelperOutput.Content.GetContent().Should().Be(expected);
318+
}
319+
finally
320+
{
321+
CultureInfo.CurrentCulture = originalCulture;
322+
CultureInfo.CurrentUICulture = originalUiCulture;
323+
}
278324
}
279325

280326
[Theory]

0 commit comments

Comments
 (0)