Fix unit tests to enable components before using them
authorColeman Watts <coleman@civicrm.org>
Fri, 26 Nov 2021 21:48:57 +0000 (16:48 -0500)
committerColeman Watts <coleman@civicrm.org>
Mon, 29 Nov 2021 13:46:02 +0000 (08:46 -0500)
CRM/Core/BAO/ConfigSetting.php
tests/phpunit/CRM/Batch/Form/EntryTest.php
tests/phpunit/api/v4/Entity/ConformanceTest.php

index 972ad1260dfb9d3c5fe477f6f1d88e1a4e985a1c..44b71780621b6192a5da56e572838547094ce1ef 100644 (file)
@@ -309,8 +309,8 @@ class CRM_Core_BAO_ConfigSetting {
    *   true if valid component name and enabling succeeds, else false
    */
   public static function enableComponent($componentName) {
-    $config = CRM_Core_Config::singleton();
-    if (in_array($componentName, $config->enableComponents)) {
+    $enabledComponents = Civi::settings()->get('enable_components');
+    if (in_array($componentName, $enabledComponents)) {
       // component is already enabled
       return TRUE;
     }
@@ -321,14 +321,23 @@ class CRM_Core_BAO_ConfigSetting {
     }
 
     // get enabled-components from DB and add to the list
-    $enabledComponents = Civi::settings()->get('enable_components');
     $enabledComponents[] = $componentName;
-
     self::setEnabledComponents($enabledComponents);
 
     return TRUE;
   }
 
+  /**
+   * Ensure all components are enabled
+   * @throws CRM_Core_Exception
+   */
+  public static function enableAllComponents() {
+    $allComponents = array_keys(CRM_Core_Component::getComponents());
+    if (Civi::settings()->get('enable_components') != $allComponents) {
+      self::setEnabledComponents($allComponents);
+    }
+  }
+
   /**
    * Disable specified component.
    *
index 67a04ddb12670acd159df1c68e1656e7881895f3..ba9bc9e80ec5e8281e61cf69c3b56d2daece8499 100644 (file)
@@ -88,6 +88,8 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
    */
   public function setUp(): void {
     parent::setUp();
+    \CRM_Core_BAO_ConfigSetting::enableComponent('CiviMember');
+    \CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
 
     $params = [
       'contact_type_a' => 'Individual',
index 7060fbb7ba3816178badfbded0f593fd1c533aba..85a069a180cf20e2106d9f4dfd47f05c03df2b89 100644 (file)
@@ -53,6 +53,8 @@ class ConformanceTest extends UnitTestCase implements HookInterface {
    * Set up baseline for testing
    */
   public function setUp(): void {
+    // Enable all components
+    \CRM_Core_BAO_ConfigSetting::enableAllComponents();
     $this->setUpOptionCleanup();
     $this->loadDataSet('CaseType');
     $this->loadDataSet('ConformanceTest');