diff --git a/CRM/Contribute/Import/Parser/Contribution.php b/CRM/Contribute/Import/Parser/Contribution.php index b1836cb39c10..744742bceda9 100644 --- a/CRM/Contribute/Import/Parser/Contribution.php +++ b/CRM/Contribute/Import/Parser/Contribution.php @@ -440,7 +440,7 @@ public function import(array $values): void { $softCreditParams = []; foreach ($params['SoftCreditContact'] ?? [] as $index => $softCreditContact) { $softCreditParams[$index]['soft_credit_type_id'] = $softCreditContact['soft_credit_type_id']; - $softCreditParams[$index]['contact_id'] = $this->getContactID($softCreditContact['Contact'], $softCreditContact['id'] ?? NULL, 'SoftCreditContact', $this->getDedupeRulesForEntity('SoftCreditContact')); + $softCreditParams[$index]['contact_id'] = $this->getContactID($softCreditContact['Contact'], $softCreditContact['Contact']['id'] ?? NULL, 'SoftCreditContact', $this->getDedupeRulesForEntity('SoftCreditContact')); if (empty($softCreditParams[$index]['contact_id']) && in_array($this->getActionForEntity('SoftCreditContact'), ['update', 'select'])) { throw new CRM_Core_Exception(ts('Soft Credit Contact not found')); } diff --git a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php index 90927307ec42..3f7658de5318 100644 --- a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php @@ -103,6 +103,7 @@ public function testImportParserWithSoftCreditsByExternalIdentifier(string $thou ['name' => 'financial_type_id'], ['name' => 'external_identifier'], ['name' => 'soft_credit.contact.external_identifier', 'soft_credit_type_id' => 1], + ['name' => ''], ]; $this->importCSV('contributions_amount_validate.csv', $mapping, ['onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP]); @@ -119,6 +120,27 @@ public function testImportParserWithSoftCreditsByExternalIdentifier(string $thou $this->assertEquals(1, $dataSource->getRowCount([CRM_Import_Parser::ERROR])); $this->assertEquals(1, $dataSource->getRowCount([CRM_Contribute_Import_Parser_Contribution::SOFT_CREDIT])); $this->assertEquals(1, $dataSource->getRowCount([CRM_Import_Parser::VALID])); + + // Now try the import with the dots swapped to double underscores. The parser + // layer and api understand the dots - but QuickForm has to play switcheroo as the dots + // break the hierarchical multiselect js. QuickForm uses a double underscore as a stand in.; + $this->validateSoftCreditImport([ + ['name' => 'total_amount'], + ['name' => 'receive_date'], + ['name' => 'financial_type_id'], + ['name' => 'external_identifier'], + ['name' => 'soft_credit__contact__external_identifier', 'soft_credit_type_id' => 1], + ]); + $this->validateSoftCreditImport([ + ['name' => 'total_amount'], + ['name' => 'receive_date'], + ['name' => 'financial_type_id'], + ['name' => 'external_identifier'], + [], + [], + [], + ['name' => 'soft_credit__contact__id', 'soft_credit_type_id' => 1], + ]); } /** @@ -874,4 +896,20 @@ public function testEnableExtension(): void { $this->assertEquals('ERROR', $result->first()['_status']); } + /** + * @param array $mapping + * + * @throws \CRM_Core_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + protected function validateSoftCreditImport(array $mapping): void { + Contribution::delete()->addWhere('id', '>', 0)->execute(); + $this->callAPISuccessGetCount('ContributionSoft', [], 0); + $this->importCSV('contributions_amount_validate.csv', $mapping, ['onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP]); + $dataSource = new CRM_Import_DataSource_CSV($this->userJobID); + // Check a row imported. + $this->assertEquals(1, $dataSource->getRowCount([CRM_Import_Parser::VALID])); + $this->callAPISuccessGetCount('ContributionSoft', [], 1); + } + } diff --git a/tests/phpunit/CRM/Contribute/Import/Parser/data/contributions_amount_validate.csv b/tests/phpunit/CRM/Contribute/Import/Parser/data/contributions_amount_validate.csv index bc933de80ffe..5c9ccadab26e 100644 --- a/tests/phpunit/CRM/Contribute/Import/Parser/data/contributions_amount_validate.csv +++ b/tests/phpunit/CRM/Contribute/Import/Parser/data/contributions_amount_validate.csv @@ -1,3 +1,3 @@ -Total Amount,Receive Date,Financial Type,External identifier,Soft Credit Ext ID,Email,Email - soft credit -"1,230.99",2008-09-20,Donation,ext-1,ext-2,harry@example.com,the-firm@example.com -"1.230,99",2008-09-20,Donation,ext-1,ext-2,, +Total Amount,Receive Date,Financial Type,External identifier,Soft Credit Ext ID,Email,Email - soft credit,Soft credit contact ID +"1,230.99",2008-09-20,Donation,ext-1,ext-2,harry@example.com,the-firm@example.com,4 +"1.230,99",2008-09-20,Donation,ext-1,ext-2,,,4