Skip to content

Commit

Permalink
ensure multi-value core fields can be imported without backtrace
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcclelland committed Sep 13, 2021
1 parent b3c3741 commit 65a6482
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,12 @@ public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) {
->setLoadOptions(TRUE)
->execute()->indexBy('name');
foreach ($fields as $fieldName => $fieldSpec) {
if (isset($formatted[$fieldName]) && is_array($formatted[$fieldName])) {
// If we have an array at this stage, it's probably a multi-select
// field that has already been parsed properly into the value that
// should be inserted into the database.
continue;
}
if (!empty($formatted[$fieldName])
&& empty($fieldSpec['options'][$formatted[$fieldName]])) {
$formatted[$fieldName] = array_search($formatted[$fieldName], $fieldSpec['options'], TRUE) ?? $formatted[$fieldName];
Expand Down
39 changes: 39 additions & 0 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,45 @@ public function testImportAmbiguousStateCountry(): void {
$this->assertEquals(array_search('United States', $countries), $addresses['values'][1]['country_id']);
}

/**
* Test importing fields with various options.
*
* Ensure we can import multiple preferred_communication_methods, single
* gender, and single preferred language using both labels and values.
*
* @throws \CRM_Core_Exception @throws \CiviCRM_API3_Exception
*/
public function testImportFieldsWithVariousOptions() {
$processor = new CRM_Import_ImportProcessor();
$processor->setContactType('Individual');
$processor->setMappingFields(
[
['name' => 'first_name'],
['name' => 'last_name'],
['name' => 'preferred_communication_method'],
['name' => 'gender_id'],
['name' => 'preferred_language'],
],
);
$importer = $processor->getImporterObject();
$fields = ['Ima', 'Texter', "SMS,Phone", "Female", "Danish"];
$importer->import(CRM_Import_Parser::DUPLICATE_NOCHECK, $fields);
$contact = $this->callAPISuccessGetSingle('Contact', ['last_name' => 'Texter']);

$this->assertEquals([4, 1], $contact['preferred_communication_method'], "Import multiple preferred communication methods using labels.");
$this->assertEquals(1, $contact['gender_id'], "Import gender with label.");
$this->assertEquals('da_DK', $contact['preferred_language'], "Import preferred language with label.");

$importer = $processor->getImporterObject();
$fields = ['Ima', 'Texter', "4,1", "1", "da_DK"];
$importer->import(CRM_Import_Parser::DUPLICATE_NOCHECK, $fields);
$contact = $this->callAPISuccessGetSingle('Contact', ['last_name' => 'Texter']);

$this->assertEquals([4, 1], $contact['preferred_communication_method'], "Import multiple preferred communication methods using values.");
$this->assertEquals(1, $contact['gender_id'], "Import gender with id.");
$this->assertEquals('da_DK', $contact['preferred_language'], "Import preferred language with value.");
}

/**
* Run the import parser.
*
Expand Down

0 comments on commit 65a6482

Please sign in to comment.