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