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