Merge pull request #7647 from colemanw/CRM-17645
[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 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
TO
27
28
29require_once 'CiviTest/CiviUnitTestCase.php';
e9479dcf
EM
30
31/**
32 * Class api_v3_GroupContactTest
33 */
6a488035
TO
34class api_v3_GroupContactTest extends CiviUnitTestCase {
35
36 protected $_contactId;
37 protected $_contactId1;
7fbb4198 38 protected $_apiversion = 3;
97089930 39
40 /**
41 * @var int
42 */
6a488035 43 protected $_groupId1;
6a488035 44
97089930 45 /**
46 * @var int
47 */
48 protected $_groupId2;
49
c490a46a 50 /**
eceb18cc 51 * Set up for group contact tests.
c490a46a
CW
52 *
53 * @todo set up calls function that doesn't work @ the moment
54 */
00be9182 55 public function setUp() {
6a488035 56 parent::setUp();
10f7c4d2 57 $this->useTransaction(TRUE);
6a488035 58
e4d5f1e2 59 $this->_contactId = $this->individualCreate();
6a488035 60
72e41e3a 61 $this->_groupId1 = $this->groupCreate();
97089930 62
63 $this->callAPISuccess('group_contact', 'create', array(
6a488035
TO
64 'contact_id' => $this->_contactId,
65 'group_id' => $this->_groupId1,
97089930 66 ));
6a488035 67
97089930 68 $this->_groupId2 = $this->groupCreate(array(
6a488035
TO
69 'name' => 'Test Group 2',
70 'domain_id' => 1,
71 'title' => 'New Test Group2 Created',
72 'description' => 'New Test Group2 Created',
73 'is_active' => 1,
74 'visibility' => 'User and User Admin Only',
97089930 75 ));
6a488035
TO
76
77 $this->_group = array(
6c6e6187 78 $this->_groupId1 => array(
92915c55 79 'title' => 'New Test Group Created',
6a488035
TO
80 'visibility' => 'Public Pages',
81 'in_method' => 'API',
82 ),
83 $this->_groupId2 => array(
84 'title' => 'New Test Group2 Created',
85 'visibility' => 'User and User Admin Only',
86 'in_method' => 'API',
87 ),
88 );
89 }
90
36e590d4
TO
91 ///////////////// civicrm_group_contact_get methods
92
389bcebf 93 /**
36e590d4 94 * Test GroupContact.get by ID.
389bcebf 95 */
00be9182 96 public function testGet() {
6a488035
TO
97 $params = array(
98 'contact_id' => $this->_contactId,
6a488035 99 );
7fbb4198 100 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
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']);
105 }
106 }
107
00be9182 108 public function testGetGroupID() {
5c49fee0 109 $description = "Get all from group and display contacts.";
92915c55
TO
110 $subfile = "GetWithGroupID";
111 $params = array(
6a488035 112 'group_id' => $this->_groupId1,
6a488035
TO
113 'api.group.get' => 1,
114 'sequential' => 1,
115 );
7fbb4198 116 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
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']);
121 }
122 }
123
00be9182 124 public function testCreateWithEmptyParams() {
6a488035 125 $params = array();
d0e1eff2 126 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035 127 $this->assertEquals($groups['error_message'],
d0e1eff2 128 'Mandatory key(s) missing from params array: group_id, contact_id'
6a488035
TO
129 );
130 }
131
00be9182 132 public function testCreateWithoutGroupIdParams() {
6a488035 133 $params = array(
92915c55
TO
134 'contact_id' => $this->_contactId,
135 );
6a488035 136
d0e1eff2 137 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035
TO
138 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
139 }
140
00be9182 141 public function testCreateWithoutContactIdParams() {
6a488035 142 $params = array(
92915c55
TO
143 'group_id' => $this->_groupId1,
144 );
d0e1eff2 145 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035
TO
146 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
147 }
148
00be9182 149 public function testCreate() {
6a488035
TO
150 $cont = array(
151 'first_name' => 'Amiteshwar',
152 'middle_name' => 'L.',
153 'last_name' => 'Prasad',
154 'prefix_id' => 3,
155 'suffix_id' => 3,
156 'email' => 'amiteshwar.prasad@civicrm.org',
92915c55
TO
157 'contact_type' => 'Individual',
158 );
6a488035
TO
159
160 $this->_contactId1 = $this->individualCreate($cont);
161 $params = array(
162 'contact_id' => $this->_contactId,
163 'contact_id.2' => $this->_contactId1,
164 'group_id' => $this->_groupId1,
6a488035
TO
165 );
166
7fbb4198 167 $result = $this->callAPIAndDocument('group_contact', 'create', $params, __FUNCTION__, __FILE__);
97089930 168 $this->assertEquals($result['not_added'], 1);
169 $this->assertEquals($result['added'], 1);
170 $this->assertEquals($result['total_count'], 2);
6a488035
TO
171 }
172
36e590d4
TO
173 ///////////////// civicrm_group_contact_remove methods
174
389bcebf 175 /**
36e590d4 176 * Test GroupContact.delete by contact+group ID.
389bcebf 177 */
00be9182 178 public function testDelete() {
6a488035
TO
179 $params = array(
180 'contact_id' => $this->_contactId,
10f7c4d2 181 'group_id' => $this->_groupId1,
6a488035
TO
182 );
183
7fbb4198 184 $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
97089930 185 $this->assertEquals($result['removed'], 1);
186 $this->assertEquals($result['total_count'], 1);
6a488035 187 }
3d700d00 188
00be9182 189 public function testDeletePermanent() {
a9739e5d 190 $result = $this->callAPISuccess('group_contact', 'get', array('contact_id' => $this->_contactId));
3d700d00 191 $params = array(
a9739e5d 192 'id' => $result['id'],
3d700d00
CW
193 'skip_undelete' => TRUE,
194 );
195 $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
196 $result = $this->callAPISuccess('group_contact', 'get', $params);
97089930 197 $this->assertEquals(0, $result['count']);
198 $this->assertArrayNotHasKey('id', $result);
3d700d00 199 }
96025800 200
242e73d4 201 /**
202 * CRM-16945 duplicate groups are showing up when contacts are hard-added to child groups or smart groups.
203 *
204 * Fix documented in
205 *
206 * Test illustrates this (& ensures once fixed it will stay fixed).
207 */
208 public function testAccurateCountWithSmartGroups() {
209 $childGroupID = $this->groupCreate(array(
210 'name' => 'Child group',
211 'domain_id' => 1,
212 'title' => 'Child group',
213 'description' => 'Child group',
214 'is_active' => 1,
215 'parents' => $this->_groupId1,
216 'visibility' => 'User and User Admin Only',
217 ));
218
219 $params = array(
220 'name' => 'Individuals',
221 'title' => 'Individuals',
222 'is_active' => 1,
223 'parents' => $this->_groupId1,
224 'formValues' => array('contact_type' => 'Goat'),
225 );
226 $smartGroup2 = CRM_Contact_BAO_Group::createSmartGroup($params);
227
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));
232
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);
239 }
240 $this->assertEquals(3, $groups['count']);
241
242 }
243
6a488035 244}