GroupContactTest.php 12.9 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
4
 | CiviCRM version 5                                                  |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
Seamus Lee's avatar
Seamus Lee committed
6
 | Copyright CiviCRM LLC (c) 2004-2019                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29 30

/**
 * Test class for CRM_Contact_BAO_GroupContact BAO
 *
totten's avatar
totten committed
31
 * @package   CiviCRM
32
 * @group headless
totten's avatar
totten committed
33 34 35 36 37 38 39 40 41 42 43 44 45
 */
class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase {

  /**
   * Sets up the fixture, for example, opens a network connection.
   * This method is called before a test is executed.
   */
  protected function setUp() {
    parent::setUp();
  }

  /**
   * Tears down the fixture, for example, closes a network connection.
46
   *
totten's avatar
totten committed
47 48
   * This method is called after a test is executed.
   */
totten's avatar
totten committed
49 50
  protected function tearDown() {
  }
totten's avatar
totten committed
51 52

  /**
53
   * Test case for add( ).
totten's avatar
totten committed
54
   */
55
  public function testAdd() {
totten's avatar
totten committed
56 57 58 59 60 61

    //creates a test group contact by recursively creation
    //lets create 10 groupContacts for fun
    $groupContacts = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_GroupContact', NULL, 10);

    //check the group contact id is not null for each of them
totten's avatar
totten committed
62 63
    foreach ($groupContacts as $gc) {
      $this->assertNotNull($gc->id);
totten's avatar
totten committed
64
    }
totten's avatar
totten committed
65 66

    //cleanup
totten's avatar
totten committed
67 68
    foreach ($groupContacts as $gc) {
      $gc->deleteTestObjects('CRM_Contact_DAO_GroupContact');
totten's avatar
totten committed
69
    }
totten's avatar
totten committed
70 71 72
  }

  /**
colemanw's avatar
colemanw committed
73
   * Test case for getGroupId( )
totten's avatar
totten committed
74
   */
75
  public function testGetGroupId() {
totten's avatar
totten committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

    //creates a test groupContact object
    //force group_id to 1 so we can compare
    $groupContact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_GroupContact');

    //check the group contact id is not null
    $this->assertNotNull($groupContact->id);

    $groupId = CRM_Core_DAO::singleValueQuery('select max(id) from civicrm_group');

    $this->assertEquals($groupContact->group_id, $groupId, 'Check for group_id');

    //cleanup
    $groupContact->deleteTestObjects('CRM_Contact_DAO_GroupContact');
  }

  /**
   *  Test case for contact search: CRM-6706, CRM-6586 Parent Group search should return contacts from child groups too.
   */
95
  public function testContactSearchByParentGroup() {
totten's avatar
totten committed
96
    // create a parent group
97
    $parentGroup = $this->callAPISuccess('Group', 'create', array(
totten's avatar
totten committed
98 99 100 101
      'title' => 'Parent Group',
      'description' => 'Parent Group',
      'visibility' => 'User and User Admin Only',
      'is_active' => 1,
102
    ));
totten's avatar
totten committed
103 104

    // create a child group
105
    $childGroup = $this->callAPISuccess('Group', 'create', array(
totten's avatar
totten committed
106 107 108
      'title' => 'Child Group',
      'description' => 'Child Group',
      'visibility' => 'User and User Admin Only',
109
      'parents' => $parentGroup['id'],
totten's avatar
totten committed
110
      'is_active' => 1,
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    ));

    // 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;}}',
    ));
    // Create contact with Gender - Male
    $childSmartGroupContact = $this->individualCreate(array(
      'gender_id' => "Male",
      'first_name' => 'C',
    ), 1);
    // then create smart group
    $childSmartGroup = $this->callAPISuccess('Group', 'create', array(
      'title' => 'Child Smart Group',
      'description' => 'Child Smart Group',
      'visibility' => 'User and User Admin Only',
      'saved_search_id' => $batch['id'],
      'is_active' => 1,
      'parents' => $parentGroup['id'],
    ));

    $this->callAPISuccess('Group', 'create', array(
      'id' => $parentGroup['id'],
      'children' => implode(',', array($childGroup['id'], $childSmartGroup['id'])),
    ));
totten's avatar
totten committed
136 137 138 139 140

    // Create a contact within parent group
    $parentContactParams = array(
      'first_name' => 'Parent1 Fname',
      'last_name' => 'Parent1 Lname',
141
      'group' => array($parentGroup['id'] => 1),
totten's avatar
totten committed
142
    );
143
    $parentContact = $this->individualCreate($parentContactParams);
totten's avatar
totten committed
144 145 146 147 148

    // create a contact within child dgroup
    $childContactParams = array(
      'first_name' => 'Child1 Fname',
      'last_name' => 'Child2 Lname',
149
      'group' => array($childGroup['id'] => 1),
totten's avatar
totten committed
150
    );
