CRM-19784: Fixed test by creating a method for disabling the geocoder.
authorFrank J. Gómez <frank@ginkgostreet.com>
Fri, 2 Feb 2018 21:45:41 +0000 (16:45 -0500)
committereileen <emcnaughton@wikimedia.org>
Mon, 5 Feb 2018 20:36:45 +0000 (09:36 +1300)
CRM/Contact/Import/Parser/Contact.php
CRM/Utils/GeocodeProvider.php
tests/phpunit/CRM/Utils/GeocodeTest.php

index a6925e439aec30ddbb59f4ae95c8adefd2a3ae6e..3468592260e1850701487d9e1bdd5ea20f706bfe 100644 (file)
@@ -474,7 +474,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
     $this->_unparsedStreetAddressContacts = array();
     if (!$doGeocodeAddress) {
       // CRM-5854, reset the geocode method to null to prevent geocoding
-      $config->geocodeMethod = '';
+      CRM_Utils_GeocodeProvider::disableForSession();
     }
 
     // first make sure this is a valid line
index 172814aceb5bd7b88d8380b585ac889332083754..5aa5a418a1febf835abdc1ae2245aa07a6cb05e4 100644 (file)
  */
 class CRM_Utils_GeocodeProvider {
 
+  /**
+   * Caches the provider class name. Disables geocoding when set to FALSE.
+   *
+   * @var string|bool
+   */
+  private static $providerClassName;
+
   /**
    * Instantiate a geocode object of the system-configured type.
    *
@@ -58,24 +65,51 @@ class CRM_Utils_GeocodeProvider {
    *   Class name if usable, else false.
    */
   public static function getUsableClassName() {
-    $provider = Civi::settings()->get('geoProvider');
-    if (!class_exists($provider)) {
-      if (strlen($provider)) {
-        Civi::log()->error('Configured geocoder has been removed from the system', ['geocode_class' => $provider]);
+    if (self::$providerClassName === NULL) {
+      $provider = Civi::settings()->get('geoProvider');
+      if (!class_exists($provider)) {
+        if (class_exists('CRM_Utils_Geocode_' . $provider)) {
+          $provider = 'CRM_Utils_Geocode_' . $provider;
+        }
+        else {
+          if (strlen($provider)) {
+            Civi::log()
+              ->error('Configured geocoder has been removed from the system', ['geocode_class' => $provider]);
+          }
+          $provider = FALSE;
+        }
+      }
+
+      // Ideally geocoding providers would be required to implement an interface
+      // or extend a base class. While we identify and implement a geocoding
+      // abstraction library (rather than continue to roll our own), we settle for
+      // this check.
+      if (!method_exists($provider, 'format')) {
+        Civi::log()->error('Configured geocoder is invalid, must provide a format method', ['geocode_class' => $provider]);
+        $provider = FALSE;
       }
-      return FALSE;
-    }
 
-    // Ideally geocoding providers would be required to implement an interface
-    // or extend a base class. While we identify and implement a geocoding
-    // abstraction library (rather than continue to roll our own), we settle for
-    // this check.
-    if (!method_exists($provider, 'format')) {
-      Civi::log()->error('Configured geocoder is invalid, must provide a format method', ['geocode_class' => $provider]);
-      return FALSE;
+      self::$providerClassName = $provider;
     }
 
-    return $provider;
+    return self::$providerClassName;
+  }
+
+  /**
+   * Disable GeoProvider within a session.
+   *
+   * This disables geocoding by causing getUsableClassName() to bail out.
+   */
+  public static function disableForSession() {
+    self::$providerClassName = FALSE;
+  }
+
+  /**
+   * Reset geoprovider (after it has been disabled).
+   */
+  public static function reset() {
+    self::$providerClassName = NULL;
+    self::getUsableClassName();
   }
 
 }
index 56e348e560cb1ab2dc65d2d22be3bc2e39c516cc..b5361b0eb6360bf60cf7ebad0a99af82b7371906 100644 (file)
@@ -30,9 +30,7 @@ class CRM_Utils_GeocodeTest extends CiviUnitTestCase {
       'geoProvider' => "Google",
     ));
 
-    // Set geocodeMethod to empty.
-    $config = CRM_Core_Config::singleton();
-    $config->geocodeMethod = '';
+    CRM_Utils_GeocodeProvider::disableForSession();
 
     // Save a contact with geo coding disabled.
     $params = array(
@@ -53,8 +51,7 @@ class CRM_Utils_GeocodeTest extends CiviUnitTestCase {
     $this->assertArrayNotHasKey('geo_code_1', $address_values, 'No geocoding when geocodeMethod is empty');
 
     // Run the geocode job on that specific contact
-    $config->geocodeMethod = 'CRM_Utils_Geocode_Google';
-
+    CRM_Utils_GeocodeProvider::reset();
     try {
       $params_geocode = array(
         'start' => $contact_values['id'],