3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Test APIv3 civicrm_create_custom_group
18 class api_v3_CustomFieldTest
extends CiviUnitTestCase
{
21 * Clean up after test.
23 * @throws \CRM_Core_Exception
25 public function tearDown() {
29 'civicrm_entity_file',
35 * Check with no label.
37 public function testCustomFieldCreateWithoutLabel() {
38 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'text_test_group']);
40 'custom_group_id' => $customGroup['id'],
41 'name' => 'test_textfield2',
42 'html_type' => 'Text',
43 'data_type' => 'String',
44 'default_value' => 'abc',
51 $customField = $this->callAPIFailure('custom_field', 'create', $params);
52 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label');
58 public function testCustomFieldCreateWithEdit() {
59 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'text_test_group']);
61 'custom_group_id' => $customGroup['id'],
62 'name' => 'test_textfield2',
64 'html_type' => 'Text',
65 'data_type' => 'String',
66 'default_value' => 'abc',
73 $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__
, __FILE__
);
74 $params['id'] = $customField['id'];
75 $customField = $this->callAPISuccess('custom_field', 'create', $params);
77 $this->assertNotNull($customField['id']);
81 * Check without groupId.
83 public function testCustomFieldCreateWithoutGroupID() {
85 'name' => 'test_textfield1',
87 'html_type' => 'Text',
88 'data_type' => 'String',
89 'default_value' => 'abc',
97 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
98 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id');
102 * Check for Each data type: loop through available form input types
104 public function testCustomFieldCreateAllAvailableFormInputs() {
105 $gid = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'testAllFormInputs']);
107 $dtype = CRM_Core_BAO_CustomField
::dataType();
108 $htype = CRM_Custom_Form_Field
::$_dataToHTML;
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));
121 * Can't figure out the point of this?
125 * @param array $params
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');
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),
162 public function _buildBasicParams($gid, $htype, $dtype) {
164 'custom_group_id' => $gid,
165 'label' => $dtype . $htype,
166 'html_type' => $htype,
167 'data_type' => $dtype,
170 'is_searchable' => 0,
177 * Test using example code.
179 /*function testCustomFieldCreateExample( )
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);
190 * Check with data type - Options with option_values
192 public function testCustomFieldCreateWithEmptyOptionGroup() {
193 $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'select_test_group']);
195 'custom_group_id' => $customGroup['id'],
196 'label' => 'Country',
197 'html_type' => 'Select',
198 'data_type' => 'String',
201 'is_searchable' => 0,
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',
212 $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0));
213 $optionGroup = $this->callAPISuccess('option_group', 'getsingle', [
214 'id' => $optionGroupID,
216 $this->assertEquals($optionGroup['title'], 'Country');
217 $optionValueCount = $this->callAPISuccess('option_value', 'getcount', [
218 'option_group_id' => $optionGroupID,
220 $this->assertEquals(0, $optionValueCount);
224 * Check with non-ascii labels
226 public function testCustomFieldCreateWithNonAsciiLabel() {
227 $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'select_test_group']);
229 'custom_group_id' => $customGroup['id'],
230 'label' => 'ôôôô',
231 'html_type' => 'Select',
232 'data_type' => 'String',
235 'is_searchable' => 0,
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']);
246 * Test custom field with existing option group.
248 public function testCustomFieldExistingOptionGroup() {
249 $customGroup = $this->customGroupCreate(['extends' => 'Organization', 'title' => 'test_group']);
251 'custom_group_id' => $customGroup['id'],
252 // Just to say something:
253 'label' => 'Organization Gender',
254 'html_type' => 'Select',
255 'data_type' => 'Int',
258 'is_searchable' => 0,
260 // Option group id 3: gender
261 'option_group_id' => 3,
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',
271 $this->assertEquals($optionGroupID, 3);
275 * Test adding an optionGroup to an existing field doesn't cause a fatal error.
277 * (this was happening due to a check running despite no existing option_group_id)
279 * @throws \CiviCRM_API3_Exception
281 public function testUpdateCustomFieldAddOptionGroup() {
282 $customGroup = $this->customGroupCreate(['extends' => 'Organization', 'title' => 'test_group']);
284 'custom_group_id' => $customGroup['id'],
285 'label' => 'Organization Gender',
286 'html_type' => 'Text',
287 'data_type' => 'Int',
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',
299 * Test custom field get works & return param works
301 public function testCustomFieldGetReturnOptions() {
302 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']);
303 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
305 $result = $this->callAPISuccess('custom_field', 'getsingle', [
306 'id' => $customField['id'],
307 'return' => 'data_type',
309 $this->assertTrue(array_key_exists('data_type', $result));
310 $this->assertFalse(array_key_exists('custom_group_id', $result));
314 * Test custom field get works & return param works
316 public function testCustomFieldGetReturnArray() {
317 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']);
318 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
320 $result = $this->callAPISuccess('custom_field', 'getsingle', [
321 'id' => $customField['id'],
322 'return' => ['data_type'],
324 $this->assertTrue(array_key_exists('data_type', $result));
325 $this->assertFalse(array_key_exists('custom_group_id', $result));
329 * Test custom field get works & return param works
331 public function testCustomFieldGetReturnTwoOptions() {
332 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'test_group']);
333 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
335 $result = $this->callAPISuccess('custom_field', 'getsingle', [
336 'id' => $customField['id'],
337 'return' => 'data_type, custom_group_id',
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));
344 public function testCustomFieldCreateWithOptionValues() {
345 $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'select_test_group']);
363 'custom_group_id' => $customGroup['id'],
364 'label' => 'Our special field',
365 'html_type' => 'Select',
366 'data_type' => 'String',
369 'is_searchable' => 0,
371 'option_values' => $option_values,
375 $customField = $this->callAPISuccess('custom_field', 'create', $params);
377 $this->assertAPISuccess($customField);
378 $this->assertNotNull($customField['id']);
380 'options' => ['get_options' => 'custom_' . $customField['id']],
381 'action' => 'create',
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]);
389 'field' => 'custom_' . $customField['id'],
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]);
397 ///////////////// civicrm_custom_field_delete methods
400 * Check without Field ID.
402 public function testCustomFieldDeleteWithoutFieldID() {
404 $customField = $this->callAPIFailure('custom_field', 'delete', $params,
405 'Mandatory key(s) missing from params array: id');
409 * Check without valid array.
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']);
417 'id' => $customField['id'],
419 $result = $this->callAPIAndDocument('custom_field', 'delete', $params, __FUNCTION__
, __FILE__
);
421 $this->assertAPISuccess($result);
425 * Check for Option Value.
427 public function testCustomFieldOptionValueDelete() {
428 $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'ABC']);
429 $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC');
431 'id' => $customOptionValueFields,
434 $customField = $this->callAPISuccess('custom_field', 'delete', $customOptionValueFields);
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.
442 public function testGetfields_CrossEntityPollution() {
443 $auxEntities = ['Email', 'Address', 'LocBlock', 'Membership', 'ContributionPage', 'ReportInstance'];
444 $allEntities = array_merge(['Contact', 'Activity'], $auxEntities);
446 // Baseline - getfields doesn't reporting any customfields for any entities
447 foreach ($allEntities as $entity) {
450 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', [])),
451 "Baseline custom fields for $entity should be empty"
456 $contactGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'test_group_c']);
457 $contactField = $this->customFieldCreate([
458 'custom_group_id' => $contactGroup['id'],
459 'label' => 'For Contacts',
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',
471 ['custom_' . $contactField['id'], 'custom_' . $indivField['id']],
472 $this->getCustomFieldKeys($this->callAPISuccess('Contact', 'getfields', [])),
473 'Contact custom fields'
476 ['custom_' . $contactField['id'], 'custom_' . $indivField['id']],
477 $this->getCustomFieldKeys($this->callAPISuccess('Individual', 'getfields', [])),
478 'Individual custom fields'
481 ['custom_' . $contactField['id']],
482 $this->getCustomFieldKeys($this->callAPISuccess('Organization', 'getfields', [])),
483 'Organization custom fields'
486 ['custom_' . $activityField['id']],
487 $this->getCustomFieldKeys($this->callAPISuccess('Activity', 'getfields', [])),
488 'Activity custom fields'
490 foreach ($auxEntities as $entity) {
493 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', [])),
494 "Custom fields for $entity should be empty"
500 * Test setting and getting a custom file field value.
502 * Uses the "attachment" api for setting value.
504 public function testCustomFileField() {
505 $customGroup = $this->customGroupCreate(['title' => 'attachment_test_group']);
507 'custom_group_id' => $customGroup['id'],
508 'name' => 'test_file_attachment',
509 'label' => 'test_file_attachment',
510 'html_type' => 'File',
511 'data_type' => 'File',
514 $customField = $this->callAPISuccess('custom_field', 'create', $params);
515 $cfId = 'custom_' . $customField['id'];
517 $cid = $this->individualCreate();
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,
526 $this->assertAttachmentExistence(TRUE, $attachment);
528 $result = $this->callAPISuccess('contact', 'getsingle', [
533 $this->assertEquals($attachment['id'], $result[$cfId]);
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']);
542 $this->assertEquals(0, $result['is_active']);
544 $this->customGroupDelete($customGroup['id']);
547 public function testCustomFieldCreateWithOptionGroupName() {
548 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_custom_group']);
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',
557 $result = $this->callAPISuccess('CustomField', 'create', $params);
561 * @param $getFieldsResult
565 public function getCustomFieldKeys($getFieldsResult) {
566 $isCustom = function ($key) {
567 return preg_match('/^custom_/', $key);
569 $r = array_values(array_filter(array_keys($getFieldsResult['values']), $isCustom));
574 public function testMakeSearchableContactReferenceFieldUnsearchable() {
575 $customGroup = $this->customGroupCreate([
576 'name' => 'testCustomGroup',
577 'title' => 'testCustomGroup',
578 'extends' => 'Individual',
581 'name' => 'testCustomField',
582 'label' => 'testCustomField',
583 'custom_group_id' => 'testCustomGroup',
584 'data_type' => 'ContactReference',
585 'html_type' => 'Autocomplete-Select',
586 'is_searchable' => '1',
588 $result = $this->callAPISuccess('CustomField', 'create', $params);
590 'id' => $result['id'],
591 'is_searchable' => 0,
593 $result = $this->callAPISuccess('CustomField', 'create', $params);
597 * Test disabling a searchable contact reference field.
599 public function testDisableSearchableContactReferenceField() {
600 $customGroup = $this->customGroupCreate([
601 'name' => 'testCustomGroup',
602 'title' => 'testCustomGroup',
603 'extends' => 'Individual',
606 'name' => 'testCustomField',
607 'label' => 'testCustomField',
608 'custom_group_id' => 'testCustomGroup',
609 'data_type' => 'ContactReference',
610 'html_type' => 'Autocomplete-Select',
611 'is_searchable' => '1',
613 $result = $this->callAPISuccess('CustomField', 'create', $params);
615 'id' => $result['id'],
618 $this->callAPISuccess('CustomField', 'create', $params);