| name | drupal-dtt |
| description | Drupal Test Traits (DTT) ExistingSite tests — testing against a live Drupal site, creating content, asserting pages, and running tests with DDEV. |
Drupal Test Traits (DTT) — ExistingSite Tests
When to Use
- Testing against a real, running Drupal site
- Smoke tests and integration tests against production-like data
- When you need real content, config, and users
- Regression tests for bugs found in production
Basic Structure
namespace Drupal\Tests\my_module\ExistingSite;
use weitzman\DrupalTestTraits\ExistingSiteBase;
final class MyFeatureTest extends ExistingSiteBase {
public function testHomepageLoads(): void {
$this->drupalGet('/');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('Welcome');
}
public function testArticleCreation(): void {
$user = $this->createUser();
$user->addRole('editor');
$user->save();
$this->drupalLogin($user);
$node = $this->createNode([
'type' => 'article',
'title' => 'Test Article',
'status' => 1,
]);
$this->drupalGet('/node/' . $node->id());
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('Test Article');
}
public function testFormSubmission(): void {
$this->drupalGet('/contact');
$this->assertSession()->statusCodeEquals(200);
$this->submitForm([
'name' => 'Test User',
'mail' => 'test@example.com',
'message[0][value]' => 'Test message',
], 'Send message');
$this->assertSession()->pageTextContains('Your message has been sent.');
}
}
Common Assertions
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->statusCodeEquals(403);
$this->assertSession()->statusCodeEquals(404);
$this->assertSession()->pageTextContains('Expected text');
$this->assertSession()->pageTextNotContains('Missing text');
$this->assertSession()->elementExists('css', '.my-class');
$this->assertSession()->elementTextContains('css', 'h1', 'Page Title');
$this->assertSession()->linkExists('My Link');
$this->assertSession()->linkByHrefExists('/my-path');
$this->assertSession()->fieldExists('edit-title-0-value');
$this->assertSession()->fieldValueEquals('edit-title-0-value', 'My Title');
$this->assertSession()->responseHeaderContains('X-Drupal-Cache', 'HIT');
Helpers
$this->drupalGet('/path');
$this->drupalGet(Url::fromRoute('my_module.page'));
$this->drupalLogin($user);
$this->drupalLogout();
$node = $this->createNode(['type' => 'article', 'title' => 'Test']);
$user = $this->createUser([], NULL, FALSE, ['mail' => 'test@test.com']);
$term = $this->createTerm($vocabulary);
$this->submitForm(['field_name' => 'value'], 'Submit button label');
$this->clickLink('Link text');
$page = $this->getSession()->getPage();
$element = $page->find('css', '.my-selector');
Running Tests
ddev exec vendor/bin/phpunit tests/
ddev exec vendor/bin/phpunit tests/src/ExistingSite/MyFeatureTest.php
ddev exec vendor/bin/phpunit tests/ --group my_module
ddev exec vendor/bin/phpunit tests/ --verbose