Allow partial spoofing of the user agent during web/browser tests

Created on 19 October 2016, over 7 years ago
Updated 22 March 2023, about 1 year ago

I'm working on a module that has some business logic around the user agent, but I cannot write functional tests for this because changing the user agent breaks the drupal_valid_test_ua function which validates if a Drupal request originates from a test.

BrowserTestBase has this code that is called before each request:

  protected function prepareRequest() {
    $session = $this->getSession();
    $session->setCookie('SIMPLETEST_USER_AGENT', drupal_generate_test_ua($this->databasePrefix));
  }

Sprinkled throughout core are calls to drupal_valid_test_ua which has a regex for checking the user agent is valid for a test:

preg_match("/^simple(\w+\d+):(.+):(.+):(.+)$/", $user_agent, $matches)

I propose that the regex be changed to something like this, so other user agent text can be added to the end, separated by a space:

preg_match("/^simple(\w+\d+):(.+):(.+):([^ ]+)/", $user_agent, $matches)
📌 Task
Status

Needs work

Version

9.5

Component
Simpletest  →

Last updated 1 day ago

Created by

🇺🇸United States bkosborne New Jersey, USA

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024