Merge pull request #7656 from totten/master-civi-ns
[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-2015 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
30
31 /**
32 * Class api_v3_GroupContactTest
33 */
34 class api_v3_GroupContactTest extends CiviUnitTestCase {
35
36 protected $_contactId;
37 protected $_contactId1;
38 protected $_apiversion = 3;
39
40 /**
41 * @var int
42 */
43 protected $_groupId1;
44
45 /**
46 * @var int
47 */
48 protected $_groupId2;
49
50 /**
51 * Set up for group contact tests.
52 *
53 * @todo set up calls function that doesn't work @ the moment
54 */
55 public function setUp() {
56 parent::setUp();
57 $this->useTransaction(TRUE);
58
59 $this->_contactId = $this->individualCreate();
60
61 $this->_groupId1 = $this->groupCreate();
62
63 $this->callAPISuccess('group_contact', 'create', array(
64 'contact_id' => $this->_contactId,
65 'group_id' => $this->_groupId1,
66 ));
67
68 $this->_groupId2 = $this->groupCreate(array(
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',
75 ));
76
77 $this->_group = array(
78 $this->_groupId1 => array(
79 'title' => 'New Test Group Created',
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
91 ///////////////// civicrm_group_contact_get methods
92
93 /**
94 * Test GroupContact.get by ID.
95 */
96 public function testGet() {
97 $params = array(
98 'contact_id' => $this->_contactId,
99 );
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']);
105 }
106 }
107
108 public function testGetGroupID() {
109 $description = "Get all from group and display contacts.";
110 $subfile = "GetWithGroupID";
111 $params = array(
112 'group_id' => $this->_groupId1,
113 'api.group.get' => 1,
114 'sequential' => 1,
115 );
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']);
121 }
122 }
123
124 public function testCreateWithEmptyParams() {
125 $params = array();
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'
129 );
130 }
131
132 public function testCreateWithoutGroupIdParams() {
133 $params = array(
134 'contact_id' => $this->_contactId,
135 );
136
137 $groups = $this->callAPIFailure('group_contact', 'create', $params);
138 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
139 }
140
141 public function testCreateWithoutContactIdParams() {
142 $params = array(
143 'group_id' => $this->_groupId1,
144 );
145 $groups = $this->callAPIFailure('group_contact', 'create', $params);
146 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
147 }
148
149 public function testCreate() {
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',
157 'contact_type' => 'Individual',
158 );
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,
165 );
166
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);
171 }
172
173 ///////////////// civicrm_group_contact_remove methods
174
175 /**
176 * Test GroupContact.delete by contact+group ID.
177 */
178 public function testDelete() {
179 $params = array(
180 'contact_id' => $this->_contactId,
181 'group_id' => $this->_groupId1,
182 );
183
184 $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
185 $this->assertEquals($result['removed'], 1);
186 $this->assertEquals($result['total_count'], 1);
187 }
188
189 public function testDeletePermanent() {
190 $result = $this->callAPISuccess('group_contact', 'get', array('contact_id' => $this->_contactId));
191 $params = array(
192 'id' => $result['id'],
193 'skip_undelete' => TRUE,
194 );
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);
199 }
200
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
244 }