4 * Class CRM_Dedupe_DedupeFinderTest
7 class CRM_Dedupe_DedupeFinderTest
extends CiviUnitTestCase
{
8 public function testFuzzyDupes() {
9 // make dupe checks based on based on following contact sets:
10 // FIRST - LAST - EMAIL
11 // ---------------------------------
12 // robin - hood - robin@example.com
13 // robin - hood - hood@example.com
14 // robin - dale - robin@example.com
15 // little - dale - dale@example.com
16 // will - dale - dale@example.com
17 // will - dale - will@example.com
18 // will - dale - will@example.com
20 // create a group to hold contacts, so that dupe checks don't consider any other contacts in the DB
22 'name' => 'Dupe Group',
23 'title' => 'New Test Dupe Group',
26 'visibility' => 'Public Pages',
29 $result = $this->callAPISuccess('group', 'create', $params);
30 $groupId = $result['id'];
33 // FIXME: move create params to separate function
36 'first_name' => 'robin',
37 'last_name' => 'hood',
38 'email' => 'robin@example.com',
39 'contact_type' => 'Individual',
42 'first_name' => 'robin',
43 'last_name' => 'hood',
44 'email' => 'hood@example.com',
45 'contact_type' => 'Individual',
48 'first_name' => 'robin',
49 'last_name' => 'dale',
50 'email' => 'robin@example.com',
51 'contact_type' => 'Individual',
54 'first_name' => 'little',
55 'last_name' => 'dale',
56 'email' => 'dale@example.com',
57 'contact_type' => 'Individual',
60 'first_name' => 'will',
61 'last_name' => 'dale',
62 'email' => 'dale@example.com',
63 'contact_type' => 'Individual',
66 'first_name' => 'will',
67 'last_name' => 'dale',
68 'email' => 'will@example.com',
69 'contact_type' => 'Individual',
72 'first_name' => 'will',
73 'last_name' => 'dale',
74 'email' => 'will@example.com',
75 'contact_type' => 'Individual',
80 foreach ($params as $param) {
81 $contact = $this->callAPISuccess('contact', 'create', $param);
82 $contactIds[$count++
] = $contact['id'];
85 'contact_id' => $contact['id'],
86 'group_id' => $groupId,
88 $this->callAPISuccess('group_contact', 'create', $grpParams);
91 // verify that all contacts have been created separately
92 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
94 $dao = new CRM_Dedupe_DAO_RuleGroup();
95 $dao->contact_type
= 'Individual';
96 $dao->level
= 'Fuzzy';
100 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($dao->id
, $groupId);
102 // -------------------------------------------------------------------------
103 // default dedupe rule: threshold = 20 => (First + Last + Email) Matches ( 1 pair )
104 // --------------------------------------------------------------------------
105 // will - dale - will@example.com
106 // will - dale - will@example.com
107 // so 1 pair for - first + last + mail
108 $this->assertEquals(count($foundDupes), 1, 'Check Individual-Fuzzy dupe rule for dupesInGroup().');
110 foreach ($contactIds as $contactId) {
111 $this->contactDelete($contactId);
114 $params = array('id' => $groupId, 'version' => 3);
115 civicrm_api('group', 'delete', $params);
118 public function testDupesByParams() {
119 // make dupe checks based on based on following contact sets:
120 // FIRST - LAST - EMAIL
121 // ---------------------------------
122 // robin - hood - robin@example.com
123 // robin - hood - hood@example.com
124 // robin - dale - robin@example.com
125 // little - dale - dale@example.com
126 // will - dale - dale@example.com
127 // will - dale - will@example.com
128 // will - dale - will@example.com
131 // FIXME: move create params to separate function
134 'first_name' => 'robin',
135 'last_name' => 'hood',
136 'email' => 'robin@example.com',
137 'contact_type' => 'Individual',
140 'first_name' => 'robin',
141 'last_name' => 'hood',
142 'email' => 'hood@example.com',
143 'contact_type' => 'Individual',
146 'first_name' => 'robin',
147 'last_name' => 'dale',
148 'email' => 'robin@example.com',
149 'contact_type' => 'Individual',
152 'first_name' => 'little',
153 'last_name' => 'dale',
154 'email' => 'dale@example.com',
155 'contact_type' => 'Individual',
158 'first_name' => 'will',
159 'last_name' => 'dale',
160 'email' => 'dale@example.com',
161 'contact_type' => 'Individual',
164 'first_name' => 'will',
165 'last_name' => 'dale',
166 'email' => 'will@example.com',
167 'contact_type' => 'Individual',
170 'first_name' => 'will',
171 'last_name' => 'dale',
172 'email' => 'will@example.com',
173 'contact_type' => 'Individual',
179 foreach ($params as $param) {
180 $contact = $this->callAPISuccess('contact', 'create', $param);
182 'contact_id' => $contact['id'],
183 'street_address' => 'Ambachtstraat 23',
184 'location_type_id' => 1,
186 $this->callAPISuccess('address', 'create', $params);
187 $contactIds[$count++
] = $contact['id'];
190 // verify that all contacts have been created separately
191 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
193 $dao = new CRM_Dedupe_DAO_RuleGroup();
194 $dao->contact_type
= 'Individual';
195 $dao->used
= 'General';
196 $dao->is_default
= 1;
200 'first_name' => 'robin',
201 'last_name' => 'hood',
202 'email' => 'hood@example.com',
203 'street_address' => 'Ambachtstraat 23',
205 CRM_Core_TemporaryErrorScope
::useException();
206 $ids = CRM_Contact_BAO_Contact
::getDuplicateContacts($fields, 'Individual', 'General');
208 // Check with default Individual-General rule
209 $this->assertEquals(count($ids), 2, 'Check Individual-General rule for dupesByParams().');
211 // delete all created contacts
212 foreach ($contactIds as $contactId) {
213 $this->contactDelete($contactId);