mcpField
D
drupal-dtt

drupal-dtt

github.com/edutrulTesting & quality7350 installsUpdated 21w ago
Repository

About drupal-dtt

A Drupal testing skill for Drupal Test Traits (DTT) ExistingSite tests against a running site. It helps developers write and run smoke, integration, and production-data regression tests using real content, configuration, and users.

Key features

  1. 01Test against a running Drupal site
  2. 02Write smoke and integration tests
  3. 03Assert HTTP status and page content
  4. 04Navigate pages and manage login state
  5. 05Run all tests or selected PHPUnit tests

Use cases

  1. 01Check site behavior with real content
  2. 02Test regressions found in production
  3. 03Run a targeted ExistingSite test

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/edutrul/drupal-ai --skill drupal-dtt

After installing, type/drupal-dttin Claude Code to confirm it loaded.

Bundled filesREADME.md

Details

From the project README

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

// tests/src/ExistingSite/MyFeatureTest.php
namespace Drupal\Tests\my_module\ExistingSite;

use weitzman\DrupalTestTraits\ExistingSiteBase;

/**
 * Tests My Feature on an existing site.
 *
 * @group my_module
 * @group existing_site
 */
final class MyFeatureTest extends ExistingSiteBase {

  public function testHomepageLoads(): void {
    $this->drupalGet('/');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('Welcome');
  }

  public function testArticleCreation(): void {
    // Create a user with role
    $user = $this->createUser();
    $user->addRole('editor');
    $user->save();
    $this->drupalLogin($user);

    // Create a node
    $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' => '[email protected]',
      'message[0][value]' => 'Test message',
    ], 'Send message');

    $this->assertSession()->pageTextContains('Your message has been sent.');
  }

}

Common Assertions

// HTTP status
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->statusCodeEquals(403);
$this->assertSession()->statusCodeEquals(404);

// Page content
$this->assertSession()->pageTextContains('Expected text');
$this->assertSession()->pageTextNotContains('Missing text');

// Elements
$this->assertSession()->elementExists('css', '.my-class');
$this->assertSession()->elementTextContains('css', 'h1', 'Page Title');
$this->assertSession()->linkExists('My Link');
$this->assertSession()->linkByHrefExists('/my-path');

// Fields
$this->assertSession()->fieldExists('edit-title-0-value');
$this->assertSession()->fieldValueEquals('edit-title-0-value', 'My Title');

// Response headers
$this->assertSession()->responseHeaderContains('X-Drupal-Cache', 'HIT');

Helpers

// Navigate to URL
$this->drupalGet('/path');
$this->drupalGet(Url::fromRoute('my_module.page'));

// Login
$this->drupalLogin($user);
$this->drupalLogout();

// Create content (automatically cleaned up after test)
$node = $this->createNode(['type' => 'article', 'title' => 'Test']);
$user = $this->createUser([], NULL, FALSE, ['mail' => '[email protected]']);
$term = $this->createTerm($vocabulary);

// Submit form
$this->submitForm(['field_name' => 'value'], 'Submit button label');

// Click link
$this->clickLink('Link text');

// Find element
$page = $this->getSession()->getPage();
$element = $page->find('css', '.my-selector');

Running Tests

# Run all DTT tests
ddev exec vendor/bin/phpunit tests/

# Run specific test
ddev exec vendor/bin/phpunit tests/src/ExistingSite/MyFeatureTest.php

# Run with group filter
ddev exec vendor/bin/phpunit tests/ --group my_module

# Verbose output
ddev exec vendor/bin/phpunit tests/ --verbose

Comments

Markdown supported