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 * Test case for add( ).
23 public function testAdd() {
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);
29 //check the group contact id is not null for each of them
30 foreach ($groupContacts as $gc) {
31 $this->assertNotNull($gc->id
);
35 foreach ($groupContacts as $gc) {
36 $gc->deleteTestObjects('CRM_Contact_DAO_GroupContact');
41 * Test case for getGroupId( )
43 public function testGetGroupId() {
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');
49 //check the group contact id is not null
50 $this->assertNotNull($groupContact->id
);
52 $groupId = CRM_Core_DAO
::singleValueQuery('select max(id) from civicrm_group');
54 $this->assertEquals($groupContact->group_id
, $groupId, 'Check for group_id');
57 $groupContact->deleteTestObjects('CRM_Contact_DAO_GroupContact');
61 * Test case for contact search: CRM-6706, CRM-6586 Parent Group search should return contacts from child groups too.
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',
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'],
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;}}',
87 // Create contact with Gender - Male
88 $childSmartGroupContact = $this->individualCreate([
89 'gender_id' => "Male",
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'],
99 'parents' => $parentGroup['id'],
102 $this->callAPISuccess('Group', 'create', [
103 'id' => $parentGroup['id'],
104 'children' => implode(',', [$childGroup['id'], $childSmartGroup['id']]),
107 // Create a contact within parent group
108 $parentContactParams = [
109 'first_name' => 'Parent1 Fname',
110 'last_name' => 'Parent1 Lname',
111 'group' => [$parentGroup['id'] => 1],
113 $parentContact = $this->individualCreate($parentContactParams);
115 // create a contact within child dgroup
116 $childContactParams = [
117 'first_name' => 'Child1 Fname',
118 'last_name' => 'Child2 Lname',
119 'group' => [$childGroup['id'] => 1],
121 $childContact = $this->individualCreate($childContactParams);
123 // Check if searching by parent group returns both parent and child group contacts
124 $result = $this->callAPISuccess('contact', 'get', [
125 'group' => $parentGroup['id'],
127 $validContactIds = [$parentContact, $childContact];
128 $resultContactIds = [];
129 foreach ($result['values'] as $k => $v) {
130 $resultContactIds[] = $v['contact_id'];
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');
135 // Check if searching by child group returns just child group contacts
136 $result = $this->callAPISuccess('contact', 'get', [
137 'group' => $childGroup['id'],
139 $validChildContactIds = [$childContact];
140 $resultChildContactIds = [];
141 foreach ($result['values'] as $k => $v) {
142 $resultChildContactIds[] = $v['contact_id'];
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');
147 // Check if searching by smart child group returns just smart child group contacts
148 $result = $this->callAPISuccess('contact', 'get', [
149 'group' => $childSmartGroup['id'],
151 $validChildContactIds = [$childSmartGroupContact];
152 $resultChildContactIds = [];
153 foreach ($result['values'] as $k => $v) {
154 $resultChildContactIds[] = $v['contact_id'];
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');
160 $this->callAPISuccess('Contact', 'delete', ['id' => $parentContact]);
161 $this->callAPISuccess('Contact', 'delete', ['id' => $childContact]);
162 $this->callAPISuccess('Contact', 'delete', ['id' => $childSmartGroupContact]);
166 * CRM-19698: Test case for combine contact search in regular and smart group
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',
177 // Create contact with Gender - Male
178 $contact1 = $this->individualCreate([
179 'gender_id' => "Male",
183 // Create contact with Gender - Male and in regular group
184 $contact2 = $this->individualCreate([
185 'group' => [$regularGroup['id'] => 1],
186 'gender_id' => "Male",
190 // Create contact with Gender - Female and in regular group
191 $contact3 = $this->individualCreate([
192 'group' => [$regularGroup['id'] => 1],
193 'gender_id' => "Female",
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;}}',
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'],
210 //Case 1: Find all contacts in regular group
212 'form_value' => ['group' => $regularGroup['id']],
213 'expected_count' => 2,
214 'expected_contact' => [$contact2, $contact3],
216 //Case 2: Find all contacts in smart group
218 'form_value' => ['group' => $smartGroup['id']],
219 'expected_count' => 2,
220 'expected_contact' => [$contact1, $contact2],
222 //Case 3: Find all contacts in regular group and smart group
224 'form_value' => ['group' => ['IN' => [$regularGroup['id'], $smartGroup['id']]]],
225 'expected_count' => 3,
226 'expected_contact' => [$contact1, $contact2, $contact3],
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'];
236 $this->assertEquals($case['expected_count'], count($groupContacts));
237 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
242 * CRM-19333: Test case for contact search on basis of group type
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);
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',
254 'group_type' => $mailingListGT,
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',
262 'group_type' => $accessControlGT,
265 // create contact in 'Group 1'
266 $contact1 = $this->individualCreate([
267 'group' => [$group1['id'] => 1],
271 // create contact in 'Group 2'
272 $contact2 = $this->individualCreate([
273 'group' => [$group2['id'] => 1],
278 //Case 1: Find contacts in group type - Mailing List
280 'form_value' => ['group_type' => [$mailingListGT]],
281 'expected_count' => 1,
282 'expected_contact' => [$contact1],
284 //Case 2: Find contacts in group type - Access Control
286 'form_value' => ['group_type' => [$accessControlGT]],
287 'expected_count' => 1,
288 'expected_contact' => [$contact2],
290 //Case 3: Find contacts in group type - Mailing List or Access List
292 'form_value' => ['group_type' => [$mailingListGT, $accessControlGT]],
293 'expected_count' => 2,
294 'expected_contact' => [$contact1, $contact2],
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'];
305 $this->assertEquals($case['expected_count'], count($groupContacts));
306 $this->checkArrayEquals($case['expected_contact'], $groupContacts);