Skip to content

Commit

Permalink
Merge pull request #278 from SkillsFundingAgency/CON-4294_December-st…
Browse files Browse the repository at this point in the history
…op-date-bug

[CON-4294] Fix bug with incrementing the month but not the year for f…
  • Loading branch information
Najamuddin-Muhammad authored Dec 1, 2021
2 parents d9b4a76 + d552043 commit b275230
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -84,13 +85,33 @@ public void WhenDateTimeIsEqualToOrAfterMonthYearModel_IsEqualToOrBeforeMonthYea
}

[Test]
public void WhenDateTimeIsBeforeMonthYearModel_IsEqualToOrBeforeMonthYearOfDateTime_ReturnsTrue()
public void WhenDateTimeIsBeforeMonthYearModel_IsEqualToOrBeforeMonthYearOfDateTime_ReturnsFalse()
{
var dateTime = new DateTime(2020, 1, 1);

var actualResult = _monthYear.IsEqualToOrBeforeMonthYearOfDateTime(dateTime);

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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -86,7 +88,9 @@ public void WhenStopDateIsEqualToStartDate_ThenValidates(int? month, int? year)

private void AssertValidationResult<T>(Expression<Func<StopRequestViewModel, T>> property, StopRequestViewModel instance, bool expectedValid, string expectedErrorMessage = null)
{
var validator = new StopRequestViewModelValidator();
var currentDateMock = new Mock<ICurrentDateTime>();
currentDateMock.Setup(x => x.UtcNow).Returns(DateTime.UtcNow);
var validator = new StopRequestViewModelValidator(currentDateMock.Object);

if (expectedValid)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using FluentValidation;
using SFA.DAS.CommitmentsV2.Shared.Interfaces;
using SFA.DAS.EmployerCommitmentsV2.Web.Extensions;
using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice;

namespace SFA.DAS.EmployerCommitmentsV2.Web.Validators
{
public class StopRequestViewModelValidator : AbstractValidator<StopRequestViewModel>
{
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);
Expand All @@ -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);

Expand Down

0 comments on commit b275230

Please sign in to comment.