From 356013b6566539e1b30c7ab8383cbc400259035a Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 26 Mar 2020 15:30:41 +1300 Subject: [PATCH] Add more support for defining a pseudoconstant for a setting This extends the ways in which a pseudoconstant can be defined in a setting to better reflect the ways that work for the DAO objects. In this one field is converted - default_invoice_page under CiviContribute settings. (Described as Default invoice payment page) The expected result is that the options in that page load the same as before. This change will reduce the temptation to call silly core functions from extensions.... --- Civi/Core/SettingsMetadata.php | 19 +++++++++++----- settings/Contribute.setting.php | 5 +++-- tests/phpunit/CRM/Core/BAO/SettingTest.php | 25 ++++++++++++++++++++-- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/Civi/Core/SettingsMetadata.php b/Civi/Core/SettingsMetadata.php index 99b0b87c5a31..fb5c05aed8a9 100644 --- a/Civi/Core/SettingsMetadata.php +++ b/Civi/Core/SettingsMetadata.php @@ -147,15 +147,24 @@ protected static function loadOptions(&$settingSpec) { if (empty($spec['pseudoconstant'])) { continue; } + $pseudoconstant = $spec['pseudoconstant']; // It would be nice if we could leverage CRM_Core_PseudoConstant::get() somehow, // but it's tightly coupled to DAO/field. However, if you really need to support // more pseudoconstant types, then probably best to refactor it. For now, KISS. - if (!empty($spec['pseudoconstant']['callback'])) { - $spec['options'] = Resolver::singleton()->call($spec['pseudoconstant']['callback'], []); + if (!empty($pseudoconstant['callback'])) { + $spec['options'] = Resolver::singleton()->call($pseudoconstant['callback'], []); } - elseif (!empty($spec['pseudoconstant']['optionGroupName'])) { - $keyColumn = \CRM_Utils_Array::value('keyColumn', $spec['pseudoconstant'], 'value'); - $spec['options'] = \CRM_Core_OptionGroup::values($spec['pseudoconstant']['optionGroupName'], FALSE, FALSE, TRUE, NULL, 'label', TRUE, FALSE, $keyColumn); + elseif (!empty($pseudoconstant['optionGroupName'])) { + $keyColumn = \CRM_Utils_Array::value('keyColumn', $pseudoconstant, 'value'); + $spec['options'] = \CRM_Core_OptionGroup::values($pseudoconstant['optionGroupName'], FALSE, FALSE, TRUE, NULL, 'label', TRUE, FALSE, $keyColumn); + } + if (!empty($pseudoconstant['table'])) { + $params = [ + 'condition' => $pseudoconstant['condition'] ?? [], + 'keyColumn' => $pseudoconstant['keyColumn'] ?? NULL, + 'labelColumn' => $pseudoconstant['labelColumn'] ?? NULL, + ]; + $spec['options'] = \CRM_Core_PseudoConstant::renderOptionsFromTablePseudoconstant($pseudoconstant, $params, ($spec['localize_context'] ?? NULL), 'get'); } } } diff --git a/settings/Contribute.setting.php b/settings/Contribute.setting.php index b9bd3699f0a8..57b57eda2a6e 100644 --- a/settings/Contribute.setting.php +++ b/settings/Contribute.setting.php @@ -188,8 +188,9 @@ 'quick_form_type' => 'Select', 'default' => NULL, 'pseudoconstant' => [ - // @todo - handle table style pseudoconstants for settings & avoid deprecated function. - 'callback' => 'CRM_Contribute_PseudoConstant::contributionPage', + 'table' => 'civicrm_contribution_page', + 'keyColumn' => 'id', + 'labelColumn' => 'title', ], 'html_type' => 'select', 'add' => '4.7', diff --git a/tests/phpunit/CRM/Core/BAO/SettingTest.php b/tests/phpunit/CRM/Core/BAO/SettingTest.php index 2901412599e2..c986c6c6ea73 100644 --- a/tests/phpunit/CRM/Core/BAO/SettingTest.php +++ b/tests/phpunit/CRM/Core/BAO/SettingTest.php @@ -15,17 +15,29 @@ */ class CRM_Core_BAO_SettingTest extends CiviUnitTestCase { + /** + * Original value of civicrm_setting global. + * @var array + */ + private $origSetting; + public function setUp() { parent::setUp(); global $civicrm_setting; $this->origSetting = $civicrm_setting; - CRM_Utils_Cache::singleton()->flush(); + CRM_Utils_Cache::singleton()->clear(); } + /** + * Clean up after test. + * + * @throws \CRM_Core_Exception + */ public function tearDown() { global $civicrm_setting; $civicrm_setting = $this->origSetting; - CRM_Utils_Cache::singleton()->flush(); + $this->quickCleanup(['civicrm_contribution']); + CRM_Utils_Cache::singleton()->clear(); parent::tearDown(); } @@ -227,4 +239,13 @@ public function testSetCivicrmEnvironment() { $this->assertEquals('Development', $environment); } + /** + * Test that options defined as a pseudoconstant can be converted to options. + */ + public function testPseudoConstants() { + $this->contributionPageCreate(); + $metadata = \Civi\Core\SettingsMetadata::getMetadata(['name' => ['default_invoice_page']], NULL, TRUE); + $this->assertEquals('Test Contribution Page', $metadata['default_invoice_page']['options'][1]); + } + }