From 2b045ddda290d41157e35bca77ef5290247c63fa Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 28 Jan 2016 16:27:07 -0800 Subject: [PATCH] civicrm.settings.php.template, ExternalBatch - Read unit test DSN from $_CV In the previous formulation, a dev would need to perform more steps during setup, ie 1. Edit main civicrm.settings.php to add test DSN 2. Run `cv vars:fill` and edit ~/.cv.json This patch makes the first step redundant. This patch also makes it easier to swtich DB's for parallel test execution. --- Civi/API/ExternalBatch.php | 46 +++++++++++-------- .../CRM/common/civicrm.settings.php.template | 6 +-- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/Civi/API/ExternalBatch.php b/Civi/API/ExternalBatch.php index f153795021..1a2230077a 100644 --- a/Civi/API/ExternalBatch.php +++ b/Civi/API/ExternalBatch.php @@ -28,7 +28,7 @@ class ExternalBatch { protected $settingsPath; - protected $env = array(); + protected $env; /** * @var array @@ -51,9 +51,7 @@ class ExternalBatch { $this->root = $civicrm_root; $this->settingsPath = defined('CIVICRM_SETTINGS_PATH') ? CIVICRM_SETTINGS_PATH : NULL; $this->defaultParams = $defaultParams; - $this->addEnv(array( - 'CIVICRM_UF' => CIVICRM_UF, - )); + $this->env = $_ENV; } /** @@ -182,23 +180,35 @@ class ExternalBatch { public function createProcess($apiCall) { $parts = array(); - $executableFinder = new PhpExecutableFinder(); - $php = $executableFinder->find(); - if (!$php) { - throw new \CRM_Core_Exception("Failed to locate PHP interpreter."); + if (defined('CIVICRM_TEST') && CIVICRM_TEST) { + // When testing, civicrm.settings.php may rely on $_CV, which is only + // populated/propagated if we execute through `cv`. + $parts[] = 'cv api'; + $parts[] = escapeshellarg($apiCall['entity'] . '.' . $apiCall['action']); + $parts[] = "--out=json-strict"; + foreach ($apiCall['params'] as $key => $value) { + $parts[] = escapeshellarg("$key=$value"); + } } - $parts[] = $php; - - $parts[] = escapeshellarg($this->root . '/bin/cli.php'); - $parts[] = escapeshellarg("-e=" . $apiCall['entity']); - $parts[] = escapeshellarg("-a=" . $apiCall['action']); - $parts[] = "--json"; - $parts[] = escapeshellarg("-u=dummyuser"); - foreach ($apiCall['params'] as $key => $value) { - $parts[] = escapeshellarg("--$key=$value"); + else { + // But in production, we may not have `cv` installed. + $executableFinder = new PhpExecutableFinder(); + $php = $executableFinder->find(); + if (!$php) { + throw new \CRM_Core_Exception("Failed to locate PHP interpreter."); + } + $parts[] = $php; + $parts[] = escapeshellarg($this->root . '/bin/cli.php'); + $parts[] = escapeshellarg("-e=" . $apiCall['entity']); + $parts[] = escapeshellarg("-a=" . $apiCall['action']); + $parts[] = "--json"; + $parts[] = escapeshellarg("-u=dummyuser"); + foreach ($apiCall['params'] as $key => $value) { + $parts[] = escapeshellarg("--$key=$value"); + } } - $command = implode(" ", $parts); + $command = implode(" ", $parts); $env = array_merge($this->env, array( 'CIVICRM_SETTINGS' => $this->settingsPath, )); diff --git a/templates/CRM/common/civicrm.settings.php.template b/templates/CRM/common/civicrm.settings.php.template index 3da5559f4d..2b2060f170 100644 --- a/templates/CRM/common/civicrm.settings.php.template +++ b/templates/CRM/common/civicrm.settings.php.template @@ -94,11 +94,11 @@ if (!defined('CIVICRM_UF_DSN') && CIVICRM_UF !== 'UnitTests') { * */ if (!defined('CIVICRM_DSN')) { - if (CIVICRM_UF !== 'UnitTests') { - define('CIVICRM_DSN', 'mysql://%%dbUser%%:%%dbPass%%@%%dbHost%%/%%dbName%%?new_link=true'); + if (CIVICRM_UF === 'UnitTests' && isset($GLOBALS['_CV']['TEST_DB_DSN'])) { + define('CIVICRM_DSN', $GLOBALS['_CV']['TEST_DB_DSN']); } else { - define('CIVICRM_DSN', 'mysql://%%testUser%%:%%testPass%%@%%testHost%%/%%testName%%?new_link=true'); + define('CIVICRM_DSN', 'mysql://%%dbUser%%:%%dbPass%%@%%dbHost%%/%%dbName%%?new_link=true'); } } -- 2.25.1