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 CRM_Utils_GeocodeProvider
::disableForSession();
35 // Save a contact with geo coding disabled.
37 'first_name' => 'Abraham',
38 'last_name' => 'Lincoln',
39 'contact_type' => 'Individual',
40 'api.Address.create' => array(
41 'street_address' => '1600 Pennsylvania Avenue',
42 'city' => 'Washington',
43 'state_province' => 'DC',
44 'location_type_id' => 1,
47 $result = civicrm_api3('Contact', 'create', $params);
48 $contact_values = array_pop($result['values']);
49 $address_values = array_pop($contact_values['api.Address.create']['values']);
51 $this->assertArrayNotHasKey('geo_code_1', $address_values, 'No geocoding when geocodeMethod is empty');
53 // Run the geocode job on that specific contact
54 CRM_Utils_GeocodeProvider
::reset();
56 $params_geocode = array(
57 'start' => $contact_values['id'],
58 'end' => $contact_values['id'] +
1,
62 $result_geocode = civicrm_api3('Job', 'geocode', $params_geocode);
64 catch (CiviCRM_API3_Exception
$e) {
65 if ($e->getMessage() == 'Aborting batch geocoding. Hit the over query limit on geocoder.') {
66 $this->markTestIncomplete('Job.geocode error_message: A fatal error was triggered: Aborting batch geocoding. Hit the over query limit on geocoder.');
72 $params_address_getsingle = array(
73 'contact_id' => $contact_values['id'],
75 $result_address_getsingle = civicrm_api3('Address', 'getsingle', $params_address_getsingle);
77 // We should get a geo code setting.
78 $this->assertApproxEquals('38.89', CRM_Utils_Array
::value('geo_code_1', $result_address_getsingle), 1);