Merge in 5.38
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / GroupContactTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test class for CRM_Contact_BAO_GroupContact BAO
14 *
15 * @package CiviCRM
16 * @group headless
17 */
18 class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase {
19
20 /**
21 * Test case for add( ).
22 */
23 public function testAdd() {
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
30 foreach ($groupContacts as $gc) {
31 $this->assertNotNull($gc->id);
32 }
33
34 //cleanup
35 foreach ($groupContacts as $gc) {
36 $gc->deleteTestObjects('CRM_Contact_DAO_GroupContact');
37 }
38 }
39
40 /**
41 * Test case for getGroupId( )
42 */
43 public function testGetGroupId() {
44
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.
62 *
63 * @throws \Exception
64 */
65 public function testContactSearchByParentGroup() {
66 // create a parent group
67 $parentGroup = $this->callAPISuccess('Group', 'create', [
68 'title' => 'Parent Group',
69 'description' => 'Parent Group',
70 'visibility' => 'User and User Admin Only',
71 'is_active' => 1,
72 ]);
73
74 // create a child group
75 $childGroup = $this->callAPISuccess('Group', 'create', [
76 'title' => 'Child Group',
77 'description' => 'Child Group',
78 'visibility' => 'User and User Admin Only',
79 'parents' => $parentGroup['id'],
80 'is_active' => 1,
81 ]);
82
83 // create smart group based on saved criteria Gender = Male
84 $batch = $this->callAPISuccess('SavedSearch', 'create', [
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;}}',
86 ]);
87 // Create contact with Gender - Male
88 $childSmartGroupContact = $this->individualCreate([
89 'gender_id' => "Male",
90 'first_name' => 'C',
91 ], 1);
92 // then create smart group
93 $childSmartGroup = $this->callAPISuccess('Group', 'create', [
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'],
100 ]);
101
102 $this->callAPISuccess('Group', 'create', [
103 'id' => $parentGroup['id'],
104 'children' => implode(',', [$childGroup['id'], $childSmartGroup['id']]),
105 ]);
106
107 // Create a contact within parent group
108 $parentContactParams = [
109 'first_name' => 'Parent1 Fname',
110 'last_name' => 'Parent1 Lname',
111 'group' => [$parentGroup['id'] => 1],
112 ];
113 $parentContact = $this->individualCreate($parentContactParams);
114
115 // create a contact within child dgroup
116 $childContactParams = [
117 'first_name' => 'Child1 Fname',
118 'last_name' => 'Child2 Lname',
119 'group' => [$childGroup['id'] => 1],
120 ];
121 $childContact = $this->individualCreate($childContactParams);
122
123 // Check if searching by parent group returns both parent and child group contacts
124 $result = $this->callAPISuccess('contact', 'get', [
125 'group' => $parentGroup['id'],
126 ]);
127 $validContactIds = [$parentContact, $childContact];
128 $resultContactIds = [];
129 foreach ($result['values'] as $k => $v) {
130 $resultContactIds[] = $v['contact_id'];
131 }
132 $this->assertEquals(3, count($resultContactIds), 'Check the count of returned values');
133 $this->assertEquals([], array_diff($validContactIds, $resultContactIds), 'Check that the difference between two arrays should be blank array');
134
135 // Check if searching by child group returns just child group contacts
136 $result = $this->callAPISuccess('contact', 'get', [
137 'group' => $childGroup['id'],
138 ]);
139 $validChildContactIds = [$childContact];
140 $resultChildContactIds = [];
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');
145 $this->assertEquals([], array_diff($validChildContactIds, $resultChildContactIds), 'Check that the difference between two arrays should be blank array');
146
147 // Check if searching by smart child group returns just smart child group contacts
148 $result = $this->callAPISuccess('contact', 'get', [
149 'group' => $childSmartGroup['id'],
150 ]);
151 $validChildContactIds = [$childSmartGroupContact];
152 $resultChildContactIds = [];
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');
157 $this->assertEquals([], array_diff($validChildContactIds, $resultChildContactIds), 'Check that the difference between two arrays should be blank array');
158
159 //cleanup
160 $this->callAPISuccess('Contact', 'delete', ['id' => $parentContact]);
161 $this->callAPISuccess('Contact', 'delete', ['id' => $childContact]);
162 $this->callAPISuccess('Contact', 'delete', ['id' => $childSmartGroupContact]);
163 }
164
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
170 $regularGroup = $this->callAPISuccess('Group', 'create', [
171 'title' => 'Regular Group',
172 'description' => 'Regular Group',
173 'visibility' => 'User and User Admin Only',
174 'is_active' => 1,
175 ]);
176
177 // Create contact with Gender - Male
178 $contact1 = $this->individualCreate([
179 'gender_id' => "Male",
180 'first_name' => 'A',
181 ]);
182
183 // Create contact with Gender - Male and in regular group
184 $contact2 = $this->individualCreate([
185 'group' => [$regularGroup['id'] => 1],
186 'gender_id' => "Male",
187 'first_name' => 'B',
188 ], 1);
189
190 // Create contact with Gender - Female and in regular group
191 $contact3 = $this->individualCreate([
192 'group' => [$regularGroup['id'] => 1],
193 'gender_id' => "Female",
194 'first_name' => 'C',
195 ], 1);
196
197 // create smart group based on saved criteria Gender = Male
198 $batch = $this->callAPISuccess('SavedSearch', 'create', [
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;}}',
200 ]);
201 $smartGroup = $this->callAPISuccess('Group', 'create', [
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,
207 ]);
208
209 $useCases = [
210 //Case 1: Find all contacts in regular group
211 [
212 'form_value' => ['group' => $regularGroup['id']],
213 'expected_count' => 2,
214 'expected_contact' => [$contact2, $contact3],
215 ],
216 //Case 2: Find all contacts in smart group
217 [
218 'form_value' => ['group' => $smartGroup['id']],
219 'expected_count' => 2,
220 'expected_contact' => [$contact1, $contact2],
221 ],
222 //Case 3: Find all contacts in regular group and smart group
223 [
224 'form_value' => ['group' => ['IN' => [$regularGroup['id'], $smartGroup['id']]]],
225 'expected_count' => 3,
226 'expected_contact' => [$contact1, $contact2, $contact3],
227 ],
228 ];
229 foreach ($useCases as $case) {
230 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value']));
231 list($select, $from, $where, $having) = $query->query();
232 $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.* $from $where ORDER BY contact_a.first_name")->fetchAll();
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
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
250 $group1 = $this->callAPISuccess('Group', 'create', [
251 'title' => 'Group 1',
252 'visibility' => 'User and User Admin Only',
253 'is_active' => 1,
254 'group_type' => $mailingListGT,
255 ]);
256
257 // create group with group type - Access Control
258 $group2 = $this->callAPISuccess('Group', 'create', [
259 'title' => 'Group 2',
260 'visibility' => 'User and User Admin Only',
261 'is_active' => 1,
262 'group_type' => $accessControlGT,
263 ]);
264
265 // create contact in 'Group 1'
266 $contact1 = $this->individualCreate([
267 'group' => [$group1['id'] => 1],
268 'first_name' => 'A',
269 ]);
270
271 // create contact in 'Group 2'
272 $contact2 = $this->individualCreate([
273 'group' => [$group2['id'] => 1],
274 'first_name' => 'B',
275 ], 1);
276
277 $useCases = [
278 //Case 1: Find contacts in group type - Mailing List
279 [
280 'form_value' => ['group_type' => [$mailingListGT]],
281 'expected_count' => 1,
282 'expected_contact' => [$contact1],
283 ],
284 //Case 2: Find contacts in group type - Access Control
285 [
286 'form_value' => ['group_type' => [$accessControlGT]],
287 'expected_count' => 1,
288 'expected_contact' => [$contact2],
289 ],
290 //Case 3: Find contacts in group type - Mailing List or Access List
291 [
292 'form_value' => ['group_type' => [$mailingListGT, $accessControlGT]],
293 'expected_count' => 2,
294 'expected_contact' => [$contact1, $contact2],
295 ],
296 ];
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();
301 $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id, contact_a.first_name $from $where ORDER BY contact_a.first_name")->fetchAll();
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
310 }