From 5c7c9e634a130930958afbe82c99223aea0f1fe5 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 30 Oct 2022 20:37:11 +1300 Subject: [PATCH] dev/core#3879 Fix failure to accept soft credit contact id --- CRM/Contribute/Import/Parser/Contribution.php | 2 +- .../Import/Parser/ContributionTest.php | 38 +++++++++++++++++++ .../data/contributions_amount_validate.csv | 6 +-- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/CRM/Contribute/Import/Parser/Contribution.php b/CRM/Contribute/Import/Parser/Contribution.php index b1836cb39c..744742bced 100644 --- a/CRM/Contribute/Import/Parser/Contribution.php +++ b/CRM/Contribute/Import/Parser/Contribution.php @@ -440,7 +440,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser { $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 90927307ec..3f7658de53 100644 --- a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php @@ -103,6 +103,7 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { ['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 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { $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 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { $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 bc933de80f..5c9ccadab2 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 -- 2.25.1