4 * Class CRM_Core_BAO_CustomFieldTest
8 class CRM_Core_BAO_CustomFieldTest
extends CiviUnitTestCase
{
10 use CRMTraits_Custom_CustomDataTrait
;
12 protected $customFieldID;
15 * Clean up after test.
19 public function tearDown() {
20 $this->quickCleanup([], TRUE);
25 * Test creating a custom field.
27 public function testCreateCustomField() {
28 $customGroup = $this->createCustomField();
29 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
30 'Database check for created CustomField.'
33 'id' => $customFieldID,
34 'label' => 'editTestFld',
36 'data_type' => 'String',
37 'html_type' => 'Text',
38 'custom_group_id' => $customGroup['id'],
41 CRM_Core_BAO_CustomField
::create($fields);
42 $this->assertDBNotNull('CRM_Core_DAO_CustomField', 1, 'id', 'is_active', 'Database check for edited CustomField.');
43 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $fields['label'], 'id', 'label', 'Database check for edited CustomField.');
45 $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.');
46 $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.');
47 $this->assertEquals(strtolower("{$dbFieldName}_{$customFieldID}"), $dbColumnName,
48 "Column name ends in ID");
50 $this->customGroupDelete($customGroup['id']);
54 * Test custom field create accepts passed column name.
56 public function testCreateCustomFieldColumnName() {
57 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
59 'label' => 'testFld 2',
60 'column_name' => 'special_colname',
61 'data_type' => 'String',
62 'html_type' => 'Text',
63 'custom_group_id' => $customGroup['id'],
65 CRM_Core_BAO_CustomField
::create($fields);
66 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
67 'Database check for created CustomField.'
69 $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.');
70 $this->assertEquals($fields['column_name'], $dbColumnName,
71 "Column name set as specified");
73 $this->customGroupDelete($customGroup['id']);
77 * Test that name is used for the column.
79 public function testCreateCustomFieldName() {
80 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
82 'label' => 'testFld 2',
83 'name' => 'special_fldlname',
84 'data_type' => 'String',
85 'html_type' => 'Text',
86 'custom_group_id' => $customGroup['id'],
88 CRM_Core_BAO_CustomField
::create($fields);
89 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
90 'Database check for created CustomField.'
92 $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.');
93 $this->assertEquals($fields['name'], $dbFieldName,
94 "Column name set as specified");
96 $this->customGroupDelete($customGroup['id']);
100 * Test get fields function.
102 public function testGetFields() {
103 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
105 'label' => 'testFld1',
106 'data_type' => 'String',
107 'html_type' => 'Text',
109 'custom_group_id' => $customGroup['id'],
111 CRM_Core_BAO_CustomField
::create($fields);
112 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
113 'Database check for created CustomField.'
116 'label' => 'testFld2',
117 'data_type' => 'String',
118 'html_type' => 'Text',
120 'custom_group_id' => $customGroup['id'],
122 CRM_Core_BAO_CustomField
::create($fields);
123 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
124 'Database check for created CustomField.'
127 $this->customGroupDelete($customGroup['id']);
133 public function testGetDisplayedValues() {
134 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
137 'data_type' => 'Country',
138 'html_type' => 'Select Country',
140 'United States' => 1228,
145 'data_type' => 'StateProvince',
146 'html_type' => 'Multi-Select State/Province',
150 'Alabama, Alaska' => [1000, 1001],
154 'data_type' => 'String',
155 'html_type' => 'Radio',
164 'data_type' => 'String',
165 'html_type' => 'CheckBox',
173 'Label1' => ['key1'],
175 'Label2, Label3' => ['key2', 'key3'],
176 'Label3, Label4' => CRM_Utils_Array
::implodePadded(['key3', 'key4']),
177 'Label1, Label4' => ['key1' => 1, 'key4' => 1],
181 'data_type' => 'Date',
182 'html_type' => 'Select Date',
183 'date_format' => 'd M yy',
186 '1 Jun 1999 1:30PM' => '1999-06-01 13:30',
191 foreach ($fieldsToCreate as $num => $field) {
192 $params = $field +
['label' => 'test field ' . $num, 'custom_group_id' => $customGroup['id']];
193 unset($params['tests']);
194 $createdField = $this->callAPISuccess('customField', 'create', $params);
195 foreach ($field['tests'] as $expected => $input) {
196 $this->assertEquals($expected, CRM_Core_BAO_CustomField
::displayValue($input, $createdField['id']));
200 $this->customGroupDelete($customGroup['id']);
204 * Test CRM_Core_BAO_CustomField::displayValue.
206 * @throws \CRM_Core_Exception
209 public function testGetDisplayedValuesContactRef() {
210 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
212 'data_type' => 'ContactReference',
213 'html_type' => 'Autocomplete-Select',
214 'label' => 'test ref',
215 'custom_group_id' => $customGroup['id'],
217 $createdField = $this->callAPISuccess('customField', 'create', $params);
218 $contact1 = $this->individualCreate();
219 $contact2 = $this->individualCreate(['custom_' . $createdField['id'] => $contact1['id']]);
221 $this->assertEquals($contact1['display_name'], CRM_Core_BAO_CustomField
::displayValue($contact2['id'], $createdField['id']));
222 $this->assertEquals("Bob", CRM_Core_BAO_CustomField
::displayValue("Bob", $createdField['id']));
224 $this->contactDelete($contact2['id']);
225 $this->contactDelete($contact1['id']);
226 $this->customGroupDelete($customGroup['id']);
229 public function testDeleteCustomField() {
230 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
232 'custom_group_id' => $customGroup['id'],
233 'label' => 'Throwaway Field',
234 'dataType' => 'Memo',
235 'htmlType' => 'TextArea',
238 $customField = $this->customFieldCreate($fields);
239 $fieldObject = new CRM_Core_BAO_CustomField();
240 $fieldObject->id
= $customField['id'];
241 $fieldObject->find(TRUE);
242 CRM_Core_BAO_CustomField
::deleteField($fieldObject);
243 $this->assertDBNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id',
244 'custom_group_id', 'Database check for deleted Custom Field.'
246 $this->customGroupDelete($customGroup['id']);
250 * Move a custom field from $groupA to $groupB.
252 * Make sure that data records are correctly matched and created.
254 * @throws \CRM_Core_Exception
256 public function testMoveField() {
257 $countriesByName = array_flip(CRM_Core_PseudoConstant
::country(FALSE, FALSE));
258 $this->assertTrue($countriesByName['Andorra'] > 0);
260 'A' => $this->customGroupCreate([
261 'title' => 'Test_Group A',
262 'name' => 'test_group_a',
263 'extends' => ['Individual'],
269 'B' => $this->customGroupCreate([
270 'title' => 'Test_Group B',
271 'name' => 'test_group_b',
272 'extends' => ['Individual'],
279 $groupA = $groups['A']['values'][$groups['A']['id']];
280 $groupB = $groups['B']['values'][$groups['B']['id']];
281 $countryA = $this->customFieldCreate([
282 'custom_group_id' => $groups['A']['id'],
283 'label' => 'Country A',
284 'dataType' => 'Country',
285 'htmlType' => 'Select Country',
286 'default_value' => NULL,
288 $countryB = $this->customFieldCreate([
289 'custom_group_id' => $groups['A']['id'],
290 'label' => 'Country B',
291 'dataType' => 'Country',
292 'htmlType' => 'Select Country',
293 'default_value' => NULL,
295 $countryC = $this->customFieldCreate([
296 'custom_group_id' => $groups['B']['id'],
297 'label' => 'Country C',
298 'dataType' => 'Country',
299 'htmlType' => 'Select Country',
300 'default_value' => NULL,
304 'countryA' => $countryA['values'][$countryA['id']],
305 'countryB' => $countryB['values'][$countryB['id']],
306 'countryC' => $countryC['values'][$countryC['id']],
309 'alice' => $this->individualCreate([
310 'first_name' => 'Alice',
311 'last_name' => 'Albertson',
312 'custom_' . $fields['countryA']['id'] => $countriesByName['Andorra'],
313 'custom_' . $fields['countryB']['id'] => $countriesByName['Barbados'],
315 'bob' => $this->individualCreate([
316 'first_name' => 'Bob',
317 'last_name' => 'Roberts',
318 'custom_' . $fields['countryA']['id'] => $countriesByName['Austria'],
319 'custom_' . $fields['countryB']['id'] => $countriesByName['Bermuda'],
320 'custom_' . $fields['countryC']['id'] => $countriesByName['Chad'],
322 'carol' => $this->individualCreate([
323 'first_name' => 'Carol',
324 'last_name' => 'Carolson',
325 'custom_' . $fields['countryC']['id'] => $countriesByName['Cambodia'],
330 CRM_Core_BAO_CustomField
::moveField($fields['countryB']['id'], $groupB['id']);
332 // Group[A] no longer has fields[countryB]
333 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
335 $this->assertDBQuery(1, "SELECT {$fields['countryB']['column_name']} FROM " . $groupA['table_name']);
336 $this->fail('Expected exception when querying column on wrong table');
338 catch (PEAR_Exception
$e) {
342 // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before
343 $this->assertDBQuery(1,
344 "SELECT count(*) FROM {$groupB['table_name']}
346 AND {$fields['countryB']['column_name']} = %3
347 AND {$fields['countryC']['column_name']} is null",
349 1 => [$contacts['alice'], 'Integer'],
350 3 => [$countriesByName['Barbados'], 'Integer'],
354 // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record
355 $this->assertDBQuery(1,
356 "SELECT count(*) FROM {$groupB['table_name']}
358 AND {$fields['countryB']['column_name']} = %3
359 AND {$fields['countryC']['column_name']} = %4",
361 1 => [$contacts['bob'], 'Integer'],
362 3 => [$countriesByName['Bermuda'], 'Integer'],
363 4 => [$countriesByName['Chad'], 'Integer'],
367 // Carol: Group[B] still has fields[countryC] but did not get fields[countryB]
368 $this->assertDBQuery(1,
369 "SELECT count(*) FROM {$groupB['table_name']}
371 AND {$fields['countryB']['column_name']} is null
372 AND {$fields['countryC']['column_name']} = %4",
374 1 => [$contacts['carol'], 'Integer'],
375 4 => [$countriesByName['Cambodia'], 'Integer'],
379 $this->customGroupDelete($groups['A']['id']);
380 $this->customGroupDelete($groupB['id']);
384 * Test get custom field id function.
386 * @throws \CiviCRM_API3_Exception
388 public function testGetCustomFieldID() {
389 $this->createCustomField();
390 $fieldID = CRM_Core_BAO_CustomField
::getCustomFieldID('testFld');
391 $this->assertEquals($this->customFieldID
, $fieldID);
393 $fieldID = CRM_Core_BAO_CustomField
::getCustomFieldID('testFld', 'new custom group');
394 $this->assertEquals($this->customFieldID
, $fieldID);
396 $fieldID = CRM_Core_BAO_CustomField
::getCustomFieldID('testFld', 'new custom group', TRUE);
397 $this->assertEquals('custom_' . $this->customFieldID
, $fieldID);
399 // create field with same name in a different group
400 $this->createCustomField('other custom group');
401 $otherFieldID = CRM_Core_BAO_CustomField
::getCustomFieldID('testFld', 'other custom group');
402 // make sure it does not return the field ID of the first field
403 $this->assertNotEquals($fieldID, $otherFieldID);
407 * Create a custom field
409 * @param string $groupTitle
413 protected function createCustomField($groupTitle = 'new custom group') {
414 $customGroup = $this->customGroupCreate([
415 'extends' => 'Individual',
416 'title' => $groupTitle,
419 'label' => 'testFld',
420 'data_type' => 'String',
421 'html_type' => 'Text',
422 'custom_group_id' => $customGroup['id'],
424 $field = CRM_Core_BAO_CustomField
::create($fields);
425 $this->customFieldID
= $field->id
;
430 * Test the getFieldsForImport function.
434 public function testGetFieldsForImport() {
435 $this->entity
= 'Contact';
436 $this->createCustomGroupWithFieldsOfAllTypes();
437 $customGroupID = $this->ids
['CustomGroup']['Custom Group'];
439 $this->getCustomFieldName('country') => [
440 'name' => $this->getCustomFieldName('country'),
442 'title' => 'Country',
443 'headerPattern' => '//',
445 'custom_field_id' => $this->getCustomFieldID('country'),
446 'options_per_line' => NULL,
447 'text_length' => NULL,
448 'data_type' => 'Int',
449 'html_type' => 'Select Country',
450 'is_search_range' => '0',
451 'id' => $this->getCustomFieldID('country'),
452 'label' => 'Country',
453 'groupTitle' => 'Custom Group',
454 'default_value' => NULL,
455 'custom_group_id' => $customGroupID,
456 'extends' => 'Contact',
457 'extends_entity_column_value' => NULL,
458 'extends_entity_column_id' => NULL,
460 'is_multiple' => '0',
461 'option_group_id' => NULL,
462 'date_format' => NULL,
463 'time_format' => NULL,
464 'is_required' => '0',
465 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
466 'column_name' => 'country_' . $this->getCustomFieldID('country'),
467 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.country_' . $this->getCustomFieldID('country'),
468 'extends_table' => 'civicrm_contact',
469 'search_table' => 'contact_a',
471 $this->getCustomFieldName('file') => [
472 'name' => $this->getCustomFieldName('file'),
474 'title' => 'Custom Field',
475 'headerPattern' => '//',
477 'custom_field_id' => $this->getCustomFieldID('file'),
478 'options_per_line' => NULL,
479 'text_length' => NULL,
480 'data_type' => 'File',
481 'html_type' => 'File',
482 'is_search_range' => '0',
483 'id' => $this->getCustomFieldID('file'),
484 'label' => 'Custom Field',
485 'groupTitle' => 'Custom Group',
486 'default_value' => NULL,
487 'custom_group_id' => $customGroupID,
488 'extends' => 'Contact',
489 'extends_entity_column_value' => NULL,
490 'extends_entity_column_id' => NULL,
492 'is_multiple' => '0',
493 'option_group_id' => NULL,
494 'date_format' => NULL,
495 'time_format' => NULL,
496 'is_required' => '0',
497 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
498 'column_name' => 'custom_field_' . $this->getCustomFieldID('file'),
499 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.custom_field_' . $this->getCustomFieldID('file'),
500 'extends_table' => 'civicrm_contact',
501 'search_table' => 'contact_a',
503 $this->getCustomFieldName('text') => [
504 'name' => $this->getCustomFieldName('text'),
506 'title' => 'Enter text here',
507 'headerPattern' => '//',
509 'custom_field_id' => $this->getCustomFieldID('text'),
510 'options_per_line' => NULL,
511 'text_length' => NULL,
512 'data_type' => 'String',
513 'html_type' => 'Text',
514 'is_search_range' => '0',
515 'id' => $this->getCustomFieldID('text'),
516 'label' => 'Enter text here',
517 'groupTitle' => 'Custom Group',
518 'default_value' => 'xyz',
519 'custom_group_id' => '1',
520 'extends' => 'Contact',
521 'extends_entity_column_value' => NULL,
522 'extends_entity_column_id' => NULL,
524 'is_multiple' => '0',
525 'option_group_id' => NULL,
526 'date_format' => NULL,
527 'time_format' => NULL,
528 'is_required' => '1',
529 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
530 'column_name' => 'enter_text_here_' . $this->getCustomFieldID('text'),
531 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.enter_text_here_' . $this->getCustomFieldID('text'),
532 'extends_table' => 'civicrm_contact',
533 'search_table' => 'contact_a',
535 $this->getCustomFieldName('select_string') => [
536 'name' => $this->getCustomFieldName('select_string'),
538 'title' => 'Pick Color',
539 'headerPattern' => '//',
541 'custom_field_id' => $this->getCustomFieldID('select_string'),
542 'options_per_line' => NULL,
543 'text_length' => NULL,
544 'data_type' => 'String',
545 'html_type' => 'Select',
546 'is_search_range' => '0',
547 'id' => $this->getCustomFieldID('select_string'),
548 'label' => 'Pick Color',
549 'groupTitle' => 'Custom Group',
550 'default_value' => NULL,
551 'custom_group_id' => $customGroupID,
552 'extends' => 'Contact',
553 'extends_entity_column_value' => NULL,
554 'extends_entity_column_id' => NULL,
556 'is_multiple' => '0',
557 'option_group_id' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id']),
558 'date_format' => NULL,
559 'time_format' => NULL,
560 'is_required' => '1',
561 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
562 'column_name' => 'pick_color_' . $this->getCustomFieldID('select_string'),
563 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.pick_color_' . $this->getCustomFieldID('select_string'),
564 'extends_table' => 'civicrm_contact',
565 'search_table' => 'contact_a',
566 'pseudoconstant' => [
567 'optionGroupName' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']),
568 'optionEditPath' => 'civicrm/admin/options/' . $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']),
571 $this->getCustomFieldName('select_date') => [
572 'name' => $this->getCustomFieldName('select_date'),
574 'title' => 'test_date',
575 'headerPattern' => '//',
577 'custom_field_id' => $this->getCustomFieldID('select_date'),
578 'options_per_line' => NULL,
579 'text_length' => NULL,
580 'data_type' => 'Date',
581 'html_type' => 'Select Date',
582 'is_search_range' => '0',
583 'date_format' => 'mm/dd/yy',
584 'time_format' => '1',
585 'id' => $this->getCustomFieldID('select_date'),
586 'label' => 'test_date',
587 'groupTitle' => 'Custom Group',
588 'default_value' => '20090711',
589 'custom_group_id' => $customGroupID,
590 'extends' => 'Contact',
591 'extends_entity_column_value' => NULL,
592 'extends_entity_column_id' => NULL,
594 'is_multiple' => '0',
595 'option_group_id' => NULL,
596 'is_required' => '0',
597 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
598 'column_name' => 'test_date_' . $this->getCustomFieldID('select_date'),
599 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_date_' . $this->getCustomFieldID('select_date'),
600 'extends_table' => 'civicrm_contact',
601 'search_table' => 'contact_a',
603 $this->getCustomFieldName('link') => [
604 'name' => $this->getCustomFieldName('link'),
606 'title' => 'test_link',
607 'headerPattern' => '//',
609 'custom_field_id' => $this->getCustomFieldID('link'),
610 'options_per_line' => NULL,
611 'text_length' => NULL,
612 'data_type' => 'Link',
613 'html_type' => 'Link',
614 'is_search_range' => '0',
615 'id' => $this->getCustomFieldID('link'),
616 'label' => 'test_link',
617 'groupTitle' => 'Custom Group',
618 'default_value' => 'http://civicrm.org',
619 'custom_group_id' => $customGroupID,
620 'extends' => 'Contact',
621 'extends_entity_column_value' => NULL,
622 'extends_entity_column_id' => NULL,
624 'is_multiple' => '0',
625 'option_group_id' => NULL,
626 'date_format' => NULL,
627 'time_format' => NULL,
628 'is_required' => '1',
629 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
630 'column_name' => 'test_link_' . $this->getCustomFieldID('link'),
631 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_link_' . $this->getCustomFieldID('link'),
632 'extends_table' => 'civicrm_contact',
633 'search_table' => 'contact_a',
636 $this->assertEquals($expected, CRM_Core_BAO_CustomField
::getFieldsForImport());
640 * Test the bulk create function works.
642 public function testBulkCreate() {
643 $customGroup = $this->customGroupCreate([
644 'extends' => 'Individual',
645 'title' => 'my bulk group',
647 CRM_Core_BAO_CustomField
::bulkSave([
650 'data_type' => 'String',
651 'html_type' => 'Text',
652 'column_name' => 'my_text',
655 'label' => 'test_link',
656 'data_type' => 'Link',
657 'html_type' => 'Link',
658 'is_search_range' => '0',
662 'custom_group_id' => $customGroup['id'],
664 'is_searchable' => 1,
666 $dao = CRM_Core_DAO
::executeQuery(('SHOW CREATE TABLE ' . $customGroup['values'][$customGroup['id']]['table_name']));
668 $this->assertContains('`test_link_2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL', $dao->Create_Table
);
669 $this->assertContains('KEY `INDEX_my_text` (`my_text`)', $dao->Create_Table
);