Merge pull request #12160 from aydun/core_124_event_approval_fixes
[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-2018 |
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 // valid Street address to be parsed ( $locale = 'en_US' )
349 $street_address = "54A Excelsior Ave. Apt 1C";
350 $locale = 'en_US';
351 $parsedStreetAddress = CRM_Core_BAO_Address::parseStreetAddress($street_address, $locale);
352 $this->assertEquals($parsedStreetAddress['street_name'], 'Excelsior Ave.');
353 $this->assertEquals($parsedStreetAddress['street_unit'], 'Apt 1C');
354 $this->assertEquals($parsedStreetAddress['street_number'], '54');
355 $this->assertEquals($parsedStreetAddress['street_number_suffix'], 'A');
356
357 // invalid Street address ( $locale = 'en_US' )
358 $street_address = "West St. Apt 1";
359 $locale = 'en_US';
360 $parsedStreetAddress = CRM_Core_BAO_Address::parseStreetAddress($street_address, $locale);
361 $this->assertEquals($parsedStreetAddress['street_name'], 'West St.');
362 $this->assertEquals($parsedStreetAddress['street_unit'], 'Apt 1');
363 $this->assertNotContains('street_number', $parsedStreetAddress);
364 $this->assertNotContains('street_number_suffix', $parsedStreetAddress);
365
366 // valid Street address to be parsed ( $locale = 'fr_CA' )
367 $street_address = "2-123CA Main St";
368 $locale = 'fr_CA';
369 $parsedStreetAddress = CRM_Core_BAO_Address::parseStreetAddress($street_address, $locale);
370 $this->assertEquals($parsedStreetAddress['street_name'], 'Main St');
371 $this->assertEquals($parsedStreetAddress['street_unit'], '2');
372 $this->assertEquals($parsedStreetAddress['street_number'], '123');
373 $this->assertEquals($parsedStreetAddress['street_number_suffix'], 'CA');
374
375 // invalid Street address ( $locale = 'fr_CA' )
376 $street_address = "123 Main St";
377 $locale = 'fr_CA';
378 $parsedStreetAddress = CRM_Core_BAO_Address::parseStreetAddress($street_address, $locale);
379 $this->assertEquals($parsedStreetAddress['street_name'], 'Main St');
380 $this->assertEquals($parsedStreetAddress['street_number'], '123');
381 $this->assertNotContains('street_unit', $parsedStreetAddress);
382 $this->assertNotContains('street_number_suffix', $parsedStreetAddress);
383 }
384
385 /**
386 * @dataProvider supportedAddressParsingLocales
387 */
388 public function testIsSupportedByAddressParsingReturnTrueForSupportedLocales($locale) {
389 $isSupported = CRM_Core_BAO_Address::isSupportedParsingLocale($locale);
390 $this->assertTrue($isSupported);
391 }
392
393 /**
394 * @dataProvider supportedAddressParsingLocales
395 */
396 public function testIsSupportedByAddressParsingReturnTrueForSupportedDefaultLocales($locale) {
397 CRM_Core_Config::singleton()->lcMessages = $locale;
398 $isSupported = CRM_Core_BAO_Address::isSupportedParsingLocale();
399 $this->assertTrue($isSupported);
400
401 }
402
403 public function supportedAddressParsingLocales() {
404 return array(
405 array('en_US'),
406 array('en_CA'),
407 array('fr_CA'),
408 );
409 }
410
411 /**
412 * @dataProvider sampleOFUnsupportedAddressParsingLocales
413 */
414 public function testIsSupportedByAddressParsingReturnFalseForUnSupportedLocales($locale) {
415 $isNotSupported = CRM_Core_BAO_Address::isSupportedParsingLocale($locale);
416 $this->assertFalse($isNotSupported);
417 }
418
419 /**
420 * @dataProvider sampleOFUnsupportedAddressParsingLocales
421 */
422 public function testIsSupportedByAddressParsingReturnFalseForUnSupportedDefaultLocales($locale) {
423 CRM_Core_Config::singleton()->lcMessages = $locale;
424 $isNotSupported = CRM_Core_BAO_Address::isSupportedParsingLocale();
425 $this->assertFalse($isNotSupported);
426 }
427
428 public function sampleOFUnsupportedAddressParsingLocales() {
429 return array(
430 array('en_GB'),
431 array('af_ZA'),
432 array('da_DK'),
433 );
434 }
435
436 /**
437 * CRM-21214 - Ensure all child addresses are updated correctly - 1.
438 * 1. First, create three contacts: A, B, and C
439 * 2. Create an address for contact A
440 * 3. Use contact A's address for contact B
441 * 4. Use contact B's address for contact C
442 * 5. Change contact A's address
443 * Address of Contact C should reflect contact A's address change
444 * Also, Contact C's address' master_id should be Contact A's address id.
445 */
446 public function testSharedAddressChaining1() {
447 $contactIdA = $this->individualCreate(array(), 0);
448 $contactIdB = $this->individualCreate(array(), 1);
449 $contactIdC = $this->individualCreate(array(), 2);
450
451 $addressParamsA = array(
452 'street_address' => '123 Fake St.',
453 'location_type_id' => '1',
454 'is_primary' => '1',
455 'contact_id' => $contactIdA,
456 );
457 $addAddressA = CRM_Core_BAO_Address::add($addressParamsA, FALSE);
458
459 $addressParamsB = array(
460 'street_address' => '123 Fake St.',
461 'location_type_id' => '1',
462 'is_primary' => '1',
463 'master_id' => $addAddressA->id,
464 'contact_id' => $contactIdB,
465 );
466 $addAddressB = CRM_Core_BAO_Address::add($addressParamsB, FALSE);
467
468 $addressParamsC = array(
469 'street_address' => '123 Fake St.',
470 'location_type_id' => '1',
471 'is_primary' => '1',
472 'master_id' => $addAddressB->id,
473 'contact_id' => $contactIdC,
474 );
475 $addAddressC = CRM_Core_BAO_Address::add($addressParamsC, FALSE);
476
477 $updatedAddressParamsA = array(
478 'id' => $addAddressA->id,
479 'street_address' => '1313 New Address Lane',
480 'location_type_id' => '1',
481 'is_primary' => '1',
482 'contact_id' => $contactIdA,
483 );
484 $updatedAddressA = CRM_Core_BAO_Address::add($updatedAddressParamsA, FALSE);
485
486 // CRM-21214 - Has Address C been updated with Address A's new values?
487 $newAddressC = new CRM_Core_DAO_Address();
488 $newAddressC->id = $addAddressC->id;
489 $newAddressC->find(TRUE);
490 $newAddressC->fetch(TRUE);
491
492 $this->assertEquals($updatedAddressA->street_address, $newAddressC->street_address);
493 $this->assertEquals($updatedAddressA->id, $newAddressC->master_id);
494 }
495
496 /**
497 * CRM-21214 - Ensure all child addresses are updated correctly - 2.
498 * 1. First, create three contacts: A, B, and C
499 * 2. Create an address for contact A and B
500 * 3. Use contact A's address for contact C
501 * 4. Use contact B's address for contact A
502 * 5. Change contact B's address
503 * Address of Contact C should reflect contact B's address change
504 * Also, Contact C's address' master_id should be Contact B's address id.
505 */
506 public function testSharedAddressChaining2() {
507 $contactIdA = $this->individualCreate(array(), 0);
508 $contactIdB = $this->individualCreate(array(), 1);
509 $contactIdC = $this->individualCreate(array(), 2);
510
511 $addressParamsA = array(
512 'street_address' => '123 Fake St.',
513 'location_type_id' => '1',
514 'is_primary' => '1',
515 'contact_id' => $contactIdA,
516 );
517 $addAddressA = CRM_Core_BAO_Address::add($addressParamsA, FALSE);
518
519 $addressParamsB = array(
520 'street_address' => '123 Fake St.',
521 'location_type_id' => '1',
522 'is_primary' => '1',
523 'contact_id' => $contactIdB,
524 );
525 $addAddressB = CRM_Core_BAO_Address::add($addressParamsB, FALSE);
526
527 $addressParamsC = array(
528 'street_address' => '123 Fake St.',
529 'location_type_id' => '1',
530 'is_primary' => '1',
531 'master_id' => $addAddressA->id,
532 'contact_id' => $contactIdC,
533 );
534 $addAddressC = CRM_Core_BAO_Address::add($addressParamsC, FALSE);
535
536 $updatedAddressParamsA = array(
537 'id' => $addAddressA->id,
538 'street_address' => '123 Fake St.',
539 'location_type_id' => '1',
540 'is_primary' => '1',
541 'master_id' => $addAddressB->id,
542 'contact_id' => $contactIdA,
543 );
544 $updatedAddressA = CRM_Core_BAO_Address::add($updatedAddressParamsA, FALSE);
545
546 $updatedAddressParamsB = array(
547 'id' => $addAddressB->id,
548 'street_address' => '1313 New Address Lane',
549 'location_type_id' => '1',
550 'is_primary' => '1',
551 'contact_id' => $contactIdB,
552 );
553 $updatedAddressB = CRM_Core_BAO_Address::add($updatedAddressParamsB, FALSE);
554
555 // CRM-21214 - Has Address C been updated with Address B's new values?
556 $newAddressC = new CRM_Core_DAO_Address();
557 $newAddressC->id = $addAddressC->id;
558 $newAddressC->find(TRUE);
559 $newAddressC->fetch(TRUE);
560
561 $this->assertEquals($updatedAddressB->street_address, $newAddressC->street_address);
562 $this->assertEquals($updatedAddressB->id, $newAddressC->master_id);
563 }
564
565 /**
566 * CRM-21214 - Ensure all child addresses are updated correctly - 3.
567 * 1. First, create a contact: A
568 * 2. Create an address for contact A
569 * 3. Use contact A's address for contact A's address
570 * An error should be given, and master_id should remain the same.
571 */
572 public function testSharedAddressChaining3() {
573 $contactIdA = $this->individualCreate(array(), 0);
574
575 $addressParamsA = array(
576 'street_address' => '123 Fake St.',
577 'location_type_id' => '1',
578 'is_primary' => '1',
579 'contact_id' => $contactIdA,
580 );
581 $addAddressA = CRM_Core_BAO_Address::add($addressParamsA, FALSE);
582
583 $updatedAddressParamsA = array(
584 'id' => $addAddressA->id,
585 'street_address' => '123 Fake St.',
586 'location_type_id' => '1',
587 'is_primary' => '1',
588 'master_id' => $addAddressA->id,
589 'contact_id' => $contactIdA,
590 );
591 $updatedAddressA = CRM_Core_BAO_Address::add($updatedAddressParamsA, FALSE);
592
593 // CRM-21214 - AdressA shouldn't be master of itself.
594 $this->assertEmpty($updatedAddressA->master_id);
595 }
596
597 }