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
- Test content follows a naming convention - titles prefixed with
[TEST] - Views are registered in Testmode configuration
- Behat scenarios tagged with
@testmodeautomatically enable and disable filtering - When enabled, registered views only show content matching the
[TEST]pattern
Configuration
Testmode is configured through the testmode.settings configuration object:
| Key | Type | Description |
|---|---|---|
views_node | string[] | Node view machine names to filter |
views_term | string[] | Term view machine names to filter |
views_user | string[] | User view machine names to filter |
pattern_node | string[] | MySQL LIKE patterns for node titles |
pattern_term | string[] | MySQL LIKE patterns for term names |
pattern_user | string[] | MySQL LIKE patterns for user emails |
list_term | bool | Whether 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_testmodedeploy hook - Includes the
pages.featureBehat feature demonstrating the@testmodetag against the generated content it filters out