Skip to content

Commit

Permalink
Fix duplicate households on 'Merge same household' exports
Browse files Browse the repository at this point in the history
Add test
  • Loading branch information
eileenmcnaughton committed Jun 6, 2019
1 parent 1c1c879 commit ebebd62
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions CRM/Export/BAO/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ public static function exportComponents(
$processor->setHouseholdMergeReturnProperties(array_diff_key($returnProperties, array_fill_keys(['location_type', 'im_provider'], 1)));
}

// This perhaps only needs calling when $mergeSameHousehold == 1
self::buildRelatedContactArray($selectAll, $ids, $processor, $componentTable);

// make sure the groups stuff is included only if specifically specified
Expand Down
24 changes: 24 additions & 0 deletions CRM/Export/BAO/ExportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ class CRM_Export_BAO_ExportProcessor {
*/
protected $exportedHouseholds = [];

/**
* Households to skip during export as they will be exported via their relationships anyway.
*
* @var array
*/
protected $householdsToSkip = [];

/**
* Get return properties by relationship.
* @return array
Expand Down Expand Up @@ -233,6 +240,9 @@ public function setRelationshipTypes() {
*/
public function setRelationshipValue($relationshipType, $contactID, $field, $value) {
$this->relatedContactValues[$relationshipType][$contactID][$field] = $value;
if ($field === 'id') {
$this->householdsToSkip[] = $value;
}
}

/**
Expand Down Expand Up @@ -640,6 +650,9 @@ public function getMetadata() {
* @return array|bool
*/
public function buildRow($query, $iterationDAO, $outputColumns, $metadata, $paymentDetails, $addPaymentHeader, $paymentTableId) {
if ($this->isHouseholdToSkip($iterationDAO->contact_id)) {
return FALSE;
}
$phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
$imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');

Expand Down Expand Up @@ -1356,4 +1369,15 @@ protected function buildRelationshipFieldsForRow(&$row, $contactID, $value, $fie
}
}

/**
* Is this contact a household that is already set to be exported by virtue of it's household members.
*
* @param int $contactID
*
* @return bool
*/
protected function isHouseholdToSkip($contactID) {
return in_array($contactID, $this->householdsToSkip);
}

}
23 changes: 13 additions & 10 deletions tests/phpunit/CRM/Export/BAO/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ public function setUpContactExportData() {
*
* @param int $isPrimaryOnly
*
* @dataProvider getPrimarySearchOptions
* @dataProvider getBooleanDataProvider
* @throws \CRM_Core_Exception
*/
public function testExportPrimaryAddress($isPrimaryOnly) {
\Civi::settings()->set('searchPrimaryDetailsOnly', $isPrimaryOnly);
Expand Down Expand Up @@ -399,14 +400,6 @@ public function testExportPrimaryAddress($isPrimaryOnly) {
\Civi::settings()->set('searchPrimaryDetailsOnly', FALSE);
}

/**
* Get the options for the primary search setting field.
* @return array
*/
public function getPrimarySearchOptions() {
return [[TRUE], [FALSE]];
}

/**
* Test that when exporting a pseudoField it is reset for NULL entries.
*
Expand Down Expand Up @@ -506,10 +499,19 @@ public function testExportRelationships() {
* Test exporting relationships.
*
* This is to ensure that CRM-13995 remains fixed.
*
* @dataProvider getBooleanDataProvider
*
* @param bool $includeHouseHold
*
* @throws \CRM_Core_Exception
*/
public function testExportRelationshipsMergeToHousehold() {
public function testExportRelationshipsMergeToHousehold($includeHouseHold) {
list($householdID, $houseHoldTypeID) = $this->setUpHousehold();

if ($includeHouseHold) {
$this->contactIDs[] = $householdID;
}
$selectedFields = [
['Individual', $houseHoldTypeID . '_a_b', 'state_province', ''],
['Individual', $houseHoldTypeID . '_a_b', 'city', ''],
Expand All @@ -536,6 +538,7 @@ public function testExportRelationshipsMergeToHousehold() {
);
$dao = CRM_Core_DAO::executeQuery("SELECT * FROM {$tableName}");
while ($dao->fetch()) {
$this->assertEquals(1, $dao->N);
$this->assertEquals('Portland', $dao->city);
$this->assertEquals('ME', $dao->state_province);
$this->assertEquals($householdID, $dao->civicrm_primary_id);
Expand Down
9 changes: 9 additions & 0 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3166,6 +3166,15 @@ public function getThousandSeparators() {
return array(array('.'), array(','));
}

/**
* Get the boolean options as a provider.
*
* @return array
*/
public function getBooleanDataProvider() {
return [[TRUE], [FALSE]];
}

/**
* Set the separators for thousands and decimal points.
*
Expand Down

0 comments on commit ebebd62

Please sign in to comment.