previousnext/drupal-test-utils

Utility traits for Drupal testing.

0.0.1 2022-12-13 23:56 UTC

This package is auto-updated.

Last update: 2024-04-07 03:35:01 UTC


README

Utilities for testing Drupal!

ExpectsCacheableResponseTrait

A trait to add Dynamic Page Cache cacheability testing to every request in your Functional tests.

Usage

Once your trait is added to your test base class, you can check cachability by overriding the drupalGet function.

/**
 * {@inheritdoc}
 */
protected function drupalGet($path, array $options = [], array $headers = []): string {
  $response = parent::drupalGet($path, $options, $headers);
  $this->detectUncacheableResponse($path, $options);
  return $response;
}

Marking paths as uncacheable

There are some paths that are always uncacheable (i.e pages with forms like node/add). These paths can marked as uncacheable by adding the $uncacheableDynamicPagePatterns property to your tests. You can add a common set of these to your test base and add more specific paths in indivdual tests as these patterns will be gathered up the class hierarchy at run time.

Patterns are regular expressions matched with preg_match

E.g

 protected static array $uncacheableDynamicPagePatterns = [
   '/user/login',
   '/big_pipe/no-js',
   '/batch',
   '/node/add/.*',
 ];