From 8e2ac3671cfac37b554b4e09ba79bdfcde5568e7 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 25 Jul 2019 17:49:18 +1200 Subject: [PATCH] Add handling & test for translated contribution status --- CRM/Contribute/Import/Parser/Contribution.php | 19 ++++----- .../Import/Parser/ContributionTest.php | 40 ++++++++++++++++--- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/CRM/Contribute/Import/Parser/Contribution.php b/CRM/Contribute/Import/Parser/Contribution.php index 2b8b3f3282..b6c801431f 100644 --- a/CRM/Contribute/Import/Parser/Contribution.php +++ b/CRM/Contribute/Import/Parser/Contribution.php @@ -806,7 +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. + // @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"); } @@ -833,14 +833,6 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa } 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"); - } - break; - case 'soft_credit': // import contribution record according to select contact type // validate contact id and external identifier. @@ -1011,8 +1003,13 @@ 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']; + // Hande name or label for fields with options. + if (isset($fields[$key]) && + // Yay - just for a surprise we are inconsistent on whether we pass the pseudofield (payment_instrument) + // or the field name (contribution_status_id) + (!empty($fields[$key]['is_pseudofield_for']) || !empty($fields[$key]['pseudoconstant'])) + ) { + $realField = $fields[$key]['is_pseudofield_for'] ?? $key; $realFieldSpec = $fields[$realField]; /* @var \CRM_Core_DAO $bao */ $bao = $realFieldSpec['bao']; diff --git a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php index 3078ca4c17..44a6161911 100644 --- a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php @@ -15,8 +15,9 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { /** * Setup function. */ - public function setUp() { - parent::setUp(); + public function tearDown() { + $this->quickCleanUpFinancialEntities(); + parent::tearDown(); } /** @@ -55,9 +56,7 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { $mapperSoftCredit = [NULL, NULL, NULL, "external_identifier"]; $mapperSoftCreditType = [NULL, NULL, NULL, "1"]; $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Contribute_Import_Parser::SOFT_CREDIT, $mapperSoftCredit, NULL, $mapperSoftCreditType); - $params = [ - "contact_id" => $contact1Id, - ]; + $params = ['contact_id' => $contact1Id]; $values = []; $contributionsOfMainContact = CRM_Contribute_BAO_Contribution::retrieve($params, $values, $values); $this->assertEquals(1230.99, $contributionsOfMainContact->total_amount); @@ -110,10 +109,39 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { ]); $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']); + $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $contactID, 'payment_instrument_id' => 'random']); $this->assertEquals('not at all random', $contribution['payment_instrument']); } + /** + * Test handling of contribution statuses. + * + * @throws \CRM_Core_Exception + */ + public function testContributionStatusLabel() { + $contactID = $this->individualCreate(); + $values = ['contribution_contact_id' => $contactID, 'total_amount' => 10, 'financial_type' => 'Donation', 'payment_instrument' => 'Check', 'contribution_status_id' => 'Pending']; + // 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('Pending', $contribution['contribution_status']); + + $this->callAPISuccess('OptionValue', 'create', [ + 'option_group_id' => 'contribution_status', + 'value' => 777, + 'name' => 'random', + 'label' => 'not at all random', + ]); + $values['contribution_status_id'] = 'not at all random'; + $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, NULL); + $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $contactID, 'contribution_status_id' => 'random']); + $this->assertEquals('not at all random', $contribution['contribution_status']); + + $values['contribution_status_id'] = 'just say no'; + $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::ERROR); + $this->callAPISuccessGetCount('Contribution', ['contact_id' => $contactID], 2); + } + /** * Run the import parser. * -- 2.25.1