Commit | Line | Data |
---|---|---|
52356383 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
52356383 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
52356383 | 7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * @file | |
30 | * File for the CRM_Contact_Import_Form_MapFieldTest class. | |
31 | */ | |
32 | ||
33 | /** | |
92cd6df0 | 34 | * Test contact import map field. |
52356383 | 35 | * |
36 | * @package CiviCRM | |
37 | * @group headless | |
38 | */ | |
39 | class CRM_Contact_Import_Form_MapFieldTest extends CiviUnitTestCase { | |
40 | ||
92bde46a | 41 | use CRM_Contact_Import_MetadataTrait; |
42 | use CRMTraits_Custom_CustomDataTrait; | |
43 | ||
44 | /** | |
45 | * Map field form. | |
46 | * | |
47 | * @var CRM_Contact_Import_Form_MapField | |
48 | */ | |
49 | protected $form; | |
50 | ||
52356383 | 51 | /** |
52 | * Test the form loads without error / notice and mappings are assigned. | |
53 | * | |
54 | * (Added in conjunction with fixed noting on mapping assignment). | |
37353970 | 55 | * |
56 | * @dataProvider getSubmitData | |
57 | * | |
58 | * @param array $params | |
59 | * @param array $mapper | |
60 | * @param array $expecteds | |
92cd6df0 | 61 | * |
62 | * @throws \CRM_Core_Exception | |
63 | * @throws \CiviCRM_API3_Exception | |
52356383 | 64 | */ |
9099cab3 | 65 | public function testSubmit($params, $mapper, $expecteds = []) { |
92cd6df0 | 66 | 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'); |
52356383 | 67 | $form = $this->getFormObject('CRM_Contact_Import_Form_MapField'); |
92cd6df0 | 68 | /* @var CRM_Contact_Import_Form_MapField $form */ |
52356383 | 69 | $form->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); |
9099cab3 | 70 | $form->_columnNames = ['nada', 'first_name', 'last_name', 'address']; |
37353970 | 71 | $form->set('importTableName', 'civicrm_import_job_xxx'); |
72 | $form->preProcess(); | |
73 | $form->submit($params, $mapper); | |
74 | ||
75 | CRM_Core_DAO::executeQuery("DROP TABLE civicrm_import_job_xxx"); | |
76 | if (!empty($expecteds)) { | |
77 | foreach ($expecteds as $expected) { | |
9099cab3 | 78 | $result = $this->callAPISuccess($expected['entity'], 'get', array_merge($expected['values'], ['sequential' => 1])); |
37353970 | 79 | $this->assertEquals($expected['count'], $result['count']); |
80 | if (isset($expected['result'])) { | |
81 | foreach ($expected['result'] as $key => $expectedValues) { | |
82 | foreach ($expectedValues as $valueKey => $value) { | |
83 | $this->assertEquals($value, $result['values'][$key][$valueKey]); | |
84 | } | |
85 | } | |
86 | } | |
87 | } | |
88 | } | |
9099cab3 | 89 | $this->quickCleanup(['civicrm_mapping', 'civicrm_mapping_field']); |
37353970 | 90 | } |
91 | ||
92 | /** | |
93 | * Get data to pass through submit function. | |
94 | * | |
95 | * @return array | |
96 | */ | |
97 | public function getSubmitData() { | |
9099cab3 CW |
98 | return [ |
99 | 'basic_data' => [ | |
100 | [ | |
37353970 | 101 | 'saveMappingName' => '', |
102 | 'saveMappingDesc' => '', | |
9099cab3 CW |
103 | ], |
104 | [ | |
105 | 0 => [0 => 'do_not_import'], | |
106 | 1 => [0 => 'first_name'], | |
107 | 2 => [0 => 'last_name'], | |
108 | 3 => [0 => 'street_address', 1 => 2], | |
109 | ], | |
110 | ], | |
111 | 'save_mapping' => [ | |
112 | [ | |
37353970 | 113 | 'saveMappingName' => 'new mapping', |
114 | 'saveMappingDesc' => 'save it', | |
115 | 'saveMapping' => 1, | |
9099cab3 CW |
116 | ], |
117 | [ | |
118 | 0 => [0 => 'do_not_import'], | |
119 | 1 => [0 => 'first_name'], | |
120 | 2 => [0 => 'last_name'], | |
121 | 3 => [0 => 'street_address', 1 => 2], | |
122 | ], | |
123 | [ | |
124 | ['entity' => 'mapping', 'count' => 1, 'values' => ['name' => 'new mapping']], | |
125 | [ | |
37353970 | 126 | 'entity' => |
127 | 'mapping_field', | |
128 | 'count' => 4, | |
9099cab3 CW |
129 | 'values' => [], |
130 | 'result' => [ | |
131 | 0 => ['name' => '- do not import -'], | |
132 | 1 => ['name' => 'First Name'], | |
133 | 2 => ['name' => 'Last Name'], | |
134 | 3 => ['name' => 'Street Address', 'location_type_id' => 2], | |
135 | ], | |
136 | ], | |
137 | ], | |
138 | ], | |
139 | ]; | |
37353970 | 140 | } |
141 | ||
142 | /** | |
143 | * Instantiate form object | |
144 | * | |
145 | * @param string $class | |
2e4ddc6e SL |
146 | * @param array $formValues |
147 | * @param string $pageName | |
37353970 | 148 | * @return \CRM_Core_Form |
2e4ddc6e | 149 | * @throws \CRM_Core_Exception |
37353970 | 150 | */ |
2e4ddc6e | 151 | public function getFormObject($class, $formValues = [], $pageName = '') { |
37353970 | 152 | $form = parent::getFormObject($class); |
153 | $contactFields = CRM_Contact_BAO_Contact::importableFields(); | |
9099cab3 | 154 | $fields = []; |
37353970 | 155 | foreach ($contactFields as $name => $field) { |
156 | $fields[$name] = $field['title']; | |
157 | } | |
158 | $form->set('fields', $fields); | |
159 | return $form; | |
52356383 | 160 | } |
161 | ||
cb0908fc | 162 | /** |
163 | * Test the function that loads saved field mappings. | |
164 | * | |
165 | * @dataProvider mapFieldDataProvider | |
166 | * | |
167 | * @param array $fieldSpec | |
168 | * @param string $expectedJS | |
169 | * | |
170 | * @throws \CRM_Core_Exception | |
171 | * @throws \CiviCRM_API3_Exception | |
172 | */ | |
173 | public function testLoadSavedMapping($fieldSpec, $expectedJS) { | |
92bde46a | 174 | $this->setUpMapFieldForm(); |
cb0908fc | 175 | |
176 | $mapping = $this->callAPISuccess('Mapping', 'create', ['name' => 'my test']); | |
92bde46a | 177 | $this->callAPISuccess('MappingField', 'create', array_merge(['mapping_id' => $mapping['id']], $fieldSpec)); |
178 | $result = $this->loadSavedMapping($this->form, $mapping['id'], $fieldSpec['column_number']); | |
cb0908fc | 179 | $this->assertEquals($expectedJS, $result['js']); |
180 | } | |
181 | ||
92bde46a | 182 | /** |
183 | * Tests the 'final' methods for loading the direct mapping. | |
184 | * | |
185 | * In conjunction with testing our existing function this tests the methods we want to migrate to | |
186 | * to clean it up. | |
187 | * | |
188 | * @throws \CRM_Core_Exception | |
189 | * @throws \CiviCRM_API3_Exception | |
190 | */ | |
191 | public function testLoadSavedMappingDirect() { | |
192 | $this->entity = 'Contact'; | |
193 | $this->createCustomGroupWithFieldOfType(['title' => 'My Field']); | |
194 | $this->setUpMapFieldForm(); | |
195 | $mapping = $this->callAPISuccess('Mapping', 'create', ['name' => 'my test', 'label' => 'Special custom']); | |
196 | foreach ([ | |
197 | [ | |
198 | 'name' => 'Addressee', | |
199 | 'column_number' => '0', | |
200 | ], | |
201 | [ | |
202 | 'name' => 'Postal Greeting', | |
203 | 'column_number' => '1', | |
204 | ], | |
205 | [ | |
206 | 'name' => 'Phone', | |
207 | 'column_number' => '2', | |
208 | 'location_type_id' => '1', | |
209 | 'phone_type_id' => '1', | |
210 | ], | |
211 | [ | |
212 | 'name' => 'Street Address', | |
213 | 'column_number' => '3', | |
214 | ], | |
215 | [ | |
216 | 'name' => 'Enter text here :: My Field', | |
217 | 'column_number' => '4', | |
218 | ], | |
219 | [ | |
220 | 'name' => 'Street Address', | |
221 | 'column_number' => '5', | |
222 | 'location_type_id' => '1', | |
223 | ], | |
224 | [ | |
225 | 'name' => 'City', | |
226 | 'column_number' => '6', | |
227 | 'location_type_id' => '1', | |
228 | ], | |
229 | [ | |
230 | 'name' => 'State Province', | |
231 | 'column_number' => '7', | |
232 | 'relationship_type_id' => 4, | |
233 | 'relationship_direction' => 'a_b', | |
234 | 'location_type_id' => '1', | |
235 | ], | |
236 | [ | |
237 | 'name' => 'Url', | |
238 | 'column_number' => '8', | |
239 | 'relationship_type_id' => 4, | |
240 | 'relationship_direction' => 'a_b', | |
241 | 'website_type_id' => 2, | |
242 | ], | |
243 | [ | |
244 | 'name' => 'Phone', | |
245 | 'column_number' => '9', | |
246 | 'relationship_type_id' => 4, | |
247 | 'location_type_id' => '1', | |
248 | 'relationship_direction' => 'a_b', | |
249 | 'phone_type_id' => 2, | |
250 | ], | |
251 | [ | |
252 | 'name' => 'Phone', | |
253 | 'column_number' => '10', | |
254 | 'location_type_id' => '1', | |
255 | 'phone_type_id' => '3', | |
256 | ], | |
257 | ] as $mappingField) { | |
258 | $this->callAPISuccess('MappingField', 'create', array_merge([ | |
259 | 'mapping_id' => $mapping['id'], | |
260 | 'grouping' => 1, | |
261 | 'contact_type' => 'Individual', | |
262 | ], $mappingField)); | |
263 | } | |
264 | $processor = new CRM_Import_ImportProcessor(); | |
265 | $processor->setMappingID($mapping['id']); | |
266 | $processor->setMetadata($this->getContactImportMetadata()); | |
267 | $this->assertEquals(3, $processor->getPhoneOrIMTypeID(10)); | |
268 | $this->assertEquals(3, $processor->getPhoneTypeID(10)); | |
269 | $this->assertEquals(1, $processor->getLocationTypeID(10)); | |
270 | $this->assertEquals(2, $processor->getWebsiteTypeID(8)); | |
271 | $this->assertEquals('4_a_b', $processor->getRelationshipKey(9)); | |
272 | $this->assertEquals('addressee', $processor->getFieldName(0)); | |
273 | $this->assertEquals('street_address', $processor->getFieldName(3)); | |
274 | $this->assertEquals($this->getCustomFieldName('text'), $processor->getFieldName(4)); | |
275 | $this->assertEquals('url', $processor->getFieldName(8)); | |
276 | ||
277 | $processor->setContactTypeByConstant(CRM_Import_Parser::CONTACT_HOUSEHOLD); | |
278 | $this->assertEquals('Household', $processor->getContactType()); | |
279 | } | |
280 | ||
cb0908fc | 281 | /** |
282 | * Get data for map field tests. | |
283 | */ | |
284 | public function mapFieldDataProvider() { | |
285 | return [ | |
286 | [ | |
287 | ['name' => 'First Name', 'contact_type' => 'Individual', 'column_number' => 1], | |
92bde46a | 288 | "document.forms.MapField['mapper[1][1]'].style.display = 'none'; |
289 | document.forms.MapField['mapper[1][2]'].style.display = 'none'; | |
290 | document.forms.MapField['mapper[1][3]'].style.display = 'none';\n", | |
291 | ], | |
292 | [ | |
293 | ['name' => 'Phone', 'contact_type' => 'Individual', 'column_number' => 8, 'phone_type_id' => 1, 'location_type_id' => 2], | |
294 | "document.forms.MapField['mapper[8][3]'].style.display = 'none';\n", | |
cb0908fc | 295 | ], |
296 | ]; | |
297 | } | |
298 | ||
299 | /** | |
300 | * Wrapper for loadSavedMapping. | |
301 | * | |
302 | * This signature of the function we are calling is funky as a new extraction & will be refined. | |
303 | * | |
304 | * @param \CRM_Contact_Import_Form_MapField $form | |
305 | * | |
306 | * @param int $mappingID | |
307 | * @param int $columnNumber | |
308 | * | |
309 | * @return array | |
310 | * | |
311 | * @throws \CiviCRM_API3_Exception | |
312 | */ | |
313 | protected function loadSavedMapping($form, $mappingID, $columnNumber) { | |
314 | list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $mappingRelation, $mappingOperator, $mappingValue, $mappingWebsiteType) = CRM_Core_BAO_Mapping::getMappingFields($mappingID, TRUE); | |
315 | ||
316 | //get loaded Mapping Fields | |
317 | $mappingName = CRM_Utils_Array::value(1, $mappingName); | |
318 | $mappingLocation = CRM_Utils_Array::value(1, $mappingLocation); | |
319 | $mappingPhoneType = CRM_Utils_Array::value(1, $mappingPhoneType); | |
320 | $mappingImProvider = CRM_Utils_Array::value(1, $mappingImProvider); | |
321 | $mappingRelation = CRM_Utils_Array::value(1, $mappingRelation); | |
322 | $mappingWebsiteType = CRM_Utils_Array::value(1, $mappingWebsiteType); | |
323 | $defaults = []; | |
a2ca56ec | 324 | |
cb0908fc | 325 | $js = ''; |
326 | $hasColumnNames = TRUE; | |
327 | // @todo - can use metadata trait once https://github.com/civicrm/civicrm-core/pull/15018 is merged. | |
328 | $dataPatterns = []; | |
329 | $columnPatterns = []; | |
a2ca56ec | 330 | $processor = new CRM_Import_ImportProcessor(); |
331 | $processor->setMappingID($mappingID); | |
332 | $processor->setFormName('document.forms.MapField'); | |
333 | $processor->setMetadata($this->getContactImportMetadata()); | |
cb0908fc | 334 | |
a2ca56ec | 335 | $return = $form->loadSavedMapping($processor, $mappingName, $columnNumber, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns); |
cb0908fc | 336 | return ['defaults' => $return[0], 'js' => $return[1]]; |
337 | } | |
338 | ||
92bde46a | 339 | /** |
340 | * Set up the mapping form. | |
341 | * | |
342 | * @throws \CRM_Core_Exception | |
343 | */ | |
344 | private function setUpMapFieldForm() { | |
345 | $this->form = $this->getFormObject('CRM_Contact_Import_Form_MapField'); | |
346 | $this->form->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); | |
347 | } | |
348 | ||
52356383 | 349 | } |