Add more support for defining a pseudoconstant for a setting
authoreileen <emcnaughton@wikimedia.org>
Thu, 26 Mar 2020 02:30:41 +0000 (15:30 +1300)
committereileen <emcnaughton@wikimedia.org>
Sat, 28 Mar 2020 01:09:39 +0000 (14:09 +1300)
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
settings/Contribute.setting.php
tests/phpunit/CRM/Core/BAO/SettingTest.php

index 99b0b87c5a315b2f5bbfba22ee1318e67801a1cd..fb5c05aed8a906d36e130a8b4c04196e224f7047 100644 (file)
@@ -147,15 +147,24 @@ class SettingsMetadata {
       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');
       }
     }
   }
index b9bd3699f0a8d7efbeaeac58f6d36fbfb4aa465f..57b57eda2a6e2a03c55493d318df7cb77508cfdb 100644 (file)
@@ -188,8 +188,9 @@ return [
     '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',
index 2901412599e2514b53dd836f124b86caf7218821..c986c6c6ea73efb43d60734704c9a0469e2d1ff4 100644 (file)
  */
 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 @@ class CRM_Core_BAO_SettingTest extends CiviUnitTestCase {
     $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]);
+  }
+
 }