function TestBase::restoreEnvironment

Cleans up the test environment and restores the original environment.

Deletes created files, database tables, and reverts environment changes.

This method needs to be invoked for both unit and integration tests.

See also

TestBase::prepareDatabasePrefix()

TestBase::changeDatabasePrefix()

TestBase::prepareEnvironment()

1 call to TestBase::restoreEnvironment()
TestBase::run in core/modules/simpletest/src/TestBase.php
Run all tests in this class.

File

core/modules/simpletest/src/TestBase.php, line 1208

Class

TestBase
Base class for Drupal tests.

Namespace

Drupal\simpletest

Code

private function restoreEnvironment() {
    // Destroy the session if one was started during the test-run.
    $_SESSION = [];
    if (PHP_SAPI !== 'cli' && session_status() === PHP_SESSION_ACTIVE) {
        session_destroy();
        $params = session_get_cookie_params();
        setcookie(session_name(), '', REQUEST_TIME - 3600, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
    }
    session_name($this->originalSessionName);
    // 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();
    if ($this->container && $this->container
        ->has('state') && ($state = $this->container
        ->get('state'))) {
        $captured_emails = $state->get('system.test_mail_collector') ?: [];
        $emailCount = count($captured_emails);
        if ($emailCount) {
            $message = $emailCount == 1 ? '1 email was sent during this test.' : $emailCount . ' emails were sent during this test.';
            $this->pass($message, 'Email');
        }
    }
    // Sleep for 50ms to allow shutdown functions and terminate events to
    // complete. Further information: https://www.drupal.org/node/2194357.
    usleep(50000);
    // Remove all prefixed tables.
    $original_connection_info = Database::getConnectionInfo('simpletest_original_default');
    $original_prefix = $original_connection_info['default']['prefix']['default'];
    $test_connection_info = Database::getConnectionInfo('default');
    $test_prefix = $test_connection_info['default']['prefix']['default'];
    if ($original_prefix != $test_prefix) {
        $tables = Database::getConnection()->schema()
            ->findTables('%');
        foreach ($tables as $table) {
            if (Database::getConnection()->schema()
                ->dropTable($table)) {
                unset($tables[$table]);
            }
        }
    }
    // In case a fatal error occurred that was not in the test process read the
    // log to pick up any fatal errors.
    (new TestDatabase($this->databasePrefix))
        ->logRead($this->testId, get_class($this));
    // Restore original dependency injection container.
    $this->container = $this->originalContainer;
    \Drupal::setContainer($this->originalContainer);
    // Delete test site directory.
    \Drupal::service('file_system')->deleteRecursive($this->siteDirectory, [
        $this,
        'filePreDeleteCallback',
    ]);
    // Restore original database connection.
    Database::removeConnection('default');
    Database::renameConnection('simpletest_original_default', 'default');
    // Reset all static variables.
    // All destructors of statically cached objects have been invoked above;
    // this second reset is guaranteed to reset everything to nothing.
    drupal_static_reset();
    // Restore original in-memory configuration.
    $GLOBALS['config'] = $this->originalConfig;
    $GLOBALS['conf'] = $this->originalConf;
    new Settings($this->originalSettings);
    // Re-initialize original stream wrappers of the parent site.
    // This must happen after static variables have been reset and the original
    // container and $config_directories are restored, as simpletest_log_read()
    // uses the public stream wrapper to locate the error.log.
    $this->originalContainer
        ->get('stream_wrapper_manager')
        ->register();
    if (isset($this->originalPrefix)) {
        drupal_valid_test_ua($this->originalPrefix);
    }
    else {
        drupal_valid_test_ua(FALSE);
    }
    // Restore original shutdown callbacks.
    $callbacks =& drupal_register_shutdown_function();
    $callbacks = $this->originalShutdownCallbacks;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.