Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / CustomFieldTest.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_create_custom_group
14 *
15 * @package CiviCRM
16 * @group headless
17 */
18 class api_v3_CustomFieldTest extends CiviUnitTestCase {
19
20 /**
21 * Clean up after test.
22 *
23 * @throws \CRM_Core_Exception
24 */
25 public function tearDown() {
26 $this->quickCleanup([
27 'civicrm_contact',
28 'civicrm_file',
29 'civicrm_entity_file',
30 ], TRUE);
31 parent::tearDown();
32 }
33
34 /**
35 * Check with no label.
36 */
37 public function testCustomFieldCreateWithoutLabel() {
38 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'text_test_group']);
39 $params = [
40 'custom_group_id' => $customGroup['id'],
41 'name' => 'test_textfield2',
42 'html_type' => 'Text',
43 'data_type' => 'String',
44 'default_value' => 'abc',
45 'weight' => 4,
46 'is_required' => 1,
47 'is_searchable' => 0,
48 'is_active' => 1,
49 ];
50
51 $customField = $this->callAPIFailure('custom_field', 'create', $params);
52 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label');
53 }
54
55 /**
56 * Check with edit.
57 */
58 public function testCustomFieldCreateWithEdit() {
59 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'text_test_group']);
60 $params = [
61 'custom_group_id' => $customGroup['id'],
62 'name' => 'test_textfield2',
63 'label' => 'Name1',
64 'html_type' => 'Text',
65 'data_type' => 'String',
66 'default_value' => 'abc',
67 'weight' => 4,
68 'is_required' => 1,
69 'is_searchable' => 0,
70 'is_active' => 1,
71 ];
72
73 $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__, __FILE__);
74 $params['id'] = $customField['id'];
75 $customField = $this->callAPISuccess('custom_field', 'create', $params);
76
77 $this->assertNotNull($customField['id']);
78 }
79
80 /**
81 * Check without groupId.
82 */
83 public function testCustomFieldCreateWithoutGroupID() {
84 $fieldParams = [
85 'name' => 'test_textfield1',
86 'label' => 'Name',
87 'html_type' => 'Text',
88 'data_type' => 'String',
89 'default_value' => 'abc',
90 'weight' => 4,
91 'is_required' => 1,
92 'is_searchable' => 0,
93 'is_active' => 1,
94
95 ];
96
97 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
98 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id');
99 }
100
101 /**
102 * Check for Each data type: loop through available form input types
103 */
104 public function testCustomFieldCreateAllAvailableFormInputs() {
105 $gid = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'testAllFormInputs']);
106
107 $dtype = CRM_Core_BAO_CustomField::dataType();
108 $htype = CRM_Core_BAO_CustomField::dataToHtml();
109
110 $n = 0;
111 foreach ($dtype as $dkey => $dvalue) {
112 foreach ($htype[$n] as $hkey => $hvalue) {
113 //echo $dkey."][".$hvalue."\n";
114 $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey));
115 }
116 $n++;
117 }
118 }
119
120 /*
121 * Can't figure out the point of this?
122 */
123
124 /**
125 * @param array $params
126 */
127 public function _loopingCustomFieldCreateTest($params) {
128 $customField = $this->callAPISuccess('custom_field', 'create', $params);
129 $this->assertNotNull($customField['id']);
130 $this->getAndCheck($params, $customField['id'], 'CustomField');
131 }
132
133 /**
134 * @param int $gid
135 * @param $htype
136 * @param $dtype
137 *
138 * @return array
139 */
140 public function _buildParams($gid, $htype, $dtype) {
141 $params = $this->_buildBasicParams($gid, $htype, $dtype);
142 /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues()
143 if ($htype == 'Multi-Select')
144 $params = array_merge($params, array(
145 'option_label' => array( 'Label1','Label2'),
146 'option_value' => array( 'val1', 'val2' ),
147 'option_weight' => array( 1, 2),
148 'option_status' => array( 1, 1),
149 ));
150 */
151
152 return $params;
153 }
154
155 /**
156 * @param int $gid
157 * @param $htype
158 * @param $dtype
159 *
160 * @return array
161 */
162 public function _buildBasicParams($gid, $htype, $dtype) {
163 return [
164 'custom_group_id' => $gid,
165 'label' => $dtype . $htype,
166 'html_type' => $htype,
167 'data_type' => $dtype,
168 'weight' => 4,
169 'is_required' => 0,
170 'is_searchable' => 0,
171 'is_active' => 1,
172
173 ];
174 }
175
176 /**
177 * Test using example code.
178 */
179 /*function testCustomFieldCreateExample( )
180 {
181
182 $customGroup = $this->customGroupCreate('Individual','date_test_group',3);
183 require_once 'api/v3/examples/CustomField/Create.ex.php';
184 $result = custom_field_create_example();
185 $expectedResult = custom_field_create_expectedresult();
186 $this->assertEquals($result,$expectedResult);
187 }*/
188
189 /**
190 * Check with data type - Options with option_values
191 */
192 public function testCustomFieldCreateWithEmptyOptionGroup() {
193 $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'select_test_group']);
194 $params = [
195 'custom_group_id' => $customGroup['id'],
196 'label' => 'Country',
197 'html_type' => 'Select',
198 'data_type' => 'String',
199 'weight' => 4,
200 'is_required' => 1,
201 'is_searchable' => 0,
202 'is_active' => 1,
203 ];
204
205 $customField = $this->callAPISuccess('custom_field', 'create', $params);
206 $this->assertNotNull($customField['id']);
207 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', [
208 'id' => $customField['id'],
209 'return' => 'option_group_id',
210 ]);
211
212 $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0));
213 $optionGroup = $this->callAPISuccess('option_group', 'getsingle', [
214 'id' => $optionGroupID,
215 ]);
216 $this->assertEquals($optionGroup['title'], 'Country');
217 $optionValueCount = $this->callAPISuccess('option_value', 'getcount', [
218 'option_group_id' => $optionGroupID,
219 ]);
220 $this->assertEquals(0, $optionValueCount);
221 }
222
223 /**
224 * Check with non-ascii labels
225 */
226 public function testCustomFieldCreateWithNonAsciiLabel() {
227 $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'select_test_group']);
228 $params = [
229 'custom_group_id' => $customGroup['id'],
230 'label' => 'ôôôô',
231 'html_type' => 'Select',
232 'data_type' => 'String',
233 'weight' => 4,
234 'is_required' => 1,
235 'is_searchable' => 0,
236 'is_active' => 1,
237 ];
238 $customField = $this->callAPISuccess('custom_field', 'create', $params);
239 $this->assertNotNull($customField['id']);
240 $params['label'] = 'ààà';
241 $customField = $this->callAPISuccess('custom_field', 'create', $params);
242 $this->assertNotNull($customField['id']);
243 }
244
245 /**
246 * Test custom field with existing option group.
247 */
248 public function testCustomFieldExistingOptionGroup() {
249 $customGroup = $this->customGroupCreate(['extends' => 'Organization', 'title' => 'test_group']);
250 $params = [
251 'custom_group_id' => $customGroup['id'],
252 // Just to say something:
253 'label' => 'Organization Gender',
254 'html_type' => 'Select',
255 'data_type' => 'Int',
256 'weight' => 4,
257 'is_required' => 1,
258 'is_searchable' => 0,
259 'is_active' => 1,
260 // Option group id 3: gender
261 'option_group_id' => 3,
262 ];
263
264 $customField = $this->callAPISuccess('custom_field', 'create', $params);
265 $this->assertNotNull($customField['id']);
266 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', [
267 'id' => $customField['id'],
268 'return' => 'option_group_id',
269 ]);
270
271 $this->assertEquals($optionGroupID, 3);
272 }
273
274 /**
275 * Test adding an optionGroup to an existing field doesn't cause a fatal error.
276 *
277 * (this was happening due to a check running despite no existing option_group_id)
278 *
279 * @throws \CiviCRM_API3_Exception
280 */
281 public function testUpdateCustomFieldAddOptionGroup() {
282 $customGroup = $this->customGroupCreate(['extends' => 'Organization', 'title' => 'test_group']);
283 $params = [
284 'custom_group_id' => $customGroup['id'],
285 'label' => 'Organization Gender',
286 'html_type' => 'Text',
287 'data_type' => 'Int',
288 ];
289
290 $customField = $this->callAPISuccess('custom_field', 'create', $params);
291 $this->callAPISuccess('CustomField', 'create', [
292 'option_group_id' => civicrm_api3('OptionGroup', 'getvalue', ['options' => ['limit' => 1], 'return' => 'id']),
293 'id' => $customField['id'],
294 'html_type' => 'Select',
295 ]);
296 }
297
298 /**
299 * Test custom field get works & return param works
300 */
301 public function testCustomFieldGetReturnOptions() {
302 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']);
303 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
304
305 $result = $this->callAPISuccess('custom_field', 'getsingle', [
306 'id' => $customField['id'],
307 'return' => 'data_type',
308 ]);
309 $this->assertTrue(array_key_exists('data_type', $result));
310 $this->assertFalse(array_key_exists('custom_group_id', $result));
311 }
312
313 /**
314 * Test custom field get works & return param works
315 */
316 public function testCustomFieldGetReturnArray() {
317 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']);
318 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
319
320 $result = $this->callAPISuccess('custom_field', 'getsingle', [
321 'id' => $customField['id'],
322 'return' => ['data_type'],
323 ]);
324 $this->assertTrue(array_key_exists('data_type', $result));
325 $this->assertFalse(array_key_exists('custom_group_id', $result));
326 }
327
328 /**
329 * Test custom field get works & return param works
330 */
331 public function testCustomFieldGetReturnTwoOptions() {
332 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'test_group']);
333 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
334
335 $result = $this->callAPISuccess('custom_field', 'getsingle', [
336 'id' => $customField['id'],
337 'return' => 'data_type, custom_group_id',
338 ]);
339 $this->assertTrue(array_key_exists('data_type', $result));
340 $this->assertTrue(array_key_exists('custom_group_id', $result));
341 $this->assertFalse(array_key_exists('label', $result));
342 }
343
344 public function testCustomFieldCreateWithOptionValues() {
345 $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'select_test_group']);
346
347 $option_values = [
348 [
349 'weight' => 1,
350 'label' => 'Label1',
351 'value' => 1,
352 'is_active' => 1,
353 ],
354 [
355 'weight' => 2,
356 'label' => 'Label2',
357 'value' => 2,
358 'is_active' => 1,
359 ],
360 ];
361
362 $params = [
363 'custom_group_id' => $customGroup['id'],
364 'label' => 'Our special field',
365 'html_type' => 'Select',
366 'data_type' => 'String',
367 'weight' => 4,
368 'is_required' => 1,
369 'is_searchable' => 0,
370 'is_active' => 1,
371 'option_values' => $option_values,
372
373 ];
374
375 $customField = $this->callAPISuccess('custom_field', 'create', $params);
376
377 $this->assertAPISuccess($customField);
378 $this->assertNotNull($customField['id']);
379 $getFieldsParams = [
380 'options' => ['get_options' => 'custom_' . $customField['id']],
381 'action' => 'create',
382 ];
383 $description = "Demonstrates retrieving metadata with custom field options.";
384 $subfile = "GetFieldsOptions";
385 $fields = $this->callAPIAndDocument('contact', 'getfields', $getFieldsParams, __FUNCTION__, 'ContactTest.php', $description, $subfile);
386 $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]);
387 $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]);
388 $getOptionsArray = [
389 'field' => 'custom_' . $customField['id'],
390 ];
391 $description = "Demonstrates retrieving options for a custom field.";
392 $subfile = "GetOptions";
393 $result = $this->callAPIAndDocument('contact', 'getoptions', $getOptionsArray, __FUNCTION__, 'ContactTest.php', $description, '');
394 $this->assertEquals('Label1', $result['values'][1]);
395 }
396
397 ///////////////// civicrm_custom_field_delete methods
398
399 /**
400 * Check without Field ID.
401 */
402 public function testCustomFieldDeleteWithoutFieldID() {
403 $params = [];
404 $customField = $this->callAPIFailure('custom_field', 'delete', $params,
405 'Mandatory key(s) missing from params array: id');
406 }
407
408 /**
409 * Check without valid array.
410 */
411 public function testCustomFieldDelete() {
412 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']);
413 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
414 $this->assertNotNull($customField['id']);
415
416 $params = [
417 'id' => $customField['id'],
418 ];
419 $result = $this->callAPIAndDocument('custom_field', 'delete', $params, __FUNCTION__, __FILE__);
420
421 $this->assertAPISuccess($result);
422 }
423
424 /**
425 * Check for Option Value.
426 */
427 public function testCustomFieldOptionValueDelete() {
428 $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'ABC']);
429 $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC');
430 $params = [
431 'id' => $customOptionValueFields,
432 ];
433
434 $customField = $this->callAPISuccess('custom_field', 'delete', $customOptionValueFields);
435 }
436
437 /**
438 * If there's one custom group for "Contact" and one for "Activity", then "Contact.getfields"
439 * and "Activity.getfields" should return only their respective fields (not the other's fields),
440 * and unrelated entities should return no custom fields.
441 */
442 public function testGetfields_CrossEntityPollution() {
443 $auxEntities = ['Email', 'Address', 'LocBlock', 'Membership', 'ContributionPage', 'ReportInstance'];
444 $allEntities = array_merge(['Contact', 'Activity'], $auxEntities);
445
446 // Baseline - getfields doesn't reporting any customfields for any entities
447 foreach ($allEntities as $entity) {
448 $this->assertEquals(
449 [],
450 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', [])),
451 "Baseline custom fields for $entity should be empty"
452 );
453 }
454
455 // Add some fields
456 $contactGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'test_group_c']);
457 $contactField = $this->customFieldCreate([
458 'custom_group_id' => $contactGroup['id'],
459 'label' => 'For Contacts',
460 ]);
461 $indivGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group_i']);
462 $indivField = $this->customFieldCreate(['custom_group_id' => $indivGroup['id'], 'label' => 'For Individuals']);
463 $activityGroup = $this->customGroupCreate(['extends' => 'Activity', 'title' => 'test_group_a']);
464 $activityField = $this->customFieldCreate([
465 'custom_group_id' => $activityGroup['id'],
466 'label' => 'For Activities',
467 ]);
468
469 // Check getfields
470 $this->assertEquals(
471 ['custom_' . $contactField['id'], 'custom_' . $indivField['id']],
472 $this->getCustomFieldKeys($this->callAPISuccess('Contact', 'getfields', [])),
473 'Contact custom fields'
474 );
475 $this->assertEquals(
476 ['custom_' . $contactField['id'], 'custom_' . $indivField['id']],
477 $this->getCustomFieldKeys($this->callAPISuccess('Individual', 'getfields', [])),
478 'Individual custom fields'
479 );
480 $this->assertEquals(
481 ['custom_' . $contactField['id']],
482 $this->getCustomFieldKeys($this->callAPISuccess('Organization', 'getfields', [])),
483 'Organization custom fields'
484 );
485 $this->assertEquals(
486 ['custom_' . $activityField['id']],
487 $this->getCustomFieldKeys($this->callAPISuccess('Activity', 'getfields', [])),
488 'Activity custom fields'
489 );
490 foreach ($auxEntities as $entity) {
491 $this->assertEquals(
492 [],
493 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', [])),
494 "Custom fields for $entity should be empty"
495 );
496 }
497 }
498
499 /**
500 * Test setting and getting a custom file field value.
501 *
502 * Uses the "attachment" api for setting value.
503 */
504 public function testCustomFileField() {
505 $customGroup = $this->customGroupCreate(['title' => 'attachment_test_group']);
506 $params = [
507 'custom_group_id' => $customGroup['id'],
508 'name' => 'test_file_attachment',
509 'label' => 'test_file_attachment',
510 'html_type' => 'File',
511 'data_type' => 'File',
512 'is_active' => 1,
513 ];
514 $customField = $this->callAPISuccess('custom_field', 'create', $params);
515 $cfId = 'custom_' . $customField['id'];
516
517 $cid = $this->individualCreate();
518
519 $attachment = $this->callAPISuccess('attachment', 'create', [
520 'name' => CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC) . '_testCustomFileField.txt',
521 'mime_type' => 'text/plain',
522 'content' => 'My test content',
523 'field_name' => $cfId,
524 'entity_id' => $cid,
525 ]);
526 $this->assertAttachmentExistence(TRUE, $attachment);
527
528 $result = $this->callAPISuccess('contact', 'getsingle', [
529 'id' => $cid,
530 'return' => $cfId,
531 ]);
532
533 $this->assertEquals($attachment['id'], $result[$cfId]);
534 }
535
536 public function testUpdateCustomField() {
537 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
538 $params = ['id' => $customGroup['id'], 'is_active' => 0];
539 $result = $this->callAPISuccess('CustomGroup', 'create', $params);
540 $result = array_shift($result['values']);
541
542 $this->assertEquals(0, $result['is_active']);
543
544 $this->customGroupDelete($customGroup['id']);
545 }
546
547 public function testCustomFieldCreateWithOptionGroupName() {
548 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_custom_group']);
549 $params = [
550 'custom_group_id' => $customGroup['id'],
551 'name' => 'Activity type',
552 'label' => 'Activity type',
553 'data_type' => 'String',
554 'html_type' => 'Select',
555 'option_group_id' => 'activity_type',
556 ];
557 $result = $this->callAPISuccess('CustomField', 'create', $params);
558 }
559
560 /**
561 * @param $getFieldsResult
562 *
563 * @return array
564 */
565 public function getCustomFieldKeys($getFieldsResult) {
566 $isCustom = function ($key) {
567 return preg_match('/^custom_/', $key);
568 };
569 $r = array_values(array_filter(array_keys($getFieldsResult['values']), $isCustom));
570 sort($r);
571 return $r;
572 }
573
574 public function testMakeSearchableContactReferenceFieldUnsearchable() {
575 $customGroup = $this->customGroupCreate([
576 'name' => 'testCustomGroup',
577 'title' => 'testCustomGroup',
578 'extends' => 'Individual',
579 ]);
580 $params = [
581 'name' => 'testCustomField',
582 'label' => 'testCustomField',
583 'custom_group_id' => 'testCustomGroup',
584 'data_type' => 'ContactReference',
585 'html_type' => 'Autocomplete-Select',
586 'is_searchable' => '1',
587 ];
588 $result = $this->callAPISuccess('CustomField', 'create', $params);
589 $params = [
590 'id' => $result['id'],
591 'is_searchable' => 0,
592 ];
593 $result = $this->callAPISuccess('CustomField', 'create', $params);
594 }
595
596 /**
597 * Test disabling a searchable contact reference field.
598 */
599 public function testDisableSearchableContactReferenceField() {
600 $customGroup = $this->customGroupCreate([
601 'name' => 'testCustomGroup',
602 'title' => 'testCustomGroup',
603 'extends' => 'Individual',
604 ]);
605 $params = [
606 'name' => 'testCustomField',
607 'label' => 'testCustomField',
608 'custom_group_id' => 'testCustomGroup',
609 'data_type' => 'ContactReference',
610 'html_type' => 'Autocomplete-Select',
611 'is_searchable' => '1',
612 ];
613 $result = $this->callAPISuccess('CustomField', 'create', $params);
614 $params = [
615 'id' => $result['id'],
616 'is_active' => 0,
617 ];
618 $this->callAPISuccess('CustomField', 'create', $params);
619 }
620
621 }