From a37d6c4a847b05942890dfdb907f8045dddf7cd2 Mon Sep 17 00:00:00 2001 From: eileen Date: Sun, 1 Sep 2019 12:05:53 +1200 Subject: [PATCH] Extend loadSavedMapping to check defaults By using the metadata already available on the function we don't need to use a class param and can simplify and test --- CRM/Contact/Import/Form/MapField.php | 6 +++--- .../CRM/Contact/Import/Form/MapFieldTest.php | 16 +++++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index da428dd6bd..e78e2ea8f2 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -914,7 +914,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $jsSet = TRUE; } else { - $mappingHeader = array_keys((array) $this->_mapperFields, $mappingName[$i]); + $mappingHeader = $processor->getFieldName($i); $websiteTypeId = $processor->getWebsiteTypeID($i); $locationId = $processor->getLocationTypeID($i); $phoneType = $processor->getPhoneTypeID($i); @@ -922,14 +922,14 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $typeId = $processor->getPhoneOrIMTypeID($i); if ($websiteTypeId) { - $defaults["mapper[$i]"] = [$mappingHeader[0], $websiteTypeId]; + $defaults["mapper[$i]"] = [$mappingHeader, $websiteTypeId]; } else { if (!$locationId) { $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n"; } //default for IM/phone without related contact - $defaults["mapper[$i]"] = [$mappingHeader[0] ?? '', $locationId, $typeId]; + $defaults["mapper[$i]"] = [$mappingHeader ?? '', $locationId, $typeId]; } if ((!$phoneType) && (!$imProvider)) { diff --git a/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php b/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php index d65cf61e91..b85ad00dc6 100644 --- a/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php +++ b/tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php @@ -166,17 +166,19 @@ class CRM_Contact_Import_Form_MapFieldTest extends CiviUnitTestCase { * * @param array $fieldSpec * @param string $expectedJS + * @param array $expectedDefaults * * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception */ - public function testLoadSavedMapping($fieldSpec, $expectedJS) { + public function testLoadSavedMapping($fieldSpec, $expectedJS, $expectedDefaults) { $this->setUpMapFieldForm(); $mapping = $this->callAPISuccess('Mapping', 'create', ['name' => 'my test']); $this->callAPISuccess('MappingField', 'create', array_merge(['mapping_id' => $mapping['id']], $fieldSpec)); $result = $this->loadSavedMapping($this->form, $mapping['id'], $fieldSpec['column_number']); $this->assertEquals($expectedJS, $result['js']); + $this->assertEquals($expectedDefaults, $result['defaults']); } /** @@ -288,26 +290,30 @@ class CRM_Contact_Import_Form_MapFieldTest extends CiviUnitTestCase { "document.forms.MapField['mapper[1][1]'].style.display = 'none'; document.forms.MapField['mapper[1][2]'].style.display = 'none'; document.forms.MapField['mapper[1][3]'].style.display = 'none';\n", + ['mapper[1]' => ['first_name', 0, NULL]], ], [ ['name' => 'Phone', 'contact_type' => 'Individual', 'column_number' => 8, 'phone_type_id' => 1, 'location_type_id' => 2], "document.forms.MapField['mapper[8][3]'].style.display = 'none';\n", + ['mapper[8]' => ['phone', 2, 1]], ], [ - ['name' => 'Phone', 'contact_type' => 'Individual', 'column_number' => 0, 'im_provider_id' => 1, 'location_type_id' => 2], + ['name' => 'IM Screen Name', 'contact_type' => 'Individual', 'column_number' => 0, 'im_provider_id' => 1, 'location_type_id' => 2], "document.forms.MapField['mapper[0][3]'].style.display = 'none';\n", + ['mapper[0]' => ['im', 2, 1]], ], [ - ['name' => 'Website', 'contact_type' => 'Individual', 'column_number' => 0, 'website_type_id'], - "document.forms.MapField['mapper[0][1]'].style.display = 'none'; -document.forms.MapField['mapper[0][2]'].style.display = 'none'; + ['name' => 'Website', 'contact_type' => 'Individual', 'column_number' => 0, 'website_type_id' => 1], + "document.forms.MapField['mapper[0][2]'].style.display = 'none'; document.forms.MapField['mapper[0][3]'].style.display = 'none';\n", + ['mapper[0]' => ['url', 1]], ], [ // Yes, the relationship mapping really does use url whereas non relationship uses website because... legacy ['name' => 'Url', 'contact_type' => 'Individual', 'column_number' => 0, 'website_type_id', 'relationship_type_id' => 1, 'relationship_direction' => 'a_b'], "document.forms.MapField['mapper[0][2]'].style.display = 'none'; document.forms.MapField['mapper[0][3]'].style.display = 'none';\n", + ['mapper[0]' => ['1_a_b', 'url', 0, NULL]], ], ]; } -- 2.25.1