[Test] Add test to demonstrate bug that turns out not to exist
authoreileen <emcnaughton@wikimedia.org>
Thu, 25 Jul 2019 00:19:15 +0000 (12:19 +1200)
committereileen <emcnaughton@wikimedia.org>
Fri, 26 Jul 2019 21:13:28 +0000 (09:13 +1200)
I thought there was a bug in import handling in custom data so I wrote a test to try to capture it - tests pass & I'm off to re-check
my data & see if the bug was more of a pebkac error.

Anyway test is good

tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
tests/phpunit/CRMTraits/Custom/CustomDataTrait.php

index 835f393f27545d1b892192f5d0b0b00a2e9beb84..5f3094da2056c3f10f0fcc76bdfe5a2a9d5bc046 100644 (file)
  * @group headless
  */
 class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
-  protected $_tablesToTruncate = ['civicrm_address', 'civicrm_phone', 'civicrm_email'];
+  use CRMTraits_Custom_CustomDataTrait;
+
+  /**
+   * Main entity for the class.
+   *
+   * @var string
+   */
+  protected $entity = 'Contact';
 
   /**
    * Setup function.
@@ -46,6 +53,16 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
     parent::setUp();
   }
 
+  /**
+   * Tear down after test.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function tearDown() {
+    $this->quickCleanup(['civicrm_address', 'civicrm_phone', 'civicrm_email'], TRUE);
+    parent::tearDown();
+  }
+
   /**
    * Test that import parser will add contact with employee of relationship.
    *
@@ -316,6 +333,44 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
     $this->callAPISuccessGetSingle('Contact', $contactValues);
   }
 
+  /**
+   * Test that labels work for importing custom data.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testCustomDataLabel() {
+    $this->createCustomGroupWithFieldOfType([], 'select');
+    $contactValues = [
+      'first_name' => 'Bill',
+      'last_name' => 'Gates',
+      'email' => 'bill.gates@microsoft.com',
+      'nick_name' => 'Billy-boy',
+      $this->getCustomFieldName('select') => 'Yellow',
+    ];
+    $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, [NULL, NULL, 'Primary', NULL, NULL]);
+    $contact = $this->callAPISuccessGetSingle('Contact', array_merge($contactValues, ['return' => $this->getCustomFieldName('select')]));
+    $this->assertEquals('Y', $contact[$this->getCustomFieldName('select')]);
+  }
+
+  /**
+   * Test that names work for importing custom data.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testCustomDataName() {
+    $this->createCustomGroupWithFieldOfType([], 'select');
+    $contactValues = [
+      'first_name' => 'Bill',
+      'last_name' => 'Gates',
+      'email' => 'bill.gates@microsoft.com',
+      'nick_name' => 'Billy-boy',
+      $this->getCustomFieldName('select') => 'Y',
+    ];
+    $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, [NULL, NULL, 'Primary', NULL, NULL]);
+    $contact = $this->callAPISuccessGetSingle('Contact', array_merge($contactValues, ['return' => $this->getCustomFieldName('select')]));
+    $this->assertEquals('Y', $contact[$this->getCustomFieldName('select')]);
+  }
+
   /**
    * Test that the import parser adds the address to the primary location.
    *
index af0e1936840d80865e1a00ae4ad0e78e818c8149..963daae894a2da842865c5d9f10d39a322fe7048 100644 (file)
@@ -72,12 +72,21 @@ trait CRMTraits_Custom_CustomDataTrait {
    * @throws \CRM_Core_Exception
    */
   public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = '') {
-    if ($customFieldType !== 'text') {
+    $supported = ['text', 'select'];
+    if (!in_array($customFieldType, $supported)) {
       throw new CRM_Core_Exception('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do');
     }
     $groupParams['title'] = empty($groupParams['title']) ? $identifier . 'Group with field ' . $customFieldType : $groupParams['title'];
     $this->createCustomGroup($groupParams);
-    $customField = $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]);
+    switch ($customFieldType) {
+      case 'text':
+        $customField = $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]);
+        break;
+
+      case 'select':
+        $customField = $this->createSelectCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]);
+        break;
+    }
     $this->ids['CustomField'][$identifier . $customFieldType] = $customField['id'];
   }
 
@@ -90,38 +99,7 @@ trait CRMTraits_Custom_CustomDataTrait {
     $customField = $this->createTextCustomField(['custom_group_id' => $customGroupID]);
     $ids['text'] = $customField['id'];
 
-    $optionValue[] = [
-      'label' => 'Red',
-      'value' => 'R',
-      'weight' => 1,
-      'is_active' => 1,
-    ];
-    $optionValue[] = [
-      'label' => 'Yellow',
-      'value' => 'Y',
-      'weight' => 2,
-      'is_active' => 1,
-    ];
-    $optionValue[] = [
-      'label' => 'Green',
-      'value' => 'G',
-      'weight' => 3,
-      'is_active' => 1,
-    ];
-
-    $params = [
-      'label' => 'Pick Color',
-      'html_type' => 'Select',
-      'data_type' => 'String',
-      'weight' => 2,
-      'is_required' => 1,
-      'is_searchable' => 0,
-      'is_active' => 1,
-      'option_values' => $optionValue,
-      'custom_group_id' => $customGroupID,
-    ];
-
-    $customField = $this->callAPISuccess('custom_field', 'create', $params);
+    $customField = $this->createSelectCustomField(['custom_group_id' => $customGroupID]);
     $ids['select_string'] = $customField['id'];
 
     $params = [
@@ -229,4 +207,49 @@ trait CRMTraits_Custom_CustomDataTrait {
     return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
   }
 
+  /**
+   * Create custom select field.
+   *
+   * @param array $params
+   *   Parameter overrides, must include custom_group_id.
+   *
+   * @return array
+   */
+  protected function createSelectCustomField(array $params): array {
+    $optionValue = [
+      [
+        'label' => 'Red',
+        'value' => 'R',
+        'weight' => 1,
+        'is_active' => 1,
+      ],
+      [
+        'label' => 'Yellow',
+        'value' => 'Y',
+        'weight' => 2,
+        'is_active' => 1,
+      ],
+      [
+        'label' => 'Green',
+        'value' => 'G',
+        'weight' => 3,
+        'is_active' => 1,
+      ],
+    ];
+
+    $params = array_merge([
+      'label' => 'Pick Color',
+      'html_type' => 'Select',
+      'data_type' => 'String',
+      'weight' => 2,
+      'is_required' => 1,
+      'is_searchable' => 0,
+      'is_active' => 1,
+      'option_values' => $optionValue,
+    ], $params);
+
+    $customField = $this->callAPISuccess('custom_field', 'create', $params);
+    return $customField['values'][$customField['id']];
+  }
+
 }