I recently gave a [talk on automated testing in Drupal][0] talk at [DrupalCamp Dublin][1] and as a lunch and learn session for my colleagues at Microserve. As part of the talk, I gave an example of how to build a Drupal 8 module using a test driven approach. I’ve released the [module code on GitHub][2], and this post outlines the steps of the process.
You have created a `core/phpunit.xml` file based on `core/phpunit.xml.dist`, and populated it with your database credentials so that PHPUnit can bootstrap the Drupal database as part of the tests. [Here is an example][5].
We can also add the test file structure at this point too. We’ll call it `PageTestTest.php` and put it within a `tests/src/Functional` directory. As this is a functional test, it extends the `BrowserTestBase` class, and we need to ensure that the tdd_dublin module is enabled by adding it to the `$modules` array.
```language-php
// tests/src/Functional/PageListTest.php
namespace Drupal\Tests\tdd_dublin\Functional;
use Drupal\Tests\BrowserTestBase\BrowserTestBase;
class PageListTest extends BrowserTestBase {
protected static $modules = ['tdd_dublin'];
}
```
With this in place, we can now start adding test methods.
## Ensure that the Listing page Exists
### Writing the First Test
Let’s start by testing that the listing page exists at /pages. We can do this by loading the page and checking the status code. If the page exists, the code will be 200, otherwise it will be 404.
I usually like to write comments first within the test method, just to outline the steps that I'm going to take and then replace it with code.
```language-php
public function testListingPageExists() {
// Go to /pages and check that it is accessible by checking the status
// code.
}
```
We can use the `drupalGet()` method to browse to the required path, i.e. `/pages`, and then write an assertion for the response code value.
```language-php
public function testListingPageExists() {
$this->drupalGet('pages');
$this->assertSession()->statusCodeEquals(200);
}
```
### Running the Test
In order to run the tests, you either need to include `-c core` or be inside the `core` directory when running the command, to ensure that the test classes are autoloaded so can be found, though the path to the `vendor` directory may be different depending on your project structure. You can also specify a path within which to run the tests - e.g. within the module’s `test` directory.
Note: I’m using Docksal, and I’ve noticed that I need to run the tests from within the CLI container. You can do this by running the `fin bash` command.
Behat\Mink\Exception\ExpectationException: Current response status code is 404, but 200 expected.
FAILURES!
Tests: 1, Assertions: 1, Errors: 1.
```
Because the route does not yet exist, the response code returned is 404, so the test fails.
Now we can make it pass by adding the page. For this, I will use the Views module, though you could achieve the same result with a custom route and a Controller.
### Building the View
To begin with, I will create a view showing all types of content with a default sort order of newest first. We will use further tests to ensure that only the correct content is returned and that it is ordered correctly.
With the first version of the view built, it needs to be incldued within the module so that it can be enabled when the test is run. To do this, we need to export the configuration for the view, and place it within the module’s `config/install` directory. This can be done using the `drush config-export` command or from within the Drupal UI. In either case, the `uid` line at the top of the file needs to be removed so the configuration can be installed.
Drupal\Core\Config\UnmetDependenciesException: Configuration objects provided by <emclass="placeholder">tdd_dublin</em> have unmet dependencies: <emclass="placeholder">node.type.page (node), views.view.pages (node, views)</em>
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
```
This error is identifying unmet dependencies within the module’s configuration. In this case, the view that we’ve added depends on the node and views modules, but these aren’t enabled. To fix this, we can add the extra modules as dependencies of tdd_dublin so they will be enabled too.
Drupal\Core\Config\UnmetDependenciesException: Configuration objects provided by <emclass="placeholder">tdd_dublin</em> have unmet dependencies: <emclass="placeholder">views.view.pages (node.type.page)</em>
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
```
With the modules enabled, we can see one more unmet dependency for `node.type.page`. This means that we need a page content type to be able to install the view. We can fix this in the same way as before, by exporting the configuration and copying it into the `config/install` directory.
With this in place, the test should now pass - and it does.
```language-plain
Time: 26.04 seconds, Memory: 6.00MB
OK (1 test, 1 assertion)
```
We now have a test to ensure that the listing page exists.
## Ensure that only Published Pages are Shown
### Writing the Test
Now that we have a working page, we can now move on to checking that the correct content is returned. Again, I’ll start by writing comments and then translate that into code.
The objectives of this test are:
* To ensure that only page nodes are returned.
* To ensure that only published nodes are returned.
```language-php
public function testOnlyPublishedPagesAreShown() {
// Given that a have a mixture of published and unpublished pages, as well
// as other types of content.
// When I view the page.
// Then I should only see the published pages.
}
```
In order to test the different scenarios, I will create an additional "article" content type, create a node of this type as well as one published and one unpublished page. From this combination, I only expect one node to be visible.
```language-php
public function testOnlyPublishedPagesAreShown() {
We could use `drupalGet()` again to browse to the page and write assertions based on the rendered HTML, though I’d rather do this against the data returned from the view itself. This is so that the test isn’t too tightly coupled to the presentation logic, and we won’t be in a situation where at a later date the test fails because of changes made to how the data is displayed.
Rather, I’m going to use `views_get_view_result()` to programmatically get the result of the view. This returns an array of `Drupal\views\ResultRow` objects, which contain the nodes. I can use `array_column` to extract the node IDs from the view result into an array.
```language-php
public function testOnlyPublishedPagesAreShown() {
From the generated nodes, I can use `assertEquals()` to compare the returned node IDs from the view against an array of expected node IDs - in this case, I expect only node 1 to be returned.
```language-php
public function testOnlyPublishedPagesAreShown() {
The test fails as no extra conditions have been added to the view, though the default "Content: Published" filter is already excluding one of the page nodes. We can see from the output from the test that node 1 (a page) and node 2 (the article) are both being returned.
Once the view is updated and the configuration is updated within the module, the test should then pass - and it does.
```language-plain
Time: 24.76 seconds, Memory: 6.00MB
OK (1 test, 3 assertions)
```
## Ensure that the Pages are in the Correct Order
### Writing the Test
As we know that the correct content is being returned, we can now focus on displaying it in the correct order. We’ll start again by adding a new test method and filling out the comments.
```language-php
public function testResultsAreOrderedAlphabetically() {
// Given I have multiple nodes with different titles.
// When I view the pages list.
// Then I should see pages in the correct order.
}
```
To begin with this time, I’ll create a number of different nodes and specify the title for each. These are intentionally in the incorrect order alphabetically so that we can see the test fail initially and then see it pass after making a change so we know that the change worked.
```language-php
public function testResultsAreOrderedAlphabetically() {
$this->drupalCreateNode(['title' => 'Page A']);
$this->drupalCreateNode(['title' => 'Page D']);
$this->drupalCreateNode(['title' => 'Page C']);
$this->drupalCreateNode(['title' => 'Page B']);
// When I view the pages list.
// Then I should see pages in the correct order.
}
```
We can use the same method as the previous test to get the returned IDs, using `views_get_view_result()` and `array_column()`, and assert that the returned node IDs match the expected node IDs in the specified order. Based on the defined titles, the order should be 1, 4, 3, 2.
```language-php
public function testResultsAreOrderedAlphabetically() {
As expected the test fails, as the default sort criteria in the view orders the results by their created date.
In the test output, we can see the returned results are in sequential order so the results array does not match the expected one.
This would be particularly more complicated to test if I was using `drupalGet()` and having to parse the HTML, compared to getting the results as an array from the view programmatically.
Again, once the view has been updated and exported, the test should pass - and it does.
```language-plain
Time: 27.55 seconds, Memory: 6.00MB
OK (1 test, 2 assertions)
```
## Ensure all Tests Still Pass
Now we know that all the tests pass individually, all of the module tests should now be run to ensure that they all still pass and that there have been no regressions due to any of the changes.
They all pass, so we be confident that the code works as expected, we can continue to refactor if needed, and if any changes are made to this module at a later date, we have the tests to ensure that any regressions are caught and fixed before deployment.
I’ve started looking into whether some of the tests can be rewritten as kernel tests, which should result in quicker test execution. I will post any updated code to the [GitHub repository][3], and will also do another blog post highlighting the differences between functional and kernel tests and the steps taken to do the conversion.