Merge pull request #15821 from seamuslee001/dev_core_183_custom_group
[civicrm-core.git] / tests / phpunit / api / v3 / GroupOrganizationTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test class for GroupOrganization API - civicrm_group_organization_*
14 *
15 * @package CiviCRM
16 * @group headless
17 */
18 class api_v3_GroupOrganizationTest extends CiviUnitTestCase {
19 protected $_apiversion;
20
21 /**
22 * Sets up the fixture, for example, opens a network connection.
23 * This method is called before a test is executed.
24 */
25 protected function setUp() {
26 $this->_apiversion = 3;
27 parent::setUp();
28 $this->useTransaction(TRUE);
29 $this->_groupID = $this->groupCreate();
30
31 $this->_orgID = $this->organizationCreate(NULL);
32 }
33
34 ///////////////// civicrm_group_organization_get methods
35
36 /**
37 * Test civicrm_group_organization_get with valid params.
38 *
39 * @dataProvider versionThreeAndFour
40 */
41 public function testGroupOrganizationGet() {
42
43 $params = [
44 'organization_id' => $this->_orgID,
45 'group_id' => $this->_groupID,
46 ];
47 $result = $this->callAPISuccess('group_organization', 'create', $params);
48 $paramsGet = [
49 'organization_id' => $result['id'],
50 ];
51 $result = $this->callAPIAndDocument('group_organization', 'get', $paramsGet, __FUNCTION__, __FILE__);
52 }
53
54 /**
55 * Test civicrm_group_organization_get with group_id.
56 *
57 * @dataProvider versionThreeAndFour
58 */
59 public function testGroupOrganizationGetWithGroupId() {
60 $createParams = [
61 'organization_id' => $this->_orgID,
62 'group_id' => $this->_groupID,
63 ];
64 $createResult = $this->callAPISuccess('group_organization', 'create', $createParams);
65
66 $getParams = [
67 'group_id' => $this->_groupID,
68 'sequential' => 1,
69 ];
70 $getResult = $this->callAPISuccess('group_organization', 'get', $getParams);
71 $this->assertEquals($createResult['values'][$createResult['id']], $getResult['values'][0]);
72 }
73
74 /**
75 * Test civicrm_group_organization_get with empty params.
76 *
77 * @dataProvider versionThreeAndFour
78 */
79 public function testGroupOrganizationGetWithEmptyParams() {
80 $params = [];
81 $result = $this->callAPISuccess('group_organization', 'get', $params);
82
83 $this->assertAPISuccess($result);
84 }
85
86 /**
87 * Test civicrm_group_organization_get invalid keys.
88 *
89 * @dataProvider versionThreeAndFour
90 */
91 public function testGroupOrganizationGetWithInvalidKeys() {
92 $params = [
93 'invalid_key' => 1,
94 ];
95 $result = $this->callAPISuccess('group_organization', 'get', $params);
96
97 $this->assertAPISuccess($result);
98 }
99
100 ///////////////// civicrm_group_organization_create methods
101
102 /**
103 * Check with valid params.
104 *
105 * @dataProvider versionThreeAndFour
106 */
107 public function testGroupOrganizationCreate() {
108 $params = [
109 'organization_id' => $this->_orgID,
110 'group_id' => $this->_groupID,
111 ];
112 $result = $this->callAPIAndDocument('group_organization', 'create', $params, __FUNCTION__, __FILE__);
113 }
114
115 /**
116 * CRM-13841 - Load Group Org before save
117 *
118 * @dataProvider versionThreeAndFour
119 */
120 public function testGroupOrganizationCreateTwice() {
121 $params = [
122 'organization_id' => $this->_orgID,
123 'group_id' => $this->_groupID,
124 ];
125 $result = $this->callAPISuccess('group_organization', 'create', $params);
126 $result2 = $this->callAPISuccess('group_organization', 'create', $params);
127 $this->assertEquals($result['values'], $result2['values']);
128 }
129
130 /**
131 * Check with empty params array.
132 *
133 * @dataProvider versionThreeAndFour
134 */
135 public function testGroupOrganizationCreateWithEmptyParams() {
136 $params = [];
137 $result = $this->callAPIFailure('group_organization', 'create', $params);
138 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: group_id, organization_id');
139 }
140
141 /**
142 * Check with invalid params keys.
143 *
144 * @dataProvider versionThreeAndFour
145 */
146 public function testGroupOrganizationCreateWithInvalidKeys() {
147 $params = [
148 'invalid_key' => 1,
149 ];
150 $result = $this->callAPIFailure('group_organization', 'create', $params);
151 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: group_id, organization_id');
152 }
153
154 ///////////////// civicrm_group_organization_remove methods
155
156 /**
157 * Test civicrm_group_organization_remove with empty params.
158 *
159 * @dataProvider versionThreeAndFour
160 */
161 public function testGroupOrganizationDeleteWithEmptyParams() {
162 $params = [];
163 $result = $this->callAPIFailure('group_organization', 'delete', $params);
164 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
165 }
166
167 /**
168 * Test civicrm_group_organization_remove with valid params.
169 *
170 * @dataProvider versionThreeAndFour
171 */
172 public function testGroupOrganizationDelete() {
173 $paramsC = [
174 'organization_id' => $this->_orgID,
175 'group_id' => $this->_groupID,
176 ];
177 $result = $this->callAPISuccess('group_organization', 'create', $paramsC);
178
179 $params = [
180 'id' => $result['id'],
181 ];
182 $result = $this->callAPIAndDocument('group_organization', 'delete', $params, __FUNCTION__, __FILE__);
183 }
184
185 /**
186 * Test civicrm_group_organization_remove with invalid params key.
187 *
188 * @dataProvider versionThreeAndFour
189 */
190 public function testGroupOrganizationDeleteWithInvalidKey() {
191 $paramsDelete = [
192 'invalid_key' => 1,
193 ];
194 $result = $this->callAPIFailure('group_organization', 'delete', $paramsDelete);
195 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
196 }
197
198 }