summaryrefslogtreecommitdiff
path: root/modules/simpletest/drupal_web_test_case.php
diff options
context:
space:
mode:
Diffstat (limited to 'modules/simpletest/drupal_web_test_case.php')
-rw-r--r--modules/simpletest/drupal_web_test_case.php192
1 files changed, 109 insertions, 83 deletions
diff --git a/modules/simpletest/drupal_web_test_case.php b/modules/simpletest/drupal_web_test_case.php
index 3232c9df6..d96db0cce 100644
--- a/modules/simpletest/drupal_web_test_case.php
+++ b/modules/simpletest/drupal_web_test_case.php
@@ -2,6 +2,18 @@
// $Id$
/**
+ * Global variable that holds information about the tests being run.
+ *
+ * An array, with the following keys:
+ * - 'test_run_id': the ID of the test being run, in the form 'simpletest_%"
+ * - 'in_child_site': TRUE if the current request is a cURL request from
+ * the parent site.
+ *
+ * @var array
+ */
+global $drupal_test_info;
+
+/**
* Base class for Drupal tests.
*
* Do not extend this class, use one of the subclasses in this file.
@@ -15,11 +27,11 @@ abstract class DrupalTestCase {
protected $testId;
/**
- * The original database prefix, before it was changed for testing purposes.
+ * The database prefix of this test run.
*
* @var string
*/
- protected $originalPrefix = NULL;
+ protected $databasePrefix = NULL;
/**
* The original file directory, before it was changed for testing purposes.
@@ -90,8 +102,6 @@ abstract class DrupalTestCase {
* is the caller function itself.
*/
protected function assert($status, $message = '', $group = 'Other', array $caller = NULL) {
- global $db_prefix;
-
// Convert boolean status to string status.
if (is_bool($status)) {
$status = $status ? 'pass' : 'fail';
@@ -105,10 +115,6 @@ abstract class DrupalTestCase {
$caller = $this->getAssertionCall();
}
- // Switch to non-testing database to store results in.
- $current_db_prefix = $db_prefix;
- $db_prefix = $this->originalPrefix;
-
// Creation assertion array that can be displayed while tests are running.
$this->assertions[] = $assertion = array(
'test_id' => $this->testId,
@@ -122,12 +128,11 @@ abstract class DrupalTestCase {
);
// Store assertion for display after the test has completed.
- db_insert('simpletest')
+ Database::getConnection('default', 'simpletest_original_default')
+ ->insert('simpletest')
->fields($assertion)
->execute();
- // Return to testing prefix.
- $db_prefix = $current_db_prefix;
// We do not use a ternary operator here to allow a breakpoint on
// test failure.
if ($status == 'pass') {
@@ -560,10 +565,9 @@ class DrupalUnitTestCase extends DrupalTestCase {
}
protected function setUp() {
- global $db_prefix, $conf;
+ global $conf;
- // Store necessary current values before switching to prefixed database.
- $this->originalPrefix = $db_prefix;
+ // Store necessary current values before switching to the test environment.
$this->originalFileDirectory = file_directory_path();
spl_autoload_register('db_autoload');
@@ -572,11 +576,21 @@ class DrupalUnitTestCase extends DrupalTestCase {
drupal_static_reset();
// Generate temporary prefixed database to ensure that tests have a clean starting point.
- $db_prefix = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
- $conf['file_public_path'] = $this->originalFileDirectory . '/' . $db_prefix;
+ $this->databasePrefix = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
+ $conf['file_public_path'] = $this->originalFileDirectory . '/' . $this->databasePrefix;
+
+ // Clone the current connection and replace the current prefix.
+ $connection_info = Database::getConnectionInfo('default');
+ Database::renameConnection('default', 'simpletest_original_default');
+ foreach ($connection_info as $target => $value) {
+ $connection_info[$target]['prefix'] = array(
+ 'default' => $value['prefix']['default'] . $this->databasePrefix,
+ );
+ }
+ Database::addConnectionInfo('default', 'default', $connection_info['default']);
// Set user agent to be consistent with web test case.
- $_SERVER['HTTP_USER_AGENT'] = $db_prefix;
+ $_SERVER['HTTP_USER_AGENT'] = $this->databasePrefix;
// If locale is enabled then t() will try to access the database and
// subsequently will fail as the database is not accessible.
@@ -589,15 +603,16 @@ class DrupalUnitTestCase extends DrupalTestCase {
}
protected function tearDown() {
- global $db_prefix, $conf;
- if (preg_match('/simpletest\d+/', $db_prefix)) {
- $conf['file_public_path'] = $this->originalFileDirectory;
- // Return the database prefix to the original.
- $db_prefix = $this->originalPrefix;
- // Restore modules if necessary.
- if (isset($this->originalModuleList)) {
- module_list(TRUE, FALSE, FALSE, $this->originalModuleList);
- }
+ global $conf;
+
+ // Get back to the original connection.
+ Database::removeConnection('default');
+ Database::renameConnection('simpletest_original_default', 'default');
+
+ $conf['file_public_path'] = $this->originalFileDirectory;
+ // Restore modules if necessary.
+ if (isset($this->originalModuleList)) {
+ module_list(TRUE, FALSE, FALSE, $this->originalModuleList);
}
}
}
@@ -1107,12 +1122,28 @@ class DrupalWebTestCase extends DrupalTestCase {
* either a single array or a variable number of string arguments.
*/
protected function setUp() {
- global $db_prefix, $user, $language, $conf;
+ global $user, $language, $conf;
+
+ // Generate a temporary prefixed database to ensure that tests have a clean starting point.
+ $this->databasePrefix = 'simpletest' . mt_rand(1000, 1000000);
+ db_update('simpletest_test_id')
+ ->fields(array('last_prefix' => $this->databasePrefix))
+ ->condition('test_id', $this->testId)
+ ->execute();
+
+ // Clone the current connection and replace the current prefix.
+ $connection_info = Database::getConnectionInfo('default');
+ Database::renameConnection('default', 'simpletest_original_default');
+ foreach ($connection_info as $target => $value) {
+ $connection_info[$target]['prefix'] = array(
+ 'default' => $value['prefix']['default'] . $this->databasePrefix,
+ );
+ }
+ Database::addConnectionInfo('default', 'default', $connection_info['default']);
// Store necessary current values before switching to prefixed database.
$this->originalLanguage = $language;
$this->originalLanguageDefault = variable_get('language_default');
- $this->originalPrefix = $db_prefix;
$this->originalFileDirectory = file_directory_path();
$this->originalProfile = drupal_get_profile();
$clean_url_original = variable_get('clean_url', 0);
@@ -1125,18 +1156,10 @@ class DrupalWebTestCase extends DrupalTestCase {
$this->originalShutdownCallbacks = $callbacks;
$callbacks = array();
- // Generate temporary prefixed database to ensure that tests have a clean starting point.
- $db_prefix_new = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
- db_update('simpletest_test_id')
- ->fields(array('last_prefix' => $db_prefix_new))
- ->condition('test_id', $this->testId)
- ->execute();
- $db_prefix = $db_prefix_new;
-
// Create test directory ahead of installation so fatal errors and debug
// information can be logged during installation process.
// Use temporary files directory with the same prefix as the database.
- $public_files_directory = $this->originalFileDirectory . '/simpletest/' . substr($db_prefix, 10);
+ $public_files_directory = $this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10);
$private_files_directory = $public_files_directory . '/private';
$temp_files_directory = $private_files_directory . '/temp';
@@ -1154,6 +1177,11 @@ class DrupalWebTestCase extends DrupalTestCase {
$conf = array();
drupal_static_reset();
+ // Set the test information for use in other parts of Drupal.
+ $test_info = &$GLOBALS['drupal_test_info'];
+ $test_info['test_run_id'] = $this->databasePrefix;
+ $test_info['in_child_site'] = FALSE;
+
include_once DRUPAL_ROOT . '/includes/install.inc';
drupal_install_system();
@@ -1230,8 +1258,9 @@ class DrupalWebTestCase extends DrupalTestCase {
* setup a clean environment for the current test run.
*/
protected function preloadRegistry() {
- db_query('INSERT INTO {registry} SELECT * FROM ' . $this->originalPrefix . 'registry');
- db_query('INSERT INTO {registry_file} SELECT * FROM ' . $this->originalPrefix . 'registry_file');
+ $original_connection = Database::getConnection('default', 'simpletest_original_default');
+ db_query('INSERT INTO {registry} SELECT * FROM ' . $original_connection->prefixTables('{registry}'));
+ db_query('INSERT INTO {registry_file} SELECT * FROM ' . $original_connection->prefixTables('{registry_file}'));
}
/**
@@ -1257,14 +1286,11 @@ class DrupalWebTestCase extends DrupalTestCase {
* and reset the database prefix.
*/
protected function tearDown() {
- global $db_prefix, $user, $language;
+ global $user, $language;
// In case a fatal error occured that was not in the test process read the
// log to pick up any fatal errors.
- $db_prefix_temp = $db_prefix;
- $db_prefix = $this->originalPrefix;
- simpletest_log_read($this->testId, $db_prefix, get_class($this), TRUE);
- $db_prefix = $db_prefix_temp;
+ simpletest_log_read($this->testId, $this->databasePrefix, get_class($this), TRUE);
$emailCount = count(variable_get('drupal_test_email_collector', array()));
if ($emailCount) {
@@ -1272,53 +1298,52 @@ class DrupalWebTestCase extends DrupalTestCase {
$this->pass($message, t('E-mail'));
}
- if (preg_match('/simpletest\d+/', $db_prefix)) {
- // Delete temporary files directory.
- file_unmanaged_delete_recursive($this->originalFileDirectory . '/simpletest/' . substr($db_prefix, 10));
+ // Delete temporary files directory.
+ file_unmanaged_delete_recursive($this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10));
- // Remove all prefixed tables (all the tables in the schema).
- $schema = drupal_get_schema(NULL, TRUE);
- $ret = array();
- foreach ($schema as $name => $table) {
- db_drop_table($name);
- }
-
- // Return the database prefix to the original.
- $db_prefix = $this->originalPrefix;
+ // Remove all prefixed tables (all the tables in the schema).
+ $schema = drupal_get_schema(NULL, TRUE);
+ $ret = array();
+ foreach ($schema as $name => $table) {
+ db_drop_table($name);
+ }
- // Restore original shutdown callbacks array to prevent original
- // environment of calling handlers from test run.
- $callbacks = &drupal_register_shutdown_function();
- $callbacks = $this->originalShutdownCallbacks;
+ // Get back to the original connection.
+ Database::removeConnection('default');
+ Database::renameConnection('simpletest_original_default', 'default');
- // Return the user to the original one.
- $user = $this->originalUser;
- drupal_save_session(TRUE);
+ // Restore original shutdown callbacks array to prevent original
+ // environment of calling handlers from test run.
+ $callbacks = &drupal_register_shutdown_function();
+ $callbacks = $this->originalShutdownCallbacks;
- // Ensure that internal logged in variable and cURL options are reset.
- $this->loggedInUser = FALSE;
- $this->additionalCurlOptions = array();
+ // Return the user to the original one.
+ $user = $this->originalUser;
+ drupal_save_session(TRUE);
- // Reload module list and implementations to ensure that test module hooks
- // aren't called after tests.
- module_list(TRUE);
- module_implements('', FALSE, TRUE);
+ // Ensure that internal logged in variable and cURL options are reset.
+ $this->loggedInUser = FALSE;
+ $this->additionalCurlOptions = array();
- // Reset the Field API.
- field_cache_clear();
+ // Reload module list and implementations to ensure that test module hooks
+ // aren't called after tests.
+ module_list(TRUE);
+ module_implements('', FALSE, TRUE);
- // Rebuild caches.
- $this->refreshVariables();
+ // Reset the Field API.
+ field_cache_clear();
- // Reset language.
- $language = $this->originalLanguage;
- if ($this->originalLanguageDefault) {
- $GLOBALS['conf']['language_default'] = $this->originalLanguageDefault;
- }
+ // Rebuild caches.
+ $this->refreshVariables();
- // Close the CURL handler.
- $this->curlClose();
+ // Reset language.
+ $language = $this->originalLanguage;
+ if ($this->originalLanguageDefault) {
+ $GLOBALS['conf']['language_default'] = $this->originalLanguageDefault;
}
+
+ // Close the CURL handler.
+ $this->curlClose();
}
/**
@@ -1330,7 +1355,7 @@ class DrupalWebTestCase extends DrupalTestCase {
* See the description of $curl_options for other options.
*/
protected function curlInitialize() {
- global $base_url, $db_prefix;
+ global $base_url;
if (!isset($this->curlHandle)) {
$this->curlHandle = curl_init();
@@ -1342,6 +1367,7 @@ class DrupalWebTestCase extends DrupalTestCase {
CURLOPT_SSL_VERIFYPEER => FALSE, // Required to make the tests run on https.
CURLOPT_SSL_VERIFYHOST => FALSE, // Required to make the tests run on https.
CURLOPT_HEADERFUNCTION => array(&$this, 'curlHeaderCallback'),
+ CURLOPT_USERAGENT => $this->databasePrefix,
);
if (isset($this->httpauth_credentials)) {
$curl_options[CURLOPT_HTTPAUTH] = $this->httpauth_method;
@@ -1354,7 +1380,7 @@ class DrupalWebTestCase extends DrupalTestCase {
}
// We set the user agent header on each request so as to use the current
// time and a new uniqid.
- if (preg_match('/simpletest\d+/', $db_prefix, $matches)) {
+ if (preg_match('/simpletest\d+/', $this->databasePrefix, $matches)) {
curl_setopt($this->curlHandle, CURLOPT_USERAGENT, drupal_generate_test_ua($matches[0]));
}
}