Merge pull request #20498 from seamuslee001/fix_authx_drupal89
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / GroupContactTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
7d61e75f
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035 11
6a488035
TO
12/**
13 * Test class for CRM_Contact_BAO_GroupContact BAO
14 *
6c6e6187 15 * @package CiviCRM
acb109b7 16 * @group headless
6a488035
TO
17 */
18class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase {
19
6a488035 20 /**
9b1e4469 21 * Test case for add( ).
6a488035 22 */
00be9182 23 public function testAdd() {
6a488035
TO
24
25 //creates a test group contact by recursively creation
26 //lets create 10 groupContacts for fun
27 $groupContacts = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_GroupContact', NULL, 10);
28
29 //check the group contact id is not null for each of them
4f99ca55
TO
30 foreach ($groupContacts as $gc) {
31 $this->assertNotNull($gc->id);
6c6e6187 32 }
6a488035
TO
33
34 //cleanup
4f99ca55
TO
35 foreach ($groupContacts as $gc) {
36 $gc->deleteTestObjects('CRM_Contact_DAO_GroupContact');
6c6e6187 37 }
6a488035
TO
38 }
39
40 /**
100fef9d 41 * Test case for getGroupId( )
6a488035 42 */
00be9182 43 public function testGetGroupId() {
6a488035 44
6a488035
TO
45 //creates a test groupContact object
46 //force group_id to 1 so we can compare
47 $groupContact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_GroupContact');
48
49 //check the group contact id is not null
50 $this->assertNotNull($groupContact->id);
51
52 $groupId = CRM_Core_DAO::singleValueQuery('select max(id) from civicrm_group');
53
54 $this->assertEquals($groupContact->group_id, $groupId, 'Check for group_id');
55
56 //cleanup
57 $groupContact->deleteTestObjects('CRM_Contact_DAO_GroupContact');
58 }
59
60 /**
61 * Test case for contact search: CRM-6706, CRM-6586 Parent Group search should return contacts from child groups too.
673fae6c 62 *
63 * @throws \Exception
6a488035 64 */
00be9182 65 public function testContactSearchByParentGroup() {
6a488035 66 // create a parent group
9099cab3 67 $parentGroup = $this->callAPISuccess('Group', 'create', [
6a488035
TO
68 'title' => 'Parent Group',
69 'description' => 'Parent Group',
70 'visibility' => 'User and User Admin Only',
6a488035 71 'is_active' => 1,
9099cab3 72 ]);
6a488035
TO
73
74 // create a child group
9099cab3 75 $childGroup = $this->callAPISuccess('Group', 'create', [
6a488035
TO
76 'title' => 'Child Group',
77 'description' => 'Child Group',
78 'visibility' => 'User and User Admin Only',
485a3a1f 79 'parents' => $parentGroup['id'],
6a488035 80 'is_active' => 1,
9099cab3 81 ]);
783144b0 82
83 // create smart group based on saved criteria Gender = Male
9099cab3 84 $batch = $this->callAPISuccess('SavedSearch', 'create', [
783144b0 85 '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;}}',
9099cab3 86 ]);
783144b0 87 // Create contact with Gender - Male
9099cab3 88 $childSmartGroupContact = $this->individualCreate([
783144b0 89 'gender_id' => "Male",
90 'first_name' => 'C',
9099cab3 91 ], 1);
783144b0 92 // then create smart group
9099cab3 93 $childSmartGroup = $this->callAPISuccess('Group', 'create', [
783144b0 94 'title' => 'Child Smart Group',
95 'description' => 'Child Smart Group',
96 'visibility' => 'User and User Admin Only',
97 'saved_search_id' => $batch['id'],
98 'is_active' => 1,
99 'parents' => $parentGroup['id'],
9099cab3 100 ]);
783144b0 101
9099cab3 102 $this->callAPISuccess('Group', 'create', [
783144b0 103 'id' => $parentGroup['id'],
9099cab3
CW
104 'children' => implode(',', [$childGroup['id'], $childSmartGroup['id']]),
105 ]);
6a488035
TO
106
107 // Create a contact within parent group
9099cab3 108 $parentContactParams = [
6a488035
TO
109 'first_name' => 'Parent1 Fname',
110 'last_name' => 'Parent1 Lname',
9099cab3
CW
111 'group' => [$parentGroup['id'] => 1],
112 ];
f2040bc6 113 $parentContact = $this->individualCreate($parentContactParams);
6a488035
TO
114
115 // create a contact within child dgroup
9099cab3 116 $childContactParams = [
6a488035
TO
117 'first_name' => 'Child1 Fname',
118 'last_name' => 'Child2 Lname',
673fae6c 119 'group' => [$childGroup['id'] => 1],
9099cab3 120 ];
f2040bc6 121 $childContact = $this->individualCreate($childContactParams);
6a488035
TO
122
123 // Check if searching by parent group returns both parent and child group contacts
9099cab3 124 $result = $this->callAPISuccess('contact', 'get', [
485a3a1f 125 'group' => $parentGroup['id'],
9099cab3
CW
126 ]);
127 $validContactIds = [$parentContact, $childContact];
128 $resultContactIds = [];
6a488035
TO
129 foreach ($result['values'] as $k => $v) {
130 $resultContactIds[] = $v['contact_id'];
131 }
783144b0 132 $this->assertEquals(3, count($resultContactIds), 'Check the count of returned values');
9099cab3 133 $this->assertEquals([], array_diff($validContactIds, $resultContactIds), 'Check that the difference between two arrays should be blank array');
6a488035
TO
134
135 // Check if searching by child group returns just child group contacts
9099cab3 136 $result = $this->callAPISuccess('contact', 'get', [
485a3a1f 137 'group' => $childGroup['id'],
9099cab3
CW
138 ]);
139 $validChildContactIds = [$childContact];
140 $resultChildContactIds = [];
6a488035
TO
141 foreach ($result['values'] as $k => $v) {
142 $resultChildContactIds[] = $v['contact_id'];
143 }
144 $this->assertEquals(1, count($resultChildContactIds), 'Check the count of returned values');
9099cab3 145 $this->assertEquals([], array_diff($validChildContactIds, $resultChildContactIds), 'Check that the difference between two arrays should be blank array');
783144b0 146
147 // Check if searching by smart child group returns just smart child group contacts
9099cab3 148 $result = $this->callAPISuccess('contact', 'get', [
783144b0 149 'group' => $childSmartGroup['id'],
9099cab3
CW
150 ]);
151 $validChildContactIds = [$childSmartGroupContact];
152 $resultChildContactIds = [];
783144b0 153 foreach ($result['values'] as $k => $v) {
154 $resultChildContactIds[] = $v['contact_id'];
155 }
156 $this->assertEquals(1, count($resultChildContactIds), 'Check the count of returned values');
9099cab3 157 $this->assertEquals([], array_diff($validChildContactIds, $resultChildContactIds), 'Check that the difference between two arrays should be blank array');
783144b0 158
159 //cleanup
9099cab3
CW
160 $this->callAPISuccess('Contact', 'delete', ['id' => $parentContact]);
161 $this->callAPISuccess('Contact', 'delete', ['id' => $childContact]);
162 $this->callAPISuccess('Contact', 'delete', ['id' => $childSmartGroupContact]);
6a488035 163 }
96025800 164
3a484391 165 /**
166 * CRM-19698: Test case for combine contact search in regular and smart group
167 */
168 public function testContactCombineGroupSearch() {
169 // create regular group based
9099cab3 170 $regularGroup = $this->callAPISuccess('Group', 'create', [
3a484391 171 'title' => 'Regular Group',
172 'description' => 'Regular Group',
173 'visibility' => 'User and User Admin Only',
174 'is_active' => 1,
9099cab3 175 ]);
3a484391 176
177 // Create contact with Gender - Male
9099cab3 178 $contact1 = $this->individualCreate([
3a484391 179 'gender_id' => "Male",
3cf708ce 180 'first_name' => 'A',
9099cab3 181 ]);
3a484391 182
183 // Create contact with Gender - Male and in regular group
9099cab3
CW
184 $contact2 = $this->individualCreate([
185 'group' => [$regularGroup['id'] => 1],
3a484391 186 'gender_id' => "Male",
3cf708ce 187 'first_name' => 'B',
9099cab3 188 ], 1);
3a484391 189
190 // Create contact with Gender - Female and in regular group
9099cab3
CW
191 $contact3 = $this->individualCreate([
192 'group' => [$regularGroup['id'] => 1],
3a484391 193 'gender_id' => "Female",
3cf708ce 194 'first_name' => 'C',
9099cab3 195 ], 1);
3a484391 196
197 // create smart group based on saved criteria Gender = Male
9099cab3 198 $batch = $this->callAPISuccess('SavedSearch', 'create', [
3a484391 199 '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;}}',
9099cab3
CW
200 ]);
201 $smartGroup = $this->callAPISuccess('Group', 'create', [
3a484391 202 'title' => 'Smart Group',
203 'description' => 'Smart Group',
204 'visibility' => 'User and User Admin Only',
205 'saved_search_id' => $batch['id'],
206 'is_active' => 1,
9099cab3 207 ]);
3a484391 208
9099cab3 209 $useCases = [
3a484391 210 //Case 1: Find all contacts in regular group
9099cab3
CW
211 [
212 'form_value' => ['group' => $regularGroup['id']],
3a484391 213 'expected_count' => 2,
9099cab3
CW
214 'expected_contact' => [$contact2, $contact3],
215 ],
3a484391 216 //Case 2: Find all contacts in smart group
9099cab3
CW
217 [
218 'form_value' => ['group' => $smartGroup['id']],
3a484391 219 'expected_count' => 2,
9099cab3
CW
220 'expected_contact' => [$contact1, $contact2],
221 ],
883e1e76 222 //Case 3: Find all contacts in regular group and smart group
9099cab3
CW
223 [
224 'form_value' => ['group' => ['IN' => [$regularGroup['id'], $smartGroup['id']]]],
3a484391 225 'expected_count' => 3,
9099cab3
CW
226 'expected_contact' => [$contact1, $contact2, $contact3],
227 ],
228 ];
3a484391 229 foreach ($useCases as $case) {
883e1e76 230 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value']));
3a484391 231 list($select, $from, $where, $having) = $query->query();
3cf708ce 232 $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.* $from $where ORDER BY contact_a.first_name")->fetchAll();
3a484391 233 foreach ($groupContacts as $key => $value) {
234 $groupContacts[$key] = $value['id'];
235 }
236 $this->assertEquals($case['expected_count'], count($groupContacts));
237 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
238 }
239 }
240
d412bcca 241 /**
242 * CRM-19333: Test case for contact search on basis of group type
243 */
244 public function testbyGroupType() {
245 $groupTypes = CRM_Core_BAO_OptionValue::getOptionValuesAssocArrayFromName('group_type');
246 $mailingListGT = array_search('Mailing List', $groupTypes);
247 $accessControlGT = array_search('Access Control', $groupTypes);
248
249 // create group with group type - Mailing list
9099cab3 250 $group1 = $this->callAPISuccess('Group', 'create', [
d412bcca 251 'title' => 'Group 1',
252 'visibility' => 'User and User Admin Only',
253 'is_active' => 1,
254 'group_type' => $mailingListGT,
9099cab3 255 ]);
d412bcca 256
257 // create group with group type - Access Control
9099cab3 258 $group2 = $this->callAPISuccess('Group', 'create', [
d412bcca 259 'title' => 'Group 2',
260 'visibility' => 'User and User Admin Only',
261 'is_active' => 1,
262 'group_type' => $accessControlGT,
9099cab3 263 ]);
d412bcca 264
265 // create contact in 'Group 1'
9099cab3
CW
266 $contact1 = $this->individualCreate([
267 'group' => [$group1['id'] => 1],
3cf708ce 268 'first_name' => 'A',
9099cab3 269 ]);
d412bcca 270
271 // create contact in 'Group 2'
9099cab3
CW
272 $contact2 = $this->individualCreate([
273 'group' => [$group2['id'] => 1],
3cf708ce 274 'first_name' => 'B',
9099cab3 275 ], 1);
d412bcca 276
9099cab3 277 $useCases = [
d412bcca 278 //Case 1: Find contacts in group type - Mailing List
9099cab3
CW
279 [
280 'form_value' => ['group_type' => [$mailingListGT]],
d412bcca 281 'expected_count' => 1,
9099cab3
CW
282 'expected_contact' => [$contact1],
283 ],
d412bcca 284 //Case 2: Find contacts in group type - Access Control
9099cab3
CW
285 [
286 'form_value' => ['group_type' => [$accessControlGT]],
d412bcca 287 'expected_count' => 1,
9099cab3
CW
288 'expected_contact' => [$contact2],
289 ],
d412bcca 290 //Case 3: Find contacts in group type - Mailing List or Access List
9099cab3
CW
291 [
292 'form_value' => ['group_type' => [$mailingListGT, $accessControlGT]],
d412bcca 293 'expected_count' => 2,
9099cab3
CW
294 'expected_contact' => [$contact1, $contact2],
295 ],
296 ];
d412bcca 297
298 foreach ($useCases as $case) {
299 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value']));
300 list($select, $from, $where, $having) = $query->query();
7d3b1a9d 301 $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id, contact_a.first_name $from $where ORDER BY contact_a.first_name")->fetchAll();
d412bcca 302 foreach ($groupContacts as $key => $value) {
303 $groupContacts[$key] = $value['id'];
304 }
305 $this->assertEquals($case['expected_count'], count($groupContacts));
306 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
307 }
308 }
309
6a488035 310}