From da0136df570d7d3d1a0a2109f42575e5de2493df Mon Sep 17 00:00:00 2001 From: jitendrapurohit Date: Tue, 13 Sep 2016 16:56:11 +0530 Subject: [PATCH] CRM-19702 - Fix contact reference field filter --- CRM/Contact/BAO/Group.php | 2 +- CRM/Contact/Page/AJAX.php | 12 +++-- tests/phpunit/CRM/Contact/Page/AjaxTest.php | 51 +++++++++++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index 15aa5fa5cb..b3f3134cd6 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -130,7 +130,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { * @param int $id */ public static function getGroupContacts($id) { - $params = array(array('group', 'IN', array($id => 1), 0, 0)); + $params = array(array('group', 'IN', array(1 => $id), 0, 0)); list($contacts, $_) = CRM_Contact_BAO_Query::apiQuery($params, array('contact_id')); return $contacts; } diff --git a/CRM/Contact/Page/AJAX.php b/CRM/Contact/Page/AJAX.php index 833d4e8879..342e4883c8 100644 --- a/CRM/Contact/Page/AJAX.php +++ b/CRM/Contact/Page/AJAX.php @@ -69,12 +69,13 @@ class CRM_Contact_Page_AJAX { parse_str($cf['filter'], $filterParams); $action = CRM_Utils_Array::value('action', $filterParams); - - if (!empty($action) && - !in_array($action, array('get', 'lookup')) - ) { + if (!empty($action) && !in_array($action, array('get', 'lookup'))) { CRM_Utils_System::civiExit('error'); } + + if (!empty($filterParams['group'])) { + $filterParams['group'] = explode(',', $filterParams['group']); + } } $list = array_keys(CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, @@ -149,6 +150,9 @@ class CRM_Contact_Page_AJAX { $contactList[] = array('id' => $value['id'], 'text' => implode(' :: ', $view)); } + if (!empty($_GET['is_unit_test'])) { + return $contactList; + } CRM_Utils_JSON::output($contactList); } diff --git a/tests/phpunit/CRM/Contact/Page/AjaxTest.php b/tests/phpunit/CRM/Contact/Page/AjaxTest.php index 6b98734914..353c6cb423 100644 --- a/tests/phpunit/CRM/Contact/Page/AjaxTest.php +++ b/tests/phpunit/CRM/Contact/Page/AjaxTest.php @@ -210,4 +210,55 @@ class CRM_Contact_Page_AjaxTest extends CiviUnitTestCase { $this->assertEquals(array('data' => array(), 'recordsTotal' => 0, 'recordsFiltered' => 0), $result); } + /** + * Test to check contact reference field + */ + public function testContactReference() { + //create group + $groupId1 = $this->groupCreate(); + $groupId2 = $this->groupCreate(array( + 'name' => 'Test Group 2', + 'domain_id' => 1, + 'title' => 'New Test Group2 Created', + 'description' => 'New Test Group2 Created', + 'is_active' => 1, + 'visibility' => 'User and User Admin Only', + )); + + $contactIds = array(); + foreach (array($groupId1, $groupId2) as $groupId) { + $this->groupContactCreate($groupId); + $contactIds = array_merge($contactIds, CRM_Contact_BAO_Group::getGroupContacts($groupId)); + } + $contactIds = array_column($contactIds, 'contact_id'); + + // create custom group with contact reference field + $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group')); + $params = array( + 'custom_group_id' => $customGroup['id'], + 'name' => 'Worker_Lookup', + 'label' => 'Worker Lookup', + // limit this field to two groups created above + 'filter' => "action=lookup&group={$groupId1},{$groupId2}", + 'html_type' => 'Autocomplete-Select', + 'data_type' => 'ContactReference', + 'weight' => 4, + 'is_searchable' => 1, + 'is_active' => 1, + ); + $customField = $this->callAPISuccess('custom_field', 'create', $params); + + $_GET = array( + 'id' => $customField['id'], + 'is_unit_test' => TRUE, + ); + $contactList = CRM_Contact_Page_AJAX::contactReference(); + $contactList = array_column($contactList, 'id'); + + //assert each returned contact id to be present in group contact + foreach ($contactList as $contactId) { + $this->assertTrue(in_array($contactId, $contactIds)); + } + } + } -- 2.25.1