Fix Contact Import tests custom date testing, remove duplicate handling
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 22 Mar 2023 22:28:25 +0000 (11:28 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 22 Mar 2023 22:29:59 +0000 (11:29 +1300)
All the code in the formatCommonData function is legacy contact
import handling for stuff that is already handled in the generic
metadata based code. This fixes the test so it is actually testing
it for contact and address custom date fields & removes

CRM/Contact/Import/Form/DataSource.php
CRM/Contact/Import/Parser/Contact.php
CRM/Import/Parser.php
tests/phpunit/CRM/Batch/Form/EntryTest.php
tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php

index f3cfba803e6229e9a3147f8ead8796aa98303190..84530dc29fe964a2b38ded39e4810968f5a3b387 100644 (file)
@@ -111,15 +111,12 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Import_Form_DataSource {
     // Once the mentioned forms no longer call $this->get() all this 'setting'
     // is obsolete.
     $storeParams = [
-      'dateFormats' => $this->getSubmittedValue('dateFormats'),
       'savedMapping' => $this->getSubmittedValue('savedMapping'),
     ];
 
     foreach ($storeParams as $storeName => $value) {
       $this->set($storeName, $value);
     }
-    CRM_Core_Session::singleton()->set('dateTypes', $storeParams['dateFormats']);
-
   }
 
   /**
index 31fda9dfa1e73b19c0da5103efe2adbbbe29168d..079ecf9b82c51734d22579825cab21ef4435d089 100644 (file)
@@ -301,20 +301,12 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
     $addressCustomFields = CRM_Core_BAO_CustomField::getFields('Address');
     $customFields = $customFields + $addressCustomFields;
 
-    //format date first
-    $session = CRM_Core_Session::singleton();
-    $dateType = $session->get("dateTypes");
     foreach ($params as $key => $val) {
       $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key);
       if ($customFieldID &&
         !array_key_exists($customFieldID, $addressCustomFields)
       ) {
-        //we should not update Date to null, CRM-4062
-        if ($val && ($customFields[$customFieldID]['data_type'] == 'Date')) {
-          //CRM-21267
-          $this->formatCustomDate($params, $formatted, $dateType, $key);
-        }
-        elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
+        if ($customFields[$customFieldID]['data_type'] == 'Boolean') {
           if (empty($val) && !is_numeric($val) && $this->isFillDuplicates()) {
             //retain earlier value when Import mode is `Fill`
             unset($params[$key]);
index a40f15cab927cfa37c4acceaf0463f21b81bd0c9..7ba284499b4f69245e420428b7563db1d1136b4d 100644 (file)
@@ -2139,26 +2139,6 @@ abstract class CRM_Import_Parser implements UserJobInterface {
     $this->getDataSourceObject()->updateStatus($id, $status, $message, $entityID, $additionalFields);
   }
 
-  /**
-   * Convert any given date string to default date array.
-   *
-   * @param array $params
-   *   Has given date-format.
-   * @param array $formatted
-   *   Store formatted date in this array.
-   * @param int $dateType
-   *   Type of date.
-   * @param string $dateParam
-   *   Index of params.
-   *
-   * @deprecated
-   */
-  public static function formatCustomDate(&$params, &$formatted, $dateType, $dateParam) {
-    //fix for CRM-2687
-    CRM_Utils_Date::convertToDefaultDate($params, $dateType, $dateParam);
-    $formatted[$dateParam] = CRM_Utils_Date::processDate($params[$dateParam]);
-  }
-
   /**
    * Get the value to use for option comparison purposes.
    *
index 2213cddb08e9e6eb81f480c22cff029dfa7ea85c..0d2a918f2e3207b88db223160f989de7dd89e61a 100644 (file)
@@ -150,9 +150,6 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
     $this->contactID2 = $this->individualCreate($contact2Params);
     $this->contactID3 = $this->individualCreate(['first_name' => 'bobby', 'email' => 'c@d.com']);
     $this->contactID4 = $this->individualCreate(['first_name' => 'bobbynita', 'email' => 'c@de.com']);
-
-    $session = CRM_Core_Session::singleton();
-    $session->set('dateTypes', 1);
   }
 
   /**
index 68089f76a47b25c18e2241f6f75f03761f3e60e5..a085d088173edbb24544020f7f253f234c185752 100644 (file)
@@ -1367,38 +1367,36 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
    * @throws \CRM_Core_Exception
    */
   public function testValidateDateData(string $csv, int $dateType): void {
-    $addressCustomGroupID = $this->createCustomGroup(['extends' => 'Address', 'name' => 'Address']);
-    $contactCustomGroupID = $this->createCustomGroup(['extends' => 'Contact', 'name' => 'Contact']);
-    $addressCustomFieldID = $this->createDateCustomField(['custom_group_id' => $addressCustomGroupID])['id'];
-    $contactCustomFieldID = $this->createDateCustomField(['custom_group_id' => $contactCustomGroupID])['id'];
+    $this->createCustomGroupWithFieldOfType(['extends' => 'Address', 'name' => 'Address'], 'date', 'address_');
+    $this->createCustomGroupWithFieldOfType(['extends' => 'Contact', 'name' => 'Contact'], 'date', 'contact_');
     $mapper = [
       ['first_name'],
       ['last_name'],
       ['birth_date'],
       ['deceased_date'],
-      ['custom_' . $contactCustomFieldID],
-      ['custom_' . $addressCustomFieldID, 1],
+      [$this->getCustomFieldName('contact_date')],
+      [$this->getCustomFieldName('address_date'), 1],
       ['street_address', 1],
       ['do_not_import'],
+      ['do_not_import'],
     ];
-    // Date types should be picked up from submitted values but still some clean up to do.
-    CRM_Core_Session::singleton()->set('dateTypes', $dateType);
     $this->validateMultiRowCsv($csv, $mapper, 'custom_date_one', ['dateFormats' => $dateType]);
+    $this->importCSV($csv, $mapper);
     $fields = [
       'contact_id.birth_date',
       'contact_id.deceased_date',
       'contact_id.is_deceased',
-      'contact_id.custom_' . $contactCustomFieldID,
-      $addressCustomFieldID,
+      'contact_id.' . $this->getCustomFieldName('contact_date', 4),
+      $this->getCustomFieldName('address_date', 4),
     ];
     $contacts = Address::get()->addWhere('contact_id.first_name', '=', 'Joe')->setSelect($fields)->execute();
     foreach ($contacts as $contact) {
       foreach ($fields as $field) {
-        if ($field === 'contact_is_deceased') {
+        if ($field === 'contact_id.is_deceased') {
           $this->assertTrue($contact[$field]);
         }
         else {
-          $this->assertEquals('2008-09-01', $contact[$field]);
+          $this->assertEquals('2008-09-01', substr($contact[$field], 0, 10), $field);
         }
       }
     }