151
    $childContact = $this->individualCreate($childContactParams);
totten's avatar
totten committed
152 153

    // Check if searching by parent group  returns both parent and child group contacts
154
    $result = $this->callAPISuccess('contact', 'get', array(
155
      'group' => $parentGroup['id'],
156
    ));
totten's avatar
totten committed
157
    $validContactIds = array($parentContact, $childContact);
totten's avatar
totten committed
158 159 160 161
    $resultContactIds = array();
    foreach ($result['values'] as $k => $v) {
      $resultContactIds[] = $v['contact_id'];
    }
162
    $this->assertEquals(3, count($resultContactIds), 'Check the count of returned values');
totten's avatar
totten committed
163 164 165
    $this->assertEquals(array(), array_diff($validContactIds, $resultContactIds), 'Check that the difference between two arrays should be blank array');

    // Check if searching by child group returns just child group contacts
166
    $result = $this->callAPISuccess('contact', 'get', array(
167
      'group' => $childGroup['id'],
168
    ));
totten's avatar
totten committed
169 170 171 172 173 174 175
    $validChildContactIds = array($childContact);
    $resultChildContactIds = array();
    foreach ($result['values'] as $k => $v) {
      $resultChildContactIds[] = $v['contact_id'];
    }
    $this->assertEquals(1, count($resultChildContactIds), 'Check the count of returned values');
    $this->assertEquals(array(), array_diff($validChildContactIds, $resultChildContactIds), 'Check that the difference between two arrays should be blank array');
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

    // Check if searching by smart child group returns just smart child group contacts
    $result = $this->callAPISuccess('contact', 'get', array(
      'group' => $childSmartGroup['id'],
    ));
    $validChildContactIds = array($childSmartGroupContact);
    $resultChildContactIds = array();
    foreach ($result['values'] as $k => $v) {
      $resultChildContactIds[] = $v['contact_id'];
    }
    $this->assertEquals(1, count($resultChildContactIds), 'Check the count of returned values');
    $this->assertEquals(array(), array_diff($validChildContactIds, $resultChildContactIds), 'Check that the difference between two arrays should be blank array');

    //cleanup
    $this->callAPISuccess('Contact', 'delete', array('id' => $parentContact));
    $this->callAPISuccess('Contact', 'delete', array('id' => $childContact));
    $this->callAPISuccess('Contact', 'delete', array('id' => $childSmartGroupContact));
totten's avatar
totten committed
193
  }
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  /**
   *  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",
deb.monish's avatar
deb.monish committed
210
      'first_name' => 'A',
211 212 213 214 215 216
    ));

    // Create contact with Gender - Male and in regular group
    $contact2 = $this->individualCreate(array(
      'group' => array($regularGroup['id'] => 1),
      'gender_id' => "Male",
deb.monish's avatar
deb.monish committed
217
      'first_name' => 'B',
218 219 220 221 222 223
    ), 1);

    // Create contact with Gender - Female and in regular group
    $contact3 = $this->individualCreate(array(
      'group' => array($regularGroup['id'] => 1),
      'gender_id' => "Female",
deb.monish's avatar
deb.monish committed
224
      'first_name' => 'C',
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    ), 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),
      ),
252
      //Case 3: Find all contacts in regular group and smart group
253 254 255 256 257 258 259
      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) {
260
      $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value']));
261
      list($select, $from, $where, $having) = $query->query();
deb.monish's avatar
deb.monish committed
262
      $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.* $from $where ORDER BY contact_a.first_name")->fetchAll();
263 264 265 266 267 268 269 270
      foreach ($groupContacts as $key => $value) {
        $groupContacts[$key] = $value['id'];
      }
      $this->assertEquals($case['expected_count'], count($groupContacts));
      $this->checkArrayEquals($case['expected_contact'], $groupContacts);
    }
  }

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  /**
   *  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),
deb.monish's avatar
deb.monish committed
298
      'first_name' => 'A',
299 300 301 302 303
    ));

    // create contact in 'Group 2'
    $contact2 = $this->individualCreate(array(
      'group' => array($group2['id'] => 1),
deb.monish's avatar
deb.monish committed
304
      'first_name' => 'B',
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    ), 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();
331
      $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id, contact_a.first_name $from $where ORDER BY contact_a.first_name")->fetchAll();
332 333 334 335 336 337 338 339
      foreach ($groupContacts as $key => $value) {
        $groupContacts[$key] = $value['id'];
      }
      $this->assertEquals($case['expected_count'], count($groupContacts));
      $this->checkArrayEquals($case['expected_contact'], $groupContacts);
    }
  }

totten's avatar
totten committed
340
}