Add in Country and StateProvince APIv4 Entities
[civicrm-core.git] / tests / phpunit / api / v3 / AddressTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
7d61e75f
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035
TO
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * Test APIv3 civicrm_activity_* functions
14 *
6c6e6187
TO
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contact
6a488035
TO
17 */
18
4cbe18b8
EM
19/**
20 * Class api_v3_AddressTest
acb109b7 21 * @group headless
4cbe18b8 22 */
6a488035 23class api_v3_AddressTest extends CiviUnitTestCase {
6a488035
TO
24 protected $_contactID;
25 protected $_locationType;
26 protected $_params;
b7c9bc4c 27
430ae6dd
TO
28 protected $_entity;
29
00be9182 30 public function setUp() {
6a488035
TO
31 $this->_entity = 'Address';
32 parent::setUp();
33
34 $this->_contactID = $this->organizationCreate();
35 $this->_locationType = $this->locationTypeCreate();
2683ce94 36 CRM_Core_PseudoConstant::flush();
6a488035 37
9099cab3 38 $this->_params = [
6a488035
TO
39 'contact_id' => $this->_contactID,
40 'location_type_id' => $this->_locationType->id,
41 'street_name' => 'Ambachtstraat',
42 'street_number' => '23',
43 'street_address' => 'Ambachtstraat 23',
44 'postal_code' => '6971 BN',
45 'country_id' => '1152',
46 'city' => 'Brummen',
47 'is_primary' => 1,
9099cab3 48 ];
6a488035
TO
49 }
50
00be9182 51 public function tearDown() {
6a488035
TO
52 $this->locationTypeDelete($this->_locationType->id);
53 $this->contactDelete($this->_contactID);
9099cab3 54 $this->quickCleanup(['civicrm_address', 'civicrm_relationship']);
fda18dc3 55 parent::tearDown();
6a488035
TO
56 }
57
2d932085
CW
58 /**
59 * @param int $version
60 * @dataProvider versionThreeAndFour
61 */
62 public function testCreateAddress($version) {
63 $this->_apiversion = $version;
4e420887 64 $result = $this->callAPIAndDocument('address', 'create', $this->_params, __FUNCTION__, __FILE__);
ba4a1892
TM
65 $this->assertEquals(1, $result['count']);
66 $this->assertNotNull($result['values'][$result['id']]['id']);
6a488035
TO
67 $this->getAndCheck($this->_params, $result['id'], 'address');
68 }
4e420887 69
2d932085
CW
70 /**
71 * @param int $version
72 * @dataProvider versionThreeAndFour
73 */
74 public function testCreateAddressParsing($version) {
75 $this->_apiversion = $version;
9099cab3 76 $params = [
6a488035
TO
77 'street_parsing' => 1,
78 'street_address' => '54A Excelsior Ave. Apt 1C',
79 'location_type_id' => $this->_locationType->id,
80 'contact_id' => $this->_contactID,
9099cab3 81 ];
5896d037 82 $subfile = "AddressParse";
5c49fee0 83 $description = "Demonstrates Use of address parsing param.";
4e420887 84 $result = $this->callAPIAndDocument('address', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
ba4a1892
TM
85 $this->assertEquals(54, $result['values'][$result['id']]['street_number']);
86 $this->assertEquals('A', $result['values'][$result['id']]['street_number_suffix']);
87 $this->assertEquals('Excelsior Ave.', $result['values'][$result['id']]['street_name']);
88 $this->assertEquals('Apt 1C', $result['values'][$result['id']]['street_unit']);
9099cab3 89 $this->callAPISuccess('address', 'delete', ['id' => $result['id']]);
6a488035
TO
90
91 }
92
c490a46a 93 /**
fe482240 94 * Is_primary should be set as a default.
2d932085
CW
95 * @param int $version
96 * @dataProvider versionThreeAndFour
c490a46a 97 */
2d932085
CW
98 public function testCreateAddressTestDefaults($version) {
99 $this->_apiversion = $version;
6a488035
TO
100 $params = $this->_params;
101 unset($params['is_primary']);
4e420887 102 $result = $this->callAPISuccess('address', 'create', $params);
ba4a1892
TM
103 $this->assertEquals(1, $result['count']);
104 $this->assertEquals(1, $result['values'][$result['id']]['is_primary']);
6a488035
TO
105 $this->getAndCheck($this->_params, $result['id'], 'address');
106 }
107
0e70279a
AS
108 /**
109 * If no location is specified when creating a new address, it should default to
110 * the LocationType default
111 *
112 * @param int $version
113 * @dataProvider versionThreeAndFour
114 */
115 public function testCreateAddressDefaultLocation($version) {
116 $this->_apiversion = $version;
117 $params = $this->_params;
118 unset($params['location_type_id']);
119 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
120 $this->assertEquals(CRM_Core_BAO_LocationType::getDefault()->id, $result['values'][$result['id']]['location_type_id']);
121 $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]);
122 }
123
2d932085
CW
124 /**
125 * FIXME: Api4
126 */
6a488035
TO
127 public function testCreateAddressTooLongSuffix() {
128 $params = $this->_params;
129 $params['street_number_suffix'] = 'really long string';
a41f119c
EM
130 $this->callAPIFailure('address', 'create', $params);
131 }
132
133 /**
eceb18cc 134 * Create an address with a master ID and ensure that a relationship is created.
2d932085
CW
135 * @param int $version
136 * @dataProvider versionThreeAndFour
a41f119c 137 */
2d932085
CW
138 public function testCreateAddressWithMasterRelationshipHousehold($version) {
139 $this->_apiversion = $version;
a41f119c 140 $householdID = $this->householdCreate();
9099cab3 141 $address = $this->callAPISuccess('address', 'create', array_merge($this->_params, $this->_params, ['contact_id' => $householdID]));
a41f119c 142 $individualID = $this->individualCreate();
9099cab3 143 $individualParams = [
a41f119c
EM
144 'contact_id' => $individualID,
145 'master_id' => $address['id'],
9099cab3 146 ];
a41f119c 147 $this->callAPISuccess('address', 'create', array_merge($this->_params, $individualParams));
9099cab3 148 $this->callAPISuccess('relationship', 'getcount', [
92915c55
TO
149 'contact_id_a' => $individualID,
150 'contact_id_b' => $this->_contactID,
9099cab3 151 ]);
a41f119c
EM
152 }
153
154 /**
eceb18cc 155 * Create an address with a master ID and ensure that a relationship is created.
2d932085
CW
156 * @param int $version
157 * @dataProvider versionThreeAndFour
6c6e6187 158 */
2d932085
CW
159 public function testCreateAddressWithMasterRelationshipOrganization($version) {
160 $this->_apiversion = $version;
a41f119c
EM
161 $address = $this->callAPISuccess('address', 'create', $this->_params);
162 $individualID = $this->individualCreate();
9099cab3 163 $individualParams = [
a41f119c
EM
164 'contact_id' => $individualID,
165 'master_id' => $address['id'],
9099cab3 166 ];
a41f119c 167 $this->callAPISuccess('address', 'create', array_merge($this->_params, $individualParams));
9099cab3 168 $this->callAPISuccess('relationship', 'getcount', [
92915c55
TO
169 'contact_id_a' => $individualID,
170 'contact_id_b' => $this->_contactID,
9099cab3 171 ], 1);
e0fb9216
CW
172 }
173
174 /**
175 * Create an address with a master ID and relationship creation disabled.
2d932085
CW
176 * @param int $version
177 * @dataProvider versionThreeAndFour
e0fb9216 178 */
2d932085
CW
179 public function testCreateAddressWithoutMasterRelationshipOrganization($version) {
180 $this->_apiversion = $version;
e0fb9216
CW
181 $address = $this->callAPISuccess('address', 'create', $this->_params);
182 $individualID = $this->individualCreate();
9099cab3 183 $individualParams = [
e0fb9216
CW
184 'contact_id' => $individualID,
185 'master_id' => $address['id'],
186 'update_current_employer' => 0,
9099cab3 187 ];
e0fb9216 188 $this->callAPISuccess('address', 'create', array_merge($this->_params, $individualParams));
9099cab3 189 $this->callAPISuccess('relationship', 'getcount', [
e0fb9216
CW
190 'contact_id_a' => $individualID,
191 'contact_id_b' => $this->_contactID,
9099cab3 192 ], 0);
a41f119c
EM
193 }
194
195 /**
eceb18cc 196 * Create an address with a master ID and ensure that a relationship is created.
2d932085
CW
197 * @param int $version
198 * @dataProvider versionThreeAndFour
a41f119c 199 */
2d932085
CW
200 public function testCreateAddressWithMasterRelationshipChangingOrganization($version) {
201 $this->_apiversion = $version;
a41f119c
EM
202 $address = $this->callAPISuccess('address', 'create', $this->_params);
203 $organisation2ID = $this->organizationCreate();
9099cab3 204 $address2 = $this->callAPISuccess('address', 'create', array_merge($this->_params, ['contact_id' => $organisation2ID]));
a41f119c 205 $individualID = $this->individualCreate();
9099cab3 206 $individualParams = array_merge($this->_params, [
a41f119c
EM
207 'contact_id' => $individualID,
208 'master_id' => $address['id'],
9099cab3 209 ]);
a41f119c
EM
210 $individualAddress = $this->callAPISuccess('address', 'create', $individualParams);
211 $individualParams['master_id'] = $address2['id'];
212 $individualParams['id'] = $individualAddress['id'];
213 $this->callAPISuccess('address', 'create', $individualParams);
9099cab3 214 $this->callAPISuccessGetCount('relationship', ['contact_id_a' => $individualID], 2);
a41f119c 215 $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?');
9099cab3
CW
216 $this->callAPISuccessGetCount('relationship', ['contact_id_a' => $individualID, 'is_active' => FALSE], 1);
217 $this->callAPISuccessGetCount('relationship', [
92915c55
TO
218 'contact_id_a' => $individualID,
219 'is_active' => TRUE,
220 'contact_id_b' => $organisation2ID,
9099cab3 221 ], 1);
a41f119c
EM
222
223 }
6a488035 224
c490a46a 225 /**
fe482240
EM
226 * Is_primary should be set as a default.
227 *
228 * ie. create the address, unset the params & recreate.
c490a46a 229 * is_primary should be 0 before & after the update. ie - having no other address
fe482240 230 * is_primary is invalid.
2d932085
CW
231 * @param int $version
232 * @dataProvider versionThreeAndFour
c490a46a 233 */
2d932085
CW
234 public function testCreateAddressTestDefaultWithID($version) {
235 $this->_apiversion = $version;
6a488035
TO
236 $params = $this->_params;
237 $params['is_primary'] = 0;
4e420887 238 $result = $this->callAPISuccess('address', 'create', $params);
6a488035
TO
239 unset($params['is_primary']);
240 $params['id'] = $result['id'];
2d932085 241 $this->callAPISuccess('address', 'create', $params);
9099cab3 242 $result = $this->callAPISuccess('address', 'get', ['contact_id' => $params['contact_id']]);
a41f119c
EM
243 $this->assertEquals(1, $result['count']);
244 $this->assertEquals(1, $result['values'][$result['id']]['is_primary']);
6a488035
TO
245 $this->getAndCheck($params, $result['id'], 'address', __FUNCTION__);
246 }
6a488035 247
a41f119c 248 /**
eceb18cc 249 * test address deletion.
2d932085
CW
250 * @param int $version
251 * @dataProvider versionThreeAndFour
a41f119c 252 */
2d932085
CW
253 public function testDeleteAddress($version) {
254 $this->_apiversion = $version;
a41f119c 255 //check there are no address to start with
9099cab3 256 $get = $this->callAPISuccess('address', 'get', [
4e420887 257 'location_type_id' => $this->_locationType->id,
9099cab3 258 ]);
a41f119c 259 $this->assertEquals(0, $get['count'], 'Contact already exists ');
6a488035
TO
260
261 //create one
4e420887 262 $create = $this->callAPISuccess('address', 'create', $this->_params);
6a488035 263
9099cab3 264 $result = $this->callAPIAndDocument('address', 'delete', ['id' => $create['id']], __FUNCTION__, __FILE__);
ba4a1892 265 $this->assertEquals(1, $result['count']);
9099cab3 266 $get = $this->callAPISuccess('address', 'get', [
5896d037 267 'location_type_id' => $this->_locationType->id,
9099cab3 268 ]);
6a488035
TO
269 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
270 }
271
272 /**
273 * Test civicrm_address_get - success expected.
2d932085
CW
274 * @param int $version
275 * @dataProvider versionThreeAndFour
6a488035 276 */
2d932085
CW
277 public function testGetAddress($version) {
278 $this->_apiversion = $version;
4e420887 279 $address = $this->callAPISuccess('address', 'create', $this->_params);
6a488035 280
9099cab3 281 $params = [
6a488035
TO
282 'contact_id' => $this->_contactID,
283 'street_name' => $address['values'][$address['id']]['street_name'],
9099cab3 284 ];
4e420887 285 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__);
9099cab3 286 $this->callAPISuccess('Address', 'delete', ['id' => $result['id']]);
a41f119c
EM
287 $this->assertEquals($address['values'][$address['id']]['location_type_id'], $result['values'][$address['id']]['location_type_id']);
288 $this->assertEquals($address['values'][$address['id']]['is_primary'], $result['values'][$address['id']]['is_primary']);
289 $this->assertEquals($address['values'][$address['id']]['street_address'], $result['values'][$address['id']]['street_address']);
6a488035
TO
290 }
291
292 /**
293 * Test civicrm_address_get - success expected.
2d932085
CW
294 * @param int $version
295 * @dataProvider versionThreeAndFour
6a488035 296 */
2d932085
CW
297 public function testGetSingleAddress($version) {
298 $this->_apiversion = $version;
4e420887 299 $this->callAPISuccess('address', 'create', $this->_params);
9099cab3 300 $params = [
6a488035 301 'contact_id' => $this->_contactID,
9099cab3 302 ];
4e420887 303 $address = $this->callAPISuccess('Address', 'getsingle', ($params));
ba4a1892 304 $this->assertEquals($address['location_type_id'], $this->_params['location_type_id']);
9099cab3 305 $this->callAPISuccess('address', 'delete', ['id' => $address['id']]);
6a488035
TO
306 }
307
308 /**
309 * Test civicrm_address_get with sort option- success expected.
2d932085
CW
310 * @param int $version
311 * @dataProvider versionThreeAndFour
6a488035 312 */
2d932085
CW
313 public function testGetAddressSort($version) {
314 $this->_apiversion = $version;
4e420887 315 $create = $this->callAPISuccess('address', 'create', $this->_params);
9099cab3 316 $this->callAPISuccess('address', 'create', array_merge($this->_params, ['street_address' => 'yzy']));
5896d037 317 $subfile = "AddressSort";
5c49fee0 318 $description = "Demonstrates Use of sort filter.";
9099cab3
CW
319 $params = [
320 'options' => [
6a488035
TO
321 'sort' => 'street_address DESC',
322 'limit' => 2,
9099cab3 323 ],
6a488035 324 'sequential' => 1,
9099cab3 325 ];
4e420887 326 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
7ecf6275
EM
327 $this->assertEquals(2, $result['count']);
328 $this->assertEquals('Ambachtstraat 23', $result['values'][1]['street_address']);
9099cab3 329 $this->callAPISuccess('address', 'delete', ['id' => $create['id']]);
6a488035
TO
330 }
331
332 /**
333 * Test civicrm_address_get with sort option- success expected.
2d932085
CW
334 * @param int $version
335 * @dataProvider versionThreeAndFour
6a488035 336 */
2d932085
CW
337 public function testGetAddressLikeSuccess($version) {
338 $this->_apiversion = $version;
4e420887 339 $this->callAPISuccess('address', 'create', $this->_params);
5896d037 340 $subfile = "AddressLike";
5c49fee0 341 $description = "Demonstrates Use of Like.";
9099cab3
CW
342 $params = [
343 'street_address' => ['LIKE' => '%mb%'],
6a488035 344 'sequential' => 1,
9099cab3 345 ];
4e420887 346 $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
ba4a1892
TM
347 $this->assertEquals(1, $result['count']);
348 $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address']);
9099cab3 349 $this->callAPISuccess('address', 'delete', ['id' => $result['id']]);
6a488035
TO
350 }
351
352 /**
353 * Test civicrm_address_get with sort option- success expected.
2d932085
CW
354 * @param int $version
355 * @dataProvider versionThreeAndFour
6a488035 356 */
2d932085
CW
357 public function testGetAddressLikeFail($version) {
358 $this->_apiversion = $version;
4e420887 359 $create = $this->callAPISuccess('address', 'create', $this->_params);
9099cab3
CW
360 $params = [
361 'street_address' => ['LIKE' => "'%xy%'"],
6a488035 362 'sequential' => 1,
9099cab3 363 ];
4e420887 364 $result = $this->callAPISuccess('Address', 'Get', ($params));
ba4a1892 365 $this->assertEquals(0, $result['count']);
9099cab3 366 $this->callAPISuccess('address', 'delete', ['id' => $create['id']]);
6a488035
TO
367 }
368
a41f119c 369 /**
2d932085 370 * FIXME: Api4 custom address fields broken?
a41f119c 371 */
00be9182 372 public function testGetWithCustom() {
6a488035
TO
373 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
374
375 $params = $this->_params;
376 $params['custom_' . $ids['custom_field_id']] = "custom string";
377
4e420887 378 $result = $this->callAPISuccess($this->_entity, 'create', $params);
6a488035 379
9099cab3 380 $getParams = ['id' => $result['id'], 'return' => ['custom']];
4e420887 381 $check = $this->callAPISuccess($this->_entity, 'get', $getParams);
6a488035
TO
382
383 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
384
385 $this->customFieldDelete($ids['custom_field_id']);
386 $this->customGroupDelete($ids['custom_group_id']);
9099cab3 387 $this->callAPISuccess('address', 'delete', ['id' => $result['id']]);
6a488035
TO
388 }
389
a41f119c 390 /**
2d932085
CW
391 * @param int $version
392 * @dataProvider versionThreeAndFour
a41f119c 393 */
2d932085
CW
394 public function testCreateAddressPrimaryHandlingChangeToPrimary($version) {
395 $this->_apiversion = $version;
6a488035
TO
396 $params = $this->_params;
397 unset($params['is_primary']);
4e420887 398 $address1 = $this->callAPISuccess('address', 'create', $params);
a15773db 399 $this->assertApiSuccess($address1);
6a488035 400 //now we check & make sure it has been set to primary
9099cab3 401 $check = $this->callAPISuccess('address', 'getcount', [
5896d037
TO
402 'is_primary' => 1,
403 'id' => $address1['id'],
9099cab3 404 ]);
6a488035 405 $this->assertEquals(1, $check);
9099cab3 406 $this->callAPISuccess('address', 'delete', ['id' => $address1['id']]);
6a488035 407 }
a41f119c
EM
408
409 /**
2d932085
CW
410 * @param int $version
411 * @dataProvider versionThreeAndFour
a41f119c 412 */
2d932085
CW
413 public function testCreateAddressPrimaryHandlingChangeExisting($version) {
414 $this->_apiversion = $version;
4e420887 415 $address1 = $this->callAPISuccess('address', 'create', $this->_params);
a41f119c 416 $this->callAPISuccess('address', 'create', $this->_params);
9099cab3 417 $check = $this->callAPISuccess('address', 'getcount', [
5896d037
TO
418 'is_primary' => 1,
419 'contact_id' => $this->_contactID,
9099cab3 420 ]);
6a488035 421 $this->assertEquals(1, $check);
9099cab3 422 $this->callAPISuccess('address', 'delete', ['id' => $address1['id']]);
6a488035 423 }
96025800 424
a5ac91a8
SL
425 /**
426 * Test Creating address of same type alreay ind the database
f623557b
SL
427 * This is legacy API v3 behaviour and not correct behaviour
428 * however we are too far down the path wiwth v3 to fix this
429 * @link https://chat.civicrm.org/civicrm/pl/zcq3jkg69jdt5g4aqze6bbe9pc
2d932085 430 * FIXME: Api4
a5ac91a8
SL
431 */
432 public function testCreateDuplicateLocationTypes() {
433 $address1 = $this->callAPISuccess('address', 'create', $this->_params);
9099cab3 434 $address2 = $this->callAPISuccess('address', 'create', [
a5ac91a8
SL
435 'location_type_id' => $this->_locationType->id,
436 'street_address' => '1600 Pensilvania Avenue',
437 'city' => 'Washington DC',
438 'is_primary' => 0,
439 'is_billing' => 0,
440 'contact_id' => $this->_contactID,
9099cab3
CW
441 ]);
442 $check = $this->callAPISuccess('address', 'getcount', [
a5ac91a8
SL
443 'contact_id' => $this->_contactID,
444 'location_type_id' => $this->_locationType->id,
9099cab3 445 ]);
a5ac91a8 446 $this->assertEquals(2, $check);
9099cab3
CW
447 $this->callAPISuccess('address', 'delete', ['id' => $address1['id']]);
448 $this->callAPISuccess('address', 'delete', ['id' => $address2['id']]);
a5ac91a8
SL
449 }
450
6f736521 451 public function testGetWithJoin() {
9099cab3
CW
452 $cid = $this->individualCreate([
453 'api.Address.create' => [
6f736521
CW
454 'street_address' => __FUNCTION__,
455 'location_type_id' => $this->_locationType->id,
9099cab3
CW
456 ],
457 ]);
458 $result = $this->callAPISuccess('address', 'getsingle', [
6f736521
CW
459 'check_permissions' => TRUE,
460 'contact_id' => $cid,
461 'street_address' => __FUNCTION__,
462 'return' => 'contact_id.contact_type',
9099cab3 463 ]);
6f736521
CW
464 $this->assertEquals('Individual', $result['contact_id.contact_type']);
465 }
466
eaf39b47
MH
467 /**
468 * Test Address create with a state name that at least two countries have, e.g. Maryland, United States vs. Maryland, Liberia
469 *
470 * @see https://lab.civicrm.org/dev/core/issues/725
471 */
472 public function testCreateAddressStateProvinceIDCorrectForCountry() {
473 $params = $this->_params;
474 $params['sequential'] = 1;
39b959db
SL
475 // United States country id
476 $params['country_id'] = '1228';
eaf39b47
MH
477 $params['state_province_id'] = 'Maryland';
478 $params['city'] = 'Baltimore';
479 $params['street_address'] = '600 N Charles St.';
480 $params['postal_code'] = '21201';
481 unset($params['street_name']);
482 unset($params['street_number']);
483 $address1 = $this->callAPISuccess('address', 'create', $params);
484 // should find state_province_id of 1019, Maryland, United States ... NOT 3497, Maryland, Liberia
485 $this->assertEquals('1019', $address1['values'][0]['state_province_id']);
63b56894
CW
486
487 // Now try it in Liberia
488 $params = $this->_params;
489 $params['sequential'] = 1;
39b959db
SL
490 // Liberia country id
491 $params['country_id'] = '1122';
63b56894
CW
492 $params['state_province_id'] = 'Maryland';
493 $address2 = $this->callAPISuccess('address', 'create', $params);
494 $this->assertEquals('3497', $address2['values'][0]['state_province_id']);
eaf39b47
MH
495 }
496
19381641
MH
497 public function getSymbolicCountryStateExamples() {
498 return [
499 // [mixed $inputCountry, mixed $inputState, int $expectCountry, int $expectState]
500 [1228, 1004, 1228, 1004],
501 //['US', 'CA', 1228, 1004],
502 //['US', 'TX', 1228, 1042],
503 ['US', 'California', 1228, 1004],
504 [1228, 'Texas', 1228, 1042],
505 // Don't think these have been supported?
506 // ['United States', 1004, 1228, 1004] ,
507 // ['United States', 'TX', 1228, 1042],
508 ];
509 }
510
511 /**
512 * @param mixed $inputCountry
513 * Ex: 1228 or 'US'
514 * @param mixed $inputState
515 * Ex: 1004 or 'CA'
516 * @param int $expectCountry
517 * @param int $expectState
518 * @dataProvider getSymbolicCountryStateExamples
519 */
520 public function testCreateAddressSymbolicCountryAndState($inputCountry, $inputState, $expectCountry, $expectState) {
521 $cid = $this->individualCreate();
522 $r = $this->callAPISuccess('Address', 'create', [
523 'contact_id' => $cid,
524 'location_type_id' => 1,
525 'street_address' => '123 Some St',
526 'city' => 'Hereville',
39b959db
SL
527 //'US',
528 'country_id' => $inputCountry,
529 // 'California',
530 'state_province_id' => $inputState,
19381641
MH
531 'postal_code' => '94100',
532 ]);
533 $created = CRM_Utils_Array::first($r['values']);
534 $this->assertEquals($expectCountry, $created['country_id']);
535 $this->assertEquals($expectState, $created['state_province_id']);
536 }
537
2d932085
CW
538 /**
539 * @param int $version
540 * @dataProvider versionThreeAndFour
541 */
542 public function testBuildStateProvinceOptionsWithDodgyProvinceLimit($version) {
543 $this->_apiversion = $version;
62d7cac4
SL
544 $provinceLimit = [1228, "abcd;ef"];
545 $this->callAPISuccess('setting', 'create', [
2d932085 546 'provinceLimit' => $provinceLimit,
62d7cac4
SL
547 ]);
548 $result = $this->callAPIFailure('address', 'getoptions', ['field' => 'state_province_id']);
549 // confirm that we hit our error not a SQLI.
550 $this->assertEquals('Province limit or default country setting is incorrect', $result['error_message']);
551 $this->callAPISuccess('setting', 'create', [
2d932085 552 'provinceLimit' => [1228],
62d7cac4
SL
553 ]);
554 // Now confirm with a correct province setting it works fine
555 $this->callAPISuccess('address', 'getoptions', ['field' => 'state_province_id']);
556 }
557
2d932085
CW
558 /**
559 * @param int $version
560 * @dataProvider versionThreeAndFour
561 */
562 public function testBuildCountryWithDodgyCountryLimitSetting($version) {
563 $this->_apiversion = $version;
62d7cac4
SL
564 $countryLimit = [1228, "abcd;ef"];
565 $this->callAPISuccess('setting', 'create', [
2d932085 566 'countryLimit' => $countryLimit,
62d7cac4
SL
567 ]);
568 $result = $this->callAPIFailure('address', 'getoptions', ['field' => 'country_id']);
569 // confirm that we hit our error not a SQLI.
570 $this->assertEquals('Available Country setting is incorrect', $result['error_message']);
571 $this->callAPISuccess('setting', 'create', [
2d932085 572 'countryLimit' => [1228],
62d7cac4
SL
573 ]);
574 // Now confirm with a correct province setting it works fine
575 $this->callAPISuccess('address', 'getoptions', ['field' => 'country_id']);
576 }
577
578 public function testBuildCountyWithDodgeStateProvinceFiltering() {
579 $result = $this->callAPIFailure('Address', 'getoptions', [
580 'field' => 'county_id',
581 'state_province_id' => "abcd;ef",
582 ]);
583 $this->assertEquals('Can only accept Integers for state_province_id filtering', $result['error_message']);
584 $goodResult = $this->callAPISuccess('Address', 'getoptions', [
585 'field' => 'county_id',
586 'state_province_id' => 1004,
587 ]);
588 $this->assertEquals('San Francisco', $goodResult['values'][4]);
589 }
590
a8fdb24e
CW
591 public function testGetOptionsAbbr() {
592 $result = $this->callAPISuccess('Address', 'getoptions', [
593 'field' => 'country_id',
594 'context' => "abbreviate",
595 ]);
596 $this->assertContains('US', $result['values']);
597 $this->assertNotContains('United States', $result['values']);
598 $result = $this->callAPISuccess('Address', 'getoptions', [
599 'field' => 'state_province_id',
600 'context' => "abbreviate",
601 ]);
602 $this->assertContains('AL', $result['values']);
603 $this->assertNotContains('Alabama', $result['values']);
604 }
605
6a488035 606}