GeocodeTest.php 2.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * Class CRM_Utils_GeocodeTest
 * @group headless
 */
class CRM_Utils_GeocodeTest extends CiviUnitTestCase {

  public function setUp() {
    parent::setUp();
  }

Seamus Lee's avatar
Seamus Lee committed
12 13
  public function tearDown() {
    parent::tearDown();
14 15 16 17 18
  }

  public function testStateProvinceFormat() {
    $params = array('state_province_id' => 1022, 'country' => 'U.S.A');
    $formatted = CRM_Utils_Geocode_Google::format($params);
19 20 21
    if (isset($params['geo_code_error']) && $params['geo_code_error'] == 'OVER_QUERY_LIMIT') {
      $this->markTestIncomplete('geo_code_error: OVER_QUERY_LIMIT');
    }
22
    $this->assertTrue($formatted);
23 24
    $this->assertApproxEquals('46.72', $params['geo_code_1'], 1);
    $this->assertApproxEquals('-94.68', $params['geo_code_2'], 1);
25 26
  }

27 28 29 30 31 32
  public function testGeocodeMethodOff() {
    // Set a geocoding provider.
    $result = civicrm_api3('Setting', 'create', array(
      'geoProvider' => "Google",
    ));

33
    CRM_Utils_GeocodeProvider::disableForSession();
34 35

    // Save a contact with geo coding disabled.
36 37 38 39 40 41 42 43
    $params = array(
      'first_name' => 'Abraham',
      'last_name' => 'Lincoln',
      'contact_type' => 'Individual',
      'api.Address.create' => array(
        'street_address' => '1600 Pennsylvania Avenue',
        'city' => 'Washington',
        'state_province' => 'DC',
Jamie McClelland's avatar
Jamie McClelland committed
44 45
        'location_type_id' => 1,
      ),
46 47 48 49 50
    );
    $result = civicrm_api3('Contact', 'create', $params);
    $contact_values = array_pop($result['values']);
    $address_values = array_pop($contact_values['api.Address.create']['values']);

51 52 53
    $this->assertArrayNotHasKey('geo_code_1', $address_values, 'No geocoding when geocodeMethod is empty');

    // Run the geocode job on that specific contact
54
    CRM_Utils_GeocodeProvider::reset();
55 56 57 58 59 60 61 62 63 64
    try {
      $params_geocode = array(
        'start' => $contact_values['id'],
        'end' => $contact_values['id'] + 1,
        'geocoding' => 1,
        'parse' => 0,
      );
      $result_geocode = civicrm_api3('Job', 'geocode', $params_geocode);
    }
    catch (CiviCRM_API3_Exception $e) {
eileen's avatar
eileen committed
65
      if ($e->getMessage() == 'Aborting batch geocoding. Hit the over query limit on geocoder.') {
66 67
        $this->markTestIncomplete('Job.geocode error_message: A fatal error was triggered: Aborting batch geocoding. Hit the over query limit on geocoder.');
      }
68 69 70
      else {
        throw $e;
      }
71 72 73 74 75 76 77 78
    }
    $params_address_getsingle = array(
      'contact_id' => $contact_values['id'],
    );
    $result_address_getsingle = civicrm_api3('Address', 'getsingle', $params_address_getsingle);

    // We should get a geo code setting.
    $this->assertApproxEquals('38.89', CRM_Utils_Array::value('geo_code_1', $result_address_getsingle), 1);
79
  }
80

81
}