From 714ab07093ca650d860ee9e68950c235f7fd78a4 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 29 Jul 2019 00:53:17 +1200 Subject: [PATCH] Convert testGender Export to new test format. This includes a tangental cleanup & fix whereby the test covers a label longer than 16 char & the code for prefix_id & suffix_id is consolidated by handling the special casing in the metadata declaration rather than in multiple places. --- CRM/Contact/BAO/Query.php | 37 +++++++++++++++++---- CRM/Export/BAO/ExportProcessor.php | 17 +++++----- tests/phpunit/CRM/Export/BAO/ExportTest.php | 18 ++++++---- 3 files changed, 50 insertions(+), 22 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 970922ec82..7a6b045758 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -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. * @@ -481,6 +507,10 @@ class CRM_Contact_BAO_Query { } 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']); @@ -6032,12 +6062,7 @@ AND displayRelType.is_active = 1 } } 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. diff --git a/CRM/Export/BAO/ExportProcessor.php b/CRM/Export/BAO/ExportProcessor.php index 30bd8e1f7d..5f5a77e653 100644 --- a/CRM/Export/BAO/ExportProcessor.php +++ b/CRM/Export/BAO/ExportProcessor.php @@ -1403,18 +1403,17 @@ class CRM_Export_BAO_ExportProcessor { } $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'])) { diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php index 30b5d9649a..a1ca14a251 100644 --- a/tests/phpunit/CRM/Export/BAO/ExportTest.php +++ b/tests/phpunit/CRM/Export/BAO/ExportTest.php @@ -522,12 +522,16 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { * 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']); } /** @@ -2484,12 +2488,12 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { '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)', @@ -2670,12 +2674,12 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { '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)', -- 2.25.1