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'] = array(
38 'src' => 'first_name',
44 $_REQUEST['is_unit_test'] = TRUE;
45 $result = CRM_Contact_Page_AJAX
::getDedupes();
46 $this->assertEquals(array('data' => array(), 'recordsTotal' => 0, 'recordsFiltered' => 0), $result);
49 public function testGetDedupesPostCode() {
51 $_REQUEST['rgid'] = 1;
52 $_REQUEST['snippet'] = 4;
53 $_REQUEST['draw'] = 3;
54 $_REQUEST['columns'] = array(
56 'data' => 'is_selected_input',
66 'data' => 'src_image',
88 'data' => 'dst_image',
101 'searchable' => TRUE,
110 'data' => 'src_email',
112 'searchable' => TRUE,
121 'data' => 'dst_email',
123 'searchable' => TRUE,
132 'data' => 'src_street',
134 'searchable' => TRUE,
143 'data' => 'dst_street',
145 'searchable' => TRUE,
154 'data' => 'src_postcode',
156 'searchable' => TRUE,
165 'data' => 'dst_postcode',
167 'searchable' => TRUE,
176 'data' => 'conflicts',
178 'searchable' => TRUE,
189 'searchable' => TRUE,
200 'searchable' => TRUE,
201 'orderable' => FALSE,
209 $_REQUEST['start'] = 0;
210 $_REQUEST['length'] = 10;
211 $_REQUEST['search'] = array(
216 $_REQUEST['_'] = 1466478641007;
217 $_REQUEST['Drupal_toolbar_collapsed'] = 0;
218 $_REQUEST['has_js'] = 1;
219 $_REQUEST['SESSa06550b3043ecca303761d968e3c846a'] = 'qxSxw0F_UmBITMM0JaVwTRcHV1bQqBSHNmBMY9AA8Wk';
221 $_REQUEST['is_unit_test'] = TRUE;
223 $result = CRM_Contact_Page_AJAX
::getDedupes();
224 $this->assertEquals(array('data' => array(), 'recordsTotal' => 0, 'recordsFiltered' => 0), $result);
228 * CRM-20621 : Test to check usage count of Tag tree
230 public function testGetTagTree() {
232 // create three contacts
233 for ($i = 0; $i < 3; $i++
) {
234 $contacts[] = $this->individualCreate();
237 // Create Tag called as 'Parent Tag'
238 $parentTag = $this->tagCreate(array(
239 'name' => 'Parent Tag',
240 'used_for' => 'civicrm_contact',
242 //assign first contact to parent tag
244 'entity_id' => $contacts[0],
245 'entity_table' => 'civicrm_contact',
246 'tag_id' => $parentTag['id'],
248 // TODO: EntityTag.create API is not working
249 CRM_Core_BAO_EntityTag
::add($params);
251 // Create child Tag of $parentTag
252 $childTag1 = $this->tagCreate(array(
253 'name' => 'Child Tag Level 1',
254 'parent_id' => $parentTag['id'],
255 'used_for' => 'civicrm_contact',
257 //assign contact to this level 1 child tag
259 'entity_id' => $contacts[1],
260 'entity_table' => 'civicrm_contact',
261 'tag_id' => $childTag1['id'],
263 CRM_Core_BAO_EntityTag
::add($params);
265 // Create child Tag of $childTag1
266 $childTag2 = $this->tagCreate(array(
267 'name' => 'Child Tag Level 2',
268 'parent_id' => $childTag1['id'],
269 'used_for' => 'civicrm_contact',
271 //assign contact to this level 2 child tag
273 'entity_id' => $contacts[2],
274 'entity_table' => 'civicrm_contact',
275 'tag_id' => $childTag2['id'],
277 CRM_Core_BAO_EntityTag
::add($params);
279 // CASE I : check the usage count of parent tag which need to be 1
280 // as the one contact added
281 $_REQUEST['is_unit_test'] = TRUE;
282 $parentTagTreeResult = CRM_Admin_Page_AJAX
::getTagTree();
283 foreach ($parentTagTreeResult as $result) {
284 if ($result['id'] == $parentTag['id']) {
285 $this->assertEquals(1, $result['data']['usages']);
289 // CASE 2 : check the usage count of level 1 child tag, which needs to be 1
290 // as it should include the count of added one contact
291 $_GET['parent_id'] = $parentTag['id'];
292 $childTagTree = CRM_Admin_Page_AJAX
::getTagTree();
293 $this->assertEquals(1, $childTagTree[0]['data']['usages']);
295 // CASE 2 : check the usage count of child tag at level 2
296 //which needs to be 1 as it has no child tag
297 $_GET['parent_id'] = $childTag1['id'];
298 $childTagTree = CRM_Admin_Page_AJAX
::getTagTree();
299 $this->assertEquals(1, $childTagTree[0]['data']['usages']);
301 // CASE 3 : check the tag IDs returned on searching with 'Level'
302 // which needs to array('parent tag id', 'level 1 child tag id', 'level 2 child tag id')
303 unset($_GET['parent_id']);
304 $_GET['str'] = 'Level';
305 $tagIDs = CRM_Admin_Page_AJAX
::getTagTree();
306 $expectedTagIDs = array($parentTag['id'], $childTag1['id'], $childTag2['id']);
307 $this->checkArrayEquals($tagIDs, $expectedTagIDs);
309 // CASE 4 : check the tag IDs returned on searching with 'Level 1'
310 // which needs to array('parent tag id', 'level 1 child tag id')
311 $_GET['str'] = 'Level 1';
312 $tagIDs = CRM_Admin_Page_AJAX
::getTagTree();
313 $expectedTagIDs = array($parentTag['id'], $childTag1['id']);
314 $this->checkArrayEquals($tagIDs, $expectedTagIDs);
317 foreach ($contacts as $id) {
318 $this->callAPISuccess('Contact', 'delete', array('id' => $id));
320 $this->callAPISuccess('Tag', 'delete', array('id' => $childTag2['id']));
321 $this->callAPISuccess('Tag', 'delete', array('id' => $childTag1['id']));
322 $this->callAPISuccess('Tag', 'delete', array('id' => $parentTag['id']));
326 * Test to check contact reference field
328 public function testContactReference() {
330 $groupId1 = $this->groupCreate();
331 $groupId2 = $this->groupCreate(array(
332 'name' => 'Test Group 2',
334 'title' => 'New Test Group2 Created',
335 'description' => 'New Test Group2 Created',
337 'visibility' => 'User and User Admin Only',
340 $contactIds = array();
341 foreach (array($groupId1, $groupId2) as $groupId) {
342 $this->groupContactCreate($groupId);
343 $contactIds = array_merge($contactIds, CRM_Contact_BAO_Group
::getGroupContacts($groupId));
345 $contactIds = CRM_Utils_Array
::collect('contact_id', $contactIds);
347 // create custom group with contact reference field
348 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
350 'custom_group_id' => $customGroup['id'],
351 'name' => 'Worker_Lookup',
352 'label' => 'Worker Lookup',
353 // limit this field to two groups created above
354 'filter' => "action=lookup&group={$groupId1},{$groupId2}",
355 'html_type' => 'Autocomplete-Select',
356 'data_type' => 'ContactReference',
358 'is_searchable' => 1,
361 $customField = $this->callAPISuccess('custom_field', 'create', $params);
364 'id' => $customField['id'],
365 'is_unit_test' => TRUE,
367 $contactList = CRM_Contact_Page_AJAX
::contactReference();
368 $contactList = CRM_Utils_Array
::collect('id', $contactList);
370 //assert each returned contact id to be present in group contact
371 foreach ($contactList as $contactId) {
372 $this->assertTrue(in_array($contactId, $contactIds));