3 * Class CRM_Utils_GeocodeTest
6 class CRM_Utils_GeocodeTest
extends CiviUnitTestCase
{
8 public function setUp() {
12 public function tearDown() {
16 public function testStateProvinceFormat() {
17 $params = array('state_province_id' => 1022, 'country' => 'U.S.A');
18 $formatted = CRM_Utils_Geocode_Google
::format($params);
19 if (isset($params['geo_code_error']) && $params['geo_code_error'] == 'OVER_QUERY_LIMIT') {
20 $this->markTestIncomplete('geo_code_error: OVER_QUERY_LIMIT');
22 $this->assertTrue($formatted);
23 $this->assertApproxEquals('46.72', $params['geo_code_1'], 1);
24 $this->assertApproxEquals('-94.68', $params['geo_code_2'], 1);
27 public function testGeocodeMethodOff() {
28 // Set a geocoding provider.
29 $result = civicrm_api3('Setting', 'create', array(
30 'geoProvider' => "Google",
33 // Set geocodeMethod to empty.
34 $config = CRM_Core_Config
::singleton();
35 $config->geocodeMethod
= '';
37 // Save a contact with geo coding disabled.
39 'first_name' => 'Abraham',
40 'last_name' => 'Lincoln',
41 'contact_type' => 'Individual',
42 'api.Address.create' => array(
43 'street_address' => '1600 Pennsylvania Avenue',
44 'city' => 'Washington',
45 'state_province' => 'DC',
46 'location_type_id' => 1,
49 $result = civicrm_api3('Contact', 'create', $params);
50 $contact_values = array_pop($result['values']);
51 $address_values = array_pop($contact_values['api.Address.create']['values']);
53 $this->assertArrayNotHasKey('geo_code_1', $address_values, 'No geocoding when geocodeMethod is empty');
55 // Run the geocode job on that specific contact
56 $config->geocodeMethod
= 'CRM_Utils_Geocode_Google';
59 $params_geocode = array(
60 'start' => $contact_values['id'],
61 'end' => $contact_values['id'] +
1,
65 $result_geocode = civicrm_api3('Job', 'geocode', $params_geocode);
67 catch (CiviCRM_API3_Exception
$e) {
68 if ($e->getMessage() == 'A fatal error was triggered: Aborting batch geocoding. Hit the over query limit on geocoder.') {
69 $this->markTestIncomplete('Job.geocode error_message: A fatal error was triggered: Aborting batch geocoding. Hit the over query limit on geocoder.');
75 $params_address_getsingle = array(
76 'contact_id' => $contact_values['id'],
78 $result_address_getsingle = civicrm_api3('Address', 'getsingle', $params_address_getsingle);
80 // We should get a geo code setting.
81 $this->assertApproxEquals('38.89', CRM_Utils_Array
::value('geo_code_1', $result_address_getsingle), 1);