mcpField
D
drupal-kernel

drupal-kernel

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

About drupal-kernel

A Drupal testing skill for developers writing Kernel tests. It covers service-container, database, entity, configuration, and hook behavior without requiring a full browser test.

Key features

  1. 01Service-container testing
  2. 02Entity and database setup
  3. 03Configuration assertions and updates
  4. 04Hook behavior verification

Use cases

  1. 01Test services using Drupal's container
  2. 02Verify entity CRUD and database operations
  3. 03Check configuration changes
  4. 04Confirm hooks alter saved entities

Install

Copy the command and run it in a terminal.

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

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

Bundled filesREADME.md

Details

From the project README

Drupal Kernel Tests

When to Use

  • Testing services that interact with Drupal's service container
  • Database operations and entity CRUD
  • Hook implementations
  • Config operations
  • Tests needing a real database but not a full browser

Basic Structure

// tests/src/Kernel/MyServiceTest.php
namespace Drupal\Tests\my_module\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;

/**
 * Tests MyService integration.
 *
 * @group my_module
 */
final class MyServiceTest extends KernelTestBase {

  protected static $modules = [
    'system',
    'user',
    'node',
    'field',
    'text',
    'my_module',
  ];

  protected function setUp(): void {
    parent::setUp();

    $this->installSchema('system', ['sequences']);
    $this->installSchema('node', ['node_access']);
    $this->installEntitySchema('user');
    $this->installEntitySchema('node');
    $this->installConfig('my_module');

    // Create content type
    NodeType::create(['type' => 'article', 'name' => 'Article'])->save();
  }

  public function testServiceProcessesNode(): void {
    $node = Node::create([
      'type' => 'article',
      'title' => 'Test Node',
      'status' => 1,
    ]);
    $node->save();

    $service = $this->container->get('my_module.my_service');
    $result = $service->process($node);

    $this->assertTrue($result);
  }

}

installEntitySchema vs installSchema

// For entity types (creates entity tables)
$this->installEntitySchema('node');
$this->installEntitySchema('user');
$this->installEntitySchema('taxonomy_term');
$this->installEntitySchema('paragraph');

// For specific table schemas (not entity-based)
$this->installSchema('system', ['sequences', 'key_value']);
$this->installSchema('node', ['node_access']);

// For config from a module's config/install
$this->installConfig('my_module');
$this->installConfig(['node', 'user']);

Creating Test Users

use Drupal\user\Entity\User;

$user = User::create([
  'name' => 'test_user',
  'mail' => '[email protected]',
  'status' => 1,
]);
$user->save();

// Set as current user
$this->setCurrentUser($user);

Testing Config

$config = $this->config('my_module.settings');
$this->assertTrue($config->get('enabled'));

// Update config
$this->config('my_module.settings')
  ->set('enabled', FALSE)
  ->save();

Testing Hooks

public function testHookNodePresave(): void {
  $node = Node::create(['type' => 'article', 'title' => 'Test']);
  $node->save();

  // Reload to verify hook ran
  $node = Node::load($node->id());
  $this->assertEquals('Modified by hook', $node->get('field_processed')->value);
}

Comments

Markdown supported