diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 3453b2a..397f87c 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -2503,25 +2503,17 @@ function typed_data() { /** * Returns the test prefix if this is an internal request from SimpleTest. * - * @param string $new_prefix - * Internal use only. A new prefix to be stored. Passed in by tests that use - * the test runner from within a test. - * - * @return + * @return string|false * Either the simpletest prefix (the string "simpletest" followed by any * number of digits) or FALSE if the user agent does not contain a valid * HMAC and timestamp. + * + * @see drupal_generate_test_ua() + * @see WebTestBase::curlInitialize() + * @see UnitTestBase::setUp() */ -function drupal_valid_test_ua($new_prefix = NULL) { +function drupal_valid_test_ua() { global $drupal_hash_salt; - static $test_prefix; - - if (isset($new_prefix)) { - $test_prefix = $new_prefix; - } - if (isset($test_prefix)) { - return $test_prefix; - } if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/^(simpletest\d+);(.+);(.+);(.+)$/", $_SERVER['HTTP_USER_AGENT'], $matches)) { list(, $prefix, $time, $salt, $hmac) = $matches; @@ -2538,9 +2530,7 @@ function drupal_valid_test_ua($new_prefix = NULL) { return $test_prefix; } } - - $test_prefix = FALSE; - return $test_prefix; + return FALSE; } /** @@ -2548,6 +2538,9 @@ function drupal_valid_test_ua($new_prefix = NULL) { */ function drupal_generate_test_ua($prefix) { global $drupal_hash_salt; + // As of now, $drupal_hash_salt can only be changed in settings.php and does + // not change mid-request. As long as that is the case, it is safe to + // statically cache the computed $key. static $key; if (!isset($key)) { diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php index 916cb0d..7d6d0c5 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php @@ -17,8 +17,12 @@ /** * Base class for Drupal tests. * - * Do not extend this class directly, use either Drupal\simpletest\WebTestBaseBase - * or Drupal\simpletest\UnitTestBaseBase. + * Do not extend this class directly, use either Drupal\simpletest\WebTestBase + * or Drupal\simpletest\UnitTestBase. + * + * Requirements: + * - global $drupal_hash_salt needs to be set for drupal_generate_test_ua() to + * generate a user agent HTTP header. */ abstract class TestBase { /** @@ -123,13 +127,6 @@ protected $verboseDirectory; /** - * The original database prefix when running inside Simpletest. - * - * @var string - */ - protected $originalPrefix; - - /** * URL to the verbose output file directory. * * @var string @@ -738,13 +735,6 @@ protected function prepareEnvironment() { global $user, $conf; $language_interface = language(LANGUAGE_TYPE_INTERFACE); - // When running the test runner within a test, back up the original database - // prefix and re-set the new/nested prefix in drupal_valid_test_ua(). - if (drupal_valid_test_ua()) { - $this->originalPrefix = drupal_valid_test_ua(); - drupal_valid_test_ua($this->databasePrefix); - } - // Backup current in-memory configuration. $this->originalConf = $conf; @@ -905,6 +895,12 @@ protected function tearDown() { $this->pass($message, t('E-mail')); } + // Reset all static variables before deleting the testing environment. + // Static variables may contain objects with magic destructors; resetting + // all statics causes them to be invoked and they might write data based on + // the environment information they were given within the test. + drupal_static_reset(); + // Delete temporary files directory. file_unmanaged_delete_recursive($this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10), array($this, 'filePreDeleteCallback')); @@ -938,9 +934,6 @@ protected function tearDown() { // Restore original statics and globals. drupal_container($this->originalContainer); $GLOBALS['config_directories'] = $this->originalConfigDirectories; - if (isset($this->originalPrefix)) { - drupal_valid_test_ua($this->originalPrefix); - } // Reset module list and module load status. module_list_reset(); diff --git a/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php index 98ed71d..adac091 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php @@ -67,7 +67,8 @@ protected function setUp() { } // Set user agent to be consistent with WebTestBase. - $_SERVER['HTTP_USER_AGENT'] = $this->databasePrefix; + // @see WebTestBase::curlInitialize() + $_SERVER['HTTP_USER_AGENT'] = drupal_generate_test_ua($this->databasePrefix); $this->setup = TRUE; }