From b11ece178357c8d79eb8fcf6fd0eb02d7dce4cb3 Mon Sep 17 00:00:00 2001 From: eileen Date: Sat, 15 Aug 2020 10:47:54 +1200 Subject: [PATCH] Export fix on long custom fields Fixes a code miss wereby improved metadata meant the code intended to handle custom fields was not being hit, adds tests for fixed variant of a long option value --- CRM/Export/BAO/ExportProcessor.php | 42 +++++++------------ tests/phpunit/CRM/Export/BAO/ExportTest.php | 6 +++ .../CRMTraits/Custom/CustomDataTrait.php | 21 ++++++++++ 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/CRM/Export/BAO/ExportProcessor.php b/CRM/Export/BAO/ExportProcessor.php index 861c687abf01..8798d95371fc 100644 --- a/CRM/Export/BAO/ExportProcessor.php +++ b/CRM/Export/BAO/ExportProcessor.php @@ -1457,11 +1457,22 @@ public function getSqlColumnDefinition($fieldName, $columnName) { return "`$fieldName` varchar(16)"; case CRM_Utils_Type::T_STRING: - if (isset($queryFields[$columnName]['maxlength'])) { - return "`$fieldName` varchar({$queryFields[$columnName]['maxlength']})"; + if (isset($fieldSpec['maxlength'])) { + return "`$fieldName` varchar({$fieldSpec['maxlength']})"; } - else { - return "`$fieldName` varchar(255)"; + $dataType = $fieldSpec['data_type'] ?? ''; + // set the sql columns for custom data + switch ($dataType) { + case 'String': + // May be option labels, which could be up to 512 characters + $length = max(512, CRM_Utils_Array::value('text_length', $fieldSpec)); + return "`$fieldName` varchar($length)"; + + case 'Memo': + return "`$fieldName` text"; + + default: + return "`$fieldName` varchar(255)"; } case CRM_Utils_Type::T_TEXT: @@ -1501,28 +1512,7 @@ public function getSqlColumnDefinition($fieldName, $columnName) { return "`$fieldName` text"; } else { - // set the sql columns for custom data - if (isset($queryFields[$columnName]['data_type'])) { - - switch ($queryFields[$columnName]['data_type']) { - case 'String': - // May be option labels, which could be up to 512 characters - $length = max(512, CRM_Utils_Array::value('text_length', $queryFields[$columnName])); - return "`$fieldName` varchar($length)"; - - case 'Link': - return "`$fieldName` varchar(255)"; - - case 'Memo': - return "`$fieldName` text"; - - default: - return "`$fieldName` varchar(255)"; - } - } - else { - return "`$fieldName` text"; - } + return "`$fieldName` text"; } } } diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php index b01ceb70a0a6..4ef7c1cc7dd6 100644 --- a/tests/phpunit/CRM/Export/BAO/ExportTest.php +++ b/tests/phpunit/CRM/Export/BAO/ExportTest.php @@ -679,7 +679,9 @@ public function testExportRelationshipsMergeToHouseholdAllFields() { /** * Test custom data exporting. * + * @throws \API_Exception * @throws \CRM_Core_Exception + * @throws \Civi\API\Exception\UnauthorizedException * @throws \League\Csv\Exception */ public function testExportCustomData() { @@ -690,17 +692,20 @@ public function testExportCustomData() { for ($i = 0; $i < 70; $i++) { $longString .= 'Blah'; } + $this->addOptionToCustomField('select_string', ['label' => $longString, 'name' => 'blah']); $this->callAPISuccess('Contact', 'create', [ 'id' => $this->contactIDs[1], $this->getCustomFieldName('text') => $longString, $this->getCustomFieldName('country') => 'LA', + $this->getCustomFieldName('select_string') => 'blah', 'api.Address.create' => ['location_type_id' => 'Billing', 'city' => 'Waipu'], ]); $selectedFields = [ ['name' => 'city', 'location_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'location_type_id', 'Billing')], ['name' => $this->getCustomFieldName('text')], ['name' => $this->getCustomFieldName('country')], + ['name' => $this->getCustomFieldName('select_string')], ]; $this->doExportTest([ @@ -711,6 +716,7 @@ public function testExportCustomData() { $this->assertEquals($longString, $row['Enter text here']); $this->assertEquals('Waipu', $row['Billing-City']); $this->assertEquals("Lao People's Democratic Republic", $row['Country']); + $this->assertEquals($longString, $row['Pick Color']); } /** diff --git a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php index 759d62a9d17b..251326c7ce75 100644 --- a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php +++ b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php @@ -10,6 +10,8 @@ */ use Civi\Api4\CustomGroup; +use Civi\Api4\CustomField; +use Civi\Api4\OptionValue; /** * Trait Custom Data trait. @@ -164,6 +166,25 @@ protected function getCustomFieldName($key) { return 'custom_' . $this->getCustomFieldID($key); } + /** + * Add another option to the custom field. + * + * @param string $key + * @param array $values + * + * @return int + * @throws \API_Exception + */ + protected function addOptionToCustomField($key, $values) { + $optionGroupID = CustomField::get(FALSE) + ->addWhere('id', '=', $this->getCustomFieldID($key)) + ->addSelect('option_group_id') + ->execute()->first()['option_group_id']; + return (int) OptionValue::create(FALSE) + ->setValues(array_merge(['option_group_id' => $optionGroupID], $values)) + ->execute()->first()['value']; + } + /** * Get the custom field name for the relevant key. *