Convert custom data export test to use newer function.
authoreileen <emcnaughton@wikimedia.org>
Sun, 28 Jul 2019 14:52:56 +0000 (02:52 +1200)
committereileen <emcnaughton@wikimedia.org>
Wed, 31 Jul 2019 08:23:22 +0000 (20:23 +1200)
Fix handling for custom country fields to be where it is actually hit now

CRM/Core/BAO/CustomField.php
CRM/Export/BAO/ExportProcessor.php
tests/phpunit/CRM/Core/BAO/CustomFieldTest.php
tests/phpunit/CRM/Export/BAO/ExportTest.php
tests/phpunit/CRMTraits/Custom/CustomDataTrait.php

index ffcbace7b9dc21eedc8a7f420f998730cace22eb..695deacabab55e08c31a6fefa23e41ff554bc2c3 100644 (file)
@@ -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);
index 0f6d1af745b104ccbb6ea13a9cdf48c500767fe4..9d61472ba0d1f68898a05e68332bfa10657f18fd 100644 (file)
@@ -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)";
 
index ffb3e19a91f94346f858ff5b2e203f0cbd1cced9..1867cabe2ecffc543c60907a66027eabe14670ca 100644 (file)
@@ -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'),
index 4dc7c2523da741b029cd6cb6f0e16c3df46c196a..cdbeb5cfd8de277ec52cd3a4818c6a93265a9324 100644 (file)
@@ -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']);
   }
 
   /**
index 963daae894a2da842865c5d9f10d39a322fe7048..f8dba7b5318867110c96306bea4ef1fc14c30dc4 100644 (file)
@@ -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',