2

Here's the best I've come up with so far:

/**
 * Returns TRUE if our code is running via a simpletest.
 *
 * We don't check for the database prefix so that this can be used by Unit
 * Tests also.
 *
 * We could set a variable in the test setUp method and check that variable,
 * but then some dummy could misuse that variable in code intended for
 * production.
 */
function check_is_simpletest() {
    return class_exists('DrupalTestCase', FALSE) || strpos($_SERVER['HTTP_USER_AGENT'], 'simpletest') === 0;
}

Is this crazy? I'd expect it to break in the Testing UI pages of the site, but I'm only concerned with testing custom code which will not affect those pages, and I usually test via drush.

| improve this question | | | | |
3

In Drupal 7, take a look at drupal_valid_test_ua(), it appears to return the database prefix when simpletest is running, or FALSE otherwise

It is used in _drupal_bootstrap_database():

  // The user agent header is used to pass a database prefix in the request when
  // running tests. However, for security reasons, it is imperative that we
  // validate we ourselves made the request.
  if ($test_prefix = drupal_valid_test_ua()) {
    // Set the test run id for use in other parts of Drupal.
    $test_info = &$GLOBALS['drupal_test_info'];
    $test_info['test_run_id'] = $test_prefix;
    $test_info['in_child_site'] = TRUE;
| improve this answer | | | | |
4

In Drupal 7, drupal_http_request() use the following code to add an extra header, when tests are running.

  // If the database prefix is being used by SimpleTest to run the tests in a copied
  // database then set the user-agent header to the database prefix so that any
  // calls to other Drupal pages will run the SimpleTest prefixed database. The
  // user-agent is used to ensure that multiple testing sessions running at the
  // same time won't interfere with each other as they would if the database
  // prefix were stored statically in a file or database variable.
  $test_info = &$GLOBALS['drupal_test_info'];
  if (!empty($test_info['test_run_id'])) {
    $options['headers']['User-Agent'] = drupal_generate_test_ua($test_info['test_run_id']);
  }

In your module, similar code should work.

  $test_info = $GLOBALS['drupal_test_info'];
  if (!empty($test_info['test_run_id'])) {
    // Drupal is running tests.
  }

In Drupal 6, the following code is present in drupal_http_request().

  // If the database prefix is being used by SimpleTest to run the tests in a copied
  // database then set the user-agent header to the database prefix so that any
  // calls to other Drupal pages will run the SimpleTest prefixed database. The
  // user-agent is used to ensure that multiple testing sessions running at the
  // same time won't interfere with each other as they would if the database
  // prefix were stored statically in a file or database variable.
  if (is_string($db_prefix) && preg_match("/^simpletest\d+$/", $db_prefix, $matches)) {
    $defaults['User-Agent'] = 'User-Agent: ' . $matches[0];
  }

The following code should work for a Drupal 6 module.

  global $db_prefix;

  if (is_string($db_prefix) && preg_match("/^simpletest\d+$/", $db_prefix)) {
    // Drupal is running tests.
  }
| improve this answer | | | | |
1

This worked for me in Drupal 8:

if ( array_key_exists('simpletest.settings', $GLOBALS['conf']) ) {
  return;
}

I used this in hook_node_presave in a .module file, to stop the hook code being executed during testing.

| improve this answer | | | | |
  • drupal_valid_test_ua() still works as well. – wizonesolutions Nov 6 '17 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.