You are here

GSoC’ 16: Port Search Configuration module; coding week #11

A sample test result.

I have been involved with the Google Summer of Code' 16 project Search Configuration module to Drupal 8. It is entering into the final week of coding. I have really enjoyed the project process. This give university students a unique opportunity to code for real world projects that will be used by the various open source organisations.
Past week I dealt with testing certain components of my ported module. I worked on implementing the simpletest to test the various functionalities. I would like to take this opportunity to share with you the features of simpletest and how to implement it to test the proper working of the functionalities of various sections of the Drupal modules.

The test files are generally kept in src/Tests/ of the root of the module directory. The name of the test file should be suffixed by Test.php. Add a class that extends the webTestBase. Also, you need to add a namespace for the easy location of the class.

Add the name of the module to get loaded. i.e,
public static $module = ['search_config'];

Now lets start writing the test cases. The functions which should be executed should be prefixed by 'test'. For instance, to navigate to a particular link and check whether it returns the correct page;
public function testLink() {
$this->drupalGet('mypages/search');
$this->assertResponse(200);
}

You can run this test by going to Configuration->Testing->Select the test.
Also, add this test file to the .info.yml file. The following line of code will be sufficient enough.
files[]: src/Tests/fileTest.php

There are various types of test cases, here are the assertions available.
So, here is the complete structure of a sample test file to check the accessibility of a page in my search configuration module:
<?php

namespace Drupal\search_config\Tests;
use Drupal\simpletest\WebTestBase;

/**
* Class searchTest
* @group search_config
*/
class SearchConfigFormTest extends WebTestBase {
public static $module = ['search_config'];

/**
* Function to test the accessibility of the search configuration module location.
*/
public function testSearchConfigURL() {
$this->drupalGet('admin/config/search/pages');
$this->assertResponse(200);
}
}

We need to ensure that we write the group of test, i.e the @group tag. Here in this case is the search_config. The tests won't appear in the testing option in the absence of this tag. Also, ensure that a good definition of the function is given as comments for better understanding for the users reading the code.
You could also try out the various other assertions available to explore the various functionalities implemented. The development version of the module is currently available in the Drupal core.

Stay tuned for further updates on this porting process.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.