<?php
+use Civi\Api4\Mapping;
+use Civi\Api4\MappingField;
+
/**
* Class CRM_Import_ImportProcessor.
*
* @throws \CiviCRM_API3_Exception
*/
protected function loadSavedMapping() {
+ $fields = civicrm_api3('MappingField', 'get', [
+ 'mapping_id' => $this->getMappingID(),
+ 'options' => ['limit' => 0],
+ ])['values'];
+ foreach ($fields as $index => $field) {
+ $fieldSpec = $this->getMetadata()[$fields[$index]['name']];
+ $fields[$index]['label'] = $fieldSpec['title'];
+ if (empty($field['location_type_id']) && !empty($fieldSpec['hasLocationType'])) {
+ $fields[$index]['location_type_id'] = 'Primary';
+ }
+ }
+ $this->mappingFields = $this->rekeyBySortedColumnNumbers($fields);
+ }
+
+ /**
+ * Load the mapping from the database into the pre-5.50 format.
+ *
+ * This is preserved as a copy the upgrade script can use - since the
+ * upgrade allows the other to be 'fixed'.
+ *
+ * @throws \CiviCRM_API3_Exception
+ */
+ protected function legacyLoadSavedMapping() {
$fields = civicrm_api3('MappingField', 'get', [
'mapping_id' => $this->getMappingID(),
'options' => ['limit' => 0],
return [(string) $this->getFieldName($column), $this->getLocationTypeID($column), $this->getPhoneOrIMTypeID($column)];
}
+ /**
+ * This exists for use in the FiveFifty Upgrade
+ *
+ * @throws \API_Exception|\CiviCRM_API3_Exception
+ */
+ public static function convertSavedFields(): void {
+ $mappings = Mapping::get(FALSE)
+ ->setSelect(['id', 'contact_type'])
+ ->addWhere('mapping_type_id:name', '=', 'Import Contact')
+ ->execute();
+
+ foreach ($mappings as $mapping) {
+ $processor = new CRM_Import_ImportProcessor();
+ $processor->setMappingID($mapping['id']);
+ $processor->setMetadata(CRM_Contact_BAO_Contact::importableFields('All'));
+ $processor->legacyLoadSavedMapping();;
+ foreach ($processor->getMappingFields() as $field) {
+ // The if is mostly precautionary against running this more than once
+ // - which is common in dev if not live...
+ if ($field['name']) {
+ MappingField::update(FALSE)
+ ->setValues(['name' => $field['name']])
+ ->addWhere('id', '=', $field['id'])
+ ->execute();
+ }
+ }
+ }
+ }
+
}
* File for the CRM_Contact_Import_Form_MapFieldTest class.
*/
+use Civi\Api4\MappingField;
use Civi\Api4\UserJob;
/**
use CRM_Contact_Import_MetadataTrait;
use CRMTraits_Custom_CustomDataTrait;
+ /**
+ * Cleanup mappings in DB.
+ */
+ public function tearDown(): void {
+ $this->quickCleanup(['civicrm_mapping', 'civicrm_mapping_field'], TRUE);
+ parent::tearDown();
+ }
+
/**
* Map field form.
*
* @throws \CiviCRM_API3_Exception
*/
public function testSubmit(array $params, array $mapper, array $expecteds = []): void {
- $form = $this->getMapFieldFormObject();
+ $form = $this->getMapFieldFormObject(['mapper' => $mapper]);
+ /* @var CRM_Contact_Import_Form_MapField $form */
$form->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
$form->preProcess();
$form->submit($params, $mapper);
* In conjunction with testing our existing function this tests the methods we want to migrate to
* to clean it up.
*
- * @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testLoadSavedMappingDirect(): void {
- $this->entity = 'Contact';
- $this->createCustomGroupWithFieldOfType(['title' => 'My Field']);
+ $mapping = $this->storeComplexMapping();
$this->setUpMapFieldForm();
- $mapping = $this->callAPISuccess('Mapping', 'create', ['name' => 'my test', 'label' => 'Special custom']);
- foreach ([
- [
- 'name' => 'Addressee',
- 'column_number' => '0',
- ],
- [
- 'name' => 'Postal Greeting',
- 'column_number' => '1',
- ],
- [
- 'name' => 'Phone',
- 'column_number' => '2',
- 'location_type_id' => '1',
- 'phone_type_id' => '1',
- ],
- [
- 'name' => 'Street Address',
- 'column_number' => '3',
- ],
- [
- 'name' => 'Enter text here :: My Field',
- 'column_number' => '4',
- ],
- [
- 'name' => 'Street Address',
- 'column_number' => '5',
- 'location_type_id' => '1',
- ],
- [
- 'name' => 'City',
- 'column_number' => '6',
- 'location_type_id' => '1',
- ],
- [
- 'name' => 'State Province',
- 'column_number' => '7',
- 'relationship_type_id' => 4,
- 'relationship_direction' => 'a_b',
- 'location_type_id' => '1',
- ],
- [
- 'name' => 'Url',
- 'column_number' => '8',
- 'relationship_type_id' => 4,
- 'relationship_direction' => 'a_b',
- 'website_type_id' => 2,
- ],
- [
- 'name' => 'Phone',
- 'column_number' => '9',
- 'relationship_type_id' => 4,
- 'location_type_id' => '1',
- 'relationship_direction' => 'a_b',
- 'phone_type_id' => 2,
- ],
- [
- 'name' => 'Phone',
- 'column_number' => '10',
- 'location_type_id' => '1',
- 'phone_type_id' => '3',
- ],
- ] as $mappingField) {
- $this->callAPISuccess('MappingField', 'create', array_merge([
- 'mapping_id' => $mapping['id'],
- 'grouping' => 1,
- 'contact_type' => 'Individual',
- ], $mappingField));
- }
$processor = new CRM_Import_ImportProcessor();
$processor->setMappingID($mapping['id']);
$processor->setMetadata($this->getContactImportMetadata());
$this->form->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
}
+ /**
+ * Tests the routing used in the 5.50 upgrade script to stop using labels...
+ *
+ * @throws \API_Exception
+ * @throws \CiviCRM_API3_Exception
+ */
+ public function testConvertFields(): void {
+ $mapping = $this->storeComplexMapping(TRUE);
+ CRM_Import_ImportProcessor::convertSavedFields();
+ $updatedMapping = MappingField::get()
+ ->addWhere('mapping_id', '=', $mapping['id'])
+ ->addSelect('id', 'name')->execute();
+
+ $expected = [
+ 0 => 'addressee',
+ 1 => 'postal_greeting',
+ 2 => 'phone',
+ 3 => 'street_address',
+ 4 => 'custom_1',
+ 5 => 'street_address',
+ 6 => 'city',
+ 7 => 'state_province',
+ 8 => 'url',
+ 9 => 'phone',
+ 10 => 'phone',
+ ];
+ foreach ($updatedMapping as $index => $mappingField) {
+ $this->assertEquals($expected[$index], $mappingField['name']);
+ }
+ }
+
+ /**
+ * Store a mapping with a complex set of fields.
+ *
+ * @param bool $legacyMode
+ *
+ * @return array
+ */
+ private function storeComplexMapping(bool $legacyMode = FALSE): array {
+ $this->createCustomGroupWithFieldOfType(['title' => 'My Field']);
+ $mapping = $this->callAPISuccess('Mapping', 'create', [
+ 'name' => 'my test',
+ 'label' => 'Special custom',
+ 'mapping_type_id' => 'Import Contact',
+ ]);
+ foreach (
+ [
+ [
+ 'name' => $legacyMode ? 'Addressee' : 'addressee',
+ 'column_number' => '0',
+ ],
+ [
+ 'name' => $legacyMode ? 'Postal Greeting' : 'postal_greeting',
+ 'column_number' => '1',
+ ],
+ [
+ 'name' => $legacyMode ? 'Phone' : 'phone',
+ 'column_number' => '2',
+ 'location_type_id' => '1',
+ 'phone_type_id' => '1',
+ ],
+ [
+ 'name' => $legacyMode ? 'Street Address' : 'street_address',
+ 'column_number' => '3',
+ ],
+ [
+ 'name' => $legacyMode ? 'Enter text here :: My Field' : $this->getCustomFieldName('text'),
+ 'column_number' => '4',
+ ],
+ [
+ 'name' => $legacyMode ? 'Street Address' : 'street_address',
+ 'column_number' => '5',
+ 'location_type_id' => '1',
+ ],
+ [
+ 'name' => $legacyMode ? 'City' : 'city',
+ 'column_number' => '6',
+ 'location_type_id' => '1',
+ ],
+ [
+ 'name' => $legacyMode ? 'State Province' : 'state_province',
+ 'column_number' => '7',
+ 'relationship_type_id' => 4,
+ 'relationship_direction' => 'a_b',
+ 'location_type_id' => '1',
+ ],
+ [
+ 'name' => $legacyMode ? 'Url' : 'url',
+ 'column_number' => '8',
+ 'relationship_type_id' => 4,
+ 'relationship_direction' => 'a_b',
+ 'website_type_id' => 2,
+ ],
+ [
+ 'name' => $legacyMode ? 'Phone' : 'phone',
+ 'column_number' => '9',
+ 'relationship_type_id' => 4,
+ 'location_type_id' => '1',
+ 'relationship_direction' => 'a_b',
+ 'phone_type_id' => 2,
+ ],
+ [
+ 'name' => $legacyMode ? 'Phone' : 'phone',
+ 'column_number' => '10',
+ 'location_type_id' => '1',
+ 'phone_type_id' => '3',
+ ],
+ ] as $mappingField) {
+ $this->callAPISuccess('MappingField', 'create', array_merge([
+ 'mapping_id' => $mapping['id'],
+ 'grouping' => 1,
+ 'contact_type' => 'Individual',
+ ], $mappingField));
+ }
+ return $mapping;
+ }
+
}