Merge pull request #14441 from colemanw/sortable
[civicrm-core.git] / tests / phpunit / CRM / Dedupe / MergerTest.php
1 <?php
2
3 /**
4 * Class CRM_Dedupe_DedupeMergerTest
5 *
6 * @group headless
7 */
8 class CRM_Dedupe_MergerTest extends CiviUnitTestCase {
9
10 protected $_groupId;
11
12 protected $_contactIds = [];
13
14 /**
15 * Tear down.
16 *
17 * @throws \Exception
18 */
19 public function tearDown() {
20 $this->quickCleanup([
21 'civicrm_contact',
22 'civicrm_group_contact',
23 'civicrm_group',
24 ]);
25 parent::tearDown();
26 }
27
28 public function createDupeContacts() {
29 // create a group to hold contacts, so that dupe checks don't consider any other contacts in the DB
30 $params = [
31 'name' => 'Test Dupe Merger Group',
32 'title' => 'Test Dupe Merger Group',
33 'domain_id' => 1,
34 'is_active' => 1,
35 'visibility' => 'Public Pages',
36 ];
37
38 $result = $this->callAPISuccess('group', 'create', $params);
39 $this->_groupId = $result['id'];
40
41 // contact data set
42
43 // make dupe checks based on based on following contact sets:
44 // FIRST - LAST - EMAIL
45 // ---------------------------------
46 // robin - hood - robin@example.com
47 // robin - hood - robin@example.com
48 // robin - hood - hood@example.com
49 // robin - dale - robin@example.com
50 // little - dale - dale@example.com
51 // little - dale - dale@example.com
52 // will - dale - dale@example.com
53 // will - dale - will@example.com
54 // will - dale - will@example.com
55 $params = [
56 [
57 'first_name' => 'robin',
58 'last_name' => 'hood',
59 'email' => 'robin@example.com',
60 'contact_type' => 'Individual',
61 ],
62 [
63 'first_name' => 'robin',
64 'last_name' => 'hood',
65 'email' => 'robin@example.com',
66 'contact_type' => 'Individual',
67 ],
68 [
69 'first_name' => 'robin',
70 'last_name' => 'hood',
71 'email' => 'hood@example.com',
72 'contact_type' => 'Individual',
73 ],
74 [
75 'first_name' => 'robin',
76 'last_name' => 'dale',
77 'email' => 'robin@example.com',
78 'contact_type' => 'Individual',
79 ],
80 [
81 'first_name' => 'little',
82 'last_name' => 'dale',
83 'email' => 'dale@example.com',
84 'contact_type' => 'Individual',
85 ],
86 [
87 'first_name' => 'little',
88 'last_name' => 'dale',
89 'email' => 'dale@example.com',
90 'contact_type' => 'Individual',
91 ],
92 [
93 'first_name' => 'will',
94 'last_name' => 'dale',
95 'email' => 'dale@example.com',
96 'contact_type' => 'Individual',
97 ],
98 [
99 'first_name' => 'will',
100 'last_name' => 'dale',
101 'email' => 'will@example.com',
102 'contact_type' => 'Individual',
103 ],
104 [
105 'first_name' => 'will',
106 'last_name' => 'dale',
107 'email' => 'will@example.com',
108 'contact_type' => 'Individual',
109 ],
110 ];
111
112 $count = 1;
113 foreach ($params as $param) {
114 $param['version'] = 3;
115 $contact = civicrm_api('contact', 'create', $param);
116 $this->_contactIds[$count++] = $contact['id'];
117
118 $grpParams = [
119 'contact_id' => $contact['id'],
120 'group_id' => $this->_groupId,
121 'version' => 3,
122 ];
123 $this->callAPISuccess('group_contact', 'create', $grpParams);
124 }
125 }
126
127 /**
128 * Delete all created contacts.
129 */
130 public function deleteDupeContacts() {
131 foreach ($this->_contactIds as $contactId) {
132 $this->contactDelete($contactId);
133 }
134 $this->groupDelete($this->_groupId);
135 }
136
137 /**
138 * Test the batch merge.
139 */
140 public function testBatchMergeSelectedDuplicates() {
141 $this->createDupeContacts();
142
143 // verify that all contacts have been created separately
144 $this->assertEquals(count($this->_contactIds), 9, 'Check for number of contacts.');
145
146 $dao = new CRM_Dedupe_DAO_RuleGroup();
147 $dao->contact_type = 'Individual';
148 $dao->name = 'IndividualSupervised';
149 $dao->is_default = 1;
150 $dao->find(TRUE);
151
152 $foundDupes = CRM_Dedupe_Finder::dupesInGroup($dao->id, $this->_groupId);
153
154 // -------------------------------------------------------------------------
155 // Name and Email (reserved) Matches ( 3 pairs )
156 // --------------------------------------------------------------------------
157 // robin - hood - robin@example.com
158 // robin - hood - robin@example.com
159 // little - dale - dale@example.com
160 // little - dale - dale@example.com
161 // will - dale - will@example.com
162 // will - dale - will@example.com
163 // so 3 pairs for - first + last + mail
164 $this->assertEquals(count($foundDupes), 3, 'Check Individual-Supervised dupe rule for dupesInGroup().');
165
166 // Run dedupe finder as the browser would
167 //avoid invalid key error
168 $_SERVER['REQUEST_METHOD'] = 'GET';
169 $object = new CRM_Contact_Page_DedupeFind();
170 $object->set('gid', $this->_groupId);
171 $object->set('rgid', $dao->id);
172 $object->set('action', CRM_Core_Action::UPDATE);
173 $object->setEmbedded(TRUE);
174 @$object->run();
175
176 // Retrieve pairs from prev next cache table
177 $select = ['pn.is_selected' => 'is_selected'];
178 $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($dao->id, $this->_groupId);
179 $pnDupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
180 $this->assertEquals(count($foundDupes), count($pnDupePairs), 'Check number of dupe pairs in prev next cache.');
181
182 // mark first two pairs as selected
183 CRM_Core_DAO::singleValueQuery("UPDATE civicrm_prevnext_cache SET is_selected = 1 WHERE id IN ({$pnDupePairs[0]['prevnext_id']}, {$pnDupePairs[1]['prevnext_id']})");
184
185 $pnDupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
186 $this->assertEquals($pnDupePairs[0]['is_selected'], 1, 'Check if first record in dupe pairs is marked as selected.');
187 $this->assertEquals($pnDupePairs[0]['is_selected'], 1, 'Check if second record in dupe pairs is marked as selected.');
188
189 // batch merge selected dupes
190 $result = CRM_Dedupe_Merger::batchMerge($dao->id, $this->_groupId, 'safe', 5, 1);
191 $this->assertEquals(count($result['merged']), 2, 'Check number of merged pairs.');
192
193 $stats = $this->callAPISuccess('Dedupe', 'getstatistics', [
194 'group_id' => $this->_groupId,
195 'rule_group_id' => $dao->id,
196 'check_permissions' => TRUE,
197 ])['values'];
198 $this->assertEquals(['merged' => 2, 'skipped' => 0], $stats);
199
200 // retrieve pairs from prev next cache table
201 $pnDupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
202 $this->assertEquals(count($pnDupePairs), 1, 'Check number of remaining dupe pairs in prev next cache.');
203
204 $this->deleteDupeContacts();
205 }
206
207 /**
208 * Test the batch merge.
209 */
210 public function testBatchMergeAllDuplicates() {
211 $this->createDupeContacts();
212
213 // verify that all contacts have been created separately
214 $this->assertEquals(count($this->_contactIds), 9, 'Check for number of contacts.');
215
216 $dao = new CRM_Dedupe_DAO_RuleGroup();
217 $dao->contact_type = 'Individual';
218 $dao->name = 'IndividualSupervised';
219 $dao->is_default = 1;
220 $dao->find(TRUE);
221
222 $foundDupes = CRM_Dedupe_Finder::dupesInGroup($dao->id, $this->_groupId);
223
224 // -------------------------------------------------------------------------
225 // Name and Email (reserved) Matches ( 3 pairs )
226 // --------------------------------------------------------------------------
227 // robin - hood - robin@example.com
228 // robin - hood - robin@example.com
229 // little - dale - dale@example.com
230 // little - dale - dale@example.com
231 // will - dale - will@example.com
232 // will - dale - will@example.com
233 // so 3 pairs for - first + last + mail
234 $this->assertEquals(count($foundDupes), 3, 'Check Individual-Supervised dupe rule for dupesInGroup().');
235
236 // Run dedupe finder as the browser would
237 //avoid invalid key error
238 $_SERVER['REQUEST_METHOD'] = 'GET';
239 $object = new CRM_Contact_Page_DedupeFind();
240 $object->set('gid', $this->_groupId);
241 $object->set('rgid', $dao->id);
242 $object->set('action', CRM_Core_Action::UPDATE);
243 $object->setEmbedded(TRUE);
244 @$object->run();
245
246 // Retrieve pairs from prev next cache table
247 $select = ['pn.is_selected' => 'is_selected'];
248 $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($dao->id, $this->_groupId);
249 $pnDupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
250
251 $this->assertEquals(count($foundDupes), count($pnDupePairs), 'Check number of dupe pairs in prev next cache.');
252
253 // batch merge all dupes
254 $result = CRM_Dedupe_Merger::batchMerge($dao->id, $this->_groupId, 'safe', 5, 2);
255 $this->assertEquals(count($result['merged']), 3, 'Check number of merged pairs.');
256
257 $stats = $this->callAPISuccess('Dedupe', 'getstatistics', [
258 'rule_group_id' => $dao->id,
259 'group_id' => $this->_groupId,
260 ]);
261 // retrieve pairs from prev next cache table
262 $pnDupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
263 $this->assertEquals(count($pnDupePairs), 0, 'Check number of remaining dupe pairs in prev next cache.');
264
265 $this->deleteDupeContacts();
266 }
267
268 /**
269 * The goal of this function is to test that all required tables are returned.
270 */
271 public function testGetCidRefs() {
272 $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'Contacts');
273 $this->assertEquals(array_merge($this->getStaticCIDRefs(), $this->getHackedInCIDRef()), CRM_Dedupe_Merger::cidRefs());
274 $this->assertEquals(array_merge($this->getCalculatedCIDRefs(), $this->getHackedInCIDRef()), CRM_Dedupe_Merger::cidRefs());
275 }
276
277 /**
278 * Get the list of not-really-cid-refs that are currently hacked in.
279 *
280 * This is hacked into getCIDs function.
281 *
282 * @return array
283 */
284 public function getHackedInCIDRef() {
285 return [
286 'civicrm_entity_tag' => [
287 0 => 'entity_id',
288 ],
289 ];
290 }
291
292 /**
293 * Test function that gets duplicate pairs.
294 *
295 * It turns out there are 2 code paths retrieving this data so my initial
296 * focus is on ensuring they match.
297 */
298 public function testGetMatches() {
299 $this->setupMatchData();
300
301 $pairs = $this->callAPISuccess('Dedupe', 'getduplicates', [
302 'rule_group_id' => 1,
303 ])['values'];
304 $this->assertEquals([
305 0 => [
306 'srcID' => $this->contacts[1]['id'],
307 'srcName' => 'Mr. Mickey Mouse II',
308 'dstID' => $this->contacts[0]['id'],
309 'dstName' => 'Mr. Mickey Mouse II',
310 'weight' => 20,
311 'canMerge' => TRUE,
312 ],
313 1 => [
314 'srcID' => $this->contacts[3]['id'],
315 'srcName' => 'Mr. Minnie Mouse II',
316 'dstID' => $this->contacts[2]['id'],
317 'dstName' => 'Mr. Minnie Mouse II',
318 'weight' => 20,
319 'canMerge' => TRUE,
320 ],
321 ], $pairs);
322 }
323
324 /**
325 * Test function that gets organization pairs.
326 *
327 * Note the rule will match on organization_name OR email - hence lots of
328 * matches.
329 *
330 * @throws \Exception
331 */
332 public function testGetOrganizationMatches() {
333 $this->setupMatchData();
334 $ruleGroups = $this->callAPISuccessGetSingle('RuleGroup', [
335 'contact_type' => 'Organization',
336 'used' => 'Supervised',
337 ]);
338
339 $pairs = CRM_Dedupe_Merger::getDuplicatePairs(
340 $ruleGroups['id'],
341 NULL,
342 TRUE,
343 25,
344 FALSE
345 );
346
347 $expectedPairs = [
348 0 => [
349 'srcID' => $this->contacts[5]['id'],
350 'srcName' => 'Walt Disney Ltd',
351 'dstID' => $this->contacts[4]['id'],
352 'dstName' => 'Walt Disney Ltd',
353 'weight' => 20,
354 'canMerge' => TRUE,
355 ],
356 1 => [
357 'srcID' => $this->contacts[7]['id'],
358 'srcName' => 'Walt Disney',
359 'dstID' => $this->contacts[6]['id'],
360 'dstName' => 'Walt Disney',
361 'weight' => 10,
362 'canMerge' => TRUE,
363 ],
364 2 => [
365 'srcID' => $this->contacts[6]['id'],
366 'srcName' => 'Walt Disney',
367 'dstID' => $this->contacts[4]['id'],
368 'dstName' => 'Walt Disney Ltd',
369 'weight' => 10,
370 'canMerge' => TRUE,
371 ],
372 3 => [
373 'srcID' => $this->contacts[6]['id'],
374 'srcName' => 'Walt Disney',
375 'dstID' => $this->contacts[5]['id'],
376 'dstName' => 'Walt Disney Ltd',
377 'weight' => 10,
378 'canMerge' => TRUE,
379 ],
380 ];
381 usort($pairs, [__CLASS__, 'compareDupes']);
382 usort($expectedPairs, [__CLASS__, 'compareDupes']);
383 $this->assertEquals($expectedPairs, $pairs);
384 }
385
386 /**
387 * Function to sort $duplicate records in a stable way.
388 *
389 * @param array $a
390 * @param array $b
391 *
392 * @return int
393 */
394 public static function compareDupes($a, $b) {
395 foreach (['srcName', 'dstName', 'srcID', 'dstID'] as $field) {
396 if ($a[$field] != $b[$field]) {
397 return ($a[$field] < $b[$field]) ? 1 : -1;
398 }
399 }
400 return 0;
401 }
402
403 /**
404 * Test function that gets organization duplicate pairs.
405 *
406 * @throws \Exception
407 */
408 public function testGetOrganizationMatchesInGroup() {
409 $this->setupMatchData();
410 $ruleGroups = $this->callAPISuccessGetSingle('RuleGroup', [
411 'contact_type' => 'Organization',
412 'used' => 'Supervised',
413 ]);
414
415 $groupID = $this->groupCreate(['title' => 'she-mice']);
416
417 $this->callAPISuccess('GroupContact', 'create', [
418 'group_id' => $groupID,
419 'contact_id' => $this->contacts[4]['id'],
420 ]);
421
422 $pairs = CRM_Dedupe_Merger::getDuplicatePairs(
423 $ruleGroups['id'],
424 $groupID,
425 TRUE,
426 25,
427 FALSE
428 );
429
430 $this->assertEquals([
431 0 => [
432 'srcID' => $this->contacts[5]['id'],
433 'srcName' => 'Walt Disney Ltd',
434 'dstID' => $this->contacts[4]['id'],
435 'dstName' => 'Walt Disney Ltd',
436 'weight' => 20,
437 'canMerge' => TRUE,
438 ],
439 1 => [
440 'srcID' => $this->contacts[6]['id'],
441 'srcName' => 'Walt Disney',
442 'dstID' => $this->contacts[4]['id'],
443 'dstName' => 'Walt Disney Ltd',
444 'weight' => 10,
445 'canMerge' => TRUE,
446 ],
447 ], $pairs);
448
449 $this->callAPISuccess('GroupContact', 'create', [
450 'group_id' => $groupID,
451 'contact_id' => $this->contacts[5]['id'],
452 ]);
453 CRM_Core_DAO::executeQuery("DELETE FROM civicrm_prevnext_cache");
454 $pairs = CRM_Dedupe_Merger::getDuplicatePairs(
455 $ruleGroups['id'],
456 $groupID,
457 TRUE,
458 25,
459 FALSE
460 );
461
462 $this->assertEquals([
463 0 => [
464 'srcID' => $this->contacts[5]['id'],
465 'srcName' => 'Walt Disney Ltd',
466 'dstID' => $this->contacts[4]['id'],
467 'dstName' => 'Walt Disney Ltd',
468 'weight' => 20,
469 'canMerge' => TRUE,
470 ],
471 1 => [
472 'srcID' => $this->contacts[6]['id'],
473 'srcName' => 'Walt Disney',
474 'dstID' => $this->contacts[4]['id'],
475 'dstName' => 'Walt Disney Ltd',
476 'weight' => 10,
477 'canMerge' => TRUE,
478 ],
479 2 => [
480 'srcID' => $this->contacts[6]['id'],
481 'srcName' => 'Walt Disney',
482 'dstID' => $this->contacts[5]['id'],
483 'dstName' => 'Walt Disney Ltd',
484 'weight' => 10,
485 'canMerge' => TRUE,
486 ],
487 ], $pairs);
488 }
489
490 /**
491 * Test function that gets duplicate pairs.
492 *
493 * It turns out there are 2 code paths retrieving this data so my initial
494 * focus is on ensuring they match.
495 */
496 public function testGetMatchesInGroup() {
497 $this->setupMatchData();
498
499 $groupID = $this->groupCreate(['title' => 'she-mice']);
500
501 $this->callAPISuccess('GroupContact', 'create', [
502 'group_id' => $groupID,
503 'contact_id' => $this->contacts[3]['id'],
504 ]);
505
506 $pairs = CRM_Dedupe_Merger::getDuplicatePairs(
507 1,
508 $groupID,
509 TRUE,
510 25,
511 FALSE
512 );
513
514 $this->assertEquals([
515 0 => [
516 'srcID' => $this->contacts[3]['id'],
517 'srcName' => 'Mr. Minnie Mouse II',
518 'dstID' => $this->contacts[2]['id'],
519 'dstName' => 'Mr. Minnie Mouse II',
520 'weight' => 20,
521 'canMerge' => TRUE,
522 ],
523 ], $pairs);
524 }
525
526 /**
527 * Test the special info handling is unchanged after cleanup.
528 *
529 * Note the handling is silly - we are testing to lock in over short term
530 * changes not to imply any contract on the function.
531 */
532 public function testGetRowsElementsAndInfoSpecialInfo() {
533 $contact1 = $this->individualCreate([
534 'preferred_communication_method' => [],
535 'communication_style_id' => 'Familiar',
536 'prefix_id' => 'Mrs.',
537 'suffix_id' => 'III',
538 ]);
539 $contact2 = $this->individualCreate([
540 'preferred_communication_method' => [
541 'SMS',
542 'Fax',
543 ],
544 'communication_style_id' => 'Formal',
545 'gender_id' => 'Female',
546 ]);
547 $rowsElementsAndInfo = CRM_Dedupe_Merger::getRowsElementsAndInfo($contact1, $contact2);
548 $rows = $rowsElementsAndInfo['rows'];
549 $this->assertEquals([
550 'main' => 'Mrs.',
551 'other' => 'Mr.',
552 'title' => 'Individual Prefix',
553 ], $rows['move_prefix_id']);
554 $this->assertEquals([
555 'main' => 'III',
556 'other' => 'II',
557 'title' => 'Individual Suffix',
558 ], $rows['move_suffix_id']);
559 $this->assertEquals([
560 'main' => '',
561 'other' => 'Female',
562 'title' => 'Gender',
563 ], $rows['move_gender_id']);
564 $this->assertEquals([
565 'main' => 'Familiar',
566 'other' => 'Formal',
567 'title' => 'Communication Style',
568 ], $rows['move_communication_style_id']);
569 $this->assertEquals(1, $rowsElementsAndInfo['migration_info']['move_communication_style_id']);
570 $this->assertEquals([
571 'main' => '',
572 'other' => 'SMS, Fax',
573 'title' => 'Preferred Communication Method',
574 ], $rows['move_preferred_communication_method']);
575 $this->assertEquals('\ 14\ 15\ 1', $rowsElementsAndInfo['migration_info']['move_preferred_communication_method']);
576 }
577
578 /**
579 * Test migration of Membership.
580 */
581 public function testMergeMembership() {
582 // Contacts setup
583 $this->setupMatchData();
584 $originalContactID = $this->contacts[0]['id'];
585 $duplicateContactID = $this->contacts[1]['id'];
586
587 //Add Membership for the duplicate contact.
588 $memTypeId = $this->membershipTypeCreate();
589 $this->callAPISuccess('Membership', 'create', [
590 'membership_type_id' => $memTypeId,
591 'contact_id' => $duplicateContactID,
592 ]);
593 //Assert if 'add new' checkbox is enabled on the merge form.
594 $rowsElementsAndInfo = CRM_Dedupe_Merger::getRowsElementsAndInfo($originalContactID, $duplicateContactID);
595 foreach ($rowsElementsAndInfo['elements'] as $element) {
596 if (!empty($element[3]) && $element[3] == 'add new') {
597 $checkedAttr = ['checked' => 'checked'];
598 $this->checkArrayEquals($element[4], $checkedAttr);
599 }
600 }
601
602 //Merge and move the mem to the main contact.
603 $this->mergeContacts($originalContactID, $duplicateContactID, [
604 'move_rel_table_memberships' => 1,
605 'operation' => ['move_rel_table_memberships' => ['add' => 1]],
606 ]);
607
608 //Check if membership is correctly transferred to original contact.
609 $originalContactMembership = $this->callAPISuccess('Membership', 'get', [
610 'membership_type_id' => $memTypeId,
611 'contact_id' => $originalContactID,
612 ]);
613 $this->assertEquals(1, $originalContactMembership['count']);
614 }
615
616 /**
617 * CRM-19653 : Test that custom field data should/shouldn't be overriden on
618 * selecting/not selecting option to migrate data respectively
619 */
620 public function testCustomDataOverwrite() {
621 // Create Custom Field
622 $createGroup = $this->setupCustomGroupForIndividual();
623 $createField = $this->setupCustomField('Graduation', $createGroup);
624 $customFieldName = "custom_" . $createField['id'];
625
626 // Contacts setup
627 $this->setupMatchData();
628
629 $originalContactID = $this->contacts[0]['id'];
630 // used as duplicate contact in 1st use-case
631 $duplicateContactID1 = $this->contacts[1]['id'];
632 // used as duplicate contact in 2nd use-case
633 $duplicateContactID2 = $this->contacts[2]['id'];
634
635 // update the text custom field for original contact with value 'abc'
636 $this->callAPISuccess('Contact', 'create', [
637 'id' => $originalContactID,
638 "{$customFieldName}" => 'abc',
639 ]);
640 $this->assertCustomFieldValue($originalContactID, 'abc', $customFieldName);
641
642 // update the text custom field for duplicate contact 1 with value 'def'
643 $this->callAPISuccess('Contact', 'create', [
644 'id' => $duplicateContactID1,
645 "{$customFieldName}" => 'def',
646 ]);
647 $this->assertCustomFieldValue($duplicateContactID1, 'def', $customFieldName);
648
649 // update the text custom field for duplicate contact 2 with value 'ghi'
650 $this->callAPISuccess('Contact', 'create', [
651 'id' => $duplicateContactID2,
652 "{$customFieldName}" => 'ghi',
653 ]);
654 $this->assertCustomFieldValue($duplicateContactID2, 'ghi', $customFieldName);
655
656 /*** USE-CASE 1: DO NOT OVERWRITE CUSTOM FIELD VALUE **/
657 $this->mergeContacts($originalContactID, $duplicateContactID1, [
658 "move_{$customFieldName}" => NULL,
659 ]);
660 $this->assertCustomFieldValue($originalContactID, 'abc', $customFieldName);
661
662 /*** USE-CASE 2: OVERWRITE CUSTOM FIELD VALUE **/
663 $this->mergeContacts($originalContactID, $duplicateContactID2, [
664 "move_{$customFieldName}" => 'ghi',
665 ]);
666 $this->assertCustomFieldValue($originalContactID, 'ghi', $customFieldName);
667
668 // cleanup created custom set
669 $this->callAPISuccess('CustomField', 'delete', ['id' => $createField['id']]);
670 $this->callAPISuccess('CustomGroup', 'delete', ['id' => $createGroup['id']]);
671 }
672
673 /**
674 * Creatd Date merge cases
675 * @return array
676 */
677 public function createdDateMergeCases() {
678 $cases = [];
679 // Normal pattern merge into the lower id
680 $cases[] = [0, 1];
681 // Check if we flipped the contacts that it still does right thing
682 $cases[] = [1, 0];
683 return $cases;
684 }
685
686 /**
687 * dev/core#996 Ensure that the oldest created date is retained even if duplicates have been flipped
688 * @dataProvider createdDateMergeCases
689 */
690 public function testCreatedDatePostMerge($keepContactKey, $duplicateContactKey) {
691 $this->setupMatchData();
692 $lowerContactCreatedDate = $this->callAPISuccess('Contact', 'getsingle', [
693 'id' => $this->contacts[0]['id'],
694 'return' => ['created_date'],
695 ])['created_date'];
696 // Assume contats have been flipped in the UL so merging into the higher id
697 $this->mergeContacts($this->contacts[$keepContactKey]['id'], $this->contacts[$duplicateContactKey]['id'], []);
698 $this->assertEquals($lowerContactCreatedDate, $this->callAPISuccess('Contact', 'getsingle', ['id' => $this->contacts[$keepContactKey]['id'], 'return' => ['created_date']])['created_date']);
699 }
700
701 /**
702 * Verifies that when a contact with a custom field value is merged into a
703 * contact without a record int its corresponding custom group table, and none
704 * of the custom fields of that custom table are selected, the value is not
705 * merged in.
706 */
707 public function testMigrationOfUnselectedCustomDataOnEmptyCustomRecord() {
708 // Create Custom Fields
709 $createGroup = $this->setupCustomGroupForIndividual();
710 $customField1 = $this->setupCustomField('TestField', $createGroup);
711
712 // Create multi-value custom field
713 $multiGroup = $this->CustomGroupMultipleCreateByParams();
714 $multiField = $this->customFieldCreate([
715 'custom_group_id' => $multiGroup['id'],
716 'label' => 'field_1' . $multiGroup['id'],
717 'in_selector' => 1,
718 ]);
719
720 // Contacts setup
721 $this->setupMatchData();
722 $originalContactID = $this->contacts[0]['id'];
723 $duplicateContactID = $this->contacts[1]['id'];
724
725 // Update the text custom fields for duplicate contact
726 $this->callAPISuccess('Contact', 'create', [
727 'id' => $duplicateContactID,
728 "custom_{$customField1['id']}" => 'abc',
729 "custom_{$multiField['id']}" => 'def',
730 ]);
731 $this->assertCustomFieldValue($duplicateContactID, 'abc', "custom_{$customField1['id']}");
732 $this->assertCustomFieldValue($duplicateContactID, 'def', "custom_{$multiField['id']}");
733
734 // Merge, and ensure that no value was migrated
735 $this->mergeContacts($originalContactID, $duplicateContactID, [
736 "move_custom_{$customField1['id']}" => NULL,
737 "move_rel_table_custom_{$multiGroup['id']}" => NULL,
738 ]);
739 $this->assertCustomFieldValue($originalContactID, '', "custom_{$customField1['id']}");
740 $this->assertCustomFieldValue($originalContactID, '', "custom_{$multiField['id']}");
741
742 // cleanup created custom set
743 $this->callAPISuccess('CustomField', 'delete', ['id' => $customField1['id']]);
744 $this->callAPISuccess('CustomGroup', 'delete', ['id' => $createGroup['id']]);
745 $this->callAPISuccess('CustomField', 'delete', ['id' => $multiField['id']]);
746 $this->callAPISuccess('CustomGroup', 'delete', ['id' => $multiGroup['id']]);
747 }
748
749 /**
750 * Tests that if only part of the custom fields of a custom group are selected
751 * for a merge, only those values are merged, while all other fields of the
752 * custom group retain their original value, specifically for a contact with
753 * no records on the custom group table.
754 */
755 public function testMigrationOfSomeCustomDataOnEmptyCustomRecord() {
756 // Create Custom Fields
757 $createGroup = $this->setupCustomGroupForIndividual();
758 $customField1 = $this->setupCustomField('Test1', $createGroup);
759 $customField2 = $this->setupCustomField('Test2', $createGroup);
760
761 // Create multi-value custom field
762 $multiGroup = $this->CustomGroupMultipleCreateByParams();
763 $multiField = $this->customFieldCreate([
764 'custom_group_id' => $multiGroup['id'],
765 'label' => 'field_1' . $multiGroup['id'],
766 'in_selector' => 1,
767 ]);
768
769 // Contacts setup
770 $this->setupMatchData();
771 $originalContactID = $this->contacts[0]['id'];
772 $duplicateContactID = $this->contacts[1]['id'];
773
774 // Update the text custom fields for duplicate contact
775 $this->callAPISuccess('Contact', 'create', [
776 'id' => $duplicateContactID,
777 "custom_{$customField1['id']}" => 'abc',
778 "custom_{$customField2['id']}" => 'def',
779 "custom_{$multiField['id']}" => 'ghi',
780 ]);
781 $this->assertCustomFieldValue($duplicateContactID, 'abc', "custom_{$customField1['id']}");
782 $this->assertCustomFieldValue($duplicateContactID, 'def', "custom_{$customField2['id']}");
783 $this->assertCustomFieldValue($duplicateContactID, 'ghi', "custom_{$multiField['id']}");
784
785 // Perform merge
786 $this->mergeContacts($originalContactID, $duplicateContactID, [
787 "move_custom_{$customField1['id']}" => NULL,
788 "move_custom_{$customField2['id']}" => 'def',
789 "move_rel_table_custom_{$multiGroup['id']}" => '1',
790 ]);
791 $this->assertCustomFieldValue($originalContactID, '', "custom_{$customField1['id']}");
792 $this->assertCustomFieldValue($originalContactID, 'def', "custom_{$customField2['id']}");
793 $this->assertCustomFieldValue($originalContactID, 'ghi', "custom_{$multiField['id']}");
794
795 // cleanup created custom set
796 $this->callAPISuccess('CustomField', 'delete', ['id' => $customField1['id']]);
797 $this->callAPISuccess('CustomField', 'delete', ['id' => $customField2['id']]);
798 $this->callAPISuccess('CustomGroup', 'delete', ['id' => $createGroup['id']]);
799 $this->callAPISuccess('CustomField', 'delete', ['id' => $multiField['id']]);
800 $this->callAPISuccess('CustomGroup', 'delete', ['id' => $multiGroup['id']]);
801 }
802
803 /**
804 * Calls merge method on given contacts, with values given in $params array.
805 *
806 * @param $originalContactID
807 * ID of target contact
808 * @param $duplicateContactID
809 * ID of contact to be merged
810 * @param $params
811 * Array of fields to be merged from source into target contact, of the form
812 * ['move_<fieldName>' => <fieldValue>]
813 *
814 * @throws \CRM_Core_Exception
815 * @throws \CiviCRM_API3_Exception
816 */
817 private function mergeContacts($originalContactID, $duplicateContactID, $params) {
818 $rowsElementsAndInfo = CRM_Dedupe_Merger::getRowsElementsAndInfo($originalContactID, $duplicateContactID);
819
820 $migrationData = [
821 'main_details' => $rowsElementsAndInfo['main_details'],
822 'other_details' => $rowsElementsAndInfo['other_details'],
823 ];
824
825 // Migrate data of duplicate contact
826 CRM_Dedupe_Merger::moveAllBelongings($originalContactID, $duplicateContactID, array_merge($migrationData, $params));
827 }
828
829 /**
830 * Checks if the expected value for the given field corresponds to what is
831 * stored in the database for the given contact ID.
832 *
833 * @param $contactID
834 * @param $expectedValue
835 * @param $customFieldName
836 */
837 private function assertCustomFieldValue($contactID, $expectedValue, $customFieldName) {
838 $data = $this->callAPISuccess('Contact', 'getsingle', [
839 'id' => $contactID,
840 'return' => [$customFieldName],
841 ]);
842
843 $this->assertEquals($expectedValue, $data[$customFieldName], "Custom field value was supposed to be '{$expectedValue}', '{$data[$customFieldName]}' found.");
844 }
845
846 /**
847 * Creates a custom group to run tests on contacts that are individuals.
848 *
849 * @return array
850 * Data for the created custom group record
851 */
852 private function setupCustomGroupForIndividual() {
853 $customGroup = $this->callAPISuccess('custom_group', 'get', [
854 'name' => 'test_group',
855 ]);
856
857 if ($customGroup['count'] > 0) {
858 $this->callAPISuccess('CustomGroup', 'delete', ['id' => $customGroup['id']]);
859 }
860
861 $customGroup = $this->callAPISuccess('custom_group', 'create', [
862 'title' => 'Test_Group',
863 'name' => 'test_group',
864 'extends' => ['Individual'],
865 'style' => 'Inline',
866 'is_multiple' => FALSE,
867 'is_active' => 1,
868 ]);
869
870 return $customGroup;
871 }
872
873 /**
874 * Creates a custom field on the provided custom group with the given field
875 * label.
876 *
877 * @param $fieldLabel
878 * @param $createGroup
879 *
880 * @return array
881 * Data for the created custom field record
882 */
883 private function setupCustomField($fieldLabel, $createGroup) {
884 return $this->callAPISuccess('custom_field', 'create', [
885 'label' => $fieldLabel,
886 'data_type' => 'Alphanumeric',
887 'html_type' => 'Text',
888 'custom_group_id' => $createGroup['id'],
889 ]);
890 }
891
892 /**
893 * Set up some contacts for our matching.
894 */
895 public function setupMatchData() {
896 $fixtures = [
897 [
898 'first_name' => 'Mickey',
899 'last_name' => 'Mouse',
900 'email' => 'mickey@mouse.com',
901 ],
902 [
903 'first_name' => 'Mickey',
904 'last_name' => 'Mouse',
905 'email' => 'mickey@mouse.com',
906 ],
907 [
908 'first_name' => 'Minnie',
909 'last_name' => 'Mouse',
910 'email' => 'mickey@mouse.com',
911 ],
912 [
913 'first_name' => 'Minnie',
914 'last_name' => 'Mouse',
915 'email' => 'mickey@mouse.com',
916 ],
917 ];
918 foreach ($fixtures as $fixture) {
919 $contactID = $this->individualCreate($fixture);
920 $this->contacts[] = array_merge($fixture, ['id' => $contactID]);
921 sleep(5);
922 }
923 $organizationFixtures = [
924 [
925 'organization_name' => 'Walt Disney Ltd',
926 'email' => 'walt@disney.com',
927 ],
928 [
929 'organization_name' => 'Walt Disney Ltd',
930 'email' => 'walt@disney.com',
931 ],
932 [
933 'organization_name' => 'Walt Disney',
934 'email' => 'walt@disney.com',
935 ],
936 [
937 'organization_name' => 'Walt Disney',
938 'email' => 'walter@disney.com',
939 ],
940 ];
941 foreach ($organizationFixtures as $fixture) {
942 $contactID = $this->organizationCreate($fixture);
943 $this->contacts[] = array_merge($fixture, ['id' => $contactID]);
944 }
945 }
946
947 /**
948 * Get the list of tables that refer to the CID.
949 *
950 * This is a statically maintained (in this test list).
951 *
952 * There is also a check against an automated list but having both seems to
953 * add extra stability to me. They do not change often.
954 */
955 public function getStaticCIDRefs() {
956 return [
957 'civicrm_acl_cache' => [
958 0 => 'contact_id',
959 ],
960 'civicrm_acl_contact_cache' => [
961 0 => 'contact_id',
962 ],
963 'civicrm_action_log' => [
964 0 => 'contact_id',
965 ],
966 'civicrm_activity_contact' => [
967 0 => 'contact_id',
968 ],
969 'civicrm_address' => [
970 0 => 'contact_id',
971 ],
972 'civicrm_batch' => [
973 0 => 'created_id',
974 1 => 'modified_id',
975 ],
976 'civicrm_campaign' => [
977 0 => 'created_id',
978 1 => 'last_modified_id',
979 ],
980 'civicrm_case_contact' => [
981 0 => 'contact_id',
982 ],
983 'civicrm_contact' => [
984 0 => 'primary_contact_id',
985 1 => 'employer_id',
986 ],
987 'civicrm_contribution' => [
988 0 => 'contact_id',
989 ],
990 'civicrm_contribution_page' => [
991 0 => 'created_id',
992 ],
993 'civicrm_contribution_recur' => [
994 0 => 'contact_id',
995 ],
996 'civicrm_contribution_soft' => [
997 0 => 'contact_id',
998 ],
999 'civicrm_custom_group' => [
1000 0 => 'created_id',
1001 ],
1002 'civicrm_dashboard_contact' => [
1003 0 => 'contact_id',
1004 ],
1005 'civicrm_dedupe_exception' => [
1006 0 => 'contact_id1',
1007 1 => 'contact_id2',
1008 ],
1009 'civicrm_domain' => [
1010 0 => 'contact_id',
1011 ],
1012 'civicrm_email' => [
1013 0 => 'contact_id',
1014 ],
1015 'civicrm_event' => [
1016 0 => 'created_id',
1017 ],
1018 'civicrm_event_carts' => [
1019 0 => 'user_id',
1020 ],
1021 'civicrm_financial_account' => [
1022 0 => 'contact_id',
1023 ],
1024 'civicrm_financial_item' => [
1025 0 => 'contact_id',
1026 ],
1027 'civicrm_grant' => [
1028 0 => 'contact_id',
1029 ],
1030 'civicrm_group' => [
1031 0 => 'created_id',
1032 1 => 'modified_id',
1033 ],
1034 'civicrm_group_contact' => [
1035 0 => 'contact_id',
1036 ],
1037 'civicrm_group_contact_cache' => [
1038 0 => 'contact_id',
1039 ],
1040 'civicrm_group_organization' => [
1041 0 => 'organization_id',
1042 ],
1043 'civicrm_im' => [
1044 0 => 'contact_id',
1045 ],
1046 'civicrm_log' => [
1047 0 => 'modified_id',
1048 ],
1049 'civicrm_mailing' => [
1050 0 => 'created_id',
1051 1 => 'scheduled_id',
1052 2 => 'approver_id',
1053 ],
1054 'civicrm_file' => [
1055 'created_id',
1056 ],
1057 'civicrm_mailing_abtest' => [
1058 0 => 'created_id',
1059 ],
1060 'civicrm_mailing_event_queue' => [
1061 0 => 'contact_id',
1062 ],
1063 'civicrm_mailing_event_subscribe' => [
1064 0 => 'contact_id',
1065 ],
1066 'civicrm_mailing_recipients' => [
1067 0 => 'contact_id',
1068 ],
1069 'civicrm_membership' => [
1070 0 => 'contact_id',
1071 ],
1072 'civicrm_membership_log' => [
1073 0 => 'modified_id',
1074 ],
1075 'civicrm_membership_type' => [
1076 0 => 'member_of_contact_id',
1077 ],
1078 'civicrm_note' => [
1079 0 => 'contact_id',
1080 ],
1081 'civicrm_openid' => [
1082 0 => 'contact_id',
1083 ],
1084 'civicrm_participant' => [
1085 0 => 'contact_id',
1086 //CRM-16761
1087 1 => 'transferred_to_contact_id',
1088 ],
1089 'civicrm_payment_token' => [
1090 0 => 'contact_id',
1091 1 => 'created_id',
1092 ],
1093 'civicrm_pcp' => [
1094 0 => 'contact_id',
1095 ],
1096 'civicrm_phone' => [
1097 0 => 'contact_id',
1098 ],
1099 'civicrm_pledge' => [
1100 0 => 'contact_id',
1101 ],
1102 'civicrm_print_label' => [
1103 0 => 'created_id',
1104 ],
1105 'civicrm_relationship' => [
1106 0 => 'contact_id_a',
1107 1 => 'contact_id_b',
1108 ],
1109 'civicrm_report_instance' => [
1110 0 => 'created_id',
1111 1 => 'owner_id',
1112 ],
1113 'civicrm_setting' => [
1114 0 => 'contact_id',
1115 1 => 'created_id',
1116 ],
1117 'civicrm_subscription_history' => [
1118 0 => 'contact_id',
1119 ],
1120 'civicrm_survey' => [
1121 0 => 'created_id',
1122 1 => 'last_modified_id',
1123 ],
1124 'civicrm_tag' => [
1125 0 => 'created_id',
1126 ],
1127 'civicrm_uf_group' => [
1128 0 => 'created_id',
1129 ],
1130 'civicrm_uf_match' => [
1131 0 => 'contact_id',
1132 ],
1133 'civicrm_value_testgetcidref_1' => [
1134 0 => 'entity_id',
1135 ],
1136 'civicrm_website' => [
1137 0 => 'contact_id',
1138 ],
1139 ];
1140 }
1141
1142 /**
1143 * Get a list of CIDs that is calculated off the schema.
1144 *
1145 * Note this is an expensive and table locking query. Should be safe in tests
1146 * though.
1147 */
1148 public function getCalculatedCIDRefs() {
1149 $cidRefs = [];
1150 $sql = "
1151 SELECT
1152 table_name,
1153 column_name
1154 FROM information_schema.key_column_usage
1155 WHERE
1156 referenced_table_schema = database() AND
1157 referenced_table_name = 'civicrm_contact' AND
1158 referenced_column_name = 'id';
1159 ";
1160 $dao = CRM_Core_DAO::executeQuery($sql);
1161 while ($dao->fetch()) {
1162 $cidRefs[$dao->table_name][] = $dao->column_name;
1163 }
1164 // Do specific re-ordering changes to make this the same as the ref validated one.
1165 // The above query orders by FK alphabetically.
1166 // There might be cleverer ways to do this but it shouldn't change much.
1167 $cidRefs['civicrm_contact'][0] = 'primary_contact_id';
1168 $cidRefs['civicrm_contact'][1] = 'employer_id';
1169 $cidRefs['civicrm_acl_contact_cache'][0] = 'contact_id';
1170 $cidRefs['civicrm_mailing'][0] = 'created_id';
1171 $cidRefs['civicrm_mailing'][1] = 'scheduled_id';
1172 $cidRefs['civicrm_mailing'][2] = 'approver_id';
1173 return $cidRefs;
1174 }
1175
1176 }