CRM-14798 - CRM_Utils_Check - Display warning if site has mismatched file names
authorTim Otten <totten@civicrm.org>
Sat, 28 Jun 2014 00:43:10 +0000 (17:43 -0700)
committertotten@civicrm.org <Tim Otten>
Tue, 1 Jul 2014 22:11:48 +0000 (15:11 -0700)
CRM/Utils/Check.php
CRM/Utils/Check/Case.php [new file with mode: 0644]

index 822f40de27988a86a99b9f4daafad1e70843b9ae..32782c3ebd3e6f91b3ffda90cccdf2ab364c2e0d 100644 (file)
@@ -119,12 +119,24 @@ class CRM_Utils_Check {
    * https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_requirements
    */
   public function checkAll() {
-    $security = new CRM_Utils_Check_Security();
-    $env = new CRM_Utils_Check_Env();
-    $messages = array_merge(
-      $security->checkAll(),
-      $env->checkAll()
-    );
+    $checks = array();
+    $checks[] = new CRM_Utils_Check_Security();
+    $checks[] = new CRM_Utils_Check_Env();
+
+    $compInfo = CRM_Core_Component::getEnabledComponents();
+    foreach ($compInfo as $compObj) {
+      switch ($compObj->info['name']) {
+        case 'CiviCase':
+          $checks[] = new CRM_Utils_Check_Case(CRM_Case_XMLRepository::singleton(), CRM_Case_PseudoConstant::caseType('name'));
+          break;
+        default:
+      }
+    }
+
+    $messages = array();
+    foreach ($checks as $check) {
+      $messages = array_merge($messages, $check->checkAll());
+    }
     return $messages;
   }
 
diff --git a/CRM/Utils/Check/Case.php b/CRM/Utils/Check/Case.php
new file mode 100644 (file)
index 0000000..a3995ec
--- /dev/null
@@ -0,0 +1,117 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.4                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+*/
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC (c) 2004-2014
+ * $Id: $
+ *
+ */
+class CRM_Utils_Check_Case {
+
+  /**
+   * @var CRM_Case_XMLRepository
+   */
+  protected $xmlRepo;
+
+  /**
+   * @var array<string>
+   */
+  protected $caseTypeNames;
+
+  /**
+   * @param CRM_Case_XMLRepository $xmlRepo
+   * @param array<string> $caseTypeNames
+   */
+  function __construct($xmlRepo, $caseTypeNames) {
+    $this->caseTypeNames = $caseTypeNames;
+    $this->xmlRepo = $xmlRepo;
+  }
+
+  /**
+   * Run some sanity checks.
+   *
+   * @return array<CRM_Utils_Check_Message>
+   */
+  public function checkAll() {
+    $messages = array_merge(
+      $this->checkCaseTypeNameConsistency()
+    );
+    return $messages;
+  }
+
+  /**
+   * Check that the case-type names don't rely on double-munging.
+   *
+   * @return array<CRM_Utils_Check_Message> an empty array, or a list of warnings
+   */
+  public function checkCaseTypeNameConsistency() {
+    $messages = array();
+
+    foreach ($this->caseTypeNames as $caseTypeName) {
+      $normalFile = $this->xmlRepo->findXmlFile($caseTypeName);
+      $mungedFile = $this->xmlRepo->findXmlFile(CRM_Case_XMLProcessor::mungeCaseType($caseTypeName));
+
+      if ($normalFile && $mungedFile && $normalFile == $mungedFile) {
+        // ok
+      }
+      elseif ($normalFile && $mungedFile) {
+        $messages[] = new CRM_Utils_Check_Message(
+          __FUNCTION__,
+          ts('Case type "%2" has duplicate XML files ("%3" and "%4").<br /><a href="%1">Read more about this warning</a>', array(
+            1 => CRM_Utils_System::getWikiBaseURL() . __FUNCTION__,
+            2 => $caseTypeName,
+            3 => $normalFile,
+            4 => $mungedFile,
+          )),
+          ts('CiviCase')
+        );
+      }
+      elseif ($normalFile && !$mungedFile) {
+        // ok
+      }
+      elseif (!$normalFile && $mungedFile) {
+        $messages[] = new CRM_Utils_Check_Message(
+          __FUNCTION__,
+          ts('Case type "%2" corresponds to XML file ("%3") The XML file should be named "%4".<br /><a href="%1">Read more about this warning</a>', array(
+            1 => CRM_Utils_System::getWikiBaseURL() . __FUNCTION__,
+            2 => $caseTypeName,
+            3 => $mungedFile,
+            4 => "{$caseTypeName}.xml",
+          )),
+          ts('CiviCase')
+        );
+      }
+      elseif (!$normalFile && !$mungedFile) {
+        // ok -- probably a new or DB-based CaseType
+      }
+    }
+
+    return $messages;
+  }
+}
\ No newline at end of file