Drupal is a registered trademark of Dries Buytaert
Menu Per Role 8.x-1.9 Minor update available for module menu_per_role (8.x-1.9). Bible 3.4.1 Minor update available for module bible (3.4.1). Bible 3.4.0 Minor update available for module bible (3.4.0). GEO Starter JSON-LD 1.1.1 Minor update available for module geo_starter_jsonld (1.1.1). SQLite VDB Provider 1.4.0 Minor update available for module ai_vdb_provider_sqlite (1.4.0). SQLite VDB Provider 1.3.0 Minor update available for module ai_vdb_provider_sqlite (1.3.0). Burndown 1.0.70 Minor update available for module burndown (1.0.70). Webform MailerLite integration 1.0.4 Minor update available for module webform_mailerlite (1.0.4). Menu Per Role 2.0.0 Module menu_per_role updated after 7 months of inactivity (2.0.0). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs.

Datetime testing

1 sites No security coverage
View on drupal.org

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.

Activity

Total releases
1
First release
Jun 2026
Latest release
1 month ago
Releases (12 mo)
1 ▲ from 0
Maintenance
Active

Releases

Version Type Release date
8.x-1.0-beta7 Pre-release Jun 5, 2026