From 5e8660a8047e4eca0dcfd9772ce2458de649b5ea Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 8 Sep 2021 14:32:43 +1200 Subject: [PATCH] dev/core#2823 Move validation into validation function As the code comments suggest the handling of a module being unrecognised sould be handled in the validate not the enable function --- CRM/Core/ManagedEntities.php | 49 ++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/CRM/Core/ManagedEntities.php b/CRM/Core/ManagedEntities.php index cd03306ce0..2e66ec147d 100644 --- a/CRM/Core/ManagedEntities.php +++ b/CRM/Core/ManagedEntities.php @@ -142,15 +142,9 @@ class CRM_Core_ManagedEntities { // index by moduleName,name $decls = $this->createDeclarationIndex($this->moduleIndex, $this->getDeclarations()); foreach ($decls as $moduleName => $todos) { - if (isset($this->moduleIndex[TRUE][$moduleName])) { + if ($this->isModuleEnabled($moduleName)) { $this->reconcileEnabledModule($this->moduleIndex[TRUE][$moduleName], $todos); } - elseif (isset($this->moduleIndex[FALSE][$moduleName])) { - // do nothing -- module should get swept up later - } - else { - throw new Exception("Entity declaration references invalid or inactive module name [$moduleName]"); - } } } @@ -443,18 +437,53 @@ class CRM_Core_ManagedEntities { * string on error, or FALSE */ protected function validate($declarations) { - foreach ($declarations as $declare) { + foreach ($declarations as $module => $declare) { foreach (['name', 'module', 'entity', 'params'] as $key) { if (empty($declare[$key])) { $str = print_r($declare, TRUE); - return ("Managed Entity is missing field \"$key\": $str"); + return ts('Managed Entity (%1) is missing field "%2": %3', [$module, $key, $str]); } } - // FIXME: validate that each 'module' is known + if (!$this->isModuleRecognised($declare['module'])) { + return ts('Entity declaration references invalid or inactive module name [%1]', [$declare['module']]); + } } return FALSE; } + /** + * Is the module recognised (as an enabled or disabled extension in the system). + * + * @param string $module + * + * @return bool + */ + protected function isModuleRecognised(string $module): bool { + return $this->isModuleDisabled($module) || $this->isModuleEnabled($module); + } + + /** + * Is the module enabled. + * + * @param string $module + * + * @return bool + */ + protected function isModuleEnabled(string $module): bool { + return isset($this->moduleIndex[TRUE][$module]); + } + + /** + * Is the module disabled. + * + * @param string $module + * + * @return bool + */ + protected function isModuleDisabled(string $module): bool { + return isset($this->moduleIndex[FALSE][$module]); + } + /** * @param array $declarations * -- 2.25.1