From e95fbe727724c83b043fc7817493d84d5de1537d Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 12 Apr 2016 15:19:46 -0700 Subject: [PATCH] CRM_AllTests - Fix hidden GroupTest/TransactionTest interaction (#8115) There's a hidden test interaction where CRM_Contact_BAO_GroupTest causes CRM_Core_TransactionTest to fail, and this seems to involve munging the active DB connection. With this revision, CRM_Contact_BAO_GroupTest uses the same DB connection as everything else. The failure was reproducible using this command: ``` env CIVICRM_UF=UnitTests PHPUNIT_TESTS="CRM_Contact_BAO_GroupTest::testGroupData CRM_Core_TransactionTest::testBasicCommit" phpunit4 tests/phpunit/EnvTests.php ``` --- CRM/Core/DAO.php | 9 +++++++++ CRM/Utils/File.php | 13 ++++++++++--- tests/phpunit/CRM/Contact/BAO/GroupTest.php | 3 +-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index dc987eb6cd..c124c0957d 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -115,6 +115,15 @@ class CRM_Core_DAO extends DB_DataObject { CRM_Core_DAO::executeQuery('SET @uniqueID = %1', array(1 => array(CRM_Utils_Request::id(), 'String'))); } + /** + * @return DB_common + */ + public static function getConnection() { + global $_DB_DATAOBJECT; + $dao = new CRM_Core_DAO(); + return $_DB_DATAOBJECT['CONNECTIONS'][$dao->_database_dsn_md5]; + } + /** * @param string $fieldName * @param $fieldDef diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php index 686c7dcd95..16996e8f24 100644 --- a/CRM/Utils/File.php +++ b/CRM/Utils/File.php @@ -287,16 +287,23 @@ class CRM_Utils_File { } /** - * @param $dsn + * @param string|NULL $dsn + * Use NULL to load the default/active connection from CRM_Core_DAO. + * Otherwise, give a full DSN string. * @param string $fileName * @param null $prefix * @param bool $isQueryString * @param bool $dieOnErrors */ public static function sourceSQLFile($dsn, $fileName, $prefix = NULL, $isQueryString = FALSE, $dieOnErrors = TRUE) { - require_once 'DB.php'; + if ($dsn === NULL) { + $db = CRM_Core_DAO::getConnection(); + } + else { + require_once 'DB.php'; + $db = DB::connect($dsn); + } - $db = DB::connect($dsn); if (PEAR::isError($db)) { die("Cannot open $dsn: " . $db->getMessage()); } diff --git a/tests/phpunit/CRM/Contact/BAO/GroupTest.php b/tests/phpunit/CRM/Contact/BAO/GroupTest.php index 58cd7fb614..2a9c928c8f 100644 --- a/tests/phpunit/CRM/Contact/BAO/GroupTest.php +++ b/tests/phpunit/CRM/Contact/BAO/GroupTest.php @@ -111,9 +111,8 @@ class CRM_Contact_BAO_GroupTest extends CiviUnitTestCase { * Load saved search sql files into the DB. */ public function loadSavedSearches() { - $dsn = CRM_Core_Config::singleton()->dsn; foreach (glob(dirname(__FILE__) . "/SavedSearchDataSets/*.sql") as $file) { - CRM_Utils_File::sourceSQLFile($dsn, $file); + CRM_Utils_File::sourceSQLFile(NULL, $file); } } -- 2.25.1