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");
}
}
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.
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'];
/**
* Setup function.
*/
- public function setUp() {
- parent::setUp();
+ public function tearDown() {
+ $this->quickCleanUpFinancialEntities();
+ parent::tearDown();
}
/**
$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);
]);
$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.
*