Unverified Commit 4461a858 authored by colemanw's avatar colemanw Committed by GitHub

Merge pull request #14817 from eileenmcnaughton/export_up

[TEST] export - add unit test covering merge to same address addressee handling
parents bdf5cf87 d97cefdc
......@@ -93,6 +93,13 @@ class CRM_Export_BAO_ExportProcessor {
*/
protected $additionalFieldsForSameAddressMerge = [];
/**
* Fields used for merging same contacts.
*
* @var array
*/
protected $contactGreetingFields = [];
/**
* Get additional non-visible fields for address merge purposes.
*
......@@ -330,6 +337,38 @@ class CRM_Export_BAO_ExportProcessor {
$this->temporaryTable = $temporaryTable;
}
protected $postalGreetingTemplate;
/**
* @return mixed
*/
public function getPostalGreetingTemplate() {
return $this->postalGreetingTemplate;
}
/**
* @param mixed $postalGreetingTemplate
*/
public function setPostalGreetingTemplate($postalGreetingTemplate) {
$this->postalGreetingTemplate = $postalGreetingTemplate;
}
/**
* @return mixed
*/
public function getAddresseeGreetingTemplate() {
return $this->addresseeGreetingTemplate;
}
/**
* @param mixed $addresseeGreetingTemplate
*/
public function setAddresseeGreetingTemplate($addresseeGreetingTemplate) {
$this->addresseeGreetingTemplate = $addresseeGreetingTemplate;
}
protected $addresseeGreetingTemplate;
/**
* CRM_Export_BAO_ExportProcessor constructor.
*
......@@ -1821,12 +1860,11 @@ class CRM_Export_BAO_ExportProcessor {
$contact = NULL;
$greetingFields = [
'postal_greeting',
'addressee',
'postal_greeting' => $this->getPostalGreetingTemplate(),
'addressee' => $this->getAddresseeGreetingTemplate(),
];
foreach ($greetingFields as $greeting) {
foreach ($greetingFields as $greeting => $greetingLabel) {
if (!empty($exportParams[$greeting])) {
$greetingLabel = $exportParams[$greeting];
if (empty($contact)) {
$values = [
'id' => $contactId,
......@@ -1857,7 +1895,6 @@ class CRM_Export_BAO_ExportProcessor {
* @return array
*/
public function buildMasterCopyArray($sql, $exportParams, $sharedAddress = FALSE) {
static $contactGreetingTokens = [];
$addresseeOptions = CRM_Core_OptionGroup::values('addressee');
$postalOptions = CRM_Core_OptionGroup::values('postal_greeting');
......@@ -1873,24 +1910,24 @@ class CRM_Export_BAO_ExportProcessor {
$copyAddressee = $dao->copy_addressee;
if (!$sharedAddress) {
if (!isset($contactGreetingTokens[$dao->master_contact_id])) {
$contactGreetingTokens[$dao->master_contact_id] = $this->replaceMergeTokens($dao->master_contact_id, $exportParams);
if (!isset($this->contactGreetingFields[$dao->master_contact_id])) {
$this->contactGreetingFields[$dao->master_contact_id] = $this->replaceMergeTokens($dao->master_contact_id, $exportParams);
}
$masterPostalGreeting = CRM_Utils_Array::value('postal_greeting',
$contactGreetingTokens[$dao->master_contact_id], $dao->master_postal_greeting
$this->contactGreetingFields[$dao->master_contact_id], $dao->master_postal_greeting
);
$masterAddressee = CRM_Utils_Array::value('addressee',
$contactGreetingTokens[$dao->master_contact_id], $dao->master_addressee
$this->contactGreetingFields[$dao->master_contact_id], $dao->master_addressee
);
if (!isset($contactGreetingTokens[$dao->copy_contact_id])) {
$contactGreetingTokens[$dao->copy_contact_id] = $this->replaceMergeTokens($dao->copy_contact_id, $exportParams);
$this->contactGreetingFields[$dao->copy_contact_id] = $this->replaceMergeTokens($dao->copy_contact_id, $exportParams);
}
$copyPostalGreeting = CRM_Utils_Array::value('postal_greeting',
$contactGreetingTokens[$dao->copy_contact_id], $dao->copy_postal_greeting
$this->contactGreetingFields[$dao->copy_contact_id], $dao->copy_postal_greeting
);
$copyAddressee = CRM_Utils_Array::value('addressee',
$contactGreetingTokens[$dao->copy_contact_id], $dao->copy_addressee
$this->contactGreetingFields[$dao->copy_contact_id], $dao->copy_addressee
);
}
......@@ -1977,6 +2014,13 @@ class CRM_Export_BAO_ExportProcessor {
}
}
}
if (!empty($exportParams['postal_greeting'])) {
$this->setPostalGreetingTemplate($exportParams['postal_greeting']);
}
if (!empty($exportParams['addressee'])) {
$this->setAddresseeGreetingTemplate($exportParams['addressee']);
}
$tableName = $this->getTemporaryTable();
// check if any records are present based on if they have used shared address feature,
// and not based on if city / state .. matches.
......
......@@ -1089,20 +1089,8 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
* @throws \League\Csv\Exception
*/
public function testMergeSameAddress() {
$this->setUpContactExportData();
$this->contactIDs[] = $contact3 = $this->individualCreate(['first_name' => 'Sarah', 'last_name' => 'Smith', 'prefix_id' => 'Dr.']);
// Create address for contact A.
$params = [
'contact_id' => $contact3,
'location_type_id' => 'Home',
'street_address' => 'Ambachtstraat 23',
'postal_code' => '6971 BN',
'country_id' => '1152',
'city' => 'Brummen',
'is_primary' => 1,
];
$this->callAPISuccess('address', 'create', $params);
$this->doExportTest(['mergeSameAddress' => TRUE, 'ids' => $this->contactIDs]);
$this->setUpContactSameAddressExportData();
$this->doExportTest(['mergeSameAddress' => TRUE]);
// ie 2 merged, one extra.
$this->assertCount(2, $this->csv);
$expected = [
......@@ -1193,6 +1181,41 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
$this->assertExpectedOutput($expected, $this->csv->fetchOne());
}
/**
* Tests the options for greeting templates when choosing to merge same address.
*
* @throws \CRM_Core_Exception
* @throws \League\Csv\Exception
*/
public function testMergeSameAddressGreetingOptions() {
$this->setUpContactSameAddressExportData();
$this->callAPISuccess('OptionValue', 'create', [
'option_group_id' => 'postal_greeting',
'label' => '{contact.individual_suffix} {contact.last_name} and first is {contact.first_name} ',
// This hard coded number makes it available for export use.
'filter' => 4,
]);
$this->doExportTest([
'mergeSameAddress' => TRUE,
'exportParams' => [
'postal_greeting' => '2',
'postal_greeting_other' => '',
'addressee' => '2',
'addressee_other' => 'random string {contact.display_name}',
'mergeOption' => '1',
'additional_group' => '',
'mapping' => '',
],
]);
$this->assertExpectedOutput([
'Addressee' => 'random string Mr. Anthony Anderson II, Dr. Sarah Smith II',
'Email Greeting' => 'II Anderson and first is Anthony , II Smith Sarah ',
'Postal Greeting' => 'II Anderson and first is Anthony , II Smith Sarah ',
], $this->csv->fetchOne());
// 3 contacts merged to 2.
$this->assertCount(2, $this->csv);
}
/**
* Test that deceased and do not mail contacts are removed from contacts before
*
......@@ -2870,20 +2893,22 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
protected function doExportTest($params) {
$this->startCapturingOutput();
try {
$defaultClause = (empty($params['ids']) ? NULL : "contact_a.id IN (" . implode(',', $params['ids']) . ")");
$exportMode = CRM_Utils_Array::value('exportMode', $params, CRM_Export_Form_Select::CONTACT_EXPORT);
$ids = CRM_Utils_Array::value('ids', $params, ($exportMode === CRM_Export_Form_Select::CONTACT_EXPORT ? $this->contactIDs : []));
$defaultClause = (empty($ids) ? NULL : "contact_a.id IN (" . implode(',', $ids) . ")");
CRM_Export_BAO_Export::exportComponents(
CRM_Utils_Array::value('selectAll', $params, (empty($params['fields']))),
CRM_Utils_Array::value('ids', $params, []),
$ids,
CRM_Utils_Array::value('params', $params, []),
CRM_Utils_Array::value('order', $params),
CRM_Utils_Array::value('fields', $params, []),
CRM_Utils_Array::value('moreReturnProperties', $params),
CRM_Utils_Array::value('exportMode', $params, CRM_Export_Form_Select::CONTACT_EXPORT),
$exportMode,
CRM_Utils_Array::value('componentClause', $params, $defaultClause),
CRM_Utils_Array::value('componentTable', $params),
CRM_Utils_Array::value('mergeSameAddress', $params, FALSE),
CRM_Utils_Array::value('mergeSameHousehold', $params, FALSE)
CRM_Utils_Array::value('mergeSameHousehold', $params, FALSE),
CRM_Utils_Array::value('exportParams', $params, [])
);
}
catch (CRM_Core_Exception_PrematureExitException $e) {
......@@ -2955,4 +2980,25 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
], $result);
}
/**
* Set up contacts which will be merged with the same address option.
*
* @throws \CRM_Core_Exception
*/
protected function setUpContactSameAddressExportData() {
$this->setUpContactExportData();
$this->contactIDs[] = $contact3 = $this->individualCreate(['first_name' => 'Sarah', 'last_name' => 'Smith', 'prefix_id' => 'Dr.']);
// Create address for contact A.
$params = [
'contact_id' => $contact3,
'location_type_id' => 'Home',
'street_address' => 'Ambachtstraat 23',
'postal_code' => '6971 BN',
'country_id' => '1152',
'city' => 'Brummen',
'is_primary' => 1,
];
$this->callAPISuccess('address', 'create', $params);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment