Skip to content

Commit

Permalink
Merge pull request #13275 from eileenmcnaughton/export_more_im
Browse files Browse the repository at this point in the history
[REF] Export add test for phone details, fix phone_type_id
  • Loading branch information
seamuslee001 authored Dec 17, 2018
2 parents c95f771 + 001a515 commit 0237527
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 21 deletions.
8 changes: 6 additions & 2 deletions CRM/Export/BAO/ExportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,9 @@ public function getTransformedFieldValue($field, $iterationDAO, $fieldValue, $me
return $i18n->crm_translate($fieldValue, $metadata[$field]);
}
if (!empty($metadata[$field]['pseudoconstant'])) {
if (!empty($metadata[$field]['bao'])) {
return CRM_Core_PseudoConstant::getLabel($metadata[$field]['bao'], $metadata[$field]['name'], $fieldValue);
}
// This is not our normal syntax for pseudoconstants but I am a bit loath to
// call an external function until sure it is not increasing php processing given this
// may be iterated 100,000 times & we already have the $imProvider var loaded.
Expand Down Expand Up @@ -1188,7 +1191,7 @@ public function getMungedFieldName($field) {
* @return string
*/
protected function getOutputSpecificationIndex($key, $relationshipType, $locationType, $entityLabel) {
if ($locationType || $entityLabel || $key === 'im') {
if ($entityLabel || $key === 'im') {
// Just cos that's the history...
if ($key !== 'master_id') {
$key = $this->getHeaderForRow($key);
Expand Down Expand Up @@ -1241,8 +1244,9 @@ protected function getOutputSpecificationLabel($key, $relationshipType, $locatio
* @return string
*/
protected function getOutputSpecificationFieldKey($key, $relationshipType, $locationType, $entityLabel) {
if ($relationshipType || $entityLabel || $key === 'im') {
if ($entityLabel || $key === 'im') {
if ($key !== 'state_province' && $key !== 'id') {
// @todo - test removing this - indexing by $key should be fine...
$key = $this->getHeaderForRow($key);
}
}
Expand Down
118 changes: 99 additions & 19 deletions tests/phpunit/CRM/Export/BAO/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,23 +416,7 @@ public function getPrimarySearchOptions() {
public function testExportPseudoField() {
$this->setUpContactExportData();
$selectedFields = [['Individual', 'gender_id']];
list($tableName, $sqlColumns) = CRM_Export_BAO_Export::exportComponents(
TRUE,
$this->contactIDs[1],
array(),
NULL,
$selectedFields,
NULL,
CRM_Export_Form_Select::CONTACT_EXPORT,
"contact_a.id IN (" . implode(",", $this->contactIDs) . ")",
NULL,
FALSE,
FALSE,
array(
'exportOption' => CRM_Export_Form_Select::CONTACT_EXPORT,
'suppress_csv_for_testing' => TRUE,
)
);
list($tableName, $sqlColumns) = $this->doExport($selectedFields, $this->contactIDs);
$this->assertEquals('Female,', CRM_Core_DAO::singleValueQuery("SELECT GROUP_CONCAT(gender_id) FROM {$tableName}"));
}

Expand Down Expand Up @@ -833,6 +817,101 @@ public function testExportIMData() {

}


/**
* Test phone data export.
*
* Less over the top complete than the im test.
*/
public function testExportPhoneData() {
$this->contactIDs[] = $this->individualCreate();
$this->contactIDs[] = $this->individualCreate();
$locationTypes = ['Billing' => 'Billing', 'Home' => 'Home'];
$phoneTypes = ['Mobile', 'Phone'];
foreach ($this->contactIDs as $contactID) {
$this->callAPISuccess('Phone', 'create', [
'contact_id' => $contactID,
'location_type_id' => 'Billing',
'phone_type_id' => 'Mobile',
'phone' => 'Billing' . 'Mobile' . $contactID,
'is_primary' => 1,
]);
$this->callAPISuccess('Phone', 'create', [
'contact_id' => $contactID,
'location_type_id' => 'Home',
'phone_type_id' => 'Phone',
'phone' => 'Home' . 'Phone' . $contactID,
]);
}

$relationships = [
$this->contactIDs[1] => ['label' => 'Spouse of']
];

foreach ($relationships as $contactID => $relationshipType) {
$relationshipTypeID = $this->callAPISuccess('RelationshipType', 'getvalue', ['label_a_b' => $relationshipType['label'], 'return' => 'id']);
$result = $this->callAPISuccess('Relationship', 'create', [
'contact_id_a' => $this->contactIDs[0],
'relationship_type_id' => $relationshipTypeID,
'contact_id_b' => $contactID
]);
$relationships[$contactID]['id'] = $result['id'];
$relationships[$contactID]['relationship_type_id'] = $relationshipTypeID;
}

$fields = [['Individual', 'contact_id']];
// ' ' denotes primary location type.
foreach (array_keys(array_merge($locationTypes, [' ' => ['Primary']])) as $locationType) {
$fields[] = [
'Individual',
'phone',
CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'location_type_id', $locationType),
];
$fields[] = [
'Individual',
'phone_type_id',
CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'location_type_id', $locationType),
];
foreach ($relationships as $contactID => $relationship) {
$fields[] = [
'Individual',
$relationship['relationship_type_id'] . '_a_b',
'phone_type_id',
CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'location_type_id', $locationType),
];
}
foreach ($phoneTypes as $phoneType) {
$fields[] = [
'Individual',
'phone',
CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'location_type_id', $locationType),
CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'phone_type_id', $phoneType),
];
foreach ($relationships as $contactID => $relationship) {
$fields[] = [
'Individual',
$relationship['relationship_type_id'] . '_a_b',
'phone_type_id',
CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'location_type_id', $locationType),
CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'phone_type_id', $phoneType),
];
}
}
}
list($tableName) = $this->doExport($fields, $this->contactIDs[0]);

$dao = CRM_Core_DAO::executeQuery('SELECT * FROM ' . $tableName);
while ($dao->fetch()) {
// note there is some chance these might be random on some mysql co
$this->assertEquals('BillingMobile3', $dao->billing_phone_mobile);
$this->assertEquals('', $dao->billing_phone_phone);
$relField = '2_a_b_phone_type_id';
$this->assertEquals('Phone', $dao->$relField);
$this->assertEquals('Mobile', $dao->phone_type_id);
$this->assertEquals('Mobile', $dao->billing_phone_type_id);
}
}

/**
* Export City against multiple location types.
*/
Expand Down Expand Up @@ -1114,15 +1193,16 @@ protected function setUpHousehold() {
* @return array
*/
protected function doExport($selectedFields, $id, $exportMode = CRM_Export_Form_Select::CONTACT_EXPORT) {
$ids = (array) $id;
list($tableName, $sqlColumns) = CRM_Export_BAO_Export::exportComponents(
TRUE,
array($id),
$ids,
array(),
NULL,
$selectedFields,
NULL,
$exportMode,
"contact_a.id IN ({$id})",
"contact_a.id IN (" . implode(',', $ids) . ")",
NULL,
FALSE,
FALSE,
Expand Down

0 comments on commit 0237527

Please sign in to comment.