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