Skip to content

Commit

Permalink
[#8640] HtmlHelper: fix broken detection of current time (#8746)
Browse files Browse the repository at this point in the history
  • Loading branch information
whipermr5 authored Apr 2, 2018
1 parent e520c0e commit e47e73b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ protected void prepareTestData() {

FeedbackSessionAttributes gracedFeedbackSession =
BackDoor.getFeedbackSession("SHomeUiT.CS2104", "Graced Feedback Session");
// TODO: change to actual now once HTML time zone detection is fixed
int zoneOffsetInSeconds = gracedFeedbackSession.getTimeZone().getRules().getOffset(Instant.now()).getTotalSeconds();
Instant nowMinusZoneOffset = Instant.now().minusSeconds(zoneOffsetInSeconds);
gracedFeedbackSession.setEndTime(nowMinusZoneOffset);
gracedFeedbackSession.setEndTime(Instant.now());
BackDoor.editFeedbackSession(gracedFeedbackSession);
}

Expand Down
37 changes: 12 additions & 25 deletions src/test/java/teammates/test/driver/HtmlHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.helper.W3CDom;
Expand Down Expand Up @@ -44,7 +41,6 @@ public final class HtmlHelper {
"([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\\.([0-9]{3}|[0-9]{6}))?Z";
private static final String REGEX_ADMIN_INSTITUTE_FOOTER = ".*?";
private static final String REGEX_SESSION_TOKEN = REGEX_UPPERCASE_HEXADECIMAL_CHAR_32;
private static final String REGEX_TIMEZONE_OFFSET = "UTC([+-]\\d{4})";

private HtmlHelper() {
// utility class
Expand Down Expand Up @@ -409,15 +405,16 @@ private static String suppressVariationsInInjectedValues(String content) {
*/
private static String replaceUnpredictableValuesWithPlaceholders(String content) {
Date now = new Date();
Date nowMinusOneDay = TimeHelper.convertInstantToDate(TimeHelper.getInstantDaysOffsetFromNow(-1));
Date nowPlusOneDay = TimeHelper.convertInstantToDate(TimeHelper.getInstantDaysOffsetFromNow(1));
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy, ");
// get session's time zone from content.
// this method is not applicable for pages with multiple time zones like InstructorSearchPage
sdf.setTimeZone(getTimeZone(content));
String dateTimeNow = sdf.format(now);
String dateToday = sdf.format(now);
String dateYesterday = sdf.format(nowMinusOneDay);
String dateTomorrow = sdf.format(nowPlusOneDay);
SimpleDateFormat sdfForIso8601 = new SimpleDateFormat("yyyy-MM-dd'T'");
String dateTimeNowInIso8601 = sdfForIso8601.format(now);
String dateTodayInIso8601 = sdfForIso8601.format(now);
SimpleDateFormat sdfForCoursesPage = new SimpleDateFormat("d MMM yyyy");
String dateTimeNowInCoursesPageFormat = sdfForCoursesPage.format(now);
String dateTodayInCoursesPageFormat = sdfForCoursesPage.format(now);
return content // dev server admin absolute URLs (${teammates.url}/_ah/...)
.replace("\"" + TestProperties.TEAMMATES_URL + "/_ah", "\"/_ah")
// logout URL generated by Google
Expand Down Expand Up @@ -467,9 +464,11 @@ private static String replaceUnpredictableValuesWithPlaceholders(String content)
.replaceAll("commentBar-" + REGEX_COMMENT_ID, "commentBar-\\${comment\\.id}")
.replaceAll("plainCommentText-" + REGEX_COMMENT_ID, "plainCommentText-\\${comment\\.id}")
// date/time now e.g [Thu, 07 May 2015, 07:52 PM]
.replaceAll(dateTimeNow + REGEX_DISPLAY_TIME, "\\${datetime\\.now}")
.replaceAll(dateTimeNowInIso8601 + REGEX_DISPLAY_TIME_ISO_8601_UTC, "\\${datetime\\.now\\.iso8601utc}")
.replaceAll(dateTimeNowInCoursesPageFormat, "\\${datetime\\.now\\.courses}")
// account for time zone differences by accepting dates within one day from now
.replaceAll("(" + dateToday + "|" + dateYesterday + "|" + dateTomorrow + ")" + REGEX_DISPLAY_TIME,
"\\${datetime\\.now}")
.replaceAll(dateTodayInIso8601 + REGEX_DISPLAY_TIME_ISO_8601_UTC, "\\${datetime\\.now\\.iso8601utc}")
.replaceAll(dateTodayInCoursesPageFormat, "\\${datetime\\.now\\.courses}")
// admin footer, test institute section
.replaceAll("(?s)<div( class=\"col-md-8\"| id=\"adminInstitute\"){2}>"
+ REGEX_ADMIN_INSTITUTE_FOOTER + "</div>",
Expand Down Expand Up @@ -531,16 +530,4 @@ public static String injectContextDependentValuesForTest(String content) {
TimeHelper.formatDateTimeForInstructorCoursesPage(now, "UTC"));
}

private static TimeZone getTimeZone(String content) {
// searches for first String of pattern "UTC+xxxx" in the content.
Pattern pattern = Pattern.compile(REGEX_TIMEZONE_OFFSET);
Matcher matcher = pattern.matcher(content);
// set default time zone offset.
String timeZoneOffset = "+0000";
if (matcher.find()) {
timeZoneOffset = matcher.group(1);
}
return TimeZone.getTimeZone("GMT" + timeZoneOffset);
}

}

0 comments on commit e47e73b

Please sign in to comment.