From 90d6f05facedd14e6abf39b86bddff5ae1320eed Mon Sep 17 00:00:00 2001 From: David Rothstein Date: Sun, 2 Nov 2014 12:51:07 -0500 Subject: Issue #1679570 by mgifford, lucascaro, sun: Fixed TestBase does not always use the correct database connection for handling assertions. --- modules/simpletest/drupal_web_test_case.php | 35 ++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'modules/simpletest') diff --git a/modules/simpletest/drupal_web_test_case.php b/modules/simpletest/drupal_web_test_case.php index 2a5aae8a9..39dacfa1d 100644 --- a/modules/simpletest/drupal_web_test_case.php +++ b/modules/simpletest/drupal_web_test_case.php @@ -143,15 +143,7 @@ abstract class DrupalTestCase { ); // Store assertion for display after the test has completed. - try { - $connection = Database::getConnection('default', 'simpletest_original_default'); - } - catch (DatabaseConnectionNotDefinedException $e) { - // If the test was not set up, the simpletest_original_default - // connection does not exist. - $connection = Database::getConnection('default', 'default'); - } - $connection + self::getDatabaseConnection() ->insert('simpletest') ->fields($assertion) ->execute(); @@ -166,6 +158,25 @@ abstract class DrupalTestCase { } } + /** + * Returns the database connection to the site running Simpletest. + * + * @return DatabaseConnection + * The database connection to use for inserting assertions. + */ + public static function getDatabaseConnection() { + try { + $connection = Database::getConnection('default', 'simpletest_original_default'); + } + catch (DatabaseConnectionNotDefinedException $e) { + // If the test was not set up, the simpletest_original_default + // connection does not exist. + $connection = Database::getConnection('default', 'default'); + } + + return $connection; + } + /** * Store an assertion from outside the testing context. * @@ -205,7 +216,8 @@ abstract class DrupalTestCase { 'file' => $caller['file'], ); - return db_insert('simpletest') + return self::getDatabaseConnection() + ->insert('simpletest') ->fields($assertion) ->execute(); } @@ -221,7 +233,8 @@ abstract class DrupalTestCase { * @see DrupalTestCase::insertAssert() */ public static function deleteAssert($message_id) { - return (bool) db_delete('simpletest') + return (bool) self::getDatabaseConnection() + ->delete('simpletest') ->condition('message_id', $message_id) ->execute(); } -- cgit v1.2.3