3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Class api_v3_GroupContactTest
34 class api_v3_GroupContactTest
extends CiviUnitTestCase
{
36 protected $_contactId;
37 protected $_contactId1;
38 protected $_apiversion = 3;
51 * Set up for group contact tests.
53 * @todo set up calls function that doesn't work @ the moment
55 public function setUp() {
57 $this->useTransaction(TRUE);
59 $this->_contactId
= $this->individualCreate();
61 $this->_groupId1
= $this->groupCreate();
63 $this->callAPISuccess('group_contact', 'create', array(
64 'contact_id' => $this->_contactId
,
65 'group_id' => $this->_groupId1
,
68 $this->_groupId2
= $this->groupCreate(array(
69 'name' => 'Test Group 2',
71 'title' => 'New Test Group2 Created',
72 'description' => 'New Test Group2 Created',
74 'visibility' => 'User and User Admin Only',
77 $this->_group
= array(
78 $this->_groupId1
=> array(
79 'title' => 'New Test Group Created',
80 'visibility' => 'Public Pages',
83 $this->_groupId2
=> array(
84 'title' => 'New Test Group2 Created',
85 'visibility' => 'User and User Admin Only',
91 ///////////////// civicrm_group_contact_get methods
94 * Test GroupContact.get by ID.
96 public function testGet() {
98 'contact_id' => $this->_contactId
,
100 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__
, __FILE__
);
101 foreach ($result['values'] as $v) {
102 $this->assertEquals($v['title'], $this->_group
[$v['group_id']]['title']);
103 $this->assertEquals($v['visibility'], $this->_group
[$v['group_id']]['visibility']);
104 $this->assertEquals($v['in_method'], $this->_group
[$v['group_id']]['in_method']);
108 public function testGetGroupID() {
109 $description = "Get all from group and display contacts.";
110 $subfile = "GetWithGroupID";
112 'group_id' => $this->_groupId1
,
113 'api.group.get' => 1,
116 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
117 foreach ($result['values'][0]['api.group.get']['values'] as $values) {
118 $key = $values['id'];
119 $this->assertEquals($values['title'], $this->_group
[$key]['title']);
120 $this->assertEquals($values['visibility'], $this->_group
[$key]['visibility']);
124 public function testCreateWithEmptyParams() {
126 $groups = $this->callAPIFailure('group_contact', 'create', $params);
127 $this->assertEquals($groups['error_message'],
128 'Mandatory key(s) missing from params array: group_id, contact_id'
132 public function testCreateWithoutGroupIdParams() {
134 'contact_id' => $this->_contactId
,
137 $groups = $this->callAPIFailure('group_contact', 'create', $params);
138 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
141 public function testCreateWithoutContactIdParams() {
143 'group_id' => $this->_groupId1
,
145 $groups = $this->callAPIFailure('group_contact', 'create', $params);
146 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
149 public function testCreate() {
151 'first_name' => 'Amiteshwar',
152 'middle_name' => 'L.',
153 'last_name' => 'Prasad',
156 'email' => 'amiteshwar.prasad@civicrm.org',
157 'contact_type' => 'Individual',
160 $this->_contactId1
= $this->individualCreate($cont);
162 'contact_id' => $this->_contactId
,
163 'contact_id.2' => $this->_contactId1
,
164 'group_id' => $this->_groupId1
,
167 $result = $this->callAPIAndDocument('group_contact', 'create', $params, __FUNCTION__
, __FILE__
);
168 $this->assertEquals($result['not_added'], 1);
169 $this->assertEquals($result['added'], 1);
170 $this->assertEquals($result['total_count'], 2);
173 ///////////////// civicrm_group_contact_remove methods
176 * Test GroupContact.delete by contact+group ID.
178 public function testDelete() {
180 'contact_id' => $this->_contactId
,
181 'group_id' => $this->_groupId1
,
184 $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__
, __FILE__
);
185 $this->assertEquals($result['removed'], 1);
186 $this->assertEquals($result['total_count'], 1);
189 public function testDeletePermanent() {
190 $result = $this->callAPISuccess('group_contact', 'get', array('contact_id' => $this->_contactId
));
192 'id' => $result['id'],
193 'skip_undelete' => TRUE,
195 $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__
, __FILE__
);
196 $result = $this->callAPISuccess('group_contact', 'get', $params);
197 $this->assertEquals(0, $result['count']);
198 $this->assertArrayNotHasKey('id', $result);
202 * CRM-16945 duplicate groups are showing up when contacts are hard-added to child groups or smart groups.
206 * Test illustrates this (& ensures once fixed it will stay fixed).
208 public function testAccurateCountWithSmartGroups() {
209 $childGroupID = $this->groupCreate(array(
210 'name' => 'Child group',
212 'title' => 'Child group',
213 'description' => 'Child group',
215 'parents' => $this->_groupId1
,
216 'visibility' => 'User and User Admin Only',
220 'name' => 'Individuals',
221 'title' => 'Individuals',
223 'parents' => $this->_groupId1
,
224 'formValues' => array('contact_type' => 'Goat'),
226 $smartGroup2 = CRM_Contact_BAO_Group
::createSmartGroup($params);
228 $this->callAPISuccess('GroupContact', 'create', array('contact_id' => $this->_contactId
, 'status' => 'Added', 'group_id' => $this->_groupId2
));
229 $this->callAPISuccess('GroupContact', 'create', array('contact_id' => $this->_contactId
, 'status' => 'Added', 'group_id' => $smartGroup2->id
));
230 $this->callAPISuccess('GroupContact', 'create', array('contact_id' => $this->_contactId
, 'status' => 'Added', 'group_id' => $childGroupID));
231 $groups = $this->callAPISuccess('GroupContact', 'get', array('contact_id' => $this->_contactId
));
233 // Although the contact is actually hard-added to 4 groups the smart groups are conventionally not returned by the api or displayed
234 // 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.
235 // 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
236 // so 2 on them have group ids that do not match the group contact id they have been keyed by.
237 foreach ($groups['values'] as $groupContactID => $groupContactRecord) {
238 $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);
240 $this->assertEquals(3, $groups['count']);