Merge pull request #10155 from totten/master-pdf-save2
[civicrm-core.git] / tests / phpunit / api / v3 / GroupContactTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
15a4309a 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
32class 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
36e590d4
TO
89 ///////////////// civicrm_group_contact_get methods
90
389bcebf 91 /**
36e590d4 92 * Test GroupContact.get by ID.
389bcebf 93 */
00be9182 94 public function testGet() {
6a488035
TO
95 $params = array(
96 'contact_id' => $this->_contactId,
6a488035 97 );
7fbb4198 98 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
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']);
103 }
104 }
105
00be9182 106 public function testGetGroupID() {
5c49fee0 107 $description = "Get all from group and display contacts.";
92915c55
TO
108 $subfile = "GetWithGroupID";
109 $params = array(
6a488035 110 'group_id' => $this->_groupId1,
6a488035
TO
111 'api.group.get' => 1,
112 'sequential' => 1,
113 );
7fbb4198 114 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
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']);
119 }
120 }
121
00be9182 122 public function testCreateWithEmptyParams() {
6a488035 123 $params = array();
d0e1eff2 124 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035 125 $this->assertEquals($groups['error_message'],
d0e1eff2 126 'Mandatory key(s) missing from params array: group_id, contact_id'
6a488035
TO
127 );
128 }
129
00be9182 130 public function testCreateWithoutGroupIdParams() {
6a488035 131 $params = array(
92915c55
TO
132 'contact_id' => $this->_contactId,
133 );
6a488035 134
d0e1eff2 135 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035
TO
136 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
137 }
138
00be9182 139 public function testCreateWithoutContactIdParams() {
6a488035 140 $params = array(
92915c55
TO
141 'group_id' => $this->_groupId1,
142 );
d0e1eff2 143 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035
TO
144 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
145 }
146
00be9182 147 public function testCreate() {
6a488035
TO
148 $cont = array(
149 'first_name' => 'Amiteshwar',
150 'middle_name' => 'L.',
151 'last_name' => 'Prasad',
152 'prefix_id' => 3,
153 'suffix_id' => 3,
154 'email' => 'amiteshwar.prasad@civicrm.org',
92915c55
TO
155 'contact_type' => 'Individual',
156 );
6a488035
TO
157
158 $this->_contactId1 = $this->individualCreate($cont);
159 $params = array(
160 'contact_id' => $this->_contactId,
161 'contact_id.2' => $this->_contactId1,
162 'group_id' => $this->_groupId1,
6a488035
TO
163 );
164
7fbb4198 165 $result = $this->callAPIAndDocument('group_contact', 'create', $params, __FUNCTION__, __FILE__);
97089930 166 $this->assertEquals($result['not_added'], 1);
167 $this->assertEquals($result['added'], 1);
168 $this->assertEquals($result['total_count'], 2);
6a488035
TO
169 }
170
36e590d4
TO
171 ///////////////// civicrm_group_contact_remove methods
172
389bcebf 173 /**
36e590d4 174 * Test GroupContact.delete by contact+group ID.
389bcebf 175 */
00be9182 176 public function testDelete() {
6a488035
TO
177 $params = array(
178 'contact_id' => $this->_contactId,
10f7c4d2 179 'group_id' => $this->_groupId1,
6a488035
TO
180 );
181
7fbb4198 182 $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
97089930 183 $this->assertEquals($result['removed'], 1);
184 $this->assertEquals($result['total_count'], 1);
6a488035 185 }
3d700d00 186
00be9182 187 public function testDeletePermanent() {
a9739e5d 188 $result = $this->callAPISuccess('group_contact', 'get', array('contact_id' => $this->_contactId));
3d700d00 189 $params = array(
a9739e5d 190 'id' => $result['id'],
3d700d00
CW
191 'skip_undelete' => TRUE,
192 );
193 $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
194 $result = $this->callAPISuccess('group_contact', 'get', $params);
97089930 195 $this->assertEquals(0, $result['count']);
196 $this->assertArrayNotHasKey('id', $result);
3d700d00 197 }
96025800 198
3c64f1f6
SL
199 /**
200 * CRM-19496 When id is used rather than contact_id and group_id ensure that remove function still works.
201 *
202 */
203 public function testDeleteWithId() {
204 $groupContactParams = array(
205 'contact_id' => $this->_contactId,
206 'group_id' => $this->_groupId1,
207 );
208 $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams);
209 $params = array(
210 'id' => $groupContact['id'],
211 'status' => 'Removed',
212 );
213 $result = $this->callAPISuccess('group_contact', 'delete', $params);
214 $this->assertEquals($result['removed'], 1);
215 $this->assertEquals($result['total_count'], 1);
216 }
217
218 /**
219 * CRM-19496 When id is used rather than contact_id and group_id ensure that remove function still works.
220 *
221 */
222 public function testDeleteAndReAddWithId() {
223 $groupContactParams = array(
224 'contact_id' => $this->_contactId,
225 'group_id' => $this->_groupId1,
226 );
227 $groupContact = $this->callAPISuccess('group_contact', 'get', $groupContactParams);
228 $params = array(
229 'id' => $groupContact['id'],
230 'status' => 'Removed',
231 );
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);
239 }
240
85df4a81
SL
241 /**
242 * CRM-19979 test that group cotnact delete action works when contact is in status of pendin.
243 */
244 public function testDeleteWithPending() {
245 $groupId3 = $this->groupCreate(array(
246 'name' => 'Test Group 3',
247 'domain_id' => 1,
248 'title' => 'New Test Group3 Created',
249 'description' => 'New Test Group3 Created',
250 'is_active' => 1,
251 'visibility' => 'User and User Admin Only',
252 ));
253 $groupContactCreateParams = array(
254 'contact_id' => $this->_contactId,
255 'group_id' => $groupId3,
256 'status' => 'Pending',
257 );
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'));
bd9042db
SL
261 $this->callAPISuccess('groupContact', 'delete', array('id' => $groupGetContact['id'], 'skip_undelete' => TRUE));
262 $this->callAPISuccess('group', 'delete', array('id' => $groupId3));
263 }
264
265 /**
266 * CRM-19979 test that group cotnact delete action works when contact is in status of pendin and is a permanent delete.
267 */
268 public function testPermanentDeleteWithPending() {
269 $groupId3 = $this->groupCreate(array(
270 'name' => 'Test Group 3',
271 'domain_id' => 1,
272 'title' => 'New Test Group3 Created',
273 'description' => 'New Test Group3 Created',
274 'is_active' => 1,
275 'visibility' => 'User and User Admin Only',
276 ));
277 $groupContactCreateParams = array(
278 'contact_id' => $this->_contactId,
279 'group_id' => $groupId3,
280 'status' => 'Pending',
281 );
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));
85df4a81
SL
286 }
287
242e73d4 288 /**
289 * CRM-16945 duplicate groups are showing up when contacts are hard-added to child groups or smart groups.
290 *
291 * Fix documented in
292 *
293 * Test illustrates this (& ensures once fixed it will stay fixed).
294 */
295 public function testAccurateCountWithSmartGroups() {
296 $childGroupID = $this->groupCreate(array(
297 'name' => 'Child group',
298 'domain_id' => 1,
299 'title' => 'Child group',
300 'description' => 'Child group',
301 'is_active' => 1,
302 'parents' => $this->_groupId1,
303 'visibility' => 'User and User Admin Only',
304 ));
305
306 $params = array(
307 'name' => 'Individuals',
308 'title' => 'Individuals',
309 'is_active' => 1,
310 'parents' => $this->_groupId1,
311 'formValues' => array('contact_type' => 'Goat'),
312 );
313 $smartGroup2 = CRM_Contact_BAO_Group::createSmartGroup($params);
314
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));
319
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);
326 }
327 $this->assertEquals(3, $groups['count']);
328
329 }
330
6a488035 331}