X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2FCRM%2FCore%2FBAO%2FCustomFieldTest.php;h=8b55cfe182e8f7f58975861e6ccf834e6fe61583;hb=ddc7d6e7b3777edccc734a19dc9d59d837693fdc;hp=0b97de581618dc9647b857eed5f09db10a4d32d9;hpb=41de5d4a9d8bc932899308987824131a7b41a838;p=civicrm-core.git diff --git a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php index 0b97de5816..8b55cfe182 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php @@ -461,12 +461,52 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'option_group_id' => NULL, 'date_format' => NULL, 'time_format' => NULL, - 'is_required' => '0', + 'is_required' => 0, + 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, + 'column_name' => $this->getCustomFieldColumnName('country'), + 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('country'), + 'extends_table' => 'civicrm_contact', + 'search_table' => 'contact_a', + 'serialize' => NULL, + 'pseudoconstant' => [ + 'table' => 'civicrm_country', + 'keyColumn' => 'id', + 'labelColumn' => 'name', + 'nameColumn' => 'iso_code', + ], + ], + $this->getCustomFieldName('multi_country') => [ + 'name' => $this->getCustomFieldName('multi_country'), + 'type' => 1, + 'title' => 'Country-multi', + 'headerPattern' => '//', + 'import' => 1, + 'custom_field_id' => $this->getCustomFieldID('multi_country'), + 'options_per_line' => NULL, + 'text_length' => NULL, + 'data_type' => 'Country', + 'html_type' => 'Select Country', + 'is_search_range' => '0', + 'id' => $this->getCustomFieldID('multi_country'), + 'label' => 'Country-multi', + 'groupTitle' => 'Custom Group', + 'default_value' => NULL, + 'custom_group_id' => $customGroupID, + 'extends' => 'Contact', + 'extends_entity_column_value' => NULL, + 'extends_entity_column_id' => NULL, + 'is_view' => '0', + 'is_multiple' => '0', + 'option_group_id' => NULL, + 'date_format' => NULL, + 'time_format' => NULL, + 'is_required' => 0, 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, - 'column_name' => 'country_' . $this->getCustomFieldID('country'), - 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.country_' . $this->getCustomFieldID('country'), + 'column_name' => $this->getCustomFieldColumnName('multi_country'), + 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('multi_country'), 'extends_table' => 'civicrm_contact', 'search_table' => 'contact_a', + 'serialize' => 1, 'pseudoconstant' => [ 'table' => 'civicrm_country', 'keyColumn' => 'id', @@ -477,7 +517,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { $this->getCustomFieldName('file') => [ 'name' => $this->getCustomFieldName('file'), 'type' => 2, - 'title' => 'Custom Field', + 'title' => 'My file', 'headerPattern' => '//', 'import' => 1, 'custom_field_id' => $this->getCustomFieldID('file'), @@ -487,7 +527,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'html_type' => 'File', 'is_search_range' => '0', 'id' => $this->getCustomFieldID('file'), - 'label' => 'Custom Field', + 'label' => 'My file', 'groupTitle' => 'Custom Group', 'default_value' => NULL, 'custom_group_id' => $customGroupID, @@ -499,12 +539,13 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'option_group_id' => NULL, 'date_format' => NULL, 'time_format' => NULL, - 'is_required' => '0', + 'is_required' => 0, 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, - 'column_name' => 'custom_field_' . $this->getCustomFieldID('file'), - 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.custom_field_' . $this->getCustomFieldID('file'), + 'column_name' => 'my_file_' . $this->getCustomFieldID('file'), + 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.my_file_' . $this->getCustomFieldID('file'), 'extends_table' => 'civicrm_contact', 'search_table' => 'contact_a', + 'serialize' => NULL, ], $this->getCustomFieldName('text') => [ 'name' => $this->getCustomFieldName('text'), @@ -531,13 +572,14 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'option_group_id' => NULL, 'date_format' => NULL, 'time_format' => NULL, - 'is_required' => '1', + 'is_required' => 0, 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, 'column_name' => 'enter_text_here_' . $this->getCustomFieldID('text'), 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.enter_text_here_' . $this->getCustomFieldID('text'), 'extends_table' => 'civicrm_contact', 'search_table' => 'contact_a', 'maxlength' => 300, + 'serialize' => NULL, ], $this->getCustomFieldName('select_string') => [ 'name' => $this->getCustomFieldName('select_string'), @@ -564,12 +606,13 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'option_group_id' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id']), 'date_format' => NULL, 'time_format' => NULL, - 'is_required' => '1', + 'is_required' => 0, 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, 'column_name' => 'pick_color_' . $this->getCustomFieldID('select_string'), 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.pick_color_' . $this->getCustomFieldID('select_string'), 'extends_table' => 'civicrm_contact', 'search_table' => 'contact_a', + 'serialize' => NULL, 'pseudoconstant' => [ 'optionGroupName' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']), 'optionEditPath' => 'civicrm/admin/options/' . $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']), @@ -606,6 +649,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_date_' . $this->getCustomFieldID('select_date'), 'extends_table' => 'civicrm_contact', 'search_table' => 'contact_a', + 'serialize' => NULL, ], $this->getCustomFieldName('link') => [ 'name' => $this->getCustomFieldName('link'), @@ -632,12 +676,13 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'option_group_id' => NULL, 'date_format' => NULL, 'time_format' => NULL, - 'is_required' => '1', + 'is_required' => 0, 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, 'column_name' => 'test_link_' . $this->getCustomFieldID('link'), 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_link_' . $this->getCustomFieldID('link'), 'extends_table' => 'civicrm_contact', 'search_table' => 'contact_a', + 'serialize' => NULL, ], $this->getCustomFieldName('int') => [ 'name' => $this->getCustomFieldName('int'), @@ -664,12 +709,158 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'option_group_id' => NULL, 'date_format' => NULL, 'time_format' => NULL, - 'is_required' => '1', + 'is_required' => 0, 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, 'column_name' => $this->getCustomFieldColumnName('int'), 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('int'), 'extends_table' => 'civicrm_contact', 'search_table' => 'contact_a', + 'serialize' => NULL, + ], + $this->getCustomFieldName('contact_reference') => [ + 'name' => $this->getCustomFieldName('contact_reference'), + 'type' => CRM_Utils_Type::T_INT, + 'title' => 'Contact reference field', + 'headerPattern' => '//', + 'import' => 1, + 'custom_field_id' => $this->getCustomFieldID('contact_reference'), + 'options_per_line' => NULL, + 'text_length' => NULL, + 'data_type' => 'ContactReference', + 'html_type' => 'Autocomplete-Select', + 'is_search_range' => '0', + 'id' => $this->getCustomFieldID('contact_reference'), + 'label' => 'Contact reference field', + 'groupTitle' => 'Custom Group', + 'default_value' => NULL, + 'custom_group_id' => $customGroupID, + 'extends' => 'Contact', + 'extends_entity_column_value' => NULL, + 'extends_entity_column_id' => NULL, + 'is_view' => '0', + 'is_multiple' => '0', + 'option_group_id' => NULL, + 'date_format' => NULL, + 'time_format' => NULL, + 'is_required' => 0, + 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, + 'column_name' => $this->getCustomFieldColumnName('contact_reference'), + 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('contact_reference'), + 'extends_table' => 'civicrm_contact', + 'search_table' => 'contact_a', + 'serialize' => NULL, + ], + $this->getCustomFieldName('state') => [ + 'name' => $this->getCustomFieldName('state'), + 'id' => $this->getCustomFieldID('state'), + 'label' => 'State', + 'headerPattern' => '//', + 'title' => 'State', + 'custom_field_id' => $this->getCustomFieldID('state'), + 'groupTitle' => 'Custom Group', + 'default_value' => NULL, + 'custom_group_id' => $customGroupID, + 'extends' => 'Contact', + 'extends_entity_column_value' => NULL, + 'extends_entity_column_id' => NULL, + 'is_view' => '0', + 'is_multiple' => '0', + 'option_group_id' => NULL, + 'date_format' => NULL, + 'time_format' => NULL, + 'is_required' => 0, + 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, + 'column_name' => $this->getCustomFieldColumnName('state'), + 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('state'), + 'extends_table' => 'civicrm_contact', + 'search_table' => 'contact_a', + 'serialize' => NULL, + 'pseudoconstant' => [ + 'table' => 'civicrm_state_province', + 'keyColumn' => 'id', + 'labelColumn' => 'name', + ], + 'import' => 1, + 'data_type' => 'StateProvince', + 'type' => 1, + 'html_type' => 'Select State/Province', + 'text_length' => NULL, + 'options_per_line' => NULL, + 'is_search_range' => '0', + ], + $this->getCustomFieldName('multi_state') => [ + 'id' => $this->getCustomFieldID('multi_state'), + 'label' => 'State-multi', + 'headerPattern' => '//', + 'title' => 'State-multi', + 'custom_field_id' => $this->getCustomFieldID('multi_state'), + 'groupTitle' => 'Custom Group', + 'default_value' => NULL, + 'custom_group_id' => $customGroupID, + 'extends' => 'Contact', + 'extends_entity_column_value' => NULL, + 'extends_entity_column_id' => NULL, + 'is_view' => '0', + 'is_multiple' => '0', + 'option_group_id' => NULL, + 'date_format' => NULL, + 'time_format' => NULL, + 'is_required' => 0, + 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, + 'column_name' => $this->getCustomFieldColumnName('multi_state'), + 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('multi_state'), + 'extends_table' => 'civicrm_contact', + 'search_table' => 'contact_a', + 'serialize' => 1, + 'pseudoconstant' => [ + 'table' => 'civicrm_state_province', + 'keyColumn' => 'id', + 'labelColumn' => 'name', + ], + 'import' => 1, + 'data_type' => 'StateProvince', + 'name' => $this->getCustomFieldName('multi_state'), + 'type' => 1, + 'html_type' => 'Select State/Province', + 'text_length' => NULL, + 'options_per_line' => NULL, + 'is_search_range' => '0', + ], + $this->getCustomFieldName('boolean') => [ + 'id' => $this->getCustomFieldID('boolean'), + 'label' => 'Yes No', + 'headerPattern' => '//', + 'title' => 'Yes No', + 'custom_field_id' => $this->getCustomFieldID('boolean'), + 'groupTitle' => 'Custom Group', + 'default_value' => NULL, + 'custom_group_id' => $customGroupID, + 'extends' => 'Contact', + 'extends_entity_column_value' => NULL, + 'extends_entity_column_id' => NULL, + 'is_view' => '0', + 'is_multiple' => '0', + 'option_group_id' => NULL, + 'date_format' => NULL, + 'time_format' => NULL, + 'is_required' => 0, + 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, + 'column_name' => $this->getCustomFieldColumnName('boolean'), + 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('boolean'), + 'extends_table' => 'civicrm_contact', + 'search_table' => 'contact_a', + 'import' => 1, + 'data_type' => 'Boolean', + 'name' => $this->getCustomFieldName('boolean'), + 'type' => 16, + 'html_type' => 'Radio', + 'text_length' => NULL, + 'options_per_line' => NULL, + 'is_search_range' => '0', + 'serialize' => NULL, + 'pseudoconstant' => [ + 'callback' => 'CRM_Core_SelectValues::boolean', + ], ], ]; $this->assertEquals($expected, CRM_Core_BAO_CustomField::getFieldsForImport()); @@ -731,4 +922,50 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { $this->assertEquals($expectedDisplayValue, CRM_Core_BAO_CustomField::displayValue($file['id'], $fileField['id'])); } + /** + * Test for hook_civicrm_alterCustomFieldDisplayValue(). + */ + public function testAlterCustomFieldDisplayValueHook() { + CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_alterCustomFieldDisplayValue', [$this, 'alterCustomFieldDisplayValue']); + $customGroupId = $this->customGroupCreate([ + 'extends' => 'Individual', + 'title' => 'Test Contactcustom Group', + ])['id']; + $fieldId = $this->customFieldCreate([ + 'custom_group_id' => $customGroupId, + 'name' => 'alter_cf_field', + 'label' => 'Alter CF Field', + ])['id']; + $contactId = $this->individualCreate(['custom_' . $fieldId => 'Test']); + + $this->assertEquals('Test', $this->callAPISuccessGetValue('Contact', + ['id' => $contactId, 'return' => "custom_{$fieldId}"] + )); + + $values = []; + $fields = [ + 'custom_' . $fieldId => $this->callAPISuccess('Contact', 'getfield', [ + 'name' => 'custom_' . $fieldId, + 'action' => 'get', + ])['values'], + ]; + + // CRM_Core_BAO_UFGroup::getValues() invokes CRM_Core_BAO_CustomField::displayValue() function. + CRM_Core_BAO_UFGroup::getValues($contactId, $fields, $values); + $this->assertEquals('New value', $values['Alter CF Field']); + } + + /** + * @param string $displayValue + * @param mixed $value + * @param int $entityId + * @param array $fieldInfo + * + */ + public function alterCustomFieldDisplayValue(&$displayValue, $value, $entityId, $fieldInfo) { + if ($fieldInfo['name'] == 'alter_cf_field') { + $displayValue = 'New value'; + } + } + }