Skip to content

Commit

Permalink
Merge pull request #10797 from agileware/CIVICRM-230
Browse files Browse the repository at this point in the history
CIVICRM-230 CRM-17040 Import Contributions using External Ids causes …
  • Loading branch information
eileenmcnaughton authored Aug 21, 2017
2 parents 74c7dfd + 29cd38f commit 57da476
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CRM/Utils/DeprecatedUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,9 @@ function _civicrm_api3_deprecated_check_contact_dedupe($params) {
if ($field == NULL || $field === '') {
continue;
}
if (is_array($field)) {
// CRM-17040, Considering only primary contact when importing contributions. So contribution inserts into primary contact
// instead of soft credit contact.
if (is_array($field) && $key != "soft_credit") {
foreach ($field as $value) {
$break = FALSE;
if (is_array($value)) {
Expand Down
85 changes: 85 additions & 0 deletions tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* @file
* File for the CRM_Contribute_Import_Parser_ContributionTest class.
*/
/**
* Test Contribution import parser.
*
* @package CiviCRM
* @group headless
*/
class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
protected $_tablesToTruncate = array();
/**
* Setup function.
*/
public function setUp() {
parent::setUp();
}
/**
* Test import parser will add contribution and soft contribution each for different contact.
*
* In this case primary contact and secondary contact both are identified by external identifier.
*
* @throws \Exception
*/
public function testImportParserWithSoftCreditsByExternalIdentifier() {
$contact1Params = array(
'first_name' => 'Contact',
'last_name' => 'One',
'external_identifier' => 'ext-1',
'contact_type' => 'Individual',
);
$contact2Params = array(
'first_name' => 'Contact',
'last_name' => 'Two',
'external_identifier' => 'ext-2',
'contact_type' => 'Individual',
);
$contact1Id = $this->individualCreate($contact1Params);
$contact2Id = $this->individualCreate($contact2Params);
$values = array(
"total_amount" => 10,
"financial_type" => "Donation",
"external_identifier" => "ext-1",
"soft_credit" => "ext-2",
);
$mapperSoftCredit = array(NULL, NULL, NULL, "external_identifier");
$mapperSoftCreditType = array(NULL, NULL, NULL, "1");
$this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Contribute_Import_Parser::SOFT_CREDIT, $mapperSoftCredit, NULL, $mapperSoftCreditType);
$params = array(
"contact_id" => $contact1Id,
);
$values = array();
$contributionsOfMainContact = CRM_Contribute_BAO_Contribution::retrieve($params, $values, $values);
$params["contact_id"] = $contact2Id;
$contributionsOfSoftContact = CRM_Contribute_BAO_ContributionSoft::retrieve($params, $values);
$this->assertEquals(1, count($contributionsOfMainContact), 'Contribution not added for primary contact');
$this->assertEquals(1, count($contributionsOfSoftContact), 'Soft Contribution not added for secondary contact');
}
/**
* Run the import parser.
*
* @param array $originalValues
*
* @param int $onDuplicateAction
* @param int $expectedResult
* @param array|null $mapperSoftCredit
* @param array|null $mapperPhoneType
* @param array|null $mapperSoftCreditType
* @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) {
if (!$fields) {
$fields = array_keys($originalValues);
}
$values = array_values($originalValues);
$parser = new CRM_Contribute_Import_Parser_Contribution($fields, $mapperSoftCredit, $mapperPhoneType, $mapperSoftCreditType);
$parser->_contactType = 'Individual';
$parser->init();
$this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values), 'Return code from parser import was not as expected');
}

}

0 comments on commit 57da476

Please sign in to comment.