Deletes created files, database tables, and reverts all environment changes.
This method needs to be invoked for both unit and integration tests.
TestBase::prepareDatabasePrefix()
TestBase::changeDatabasePrefix()
TestBase::prepareEnvironment()
protected function tearDown() {
global $user, $conf;
// Reset all static variables.
// Unsetting static variables will potentially invoke destruct methods,
// which might call into functions that prime statics and caches again.
// In that case, all functions are still operating on the test environment,
// which means they may need to access its filesystem and database.
drupal_static_reset();
// 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) {
// Remove all prefixed tables.
$connection_info = Database::getConnectionInfo('default');
$tables = db_find_tables($connection_info['default']['prefix']['default'] . '%');
$prefix_length = strlen($connection_info['default']['prefix']['default']);
foreach ($tables as $table) {
if (db_drop_table(substr($table, $prefix_length))) {
unset($tables[$table]);
}
}
if (!empty($tables)) {
$this
->fail('Failed to drop all prefixed tables.');
}
}
// In case a fatal error occurred that was not in the test process read the
// log to pick up any fatal errors.
simpletest_log_read($this->testId, $this->databasePrefix, get_class($this), TRUE);
if (($container = drupal_container()) && $container
->has('keyvalue')) {
$captured_emails = state()
->get('system.test_email_collector') ?: array();
$emailCount = count($captured_emails);
if ($emailCount) {
$message = format_plural($emailCount, '1 e-mail was sent during this test.', '@count e-mails were sent during this test.');
$this
->pass($message, t('E-mail'));
}
}
// Delete temporary files directory.
file_unmanaged_delete_recursive($this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10), array(
$this,
'filePreDeleteCallback',
));
// Restore original database connection.
Database::removeConnection('default');
Database::renameConnection('simpletest_original_default', 'default');
// @see TestBase::changeDatabasePrefix()
global $databases;
$connection_info = Database::getConnectionInfo('default');
$databases['default']['default'] = $connection_info['default'];
// Restore original globals.
if (isset($this->originalThemeKey)) {
$GLOBALS['theme_key'] = $this->originalThemeKey;
}
$GLOBALS['theme'] = $this->originalTheme;
// Reset all static variables.
// All destructors of statically cached objects have been invoked above;
// this second reset is guranteed to reset everything to nothing.
drupal_static_reset();
// Reset static in language().
// Restoring drupal_container() makes language() return the proper languages
// already, but it contains an additional static that needs to be reset. The
// reset can happen before the container is restored, as it is unnecessary
// to reset the language_manager service.
language(NULL, TRUE);
// Restore original in-memory configuration.
$conf = $this->originalConf;
// 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();
module_load_all(FALSE, TRUE);
// Restore original shutdown callbacks.
$callbacks =& drupal_register_shutdown_function();
$callbacks = $this->originalShutdownCallbacks;
// Restore original user session.
$user = $this->originalUser;
drupal_save_session(TRUE);
}