Merge pull request #15843 from totten/master-simplehead
[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 with not array.
204 */
205 public function testCustomGroupCreateNotArray() {
206 $params = NULL;
207 $customGroup = $this->callAPIFailure('custom_group', 'create', $params);
208 $this->assertEquals($customGroup['error_message'], 'Input variable `params` is not an array');
209 }
210
211 /**
212 * Check without title.
213 */
214 public function testCustomGroupCreateNoTitle() {
215 $params = [
216 'extends' => ['Contact'],
217 'weight' => 5,
218 'collapse_display' => 1,
219 'style' => 'Tab',
220 'help_pre' => 'This is Pre Help For Test Group 2',
221 'help_post' => 'This is Post Help For Test Group 2',
222 ];
223
224 $customGroup = $this->callAPIFailure('custom_group', 'create', $params,
225 'Mandatory key(s) missing from params array: title');
226 }
227
228 /**
229 * Check for household without weight.
230 */
231 public function testCustomGroupCreateHouseholdNoWeight() {
232 $params = [
233 'title' => 'Test_Group_3',
234 'name' => 'test_group_3',
235 'extends' => ['Household'],
236 'collapse_display' => 1,
237 'style' => 'Tab',
238 'help_pre' => 'This is Pre Help For Test Group 3',
239 'help_post' => 'This is Post Help For Test Group 3',
240 'is_active' => 1,
241 ];
242
243 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
244 $this->assertNotNull($customGroup['id']);
245 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Household');
246 $this->assertEquals($customGroup['values'][$customGroup['id']]['style'], 'Tab');
247 }
248
249 /**
250 * Check for Contribution Donation.
251 */
252 public function testCustomGroupCreateContributionDonation() {
253 $params = [
254 'title' => 'Test_Group_6',
255 'name' => 'test_group_6',
256 'extends' => ['Contribution', [1]],
257 'weight' => 6,
258 'collapse_display' => 1,
259 'style' => 'Inline',
260 'help_pre' => 'This is Pre Help For Test Group 6',
261 'help_post' => 'This is Post Help For Test Group 6',
262 'is_active' => 1,
263 ];
264
265 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
266 $this->assertNotNull($customGroup['id']);
267 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Contribution');
268 }
269
270 /**
271 * Check with valid array.
272 */
273 public function testCustomGroupCreateGroup() {
274 $params = [
275 'domain_id' => 1,
276 'title' => 'Test_Group_8',
277 'name' => 'test_group_8',
278 'extends' => ['Group'],
279 'weight' => 7,
280 'collapse_display' => 1,
281 'is_active' => 1,
282 'style' => 'Inline',
283 'help_pre' => 'This is Pre Help For Test Group 8',
284 'help_post' => 'This is Post Help For Test Group 8',
285 ];
286
287 $customGroup = $this->callAPISuccess('CustomGroup', 'create', $params);
288 $this->assertNotNull($customGroup['id']);
289 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Group');
290 }
291
292 /**
293 * Test an empty update does not trigger e-notices when is_multiple has been set.
294 */
295 public function testCustomGroupEmptyUpdate() {
296 $customGroup = $this->callAPISuccess('CustomGroup', 'create', array_merge($this->_params, ['is_multiple' => 1]));
297 $this->callAPISuccess('CustomGroup', 'create', ['id' => $customGroup['id']]);
298 }
299
300 /**
301 * Test an update when is_multiple is an emtpy string this can occur in form submissions for custom groups that extend activites.
302 * dev/core#227.
303 */
304 public function testCustomGroupEmptyisMultipleUpdate() {
305 $customGroup = $this->callAPISuccess('CustomGroup', 'create', array_merge($this->_params, ['is_multiple' => 0]));
306 $this->callAPISuccess('CustomGroup', 'create', ['id' => $customGroup['id'], 'is_multiple' => '']);
307 }
308
309 /**
310 * Check with Activity - Meeting Type
311 */
312 public function testCustomGroupCreateActivityMeeting() {
313 $params = [
314 'title' => 'Test_Group_10',
315 'name' => 'test_group_10',
316 'extends' => ['Activity', [1]],
317 'weight' => 8,
318 'collapse_display' => 1,
319 'style' => 'Inline',
320 'help_pre' => 'This is Pre Help For Test Group 10',
321 'help_post' => 'This is Post Help For Test Group 10',
322 ];
323
324 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
325 $this->assertNotNull($customGroup['id']);
326 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Activity');
327 }
328
329 ///////////////// civicrm_custom_group_delete methods
330
331 /**
332 * Check without GroupID.
333 */
334 public function testCustomGroupDeleteWithoutGroupID() {
335 $customGroup = $this->callAPIFailure('custom_group', 'delete', []);
336 $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: id');
337 }
338
339 /**
340 * Check with no array.
341 */
342 public function testCustomGroupDeleteNoArray() {
343 $params = NULL;
344 $customGroup = $this->callAPIFailure('custom_group', 'delete', $params);
345 $this->assertEquals($customGroup['error_message'], 'Input variable `params` is not an array');
346 }
347
348 /**
349 * Check with valid custom group id.
350 */
351 public function testCustomGroupDelete() {
352 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']);
353 $params = [
354 'id' => $customGroup['id'],
355 ];
356 $result = $this->callAPIAndDocument('custom_group', 'delete', $params, __FUNCTION__, __FILE__);
357 $this->assertAPISuccess($result);
358 }
359
360 /**
361 * Main success get function.
362 */
363 public function testGetCustomGroupSuccess() {
364
365 $this->callAPISuccess($this->_entity, 'create', $this->_params);
366 $params = [];
367 $result = $this->callAPIAndDocument($this->_entity, 'get', $params, __FUNCTION__, __FILE__);
368 $values = $result['values'][$result['id']];
369 foreach ($this->_params as $key => $value) {
370 if ($key == 'weight') {
371 continue;
372 }
373 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
374 }
375 }
376
377 public function testUpdateCustomGroup() {
378 $customGroup = $this->customGroupCreate();
379 $customGroupId = $customGroup['id'];
380
381 //update is_active
382 $params = ['id' => $customGroupId, 'is_active' => 0];
383 $result = $this->callAPISuccess('CustomGroup', 'create', $params);
384 $result = array_shift($result['values']);
385
386 $this->assertEquals(0, $result['is_active']);
387 $this->customGroupDelete($customGroupId);
388 }
389
390 }