From 95bc2868a7d01c7b1237065a138f3af78763c80e Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Tue, 4 Apr 2023 12:59:31 -0400 Subject: [PATCH] Fix multi-value custom fields on participant import --- CRM/Event/Import/Parser/Participant.php | 33 +------------------ .../Event/Import/Parser/ParticipantTest.php | 9 +++++ .../Parser/data/participant_with_ext_id.csv | 4 +-- 3 files changed, 12 insertions(+), 34 deletions(-) diff --git a/CRM/Event/Import/Parser/Participant.php b/CRM/Event/Import/Parser/Participant.php index ac4fee8715..27456bb4db 100644 --- a/CRM/Event/Import/Parser/Participant.php +++ b/CRM/Event/Import/Parser/Participant.php @@ -123,21 +123,11 @@ class CRM_Event_Import_Parser_Participant extends CRM_Import_Parser { throw new CRM_Core_Exception($formatError['error_message']); } - if (!$this->isUpdateExisting()) { - $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, - NULL, - 'Participant' - ); - } - else { + if ($this->isUpdateExisting()) { if (!empty($formatValues['participant_id'])) { $dao = new CRM_Event_BAO_Participant(); $dao->id = $formatValues['participant_id']; - $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($formatted, - $formatValues['participant_id'], - 'Participant' - ); if ($dao->find(TRUE)) { $ids = [ 'participant' => $formatValues['participant_id'], @@ -278,27 +268,6 @@ class CRM_Event_Import_Parser_Participant extends CRM_Import_Parser { continue; } - // Handling Custom Data - if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { - $values[$key] = $value; - $type = $customFields[$customFieldID]['html_type']; - if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) { - $values[$key] = self::unserializeCustomValue($customFieldID, $value, $type); - } - elseif ($type == 'Select' || $type == 'Radio') { - $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); - foreach ($customOption as $customFldID => $customValue) { - $val = $customValue['value'] ?? NULL; - $label = $customValue['label'] ?? NULL; - $label = strtolower($label); - $value = strtolower(trim($value)); - if (($value == $label) || ($value == strtolower($val))) { - $values[$key] = $val; - } - } - } - } - switch ($key) { case 'participant_contact_id': if (!CRM_Utils_Rule::integer($value)) { diff --git a/tests/phpunit/CRM/Event/Import/Parser/ParticipantTest.php b/tests/phpunit/CRM/Event/Import/Parser/ParticipantTest.php index 5574312559..3d87192e17 100644 --- a/tests/phpunit/CRM/Event/Import/Parser/ParticipantTest.php +++ b/tests/phpunit/CRM/Event/Import/Parser/ParticipantTest.php @@ -114,6 +114,8 @@ class CRM_Event_Import_Parser_ParticipantTest extends CiviUnitTestCase { /** * Test that an external id will not match to a deleted contact.. + * + * @throws \CRM_Core_Exception */ public function testImportWithExternalID() :void { $this->eventCreatePaid(['title' => 'Rain-forest Cup Youth Soccer Tournament']); @@ -131,6 +133,7 @@ class CRM_Event_Import_Parser_ParticipantTest extends CiviUnitTestCase { ['name' => 'status_id'], ['name' => 'register_date'], ['name' => 'do_not_import'], + ['name' => 'do_not_import'], ]); $dataSource = new CRM_Import_DataSource_CSV($this->userJobID); $row = $dataSource->getRow(); @@ -139,9 +142,12 @@ class CRM_Event_Import_Parser_ParticipantTest extends CiviUnitTestCase { /** * Test that imports work generally. + * + * @throws \CRM_Core_Exception */ public function testImportParticipant() :void { $this->eventCreatePaid(['title' => 'Rain-forest Cup Youth Soccer Tournament']); + $this->createCustomGroupWithFieldOfType(['extends' => 'Participant'], 'checkbox'); $contactID = $this->individualCreate(['external_identifier' => 'ref-77']); $this->importCSV('participant_with_ext_id.csv', [ ['name' => 'event_id'], @@ -156,6 +162,7 @@ class CRM_Event_Import_Parser_ParticipantTest extends CiviUnitTestCase { ['name' => 'status_id'], ['name' => 'register_date'], ['name' => 'do_not_import'], + ['name' => $this->getCustomFieldName('checkbox')], ]); $dataSource = new CRM_Import_DataSource_CSV($this->userJobID); $row = $dataSource->getRow(); @@ -171,12 +178,14 @@ class CRM_Event_Import_Parser_ParticipantTest extends CiviUnitTestCase { $this->assertEquals('2022-12-07 00:00:00', $result['participant_register_date']); $this->assertEquals(['Attendee', 'Volunteer'], $result['participant_role']); $this->assertEquals(0, $result['participant_is_pay_later']); + $this->assertEquals(['P', 'M'], array_keys($result[$this->getCustomFieldName('checkbox')])); } /** * @param array $submittedValues * * @return int + * @throws \CRM_Core_Exception */ protected function getUserJobID(array $submittedValues = []): int { $userJobID = UserJob::create()->setValues([ diff --git a/tests/phpunit/CRM/Event/Import/Parser/data/participant_with_ext_id.csv b/tests/phpunit/CRM/Event/Import/Parser/data/participant_with_ext_id.csv index 8cf0050d54..05fb42a7c4 100644 --- a/tests/phpunit/CRM/Event/Import/Parser/data/participant_with_ext_id.csv +++ b/tests/phpunit/CRM/Event/Import/Parser/data/participant_with_ext_id.csv @@ -1,2 +1,2 @@ -Event Title,Campaign ID,External Identifier,Fee Amount,Fee Currency,Fee level,Is Pay Later,Participant Role,Participant Source,Participant Status,Register date,Do not import -Rain-forest Cup Youth Soccer Tournament,Soccer Cup,ref-77,5000.55,USD,High,No,"Attendee, Volunteer",Phoned up,Registered,2022-12-07, +Event Title,Campaign ID,External Identifier,Fee Amount,Fee Currency,Fee level,Is Pay Later,Participant Role,Participant Source,Participant Status,Register date,Do not import,Color +Rain-forest Cup Youth Soccer Tournament,Soccer Cup,ref-77,5000.55,USD,High,No,"Attendee, Volunteer",Phoned up,Registered,2022-12-07,,"Purple, Mauve" -- 2.25.1