Issue #1541958 by sun, effulgentsia, alexpott, tim.plunkett, Berdir, David_Rothstein: Fixed Split setUp() into specific sub-methods.

8.0.x
catch 2012-07-18 11:06:49 +01:00
parent b91a015b6c
commit a108b4446b
4 changed files with 40 additions and 0 deletions

View File

@ -86,6 +86,10 @@ abstract class TestBase {
*/
protected $setup = FALSE;
protected $setupDatabasePrefix = FALSE;
protected $setupEnvironment = FALSE;
/**
* Constructor for Test.
*
@ -582,6 +586,12 @@ abstract class TestBase {
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 @@ abstract class TestBase {
);
}
Database::addConnectionInfo('default', 'default', $connection_info['default']);
// Indicate the database prefix was set up correctly.
$this->setupDatabasePrefix = TRUE;
}
/**
@ -661,6 +674,9 @@ abstract class TestBase {
$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;
}
/**

View File

@ -43,6 +43,9 @@ abstract class UnitTestBase extends TestBase {
// 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 @@ abstract class UnitTestBase extends TestBase {
// 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;

View File

@ -586,6 +586,9 @@ abstract class WebTestBase extends TestBase {
// 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 @@ abstract class WebTestBase extends TestBase {
// 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 @@ abstract class WebTestBase extends TestBase {
* 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'] . '%');

View File

@ -93,6 +93,9 @@ abstract class UpgradePathTestBase extends WebTestBase {
// 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 @@ abstract class UpgradePathTestBase extends WebTestBase {
// 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.