From 94d2b28e89cce77a01214cdfb0a51d29e6613c2b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Frank=20J=2E=20G=C3=B3mez?= Date: Fri, 2 Feb 2018 16:45:41 -0500 Subject: [PATCH] CRM-19784: Fixed test by creating a method for disabling the geocoder. --- CRM/Contact/Import/Parser/Contact.php | 2 +- CRM/Utils/GeocodeProvider.php | 62 +++++++++++++++++++------ tests/phpunit/CRM/Utils/GeocodeTest.php | 7 +-- 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index a6925e439a..3468592260 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -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 diff --git a/CRM/Utils/GeocodeProvider.php b/CRM/Utils/GeocodeProvider.php index 172814aceb..5aa5a418a1 100644 --- a/CRM/Utils/GeocodeProvider.php +++ b/CRM/Utils/GeocodeProvider.php @@ -33,6 +33,13 @@ */ 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(); } } diff --git a/tests/phpunit/CRM/Utils/GeocodeTest.php b/tests/phpunit/CRM/Utils/GeocodeTest.php index 56e348e560..b5361b0eb6 100644 --- a/tests/phpunit/CRM/Utils/GeocodeTest.php +++ b/tests/phpunit/CRM/Utils/GeocodeTest.php @@ -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'], -- 2.25.1