From 65a64826526e32b584e84cc467386143db4aff99 Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Fri, 10 Sep 2021 16:31:50 -0400 Subject: [PATCH] ensure multi-value core fields can be imported without backtrace --- CRM/Contact/Import/Parser/Contact.php | 6 +++ .../CRM/Contact/Import/Parser/ContactTest.php | 39 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index bc5c305797..59dafa4560 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -621,6 +621,12 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { ->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]; diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index 2e7d06a73d..a52db3dc35 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -850,6 +850,45 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { $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. * -- 2.25.1