add sort order
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / GroupContactTest.php
index 5dec7669fb5cf74755d614b186f0a02c46a29a76..9bc3f107dd0f45dc2bce9d9a681f48724f7bc4b8 100644 (file)
@@ -155,4 +155,150 @@ class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase {
     $this->assertEquals(array(), array_diff($validChildContactIds, $resultChildContactIds), 'Check that the difference between two arrays should be blank array');
   }
 
+
+  /**
+   *  CRM-19698: Test case for combine contact search in regular and smart group
+   */
+  public function testContactCombineGroupSearch() {
+    // create regular group based
+    $regularGroup = $this->callAPISuccess('Group', 'create', array(
+      'title' => 'Regular Group',
+      'description' => 'Regular Group',
+      'visibility' => 'User and User Admin Only',
+      'is_active' => 1,
+    ));
+
+    // Create contact with Gender - Male
+    $contact1 = $this->individualCreate(array(
+      'gender_id' => "Male",
+      'first_name' => 'A',
+    ));
+
+    // Create contact with Gender - Male and in regular group
+    $contact2 = $this->individualCreate(array(
+      'group' => array($regularGroup['id'] => 1),
+      'gender_id' => "Male",
+      'first_name' => 'B',
+    ), 1);
+
+    // Create contact with Gender - Female and in regular group
+    $contact3 = $this->individualCreate(array(
+      'group' => array($regularGroup['id'] => 1),
+      'gender_id' => "Female",
+      'first_name' => 'C',
+    ), 1);
+
+    // create smart group based on saved criteria Gender = Male
+    $batch = $this->callAPISuccess('SavedSearch', 'create', array(
+      'form_values' => 'a:1:{i:0;a:5:{i:0;s:9:"gender_id";i:1;s:1:"=";i:2;i:2;i:3;i:0;i:4;i:0;}}',
+    ));
+    $smartGroup = $this->callAPISuccess('Group', 'create', array(
+      'title' => 'Smart Group',
+      'description' => 'Smart Group',
+      'visibility' => 'User and User Admin Only',
+      'saved_search_id' => $batch['id'],
+      'is_active' => 1,
+    ));
+
+    $useCases = array(
+      //Case 1: Find all contacts in regular group
+      array(
+        'form_value' => array('group' => $regularGroup['id']),
+        'expected_count' => 2,
+        'expected_contact' => array($contact2, $contact3),
+      ),
+      //Case 2: Find all contacts in smart group
+      array(
+        'form_value' => array('group' => $smartGroup['id']),
+        'expected_count' => 2,
+        'expected_contact' => array($contact1, $contact2),
+      ),
+      //Case 3: Find all contacts in regular group and smart group
+      array(
+        'form_value' => array('group' => array('IN' => array($regularGroup['id'], $smartGroup['id']))),
+        'expected_count' => 3,
+        'expected_contact' => array($contact1, $contact2, $contact3),
+      ),
+    );
+    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.* $from $where ORDER BY contact_a.first_name")->fetchAll();
+      foreach ($groupContacts as $key => $value) {
+        $groupContacts[$key] = $value['id'];
+      }
+      $this->assertEquals($case['expected_count'], count($groupContacts));
+      $this->checkArrayEquals($case['expected_contact'], $groupContacts);
+    }
+  }
+
+  /**
+   *  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),
+      'first_name' => 'A',
+    ));
+
+    // create contact in 'Group 2'
+    $contact2 = $this->individualCreate(array(
+      'group' => array($group2['id'] => 1),
+      'first_name' => 'B',
+    ), 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 ORDER BY contact_a.first_name")->fetchAll();
+      foreach ($groupContacts as $key => $value) {
+        $groupContacts[$key] = $value['id'];
+      }
+      $this->assertEquals($case['expected_count'], count($groupContacts));
+      $this->checkArrayEquals($case['expected_contact'], $groupContacts);
+    }
+  }
+
 }