Testing Drupal module
Part VI of the practical guide to creating basic Drupal 8 modules
From .info to tests, just the basics
If you've been following this practical guide to creating basic Drupal 8 modules from the beginning, we're now ready for some quality control. If you just want to jump in and play along, you can get the Lorem ipsum module straight from Drupal.
Let's check that the module works properly by writing a few custom tests that can be run through Drupal itself.
/tests/src/Functional/LoremIpsumTest.php
<?php namespace Drupal\Tests\loremipsum\Functional; use Drupal\Tests\BrowserTestBase; /** * Tests for the Lorem Ipsum module. * * @group loremipsum */ class LoremIpsumTests extends BrowserTestBase { /** * Modules to install. * * @var array */ protected static $modules = array('loremipsum'); /** * A simple user. * * @var \Drupal\user\Entity\User */ private $user; /** * Perform initial setup tasks that run before every test method. */ public function setUp() { parent::setUp(); $this->user = $this->drupalCreateUser(array( 'administer site configuration', 'generate lorem ipsum', )); } }
Testing starts with extending the BrowserTestBase class. It's important to note that Drupal runs tests in a test database, which is created, populated as needed, and then destroyed. This means you can test on development, staging, and even — if you're brave! — production environments.
As you can see above, the first step for tests is to perform some initial setup by defining the modules to test (just one for now), creating a test user, and granting the necessary permissions.
Next, we test access to the dummy text generation page:
/** * Tests that the Lorem ipsum page can be reached. */ public function testLoremIpsumPageExists() { // Login. $this->drupalLogin($this->user); // Generator test: $this->drupalGet('loremipsum/generate/4/20'); $this->assertSession()->statusCodeEquals(200); }
And the configuration form:
/** * Tests the config form. */ public function testConfigForm() { // Login. $this->drupalLogin($this->user); // Access config page. $this->drupalGet('admin/config/development/loremipsum'); $this->assertSession()->statusCodeEquals(200); // Test the form elements exist and have defaults. $config = $this->config('loremipsum.settings'); $this->assertSession()->fieldValueEquals( 'page_title', $config->get('loremipsum.page_title'), ); $this->assertSession()->fieldValueEquals( 'source_text', $config->get('loremipsum.source_text'), );
Next we test that the configuration form can be submitted:
// Test form submission. $this->drupalPostForm(NULL, array( 'page_title' => 'Test lorem ipsum', 'source_text' => 'Test phrase 1 \nTest phrase 2 \nTest phrase 3 \n', ), t('Save configuration')); $this->assertSession()->pageTextContains('The configuration options have been saved.');
And assert that the new values are present:
// Test the new values are there. $this->drupalGet('admin/config/development/loremipsum'); $this->assertSession()->statusCodeEquals(200); $this->assertSession()->fieldValueEquals( 'page_title', 'Test lorem ipsum', ); $this->assertSession()->fieldValueEquals( 'source_text', 'Test phrase 1 \nTest phrase 2 \nTest phrase 3 \n', ); } }
To run the tests:
1. Enable the CORE, Testing and Development, and Lorem ipsum modules
2. Go to admin/config/development/testing
3. Select the “Loremipsum” test
4. Click “Run tests”
And that's it for this lesson! If you'd like, grab a copy of this code from the Lorem ipsum project page — keep in mind that the current developer version includes built-in theming. Also, if you have any questions, don’t hesitate to write me. Happy coding!
Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.