Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-05-28-14-04-12
[civicrm-core.git] / tests / phpunit / api / v3 / GroupContactTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29
30 require_once 'CiviTest/CiviUnitTestCase.php';
31 class api_v3_GroupContactTest extends CiviUnitTestCase {
32
33 protected $_contactId;
34 protected $_contactId1;
35 protected $_apiversion;
36 protected $_groupId1;
37 public $_eNoticeCompliant = True;
38
39 function get_info() {
40 return array(
41 'name' => 'Group Contact Create',
42 'description' => 'Test all Group Contact Create API methods.',
43 'group' => 'CiviCRM API Tests',
44 );
45 }
46
47 /*
48 * Set up for group contact tests
49 *
50 * @todo set up calls function that doesn't work @ the moment
51 */
52 function setUp() {
53 $this->_apiversion = 3;
54 parent::setUp();
55
56 $this->_contactId = $this->individualCreate(NULL);
57
58 $this->_groupId1 = $this->groupCreate(NULL);
59 $params = array(
60 'contact_id' => $this->_contactId,
61 'group_id' => $this->_groupId1,
62 'version' => $this->_apiversion,
63 );
64
65 $result = civicrm_api('group_contact', 'create', $params);
66
67 $group = array(
68 'name' => 'Test Group 2',
69 'domain_id' => 1,
70 'title' => 'New Test Group2 Created',
71 'description' => 'New Test Group2 Created',
72 'is_active' => 1,
73 'visibility' => 'User and User Admin Only',
74 'version' => $this->_apiversion,
75 );
76
77 $this->_groupId2 = $this->groupCreate($group, 3);
78 $params = array(
79 'contact_id.1' => $this->_contactId,
80 'group_id' => $this->_groupId2,
81 'version' => $this->_apiversion,
82 );
83 //@todo uncomment me when I work
84 //civicrm_group_contact_create( $params );
85
86 $this->_group = array(
87 $this->_groupId1 => array('title' => 'New Test Group Created',
88 'visibility' => 'Public Pages',
89 'in_method' => 'API',
90 ),
91 $this->_groupId2 => array(
92 'title' => 'New Test Group2 Created',
93 'visibility' => 'User and User Admin Only',
94 'in_method' => 'API',
95 ),
96 );
97 }
98
99 function tearDown() {
100 $tablesToTruncate = array(
101 'civicrm_contact',
102 'civicrm_group',
103 );
104 $this->quickCleanup($tablesToTruncate);
105 }
106
107 ///////////////// civicrm_group_contact_get methods
108 function testGet() {
109 $params = array(
110 'contact_id' => $this->_contactId,
111 'version' => $this->_apiversion,
112 );
113 $result = civicrm_api('group_contact', 'get', $params);
114 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
115 foreach ($result['values'] as $v) {
116 $this->assertEquals($v['title'], $this->_group[$v['group_id']]['title']);
117 $this->assertEquals($v['visibility'], $this->_group[$v['group_id']]['visibility']);
118 $this->assertEquals($v['in_method'], $this->_group[$v['group_id']]['in_method']);
119 }
120 }
121
122 function testGetGroupID() {
123 $description = "Get all from group and display contacts";
124 $subfile = "GetWithGroupID";
125 $params = array(
126 'group_id' => $this->_groupId1,
127 'version' => $this->_apiversion,
128 'api.group.get' => 1,
129 'sequential' => 1,
130 );
131 $result = civicrm_api('group_contact', 'get', $params);
132 $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, $subfile);
133 foreach ($result['values'][0]['api.group.get']['values'] as $values) {
134 $key = $values['id'];
135 $this->assertEquals($values['title'], $this->_group[$key]['title']);
136 $this->assertEquals($values['visibility'], $this->_group[$key]['visibility']);
137 }
138 }
139
140
141 ///////////////// civicrm_group_contact_add methods
142 function testCreateWithWrongParamsType() {
143 $params = 1;
144 $groups = civicrm_api('group_contact', 'create', $params);
145
146 $this->assertEquals($groups['is_error'], 1);
147 $this->assertEquals($groups['error_message'], 'Input variable `params` is not an array');
148 }
149
150 function testCreateWithEmptyParams() {
151 $params = array();
152 $groups = civicrm_api('group_contact', 'create', $params);
153
154 $this->assertEquals($groups['is_error'], 1);
155 $this->assertEquals($groups['error_message'],
156 'Mandatory key(s) missing from params array: version, group_id, contact_id'
157 );
158 }
159
160 function testCreateWithoutGroupIdParams() {
161 $params = array(
162 'contact_id' => $this->_contactId,
163 'version' => $this->_apiversion,
164 );
165
166 $groups = civicrm_api('group_contact', 'create', $params);
167
168 $this->assertEquals($groups['is_error'], 1);
169 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
170 }
171
172 function testCreateWithoutContactIdParams() {
173 $params = array(
174 'group_id' => $this->_groupId1,
175 'version' => $this->_apiversion,
176 );
177 $groups = civicrm_api('group_contact', 'create', $params);
178
179 $this->assertEquals($groups['is_error'], 1);
180 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
181 }
182
183 function testCreate() {
184 $cont = array(
185 'first_name' => 'Amiteshwar',
186 'middle_name' => 'L.',
187 'last_name' => 'Prasad',
188 'prefix_id' => 3,
189 'suffix_id' => 3,
190 'email' => 'amiteshwar.prasad@civicrm.org',
191 'contact_type' => 'Individual',
192 'version' => $this->_apiversion,
193 );
194
195 $this->_contactId1 = $this->individualCreate($cont);
196 $params = array(
197 'contact_id' => $this->_contactId,
198 'contact_id.2' => $this->_contactId1,
199 'group_id' => $this->_groupId1,
200 'version' => $this->_apiversion,
201 );
202
203 $result = civicrm_api('group_contact', 'create', $params);
204 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
205 $this->assertEquals($result['is_error'], 0, "in line " . __LINE__);
206 $this->assertEquals($result['not_added'], 1, "in line " . __LINE__);
207 $this->assertEquals($result['added'], 1, "in line " . __LINE__);
208 $this->assertEquals($result['total_count'], 2, "in line " . __LINE__);
209 }
210
211 ///////////////// civicrm_group_contact_remove methods
212 function testDelete() {
213 $params = array(
214 'contact_id' => $this->_contactId,
215 'group_id' => 1,
216 'version' => $this->_apiversion,
217 );
218
219 $result = civicrm_api('group_contact', 'delete', $params);
220 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
221 $this->assertEquals($result['is_error'], 0, "in line " . __LINE__);
222 $this->assertEquals($result['removed'], 1, "in line " . __LINE__);
223 $this->assertEquals($result['total_count'], 1, "in line " . __LINE__);
224 }
225 }
226