From 994a070c029506cea34c8f68ffe9c59a7c90d0a0 Mon Sep 17 00:00:00 2001 From: jitendrapurohit Date: Wed, 7 Sep 2016 15:07:38 +0530 Subject: [PATCH] CRM-17681 - "Master address belongs to" field gives the master address id rather than the contact name --- CRM/Contact/BAO/Query.php | 1 + CRM/Export/BAO/Export.php | 16 ++--- tests/phpunit/CRM/Export/BAO/ExportTest.php | 70 ++++++++++++++++++++- 3 files changed, 78 insertions(+), 9 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index eeb4281951..e351444690 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -369,6 +369,7 @@ class CRM_Contact_BAO_Query { 'email', 'im', 'address_name', + 'master_id', ); /** diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index 6077d1a83d..e180c596b0 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -810,10 +810,10 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c elseif ($field == 'provider_id' || $field == 'im_provider') { $fieldValue = CRM_Utils_Array::value($fieldValue, $imProviders); } - elseif ($field == 'master_id') { + elseif (strstr($field, 'master_id')) { $masterAddressId = NULL; - if (isset($iterationDAO->master_id)) { - $masterAddressId = $iterationDAO->master_id; + if (isset($iterationDAO->$field)) { + $masterAddressId = $iterationDAO->$field; } // get display name of contact that address is shared. $fieldValue = CRM_Contact_BAO_Contact::getMasterDisplayName($masterAddressId, $iterationDAO->contact_id); @@ -1127,14 +1127,16 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c if (empty($exportParams['suppress_csv_for_testing'])) { self::writeCSVFromTable($exportTempTable, $headerRows, $sqlColumns, $exportMode); } + else { + // return tableName and sqlColumns in test context + return array($exportTempTable, $sqlColumns); + } // delete the export temp table and component table $sql = "DROP TABLE IF EXISTS {$exportTempTable}"; CRM_Core_DAO::executeQuery($sql); - // Do not exit in test context. - if (empty($exportParams['suppress_csv_for_testing'])) { - CRM_Utils_System::civiExit(); - } + + CRM_Utils_System::civiExit(); } else { CRM_Core_Error::fatal(ts('No records to export')); diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php index 65ff7f4fbe..b40441669b 100644 --- a/tests/phpunit/CRM/Export/BAO/ExportTest.php +++ b/tests/phpunit/CRM/Export/BAO/ExportTest.php @@ -24,7 +24,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * Basic test to ensure the exportComponents function completes without error. */ public function testExportComponentsNull() { - CRM_Export_BAO_Export::exportComponents( + list($tableName, $sqlColumns) = CRM_Export_BAO_Export::exportComponents( TRUE, array(), array(), @@ -41,6 +41,10 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 'suppress_csv_for_testing' => TRUE, ) ); + + // delete the export temp table and component table + $sql = "DROP TABLE IF EXISTS {$tableName}"; + CRM_Core_DAO::executeQuery($sql); } /** @@ -59,7 +63,8 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { array('Individual', 'email', 1), array('Contribution', 'trxn_id'), ); - CRM_Export_BAO_Export::exportComponents( + + list($tableName, $sqlColumns) = CRM_Export_BAO_Export::exportComponents( TRUE, $this->contributionIDs, array(), @@ -76,6 +81,10 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { 'suppress_csv_for_testing' => TRUE, ) ); + + // delete the export temp table and component table + $sql = "DROP TABLE IF EXISTS {$tableName}"; + CRM_Core_DAO::executeQuery($sql); } /** * Test the function that extracts the arrays used to structure the output. @@ -158,4 +167,61 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { $this->contactIDs[] = $this->individualCreate(); } + public function testExportMasterAddress() { + $contactA = $this->individualCreate(array(), 0); + $contactB = $this->individualCreate(array(), 1); + + //create address for contact A + $params = array( + 'contact_id' => $contactA, + 'location_type_id' => 'Home', + 'street_address' => 'Ambachtstraat 23', + 'postal_code' => '6971 BN', + 'country_id' => '1152', + 'city' => 'Brummen', + 'is_primary' => 1, + ); + $result = $this->callAPISuccess('address', 'create', $params); + $addressId = $result['id']; + + //share address with contact B + $result = $this->callAPISuccess('address', 'create', array( + 'contact_id' => $contactB, + 'location_type_id' => "Home", + 'master_id' => $addressId, + )); + + //export the master address for contact B + $selectedFields = array( + array('Individual', 'master_id', 1), + ); + list($tableName, $sqlColumns) = CRM_Export_BAO_Export::exportComponents( + TRUE, + array($contactB), + array(), + NULL, + $selectedFields, + NULL, + CRM_Export_Form_Select::CONTACT_EXPORT, + "contact_a.id IN ({$contactB})", + NULL, + FALSE, + FALSE, + array( + 'exportOption' => CRM_Export_Form_Select::CONTACT_EXPORT, + 'suppress_csv_for_testing' => TRUE, + ) + ); + $field = key($sqlColumns); + + //assert the exported result + $masterName = CRM_Core_DAO::singleValueQuery("SELECT {$field} FROM {$tableName}"); + $displayName = CRM_Contact_BAO_Contact::getMasterDisplayName(NULL, $contactB); + $this->assertEquals($displayName, $masterName); + + // delete the export temp table and component table + $sql = "DROP TABLE IF EXISTS {$tableName}"; + CRM_Core_DAO::executeQuery($sql); + } + } -- 2.25.1