diff --git a/core/modules/simpletest/config/schema/simpletest.schema.yml b/core/modules/simpletest/config/schema/simpletest.schema.yml index 5423adc..21fd498 100644 --- a/core/modules/simpletest/config/schema/simpletest.schema.yml +++ b/core/modules/simpletest/config/schema/simpletest.schema.yml @@ -4,6 +4,13 @@ simpletest.settings: type: mapping label: 'Testing' mapping: + clear: + type: mapping + label: 'Clean-up options' + mapping: + artifacts: + type: boolean + label: 'Clear test artifacts' clear_results: type: boolean label: 'Clear results after each complete test suite run' diff --git a/core/modules/simpletest/config/simpletest.settings.yml b/core/modules/simpletest/config/simpletest.settings.yml index a2254cc..bfa6958 100644 --- a/core/modules/simpletest/config/simpletest.settings.yml +++ b/core/modules/simpletest/config/simpletest.settings.yml @@ -1,3 +1,5 @@ +clear: + artifacts: true clear_results: true httpauth: method: 1 diff --git a/core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestSettingsForm.php b/core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestSettingsForm.php index 24bdb85..94ed19d 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestSettingsForm.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/Form/SimpletestSettingsForm.php @@ -30,18 +30,24 @@ public function buildForm(array $form, array &$form_state) { '#type' => 'details', '#title' => $this->t('General'), ); - $form['general']['simpletest_clear_results'] = array( - '#type' => 'checkbox', - '#title' => $this->t('Clear results after each complete test suite run'), - '#description' => $this->t('By default SimpleTest will clear the results after they have been viewed on the results page, but in some cases it may be useful to leave the results in the database. The results can then be viewed at admin/config/development/testing/results/[test_id]. The test ID can be found in the database, simpletest table, or kept track of when viewing the results the first time. Additionally, some modules may provide more analysis or features that require this setting to be disabled.'), - '#default_value' => $config->get('clear_results'), - ); $form['general']['simpletest_verbose'] = array( '#type' => 'checkbox', '#title' => $this->t('Provide verbose information when running tests'), '#description' => $this->t('The verbose data will be printed along with the standard assertions and is useful for debugging. The verbose data will be erased between each test suite run. The verbose data output is very detailed and should only be used when debugging.'), '#default_value' => $config->get('verbose'), ); + $form['general']['simpletest_clear_artifacts'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Keep test artifacts'), + '#description' => $this->t('Retains the test site directory and database tables of all executed tests instead of deleting them to allow for advanced debugging.'), + '#default_value' => !$config->get('clear.artifacts'), + ); + $form['general']['simpletest_clear_results'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Clear results after each complete test suite run'), + '#description' => $this->t('By default SimpleTest will clear the results after they have been viewed on the results page, but in some cases it may be useful to leave the results in the database. The results can then be viewed at admin/config/development/testing/results/[test_id]. The test ID can be found in the database, simpletest table, or kept track of when viewing the results the first time. Additionally, some modules may provide more analysis or features that require this setting to be disabled.'), + '#default_value' => $config->get('clear_results'), + ); $form['httpauth'] = array( '#type' => 'details', @@ -108,8 +114,9 @@ public function validateForm(array &$form, array &$form_state) { */ public function submitForm(array &$form, array &$form_state) { $this->configFactory->get('simpletest.settings') - ->set('clear_results', $form_state['values']['simpletest_clear_results']) ->set('verbose', $form_state['values']['simpletest_verbose']) + ->set('clear.artifacts', !$form_state['values']['simpletest_clear_artifacts']) + ->set('clear_results', $form_state['values']['simpletest_clear_results']) ->set('httpauth.method', $form_state['values']['simpletest_httpauth_method']) ->set('httpauth.username', $form_state['values']['simpletest_httpauth_username']) ->set('httpauth.password', $form_state['values']['simpletest_httpauth_password']) diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php index 543105f..29bc1f9 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php @@ -92,6 +92,13 @@ protected $skipClasses = array(__CLASS__ => TRUE); /** + * Whether to delete all test artifacts after a test run. + * + * @var bool + */ + public $clearArtifacts; + + /** * TRUE if verbose debugging is enabled. * * @var boolean @@ -751,6 +758,7 @@ public function run(array $methods = array()) { $simpletest_config = \Drupal::config('simpletest.settings'); $class = get_class($this); + // Unless preset from run-tests.sh, retrieve the current verbose setting. if (!isset($this->verbose)) { $this->verbose = $simpletest_config->get('verbose'); @@ -765,6 +773,11 @@ public function run(array $methods = array()) { } $this->verboseClassName = str_replace("\\", "_", $class); } + + if (!isset($this->clearArtifacts)) { + $this->clearArtifacts = $simpletest_config->get('clear.artifacts'); + } + // HTTP auth settings (:) for the simpletest browser // when sending requests to the test site. $this->httpauth_method = (int) $simpletest_config->get('httpauth.method'); @@ -1143,28 +1156,29 @@ private function restoreEnvironment() { } } - // Remove all prefixed tables. - // @todo Connection prefix info is not normalized into an array. - $original_connection_info = Database::getConnectionInfo('simpletest_original_default'); - $original_prefix = is_array($original_connection_info['default']['prefix']) ? $original_connection_info['default']['prefix']['default'] : $original_connection_info['default']['prefix']; - $test_connection_info = Database::getConnectionInfo('default'); - $test_prefix = is_array($test_connection_info['default']['prefix']) ? $test_connection_info['default']['prefix']['default'] : $test_connection_info['default']['prefix']; - if ($original_prefix != $test_prefix) { - $tables = Database::getConnection()->schema()->findTables($test_prefix . '%'); - $prefix_length = strlen($test_prefix); - foreach ($tables as $table) { - if (Database::getConnection()->schema()->dropTable(substr($table, $prefix_length))) { - 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. simpletest_log_read($this->testId, $this->databasePrefix, get_class($this)); - // Delete test site directory. - file_unmanaged_delete_recursive($this->siteDirectory, array($this, 'filePreDeleteCallback')); + if ($this->clearArtifacts) { + // Remove all prefixed tables. + // @todo Connection prefix info is not normalized into an array. + $original_connection_info = Database::getConnectionInfo('simpletest_original_default'); + $original_prefix = is_array($original_connection_info['default']['prefix']) ? $original_connection_info['default']['prefix']['default'] : $original_connection_info['default']['prefix']; + $test_connection_info = Database::getConnectionInfo('default'); + $test_prefix = is_array($test_connection_info['default']['prefix']) ? $test_connection_info['default']['prefix']['default'] : $test_connection_info['default']['prefix']; + if ($original_prefix != $test_prefix) { + $tables = Database::getConnection()->schema()->findTables($test_prefix . '%'); + $prefix_length = strlen($test_prefix); + foreach ($tables as $table) { + if (Database::getConnection()->schema()->dropTable(substr($table, $prefix_length))) { + unset($tables[$table]); + } + } + } + // Delete test site directory. + file_unmanaged_delete_recursive($this->siteDirectory, array($this, 'filePreDeleteCallback')); + } // Restore original database connection. Database::removeConnection('default'); diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh index 5594dd5..eb45d84 100755 --- a/core/scripts/run-tests.sh +++ b/core/scripts/run-tests.sh @@ -153,6 +153,11 @@ function simpletest_script_help() { --verbose Output detailed assertion messages in addition to summary. + --keep-artifacts + + Retains the test site directory and database tables of all + executed tests. By default, all artifacts are deleted. + --keep-results Keeps detailed assertion results (in the database) after tests @@ -210,6 +215,7 @@ function simpletest_script_parse_args() { 'file' => FALSE, 'color' => FALSE, 'verbose' => FALSE, + 'keep-artifacts' => FALSE, 'keep-results' => FALSE, 'test_names' => array(), 'repeat' => 1, @@ -495,6 +501,7 @@ function simpletest_script_run_one_test($test_id, $test_class) { $container->set('request', $request); $test = new $test_class($test_id); + $test->clearArtifacts = !$args['keep-artifacts']; $test->dieOnFail = (bool) $args['die-on-fail']; $test->verbose = (bool) $args['verbose']; $test->run(); @@ -531,7 +538,7 @@ function simpletest_script_command($test_id, $test_class) { $command .= ' --url ' . escapeshellarg($args['url']); $command .= ' --php ' . escapeshellarg($php); $command .= " --test-id $test_id"; - foreach (array('verbose', 'keep-results', 'color', 'die-on-fail') as $arg) { + foreach (array('verbose', 'keep-results', 'keep-artifacts', 'color', 'die-on-fail') as $arg) { if ($args[$arg]) { $command .= ' --' . $arg; }