From: Tim Otten Date: Tue, 12 Apr 2016 22:19:46 +0000 (-0700) Subject: CRM_AllTests - Fix hidden GroupTest/TransactionTest interaction (#8115) X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=e95fbe727724c83b043fc7817493d84d5de1537d;p=civicrm-core.git 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 ``` --- 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); } }