3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class api_v3_GroupContactTest
32 class api_v3_GroupContactTest
extends CiviUnitTestCase
{
34 protected $_contactId;
35 protected $_contactId1;
36 protected $_apiversion = 3;
49 * Set up for group contact tests.
51 * @todo set up calls function that doesn't work @ the moment
53 public function setUp() {
55 $this->useTransaction(TRUE);
57 $this->_contactId
= $this->individualCreate();
59 $this->_groupId1
= $this->groupCreate();
61 $this->callAPISuccess('group_contact', 'create', array(
62 'contact_id' => $this->_contactId
,
63 'group_id' => $this->_groupId1
,
66 $this->_groupId2
= $this->groupCreate(array(
67 'name' => 'Test Group 2',
69 'title' => 'New Test Group2 Created',
70 'description' => 'New Test Group2 Created',
72 'visibility' => 'User and User Admin Only',
75 $this->_group
= array(
76 $this->_groupId1
=> array(
77 'title' => 'New Test Group Created',
78 'visibility' => 'Public Pages',
81 $this->_groupId2
=> array(
82 'title' => 'New Test Group2 Created',
83 'visibility' => 'User and User Admin Only',
89 ///////////////// civicrm_group_contact_get methods
92 * Test GroupContact.get by ID.
94 public function testGet() {
96 'contact_id' => $this->_contactId
,
98 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__
, __FILE__
);
99 foreach ($result['values'] as $v) {
100 $this->assertEquals($v['title'], $this->_group
[$v['group_id']]['title']);
101 $this->assertEquals($v['visibility'], $this->_group
[$v['group_id']]['visibility']);
102 $this->assertEquals($v['in_method'], $this->_group
[$v['group_id']]['in_method']);
106 public function testGetGroupID() {
107 $description = "Get all from group and display contacts.";
108 $subfile = "GetWithGroupID";
110 'group_id' => $this->_groupId1
,
111 'api.group.get' => 1,
114 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
115 foreach ($result['values'][0]['api.group.get']['values'] as $values) {
116 $key = $values['id'];
117 $this->assertEquals($values['title'], $this->_group
[$key]['title']);
118 $this->assertEquals($values['visibility'], $this->_group
[$key]['visibility']);
122 public function testCreateWithEmptyParams() {
124 $groups = $this->callAPIFailure('group_contact', 'create', $params);
125 $this->assertEquals($groups['error_message'],
126 'Mandatory key(s) missing from params array: group_id, contact_id'
130 public function testCreateWithoutGroupIdParams() {
132 'contact_id' => $this->_contactId
,
135 $groups = $this->callAPIFailure('group_contact', 'create', $params);
136 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
139 public function testCreateWithoutContactIdParams() {
141 'group_id' => $this->_groupId1
,
143 $groups = $this->callAPIFailure('group_contact', 'create', $params);
144 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
147 public function testCreate() {
149 'first_name' => 'Amiteshwar',
150 'middle_name' => 'L.',
151 'last_name' => 'Prasad',
154 'email' => 'amiteshwar.prasad@civicrm.org',
155 'contact_type' => 'Individual',
158 $this->_contactId1
= $this->individualCreate($cont);
160 'contact_id' => $this->_contactId
,
161 'contact_id.2' => $this->_contactId1
,
162 'group_id' => $this->_groupId1
,
165 $result = $this->callAPIAndDocument('group_contact', 'create', $params, __FUNCTION__
, __FILE__
);
166 $this->assertEquals($result['not_added'], 1);
167 $this->assertEquals($result['added'], 1);
168 $this->assertEquals($result['total_count'], 2);
171 ///////////////// civicrm_group_contact_remove methods
174 * Test GroupContact.delete by contact+group ID.
176 public function testDelete() {
178 'contact_id' => $this->_contactId
,
179 'group_id' => $this->_groupId1
,
182 $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__
, __FILE__
);
183 $this->assertEquals($result['removed'], 1);
184 $this->assertEquals($result['total_count'], 1);
187 public function testDeletePermanent() {
188 $result = $this->callAPISuccess('group_contact', 'get', array('contact_id' => $this->_contactId
));
190 'id' => $result['id'],
191 'skip_undelete' => TRUE,
193 $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__
, __FILE__
);
194 $result = $this->callAPISuccess('group_contact', 'get', $params);
195 $this->assertEquals(0, $result['count']);
196 $this->assertArrayNotHasKey('id', $result);
200 * CRM-19496 When id is used rather than contact_id and group_id ensure that remove function still works.
203 public function testDeleteWithId() {
204 $groupContactParams = array(
205 'contact_id' => $this->_contactId
,
206 'group_id' => $this->_groupId1
,
208 $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams);
210 'id' => $groupContact['id'],
211 'status' => 'Removed',
213 $result = $this->callAPISuccess('group_contact', 'delete', $params);
214 $this->assertEquals($result['removed'], 1);
215 $this->assertEquals($result['total_count'], 1);
219 * CRM-19496 When id is used rather than contact_id and group_id ensure that remove function still works.
222 public function testDeleteAndReAddWithId() {
223 $groupContactParams = array(
224 'contact_id' => $this->_contactId
,
225 'group_id' => $this->_groupId1
,
227 $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams);
229 'id' => $groupContact['id'],
230 'status' => 'Removed',
232 $result = $this->callAPISuccess('group_contact', 'delete', $params);
233 $this->assertEquals($result['removed'], 1);
234 $this->assertEquals($result['total_count'], 1);
235 $params = array_merge($params, array('status' => 'Added'));
236 $result2 = $this->callAPISuccess('group_contact', 'delete', $params);
237 $this->assertEquals($result2['added'], 1);
238 $this->assertEquals($result2['total_count'], 1);
242 * CRM-19979 test that group cotnact delete action works when contact is in status of pendin.
244 public function testDeleteWithPending() {
245 $groupId3 = $this->groupCreate(array(
246 'name' => 'Test Group 3',
248 'title' => 'New Test Group3 Created',
249 'description' => 'New Test Group3 Created',
251 'visibility' => 'User and User Admin Only',
253 $groupContactCreateParams = array(
254 'contact_id' => $this->_contactId
,
255 'group_id' => $groupId3,
256 'status' => 'Pending',
258 $groupContact = $this->callAPISuccess('groupContact', 'create', $groupContactCreateParams);
259 $groupGetContact = $this->CallAPISuccess('groupContact', 'get', $groupContactCreateParams);
260 $this->callAPISuccess('groupContact', 'delete', array('id' => $groupGetContact['id'], 'status' => 'Removed'));
261 $this->callAPISuccess('groupContact', 'delete', array('id' => $groupGetContact['id'], 'skip_undelete' => TRUE));
262 $this->callAPISuccess('group', 'delete', array('id' => $groupId3));
266 * CRM-19979 test that group cotnact delete action works when contact is in status of pendin and is a permanent delete.
268 public function testPermanentDeleteWithPending() {
269 $groupId3 = $this->groupCreate(array(
270 'name' => 'Test Group 3',
272 'title' => 'New Test Group3 Created',
273 'description' => 'New Test Group3 Created',
275 'visibility' => 'User and User Admin Only',
277 $groupContactCreateParams = array(
278 'contact_id' => $this->_contactId
,
279 'group_id' => $groupId3,
280 'status' => 'Pending',
282 $groupContact = $this->callAPISuccess('groupContact', 'create', $groupContactCreateParams);
283 $groupGetContact = $this->CallAPISuccess('groupContact', 'get', $groupContactCreateParams);
284 $this->callAPISuccess('groupContact', 'delete', array('id' => $groupGetContact['id'], 'skip_undelete' => TRUE));
285 $this->callAPISuccess('group', 'delete', array('id' => $groupId3));
289 * CRM-16945 duplicate groups are showing up when contacts are hard-added to child groups or smart groups.
293 * Test illustrates this (& ensures once fixed it will stay fixed).
295 public function testAccurateCountWithSmartGroups() {
296 $childGroupID = $this->groupCreate(array(
297 'name' => 'Child group',
299 'title' => 'Child group',
300 'description' => 'Child group',
302 'parents' => $this->_groupId1
,
303 'visibility' => 'User and User Admin Only',
307 'name' => 'Individuals',
308 'title' => 'Individuals',
310 'parents' => $this->_groupId1
,
311 'formValues' => array('contact_type' => 'Goat'),
313 $smartGroup2 = CRM_Contact_BAO_Group
::createSmartGroup($params);
315 $this->callAPISuccess('GroupContact', 'create', array('contact_id' => $this->_contactId
, 'status' => 'Added', 'group_id' => $this->_groupId2
));
316 $this->callAPISuccess('GroupContact', 'create', array('contact_id' => $this->_contactId
, 'status' => 'Added', 'group_id' => $smartGroup2->id
));
317 $this->callAPISuccess('GroupContact', 'create', array('contact_id' => $this->_contactId
, 'status' => 'Added', 'group_id' => $childGroupID));
318 $groups = $this->callAPISuccess('GroupContact', 'get', array('contact_id' => $this->_contactId
));
320 // Although the contact is actually hard-added to 4 groups the smart groups are conventionally not returned by the api or displayed
321 // 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.
322 // 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
323 // so 2 on them have group ids that do not match the group contact id they have been keyed by.
324 foreach ($groups['values'] as $groupContactID => $groupContactRecord) {
325 $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);
327 $this->assertEquals(3, $groups['count']);