From 98d41b7df288ef827e0c4fb70a8965934c837010 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 30 Jan 2017 15:11:36 -0500 Subject: [PATCH] CRM-19693 - Correctly handle all components being disabled --- CRM/Core/OptionGroup.php | 10 ++++++++-- tests/phpunit/api/v3/OptionValueTest.php | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CRM/Core/OptionGroup.php b/CRM/Core/OptionGroup.php index 2c99877aa6..c6e8a79c9a 100644 --- a/CRM/Core/OptionGroup.php +++ b/CRM/Core/OptionGroup.php @@ -145,8 +145,14 @@ WHERE v.option_group_id = g.id if ($onlyActive) { $query .= " AND v.is_active = 1 "; - $enabledComponents = '"' . implode('","', CRM_Core_Config::singleton()->enableComponents) . '"'; - $query .= " AND (v.component_id IS NULL OR v.component_id IN (SELECT id FROM civicrm_component WHERE name IN ($enabledComponents))) "; + // Only show options for enabled components + $componentClause = ' v.component_id IS NULL '; + $enabledComponents = CRM_Core_Config::singleton()->enableComponents; + if ($enabledComponents) { + $enabledComponents = '"' . implode('","', $enabledComponents) . '"'; + $componentClause .= " OR v.component_id IN (SELECT id FROM civicrm_component WHERE name IN ($enabledComponents)) "; + } + $query .= " AND ($componentClause) "; } if (in_array($name, self::$_domainIDGroups)) { $query .= " AND v.domain_id = " . CRM_Core_Config::domainID(); diff --git a/tests/phpunit/api/v3/OptionValueTest.php b/tests/phpunit/api/v3/OptionValueTest.php index 8c65204f34..d38a8355bf 100644 --- a/tests/phpunit/api/v3/OptionValueTest.php +++ b/tests/phpunit/api/v3/OptionValueTest.php @@ -213,6 +213,8 @@ class api_v3_OptionValueTest extends CiviUnitTestCase { * Check that component is honoured when fetching options. */ public function testGetOptionWithComponent() { + $components = Civi::settings()->get('enable_components'); + CRM_Core_BAO_ConfigSetting::enableComponent('CiviContribute'); $this->callAPISuccess('option_group', 'get', array( 'name' => 'gender', 'api.option_value.create' => array( @@ -226,6 +228,7 @@ class api_v3_OptionValueTest extends CiviUnitTestCase { 'context' => 'create', )); $this->assertContains('Contrib', $genders['values']); + // Disable relevant component CRM_Core_BAO_ConfigSetting::disableComponent('CiviContribute'); CRM_Core_PseudoConstant::flush(); @@ -241,6 +244,19 @@ class api_v3_OptionValueTest extends CiviUnitTestCase { 'context' => 'get', )); $this->assertContains('Contrib', $genders['values']); + + // Now disable all components and ensure we can still fetch options with no errors + CRM_Core_BAO_ConfigSetting::setEnabledComponents(array()); + CRM_Core_PseudoConstant::flush(); + // New option should still be hidden for "create" context + $genders = $this->callAPISuccess('contact', 'getoptions', array( + 'field' => 'gender_id', + 'context' => 'create', + )); + $this->assertNotContains('Contrib', $genders['values']); + + // Restore original state + CRM_Core_BAO_ConfigSetting::setEnabledComponents($components); } /** -- 2.25.1