From 315a6e2aee6cb274aba49c72080c1949771eb545 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 25 Jul 2019 13:22:29 +1200 Subject: [PATCH] dev/financial#36 [IMPORT] fix & test mishandling on payment_instrument labels --- CRM/Contribute/Import/Parser/Contribution.php | 30 ++++++++++++------- .../Import/Parser/ContributionTest.php | 27 ++++++++++++++++- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/CRM/Contribute/Import/Parser/Contribution.php b/CRM/Contribute/Import/Parser/Contribution.php index f07efa7b3b..2b8b3f3282 100644 --- a/CRM/Contribute/Import/Parser/Contribution.php +++ b/CRM/Contribute/Import/Parser/Contribution.php @@ -650,11 +650,10 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa require_once 'CRM/Utils/DeprecatedUtils.php'; // copy all the contribution fields as is require_once 'api/v3/utils.php'; - $fields = CRM_Contribute_DAO_Contribution::fields(); + $fields = CRM_Core_DAO::getExportableFieldsWithPseudoConstants('CRM_Contribute_BAO_Contribution'); _civicrm_api3_store_values($fields, $params, $values); - require_once 'CRM/Core/OptionGroup.php'; $customFields = CRM_Core_BAO_CustomField::getFields('Contribution', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE); foreach ($params as $key => $value) { @@ -807,6 +806,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa case 'total_amount': case 'fee_amount': case 'net_amount': + // @todo add test like testPaymentTypeLabel & remove these lines as we can anticipate error will still be caught & handled. if (!CRM_Utils_Rule::money($value)) { return civicrm_api3_create_error("$key not a valid amount: $value"); } @@ -819,6 +819,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa break; case 'financial_type': + // @todo add test like testPaymentTypeLabel & remove these lines in favour of 'default' part of switch. require_once 'CRM/Contribute/PseudoConstant.php'; $contriTypes = CRM_Contribute_PseudoConstant::financialType(); foreach ($contriTypes as $val => $type) { @@ -832,15 +833,8 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa } break; - case 'payment_instrument': - require_once 'CRM/Core/PseudoConstant.php'; - $values['payment_instrument_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', $value); - if (empty($values['payment_instrument_id'])) { - return civicrm_api3_create_error("Payment Instrument is not valid: $value"); - } - break; - case 'contribution_status_id': + // @todo add test like testPaymentTypeLabel & remove these lines in favour of 'default' part of switch. require_once 'CRM/Core/PseudoConstant.php'; if (!$values['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $value)) { return civicrm_api3_create_error("Contribution Status is not valid: $value"); @@ -1017,6 +1011,22 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa break; default: + if (isset($fields[$key]) && !empty($fields[$key]['is_pseudofield_for'])) { + $realField = $fields[$key]['is_pseudofield_for']; + $realFieldSpec = $fields[$realField]; + /* @var \CRM_Core_DAO $bao */ + $bao = $realFieldSpec['bao']; + // Get names & labels - we will try to match name first but if not available then see if + // we have a label that can be converted to a name. + // For historical reasons use validate as context - ie disabled name matches ARE permitted per prior to change. + $nameOptions = $bao::buildOptions($realField, 'validate'); + if (!isset($nameOptions[$value])) { + $labelOptions = array_flip($bao::buildOptions($realField, 'match')); + if (isset($labelOptions[$params[$key]])) { + $values[$key] = $labelOptions[$params[$key]]; + } + } + } break; } } diff --git a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php index 44dd252fb1..3078ca4c17 100644 --- a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php @@ -89,6 +89,31 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { $this->assertEquals('20191020', $params['receive_date']); } + /** + * Test payment types are passed. + * + * @throws \CRM_Core_Exception + */ + public function testPaymentTypeLabel() { + $contactID = $this->individualCreate(); + $values = ['contribution_contact_id' => $contactID, 'total_amount' => 10, 'financial_type' => 'Donation', 'payment_instrument' => 'Check']; + // Note that the expected result should logically be CRM_Import_Parser::valid but writing test to reflect not fix here + $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, NULL); + $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $contactID]); + $this->assertEquals('Check', $contribution['payment_instrument']); + + $this->callAPISuccess('OptionValue', 'create', [ + 'option_group_id' => 'payment_instrument', + 'value' => 777, + 'name' => 'random', + 'label' => 'not at all random', + ]); + $values = ['contribution_contact_id' => $contactID, 'total_amount' => 10, 'financial_type' => 'Donation', 'payment_instrument' => 'not at all random']; + $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, NULL); + $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $contactID, 'payment_instrument_id' => 'random']); + $this->assertEquals('not at all random', $contribution['payment_instrument']); + } + /** * Run the import parser. * @@ -102,7 +127,7 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { * @param array|null $fields * Array of field names. Will be calculated from $originalValues if not passed in. */ - protected function runImport($originalValues, $onDuplicateAction, $expectedResult, $mapperSoftCredit = NULL, $mapperPhoneType = NULL, $mapperSoftCreditType = NULL, $fields = NULL) { + protected function runImport($originalValues, $onDuplicateAction, $expectedResult, $mapperSoftCredit = [], $mapperPhoneType = NULL, $mapperSoftCreditType = [], $fields = NULL) { if (!$fields) { $fields = array_keys($originalValues); } -- 2.25.1