oliverdavies.uk/content/node.94472cb5-7302-4b56-9fb8-ad96fe2971f8.json
2025-05-11 20:02:14 +01:00

100 lines
No EOL
7.8 KiB
JSON

{
"uuid": [
{
"value": "94472cb5-7302-4b56-9fb8-ad96fe2971f8"
}
],
"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:55+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": "The simplest Drupal test"
}
],
"created": [
{
"value": "2022-09-14T00:00:00+00:00"
}
],
"changed": [
{
"value": "2025-05-11T09:00:55+00:00"
}
],
"promote": [
{
"value": false
}
],
"sticky": [
{
"value": false
}
],
"default_langcode": [
{
"value": true
}
],
"revision_translation_affected": [
{
"value": true
}
],
"path": [
{
"alias": "\/daily\/2022\/09\/14\/simpletest-drupal-test",
"langcode": "en"
}
],
"body": [
{
"value": "\n <p>Most of my work uses the Drupal framework, and I've given talks and workshops on automated testing and building custom Drupal modules with test-driven development. Today, I wanted to see how quickly I could get a working test suite on a new Drupal project.<\/p>\n\n<p>I cloned a fresh version of my <a href=\"https:\/\/github.com\/opdavies\/docker-examples\">Docker Examples repository<\/a> and started the Drupal example.<\/p>\n\n<p>I ran <code>mkdir -p web\/modules\/custom\/example\/tests\/src\/Functional<\/code> to create the directory structure that I needed, and then <code>touch web\/modules\/custom\/example\/tests\/src\/Functional\/ExampleTest.php<\/code> to create a new test file and populated it with some initial code:<\/p>\n\n<pre><code class=\"language-php\">&lt;?php\n\nnamespace Drupal\\Tests\\example\\Functional;\n\nuse Drupal\\Tests\\BrowserTestBase;\nuse Symfony\\Component\\HttpFoundation\\Response;\n\nclass ExampleTest extends BrowserTestBase {\n\n protected $defaultTheme = 'stark';\n\n}\n<\/code><\/pre>\n\n<p>For the simplest test, I decided to test some existing Drupal core functionality - that an anonymous user can view the front page:<\/p>\n\n<pre><code class=\"language-php\">\/** @test *\/\npublic function the_front_page_loads_for_anonymous_users() {\n $this-&gt;drupalGet('&lt;front&gt;');\n\n $this-&gt;assertSession()-&gt;statusCodeEquals(Response::HTTP_OK);\n}\n<\/code><\/pre>\n\n<p>To execute the test, I ran <code>SIMPLETEST_DB=sqlite:\/\/localhost\/\/dev\/shm\/test.sqlite SIMPLETEST_BASE_URL=http:\/\/web phpunit -c web\/core web\/modules\/custom<\/code>. The environment variables could be added to a <code>phpunit.xml.dist<\/code> file but I decided to add them to the command and use Drupal core's PHPUnit configuration file.<\/p>\n\n<p>As this is existing functionalty, the test passes. I can change either the path or the response code to ensure it also fails when expected.<\/p>\n\n<p>With the first test working, it's easy to add more for other functionality, such as whether different users should be able to access administration pages:<\/p>\n\n<pre><code class=\"language-php\">\/** @test *\/\npublic function the_admin_page_is_not_accessible_to_anonymous_users() {\n $this-&gt;drupalGet('admin');\n\n $this-&gt;assertSession()-&gt;statusCodeEquals(Response::HTTP_FORBIDDEN);\n}\n\n\/** @test *\/\npublic function the_admin_page_is_accessible_by_admin_users() {\n $adminUser = $this-&gt;createUser([\n 'access administration pages',\n ]);\n\n $this-&gt;drupalLogin($adminUser);\n\n $this-&gt;drupalGet('admin');\n\n $this-&gt;assertSession()-&gt;statusCodeEquals(Response::HTTP_OK);\n}\n<\/code><\/pre>\n\n<p>Hopefully, this shows how quickly you can get tests running for a Drupal module. If you'd like to see more, the slides and video recording of my <a href=\"https:\/\/www.oliverdavies.uk\/presentations\/tdd-test-driven-drupal\">Test-Driven Drupal talk<\/a> are online.<\/p>\n\n ",
"format": "full_html",
"processed": "\n <p>Most of my work uses the Drupal framework, and I've given talks and workshops on automated testing and building custom Drupal modules with test-driven development. Today, I wanted to see how quickly I could get a working test suite on a new Drupal project.<\/p>\n\n<p>I cloned a fresh version of my <a href=\"https:\/\/github.com\/opdavies\/docker-examples\">Docker Examples repository<\/a> and started the Drupal example.<\/p>\n\n<p>I ran <code>mkdir -p web\/modules\/custom\/example\/tests\/src\/Functional<\/code> to create the directory structure that I needed, and then <code>touch web\/modules\/custom\/example\/tests\/src\/Functional\/ExampleTest.php<\/code> to create a new test file and populated it with some initial code:<\/p>\n\n<pre><code class=\"language-php\">&lt;?php\n\nnamespace Drupal\\Tests\\example\\Functional;\n\nuse Drupal\\Tests\\BrowserTestBase;\nuse Symfony\\Component\\HttpFoundation\\Response;\n\nclass ExampleTest extends BrowserTestBase {\n\n protected $defaultTheme = 'stark';\n\n}\n<\/code><\/pre>\n\n<p>For the simplest test, I decided to test some existing Drupal core functionality - that an anonymous user can view the front page:<\/p>\n\n<pre><code class=\"language-php\">\/** @test *\/\npublic function the_front_page_loads_for_anonymous_users() {\n $this-&gt;drupalGet('&lt;front&gt;');\n\n $this-&gt;assertSession()-&gt;statusCodeEquals(Response::HTTP_OK);\n}\n<\/code><\/pre>\n\n<p>To execute the test, I ran <code>SIMPLETEST_DB=sqlite:\/\/localhost\/\/dev\/shm\/test.sqlite SIMPLETEST_BASE_URL=http:\/\/web phpunit -c web\/core web\/modules\/custom<\/code>. The environment variables could be added to a <code>phpunit.xml.dist<\/code> file but I decided to add them to the command and use Drupal core's PHPUnit configuration file.<\/p>\n\n<p>As this is existing functionalty, the test passes. I can change either the path or the response code to ensure it also fails when expected.<\/p>\n\n<p>With the first test working, it's easy to add more for other functionality, such as whether different users should be able to access administration pages:<\/p>\n\n<pre><code class=\"language-php\">\/** @test *\/\npublic function the_admin_page_is_not_accessible_to_anonymous_users() {\n $this-&gt;drupalGet('admin');\n\n $this-&gt;assertSession()-&gt;statusCodeEquals(Response::HTTP_FORBIDDEN);\n}\n\n\/** @test *\/\npublic function the_admin_page_is_accessible_by_admin_users() {\n $adminUser = $this-&gt;createUser([\n 'access administration pages',\n ]);\n\n $this-&gt;drupalLogin($adminUser);\n\n $this-&gt;drupalGet('admin');\n\n $this-&gt;assertSession()-&gt;statusCodeEquals(Response::HTTP_OK);\n}\n<\/code><\/pre>\n\n<p>Hopefully, this shows how quickly you can get tests running for a Drupal module. If you'd like to see more, the slides and video recording of my <a href=\"https:\/\/www.oliverdavies.uk\/presentations\/tdd-test-driven-drupal\">Test-Driven Drupal talk<\/a> are online.<\/p>\n\n ",
"summary": null
}
],
"feeds_item": [
{
"imported": "2025-05-11T09:00:55+00:00",
"guid": null,
"hash": "00dc66fc251e796aa7a1f167673d4f33",
"target_type": "feeds_feed",
"target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76"
}
]
}