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

Fix duplicate households on 'Merge same household' exports #14443

Merged
merged 1 commit into from
Jun 15, 2019
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
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