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