3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * File for the CRM_Contact_Imports_Parser_ContactTest class.
34 * Test contact import parser.
39 class CRM_Contact_Import_Parser_ContactTest
extends CiviUnitTestCase
{
40 protected $_tablesToTruncate = array();
45 public function setUp() {
50 * Test import parser will update based on a rule match.
52 * In this case the contact has no external identifier.
56 public function testImportParserWithUpdateWithoutExternalIdentifier() {
57 list($originalValues, $result) = $this->setUpBaseContact();
58 $originalValues['nick_name'] = 'Old Bill';
59 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
60 $originalValues['id'] = $result['id'];
61 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
62 $this->callAPISuccessGetSingle('Contact', $originalValues);
66 * Test import parser will update contacts with an external identifier.
68 * This is the basic test where the identifier matches the import parameters.
72 public function testImportParserWithUpdateWithExternalIdentifier() {
73 list($originalValues, $result) = $this->setUpBaseContact(array('external_identifier' => 'windows'));
75 $this->assertEquals($result['id'], CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', 'windows', 'id', 'external_identifier', TRUE));
76 $this->assertEquals('windows', $result['external_identifier']);
78 $originalValues['nick_name'] = 'Old Bill';
79 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
80 $originalValues['id'] = $result['id'];
82 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
83 $this->callAPISuccessGetSingle('Contact', $originalValues);
87 * Test import parser will fallback to external identifier.
89 * In this case no primary match exists (e.g the details are not supplied) so it falls back on external identifier.
95 public function testImportParserWithUpdateWithExternalIdentifierButNoPrimaryMatch() {
96 list($originalValues, $result) = $this->setUpBaseContact(array(
97 'external_identifier' => 'windows',
101 $this->assertEquals('windows', $result['external_identifier']);
103 $originalValues['nick_name'] = 'Old Bill';
104 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
105 $originalValues['id'] = $result['id'];
107 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
108 $this->callAPISuccessGetSingle('Contact', $originalValues);
112 * Test that the import parser adds the external identifier where none is set.
116 public function testImportParserWithUpdateWithNoExternalIdentifier() {
117 list($originalValues, $result) = $this->setUpBaseContact();
118 $originalValues['nick_name'] = 'Old Bill';
119 $originalValues['external_identifier'] = 'windows';
120 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
121 $originalValues['id'] = $result['id'];
122 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
123 $this->callAPISuccessGetSingle('Contact', $originalValues);
127 * Test that the import parser changes the external identifier when there is a dedupe match.
131 public function testImportParserWithUpdateWithChangedExternalIdentifier() {
132 list($contactValues, $result) = $this->setUpBaseContact(array('external_identifier' => 'windows'));
133 $contact_id = $result['id'];
134 $contactValues['nick_name'] = 'Old Bill';
135 $contactValues['external_identifier'] = 'android';
136 $this->runImport($contactValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
137 $contactValues['id'] = $contact_id;
138 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $contact_id, 'return' => 'nick_name')));
139 $this->callAPISuccessGetSingle('Contact', $contactValues);
143 * Test that the import parser adds the address to the right location.
147 public function testImportBillingAddress() {
148 list($contactValues) = $this->setUpBaseContact();
149 $contactValues['nick_name'] = 'Old Bill';
150 $contactValues['external_identifier'] = 'android';
151 $contactValues['street_address'] = 'Big Mansion';
152 $contactValues['phone'] = '911';
153 $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));
154 $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion'));
155 $this->assertEquals(2, $address['location_type_id']);
157 $phone = $this->callAPISuccessGetSingle('Phone', array('phone' => '911'));
158 $this->assertEquals(2, $phone['location_type_id']);
160 $contact = $this->callAPISuccessGetSingle('Contact', $contactValues);
161 $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id']));
165 * Test that the import parser adds the address to the primary location.
169 public function testImportPrimaryAddress() {
170 list($contactValues) = $this->setUpBaseContact();
171 $contactValues['nick_name'] = 'Old Bill';
172 $contactValues['external_identifier'] = 'android';
173 $contactValues['street_address'] = 'Big Mansion';
174 $contactValues['phone'] = 12334;
175 $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'));
176 $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion'));
177 $this->assertEquals(1, $address['location_type_id']);
178 $this->assertEquals(1, $address['is_primary']);
180 $phone = $this->callAPISuccessGetSingle('Phone', array('phone' => '12334'));
181 $this->assertEquals(1, $phone['location_type_id']);
183 $contact = $this->callAPISuccessGetSingle('Contact', $contactValues);
184 $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id']));
188 * Test that the import parser adds the address to the primary location.
192 public function testImportDeceased() {
193 list($contactValues) = $this->setUpBaseContact();
194 CRM_Core_Session
::singleton()->set("dateTypes", 1);
195 $contactValues['birth_date'] = '1910-12-17';
196 $contactValues['deceased_date'] = '2010-12-17';
197 $this->runImport($contactValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
198 $contact = $this->callAPISuccessGetSingle('Contact', $contactValues);
199 $this->assertEquals('1910-12-17', $contact['birth_date']);
200 $this->assertEquals('2010-12-17', $contact['deceased_date']);
201 $this->assertEquals(1, $contact['is_deceased']);
202 $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id']));
207 * Test that the import parser adds the address to the primary location.
211 public function testImportTwoAddressFirstPrimary() {
212 list($contactValues) = $this->setUpBaseContact();
213 $contactValues['nick_name'] = 'Old Bill';
214 $contactValues['external_identifier'] = 'android';
215 $contactValues['street_address'] = 'Big Mansion';
216 $contactValues['phone'] = 12334;
217 $fields = array_keys($contactValues);
218 $contactValues['street_address_2'] = 'Teeny Mansion';
219 $contactValues['phone_2'] = 4444;
220 $fields[] = 'street_address';
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 => 'Primary', 6 => 'Primary', 7 => 3, 8 => 3), $fields);
223 $contact = $this->callAPISuccessGetSingle('Contact', array('external_identifier' => 'android'));
224 $address = $this->callAPISuccess('Address', 'get', array('contact_id' => $contact['id'], 'sequential' => 1));
226 $this->assertEquals(3, $address['values'][0]['location_type_id']);
227 $this->assertEquals(0, $address['values'][0]['is_primary']);
228 $this->assertEquals('Teeny Mansion', $address['values'][0]['street_address']);
230 $this->assertEquals(1, $address['values'][1]['location_type_id']);
231 $this->assertEquals(1, $address['values'][1]['is_primary']);
232 $this->assertEquals('Big Mansion', $address['values'][1]['street_address']);
234 $phone = $this->callAPISuccess('Phone', 'get', array('contact_id' => $contact['id'], 'sequential' => 1));
235 $this->assertEquals(1, $phone['values'][0]['location_type_id']);
236 $this->assertEquals(1, $phone['values'][0]['is_primary']);
237 $this->assertEquals(12334, $phone['values'][0]['phone']);
238 $this->assertEquals(3, $phone['values'][1]['location_type_id']);
239 $this->assertEquals(0, $phone['values'][1]['is_primary']);
240 $this->assertEquals(4444, $phone['values'][1]['phone']);
242 $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id']));
246 * Test that the import parser adds the address to the primary location.
250 public function testImportTwoAddressSecondPrimary() {
251 list($contactValues) = $this->setUpBaseContact();
252 $contactValues['nick_name'] = 'Old Bill';
253 $contactValues['external_identifier'] = 'android';
254 $contactValues['street_address'] = 'Big Mansion';
255 $contactValues['phone'] = 12334;
256 $fields = array_keys($contactValues);
257 $contactValues['street_address_2'] = 'Teeny Mansion';
258 $contactValues['phone_2'] = 4444;
259 $fields[] = 'street_address';
261 $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);
262 $contact = $this->callAPISuccessGetSingle('Contact', array('external_identifier' => 'android'));
263 $address = $this->callAPISuccess('Address', 'get', array('contact_id' => $contact['id'], 'sequential' => 1));
265 $this->assertEquals(1, $address['values'][0]['location_type_id']);
266 $this->assertEquals(1, $address['values'][0]['is_primary']);
267 $this->assertEquals('Teeny Mansion', $address['values'][0]['street_address']);
269 $this->assertEquals(3, $address['values'][1]['location_type_id']);
270 $this->assertEquals(0, $address['values'][1]['is_primary']);
271 $this->assertEquals('Big Mansion', $address['values'][1]['street_address']);
273 $phone = $this->callAPISuccess('Phone', 'get', array('contact_id' => $contact['id'], 'sequential' => 1));
274 $this->assertEquals(3, $phone['values'][0]['location_type_id']);
275 $this->assertEquals(0, $phone['values'][0]['is_primary']);
276 $this->assertEquals(12334, $phone['values'][0]['phone']);
277 $this->assertEquals(1, $phone['values'][1]['location_type_id']);
278 $this->assertEquals(1, $phone['values'][1]['is_primary']);
279 $this->assertEquals(4444, $phone['values'][1]['phone']);
281 $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id']));
285 * Test that the import parser updates the address on the existing primary location.
289 public function testImportPrimaryAddressUpdate() {
290 list($contactValues) = $this->setUpBaseContact(array('external_identifier' => 'android'));
291 $contactValues['nick_name'] = 'Old Bill';
292 $contactValues['external_identifier'] = 'android';
293 $contactValues['street_address'] = 'Big Mansion';
294 $contactValues['city'] = 'Big City';
295 $contactID = $this->callAPISuccessGetValue('Contact', array('external_identifier' => 'android', 'return' => 'id'));
296 $originalAddress = $this->callAPISuccess('Address', 'create', array('location_type_id' => 2, 'street_address' => 'small house', 'contact_id' => $contactID));
297 $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'));
298 $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion'));
299 $this->assertEquals(2, $address['location_type_id']);
300 $this->assertEquals($originalAddress['id'], $address['id']);
301 $this->assertEquals('Big City', $address['city']);
302 $this->callAPISuccessGetSingle('Contact', $contactValues);
306 * Test that setting duplicate action to fill doesn't blow away data
307 * that exists, but does fill in where it's empty.
311 public function testImportFill() {
312 // Create a custom field group for testing.
313 $custom_group_name = 'importFillGroup';
314 $results = $this->callAPISuccess('customGroup', 'get', array('title' => $custom_group_name));
315 if ($results['count'] == 0) {
317 'title' => $custom_group_name,
318 'extends' => 'Individual',
321 $customGroup = $this->callAPISuccess('customGroup', 'create', $api_params);
324 // Add two custom fields.
326 'custom_group_id' => $customGroup['id'],
327 'label' => 'importFillField1',
328 'html_type' => 'Select',
329 'data_type' => 'String',
330 'option_values' => array(
335 $result = $this->callAPISuccess('custom_field', 'create', $api_params);
336 $customField1 = $result['id'];
339 'custom_group_id' => $customGroup['id'],
340 'label' => 'importFillField2',
341 'html_type' => 'Select',
342 'data_type' => 'String',
343 'option_values' => array(
348 $result = $this->callAPISuccess('custom_field', 'create', $api_params);
349 $customField2 = $result['id'];
351 // Now set up values.
352 $original_gender = 'Male';
353 $original_custom1 = 'foo';
354 $original_job_title = '';
355 $original_custom2 = '';
356 $original_email = 'test-import-fill@example.org';
358 $import_gender = 'Female';
359 $import_custom1 = 'bar';
360 $import_job_title = 'Chief data importer';
361 $import_custom2 = 'baz';
363 // Create contact with both one known core field and one custom
366 'contact_type' => 'Individual',
367 'email' => $original_email,
368 'gender' => $original_gender,
369 'custom_' . $customField1 => $original_custom1,
371 $result = $this->callAPISuccess('contact', 'create', $api_params);
372 $contact_id = $result['id'];
376 'email' => $original_email,
377 'gender_id' => $import_gender,
378 'custom_' . $customField1 => $import_custom1,
379 'job_title' => $import_job_title,
380 'custom_' . $customField2 => $import_custom2,
383 $this->runImport($import, CRM_Import_Parser
::DUPLICATE_FILL
, CRM_Import_Parser
::VALID
);
386 'gender' => $original_gender,
387 'custom_' . $customField1 => $original_custom1,
388 'job_title' => $import_job_title,
389 'custom_' . $customField2 => $import_custom2,
396 'custom_' . $customField1,
398 'custom_' . $customField2,
401 $result = civicrm_api3('Contact', 'get', $params);
402 $values = array_pop($result['values']);
403 foreach ($expected as $field => $expected_value) {
404 if (!isset($values[$field])) {
408 $given_value = $values[$field];
412 // job_title: Chief Data Importer
413 // importFillField1: foo
414 // importFillField2: baz
415 $this->assertEquals($expected_value, $given_value, "$field properly handled during Fill import");
420 * Run the import parser.
422 * @param array $originalValues
424 * @param int $onDuplicateAction
425 * @param int $expectedResult
426 * @param array|null $mapperLocType
427 * @param array|null $fields
428 * Array of field names. Will be calculated from $originalValues if not passed in, but
429 * that method does not cope with duplicates.
431 protected function runImport($originalValues, $onDuplicateAction, $expectedResult, $mapperLocType = NULL, $fields = NULL) {
433 $fields = array_keys($originalValues);
435 $values = array_values($originalValues);
436 $parser = new CRM_Contact_Import_Parser_Contact($fields, $mapperLocType);
437 $parser->_contactType
= 'Individual';
438 $parser->_onDuplicate
= $onDuplicateAction;
440 $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values), 'Return code from parser import was not as expected');
444 * Set up the underlying contact.
446 * @param array $params
447 * Optional extra parameters to set.
452 protected function setUpBaseContact($params = array()) {
453 $originalValues = array_merge(array(
454 'first_name' => 'Bill',
455 'last_name' => 'Gates',
456 'email' => 'bill.gates@microsoft.com',
457 'nick_name' => 'Billy-boy',
459 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
460 $result = $this->callAPISuccessGetSingle('Contact', $originalValues);
461 return array($originalValues, $result);