ContributionTest - Auto-disable all newly activated extensions
authorTim Otten <totten@civicrm.org>
Tue, 18 Oct 2022 06:34:18 +0000 (23:34 -0700)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 20 Oct 2022 01:56:30 +0000 (14:56 +1300)
tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php

index e9f184d57a59d8aedee1e87b037d0a853048770f..032aeb0cea46c198823abae3c5f919f75f74946e 100644 (file)
@@ -32,6 +32,26 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
    */
   protected $entity = 'Contribution';
 
+  /**
+   * These extensions are inactive at the start. They may be activated during the test. They should be deactivated at the end.
+   *
+   * For the moment, the test is simply hard-coded to cleanup in a specific order. It's tempting to auto-detect and auto-uninstall these.
+   * However, the shape of their dependencies makes it tricky to auto-uninstall (e.g. some exts have managed-entities that rely on other
+   * exts -- you need to fully disable+uninstall the downstream managed-entity-ext before disabling or uninstalling the upstream
+   * entity-provider-ext).
+   *
+   * You may need to edit `$toggleExts` whenever the dependency-graph changes.
+   *
+   * @var string[]
+   */
+  protected $toggleExts = ['civiimport', 'org.civicrm.search_kit', 'org.civicrm.afform', 'authx'];
+
+  protected function setUp(): void {
+    parent::setUp();
+    $origExtensions = array_column(CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'fullName');
+    $this->assertEquals([], array_intersect($origExtensions, $this->toggleExts), 'These extensions may be enabled and disabled during the test. The start-state and end-state should be the same. It appears that we have an unexpected start-state. Perhaps another test left us with a weird start-state?');
+  }
+
   /**
    * Cleanup function.
    *
@@ -44,7 +64,10 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
     DedupeRule::delete()
       ->addWhere('rule_table', '!=', 'civicrm_email')
       ->addWhere('dedupe_rule_group_id.name', '=', 'IndividualUnsupervised')->execute();
-    $this->callAPISuccess('Extension', 'disable', ['key' => 'civiimport']);
+    foreach ($this->toggleExts as $ext) {
+      CRM_Extension_System::singleton()->getManager()->disable([$ext]);
+      CRM_Extension_System::singleton()->getManager()->uninstall([$ext]);
+    }
     parent::tearDown();
   }