Book Image

Drupal 9 Module Development - Third Edition

By : Daniel Sipos
Book Image

Drupal 9 Module Development - Third Edition

By: Daniel Sipos

Overview of this book

With its latest release, Drupal 9, the popular open source CMS platform has been updated with new functionalities for building complex Drupal apps with ease. This third edition of the Drupal Module Development guide covers these new Drupal features, helping you to stay on top of code deprecations and the changing architecture with every release. The book starts by introducing you to the Drupal 9 architecture and its subsystems before showing you how to create your first module with basic functionality. You’ll explore the Drupal logging and mailing systems, learn how to output data using the theme layer, and work with menus and links programmatically. Once you’ve understood the different kinds of data storage, this Drupal guide will demonstrate how to create custom entities and field types and leverage the Database API for lower-level database queries. You’ll also learn how to introduce JavaScript into your module, work with various file systems, and ensure that your code works on multilingual sites. Finally, you’ll work with Views, create automated tests for your functionality, and write secure code. By the end of the book, you’ll have learned how to develop custom modules that can provide solutions to complex business problems, and who knows, maybe you’ll even contribute to the Drupal community!
Table of Contents (20 chapters)
3
Chapter 3: Logging and Mailing

PHPUnit

Drupal uses PHPUnit as the testing framework for all types of tests. In this section, we will see how we can work with it to run tests.

Important note:

In your development environment (or wherever you want to run the tests), make sure you have the composer dependencies installed with the --dev flag. This will include PHPUnit. Remember not to do this in your production environment as you can compromise the security of your application.

Although Drupal has a UI for running tests, PHPUnit is not well integrated with this. So, it's recommended that we run the tests using the command line instead. Actually, it's very easy to do so. To run an entire test suite (of a certain type), we have to navigate to the Drupal core folder (this works in a normal Drupal site installation where the vendor folder is located there):

cd core  

And run the following command:

../vendor/bin/phpunit --testsuite=unit  

This command goes back a folder through...