Merge pull request #11407 from cividesk/CRM-21553
[civicrm-core.git] / tests / phpunit / CRM / Utils / GeocodeTest.php
CommitLineData
0c5d7969
SL
1<?php
2/**
3 * Class CRM_Utils_GeocodeTest
4 * @group headless
5 */
6class CRM_Utils_GeocodeTest extends CiviUnitTestCase {
7
8 public function setUp() {
9 parent::setUp();
10 }
11
0aef9f5a
SL
12 public function tearDown() {
13 parent::tearDown();
0c5d7969
SL
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);
b83ea84c
D
19 if (isset($params['geo_code_error']) && $params['geo_code_error'] == 'OVER_QUERY_LIMIT') {
20 $this->markTestIncomplete('geo_code_error: OVER_QUERY_LIMIT');
21 }
0c5d7969 22 $this->assertTrue($formatted);
61e0e289
SL
23 $this->assertApproxEquals('46.72', $params['geo_code_1'], 1);
24 $this->assertApproxEquals('-94.68', $params['geo_code_2'], 1);
0c5d7969
SL
25 }
26
fd78a9e5
JM
27 public function testGeocodeMethodOff() {
28 // Set a geocoding provider.
29 $result = civicrm_api3('Setting', 'create', array(
30 'geoProvider' => "Google",
31 ));
32
94d2b28e 33 CRM_Utils_GeocodeProvider::disableForSession();
b83ea84c
D
34
35 // Save a contact with geo coding disabled.
fd78a9e5
JM
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',
f3171718
JM
44 'location_type_id' => 1,
45 ),
fd78a9e5
JM
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']);
fd78a9e5 50
b83ea84c
D
51 $this->assertArrayNotHasKey('geo_code_1', $address_values, 'No geocoding when geocodeMethod is empty');
52
53 // Run the geocode job on that specific contact
94d2b28e 54 CRM_Utils_GeocodeProvider::reset();
b83ea84c
D
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) {
b679c5b1 65 if ($e->getMessage() == 'Aborting batch geocoding. Hit the over query limit on geocoder.') {
b83ea84c
D
66 $this->markTestIncomplete('Job.geocode error_message: A fatal error was triggered: Aborting batch geocoding. Hit the over query limit on geocoder.');
67 }
29bcf8ce
D
68 else {
69 throw $e;
70 }
b83ea84c
D
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);
fd78a9e5 79 }
31466f3c 80
0c5d7969 81}