add sort order
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / GroupContactTest.php
index 719948faeb3f14a591480f6b582aab8d26a2a204..9bc3f107dd0f45dc2bce9d9a681f48724f7bc4b8 100644 (file)
@@ -25,8 +25,6 @@
  +--------------------------------------------------------------------+
  */
 
-require_once 'CiviTest/Contact.php';
-
 /**
  * Test class for CRM_Contact_BAO_GroupContact BAO
  *
@@ -96,48 +94,45 @@ class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase {
    */
   public function testContactSearchByParentGroup() {
     // create a parent group
-    // TODO: This is not an API test!!
     $groupParams1 = array(
       'title' => 'Parent Group',
       'description' => 'Parent Group',
       'visibility' => 'User and User Admin Only',
-      'parents' => '',
       'is_active' => 1,
     );
-    $parentGroup = CRM_Contact_BAO_Group::create($groupParams1);
+    $parentGroup = $this->callAPISuccess('Group', 'create', $groupParams1);
 
     // create a child group
     $groupParams2 = array(
       'title' => 'Child Group',
       'description' => 'Child Group',
       'visibility' => 'User and User Admin Only',
-      'parents' => $parentGroup->id,
+      'parents' => $parentGroup['id'],
       'is_active' => 1,
     );
-    $childGroup = CRM_Contact_BAO_Group::create($groupParams2);
+    $childGroup = $this->callAPISuccess('Group', 'create', $groupParams2);
 
     // Create a contact within parent group
     $parentContactParams = array(
       'first_name' => 'Parent1 Fname',
       'last_name' => 'Parent1 Lname',
-      'group' => array($parentGroup->id => 1),
+      'group' => array($parentGroup['id'] => 1),
     );
-    $parentContact = Contact::createIndividual($parentContactParams);
+    $parentContact = $this->individualCreate($parentContactParams);
 
     // create a contact within child dgroup
     $childContactParams = array(
       'first_name' => 'Child1 Fname',
       'last_name' => 'Child2 Lname',
-      'group' => array($childGroup->id => 1),
+      'group' => array($childGroup['id'] => 1),
     );
-    $childContact = Contact::createIndividual($childContactParams);
+    $childContact = $this->individualCreate($childContactParams);
 
     // Check if searching by parent group  returns both parent and child group contacts
     $searchParams = array(
-      'group' => $parentGroup->id,
-      'version' => 3,
+      'group' => $parentGroup['id'],
     );
-    $result = civicrm_api('contact', 'get', $searchParams);
+    $result = $this->callAPISuccess('contact', 'get', $searchParams);
     $validContactIds = array($parentContact, $childContact);
     $resultContactIds = array();
     foreach ($result['values'] as $k => $v) {
@@ -148,10 +143,9 @@ class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase {
 
     // Check if searching by child group returns just child group contacts
     $searchParams = array(
-      'group' => $childGroup->id,
-      'version' => 3,
+      'group' => $childGroup['id'],
     );
-    $result = civicrm_api('contact', 'get', $searchParams);
+    $result = $this->callAPISuccess('contact', 'get', $searchParams);
     $validChildContactIds = array($childContact);
     $resultChildContactIds = array();
     foreach ($result['values'] as $k => $v) {
@@ -161,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);
+    }
+  }
+
 }