3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Test class for CRM_Contact_BAO_GroupContact BAO
18 class CRM_Contact_BAO_GroupContactTest
extends CiviUnitTestCase
{
21 * Sets up the fixture, for example, opens a network connection.
22 * This method is called before a test is executed.
24 protected function setUp() {
29 * Tears down the fixture, for example, closes a network connection.
31 * This method is called after a test is executed.
33 protected function tearDown() {
37 * Test case for add( ).
39 public function testAdd() {
41 //creates a test group contact by recursively creation
42 //lets create 10 groupContacts for fun
43 $groupContacts = CRM_Core_DAO
::createTestObject('CRM_Contact_DAO_GroupContact', NULL, 10);
45 //check the group contact id is not null for each of them
46 foreach ($groupContacts as $gc) {
47 $this->assertNotNull($gc->id
);
51 foreach ($groupContacts as $gc) {
52 $gc->deleteTestObjects('CRM_Contact_DAO_GroupContact');
57 * Test case for getGroupId( )
59 public function testGetGroupId() {
61 //creates a test groupContact object
62 //force group_id to 1 so we can compare
63 $groupContact = CRM_Core_DAO
::createTestObject('CRM_Contact_DAO_GroupContact');
65 //check the group contact id is not null
66 $this->assertNotNull($groupContact->id
);
68 $groupId = CRM_Core_DAO
::singleValueQuery('select max(id) from civicrm_group');
70 $this->assertEquals($groupContact->group_id
, $groupId, 'Check for group_id');
73 $groupContact->deleteTestObjects('CRM_Contact_DAO_GroupContact');
77 * Test case for contact search: CRM-6706, CRM-6586 Parent Group search should return contacts from child groups too.
81 public function testContactSearchByParentGroup() {
82 // create a parent group
83 $parentGroup = $this->callAPISuccess('Group', 'create', [
84 'title' => 'Parent Group',
85 'description' => 'Parent Group',
86 'visibility' => 'User and User Admin Only',
90 // create a child group
91 $childGroup = $this->callAPISuccess('Group', 'create', [
92 'title' => 'Child Group',
93 'description' => 'Child Group',
94 'visibility' => 'User and User Admin Only',
95 'parents' => $parentGroup['id'],
99 // create smart group based on saved criteria Gender = Male
100 $batch = $this->callAPISuccess('SavedSearch', 'create', [
101 '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;}}',
103 // Create contact with Gender - Male
104 $childSmartGroupContact = $this->individualCreate([
105 'gender_id' => "Male",
108 // then create smart group
109 $childSmartGroup = $this->callAPISuccess('Group', 'create', [
110 'title' => 'Child Smart Group',
111 'description' => 'Child Smart Group',
112 'visibility' => 'User and User Admin Only',
113 'saved_search_id' => $batch['id'],
115 'parents' => $parentGroup['id'],
118 $this->callAPISuccess('Group', 'create', [
119 'id' => $parentGroup['id'],
120 'children' => implode(',', [$childGroup['id'], $childSmartGroup['id']]),
123 // Create a contact within parent group
124 $parentContactParams = [
125 'first_name' => 'Parent1 Fname',
126 'last_name' => 'Parent1 Lname',
127 'group' => [$parentGroup['id'] => 1],
129 $parentContact = $this->individualCreate($parentContactParams);
131 // create a contact within child dgroup
132 $childContactParams = [
133 'first_name' => 'Child1 Fname',
134 'last_name' => 'Child2 Lname',
135 'group' => [$childGroup['id'] => 1],
137 $childContact = $this->individualCreate($childContactParams);
139 // Check if searching by parent group returns both parent and child group contacts
140 $result = $this->callAPISuccess('contact', 'get', [
141 'group' => $parentGroup['id'],
143 $validContactIds = [$parentContact, $childContact];
144 $resultContactIds = [];
145 foreach ($result['values'] as $k => $v) {
146 $resultContactIds[] = $v['contact_id'];
148 $this->assertEquals(3, count($resultContactIds), 'Check the count of returned values');
149 $this->assertEquals([], array_diff($validContactIds, $resultContactIds), 'Check that the difference between two arrays should be blank array');
151 // Check if searching by child group returns just child group contacts
152 $result = $this->callAPISuccess('contact', 'get', [
153 'group' => $childGroup['id'],
155 $validChildContactIds = [$childContact];
156 $resultChildContactIds = [];
157 foreach ($result['values'] as $k => $v) {
158 $resultChildContactIds[] = $v['contact_id'];
160 $this->assertEquals(1, count($resultChildContactIds), 'Check the count of returned values');
161 $this->assertEquals([], array_diff($validChildContactIds, $resultChildContactIds), 'Check that the difference between two arrays should be blank array');
163 // Check if searching by smart child group returns just smart child group contacts
164 $result = $this->callAPISuccess('contact', 'get', [
165 'group' => $childSmartGroup['id'],
167 $validChildContactIds = [$childSmartGroupContact];
168 $resultChildContactIds = [];
169 foreach ($result['values'] as $k => $v) {
170 $resultChildContactIds[] = $v['contact_id'];
172 $this->assertEquals(1, count($resultChildContactIds), 'Check the count of returned values');
173 $this->assertEquals([], array_diff($validChildContactIds, $resultChildContactIds), 'Check that the difference between two arrays should be blank array');
176 $this->callAPISuccess('Contact', 'delete', ['id' => $parentContact]);
177 $this->callAPISuccess('Contact', 'delete', ['id' => $childContact]);
178 $this->callAPISuccess('Contact', 'delete', ['id' => $childSmartGroupContact]);
182 * CRM-19698: Test case for combine contact search in regular and smart group
184 public function testContactCombineGroupSearch() {
185 // create regular group based
186 $regularGroup = $this->callAPISuccess('Group', 'create', [
187 'title' => 'Regular Group',
188 'description' => 'Regular Group',
189 'visibility' => 'User and User Admin Only',
193 // Create contact with Gender - Male
194 $contact1 = $this->individualCreate([
195 'gender_id' => "Male",
199 // Create contact with Gender - Male and in regular group
200 $contact2 = $this->individualCreate([
201 'group' => [$regularGroup['id'] => 1],
202 'gender_id' => "Male",
206 // Create contact with Gender - Female and in regular group
207 $contact3 = $this->individualCreate([
208 'group' => [$regularGroup['id'] => 1],
209 'gender_id' => "Female",
213 // create smart group based on saved criteria Gender = Male
214 $batch = $this->callAPISuccess('SavedSearch', 'create', [
215 '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;}}',
217 $smartGroup = $this->callAPISuccess('Group', 'create', [
218 'title' => 'Smart Group',
219 'description' => 'Smart Group',
220 'visibility' => 'User and User Admin Only',
221 'saved_search_id' => $batch['id'],
226 //Case 1: Find all contacts in regular group
228 'form_value' => ['group' => $regularGroup['id']],
229 'expected_count' => 2,
230 'expected_contact' => [$contact2, $contact3],
232 //Case 2: Find all contacts in smart group
234 'form_value' => ['group' => $smartGroup['id']],
235 'expected_count' => 2,
236 'expected_contact' => [$contact1, $contact2],
238 //Case 3: Find all contacts in regular group and smart group
240 'form_value' => ['group' => ['IN' => [$regularGroup['id'], $smartGroup['id']]]],
241 'expected_count' => 3,
242 'expected_contact' => [$contact1, $contact2, $contact3],
245 foreach ($useCases as $case) {
246 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
247 list($select, $from, $where, $having) = $query->query();
248 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.* $from $where ORDER BY contact_a.first_name")->fetchAll();
249 foreach ($groupContacts as $key => $value) {
250 $groupContacts[$key] = $value['id'];
252 $this->assertEquals($case['expected_count'], count($groupContacts));
253 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
258 * CRM-19333: Test case for contact search on basis of group type
260 public function testbyGroupType() {
261 $groupTypes = CRM_Core_BAO_OptionValue
::getOptionValuesAssocArrayFromName('group_type');
262 $mailingListGT = array_search('Mailing List', $groupTypes);
263 $accessControlGT = array_search('Access Control', $groupTypes);
265 // create group with group type - Mailing list
266 $group1 = $this->callAPISuccess('Group', 'create', [
267 'title' => 'Group 1',
268 'visibility' => 'User and User Admin Only',
270 'group_type' => $mailingListGT,
273 // create group with group type - Access Control
274 $group2 = $this->callAPISuccess('Group', 'create', [
275 'title' => 'Group 2',
276 'visibility' => 'User and User Admin Only',
278 'group_type' => $accessControlGT,
281 // create contact in 'Group 1'
282 $contact1 = $this->individualCreate([
283 'group' => [$group1['id'] => 1],
287 // create contact in 'Group 2'
288 $contact2 = $this->individualCreate([
289 'group' => [$group2['id'] => 1],
294 //Case 1: Find contacts in group type - Mailing List
296 'form_value' => ['group_type' => [$mailingListGT]],
297 'expected_count' => 1,
298 'expected_contact' => [$contact1],
300 //Case 2: Find contacts in group type - Access Control
302 'form_value' => ['group_type' => [$accessControlGT]],
303 'expected_count' => 1,
304 'expected_contact' => [$contact2],
306 //Case 3: Find contacts in group type - Mailing List or Access List
308 'form_value' => ['group_type' => [$mailingListGT, $accessControlGT]],
309 'expected_count' => 2,
310 'expected_contact' => [$contact1, $contact2],
314 foreach ($useCases as $case) {
315 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
316 list($select, $from, $where, $having) = $query->query();
317 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.id, contact_a.first_name $from $where ORDER BY contact_a.first_name")->fetchAll();
318 foreach ($groupContacts as $key => $value) {
319 $groupContacts[$key] = $value['id'];
321 $this->assertEquals($case['expected_count'], count($groupContacts));
322 $this->checkArrayEquals($case['expected_contact'], $groupContacts);