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 | */ | |
14 | protected $contactIDs = array(); | |
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) { | |
32 | $this->callAPISuccess('group', 'delete', array('id' => $this->groupID)); | |
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() { | |
6a488035 TO |
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 - 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 | |
a2dd33d3 | 55 | $ruleGroup = $this->callAPISuccessGetSingle('RuleGroup', array('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 | ||
ce217e17 | 61 | /** |
62 | * Test that a rule set to is_reserved = 0 works. | |
63 | * | |
64 | * There is a different search used dependent on this variable. | |
65 | */ | |
cf6f63dd AH |
66 | public function testCustomRule() { |
67 | $this->setupForGroupDedupe(); | |
68 | ||
69 | $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', array( | |
70 | 'contact_type' => 'Individual', | |
71 | 'threshold' => 8, | |
72 | 'used' => 'General', | |
73 | 'name' => 'TestRule', | |
74 | 'title' => 'TestRule', | |
75 | 'is_reserved' => 0, | |
76 | )); | |
ce217e17 | 77 | $rules = []; |
78 | foreach (array('birth_date', 'first_name', 'last_name') as $field) { | |
79 | $rules[$field] = $this->callAPISuccess('Rule', 'create', [ | |
69947f59 | 80 | 'dedupe_rule_group_id' => $ruleGroup['id'], |
81 | 'rule_table' => 'civicrm_contact', | |
82 | 'rule_weight' => 4, | |
83 | 'rule_field' => $field, | |
84 | ]); | |
cf6f63dd AH |
85 | } |
86 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($ruleGroup['id'], $this->groupID); | |
87 | $this->assertEquals(count($foundDupes), 4); | |
ce217e17 | 88 | CRM_Dedupe_Finder::dupes($ruleGroup['id']); |
89 | ||
cf6f63dd AH |
90 | } |
91 | ||
92eadb36 D |
92 | /** |
93 | * Test a custom rule with a non-default field. | |
94 | */ | |
95 | public function testCustomRuleWithAddress() { | |
96 | $this->setupForGroupDedupe(); | |
97 | ||
98 | $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', array( | |
99 | 'contact_type' => 'Individual', | |
100 | 'threshold' => 10, | |
101 | 'used' => 'General', | |
102 | 'name' => 'TestRule', | |
103 | 'title' => 'TestRule', | |
104 | 'is_reserved' => 0, | |
105 | )); | |
106 | $rules = []; | |
107 | foreach (array('postal_code') as $field) { | |
108 | $rules[$field] = $this->callAPISuccess('Rule', 'create', [ | |
109 | 'dedupe_rule_group_id' => $ruleGroup['id'], | |
110 | 'rule_table' => 'civicrm_address', | |
111 | 'rule_weight' => 10, | |
112 | 'rule_field' => $field, | |
113 | ]); | |
114 | } | |
115 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($ruleGroup['id'], $this->groupID); | |
116 | $this->assertEquals(count($foundDupes), 1); | |
117 | CRM_Dedupe_Finder::dupes($ruleGroup['id']); | |
118 | ||
119 | } | |
120 | ||
a2dd33d3 | 121 | /** |
122 | * Test the supervised dedupe rule against a group. | |
123 | * | |
124 | * @throws \Exception | |
125 | */ | |
126 | public function testSupervisedDupes() { | |
127 | $this->setupForGroupDedupe(); | |
6ba3b65c | 128 | $ruleGroup = $this->callAPISuccessGetSingle('RuleGroup', array('is_reserved' => 1, 'contact_type' => 'Individual', 'used' => 'Supervised')); |
a2dd33d3 | 129 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($ruleGroup['id'], $this->groupID); |
130 | // ------------------------------------------------------------------------- | |
131 | // default dedupe rule: threshold = 20 => (First + Last + Email) Matches ( 1 pair ) | |
132 | // -------------------------------------------------------------------------- | |
133 | // will - dale - will@example.com | |
134 | // will - dale - will@example.com | |
135 | // so 1 pair for - first + last + mail | |
136 | $this->assertEquals(count($foundDupes), 1, 'Check Individual-Fuzzy dupe rule for dupesInGroup().'); | |
137 | } | |
138 | ||
139 | /** | |
140 | * Test dupesByParams function. | |
141 | */ | |
142 | public function testDupesByParams() { | |
143 | // make dupe checks based on based on following contact sets: | |
144 | // FIRST - LAST - EMAIL | |
145 | // --------------------------------- | |
146 | // robin - hood - robin@example.com | |
147 | // robin - hood - hood@example.com | |
148 | // robin - dale - robin@example.com | |
149 | // little - dale - dale@example.com | |
150 | // will - dale - dale@example.com | |
151 | // will - dale - will@example.com | |
152 | // will - dale - will@example.com | |
6a488035 TO |
153 | |
154 | // contact data set | |
155 | // FIXME: move create params to separate function | |
156 | $params = array( | |
157 | array( | |
158 | 'first_name' => 'robin', | |
159 | 'last_name' => 'hood', | |
160 | 'email' => 'robin@example.com', | |
161 | 'contact_type' => 'Individual', | |
162 | ), | |
163 | array( | |
164 | 'first_name' => 'robin', | |
165 | 'last_name' => 'hood', | |
166 | 'email' => 'hood@example.com', | |
167 | 'contact_type' => 'Individual', | |
168 | ), | |
169 | array( | |
170 | 'first_name' => 'robin', | |
171 | 'last_name' => 'dale', | |
172 | 'email' => 'robin@example.com', | |
173 | 'contact_type' => 'Individual', | |
174 | ), | |
175 | array( | |
176 | 'first_name' => 'little', | |
177 | 'last_name' => 'dale', | |
178 | 'email' => 'dale@example.com', | |
179 | 'contact_type' => 'Individual', | |
180 | ), | |
181 | array( | |
182 | 'first_name' => 'will', | |
183 | 'last_name' => 'dale', | |
184 | 'email' => 'dale@example.com', | |
185 | 'contact_type' => 'Individual', | |
186 | ), | |
187 | array( | |
188 | 'first_name' => 'will', | |
189 | 'last_name' => 'dale', | |
190 | 'email' => 'will@example.com', | |
191 | 'contact_type' => 'Individual', | |
192 | ), | |
193 | array( | |
194 | 'first_name' => 'will', | |
195 | 'last_name' => 'dale', | |
196 | 'email' => 'will@example.com', | |
197 | 'contact_type' => 'Individual', | |
198 | ), | |
199 | ); | |
200 | ||
b8cb7e46 MWMC |
201 | $this->hookClass->setHook('civicrm_findDuplicates', array($this, 'hook_civicrm_findDuplicates')); |
202 | ||
6a488035 | 203 | $count = 1; |
a2dd33d3 | 204 | |
6a488035 | 205 | foreach ($params as $param) { |
87a56b12 | 206 | $contact = $this->callAPISuccess('contact', 'create', $param); |
a2dd33d3 | 207 | $params = array( |
6a488035 | 208 | 'contact_id' => $contact['id'], |
a2dd33d3 | 209 | 'street_address' => 'Ambachtstraat 23', |
210 | 'location_type_id' => 1, | |
6a488035 | 211 | ); |
a2dd33d3 | 212 | $this->callAPISuccess('address', 'create', $params); |
213 | $contactIds[$count++] = $contact['id']; | |
6a488035 TO |
214 | } |
215 | ||
216 | // verify that all contacts have been created separately | |
217 | $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.'); | |
218 | ||
a2dd33d3 | 219 | $fields = array( |
220 | 'first_name' => 'robin', | |
221 | 'last_name' => 'hood', | |
222 | 'email' => 'hood@example.com', | |
223 | 'street_address' => 'Ambachtstraat 23', | |
224 | ); | |
225 | CRM_Core_TemporaryErrorScope::useException(); | |
b8cb7e46 | 226 | $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($fields, 'Individual', 'General', [], TRUE, NULL, ['event_id' => 1]); |
6a488035 | 227 | |
a2dd33d3 | 228 | // Check with default Individual-General rule |
229 | $this->assertEquals(count($ids), 2, 'Check Individual-General rule for dupesByParams().'); | |
6a488035 | 230 | |
a2dd33d3 | 231 | // delete all created contacts |
6a488035 | 232 | foreach ($contactIds as $contactId) { |
93ac19cd | 233 | $this->contactDelete($contactId); |
6a488035 | 234 | } |
6a488035 TO |
235 | } |
236 | ||
b8cb7e46 MWMC |
237 | /** |
238 | * Implements hook_civicrm_findDuplicates(). | |
239 | * | |
240 | * Locks in expected params | |
241 | * | |
242 | */ | |
243 | public function hook_civicrm_findDuplicates($dedupeParams, &$dedupeResults, $contextParams) { | |
244 | $expectedDedupeParams = [ | |
245 | 'check_permission' => TRUE, | |
246 | 'contact_type' => 'Individual', | |
247 | 'rule' => 'General', | |
248 | 'rule_group_id' => NULL, | |
249 | 'excluded_contact_ids' => [], | |
250 | ]; | |
251 | foreach ($expectedDedupeParams as $key => $value) { | |
252 | $this->assertEquals($value, $dedupeParams[$key]); | |
253 | } | |
254 | $expectedDedupeResults = [ | |
255 | 'ids' => [], | |
256 | 'handled' => FALSE, | |
257 | ]; | |
258 | foreach ($expectedDedupeResults as $key => $value) { | |
259 | $this->assertEquals($value, $dedupeResults[$key]); | |
260 | } | |
261 | ||
262 | $expectedContext = ['event_id' => 1]; | |
263 | foreach ($expectedContext as $key => $value) { | |
264 | $this->assertEquals($value, $contextParams[$key]); | |
265 | } | |
266 | ||
267 | return $dedupeResults; | |
268 | } | |
269 | ||
a2dd33d3 | 270 | /** |
271 | * Set up a group of dedupable contacts. | |
272 | */ | |
273 | protected function setupForGroupDedupe() { | |
274 | $params = array( | |
275 | 'name' => 'Dupe Group', | |
276 | 'title' => 'New Test Dupe Group', | |
277 | 'domain_id' => 1, | |
278 | 'is_active' => 1, | |
279 | 'visibility' => 'Public Pages', | |
280 | ); | |
281 | ||
282 | $result = $this->callAPISuccess('group', 'create', $params); | |
283 | $this->groupID = $result['id']; | |
6a488035 | 284 | |
6a488035 TO |
285 | $params = array( |
286 | array( | |
287 | 'first_name' => 'robin', | |
288 | 'last_name' => 'hood', | |
289 | 'email' => 'robin@example.com', | |
290 | 'contact_type' => 'Individual', | |
ce217e17 | 291 | 'birth_date' => '2016-01-01', |
92eadb36 | 292 | 'api.Address.create' => ['location_type_id' => 'Billing', 'postal_code' => '99999'], |
6a488035 TO |
293 | ), |
294 | array( | |
295 | 'first_name' => 'robin', | |
296 | 'last_name' => 'hood', | |
297 | 'email' => 'hood@example.com', | |
298 | 'contact_type' => 'Individual', | |
ce217e17 | 299 | 'birth_date' => '2016-01-01', |
92eadb36 | 300 | 'api.Address.create' => ['location_type_id' => 'Billing', 'postal_code' => '99999'], |
6a488035 TO |
301 | ), |
302 | array( | |
303 | 'first_name' => 'robin', | |
304 | 'last_name' => 'dale', | |
305 | 'email' => 'robin@example.com', | |
306 | 'contact_type' => 'Individual', | |
307 | ), | |
308 | array( | |
309 | 'first_name' => 'little', | |
310 | 'last_name' => 'dale', | |
311 | 'email' => 'dale@example.com', | |
312 | 'contact_type' => 'Individual', | |
313 | ), | |
314 | array( | |
315 | 'first_name' => 'will', | |
316 | 'last_name' => 'dale', | |
317 | 'email' => 'dale@example.com', | |
318 | 'contact_type' => 'Individual', | |
319 | ), | |
320 | array( | |
321 | 'first_name' => 'will', | |
322 | 'last_name' => 'dale', | |
323 | 'email' => 'will@example.com', | |
324 | 'contact_type' => 'Individual', | |
325 | ), | |
326 | array( | |
327 | 'first_name' => 'will', | |
328 | 'last_name' => 'dale', | |
329 | 'email' => 'will@example.com', | |
330 | 'contact_type' => 'Individual', | |
331 | ), | |
332 | ); | |
333 | ||
334 | $count = 1; | |
6a488035 | 335 | foreach ($params as $param) { |
87a56b12 | 336 | $contact = $this->callAPISuccess('contact', 'create', $param); |
a2dd33d3 | 337 | $this->contactIDs[$count++] = $contact['id']; |
338 | ||
339 | $grpParams = array( | |
6a488035 | 340 | 'contact_id' => $contact['id'], |
a2dd33d3 | 341 | 'group_id' => $this->groupID, |
6a488035 | 342 | ); |
a2dd33d3 | 343 | $this->callAPISuccess('group_contact', 'create', $grpParams); |
6a488035 TO |
344 | } |
345 | ||
346 | // verify that all contacts have been created separately | |
a2dd33d3 | 347 | $this->assertEquals(count($this->contactIDs), 7, 'Check for number of contacts.'); |
6a488035 | 348 | } |
96025800 | 349 | |
6a488035 | 350 | } |