Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-07-23-17-12-38
[civicrm-core.git] / tests / phpunit / api / v3 / GroupOrganizationTest.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 require_once 'CiviTest/CiviUnitTestCase.php';
30
31
32 /**
33 * Test class for GroupOrganization API - civicrm_group_organization_*
34 *
35 * @package CiviCRM
36 */
37 class api_v3_GroupOrganizationTest extends CiviUnitTestCase {
38 protected $_apiversion;
39 public $_eNoticeCompliant = True;
40
41 function get_info() {
42 return array(
43 'name' => 'Group Organization',
44 'description' => 'Test all Group Organization API methods.',
45 'group' => 'CiviCRM API Tests',
46 );
47 }
48
49 /**
50 * Sets up the fixture, for example, opens a network connection.
51 * This method is called before a test is executed.
52 *
53 * @access protected
54 */
55 protected function setUp() {
56 $this->_apiversion = 3;
57 parent::setUp();
58 $this->_groupID = $this->groupCreate(NULL);
59
60 $this->_orgID = $this->organizationCreate(NULL);
61 }
62
63 /**
64 * Tears down the fixture, for example, closes a network connection.
65 * This method is called after a test is executed.
66 *
67 * @access protected
68 */
69 protected function tearDown() {
70 // Truncate the tables
71 $this->quickCleanup(
72 array(
73 'civicrm_group',
74 'civicrm_group_organization',
75 'civicrm_contact',
76 'civicrm_uf_group',
77 'civicrm_uf_join',
78 'civicrm_uf_match',
79 )
80 );
81 }
82
83 ///////////////// civicrm_group_organization_get methods
84
85 /**
86 * Test civicrm_group_organization_get with valid params.
87 */
88 public function testGroupOrganizationGet() {
89
90 $params = array(
91 'organization_id' => $this->_orgID,
92 'group_id' => $this->_groupID,
93 'version' => $this->_apiversion,
94 );
95 $result = civicrm_api('group_organization', 'create', $params);
96 $paramsGet = array(
97 'organization_id' => $result['id'],
98 'version' => $this->_apiversion,
99 );
100 $result = civicrm_api('group_organization', 'get', $paramsGet);
101 $this->documentMe($paramsGet, $result, __FUNCTION__, __FILE__);
102 $this->assertAPISuccess($result);
103 }
104
105 /**
106 * Test civicrm_group_organization_get with group_id.
107 */
108 public function testGroupOrganizationGetWithGroupId() {
109
110 $params = array(
111 'organization_id' => $this->_orgID,
112 'group_id' => $this->_groupID,
113 'version' => $this->_apiversion,
114 'sequential' => 1,
115 );
116 $result = civicrm_api('group_organization', 'create', $params);
117
118 $paramsGet = array('organization_id' => $result['values'][0]['organization_id']);
119
120 $result = civicrm_api('group_organization', 'get', $params);
121 $this->assertAPISuccess($result);
122 }
123
124 /**
125 * Test civicrm_group_organization_get with empty params.
126 */
127 public function testGroupOrganizationGetWithEmptyParams() {
128 $params = array(
129 'version' => $this->_apiversion,
130 );
131 $result = civicrm_api('group_organization', 'get', $params);
132
133 $this->assertAPISuccess($result);
134 }
135
136 /**
137 * Test civicrm_group_organization_get with wrong params.
138 */
139 public function testGroupOrganizationGetWithWrongParams() {
140 $params = 'groupOrg';
141 $result = $this->callAPIFailure('group_organization', 'get', $params);
142 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
143 }
144
145 /**
146 * Test civicrm_group_organization_get invalid keys.
147 */
148 public function testGroupOrganizationGetWithInvalidKeys() {
149 $params = array(
150 'invalid_key' => 1,
151 'version' => $this->_apiversion,
152 );
153 $result = civicrm_api('group_organization', 'get', $params);
154
155 $this->assertAPISuccess($result);
156 }
157
158 ///////////////// civicrm_group_organization_create methods
159
160 /**
161 * check with valid params
162 */
163 public function testGroupOrganizationCreate() {
164 $params = array(
165 'organization_id' => $this->_orgID,
166 'group_id' => $this->_groupID,
167 'version' => $this->_apiversion,
168 );
169 $result = civicrm_api('group_organization', 'create', $params);
170 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
171 $this->assertAPISuccess($result, "in line " . __LINE__);
172 }
173
174 /**
175 * check with empty params array
176 */
177 public function testGroupOrganizationCreateWithEmptyParams() {
178 $params = array(
179 'version' => $this->_apiversion,
180 );
181 $result = $this->callAPIFailure('group_organization', 'create', $params);
182 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: group_id, organization_id');
183 }
184
185 /**
186 * check with invalid params
187 */
188 public function testGroupOrganizationCreateParamsNotArray() {
189 $params = 'group_org';
190 $result = $this->callAPIFailure('group_organization', 'create', $params);
191 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
192 }
193
194 /**
195 * check with invalid params keys
196 */
197 public function testGroupOrganizationCreateWithInvalidKeys() {
198 $params = array(
199 'invalid_key' => 1,
200 'version' => $this->_apiversion,
201 );
202 $result = $this->callAPIFailure('group_organization', 'create', $params);
203 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: group_id, organization_id');
204 }
205
206 ///////////////// civicrm_group_organization_remove methods
207
208 /**
209 * Test civicrm_group_organization_remove with params not an array.
210 */
211 public function testGroupOrganizationDeleteParamsNotArray() {
212 $params = 'delete';
213 $result = $this->callAPIFailure('group_organization', 'delete', $params);
214 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
215 }
216
217 /**
218 * Test civicrm_group_organization_remove with empty params.
219 */
220 public function testGroupOrganizationDeleteWithEmptyParams() {
221 $params = array(
222 'version' => $this->_apiversion,
223 );
224 $result = $this->callAPIFailure('group_organization', 'delete', $params);
225 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
226 }
227
228 /**
229 * Test civicrm_group_organization_remove with valid params.
230 */
231 public function testGroupOrganizationDelete() {
232 $paramsC = array(
233 'organization_id' => $this->_orgID,
234 'group_id' => $this->_groupID,
235 'version' => $this->_apiversion,
236 );
237 $result = civicrm_api('group_organization', 'create', $paramsC);
238
239 $params = array(
240 'id' => $result['id'],
241 'version' => $this->_apiversion,
242 );
243 $result = civicrm_api('group_organization', 'delete', $params);
244 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
245 $this->assertAPISuccess($result, "in line " . __LINE__);
246 }
247
248 /**
249 * Test civicrm_group_organization_remove with invalid params key.
250 */
251 public function testGroupOrganizationDeleteWithInvalidKey() {
252 $paramsDelete = array(
253 'invalid_key' => 1,
254 'version' => $this->_apiversion,
255 );
256 $result = $this->callAPIFailure('group_organization', 'delete', $paramsDelete);
257 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
258 }
259 }
260