Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
0eea664b | 2 | |
aba1cd8b EM |
3 | /** |
4 | * Class CRM_Dedupe_DedupeFinderTest | |
acb109b7 | 5 | * @group headless |
aba1cd8b | 6 | */ |
36741dcf | 7 | class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase { |
a2dd33d3 | 8 | |
9 | /** | |
10 | * IDs of created contacts. | |
11 | * | |
12 | * @var array | |
13 | */ | |
9099cab3 | 14 | protected $contactIDs = []; |
a2dd33d3 | 15 | |
16 | /** | |
17 | * ID of the group holding the contacts. | |
18 | * | |
19 | * @var int | |
20 | */ | |
21 | protected $groupID; | |
22 | ||
23 | /** | |
24 | * Clean up after the test. | |
25 | */ | |
26 | public function tearDown() { | |
27 | ||
28 | foreach ($this->contactIDs as $contactId) { | |
29 | $this->contactDelete($contactId); | |
30 | } | |
31 | if ($this->groupID) { | |
9099cab3 | 32 | $this->callAPISuccess('group', 'delete', ['id' => $this->groupID]); |
a2dd33d3 | 33 | } |
34 | parent::tearDown(); | |
35 | } | |
36 | ||
37 | /** | |
38 | * Test the unsupervised dedupe rule against a group. | |
39 | * | |
40 | * @throws \Exception | |
41 | */ | |
42 | public function testUnsupervisedDupes() { | |
367c9a2d | 43 | // make dupe checks based on following contact sets: |
6a488035 TO |
44 | // FIRST - LAST - EMAIL |
45 | // --------------------------------- | |
46 | // robin - hood - robin@example.com | |
47 | // robin - hood - hood@example.com | |
48 | // robin - dale - robin@example.com | |
49 | // little - dale - dale@example.com | |
50 | // will - dale - dale@example.com | |
51 | // will - dale - will@example.com | |
52 | // will - dale - will@example.com | |
a2dd33d3 | 53 | $this->setupForGroupDedupe(); |
6a488035 | 54 | |
9099cab3 | 55 | $ruleGroup = $this->callAPISuccessGetSingle('RuleGroup', ['is_reserved' => 1, 'contact_type' => 'Individual', 'used' => 'Unsupervised']); |
87a56b12 | 56 | |
a2dd33d3 | 57 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($ruleGroup['id'], $this->groupID); |
58 | $this->assertEquals(count($foundDupes), 3, 'Check Individual-Fuzzy dupe rule for dupesInGroup().'); | |
59 | } | |
60 | ||
367c9a2d JP |
61 | /** |
62 | * Test duplicate contact retrieval with 2 email fields. | |
63 | */ | |
64 | public function testUnsupervisedWithTwoEmailFields() { | |
65 | $this->setupForGroupDedupe(); | |
66 | $emails = [ | |
67 | ['hood@example.com', ''], | |
68 | ['', 'hood@example.com'], | |
69 | ]; | |
70 | for ($i = 0; $i < 2; $i++) { | |
71 | $fields = [ | |
72 | 'first_name' => 'robin', | |
73 | 'last_name' => 'hood', | |
74 | 'email-1' => $emails[$i][0], | |
75 | 'email-2' => $emails[$i][1], | |
76 | ]; | |
77 | $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, 'Individual'); | |
78 | $dedupeResults = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual'); | |
79 | $this->assertEquals(count($dedupeResults), 1); | |
80 | } | |
81 | } | |
82 | ||
ce217e17 | 83 | /** |
84 | * Test that a rule set to is_reserved = 0 works. | |
85 | * | |
86 | * There is a different search used dependent on this variable. | |
87 | */ | |
cf6f63dd AH |
88 | public function testCustomRule() { |
89 | $this->setupForGroupDedupe(); | |
90 | ||
9099cab3 | 91 | $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', [ |
cf6f63dd AH |
92 | 'contact_type' => 'Individual', |
93 | 'threshold' => 8, | |
94 | 'used' => 'General', | |
95 | 'name' => 'TestRule', | |
96 | 'title' => 'TestRule', | |
97 | 'is_reserved' => 0, | |
9099cab3 | 98 | ]); |
ce217e17 | 99 | $rules = []; |
9099cab3 | 100 | foreach (['birth_date', 'first_name', 'last_name'] as $field) { |
ce217e17 | 101 | $rules[$field] = $this->callAPISuccess('Rule', 'create', [ |
69947f59 | 102 | 'dedupe_rule_group_id' => $ruleGroup['id'], |
103 | 'rule_table' => 'civicrm_contact', | |
104 | 'rule_weight' => 4, | |
105 | 'rule_field' => $field, | |
106 | ]); | |
cf6f63dd AH |
107 | } |
108 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($ruleGroup['id'], $this->groupID); | |
109 | $this->assertEquals(count($foundDupes), 4); | |
ce217e17 | 110 | CRM_Dedupe_Finder::dupes($ruleGroup['id']); |
111 | ||
cf6f63dd AH |
112 | } |
113 | ||
92eadb36 D |
114 | /** |
115 | * Test a custom rule with a non-default field. | |
116 | */ | |
117 | public function testCustomRuleWithAddress() { | |
118 | $this->setupForGroupDedupe(); | |
119 | ||
9099cab3 | 120 | $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', [ |
92eadb36 D |
121 | 'contact_type' => 'Individual', |
122 | 'threshold' => 10, | |
123 | 'used' => 'General', | |
124 | 'name' => 'TestRule', | |
125 | 'title' => 'TestRule', | |
126 | 'is_reserved' => 0, | |
9099cab3 | 127 | ]); |
92eadb36 | 128 | $rules = []; |
9099cab3 | 129 | foreach (['postal_code'] as $field) { |
92eadb36 D |
130 | $rules[$field] = $this->callAPISuccess('Rule', 'create', [ |
131 | 'dedupe_rule_group_id' => $ruleGroup['id'], | |
132 | 'rule_table' => 'civicrm_address', | |
133 | 'rule_weight' => 10, | |
134 | 'rule_field' => $field, | |
135 | ]); | |
136 | } | |
137 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($ruleGroup['id'], $this->groupID); | |
138 | $this->assertEquals(count($foundDupes), 1); | |
139 | CRM_Dedupe_Finder::dupes($ruleGroup['id']); | |
140 | ||
141 | } | |
142 | ||
197e72c4 SL |
143 | /** |
144 | * Test a custom rule with a non-default field. | |
145 | */ | |
146 | public function testInclusiveRule() { | |
147 | $this->setupForGroupDedupe(); | |
148 | ||
149 | $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', [ | |
150 | 'contact_type' => 'Individual', | |
151 | 'threshold' => 8, | |
152 | 'used' => 'General', | |
153 | 'name' => 'TestRule', | |
154 | 'title' => 'TestRule', | |
155 | 'is_reserved' => 0, | |
156 | ]); | |
157 | $rules = []; | |
158 | foreach (['first_name', 'last_name'] as $field) { | |
159 | $rules[$field] = $this->callAPISuccess('Rule', 'create', [ | |
160 | 'dedupe_rule_group_id' => $ruleGroup['id'], | |
161 | 'rule_table' => 'civicrm_contact', | |
162 | 'rule_weight' => 4, | |
163 | 'rule_field' => $field, | |
164 | ]); | |
165 | } | |
166 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($ruleGroup['id'], $this->groupID); | |
167 | $this->assertEquals(4, count($foundDupes)); | |
168 | CRM_Dedupe_Finder::dupes($ruleGroup['id']); | |
169 | } | |
170 | ||
a2dd33d3 | 171 | /** |
172 | * Test the supervised dedupe rule against a group. | |
173 | * | |
174 | * @throws \Exception | |
175 | */ | |
176 | public function testSupervisedDupes() { | |
177 | $this->setupForGroupDedupe(); | |
9099cab3 | 178 | $ruleGroup = $this->callAPISuccessGetSingle('RuleGroup', ['is_reserved' => 1, 'contact_type' => 'Individual', 'used' => 'Supervised']); |
a2dd33d3 | 179 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($ruleGroup['id'], $this->groupID); |
180 | // ------------------------------------------------------------------------- | |
181 | // default dedupe rule: threshold = 20 => (First + Last + Email) Matches ( 1 pair ) | |
182 | // -------------------------------------------------------------------------- | |
183 | // will - dale - will@example.com | |
184 | // will - dale - will@example.com | |
185 | // so 1 pair for - first + last + mail | |
186 | $this->assertEquals(count($foundDupes), 1, 'Check Individual-Fuzzy dupe rule for dupesInGroup().'); | |
187 | } | |
188 | ||
189 | /** | |
190 | * Test dupesByParams function. | |
191 | */ | |
192 | public function testDupesByParams() { | |
193 | // make dupe checks based on based on following contact sets: | |
194 | // FIRST - LAST - EMAIL | |
195 | // --------------------------------- | |
196 | // robin - hood - robin@example.com | |
197 | // robin - hood - hood@example.com | |
198 | // robin - dale - robin@example.com | |
199 | // little - dale - dale@example.com | |
200 | // will - dale - dale@example.com | |
201 | // will - dale - will@example.com | |
202 | // will - dale - will@example.com | |
6a488035 TO |
203 | |
204 | // contact data set | |
205 | // FIXME: move create params to separate function | |
9099cab3 CW |
206 | $params = [ |
207 | [ | |
6a488035 TO |
208 | 'first_name' => 'robin', |
209 | 'last_name' => 'hood', | |
210 | 'email' => 'robin@example.com', | |
211 | 'contact_type' => 'Individual', | |
9099cab3 CW |
212 | ], |
213 | [ | |
6a488035 TO |
214 | 'first_name' => 'robin', |
215 | 'last_name' => 'hood', | |
216 | 'email' => 'hood@example.com', | |
217 | 'contact_type' => 'Individual', | |
9099cab3 CW |
218 | ], |
219 | [ | |
6a488035 TO |
220 | 'first_name' => 'robin', |
221 | 'last_name' => 'dale', | |
222 | 'email' => 'robin@example.com', | |
223 | 'contact_type' => 'Individual', | |
9099cab3 CW |
224 | ], |
225 | [ | |
6a488035 TO |
226 | 'first_name' => 'little', |
227 | 'last_name' => 'dale', | |
228 | 'email' => 'dale@example.com', | |
229 | 'contact_type' => 'Individual', | |
9099cab3 CW |
230 | ], |
231 | [ | |
6a488035 TO |
232 | 'first_name' => 'will', |
233 | 'last_name' => 'dale', | |
234 | 'email' => 'dale@example.com', | |
235 | 'contact_type' => 'Individual', | |
9099cab3 CW |
236 | ], |
237 | [ | |
6a488035 TO |
238 | 'first_name' => 'will', |
239 | 'last_name' => 'dale', | |
240 | 'email' => 'will@example.com', | |
241 | 'contact_type' => 'Individual', | |
9099cab3 CW |
242 | ], |
243 | [ | |
6a488035 TO |
244 | 'first_name' => 'will', |
245 | 'last_name' => 'dale', | |
246 | 'email' => 'will@example.com', | |
247 | 'contact_type' => 'Individual', | |
9099cab3 CW |
248 | ], |
249 | ]; | |
6a488035 | 250 | |
9099cab3 | 251 | $this->hookClass->setHook('civicrm_findDuplicates', [$this, 'hook_civicrm_findDuplicates']); |
b8cb7e46 | 252 | |
6a488035 | 253 | $count = 1; |
a2dd33d3 | 254 | |
6a488035 | 255 | foreach ($params as $param) { |
87a56b12 | 256 | $contact = $this->callAPISuccess('contact', 'create', $param); |
9099cab3 | 257 | $params = [ |
6a488035 | 258 | 'contact_id' => $contact['id'], |
a2dd33d3 | 259 | 'street_address' => 'Ambachtstraat 23', |
260 | 'location_type_id' => 1, | |
9099cab3 | 261 | ]; |
a2dd33d3 | 262 | $this->callAPISuccess('address', 'create', $params); |
263 | $contactIds[$count++] = $contact['id']; | |
6a488035 TO |
264 | } |
265 | ||
266 | // verify that all contacts have been created separately | |
267 | $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.'); | |
268 | ||
9099cab3 | 269 | $fields = [ |
a2dd33d3 | 270 | 'first_name' => 'robin', |
271 | 'last_name' => 'hood', | |
272 | 'email' => 'hood@example.com', | |
273 | 'street_address' => 'Ambachtstraat 23', | |
9099cab3 | 274 | ]; |
a2dd33d3 | 275 | CRM_Core_TemporaryErrorScope::useException(); |
b8cb7e46 | 276 | $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($fields, 'Individual', 'General', [], TRUE, NULL, ['event_id' => 1]); |
6a488035 | 277 | |
a2dd33d3 | 278 | // Check with default Individual-General rule |
279 | $this->assertEquals(count($ids), 2, 'Check Individual-General rule for dupesByParams().'); | |
6a488035 | 280 | |
a2dd33d3 | 281 | // delete all created contacts |
6a488035 | 282 | foreach ($contactIds as $contactId) { |
93ac19cd | 283 | $this->contactDelete($contactId); |
6a488035 | 284 | } |
6a488035 TO |
285 | } |
286 | ||
b8cb7e46 MWMC |
287 | /** |
288 | * Implements hook_civicrm_findDuplicates(). | |
289 | * | |
290 | * Locks in expected params | |
291 | * | |
292 | */ | |
293 | public function hook_civicrm_findDuplicates($dedupeParams, &$dedupeResults, $contextParams) { | |
294 | $expectedDedupeParams = [ | |
295 | 'check_permission' => TRUE, | |
296 | 'contact_type' => 'Individual', | |
297 | 'rule' => 'General', | |
298 | 'rule_group_id' => NULL, | |
299 | 'excluded_contact_ids' => [], | |
300 | ]; | |
301 | foreach ($expectedDedupeParams as $key => $value) { | |
302 | $this->assertEquals($value, $dedupeParams[$key]); | |
303 | } | |
304 | $expectedDedupeResults = [ | |
305 | 'ids' => [], | |
306 | 'handled' => FALSE, | |
307 | ]; | |
308 | foreach ($expectedDedupeResults as $key => $value) { | |
309 | $this->assertEquals($value, $dedupeResults[$key]); | |
310 | } | |
311 | ||
312 | $expectedContext = ['event_id' => 1]; | |
313 | foreach ($expectedContext as $key => $value) { | |
314 | $this->assertEquals($value, $contextParams[$key]); | |
315 | } | |
316 | ||
317 | return $dedupeResults; | |
318 | } | |
319 | ||
a2dd33d3 | 320 | /** |
321 | * Set up a group of dedupable contacts. | |
322 | */ | |
323 | protected function setupForGroupDedupe() { | |
9099cab3 | 324 | $params = [ |
a2dd33d3 | 325 | 'name' => 'Dupe Group', |
326 | 'title' => 'New Test Dupe Group', | |
327 | 'domain_id' => 1, | |
328 | 'is_active' => 1, | |
329 | 'visibility' => 'Public Pages', | |
9099cab3 | 330 | ]; |
a2dd33d3 | 331 | |
332 | $result = $this->callAPISuccess('group', 'create', $params); | |
333 | $this->groupID = $result['id']; | |
6a488035 | 334 | |
9099cab3 CW |
335 | $params = [ |
336 | [ | |
6a488035 TO |
337 | 'first_name' => 'robin', |
338 | 'last_name' => 'hood', | |
339 | 'email' => 'robin@example.com', | |
340 | 'contact_type' => 'Individual', | |
ce217e17 | 341 | 'birth_date' => '2016-01-01', |
197e72c4 | 342 | 'api.Address.create' => ['street_address' => '123 Happy world', 'location_type_id' => 'Billing', 'postal_code' => '99999'], |
9099cab3 CW |
343 | ], |
344 | [ | |
6a488035 TO |
345 | 'first_name' => 'robin', |
346 | 'last_name' => 'hood', | |
347 | 'email' => 'hood@example.com', | |
348 | 'contact_type' => 'Individual', | |
ce217e17 | 349 | 'birth_date' => '2016-01-01', |
197e72c4 | 350 | 'api.Address.create' => ['street_address' => '123 Happy World', 'location_type_id' => 'Billing', 'postal_code' => '99999'], |
9099cab3 CW |
351 | ], |
352 | [ | |
6a488035 TO |
353 | 'first_name' => 'robin', |
354 | 'last_name' => 'dale', | |
355 | 'email' => 'robin@example.com', | |
356 | 'contact_type' => 'Individual', | |
9099cab3 CW |
357 | ], |
358 | [ | |
6a488035 TO |
359 | 'first_name' => 'little', |
360 | 'last_name' => 'dale', | |
361 | 'email' => 'dale@example.com', | |
362 | 'contact_type' => 'Individual', | |
9099cab3 CW |
363 | ], |
364 | [ | |
6a488035 TO |
365 | 'first_name' => 'will', |
366 | 'last_name' => 'dale', | |
367 | 'email' => 'dale@example.com', | |
368 | 'contact_type' => 'Individual', | |
9099cab3 CW |
369 | ], |
370 | [ | |
6a488035 TO |
371 | 'first_name' => 'will', |
372 | 'last_name' => 'dale', | |
373 | 'email' => 'will@example.com', | |
374 | 'contact_type' => 'Individual', | |
9099cab3 CW |
375 | ], |
376 | [ | |
6a488035 TO |
377 | 'first_name' => 'will', |
378 | 'last_name' => 'dale', | |
379 | 'email' => 'will@example.com', | |
380 | 'contact_type' => 'Individual', | |
9099cab3 CW |
381 | ], |
382 | ]; | |
6a488035 TO |
383 | |
384 | $count = 1; | |
6a488035 | 385 | foreach ($params as $param) { |
87a56b12 | 386 | $contact = $this->callAPISuccess('contact', 'create', $param); |
a2dd33d3 | 387 | $this->contactIDs[$count++] = $contact['id']; |
388 | ||
9099cab3 | 389 | $grpParams = [ |
6a488035 | 390 | 'contact_id' => $contact['id'], |
a2dd33d3 | 391 | 'group_id' => $this->groupID, |
9099cab3 | 392 | ]; |
a2dd33d3 | 393 | $this->callAPISuccess('group_contact', 'create', $grpParams); |
6a488035 TO |
394 | } |
395 | ||
396 | // verify that all contacts have been created separately | |
a2dd33d3 | 397 | $this->assertEquals(count($this->contactIDs), 7, 'Check for number of contacts.'); |
6a488035 | 398 | } |
96025800 | 399 | |
6a488035 | 400 | } |