Merge pull request #15759 from tunbola/active-option-values-for-custom-group
[civicrm-core.git] / tests / phpunit / api / v3 / CustomGroupTest.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 APIv3 civicrm_custom_group* functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_CustomGroup
17 * @group headless
18 */
19 class api_v3_CustomGroupTest extends CiviUnitTestCase {
20 protected $_apiversion = 3;
21 protected $_entity;
22 protected $_params;
23
24 public $DBResetRequired = TRUE;
25
26 public function setUp() {
27 $this->_entity = 'CustomGroup';
28 $this->_params = [
29 'title' => 'Test_Group_1',
30 'name' => 'test_group_1',
31 'extends' => 'Individual',
32 'weight' => 4,
33 'collapse_display' => 1,
34 'style' => 'Inline',
35 'help_pre' => 'This is Pre Help For Test Group 1',
36 'help_post' => 'This is Post Help For Test Group 1',
37 'is_active' => 1,
38 ];
39 parent::setUp();
40 }
41
42 public function tearDown() {
43 $tablesToTruncate = ['civicrm_custom_group', 'civicrm_custom_field'];
44 // true tells quickCleanup to drop any tables that might have been created in the test
45 $this->quickCleanup($tablesToTruncate, TRUE);
46 }
47
48 ///////////////// civicrm_custom_group_create methods
49
50 /**
51 * Check with empty array.
52 * note that these tests are of marginal value so should not be included in copy & paste
53 * code. The SyntaxConformance is capable of testing this for all entities on create
54 * & delete (& it would be easy to add if not there)
55 */
56 public function testCustomGroupCreateNoParam() {
57 $customGroup = $this->callAPIFailure('custom_group', 'create', [],
58 'Mandatory key(s) missing from params array: title, extends'
59 );
60 }
61
62 /**
63 * Check with empty array.
64 */
65 public function testCustomGroupCreateNoExtends() {
66 $params = [
67 'domain_id' => 1,
68 'title' => 'Test_Group_1',
69 'name' => 'test_group_1',
70 'weight' => 4,
71 'collapse_display' => 1,
72 'style' => 'Tab',
73 'help_pre' => 'This is Pre Help For Test Group 1',
74 'help_post' => 'This is Post Help For Test Group 1',
75 'is_active' => 1,
76 ];
77
78 $customGroup = $this->callAPIFailure('custom_group', 'create', $params);
79 $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: extends');
80 $this->assertAPIFailure($customGroup);
81 }
82
83 /**
84 * Check with empty array.
85 */
86 public function testCustomGroupCreateInvalidExtends() {
87 $params = [
88 'domain_id' => 1,
89 'title' => 'Test_Group_1',
90 'name' => 'test_group_1',
91 'weight' => 4,
92 'collapse_display' => 1,
93 'style' => 'Tab',
94 'help_pre' => 'This is Pre Help For Test Group 1',
95 'help_post' => 'This is Post Help For Test Group 1',
96 'is_active' => 1,
97 'extends' => [],
98 ];
99
100 $customGroup = $this->callAPIFailure('custom_group', 'create', $params);
101 $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: extends');
102 }
103
104 /**
105 * Check with a string instead of array for extends.
106 */
107 public function testCustomGroupCreateExtendsString() {
108 $params = [
109 'domain_id' => 1,
110 'title' => 'Test_Group_1',
111 'name' => 'test_group_1',
112 'weight' => 4,
113 'collapse_display' => 1,
114 'style' => 'Tab',
115 'help_pre' => 'This is Pre Help For Test Group 1',
116 'help_post' => 'This is Post Help For Test Group 1',
117 'is_active' => 1,
118 'extends' => 'Individual',
119 ];
120
121 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
122 }
123
124 /**
125 * Check with valid array.
126 */
127 public function testCustomGroupCreate() {
128 $params = [
129 'title' => 'Test_Group_1',
130 'name' => 'test_group_1',
131 'extends' => ['Individual'],
132 'weight' => 4,
133 'collapse_display' => 1,
134 'style' => 'Inline',
135 'help_pre' => 'This is Pre Help For Test Group 1',
136 'help_post' => 'This is Post Help For Test Group 1',
137 'is_active' => 1,
138 ];
139
140 $result = $this->callAPIAndDocument('custom_group', 'create', $params, __FUNCTION__, __FILE__);
141 $this->assertNotNull($result['id']);
142 $this->assertEquals($result['values'][$result['id']]['extends'], 'Individual');
143 }
144
145 /**
146 * Check with valid array.
147 */
148 public function testCustomGroupGetFields() {
149 $params = [
150 'options' => ['get_options' => 'style'],
151 ];
152
153 $result = $this->callAPISuccess('custom_group', 'getfields', $params);
154 $expected = [
155 'Tab' => 'Tab',
156 'Inline' => 'Inline',
157 'Tab with table' => 'Tab with table',
158 ];
159 $this->assertEquals($expected, $result['values']['style']['options']);
160 }
161
162 /**
163 * Check with extends array length greater than 1
164 */
165 public function testCustomGroupExtendsMultipleCreate() {
166 $params = [
167 'title' => 'Test_Group_1',
168 'name' => 'test_group_1',
169 'extends' => ['Individual', 'Household'],
170 'weight' => 4,
171 'collapse_display' => 1,
172 'style' => 'Inline',
173 'help_pre' => 'This is Pre Help For Test Group 1',
174 'help_post' => 'This is Post Help For Test Group 1',
175 'is_active' => 1,
176 ];
177
178 $result = $this->callAPIFailure('custom_group', 'create', $params,
179 'implode(): Invalid arguments passed');
180 }
181
182 /**
183 * Check with style missing from params array.
184 */
185 public function testCustomGroupCreateNoStyle() {
186 $params = [
187 'title' => 'Test_Group_1',
188 'name' => 'test_group_1',
189 'extends' => ['Individual'],
190 'weight' => 4,
191 'collapse_display' => 1,
192 'help_pre' => 'This is Pre Help For Test Group 1',
193 'help_post' => 'This is Post Help For Test Group 1',
194 'is_active' => 1,
195 ];
196
197 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
198 $this->assertNotNull($customGroup['id']);
199 $this->assertEquals($customGroup['values'][$customGroup['id']]['style'], 'Inline');
200 }
201
202 /**
203 * Check without title.
204 */
205 public function testCustomGroupCreateNoTitle() {
206 $params = [
207 'extends' => ['Contact'],
208 'weight' => 5,
209 'collapse_display' => 1,
210 'style' => 'Tab',
211 'help_pre' => 'This is Pre Help For Test Group 2',
212 'help_post' => 'This is Post Help For Test Group 2',
213 ];
214
215 $customGroup = $this->callAPIFailure('custom_group', 'create', $params,
216 'Mandatory key(s) missing from params array: title');
217 }
218
219 /**
220 * Check for household without weight.
221 */
222 public function testCustomGroupCreateHouseholdNoWeight() {
223 $params = [
224 'title' => 'Test_Group_3',
225 'name' => 'test_group_3',
226 'extends' => ['Household'],
227 'collapse_display' => 1,
228 'style' => 'Tab',
229 'help_pre' => 'This is Pre Help For Test Group 3',
230 'help_post' => 'This is Post Help For Test Group 3',
231 'is_active' => 1,
232 ];
233
234 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
235 $this->assertNotNull($customGroup['id']);
236 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Household');
237 $this->assertEquals($customGroup['values'][$customGroup['id']]['style'], 'Tab');
238 }
239
240 /**
241 * Check for Contribution Donation.
242 */
243 public function testCustomGroupCreateContributionDonation() {
244 $params = [
245 'title' => 'Test_Group_6',
246 'name' => 'test_group_6',
247 'extends' => ['Contribution', [1]],
248 'weight' => 6,
249 'collapse_display' => 1,
250 'style' => 'Inline',
251 'help_pre' => 'This is Pre Help For Test Group 6',
252 'help_post' => 'This is Post Help For Test Group 6',
253 'is_active' => 1,
254 ];
255
256 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
257 $this->assertNotNull($customGroup['id']);
258 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Contribution');
259 }
260
261 /**
262 * Check with valid array.
263 */
264 public function testCustomGroupCreateGroup() {
265 $params = [
266 'domain_id' => 1,
267 'title' => 'Test_Group_8',
268 'name' => 'test_group_8',
269 'extends' => ['Group'],
270 'weight' => 7,
271 'collapse_display' => 1,
272 'is_active' => 1,
273 'style' => 'Inline',
274 'help_pre' => 'This is Pre Help For Test Group 8',
275 'help_post' => 'This is Post Help For Test Group 8',
276 ];
277
278 $customGroup = $this->callAPISuccess('CustomGroup', 'create', $params);
279 $this->assertNotNull($customGroup['id']);
280 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Group');
281 }
282
283 /**
284 * Test an empty update does not trigger e-notices when is_multiple has been set.
285 */
286 public function testCustomGroupEmptyUpdate() {
287 $customGroup = $this->callAPISuccess('CustomGroup', 'create', array_merge($this->_params, ['is_multiple' => 1]));
288 $this->callAPISuccess('CustomGroup', 'create', ['id' => $customGroup['id']]);
289 }
290
291 /**
292 * Test an update when is_multiple is an emtpy string this can occur in form submissions for custom groups that extend activites.
293 * dev/core#227.
294 */
295 public function testCustomGroupEmptyisMultipleUpdate() {
296 $customGroup = $this->callAPISuccess('CustomGroup', 'create', array_merge($this->_params, ['is_multiple' => 0]));
297 $this->callAPISuccess('CustomGroup', 'create', ['id' => $customGroup['id'], 'is_multiple' => '']);
298 }
299
300 /**
301 * Check with Activity - Meeting Type
302 */
303 public function testCustomGroupCreateActivityMeeting() {
304 $params = [
305 'title' => 'Test_Group_10',
306 'name' => 'test_group_10',
307 'extends' => ['Activity', [1]],
308 'weight' => 8,
309 'collapse_display' => 1,
310 'style' => 'Inline',
311 'help_pre' => 'This is Pre Help For Test Group 10',
312 'help_post' => 'This is Post Help For Test Group 10',
313 ];
314
315 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
316 $this->assertNotNull($customGroup['id']);
317 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Activity');
318 }
319
320 ///////////////// civicrm_custom_group_delete methods
321
322 /**
323 * Check without GroupID.
324 */
325 public function testCustomGroupDeleteWithoutGroupID() {
326 $customGroup = $this->callAPIFailure('custom_group', 'delete', []);
327 $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: id');
328 }
329
330 /**
331 * Check with valid custom group id.
332 */
333 public function testCustomGroupDelete() {
334 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']);
335 $params = [
336 'id' => $customGroup['id'],
337 ];
338 $result = $this->callAPIAndDocument('custom_group', 'delete', $params, __FUNCTION__, __FILE__);
339 $this->assertAPISuccess($result);
340 }
341
342 /**
343 * Main success get function.
344 */
345 public function testGetCustomGroupSuccess() {
346
347 $this->callAPISuccess($this->_entity, 'create', $this->_params);
348 $params = [];
349 $result = $this->callAPIAndDocument($this->_entity, 'get', $params, __FUNCTION__, __FILE__);
350 $values = $result['values'][$result['id']];
351 foreach ($this->_params as $key => $value) {
352 if ($key == 'weight') {
353 continue;
354 }
355 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
356 }
357 }
358
359 public function testUpdateCustomGroup() {
360 $customGroup = $this->customGroupCreate();
361 $customGroupId = $customGroup['id'];
362
363 //update is_active
364 $params = ['id' => $customGroupId, 'is_active' => 0];
365 $result = $this->callAPISuccess('CustomGroup', 'create', $params);
366 $result = array_shift($result['values']);
367
368 $this->assertEquals(0, $result['is_active']);
369 $this->customGroupDelete($customGroupId);
370 }
371
372 }