diff --git a/CRM/Core/BAO/OptionValue.php b/CRM/Core/BAO/OptionValue.php index 34d440128dc5..2f70600ba3d9 100644 --- a/CRM/Core/BAO/OptionValue.php +++ b/CRM/Core/BAO/OptionValue.php @@ -266,6 +266,7 @@ public static function del($optionValueId) { $hookParams = ['id' => $optionValueId]; CRM_Utils_Hook::pre('delete', 'OptionValue', $optionValueId, $hookParams); if (self::updateRecords($optionValueId, CRM_Core_Action::DELETE)) { + Civi::cache('metadata')->flush(); CRM_Core_PseudoConstant::flush(); $optionValue->delete(); CRM_Utils_Hook::post('delete', 'OptionValue', $optionValueId, $optionValue); diff --git a/CRM/Core/OptionGroup.php b/CRM/Core/OptionGroup.php index ccbe32f8adcc..04e989436c42 100644 --- a/CRM/Core/OptionGroup.php +++ b/CRM/Core/OptionGroup.php @@ -110,30 +110,28 @@ public static function &valuesCommon( * The values as specified by the params */ public static function &values( - $name, $flip = FALSE, $grouping = FALSE, + string $name, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $condition = NULL, $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE, $keyColumnName = 'value', $orderBy = 'weight' ) { - $cache = CRM_Utils_Cache::singleton(); + if (self::isDomainOptionGroup($name)) { $cacheKey = self::createCacheKey($name, CRM_Core_I18n::getLocale(), $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName, $orderBy, CRM_Core_Config::domainID()); } else { $cacheKey = self::createCacheKey($name, CRM_Core_I18n::getLocale(), $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName, $orderBy); } - + $cache = Civi::cache('metadata'); if (!$fresh) { - // Fetch from static var - if (array_key_exists($cacheKey, self::$_cache)) { - return self::$_cache[$cacheKey]; - } - // Fetch from main cache - self::$_cache[$cacheKey] = $cache->get($cacheKey); - if (self::$_cache[$cacheKey] !== NULL) { - return self::$_cache[$cacheKey]; + if ($cache->has($cacheKey)) { + $result = $cache->get($cacheKey); + return $result; } } + else { + CRM_Core_Error::deprecatedWarning('do not call to flush cache'); + } $query = " SELECT v.{$labelColumnName} as {$labelColumnName} ,v.{$keyColumnName} as value, v.grouping as `grouping` @@ -144,7 +142,7 @@ public static function &values( AND g.is_active = 1 "; if ($onlyActive) { - $query .= " AND v.is_active = 1 "; + $query .= ' AND v.is_active = 1 '; // Only show options for enabled components $componentClause = ' v.component_id IS NULL '; $enabledComponents = CRM_Core_Config::singleton()->enableComponents; @@ -155,7 +153,7 @@ public static function &values( $query .= " AND ($componentClause) "; } if (self::isDomainOptionGroup($name)) { - $query .= " AND v.domain_id = " . CRM_Core_Config::domainID(); + $query .= ' AND v.domain_id = ' . CRM_Core_Config::domainID(); } if ($condition) { @@ -172,7 +170,6 @@ public static function &values( // call option value hook CRM_Utils_Hook::optionValues($var, $name); - self::$_cache[$cacheKey] = $var; $cache->set($cacheKey, $var); return $var; diff --git a/CRM/Extension/Manager/Report.php b/CRM/Extension/Manager/Report.php index da14a38b2129..06160d34fb33 100644 --- a/CRM/Extension/Manager/Report.php +++ b/CRM/Extension/Manager/Report.php @@ -37,7 +37,7 @@ public function getGroupId() { * * @throws CRM_Core_Exception */ - public function onPreInstall(CRM_Extension_Info $info) { + public function onPreInstall(CRM_Extension_Info $info): void { $customReports = $this->getCustomReportsByName(); if (array_key_exists($info->key, $customReports)) { throw new CRM_Core_Exception(ts('This report is already registered.')); @@ -67,7 +67,7 @@ public function onPreInstall(CRM_Extension_Info $info) { 'is_active' => 1, ]; - $optionValue = CRM_Core_BAO_OptionValue::add($params); + CRM_Core_BAO_OptionValue::add($params); } /** @@ -108,14 +108,14 @@ public function onPreEnable(CRM_Extension_Info $info) { * @return array */ public function getCustomReportsByName() { - return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE); + return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE); } /** * @return array */ public function getCustomReportsById() { - return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE); + return CRM_Core_OptionGroup::values(self::REPORT_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE); } } diff --git a/CRM/Extension/Manager/Search.php b/CRM/Extension/Manager/Search.php index 13ec3bca1eb4..2f3910896c32 100644 --- a/CRM/Extension/Manager/Search.php +++ b/CRM/Extension/Manager/Search.php @@ -105,15 +105,15 @@ public function onPreEnable(CRM_Extension_Info $info) { /** * @return array */ - protected function getCustomSearchesByName() { - return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE); + protected function getCustomSearchesByName(): array { + return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE); } /** * @return array */ - protected function getCustomSearchesById() { - return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE); + protected function getCustomSearchesById(): array { + return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE); } } diff --git a/tests/phpunit/api/v3/OptionValueTest.php b/tests/phpunit/api/v3/OptionValueTest.php index 6641cde5ad7b..2e532651445b 100644 --- a/tests/phpunit/api/v3/OptionValueTest.php +++ b/tests/phpunit/api/v3/OptionValueTest.php @@ -293,7 +293,7 @@ public function testCreateOptionNoName() { /** * Check that pseudoconstant reflects new value added. */ - public function testCRM11876CreateOptionPseudoConstantUpdated() { + public function testCRM11876CreateOptionPseudoConstantUpdated(): void { $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', [ 'name' => 'payment_instrument', 'return' => 'id', @@ -304,13 +304,13 @@ public function testCRM11876CreateOptionPseudoConstantUpdated() { 'label' => $newOption, ]); - $fields = $this->callAPISuccess('contribution', 'getoptions', ['field' => 'payment_instrument_id']); - $this->assertTrue(in_array($newOption, $fields['values'])); + $fields = $this->callAPISuccess('Contribution', 'getoptions', ['field' => 'payment_instrument_id']); + $this->assertContains($newOption, $fields['values']); - $this->callAPISuccess('option_value', 'delete', ['id' => $apiResult['id']]); + $this->callAPISuccess('OptionValue', 'delete', ['id' => $apiResult['id']]); - $fields = $this->callAPISuccess('contribution', 'getoptions', ['field' => 'payment_instrument_id']); - $this->assertFalse(in_array($newOption, $fields['values'])); + $fields = $this->callAPISuccess('Contribution', 'getoptions', ['field' => 'payment_instrument_id']); + $this->assertNotContains($newOption, $fields['values']); } /**