From d67808ac5d6c43de1273640b849433b0ecc01185 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 10 Jul 2019 10:26:38 +1200 Subject: [PATCH] [REF] simplify & add tests on getMappingParams Tests extended to cover search builder (all that operator stuff --- CRM/Core/BAO/Mapping.php | 52 ++-- tests/phpunit/CRM/Core/BAO/MappingTest.php | 323 +++++++++++++-------- 2 files changed, 223 insertions(+), 152 deletions(-) diff --git a/CRM/Core/BAO/Mapping.php b/CRM/Core/BAO/Mapping.php index df7ed0671a..5a3c65e5b2 100644 --- a/CRM/Core/BAO/Mapping.php +++ b/CRM/Core/BAO/Mapping.php @@ -993,49 +993,38 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping { /** * Get the parameters for a mapping field in a saveable format from the quickform mapping format. * - * @param array $params - * @param int $mappingId + * @param array $defaults * @param array $v - * @param string $k - * @param string $key - * @param int $colCnt * * @return array */ - protected static function getMappingParams($params, $mappingId, $v, $k, $key, $colCnt) { + protected static function getMappingParams($defaults, $v) { $locationTypeId = NULL; $phoneTypeId = NULL; - $saveMappingFields = new CRM_Core_DAO_MappingField(); + $saveMappingFields = $defaults; - $saveMappingFields->mapping_id = $mappingId; - $saveMappingFields->name = CRM_Utils_Array::value('1', $v); - $saveMappingFields->contact_type = CRM_Utils_Array::value('0', $v); + $saveMappingFields['name'] = CRM_Utils_Array::value('1', $v); + $saveMappingFields['contact_type'] = CRM_Utils_Array::value('0', $v); $locationId = CRM_Utils_Array::value('2', $v); - $saveMappingFields->location_type_id = is_numeric($locationId) ? $locationId : NULL; + $saveMappingFields['location_type_id'] = is_numeric($locationId) ? $locationId : NULL; if ($v[1] == 'phone') { - $saveMappingFields->phone_type_id = CRM_Utils_Array::value('3', $v); + $saveMappingFields['phone_type_id'] = CRM_Utils_Array::value('3', $v); } elseif ($v[1] == 'im') { - $saveMappingFields->im_provider_id = CRM_Utils_Array::value('3', $v); + $saveMappingFields['im_provider_id'] = CRM_Utils_Array::value('3', $v); } - if (!empty($params['operator'])) { - $saveMappingFields->operator = CRM_Utils_Array::value($k, $params['operator'][$key]); - } - if (!empty($params['value'])) { - $saveMappingFields->value = CRM_Utils_Array::value($k, $params['value'][$key]); - } // Handle mapping for 'related contact' fields if (count(explode('_', CRM_Utils_Array::value('1', $v))) > 2) { list($id, $first, $second) = explode('_', CRM_Utils_Array::value('1', $v)); if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) { if (!empty($v['2'])) { - $saveMappingFields->name = CRM_Utils_Array::value('2', $v); + $saveMappingFields['name'] = CRM_Utils_Array::value('2', $v); } elseif (!empty($v['4'])) { - $saveMappingFields->name = CRM_Utils_Array::value('4', $v); + $saveMappingFields['name'] = CRM_Utils_Array::value('4', $v); } if (is_numeric(CRM_Utils_Array::value('3', $v))) { @@ -1052,16 +1041,14 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping { $phoneTypeId = CRM_Utils_Array::value('6', $v); } - $saveMappingFields->location_type_id = is_numeric($locationTypeId) ? $locationTypeId : NULL; - $saveMappingFields->phone_type_id = is_numeric($phoneTypeId) ? $phoneTypeId : NULL; - $saveMappingFields->relationship_type_id = $id; - $saveMappingFields->relationship_direction = "{$first}_{$second}"; + $saveMappingFields['location_type_id'] = is_numeric($locationTypeId) ? $locationTypeId : NULL; + $saveMappingFields['phone_type_id'] = is_numeric($phoneTypeId) ? $phoneTypeId : NULL; + $saveMappingFields['relationship_type_id'] = $id; + $saveMappingFields['relationship_direction'] = "{$first}_{$second}"; } } - $saveMappingFields->grouping = $key; - $saveMappingFields->column_number = $colCnt; - return (array) $saveMappingFields; + return $saveMappingFields; } /** @@ -1298,7 +1285,14 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping { foreach ($value as $k => $v) { if (!empty($v['1'])) { - $saveMappingParams = self::getMappingParams($params, $mappingId, $v, $k, $key, $colCnt); + $saveMappingParams = self::getMappingParams( + [ + 'mapping_id' => $mappingId, + 'grouping' => $key, + 'operator' => $params['operator'][$key][$k] ?? NULL, + 'value' => $params['value'][$key][$k] ?? NULL, + 'column_number' => $colCnt, + ], $v); $saveMappingField = new CRM_Core_DAO_MappingField(); $saveMappingField->copyValues($saveMappingParams, TRUE); $saveMappingField->save(); diff --git a/tests/phpunit/CRM/Core/BAO/MappingTest.php b/tests/phpunit/CRM/Core/BAO/MappingTest.php index 0e9ba4b7d1..50dea9ef59 100644 --- a/tests/phpunit/CRM/Core/BAO/MappingTest.php +++ b/tests/phpunit/CRM/Core/BAO/MappingTest.php @@ -7,139 +7,216 @@ */ class CRM_Core_BAO_MappingTest extends CiviUnitTestCase { + /** + * Cleanup after test. + * + * @throws \CRM_Core_Exception + */ + public function tearDown() { + $this->quickCleanup(['civicrm_mapping_field', 'civicrm_mapping']); + parent::tearDown(); + } + /** * Test calling saveMapping. + * + * @dataProvider getMappingMaps */ - public function testSaveMappingFields() { + public function testSaveMappingFields($params, $expected) { $mapping = $this->callAPISuccess('Mapping', 'create', ['name' => 'teest']); - $params = [ - 'qfKey' => '8d3bae0f77b62314516c1253176a1c1a_6756', - 'entryURL' => 'http://dmaster.local/civicrm/contribute/search?reset=1', - 'saveMappingName' => 'test', - 'saveMappingDesc' => '', - 'saveMapping' => '1', - 'mapper' => [ - 1 => - [ - ['Individual', '10_b_a', 'id'], - ['Individual', 'city', ' '], - ['Individual', 'contact_sub_type'], - ['Student', 'custom_27'], - ['Individual', 'current_employer'], - ['Individual', 'phone', '1', '2'], - ['Individual', 'postal_code', '2'], - ['Individual', 'im', '1', '1'], - ['Individual', 'url'], - ['Individual', '1_b_a', 'phone', '5', '1'], - ], - ], - ]; CRM_Core_BAO_Mapping::saveMappingFields($params, $mapping['id']); - $expected = [ - 1 => - [ - 'id' => '1', - 'mapping_id' => '1', - 'name' => 'id', - 'contact_type' => 'Individual', - 'column_number' => '0', - 'relationship_type_id' => '10', - 'relationship_direction' => 'b_a', - 'grouping' => '1', - ], - 2 => - [ - 'id' => '2', - 'mapping_id' => '1', - 'name' => 'city', - 'contact_type' => 'Individual', - 'column_number' => '1', - 'grouping' => '1', - ], - 3 => - [ - 'id' => '3', - 'mapping_id' => '1', - 'name' => 'contact_sub_type', - 'contact_type' => 'Individual', - 'column_number' => '2', - 'grouping' => '1', - ], - 4 => - [ - 'id' => '4', - 'mapping_id' => '1', - 'name' => 'custom_27', - 'contact_type' => 'Student', - 'column_number' => '3', - 'grouping' => '1', - ], - 5 => - [ - 'id' => '5', - 'mapping_id' => '1', - 'name' => 'current_employer', - 'contact_type' => 'Individual', - 'column_number' => '4', - 'grouping' => '1', - ], - 6 => - [ - 'id' => '6', - 'mapping_id' => '1', - 'name' => 'phone', - 'contact_type' => 'Individual', - 'column_number' => '5', - 'location_type_id' => '1', - 'phone_type_id' => '2', - 'grouping' => '1', - ], - 7 => - [ - 'id' => '7', - 'mapping_id' => '1', - 'name' => 'postal_code', - 'contact_type' => 'Individual', - 'column_number' => '6', - 'location_type_id' => '2', - 'grouping' => '1', + $saved = $this->callAPISuccess('MappingField', 'get', ['mapping_id' => $mapping['id']])['values']; + $this->assertEquals($expected, $saved); + } + + /** + * Get data for mapping test. + * + * @return array + */ + public function getMappingMaps() { + return [ + [ + 'params' => [ + 'qfKey' => '8d3bae0f77b62314516c1253176a1c1a_6756', + 'entryURL' => 'http://dmaster.local/civicrm/contribute/search?reset=1', + 'saveMappingName' => 'test', + 'saveMappingDesc' => '', + 'saveMapping' => '1', + 'mapper' => [ + 1 => + [ + ['Individual', '10_b_a', 'id'], + ['Individual', 'city', ' '], + ['Individual', 'contact_sub_type'], + ['Student', 'custom_27'], + ['Individual', 'current_employer'], + ['Individual', 'phone', '1', '2'], + ['Individual', 'postal_code', '2'], + ['Individual', 'im', '1', '1'], + ['Individual', 'url'], + ['Individual', '1_b_a', 'phone', '5', '1'], + ], + ], ], - 8 => - [ - 'id' => '8', - 'mapping_id' => '1', - 'name' => 'im', - 'contact_type' => 'Individual', - 'column_number' => '7', - 'location_type_id' => '1', - 'im_provider_id' => '1', - 'grouping' => '1', + 'expected' => $expected = [ + 1 => + [ + 'id' => '1', + 'mapping_id' => '1', + 'name' => 'id', + 'contact_type' => 'Individual', + 'column_number' => '0', + 'relationship_type_id' => '10', + 'relationship_direction' => 'b_a', + 'grouping' => '1', + ], + 2 => + [ + 'id' => '2', + 'mapping_id' => '1', + 'name' => 'city', + 'contact_type' => 'Individual', + 'column_number' => '1', + 'grouping' => '1', + ], + 3 => + [ + 'id' => '3', + 'mapping_id' => '1', + 'name' => 'contact_sub_type', + 'contact_type' => 'Individual', + 'column_number' => '2', + 'grouping' => '1', + ], + 4 => + [ + 'id' => '4', + 'mapping_id' => '1', + 'name' => 'custom_27', + 'contact_type' => 'Student', + 'column_number' => '3', + 'grouping' => '1', + ], + 5 => + [ + 'id' => '5', + 'mapping_id' => '1', + 'name' => 'current_employer', + 'contact_type' => 'Individual', + 'column_number' => '4', + 'grouping' => '1', + ], + 6 => + [ + 'id' => '6', + 'mapping_id' => '1', + 'name' => 'phone', + 'contact_type' => 'Individual', + 'column_number' => '5', + 'location_type_id' => '1', + 'phone_type_id' => '2', + 'grouping' => '1', + ], + 7 => + [ + 'id' => '7', + 'mapping_id' => '1', + 'name' => 'postal_code', + 'contact_type' => 'Individual', + 'column_number' => '6', + 'location_type_id' => '2', + 'grouping' => '1', + ], + 8 => + [ + 'id' => '8', + 'mapping_id' => '1', + 'name' => 'im', + 'contact_type' => 'Individual', + 'column_number' => '7', + 'location_type_id' => '1', + 'im_provider_id' => '1', + 'grouping' => '1', + ], + 9 => + [ + 'id' => '9', + 'mapping_id' => '1', + 'name' => 'url', + 'contact_type' => 'Individual', + 'column_number' => '8', + 'grouping' => '1', + ], + 10 => + [ + 'id' => '10', + 'mapping_id' => '1', + 'name' => 'phone', + 'contact_type' => 'Individual', + 'column_number' => '9', + 'location_type_id' => '5', + 'phone_type_id' => '1', + 'relationship_type_id' => '1', + 'relationship_direction' => 'b_a', + 'grouping' => '1', + ], ], - 9 => - [ - 'id' => '9', - 'mapping_id' => '1', - 'name' => 'url', - 'contact_type' => 'Individual', - 'column_number' => '8', - 'grouping' => '1', + ], + [ + 'params' => [ + 'qfKey' => '054b561dbf948548f7883151f879fe72_533', + 'entryURL' => 'http://dmaster.local/civicrm/contact/search/builder?reset=1', + 'mapper' => [ + [ + ['Contact', 'addressee_id'], + ['Individual', 'custom_2'], + [''], + [''], + [''], + ], + [[''], [''], [''], [''], ['']], + [[''], [''], [''], [''], ['']], + ], + 'operator' => [ + ['IS NOT NULL', 'IS NULL', '', '', ''], + ['', '', '', '', ''], + ['', '', '', '', ''], + ], + 'value' => [ + ['', '', '', '', ''], + ['', '', '', '', ''], + ['', '', '', '', ''], + ], + 'task' => '12', + 'radio_ts' => 'ts_sel', + 'toggleSelect' => '1', + 'mark_x_98' => '1', + 'title' => 'mmo', + ], - 10 => - [ - 'id' => '10', - 'mapping_id' => '1', - 'name' => 'phone', - 'contact_type' => 'Individual', - 'column_number' => '9', - 'location_type_id' => '5', - 'phone_type_id' => '1', - 'relationship_type_id' => '1', - 'relationship_direction' => 'b_a', - 'grouping' => '1', + 'expected' => [ + 1 => [ + 'id' => '1', + 'mapping_id' => '1', + 'name' => 'addressee_id', + 'contact_type' => 'Contact', + 'column_number' => '0', + 'grouping' => '0', + 'operator' => 'IS NOT NULL', + ], + 2 => [ + 'id' => '2', + 'mapping_id' => '1', + 'name' => 'custom_2', + 'contact_type' => 'Individual', + 'column_number' => '1', + 'grouping' => '0', + 'operator' => 'IS NULL', + ], ], + ], ]; - $saved = $this->callAPISuccess('MappingField', 'get', ['mapping_id' => $mapping['id']])['values']; - $this->assertEquals($expected, $saved); } } -- 2.25.1