Commit | Line | Data |
---|---|---|
0c5d7969 SL |
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 | ||
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 | ||
b83ea84c D |
33 | // Set geocodeMethod to empty. |
34 | $config = CRM_Core_Config::singleton(); | |
35 | $config->geocodeMethod = ''; | |
36 | ||
37 | // Save a contact with geo coding disabled. | |
fd78a9e5 JM |
38 | $params = array( |
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', | |
f3171718 JM |
46 | 'location_type_id' => 1, |
47 | ), | |
fd78a9e5 JM |
48 | ); |
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']); | |
fd78a9e5 | 52 | |
b83ea84c D |
53 | $this->assertArrayNotHasKey('geo_code_1', $address_values, 'No geocoding when geocodeMethod is empty'); |
54 | ||
55 | // Run the geocode job on that specific contact | |
56 | $config->geocodeMethod = 'CRM_Utils_Geocode_Google'; | |
fd78a9e5 | 57 | |
b83ea84c D |
58 | try { |
59 | $params_geocode = array( | |
60 | 'start' => $contact_values['id'], | |
61 | 'end' => $contact_values['id'] + 1, | |
62 | 'geocoding' => 1, | |
63 | 'parse' => 0, | |
64 | ); | |
65 | $result_geocode = civicrm_api3('Job', 'geocode', $params_geocode); | |
66 | } | |
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.'); | |
70 | } | |
29bcf8ce D |
71 | else { |
72 | throw $e; | |
73 | } | |
b83ea84c D |
74 | } |
75 | $params_address_getsingle = array( | |
76 | 'contact_id' => $contact_values['id'], | |
77 | ); | |
78 | $result_address_getsingle = civicrm_api3('Address', 'getsingle', $params_address_getsingle); | |
79 | ||
80 | // We should get a geo code setting. | |
81 | $this->assertApproxEquals('38.89', CRM_Utils_Array::value('geo_code_1', $result_address_getsingle), 1); | |
fd78a9e5 | 82 | } |
31466f3c | 83 | |
0c5d7969 | 84 | } |