From 6e9006e98ecb0a1de950a17a51f249a87fde26a6 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 23 Mar 2023 11:28:25 +1300 Subject: [PATCH] Fix Contact Import tests custom date testing, remove duplicate handling 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 | 3 --- CRM/Contact/Import/Parser/Contact.php | 10 +-------- CRM/Import/Parser.php | 20 ----------------- tests/phpunit/CRM/Batch/Form/EntryTest.php | 3 --- .../CRM/Contact/Import/Parser/ContactTest.php | 22 +++++++++---------- 5 files changed, 11 insertions(+), 47 deletions(-) diff --git a/CRM/Contact/Import/Form/DataSource.php b/CRM/Contact/Import/Form/DataSource.php index f3cfba803e..84530dc29f 100644 --- a/CRM/Contact/Import/Form/DataSource.php +++ b/CRM/Contact/Import/Form/DataSource.php @@ -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']); - } /** diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 31fda9dfa1..079ecf9b82 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -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]); diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index a40f15cab9..7ba284499b 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -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. * diff --git a/tests/phpunit/CRM/Batch/Form/EntryTest.php b/tests/phpunit/CRM/Batch/Form/EntryTest.php index 2213cddb08..0d2a918f2e 100644 --- a/tests/phpunit/CRM/Batch/Form/EntryTest.php +++ b/tests/phpunit/CRM/Batch/Form/EntryTest.php @@ -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); } /** diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index 68089f76a4..a085d08817 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -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); } } } -- 2.25.1