From c44d3d257505fa6556aa06843f8d9d8817039a57 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 8 Feb 2022 21:53:48 -0500 Subject: [PATCH] OptionValues - Specify id+name+label for option callbacks that use it Previously some API calls relied on a bug which conflated name with label, that bug has been fixed, causing some test failures. The solution is to update the option lists with a full multidimensional array with translated labels and untranslated names. --- CRM/Core/BAO/CustomField.php | 72 +++++++++++++++--- CRM/Core/DAO/StatusPreference.php | 4 +- CRM/Core/SelectValues.php | 96 ++++++++++++++++++++---- CRM/Custom/Page/Field.php | 2 +- CRM/Utils/Check.php | 41 +++++----- Civi/Api4/Action/System/Check.php | 2 +- tests/phpunit/api/v3/CustomFieldTest.php | 4 +- tests/phpunit/api/v3/CustomValueTest.php | 4 +- xml/schema/Core/StatusPreference.xml | 2 +- 9 files changed, 169 insertions(+), 58 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 21547af73c..84e20f7f1e 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -35,18 +35,66 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { */ public static function dataType() { return [ - 'String' => ts('Alphanumeric'), - 'Int' => ts('Integer'), - 'Float' => ts('Number'), - 'Money' => ts('Money'), - 'Memo' => ts('Note'), - 'Date' => ts('Date'), - 'Boolean' => ts('Yes or No'), - 'StateProvince' => ts('State/Province'), - 'Country' => ts('Country'), - 'File' => ts('File'), - 'Link' => ts('Link'), - 'ContactReference' => ts('Contact Reference'), + [ + 'id' => 'String', + 'name' => 'Alphanumeric', + 'label' => ts('Alphanumeric'), + ], + [ + 'id' => 'Int', + 'name' => 'Integer', + 'label' => ts('Integer'), + ], + [ + 'id' => 'Float', + 'name' => 'Number', + 'label' => ts('Number'), + ], + [ + 'id' => 'Money', + 'name' => 'Money', + 'label' => ts('Money'), + ], + [ + 'id' => 'Memo', + 'name' => 'Note', + 'label' => ts('Note'), + ], + [ + 'id' => 'Date', + 'name' => 'Date', + 'label' => ts('Date'), + ], + [ + 'id' => 'Boolean', + 'name' => 'Yes or No', + 'label' => ts('Yes or No'), + ], + [ + 'id' => 'StateProvince', + 'name' => 'State/Province', + 'label' => ts('State/Province'), + ], + [ + 'id' => 'Country', + 'name' => 'Country', + 'label' => ts('Country'), + ], + [ + 'id' => 'File', + 'name' => 'File', + 'label' => ts('File'), + ], + [ + 'id' => 'Link', + 'name' => 'Link', + 'label' => ts('Link'), + ], + [ + 'id' => 'ContactReference', + 'name' => 'Contact Reference', + 'label' => ts('Contact Reference'), + ], ]; } diff --git a/CRM/Core/DAO/StatusPreference.php b/CRM/Core/DAO/StatusPreference.php index 9331244143..60d0f297ef 100644 --- a/CRM/Core/DAO/StatusPreference.php +++ b/CRM/Core/DAO/StatusPreference.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Core/StatusPreference.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:73ca47fbe3712351ea43f57b993c7a92) + * (GenCodeChecksum:1fca44b28defd0dfa440e071408ae623) */ /** @@ -228,7 +228,7 @@ class CRM_Core_DAO_StatusPreference extends CRM_Core_DAO { 'bao' => 'CRM_Core_BAO_StatusPreference', 'localizable' => 0, 'pseudoconstant' => [ - 'callback' => 'CRM_Utils_Check::getSeverityList', + 'callback' => 'CRM_Utils_Check::getSeverityOptions', ], 'add' => '4.7', ], diff --git a/CRM/Core/SelectValues.php b/CRM/Core/SelectValues.php index c15c14f662..31254a4dc7 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -169,16 +169,56 @@ class CRM_Core_SelectValues { */ public static function customHtmlType() { return [ - 'Text' => ts('Single-line input field (text or numeric)'), - 'TextArea' => ts('Multi-line text box (textarea)'), - 'Select' => ts('Drop-down (select list)'), - 'Radio' => ts('Radio buttons'), - 'CheckBox' => ts('Checkbox(es)'), - 'Select Date' => ts('Select Date'), - 'File' => ts('File'), - 'RichTextEditor' => ts('Rich Text Editor'), - 'Autocomplete-Select' => ts('Autocomplete-Select'), - 'Link' => ts('Link'), + [ + 'id' => 'Text', + 'name' => 'Single-line input field (text or numeric)', + 'label' => ts('Single-line input field (text or numeric)'), + ], + [ + 'id' => 'TextArea', + 'name' => 'Multi-line text box (textarea)', + 'label' => ts('Multi-line text box (textarea)'), + ], + [ + 'id' => 'Select', + 'name' => 'Drop-down (select list)', + 'label' => ts('Drop-down (select list)'), + ], + [ + 'id' => 'Radio', + 'name' => 'Radio buttons', + 'label' => ts('Radio buttons'), + ], + [ + 'id' => 'CheckBox', + 'name' => 'Checkbox(es)', + 'label' => ts('Checkbox(es)'), + ], + [ + 'id' => 'Select Date', + 'name' => 'Select Date', + 'label' => ts('Select Date'), + ], + [ + 'id' => 'File', + 'name' => 'File', + 'label' => ts('File'), + ], + [ + 'id' => 'RichTextEditor', + 'name' => 'Rich Text Editor', + 'label' => ts('Rich Text Editor'), + ], + [ + 'id' => 'Autocomplete-Select', + 'name' => 'Autocomplete-Select', + 'label' => ts('Autocomplete-Select'), + ], + [ + 'id' => 'Link', + 'name' => 'Link', + 'label' => ts('Link'), + ], ]; } @@ -868,9 +908,21 @@ class CRM_Core_SelectValues { */ public static function billingMode() { return [ - CRM_Core_Payment::BILLING_MODE_FORM => 'form', - CRM_Core_Payment::BILLING_MODE_BUTTON => 'button', - CRM_Core_Payment::BILLING_MODE_NOTIFY => 'notify', + [ + 'id' => CRM_Core_Payment::BILLING_MODE_FORM, + 'name' => 'form', + 'label' => 'form', + ], + [ + 'id' => CRM_Core_Payment::BILLING_MODE_BUTTON, + 'name' => 'button', + 'label' => 'button', + ], + [ + 'id' => CRM_Core_Payment::BILLING_MODE_NOTIFY, + 'name' => 'notify', + 'label' => 'notify', + ], ]; } @@ -879,9 +931,21 @@ class CRM_Core_SelectValues { */ public static function contributeMode() { return [ - CRM_Core_Payment::BILLING_MODE_FORM => 'direct', - CRM_Core_Payment::BILLING_MODE_BUTTON => 'directIPN', - CRM_Core_Payment::BILLING_MODE_NOTIFY => 'notify', + [ + 'id' => CRM_Core_Payment::BILLING_MODE_FORM, + 'name' => 'direct', + 'label' => 'direct', + ], + [ + 'id' => CRM_Core_Payment::BILLING_MODE_BUTTON, + 'name' => 'directIPN', + 'label' => 'directIPN', + ], + [ + 'id' => CRM_Core_Payment::BILLING_MODE_NOTIFY, + 'name' => 'notify', + 'label' => 'notify', + ], ]; } diff --git a/CRM/Custom/Page/Field.php b/CRM/Custom/Page/Field.php index b11cd85b18..e7c3c86feb 100644 --- a/CRM/Custom/Page/Field.php +++ b/CRM/Custom/Page/Field.php @@ -149,7 +149,7 @@ class CRM_Custom_Page_Field extends CRM_Core_Page { break; } - $customFieldDataType = CRM_Core_BAO_CustomField::dataType(); + $customFieldDataType = array_column(CRM_Core_BAO_CustomField::dataType(), 'label', 'id'); $customField[$customFieldBAO->id]['data_type'] = $customFieldDataType[$customField[$customFieldBAO->id]['data_type']]; $customField[$customFieldBAO->id]['order'] = $customField[$customFieldBAO->id]['weight']; $customField[$customFieldBAO->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action, diff --git a/CRM/Utils/Check.php b/CRM/Utils/Check.php index 08dd65b5be..0c124a71c2 100644 --- a/CRM/Utils/Check.php +++ b/CRM/Utils/Check.php @@ -60,6 +60,22 @@ class CRM_Utils_Check { return self::$severityList; } + /** + * @return array[] + */ + public static function getSeverityOptions() { + return [ + ['id' => 0, 'name' => \Psr\Log\LogLevel::DEBUG, 'label' => ts('Debug')], + ['id' => 1, 'name' => \Psr\Log\LogLevel::INFO, 'label' => ts('Info')], + ['id' => 2, 'name' => \Psr\Log\LogLevel::NOTICE, 'label' => ts('Notice')], + ['id' => 3, 'name' => \Psr\Log\LogLevel::WARNING, 'label' => ts('Warning')], + ['id' => 4, 'name' => \Psr\Log\LogLevel::ERROR, 'label' => ts('Error')], + ['id' => 5, 'name' => \Psr\Log\LogLevel::CRITICAL, 'label' => ts('Critical')], + ['id' => 6, 'name' => \Psr\Log\LogLevel::ALERT, 'label' => ts('Alert')], + ['id' => 7, 'name' => \Psr\Log\LogLevel::EMERGENCY, 'label' => ts('Emergency')], + ]; + } + /** * Display daily system status alerts (admin only). */ @@ -238,28 +254,11 @@ class CRM_Utils_Check { * @return string */ public static function toStatusLabel($level) { - switch ($level) { - case 7: - return ts('System Status: Emergency'); - - case 6: - return ts('System Status: Alert'); - - case 5: - return ts('System Status: Critical'); - - case 4: - return ts('System Status: Error'); - - case 3: - return ts('System Status: Warning'); - - case 2: - return ts('System Status: Notice'); - - default: - return ts('System Status: Ok'); + if ($level > 1) { + $options = array_column(self::getSeverityOptions(), 'label', 'id'); + return ts('System Status: %1', [1 => $options[$level]]); } + return ts('System Status: Ok'); } } diff --git a/Civi/Api4/Action/System/Check.php b/Civi/Api4/Action/System/Check.php index 19df2a2a11..d25cf73d38 100644 --- a/Civi/Api4/Action/System/Check.php +++ b/Civi/Api4/Action/System/Check.php @@ -95,7 +95,7 @@ class Check extends \Civi\Api4\Generic\BasicGetAction { 'title' => 'Severity ID', 'description' => 'Integer representation of Psr\Log\LogLevel', 'data_type' => 'Integer', - 'options' => \CRM_Utils_Check::getSeverityList(), + 'options' => \CRM_Utils_Check::getSeverityOptions(), ], [ 'name' => 'is_visible', diff --git a/tests/phpunit/api/v3/CustomFieldTest.php b/tests/phpunit/api/v3/CustomFieldTest.php index f1253bfc6c..88e4ef20c7 100644 --- a/tests/phpunit/api/v3/CustomFieldTest.php +++ b/tests/phpunit/api/v3/CustomFieldTest.php @@ -106,14 +106,14 @@ class api_v3_CustomFieldTest extends CiviUnitTestCase { public function testCustomFieldCreateAllAvailableFormInputs() { $gid = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'testAllFormInputs']); - $dtype = CRM_Core_BAO_CustomField::dataType(); + $dtype = $customFieldDataType = array_column(CRM_Core_BAO_CustomField::dataType(), 'id'); $htype = CRM_Custom_Form_Field::$_dataToHTML; // Legacy html types returned by v3 $htype['StateProvince'] = ['Select State/Province']; $htype['Country'] = ['Select Country']; - foreach ($dtype as $dkey => $dvalue) { + foreach ($dtype as $dkey) { foreach ($htype[$dkey] as $hvalue) { $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey)); } diff --git a/tests/phpunit/api/v3/CustomValueTest.php b/tests/phpunit/api/v3/CustomValueTest.php index 3c77535539..8f0f3da95e 100644 --- a/tests/phpunit/api/v3/CustomValueTest.php +++ b/tests/phpunit/api/v3/CustomValueTest.php @@ -93,11 +93,11 @@ class api_v3_CustomValueTest extends CiviUnitTestCase { $this->_customField = $this->customFieldCreate(['custom_group_id' => $this->ids['string']['custom_group_id']]); $this->_customFieldID = $this->_customField['id']; - $customFieldDataType = CRM_Core_BAO_CustomField::dataType(); + $customFieldDataType = array_column(CRM_Core_BAO_CustomField::dataType(), 'id'); $dataToHtmlTypes = CRM_Custom_Form_Field::$_dataToHTML; $optionSupportingHTMLTypes = CRM_Custom_Form_Field::$htmlTypesWithOptions; - foreach ($customFieldDataType as $dataType => $label) { + foreach ($customFieldDataType as $dataType) { switch ($dataType) { // skipping File data-type & state province due to caching issues // case 'Country': diff --git a/xml/schema/Core/StatusPreference.xml b/xml/schema/Core/StatusPreference.xml index 76ad54de6e..464a581475 100644 --- a/xml/schema/Core/StatusPreference.xml +++ b/xml/schema/Core/StatusPreference.xml @@ -76,7 +76,7 @@ 1 4.7 - CRM_Utils_Check::getSeverityList + CRM_Utils_Check::getSeverityOptions -- 2.25.1