Fix group_type filter in Advance Search and api
[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 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
6a488035
TO
97 $groupParams1 = array(
98 'title' => 'Parent Group',
99 'description' => 'Parent Group',
100 'visibility' => 'User and User Admin Only',
6a488035
TO
101 'is_active' => 1,
102 );
c3137c08 103 $parentGroup = $this->callAPISuccess('Group', 'create', $groupParams1);
6a488035
TO
104
105 // create a child group
106 $groupParams2 = array(
107 'title' => 'Child Group',
108 'description' => 'Child Group',
109 'visibility' => 'User and User Admin Only',
485a3a1f 110 'parents' => $parentGroup['id'],
6a488035
TO
111 'is_active' => 1,
112 );
c3137c08 113 $childGroup = $this->callAPISuccess('Group', 'create', $groupParams2);
6a488035
TO
114
115 // Create a contact within parent group
116 $parentContactParams = array(
117 'first_name' => 'Parent1 Fname',
118 'last_name' => 'Parent1 Lname',
485a3a1f 119 'group' => array($parentGroup['id'] => 1),
6a488035 120 );
f2040bc6 121 $parentContact = $this->individualCreate($parentContactParams);
6a488035
TO
122
123 // create a contact within child dgroup
124 $childContactParams = array(
125 'first_name' => 'Child1 Fname',
126 'last_name' => 'Child2 Lname',
485a3a1f 127 'group' => array($childGroup['id'] => 1),
6a488035 128 );
f2040bc6 129 $childContact = $this->individualCreate($childContactParams);
6a488035
TO
130
131 // Check if searching by parent group returns both parent and child group contacts
132 $searchParams = array(
485a3a1f 133 'group' => $parentGroup['id'],
6a488035 134 );
c3137c08 135 $result = $this->callAPISuccess('contact', 'get', $searchParams);
92915c55 136 $validContactIds = array($parentContact, $childContact);
6a488035
TO
137 $resultContactIds = array();
138 foreach ($result['values'] as $k => $v) {
139 $resultContactIds[] = $v['contact_id'];
140 }
141 $this->assertEquals(2, count($resultContactIds), 'Check the count of returned values');
142 $this->assertEquals(array(), array_diff($validContactIds, $resultContactIds), 'Check that the difference between two arrays should be blank array');
143
144 // Check if searching by child group returns just child group contacts
145 $searchParams = array(
485a3a1f 146 'group' => $childGroup['id'],
6a488035 147 );
c3137c08 148 $result = $this->callAPISuccess('contact', 'get', $searchParams);
6a488035
TO
149 $validChildContactIds = array($childContact);
150 $resultChildContactIds = array();
151 foreach ($result['values'] as $k => $v) {
152 $resultChildContactIds[] = $v['contact_id'];
153 }
154 $this->assertEquals(1, count($resultChildContactIds), 'Check the count of returned values');
155 $this->assertEquals(array(), array_diff($validChildContactIds, $resultChildContactIds), 'Check that the difference between two arrays should be blank array');
156 }
96025800 157
3a484391 158
159 /**
160 * CRM-19698: Test case for combine contact search in regular and smart group
161 */
162 public function testContactCombineGroupSearch() {
163 // create regular group based
164 $regularGroup = $this->callAPISuccess('Group', 'create', array(
165 'title' => 'Regular Group',
166 'description' => 'Regular Group',
167 'visibility' => 'User and User Admin Only',
168 'is_active' => 1,
169 ));
170
171 // Create contact with Gender - Male
172 $contact1 = $this->individualCreate(array(
173 'gender_id' => "Male",
174 ));
175
176 // Create contact with Gender - Male and in regular group
177 $contact2 = $this->individualCreate(array(
178 'group' => array($regularGroup['id'] => 1),
179 'gender_id' => "Male",
180 ), 1);
181
182 // Create contact with Gender - Female and in regular group
183 $contact3 = $this->individualCreate(array(
184 'group' => array($regularGroup['id'] => 1),
185 'gender_id' => "Female",
186 ), 1);
187
188 // create smart group based on saved criteria Gender = Male
189 $batch = $this->callAPISuccess('SavedSearch', 'create', array(
190 '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;}}',
191 ));
192 $smartGroup = $this->callAPISuccess('Group', 'create', array(
193 'title' => 'Smart Group',
194 'description' => 'Smart Group',
195 'visibility' => 'User and User Admin Only',
196 'saved_search_id' => $batch['id'],
197 'is_active' => 1,
198 ));
199
200 $useCases = array(
201 //Case 1: Find all contacts in regular group
202 array(
203 'form_value' => array('group' => $regularGroup['id']),
204 'expected_count' => 2,
3a484391 205 'expected_contact' => array($contact2, $contact3),
206 ),
207 //Case 2: Find all contacts in smart group
208 array(
209 'form_value' => array('group' => $smartGroup['id']),
210 'expected_count' => 2,
3a484391 211 'expected_contact' => array($contact1, $contact2),
212 ),
883e1e76 213 //Case 3: Find all contacts in regular group and smart group
3a484391 214 array(
215 'form_value' => array('group' => array('IN' => array($regularGroup['id'], $smartGroup['id']))),
216 'expected_count' => 3,
3a484391 217 'expected_contact' => array($contact1, $contact2, $contact3),
218 ),
219 );
220 foreach ($useCases as $case) {
883e1e76 221 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value']));
3a484391 222 list($select, $from, $where, $having) = $query->query();
223 $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
224 foreach ($groupContacts as $key => $value) {
225 $groupContacts[$key] = $value['id'];
226 }
227 $this->assertEquals($case['expected_count'], count($groupContacts));
228 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
229 }
230 }
231
d412bcca 232 /**
233 * CRM-19333: Test case for contact search on basis of group type
234 */
235 public function testbyGroupType() {
236 $groupTypes = CRM_Core_BAO_OptionValue::getOptionValuesAssocArrayFromName('group_type');
237 $mailingListGT = array_search('Mailing List', $groupTypes);
238 $accessControlGT = array_search('Access Control', $groupTypes);
239
240 // create group with group type - Mailing list
241 $group1 = $this->callAPISuccess('Group', 'create', array(
242 'title' => 'Group 1',
243 'visibility' => 'User and User Admin Only',
244 'is_active' => 1,
245 'group_type' => $mailingListGT,
246 ));
247
248 // create group with group type - Access Control
249 $group2 = $this->callAPISuccess('Group', 'create', array(
250 'title' => 'Group 2',
251 'visibility' => 'User and User Admin Only',
252 'is_active' => 1,
253 'group_type' => $accessControlGT,
254 ));
255
256 // create contact in 'Group 1'
257 $contact1 = $this->individualCreate(array(
258 'group' => array($group1['id'] => 1),
259 ));
260
261 // create contact in 'Group 2'
262 $contact2 = $this->individualCreate(array(
263 'group' => array($group2['id'] => 1),
264 ), 1);
265
266 $useCases = array(
267 //Case 1: Find contacts in group type - Mailing List
268 array(
269 'form_value' => array('group_type' => array($mailingListGT)),
270 'expected_count' => 1,
271 'expected_contact' => array($contact1),
272 ),
273 //Case 2: Find contacts in group type - Access Control
274 array(
275 'form_value' => array('group_type' => array($accessControlGT)),
276 'expected_count' => 1,
277 'expected_contact' => array($contact2),
278 ),
279 //Case 3: Find contacts in group type - Mailing List or Access List
280 array(
281 'form_value' => array('group_type' => array($mailingListGT, $accessControlGT)),
282 'expected_count' => 2,
283 'expected_contact' => array($contact1, $contact2),
284 ),
285 );
286
287 foreach ($useCases as $case) {
288 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value']));
289 list($select, $from, $where, $having) = $query->query();
290 $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
291 foreach ($groupContacts as $key => $value) {
292 $groupContacts[$key] = $value['id'];
293 }
294 $this->assertEquals($case['expected_count'], count($groupContacts));
295 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
296 }
297 }
298
6a488035 299}