Patch #44455
openFix tests that fail when the date changes during a test run
Description
Some tests fail when the date changes while the test suite is running. For example, in the GitHub Actions run https://github.com/redmine/redmine/actions/runs/35037413503, which started at 23:50 in the runner's time zone (UTC), the following tests failed:
QueryTest#test_sort_with_group_by_timestamp_query_column_should_sort_after_date_value [test/unit/query_test.rb:2404]: Expected: [9, 10, 6] Actual: [10] MyControllerTest#test_page_with_activity [test/functional/my_controller_test.rb:238]: Expected at least 1 element matching "a[href=\"/activity?from=2026-09-16&user_id=2\"]", found 0.
The cause is that fixtures and tests use different times. Fixture files contain ERB expressions such as <%= 1.minute.ago.to_fs(:db) %> and <%= Date.today.to_fs(:db) %>, which are evaluated once when the fixtures are loaded at the start of the run. The tests call Date.today or User.current.today when they run, which can be much later. If midnight in the environment's time zone falls in between, the fixture dates are one day behind the date the tests expect. In the example above, issues 6, 9 and 10 have updated_on: <%= 1.minute.ago.to_fs(:db) %>, so the filter "updated today" no longer matches them after midnight.
Running the whole suite with the clock advanced by one day after fixture loading shows that six tests are affected:
QueryTest#test_sort_with_group_by_timestamp_query_column_should_sort_after_date_valueQueryTest#test_filter_on_fixed_version_due_dateMyControllerTest#test_page_with_activityMailerTest#test_remindersMailerTest#test_reminders_language_autoMailerTest#test_reminders_for_users
The attached patch freezes the clock for the whole test run. test/test_helper.rb captures Time.now once when it is loaded and calls travel_to with that value in before_setup, before fixtures are loaded. As a result, fixtures and tests always use the same time, no matter when the run starts or how long it takes. Tests that already use travel_to or freeze_time still work because travel_back is called automatically after each test.
The full suite passes with this change. The six tests above also pass when the frozen time is set to 23:59:59 UTC, which simulates a run that starts just before midnight.
Files
No data to display