dev/core#2823 Move validation into validation function
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 8 Sep 2021 02:32:43 +0000 (14:32 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 9 Sep 2021 06:54:31 +0000 (18:54 +1200)
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

index eff92beeb2529032a86051e73a788a0b1dac0fed..0d77755664fd98fddc5554a90e0392cda0a0ce40 100644 (file)
@@ -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
    *