Skip to main content
drupalreleases

Use direct release data with your AI assistant.

Learn more

Datetime testing

No security coverage Under active development 6 sites

This module provides tools for testing Drupal functionality that relies on dates and times. It allows developers to manipulate and freeze the current time within automated tests, ensuring consistent and predictable test results.

Introduction

The Datetime Testing module provides an API that helps with developing automated tests for functionality that depends on dates and times in Drupal.

It provides the following:

  • Drupal's datetime.time service (which informs Drupal classes about the current time) is decorated, adding functions that allow the reported time to be altered or frozen.
  • A drop-in replacement for DrupalDateTime (and thus PHP's \Datetime) is offered, which respects the current time reported by datetime.time when interpreting strings as datetimes.
  • A subcontext for the Drupal Extension for Behat, allowing scenario steps like `Given the time is 12pm` or `When "1 hour" passes`.

This is particularly intended to be helpful when writing functional tests,
because the decorated time service uses Drupal's state API to persist manipulated times across requests. Merely mocking the time service is sufficient for unit tests but not for functional tests.

Usage

This is an API-module for developers; it has no useful effect without custom code.

// You can specify the time.
\Drupal::time()->setTime('2008-12-03 09:15pm');
// Returns a timestamp equivalent to '2008-12-03 09:15pm'.
echo \Drupal::time()->getCurrentTime();

// Stop time from flowing, if you like.
\Drupal::time()->freezeTime();
\Drupal::time()->setTime('2008-12-03 09:15pm');
sleep(2);
// Still returns a timestamp equivalent to '2008-12-03 09:15pm'.
echo \Drupal::time()->getCurrentTime();

// Specify a new time using almost anything understood by PHP's strtotime().
// Note that setting the time, even relatively, discards any fractional second
// known for the current time.
\Drupal::time()->setTime('+1 hour);

// The same string parsing logic is available in your own objects
$date = new TestDateTime('+1 hour');

// This will be true.
echo ($date->getTimestamp() === \Drupal::time()->getCurrentTime());

// Now let's allow time to flow again.
\Drupal::time()->unFreezeTime();
sleep(60);
// Now returns a timestamp equivalent to '2008-12-03 09:16pm'.
echo \Drupal::time()->getCurrentTime();

// Return to the normal behavior of datetime.time.
\Drupal::time()->resetTime();

See \Drupal\datetime_testing\TestTimeInterface and other classes for further
code documentation.

Installation

Install as you would normally install a contributed Drupal module.

This module is for testing purposes only. It should not be installed on
production sites as it may slow performance. There is also no security coverage.

Configuration

In order to use the Behat steps in the provided subcontext, indicate the path
to search for the subcontext in your project's behat.yml file:

Drupal\DrupalExtension:
  subcontexts:
    paths:
      - "/app/web/modules/contrib/datetime_testing"

You do not need to declare the subcontext under the `contexts` key of behat.yml.

Core Bugs Affecting Datetime Testing

Maintainers

Current maintainers:
* Patrick Kenny (ptmkenny) - https://drupal.org/u/ptmkenny

Contributing

All contributions are welcome. Please submit MRs, not patches, so that the CI tests are run. Please add tests if possible.

If you post an issue and do not receive a response, feel free to contact me by private message in the Drupal Slack (ptmkenny) or via my Drupal.org contact form.

Ask your assistant about Datetime testing

Check compatibility with your Drupal and PHP version, maintenance and security coverage, from current release data. How it works

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
7
Tracked since
Feb 2023
Latest release
1 month ago
Releases (12 mo)
2 ▲ from 1
Maintenance
Active

Release Timeline

Releases

Version Type Core PHP Release date
8.x-1.0-beta8 Pre-release 10–11 PHP ^8.3 Sep 3, 2026
8.x-1.0-beta7 Pre-release 10–11 Jun 5, 2026
8.x-1.0-beta6 Pre-release 10–11 Oct 22, 2024
8.x-1.0-beta5 Pre-release 10–11 Jun 20, 2024
8.x-1.0-beta4 Pre-release 8–10 Jun 19, 2024
8.x-1.0-beta3 Pre-release 8–10 Sep 1, 2023
8.x-1.0-beta2 Pre-release 8–10 Feb 13, 2023