From b0eb1d74fcad845dba3e3324355781b0b616c899 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 22 Feb 2018 15:21:47 +1300 Subject: [PATCH] Refactor towards fixing / reviewing export issues --- CRM/Export/BAO/Export.php | 33 +++++------- tests/phpunit/CRM/Export/BAO/ExportTest.php | 59 +++++++++++++++++++++ 2 files changed, 72 insertions(+), 20 deletions(-) diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index 31c4218426..d9fab44146 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -348,20 +348,14 @@ class CRM_Export_BAO_Export { ); foreach ($fields as $key => $value) { - $phoneTypeId = $imProviderId = $relationField = NULL; + $relationField = NULL; $relationshipTypes = $fieldName = CRM_Utils_Array::value(1, $value); if (!$fieldName) { continue; } - // get phoneType id and IM service provider id separately - if ($fieldName == 'phone') { - $phoneTypeId = CRM_Utils_Array::value(3, $value); - } - elseif ($fieldName == 'im') { - $imProviderId = CRM_Utils_Array::value(3, $value); - } - if (array_key_exists($relationshipTypes, $contactRelationshipTypes)) { + if (array_key_exists($relationshipTypes, $contactRelationshipTypes) && (!empty($value[2]) || empty($value[4]))) { + $relPhoneTypeId = $relIMProviderId = NULL; if (!empty($value[2])) { $relationField = CRM_Utils_Array::value(2, $value); if (trim(CRM_Utils_Array::value(3, $value))) { @@ -388,11 +382,6 @@ class CRM_Export_BAO_Export { $relIMProviderId = CRM_Utils_Array::value(6, $value); } } - } - - $locTypeId = CRM_Utils_Array::value(2, $value); - - if ($relationField) { if (in_array($relationField, $locationTypeFields) && is_numeric($relLocTypeId)) { if ($relPhoneTypeId) { $returnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]]['phone-' . $relPhoneTypeId] = 1; @@ -403,18 +392,22 @@ class CRM_Export_BAO_Export { else { $returnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]][$relationField] = 1; } - $relPhoneTypeId = $relIMProviderId = NULL; } else { $returnProperties[$relationshipTypes][$relationField] = 1; } } - elseif (is_numeric($locTypeId)) { - if ($phoneTypeId) { - $returnProperties['location'][$locationTypes[$locTypeId]]['phone-' . $phoneTypeId] = 1; + + if ($relationField) { + // already handled. + } + elseif (is_numeric(CRM_Utils_Array::value(2, $value))) { + $locTypeId = $value[2]; + if ($fieldName == 'phone') { + $returnProperties['location'][$locationTypes[$locTypeId]]['phone-' . CRM_Utils_Array::value(3, $value)] = 1; } - elseif ($imProviderId) { - $returnProperties['location'][$locationTypes[$locTypeId]]['im-' . $imProviderId] = 1; + elseif ($fieldName == 'im') { + $returnProperties['location'][$locationTypes[$locTypeId]]['im-' . CRM_Utils_Array::value(3, $value)] = 1; } else { $returnProperties['location'][$locationTypes[$locTypeId]][$fieldName] = 1; diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php index ccaba5a075..6ff501f82a 100644 --- a/tests/phpunit/CRM/Export/BAO/ExportTest.php +++ b/tests/phpunit/CRM/Export/BAO/ExportTest.php @@ -368,6 +368,65 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { $this->assertEquals('Big campaign,', CRM_Core_DAO::singleValueQuery("SELECT GROUP_CONCAT(contribution_campaign_title) FROM {$tableName}")); } + /** + * Test exporting relationships. + * + * This is to ensure that CRM-13995 remains fixed. + */ + public function testExportRelationshipsMergeToHousehold() { + $this->setUpContactExportData(); + $householdID = $this->householdCreate(['city' => 'Portland', 'state_province_id' => 'Oregan']); + + $relationshipTypes = $this->callAPISuccess('RelationshipType', 'get', [])['values']; + $houseHoldTypeID = NULL; + foreach ($relationshipTypes as $id => $relationshipType) { + if ($relationshipType['name_a_b'] === 'Household Member of') { + $houseHoldTypeID = $relationshipType['id']; + } + } + $this->callAPISuccess('Relationship', 'create', [ + 'contact_id_a' => $this->contactIDs[0], + 'contact_id_b' => $householdID, + 'relationship_type_id' => $houseHoldTypeID, + ]); + $this->callAPISuccess('Relationship', 'create', [ + 'contact_id_a' => $this->contactIDs[1], + 'contact_id_b' => $householdID, + 'relationship_type_id' => $houseHoldTypeID, + ]); + + $selectedFields = [ + ['Individual', $houseHoldTypeID . '_a_b', 'state_province', ''], + ['Individual', $houseHoldTypeID . '_a_b', 'city', ''], + ['Individual', 'city', ''], + ['Individual', 'state_province', ''], + ]; + list($tableName) = CRM_Export_BAO_Export::exportComponents( + FALSE, + $this->contactIDs, + [], + NULL, + $selectedFields, + NULL, + CRM_Export_Form_Select::CONTACT_EXPORT, + "contact_a.id IN (" . implode(",", $this->contactIDs) . ")", + NULL, + FALSE, + TRUE, + [ + 'exportOption' => CRM_Export_Form_Select::CONTACT_EXPORT, + 'suppress_csv_for_testing' => TRUE, + ] + ); + $dao = CRM_Core_DAO::executeQuery("SELECT * FROM {$tableName}"); + while ($dao->fetch()) { + // Do some checks here + // $this->assertEquals('Portland', $dao->city); + // $this->assertEquals('Oregan', $dao0>state_province); + } + + } + /** * Test master_address_id field. */ -- 2.25.1