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