Skip to content

Commit

Permalink
Making Culture Invariant When Validating Date to negate deferent form…
Browse files Browse the repository at this point in the history
…ats (#17257)

* CultureInfo.InvariantCulture,

* adding unitTests by @Matthew-Wise
  • Loading branch information
IbrahimMNada authored Oct 12, 2024
1 parent 421a3a9 commit dabaeac
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Umbraco.Extensions;

namespace Umbraco.Cms.Core.PropertyEditors.Validators;
Expand All @@ -16,7 +17,7 @@ public IEnumerable<ValidationResult> Validate(object? value, string? valueType,
yield break;
}

if (DateTime.TryParse(value.ToString(), out DateTime dt) == false)
if (DateTime.TryParse(value.ToString(), CultureInfo.InvariantCulture, out DateTime dt) == false)
{
yield return new ValidationResult(
string.Format("The string value {0} cannot be parsed into a DateTime", value),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Globalization;
using NUnit.Framework;
using Umbraco.Cms.Core.PropertyEditors.Validators;

namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.PropertyEditors.Validators;

[TestFixture]
public class DateTimeValidatorTests
{
[TestCase("en-US", "yyyy-MM-dd HH:mm:ss", TestName = "US Thread, DateTimeValidator")]
[TestCase("en-US", "dd-MM-yyyy HH:mm:ss", TestName = "US Thread, DateTimeValidator ar-SA format")]
[TestCase("ar-SA", "dd-MM-yyyy HH:mm:ss", TestName = "Arabian Saudi Thread, DateTimeValidator")]
[TestCase("ar-SA", "yyyy-MM-dd HH:mm:ss", TestName = "Arabian Saudi Thread, DateTimeValidator US format")]
public void DateTimeValidatorIsCultureInvariant(string culture, string format)
{
var dateString = DateTime.Now.ToString(format);

var cultureInfo = new CultureInfo(culture);
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;

var validator = new DateTimeValidator();
var validationResults = validator.Validate(dateString, "DATETIME", new Dictionary<string, object>
{
["format"] = format
});
CollectionAssert.IsEmpty(validationResults);
}
}

0 comments on commit dabaeac

Please sign in to comment.