CiviCaseTestCase - Allow specifying the XML file. Allow hook-XML to override core...
authorTim Otten <totten@civicrm.org>
Tue, 27 May 2014 03:45:41 +0000 (20:45 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 28 May 2014 04:00:11 +0000 (21:00 -0700)
CRM/Case/XMLRepository.php
tests/phpunit/CiviTest/CiviCaseTestCase.php

index d23ffcd994e0517073f46bc6c9438771e57da512..b5c29eee155f4e8e661fb79aa05573ca10bbbdd2 100644 (file)
@@ -82,53 +82,54 @@ class CRM_Case_XMLRepository {
     if (!CRM_Utils_Array::value($caseType, $this->xml)) {
       // first check custom templates directory
       $fileName = NULL;
-      $config = CRM_Core_Config::singleton();
-      if (isset($config->customTemplateDir) &&
-        $config->customTemplateDir
-      ) {
-        // check if the file exists in the custom templates directory
-        $fileName = implode(DIRECTORY_SEPARATOR,
-          array(
-            $config->customTemplateDir,
-            'CRM',
-            'Case',
-            'xml',
-            'configuration',
-            "$caseType.xml",
-          )
-        );
+
+      if (!$fileName || !file_exists($fileName)) {
+        $caseTypesViaHook = $this->getCaseTypesViaHook();
+        if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) {
+          $fileName = $caseTypesViaHook[$caseType]['file'];
+        }
       }
 
-      if (!$fileName ||
-        !file_exists($fileName)
-      ) {
-        // check if file exists locally
-        $fileName = implode(DIRECTORY_SEPARATOR,
-          array(
-            dirname(__FILE__),
-            'xml',
-            'configuration',
-            "$caseType.xml",
-          )
-        );
+      if (!$fileName || !file_exists($fileName)) {
+        $config = CRM_Core_Config::singleton();
+        if (isset($config->customTemplateDir) && $config->customTemplateDir) {
+          // check if the file exists in the custom templates directory
+          $fileName = implode(DIRECTORY_SEPARATOR,
+            array(
+              $config->customTemplateDir,
+              'CRM',
+              'Case',
+              'xml',
+              'configuration',
+              "$caseType.xml",
+            )
+          );
+        }
+      }
 
+      if (!$fileName || !file_exists($fileName)) {
         if (!file_exists($fileName)) {
           // check if file exists locally
           $fileName = implode(DIRECTORY_SEPARATOR,
             array(
               dirname(__FILE__),
               'xml',
-              'configuration.sample',
+              'configuration',
               "$caseType.xml",
             )
           );
         }
 
         if (!file_exists($fileName)) {
-          $caseTypesViaHook = $this->getCaseTypesViaHook();
-          if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) {
-            $fileName = $caseTypesViaHook[$caseType]['file'];
-          }
+          // check if file exists locally
+          $fileName = implode(DIRECTORY_SEPARATOR,
+            array(
+              dirname(__FILE__),
+              'xml',
+              'configuration.sample',
+              "$caseType.xml",
+            )
+          );
         }
 
         if (!file_exists($fileName)) {
@@ -158,7 +159,7 @@ class CRM_Case_XMLRepository {
   }
 
   /**
-   * @return array<int,string> symbolic names of case-types
+   * @return array<string> symbolic names of case-types
    */
   public function getAllCaseTypes() {
     if ($this->allCaseTypes === NULL) {
index 5f009ebec12669478fbf22a27bac251dcdd7ab99..8f80ebc335dab466f107f08a15d85491cb4a276b 100644 (file)
@@ -32,13 +32,27 @@ require_once 'CiviTest/CiviUnitTestCase.php';
  */
 class CiviCaseTestCase extends CiviUnitTestCase {
 
+  /**
+   * @var string symbolic-name
+   */
+  protected $caseType;
+
   protected $caseTypeId;
+
   protected $caseStatusGroup;
+
   protected $optionValues;
+
   protected $_loggedInUser;
 
   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-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
@@ -66,6 +80,9 @@ class CiviCaseTestCase extends CiviUnitTestCase {
       // store for cleanup
       $this->optionValues[] = $activityTypes['id'];
     }
+
+    // TODO Our data seems inconsistent on whether name is "HousingSupport" or "housing_support"
+    $this->caseType = 'HousingSupport';
     $this->caseTypeId = 1;
     $this->tablesToTruncate = array(
       'civicrm_activity',
@@ -111,5 +128,15 @@ class CiviCaseTestCase extends CiviUnitTestCase {
   function tearDown() {
     $this->quickCleanup($this->tablesToTruncate, TRUE);
     $this->customDirectories(array('template_path' => FALSE));
+    CRM_Case_XMLRepository::singleton(TRUE);
+  }
+
+  /**
+   * Subclasses may override this if they want to be explicit about the case-type definition.
+   *
+   * @param $caseTypes
+   * @see CRM_Utils_Hook::caseTypes
+   */
+  function hook_caseTypes(&$caseTypes) {
   }
 }
\ No newline at end of file