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