Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-83316

Behat: New step allows system clock change

XMLWordPrintable

      It is sometimes useful in automated tests to run the test as if the current system time is a particular fixed time.

      Moodle now has a mechanism to achieve this in PHPunit tests via the core DI system and the \core\clock class. This only works where the underlying code gets the time using this mechanism, instead of directly calling time(), but is still useful - often you can change the code to use the new mechanism while you are writing the unit test that needs it.

      There is currently no corresponding mechanism for use in Behat tests, but it would be easy to provide one. This issue proposes a new step:

      And the current time is "2024-08-01 12:34:56"
      

      As noted above it will only work in places which actually use the new mechanism to get the time - e.g. \core\di::get(\core\clock::class)->time() instead of just time(). In Moodle, there are very few places currently using it; it is used in the mod_forum generators, and a few other places, with nowhere easy that runs on web server.

      However, in third party plugins including several local ones I am responsible for, we already use a mechanism to fake time, across the plugin in both display and generators etc, and we would like to replace this with the core feature.

      As mentioned above, it is also very easy to change core code to use the new clock so for example, it would be feasible to do this across a module as part of adding a new behat scenario.

      As part of implementing this change I also noticed a bug in frozen_clock: when creating DateTime instances with timestamp, the class has very strange (albeit documented) behaviour where it always uses the wrong timezone (even if you specify one in the constructor). We have been bitten by this on previous occasions when working on very time-sensitive code. Since this is a minor bug only applying to the test utility frozen_clock and was caught by my Behat test, I fixed it in this issue.

            quen Sam Marshall
            quen Sam Marshall
            Andrew Lyons Andrew Lyons
            Simey Lameze Simey Lameze
            CiBoT CiBoT
            Votes:
            1 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - 0 minutes
                0m
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 1 hour, 18 minutes
                1h 18m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.