oliverdavies.uk/content/node.7a90ce1b-f0d9-443b-b143-b82562fef529.json
2025-05-11 20:02:14 +01:00

100 lines
No EOL
7.1 KiB
JSON

{
"uuid": [
{
"value": "7a90ce1b-f0d9-443b-b143-b82562fef529"
}
],
"langcode": [
{
"value": "en"
}
],
"type": [
{
"target_id": "daily_email",
"target_type": "node_type",
"target_uuid": "8bde1f2f-eef9-4f2d-ae9c-96921f8193d7"
}
],
"revision_timestamp": [
{
"value": "2025-05-11T09:00:26+00:00"
}
],
"revision_uid": [
{
"target_type": "user",
"target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849"
}
],
"revision_log": [],
"status": [
{
"value": true
}
],
"uid": [
{
"target_type": "user",
"target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849"
}
],
"title": [
{
"value": "Finding the best test base\n"
}
],
"created": [
{
"value": "2023-11-27T00:00:00+00:00"
}
],
"changed": [
{
"value": "2025-05-11T09:00:26+00:00"
}
],
"promote": [
{
"value": false
}
],
"sticky": [
{
"value": false
}
],
"default_langcode": [
{
"value": true
}
],
"revision_translation_affected": [
{
"value": true
}
],
"path": [
{
"alias": "\/daily\/2023\/11\/27\/finding-the-best-test-base",
"langcode": "en"
}
],
"body": [
{
"value": "\n <p>As well as different base classes for types of tests - i.e. functional, kernel and unit - there are other test base classes within those that can be used to simplify things.<\/p>\n\n<p>For example, if we have this test:<\/p>\n\n<pre><code class=\"language-php\">&lt;?php\n\nnamespace Drupal\\Tests\\example\\Kernel;\n\nuse Drupal\\KernelTests\\KernelTestBase;\nuse Drupal\\Tests\\node\\Traits\\NodeCreationTrait;\nuse Drupal\\Tests\\user\\Traits\\UserCreationTrait;\n\nclass ExampleTest extends KernelTestBase {\n\n use NodeCreationTrait;\n use UserCreationTrait;\n\n protected static $modules = [\n 'node',\n 'user',\n ];\n\n public function setUp(): void {\n parent::setUp();\n\n $this-&gt;installEntitySchema(entity_type_id: 'node');\n $this-&gt;installEntitySchema(entity_type_id: 'user');\n }\n\n public function testExample(): void {\n $user = $this-&gt;createUser();\n\n $article = $this-&gt;createNode([\n 'title' =&gt; 'Test article',\n 'uid' =&gt; $user,\n ]);\n\n self::assertSame('1', $article-&gt;getOwnerId());\n }\n\n}\n<\/code><\/pre>\n\n<p>Both creation traits must be imported, the <code>node<\/code> and <code>user<\/code> modules must be enabled, and the entity tables must be installed.<\/p>\n\n<p>When writing a lot of tests, this can result in duplication and more complex tests that take longer to write.<\/p>\n\n<p>This can be simplified using <code>EntityKernelTestBase<\/code> instead of <code>KernelTestBase<\/code>:<\/p>\n\n<pre><code class=\"language-php\">&lt;?php\n\nuse Drupal\\KernelTests\\Core\\Entity\\EntityKernelTestBase;\nuse Drupal\\Tests\\node\\Traits\\NodeCreationTrait;\n\nclass ExampleTest extends EntityKernelTestBase {\n\n use NodeCreationTrait;\n\n protected static $modules = [\n 'node',\n ];\n\n public function testExample(): void {\n $user = $this-&gt;createUser();\n\n $article = $this-&gt;createNode([\n 'title' =&gt; 'Test article',\n 'uid' =&gt; $user,\n ]);\n\n self::assertSame('1', $article-&gt;getOwnerId());\n }\n\n}\n<\/code><\/pre>\n\n<p>The class is simpler, fewer modules must be specified, and the entity schemas are automatically installed.<\/p>\n\n<p>As well as the core modules, some contrib modules also provide their own base test cases.<\/p>\n\n<p>If you're using the Webform module, you may want to use <code>WebformAccessTestBase<\/code> instead of the standard <code>UnitTestCase<\/code>.<\/p>\n\n<p>It's definitely worth spending some time looking at what base test cases are available and which are the best ones to use for your own tests.<\/p>\n\n ",
"format": "full_html",
"processed": "\n <p>As well as different base classes for types of tests - i.e. functional, kernel and unit - there are other test base classes within those that can be used to simplify things.<\/p>\n\n<p>For example, if we have this test:<\/p>\n\n<pre><code class=\"language-php\">&lt;?php\n\nnamespace Drupal\\Tests\\example\\Kernel;\n\nuse Drupal\\KernelTests\\KernelTestBase;\nuse Drupal\\Tests\\node\\Traits\\NodeCreationTrait;\nuse Drupal\\Tests\\user\\Traits\\UserCreationTrait;\n\nclass ExampleTest extends KernelTestBase {\n\n use NodeCreationTrait;\n use UserCreationTrait;\n\n protected static $modules = [\n 'node',\n 'user',\n ];\n\n public function setUp(): void {\n parent::setUp();\n\n $this-&gt;installEntitySchema(entity_type_id: 'node');\n $this-&gt;installEntitySchema(entity_type_id: 'user');\n }\n\n public function testExample(): void {\n $user = $this-&gt;createUser();\n\n $article = $this-&gt;createNode([\n 'title' =&gt; 'Test article',\n 'uid' =&gt; $user,\n ]);\n\n self::assertSame('1', $article-&gt;getOwnerId());\n }\n\n}\n<\/code><\/pre>\n\n<p>Both creation traits must be imported, the <code>node<\/code> and <code>user<\/code> modules must be enabled, and the entity tables must be installed.<\/p>\n\n<p>When writing a lot of tests, this can result in duplication and more complex tests that take longer to write.<\/p>\n\n<p>This can be simplified using <code>EntityKernelTestBase<\/code> instead of <code>KernelTestBase<\/code>:<\/p>\n\n<pre><code class=\"language-php\">&lt;?php\n\nuse Drupal\\KernelTests\\Core\\Entity\\EntityKernelTestBase;\nuse Drupal\\Tests\\node\\Traits\\NodeCreationTrait;\n\nclass ExampleTest extends EntityKernelTestBase {\n\n use NodeCreationTrait;\n\n protected static $modules = [\n 'node',\n ];\n\n public function testExample(): void {\n $user = $this-&gt;createUser();\n\n $article = $this-&gt;createNode([\n 'title' =&gt; 'Test article',\n 'uid' =&gt; $user,\n ]);\n\n self::assertSame('1', $article-&gt;getOwnerId());\n }\n\n}\n<\/code><\/pre>\n\n<p>The class is simpler, fewer modules must be specified, and the entity schemas are automatically installed.<\/p>\n\n<p>As well as the core modules, some contrib modules also provide their own base test cases.<\/p>\n\n<p>If you're using the Webform module, you may want to use <code>WebformAccessTestBase<\/code> instead of the standard <code>UnitTestCase<\/code>.<\/p>\n\n<p>It's definitely worth spending some time looking at what base test cases are available and which are the best ones to use for your own tests.<\/p>\n\n ",
"summary": null
}
],
"feeds_item": [
{
"imported": "2025-05-11T09:00:26+00:00",
"guid": null,
"hash": "550456dfd70a84795f8c61ba84691a1d",
"target_type": "feeds_feed",
"target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76"
}
]
}