Verified Commit 5c326c9f authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3075648 by mondrake: Refactor TestDatabase::lastTestGet to use dynamic queries

parent 4936a172
Loading
Loading
Loading
Loading
+18 −13
Changes for core/lib/Drupal/Core/Test/TestDatabase.php: 18 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -244,24 +244,29 @@ public static function insertAssert($test_id, $test_class, $status, $message = '
   *   The test ID to get the last test from.
   *
   * @return array
   *   Array containing the last database prefix used and the last test class
   *   that ran.
   *   Associative array containing the last database prefix used and the
   *   last test class that ran.
   *
   * @internal
   */
  public static function lastTestGet($test_id) {
    $connection = static::getConnection();
    $last_prefix = $connection
      ->queryRange('SELECT last_prefix FROM {simpletest_test_id} WHERE test_id = :test_id', 0, 1, [
        ':test_id' => $test_id,
      ])
      ->fetchField();
    $last_test_class = $connection
      ->queryRange('SELECT test_class FROM {simpletest} WHERE test_id = :test_id ORDER BY message_id DESC', 0, 1, [
        ':test_id' => $test_id,
      ])
      ->fetchField();
    return [$last_prefix, $last_test_class];

    // Define a subquery to identify the latest 'message_id' given the
    // $test_id.
    $max_message_id_subquery = $connection
      ->select('simpletest', 'sub')
      ->condition('test_id', $test_id);
    $max_message_id_subquery->addExpression('MAX(message_id)', 'max_message_id');

    // Run a select query to return 'last_prefix' from {simpletest_test_id} and
    // 'test_class' from {simpletest}.
    $select = $connection->select($max_message_id_subquery, 'st_sub');
    $select->join('simpletest', 'st', 'st.message_id = st_sub.max_message_id');
    $select->join('simpletest_test_id', 'sttid', 'st.test_id = sttid.test_id');
    $select->addField('sttid', 'last_prefix');
    $select->addField('st', 'test_class');
    return $select->execute()->fetchAssoc();
  }

  /**
+3 −3
Changes for core/modules/simpletest/simpletest.module: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -407,8 +407,8 @@ function _simpletest_batch_finished($success, $results, $operations, $elapsed) {
    // Retrieve the last database prefix used for testing and the last test
    // class that was run from. Use the information to read the lgo file
    // in case any fatal errors caused the test to crash.
    list($last_prefix, $last_test_class) = TestDatabase::lastTestGet($test_id);
    (new TestDatabase($last_prefix))->logRead($test_id, $last_test_class);
    $last_test = TestDatabase::lastTestGet($test_id);
    (new TestDatabase($last_test['last_prefix']))->logRead($test_id, $last_test['test_class']);

    \Drupal::messenger()->addError(t('The test run did not successfully finish.'));
    \Drupal::messenger()->addWarning(t('Use the <em>Clean environment</em> button to clean-up temporary files and tables.'));
@@ -432,7 +432,7 @@ function _simpletest_batch_finished($success, $results, $operations, $elapsed) {
 */
function simpletest_last_test_get($test_id) {
  @trigger_error(__FUNCTION__ . ' is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Test\TestDatabase::lastTestGet() instead. See https://www.drupal.org/node/3075252', E_USER_DEPRECATED);
  return TestDatabase::lastTestGet($test_id);
  return array_values(TestDatabase::lastTestGet($test_id));
}

/**
+4 −3
Changes for core/scripts/run-tests.sh: 4 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -749,7 +749,7 @@ function simpletest_script_execute_batch($test_classes) {
            ['#pass' => 0, '#fail' => 1, '#exception' => 0, '#debug' => 0]
          );
          if ($args['die-on-fail']) {
            list($db_prefix) = TestDatabase::lastTestGet($child['test_id']);
            $db_prefix = TestDatabase::lastTestGet($child['test_id'])['last_prefix'];
            $test_db = new TestDatabase($db_prefix);
            $test_directory = $test_db->getTestSitePath();
            echo 'Simpletest database and files kept and test exited immediately on fail so should be reproducible if you change settings.php to use the database prefix ' . $db_prefix . ' and config directories in ' . $test_directory . "\n";
@@ -899,7 +899,8 @@ function simpletest_script_cleanup($test_id, $test_class, $exitcode) {
  }
  // Retrieve the last database prefix used for testing.
  try {
    list($db_prefix) = TestDatabase::lastTestGet($test_id);
    $last_test = TestDatabase::lastTestGet($test_id);
    $db_prefix = $last_test['last_prefix'];
  }
  catch (Exception $e) {
    echo (string) $e;
@@ -920,7 +921,7 @@ function simpletest_script_cleanup($test_id, $test_class, $exitcode) {

  // Read the log file in case any fatal errors caused the test to crash.
  try {
    (new TestDatabase($db_prefix))->logRead($test_id, $last_test_class);
    (new TestDatabase($db_prefix))->logRead($test_id, $last_test['test_class']);
  }
  catch (Exception $e) {
    echo (string) $e;