Skip to content

Commit

Permalink
Export class, another function extraction as part of code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jun 8, 2018
1 parent 5f5b9c2 commit d7647f3
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 46 deletions.
62 changes: 37 additions & 25 deletions CRM/Export/BAO/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,31 +620,7 @@ public static function exportComponents(
$contactA = 'contact_id_b';
$contactB = 'contact_id_a';
}
if ($exportMode == CRM_Export_Form_Select::CONTACT_EXPORT) {
$relIDs = $ids;
}
elseif ($exportMode == CRM_Export_Form_Select::ACTIVITY_EXPORT) {
$sourceID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Source');
$dao = CRM_Core_DAO::executeQuery("
SELECT contact_id FROM civicrm_activity_contact
WHERE activity_id IN ( " . implode(',', $ids) . ") AND
record_type_id = {$sourceID}
");

while ($dao->fetch()) {
$relIDs[] = $dao->contact_id;
}
}
else {
$component = self::exportComponent($exportMode);

if ($exportMode == CRM_Export_Form_Select::CASE_EXPORT) {
$relIDs = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($ids);
}
else {
$relIDs = CRM_Core_DAO::getContactIDsFromComponent($ids, $component);
}
}
$relIDs = self::getIDsForRelatedContact($ids, $exportMode);

$relationshipJoin = $relationshipClause = '';
if (!$selectAll && $componentTable) {
Expand Down Expand Up @@ -2186,4 +2162,40 @@ private static function fetchRelationshipDetails($relDAO, $value, $field, &$row)
}
}

/**
* Get the ids that we want to get related contact details for.
*
* @param array $ids
* @param int $exportMode
*
* @return array
*/
protected static function getIDsForRelatedContact($ids, $exportMode) {
if ($exportMode == CRM_Export_Form_Select::CONTACT_EXPORT) {
return $ids;
}
if ($exportMode == CRM_Export_Form_Select::ACTIVITY_EXPORT) {
$relIDs = [];
$sourceID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Source');
$dao = CRM_Core_DAO::executeQuery("
SELECT contact_id FROM civicrm_activity_contact
WHERE activity_id IN ( " . implode(',', $ids) . ") AND
record_type_id = {$sourceID}
");

while ($dao->fetch()) {
$relIDs[] = $dao->contact_id;
}
return $relIDs;
}
$component = self::exportComponent($exportMode);

if ($exportMode == CRM_Export_Form_Select::CASE_EXPORT) {
return CRM_Case_BAO_Case::retrieveContactIdsByCaseId($ids);
}
else {
return CRM_Core_DAO::getContactIDsFromComponent($ids, $component);
}
}

}
91 changes: 70 additions & 21 deletions tests/phpunit/CRM/Export/BAO/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
protected $masterAddressID;

public function tearDown() {
$this->quickCleanup(['civicrm_contact', 'civicrm_email', 'civicrm_address']);
$this->quickCleanup(['civicrm_contact', 'civicrm_email', 'civicrm_address', 'civicrm_relationship']);
$this->quickCleanUpFinancialEntities();
parent::tearDown();
}
Expand Down Expand Up @@ -374,26 +374,7 @@ public function testExportPseudoFieldCampaign() {
* This is to ensure that CRM-13995 remains fixed.
*/
public function testExportRelationshipsMergeToHousehold() {
$this->setUpContactExportData();
$householdID = $this->householdCreate(['api.Address.create' => ['city' => 'Portland', 'state_province_id' => 'Maine', 'location_type_id' => 'Home']]);

$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,
]);
list($householdID, $houseHoldTypeID) = $this->setUpHousehold();

$selectedFields = [
['Individual', $houseHoldTypeID . '_a_b', 'state_province', ''],
Expand Down Expand Up @@ -428,6 +409,41 @@ public function testExportRelationshipsMergeToHousehold() {

}

/**
* Test exporting relationships.
*
* This is to ensure that CRM-13995 remains fixed.
*/
public function testExportRelationshipsMergeToHouseholdAllFields() {
list($householdID) = $this->setUpHousehold();
list($tableName) = CRM_Export_BAO_Export::exportComponents(
FALSE,
$this->contactIDs,
[],
NULL,
NULL,
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()) {
$this->assertEquals('Portland', $dao->city);
$this->assertEquals('ME', $dao->state_province);
$this->assertEquals($householdID, $dao->civicrm_primary_id);
$this->assertEquals($householdID, $dao->civicrm_primary_id);
$this->assertEquals('Dear Unit Test Household', $dao->addressee);
$this->assertEquals('Unit Test Household', $dao->display_name);
}
}

/**
* Test master_address_id field.
*/
Expand Down Expand Up @@ -539,4 +555,37 @@ public function testExportDeceasedDoNotMail() {
CRM_Core_DAO::executeQuery($sql);
}

/**
* @return array
*/
protected function setUpHousehold() {
$this->setUpContactExportData();
$householdID = $this->householdCreate([
'api.Address.create' => [
'city' => 'Portland',
'state_province_id' => 'Maine',
'location_type_id' => 'Home'
]
]);

$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,
]);
return array($householdID, $houseHoldTypeID);
}

}

0 comments on commit d7647f3

Please sign in to comment.