Skip to content

Commit

Permalink
added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb committed May 30, 2018
1 parent 4759887 commit 3442cdf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
4 changes: 1 addition & 3 deletions CRM/Export/BAO/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -2048,8 +2048,6 @@ public static function fetchRelationshipDetails($relDAO, $value, $relPrefix, &$r
else {
$fieldValue = '';
}
$field = $field . '_';
$relPrefix = $field . $relationField;

if (is_object($relDAO) && $relationField == 'id') {
$row[$relPrefix][$relationField] = $relDAO->contact_id;
Expand All @@ -2066,7 +2064,7 @@ public static function fetchRelationshipDetails($relDAO, $value, $relPrefix, &$r
// and state_province (‘province’ context)
switch (TRUE) {
case (!is_object($relDAO)):
$row[$relPrefix . '_' . $fldValue] = '';
$row[$relPrefix][$fldValue] = '';
break;

case in_array('country', $type):
Expand Down
45 changes: 45 additions & 0 deletions tests/phpunit/CRM/Export/BAO/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,51 @@ public function testExportComponentsNull() {
CRM_Core_DAO::executeQuery($sql);
}

/**
* Test to ensure that 'Merge Household Members into their Households' works on export.
*/
public function testMergeHouseholdMemberOnExport() {
// Here's how this test case works - 3 contacts are created A, B and C where A and B are individual contacts.
// C is a household contact whose member is contact A. These 3 contacts are selected for export with 'Merge Household Members into their Households' = TRUE
// And at the end export table contain only 2 contacts i.e. is B and C as B is not a member of C and A is a member of C reason why it is merged with it.
$this->setUpContactExportData();
$householdID = $this->householdCreate();
$this->callAPISuccess('relationship', 'create', [
'contact_id_a' => $this->contactIDs[0],
'contact_id_b' => $householdID,
'relationship_type_id' => CRM_Core_DAO::getFieldValue('CRM_Contact_BAO_RelationshipType', 'Household Member of', 'id', 'name_a_b'),
]);

$contactIDs = array_merge($this->contactIDs, [$householdID]);
$params = ['contact_id' => $contactIDs];
list($tableName, $sqlColumns) = CRM_Export_BAO_Export::exportComponents(
FALSE,
$contactIDs,
CRM_Contact_BAO_Query::convertFormValues($params),
NULL,
NULL,
NULL,
CRM_Export_Form_Select::CONTACT_EXPORT,
NULL,
NULL,
FALSE,
TRUE,
array(
'exportOption' => 1,
'suppress_csv_for_testing' => TRUE,
)
);

$exportedRows = CRM_Utils_SQL_Select::from($tableName)->execute()->fetchAll();
$this->assertEquals(2, count($exportedRows));
// the result should contain contact B and C, as A being a household member merged with C.
$this->assertEquals([$this->contactIDs[1], $householdID], CRM_Utils_Array::collect('civicrm_primary_id', $exportedRows));

// delete the export temp table and component table
$sql = "DROP TABLE IF EXISTS {$tableName}";
CRM_Core_DAO::executeQuery($sql);
}

/**
* Basic test to ensure the exportComponents function can export selected fields for contribution.
*/
Expand Down

0 comments on commit 3442cdf

Please sign in to comment.