mcpField
D
drupal-kernel

drupal-kernel

github.com/edutrulテスト・品質7350 回インストール21 週間前に更新
リポジトリ

drupal-kernel について

Drupal 開発者向けの Kernel テストスキルです。完全なブラウザーテストを行わずに、サービスコンテナー、データベース、エンティティ、設定、フックをテストできます。

主な機能

  1. 01サービスコンテナーのテスト
  2. 02エンティティと DB のセットアップ
  3. 03設定のアサーションと更新
  4. 04フック動作の検証

活用シーン

  1. 01Drupal コンテナーを使うサービスのテスト
  2. 02エンティティ CRUD と DB 操作の検証
  3. 03設定変更の確認
  4. 04保存済みエンティティへのフック適用確認

インストール

コマンドをコピーしてターミナルで実行します。

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

インストール後、Claude Code で/drupal-kernelを入力して読み込まれたか確認します。

同梱ファイルREADME.md

詳細

Drupal カーネルテスト

使用する場面

  • Drupal のサービスコンテナと連携するサービスのテスト
  • データベース操作とエンティティの CRUD
  • フックの実装
  • 設定操作
  • 完全なブラウザー環境は不要だが、実際のデータベースが必要なテスト

基本構造

// 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 と 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']);

テストユーザーの作成

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);

設定のテスト

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

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

フックのテスト

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);
}

コメント