Skip to content

Commit

Permalink
[REF] [Import] Extract getContactType
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eileenmcnaughton committed Apr 22, 2022
1 parent 70115ad commit 6e78138
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 33 deletions.
20 changes: 2 additions & 18 deletions CRM/Contact/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,18 @@ public function preProcess() {
$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;
}
Expand Down Expand Up @@ -668,23 +666,9 @@ public function submit($params, $mapperKeys) {

$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);
}
Expand Down
7 changes: 0 additions & 7 deletions CRM/Contact/Import/MetadataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,6 @@ public function getFieldTitles() {
return CRM_Utils_Array::collect('title', $this->getContactImportMetadata());
}

/**
* Get configured contact type.
*/
protected function getContactType() {
return $this->_contactType ?? 'Individual';
}

/**
* Get configured contact sub type.
*
Expand Down
7 changes: 7 additions & 0 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ public function init() {
$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.
*
Expand Down
17 changes: 17 additions & 0 deletions CRM/Import/Forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,23 @@ protected function getSubmittableFields(): array {
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.
*
Expand Down
30 changes: 23 additions & 7 deletions tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -127,18 +127,24 @@ public function getSubmitData() {
/**
* 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',
Expand All @@ -147,7 +153,7 @@ public function getMapFieldFormObject(): CRM_Contact_Import_Form_MapField {
$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);

Expand Down Expand Up @@ -362,6 +368,16 @@ public function getHeaderMatchDataProvider() {
];
}

/**
* 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.
*
Expand Down
8 changes: 7 additions & 1 deletion tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3220,8 +3220,14 @@ public function getFormObject($class, $formValues = [], $pageName = '', $searchF
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);
Expand Down

0 comments on commit 6e78138

Please sign in to comment.