Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Globalization;
using System.Net;
using FluentAssertions;
using HtmlAgilityPack;
using Microsoft.AspNetCore.TestHost;
Expand All @@ -18,7 +19,7 @@

public ViewFieldsBindingFixture()
{
TestServerBuilder testHostBuilder = new();

Check warning on line 22 in tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Binding/ViewFieldsBindingFixture.cs

View workflow job for this annotation

GitHub Actions / build / build

'TestServerBuilder' is obsolete: 'This class is deprecated as this is based on the old .NET Core 3.0 approach app initialisation approach. Use `TestWebApplicationFactory` approach instead.'
_mockClientHandler = new MockHttpMessageHandler();
testHostBuilder
.ConfigureServices(builder =>
Expand Down Expand Up @@ -75,7 +76,8 @@
sectionNode.ChildNodes.First(n => n.Name.Equals("p", StringComparison.OrdinalIgnoreCase)).InnerText
.Should().BeEmpty();

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

sectionNode.ChildNodes.First(n => n.Name.Equals("span", StringComparison.OrdinalIgnoreCase)).InnerHtml
.Should().Be(TestConstants.TestMultilineFieldValue.Replace(Environment.NewLine, "<br>", StringComparison.OrdinalIgnoreCase));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public async Task ComponentWithAllFieldTypes_RendersFieldsCorrectly()
sectionNode.ChildNodes.First(n => n.Name.Equals("div", StringComparison.OrdinalIgnoreCase) && n.Id.Equals("div5", StringComparison.OrdinalIgnoreCase)).InnerHtml
.Should().Be(TestConstants.AllFieldsImageValue);
sectionNode.ChildNodes.First(n => n.Name.Equals("div", StringComparison.OrdinalIgnoreCase) && n.Id.Equals("div6", StringComparison.OrdinalIgnoreCase)).InnerHtml
.Should().Be(TestConstants.DateFieldValue);
.Should().Be(TestConstants.DateTimeValue.ToString("MM/dd/yyyy", CultureInfo.CurrentCulture));
sectionNode.ChildNodes.First(n => n.Name.Equals("div", StringComparison.OrdinalIgnoreCase) && n.Id.Equals("div7", StringComparison.OrdinalIgnoreCase)).InnerHtml
.Should().Be(TestConstants.MediaLibraryItemImageFieldValue);
sectionNode.ChildNodes.First(n => n.Name.Equals("div", StringComparison.OrdinalIgnoreCase) && n.Id.Equals("div8", StringComparison.OrdinalIgnoreCase)).InnerHtml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public async Task DateTagHelper_GeneratesProperDate()
HtmlNode? sectionNode = doc.DocumentNode.ChildNodes.First(n => n.HasClass("component-with-dates"));

// Assert
sectionNode.ChildNodes[1].InnerHtml.Should().Be("05/04/2012");
sectionNode.ChildNodes[3].InnerHtml.Should().Be("05/04/2012 00:00:00");
sectionNode.ChildNodes[1].InnerHtml.Should().Be(TestConstants.DateTimeValue.ToString("MM/dd/yyyy", CultureInfo.CurrentCulture));
sectionNode.ChildNodes[3].InnerHtml.Should().Be(TestConstants.DateTimeValue.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.CurrentCulture));
sectionNode.ChildNodes[5].InnerHtml.Should().Be(TestConstants.DateTimeValue.ToString(CultureInfo.CurrentCulture));
sectionNode.ChildNodes[9].InnerHtml.Should().Contain("04.05.2012");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Globalization;
using System.Net;
using System.Text.Encodings.Web;
using FluentAssertions;
using HtmlAgilityPack;
Expand Down Expand Up @@ -70,7 +71,8 @@ public async Task RichTextFieldTagHelper_DoesNotResetOtherTagHelperOutput()

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

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,46 @@ public void Process_ScDateTagWithEmptyValueInForAttribute_GeneratesEmptyOutput(
}

[Theory]
[AutoNSubstituteData]
public void Process_ScDateTagWithCustomFormat_GeneratesCustomDateFormatOutput(
DateTagHelper sut,
TagHelperContext tagHelperContext,
TagHelperOutput tagHelperOutput)
[InlineData("en-US")]
[InlineData("da-DK")]
[InlineData("uk-UA")]
public void Process_ScDateTagWithCustomFormat_GeneratesCustomDateFormatOutput(string cultureName)
{
// Arrange
const string dateFormat = "MM/dd/yyyy H:mm";
tagHelperOutput.TagName = RenderingEngineConstants.SitecoreTagHelpers.DateHtmlTag;
sut.DateFormat = dateFormat;
sut.For = GetModelExpression(new DateField(_date));
CultureInfo originalCulture = CultureInfo.CurrentCulture;
CultureInfo originalUiCulture = CultureInfo.CurrentUICulture;
CultureInfo testCulture = new CultureInfo(cultureName);

// Act
sut.Process(tagHelperContext, tagHelperOutput);

// Assert
tagHelperOutput.Content.GetContent().Should().Be(_date.ToString(dateFormat, CultureInfo.InvariantCulture));
try
{
CultureInfo.CurrentCulture = testCulture;
CultureInfo.CurrentUICulture = testCulture;

const string dateFormat = "MM/dd/yyyy H:mm";
DateTagHelper sut = new DateTagHelper(new EditableChromeRenderer());
TagHelperContext tagHelperContext = new TagHelperContext([], new Dictionary<object, object>(), Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture));
TagHelperOutput tagHelperOutput = new TagHelperOutput(string.Empty, [], (_, _) =>
{
DefaultTagHelperContent tagHelperContent = new();
return Task.FromResult<TagHelperContent>(tagHelperContent);
});

tagHelperOutput.TagName = RenderingEngineConstants.SitecoreTagHelpers.DateHtmlTag;
sut.DateFormat = dateFormat;
sut.For = GetModelExpression(new DateField(_date));

// Act
sut.Process(tagHelperContext, tagHelperOutput);

// Assert - Expect culture-specific formatting based on current culture
string expected = _date.ToString(dateFormat, testCulture);
tagHelperOutput.Content.GetContent().Should().Be(expected);
}
finally
{
CultureInfo.CurrentCulture = originalCulture;
CultureInfo.CurrentUICulture = originalUiCulture;
}
}

