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',
90 * Test GroupContact.get by ID.
92 public function testGet() {
94 'contact_id' => $this->_contactId
,
96 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__
, __FILE__
);
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']);
104 public function testGetGroupID() {
105 $description = "Get all from group and display contacts.";
106 $subfile = "GetWithGroupID";
108 'group_id' => $this->_groupId1
,
109 'api.group.get' => 1,
112 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
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']);
120 public function testCreateWithEmptyParams() {
122 $groups = $this->callAPIFailure('group_contact', 'create', $params);
123 $this->assertEquals($groups['error_message'],
124 'Mandatory key(s) missing from params array: group_id, contact_id'
128 public function testCreateWithoutGroupIdParams() {
130 'contact_id' => $this->_contactId
,
133 $groups = $this->callAPIFailure('group_contact', 'create', $params);
134 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
137 public function testCreateWithoutContactIdParams() {
139 'group_id' => $this->_groupId1
,
141 $groups = $this->callAPIFailure('group_contact', 'create', $params);
142 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
145 public function testCreate() {
147 'first_name' => 'Amiteshwar',
148 'middle_name' => 'L.',
149 'last_name' => 'Prasad',
152 'email' => 'amiteshwar.prasad@civicrm.org',
153 'contact_type' => 'Individual',
156 $this->_contactId1
= $this->individualCreate($cont);
158 'contact_id' => $this->_contactId
,
159 'contact_id.2' => $this->_contactId1
,
160 'group_id' => $this->_groupId1
,
163 $result = $this->callAPIAndDocument('group_contact', 'create', $params, __FUNCTION__
, __FILE__
);
164 $this->assertEquals($result['not_added'], 1);
165 $this->assertEquals($result['added'], 1);
166 $this->assertEquals($result['total_count'], 2);
170 * Test GroupContact.delete by contact+group ID.
172 public function testDelete() {
174 'contact_id' => $this->_contactId
,
175 'group_id' => $this->_groupId1
,
178 $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__
, __FILE__
);
179 $this->assertEquals($result['removed'], 1);
180 $this->assertEquals($result['total_count'], 1);
183 public function testDeletePermanent() {
184 $result = $this->callAPISuccess('group_contact', 'get', array('contact_id' => $this->_contactId
));
186 'id' => $result['id'],
187 'skip_undelete' => TRUE,
189 $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__
, __FILE__
);
190 $result = $this->callAPISuccess('group_contact', 'get', $params);
191 $this->assertEquals(0, $result['count']);
192 $this->assertArrayNotHasKey('id', $result);
196 * CRM-19496 When id is used rather than contact_id and group_id ensure that remove function still works.
199 public function testDeleteWithId() {
200 $groupContactParams = array(
201 'contact_id' => $this->_contactId
,
202 'group_id' => $this->_groupId1
,
204 $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams);
206 'id' => $groupContact['id'],
207 'status' => 'Removed',
209 $result = $this->callAPISuccess('group_contact', 'delete', $params);
210 $this->assertEquals($result['removed'], 1);
211 $this->assertEquals($result['total_count'], 1);
215 * CRM-19496 When id is used rather than contact_id and group_id ensure that remove function still works.
218 public function testDeleteAndReAddWithId() {
219 $groupContactParams = array(
220 'contact_id' => $this->_contactId
,
221 'group_id' => $this->_groupId1
,
223 $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams);
225 'id' => $groupContact['id'],
226 'status' => 'Removed',
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);
238 * CRM-19979 test that group cotnact delete action works when contact is in status of pendin.
240 public function testDeleteWithPending() {
241 $groupId3 = $this->groupCreate(array(
242 'name' => 'Test Group 3',
244 'title' => 'New Test Group3 Created',
245 'description' => 'New Test Group3 Created',
247 'visibility' => 'User and User Admin Only',
249 $groupContactCreateParams = array(
250 'contact_id' => $this->_contactId
,
251 'group_id' => $groupId3,
252 'status' => 'Pending',
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'));
257 $this->callAPISuccess('groupContact', 'delete', array('id' => $groupGetContact['id'], 'skip_undelete' => TRUE));
258 $this->callAPISuccess('group', 'delete', array('id' => $groupId3));
262 * CRM-19979 test that group cotnact delete action works when contact is in status of pendin and is a permanent delete.
264 public function testPermanentDeleteWithPending() {
265 $groupId3 = $this->groupCreate(array(
266 'name' => 'Test Group 3',
268 'title' => 'New Test Group3 Created',
269 'description' => 'New Test Group3 Created',
271 'visibility' => 'User and User Admin Only',
273 $groupContactCreateParams = array(
274 'contact_id' => $this->_contactId
,
275 'group_id' => $groupId3,
276 'status' => 'Pending',
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));
285 * CRM-16945 duplicate groups are showing up when contacts are hard-added to child groups or smart groups.
289 * Test illustrates this (& ensures once fixed it will stay fixed).
291 public function testAccurateCountWithSmartGroups() {
292 $childGroupID = $this->groupCreate(array(
293 'name' => 'Child group',
295 'title' => 'Child group',
296 'description' => 'Child group',
298 'parents' => $this->_groupId1
,
299 'visibility' => 'User and User Admin Only',
303 'name' => 'Individuals',
304 'title' => 'Individuals',
306 'parents' => $this->_groupId1
,
307 'formValues' => array('contact_type' => 'Goat'),
309 $smartGroup2 = CRM_Contact_BAO_Group
::createSmartGroup($params);
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
));
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);
323 $this->assertEquals(3, $groups['count']);