| 1 | <?php |
| 2 | /** |
| 3 | * Class CRM_Utils_GeocodeTest |
| 4 | * @group headless |
| 5 | */ |
| 6 | class CRM_Utils_GeocodeTest extends CiviUnitTestCase { |
| 7 | |
| 8 | public function setUp() { |
| 9 | parent::setUp(); |
| 10 | } |
| 11 | |
| 12 | public function tearDown() { |
| 13 | parent::tearDown(); |
| 14 | } |
| 15 | |
| 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'); |
| 21 | } |
| 22 | $this->assertTrue($formatted); |
| 23 | $this->assertApproxEquals('46.72', $params['geo_code_1'], 1); |
| 24 | $this->assertApproxEquals('-94.68', $params['geo_code_2'], 1); |
| 25 | } |
| 26 | |
| 27 | public function testGeocodeMethodOff() { |
| 28 | // Set a geocoding provider. |
| 29 | $result = civicrm_api3('Setting', 'create', array( |
| 30 | 'geoProvider' => "Google", |
| 31 | )); |
| 32 | |
| 33 | CRM_Utils_GeocodeProvider::disableForSession(); |
| 34 | |
| 35 | // Save a contact with geo coding disabled. |
| 36 | $params = array( |
| 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, |
| 45 | ), |
| 46 | ); |
| 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']); |
| 50 | |
| 51 | $this->assertArrayNotHasKey('geo_code_1', $address_values, 'No geocoding when geocodeMethod is empty'); |
| 52 | |
| 53 | // Run the geocode job on that specific contact |
| 54 | CRM_Utils_GeocodeProvider::reset(); |
| 55 | try { |
| 56 | $params_geocode = array( |
| 57 | 'start' => $contact_values['id'], |
| 58 | 'end' => $contact_values['id'] + 1, |
| 59 | 'geocoding' => 1, |
| 60 | 'parse' => 0, |
| 61 | ); |
| 62 | $result_geocode = civicrm_api3('Job', 'geocode', $params_geocode); |
| 63 | } |
| 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.'); |
| 67 | } |
| 68 | else { |
| 69 | throw $e; |
| 70 | } |
| 71 | } |
| 72 | $params_address_getsingle = array( |
| 73 | 'contact_id' => $contact_values['id'], |
| 74 | ); |
| 75 | $result_address_getsingle = civicrm_api3('Address', 'getsingle', $params_address_getsingle); |
| 76 | |
| 77 | // We should get a geo code setting. |
| 78 | $this->assertApproxEquals('38.89', CRM_Utils_Array::value('geo_code_1', $result_address_getsingle), 1); |
| 79 | } |
| 80 | |
| 81 | } |