6 class CRM_Contact_Page_AjaxTest
extends CiviUnitTestCase
{
11 * We are messing with globals so fix afterwards.
15 protected $originalRequest = [];
17 public function setUp() {
18 $this->useTransaction(TRUE);
20 $this->originalRequest
= $_REQUEST;
23 public function tearDown() {
24 $_REQUEST = $this->originalRequest
;
29 * Minimal test on the testGetDupes function to make sure it completes without error.
31 public function testGetDedupes() {
33 $_REQUEST['rgid'] = 1;
34 $_REQUEST['columns'] = [
38 'src' => 'first_name',
44 $_REQUEST['is_unit_test'] = TRUE;
45 $result = CRM_Contact_Page_AJAX
::getDedupes();
46 $this->assertEquals(['data' => [], 'recordsTotal' => 0, 'recordsFiltered' => 0], $result);
50 * Tests the 'guts' of the processDupes function.
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]);
65 public function testGetDedupesPostCode() {
67 $_REQUEST['rgid'] = 1;
68 $_REQUEST['snippet'] = 4;
69 $_REQUEST['draw'] = 3;
70 $_REQUEST['columns'] = [
72 'data' => 'is_selected_input',
82 'data' => 'src_image',
104 'data' => 'dst_image',
106 'searchable' => TRUE,
107 'orderable' => FALSE,
117 'searchable' => TRUE,
126 'data' => 'src_email',
128 'searchable' => TRUE,
137 'data' => 'dst_email',
139 'searchable' => TRUE,
148 'data' => 'src_street',
150 'searchable' => TRUE,
159 'data' => 'dst_street',
161 'searchable' => TRUE,
170 'data' => 'src_postcode',
172 'searchable' => TRUE,
181 'data' => 'dst_postcode',
183 'searchable' => TRUE,
192 'data' => 'conflicts',
194 'searchable' => TRUE,
205 'searchable' => TRUE,
216 'searchable' => TRUE,
217 'orderable' => FALSE,
225 $_REQUEST['start'] = 0;
226 $_REQUEST['length'] = 10;
227 $_REQUEST['search'] = [
232 $_REQUEST['_'] = 1466478641007;
233 $_REQUEST['Drupal_toolbar_collapsed'] = 0;
234 $_REQUEST['has_js'] = 1;
235 $_REQUEST['SESSa06550b3043ecca303761d968e3c846a'] = 'qxSxw0F_UmBITMM0JaVwTRcHV1bQqBSHNmBMY9AA8Wk';
237 $_REQUEST['is_unit_test'] = TRUE;
239 $result = CRM_Contact_Page_AJAX
::getDedupes();
240 $this->assertEquals(['data' => [], 'recordsTotal' => 0, 'recordsFiltered' => 0], $result);
244 * CRM-20621 : Test to check usage count of Tag tree
246 public function testGetTagTree() {
248 // create three contacts
249 for ($i = 0; $i < 3; $i++
) {
250 $contacts[] = $this->individualCreate();
253 // Create Tag called as 'Parent Tag'
254 $parentTag = $this->tagCreate([
255 'name' => 'Parent Tag',
256 'used_for' => 'civicrm_contact',
258 //assign first contact to parent tag
260 'entity_id' => $contacts[0],
261 'entity_table' => 'civicrm_contact',
262 'tag_id' => $parentTag['id'],
264 // TODO: EntityTag.create API is not working
265 CRM_Core_BAO_EntityTag
::add($params);
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',
273 //assign contact to this level 1 child tag
275 'entity_id' => $contacts[1],
276 'entity_table' => 'civicrm_contact',
277 'tag_id' => $childTag1['id'],
279 CRM_Core_BAO_EntityTag
::add($params);
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',
287 //assign contact to this level 2 child tag
289 'entity_id' => $contacts[2],
290 'entity_table' => 'civicrm_contact',
291 'tag_id' => $childTag2['id'],
293 CRM_Core_BAO_EntityTag
::add($params);
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']);
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']);
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']);
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);
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);
333 foreach ($contacts as $id) {
334 $this->callAPISuccess('Contact', 'delete', ['id' => $id]);
336 $this->callAPISuccess('Tag', 'delete', ['id' => $childTag2['id']]);
337 $this->callAPISuccess('Tag', 'delete', ['id' => $childTag1['id']]);
338 $this->callAPISuccess('Tag', 'delete', ['id' => $parentTag['id']]);
342 * Test to check contact reference field
344 public function testContactReference() {
346 $groupId1 = $this->groupCreate();
347 $groupId2 = $this->groupCreate([
348 'name' => 'Test Group 2',
350 'title' => 'New Test Group2 Created',
351 'description' => 'New Test Group2 Created',
353 'visibility' => 'User and User Admin Only',
357 foreach ([$groupId1, $groupId2] as $groupId) {
358 $this->groupContactCreate($groupId);
359 $contactIds = array_merge($contactIds, CRM_Contact_BAO_Group
::getGroupContacts($groupId));
361 $contactIds = CRM_Utils_Array
::collect('contact_id', $contactIds);
363 // create custom group with contact reference field
364 $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'select_test_group']);
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',
374 'is_searchable' => 1,
377 $customField = $this->callAPISuccess('custom_field', 'create', $params);
380 'id' => $customField['id'],
381 'is_unit_test' => TRUE,
383 $contactList = CRM_Contact_Page_AJAX
::contactReference();
384 $contactList = CRM_Utils_Array
::collect('id', $contactList);
386 //assert each returned contact id to be present in group contact
387 foreach ($contactList as $contactId) {
388 $this->assertTrue(in_array($contactId, $contactIds));