Merge pull request #7797 from JKingsnorth/CRM-17977
[civicrm-core.git] / tests / phpunit / api / v3 / GroupContactTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class api_v3_GroupContactTest
30 * @group headless
31 */
32 class api_v3_GroupContactTest extends CiviUnitTestCase {
33
34 protected $_contactId;
35 protected $_contactId1;
36 protected $_apiversion = 3;
37
38 /**
39 * @var int
40 */
41 protected $_groupId1;
42
43 /**
44 * @var int
45 */
46 protected $_groupId2;
47
48 /**
49 * Set up for group contact tests.
50 *
51 * @todo set up calls function that doesn't work @ the moment
52 */
53 public function setUp() {
54 parent::setUp();
55 $this->useTransaction(TRUE);
56
57 $this->_contactId = $this->individualCreate();
58
59 $this->_groupId1 = $this->groupCreate();
60
61 $this->callAPISuccess('group_contact', 'create', array(
62 'contact_id' => $this->_contactId,
63 'group_id' => $this->_groupId1,
64 ));
65
66 $this->_groupId2 = $this->groupCreate(array(
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',
73 ));
74
75 $this->_group = array(
76 $this->_groupId1 => array(
77 'title' => 'New Test Group Created',
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
89 ///////////////// civicrm_group_contact_get methods
90
91 /**
92 * Test GroupContact.get by ID.
93 */
94 public function testGet() {
95 $params = array(
96 'contact_id' => $this->_contactId,
97 );
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']);
103 }
104 }
105
106 public function testGetGroupID() {
107 $description = "Get all from group and display contacts.";
108 $subfile = "GetWithGroupID";
109 $params = array(
110 'group_id' => $this->_groupId1,
111 'api.group.get' => 1,
112 'sequential' => 1,
113 );
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']);
119 }
120 }
121
122 public function testCreateWithEmptyParams() {
123 $params = array();
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'
127 );
128 }
129
130 public function testCreateWithoutGroupIdParams() {
131 $params = array(
132 'contact_id' => $this->_contactId,
133 );
134
135 $groups = $this->callAPIFailure('group_contact', 'create', $params);
136 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
137 }
138
139 public function testCreateWithoutContactIdParams() {
140 $params = array(
141 'group_id' => $this->_groupId1,
142 );
143 $groups = $this->callAPIFailure('group_contact', 'create', $params);
144 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
145 }
146
147 public function testCreate() {
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',
155 'contact_type' => 'Individual',
156 );
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,
163 );
164
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);
169 }
170
171 ///////////////// civicrm_group_contact_remove methods
172
173 /**
174 * Test GroupContact.delete by contact+group ID.
175 */
176 public function testDelete() {
177 $params = array(
178 'contact_id' => $this->_contactId,
179 'group_id' => $this->_groupId1,
180 );
181
182 $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
183 $this->assertEquals($result['removed'], 1);
184 $this->assertEquals($result['total_count'], 1);
185 }
186
187 public function testDeletePermanent() {
188 $result = $this->callAPISuccess('group_contact', 'get', array('contact_id' => $this->_contactId));
189 $params = array(
190 'id' => $result['id'],
191 'skip_undelete' => TRUE,
192 );
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);
197 }
198
199 /**
200 * CRM-16945 duplicate groups are showing up when contacts are hard-added to child groups or smart groups.
201 *
202 * Fix documented in
203 *
204 * Test illustrates this (& ensures once fixed it will stay fixed).
205 */
206 public function testAccurateCountWithSmartGroups() {
207 $childGroupID = $this->groupCreate(array(
208 'name' => 'Child group',
209 'domain_id' => 1,
210 'title' => 'Child group',
211 'description' => 'Child group',
212 'is_active' => 1,
213 'parents' => $this->_groupId1,
214 'visibility' => 'User and User Admin Only',
215 ));
216
217 $params = array(
218 'name' => 'Individuals',
219 'title' => 'Individuals',
220 'is_active' => 1,
221 'parents' => $this->_groupId1,
222 'formValues' => array('contact_type' => 'Goat'),
223 );
224 $smartGroup2 = CRM_Contact_BAO_Group::createSmartGroup($params);
225
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));
230
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);
237 }
238 $this->assertEquals(3, $groups['count']);
239
240 }
241
242 }