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