tests - add autogenerated comment blocks
[civicrm-core.git] / tests / phpunit / CRM / Dedupe / DedupeFinderTest.php
1 <?php
2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/Contact.php';
4
5 /**
6 * Class CRM_Dedupe_DedupeFinderTest
7 */
8 class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
9 function testFuzzyDupes() {
10 // make dupe checks based on based on following contact sets:
11 // FIRST - LAST - EMAIL
12 // ---------------------------------
13 // robin - hood - robin@example.com
14 // robin - hood - hood@example.com
15 // robin - dale - robin@example.com
16 // little - dale - dale@example.com
17 // will - dale - dale@example.com
18 // will - dale - will@example.com
19 // will - dale - will@example.com
20
21 // create a group to hold contacts, so that dupe checks don't consider any other contacts in the DB
22 $params = array(
23 'name' => 'Dupe Group',
24 'title' => 'New Test Dupe Group',
25 'domain_id' => 1,
26 'is_active' => 1,
27 'visibility' => 'Public Pages',
28 'version' => 3,
29 );
30 // TODO: This is not an API test!!
31 $result = civicrm_api('group', 'create', $params);
32 $groupId = $result['id'];
33
34 // contact data set
35 // FIXME: move create params to separate function
36 $params = array(
37 array(
38 'first_name' => 'robin',
39 'last_name' => 'hood',
40 'email' => 'robin@example.com',
41 'contact_type' => 'Individual',
42 ),
43 array(
44 'first_name' => 'robin',
45 'last_name' => 'hood',
46 'email' => 'hood@example.com',
47 'contact_type' => 'Individual',
48 ),
49 array(
50 'first_name' => 'robin',
51 'last_name' => 'dale',
52 'email' => 'robin@example.com',
53 'contact_type' => 'Individual',
54 ),
55 array(
56 'first_name' => 'little',
57 'last_name' => 'dale',
58 'email' => 'dale@example.com',
59 'contact_type' => 'Individual',
60 ),
61 array(
62 'first_name' => 'will',
63 'last_name' => 'dale',
64 'email' => 'dale@example.com',
65 'contact_type' => 'Individual',
66 ),
67 array(
68 'first_name' => 'will',
69 'last_name' => 'dale',
70 'email' => 'will@example.com',
71 'contact_type' => 'Individual',
72 ),
73 array(
74 'first_name' => 'will',
75 'last_name' => 'dale',
76 'email' => 'will@example.com',
77 'contact_type' => 'Individual',
78 ),
79 );
80
81 $count = 1;
82 // TODO: This is not an API test!!
83 foreach ($params as $param) {
84 $param['version'] = 3;
85 $contact = civicrm_api('contact', 'create', $param);
86 $contactIds[$count++] = $contact['id'];
87
88 $grpParams = array(
89 'contact_id' => $contact['id'],
90 'group_id' => $groupId,
91 'version' => 3,
92 );
93 $res = civicrm_api('group_contact', 'create', $grpParams);
94 }
95
96 // verify that all contacts have been created separately
97 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
98
99 $dao = new CRM_Dedupe_DAO_RuleGroup();
100 $dao->contact_type = 'Individual';
101 $dao->level = 'Fuzzy';
102 $dao->is_default = 1;
103 $dao->find(TRUE);
104
105 $foundDupes = CRM_Dedupe_Finder::dupesInGroup($dao->id, $groupId);
106
107 // -------------------------------------------------------------------------
108 // default dedupe rule: threshold = 20 => (First + Last + Email) Matches ( 1 pair )
109 // --------------------------------------------------------------------------
110 // will - dale - will@example.com
111 // will - dale - will@example.com
112 // so 1 pair for - first + last + mail
113 $this->assertEquals(count($foundDupes), 1, 'Check Individual-Fuzzy dupe rule for dupesInGroup().');
114
115 // delete all created contacts
116 foreach ($contactIds as $contactId) {
117 Contact::delete($contactId);
118 }
119 // delete dupe group
120 $params = array('id' => $groupId, 'version' => 3);
121 civicrm_api('group', 'delete', $params);
122 }
123
124 function testDupesByParams() {
125 // make dupe checks based on based on following contact sets:
126 // FIRST - LAST - EMAIL
127 // ---------------------------------
128 // robin - hood - robin@example.com
129 // robin - hood - hood@example.com
130 // robin - dale - robin@example.com
131 // little - dale - dale@example.com
132 // will - dale - dale@example.com
133 // will - dale - will@example.com
134 // will - dale - will@example.com
135
136 // contact data set
137 // FIXME: move create params to separate function
138 $params = array(
139 array(
140 'first_name' => 'robin',
141 'last_name' => 'hood',
142 'email' => 'robin@example.com',
143 'contact_type' => 'Individual',
144 ),
145 array(
146 'first_name' => 'robin',
147 'last_name' => 'hood',
148 'email' => 'hood@example.com',
149 'contact_type' => 'Individual',
150 ),
151 array(
152 'first_name' => 'robin',
153 'last_name' => 'dale',
154 'email' => 'robin@example.com',
155 'contact_type' => 'Individual',
156 ),
157 array(
158 'first_name' => 'little',
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' => 'dale@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 array(
176 'first_name' => 'will',
177 'last_name' => 'dale',
178 'email' => 'will@example.com',
179 'contact_type' => 'Individual',
180 ),
181 );
182
183 $count = 1;
184 // TODO: This is not an API test!!
185 foreach ($params as $param) {
186 $param['version'] = 3;
187 $contact = civicrm_api('contact', 'create', $param);
188 $params = array(
189 'contact_id' => $contact['id'],
190 'street_address' => 'Ambachtstraat 23',
191 'location_type_id' => 1,
192 'version' => 3,
193 );
194 $result = civicrm_api( 'address','create',$params );
195 $contactIds[$count++] = $contact['id'];
196 }
197
198 // verify that all contacts have been created separately
199 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
200
201 $dao = new CRM_Dedupe_DAO_RuleGroup();
202 $dao->contact_type = 'Individual';
203 $dao->used = 'General';
204 $dao->is_default = 1;
205 $dao->find(TRUE);
206
207 $fields = array(
208 'first_name' => 'robin',
209 'last_name' => 'hood',
210 'email' => 'hood@example.com',
211 'street_address' => 'Ambachtstraat 23',
212 );
213 $errorScope = CRM_Core_TemporaryErrorScope::useException();
214 $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, 'Individual');
215 $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'General');
216
217 // Check with default Individual-General rule
218 $this->assertEquals(count($ids), 2, 'Check Individual-General rule for dupesByParams().');
219
220 // delete all created contacts
221 foreach ($contactIds as $contactId) {
222 Contact::delete($contactId);
223 }
224 }
225 }
226
227
228