Fix group_type filter in Advance Search and api
authordeb.monish <monish.deb@webaccessglobal.com>
Tue, 13 Dec 2016 16:23:34 +0000 (21:53 +0530)
committerdeb.monish <monish.deb@webaccessglobal.com>
Tue, 13 Dec 2016 17:30:58 +0000 (23:00 +0530)
CRM/Contact/BAO/Query.php
tests/phpunit/CRM/Contact/BAO/GroupContactTest.php

index c42b024724908cf12a4725d8fae2aa43cd438c3f..cb9a448a34519991bc45da806f37edbee4d4221c 100644 (file)
@@ -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')) {
index 0485d92c6030b5da1821c4dac4f90c629aa4d409..c25ffa7cebea0147798b056721c7369deb388e2c 100644 (file)
@@ -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);
+    }
+  }
+
 }