Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFC] Add unit test towards CRM-20759 #10594

Merged
merged 3 commits into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions CRM/Contact/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -876,15 +876,11 @@ public function submit($params, $mapperKeys) {
$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:
Expand All @@ -901,7 +897,7 @@ public function submit($params, $mapperKeys) {

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;

Expand Down Expand Up @@ -930,7 +926,7 @@ public function submit($params, $mapperKeys) {
}
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') {
Expand All @@ -943,7 +939,7 @@ public function submit($params, $mapperKeys) {
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;
}
Expand Down
50 changes: 18 additions & 32 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -2278,14 +2278,9 @@ protected function formatContactParameters(&$values, &$params) {
// 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);

Expand Down Expand Up @@ -2417,50 +2412,41 @@ protected function formatContactParameters(&$values, &$params) {

// get the formatted location blocks into params - w/ 3.0 format, CRM-4605
if (!empty($values['location_type_id'])) {
static $fields = NULL;
if ($fields == NULL) {
$fields = array();
}

foreach (array(
'Phone',
'Email',
'IM',
'OpenID',
'Phone_Ext',
) as $block) {
$name = strtolower($block);
if (!array_key_exists($name, $values)) {
$blockTypes = array(
'phone' => 'Phone',
'email' => 'Email',
'im' => 'IM',
'openid' => 'OpenID',
'phone_ext' => 'Phone',
);
foreach ($blockTypes as $blockFieldName => $block) {
if (!array_key_exists($blockFieldName, $values)) {
continue;
}

if ($name == 'phone_ext') {
$block = 'Phone';
}

// block present in value array.
if (!array_key_exists($name, $params) || !is_array($params[$name])) {
$params[$name] = array();
if (!array_key_exists($blockFieldName, $params) || !is_array($params[$blockFieldName])) {
$params[$blockFieldName] = array();
}

if (!array_key_exists($block, $fields)) {
$className = "CRM_Core_DAO_$block";
$fields[$block] =& $className::fields();
$fields[$block] = $className::fields();
}

$blockCnt = count($params[$name]);
$blockCnt = count($params[$blockFieldName]);

// copy value to dao field name.
if ($name == 'im') {
$values['name'] = $values[$name];
if ($blockFieldName == 'im') {
$values['name'] = $values[$blockFieldName];
}

_civicrm_api3_store_values($fields[$block], $values,
$params[$name][++$blockCnt]
$params[$blockFieldName][++$blockCnt]
);

if (empty($params['id']) && ($blockCnt == 1)) {
$params[$name][$blockCnt]['is_primary'] = TRUE;
$params[$blockFieldName][$blockCnt]['is_primary'] = TRUE;
}

// we only process single block at a time.
Expand Down
100 changes: 95 additions & 5 deletions tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
22 changes: 20 additions & 2 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,36 @@ public function testImportParserWithUpdateWithChangedExternalIdentifier() {
$this->callAPISuccessGetSingle('Contact', $contactValues);
}

/**
* Test that the import parser changes the external identifier when there is a dedupe match.
*
* @throws \Exception
*/
public function testImportBillingAddress() {
list($contactValues) = $this->setUpBaseContact();
$contactValues['nick_name'] = 'Old Bill';
$contactValues['external_identifier'] = 'android';
$contactValues['street_address'] = 'Big Mansion';
$this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 2));
$address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion'));
$this->assertEquals(2, $address['location_type_id']);

$this->callAPISuccessGetSingle('Contact', $contactValues);
}

/**
* Run the import parser.
*
* @param array $originalValues
*
* @param int $onDuplicateAction
* @param int $expectedResult
* @param array|null $mapperLocType
*/
protected function runImport($originalValues, $onDuplicateAction, $expectedResult) {
protected function runImport($originalValues, $onDuplicateAction, $expectedResult, $mapperLocType = NULL) {
$fields = array_keys($originalValues);
$values = array_values($originalValues);
$parser = new CRM_Contact_Import_Parser_Contact($fields);
$parser = new CRM_Contact_Import_Parser_Contact($fields, $mapperLocType);
$parser->_contactType = 'Individual';
$parser->_onDuplicate = $onDuplicateAction;
$parser->init();
Expand Down