Skip to content

Commit

Permalink
fix: Adjust data set expiry day comparison (#19806)
Browse files Browse the repository at this point in the history
* Fix expiry day comparison
  • Loading branch information
jason-p-pickering authored Jan 30, 2025
1 parent eabb9bc commit 7d596bd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,12 @@ public boolean isLocked(UserDetails user, Period period, Date now) {
|| user != null && user.isAuthorized(Authorities.F_EDIT_EXPIRED.name())) {
return false;
}

// Adds effectively 1 day to the end date since the date's time portion is set to 00:00.
// This means the comparison starts on the day after the end date at midnight.
Date dataEntryOpenedDate = addDays(period.getEndDate(), 1);
Date date = now != null ? now : new Date();
return !Period.isDateWithTimeInTimeFrame(null, addDays(period.getEndDate(), expiryDays), date);
return !Period.isDateWithTimeInTimeFrame(null, addDays(dataEntryOpenedDate, expiryDays), date);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
*/
package org.hisp.dhis.dataset;

import static org.hisp.dhis.util.DateUtils.addDays;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.common.collect.Sets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.function.Function;
import org.hisp.dhis.dataelement.DataElement;
Expand Down Expand Up @@ -123,29 +125,34 @@ void testIsLocked_LastDayOfPeriod() {
assertIsLocked(false, Period::getEndDate);
}

@Test
void testIsLocked_AfterLastDayOfPeriod() {
// 12 hours after the end is still ok (12/24 = 0.5 days)
assertIsLocked(false, period -> addDays(period.getEndDate(), 0.5));

// expiryDays is 1 so 1 extra day after the end is still ok
assertIsLocked(false, period -> addDays(period.getEndDate(), 1));

// 1.5 days after the end is too much
assertIsLocked(true, period -> addDays(period.getEndDate(), 1.5));

// 2 days after the end is too much
assertIsLocked(true, period -> addDays(period.getEndDate(), 2));

// 60 hours is 2.5 days (60 / 24) which is too much
assertIsLocked(true, period -> addDays(period.getEndDate(), 2.5));
}

private static void assertIsLocked(boolean expected, Function<Period, Date> actual) {
Date now = new Date();
Period thisMonth = PeriodType.getPeriodType(PeriodTypeEnum.MONTHLY).createPeriod(now);
DataSet ds = new DataSet();
ds.setExpiryDays(1);
assertEquals(expected, ds.isLocked(null, thisMonth, actual.apply(thisMonth)));
}

private static Date createDateFromSimpleDateFormat(String date) {
try {
return new SimpleDateFormat("MMM d yyyy HH:mm:ss").parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}

@Test
void isLockedAtCertainTime() {
Date someTimeAgo = createDateFromSimpleDateFormat("Jan 1 2022 12:15:56");
Period thisMonth = PeriodType.getPeriodType(PeriodTypeEnum.MONTHLY).createPeriod(someTimeAgo);
Date periodEndDate = createDateFromSimpleDateFormat("Jan 31 2022 00:00:00");
assertEquals("202201", thisMonth.getIsoDate());
assertEquals(periodEndDate, thisMonth.getEndDate());
DataSet ds = new DataSet();
ds.setExpiryDays(1.5);
Date dataEntryClosed = createDateFromSimpleDateFormat("Feb 2 2022 12:00:01");
assertTrue(ds.isLocked(null, thisMonth, dataEntryClosed));
Date dataEntryOpen = createDateFromSimpleDateFormat("Feb 2 2022 12:00:00");
assertFalse(ds.isLocked(null, thisMonth, dataEntryOpen));
}
}

0 comments on commit 7d596bd

Please sign in to comment.