4 * Class CRM_Dedupe_DedupeMergerTest
7 class CRM_Dedupe_MergerTest
extends CiviUnitTestCase
{
10 protected $_contactIds = array();
12 public function tearDown() {
13 $this->quickCleanup(array('civicrm_contact', 'civicrm_group_contact', 'civicrm_group'));
17 public function createDupeContacts() {
18 // create a group to hold contacts, so that dupe checks don't consider any other contacts in the DB
20 'name' => 'Test Dupe Merger Group',
21 'title' => 'Test Dupe Merger Group',
24 'visibility' => 'Public Pages',
27 $result = $this->callAPISuccess('group', 'create', $params);
28 $this->_groupId
= $result['id'];
32 // make dupe checks based on based on following contact sets:
33 // FIRST - LAST - EMAIL
34 // ---------------------------------
35 // robin - hood - robin@example.com
36 // robin - hood - robin@example.com
37 // robin - hood - hood@example.com
38 // robin - dale - robin@example.com
39 // little - dale - dale@example.com
40 // little - dale - dale@example.com
41 // will - dale - dale@example.com
42 // will - dale - will@example.com
43 // will - dale - will@example.com
46 'first_name' => 'robin',
47 'last_name' => 'hood',
48 'email' => 'robin@example.com',
49 'contact_type' => 'Individual',
52 'first_name' => 'robin',
53 'last_name' => 'hood',
54 'email' => 'robin@example.com',
55 'contact_type' => 'Individual',
58 'first_name' => 'robin',
59 'last_name' => 'hood',
60 'email' => 'hood@example.com',
61 'contact_type' => 'Individual',
64 'first_name' => 'robin',
65 'last_name' => 'dale',
66 'email' => 'robin@example.com',
67 'contact_type' => 'Individual',
70 'first_name' => 'little',
71 'last_name' => 'dale',
72 'email' => 'dale@example.com',
73 'contact_type' => 'Individual',
76 'first_name' => 'little',
77 'last_name' => 'dale',
78 'email' => 'dale@example.com',
79 'contact_type' => 'Individual',
82 'first_name' => 'will',
83 'last_name' => 'dale',
84 'email' => 'dale@example.com',
85 'contact_type' => 'Individual',
88 'first_name' => 'will',
89 'last_name' => 'dale',
90 'email' => 'will@example.com',
91 'contact_type' => 'Individual',
94 'first_name' => 'will',
95 'last_name' => 'dale',
96 'email' => 'will@example.com',
97 'contact_type' => 'Individual',
102 foreach ($params as $param) {
103 $param['version'] = 3;
104 $contact = civicrm_api('contact', 'create', $param);
105 $this->_contactIds
[$count++
] = $contact['id'];
108 'contact_id' => $contact['id'],
109 'group_id' => $this->_groupId
,
112 $this->callAPISuccess('group_contact', 'create', $grpParams);
117 * Delete all created contacts.
119 public function deleteDupeContacts() {
120 foreach ($this->_contactIds
as $contactId) {
121 $this->contactDelete($contactId);
123 $this->groupDelete($this->_groupId
);
127 * Test the batch merge.
129 public function testBatchMergeSelectedDuplicates() {
130 $this->createDupeContacts();
132 // verify that all contacts have been created separately
133 $this->assertEquals(count($this->_contactIds
), 9, 'Check for number of contacts.');
135 $dao = new CRM_Dedupe_DAO_RuleGroup();
136 $dao->contact_type
= 'Individual';
137 $dao->name
= 'IndividualSupervised';
138 $dao->is_default
= 1;
141 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($dao->id
, $this->_groupId
);
143 // -------------------------------------------------------------------------
144 // Name and Email (reserved) Matches ( 3 pairs )
145 // --------------------------------------------------------------------------
146 // robin - hood - robin@example.com
147 // robin - hood - robin@example.com
148 // little - dale - dale@example.com
149 // little - dale - dale@example.com
150 // will - dale - will@example.com
151 // will - dale - will@example.com
152 // so 3 pairs for - first + last + mail
153 $this->assertEquals(count($foundDupes), 3, 'Check Individual-Supervised dupe rule for dupesInGroup().');
155 // Run dedupe finder as the browser would
156 $_SERVER['REQUEST_METHOD'] = 'GET'; //avoid invalid key error
157 $object = new CRM_Contact_Page_DedupeFind();
158 $object->set('gid', $this->_groupId
);
159 $object->set('rgid', $dao->id
);
160 $object->set('action', CRM_Core_Action
::UPDATE
);
161 $object->setEmbedded(TRUE);
164 // Retrieve pairs from prev next cache table
165 $select = array('pn.is_selected' => 'is_selected');
166 $cacheKeyString = CRM_Dedupe_Merger
::getMergeCacheKeyString($dao->id
, $this->_groupId
);
167 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
169 $this->assertEquals(count($foundDupes), count($pnDupePairs), 'Check number of dupe pairs in prev next cache.');
171 // mark first two pairs as selected
172 CRM_Core_DAO
::singleValueQuery("UPDATE civicrm_prevnext_cache SET is_selected = 1 WHERE id IN ({$pnDupePairs[0]['prevnext_id']}, {$pnDupePairs[1]['prevnext_id']})");
174 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
175 $this->assertEquals($pnDupePairs[0]['is_selected'], 1, 'Check if first record in dupe pairs is marked as selected.');
176 $this->assertEquals($pnDupePairs[0]['is_selected'], 1, 'Check if second record in dupe pairs is marked as selected.');
178 // batch merge selected dupes
179 $result = CRM_Dedupe_Merger
::batchMerge($dao->id
, $this->_groupId
, 'safe', 5, 1);
180 $this->assertEquals(count($result['merged']), 2, 'Check number of merged pairs.');
182 // retrieve pairs from prev next cache table
183 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
184 $this->assertEquals(count($pnDupePairs), 1, 'Check number of remaining dupe pairs in prev next cache.');
186 $this->deleteDupeContacts();
190 * Test the batch merge.
192 public function testBatchMergeAllDuplicates() {
193 $this->createDupeContacts();
195 // verify that all contacts have been created separately
196 $this->assertEquals(count($this->_contactIds
), 9, 'Check for number of contacts.');
198 $dao = new CRM_Dedupe_DAO_RuleGroup();
199 $dao->contact_type
= 'Individual';
200 $dao->name
= 'IndividualSupervised';
201 $dao->is_default
= 1;
204 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($dao->id
, $this->_groupId
);
206 // -------------------------------------------------------------------------
207 // Name and Email (reserved) Matches ( 3 pairs )
208 // --------------------------------------------------------------------------
209 // robin - hood - robin@example.com
210 // robin - hood - robin@example.com
211 // little - dale - dale@example.com
212 // little - dale - dale@example.com
213 // will - dale - will@example.com
214 // will - dale - will@example.com
215 // so 3 pairs for - first + last + mail
216 $this->assertEquals(count($foundDupes), 3, 'Check Individual-Supervised dupe rule for dupesInGroup().');
218 // Run dedupe finder as the browser would
219 $_SERVER['REQUEST_METHOD'] = 'GET'; //avoid invalid key error
220 $object = new CRM_Contact_Page_DedupeFind();
221 $object->set('gid', $this->_groupId
);
222 $object->set('rgid', $dao->id
);
223 $object->set('action', CRM_Core_Action
::UPDATE
);
224 $object->setEmbedded(TRUE);
227 // Retrieve pairs from prev next cache table
228 $select = array('pn.is_selected' => 'is_selected');
229 $cacheKeyString = CRM_Dedupe_Merger
::getMergeCacheKeyString($dao->id
, $this->_groupId
);
230 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
232 $this->assertEquals(count($foundDupes), count($pnDupePairs), 'Check number of dupe pairs in prev next cache.');
234 // batch merge all dupes
235 $result = CRM_Dedupe_Merger
::batchMerge($dao->id
, $this->_groupId
, 'safe', 5, 2);
236 $this->assertEquals(count($result['merged']), 3, 'Check number of merged pairs.');
238 // retrieve pairs from prev next cache table
239 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
240 $this->assertEquals(count($pnDupePairs), 0, 'Check number of remaining dupe pairs in prev next cache.');
242 $this->deleteDupeContacts();
246 * The goal of this function is to test that all required tables are returned.
248 public function testGetCidRefs() {
249 $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'Contacts');
250 $this->assertEquals(array_merge($this->getStaticCIDRefs(), $this->getHackedInCIDRef()), CRM_Dedupe_Merger
::cidRefs());
251 $this->assertEquals(array_merge($this->getCalculatedCIDRefs(), $this->getHackedInCIDRef()), CRM_Dedupe_Merger
::cidRefs());
255 * Get the list of not-really-cid-refs that are currently hacked in.
257 * This is hacked into getCIDs function.
261 public function getHackedInCIDRef() {
263 'civicrm_entity_tag' => array(
270 * Test function that gets duplicate pairs.
272 * It turns out there are 2 code paths retrieving this data so my initial focus is on ensuring
275 public function testGetMatches() {
276 $this->setupMatchData();
277 $pairs = CRM_Dedupe_Merger
::getDuplicatePairs(
285 $this->assertEquals(array(
287 'srcID' => $this->contacts
[1]['id'],
288 'srcName' => 'Mr. Mickey Mouse II',
289 'dstID' => $this->contacts
[0]['id'],
290 'dstName' => 'Mr. Mickey Mouse II',
295 'srcID' => $this->contacts
[3]['id'],
296 'srcName' => 'Mr. Minnie Mouse II',
297 'dstID' => $this->contacts
[2]['id'],
298 'dstName' => 'Mr. Minnie Mouse II',
306 * Test function that gets organization pairs.
308 * Note the rule will match on organization_name OR email - hence lots of matches.
310 public function testGetOrganizationMatches() {
311 $this->setupMatchData();
312 $ruleGroups = $this->callAPISuccessGetSingle('RuleGroup', array('contact_type' => 'Organization', 'used' => 'Supervised'));
314 $pairs = CRM_Dedupe_Merger
::getDuplicatePairs(
322 $expectedPairs = array(
324 'srcID' => $this->contacts
[5]['id'],
325 'srcName' => 'Walt Disney Ltd',
326 'dstID' => $this->contacts
[4]['id'],
327 'dstName' => 'Walt Disney Ltd',
332 'srcID' => $this->contacts
[7]['id'],
333 'srcName' => 'Walt Disney',
334 'dstID' => $this->contacts
[6]['id'],
335 'dstName' => 'Walt Disney',
340 'srcID' => $this->contacts
[6]['id'],
341 'srcName' => 'Walt Disney',
342 'dstID' => $this->contacts
[4]['id'],
343 'dstName' => 'Walt Disney Ltd',
348 'srcID' => $this->contacts
[6]['id'],
349 'srcName' => 'Walt Disney',
350 'dstID' => $this->contacts
[5]['id'],
351 'dstName' => 'Walt Disney Ltd',
356 usort($pairs, array(__CLASS__
, 'compareDupes'));
357 usort($expectedPairs, array(__CLASS__
, 'compareDupes'));
358 $this->assertEquals($expectedPairs, $pairs);
362 * Function to sort $duplicate records in a stable way.
368 public static function compareDupes($a, $b) {
369 foreach (array('srcName', 'dstName', 'srcID', 'dstID') as $field) {
370 if ($a[$field] != $b[$field]) {
371 return ($a[$field] < $b[$field]) ?
1 : -1;
378 * Test function that gets organization duplicate pairs.
380 public function testGetOrganizationMatchesInGroup() {
381 $this->setupMatchData();
382 $ruleGroups = $this->callAPISuccessGetSingle('RuleGroup', array('contact_type' => 'Organization', 'used' => 'Supervised'));
384 $groupID = $this->groupCreate(array('title' => 'she-mice'));
386 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupID, 'contact_id' => $this->contacts
[4]['id']));
388 $pairs = CRM_Dedupe_Merger
::getDuplicatePairs(
396 $this->assertEquals(array(
398 'srcID' => $this->contacts
[5]['id'],
399 'srcName' => 'Walt Disney Ltd',
400 'dstID' => $this->contacts
[4]['id'],
401 'dstName' => 'Walt Disney Ltd',
406 'srcID' => $this->contacts
[6]['id'],
407 'srcName' => 'Walt Disney',
408 'dstID' => $this->contacts
[4]['id'],
409 'dstName' => 'Walt Disney Ltd',
415 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupID, 'contact_id' => $this->contacts
[5]['id']));
416 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_prevnext_cache");
417 $pairs = CRM_Dedupe_Merger
::getDuplicatePairs(
425 $this->assertEquals(array(
427 'srcID' => $this->contacts
[5]['id'],
428 'srcName' => 'Walt Disney Ltd',
429 'dstID' => $this->contacts
[4]['id'],
430 'dstName' => 'Walt Disney Ltd',
435 'srcID' => $this->contacts
[6]['id'],
436 'srcName' => 'Walt Disney',
437 'dstID' => $this->contacts
[4]['id'],
438 'dstName' => 'Walt Disney Ltd',
443 'srcID' => $this->contacts
[6]['id'],
444 'srcName' => 'Walt Disney',
445 'dstID' => $this->contacts
[5]['id'],
446 'dstName' => 'Walt Disney Ltd',
454 * Test function that gets duplicate pairs.
456 * It turns out there are 2 code paths retrieving this data so my initial focus is on ensuring
459 public function testGetMatchesInGroup() {
460 $this->setupMatchData();
462 $groupID = $this->groupCreate(array('title' => 'she-mice'));
464 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupID, 'contact_id' => $this->contacts
[3]['id']));
466 $pairs = CRM_Dedupe_Merger
::getDuplicatePairs(
474 $this->assertEquals(array(
476 'srcID' => $this->contacts
[3]['id'],
477 'srcName' => 'Mr. Minnie Mouse II',
478 'dstID' => $this->contacts
[2]['id'],
479 'dstName' => 'Mr. Minnie Mouse II',
487 * CRM-19653 : Test that custom field data should/shouldn't be overriden on
488 * selecting/not selecting option to migrate data respectively
490 public function testCustomDataOverwrite() {
491 // Create Custom Field
492 $createGroup = $this->setupCustomGroupForIndividual();
493 $createField = $this->setupCustomField('Graduation', $createGroup);
494 $customFieldName = "custom_" . $createField['id'];
497 $this->setupMatchData();
499 $originalContactID = $this->contacts
[0]['id'];
500 $duplicateContactID1 = $this->contacts
[1]['id']; // used as duplicate contact in 1st use-case
501 $duplicateContactID2 = $this->contacts
[2]['id']; // used as duplicate contact in 2nd use-case
503 // update the text custom field for original contact with value 'abc'
504 $this->callAPISuccess('Contact', 'create', array(
505 'id' => $originalContactID,
506 "{$customFieldName}" => 'abc',
508 $this->assertCustomFieldValue($originalContactID, 'abc', $customFieldName);
510 // update the text custom field for duplicate contact 1 with value 'def'
511 $this->callAPISuccess('Contact', 'create', array(
512 'id' => $duplicateContactID1,
513 "{$customFieldName}" => 'def',
515 $this->assertCustomFieldValue($duplicateContactID1, 'def', $customFieldName);
517 // update the text custom field for duplicate contact 2 with value 'ghi'
518 $this->callAPISuccess('Contact', 'create', array(
519 'id' => $duplicateContactID2,
520 "{$customFieldName}" => 'ghi',
522 $this->assertCustomFieldValue($duplicateContactID2, 'ghi', $customFieldName);
524 /*** USE-CASE 1: DO NOT OVERWRITE CUSTOM FIELD VALUE **/
525 $this->mergeContacts($originalContactID, $duplicateContactID1, array(
526 "move_{$customFieldName}" => NULL,
528 $this->assertCustomFieldValue($originalContactID, 'abc', $customFieldName);
530 /*** USE-CASE 2: OVERWRITE CUSTOM FIELD VALUE **/
531 $this->mergeContacts($originalContactID, $duplicateContactID2, array(
532 "move_{$customFieldName}" => 'ghi',
534 $this->assertCustomFieldValue($originalContactID, 'ghi', $customFieldName);
536 // cleanup created custom set
537 $this->callAPISuccess('CustomField', 'delete', array('id' => $createField['id']));
538 $this->callAPISuccess('CustomGroup', 'delete', array('id' => $createGroup['id']));
542 * Verifies that when a contact with a custom field value is merged into a
543 * contact without a record int its corresponding custom group table, and none
544 * of the custom fields of that custom table are selected, the value is not
547 public function testMigrationOfUnselectedCustomDataOnEmptyCustomRecord() {
548 // Create Custom Fields
549 $createGroup = $this->setupCustomGroupForIndividual();
550 $customField1 = $this->setupCustomField('TestField', $createGroup);
552 // Create multi-value custom field
553 $multiGroup = $this->CustomGroupMultipleCreateByParams();
554 $multiField = $this->customFieldCreate(array(
555 'custom_group_id' => $multiGroup['id'],
556 'label' => 'field_1' . $multiGroup['id'],
561 $this->setupMatchData();
562 $originalContactID = $this->contacts
[0]['id'];
563 $duplicateContactID = $this->contacts
[1]['id'];
565 // Update the text custom fields for duplicate contact
566 $this->callAPISuccess('Contact', 'create', array(
567 'id' => $duplicateContactID,
568 "custom_{$customField1['id']}" => 'abc',
569 "custom_{$multiField['id']}" => 'def',
571 $this->assertCustomFieldValue($duplicateContactID, 'abc', "custom_{$customField1['id']}");
572 $this->assertCustomFieldValue($duplicateContactID, 'def', "custom_{$multiField['id']}");
574 // Merge, and ensure that no value was migrated
575 $this->mergeContacts($originalContactID, $duplicateContactID, array(
576 "move_custom_{$customField1['id']}" => NULL,
577 "move_rel_table_custom_{$multiGroup['id']}" => NULL,
579 $this->assertCustomFieldValue($originalContactID, '', "custom_{$customField1['id']}");
580 $this->assertCustomFieldValue($originalContactID, '', "custom_{$multiField['id']}");
582 // cleanup created custom set
583 $this->callAPISuccess('CustomField', 'delete', array('id' => $customField1['id']));
584 $this->callAPISuccess('CustomGroup', 'delete', array('id' => $createGroup['id']));
585 $this->callAPISuccess('CustomField', 'delete', array('id' => $multiField['id']));
586 $this->callAPISuccess('CustomGroup', 'delete', array('id' => $multiGroup['id']));
590 * Tests that if only part of the custom fields of a custom group are selected
591 * for a merge, only those values are merged, while all other fields of the
592 * custom group retain their original value, specifically for a contact with
593 * no records on the custom group table.
595 public function testMigrationOfSomeCustomDataOnEmptyCustomRecord() {
596 // Create Custom Fields
597 $createGroup = $this->setupCustomGroupForIndividual();
598 $customField1 = $this->setupCustomField('Test1', $createGroup);
599 $customField2 = $this->setupCustomField('Test2', $createGroup);
601 // Create multi-value custom field
602 $multiGroup = $this->CustomGroupMultipleCreateByParams();
603 $multiField = $this->customFieldCreate(array(
604 'custom_group_id' => $multiGroup['id'],
605 'label' => 'field_1' . $multiGroup['id'],
610 $this->setupMatchData();
611 $originalContactID = $this->contacts
[0]['id'];
612 $duplicateContactID = $this->contacts
[1]['id'];
614 // Update the text custom fields for duplicate contact
615 $this->callAPISuccess('Contact', 'create', array(
616 'id' => $duplicateContactID,
617 "custom_{$customField1['id']}" => 'abc',
618 "custom_{$customField2['id']}" => 'def',
619 "custom_{$multiField['id']}" => 'ghi',
621 $this->assertCustomFieldValue($duplicateContactID, 'abc', "custom_{$customField1['id']}");
622 $this->assertCustomFieldValue($duplicateContactID, 'def', "custom_{$customField2['id']}");
623 $this->assertCustomFieldValue($duplicateContactID, 'ghi', "custom_{$multiField['id']}");
626 $this->mergeContacts($originalContactID, $duplicateContactID, array(
627 "move_custom_{$customField1['id']}" => NULL,
628 "move_custom_{$customField2['id']}" => 'def',
629 "move_rel_table_custom_{$multiGroup['id']}" => '1',
631 $this->assertCustomFieldValue($originalContactID, '', "custom_{$customField1['id']}");
632 $this->assertCustomFieldValue($originalContactID, 'def', "custom_{$customField2['id']}");
633 $this->assertCustomFieldValue($originalContactID, 'ghi', "custom_{$multiField['id']}");
635 // cleanup created custom set
636 $this->callAPISuccess('CustomField', 'delete', array('id' => $customField1['id']));
637 $this->callAPISuccess('CustomField', 'delete', array('id' => $customField2['id']));
638 $this->callAPISuccess('CustomGroup', 'delete', array('id' => $createGroup['id']));
639 $this->callAPISuccess('CustomField', 'delete', array('id' => $multiField['id']));
640 $this->callAPISuccess('CustomGroup', 'delete', array('id' => $multiGroup['id']));
644 * Calls merge method on given contacts, with values given in $params array.
646 * @param $originalContactID
647 * ID of target contact
648 * @param $duplicateContactID
649 * ID of contact to be merged
651 * Array of fields to be merged from source into target contact, of the form
652 * ['move_<fieldName>' => <fieldValue>]
654 private function mergeContacts($originalContactID, $duplicateContactID, $params) {
655 $rowsElementsAndInfo = CRM_Dedupe_Merger
::getRowsElementsAndInfo($originalContactID, $duplicateContactID);
657 $migrationData = array(
658 'main_details' => $rowsElementsAndInfo['main_details'],
659 'other_details' => $rowsElementsAndInfo['other_details'],
662 // Migrate data of duplicate contact
663 CRM_Dedupe_Merger
::moveAllBelongings($originalContactID, $duplicateContactID, array_merge($migrationData, $params));
667 * Checks if the expected value for the given field corresponds to what is
668 * stored in the database for the given contact ID.
671 * @param $expectedValue
672 * @param $customFieldName
674 private function assertCustomFieldValue($contactID, $expectedValue, $customFieldName) {
675 $data = $this->callAPISuccess('Contact', 'getsingle', array(
677 'return' => array($customFieldName),
680 $this->assertEquals($expectedValue, $data[$customFieldName], "Custom field value was supposed to be '{$expectedValue}', '{$data[$customFieldName]}' found.");
684 * Creates a custom group to run tests on contacts that are individuals.
687 * Data for the created custom group record
689 private function setupCustomGroupForIndividual() {
690 $customGroup = $this->callAPISuccess('custom_group', 'get', array(
691 'name' => 'test_group',
694 if ($customGroup['count'] > 0) {
695 $this->callAPISuccess('CustomGroup', 'delete', array('id' => $customGroup['id']));
698 $customGroup = $this->callAPISuccess('custom_group', 'create', array(
699 'title' => 'Test_Group',
700 'name' => 'test_group',
701 'extends' => array('Individual'),
703 'is_multiple' => FALSE,
711 * Creates a custom field on the provided custom group with the given field
715 * @param $createGroup
718 * Data for the created custom field record
720 private function setupCustomField($fieldLabel, $createGroup) {
721 return $this->callAPISuccess('custom_field', 'create', array(
722 'label' => $fieldLabel,
723 'data_type' => 'Alphanumeric',
724 'html_type' => 'Text',
725 'custom_group_id' => $createGroup['id'],
730 * Set up some contacts for our matching.
732 public function setupMatchData() {
735 'first_name' => 'Mickey',
736 'last_name' => 'Mouse',
737 'email' => 'mickey@mouse.com',
740 'first_name' => 'Mickey',
741 'last_name' => 'Mouse',
742 'email' => 'mickey@mouse.com',
745 'first_name' => 'Minnie',
746 'last_name' => 'Mouse',
747 'email' => 'mickey@mouse.com',
750 'first_name' => 'Minnie',
751 'last_name' => 'Mouse',
752 'email' => 'mickey@mouse.com',
755 foreach ($fixtures as $fixture) {
756 $contactID = $this->individualCreate($fixture);
757 $this->contacts
[] = array_merge($fixture, array('id' => $contactID));
759 $organizationFixtures = array(
761 'organization_name' => 'Walt Disney Ltd',
762 'email' => 'walt@disney.com',
765 'organization_name' => 'Walt Disney Ltd',
766 'email' => 'walt@disney.com',
769 'organization_name' => 'Walt Disney',
770 'email' => 'walt@disney.com',
773 'organization_name' => 'Walt Disney',
774 'email' => 'walter@disney.com',
777 foreach ($organizationFixtures as $fixture) {
778 $contactID = $this->organizationCreate($fixture);
779 $this->contacts
[] = array_merge($fixture, array('id' => $contactID));
785 * Get the list of tables that refer to the CID.
787 * This is a statically maintained (in this test list).
789 * There is also a check against an automated list but having both seems to add extra stability to me. They do
792 public function getStaticCIDRefs() {
794 'civicrm_acl_cache' => array(
797 'civicrm_acl_contact_cache' => array(
801 'civicrm_action_log' => array(
804 'civicrm_activity_contact' => array(
807 'civicrm_address' => array(
810 'civicrm_batch' => array(
814 'civicrm_campaign' => array(
816 1 => 'last_modified_id',
818 'civicrm_case_contact' => array(
821 'civicrm_contact' => array(
822 0 => 'primary_contact_id',
825 'civicrm_contribution' => array(
828 'civicrm_contribution_page' => array(
831 'civicrm_contribution_recur' => array(
834 'civicrm_contribution_soft' => array(
837 'civicrm_custom_group' => array(
840 'civicrm_dashboard_contact' => array(
843 'civicrm_dedupe_exception' => array(
847 'civicrm_domain' => array(
850 'civicrm_email' => array(
853 'civicrm_event' => array(
856 'civicrm_event_carts' => array(
859 'civicrm_financial_account' => array(
862 'civicrm_financial_item' => array(
865 'civicrm_grant' => array(
868 'civicrm_group' => array(
872 'civicrm_group_contact' => array(
875 'civicrm_group_contact_cache' => array(
878 'civicrm_group_organization' => array(
879 0 => 'organization_id',
881 'civicrm_im' => array(
884 'civicrm_log' => array(
887 'civicrm_mailing' => array(
892 'civicrm_file' => array(
895 'civicrm_mailing_abtest' => array(
898 'civicrm_mailing_event_queue' => array(
901 'civicrm_mailing_event_subscribe' => array(
904 'civicrm_mailing_recipients' => array(
907 'civicrm_membership' => array(
910 'civicrm_membership_log' => array(
913 'civicrm_membership_type' => array(
914 0 => 'member_of_contact_id',
916 'civicrm_note' => array(
919 'civicrm_openid' => array(
922 'civicrm_participant' => array(
924 1 => 'transferred_to_contact_id', //CRM-16761
926 'civicrm_payment_token' => array(
930 'civicrm_pcp' => array(
933 'civicrm_phone' => array(
936 'civicrm_pledge' => array(
939 'civicrm_print_label' => array(
942 'civicrm_relationship' => array(
946 'civicrm_report_instance' => array(
950 'civicrm_setting' => array(
954 'civicrm_subscription_history' => array(
957 'civicrm_survey' => array(
959 1 => 'last_modified_id',
961 'civicrm_tag' => array(
964 'civicrm_uf_group' => array(
967 'civicrm_uf_match' => array(
970 'civicrm_value_testgetcidref_1' => array(
973 'civicrm_website' => array(
980 * Get a list of CIDs that is calculated off the schema.
982 * Note this is an expensive and table locking query. Should be safe in tests though.
984 public function getCalculatedCIDRefs() {
990 FROM information_schema.key_column_usage
992 referenced_table_schema = database() AND
993 referenced_table_name = 'civicrm_contact' AND
994 referenced_column_name = 'id';
996 $dao = CRM_Core_DAO
::executeQuery($sql);
997 while ($dao->fetch()) {
998 $cidRefs[$dao->table_name
][] = $dao->column_name
;
1000 // Do specific re-ordering changes to make this the same as the ref validated one.
1001 // The above query orders by FK alphabetically.
1002 // There might be cleverer ways to do this but it shouldn't change much.
1003 $cidRefs['civicrm_contact'][0] = 'primary_contact_id';
1004 $cidRefs['civicrm_contact'][1] = 'employer_id';
1005 $cidRefs['civicrm_acl_contact_cache'][0] = 'user_id';
1006 $cidRefs['civicrm_acl_contact_cache'][1] = 'contact_id';
1007 $cidRefs['civicrm_mailing'][0] = 'created_id';
1008 $cidRefs['civicrm_mailing'][1] = 'scheduled_id';
1009 $cidRefs['civicrm_mailing'][2] = 'approver_id';