CRM_Case_ManagedEntities - Fix bugs caused by double-caching
authorcolemanw <coleman@civicrm.org>
Fri, 15 Sep 2023 21:32:22 +0000 (17:32 -0400)
committercolemanw <coleman@civicrm.org>
Fri, 15 Sep 2023 23:30:03 +0000 (19:30 -0400)
The list of case types was being cached twice, once by CRM_Core_Pseudoconstant
and yet again in CRM_Case_XMLRepository::$allCaseTypes.
This made getting a fresh list of case types unnecessarily difficult.

CRM/Case/XMLRepository.php
mixin/mgd-php@1/example/tests/mixin/ManagedCaseTypeTest.php

index 000ccdb52f651844ecb82136d7ad5a668c077e47..70fc9088b2ede6744411c89bc5c86dc18ca44c5c 100644 (file)
@@ -32,11 +32,11 @@ class CRM_Case_XMLRepository {
   protected $hookCache = NULL;
 
   /**
-   * Symbolic names of case-types.
+   * Override case types, only used by unit tests
    *
    * @var array|null
    */
-  protected $allCaseTypes = NULL;
+  protected $unitTestCaseTypes = NULL;
 
   /**
    * @param bool $fresh
@@ -52,18 +52,18 @@ class CRM_Case_XMLRepository {
   public function flush() {
     $this->xml = [];
     $this->hookCache = NULL;
-    $this->allCaseTypes = NULL;
+    $this->unitTestCaseTypes = NULL;
     CRM_Core_DAO::$_dbColumnValueCache = [];
   }
 
   /**
    * Class constructor.
    *
-   * @param array $allCaseTypes
+   * @param array $unitTestCaseTypes
    * @param array $xml
    */
-  public function __construct($allCaseTypes = NULL, $xml = []) {
-    $this->allCaseTypes = $allCaseTypes;
+  public function __construct($unitTestCaseTypes = NULL, $xml = []) {
+    $this->unitTestCaseTypes = $unitTestCaseTypes;
     $this->xml = $xml;
   }
 
@@ -215,13 +215,11 @@ class CRM_Case_XMLRepository {
   }
 
   /**
-   * @return array<string> symbolic names of case-types
+   * @return string[]
+   *   symbolic names of case-types
    */
   public function getAllCaseTypes() {
-    if ($this->allCaseTypes === NULL) {
-      $this->allCaseTypes = CRM_Case_PseudoConstant::caseType("name");
-    }
-    return $this->allCaseTypes;
+    return $this->unitTestCaseTypes ?? CRM_Case_PseudoConstant::caseType("name");
   }
 
   /**
index 097ce106b53b76c136f8852083bea7d8fd50f051..db7b74b81a1163a66f1d91e4e744042b10f425a9 100644 (file)
@@ -24,10 +24,6 @@ class ManagedCaseTypeTest extends \PHPUnit\Framework\Assert {
     $this->assertEquals(TRUE, $items[0]['is_active']);
     $this->assertEquals(1, count($items));
 
-    // This is normally handled by `CRM_Case_BAO_CaseType` calling `CRM_Core_ManagedEntities::scheduleReconciliation`
-    // But due to timing issues with the E2E tests the scheduled reconciliation hasn't happened yet.
-    \Civi\Api4\Managed::reconcile(FALSE)->addModule('civicrm')->execute();
-
     $actTypes = $cv->api4('OptionValue', 'get', [
       'where' => [['option_group_id:name', '=', 'activity_type'], ['name', '=', 'Nibble']],
     ]);
@@ -48,12 +44,6 @@ class ManagedCaseTypeTest extends \PHPUnit\Framework\Assert {
     $items = $cv->api4('CaseType', 'get', ['where' => [['name', '=', 'BunnyDance']]]);
     $this->assertEquals(0, count($items));
 
-    // This is normally handled by `CRM_Case_BAO_CaseType` calling `CRM_Core_ManagedEntities::scheduleReconciliation`
-    // But static caching seems to interfere.
-    \Civi\Api4\Managed::reconcile(FALSE)->execute();
-    \CRM_Core_PseudoConstant::flush();
-    \Civi\Api4\Managed::reconcile(FALSE)->addModule('civicrm')->execute();
-
     $actTypes = $cv->api4('OptionValue', 'get', [
       'where' => [['option_group_id:name', '=', 'activity_type'], ['name', '=', 'Nibble']],
     ]);