From d989f2a4620edaa8ee2bc7fd1ae8200dccde3f59 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 29 Jul 2019 02:52:56 +1200 Subject: [PATCH] Convert custom data export test to use newer function. Fix handling for custom country fields to be where it is actually hit now --- CRM/Core/BAO/CustomField.php | 1 - CRM/Export/BAO/ExportProcessor.php | 5 ++- .../phpunit/CRM/Core/BAO/CustomFieldTest.php | 8 +++- tests/phpunit/CRM/Export/BAO/ExportTest.php | 38 ++++++++++--------- .../CRMTraits/Custom/CustomDataTrait.php | 2 +- 5 files changed, 31 insertions(+), 23 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index ffcbace7b9..695deacaba 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -541,7 +541,6 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { // Search table is used by query object searches.. $fields[$dao->id]['search_table'] = ($fields[$dao->id]['extends_table'] == 'civicrm_contact') ? 'contact_a' : $fields[$dao->id]['extends_table']; self::getOptionsForField($fields[$dao->id], $dao->option_group_name); - } Civi::cache('fields')->set("custom importableFields $cacheKey", $fields); diff --git a/CRM/Export/BAO/ExportProcessor.php b/CRM/Export/BAO/ExportProcessor.php index 0f6d1af745..9d61472ba0 100644 --- a/CRM/Export/BAO/ExportProcessor.php +++ b/CRM/Export/BAO/ExportProcessor.php @@ -1413,6 +1413,9 @@ class CRM_Export_BAO_ExportProcessor { switch ($fieldSpec['type']) { case CRM_Utils_Type::T_INT: case CRM_Utils_Type::T_BOOLEAN: + if (in_array(CRM_Utils_Array::value('data_type', $fieldSpec), ['Country', 'StateProvince', 'ContactReference'])) { + return "$fieldName varchar(255)"; + } return "$fieldName varchar(16)"; case CRM_Utils_Type::T_STRING: @@ -1469,8 +1472,6 @@ class CRM_Export_BAO_ExportProcessor { $length = max(512, CRM_Utils_Array::value('text_length', $queryFields[$columnName])); return "$fieldName varchar($length)"; - case 'Country': - case 'StateProvince': case 'Link': return "$fieldName varchar(255)"; diff --git a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php index ffb3e19a91..1867cabe2e 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php @@ -445,7 +445,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'custom_field_id' => $this->getCustomFieldID('country'), 'options_per_line' => NULL, 'text_length' => NULL, - 'data_type' => 'Int', + 'data_type' => 'Country', 'html_type' => 'Select Country', 'is_search_range' => '0', 'id' => $this->getCustomFieldID('country'), @@ -467,6 +467,12 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.country_' . $this->getCustomFieldID('country'), 'extends_table' => 'civicrm_contact', 'search_table' => 'contact_a', + 'pseudoconstant' => [ + 'table' => 'civicrm_country', + 'keyColumn' => 'id', + 'labelColumn' => 'name', + 'nameColumn' => 'iso_code', + ], ], $this->getCustomFieldName('file') => [ 'name' => $this->getCustomFieldName('file'), diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php index 4dc7c2523d..cdbeb5cfd8 100644 --- a/tests/phpunit/CRM/Export/BAO/ExportTest.php +++ b/tests/phpunit/CRM/Export/BAO/ExportTest.php @@ -7,6 +7,7 @@ */ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { + use CRMTraits_Custom_CustomDataTrait; /** * Contact IDs created for testing. * @@ -700,34 +701,35 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { } /** - * Test master_address_id field. + * Test custom data exporting. + * + * @throws \CRM_Core_Exception + * @throws \League\Csv\Exception */ public function testExportCustomData() { $this->setUpContactExportData(); - - $customData = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php'); - + $this->entity = 'Contact'; + $this->createCustomGroupWithFieldsOfAllTypes(); $this->callAPISuccess('Contact', 'create', [ 'id' => $this->contactIDs[1], - 'custom_' . $customData['custom_field_id'] => 'BlahdeBlah', + $this->getCustomFieldName('text') => 'BlahdeBlah', + $this->getCustomFieldName('country') => 'LA', 'api.Address.create' => ['location_type_id' => 'Billing', 'city' => 'Waipu'], ]); $selectedFields = [ - ['Individual', 'city', CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'location_type_id', 'Billing')], - ['Individual', 'custom_1'], + ['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')], ]; - list($tableName, $sqlColumns) = $this->doExport($selectedFields, $this->contactIDs[1]); - $this->assertEquals([ - 'billing_city' => 'billing_city varchar(64)', - 'custom_1' => 'custom_1 varchar(255)', - ], $sqlColumns); - - $dao = CRM_Core_DAO::executeQuery('SELECT * FROM ' . $tableName); - while ($dao->fetch()) { - $this->assertEquals('BlahdeBlah', $dao->custom_1); - $this->assertEquals('Waipu', $dao->billing_city); - } + $this->doExportTest([ + 'fields' => $selectedFields, + 'ids' => [$this->contactIDs[1]], + ]); + $row = $this->csv->fetchOne(); + $this->assertEquals('BlahdeBlah', $row['Enter text here']); + $this->assertEquals('Waipu', $row['Billing-City']); + $this->assertEquals("Lao People's Democratic Republic", $row['Country']); } /** diff --git a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php index 963daae894..f8dba7b531 100644 --- a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php +++ b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php @@ -142,7 +142,7 @@ trait CRMTraits_Custom_CustomDataTrait { $ids['file'] = $fileField['id']; $ids['country'] = $this->customFieldCreate([ 'custom_group_id' => $customGroupID, - 'data_type' => 'Int', + 'data_type' => 'Country', 'html_type' => 'Select Country', 'default_value' => '', 'label' => 'Country', -- 2.25.1