From: Seamus Lee Date: Tue, 22 Mar 2022 23:19:35 +0000 (+1100) Subject: dev/core#3132 Ensure that contacts are not placed in null island if geocoders return... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=d3d8fc4caac6d23ad2589294088236c60e95c19a;p=civicrm-core.git dev/core#3132 Ensure that contacts are not placed in null island if geocoders return 'null' string for geocodes --- diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index 09e45a24c0..aa23c3d0fe 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -1313,7 +1313,10 @@ SELECT is_primary, // core#2379 - Limit geocode length to 14 characters to avoid validation error on save in UI. foreach (['geo_code_1', 'geo_code_2'] as $geocode) { if ($params[$geocode] ?? FALSE) { - $params[$geocode] = (float) substr($params[$geocode], 0, 14); + // ensure that if the geocoding provider (Google, OSM etc) has returned the string 'null' because they can't geocode, ensure that contacts are not placed on null island 0,0 + if ($params[$geocode] !== 'null') { + $params[$geocode] = (float) substr($params[$geocode], 0, 14); + } } } return $providerExists; diff --git a/tests/phpunit/CRM/Utils/Geocode/TestProvider.php b/tests/phpunit/CRM/Utils/Geocode/TestProvider.php new file mode 100644 index 0000000000..fca9ec36fb --- /dev/null +++ b/tests/phpunit/CRM/Utils/Geocode/TestProvider.php @@ -0,0 +1,11 @@ +callAPISuccess('Address', 'create', ['id' => $secondAddress['id'], 'contact_id' => $contactIdB, 'master_id' => $firstAddress['id']]); } + /** + * Ensure that when geocoding fails and geocoders return the string 'null' that it is not translated into int 0 for geo_code_1 and geo_code_2 which would place the contact on null island (0,0) + */ + public function testGeocodingAddress(): void { + $this->callAPISuccess('Setting', 'create', ['geoProvider' => 'TestProvider']); + $cid = $this->individualCreate(); + $r = $this->callAPISuccess('Address', 'create', [ + 'contact_id' => $cid, + 'location_type_id' => 1, + // Trigger geocoding to return 'null's for geo_code_1 and geo_code_2 + 'street_address' => 'Does not exist', + 'city' => 'Hereville', + //'US', + 'country_id' => 'US', + // 'California', + 'state_province_id' => 'California', + 'postal_code' => '94100', + ]); + $createdAddress = $this->callAPISuccess('Address', 'get', ['id' => $r['id']])['values'][$r['id']]; + // If we have stored NULL values, then geo_code_1 should not be returned. + $this->assertFalse(isset($createdAddress['geo_code_1'])); + } + }