This project is not covered by Drupal’s security advisory policy.

This module aims to help with test setup and assertions, as well as generally improve the API surface when writing tests (particularly Kernel Tests).

The module is broken down into three areas -

  • HTTP
  • Installs
  • Support

Each section has a number of traits that aim to improve the readability and testability of code, with the intention of being complementary to current tests.

HTTP

For testing request / response flow, the module provides a MakesHttpRequests trait.

This trait allows you to execute requests using the following HTTP verbs -

  • DELETE
  • GET
  • PATCH
  • POST
  • PUT

Each HTTP request inside a test returns an instance of TestResponse which provides an expanded API surface for assertions, such as assertions against status codes and JSON data.

The following example makes use of support methods on the `InteractsWithAuthentication` trait.

$this->actingAsAnonymous()->get($authenticatedRoute)->assertForbidden()

$this->actingAs($authenticatedUser)->get($authenticatedRoute)->assertOk()

Installs

Some tests may depend on active configuration from `config/sync`, such as fields. For this reason the module provides the `InstallsExportedConfig` trait.

This trait is comprised of many other traits that attempt to install active configuration for -

  • Blocks
  • Entity Types
  • Fields
  • Image Styles
  • Menus
  • Modules
  • Roles
  • Themes
  • Views
  • Vocabularies

If required, you can use the `installExportedConfig` method on the `InstallsExportedConfig` trait to install YAML configuration file.

E.G.

$this->installExportedConfig('node.type.page')

Support

The module provides supporting traits in an attempt to keep tests looking clean. These traits are

InteractsWithAuthentication

Provides a couple of helper methods to set the current user to either a valid User entity or an anonymous user.

InteractsWithBatches

To execute batches inside of tests.

InteractsWithLanguages

To set up languages and their optional prefixes.

InteractsWithMail

Uses Drupals test mail collector and provides a TestMail class for extra assertions on mail sent during test run.

InteractsWithQueues

Provides helper methods to interact with queues.

WithoutEvents

Prevents firing events during a test, allowing for assertions such as -

  • expectsEventsto pre assert a given event will be fired
  • doesntExpectsEventsto pre assert a given event won't be fired
  • assertDispatched to assert a given event was fired, with optional callback to assert against that events details
  • assertNotDispatchedto assert a given event was not fired

WithoutEventSubscribers

Prevents subscribers to particular events from being actioned, either by service name or class string.

The options are -

  • withoutSubscribers to prevent all subscribers from being actioned
  • withoutSubscribersForEvents to prevent only the given events subscribers from being actioned

Project information

  • caution Minimally maintained
    Maintainers monitor issues, but fast responses are not guaranteed.
  • Module categories: Developer Tools
  • Ecosystem: Drupal Platform
  • Created by aidenbradley on , updated
  • shield alertThis project is not covered by the security advisory policy.
    Use at your own risk! It may have publicly disclosed vulnerabilities.

Releases