6 class CRM_Contact_Page_AjaxTest
extends CiviUnitTestCase
{
9 public function setUp() {
10 $this->useTransaction(TRUE);
15 * Minimal test on the testGetDupes function to make sure it completes without error.
17 public function testGetDedupes() {
19 $_REQUEST['rgid'] = 1;
20 $_REQUEST['columns'] = array(
24 'src' => 'first_name',
30 $_REQUEST['is_unit_test'] = TRUE;
31 $result = CRM_Contact_Page_AJAX
::getDedupes();
32 $this->assertEquals(array('data' => array(), 'recordsTotal' => 0, 'recordsFiltered' => 0), $result);
35 public function testGetDedupesPostCode() {
37 $_REQUEST['rgid'] = 1;
38 $_REQUEST['snippet'] = 4;
39 $_REQUEST['draw'] = 3;
40 $_REQUEST['columns'] = array(
42 'data' => 'is_selected_input',
52 'data' => 'src_image',
74 'data' => 'dst_image',
96 'data' => 'src_email',
107 'data' => 'dst_email',
109 'searchable' => TRUE,
118 'data' => 'src_street',
120 'searchable' => TRUE,
129 'data' => 'dst_street',
131 'searchable' => TRUE,
140 'data' => 'src_postcode',
142 'searchable' => TRUE,
151 'data' => 'dst_postcode',
153 'searchable' => TRUE,
162 'data' => 'conflicts',
164 'searchable' => TRUE,
175 'searchable' => TRUE,
186 'searchable' => TRUE,
187 'orderable' => FALSE,
195 $_REQUEST['start'] = 0;
196 $_REQUEST['length'] = 10;
197 $_REQUEST['search'] = array(
202 $_REQUEST['_'] = 1466478641007;
203 $_REQUEST['Drupal_toolbar_collapsed'] = 0;
204 $_REQUEST['has_js'] = 1;
205 $_REQUEST['SESSa06550b3043ecca303761d968e3c846a'] = 'qxSxw0F_UmBITMM0JaVwTRcHV1bQqBSHNmBMY9AA8Wk';
207 $_REQUEST['is_unit_test'] = TRUE;
209 $result = CRM_Contact_Page_AJAX
::getDedupes();
210 $this->assertEquals(array('data' => array(), 'recordsTotal' => 0, 'recordsFiltered' => 0), $result);
214 * CRM-20621 : Test to check usage count of Tag tree
216 public function testGetTagTree() {
218 // create three contacts
219 for ($i = 0; $i < 3; $i++
) {
220 $contacts[] = $this->individualCreate();
223 // Create Tag called as 'Parent Tag'
224 $parentTag = $this->tagCreate(array(
225 'name' => 'Parent Tag',
226 'used_for' => 'civicrm_contact',
228 //assign first contact to parent tag
230 'entity_id' => $contacts[0],
231 'entity_table' => 'civicrm_contact',
232 'tag_id' => $parentTag['id'],
234 // TODO: EntityTag.create API is not working
235 CRM_Core_BAO_EntityTag
::add($params);
237 // Create child Tag of $parentTag
238 $childTag1 = $this->tagCreate(array(
239 'name' => 'Child Tag Level 1',
240 'parent_id' => $parentTag['id'],
241 'used_for' => 'civicrm_contact',
243 //assign contact to this level 1 child tag
245 'entity_id' => $contacts[1],
246 'entity_table' => 'civicrm_contact',
247 'tag_id' => $childTag1['id'],
249 CRM_Core_BAO_EntityTag
::add($params);
251 // Create child Tag of $childTag1
252 $childTag2 = $this->tagCreate(array(
253 'name' => 'Child Tag Level 2',
254 'parent_id' => $childTag1['id'],
255 'used_for' => 'civicrm_contact',
257 //assign contact to this level 2 child tag
259 'entity_id' => $contacts[2],
260 'entity_table' => 'civicrm_contact',
261 'tag_id' => $childTag2['id'],
263 CRM_Core_BAO_EntityTag
::add($params);
265 // CASE I : check the usage count of parent tag which need to be 1
266 // as the one contact added
267 $_REQUEST['is_unit_test'] = TRUE;
268 $parentTagTreeResult = CRM_Admin_Page_AJAX
::getTagTree();
269 foreach ($parentTagTreeResult as $result) {
270 if ($result['id'] == $parentTag['id']) {
271 $this->assertEquals(1, $result['data']['usages']);
275 // CASE 2 : check the usage count of level 1 child tag, which needs to be 1
276 // as it should include the count of added one contact
277 $_GET['parent_id'] = $parentTag['id'];
278 $childTagTree = CRM_Admin_Page_AJAX
::getTagTree();
279 $this->assertEquals(1, $childTagTree[0]['data']['usages']);
281 // CASE 2 : check the usage count of child tag at level 2
282 //which needs to be 1 as it has no child tag
283 $_GET['parent_id'] = $childTag1['id'];
284 $childTagTree = CRM_Admin_Page_AJAX
::getTagTree();
285 $this->assertEquals(1, $childTagTree[0]['data']['usages']);
288 foreach ($contacts as $id) {
289 $this->callAPISuccess('Contact', 'delete', array('id' => $id));
291 $this->callAPISuccess('Tag', 'delete', array('id' => $childTag2['id']));
292 $this->callAPISuccess('Tag', 'delete', array('id' => $childTag1['id']));
293 $this->callAPISuccess('Tag', 'delete', array('id' => $parentTag['id']));
297 * Test to check contact reference field
299 public function testContactReference() {
301 $groupId1 = $this->groupCreate();
302 $groupId2 = $this->groupCreate(array(
303 'name' => 'Test Group 2',
305 'title' => 'New Test Group2 Created',
306 'description' => 'New Test Group2 Created',
308 'visibility' => 'User and User Admin Only',
311 $contactIds = array();
312 foreach (array($groupId1, $groupId2) as $groupId) {
313 $this->groupContactCreate($groupId);
314 $contactIds = array_merge($contactIds, CRM_Contact_BAO_Group
::getGroupContacts($groupId));
316 $contactIds = CRM_Utils_Array
::collect('contact_id', $contactIds);
318 // create custom group with contact reference field
319 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
321 'custom_group_id' => $customGroup['id'],
322 'name' => 'Worker_Lookup',
323 'label' => 'Worker Lookup',
324 // limit this field to two groups created above
325 'filter' => "action=lookup&group={$groupId1},{$groupId2}",
326 'html_type' => 'Autocomplete-Select',
327 'data_type' => 'ContactReference',
329 'is_searchable' => 1,
332 $customField = $this->callAPISuccess('custom_field', 'create', $params);
335 'id' => $customField['id'],
336 'is_unit_test' => TRUE,
338 $contactList = CRM_Contact_Page_AJAX
::contactReference();
339 $contactList = CRM_Utils_Array
::collect('id', $contactList);
341 //assert each returned contact id to be present in group contact
342 foreach ($contactList as $contactId) {
343 $this->assertTrue(in_array($contactId, $contactIds));