From d412bccaece8a89588596797a26b22d630fa3f4f Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Tue, 13 Dec 2016 21:53:34 +0530 Subject: [PATCH] Fix group_type filter in Advance Search and api --- CRM/Contact/BAO/Query.php | 15 ++--- .../CRM/Contact/BAO/GroupContactTest.php | 67 +++++++++++++++++++ 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index c42b024724..cb9a448a34 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -1791,18 +1791,7 @@ class CRM_Contact_BAO_Query { return; case 'group': - $this->group($values); - return; - case 'group_type': - // so we resolve this into a list of groups & proceed as if they had been - // handed in - list($name, $op, $value, $grouping, $wildcard) = $values; - $values[0] = 'group'; - $values[1] = 'IN'; - $this->_paramLookup['group'][0][0] = 'group'; - $this->_paramLookup['group'][0][1] = 'IN'; - $this->_paramLookup['group'][0][2] = $values[2] = $this->getGroupsFromTypeCriteria($value); $this->group($values); return; @@ -2919,6 +2908,10 @@ class CRM_Contact_BAO_Query { $value = CRM_Utils_Array::value($op, $value, $value); } + if ($name == 'group_type') { + $value = array_keys($this->getGroupsFromTypeCriteria($value)); + } + $regularGroupIDs = $smartGroupIDs = array(); foreach ((array) $value as $id) { if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $id, 'saved_search_id')) { diff --git a/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php b/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php index 0485d92c60..c25ffa7ceb 100644 --- a/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php +++ b/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php @@ -229,4 +229,71 @@ class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase { } } + /** + * CRM-19333: Test case for contact search on basis of group type + */ + public function testbyGroupType() { + $groupTypes = CRM_Core_BAO_OptionValue::getOptionValuesAssocArrayFromName('group_type'); + $mailingListGT = array_search('Mailing List', $groupTypes); + $accessControlGT = array_search('Access Control', $groupTypes); + + // create group with group type - Mailing list + $group1 = $this->callAPISuccess('Group', 'create', array( + 'title' => 'Group 1', + 'visibility' => 'User and User Admin Only', + 'is_active' => 1, + 'group_type' => $mailingListGT, + )); + + // create group with group type - Access Control + $group2 = $this->callAPISuccess('Group', 'create', array( + 'title' => 'Group 2', + 'visibility' => 'User and User Admin Only', + 'is_active' => 1, + 'group_type' => $accessControlGT, + )); + + // create contact in 'Group 1' + $contact1 = $this->individualCreate(array( + 'group' => array($group1['id'] => 1), + )); + + // create contact in 'Group 2' + $contact2 = $this->individualCreate(array( + 'group' => array($group2['id'] => 1), + ), 1); + + $useCases = array( + //Case 1: Find contacts in group type - Mailing List + array( + 'form_value' => array('group_type' => array($mailingListGT)), + 'expected_count' => 1, + 'expected_contact' => array($contact1), + ), + //Case 2: Find contacts in group type - Access Control + array( + 'form_value' => array('group_type' => array($accessControlGT)), + 'expected_count' => 1, + 'expected_contact' => array($contact2), + ), + //Case 3: Find contacts in group type - Mailing List or Access List + array( + 'form_value' => array('group_type' => array($mailingListGT, $accessControlGT)), + 'expected_count' => 2, + 'expected_contact' => array($contact1, $contact2), + ), + ); + + foreach ($useCases as $case) { + $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value'])); + list($select, $from, $where, $having) = $query->query(); + $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll(); + foreach ($groupContacts as $key => $value) { + $groupContacts[$key] = $value['id']; + } + $this->assertEquals($case['expected_count'], count($groupContacts)); + $this->checkArrayEquals($case['expected_contact'], $groupContacts); + } + } + } -- 2.25.1