additional improvement and unit test
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / GroupContactTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test class for CRM_Contact_BAO_GroupContact BAO
30 *
31 * @package CiviCRM
32 * @group headless
33 */
34 class 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.
39 */
40 protected function setUp() {
41 parent::setUp();
42 }
43
44 /**
45 * Tears down the fixture, for example, closes a network connection.
46 *
47 * This method is called after a test is executed.
48 */
49 protected function tearDown() {
50 }
51
52 /**
53 * Test case for add( ).
54 */
55 public function testAdd() {
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
62 foreach ($groupContacts as $gc) {
63 $this->assertNotNull($gc->id);
64 }
65
66 //cleanup
67 foreach ($groupContacts as $gc) {
68 $gc->deleteTestObjects('CRM_Contact_DAO_GroupContact');
69 }
70 }
71
72 /**
73 * Test case for getGroupId( )
74 */
75 public function testGetGroupId() {
76
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 */
95 public function testContactSearchByParentGroup() {
96 // create a parent group
97 $groupParams1 = array(
98 'title' => 'Parent Group',
99 'description' => 'Parent Group',
100 'visibility' => 'User and User Admin Only',
101 'is_active' => 1,
102 );
103 $parentGroup = $this->callAPISuccess('Group', 'create', $groupParams1);
104
105 // create a child group
106 $groupParams2 = array(
107 'title' => 'Child Group',
108 'description' => 'Child Group',
109 'visibility' => 'User and User Admin Only',
110 'parents' => $parentGroup['id'],
111 'is_active' => 1,
112 );
113 $childGroup = $this->callAPISuccess('Group', 'create', $groupParams2);
114
115 // Create a contact within parent group
116 $parentContactParams = array(
117 'first_name' => 'Parent1 Fname',
118 'last_name' => 'Parent1 Lname',
119 'group' => array($parentGroup['id'] => 1),
120 );
121 $parentContact = $this->individualCreate($parentContactParams);
122
123 // create a contact within child dgroup
124 $childContactParams = array(
125 'first_name' => 'Child1 Fname',
126 'last_name' => 'Child2 Lname',
127 'group' => array($childGroup['id'] => 1),
128 );
129 $childContact = $this->individualCreate($childContactParams);
130
131 // Check if searching by parent group returns both parent and child group contacts
132 $searchParams = array(
133 'group' => $parentGroup['id'],
134 );
135 $result = $this->callAPISuccess('contact', 'get', $searchParams);
136 $validContactIds = array($parentContact, $childContact);
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(
146 'group' => $childGroup['id'],
147 );
148 $result = $this->callAPISuccess('contact', 'get', $searchParams);
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 }
157
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,
205 'operator' => 'AND',
206 'expected_contact' => array($contact2, $contact3),
207 ),
208 //Case 2: Find all contacts in smart group
209 array(
210 'form_value' => array('group' => $smartGroup['id']),
211 'expected_count' => 2,
212 'operator' => 'AND',
213 'expected_contact' => array($contact1, $contact2),
214 ),
215 //Case 3: Find all contacts in regular group AND smart group
216 array(
217 'form_value' => array('group' => array('IN' => array($regularGroup['id'], $smartGroup['id']))),
218 'expected_count' => 1,
219 'operator' => 'AND',
220 'expected_contact' => array($contact2),
221 ),
222 //Case 4: Find all contacts in regular group OR smart group
223 array(
224 'form_value' => array('group' => array('IN' => array($regularGroup['id'], $smartGroup['id']))),
225 'expected_count' => 3,
226 'operator' => 'OR',
227 'expected_contact' => array($contact1, $contact2, $contact3),
228 ),
229 );
230 foreach ($useCases as $case) {
231 $query = new CRM_Contact_BAO_Query(
232 CRM_Contact_BAO_Query::convertFormValues($case['form_value']),
233 NULL, NULL,
234 FALSE, FALSE, 1,
235 FALSE, TRUE,
236 TRUE, NULL,
237 $case['operator']
238 );
239 list($select, $from, $where, $having) = $query->query();
240 $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
241 foreach ($groupContacts as $key => $value) {
242 $groupContacts[$key] = $value['id'];
243 }
244 $this->assertEquals($case['expected_count'], count($groupContacts));
245 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
246 }
247 }
248
249 }