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 | ||
cf6f63dd AH |
61 | public function testCustomRule() { |
62 | $this->setupForGroupDedupe(); | |
63 | ||
64 | $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', array( | |
65 | 'contact_type' => 'Individual', | |
66 | 'threshold' => 8, | |
67 | 'used' => 'General', | |
68 | 'name' => 'TestRule', | |
69 | 'title' => 'TestRule', | |
70 | 'is_reserved' => 0, | |
71 | )); | |
72 | foreach (array('first_name', 'last_name') as $field) { | |
73 | $ruleDao = new CRM_Dedupe_DAO_Rule(); | |
74 | $ruleDao->dedupe_rule_group_id = $ruleGroup['id']; | |
75 | $ruleDao->rule_table = 'civicrm_contact'; | |
76 | $ruleDao->rule_field = $field; | |
77 | $ruleDao->rule_length = NULL; | |
78 | $ruleDao->rule_weight = 4; | |
79 | $ruleDao->save(); | |
80 | $ruleDao->free(); | |
81 | } | |
82 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($ruleGroup['id'], $this->groupID); | |
83 | $this->assertEquals(count($foundDupes), 4); | |
84 | } | |
85 | ||
a2dd33d3 | 86 | /** |
87 | * Test the supervised dedupe rule against a group. | |
88 | * | |
89 | * @throws \Exception | |
90 | */ | |
91 | public function testSupervisedDupes() { | |
92 | $this->setupForGroupDedupe(); | |
6ba3b65c | 93 | $ruleGroup = $this->callAPISuccessGetSingle('RuleGroup', array('is_reserved' => 1, 'contact_type' => 'Individual', 'used' => 'Supervised')); |
a2dd33d3 | 94 | $foundDupes = CRM_Dedupe_Finder::dupesInGroup($ruleGroup['id'], $this->groupID); |
95 | // ------------------------------------------------------------------------- | |
96 | // default dedupe rule: threshold = 20 => (First + Last + Email) Matches ( 1 pair ) | |
97 | // -------------------------------------------------------------------------- | |
98 | // will - dale - will@example.com | |
99 | // will - dale - will@example.com | |
100 | // so 1 pair for - first + last + mail | |
101 | $this->assertEquals(count($foundDupes), 1, 'Check Individual-Fuzzy dupe rule for dupesInGroup().'); | |
102 | } | |
103 | ||
104 | /** | |
105 | * Test dupesByParams function. | |
106 | */ | |
107 | public function testDupesByParams() { | |
108 | // make dupe checks based on based on following contact sets: | |
109 | // FIRST - LAST - EMAIL | |
110 | // --------------------------------- | |
111 | // robin - hood - robin@example.com | |
112 | // robin - hood - hood@example.com | |
113 | // robin - dale - robin@example.com | |
114 | // little - dale - dale@example.com | |
115 | // will - dale - dale@example.com | |
116 | // will - dale - will@example.com | |
117 | // will - dale - will@example.com | |
6a488035 TO |
118 | |
119 | // contact data set | |
120 | // FIXME: move create params to separate function | |
121 | $params = array( | |
122 | array( | |
123 | 'first_name' => 'robin', | |
124 | 'last_name' => 'hood', | |
125 | 'email' => 'robin@example.com', | |
126 | 'contact_type' => 'Individual', | |
127 | ), | |
128 | array( | |
129 | 'first_name' => 'robin', | |
130 | 'last_name' => 'hood', | |
131 | 'email' => 'hood@example.com', | |
132 | 'contact_type' => 'Individual', | |
133 | ), | |
134 | array( | |
135 | 'first_name' => 'robin', | |
136 | 'last_name' => 'dale', | |
137 | 'email' => 'robin@example.com', | |
138 | 'contact_type' => 'Individual', | |
139 | ), | |
140 | array( | |
141 | 'first_name' => 'little', | |
142 | 'last_name' => 'dale', | |
143 | 'email' => 'dale@example.com', | |
144 | 'contact_type' => 'Individual', | |
145 | ), | |
146 | array( | |
147 | 'first_name' => 'will', | |
148 | 'last_name' => 'dale', | |
149 | 'email' => 'dale@example.com', | |
150 | 'contact_type' => 'Individual', | |
151 | ), | |
152 | array( | |
153 | 'first_name' => 'will', | |
154 | 'last_name' => 'dale', | |
155 | 'email' => 'will@example.com', | |
156 | 'contact_type' => 'Individual', | |
157 | ), | |
158 | array( | |
159 | 'first_name' => 'will', | |
160 | 'last_name' => 'dale', | |
161 | 'email' => 'will@example.com', | |
162 | 'contact_type' => 'Individual', | |
163 | ), | |
164 | ); | |
165 | ||
166 | $count = 1; | |
a2dd33d3 | 167 | |
6a488035 | 168 | foreach ($params as $param) { |
87a56b12 | 169 | $contact = $this->callAPISuccess('contact', 'create', $param); |
a2dd33d3 | 170 | $params = array( |
6a488035 | 171 | 'contact_id' => $contact['id'], |
a2dd33d3 | 172 | 'street_address' => 'Ambachtstraat 23', |
173 | 'location_type_id' => 1, | |
6a488035 | 174 | ); |
a2dd33d3 | 175 | $this->callAPISuccess('address', 'create', $params); |
176 | $contactIds[$count++] = $contact['id']; | |
6a488035 TO |
177 | } |
178 | ||
179 | // verify that all contacts have been created separately | |
180 | $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.'); | |
181 | ||
92915c55 | 182 | $dao = new CRM_Dedupe_DAO_RuleGroup(); |
6a488035 | 183 | $dao->contact_type = 'Individual'; |
a2dd33d3 | 184 | $dao->used = 'General'; |
92915c55 | 185 | $dao->is_default = 1; |
6a488035 TO |
186 | $dao->find(TRUE); |
187 | ||
a2dd33d3 | 188 | $fields = array( |
189 | 'first_name' => 'robin', | |
190 | 'last_name' => 'hood', | |
191 | 'email' => 'hood@example.com', | |
192 | 'street_address' => 'Ambachtstraat 23', | |
193 | ); | |
194 | CRM_Core_TemporaryErrorScope::useException(); | |
195 | $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($fields, 'Individual', 'General'); | |
6a488035 | 196 | |
a2dd33d3 | 197 | // Check with default Individual-General rule |
198 | $this->assertEquals(count($ids), 2, 'Check Individual-General rule for dupesByParams().'); | |
6a488035 | 199 | |
a2dd33d3 | 200 | // delete all created contacts |
6a488035 | 201 | foreach ($contactIds as $contactId) { |
93ac19cd | 202 | $this->contactDelete($contactId); |
6a488035 | 203 | } |
6a488035 TO |
204 | } |
205 | ||
a2dd33d3 | 206 | /** |
207 | * Set up a group of dedupable contacts. | |
208 | */ | |
209 | protected function setupForGroupDedupe() { | |
210 | $params = array( | |
211 | 'name' => 'Dupe Group', | |
212 | 'title' => 'New Test Dupe Group', | |
213 | 'domain_id' => 1, | |
214 | 'is_active' => 1, | |
215 | 'visibility' => 'Public Pages', | |
216 | ); | |
217 | ||
218 | $result = $this->callAPISuccess('group', 'create', $params); | |
219 | $this->groupID = $result['id']; | |
6a488035 | 220 | |
6a488035 TO |
221 | $params = array( |
222 | array( | |
223 | 'first_name' => 'robin', | |
224 | 'last_name' => 'hood', | |
225 | 'email' => 'robin@example.com', | |
226 | 'contact_type' => 'Individual', | |
227 | ), | |
228 | array( | |
229 | 'first_name' => 'robin', | |
230 | 'last_name' => 'hood', | |
231 | 'email' => 'hood@example.com', | |
232 | 'contact_type' => 'Individual', | |
233 | ), | |
234 | array( | |
235 | 'first_name' => 'robin', | |
236 | 'last_name' => 'dale', | |
237 | 'email' => 'robin@example.com', | |
238 | 'contact_type' => 'Individual', | |
239 | ), | |
240 | array( | |
241 | 'first_name' => 'little', | |
242 | 'last_name' => 'dale', | |
243 | 'email' => 'dale@example.com', | |
244 | 'contact_type' => 'Individual', | |
245 | ), | |
246 | array( | |
247 | 'first_name' => 'will', | |
248 | 'last_name' => 'dale', | |
249 | 'email' => 'dale@example.com', | |
250 | 'contact_type' => 'Individual', | |
251 | ), | |
252 | array( | |
253 | 'first_name' => 'will', | |
254 | 'last_name' => 'dale', | |
255 | 'email' => 'will@example.com', | |
256 | 'contact_type' => 'Individual', | |
257 | ), | |
258 | array( | |
259 | 'first_name' => 'will', | |
260 | 'last_name' => 'dale', | |
261 | 'email' => 'will@example.com', | |
262 | 'contact_type' => 'Individual', | |
263 | ), | |
264 | ); | |
265 | ||
266 | $count = 1; | |
6a488035 | 267 | foreach ($params as $param) { |
87a56b12 | 268 | $contact = $this->callAPISuccess('contact', 'create', $param); |
a2dd33d3 | 269 | $this->contactIDs[$count++] = $contact['id']; |
270 | ||
271 | $grpParams = array( | |
6a488035 | 272 | 'contact_id' => $contact['id'], |
a2dd33d3 | 273 | 'group_id' => $this->groupID, |
6a488035 | 274 | ); |
a2dd33d3 | 275 | $this->callAPISuccess('group_contact', 'create', $grpParams); |
6a488035 TO |
276 | } |
277 | ||
278 | // verify that all contacts have been created separately | |
a2dd33d3 | 279 | $this->assertEquals(count($this->contactIDs), 7, 'Check for number of contacts.'); |
6a488035 | 280 | } |
96025800 | 281 | |
6a488035 | 282 | } |