From 6e78138c3daf041deea955954be528a7bc5f30ac Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 22 Apr 2022 12:11:17 +1200 Subject: [PATCH] [REF] [Import] Extract getContactType Note that the function on the MetaDataTrait is moved to the Parser_Class - 3 forms use the trait - a unittest class, the Parser_Class and MapField. MapField was not calling the old function but the newly added one clashes - and highlights that the contents of the function should differ for the 2 scenarios - hence moving off the Trait and onto the 3 classes that use it --- CRM/Contact/Import/Form/MapField.php | 20 ++----------- CRM/Contact/Import/MetadataTrait.php | 7 ----- CRM/Contact/Import/Parser/Contact.php | 7 +++++ CRM/Import/Forms.php | 17 +++++++++++ .../CRM/Contact/Import/Form/MapFieldTest.php | 30 ++++++++++++++----- tests/phpunit/CiviTest/CiviUnitTestCase.php | 8 ++++- 6 files changed, 56 insertions(+), 33 deletions(-) diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index f619d5bdf0..e7fc6868f9 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -89,20 +89,18 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $highlightedFields[] = 'email'; $highlightedFields[] = 'external_identifier'; //format custom field names, CRM-2676 + $contactType = $this->getContactType(); switch ($this->get('contactType')) { case CRM_Import_Parser::CONTACT_INDIVIDUAL: - $contactType = 'Individual'; $highlightedFields[] = 'first_name'; $highlightedFields[] = 'last_name'; break; case CRM_Import_Parser::CONTACT_HOUSEHOLD: - $contactType = 'Household'; $highlightedFields[] = 'household_name'; break; case CRM_Import_Parser::CONTACT_ORGANIZATION: - $contactType = 'Organization'; $highlightedFields[] = 'organization_name'; break; } @@ -668,23 +666,9 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $saveMapping = civicrm_api3('Mapping', 'create', $mappingParams); - $contactType = $this->get('contactType'); - switch ($contactType) { - case CRM_Import_Parser::CONTACT_INDIVIDUAL: - $cType = 'Individual'; - break; - - case CRM_Import_Parser::CONTACT_HOUSEHOLD: - $cType = 'Household'; - break; - - case CRM_Import_Parser::CONTACT_ORGANIZATION: - $cType = 'Organization'; - } - $mappingID = NULL; foreach (array_keys($this->getColumnHeaders()) as $i) { - $mappingID = $this->saveMappingField($mapperKeys, $saveMapping, $cType, $i, $mapper, $parserParameters); + $mappingID = $this->saveMappingField($mapperKeys, $saveMapping, $this->getContactType(), $i, $mapper, $parserParameters); } $this->set('savedMapping', $mappingID); } diff --git a/CRM/Contact/Import/MetadataTrait.php b/CRM/Contact/Import/MetadataTrait.php index ba55c45f38..9f6390725c 100644 --- a/CRM/Contact/Import/MetadataTrait.php +++ b/CRM/Contact/Import/MetadataTrait.php @@ -103,13 +103,6 @@ trait CRM_Contact_Import_MetadataTrait { return CRM_Utils_Array::collect('title', $this->getContactImportMetadata()); } - /** - * Get configured contact type. - */ - protected function getContactType() { - return $this->_contactType ?? 'Individual'; - } - /** * Get configured contact sub type. * diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 948bb17c57..2f5b5681ea 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -261,6 +261,13 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { $this->_parseStreetAddress = CRM_Utils_Array::value('street_address_parsing', CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_options'), FALSE); } + /** + * Get configured contact type. + */ + protected function getContactType() { + return $this->_contactType ?? 'Individual'; + } + /** * Handle the values in preview mode. * diff --git a/CRM/Import/Forms.php b/CRM/Import/Forms.php index 938f8f5f6d..518f16e4fe 100644 --- a/CRM/Import/Forms.php +++ b/CRM/Import/Forms.php @@ -293,6 +293,23 @@ class CRM_Import_Forms extends CRM_Core_Form { return array_merge($this->submittableFields, $dataSourceFields); } + /** + * Get the contact type selected for the import (on the datasource form). + * + * @return string + * e.g Individual, Organization, Household. + * + * @throws \CRM_Core_Exception + */ + protected function getContactType(): string { + $contactTypeMapping = [ + CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual', + CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household', + CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization', + ]; + return $contactTypeMapping[$this->getSubmittedValue('contactType')]; + } + /** * Create a user job to track the import. * diff --git a/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php b/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php index 2c691a8dfa..2be1ea15e0 100644 --- a/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php +++ b/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php @@ -50,7 +50,7 @@ class CRM_Contact_Import_Form_MapFieldTest extends CiviUnitTestCase { * @throws \CiviCRM_API3_Exception */ public function testSubmit($params, $mapper, $expecteds = []): void { - $form = $this->getMapFieldFormObject('CRM_Contact_Import_Form_MapField'); + $form = $this->getMapFieldFormObject(); /* @var CRM_Contact_Import_Form_MapField $form */ $form->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); $form->_columnNames = ['nada', 'first_name', 'last_name', 'address']; @@ -127,18 +127,24 @@ class CRM_Contact_Import_Form_MapFieldTest extends CiviUnitTestCase { /** * Instantiate MapField form object * + * @param array $submittedValues + * Values that would be submitted by the user. + * Some defaults are provided. + * * @return \CRM_Contact_Import_Form_MapField * @throws \API_Exception * @throws \CRM_Core_Exception */ - public function getMapFieldFormObject(): CRM_Contact_Import_Form_MapField { + public function getMapFieldFormObject(array $submittedValues = []): CRM_Contact_Import_Form_MapField { CRM_Core_DAO::executeQuery('CREATE TABLE IF NOT EXISTS civicrm_tmp_d_import_job_xxx (`nada` text, `first_name` text, `last_name` text, `address` text) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci'); + $submittedValues = array_merge([ + 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL, + 'dataSource' => 'CRM_Import_DataSource_SQL', + 'sqlQuery' => 'SELECT * FROM civicrm_tmp_d_import_job_xxx', + ], $submittedValues); $userJobID = UserJob::create()->setValues([ 'metadata' => [ - 'submitted_values' => [ - 'dataSource' => 'CRM_Import_DataSource_SQL', - 'sqlQuery' => 'SELECT * FROM civicrm_tmp_d_import_job_xxx', - ], + 'submitted_values' => $submittedValues, ], 'status_id:name' => 'draft', 'type_id:name' => 'contact_import', @@ -147,7 +153,7 @@ class CRM_Contact_Import_Form_MapFieldTest extends CiviUnitTestCase { $dataSource = new CRM_Import_DataSource_SQL($userJobID); $params = ['sqlQuery' => 'SELECT * FROM civicrm_tmp_d_import_job_xxx']; $null = NULL; - $form = $this->getFormObject('CRM_Contact_Import_Form_MapField'); + $form = $this->getFormObject('CRM_Contact_Import_Form_MapField', $submittedValues); $form->set('user_job_id', $userJobID); $dataSource->postProcess($params, $null, $form); @@ -362,6 +368,16 @@ document.forms.MapField['mapper[0][3]'].style.display = 'none';\n", ]; } + /** + * This is accessed by virtue of the MetaDataTrait being included. + * + * The use of the metadataTrait came from a transitional refactor + * but it probably should be phased out again. + */ + protected function getContactType() { + return $this->_contactType ?? 'Individual'; + } + /** * Wrapper for loadSavedMapping. * diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index ac70a549f0..ecac2cd93d 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -3220,8 +3220,14 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { break; case 'CRM_Contact_Import_Form_DataSource': + case 'CRM_Contact_Import_Form_MapField': $form->controller = new CRM_Contact_Import_Controller(); - break; + $form->controller->setStateMachine(new CRM_Core_StateMachine($form->controller)); + // The submitted values should be set on one or the other of the forms in the flow. + // For test simplicity we set on all rather than figuring out which ones go where.... + $_SESSION['_' . $form->controller->_name . '_container']['values']['DataSource'] = $formValues; + $_SESSION['_' . $form->controller->_name . '_container']['values']['MapField'] = $formValues; + return $form; case strpos($class, '_Form_') !== FALSE: $form->controller = new CRM_Core_Controller_Simple($class, $pageName); -- 2.25.1