From 2634b437c08002056518c18cbbddb7046b1e1d04 Mon Sep 17 00:00:00 2001
From: webchick <drupal@webchick.net>
Date: Wed, 1 Oct 2014 01:11:18 -0700
Subject: [PATCH] Issue #2343389 by ricardoamaro, Sutharsan: Fixed Drupal core
 testing fails to write results because of SQLITE limitation .

---
 core/scripts/run-tests.sh | 49 ++++++++++++++++++++++++++++++++-------
 1 file changed, 40 insertions(+), 9 deletions(-)

diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh
index de7704d9f19f..fedc6b6fe9b6 100755
--- a/core/scripts/run-tests.sh
+++ b/core/scripts/run-tests.sh
@@ -16,6 +16,7 @@
 const SIMPLETEST_SCRIPT_COLOR_PASS = 32; // Green.
 const SIMPLETEST_SCRIPT_COLOR_FAIL = 31; // Red.
 const SIMPLETEST_SCRIPT_COLOR_EXCEPTION = 33; // Brown.
+const SIMPLETEST_SCRIPT_SQLITE_VARIABLE_LIMIT = 500;
 
 // Set defaults and get overrides.
 list($args, $count) = simpletest_script_parse_args();
@@ -71,7 +72,6 @@
 
 // Try to allocate unlimited time to run the tests.
 drupal_set_time_limit(0);
-
 simpletest_script_reporter_init();
 
 $tests_to_run = array();
@@ -907,10 +907,7 @@ function simpletest_script_reporter_display_summary($class, $results) {
 function simpletest_script_reporter_write_xml_results() {
   global $args, $test_ids, $results_map;
 
-  $results = Database::getConnection('default', 'test-runner')
-    ->query("SELECT * FROM {simpletest} WHERE test_id IN (:test_ids) ORDER BY test_class, message_id", array(
-      ':test_ids' => $test_ids,
-    ));
+  $results = simpletest_script_load_messages_by_test_id($test_ids);
 
   $test_class = '';
   $xml_files = array();
@@ -995,10 +992,7 @@ function simpletest_script_reporter_display_results() {
     echo "Detailed test results\n";
     echo "---------------------\n";
 
-    $results = Database::getConnection('default', 'test-runner')
-      ->query("SELECT * FROM {simpletest} WHERE test_id IN (:test_ids) ORDER BY test_class, message_id", array(
-        ':test_ids' => $test_ids,
-      ));
+    $results = simpletest_script_load_messages_by_test_id($test_ids);
     $test_class = '';
     foreach ($results as $result) {
       if (isset($results_map[$result->status])) {
@@ -1116,3 +1110,40 @@ function simpletest_script_print_alternatives($string, $array, $degree = 4) {
     }
   }
 }
+
+/**
+ * Loads the simpletest messages from the database.
+ *
+ * Messages are ordered by test class and message id.
+ *
+ * @param array $test_ids
+ *   Array of test IDs of the messages to be loaded.
+ *
+ * @return array
+ *   Array of simpletest messages from the database.
+ */
+function simpletest_script_load_messages_by_test_id($test_ids) {
+  global $args;
+  $results = array();
+
+  // Sqlite has a maximum number of variables per query. If required, the
+  // database query is split into chunks.
+  if (count($test_ids) > SIMPLETEST_SCRIPT_SQLITE_VARIABLE_LIMIT && !empty($args['sqlite'])) {
+    $test_id_chunks = array_chunk($test_ids, SIMPLETEST_SCRIPT_SQLITE_VARIABLE_LIMIT);
+  }
+  else {
+    $test_id_chunks = array($test_ids);
+  }
+
+  foreach ($test_id_chunks as $test_id_chunk) {
+    $result_chunk = Database::getConnection('default', 'test-runner')
+      ->query("SELECT * FROM {simpletest} WHERE test_id IN (:test_ids) ORDER BY test_class, message_id", array(
+        ':test_ids' => $test_id_chunk,
+      ))->fetchAll();
+    if ($result_chunk) {
+      $results = array_merge($results, $result_chunk);
+    }
+  }
+
+  return $results;
+}
-- 
GitLab