Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
e9479dcf EM |
28 | /** |
29 | * Class api_v3_GroupContactTest | |
acb109b7 | 30 | * @group headless |
e9479dcf | 31 | */ |
6a488035 TO |
32 | class api_v3_GroupContactTest extends CiviUnitTestCase { |
33 | ||
34 | protected $_contactId; | |
35 | protected $_contactId1; | |
7fbb4198 | 36 | protected $_apiversion = 3; |
97089930 | 37 | |
38 | /** | |
39 | * @var int | |
40 | */ | |
6a488035 | 41 | protected $_groupId1; |
6a488035 | 42 | |
97089930 | 43 | /** |
44 | * @var int | |
45 | */ | |
46 | protected $_groupId2; | |
47 | ||
c490a46a | 48 | /** |
eceb18cc | 49 | * Set up for group contact tests. |
c490a46a CW |
50 | * |
51 | * @todo set up calls function that doesn't work @ the moment | |
52 | */ | |
00be9182 | 53 | public function setUp() { |
6a488035 | 54 | parent::setUp(); |
10f7c4d2 | 55 | $this->useTransaction(TRUE); |
6a488035 | 56 | |
e4d5f1e2 | 57 | $this->_contactId = $this->individualCreate(); |
6a488035 | 58 | |
72e41e3a | 59 | $this->_groupId1 = $this->groupCreate(); |
97089930 | 60 | |
61 | $this->callAPISuccess('group_contact', 'create', array( | |
6a488035 TO |
62 | 'contact_id' => $this->_contactId, |
63 | 'group_id' => $this->_groupId1, | |
97089930 | 64 | )); |
6a488035 | 65 | |
97089930 | 66 | $this->_groupId2 = $this->groupCreate(array( |
6a488035 TO |
67 | 'name' => 'Test Group 2', |
68 | 'domain_id' => 1, | |
69 | 'title' => 'New Test Group2 Created', | |
70 | 'description' => 'New Test Group2 Created', | |
71 | 'is_active' => 1, | |
72 | 'visibility' => 'User and User Admin Only', | |
97089930 | 73 | )); |
6a488035 TO |
74 | |
75 | $this->_group = array( | |
6c6e6187 | 76 | $this->_groupId1 => array( |
92915c55 | 77 | 'title' => 'New Test Group Created', |
6a488035 TO |
78 | 'visibility' => 'Public Pages', |
79 | 'in_method' => 'API', | |
80 | ), | |
81 | $this->_groupId2 => array( | |
82 | 'title' => 'New Test Group2 Created', | |
83 | 'visibility' => 'User and User Admin Only', | |
84 | 'in_method' => 'API', | |
85 | ), | |
86 | ); | |
87 | } | |
88 | ||
389bcebf | 89 | /** |
36e590d4 | 90 | * Test GroupContact.get by ID. |
389bcebf | 91 | */ |
00be9182 | 92 | public function testGet() { |
6a488035 TO |
93 | $params = array( |
94 | 'contact_id' => $this->_contactId, | |
6a488035 | 95 | ); |
7fbb4198 | 96 | $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
97 | foreach ($result['values'] as $v) { |
98 | $this->assertEquals($v['title'], $this->_group[$v['group_id']]['title']); | |
99 | $this->assertEquals($v['visibility'], $this->_group[$v['group_id']]['visibility']); | |
100 | $this->assertEquals($v['in_method'], $this->_group[$v['group_id']]['in_method']); | |
101 | } | |
102 | } | |
103 | ||
00be9182 | 104 | public function testGetGroupID() { |
5c49fee0 | 105 | $description = "Get all from group and display contacts."; |
92915c55 TO |
106 | $subfile = "GetWithGroupID"; |
107 | $params = array( | |
6a488035 | 108 | 'group_id' => $this->_groupId1, |
6a488035 TO |
109 | 'api.group.get' => 1, |
110 | 'sequential' => 1, | |
111 | ); | |
7fbb4198 | 112 | $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
113 | foreach ($result['values'][0]['api.group.get']['values'] as $values) { |
114 | $key = $values['id']; | |
115 | $this->assertEquals($values['title'], $this->_group[$key]['title']); | |
116 | $this->assertEquals($values['visibility'], $this->_group[$key]['visibility']); | |
117 | } | |
118 | } | |
119 | ||
00be9182 | 120 | public function testCreateWithEmptyParams() { |
6a488035 | 121 | $params = array(); |
d0e1eff2 | 122 | $groups = $this->callAPIFailure('group_contact', 'create', $params); |
6a488035 | 123 | $this->assertEquals($groups['error_message'], |
d0e1eff2 | 124 | 'Mandatory key(s) missing from params array: group_id, contact_id' |
6a488035 TO |
125 | ); |
126 | } | |
127 | ||
00be9182 | 128 | public function testCreateWithoutGroupIdParams() { |
6a488035 | 129 | $params = array( |
92915c55 TO |
130 | 'contact_id' => $this->_contactId, |
131 | ); | |
6a488035 | 132 | |
d0e1eff2 | 133 | $groups = $this->callAPIFailure('group_contact', 'create', $params); |
6a488035 TO |
134 | $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id'); |
135 | } | |
136 | ||
00be9182 | 137 | public function testCreateWithoutContactIdParams() { |
6a488035 | 138 | $params = array( |
92915c55 TO |
139 | 'group_id' => $this->_groupId1, |
140 | ); | |
d0e1eff2 | 141 | $groups = $this->callAPIFailure('group_contact', 'create', $params); |
6a488035 TO |
142 | $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id'); |
143 | } | |
144 | ||
00be9182 | 145 | public function testCreate() { |
6a488035 TO |
146 | $cont = array( |
147 | 'first_name' => 'Amiteshwar', | |
148 | 'middle_name' => 'L.', | |
149 | 'last_name' => 'Prasad', | |
150 | 'prefix_id' => 3, | |
151 | 'suffix_id' => 3, | |
152 | 'email' => 'amiteshwar.prasad@civicrm.org', | |
92915c55 TO |
153 | 'contact_type' => 'Individual', |
154 | ); | |
6a488035 TO |
155 | |
156 | $this->_contactId1 = $this->individualCreate($cont); | |
157 | $params = array( | |
158 | 'contact_id' => $this->_contactId, | |
159 | 'contact_id.2' => $this->_contactId1, | |
160 | 'group_id' => $this->_groupId1, | |
6a488035 TO |
161 | ); |
162 | ||
7fbb4198 | 163 | $result = $this->callAPIAndDocument('group_contact', 'create', $params, __FUNCTION__, __FILE__); |
97089930 | 164 | $this->assertEquals($result['not_added'], 1); |
165 | $this->assertEquals($result['added'], 1); | |
166 | $this->assertEquals($result['total_count'], 2); | |
6a488035 TO |
167 | } |
168 | ||
389bcebf | 169 | /** |
36e590d4 | 170 | * Test GroupContact.delete by contact+group ID. |
389bcebf | 171 | */ |
00be9182 | 172 | public function testDelete() { |
6a488035 TO |
173 | $params = array( |
174 | 'contact_id' => $this->_contactId, | |
10f7c4d2 | 175 | 'group_id' => $this->_groupId1, |
6a488035 TO |
176 | ); |
177 | ||
7fbb4198 | 178 | $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__); |
97089930 | 179 | $this->assertEquals($result['removed'], 1); |
180 | $this->assertEquals($result['total_count'], 1); | |
6a488035 | 181 | } |
3d700d00 | 182 | |
00be9182 | 183 | public function testDeletePermanent() { |
a9739e5d | 184 | $result = $this->callAPISuccess('group_contact', 'get', array('contact_id' => $this->_contactId)); |
3d700d00 | 185 | $params = array( |
a9739e5d | 186 | 'id' => $result['id'], |
3d700d00 CW |
187 | 'skip_undelete' => TRUE, |
188 | ); | |
189 | $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__); | |
190 | $result = $this->callAPISuccess('group_contact', 'get', $params); | |
97089930 | 191 | $this->assertEquals(0, $result['count']); |
192 | $this->assertArrayNotHasKey('id', $result); | |
3d700d00 | 193 | } |
96025800 | 194 | |
3c64f1f6 SL |
195 | /** |
196 | * CRM-19496 When id is used rather than contact_id and group_id ensure that remove function still works. | |
197 | * | |
198 | */ | |
199 | public function testDeleteWithId() { | |
200 | $groupContactParams = array( | |
201 | 'contact_id' => $this->_contactId, | |
202 | 'group_id' => $this->_groupId1, | |
203 | ); | |
204 | $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams); | |
205 | $params = array( | |
206 | 'id' => $groupContact['id'], | |
207 | 'status' => 'Removed', | |
208 | ); | |
209 | $result = $this->callAPISuccess('group_contact', 'delete', $params); | |
210 | $this->assertEquals($result['removed'], 1); | |
211 | $this->assertEquals($result['total_count'], 1); | |
212 | } | |
213 | ||
214 | /** | |
215 | * CRM-19496 When id is used rather than contact_id and group_id ensure that remove function still works. | |
216 | * | |
217 | */ | |
218 | public function testDeleteAndReAddWithId() { | |
219 | $groupContactParams = array( | |
220 | 'contact_id' => $this->_contactId, | |
221 | 'group_id' => $this->_groupId1, | |
222 | ); | |
223 | $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams); | |
224 | $params = array( | |
225 | 'id' => $groupContact['id'], | |
226 | 'status' => 'Removed', | |
227 | ); | |
228 | $result = $this->callAPISuccess('group_contact', 'delete', $params); | |
229 | $this->assertEquals($result['removed'], 1); | |
230 | $this->assertEquals($result['total_count'], 1); | |
231 | $params = array_merge($params, array('status' => 'Added')); | |
232 | $result2 = $this->callAPISuccess('group_contact', 'delete', $params); | |
233 | $this->assertEquals($result2['added'], 1); | |
234 | $this->assertEquals($result2['total_count'], 1); | |
235 | } | |
236 | ||
85df4a81 SL |
237 | /** |
238 | * CRM-19979 test that group cotnact delete action works when contact is in status of pendin. | |
239 | */ | |
240 | public function testDeleteWithPending() { | |
241 | $groupId3 = $this->groupCreate(array( | |
242 | 'name' => 'Test Group 3', | |
243 | 'domain_id' => 1, | |
244 | 'title' => 'New Test Group3 Created', | |
245 | 'description' => 'New Test Group3 Created', | |
246 | 'is_active' => 1, | |
247 | 'visibility' => 'User and User Admin Only', | |
248 | )); | |
249 | $groupContactCreateParams = array( | |
250 | 'contact_id' => $this->_contactId, | |
251 | 'group_id' => $groupId3, | |
252 | 'status' => 'Pending', | |
253 | ); | |
254 | $groupContact = $this->callAPISuccess('groupContact', 'create', $groupContactCreateParams); | |
255 | $groupGetContact = $this->CallAPISuccess('groupContact', 'get', $groupContactCreateParams); | |
256 | $this->callAPISuccess('groupContact', 'delete', array('id' => $groupGetContact['id'], 'status' => 'Removed')); | |
bd9042db SL |
257 | $this->callAPISuccess('groupContact', 'delete', array('id' => $groupGetContact['id'], 'skip_undelete' => TRUE)); |
258 | $this->callAPISuccess('group', 'delete', array('id' => $groupId3)); | |
259 | } | |
260 | ||
261 | /** | |
262 | * CRM-19979 test that group cotnact delete action works when contact is in status of pendin and is a permanent delete. | |
263 | */ | |
264 | public function testPermanentDeleteWithPending() { | |
265 | $groupId3 = $this->groupCreate(array( | |
266 | 'name' => 'Test Group 3', | |
267 | 'domain_id' => 1, | |
268 | 'title' => 'New Test Group3 Created', | |
269 | 'description' => 'New Test Group3 Created', | |
270 | 'is_active' => 1, | |
271 | 'visibility' => 'User and User Admin Only', | |
272 | )); | |
273 | $groupContactCreateParams = array( | |
274 | 'contact_id' => $this->_contactId, | |
275 | 'group_id' => $groupId3, | |
276 | 'status' => 'Pending', | |
277 | ); | |
278 | $groupContact = $this->callAPISuccess('groupContact', 'create', $groupContactCreateParams); | |
279 | $groupGetContact = $this->CallAPISuccess('groupContact', 'get', $groupContactCreateParams); | |
280 | $this->callAPISuccess('groupContact', 'delete', array('id' => $groupGetContact['id'], 'skip_undelete' => TRUE)); | |
281 | $this->callAPISuccess('group', 'delete', array('id' => $groupId3)); | |
85df4a81 SL |
282 | } |
283 | ||
242e73d4 | 284 | /** |
285 | * CRM-16945 duplicate groups are showing up when contacts are hard-added to child groups or smart groups. | |
286 | * | |
287 | * Fix documented in | |
288 | * | |
289 | * Test illustrates this (& ensures once fixed it will stay fixed). | |
290 | */ | |
291 | public function testAccurateCountWithSmartGroups() { | |
292 | $childGroupID = $this->groupCreate(array( | |
293 | 'name' => 'Child group', | |
294 | 'domain_id' => 1, | |
295 | 'title' => 'Child group', | |
296 | 'description' => 'Child group', | |
297 | 'is_active' => 1, | |
298 | 'parents' => $this->_groupId1, | |
299 | 'visibility' => 'User and User Admin Only', | |
300 | )); | |
301 | ||
302 | $params = array( | |
303 | 'name' => 'Individuals', | |
304 | 'title' => 'Individuals', | |
305 | 'is_active' => 1, | |
306 | 'parents' => $this->_groupId1, | |
307 | 'formValues' => array('contact_type' => 'Goat'), | |
308 | ); | |
309 | $smartGroup2 = CRM_Contact_BAO_Group::createSmartGroup($params); | |
310 | ||
311 | $this->callAPISuccess('GroupContact', 'create', array('contact_id' => $this->_contactId, 'status' => 'Added', 'group_id' => $this->_groupId2)); | |
312 | $this->callAPISuccess('GroupContact', 'create', array('contact_id' => $this->_contactId, 'status' => 'Added', 'group_id' => $smartGroup2->id)); | |
313 | $this->callAPISuccess('GroupContact', 'create', array('contact_id' => $this->_contactId, 'status' => 'Added', 'group_id' => $childGroupID)); | |
314 | $groups = $this->callAPISuccess('GroupContact', 'get', array('contact_id' => $this->_contactId)); | |
315 | ||
316 | // Although the contact is actually hard-added to 4 groups the smart groups are conventionally not returned by the api or displayed | |
317 | // on the main part of the groups tab on the contact (which calls the same function. So, 3 groups is an OK number to return. | |
318 | // However, as of writing this test 4 groups are returned (indexed by group_contact_id, but more seriously 3/4 of those have the group id 1 | |
319 | // so 2 on them have group ids that do not match the group contact id they have been keyed by. | |
320 | foreach ($groups['values'] as $groupContactID => $groupContactRecord) { | |
321 | $this->assertEquals($groupContactRecord['group_id'], CRM_Core_DAO::singleValueQuery("SELECT group_id FROM civicrm_group_contact WHERE id = $groupContactID"), 'Group contact record mis-returned for id ' . $groupContactID); | |
322 | } | |
323 | $this->assertEquals(3, $groups['count']); | |
324 | ||
325 | } | |
326 | ||
6a488035 | 327 | } |