From: eileen Date: Wed, 28 Jun 2017 00:32:16 +0000 (+1200) Subject: CRM-20759 add e-notice fix and unit tests for saving mappings X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=373539703314b41d759ad07b2069f1079886e226;p=civicrm-core.git CRM-20759 add e-notice fix and unit tests for saving mappings --- diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index 3d74293792..5f265636f5 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -876,15 +876,11 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $mappingParams = array( 'name' => $params['saveMappingName'], 'description' => $params['saveMappingDesc'], - 'mapping_type_id' => CRM_Core_OptionGroup::getValue('mapping_type', - 'Import Contact', - 'name' - ), + 'mapping_type_id' => 'Import Contact', ); - $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams); + $saveMapping = civicrm_api3('Mapping', 'create', $mappingParams); - $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); $contactType = $this->get('contactType'); switch ($contactType) { case CRM_Import_Parser::CONTACT_INDIVIDUAL: @@ -901,7 +897,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { for ($i = 0; $i < $this->_columnCount; $i++) { $saveMappingFields = new CRM_Core_DAO_MappingField(); - $saveMappingFields->mapping_id = $saveMapping->id; + $saveMappingFields->mapping_id = $saveMapping['id']; $saveMappingFields->contact_type = $cType; $saveMappingFields->column_number = $i; @@ -930,7 +926,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { } else { $saveMappingFields->name = $mapper[$i]; - $location_id = array_keys($locationTypes, $locations[$i]); + $locationTypeID = $parserParameters['mapperLocType'][$i]; // to get phoneType id and provider id separately // before saving mappingFields of phone and IM, CRM-3140 if (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'url') { @@ -943,7 +939,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { elseif (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'im') { $saveMappingFields->im_provider_id = isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL; } - $saveMappingFields->location_type_id = isset($location_id[0]) ? $location_id[0] : NULL; + $saveMappingFields->location_type_id = is_numeric($locationTypeID) ? $locationTypeID : NULL; } $saveMappingFields->relationship_type_id = NULL; } diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index f5a96c0945..dfa2b32930 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -2278,14 +2278,9 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { // Custom // Cache the various object fields - static $fields = NULL; - - if ($fields == NULL) { - $fields = array(); - } + static $fields = array(); // first add core contact values since for other Civi modules they are not added - require_once 'CRM/Contact/BAO/Contact.php'; $contactFields = CRM_Contact_DAO_Contact::fields(); _civicrm_api3_store_values($contactFields, $values, $params); diff --git a/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php b/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php index d0a438c8ad..b9f1e87361 100644 --- a/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php +++ b/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php @@ -42,14 +42,104 @@ class CRM_Contact_Import_Form_MapFieldTest extends CiviUnitTestCase { * Test the form loads without error / notice and mappings are assigned. * * (Added in conjunction with fixed noting on mapping assignment). + * + * @dataProvider getSubmitData + * + * @param array $params + * @param array $mapper + * @param array $expecteds */ - public function testSubmit() { + public function testSubmit($params, $mapper, $expecteds = array()) { + CRM_Core_DAO::executeQuery("CREATE TABLE IF NOT EXISTS civicrm_import_job_xxx (`nada` text, `first_name` text, `last_name` text, `address` text) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"); $form = $this->getFormObject('CRM_Contact_Import_Form_MapField'); $form->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); - $form->set('fields', array()); - $form->_columnNames = array(); - $form->_importTableName = 'civicrm_cache'; - $form->submit(array(), array()); + $form->_columnNames = array('nada', 'first_name', 'last_name', 'address'); + $form->set('importTableName', 'civicrm_import_job_xxx'); + $form->preProcess(); + $form->submit($params, $mapper); + + CRM_Core_DAO::executeQuery("DROP TABLE civicrm_import_job_xxx"); + if (!empty($expecteds)) { + foreach ($expecteds as $expected) { + $result = $this->callAPISuccess($expected['entity'], 'get', array_merge($expected['values'], array('sequential' => 1))); + $this->assertEquals($expected['count'], $result['count']); + if (isset($expected['result'])) { + foreach ($expected['result'] as $key => $expectedValues) { + foreach ($expectedValues as $valueKey => $value) { + $this->assertEquals($value, $result['values'][$key][$valueKey]); + } + } + } + } + } + $this->quickCleanup(array('civicrm_mapping', 'civicrm_mapping_field')); + } + + /** + * Get data to pass through submit function. + * + * @return array + */ + public function getSubmitData() { + return array( + 'basic_data' => array( + array( + 'saveMappingName' => '', + 'saveMappingDesc' => '', + ), + array( + 0 => array(0 => 'do_not_import'), + 1 => array(0 => 'first_name'), + 2 => array(0 => 'last_name'), + 3 => array(0 => 'street_address', 1 => 2), + ), + ), + 'save_mapping' => array( + array( + 'saveMappingName' => 'new mapping', + 'saveMappingDesc' => 'save it', + 'saveMapping' => 1, + ), + array( + 0 => array(0 => 'do_not_import'), + 1 => array(0 => 'first_name'), + 2 => array(0 => 'last_name'), + 3 => array(0 => 'street_address', 1 => 2), + ), + array( + array('entity' => 'mapping', 'count' => 1, 'values' => array('name' => 'new mapping')), + array( + 'entity' => + 'mapping_field', + 'count' => 4, + 'values' => array(), + 'result' => array( + 0 => array('name' => '- do not import -'), + 1 => array('name' => 'First Name'), + 2 => array('name' => 'Last Name'), + 3 => array('name' => 'Street Address', 'location_type_id' => 2), + ), + ), + ), + ), + ); + } + + /** + * Instantiate form object + * + * @param string $class + * @return \CRM_Core_Form + */ + public function getFormObject($class) { + $form = parent::getFormObject($class); + $contactFields = CRM_Contact_BAO_Contact::importableFields(); + $fields = array(); + foreach ($contactFields as $name => $field) { + $fields[$name] = $field['title']; + } + $form->set('fields', $fields); + return $form; } }