From edbcbd965acfc9e233fa3d065477224d28c9650e Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sun, 27 Sep 2015 13:49:10 -0400 Subject: [PATCH] CRM-16373 - CRM_Core_BAO_ConfigSetting - Fix enableComponent --- CRM/Core/BAO/ConfigSetting.php | 10 +--- CRM/Core/Component.php | 20 +++---- Civi/Core/Container.php | 1 + .../CRM/Core/BAO/ConfigSettingTest.php | 53 +++++++++++++++++++ tests/phpunit/CiviTest/CiviCaseTestCase.php | 12 ++--- tests/phpunit/CiviTest/CiviUnitTestCase.php | 2 +- 6 files changed, 71 insertions(+), 27 deletions(-) create mode 100644 tests/phpunit/CRM/Core/BAO/ConfigSettingTest.php diff --git a/CRM/Core/BAO/ConfigSetting.php b/CRM/Core/BAO/ConfigSetting.php index 1d6f09c46d..561c086f56 100644 --- a/CRM/Core/BAO/ConfigSetting.php +++ b/CRM/Core/BAO/ConfigSetting.php @@ -343,17 +343,11 @@ class CRM_Core_BAO_ConfigSetting { * @param array $enabledComponents */ public static function setEnabledComponents($enabledComponents) { - $config = CRM_Core_Config::singleton(); - - // fix the config object - $config->enableComponents = $enabledComponents; + // fix the config object. update db. + Civi::settings()->set('enable_components', $enabledComponents); // also force reset of component array CRM_Core_Component::getEnabledComponents(TRUE); - - // update DB - CRM_Core_BAO_Setting::setItem($enabledComponents, - CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enable_components'); } /** diff --git a/CRM/Core/Component.php b/CRM/Core/Component.php index 3254f0047a..b7cdb3fb94 100644 --- a/CRM/Core/Component.php +++ b/CRM/Core/Component.php @@ -42,8 +42,6 @@ class CRM_Core_Component { */ const COMPONENT_INFO_CLASS = 'Info'; - private static $_info = NULL; - static $_contactSubTypes = NULL; /** @@ -52,8 +50,8 @@ class CRM_Core_Component { * @return array|null */ private static function &_info($force = FALSE) { - if (self::$_info == NULL || $force) { - self::$_info = array(); + if (!isset(Civi::$statics[__CLASS__]['info'])|| $force) { + Civi::$statics[__CLASS__]['info'] = array(); $c = array(); $config = CRM_Core_Config::singleton(); @@ -61,12 +59,12 @@ class CRM_Core_Component { foreach ($c as $name => $comp) { if (in_array($name, $config->enableComponents)) { - self::$_info[$name] = $comp; + Civi::$statics[__CLASS__]['info'][$name] = $comp; } } } - return self::$_info; + return Civi::$statics[__CLASS__]['info']; } /** @@ -90,10 +88,8 @@ class CRM_Core_Component { * @throws Exception */ public static function &getComponents($force = FALSE) { - static $_cache = NULL; - - if (!$_cache || $force) { - $_cache = array(); + if (!isset(Civi::$statics[__CLASS__]['all']) || $force) { + Civi::$statics[__CLASS__]['all'] = array(); $cr = new CRM_Core_DAO_Component(); $cr->find(FALSE); @@ -104,12 +100,12 @@ class CRM_Core_Component { if ($infoObject->info['name'] !== $cr->name) { CRM_Core_Error::fatal("There is a discrepancy between name in component registry and in info file ({$cr->name})."); } - $_cache[$cr->name] = $infoObject; + Civi::$statics[__CLASS__]['all'][$cr->name] = $infoObject; unset($infoObject); } } - return $_cache; + return Civi::$statics[__CLASS__]['all']; } /** diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 6c3f0955ba..b2928a8d64 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -357,6 +357,7 @@ class Container { ); if ($loadFromDB && $runtime->dsn) { + \CRM_Utils_Hook::singleton(TRUE); \CRM_Extension_System::singleton(TRUE); $c = new self(); diff --git a/tests/phpunit/CRM/Core/BAO/ConfigSettingTest.php b/tests/phpunit/CRM/Core/BAO/ConfigSettingTest.php new file mode 100644 index 0000000000..1768db1515 --- /dev/null +++ b/tests/phpunit/CRM/Core/BAO/ConfigSettingTest.php @@ -0,0 +1,53 @@ +name; + } + $this->assertTrue(!in_array('CiviCase', $origNames)); + + $enableResult = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase'); + $this->assertTrue($enableResult, 'Cannot enable CiviCase in line ' . __LINE__); + + $newNames = array(); + foreach (CRM_Core_Component::getEnabledComponents() as $c) { + $newNames[] = $c->name; + } + + $this->assertTrue(in_array('CiviCase', $newNames)); + $this->assertEquals(count($newNames), count($origNames) + 1); + } + +} diff --git a/tests/phpunit/CiviTest/CiviCaseTestCase.php b/tests/phpunit/CiviTest/CiviCaseTestCase.php index 553886d563..3d14d4c686 100644 --- a/tests/phpunit/CiviTest/CiviCaseTestCase.php +++ b/tests/phpunit/CiviTest/CiviCaseTestCase.php @@ -48,12 +48,6 @@ class CiviCaseTestCase extends CiviUnitTestCase { public function setUp() { parent::setUp(); - /** @var $hooks \CRM_Utils_Hook_UnitTests */ - $hooks = \CRM_Utils_Hook::singleton(); - $hooks->setHook('civicrm_caseTypes', array($this, 'hook_caseTypes')); - \CRM_Case_XMLRepository::singleton(TRUE); - \CRM_Case_XMLProcessor::flushStaticCaches(); - // CRM-9404 - set-up is a bit cumbersome but had to put something in place to set up activity types & case types //. Using XML was causing breakage as id numbers were changing over time // & was really hard to troubleshoot as involved truncating option_value table to mitigate this & not leaving DB in a @@ -113,6 +107,12 @@ class CiviCaseTestCase extends CiviUnitTestCase { $enableResult = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase'); $this->assertTrue($enableResult, 'Cannot enable CiviCase in line ' . __LINE__); + /** @var $hooks \CRM_Utils_Hook_UnitTests */ + $hooks = \CRM_Utils_Hook::singleton(); + $hooks->setHook('civicrm_caseTypes', array($this, 'hook_caseTypes')); + \CRM_Case_XMLRepository::singleton(TRUE); + \CRM_Case_XMLProcessor::flushStaticCaches(); + // create a logged in USER since the code references it for source_contact_id $this->createLoggedInUser(); $session = CRM_Core_Session::singleton(); diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 9219c9f03d..4e9605dbb8 100755 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -410,7 +410,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { $config = CRM_Core_Config::singleton(TRUE, TRUE); // ugh, performance // when running unit tests, use mockup user framework - $this->hookClass = CRM_Utils_Hook::singleton(TRUE); + $this->hookClass = CRM_Utils_Hook::singleton(); // Make sure the DB connection is setup properly $config->userSystem->setMySQLTimeZone(); -- 2.25.1