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