3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_activity_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contact
36 * Class api_v3_AddressTest
39 class api_v3_AddressTest
extends CiviUnitTestCase
{
40 protected $_apiversion = 3;
41 protected $_contactID;
42 protected $_locationType;
47 public function setUp() {
48 $this->_entity
= 'Address';
51 $this->_contactID
= $this->organizationCreate();
52 $this->_locationType
= $this->locationTypeCreate();
53 CRM_Core_PseudoConstant
::flush();
55 $this->_params
= array(
56 'contact_id' => $this->_contactID
,
57 'location_type_id' => $this->_locationType
->id
,
58 'street_name' => 'Ambachtstraat',
59 'street_number' => '23',
60 'street_address' => 'Ambachtstraat 23',
61 'postal_code' => '6971 BN',
62 'country_id' => '1152',
68 public function tearDown() {
69 $this->locationTypeDelete($this->_locationType
->id
);
70 $this->contactDelete($this->_contactID
);
71 $this->quickCleanup(array('civicrm_address', 'civicrm_relationship'));
74 public function testCreateAddress() {
75 $result = $this->callAPIAndDocument('address', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
76 $this->assertEquals(1, $result['count']);
77 $this->assertNotNull($result['values'][$result['id']]['id']);
78 $this->getAndCheck($this->_params
, $result['id'], 'address');
81 public function testCreateAddressParsing() {
83 'street_parsing' => 1,
84 'street_address' => '54A Excelsior Ave. Apt 1C',
85 'location_type_id' => $this->_locationType
->id
,
86 'contact_id' => $this->_contactID
,
88 $subfile = "AddressParse";
89 $description = "Demonstrates Use of address parsing param.";
90 $result = $this->callAPIAndDocument('address', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
91 $this->assertEquals(54, $result['values'][$result['id']]['street_number']);
92 $this->assertEquals('A', $result['values'][$result['id']]['street_number_suffix']);
93 $this->assertEquals('Excelsior Ave.', $result['values'][$result['id']]['street_name']);
94 $this->assertEquals('Apt 1C', $result['values'][$result['id']]['street_unit']);
95 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
100 * Is_primary should be set as a default.
102 public function testCreateAddressTestDefaults() {
103 $params = $this->_params
;
104 unset($params['is_primary']);
105 $result = $this->callAPISuccess('address', 'create', $params);
106 $this->assertEquals(1, $result['count']);
107 $this->assertEquals(1, $result['values'][$result['id']]['is_primary']);
108 $this->getAndCheck($this->_params
, $result['id'], 'address');
111 public function testCreateAddressTooLongSuffix() {
112 $params = $this->_params
;
113 $params['street_number_suffix'] = 'really long string';
114 $this->callAPIFailure('address', 'create', $params);
118 * Create an address with a master ID and ensure that a relationship is created.
120 public function testCreateAddressWithMasterRelationshipHousehold() {
121 $householdID = $this->householdCreate();
122 $address = $this->callAPISuccess('address', 'create', array_merge($this->_params
, $this->_params
, array('contact_id' => $householdID)));
123 $individualID = $this->individualCreate();
124 $individualParams = array(
125 'contact_id' => $individualID,
126 'master_id' => $address['id'],
128 $this->callAPISuccess('address', 'create', array_merge($this->_params
, $individualParams));
129 $this->callAPISuccess('relationship', 'getcount', array(
130 'contact_id_a' => $individualID,
131 'contact_id_b' => $this->_contactID
,
136 * Create an address with a master ID and ensure that a relationship is created.
138 public function testCreateAddressWithMasterRelationshipOrganization() {
139 $address = $this->callAPISuccess('address', 'create', $this->_params
);
140 $individualID = $this->individualCreate();
141 $individualParams = array(
142 'contact_id' => $individualID,
143 'master_id' => $address['id'],
145 $this->callAPISuccess('address', 'create', array_merge($this->_params
, $individualParams));
146 $this->callAPISuccess('relationship', 'getcount', array(
147 'contact_id_a' => $individualID,
148 'contact_id_b' => $this->_contactID
,
153 * Create an address with a master ID and relationship creation disabled.
155 public function testCreateAddressWithoutMasterRelationshipOrganization() {
156 $address = $this->callAPISuccess('address', 'create', $this->_params
);
157 $individualID = $this->individualCreate();
158 $individualParams = array(
159 'contact_id' => $individualID,
160 'master_id' => $address['id'],
161 'update_current_employer' => 0,
163 $this->callAPISuccess('address', 'create', array_merge($this->_params
, $individualParams));
164 $this->callAPISuccess('relationship', 'getcount', array(
165 'contact_id_a' => $individualID,
166 'contact_id_b' => $this->_contactID
,
171 * Create an address with a master ID and ensure that a relationship is created.
173 public function testCreateAddressWithMasterRelationshipChangingOrganization() {
174 $address = $this->callAPISuccess('address', 'create', $this->_params
);
175 $organisation2ID = $this->organizationCreate();
176 $address2 = $this->callAPISuccess('address', 'create', array_merge($this->_params
, array('contact_id' => $organisation2ID)));
177 $individualID = $this->individualCreate();
178 $individualParams = array_merge($this->_params
, array(
179 'contact_id' => $individualID,
180 'master_id' => $address['id'],
182 $individualAddress = $this->callAPISuccess('address', 'create', $individualParams);
183 $individualParams['master_id'] = $address2['id'];
184 $individualParams['id'] = $individualAddress['id'];
185 $this->callAPISuccess('address', 'create', $individualParams);
186 $this->callAPISuccessGetCount('relationship', array('contact_id_a' => $individualID), 2);
187 $this->markTestIncomplete('Remainder of test checks that employer relationship is disabled when new one is created but turns out to be not happening - by design?');
188 $this->callAPISuccessGetCount('relationship', array('contact_id_a' => $individualID, 'is_active' => FALSE), 1);
189 $this->callAPISuccessGetCount('relationship', array(
190 'contact_id_a' => $individualID,
192 'contact_id_b' => $organisation2ID,
198 * Is_primary should be set as a default.
200 * ie. create the address, unset the params & recreate.
201 * is_primary should be 0 before & after the update. ie - having no other address
202 * is_primary is invalid.
204 public function testCreateAddressTestDefaultWithID() {
205 $params = $this->_params
;
206 $params['is_primary'] = 0;
207 $result = $this->callAPISuccess('address', 'create', $params);
208 unset($params['is_primary']);
209 $params['id'] = $result['id'];
210 $result = $this->callAPISuccess('address', 'create', $params);
211 $this->callAPISuccess('address', 'get', array('contact_id' => $params['contact_id']));
212 $this->assertEquals(1, $result['count']);
213 $this->assertEquals(1, $result['values'][$result['id']]['is_primary']);
214 $this->getAndCheck($params, $result['id'], 'address', __FUNCTION__
);
218 * test address deletion.
220 public function testDeleteAddress() {
221 //check there are no address to start with
222 $get = $this->callAPISuccess('address', 'get', array(
223 'location_type_id' => $this->_locationType
->id
,
225 $this->assertEquals(0, $get['count'], 'Contact already exists ');
228 $create = $this->callAPISuccess('address', 'create', $this->_params
);
230 $result = $this->callAPIAndDocument('address', 'delete', array('id' => $create['id']), __FUNCTION__
, __FILE__
);
231 $this->assertEquals(1, $result['count']);
232 $get = $this->callAPISuccess('address', 'get', array(
233 'location_type_id' => $this->_locationType
->id
,
235 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
239 * Test civicrm_address_get - success expected.
241 public function testGetAddress() {
242 $address = $this->callAPISuccess('address', 'create', $this->_params
);
245 'contact_id' => $this->_contactID
,
246 'street_name' => $address['values'][$address['id']]['street_name'],
248 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__
, __FILE__
);
249 $this->callAPISuccess('Address', 'delete', array('id' => $result['id']));
250 $this->assertEquals($address['values'][$address['id']]['location_type_id'], $result['values'][$address['id']]['location_type_id']);
251 $this->assertEquals($address['values'][$address['id']]['is_primary'], $result['values'][$address['id']]['is_primary']);
252 $this->assertEquals($address['values'][$address['id']]['street_address'], $result['values'][$address['id']]['street_address']);
256 * Test civicrm_address_get - success expected.
258 public function testGetSingleAddress() {
259 $this->callAPISuccess('address', 'create', $this->_params
);
261 'contact_id' => $this->_contactID
,
263 $address = $this->callAPISuccess('Address', 'getsingle', ($params));
264 $this->assertEquals($address['location_type_id'], $this->_params
['location_type_id']);
265 $this->callAPISuccess('address', 'delete', array('id' => $address['id']));
269 * Test civicrm_address_get with sort option- success expected.
271 public function testGetAddressSort() {
272 $create = $this->callAPISuccess('address', 'create', $this->_params
);
273 $this->callAPISuccess('address', 'create', array_merge($this->_params
, array('street_address' => 'yzy')));
274 $subfile = "AddressSort";
275 $description = "Demonstrates Use of sort filter.";
278 'sort' => 'street_address DESC',
283 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
284 $this->assertEquals(2, $result['count']);
285 $this->assertEquals('Ambachtstraat 23', $result['values'][1]['street_address']);
286 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
290 * Test civicrm_address_get with sort option- success expected.
292 public function testGetAddressLikeSuccess() {
293 $this->callAPISuccess('address', 'create', $this->_params
);
294 $subfile = "AddressLike";
295 $description = "Demonstrates Use of Like.";
297 'street_address' => array('LIKE' => '%mb%'),
300 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
301 $this->assertEquals(1, $result['count']);
302 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address']);
303 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
307 * Test civicrm_address_get with sort option- success expected.
309 public function testGetAddressLikeFail() {
310 $create = $this->callAPISuccess('address', 'create', $this->_params
);
312 'street_address' => array('LIKE' => "'%xy%'"),
315 $result = $this->callAPISuccess('Address', 'Get', ($params));
316 $this->assertEquals(0, $result['count']);
317 $this->callAPISuccess('address', 'delete', array('id' => $create['id']));
322 public function testGetWithCustom() {
323 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
325 $params = $this->_params
;
326 $params['custom_' . $ids['custom_field_id']] = "custom string";
328 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
330 $getParams = array('id' => $result['id'], 'return' => array('custom'));
331 $check = $this->callAPISuccess($this->_entity
, 'get', $getParams);
333 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
335 $this->customFieldDelete($ids['custom_field_id']);
336 $this->customGroupDelete($ids['custom_group_id']);
337 $this->callAPISuccess('address', 'delete', array('id' => $result['id']));
342 public function testCreateAddressPrimaryHandlingChangeToPrimary() {
343 $params = $this->_params
;
344 unset($params['is_primary']);
345 $address1 = $this->callAPISuccess('address', 'create', $params);
346 $this->assertApiSuccess($address1);
347 //now we check & make sure it has been set to primary
348 $check = $this->callAPISuccess('address', 'getcount', array(
350 'id' => $address1['id'],
352 $this->assertEquals(1, $check);
353 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
358 public function testCreateAddressPrimaryHandlingChangeExisting() {
359 $address1 = $this->callAPISuccess('address', 'create', $this->_params
);
360 $this->callAPISuccess('address', 'create', $this->_params
);
361 $check = $this->callAPISuccess('address', 'getcount', array(
363 'contact_id' => $this->_contactID
,
365 $this->assertEquals(1, $check);
366 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
370 * Test Creating address of same type alreay ind the database
371 * This is legacy API v3 behaviour and not correct behaviour
372 * however we are too far down the path wiwth v3 to fix this
373 * @link https://chat.civicrm.org/civicrm/pl/zcq3jkg69jdt5g4aqze6bbe9pc
374 * @todo vis this in v4 api
376 public function testCreateDuplicateLocationTypes() {
377 $address1 = $this->callAPISuccess('address', 'create', $this->_params
);
378 $address2 = $this->callAPISuccess('address', 'create', array(
379 'location_type_id' => $this->_locationType
->id
,
380 'street_address' => '1600 Pensilvania Avenue',
381 'city' => 'Washington DC',
384 'contact_id' => $this->_contactID
,
386 $check = $this->callAPISuccess('address', 'getcount', array(
387 'contact_id' => $this->_contactID
,
388 'location_type_id' => $this->_locationType
->id
,
390 $this->assertEquals(2, $check);
391 $this->callAPISuccess('address', 'delete', array('id' => $address1['id']));
392 $this->callAPISuccess('address', 'delete', array('id' => $address2['id']));
395 public function testGetWithJoin() {
396 $cid = $this->individualCreate(array(
397 'api.Address.create' => array(
398 'street_address' => __FUNCTION__
,
399 'location_type_id' => $this->_locationType
->id
,
402 $result = $this->callAPISuccess('address', 'getsingle', array(
403 'check_permissions' => TRUE,
404 'contact_id' => $cid,
405 'street_address' => __FUNCTION__
,
406 'return' => 'contact_id.contact_type',
408 $this->assertEquals('Individual', $result['contact_id.contact_type']);
412 * Test Address create with a state name that at least two countries have, e.g. Maryland, United States vs. Maryland, Liberia
414 * @see https://lab.civicrm.org/dev/core/issues/725
416 public function testCreateAddressStateProvinceIDCorrectForCountry() {
417 $params = $this->_params
;
418 $params['sequential'] = 1;
419 // United States country id
420 $params['country_id'] = '1228';
421 $params['state_province_id'] = 'Maryland';
422 $params['city'] = 'Baltimore';
423 $params['street_address'] = '600 N Charles St.';
424 $params['postal_code'] = '21201';
425 unset($params['street_name']);
426 unset($params['street_number']);
427 $address1 = $this->callAPISuccess('address', 'create', $params);
428 // should find state_province_id of 1019, Maryland, United States ... NOT 3497, Maryland, Liberia
429 $this->assertEquals('1019', $address1['values'][0]['state_province_id']);
431 // Now try it in Liberia
432 $params = $this->_params
;
433 $params['sequential'] = 1;
434 // Liberia country id
435 $params['country_id'] = '1122';
436 $params['state_province_id'] = 'Maryland';
437 $address2 = $this->callAPISuccess('address', 'create', $params);
438 $this->assertEquals('3497', $address2['values'][0]['state_province_id']);
441 public function getSymbolicCountryStateExamples() {
443 // [mixed $inputCountry, mixed $inputState, int $expectCountry, int $expectState]
444 [1228, 1004, 1228, 1004],
445 //['US', 'CA', 1228, 1004],
446 //['US', 'TX', 1228, 1042],
447 ['US', 'California', 1228, 1004],
448 [1228, 'Texas', 1228, 1042],
449 // Don't think these have been supported?
450 // ['United States', 1004, 1228, 1004] ,
451 // ['United States', 'TX', 1228, 1042],
456 * @param mixed $inputCountry
458 * @param mixed $inputState
460 * @param int $expectCountry
461 * @param int $expectState
462 * @dataProvider getSymbolicCountryStateExamples
464 public function testCreateAddressSymbolicCountryAndState($inputCountry, $inputState, $expectCountry, $expectState) {
465 $cid = $this->individualCreate();
466 $r = $this->callAPISuccess('Address', 'create', [
467 'contact_id' => $cid,
468 'location_type_id' => 1,
469 'street_address' => '123 Some St',
470 'city' => 'Hereville',
472 'country_id' => $inputCountry,
474 'state_province_id' => $inputState,
475 'postal_code' => '94100',
477 $created = CRM_Utils_Array
::first($r['values']);
478 $this->assertEquals($expectCountry, $created['country_id']);
479 $this->assertEquals($expectState, $created['state_province_id']);
482 public function testBuildStateProvinceOptionsWithDodgyProvinceLimit() {
483 $provinceLimit = [1228, "abcd;ef"];
484 $this->callAPISuccess('setting', 'create', [
485 'provinceLimit' => $provinceLimit,
487 $result = $this->callAPIFailure('address', 'getoptions', ['field' => 'state_province_id']);
488 // confirm that we hit our error not a SQLI.
489 $this->assertEquals('Province limit or default country setting is incorrect', $result['error_message']);
490 $this->callAPISuccess('setting', 'create', [
491 'provinceLimit' => [1228],
493 // Now confirm with a correct province setting it works fine
494 $this->callAPISuccess('address', 'getoptions', ['field' => 'state_province_id']);
497 public function testBuildCountryWithDodgyCountryLimitSetting() {
498 $countryLimit = [1228, "abcd;ef"];
499 $this->callAPISuccess('setting', 'create', [
500 'countryLimit' => $countryLimit,
502 $result = $this->callAPIFailure('address', 'getoptions', ['field' => 'country_id']);
503 // confirm that we hit our error not a SQLI.
504 $this->assertEquals('Available Country setting is incorrect', $result['error_message']);
505 $this->callAPISuccess('setting', 'create', [
506 'countryLimit' => [1228],
508 // Now confirm with a correct province setting it works fine
509 $this->callAPISuccess('address', 'getoptions', ['field' => 'country_id']);
512 public function testBuildCountyWithDodgeStateProvinceFiltering() {
513 $result = $this->callAPIFailure('Address', 'getoptions', [
514 'field' => 'county_id',
515 'state_province_id' => "abcd;ef",
517 $this->assertEquals('Can only accept Integers for state_province_id filtering', $result['error_message']);
518 $goodResult = $this->callAPISuccess('Address', 'getoptions', [
519 'field' => 'county_id',
520 'state_province_id' => 1004,
522 $this->assertEquals('San Francisco', $goodResult['values'][4]);