Skip to main content

Testmode

Testmode filters site content during Behat tests, preventing live or generated content from interfering with test assertions.

This page covers how Vortex uses the module. The module documentation is the reference for its full API.

How it works

  1. Test content follows a naming convention - titles prefixed with [TEST]
  2. Views are registered in Testmode configuration
  3. Behat scenarios tagged with @testmode automatically enable and disable filtering
  4. When enabled, registered views only show content matching the [TEST] pattern

Configuration

Testmode is configured through the testmode.settings configuration object:

KeyTypeDescription
views_nodestring[]Node view machine names to filter
views_termstring[]Term view machine names to filter
views_userstring[]User view machine names to filter
pattern_nodestring[]MySQL LIKE patterns for node titles
pattern_termstring[]MySQL LIKE patterns for term names
pattern_userstring[]MySQL LIKE patterns for user emails
list_termboolWhether to filter term listings

Vortex adds testmode to config_exclude_modules in settings.testmode.php, so the module never leaks into an exported configuration.

Registering a view programmatically

The Testmode singleton reads and writes that configuration. Use it from a deploy hook to register a view:

use Drupal\testmode\Testmode;

function ys_demo_deploy_configure_testmode(): string {
$testmode = Testmode::getInstance();

$views = $testmode->getNodeViews();
if (!in_array('ys_demo_pages', $views)) {
$views[] = 'ys_demo_pages';
$testmode->setNodeViews($views);
}

return 'Configured testmode to filter the pages view.';
}

The same singleton exposes getTermViews(), getUserViews() and the matching pattern getters and setters, plus enableTestMode(), disableTestMode() and isTestMode() for driving the mode directly.

Behat integration

The @testmode tag activates Testmode for individual scenarios via TestmodeTrait from behat-steps:

@api @testmode
Scenario: Pages view shows only test content when test mode is enabled
Given the following page content:
| title | status | moderation_state |
| [TEST] First test page | 1 | published |
| [TEST] Second test page | 1 | published |
When I go to "/pages"
Then I should see "[TEST] First test page"
And I should see "[TEST] Second test page"
And I should not see "Demo page"

The [TEST] prefix in content titles matches the default [TEST% pattern configured in Testmode. Only matching content appears in registered views, so the generated Demo page nodes stay out of the assertions.

Example in Vortex

The ys_demo module:

  • Ships a pages view at /pages
  • Registers it with Testmode via the ys_demo_deploy_configure_testmode deploy hook
  • Includes the pages.feature Behat feature demonstrating the @testmode tag against the generated content it filters out