Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Export] Convert testGender Export to new test format. #14914

Merged
merged 1 commit into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,32 @@ class CRM_Contact_BAO_Query {
*/
public $_fields;

/**
* Fields hacked for legacy reasons.
*
* Generally where a field has a option group defining it's options we add them to
* the fields array as pseudofields - eg for gender we would add the key 'gender' to fields
* using CRM_Core_DAO::appendPseudoConstantsToFields($fields);
*
* The rendered results would hold an id in the gender_id field and the label in the pseudo 'Gender'
* field. The heading for the pseudofield would come form the the option group name & for the id field
* from the xml.
*
* These fields are handled in a more legacy way - ie overwriting 'gender_id' with the label on output
* via the convertToPseudoNames function. Ideally we would convert them but they would then need to be fixed
* in some other places & there are also some issues around the name (ie. Gender currently has the label in the
* schema 'Gender' so adding a second 'Gender' field to search builder & export would be confusing and the standard is
* not fully agreed here.
*
* @var array
*/
protected $legacyHackedFields = [
'gender_id' => 'gender',
'prefix_id' => 'individual_prefix',
'suffix_id' => 'individual_suffix',
'communication_style_id' => 'communication_style',
];

/**
* The cache to translate the option values into labels.
*
Expand Down Expand Up @@ -481,6 +507,10 @@ public function __construct(
}
else {
$this->_fields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE, TRUE, FALSE, !$skipPermission);
// The legacy hacked fields will output as a string rather than their underlying type.
foreach (array_keys($this->legacyHackedFields) as $fieldName) {
$this->_fields[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
}

$fields = CRM_Core_Component::getQueryFields(!$this->_skipPermission);
unset($fields['note']);
Expand Down Expand Up @@ -6032,12 +6062,7 @@ public function convertToPseudoNames(&$dao, $return = FALSE, $usedForAPI = FALSE
}
}
if (!$usedForAPI) {
foreach ([
'gender_id' => 'gender',
'prefix_id' => 'individual_prefix',
'suffix_id' => 'individual_suffix',
'communication_style_id' => 'communication_style',
] as $realField => $labelField) {
foreach ($this->legacyHackedFields as $realField => $labelField) {
// This is a temporary routine for handling these fields while
// we figure out how to handled them based on metadata in
/// export and search builder. CRM-19815, CRM-19830.
Expand Down
17 changes: 8 additions & 9 deletions CRM/Export/BAO/ExportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1403,18 +1403,17 @@ public function getSqlColumnDefinition($fieldName, $columnName) {
}

$queryFields = $this->getQueryFields();
$lookUp = ['prefix_id', 'suffix_id'];
// @todo remove the enotice avoidance here, ensure all columns are declared.
// tests will fail on the enotices until they all are & then all the 'else'
// below can go.
$fieldSpec = $queryFields[$columnName] ?? [];

// set the sql columns
if (isset($queryFields[$columnName]['type'])) {
switch ($queryFields[$columnName]['type']) {
if (isset($fieldSpec['type'])) {
switch ($fieldSpec['type']) {
case CRM_Utils_Type::T_INT:
case CRM_Utils_Type::T_BOOLEAN:
if (in_array($columnName, $lookUp)) {
return "$fieldName varchar(255)";
}
else {
return "$fieldName varchar(16)";
}
return "$fieldName varchar(16)";

case CRM_Utils_Type::T_STRING:
if (isset($queryFields[$columnName]['maxlength'])) {
Expand Down
18 changes: 11 additions & 7 deletions tests/phpunit/CRM/Export/BAO/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,12 +522,16 @@ public function testExportPrimaryAddress($isPrimaryOnly) {
* does NOT retain the gender of the former.
*
* @throws \CRM_Core_Exception
* @throws \League\Csv\Exception
*/
public function testExportPseudoField() {
$this->setUpContactExportData();
$selectedFields = [['Individual', 'gender_id']];
list($tableName, $sqlColumns) = $this->doExport($selectedFields, $this->contactIDs);
$this->assertEquals('Female,', CRM_Core_DAO::singleValueQuery("SELECT GROUP_CONCAT(gender_id) FROM {$tableName}"));
$this->callAPISuccess('OptionValue', 'create', ['option_group_id' => 'gender', 'name' => 'Really long string', 'value' => 678, 'label' => 'Really long string']);
$selectedFields = [['contact_type' => 'Individual', 'name' => 'gender_id']];
$this->callAPISuccess('Contact', 'create', ['id' => $this->contactIDs[0], 'gender_id' => 678]);
$this->doExportTest(['fields' => $selectedFields, 'ids' => $this->contactIDs]);
$row = $this->csv->fetchOne();
$this->assertEquals('Really long string', $row['Gender']);
}

/**
Expand Down Expand Up @@ -2484,12 +2488,12 @@ protected function getBasicSqlColumnDefinition($isContactExport) {
'prefix_id' => 'prefix_id varchar(255)',
'suffix_id' => 'suffix_id varchar(255)',
'formal_title' => 'formal_title varchar(64)',
'communication_style_id' => 'communication_style_id varchar(16)',
'communication_style_id' => 'communication_style_id varchar(255)',
'email_greeting_id' => 'email_greeting_id varchar(16)',
'postal_greeting_id' => 'postal_greeting_id varchar(16)',
'addressee_id' => 'addressee_id varchar(16)',
'job_title' => 'job_title varchar(255)',
'gender_id' => 'gender_id varchar(16)',
'gender_id' => 'gender_id varchar(255)',
'birth_date' => 'birth_date varchar(32)',
'is_deceased' => 'is_deceased varchar(16)',
'deceased_date' => 'deceased_date varchar(32)',
Expand Down Expand Up @@ -2670,12 +2674,12 @@ public function getContributionSqlColumns() {
'prefix_id' => 'prefix_id varchar(255)',
'suffix_id' => 'suffix_id varchar(255)',
'formal_title' => 'formal_title varchar(64)',
'communication_style_id' => 'communication_style_id varchar(16)',
'communication_style_id' => 'communication_style_id varchar(255)',
'email_greeting_id' => 'email_greeting_id varchar(16)',
'postal_greeting_id' => 'postal_greeting_id varchar(16)',
'addressee_id' => 'addressee_id varchar(16)',
'job_title' => 'job_title varchar(255)',
'gender_id' => 'gender_id varchar(16)',
'gender_id' => 'gender_id varchar(255)',
'birth_date' => 'birth_date varchar(32)',
'is_deceased' => 'is_deceased varchar(16)',
'deceased_date' => 'deceased_date varchar(32)',
Expand Down