Merge pull request #14934 from civicrm/5.16
[civicrm-core.git] / tests / phpunit / CRM / Contact / Import / Parser / ContactTest.php
index 835f393f27545d1b892192f5d0b0b00a2e9beb84..ff4e11c6c9ef64bfc2624ea51031de8124525174 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.
    *
@@ -464,20 +519,16 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
    */
   public function testImportFill() {
     // Create a custom field group for testing.
-    $custom_group_name = 'importFillGroup';
-    $results = $this->callAPISuccess('customGroup', 'get', ['title' => $custom_group_name]);
-    if ($results['count'] == 0) {
-      $api_params = [
-        'title' => $custom_group_name,
-        'extends' => 'Individual',
-        'is_active' => TRUE,
-      ];
-      $customGroup = $this->callAPISuccess('customGroup', 'create', $api_params);
-    }
+    $this->createCustomGroup([
+      'title' => 'importFillGroup',
+      'extends' => 'Individual',
+      'is_active' => TRUE,
+    ]);
+    $customGroupID = $this->ids['CustomGroup']['importFillGroup'];
 
     // Add two custom fields.
     $api_params = [
-      'custom_group_id' => $customGroup['id'],
+      'custom_group_id' => $customGroupID,
       'label' => 'importFillField1',
       'html_type' => 'Select',
       'data_type' => 'String',
@@ -490,7 +541,7 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
     $customField1 = $result['id'];
 
     $api_params = [
-      'custom_group_id' => $customGroup['id'],
+      'custom_group_id' => $customGroupID,
       'label' => 'importFillField2',
       'html_type' => 'Select',
       'data_type' => 'String',
@@ -505,8 +556,6 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
     // Now set up values.
     $original_gender = 'Male';
     $original_custom1 = 'foo';
-    $original_job_title = '';
-    $original_custom2 = '';
     $original_email = 'test-import-fill@example.org';
 
     $import_gender = 'Female';