3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-16945 duplicate groups are showing up when contacts are hard-added to child groups or smart groups.
204 * Test illustrates this (& ensures once fixed it will stay fixed).
206 public function testAccurateCountWithSmartGroups() {
207 $childGroupID = $this->groupCreate(array(
208 'name' => 'Child group',
210 'title' => 'Child group',
211 'description' => 'Child group',
213 'parents' => $this->_groupId1
,
214 'visibility' => 'User and User Admin Only',
218 'name' => 'Individuals',
219 'title' => 'Individuals',
221 'parents' => $this->_groupId1
,
222 'formValues' => array('contact_type' => 'Goat'),
224 $smartGroup2 = CRM_Contact_BAO_Group
::createSmartGroup($params);
226 $this->callAPISuccess('GroupContact', 'create', array('contact_id' => $this->_contactId
, 'status' => 'Added', 'group_id' => $this->_groupId2
));
227 $this->callAPISuccess('GroupContact', 'create', array('contact_id' => $this->_contactId
, 'status' => 'Added', 'group_id' => $smartGroup2->id
));
228 $this->callAPISuccess('GroupContact', 'create', array('contact_id' => $this->_contactId
, 'status' => 'Added', 'group_id' => $childGroupID));
229 $groups = $this->callAPISuccess('GroupContact', 'get', array('contact_id' => $this->_contactId
));
231 // Although the contact is actually hard-added to 4 groups the smart groups are conventionally not returned by the api or displayed
232 // 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.
233 // 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
234 // so 2 on them have group ids that do not match the group contact id they have been keyed by.
235 foreach ($groups['values'] as $groupContactID => $groupContactRecord) {
236 $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);
238 $this->assertEquals(3, $groups['count']);