Commit | Line | Data |
---|---|---|
e87ff4ce | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
e87ff4ce | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
e87ff4ce | 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_Imports_Parser_ContactTest class. | |
31 | */ | |
32 | ||
e87ff4ce | 33 | /** |
34 | * Test contact import parser. | |
35 | * | |
36 | * @package CiviCRM | |
acb109b7 | 37 | * @group headless |
e87ff4ce | 38 | */ |
91f1e690 | 39 | class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { |
40 | protected $_tablesToTruncate = ['civicrm_address', 'civicrm_phone']; | |
e87ff4ce | 41 | |
42 | /** | |
43 | * Setup function. | |
44 | */ | |
45 | public function setUp() { | |
46 | parent::setUp(); | |
47 | } | |
48 | ||
755a3281 AP |
49 | /** |
50 | * Test that import parser will add contact with employee of relationship. | |
51 | * | |
52 | * @throws \Exception | |
53 | */ | |
54 | public function testImportParserWtihEmployeeOfRelationship() { | |
55 | $this->organizationCreate(array( | |
56 | "organization_name" => "Agileware", | |
57 | "legal_name" => "Agileware", | |
58 | )); | |
59 | $contactImportValues = array( | |
60 | "first_name" => "Alok", | |
61 | "last_name" => "Patel", | |
62 | "Employee of" => "Agileware", | |
63 | ); | |
64 | ||
65 | $fields = array_keys($contactImportValues); | |
66 | $values = array_values($contactImportValues); | |
4d847bab | 67 | $parser = new CRM_Contact_Import_Parser_Contact($fields, []); |
755a3281 AP |
68 | $parser->_contactType = 'Individual'; |
69 | $parser->init(); | |
70 | $this->mapRelationshipFields($fields, $parser->getAllFields()); | |
71 | ||
4d847bab | 72 | $parser = new CRM_Contact_Import_Parser_Contact($fields, [], [], [], array( |
755a3281 AP |
73 | NULL, |
74 | NULL, | |
75 | $fields[2], | |
76 | ), array( | |
77 | NULL, | |
78 | NULL, | |
79 | "Organization", | |
80 | ), array( | |
81 | NULL, | |
82 | NULL, | |
83 | "organization_name", | |
4d847bab | 84 | ), [], [], [], [], []); |
755a3281 AP |
85 | |
86 | $parser->_contactType = 'Individual'; | |
87 | $parser->_onDuplicate = CRM_Import_Parser::DUPLICATE_UPDATE; | |
88 | $parser->init(); | |
89 | ||
90 | $this->assertEquals(CRM_Import_Parser::VALID, $parser->import(CRM_Import_Parser::DUPLICATE_UPDATE, $values), 'Return code from parser import was not as expected'); | |
91 | $this->callAPISuccess("Contact", "get", array( | |
92 | "first_name" => "Alok", | |
93 | "last_name" => "Patel", | |
94 | "organization_name" => "Agileware", | |
95 | )); | |
96 | } | |
97 | ||
13943a50 JM |
98 | /** |
99 | * Test that import parser will not fail when same external_identifier found of deleted contact. | |
100 | * | |
101 | * @throws \Exception | |
102 | */ | |
103 | public function testImportParserWtihDeletedContactExternalIdentifier() { | |
104 | $contactId = $this->individualCreate(array( | |
105 | "external_identifier" => "ext-1", | |
106 | )); | |
107 | CRM_Contact_BAO_Contact::deleteContact($contactId); | |
108 | list($originalValues, $result) = $this->setUpBaseContact(array( | |
109 | 'external_identifier' => 'ext-1', | |
110 | )); | |
111 | $originalValues['nick_name'] = 'Old Bill'; | |
112 | $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID); | |
113 | $originalValues['id'] = $result['id']; | |
114 | $this->assertEquals('ext-1', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'external_identifier'))); | |
115 | $this->callAPISuccessGetSingle('Contact', $originalValues); | |
116 | } | |
117 | ||
e87ff4ce | 118 | /** |
8fd37b20 | 119 | * Test import parser will update based on a rule match. |
120 | * | |
121 | * In this case the contact has no external identifier. | |
e87ff4ce | 122 | * |
123 | * @throws \Exception | |
124 | */ | |
125 | public function testImportParserWithUpdateWithoutExternalIdentifier() { | |
8fd37b20 | 126 | list($originalValues, $result) = $this->setUpBaseContact(); |
e87ff4ce | 127 | $originalValues['nick_name'] = 'Old Bill'; |
128 | $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID); | |
129 | $originalValues['id'] = $result['id']; | |
130 | $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name'))); | |
131 | $this->callAPISuccessGetSingle('Contact', $originalValues); | |
132 | } | |
133 | ||
134 | /** | |
8fd37b20 | 135 | * Test import parser will update contacts with an external identifier. |
136 | * | |
137 | * This is the basic test where the identifier matches the import parameters. | |
e87ff4ce | 138 | * |
139 | * @throws \Exception | |
140 | */ | |
141 | public function testImportParserWithUpdateWithExternalIdentifier() { | |
8fd37b20 | 142 | list($originalValues, $result) = $this->setUpBaseContact(array('external_identifier' => 'windows')); |
143 | ||
e87ff4ce | 144 | $this->assertEquals($result['id'], CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', 'windows', 'id', 'external_identifier', TRUE)); |
145 | $this->assertEquals('windows', $result['external_identifier']); | |
8fd37b20 | 146 | |
e87ff4ce | 147 | $originalValues['nick_name'] = 'Old Bill'; |
148 | $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID); | |
149 | $originalValues['id'] = $result['id']; | |
8fd37b20 | 150 | |
e87ff4ce | 151 | $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name'))); |
152 | $this->callAPISuccessGetSingle('Contact', $originalValues); | |
153 | } | |
154 | ||
65070890 | 155 | /** |
156 | * Test import parser will fallback to external identifier. | |
157 | * | |
158 | * In this case no primary match exists (e.g the details are not supplied) so it falls back on external identifier. | |
159 | * | |
160 | * CRM-17275 | |
161 | * | |
162 | * @throws \Exception | |
163 | */ | |
164 | public function testImportParserWithUpdateWithExternalIdentifierButNoPrimaryMatch() { | |
165 | list($originalValues, $result) = $this->setUpBaseContact(array( | |
166 | 'external_identifier' => 'windows', | |
167 | 'email' => NULL, | |
168 | )); | |
169 | ||
170 | $this->assertEquals('windows', $result['external_identifier']); | |
171 | ||
172 | $originalValues['nick_name'] = 'Old Bill'; | |
173 | $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID); | |
174 | $originalValues['id'] = $result['id']; | |
175 | ||
176 | $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name'))); | |
177 | $this->callAPISuccessGetSingle('Contact', $originalValues); | |
178 | } | |
179 | ||
eb5f7260 | 180 | /** |
8fd37b20 | 181 | * Test that the import parser adds the external identifier where none is set. |
eb5f7260 | 182 | * |
183 | * @throws \Exception | |
184 | */ | |
8fd37b20 | 185 | public function testImportParserWithUpdateWithNoExternalIdentifier() { |
186 | list($originalValues, $result) = $this->setUpBaseContact(); | |
eb5f7260 | 187 | $originalValues['nick_name'] = 'Old Bill'; |
188 | $originalValues['external_identifier'] = 'windows'; | |
189 | $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID); | |
190 | $originalValues['id'] = $result['id']; | |
191 | $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name'))); | |
192 | $this->callAPISuccessGetSingle('Contact', $originalValues); | |
193 | } | |
194 | ||
65070890 | 195 | /** |
196 | * Test that the import parser changes the external identifier when there is a dedupe match. | |
197 | * | |
198 | * @throws \Exception | |
199 | */ | |
200 | public function testImportParserWithUpdateWithChangedExternalIdentifier() { | |
201 | list($contactValues, $result) = $this->setUpBaseContact(array('external_identifier' => 'windows')); | |
202 | $contact_id = $result['id']; | |
203 | $contactValues['nick_name'] = 'Old Bill'; | |
204 | $contactValues['external_identifier'] = 'android'; | |
205 | $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID); | |
206 | $contactValues['id'] = $contact_id; | |
207 | $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $contact_id, 'return' => 'nick_name'))); | |
208 | $this->callAPISuccessGetSingle('Contact', $contactValues); | |
209 | } | |
210 | ||
10741f35 | 211 | /** |
6ebecfea | 212 | * Test that the import parser adds the address to the right location. |
10741f35 | 213 | * |
214 | * @throws \Exception | |
215 | */ | |
216 | public function testImportBillingAddress() { | |
217 | list($contactValues) = $this->setUpBaseContact(); | |
218 | $contactValues['nick_name'] = 'Old Bill'; | |
219 | $contactValues['external_identifier'] = 'android'; | |
220 | $contactValues['street_address'] = 'Big Mansion'; | |
6ebecfea | 221 | $contactValues['phone'] = '911'; |
222 | $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 2, 6 => 2)); | |
10741f35 | 223 | $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion')); |
224 | $this->assertEquals(2, $address['location_type_id']); | |
225 | ||
6ebecfea | 226 | $phone = $this->callAPISuccessGetSingle('Phone', array('phone' => '911')); |
227 | $this->assertEquals(2, $phone['location_type_id']); | |
228 | ||
229 | $contact = $this->callAPISuccessGetSingle('Contact', $contactValues); | |
230 | $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id'])); | |
231 | } | |
232 | ||
233 | /** | |
234 | * Test that the import parser adds the address to the primary location. | |
235 | * | |
236 | * @throws \Exception | |
237 | */ | |
238 | public function testImportPrimaryAddress() { | |
239 | list($contactValues) = $this->setUpBaseContact(); | |
240 | $contactValues['nick_name'] = 'Old Bill'; | |
241 | $contactValues['external_identifier'] = 'android'; | |
242 | $contactValues['street_address'] = 'Big Mansion'; | |
243 | $contactValues['phone'] = 12334; | |
244 | $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 'Primary', 6 => 'Primary')); | |
245 | $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion')); | |
246 | $this->assertEquals(1, $address['location_type_id']); | |
c77e8e33 | 247 | $this->assertEquals(1, $address['is_primary']); |
6ebecfea | 248 | |
91f1e690 | 249 | $this->markTestIncomplete('phone actually doesn\'t work'); |
6ebecfea | 250 | $phone = $this->callAPISuccessGetSingle('Phone', array('phone' => '12334')); |
251 | $this->assertEquals(1, $phone['location_type_id']); | |
252 | ||
253 | $contact = $this->callAPISuccessGetSingle('Contact', $contactValues); | |
254 | $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id'])); | |
255 | } | |
256 | ||
6fdc0e61 | 257 | /** |
258 | * Test that the import parser adds the address to the primary location. | |
259 | * | |
260 | * @throws \Exception | |
261 | */ | |
262 | public function testImportDeceased() { | |
263 | list($contactValues) = $this->setUpBaseContact(); | |
264 | CRM_Core_Session::singleton()->set("dateTypes", 1); | |
265 | $contactValues['birth_date'] = '1910-12-17'; | |
266 | $contactValues['deceased_date'] = '2010-12-17'; | |
267 | $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID); | |
268 | $contact = $this->callAPISuccessGetSingle('Contact', $contactValues); | |
269 | $this->assertEquals('1910-12-17', $contact['birth_date']); | |
270 | $this->assertEquals('2010-12-17', $contact['deceased_date']); | |
271 | $this->assertEquals(1, $contact['is_deceased']); | |
272 | $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id'])); | |
273 | } | |
274 | ||
275 | ||
6ebecfea | 276 | /** |
277 | * Test that the import parser adds the address to the primary location. | |
278 | * | |
279 | * @throws \Exception | |
280 | */ | |
281 | public function testImportTwoAddressFirstPrimary() { | |
282 | list($contactValues) = $this->setUpBaseContact(); | |
283 | $contactValues['nick_name'] = 'Old Bill'; | |
284 | $contactValues['external_identifier'] = 'android'; | |
285 | $contactValues['street_address'] = 'Big Mansion'; | |
286 | $contactValues['phone'] = 12334; | |
287 | $fields = array_keys($contactValues); | |
288 | $contactValues['street_address_2'] = 'Teeny Mansion'; | |
289 | $contactValues['phone_2'] = 4444; | |
290 | $fields[] = 'street_address'; | |
291 | $fields[] = 'phone'; | |
292 | $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 'Primary', 6 => 'Primary', 7 => 3, 8 => 3), $fields); | |
293 | $contact = $this->callAPISuccessGetSingle('Contact', array('external_identifier' => 'android')); | |
294 | $address = $this->callAPISuccess('Address', 'get', array('contact_id' => $contact['id'], 'sequential' => 1)); | |
295 | ||
296 | $this->assertEquals(3, $address['values'][0]['location_type_id']); | |
297 | $this->assertEquals(0, $address['values'][0]['is_primary']); | |
298 | $this->assertEquals('Teeny Mansion', $address['values'][0]['street_address']); | |
299 | ||
300 | $this->assertEquals(1, $address['values'][1]['location_type_id']); | |
301 | $this->assertEquals(1, $address['values'][1]['is_primary']); | |
302 | $this->assertEquals('Big Mansion', $address['values'][1]['street_address']); | |
303 | ||
91f1e690 | 304 | $this->markTestIncomplete('phone import primary actually IS broken'); |
6ebecfea | 305 | $phone = $this->callAPISuccess('Phone', 'get', array('contact_id' => $contact['id'], 'sequential' => 1)); |
306 | $this->assertEquals(1, $phone['values'][0]['location_type_id']); | |
307 | $this->assertEquals(1, $phone['values'][0]['is_primary']); | |
308 | $this->assertEquals(12334, $phone['values'][0]['phone']); | |
309 | $this->assertEquals(3, $phone['values'][1]['location_type_id']); | |
310 | $this->assertEquals(0, $phone['values'][1]['is_primary']); | |
311 | $this->assertEquals(4444, $phone['values'][1]['phone']); | |
312 | ||
313 | $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id'])); | |
314 | } | |
315 | ||
316 | /** | |
317 | * Test that the import parser adds the address to the primary location. | |
318 | * | |
319 | * @throws \Exception | |
320 | */ | |
321 | public function testImportTwoAddressSecondPrimary() { | |
322 | list($contactValues) = $this->setUpBaseContact(); | |
323 | $contactValues['nick_name'] = 'Old Bill'; | |
324 | $contactValues['external_identifier'] = 'android'; | |
325 | $contactValues['street_address'] = 'Big Mansion'; | |
326 | $contactValues['phone'] = 12334; | |
327 | $fields = array_keys($contactValues); | |
328 | $contactValues['street_address_2'] = 'Teeny Mansion'; | |
329 | $contactValues['phone_2'] = 4444; | |
330 | $fields[] = 'street_address'; | |
331 | $fields[] = 'phone'; | |
332 | $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 3, 6 => 3, 7 => 'Primary', 8 => 'Primary'), $fields); | |
333 | $contact = $this->callAPISuccessGetSingle('Contact', array('external_identifier' => 'android')); | |
91f1e690 | 334 | $address = $this->callAPISuccess('Address', 'get', array('contact_id' => $contact['id'], 'sequential' => 1))['values']; |
6ebecfea | 335 | |
91f1e690 | 336 | $this->assertEquals(1, $address[1]['location_type_id']); |
337 | $this->assertEquals(1, $address[1]['is_primary']); | |
338 | $this->assertEquals('Teeny Mansion', $address[1]['street_address']); | |
6ebecfea | 339 | |
91f1e690 | 340 | $this->assertEquals(3, $address[0]['location_type_id']); |
341 | $this->assertEquals(0, $address[0]['is_primary']); | |
342 | $this->assertEquals('Big Mansion', $address[0]['street_address']); | |
6ebecfea | 343 | |
91f1e690 | 344 | $this->markTestIncomplete('phone import primary actually IS broken'); |
345 | $phone = $this->callAPISuccess('Phone', 'get', array('contact_id' => $contact['id'], 'sequential' => 1))['values']; | |
346 | $this->assertEquals(3, $phone[1]['location_type_id']); | |
347 | $this->assertEquals(0, $phone[1]['is_primary']); | |
348 | $this->assertEquals(12334, $phone[1]['phone']); | |
349 | $this->assertEquals(1, $phone[0]['location_type_id']); | |
350 | $this->assertEquals(1, $phone[0]['is_primary']); | |
351 | $this->assertEquals(4444, $phone[0]['phone']); | |
6ebecfea | 352 | |
353 | $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id'])); | |
354 | } | |
355 | ||
356 | /** | |
357 | * Test that the import parser updates the address on the existing primary location. | |
358 | * | |
359 | * @throws \Exception | |
360 | */ | |
361 | public function testImportPrimaryAddressUpdate() { | |
362 | list($contactValues) = $this->setUpBaseContact(array('external_identifier' => 'android')); | |
363 | $contactValues['nick_name'] = 'Old Bill'; | |
364 | $contactValues['external_identifier'] = 'android'; | |
365 | $contactValues['street_address'] = 'Big Mansion'; | |
c77e8e33 | 366 | $contactValues['city'] = 'Big City'; |
6ebecfea | 367 | $contactID = $this->callAPISuccessGetValue('Contact', array('external_identifier' => 'android', 'return' => 'id')); |
368 | $originalAddress = $this->callAPISuccess('Address', 'create', array('location_type_id' => 2, 'street_address' => 'small house', 'contact_id' => $contactID)); | |
c77e8e33 | 369 | $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 'Primary', 6 => 'Primary')); |
6ebecfea | 370 | $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion')); |
371 | $this->assertEquals(2, $address['location_type_id']); | |
372 | $this->assertEquals($originalAddress['id'], $address['id']); | |
c77e8e33 | 373 | $this->assertEquals('Big City', $address['city']); |
10741f35 | 374 | $this->callAPISuccessGetSingle('Contact', $contactValues); |
375 | } | |
376 | ||
13943a50 JM |
377 | /** |
378 | * Test the determination of whether a custom field is valid. | |
379 | */ | |
412585fb | 380 | public function testCustomFieldValidation() { |
381 | $errorMessage = array(); | |
3b2c326e | 382 | $customGroup = $this->customGroupCreate(array( |
383 | 'extends' => 'Contact', | |
f1230f06 | 384 | 'title' => 'ABC', |
3b2c326e | 385 | )); |
412585fb | 386 | $customField = $this->customFieldOptionValueCreate($customGroup, 'fieldABC', array('html_type' => 'Multi-Select')); |
387 | $params = array( | |
388 | 'custom_' . $customField['id'] => 'Label1|Label2', | |
389 | ); | |
390 | CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); | |
391 | $this->assertEquals(array(), $errorMessage); | |
392 | } | |
393 | ||
6e975197 JM |
394 | /** |
395 | * Test that setting duplicate action to fill doesn't blow away data | |
396 | * that exists, but does fill in where it's empty. | |
397 | * | |
398 | * @throw \Exception | |
399 | */ | |
400 | public function testImportFill() { | |
401 | // Create a custom field group for testing. | |
402 | $custom_group_name = 'importFillGroup'; | |
403 | $results = $this->callAPISuccess('customGroup', 'get', array('title' => $custom_group_name)); | |
404 | if ($results['count'] == 0) { | |
405 | $api_params = array( | |
406 | 'title' => $custom_group_name, | |
407 | 'extends' => 'Individual', | |
408 | 'is_active' => TRUE, | |
409 | ); | |
410 | $customGroup = $this->callAPISuccess('customGroup', 'create', $api_params); | |
411 | } | |
412 | ||
413 | // Add two custom fields. | |
414 | $api_params = array( | |
415 | 'custom_group_id' => $customGroup['id'], | |
416 | 'label' => 'importFillField1', | |
417 | 'html_type' => 'Select', | |
418 | 'data_type' => 'String', | |
419 | 'option_values' => array( | |
420 | 'foo' => 'Foo', | |
421 | 'bar' => 'Bar', | |
422 | ), | |
423 | ); | |
424 | $result = $this->callAPISuccess('custom_field', 'create', $api_params); | |
425 | $customField1 = $result['id']; | |
426 | ||
427 | $api_params = array( | |
428 | 'custom_group_id' => $customGroup['id'], | |
429 | 'label' => 'importFillField2', | |
430 | 'html_type' => 'Select', | |
431 | 'data_type' => 'String', | |
432 | 'option_values' => array( | |
433 | 'baz' => 'Baz', | |
434 | 'boo' => 'Boo', | |
435 | ), | |
436 | ); | |
437 | $result = $this->callAPISuccess('custom_field', 'create', $api_params); | |
438 | $customField2 = $result['id']; | |
439 | ||
440 | // Now set up values. | |
441 | $original_gender = 'Male'; | |
442 | $original_custom1 = 'foo'; | |
443 | $original_job_title = ''; | |
444 | $original_custom2 = ''; | |
445 | $original_email = 'test-import-fill@example.org'; | |
446 | ||
447 | $import_gender = 'Female'; | |
448 | $import_custom1 = 'bar'; | |
449 | $import_job_title = 'Chief data importer'; | |
450 | $import_custom2 = 'baz'; | |
451 | ||
452 | // Create contact with both one known core field and one custom | |
453 | // field filled in. | |
454 | $api_params = array( | |
455 | 'contact_type' => 'Individual', | |
456 | 'email' => $original_email, | |
457 | 'gender' => $original_gender, | |
f3cfbe94 | 458 | 'custom_' . $customField1 => $original_custom1, |
6e975197 JM |
459 | ); |
460 | $result = $this->callAPISuccess('contact', 'create', $api_params); | |
461 | $contact_id = $result['id']; | |
462 | ||
463 | // Run an import. | |
464 | $import = array( | |
465 | 'email' => $original_email, | |
466 | 'gender_id' => $import_gender, | |
467 | 'custom_' . $customField1 => $import_custom1, | |
468 | 'job_title' => $import_job_title, | |
f3cfbe94 | 469 | 'custom_' . $customField2 => $import_custom2, |
6e975197 JM |
470 | ); |
471 | ||
472 | $this->runImport($import, CRM_Import_Parser::DUPLICATE_FILL, CRM_Import_Parser::VALID); | |
473 | ||
474 | $expected = array( | |
475 | 'gender' => $original_gender, | |
476 | 'custom_' . $customField1 => $original_custom1, | |
477 | 'job_title' => $import_job_title, | |
f3cfbe94 | 478 | 'custom_' . $customField2 => $import_custom2, |
6e975197 JM |
479 | ); |
480 | ||
481 | $params = array( | |
482 | 'id' => $contact_id, | |
483 | 'return' => array( | |
484 | 'gender', | |
485 | 'custom_' . $customField1, | |
486 | 'job_title', | |
f3cfbe94 | 487 | 'custom_' . $customField2, |
f4e4d7c3 | 488 | ), |
6e975197 JM |
489 | ); |
490 | $result = civicrm_api3('Contact', 'get', $params); | |
491 | $values = array_pop($result['values']); | |
f3cfbe94 | 492 | foreach ($expected as $field => $expected_value) { |
6e975197 | 493 | if (!isset($values[$field])) { |
f3cfbe94 | 494 | $given_value = NULL; |
6e975197 JM |
495 | } |
496 | else { | |
497 | $given_value = $values[$field]; | |
498 | } | |
499 | // We expect: | |
500 | // gender: Male | |
501 | // job_title: Chief Data Importer | |
502 | // importFillField1: foo | |
503 | // importFillField2: baz | |
504 | $this->assertEquals($expected_value, $given_value, "$field properly handled during Fill import"); | |
505 | } | |
506 | } | |
507 | ||
4352bd72 | 508 | /** |
509 | * CRM-19888 default country should be used if ambigous. | |
510 | */ | |
511 | public function testImportAmbiguousStateCountry() { | |
512 | $countries = CRM_Core_PseudoConstant::country(FALSE, FALSE); | |
513 | $this->callAPISuccess('Setting', 'create', array('countryLimit' => array(array_search('United States', $countries), array_search('Guyana', $countries), array_search('Netherlands', $countries)))); | |
514 | $this->callAPISuccess('Setting', 'create', array('provinceLimit' => array(array_search('United States', $countries), array_search('Guyana', $countries), array_search('Netherlands', $countries)))); | |
515 | $mapper = array(0 => NULL, 1 => NULL, 2 => 'Primary', 3 => NULL); | |
516 | list($contactValues) = $this->setUpBaseContact(); | |
517 | $fields = array_keys($contactValues); | |
518 | $addressValues = array( | |
519 | 'street_address' => 'PO Box 2716', | |
520 | 'city' => 'Midway', | |
521 | 'state_province' => 'UT', | |
522 | 'postal_code' => 84049, | |
523 | 'country' => 'United States', | |
524 | ); | |
525 | $locationTypes = $this->callAPISuccess('Address', 'getoptions', array('field' => 'location_type_id')); | |
526 | $locationTypes = $locationTypes['values']; | |
527 | foreach ($addressValues as $field => $value) { | |
528 | $contactValues['home_' . $field] = $value; | |
529 | $mapper[] = array_search('Home', $locationTypes); | |
530 | $contactValues['work_' . $field] = $value; | |
531 | $mapper[] = array_search('Work', $locationTypes); | |
532 | $fields[] = $field; | |
533 | $fields[] = $field; | |
534 | } | |
535 | $contactValues['work_country'] = ''; | |
536 | ||
537 | $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, $mapper, $fields); | |
538 | $addresses = $this->callAPISuccess('Address', 'get', array('contact_id' => array('>' => 2), 'sequential' => 1)); | |
539 | $this->assertEquals(2, $addresses['count']); | |
540 | $this->assertEquals(array_search('United States', $countries), $addresses['values'][0]['country_id']); | |
541 | $this->assertEquals(array_search('United States', $countries), $addresses['values'][1]['country_id']); | |
542 | } | |
543 | ||
e87ff4ce | 544 | /** |
545 | * Run the import parser. | |
546 | * | |
547 | * @param array $originalValues | |
548 | * | |
549 | * @param int $onDuplicateAction | |
550 | * @param int $expectedResult | |
10741f35 | 551 | * @param array|null $mapperLocType |
91f1e690 | 552 | * Array of location types that map to the input arrays. |
6ebecfea | 553 | * @param array|null $fields |
554 | * Array of field names. Will be calculated from $originalValues if not passed in, but | |
555 | * that method does not cope with duplicates. | |
e87ff4ce | 556 | */ |
91f1e690 | 557 | protected function runImport($originalValues, $onDuplicateAction, $expectedResult, $mapperLocType = [], $fields = NULL) { |
6ebecfea | 558 | if (!$fields) { |
559 | $fields = array_keys($originalValues); | |
560 | } | |
e87ff4ce | 561 | $values = array_values($originalValues); |
10741f35 | 562 | $parser = new CRM_Contact_Import_Parser_Contact($fields, $mapperLocType); |
e87ff4ce | 563 | $parser->_contactType = 'Individual'; |
564 | $parser->_onDuplicate = $onDuplicateAction; | |
565 | $parser->init(); | |
65070890 | 566 | $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values), 'Return code from parser import was not as expected'); |
e87ff4ce | 567 | } |
ffe87781 | 568 | |
755a3281 AP |
569 | /** |
570 | * @param array $fields Array of fields to be imported | |
571 | * @param array $allfields Array of all fields which can be part of import | |
572 | */ | |
573 | private function mapRelationshipFields(&$fields, $allfields) { | |
574 | foreach ($allfields as $key => $fieldtocheck) { | |
575 | $elementIndex = array_search($fieldtocheck->_title, $fields); | |
576 | if ($elementIndex !== FALSE) { | |
577 | $fields[$elementIndex] = $key; | |
578 | } | |
579 | } | |
580 | } | |
581 | ||
8fd37b20 | 582 | /** |
583 | * Set up the underlying contact. | |
584 | * | |
585 | * @param array $params | |
586 | * Optional extra parameters to set. | |
587 | * | |
588 | * @return array | |
589 | * @throws \Exception | |
590 | */ | |
591 | protected function setUpBaseContact($params = array()) { | |
592 | $originalValues = array_merge(array( | |
593 | 'first_name' => 'Bill', | |
594 | 'last_name' => 'Gates', | |
595 | 'email' => 'bill.gates@microsoft.com', | |
596 | 'nick_name' => 'Billy-boy', | |
597 | ), $params); | |
598 | $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID); | |
599 | $result = $this->callAPISuccessGetSingle('Contact', $originalValues); | |
600 | return array($originalValues, $result); | |
601 | } | |
602 | ||
e87ff4ce | 603 | } |