1 backdrop_web_test_case.php protected BackdropWebTestCase::tearDown()

Delete created files and temporary files directory, delete the tables created by setUp(), and reset the database prefix.

Overrides BackdropTestCase::tearDown

File

core/modules/simpletest/backdrop_web_test_case.php, line 1865

Class

BackdropWebTestCase
Test case for typical Backdrop tests.

Code

protected function tearDown() {
  global $user, $language, $language_url, $settings, $config_directories;

  // 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);

  $emailCount = count(state_get('test_email_collector', array()));
  if ($emailCount) {
    $message = format_plural($emailCount, '1 email was sent during this test.', '@count emails were sent during this test.');
    $this->pass($message, t('Email'));
  }

  // Delete temporary files directory.
  file_unmanaged_delete_recursive($this->originalFileDirectory . '/simpletest/' . $this->fileDirectoryName);

  // Remove all prefixed tables.
  $connection_info = Database::getConnectionInfo('default');
  $tables = db_find_tables($connection_info['default']['prefix']['default'] . '%');
  if (empty($tables)) {
    $this->fail('Failed to find test tables to drop.');
  }
  $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 PHP 8 some tests encounter problems as some shutdown code tries to
  // access the database connection after it's been explicitly closed (for
  // example the destructor of BackdropCacheArray). We avoid this by not fully
  // destroying the test database connection.
  $close = \PHP_VERSION_ID < 80000;

  // Get back to the original connection.
  Database::removeConnection('default', $close);
  Database::renameConnection('simpletest_original_default', 'default');

  // Delete the database table prefix record.
  db_delete('simpletest_prefix')
    ->condition('test_id', $this->testId)
    ->condition('prefix', $this->databasePrefix)
    ->execute();

  // Set the configuration directories back to the originals.
  $config_directories = $this->originalConfigDirectories;

  // Restore the original settings.
  $settings = $this->originalSettings;

  // Restore original shutdown callbacks array to prevent original
  // environment of calling handlers from test run.
  $callbacks = &backdrop_register_shutdown_function();
  $callbacks = $this->originalShutdownCallbacks;

  // Return the user to the original one.
  $user = $this->originalUser;
  backdrop_save_session(TRUE);

  // Ensure that internal logged in variable and cURL options are reset.
  $this->loggedInUser = FALSE;
  $this->additionalCurlOptions = array();

  // Reload module list and implementations to ensure that test module hooks
  // aren't called after tests.
  module_list(TRUE);
  module_implements_reset();

  // Rebuild caches.
  $this->refreshVariables();

  // Reset language.
  $language = $this->originalLanguage;
  $language_url = $this->originalLanguageUrl;

  // Close the CURL handler.
  $this->curlClose();
}