Merge pull request #14082 from pradpnayak/caseReport
[civicrm-core.git] / tests / phpunit / CRM / Contact / Import / Parser / ContactTest.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 * @file
30 * File for the CRM_Contact_Imports_Parser_ContactTest class.
31 */
32
33 /**
34 * Test contact import parser.
35 *
36 * @package CiviCRM
37 * @group headless
38 */
39 class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
40 protected $_tablesToTruncate = ['civicrm_address', 'civicrm_phone', 'civicrm_email'];
41
42 /**
43 * Setup function.
44 */
45 public function setUp() {
46 parent::setUp();
47 }
48
49 /**
50 * Test that import parser will add contact with employee of relationship.
51 *
52 * @throws \Exception
53 */
54 public function testImportParserWtihEmployeeOfRelationship() {
55 $this->organizationCreate(array(
56 "organization_name" => "Agileware",
57 "legal_name" => "Agileware",
58 ));
59 $contactImportValues = array(
60 "first_name" => "Alok",
61 "last_name" => "Patel",
62 "Employee of" => "Agileware",
63 );
64
65 $fields = array_keys($contactImportValues);
66 $values = array_values($contactImportValues);
67 $parser = new CRM_Contact_Import_Parser_Contact($fields, []);
68 $parser->_contactType = 'Individual';
69 $parser->init();
70 $this->mapRelationshipFields($fields, $parser->getAllFields());
71
72 $parser = new CRM_Contact_Import_Parser_Contact($fields, [], [], [], array(
73 NULL,
74 NULL,
75 $fields[2],
76 ), array(
77 NULL,
78 NULL,
79 "Organization",
80 ), array(
81 NULL,
82 NULL,
83 "organization_name",
84 ), [], [], [], [], []);
85
86 $parser->_contactType = 'Individual';
87 $parser->_onDuplicate = CRM_Import_Parser::DUPLICATE_UPDATE;
88 $parser->init();
89
90 $this->assertEquals(CRM_Import_Parser::VALID, $parser->import(CRM_Import_Parser::DUPLICATE_UPDATE, $values), 'Return code from parser import was not as expected');
91 $this->callAPISuccess("Contact", "get", array(
92 "first_name" => "Alok",
93 "last_name" => "Patel",
94 "organization_name" => "Agileware",
95 ));
96 }
97
98 /**
99 * Test that import parser will not fail when same external_identifier found of deleted contact.
100 *
101 * @throws \Exception
102 */
103 public function testImportParserWtihDeletedContactExternalIdentifier() {
104 $contactId = $this->individualCreate(array(
105 "external_identifier" => "ext-1",
106 ));
107 CRM_Contact_BAO_Contact::deleteContact($contactId);
108 list($originalValues, $result) = $this->setUpBaseContact(array(
109 'external_identifier' => 'ext-1',
110 ));
111 $originalValues['nick_name'] = 'Old Bill';
112 $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
113 $originalValues['id'] = $result['id'];
114 $this->assertEquals('ext-1', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'external_identifier')));
115 $this->callAPISuccessGetSingle('Contact', $originalValues);
116 }
117
118 /**
119 * Test import parser will update based on a rule match.
120 *
121 * In this case the contact has no external identifier.
122 *
123 * @throws \Exception
124 */
125 public function testImportParserWithUpdateWithoutExternalIdentifier() {
126 list($originalValues, $result) = $this->setUpBaseContact();
127 $originalValues['nick_name'] = 'Old Bill';
128 $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
129 $originalValues['id'] = $result['id'];
130 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
131 $this->callAPISuccessGetSingle('Contact', $originalValues);
132 }
133
134 /**
135 * Test import parser will update contacts with an external identifier.
136 *
137 * This is the basic test where the identifier matches the import parameters.
138 *
139 * @throws \Exception
140 */
141 public function testImportParserWithUpdateWithExternalIdentifier() {
142 list($originalValues, $result) = $this->setUpBaseContact(array('external_identifier' => 'windows'));
143
144 $this->assertEquals($result['id'], CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', 'windows', 'id', 'external_identifier', TRUE));
145 $this->assertEquals('windows', $result['external_identifier']);
146
147 $originalValues['nick_name'] = 'Old Bill';
148 $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
149 $originalValues['id'] = $result['id'];
150
151 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
152 $this->callAPISuccessGetSingle('Contact', $originalValues);
153 }
154
155 /**
156 * Test import parser will fallback to external identifier.
157 *
158 * In this case no primary match exists (e.g the details are not supplied) so it falls back on external identifier.
159 *
160 * CRM-17275
161 *
162 * @throws \Exception
163 */
164 public function testImportParserWithUpdateWithExternalIdentifierButNoPrimaryMatch() {
165 list($originalValues, $result) = $this->setUpBaseContact(array(
166 'external_identifier' => 'windows',
167 'email' => NULL,
168 ));
169
170 $this->assertEquals('windows', $result['external_identifier']);
171
172 $originalValues['nick_name'] = 'Old Bill';
173 $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
174 $originalValues['id'] = $result['id'];
175
176 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
177 $this->callAPISuccessGetSingle('Contact', $originalValues);
178 }
179
180 /**
181 * Test that the import parser adds the external identifier where none is set.
182 *
183 * @throws \Exception
184 */
185 public function testImportParserWithUpdateWithNoExternalIdentifier() {
186 list($originalValues, $result) = $this->setUpBaseContact();
187 $originalValues['nick_name'] = 'Old Bill';
188 $originalValues['external_identifier'] = 'windows';
189 $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
190 $originalValues['id'] = $result['id'];
191 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
192 $this->callAPISuccessGetSingle('Contact', $originalValues);
193 }
194
195 /**
196 * Test that the import parser changes the external identifier when there is a dedupe match.
197 *
198 * @throws \Exception
199 */
200 public function testImportParserWithUpdateWithChangedExternalIdentifier() {
201 list($contactValues, $result) = $this->setUpBaseContact(array('external_identifier' => 'windows'));
202 $contact_id = $result['id'];
203 $contactValues['nick_name'] = 'Old Bill';
204 $contactValues['external_identifier'] = 'android';
205 $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
206 $contactValues['id'] = $contact_id;
207 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $contact_id, 'return' => 'nick_name')));
208 $this->callAPISuccessGetSingle('Contact', $contactValues);
209 }
210
211 /**
212 * Test that the import parser adds the address to the right location.
213 *
214 * @throws \Exception
215 */
216 public function testImportBillingAddress() {
217 list($contactValues) = $this->setUpBaseContact();
218 $contactValues['nick_name'] = 'Old Bill';
219 $contactValues['external_identifier'] = 'android';
220 $contactValues['street_address'] = 'Big Mansion';
221 $contactValues['phone'] = '911';
222 $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 2, 6 => 2));
223 $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion'));
224 $this->assertEquals(2, $address['location_type_id']);
225
226 $phone = $this->callAPISuccessGetSingle('Phone', array('phone' => '911'));
227 $this->assertEquals(2, $phone['location_type_id']);
228
229 $contact = $this->callAPISuccessGetSingle('Contact', $contactValues);
230 $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id']));
231 }
232
233 /**
234 * Test that the import parser adds the address to the primary location.
235 *
236 * @throws \Exception
237 */
238 public function testImportPrimaryAddress() {
239 list($contactValues) = $this->setUpBaseContact();
240 $contactValues['nick_name'] = 'Old Bill';
241 $contactValues['external_identifier'] = 'android';
242 $contactValues['street_address'] = 'Big Mansion';
243 $contactValues['phone'] = 12334;
244 $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => 'Primary', 3 => NULL, 4 => NULL, 5 => 'Primary', 6 => 'Primary'));
245 $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion'));
246 $this->assertEquals(1, $address['location_type_id']);
247 $this->assertEquals(1, $address['is_primary']);
248
249 $phone = $this->callAPISuccessGetSingle('Phone', array('phone' => '12334'));
250 $this->assertEquals(1, $phone['location_type_id']);
251
252 $this->callAPISuccessGetSingle('Email', array('email' => 'bill.gates@microsoft.com'));
253
254 $contact = $this->callAPISuccessGetSingle('Contact', $contactValues);
255 $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id']));
256 }
257
258 /**
259 * Test that address custom fields can be imported
260 */
261 public function testAddressWithCustomData() {
262 $ids = $this->entityCustomGroupWithSingleFieldCreate('Address', 'AddressTest.php');
263 list($contactValues) = $this->setUpBaseContact();
264 $contactValues['nick_name'] = 'Old Bill';
265 $contactValues['external_identifier'] = 'android';
266 $contactValues['street_address'] = 'Big Mansion';
267 $contactValues['custom_' . $ids['custom_field_id']] = 'Update';
268 $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 'Primary', 6 => 'Primary'));
269 $address = $this->callAPISuccessGetSingle('Address', ['street_address' => 'Big Mansion', 'return' => 'custom_' . $ids['custom_field_id']]);
270 $this->assertEquals('Update', $address['custom_' . $ids['custom_field_id']]);
271 }
272
273 /**
274 * Test that the import parser adds the address to the primary location.
275 *
276 * @throws \Exception
277 */
278 public function testImportDeceased() {
279 list($contactValues) = $this->setUpBaseContact();
280 CRM_Core_Session::singleton()->set("dateTypes", 1);
281 $contactValues['birth_date'] = '1910-12-17';
282 $contactValues['deceased_date'] = '2010-12-17';
283 $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
284 $contact = $this->callAPISuccessGetSingle('Contact', $contactValues);
285 $this->assertEquals('1910-12-17', $contact['birth_date']);
286 $this->assertEquals('2010-12-17', $contact['deceased_date']);
287 $this->assertEquals(1, $contact['is_deceased']);
288 $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id']));
289 }
290
291 /**
292 * Test that the import parser adds the address to the primary location.
293 *
294 * @throws \Exception
295 */
296 public function testImportTwoAddressFirstPrimary() {
297 list($contactValues) = $this->setUpBaseContact();
298 $contactValues['nick_name'] = 'Old Bill';
299 $contactValues['external_identifier'] = 'android';
300 $contactValues['street_address'] = 'Big Mansion';
301 $contactValues['phone'] = 12334;
302 $fields = array_keys($contactValues);
303 $contactValues['street_address_2'] = 'Teeny Mansion';
304 $contactValues['phone_2'] = 4444;
305 $fields[] = 'street_address';
306 $fields[] = 'phone';
307 $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 'Primary', 6 => 'Primary', 7 => 3, 8 => 3), $fields);
308 $contact = $this->callAPISuccessGetSingle('Contact', array('external_identifier' => 'android'));
309 $address = $this->callAPISuccess('Address', 'get', array('contact_id' => $contact['id'], 'sequential' => 1));
310
311 $this->assertEquals(3, $address['values'][0]['location_type_id']);
312 $this->assertEquals(0, $address['values'][0]['is_primary']);
313 $this->assertEquals('Teeny Mansion', $address['values'][0]['street_address']);
314
315 $this->assertEquals(1, $address['values'][1]['location_type_id']);
316 $this->assertEquals(1, $address['values'][1]['is_primary']);
317 $this->assertEquals('Big Mansion', $address['values'][1]['street_address']);
318
319 $phone = $this->callAPISuccess('Phone', 'get', array('contact_id' => $contact['id'], 'sequential' => 1));
320 $this->assertEquals(1, $phone['values'][0]['location_type_id']);
321 $this->assertEquals(1, $phone['values'][0]['is_primary']);
322 $this->assertEquals(12334, $phone['values'][0]['phone']);
323 $this->assertEquals(3, $phone['values'][1]['location_type_id']);
324 $this->assertEquals(0, $phone['values'][1]['is_primary']);
325 $this->assertEquals(4444, $phone['values'][1]['phone']);
326
327 $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id']));
328 }
329
330 /**
331 * Test that the import parser adds the address to the primary location.
332 *
333 * @throws \Exception
334 */
335 public function testImportTwoAddressSecondPrimary() {
336 list($contactValues) = $this->setUpBaseContact();
337 $contactValues['nick_name'] = 'Old Bill';
338 $contactValues['external_identifier'] = 'android';
339 $contactValues['street_address'] = 'Big Mansion';
340 $contactValues['phone'] = 12334;
341 $fields = array_keys($contactValues);
342 $contactValues['street_address_2'] = 'Teeny Mansion';
343 $contactValues['phone_2'] = 4444;
344 $fields[] = 'street_address';
345 $fields[] = 'phone';
346 $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 3, 6 => 3, 7 => 'Primary', 8 => 'Primary'), $fields);
347 $contact = $this->callAPISuccessGetSingle('Contact', array('external_identifier' => 'android'));
348 $address = $this->callAPISuccess('Address', 'get', array('contact_id' => $contact['id'], 'sequential' => 1))['values'];
349
350 $this->assertEquals(1, $address[1]['location_type_id']);
351 $this->assertEquals(1, $address[1]['is_primary']);
352 $this->assertEquals('Teeny Mansion', $address[1]['street_address']);
353
354 $this->assertEquals(3, $address[0]['location_type_id']);
355 $this->assertEquals(0, $address[0]['is_primary']);
356 $this->assertEquals('Big Mansion', $address[0]['street_address']);
357
358 $phone = $this->callAPISuccess('Phone', 'get', ['contact_id' => $contact['id'], 'sequential' => 1, 'options' => ['sort' => 'is_primary DESC']])['values'];
359 $this->assertEquals(3, $phone[1]['location_type_id']);
360 $this->assertEquals(0, $phone[1]['is_primary']);
361 $this->assertEquals(12334, $phone[1]['phone']);
362 $this->assertEquals(1, $phone[0]['location_type_id']);
363 $this->assertEquals(1, $phone[0]['is_primary']);
364 $this->assertEquals(4444, $phone[0]['phone']);
365
366 $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id']));
367 }
368
369 /**
370 * Test that the import parser updates the address on the existing primary location.
371 *
372 * @throws \Exception
373 */
374 public function testImportPrimaryAddressUpdate() {
375 list($contactValues) = $this->setUpBaseContact(array('external_identifier' => 'android'));
376 $contactValues['email'] = 'melinda.gates@microsoft.com';
377 $contactValues['phone'] = '98765';
378 $contactValues['external_identifier'] = 'android';
379 $contactValues['street_address'] = 'Big Mansion';
380 $contactValues['city'] = 'Big City';
381 $contactID = $this->callAPISuccessGetValue('Contact', array('external_identifier' => 'android', 'return' => 'id'));
382 $originalAddress = $this->callAPISuccess('Address', 'create', array('location_type_id' => 2, 'street_address' => 'small house', 'contact_id' => $contactID));
383 $originalPhone = $this->callAPISuccess('phone', 'create', array('location_type_id' => 2, 'phone' => '1234', 'contact_id' => $contactID));
384 $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => 'Primary', 3 => NULL, 4 => NULL, 5 => 'Primary', 6 => 'Primary', 7 => 'Primary'));
385 $phone = $this->callAPISuccessGetSingle('Phone', ['phone' => '98765']);
386 $this->assertEquals(2, $phone['location_type_id']);
387 $this->assertEquals($originalPhone['id'], $phone['id']);
388 $email = $this->callAPISuccess('Email', 'getsingle', ['contact_id' => $contactID]);
389 $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion'));
390 $this->assertEquals(2, $address['location_type_id']);
391 $this->assertEquals($originalAddress['id'], $address['id']);
392 $this->assertEquals('Big City', $address['city']);
393 $this->callAPISuccessGetSingle('Contact', $contactValues);
394 }
395
396 /**
397 * Test the determination of whether a custom field is valid.
398 */
399 public function testCustomFieldValidation() {
400 $errorMessage = array();
401 $customGroup = $this->customGroupCreate(array(
402 'extends' => 'Contact',
403 'title' => 'ABC',
404 ));
405 $customField = $this->customFieldOptionValueCreate($customGroup, 'fieldABC', array('html_type' => 'Multi-Select'));
406 $params = array(
407 'custom_' . $customField['id'] => 'Label1|Label2',
408 );
409 CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage);
410 $this->assertEquals(array(), $errorMessage);
411 }
412
413 /**
414 * Test that setting duplicate action to fill doesn't blow away data
415 * that exists, but does fill in where it's empty.
416 *
417 * @throw \Exception
418 */
419 public function testImportFill() {
420 // Create a custom field group for testing.
421 $custom_group_name = 'importFillGroup';
422 $results = $this->callAPISuccess('customGroup', 'get', array('title' => $custom_group_name));
423 if ($results['count'] == 0) {
424 $api_params = array(
425 'title' => $custom_group_name,
426 'extends' => 'Individual',
427 'is_active' => TRUE,
428 );
429 $customGroup = $this->callAPISuccess('customGroup', 'create', $api_params);
430 }
431
432 // Add two custom fields.
433 $api_params = array(
434 'custom_group_id' => $customGroup['id'],
435 'label' => 'importFillField1',
436 'html_type' => 'Select',
437 'data_type' => 'String',
438 'option_values' => array(
439 'foo' => 'Foo',
440 'bar' => 'Bar',
441 ),
442 );
443 $result = $this->callAPISuccess('custom_field', 'create', $api_params);
444 $customField1 = $result['id'];
445
446 $api_params = array(
447 'custom_group_id' => $customGroup['id'],
448 'label' => 'importFillField2',
449 'html_type' => 'Select',
450 'data_type' => 'String',
451 'option_values' => array(
452 'baz' => 'Baz',
453 'boo' => 'Boo',
454 ),
455 );
456 $result = $this->callAPISuccess('custom_field', 'create', $api_params);
457 $customField2 = $result['id'];
458
459 // Now set up values.
460 $original_gender = 'Male';
461 $original_custom1 = 'foo';
462 $original_job_title = '';
463 $original_custom2 = '';
464 $original_email = 'test-import-fill@example.org';
465
466 $import_gender = 'Female';
467 $import_custom1 = 'bar';
468 $import_job_title = 'Chief data importer';
469 $import_custom2 = 'baz';
470
471 // Create contact with both one known core field and one custom
472 // field filled in.
473 $api_params = array(
474 'contact_type' => 'Individual',
475 'email' => $original_email,
476 'gender' => $original_gender,
477 'custom_' . $customField1 => $original_custom1,
478 );
479 $result = $this->callAPISuccess('contact', 'create', $api_params);
480 $contact_id = $result['id'];
481
482 // Run an import.
483 $import = array(
484 'email' => $original_email,
485 'gender_id' => $import_gender,
486 'custom_' . $customField1 => $import_custom1,
487 'job_title' => $import_job_title,
488 'custom_' . $customField2 => $import_custom2,
489 );
490
491 $this->runImport($import, CRM_Import_Parser::DUPLICATE_FILL, CRM_Import_Parser::VALID);
492
493 $expected = array(
494 'gender' => $original_gender,
495 'custom_' . $customField1 => $original_custom1,
496 'job_title' => $import_job_title,
497 'custom_' . $customField2 => $import_custom2,
498 );
499
500 $params = array(
501 'id' => $contact_id,
502 'return' => array(
503 'gender',
504 'custom_' . $customField1,
505 'job_title',
506 'custom_' . $customField2,
507 ),
508 );
509 $result = civicrm_api3('Contact', 'get', $params);
510 $values = array_pop($result['values']);
511 foreach ($expected as $field => $expected_value) {
512 if (!isset($values[$field])) {
513 $given_value = NULL;
514 }
515 else {
516 $given_value = $values[$field];
517 }
518 // We expect:
519 // gender: Male
520 // job_title: Chief Data Importer
521 // importFillField1: foo
522 // importFillField2: baz
523 $this->assertEquals($expected_value, $given_value, "$field properly handled during Fill import");
524 }
525 }
526
527 /**
528 * CRM-19888 default country should be used if ambigous.
529 */
530 public function testImportAmbiguousStateCountry() {
531 $this->callAPISuccess('Setting', 'create', ['defaultContactCountry' => 1228]);
532 $countries = CRM_Core_PseudoConstant::country(FALSE, FALSE);
533 $this->callAPISuccess('Setting', 'create', array('countryLimit' => array(array_search('United States', $countries), array_search('Guyana', $countries), array_search('Netherlands', $countries))));
534 $this->callAPISuccess('Setting', 'create', array('provinceLimit' => array(array_search('United States', $countries), array_search('Guyana', $countries), array_search('Netherlands', $countries))));
535 $mapper = array(0 => NULL, 1 => NULL, 2 => 'Primary', 3 => NULL);
536 list($contactValues) = $this->setUpBaseContact();
537 $fields = array_keys($contactValues);
538 $addressValues = array(
539 'street_address' => 'PO Box 2716',
540 'city' => 'Midway',
541 'state_province' => 'UT',
542 'postal_code' => 84049,
543 'country' => 'United States',
544 );
545 $locationTypes = $this->callAPISuccess('Address', 'getoptions', array('field' => 'location_type_id'));
546 $locationTypes = $locationTypes['values'];
547 foreach ($addressValues as $field => $value) {
548 $contactValues['home_' . $field] = $value;
549 $mapper[] = array_search('Home', $locationTypes);
550 $contactValues['work_' . $field] = $value;
551 $mapper[] = array_search('Work', $locationTypes);
552 $fields[] = $field;
553 $fields[] = $field;
554 }
555 $contactValues['work_country'] = '';
556
557 $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, $mapper, $fields);
558 $addresses = $this->callAPISuccess('Address', 'get', array('contact_id' => array('>' => 2), 'sequential' => 1));
559 $this->assertEquals(2, $addresses['count']);
560 $this->assertEquals(array_search('United States', $countries), $addresses['values'][0]['country_id']);
561 $this->assertEquals(array_search('United States', $countries), $addresses['values'][1]['country_id']);
562 }
563
564 /**
565 * Run the import parser.
566 *
567 * @param array $originalValues
568 *
569 * @param int $onDuplicateAction
570 * @param int $expectedResult
571 * @param array|null $mapperLocType
572 * Array of location types that map to the input arrays.
573 * @param array|null $fields
574 * Array of field names. Will be calculated from $originalValues if not passed in, but
575 * that method does not cope with duplicates.
576 */
577 protected function runImport($originalValues, $onDuplicateAction, $expectedResult, $mapperLocType = [], $fields = NULL) {
578 if (!$fields) {
579 $fields = array_keys($originalValues);
580 }
581 $values = array_values($originalValues);
582 $parser = new CRM_Contact_Import_Parser_Contact($fields, $mapperLocType);
583 $parser->_contactType = 'Individual';
584 $parser->_onDuplicate = $onDuplicateAction;
585 $parser->init();
586 $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values), 'Return code from parser import was not as expected');
587 }
588
589 /**
590 * @param array $fields Array of fields to be imported
591 * @param array $allfields Array of all fields which can be part of import
592 */
593 private function mapRelationshipFields(&$fields, $allfields) {
594 foreach ($allfields as $key => $fieldtocheck) {
595 $elementIndex = array_search($fieldtocheck->_title, $fields);
596 if ($elementIndex !== FALSE) {
597 $fields[$elementIndex] = $key;
598 }
599 }
600 }
601
602 /**
603 * Set up the underlying contact.
604 *
605 * @param array $params
606 * Optional extra parameters to set.
607 *
608 * @return array
609 * @throws \Exception
610 */
611 protected function setUpBaseContact($params = array()) {
612 $originalValues = array_merge(array(
613 'first_name' => 'Bill',
614 'last_name' => 'Gates',
615 'email' => 'bill.gates@microsoft.com',
616 'nick_name' => 'Billy-boy',
617 ), $params);
618 $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
619 $result = $this->callAPISuccessGetSingle('Contact', $originalValues);
620 return array($originalValues, $result);
621 }
622
623 }