diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module index dbd2a9aafac296a34cadebe0abd925217c6fde76..ed3641239c807224aa9906ee95eb61c8babce359 100644 --- a/core/modules/simpletest/simpletest.module +++ b/core/modules/simpletest/simpletest.module @@ -11,7 +11,6 @@ use Drupal\Core\File\Exception\FileException; use Drupal\Core\Render\Element; use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\simpletest\TestBase; use Drupal\Core\Test\TestDatabase; use Drupal\simpletest\TestDiscovery; use Drupal\Tests\Listeners\SimpletestUiPrinter; @@ -552,11 +551,11 @@ function simpletest_log_read($test_id, $database_prefix, $test_class) { 'line' => $match[4], 'file' => $match[3], ]; - TestBase::insertAssert($test_id, $test_class, FALSE, $match[2], $match[1], $caller); + simpletest_insert_assert($test_id, $test_class, FALSE, $match[2], $match[1], $caller); } else { // Unknown format, place the entire message in the log. - TestBase::insertAssert($test_id, $test_class, FALSE, $line, 'Fatal error'); + simpletest_insert_assert($test_id, $test_class, FALSE, $line, 'Fatal error'); } $found = TRUE; } @@ -564,6 +563,62 @@ function simpletest_log_read($test_id, $database_prefix, $test_class) { return $found; } +/** + * Store an assertion from outside the testing context. + * + * This is useful for inserting assertions that can only be recorded after + * the test case has been destroyed, such as PHP fatal errors. The caller + * information is not automatically gathered since the caller is most likely + * inserting the assertion on behalf of other code. In all other respects + * the method behaves just like \Drupal\simpletest\TestBase::assert() in terms + * of storing the assertion. + * + * @param string $test_id + * The test ID to which the assertion relates. + * @param string $test_class + * The test class to store an assertion for. + * @param bool|string $status + * A boolean or a string of 'pass' or 'fail'. TRUE means 'pass'. + * @param string $message + * The assertion message. + * @param string $group + * The assertion message group. + * @param array $caller + * The an array containing the keys 'file' and 'line' that represent the file + * and line number of that file that is responsible for the assertion. + * + * @return + * Message ID of the stored assertion. + */ +function simpletest_insert_assert($test_id, $test_class, $status, $message = '', $group = 'Other', array $caller = []) { + // Convert boolean status to string status. + if (is_bool($status)) { + $status = $status ? 'pass' : 'fail'; + } + + $caller += [ + 'function' => 'Unknown', + 'line' => 0, + 'file' => 'Unknown', + ]; + + $assertion = [ + 'test_id' => $test_id, + 'test_class' => $test_class, + 'status' => $status, + 'message' => $message, + 'message_group' => $group, + 'function' => $caller['function'], + 'line' => $caller['line'], + 'file' => $caller['file'], + ]; + + return TestDatabase::getConnection() + ->insert('simpletest') + ->fields($assertion) + ->execute(); +} + /** * Gets a list of all of the tests provided by the system. * diff --git a/core/modules/simpletest/src/TestBase.php b/core/modules/simpletest/src/TestBase.php index ef886c07688b429d7d7f2a6e536543a1684f43b7..4fce148cf1c40171b843ef59e8feb13ce1836b4c 100644 --- a/core/modules/simpletest/src/TestBase.php +++ b/core/modules/simpletest/src/TestBase.php @@ -384,6 +384,10 @@ protected function assert($status, $message = '', $group = 'Other', array $calle * @return * Message ID of the stored assertion. * + * @deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use + * simpletest_insert_assert() instead. + * + * @see https://www.drupal.org/node/3030340 * @see \Drupal\simpletest\TestBase::assert() * @see \Drupal\simpletest\TestBase::deleteAssert() */ diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh index 41495c779eb47d74a1c7e8252e837cefb57cb06a..f08956e61fc44a478b72908330a1754e5c2c53ac 100755 --- a/core/scripts/run-tests.sh +++ b/core/scripts/run-tests.sh @@ -747,7 +747,7 @@ function simpletest_script_execute_batch($test_classes) { // @see https://www.drupal.org/node/2780087 $total_status = max(SIMPLETEST_SCRIPT_EXIT_FAILURE, $total_status); // Insert a fail for xml results. - TestBase::insertAssert($child['test_id'], $child['class'], FALSE, $message, 'run-tests.sh check'); + simpletest_insert_assert($child['test_id'], $child['class'], FALSE, $message, 'run-tests.sh check'); // Ensure that an error line is displayed for the class. simpletest_script_reporter_display_summary( $child['class'], @@ -950,7 +950,7 @@ function simpletest_script_cleanup($test_id, $test_class, $exitcode) { // would also delete file directories of other tests that are potentially // running concurrently. try { - \Drupal::service('file_system')->deleteRecursive($test_directory, ['Drupal\simpletest\TestBase', 'filePreDeleteCallback']); + \Drupal::service('file_system')->deleteRecursive($test_directory, ['\Drupal\Tests\BrowserTestBase', 'filePreDeleteCallback']); $messages[] = "- Removed test site directory."; } catch (FileException $e) {