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