Commit a108b444 authored by catch's avatar catch

Issue #1541958 by sun, effulgentsia, alexpott, tim.plunkett, Berdir,...

Issue #1541958 by sun, effulgentsia, alexpott, tim.plunkett, Berdir, David_Rothstein: Fixed Split setUp() into specific sub-methods.
parent b91a015b
......@@ -86,6 +86,10 @@ abstract class TestBase {
*/
protected $setup = FALSE;
protected $setupDatabasePrefix = FALSE;
protected $setupEnvironment = FALSE;
/**
* Constructor for Test.
*
......@@ -582,6 +586,12 @@ protected function prepareDatabasePrefix() {
protected function changeDatabasePrefix() {
if (empty($this->databasePrefix)) {
$this->prepareDatabasePrefix();
// If $this->prepareDatabasePrefix() failed to work, return without
// setting $this->setupDatabasePrefix to TRUE, so setUp() methods will
// know to bail out.
if (empty($this->databasePrefix)) {
return;
}
}
// Clone the current connection and replace the current prefix.
......@@ -593,6 +603,9 @@ protected function changeDatabasePrefix() {
);
}
Database::addConnectionInfo('default', 'default', $connection_info['default']);
// Indicate the database prefix was set up correctly.
$this->setupDatabasePrefix = TRUE;
}
/**
......@@ -661,6 +674,9 @@ protected function prepareEnvironment() {
$test_info = &$GLOBALS['drupal_test_info'];
$test_info['test_run_id'] = $this->databasePrefix;
$test_info['in_child_site'] = FALSE;
// Indicate the environment was set up correctly.
$this->setupEnvironment = TRUE;
}
/**
......
......@@ -43,6 +43,9 @@ protected function setUp() {
// Prepare the environment for running tests.
$this->prepareEnvironment();
if (!$this->setupEnvironment) {
return FALSE;
}
$this->originalThemeRegistry = theme_get_registry(FALSE);
// Reset all statics and variables to perform tests in a clean environment.
......@@ -65,6 +68,9 @@ protected function setUp() {
// changed, since Drupal\Core\Utility\CacheArray implementations attempt to
// write back to persistent caches when they are destructed.
$this->changeDatabasePrefix();
if (!$this->setupDatabasePrefix) {
return FALSE;
}
// Set user agent to be consistent with WebTestBase.
$_SERVER['HTTP_USER_AGENT'] = $this->databasePrefix;
......
......@@ -586,6 +586,9 @@ protected function setUp() {
// Prepare the environment for running tests.
$this->prepareEnvironment();
if (!$this->setupEnvironment) {
return FALSE;
}
// Reset all statics and variables to perform tests in a clean environment.
$conf = array();
......@@ -596,6 +599,9 @@ protected function setUp() {
// changed, since Drupal\Core\Utility\CacheArray implementations attempt to
// write back to persistent caches when they are destructed.
$this->changeDatabasePrefix();
if (!$this->setupDatabasePrefix) {
return FALSE;
}
// Preset the 'install_profile' system variable, so the first call into
// system_rebuild_module_data() (in drupal_install_system()) will register
......@@ -753,6 +759,12 @@ protected function refreshVariables() {
* and reset the database prefix.
*/
protected function tearDown() {
// Ensure that TestBase::changeDatabasePrefix() has run and TestBase::$setup
// was not tricked into TRUE, since the following code would delete the
// entire parent site otherwise.
if (!$this->setupDatabasePrefix) {
return FALSE;
}
// Remove all prefixed tables.
$connection_info = Database::getConnectionInfo('default');
$tables = db_find_tables($connection_info['default']['prefix']['default'] . '%');
......
......@@ -93,6 +93,9 @@ protected function setUp() {
// Prepare the environment for running tests.
$this->prepareEnvironment();
if (!$this->setupEnvironment) {
return FALSE;
}
// Reset all statics and variables to perform tests in a clean environment.
$conf = array();
......@@ -103,6 +106,9 @@ protected function setUp() {
// changed, since Drupal\Core\Utility\CacheArray implementations attempt to
// write back to persistent caches when they are destructed.
$this->changeDatabasePrefix();
if (!$this->setupDatabasePrefix) {
return FALSE;
}
// Unregister the registry.
// This is required to make sure that the database layer works properly.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment