Merge pull request #13730 from civicrm/5.11
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / AddressTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class CRM_Core_BAO_AddressTest
30 * @group headless
31 */
32 class CRM_Core_BAO_AddressTest extends CiviUnitTestCase {
33 public function setUp() {
34 parent::setUp();
35
36 $this->quickCleanup(array('civicrm_contact', 'civicrm_address'));
37 }
38
39 /**
40 * Create() method (create and update modes)
41 */
42 public function testCreate() {
43 $contactId = $this->individualCreate();
44
45 $params = array();
46 $params['address']['1'] = array(
47 'street_address' => 'Oberoi Garden',
48 'supplemental_address_1' => 'Attn: Accounting',
49 'supplemental_address_2' => 'Powai',
50 'supplemental_address_3' => 'Somewhere',
51 'city' => 'Athens',
52 'postal_code' => '01903',
53 'state_province_id' => '1000',
54 'country_id' => '1228',
55 'geo_code_1' => '18.219023',
56 'geo_code_2' => '-105.00973',
57 'location_type_id' => '1',
58 'is_primary' => '1',
59 'is_billing' => '0',
60 );
61
62 $params['contact_id'] = $contactId;
63
64 $fixAddress = TRUE;
65
66 CRM_Core_BAO_Address::create($params, $fixAddress, $entity = NULL);
67 $addressId = $this->assertDBNotNull('CRM_Core_DAO_Address', 'Oberoi Garden', 'id', 'street_address',
68 'Database check for created address.'
69 );
70
71 // Now call add() to modify an existing address
72
73 $params = array();
74 $params['address']['1'] = array(
75 'id' => $addressId,
76 'street_address' => '120 Terminal Road',
77 'supplemental_address_1' => 'A-wing:3037',
78 'supplemental_address_2' => 'Bandra',
79 'supplemental_address_3' => 'Somewhere',
80 'city' => 'Athens',
81 'postal_code' => '01903',
82 'state_province_id' => '1000',
83 'country_id' => '1228',
84 'geo_code_1' => '18.219023',
85 'geo_code_2' => '-105.00973',
86 'location_type_id' => '1',
87 'is_primary' => '1',
88 'is_billing' => '0',
89 );
90 $params['contact_id'] = $contactId;
91
92 $block = CRM_Core_BAO_Address::create($params, $fixAddress, $entity = NULL);
93
94 $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
95 'Database check for updated address by contactId.'
96 );
97 $this->assertDBNotNull('CRM_Core_DAO_Address', '120 Terminal Road', 'id', 'street_address',
98 'Database check for updated address by street_name.'
99 );
100 $this->contactDelete($contactId);
101 }
102
103 /**
104 * Add() method ( )
105 */
106 public function testAdd() {
107 $contactId = $this->individualCreate();
108
109 $fixParams = array(
110 'street_address' => 'E 906N Pine Pl W',
111 'supplemental_address_1' => 'Editorial Dept',
112 'supplemental_address_2' => '',
113 'supplemental_address_3' => '',
114 'city' => 'El Paso',
115 'postal_code' => '88575',
116 'postal_code_suffix' => '',
117 'state_province_id' => '1001',
118 'country_id' => '1228',
119 'geo_code_1' => '31.694842',
120 'geo_code_2' => '-106.29998',
121 'location_type_id' => '1',
122 'is_primary' => '1',
123 'is_billing' => '0',
124 'contact_id' => $contactId,
125 );
126
127 $addAddress = CRM_Core_BAO_Address::add($fixParams, $fixAddress = TRUE);
128
129 $addParams = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
130 'Database check for created contact address.'
131 );
132
133 $this->assertEquals($addAddress->street_address, 'E 906N Pine Pl W', 'In line' . __LINE__);
134 $this->assertEquals($addAddress->supplemental_address_1, 'Editorial Dept', 'In line' . __LINE__);
135 $this->assertEquals($addAddress->city, 'El Paso', 'In line' . __LINE__);
136 $this->assertEquals($addAddress->postal_code, '88575', 'In line' . __LINE__);
137 $this->assertEquals($addAddress->geo_code_1, '31.694842', 'In line' . __LINE__);
138 $this->assertEquals($addAddress->geo_code_2, '-106.29998', 'In line' . __LINE__);
139 $this->assertEquals($addAddress->country_id, '1228', 'In line' . __LINE__);
140 $this->contactDelete($contactId);
141 }
142
143 /**
144 * AllAddress() method ( )
145 */
146 public function testallAddress() {
147 $contactId = $this->individualCreate();
148
149 $fixParams = array(
150 'street_address' => 'E 906N Pine Pl W',
151 'supplemental_address_1' => 'Editorial Dept',
152 'supplemental_address_2' => '',
153 'supplemental_address_3' => '',
154 'city' => 'El Paso',
155 'postal_code' => '88575',
156 'postal_code_suffix' => '',
157 'state_province_id' => '1001',
158 'country_id' => '1228',
159 'geo_code_1' => '31.694842',
160 'geo_code_2' => '-106.29998',
161 'location_type_id' => '1',
162 'is_primary' => '1',
163 'is_billing' => '0',
164 'contact_id' => $contactId,
165 );
166
167 CRM_Core_BAO_Address::add($fixParams, $fixAddress = TRUE);
168
169 $addParams = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
170 'Database check for created contact address.'
171 );
172 $fixParams = array(
173 'street_address' => 'SW 719B Beech Dr NW',
174 'supplemental_address_1' => 'C/o OPDC',
175 'supplemental_address_2' => '',
176 'supplemental_address_3' => '',
177 'city' => 'Neillsville',
178 'postal_code' => '54456',
179 'postal_code_suffix' => '',
180 'state_province_id' => '1001',
181 'country_id' => '1228',
182 'geo_code_1' => '44.553719',
183 'geo_code_2' => '-90.61457',
184 'location_type_id' => '2',
185 'is_primary' => '',
186 'is_billing' => '1',
187 'contact_id' => $contactId,
188 );
189
190 CRM_Core_BAO_Address::add($fixParams, $fixAddress = TRUE);
191
192 $addParams = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
193 'Database check for created contact address.'
194 );
195
196 $allAddress = CRM_Core_BAO_Address::allAddress($contactId);
197
198 $this->assertEquals(count($allAddress), 2, 'Checking number of returned addresses.');
199
200 $this->contactDelete($contactId);
201 }
202
203 /**
204 * AllAddress() method ( ) with null value
205 */
206 public function testnullallAddress() {
207 $contactId = $this->individualCreate();
208
209 $fixParams = array(
210 'street_address' => 'E 906N Pine Pl W',
211 'supplemental_address_1' => 'Editorial Dept',
212 'supplemental_address_2' => '',
213 'supplemental_address_3' => '',
214 'city' => 'El Paso',
215 'postal_code' => '88575',
216 'postal_code_suffix' => '',
217 'state_province_id' => '1001',
218 'country_id' => '1228',
219 'geo_code_1' => '31.694842',
220 'geo_code_2' => '-106.29998',
221 'location_type_id' => '1',
222 'is_primary' => '1',
223 'is_billing' => '0',
224 'contact_id' => $contactId,
225 );
226
227 CRM_Core_BAO_Address::add($fixParams, $fixAddress = TRUE);
228
229 $addParams = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
230 'Database check for created contact address.'
231 );
232
233 $contact_Id = NULL;
234
235 $allAddress = CRM_Core_BAO_Address::allAddress($contact_Id);
236
237 $this->assertEquals($allAddress, NULL, 'Checking null for returned addresses.');
238
239 $this->contactDelete($contactId);
240 }
241
242 /**
243 * GetValues() method (get Address fields)
244 */
245 public function testGetValues() {
246 $contactId = $this->individualCreate();
247
248 $params = array();
249 $params['address']['1'] = array(
250 'street_address' => 'Oberoi Garden',
251 'supplemental_address_1' => 'Attn: Accounting',
252 'supplemental_address_2' => 'Powai',
253 'supplemental_address_3' => 'Somewhere',
254 'city' => 'Athens',
255 'postal_code' => '01903',
256 'state_province_id' => '1000',
257 'country_id' => '1228',
258 'geo_code_1' => '18.219023',
259 'geo_code_2' => '-105.00973',
260 'location_type_id' => '1',
261 'is_primary' => '1',
262 'is_billing' => '0',
263 );
264
265 $params['contact_id'] = $contactId;
266
267 $fixAddress = TRUE;
268
269 CRM_Core_BAO_Address::create($params, $fixAddress, $entity = NULL);
270
271 $addressId = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
272 'Database check for created address.'
273 );
274
275 $entityBlock = array('contact_id' => $contactId);
276 $address = CRM_Core_BAO_Address::getValues($entityBlock);
277 $this->assertEquals($address[1]['id'], $addressId);
278 $this->assertEquals($address[1]['contact_id'], $contactId);
279 $this->assertEquals($address[1]['street_address'], 'Oberoi Garden');
280 $this->contactDelete($contactId);
281 }
282
283 public function setStreetAddressParsing($status) {
284 $address_options = CRM_Core_BAO_Setting::valueOptions(
285 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
286 'address_options',
287 TRUE, NULL, TRUE
288 );
289 if ($status) {
290 $value = 1;
291 }
292 else {
293 $value = 0;
294 }
295 $address_options['street_address_parsing'] = $value;
296 CRM_Core_BAO_Setting::setValueOption(
297 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
298 'address_options',
299 $address_options
300 );
301 }
302
303 /**
304 * ParseStreetAddress if enabled, otherwise, don't.
305 */
306 public function testParseStreetAddressIfEnabled() {
307 // Turn off address standardization. Parsing should work without it.
308 Civi::settings()->set('address_standardization_provider', NULL);
309
310 // Ensure street parsing happens if enabled.
311 $this->setStreetAddressParsing(TRUE);
312
313 $contactId = $this->individualCreate();
314 $street_address = "54 Excelsior Ave.";
315 $params = array(
316 'contact_id' => $contactId,
317 'street_address' => $street_address,
318 'location_type_id' => 1,
319 );
320
321 $result = civicrm_api3('Address', 'create', $params);
322 $value = array_pop($result['values']);
323 $street_number = CRM_Utils_Array::value('street_number', $value);
324 $this->assertEquals($street_number, '54');
325
326 // Ensure street parsing does not happen if disabled.
327 $this->setStreetAddressParsing(FALSE);
328 $result = civicrm_api3('Address', 'create', $params);
329 $value = array_pop($result['values']);
330 $street_number = CRM_Utils_Array::value('street_number', $value);
331 $this->assertEmpty($street_number);
332
333 }
334
335 /**
336 * ParseStreetAddress() method (get street address parsed)
337 */
338 public function testParseStreetAddress() {
339
340 // valid Street address to be parsed ( without locale )
341 $street_address = "54A Excelsior Ave. Apt 1C";
342 $parsedStreetAddress = CRM_Core_BAO_Address::parseStreetAddress($street_address);
343 $this->assertEquals($parsedStreetAddress['street_name'], 'Excelsior Ave.');
344 $this->assertEquals($parsedStreetAddress['street_unit'], 'Apt 1C');
345 $this->assertEquals($parsedStreetAddress['street_number'], '54');
346 $this->assertEquals($parsedStreetAddress['street_number_suffix'], 'A');
347
348 // Out-of-range street number to be parsed.
349 $street_address = "505050505050 Main St";
350 $parsedStreetAddress = CRM_Core_BAO_Address::parseStreetAddress($street_address);
351 $this->assertEquals($parsedStreetAddress['street_name'], '');
352 $this->assertEquals($parsedStreetAddress['street_unit'], '');
353 $this->assertEquals($parsedStreetAddress['street_number'], '');
354 $this->assertEquals($parsedStreetAddress['street_number_suffix'], '');
355
356 // valid Street address to be parsed ( $locale = 'en_US' )
357 $street_address = "54A Excelsior Ave. Apt 1C";
358 $locale = 'en_US';
359 $parsedStreetAddress = CRM_Core_BAO_Address::parseStreetAddress($street_address, $locale);
360 $this->assertEquals($parsedStreetAddress['street_name'], 'Excelsior Ave.');
361 $this->assertEquals($parsedStreetAddress['street_unit'], 'Apt 1C');
362 $this->assertEquals($parsedStreetAddress['street_number'], '54');
363 $this->assertEquals($parsedStreetAddress['street_number_suffix'], 'A');
364
365 // invalid Street address ( $locale = 'en_US' )
366 $street_address = "West St. Apt 1";
367 $locale = 'en_US';
368 $parsedStreetAddress = CRM_Core_BAO_Address::parseStreetAddress($street_address, $locale);
369 $this->assertEquals($parsedStreetAddress['street_name'], 'West St.');
370 $this->assertEquals($parsedStreetAddress['street_unit'], 'Apt 1');
371 $this->assertNotContains('street_number', $parsedStreetAddress);
372 $this->assertNotContains('street_number_suffix', $parsedStreetAddress);
373
374 // valid Street address to be parsed ( $locale = 'fr_CA' )
375 $street_address = "2-123CA Main St";
376 $locale = 'fr_CA';
377 $parsedStreetAddress = CRM_Core_BAO_Address::parseStreetAddress($street_address, $locale);
378 $this->assertEquals($parsedStreetAddress['street_name'], 'Main St');
379 $this->assertEquals($parsedStreetAddress['street_unit'], '2');
380 $this->assertEquals($parsedStreetAddress['street_number'], '123');
381 $this->assertEquals($parsedStreetAddress['street_number_suffix'], 'CA');
382
383 // invalid Street address ( $locale = 'fr_CA' )
384 $street_address = "123 Main St";
385 $locale = 'fr_CA';
386 $parsedStreetAddress = CRM_Core_BAO_Address::parseStreetAddress($street_address, $locale);
387 $this->assertEquals($parsedStreetAddress['street_name'], 'Main St');
388 $this->assertEquals($parsedStreetAddress['street_number'], '123');
389 $this->assertNotContains('street_unit', $parsedStreetAddress);
390 $this->assertNotContains('street_number_suffix', $parsedStreetAddress);
391 }
392
393 /**
394 * @dataProvider supportedAddressParsingLocales
395 */
396 public function testIsSupportedByAddressParsingReturnTrueForSupportedLocales($locale) {
397 $isSupported = CRM_Core_BAO_Address::isSupportedParsingLocale($locale);
398 $this->assertTrue($isSupported);
399 }
400
401 /**
402 * @dataProvider supportedAddressParsingLocales
403 */
404 public function testIsSupportedByAddressParsingReturnTrueForSupportedDefaultLocales($locale) {
405 CRM_Core_Config::singleton()->lcMessages = $locale;
406 $isSupported = CRM_Core_BAO_Address::isSupportedParsingLocale();
407 $this->assertTrue($isSupported);
408
409 }
410
411 public function supportedAddressParsingLocales() {
412 return array(
413 array('en_US'),
414 array('en_CA'),
415 array('fr_CA'),
416 );
417 }
418
419 /**
420 * @dataProvider sampleOFUnsupportedAddressParsingLocales
421 */
422 public function testIsSupportedByAddressParsingReturnFalseForUnSupportedLocales($locale) {
423 $isNotSupported = CRM_Core_BAO_Address::isSupportedParsingLocale($locale);
424 $this->assertFalse($isNotSupported);
425 }
426
427 /**
428 * @dataProvider sampleOFUnsupportedAddressParsingLocales
429 */
430 public function testIsSupportedByAddressParsingReturnFalseForUnSupportedDefaultLocales($locale) {
431 CRM_Core_Config::singleton()->lcMessages = $locale;
432 $isNotSupported = CRM_Core_BAO_Address::isSupportedParsingLocale();
433 $this->assertFalse($isNotSupported);
434 }
435
436 public function sampleOFUnsupportedAddressParsingLocales() {
437 return array(
438 array('en_GB'),
439 array('af_ZA'),
440 array('da_DK'),
441 );
442 }
443
444 /**
445 * CRM-21214 - Ensure all child addresses are updated correctly - 1.
446 * 1. First, create three contacts: A, B, and C
447 * 2. Create an address for contact A
448 * 3. Use contact A's address for contact B
449 * 4. Use contact B's address for contact C
450 * 5. Change contact A's address
451 * Address of Contact C should reflect contact A's address change
452 * Also, Contact C's address' master_id should be Contact A's address id.
453 */
454 public function testSharedAddressChaining1() {
455 $contactIdA = $this->individualCreate(array(), 0);
456 $contactIdB = $this->individualCreate(array(), 1);
457 $contactIdC = $this->individualCreate(array(), 2);
458
459 $addressParamsA = array(
460 'street_address' => '123 Fake St.',
461 'location_type_id' => '1',
462 'is_primary' => '1',
463 'contact_id' => $contactIdA,
464 );
465 $addAddressA = CRM_Core_BAO_Address::add($addressParamsA, FALSE);
466
467 $addressParamsB = array(
468 'street_address' => '123 Fake St.',
469 'location_type_id' => '1',
470 'is_primary' => '1',
471 'master_id' => $addAddressA->id,
472 'contact_id' => $contactIdB,
473 );
474 $addAddressB = CRM_Core_BAO_Address::add($addressParamsB, FALSE);
475
476 $addressParamsC = array(
477 'street_address' => '123 Fake St.',
478 'location_type_id' => '1',
479 'is_primary' => '1',
480 'master_id' => $addAddressB->id,
481 'contact_id' => $contactIdC,
482 );
483 $addAddressC = CRM_Core_BAO_Address::add($addressParamsC, FALSE);
484
485 $updatedAddressParamsA = array(
486 'id' => $addAddressA->id,
487 'street_address' => '1313 New Address Lane',
488 'location_type_id' => '1',
489 'is_primary' => '1',
490 'contact_id' => $contactIdA,
491 );
492 $updatedAddressA = CRM_Core_BAO_Address::add($updatedAddressParamsA, FALSE);
493
494 // CRM-21214 - Has Address C been updated with Address A's new values?
495 $newAddressC = new CRM_Core_DAO_Address();
496 $newAddressC->id = $addAddressC->id;
497 $newAddressC->find(TRUE);
498 $newAddressC->fetch(TRUE);
499
500 $this->assertEquals($updatedAddressA->street_address, $newAddressC->street_address);
501 $this->assertEquals($updatedAddressA->id, $newAddressC->master_id);
502 }
503
504 /**
505 * CRM-21214 - Ensure all child addresses are updated correctly - 2.
506 * 1. First, create three contacts: A, B, and C
507 * 2. Create an address for contact A and B
508 * 3. Use contact A's address for contact C
509 * 4. Use contact B's address for contact A
510 * 5. Change contact B's address
511 * Address of Contact C should reflect contact B's address change
512 * Also, Contact C's address' master_id should be Contact B's address id.
513 */
514 public function testSharedAddressChaining2() {
515 $contactIdA = $this->individualCreate(array(), 0);
516 $contactIdB = $this->individualCreate(array(), 1);
517 $contactIdC = $this->individualCreate(array(), 2);
518
519 $addressParamsA = array(
520 'street_address' => '123 Fake St.',
521 'location_type_id' => '1',
522 'is_primary' => '1',
523 'contact_id' => $contactIdA,
524 );
525 $addAddressA = CRM_Core_BAO_Address::add($addressParamsA, FALSE);
526
527 $addressParamsB = array(
528 'street_address' => '123 Fake St.',
529 'location_type_id' => '1',
530 'is_primary' => '1',
531 'contact_id' => $contactIdB,
532 );
533 $addAddressB = CRM_Core_BAO_Address::add($addressParamsB, FALSE);
534
535 $addressParamsC = array(
536 'street_address' => '123 Fake St.',
537 'location_type_id' => '1',
538 'is_primary' => '1',
539 'master_id' => $addAddressA->id,
540 'contact_id' => $contactIdC,
541 );
542 $addAddressC = CRM_Core_BAO_Address::add($addressParamsC, FALSE);
543
544 $updatedAddressParamsA = array(
545 'id' => $addAddressA->id,
546 'street_address' => '123 Fake St.',
547 'location_type_id' => '1',
548 'is_primary' => '1',
549 'master_id' => $addAddressB->id,
550 'contact_id' => $contactIdA,
551 );
552 $updatedAddressA = CRM_Core_BAO_Address::add($updatedAddressParamsA, FALSE);
553
554 $updatedAddressParamsB = array(
555 'id' => $addAddressB->id,
556 'street_address' => '1313 New Address Lane',
557 'location_type_id' => '1',
558 'is_primary' => '1',
559 'contact_id' => $contactIdB,
560 );
561 $updatedAddressB = CRM_Core_BAO_Address::add($updatedAddressParamsB, FALSE);
562
563 // CRM-21214 - Has Address C been updated with Address B's new values?
564 $newAddressC = new CRM_Core_DAO_Address();
565 $newAddressC->id = $addAddressC->id;
566 $newAddressC->find(TRUE);
567 $newAddressC->fetch(TRUE);
568
569 $this->assertEquals($updatedAddressB->street_address, $newAddressC->street_address);
570 $this->assertEquals($updatedAddressB->id, $newAddressC->master_id);
571 }
572
573 /**
574 * CRM-21214 - Ensure all child addresses are updated correctly - 3.
575 * 1. First, create a contact: A
576 * 2. Create an address for contact A
577 * 3. Use contact A's address for contact A's address
578 * An error should be given, and master_id should remain the same.
579 */
580 public function testSharedAddressChaining3() {
581 $contactIdA = $this->individualCreate(array(), 0);
582
583 $addressParamsA = array(
584 'street_address' => '123 Fake St.',
585 'location_type_id' => '1',
586 'is_primary' => '1',
587 'contact_id' => $contactIdA,
588 );
589 $addAddressA = CRM_Core_BAO_Address::add($addressParamsA, FALSE);
590
591 $updatedAddressParamsA = array(
592 'id' => $addAddressA->id,
593 'street_address' => '123 Fake St.',
594 'location_type_id' => '1',
595 'is_primary' => '1',
596 'master_id' => $addAddressA->id,
597 'contact_id' => $contactIdA,
598 );
599 $updatedAddressA = CRM_Core_BAO_Address::add($updatedAddressParamsA, FALSE);
600
601 // CRM-21214 - AdressA shouldn't be master of itself.
602 $this->assertEmpty($updatedAddressA->master_id);
603 }
604
605 }