Merge pull request #15116 from eileenmcnaughton/import_website
[civicrm-core.git] / tests / phpunit / CRM / Contact / Import / Form / MapFieldTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 /**
34 * Test contact import map field.
35 *
36 * @package CiviCRM
37 * @group headless
38 */
39 class CRM_Contact_Import_Form_MapFieldTest extends CiviUnitTestCase {
40
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
51 /**
52 * Test the form loads without error / notice and mappings are assigned.
53 *
54 * (Added in conjunction with fixed noting on mapping assignment).
55 *
56 * @dataProvider getSubmitData
57 *
58 * @param array $params
59 * @param array $mapper
60 * @param array $expecteds
61 *
62 * @throws \CRM_Core_Exception
63 * @throws \CiviCRM_API3_Exception
64 */
65 public function testSubmit($params, $mapper, $expecteds = []) {
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');
67 $form = $this->getFormObject('CRM_Contact_Import_Form_MapField');
68 /* @var CRM_Contact_Import_Form_MapField $form */
69 $form->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
70 $form->_columnNames = ['nada', 'first_name', 'last_name', 'address'];
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) {
78 $result = $this->callAPISuccess($expected['entity'], 'get', array_merge($expected['values'], ['sequential' => 1]));
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 }
89 $this->quickCleanup(['civicrm_mapping', 'civicrm_mapping_field']);
90 }
91
92 /**
93 * Get data to pass through submit function.
94 *
95 * @return array
96 */
97 public function getSubmitData() {
98 return [
99 'basic_data' => [
100 [
101 'saveMappingName' => '',
102 'saveMappingDesc' => '',
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 [
113 'saveMappingName' => 'new mapping',
114 'saveMappingDesc' => 'save it',
115 'saveMapping' => 1,
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 [
126 'entity' =>
127 'mapping_field',
128 'count' => 4,
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 ];
140 }
141
142 /**
143 * Instantiate form object
144 *
145 * @param string $class
146 * @param array $formValues
147 * @param string $pageName
148 * @return \CRM_Core_Form
149 * @throws \CRM_Core_Exception
150 */
151 public function getFormObject($class, $formValues = [], $pageName = '') {
152 $form = parent::getFormObject($class);
153 $contactFields = CRM_Contact_BAO_Contact::importableFields();
154 $fields = [];
155 foreach ($contactFields as $name => $field) {
156 $fields[$name] = $field['title'];
157 }
158 $form->set('fields', $fields);
159 return $form;
160 }
161
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) {
174 $this->setUpMapFieldForm();
175
176 $mapping = $this->callAPISuccess('Mapping', 'create', ['name' => 'my test']);
177 $this->callAPISuccess('MappingField', 'create', array_merge(['mapping_id' => $mapping['id']], $fieldSpec));
178 $result = $this->loadSavedMapping($this->form, $mapping['id'], $fieldSpec['column_number']);
179 $this->assertEquals($expectedJS, $result['js']);
180 }
181
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
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],
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",
295 ],
296 [
297 ['name' => 'Phone', 'contact_type' => 'Individual', 'column_number' => 0, 'im_provider_id' => 1, 'location_type_id' => 2],
298 "document.forms.MapField['mapper[0][3]'].style.display = 'none';\n",
299 ],
300 [
301 ['name' => 'Website', 'contact_type' => 'Individual', 'column_number' => 0, 'website_type_id'],
302 "document.forms.MapField['mapper[0][1]'].style.display = 'none';
303 document.forms.MapField['mapper[0][2]'].style.display = 'none';
304 document.forms.MapField['mapper[0][3]'].style.display = 'none';\n",
305 ],
306 ];
307 }
308
309 /**
310 * Wrapper for loadSavedMapping.
311 *
312 * This signature of the function we are calling is funky as a new extraction & will be refined.
313 *
314 * @param \CRM_Contact_Import_Form_MapField $form
315 *
316 * @param int $mappingID
317 * @param int $columnNumber
318 *
319 * @return array
320 *
321 * @throws \CiviCRM_API3_Exception
322 */
323 protected function loadSavedMapping($form, $mappingID, $columnNumber) {
324 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $mappingRelation, $mappingOperator, $mappingValue, $mappingWebsiteType) = CRM_Core_BAO_Mapping::getMappingFields($mappingID, TRUE);
325
326 //get loaded Mapping Fields
327 $mappingName = CRM_Utils_Array::value(1, $mappingName);
328 $mappingLocation = CRM_Utils_Array::value(1, $mappingLocation);
329 $mappingPhoneType = CRM_Utils_Array::value(1, $mappingPhoneType);
330 $mappingImProvider = CRM_Utils_Array::value(1, $mappingImProvider);
331 $mappingRelation = CRM_Utils_Array::value(1, $mappingRelation);
332 $mappingWebsiteType = CRM_Utils_Array::value(1, $mappingWebsiteType);
333 $defaults = [];
334
335 $js = '';
336 $hasColumnNames = TRUE;
337 // @todo - can use metadata trait once https://github.com/civicrm/civicrm-core/pull/15018 is merged.
338 $dataPatterns = [];
339 $columnPatterns = [];
340 $processor = new CRM_Import_ImportProcessor();
341 $processor->setMappingID($mappingID);
342 $processor->setFormName('document.forms.MapField');
343 $processor->setMetadata($this->getContactImportMetadata());
344
345 $return = $form->loadSavedMapping($processor, $mappingName, $columnNumber, $mappingRelation, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns);
346 return ['defaults' => $return[0], 'js' => $return[1]];
347 }
348
349 /**
350 * Set up the mapping form.
351 *
352 * @throws \CRM_Core_Exception
353 */
354 private function setUpMapFieldForm() {
355 $this->form = $this->getFormObject('CRM_Contact_Import_Form_MapField');
356 $this->form->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
357 }
358
359 }