Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid back trace when importing preferred communication method #21433

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'],
]
);
jmcclelland marked this conversation as resolved.
Show resolved Hide resolved
$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"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interesting that this works in the test, because I can't get numbers to work for this field in the UI, either before or after the patch or using single-value or multi.
Mystery?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh - interesting. I put the values into the test because I thought, why not expand the test coverage? But didn't test entering the values in the UI. What happens when you try? Do you get an error?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't get past the import screens to see what would happen - it gives you the excel file to download with errors which says it thinks the field has an invalid value.

$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