Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-17040 Import Contributions using External Ids causes … #10797

Merged
merged 2 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}

}