$npx -y skills add edutrul/drupal-ai --skill drupal-kernelDrupal Kernel tests with KernelTestBase — testing services, database operations, hooks, and entity operations with minimal Drupal bootstrap.
| 1 | # Drupal Kernel Tests |
| 2 | |
| 3 | ## When to Use |
| 4 | |
| 5 | - Testing services that interact with Drupal's service container |
| 6 | - Database operations and entity CRUD |
| 7 | - Hook implementations |
| 8 | - Config operations |
| 9 | - Tests needing a real database but not a full browser |
| 10 | |
| 11 | ## Basic Structure |
| 12 | |
| 13 | ```php |
| 14 | // tests/src/Kernel/MyServiceTest.php |
| 15 | namespace Drupal\Tests\my_module\Kernel; |
| 16 | |
| 17 | use Drupal\KernelTests\KernelTestBase; |
| 18 | use Drupal\node\Entity\Node; |
| 19 | use Drupal\node\Entity\NodeType; |
| 20 | |
| 21 | /** |
| 22 | * Tests MyService integration. |
| 23 | * |
| 24 | * @group my_module |
| 25 | */ |
| 26 | final class MyServiceTest extends KernelTestBase { |
| 27 | |
| 28 | protected static $modules = [ |
| 29 | 'system', |
| 30 | 'user', |
| 31 | 'node', |
| 32 | 'field', |
| 33 | 'text', |
| 34 | 'my_module', |
| 35 | ]; |
| 36 | |
| 37 | protected function setUp(): void { |
| 38 | parent::setUp(); |
| 39 | |
| 40 | $this->installSchema('system', ['sequences']); |
| 41 | $this->installSchema('node', ['node_access']); |
| 42 | $this->installEntitySchema('user'); |
| 43 | $this->installEntitySchema('node'); |
| 44 | $this->installConfig('my_module'); |
| 45 | |
| 46 | // Create content type |
| 47 | NodeType::create(['type' => 'article', 'name' => 'Article'])->save(); |
| 48 | } |
| 49 | |
| 50 | public function testServiceProcessesNode(): void { |
| 51 | $node = Node::create([ |
| 52 | 'type' => 'article', |
| 53 | 'title' => 'Test Node', |
| 54 | 'status' => 1, |
| 55 | ]); |
| 56 | $node->save(); |
| 57 | |
| 58 | $service = $this->container->get('my_module.my_service'); |
| 59 | $result = $service->process($node); |
| 60 | |
| 61 | $this->assertTrue($result); |
| 62 | } |
| 63 | |
| 64 | } |
| 65 | ``` |
| 66 | |
| 67 | ## installEntitySchema vs installSchema |
| 68 | |
| 69 | ```php |
| 70 | // For entity types (creates entity tables) |
| 71 | $this->installEntitySchema('node'); |
| 72 | $this->installEntitySchema('user'); |
| 73 | $this->installEntitySchema('taxonomy_term'); |
| 74 | $this->installEntitySchema('paragraph'); |
| 75 | |
| 76 | // For specific table schemas (not entity-based) |
| 77 | $this->installSchema('system', ['sequences', 'key_value']); |
| 78 | $this->installSchema('node', ['node_access']); |
| 79 | |
| 80 | // For config from a module's config/install |
| 81 | $this->installConfig('my_module'); |
| 82 | $this->installConfig(['node', 'user']); |
| 83 | ``` |
| 84 | |
| 85 | ## Creating Test Users |
| 86 | |
| 87 | ```php |
| 88 | use Drupal\user\Entity\User; |
| 89 | |
| 90 | $user = User::create([ |
| 91 | 'name' => 'test_user', |
| 92 | 'mail' => 'test@example.com', |
| 93 | 'status' => 1, |
| 94 | ]); |
| 95 | $user->save(); |
| 96 | |
| 97 | // Set as current user |
| 98 | $this->setCurrentUser($user); |
| 99 | ``` |
| 100 | |
| 101 | ## Testing Config |
| 102 | |
| 103 | ```php |
| 104 | $config = $this->config('my_module.settings'); |
| 105 | $this->assertTrue($config->get('enabled')); |
| 106 | |
| 107 | // Update config |
| 108 | $this->config('my_module.settings') |
| 109 | ->set('enabled', FALSE) |
| 110 | ->save(); |
| 111 | ``` |
| 112 | |
| 113 | ## Testing Hooks |
| 114 | |
| 115 | ```php |
| 116 | public function testHookNodePresave(): void { |
| 117 | $node = Node::create(['type' => 'article', 'title' => 'Test']); |
| 118 | $node->save(); |
| 119 | |
| 120 | // Reload to verify hook ran |
| 121 | $node = Node::load($node->id()); |
| 122 | $this->assertEquals('Modified by hook', $node->get('field_processed')->value); |
| 123 | } |
| 124 | ``` |