From d0c84ccf3bf4adbabcb1b00cc6cee85cce5b0784 Mon Sep 17 00:00:00 2001 From: Corey Date: Wed, 1 Dec 2021 14:17:03 +0000 Subject: [PATCH 1/2] [CON-4294] Fix bug with incrementing the month but not the year for future date in December --- .../MonthYearModelExtensionsTests.cs | 27 ++++++++++++++++--- .../StopRequestViewModelValidatorTests.cs | 4 ++- .../Extensions/MonthYearModelExtension.cs | 6 ++--- .../StopRequestViewModelValidator.cs | 7 +++-- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Extensions/MonthYearModelExtensionsTests.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Extensions/MonthYearModelExtensionsTests.cs index 8ed1cc0c3..34e805b59 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Extensions/MonthYearModelExtensionsTests.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Extensions/MonthYearModelExtensionsTests.cs @@ -1,7 +1,8 @@ -using NUnit.Framework; +using System; +using System.Globalization; +using NUnit.Framework; using SFA.DAS.CommitmentsV2.Shared.Models; using SFA.DAS.EmployerCommitmentsV2.Web.Extensions; -using System; namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Extensions { @@ -84,7 +85,7 @@ public void WhenDateTimeIsEqualToOrAfterMonthYearModel_IsEqualToOrBeforeMonthYea } [Test] - public void WhenDateTimeIsBeforeMonthYearModel_IsEqualToOrBeforeMonthYearOfDateTime_ReturnsTrue() + public void WhenDateTimeIsBeforeMonthYearModel_IsEqualToOrBeforeMonthYearOfDateTime_ReturnsFalse() { var dateTime = new DateTime(2020, 1, 1); @@ -92,5 +93,25 @@ public void WhenDateTimeIsBeforeMonthYearModel_IsEqualToOrBeforeMonthYearOfDateT Assert.False(actualResult); } + + [Test] + [TestCase("01/12/2021", "01/11/2021", true)] + [TestCase("01/12/2021", "01/01/2022", false)] + public void WhenDateTimeIsInFuture_IsNotInFutureMonthYear_ReturnsFalse(string nowDateString, string proposedStopDateString, bool isValid) + { + var nowDate = DateTime.ParseExact(nowDateString, + "dd/MM/yyyy", + CultureInfo.CurrentCulture); + + var proposedStopDate = DateTime.ParseExact(proposedStopDateString, + "dd/MM/yyyy", + CultureInfo.CurrentCulture); + + var monthYear = new MonthYearModel(proposedStopDate.ToString("MMyyyy")); + + var actualResult = monthYear.IsNotInFutureMonthYear(nowDate); + + Assert.AreEqual(isValid, actualResult); + } } } diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Validators/StopRequestViewModelValidatorTests.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Validators/StopRequestViewModelValidatorTests.cs index 415b3dfec..972ab5491 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Validators/StopRequestViewModelValidatorTests.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Validators/StopRequestViewModelValidatorTests.cs @@ -1,5 +1,7 @@ using FluentValidation.TestHelper; +using Moq; using NUnit.Framework; +using SFA.DAS.CommitmentsV2.Shared.Interfaces; using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice; using SFA.DAS.EmployerCommitmentsV2.Web.Validators; using System; @@ -86,7 +88,7 @@ public void WhenStopDateIsEqualToStartDate_ThenValidates(int? month, int? year) private void AssertValidationResult(Expression> property, StopRequestViewModel instance, bool expectedValid, string expectedErrorMessage = null) { - var validator = new StopRequestViewModelValidator(); + var validator = new StopRequestViewModelValidator(Mock.Of()); if (expectedValid) { diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Extensions/MonthYearModelExtension.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Extensions/MonthYearModelExtension.cs index 076daf373..3a4c29f23 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Extensions/MonthYearModelExtension.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Extensions/MonthYearModelExtension.cs @@ -20,10 +20,10 @@ public static bool IsEqualToOrBeforeMonthYearOfDateTime(this MonthYearModel mont return monthYearModel.Date.Value <= new DateTime(datetime.Year, datetime.Month, 1); } - public static bool IsNotInFutureMonthYear(this MonthYearModel monthYearModel) + public static bool IsNotInFutureMonthYear(this MonthYearModel monthYearModel, DateTime nowDateTime) { - var dateTimeNow = DateTime.UtcNow; - var futureDateAndTime = new DateTime(dateTimeNow.Year, dateTimeNow.AddMonths(1).Month, 1); + var plusOneMonth = nowDateTime.AddMonths(1); + var futureDateAndTime = new DateTime(plusOneMonth.Year, plusOneMonth.Month, 1); return monthYearModel.Date.Value < futureDateAndTime; } } diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Validators/StopRequestViewModelValidator.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Validators/StopRequestViewModelValidator.cs index e1c7046e1..4ce99de44 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Validators/StopRequestViewModelValidator.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Validators/StopRequestViewModelValidator.cs @@ -1,4 +1,5 @@ using FluentValidation; +using SFA.DAS.CommitmentsV2.Shared.Interfaces; using SFA.DAS.EmployerCommitmentsV2.Web.Extensions; using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice; @@ -6,8 +7,10 @@ namespace SFA.DAS.EmployerCommitmentsV2.Web.Validators { public class StopRequestViewModelValidator : AbstractValidator { - public StopRequestViewModelValidator() + private readonly ICurrentDateTime _currentDateTime; + public StopRequestViewModelValidator(ICurrentDateTime currentDateTime) { + _currentDateTime = currentDateTime; RuleFor(r => r.StopDate).Must((r, StopDate) => r.StopMonth.HasValue && r.StopYear.HasValue) .WithMessage("Enter the stop date for this apprenticeship") .Unless(r => r.StopYear.HasValue || r.StopMonth.HasValue); @@ -29,7 +32,7 @@ public StopRequestViewModelValidator() .When(r => r.StopDate.IsValid); RuleFor(r => r.StopDate) - .Must((r, StopDate) => StopDate.IsNotInFutureMonthYear()) + .Must((r, StopDate) => StopDate.IsNotInFutureMonthYear(_currentDateTime.UtcNow)) .WithMessage(r => $"The stop date cannot be in the future") .When(r => r.StopDate.IsValid); From d552043a4614a2ccec31dde8fdf5f9921d1ea619 Mon Sep 17 00:00:00 2001 From: Corey Date: Wed, 1 Dec 2021 14:25:38 +0000 Subject: [PATCH 2/2] [CON-4294] Fix failing unit test since adding mocked dependency --- .../Validators/StopRequestViewModelValidatorTests.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Validators/StopRequestViewModelValidatorTests.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Validators/StopRequestViewModelValidatorTests.cs index 972ab5491..2721c9750 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Validators/StopRequestViewModelValidatorTests.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Validators/StopRequestViewModelValidatorTests.cs @@ -88,7 +88,9 @@ public void WhenStopDateIsEqualToStartDate_ThenValidates(int? month, int? year) private void AssertValidationResult(Expression> property, StopRequestViewModel instance, bool expectedValid, string expectedErrorMessage = null) { - var validator = new StopRequestViewModelValidator(Mock.Of()); + var currentDateMock = new Mock(); + currentDateMock.Setup(x => x.UtcNow).Returns(DateTime.UtcNow); + var validator = new StopRequestViewModelValidator(currentDateMock.Object); if (expectedValid) {