Testing and debugging¶
Test a site-aware module against real sites, hostnames and per-site roles, and see what Sites resolved for a page.
Base classes and traits¶
Sites ships them under tests/src, namespace Drupal\Tests\sites. A module
that has Sites in its codebase can extend them.
| Class or trait | Gives you |
|---|---|
Kernel\SitesKernelTestBase |
Sites and Sites content with their schemas installed, and the three traits below. Declare only the modules you add. |
Functional\SitesFunctionalTestBase |
BrowserTestBase with SiteCreationTrait, the stark theme, and helpers for several hostnames, the page cache and members. |
Traits\SiteCreationTrait |
createSiteEntity(), createSiteOfferingRoles(), assignContentToSite(), loadSiteEntity(), refreshSitePlugins(). |
Traits\SiteContextTrait |
setCurrentSite() and withSite(). |
Traits\SiteSchemaInstallTrait |
installSiteSchemas(): the site, configuration override and role assignment schemas, and the membership schema when Sites content is on. |
Traits\SourceGuardTrait |
assertSourceGuard(): fails when a source file matches a pattern only allowed files may hold. Works in unit tests. |
Traits\DatabaseCacheBinsTrait |
Puts a kernel test's cache bins on the database backend, so cache reads count as queries. |
createSiteEntity($id, $settings, $values) creates a site with the hostname
<id>.example.com; $settings is keyed by setting plugin, then by setting key
(Site settings). It refreshes the site plugin
caches; call refreshSitePlugins() yourself after changing a site entity in
another way.
Kernel tests¶
setCurrentSite() puts the test where a request on the site's hostname is:
the site is negotiated and active. withSite() switches to another site for
a callback, as site_switcher does, and switches back even when an assertion
fails. Until a site is set, the current site is not ready: it answers the
hub, and configuration overrides and query scoping stay off. Call
setCurrentSite('default') when the test needs the hub.
#[Group('my_module')]
#[RunTestsInSeparateProcesses]
final class MyModuleScopingTest extends SitesKernelTestBase {
protected static $modules = ['node', 'field', 'path', 'my_module'];
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('node');
$this->installSchema('node', ['node_access']);
$this->installConfig(['system']);
// Bundles first: a site reads them when its definition is built.
NodeType::create(['type' => 'page', 'name' => 'Page'])->save();
}
public function testQueryFollowsTheSite(): void {
$hydrion = $this->createSiteEntity('hydrion', ['site_filtering' => ['filter_views' => TRUE]]);
$node = Node::create(['type' => 'page', 'title' => 'About Hydrion']);
$node->save();
$this->assignContentToSite($node, $hydrion, TRUE);
$this->setCurrentSite('hydrion');
$ids = \Drupal::entityQuery('node')->accessCheck(TRUE)->execute();
$this->assertSame([(string) $node->id()], array_values($ids));
}
}
EntityQueryScopingTest in Sites is the complete version.
In a kernel test, the first user created is user 1, who passes every access
check: create it before the users you test with. The entity access handlers
remember their answers for the whole test: reset them with
resetCache() after changing a role assignment.
Guard tests¶
A concept has one owner: one service reads the pointed site, one helper
lays setting layers over each other. A guard test keeps a second spelling
out. It scans the module's sources with SourceGuardTrait and fails with
the files that break the rule:
$this->assertSourceGuard(
dirname(__DIR__, 3),
['src', 'modules'],
['/NestedArray::mergeDeep\(/'],
['src/SiteSetting/ConfigTargetSettings.php' => 'Buffers pending writes; no layer merge.'],
100,
'Lay setting layers over each other with LayerMerge::settings() only:',
);
The scan skips tests, roadmap, vendor and node_modules directories
and reads .php, .module, .install, .inc, .theme and .profile
files. Every allowed file needs a reason, and an allowed file that no longer
matches fails the test too, so dead exceptions do not pile up. The last
number is the fewest files the scan must see; fewer means wrong
directories. A satellite scans its own root.
Query-count guards¶
A per-site loop with a query inside makes a page cost more with every site.
Guard against it by its shape, not by absolute counts, which change with
every core release: measure with N sites, add sites the page has nothing to
do with, measure again, and assert the count did not grow. Count the
second of two calls, after resetting what a new request starts without
(site instances, loaded entities, per-request memos), and use
DatabaseCacheBinsTrait: in memory, a per-site cache read never reaches the
query log. SiteHostResolutionQueryTest and SiteListQueryScalingTest are
examples.
Per-site role access needs functional tests¶
The access policy adds the permissions of a per-site role to the account of the current request. A kernel test that checks access for another account, or switches accounts, does not take that path and can pass or fail wrongly. Test what a per-site role allows with real requests, logged in as a member that holds only the per-site role. A user with Administer sites passes every site gate and proves nothing. Kernel tests are fine for permissions of global roles and for membership rules.
#[Group('my_module')]
#[RunTestsInSeparateProcesses]
final class SiteEditorAccessTest extends SitesFunctionalTestBase {
protected static $modules = ['node', 'sites_content'];
protected function memberPermissions(): array {
return ['access content'];
}
public function testEditorEditsTheirSiteOnly(): void {
$this->drupalCreateContentType(['type' => 'page']);
Role::create(['id' => 'content_editor', 'label' => 'Content editor'])
->grantPermission('edit any page content')
->save();
$hydrion = $this->createSiteOfferingRoles('hydrion', ['content_editor']);
$torqline = $this->createSiteOfferingRoles('torqline', ['content_editor']);
$hydrion_page = $this->drupalCreateNode(['type' => 'page', 'title' => 'About Hydrion']);
$this->assignContentToSite($hydrion_page, $hydrion, TRUE);
$torqline_page = $this->drupalCreateNode(['type' => 'page', 'title' => 'About Torqline']);
$this->assignContentToSite($torqline_page, $torqline, TRUE);
$this->drupalLogin($this->memberWithRole('hydrion', 'content_editor'));
$this->drupalGet('node/' . $hydrion_page->id() . '/edit');
$this->assertSession()->statusCodeEquals(200);
$this->drupalGet('node/' . $torqline_page->id() . '/edit');
$this->assertSession()->statusCodeEquals(403);
}
}
memberWithRole() creates a user with memberPermissions(), makes it a
member of the site and grants the role there. SiteRoleJourneyTest in Sites
walks a site manager and a site editor through the hub and a site's hostname.
Hostnames in functional tests¶
Every request of a functional test goes to the test site's own URL.
drupalGetOnHost($host, $path) sends it with a Host header, so Sites
negotiates the site of that hostname and no DNS entry is needed. Pass the
host on every call; NULL is the hub. A redirect to a site's hostname
cannot be followed, since the hostname does not resolve: send such a request
through getHttpClient() with allow_redirects off, as
SiteRoleJourneyTest::rawStatusOnHost() does.
public function testPageCachePerHost(): void {
$this->enableAnonymousPageCache();
$path = 'node/' . $this->hydrionPage->id();
$this->drupalGetOnHost('hydrion.example.com', $path);
$this->assertPageCache('MISS');
$this->drupalGetOnHost('hydrion.example.com', $path);
$this->assertPageCache('HIT');
$this->drupalGetOnHost('torqline.example.com', $path);
$this->assertSession()->responseHeaderNotEquals('X-Drupal-Cache', 'HIT');
}
HostResolutionIsolationTest in Sites has this test with its setup.
Sites devel¶
Sites devel adds a Sites debug information block to the content region of every page. It needs Devel and Block plugin view builder, and you do not place it. For the request it dumps:
- the sites that claim the route, each with its definition, its resolved
settings and its
config_overridessetting; sensitive settings are redacted; - the active site;
- the route name and parameters;
- the languages;
- the path aliases and redirects of the route's entity, with Sites path alias and Redirect.
The block shows to users with the View the Sites debug block permission
(use sites_devel). In development mode it shows to every user who is
logged in, and Devel's dump() and kint() output does too, without
Access developer information. Development mode is the
sites_devel.development container parameter, false by default. Turn it on
in the services.yml of a development environment and rebuild the container:
parameters:
sites_devel.development: true
The former name sites.development still turns it on; the new name wins
where both are set.
Anonymous visitors never see the block without the permission. Do not enable Sites devel in production.
drush sites:info hydrion prints a site's resolved settings and configuration
overrides on the command line; see Drush commands.
Last updated 2026-11-09