Merge pull request #14620 from mattwire/entitypagetrait_defaultcontactid
[civicrm-core.git] / tests / phpunit / CRM / Contact / Page / AjaxTest.php
1 <?php
2
3 /**
4 * @group headless
5 */
6 class CRM_Contact_Page_AjaxTest extends CiviUnitTestCase {
7
8 /**
9 * Original $_REQUEST
10 *
11 * We are messing with globals so fix afterwards.
12 *
13 * @var array
14 */
15 protected $originalRequest = [];
16
17 public function setUp() {
18 $this->useTransaction(TRUE);
19 parent::setUp();
20 $this->originalRequest = $_REQUEST;
21 }
22
23 public function tearDown() {
24 $_REQUEST = $this->originalRequest;
25 parent::tearDown();
26 }
27
28 /**
29 * Minimal test on the testGetDupes function to make sure it completes without error.
30 */
31 public function testGetDedupes() {
32 $_REQUEST['gid'] = 1;
33 $_REQUEST['rgid'] = 1;
34 $_REQUEST['columns'] = [
35 [
36 'search' => [
37 'value' => [
38 'src' => 'first_name',
39 ],
40 ],
41 'data' => 'src',
42 ],
43 ];
44 $_REQUEST['is_unit_test'] = TRUE;
45 $result = CRM_Contact_Page_AJAX::getDedupes();
46 $this->assertEquals(['data' => [], 'recordsTotal' => 0, 'recordsFiltered' => 0], $result);
47 }
48
49 /**
50 * Tests the 'guts' of the processDupes function.
51 *
52 * @throws \Exception
53 */
54 public function testProcessDupes() {
55 $contact1 = $this->individualCreate();
56 $contact2 = $this->individualCreate();
57 $contact3 = $this->individualCreate();
58 CRM_Contact_Page_AJAX::markNonDuplicates($contact1, $contact2, 'dupe-nondupe');
59 CRM_Contact_Page_AJAX::markNonDuplicates($contact3, $contact1, 'dupe-nondupe');
60 $this->callAPISuccessGetSingle('Exception', ['contact_id1' => $contact1, 'contact_id2' => $contact2]);
61 // Note that in saving the order is changed to have the lowest ID first.
62 $this->callAPISuccessGetSingle('Exception', ['contact_id1' => $contact1, 'contact_id2' => $contact3]);
63 }
64
65 public function testGetDedupesPostCode() {
66 $_REQUEST['gid'] = 1;
67 $_REQUEST['rgid'] = 1;
68 $_REQUEST['snippet'] = 4;
69 $_REQUEST['draw'] = 3;
70 $_REQUEST['columns'] = [
71 0 => [
72 'data' => 'is_selected_input',
73 'name' => '',
74 'searchable' => TRUE,
75 'search' => [
76 'value' => '',
77 'regex' => FALSE,
78 ],
79 ],
80
81 1 => [
82 'data' => 'src_image',
83 'name' => '',
84 'searchable' => TRUE,
85 'orderable' => FALSE,
86 'search' => [
87 'value' => '',
88 'regex' => FALSE,
89 ],
90 ],
91
92 2 => [
93 'data' => 'src',
94 'name' => '',
95 'searchable' => TRUE,
96 'orderable' => TRUE,
97 'search' => [
98 'value' => '',
99 'regex' => FALSE,
100 ],
101 ],
102
103 3 => [
104 'data' => 'dst_image',
105 'name' => '',
106 'searchable' => TRUE,
107 'orderable' => FALSE,
108 'search' => [
109 'value' => '',
110 'regex' => FALSE,
111 ],
112 ],
113
114 4 => [
115 'data' => 'dst',
116 'name' => '',
117 'searchable' => TRUE,
118 'orderable' => TRUE,
119 'search' => [
120 'value' => '',
121 'regex' => FALSE,
122 ],
123 ],
124
125 5 => [
126 'data' => 'src_email',
127 'name' => '',
128 'searchable' => TRUE,
129 'orderable' => TRUE,
130 'search' => [
131 'value' => '',
132 'regex' => FALSE,
133 ],
134 ],
135
136 6 => [
137 'data' => 'dst_email',
138 'name' => '',
139 'searchable' => TRUE,
140 'orderable' => TRUE,
141 'search' => [
142 'value' => '',
143 'regex' => FALSE,
144 ],
145 ],
146
147 7 => [
148 'data' => 'src_street',
149 'name' => '',
150 'searchable' => TRUE,
151 'orderable' => TRUE,
152 'search' => [
153 'value' => '',
154 'regex' => FALSE,
155 ],
156 ],
157
158 8 => [
159 'data' => 'dst_street',
160 'name' => '',
161 'searchable' => TRUE,
162 'orderable' => TRUE,
163 'search' => [
164 'value' => '',
165 'regex' => FALSE,
166 ],
167 ],
168
169 9 => [
170 'data' => 'src_postcode',
171 'name' => '',
172 'searchable' => TRUE,
173 'orderable' => TRUE,
174 'search' => [
175 'value' => 123,
176 'regex' => FALSE,
177 ],
178 ],
179
180 10 => [
181 'data' => 'dst_postcode',
182 'name' => '',
183 'searchable' => TRUE,
184 'orderable' => TRUE,
185 'search' => [
186 'value' => '',
187 'regex' => FALSE,
188 ],
189 ],
190
191 11 => [
192 'data' => 'conflicts',
193 'name' => '',
194 'searchable' => TRUE,
195 'orderable' => TRUE,
196 'search' => [
197 'value' => '',
198 'regex' => FALSE,
199 ],
200 ],
201
202 12 => [
203 'data' => 'weight',
204 'name' => '',
205 'searchable' => TRUE,
206 'orderable' => TRUE,
207 'search' => [
208 'value' => '',
209 'regex' => FALSE,
210 ],
211 ],
212
213 13 => [
214 'data' => 'actions',
215 'name' => '',
216 'searchable' => TRUE,
217 'orderable' => FALSE,
218 'search' => [
219 'value' => '',
220 'regex' => FALSE,
221 ],
222 ],
223 ];
224
225 $_REQUEST['start'] = 0;
226 $_REQUEST['length'] = 10;
227 $_REQUEST['search'] = [
228 'value' => '',
229 'regex' => FALSE,
230 ];
231
232 $_REQUEST['_'] = 1466478641007;
233 $_REQUEST['Drupal_toolbar_collapsed'] = 0;
234 $_REQUEST['has_js'] = 1;
235 $_REQUEST['SESSa06550b3043ecca303761d968e3c846a'] = 'qxSxw0F_UmBITMM0JaVwTRcHV1bQqBSHNmBMY9AA8Wk';
236
237 $_REQUEST['is_unit_test'] = TRUE;
238
239 $result = CRM_Contact_Page_AJAX::getDedupes();
240 $this->assertEquals(['data' => [], 'recordsTotal' => 0, 'recordsFiltered' => 0], $result);
241 }
242
243 /**
244 * CRM-20621 : Test to check usage count of Tag tree
245 */
246 public function testGetTagTree() {
247 $contacts = [];
248 // create three contacts
249 for ($i = 0; $i < 3; $i++) {
250 $contacts[] = $this->individualCreate();
251 }
252
253 // Create Tag called as 'Parent Tag'
254 $parentTag = $this->tagCreate([
255 'name' => 'Parent Tag',
256 'used_for' => 'civicrm_contact',
257 ]);
258 //assign first contact to parent tag
259 $params = [
260 'entity_id' => $contacts[0],
261 'entity_table' => 'civicrm_contact',
262 'tag_id' => $parentTag['id'],
263 ];
264 // TODO: EntityTag.create API is not working
265 CRM_Core_BAO_EntityTag::add($params);
266
267 // Create child Tag of $parentTag
268 $childTag1 = $this->tagCreate([
269 'name' => 'Child Tag Level 1',
270 'parent_id' => $parentTag['id'],
271 'used_for' => 'civicrm_contact',
272 ]);
273 //assign contact to this level 1 child tag
274 $params = [
275 'entity_id' => $contacts[1],
276 'entity_table' => 'civicrm_contact',
277 'tag_id' => $childTag1['id'],
278 ];
279 CRM_Core_BAO_EntityTag::add($params);
280
281 // Create child Tag of $childTag1
282 $childTag2 = $this->tagCreate([
283 'name' => 'Child Tag Level 2',
284 'parent_id' => $childTag1['id'],
285 'used_for' => 'civicrm_contact',
286 ]);
287 //assign contact to this level 2 child tag
288 $params = [
289 'entity_id' => $contacts[2],
290 'entity_table' => 'civicrm_contact',
291 'tag_id' => $childTag2['id'],
292 ];
293 CRM_Core_BAO_EntityTag::add($params);
294
295 // CASE I : check the usage count of parent tag which need to be 1
296 // as the one contact added
297 $_REQUEST['is_unit_test'] = TRUE;
298 $parentTagTreeResult = CRM_Admin_Page_AJAX::getTagTree();
299 foreach ($parentTagTreeResult as $result) {
300 if ($result['id'] == $parentTag['id']) {
301 $this->assertEquals(1, $result['data']['usages']);
302 }
303 }
304
305 // CASE 2 : check the usage count of level 1 child tag, which needs to be 1
306 // as it should include the count of added one contact
307 $_GET['parent_id'] = $parentTag['id'];
308 $childTagTree = CRM_Admin_Page_AJAX::getTagTree();
309 $this->assertEquals(1, $childTagTree[0]['data']['usages']);
310
311 // CASE 2 : check the usage count of child tag at level 2
312 //which needs to be 1 as it has no child tag
313 $_GET['parent_id'] = $childTag1['id'];
314 $childTagTree = CRM_Admin_Page_AJAX::getTagTree();
315 $this->assertEquals(1, $childTagTree[0]['data']['usages']);
316
317 // CASE 3 : check the tag IDs returned on searching with 'Level'
318 // which needs to array('parent tag id', 'level 1 child tag id', 'level 2 child tag id')
319 unset($_GET['parent_id']);
320 $_GET['str'] = 'Level';
321 $tagIDs = CRM_Admin_Page_AJAX::getTagTree();
322 $expectedTagIDs = [$parentTag['id'], $childTag1['id'], $childTag2['id']];
323 $this->checkArrayEquals($tagIDs, $expectedTagIDs);
324
325 // CASE 4 : check the tag IDs returned on searching with 'Level 1'
326 // which needs to array('parent tag id', 'level 1 child tag id')
327 $_GET['str'] = 'Level 1';
328 $tagIDs = CRM_Admin_Page_AJAX::getTagTree();
329 $expectedTagIDs = [$parentTag['id'], $childTag1['id']];
330 $this->checkArrayEquals($tagIDs, $expectedTagIDs);
331
332 //cleanup
333 foreach ($contacts as $id) {
334 $this->callAPISuccess('Contact', 'delete', ['id' => $id]);
335 }
336 $this->callAPISuccess('Tag', 'delete', ['id' => $childTag2['id']]);
337 $this->callAPISuccess('Tag', 'delete', ['id' => $childTag1['id']]);
338 $this->callAPISuccess('Tag', 'delete', ['id' => $parentTag['id']]);
339 }
340
341 /**
342 * Test to check contact reference field
343 */
344 public function testContactReference() {
345 //create group
346 $groupId1 = $this->groupCreate();
347 $groupId2 = $this->groupCreate([
348 'name' => 'Test Group 2',
349 'domain_id' => 1,
350 'title' => 'New Test Group2 Created',
351 'description' => 'New Test Group2 Created',
352 'is_active' => 1,
353 'visibility' => 'User and User Admin Only',
354 ]);
355
356 $contactIds = [];
357 foreach ([$groupId1, $groupId2] as $groupId) {
358 $this->groupContactCreate($groupId);
359 $contactIds = array_merge($contactIds, CRM_Contact_BAO_Group::getGroupContacts($groupId));
360 }
361 $contactIds = CRM_Utils_Array::collect('contact_id', $contactIds);
362
363 // create custom group with contact reference field
364 $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'select_test_group']);
365 $params = [
366 'custom_group_id' => $customGroup['id'],
367 'name' => 'Worker_Lookup',
368 'label' => 'Worker Lookup',
369 // limit this field to two groups created above
370 'filter' => "action=lookup&group={$groupId1},{$groupId2}",
371 'html_type' => 'Autocomplete-Select',
372 'data_type' => 'ContactReference',
373 'weight' => 4,
374 'is_searchable' => 1,
375 'is_active' => 1,
376 ];
377 $customField = $this->callAPISuccess('custom_field', 'create', $params);
378
379 $_GET = [
380 'id' => $customField['id'],
381 'is_unit_test' => TRUE,
382 ];
383 $contactList = CRM_Contact_Page_AJAX::contactReference();
384 $contactList = CRM_Utils_Array::collect('id', $contactList);
385
386 //assert each returned contact id to be present in group contact
387 foreach ($contactList as $contactId) {
388 $this->assertTrue(in_array($contactId, $contactIds));
389 }
390 }
391
392 }