Towards CRM-20155 remove duplicate calls to dedupe functions.
[civicrm-core.git] / tests / phpunit / CRM / Dedupe / DedupeFinderTest.php
CommitLineData
6a488035 1<?php
0eea664b 2
aba1cd8b
EM
3/**
4 * Class CRM_Dedupe_DedupeFinderTest
acb109b7 5 * @group headless
aba1cd8b 6 */
36741dcf 7class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
00be9182 8 public function testFuzzyDupes() {
6a488035
TO
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
19
20 // create a group to hold contacts, so that dupe checks don't consider any other contacts in the DB
21 $params = array(
22 'name' => 'Dupe Group',
23 'title' => 'New Test Dupe Group',
24 'domain_id' => 1,
25 'is_active' => 1,
26 'visibility' => 'Public Pages',
6a488035 27 );
87a56b12 28
29 $result = $this->callAPISuccess('group', 'create', $params);
6a488035
TO
30 $groupId = $result['id'];
31
32 // contact data set
33 // FIXME: move create params to separate function
34 $params = array(
35 array(
36 'first_name' => 'robin',
37 'last_name' => 'hood',
38 'email' => 'robin@example.com',
39 'contact_type' => 'Individual',
40 ),
41 array(
42 'first_name' => 'robin',
43 'last_name' => 'hood',
44 'email' => 'hood@example.com',
45 'contact_type' => 'Individual',
46 ),
47 array(
48 'first_name' => 'robin',
49 'last_name' => 'dale',
50 'email' => 'robin@example.com',
51 'contact_type' => 'Individual',
52 ),
53 array(
54 'first_name' => 'little',
55 'last_name' => 'dale',
56 'email' => 'dale@example.com',
57 'contact_type' => 'Individual',
58 ),
59 array(
60 'first_name' => 'will',
61 'last_name' => 'dale',
62 'email' => 'dale@example.com',
63 'contact_type' => 'Individual',
64 ),
65 array(
66 'first_name' => 'will',
67 'last_name' => 'dale',
68 'email' => 'will@example.com',
69 'contact_type' => 'Individual',
70 ),
71 array(
72 'first_name' => 'will',
73 'last_name' => 'dale',
74 'email' => 'will@example.com',
75 'contact_type' => 'Individual',
76 ),
77 );
78
79 $count = 1;
6a488035 80 foreach ($params as $param) {
87a56b12 81 $contact = $this->callAPISuccess('contact', 'create', $param);
6a488035
TO
82 $contactIds[$count++] = $contact['id'];
83
84 $grpParams = array(
85 'contact_id' => $contact['id'],
86 'group_id' => $groupId,
6a488035 87 );
87a56b12 88 $this->callAPISuccess('group_contact', 'create', $grpParams);
6a488035
TO
89 }
90
91 // verify that all contacts have been created separately
92 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
93
92915c55 94 $dao = new CRM_Dedupe_DAO_RuleGroup();
6a488035 95 $dao->contact_type = 'Individual';
92915c55
TO
96 $dao->level = 'Fuzzy';
97 $dao->is_default = 1;
6a488035
TO
98 $dao->find(TRUE);
99
100 $foundDupes = CRM_Dedupe_Finder::dupesInGroup($dao->id, $groupId);
101
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().');
109
6a488035 110 foreach ($contactIds as $contactId) {
93ac19cd 111 $this->contactDelete($contactId);
6a488035
TO
112 }
113 // delete dupe group
114 $params = array('id' => $groupId, 'version' => 3);
115 civicrm_api('group', 'delete', $params);
116 }
117
00be9182 118 public function testDupesByParams() {
6a488035
TO
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
129
130 // contact data set
131 // FIXME: move create params to separate function
132 $params = array(
133 array(
134 'first_name' => 'robin',
135 'last_name' => 'hood',
136 'email' => 'robin@example.com',
137 'contact_type' => 'Individual',
138 ),
139 array(
140 'first_name' => 'robin',
141 'last_name' => 'hood',
142 'email' => 'hood@example.com',
143 'contact_type' => 'Individual',
144 ),
145 array(
146 'first_name' => 'robin',
147 'last_name' => 'dale',
148 'email' => 'robin@example.com',
149 'contact_type' => 'Individual',
150 ),
151 array(
152 'first_name' => 'little',
153 'last_name' => 'dale',
154 'email' => 'dale@example.com',
155 'contact_type' => 'Individual',
156 ),
157 array(
158 'first_name' => 'will',
159 'last_name' => 'dale',
160 'email' => 'dale@example.com',
161 'contact_type' => 'Individual',
162 ),
163 array(
164 'first_name' => 'will',
165 'last_name' => 'dale',
166 'email' => 'will@example.com',
167 'contact_type' => 'Individual',
168 ),
169 array(
170 'first_name' => 'will',
171 'last_name' => 'dale',
172 'email' => 'will@example.com',
173 'contact_type' => 'Individual',
174 ),
175 );
176
177 $count = 1;
87a56b12 178
6a488035 179 foreach ($params as $param) {
87a56b12 180 $contact = $this->callAPISuccess('contact', 'create', $param);
6a488035
TO
181 $params = array(
182 'contact_id' => $contact['id'],
183 'street_address' => 'Ambachtstraat 23',
184 'location_type_id' => 1,
6a488035 185 );
87a56b12 186 $this->callAPISuccess('address', 'create', $params);
6a488035
TO
187 $contactIds[$count++] = $contact['id'];
188 }
189
190 // verify that all contacts have been created separately
191 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
192
92915c55 193 $dao = new CRM_Dedupe_DAO_RuleGroup();
6a488035 194 $dao->contact_type = 'Individual';
92915c55
TO
195 $dao->used = 'General';
196 $dao->is_default = 1;
6a488035
TO
197 $dao->find(TRUE);
198
199 $fields = array(
200 'first_name' => 'robin',
201 'last_name' => 'hood',
202 'email' => 'hood@example.com',
203 'street_address' => 'Ambachtstraat 23',
204 );
87a56b12 205 CRM_Core_TemporaryErrorScope::useException();
fedc3428 206 $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($fields, 'Individual', 'General');
6a488035
TO
207
208 // Check with default Individual-General rule
209 $this->assertEquals(count($ids), 2, 'Check Individual-General rule for dupesByParams().');
210
211 // delete all created contacts
212 foreach ($contactIds as $contactId) {
93ac19cd 213 $this->contactDelete($contactId);
6a488035
TO
214 }
215 }
96025800 216
6a488035 217}