From 6a2d67db4768be23d0198b925eb835448043355f Mon Sep 17 00:00:00 2001
From: Lee Rowlands <lee.rowlands@previousnext.com.au>
Date: Tue, 10 Sep 2019 06:40:24 +1000
Subject: [PATCH] Issue #3079988 by Mile23, mondrake: Replace, deprecate
 simpletest_process_phpunit_results()

---
 core/lib/Drupal/Core/Test/TestDatabase.php    | 23 +++++++++++++++++++
 core/modules/simpletest/simpletest.module     | 20 +++++++---------
 .../src/Kernel/SimpletestDeprecationTest.php  |  9 ++++++++
 core/scripts/run-tests.sh                     |  2 +-
 4 files changed, 41 insertions(+), 13 deletions(-)

diff --git a/core/lib/Drupal/Core/Test/TestDatabase.php b/core/lib/Drupal/Core/Test/TestDatabase.php
index 03ff3d2f6268..70c5c084fe10 100644
--- a/core/lib/Drupal/Core/Test/TestDatabase.php
+++ b/core/lib/Drupal/Core/Test/TestDatabase.php
@@ -406,4 +406,27 @@ public static function testingSchema() {
     return $schema;
   }
 
+  /**
+   * Inserts the parsed PHPUnit results into {simpletest}.
+   *
+   * @param array[] $phpunit_results
+   *   An array of test results, as returned from
+   *   \Drupal\Core\Test\JUnitConverter::xmlToRows(). These results are in a
+   *   form suitable for inserting into the {simpletest} table of the test
+   *   results database.
+   *
+   * @internal
+   */
+  public static function processPhpUnitResults($phpunit_results) {
+    if ($phpunit_results) {
+      $query = static::getConnection()
+        ->insert('simpletest')
+        ->fields(array_keys($phpunit_results[0]));
+      foreach ($phpunit_results as $result) {
+        $query->values($result);
+      }
+      $query->execute();
+    }
+  }
+
 }
diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module
index 8dc4ecea08c5..07a2588602cc 100644
--- a/core/modules/simpletest/simpletest.module
+++ b/core/modules/simpletest/simpletest.module
@@ -204,19 +204,15 @@ function simpletest_run_phpunit_tests($test_id, array $unescaped_test_classnames
  *
  * @param array[] $phpunit_results
  *   An array of test results returned from simpletest_phpunit_xml_to_rows().
+ *
+ * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
+ *   \Drupal\Core\Test\TestDatabase::processPhpUnitResults() instead.
+ *
+ * @see https://www.drupal.org/node/3075252
  */
 function simpletest_process_phpunit_results($phpunit_results) {
-  // Insert the results of the PHPUnit test run into the database so the results
-  // are displayed along with Simpletest's results.
-  if (!empty($phpunit_results)) {
-    $query = TestDatabase::getConnection()
-      ->insert('simpletest')
-      ->fields(array_keys($phpunit_results[0]));
-    foreach ($phpunit_results as $result) {
-      $query->values($result);
-    }
-    $query->execute();
-  }
+  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Test\TestDatabase::processPhpUnitResults() instead. See https://www.drupal.org/node/3075252', E_USER_DEPRECATED);
+  TestDatabase::processPhpUnitResults($phpunit_results);
 }
 
 /**
@@ -345,7 +341,7 @@ function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
   if (is_subclass_of($test_class, TestCase::class)) {
     $runner = PhpUnitTestRunner::create(\Drupal::getContainer());
     $phpunit_results = $runner->runTests($test_id, [$test_class]);
-    simpletest_process_phpunit_results($phpunit_results);
+    TestDatabase::processPhpUnitResults($phpunit_results);
     $test_results[$test_class] = simpletest_summarize_phpunit_result($phpunit_results)[$test_class];
   }
   else {
diff --git a/core/modules/simpletest/tests/src/Kernel/SimpletestDeprecationTest.php b/core/modules/simpletest/tests/src/Kernel/SimpletestDeprecationTest.php
index b1c1a8f79c44..4ef09a903357 100644
--- a/core/modules/simpletest/tests/src/Kernel/SimpletestDeprecationTest.php
+++ b/core/modules/simpletest/tests/src/Kernel/SimpletestDeprecationTest.php
@@ -92,4 +92,13 @@ public function testDeprecatedSimpletestGenerateFile() {
     $this->assertTrue(unlink($public_file));
   }
 
+  /**
+   * @expectedDeprecation simpletest_process_phpunit_results() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Test\TestDatabase::processPhpUnitResults() instead. See https://www.drupal.org/node/3075252
+   */
+  public function testProcessPhpUnitResults() {
+    // The only safe way to test this deprecation is to call it with an empty
+    // result set. This should not touch the results database.
+    $this->assertNull(simpletest_process_phpunit_results([]));
+  }
+
 }
diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh
index f1cb4c0ce455..c4f910e2024d 100755
--- a/core/scripts/run-tests.sh
+++ b/core/scripts/run-tests.sh
@@ -782,7 +782,7 @@ function simpletest_script_run_phpunit($test_id, $class) {
 
   $runner = PhpUnitTestRunner::create(\Drupal::getContainer());
   $results = $runner->runTests($test_id, [$class], $status);
-  simpletest_process_phpunit_results($results);
+  TestDatabase::processPhpUnitResults($results);
 
   $summaries = $runner->summarizeResults($results);
   foreach ($summaries as $class => $summary) {
-- 
GitLab