Testing

Last updated on
25 January 2017

Finally for some QA. Let's check that our module works as expected by writing a few tests of our own which can be run via Drupal itself.

/src/Tests/LoremIpsumTests.php

<?php

namespace Drupal\loremipsum\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Tests for the Lorem Ipsum module.
 *
 * @group loremipsum
 */
class LoremIpsumTests extends WebTestBase {

  /**
   * Modules to install.
   *
   * @var array
   */
  public static $modules = array('loremipsum');

  /**
   * A simple user.
   *
   * @var object
   */
  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 begins by extending the WebTestBase class. It's important to note that Drupal runs tests in a testing database, which is created, populated as needed and then destroyed. This means that you can test on development, staging, and even - if you dare! - production environments.

The first order of business for tests, as you can see above, is to perform some initial setup by defining the modules to test (just the present one), creating a test user and giving it the required permissions.

Then 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->assertResponse(200);
  }

And the config form:

  /**
   * Tests the config form.
   */
  public function testConfigForm() {
    // Login.
    $this->drupalLogin($this->user);

    // Access config page.
    $this->drupalGet('admin/config/development/loremipsum');
    $this->assertResponse(200);
    // Test the form elements exist and have defaults.
    $config = $this->config('loremipsum.settings');
    $this->assertFieldByName(
      'page_title',
      $config->get('loremipsum.settings.page_title'),
      'Page title field has the default value'
    );
    $this->assertFieldByName(
      'source_text',
      $config->get('loremipsum.settings.source_text'),
      'Source text field has the default value'
    );

Next we test the config 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->assertText(
      'The configuration options have been saved.',
      'The form was saved correctly.'
    );

And assert that the new values are there:

    // Test the new values are there.
    $this->drupalGet('admin/config/development/loremipsum');
    $this->assertResponse(200);
    $this->assertFieldByName(
      'page_title',
      'Test lorem ipsum',
      'Page title is OK.'
    );
    $this->assertFieldByName(
      'source_text',
      'Test phrase 1 \nTest phrase 2 \nTest phrase 3 \n',
      'Source text is OK.'
    );
  }

}

To execute the tests
1. enable modules CORE.Testing and DEVELOPMENT:Lorem ipsum
2. go to admin/config/development/testing
3. select test 'loremipsum'
4. press 'Run tests'

And that's it for this tutorial! If you want, grab a copy of this code at the Lorem ipsum project page - keep in mind the current dev version has theming built into it. Also if you have any questions, feel free to drop me a line. Happy coding!