From 6382c24c5f1da7eb85526674f7ba7d59e3d0da6e Mon Sep 17 00:00:00 2001 From: Lema Carl Andrew Date: Thu, 25 Jan 2018 09:01:02 +0300 Subject: [PATCH] CRM-21524 - CRM\Export - Fix merge contacts when deceased --- CRM/Export/BAO/Export.php | 16 ++--- tests/phpunit/CRM/Export/BAO/ExportTest.php | 72 +++++++++++++++++++++ 2 files changed, 80 insertions(+), 8 deletions(-) diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index d6c4bc6b7dcf..bf8ade7a92b9 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -1080,6 +1080,14 @@ public static function exportComponents( if ($exportTempTable) { self::writeDetailsToTable($exportTempTable, $componentDetails, $sqlColumns); + // if postalMailing option is checked, exclude contacts who are deceased, have + // "Do not mail" privacy setting, or have no street address + if (isset($exportParams['postal_mailing_export']['postal_mailing_export']) && + $exportParams['postal_mailing_export']['postal_mailing_export'] == 1 + ) { + self::postalMailingFormat($exportTempTable, $headerRows, $sqlColumns, $exportMode); + } + // do merge same address and merge same household processing if ($mergeSameAddress) { self::mergeSameAddress($exportTempTable, $headerRows, $sqlColumns, $exportParams); @@ -1091,14 +1099,6 @@ public static function exportComponents( self::mergeSameHousehold($exportTempTable, $headerRows, $sqlColumns, $relationKeyHOH); } - // if postalMailing option is checked, exclude contacts who are deceased, have - // "Do not mail" privacy setting, or have no street address - if (isset($exportParams['postal_mailing_export']['postal_mailing_export']) && - $exportParams['postal_mailing_export']['postal_mailing_export'] == 1 - ) { - self::postalMailingFormat($exportTempTable, $headerRows, $sqlColumns, $exportMode); - } - // call export hook CRM_Utils_Hook::export($exportTempTable, $headerRows, $sqlColumns, $exportMode); diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php index df61ee75232e..6b37a8b51b0b 100644 --- a/tests/phpunit/CRM/Export/BAO/ExportTest.php +++ b/tests/phpunit/CRM/Export/BAO/ExportTest.php @@ -339,4 +339,76 @@ public function testExportMasterAddress() { CRM_Core_DAO::executeQuery($sql); } + /** + * Test that deceased and do not mail contacts are removed from contacts before + */ + public function testExportDeceasedDoNotMail() { + $contactA = $this->callAPISuccess('contact', 'create', array( + 'first_name' => 'John', + 'last_name' => 'Doe', + 'contact_type' => 'Individual', + )); + + $contactB = $this->callAPISuccess('contact', 'create', array( + 'first_name' => 'Jane', + 'last_name' => 'Doe', + 'contact_type' => 'Individual', + 'is_deceased' => 1, + )); + + //create address for contact A + $result = $this->callAPISuccess('address', 'create', array( + 'contact_id' => $contactA['id'], + 'location_type_id' => 'Home', + 'street_address' => 'ABC 12', + 'postal_code' => '123 AB', + 'country_id' => '1152', + 'city' => 'ABC', + 'is_primary' => 1, + )); + + //create address for contact B + $result = $this->callAPISuccess('address', 'create', array( + 'contact_id' => $contactB['id'], + 'location_type_id' => 'Home', + 'street_address' => 'ABC 12', + 'postal_code' => '123 AB', + 'country_id' => '1152', + 'city' => 'ABC', + 'is_primary' => 1, + )); + + //export and merge contacts with same address + list($tableName, $sqlColumns) = CRM_Export_BAO_Export::exportComponents( + TRUE, + array($contactA['id'], $contactB['id']), + array(), + NULL, + NULL, + NULL, + CRM_Export_Form_Select::CONTACT_EXPORT, + "contact_a.id IN ({$contactA['id']}, {$contactB['id']})", + NULL, + TRUE, + FALSE, + array( + 'exportOption' => CRM_Export_Form_Select::CONTACT_EXPORT, + 'mergeOption' => TRUE, + 'suppress_csv_for_testing' => TRUE, + 'postal_mailing_export' => array( + 'postal_mailing_export' => TRUE, + ), + ) + ); + + $greeting = CRM_Core_DAO::singleValueQuery("SELECT email_greeting FROM {$tableName}"); + + //Assert email_greeting is not merged + $this->assertNotContains(',', (string) $greeting); + + // delete the export temp table and component table + $sql = "DROP TABLE IF EXISTS {$tableName}"; + CRM_Core_DAO::executeQuery($sql); + } + }