[Theory]
Expand Down Expand Up @@ -258,23 +281,46 @@ public void Process_ScDateTagWithAspDataAttributeWithEmptyValueInForAttribute_Ge
}

[Theory]
[AutoNSubstituteData]
public void Process_ScDateTagWithAspDataAttributeWithCustomFormat_GeneratesCustomDateFormatOutput(
DateTagHelper sut,
TagHelperContext tagHelperContext,
TagHelperOutput tagHelperOutput)
[InlineData("en-US")]
[InlineData("da-DK")]
[InlineData("uk-UA")]
public void Process_ScDateTagWithAspDataAttributeWithCustomFormat_GeneratesCustomDateFormatOutput(string cultureName)
{
// Arrange
string dateFormat = "MM/dd/yyyy H:mm";
tagHelperOutput.TagName = RenderingEngineConstants.SitecoreTagHelpers.DateHtmlTag;
sut.DateFormat = dateFormat;
sut.DateModel = new DateField(_date);
CultureInfo originalCulture = CultureInfo.CurrentCulture;
CultureInfo originalUiCulture = CultureInfo.CurrentUICulture;
CultureInfo testCulture = new CultureInfo(cultureName);

// Act
sut.Process(tagHelperContext, tagHelperOutput);

// Assert
tagHelperOutput.Content.GetContent().Should().Be(_date.ToString(dateFormat, CultureInfo.InvariantCulture));
try
{
CultureInfo.CurrentCulture = testCulture;
CultureInfo.CurrentUICulture = testCulture;

string dateFormat = "MM/dd/yyyy H:mm";
DateTagHelper sut = new DateTagHelper(new EditableChromeRenderer());
TagHelperContext tagHelperContext = new TagHelperContext([], new Dictionary<object, object>(), Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture));
TagHelperOutput tagHelperOutput = new TagHelperOutput(string.Empty, [], (_, _) =>
{
DefaultTagHelperContent tagHelperContent = new();
return Task.FromResult<TagHelperContent>(tagHelperContent);
});

tagHelperOutput.TagName = RenderingEngineConstants.SitecoreTagHelpers.DateHtmlTag;
sut.DateFormat = dateFormat;
sut.DateModel = new DateField(_date);

// Act
sut.Process(tagHelperContext, tagHelperOutput);

// Assert - Expect culture-specific formatting based on current culture
string expected = _date.ToString(dateFormat, testCulture);
tagHelperOutput.Content.GetContent().Should().Be(expected);
}
finally
{
CultureInfo.CurrentCulture = originalCulture;
CultureInfo.CurrentUICulture = originalUiCulture;
}
}

[Theory]
Expand Down
Loading