Test fix
[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 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 27
e9479dcf
EM
28/**
29 * Class api_v3_GroupContactTest
acb109b7 30 * @group headless
e9479dcf 31 */
6a488035
TO
32class api_v3_GroupContactTest extends CiviUnitTestCase {
33
34 protected $_contactId;
35 protected $_contactId1;
7fbb4198 36 protected $_apiversion = 3;
97089930 37
38 /**
39 * @var int
40 */
6a488035 41 protected $_groupId1;
6a488035 42
97089930 43 /**
44 * @var int
45 */
46 protected $_groupId2;
47
c490a46a 48 /**
eceb18cc 49 * Set up for group contact tests.
c490a46a
CW
50 *
51 * @todo set up calls function that doesn't work @ the moment
52 */
00be9182 53 public function setUp() {
6a488035 54 parent::setUp();
10f7c4d2 55 $this->useTransaction(TRUE);
6a488035 56
e4d5f1e2 57 $this->_contactId = $this->individualCreate();
6a488035 58
72e41e3a 59 $this->_groupId1 = $this->groupCreate();
97089930 60
61 $this->callAPISuccess('group_contact', 'create', array(
6a488035
TO
62 'contact_id' => $this->_contactId,
63 'group_id' => $this->_groupId1,
97089930 64 ));
6a488035 65
97089930 66 $this->_groupId2 = $this->groupCreate(array(
6a488035
TO
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',
97089930 73 ));
6a488035
TO
74
75 $this->_group = array(
6c6e6187 76 $this->_groupId1 => array(
92915c55 77 'title' => 'New Test Group Created',
6a488035
TO
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
36e590d4
TO
89 ///////////////// civicrm_group_contact_get methods
90
389bcebf 91 /**
36e590d4 92 * Test GroupContact.get by ID.
389bcebf 93 */
00be9182 94 public function testGet() {
6a488035
TO
95 $params = array(
96 'contact_id' => $this->_contactId,
6a488035 97 );
7fbb4198 98 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
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
00be9182 106 public function testGetGroupID() {
5c49fee0 107 $description = "Get all from group and display contacts.";
92915c55
TO
108 $subfile = "GetWithGroupID";
109 $params = array(
6a488035 110 'group_id' => $this->_groupId1,
6a488035
TO
111 'api.group.get' => 1,
112 'sequential' => 1,
113 );
7fbb4198 114 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
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
00be9182 122 public function testCreateWithEmptyParams() {
6a488035 123 $params = array();
d0e1eff2 124 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035 125 $this->assertEquals($groups['error_message'],
d0e1eff2 126 'Mandatory key(s) missing from params array: group_id, contact_id'
6a488035
TO
127 );
128 }
129
00be9182 130 public function testCreateWithoutGroupIdParams() {
6a488035 131 $params = array(
92915c55
TO
132 'contact_id' => $this->_contactId,
133 );
6a488035 134
d0e1eff2 135 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035
TO
136 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
137 }
138
00be9182 139 public function testCreateWithoutContactIdParams() {
6a488035 140 $params = array(
92915c55
TO
141 'group_id' => $this->_groupId1,
142 );
d0e1eff2 143 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035
TO
144 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
145 }
146
00be9182 147 public function testCreate() {
6a488035
TO
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',
92915c55
TO
155 'contact_type' => 'Individual',
156 );
6a488035
TO
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,
6a488035
TO
163 );
164
7fbb4198 165 $result = $this->callAPIAndDocument('group_contact', 'create', $params, __FUNCTION__, __FILE__);
97089930 166 $this->assertEquals($result['not_added'], 1);
167 $this->assertEquals($result['added'], 1);
168 $this->assertEquals($result['total_count'], 2);
6a488035
TO
169 }
170
36e590d4
TO
171 ///////////////// civicrm_group_contact_remove methods
172
389bcebf 173 /**
36e590d4 174 * Test GroupContact.delete by contact+group ID.
389bcebf 175 */
00be9182 176 public function testDelete() {
6a488035
TO
177 $params = array(
178 'contact_id' => $this->_contactId,
10f7c4d2 179 'group_id' => $this->_groupId1,
6a488035
TO
180 );
181
7fbb4198 182 $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
97089930 183 $this->assertEquals($result['removed'], 1);
184 $this->assertEquals($result['total_count'], 1);
6a488035 185 }
3d700d00 186
00be9182 187 public function testDeletePermanent() {
a9739e5d 188 $result = $this->callAPISuccess('group_contact', 'get', array('contact_id' => $this->_contactId));
3d700d00 189 $params = array(
a9739e5d 190 'id' => $result['id'],
3d700d00
CW
191 'skip_undelete' => TRUE,
192 );
193 $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
194 $result = $this->callAPISuccess('group_contact', 'get', $params);
97089930 195 $this->assertEquals(0, $result['count']);
196 $this->assertArrayNotHasKey('id', $result);
3d700d00 197 }
96025800 198
242e73d4 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
6a488035 242}