Drupal Testing
Drupal is a widely used open-source content management system (CMS) written in PHP that is used to build websites. Its feature-rich platform makes it flexible and a popular choice for CMS web development. Like any other website, a website built with Drupal requires thorough testing.
Let's dive deeper into the Drupal ecosystem and understand how to test these websites using the tools that are provided within the Drupal ecosystem and also look into other efficient tools to achieve a seamless user experience.
What is Drupal?
Drupal is a powerful CMS that is largely used to create websites. Many government agencies, educational institutions, and businesses build their websites using Drupal.
Advantages of Using Drupal
- Drupal is an open-source framework.
- It is a flexible platform that works well with different content types. It is adopted as a CMS as well as a development platform. With provisions for custom content types and views, to name a few of the available features, Drupal is truly a flexible platform.
- Drupal is great for complex projects and websites due to its feature-rich platform.
- It is SEO friendly, allowing the website author to add tags and keywords.
- It provides a secure platform for building a website.
- Has a lot of add-on options for custom themes and modules, providing full control over the visual display of content on the website.
- The Drupal community is active and responsive. Hence it is easy to find answers to the most common challenges and get help from the community.
How Can You Test These Websites?
- Unit testing
- Kernel testing
- End-to-end testing
Drupal Unit Testing
Use the PHPUnit testing framework for Drupal unit testing as a foundation for your testing strategy. PHPUnit installation is straightforward: just run composer require --dev phpunit/phpunit in the root directory of your Drupal project.
- The base class \Drupal\Tests\UnitTestCase is implemented to create unit test cases. This class provides helpers and a base class for writing unit tests.
- All unit test file names should follow *Test.php pattern
- All method names should follow test* pattern (e.g.: testGetLocation())
<?php namespace Drupal\phpunit_my_example; class Unit { private $length = 0; public function setLength(int $length) { $this->length = $length; } public function getLength() { return $this->length; } }
Drupal Kernel Testing
This type of testing is an intermediate between unit testing and functional testing, helping to verify the integrations of various modules. With kernel testing, you can test limited Drupal interactions. However, it does not apply to those test scenarios that require browser or UI interactions.
Kernel tests operate in a partial Drupal environment, meaning only the core of Drupal is enabled. The base class \Drupal\KernelTests\KernelTestBase is implemented to write kernel test cases. This base class partially boots Drupal for these tests and allows the loading of services and hooks of other modules.
- ::installSchema () for database tables
- ::installConfig () for default configuration
- ::installEntitySchema () for, you guessed it - entities
public function testLabel() { $label = $this->myService->getLabel('my_service_test'); $this->assertTrue($label == 'My Test Label', $label); } public function testMessage() { $message = $this->myService->getMessage('my_service_test'); $this->assertTrue($message == 'My Test Message', $message); }
To run your tests, you can either use PHPUnit (although you won't get a very detailed output), or a run-tests.sh script - which is already included with each Drupal installation. We recommend using this script since it provides a much nicer and more detailed output.
Drupal End-to-end Testing
- Browser testing
- Javascript based testing
- Using Selenium-based tools
- testRigor
Drupal Browser Testing
These tests have the full Drupal functionality and an internally simulated web browser at their disposal. These tests are meant to test top-level tests of integrations between different Drupal systems. They are usually written for scenarios where JavaScript testing is not involved. The base class \Drupal\Tests\BrowserTestBase is involved in writing browser test cases.
In case you've previously had a chance to use WebTestBase, then BrowserTestBase is what replaces it, and you should be able to convert your old tests to BrowserTestBase rather easily.Here's all the detailed information you would need to get familiar with these tests.
Drupal JavaScript Testing
In comparison to other tests, the JavaScript function tests in Drupal are executed in an actual browser to test JavaScript and AJAX functionalities.The base class \Drupal\FunctionalJavascriptTests\WebDriverTestBase is implemented to write JavaScript test cases. This base class runs a driver that is able to support JavaScript.
To get familiar with how to write JavaScript test cases for drupal, take a look at this detailed tutorial.
Functional UI End-to-end Testing With testRigor
Tests created with testRigor are the closest to representing the actual behavior of your users. Tests are on the UI layer, meaning that no details of implementation are involved. And while end-to-end tests typically represent the entire flows, they are known to be the most fragile and slow. Not with testRigor! Tests are extremely robust and stable, and test maintenance issues are completely solved, with users now spending 95% less time on test maintenance.
The best part is that testRigor is a cloud-hosted codeless solution, meaning that anyone on the team will be able to create, edit, and maintain such tests. You can create tests going through an entire user flow in plain English, and get test results with detailed screenshots for each step - making it extremely easy to debug.
There are a lot of handy built-in features; for example, testRigor is great for testing tables. You can also easily test emails and SMS alerts.
open url "https://testrigor.com/samples/table1/" enter "This is a trap!" into table "table table-responsive table-striped table-hover table-bordered" at row "101" and column "Additional Data"
send email to "user@customer.com" with subject "Test message" and body "Hi, new content is available." check that email from "user@customer.com" is delivered
You can find more benefits of using testRigor here.
Conclusion
Like testing any other website, Drupal-based websites also require unit testing, kernel testing, and end-to-end testing. We hope that this article provides you with the tools needed to build a robust testing strategy - and make your customers satisfied with high-quality software.