CRM-16387 - bin/cli.php - Allow calls against the test database
authorTim Otten <totten@civicrm.org>
Mon, 11 May 2015 20:44:10 +0000 (13:44 -0700)
committerTim Otten <totten@civicrm.org>
Mon, 15 Jun 2015 17:34:02 +0000 (10:34 -0700)
Example:

```bash
env CIVICRM_SETTINGS=/path/to/civicrm/tests/phpunit/CiviTest/civicrm.settings.cli.php \
  php bin/cli.php -e Contact -a getfields --output
```

This will assist in creating tests for multi-process cron behavior.

CRM/Utils/System.php
CRM/Utils/System/UnitTests.php
bin/cli.class.php
tests/phpunit/CiviTest/civicrm.settings.cli.php [new file with mode: 0644]

index 112a9e1a0a905ecf8d0f0899adcdc385776d619d..2c64ef560bd39b3535412a6b64cf0678ce3dd395 100644 (file)
@@ -662,11 +662,17 @@ class CRM_Utils_System {
      * We typically call authenticate only when we need to bootstrap the CMS
      * directly via Civi and hence bypass the normal CMS auth and bootstrap
      * process typically done in CLI and cron scripts. See: CRM-12648
+     *
+     * Q: Can we move this to the userSystem class so that it can be tuned
+     * per-CMS? For example, when dealing with UnitTests UF, there's no
+     * userFrameworkDSN.
      */
     $session = CRM_Core_Session::singleton();
     $session->set('civicrmInitSession', TRUE);
 
-    $dbDrupal = DB::connect($config->userFrameworkDSN);
+    if ($config->userFrameworkDSN) {
+      $dbDrupal = DB::connect($config->userFrameworkDSN);
+    }
     return $config->userSystem->authenticate($name, $password, $loadCMSBootstrap, $realPath);
   }
 
index d57901a28ee5f526c3fd527df4d65addb22370c1..f2014b027f971beb65634ec53efee7ec597a1059 100644 (file)
@@ -52,6 +52,20 @@ class CRM_Utils_System_UnitTests extends CRM_Utils_System_Base {
     return $retVal;
   }
 
+  /**
+   * Bootstrap the phony CMS.
+   *
+   * @param string $name
+   *   Optional username for login.
+   * @param string $pass
+   *   Optional password for login.
+   *
+   * @return bool
+   */
+  public function loadBootStrap($name = NULL, $pass = NULL) {
+    return TRUE;
+  }
+
   /**
    * @inheritDoc
    */
index be58c1c654c72e3a5427748ef4c4505612c9578a..510d15e0c2774f827b148ba47b19ba6b8d2a7da1 100644 (file)
@@ -216,7 +216,12 @@ class civicrm_cli {
 
     $civicrm_root = dirname(__DIR__);
     chdir($civicrm_root);
-    require_once 'civicrm.config.php';
+    if (getenv('CIVICRM_SETTINGS')) {
+      require_once getenv('CIVICRM_SETTINGS');
+    }
+    else {
+      require_once 'civicrm.config.php';
+    }
     // autoload
     if (!class_exists('CRM_Core_ClassLoader')) {
       require_once $civicrm_root . '/CRM/Core/ClassLoader.php';
diff --git a/tests/phpunit/CiviTest/civicrm.settings.cli.php b/tests/phpunit/CiviTest/civicrm.settings.cli.php
new file mode 100644 (file)
index 0000000..c9ea113
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+// This file is loaded when spawning separate CLI processes that need to
+// talk to the test DB.
+
+require_once dirname(dirname(dirname(__DIR__))) . '/CRM/Core/ClassLoader.php';
+CRM_Core_ClassLoader::singleton()->register();
+
+define('CIVICRM_SETTINGS_PATH', __DIR__ . '/civicrm.settings.dist.php');
+define('CIVICRM_SETTINGS_LOCAL_PATH', __DIR__ . '/civicrm.settings.local.php');
+
+if (file_exists(CIVICRM_SETTINGS_LOCAL_PATH)) {
+  require_once CIVICRM_SETTINGS_LOCAL_PATH;
+}
+require_once CIVICRM_SETTINGS_PATH;