Test tearDown fixes
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 20 Sep 2021 01:45:42 +0000 (13:45 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 20 Sep 2021 02:02:09 +0000 (14:02 +1200)
Fixes places where parent:tearDown not called and civicrm_file &
civicrm_entity_file not truncated

Also note - increasing use of fail() rather than allowing
exceptions to bubble up in test functions in order
to not let tearDown be such a distraction & require annotation

tests/phpunit/CRM/Activity/BAO/ActivityTest.php
tests/phpunit/CRM/Batch/BAO/BatchTest.php
tests/phpunit/CRM/Case/BAO/CaseTest.php
tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php
tests/phpunit/CRM/Contribute/Form/Contribution/ThankYouTest.php
tests/phpunit/CRM/Contribute/Form/Task/InvoiceTest.php
tests/phpunit/CRM/Core/BAO/CustomValueTableTest.php
tests/phpunit/CRM/Utils/Mail/EmailProcessorInboundTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php
tests/phpunit/api/v3/ContactTest.php

index f615e8d892edacffb0ee1c3fb58c8035a7155f85..ab631b0c1844fd2cfc6f948089587ab254b55e71 100644 (file)
@@ -40,6 +40,8 @@ class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase {
       'civicrm_uf_match',
       'civicrm_campaign',
       'civicrm_email',
+      'civicrm_file',
+      'civicrm_entity_file',
     ];
     $this->quickCleanup($tablesToTruncate);
     $this->cleanUpAfterACLs();
index c3fa203b0fd0b9500492c3d3171e83ae405d9e83..168c6ac3b3a03bfc1ebbf8cb87c8c356a523784e 100644 (file)
@@ -35,10 +35,11 @@ class CRM_Batch_BAO_BatchTest extends CiviUnitTestCase {
    * Cleanup after test.
    *
    * @throws \CRM_Core_Exception
+   * @throws \API_Exception
    */
   public function tearDown(): void {
-    parent::tearDown();
     $this->quickCleanup(['civicrm_batch']);
+    parent::tearDown();
   }
 
   /**
index a8e4c1d280e8ecd710439c3caec980ce4cf1c25d..251c2b6f613c0798b98791a06a3429fc45585296 100644 (file)
@@ -18,6 +18,8 @@ class CRM_Case_BAO_CaseTest extends CiviUnitTestCase {
       'civicrm_case_contact',
       'civicrm_case_activity',
       'civicrm_case_type',
+      'civicrm_file',
+      'civicrm_entity_file',
       'civicrm_activity_contact',
       'civicrm_managed',
       'civicrm_relationship',
@@ -37,7 +39,7 @@ class CRM_Case_BAO_CaseTest extends CiviUnitTestCase {
   /**
    * Make sure that the latest case activity works accurately.
    */
-  public function testCaseActivity() {
+  public function testCaseActivity(): void {
     $userID = $this->createLoggedInUser();
 
     $addTimeline = civicrm_api3('Case', 'addtimeline', [
@@ -57,8 +59,8 @@ class CRM_Case_BAO_CaseTest extends CiviUnitTestCase {
   }
 
   protected function tearDown(): void {
-    parent::tearDown();
     $this->quickCleanup($this->tablesToTruncate, TRUE);
+    parent::tearDown();
   }
 
   public function testAddCaseToContact() {
index 480e7a6ef8ba88f13caedb7c00a918db9fd1faff..04ddd1f0caa93982adc7334659fc55c83a73b7c6 100644 (file)
@@ -23,6 +23,7 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase {
    */
   public function tearDown(): void {
     $this->quickCleanUpFinancialEntities();
+    parent::tearDown();
   }
 
   /**
index 28c7d5fc7e41d857909b868987ca44c207db6cfa..895cc03947151bfdfb68b8e29369dcd1c7a35aec 100644 (file)
@@ -20,9 +20,12 @@ class CRM_Contribute_Form_Contribution_ThankYouTest extends CiviUnitTestCase {
 
   /**
    * Clean up DB.
+   *
+   * @throws \CRM_Core_Exception|\API_Exception
    */
   public function tearDown(): void {
     $this->quickCleanUpFinancialEntities();
+    parent::tearDown();
   }
 
   /**
index a66569312dc3486d894cf25ee408a07c86fc7826..50bd78df9a9e3ec1ef8d4309c2bfd0750e845e7a 100644 (file)
@@ -27,6 +27,7 @@ class CRM_Contribute_Form_Task_InvoiceTest extends CiviUnitTestCase {
     $this->quickCleanUpFinancialEntities();
     $this->revertTemplateToReservedTemplate('contribution_invoice_receipt');
     CRM_Utils_Hook::singleton()->reset();
+    parent::tearDown();
   }
 
   /**
index ba5bfedaa4028cbf03d7618f7639c214145e78e6..61b74ad1b97444a39efb76b28eeefaafd2abc575 100644 (file)
@@ -6,6 +6,11 @@
  */
 class CRM_Core_BAO_CustomValueTableTest extends CiviUnitTestCase {
 
+  public function tearDown(): void {
+    $this->quickCleanup(['civicrm_file', 'civicrm_entity_file'], TRUE);
+    parent::tearDown();
+  }
+
   /**
    * Test store function for country.
    */
@@ -35,10 +40,6 @@ class CRM_Core_BAO_CustomValueTableTest extends CiviUnitTestCase {
     ];
 
     CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
-
-    $this->customFieldDelete($customField['id']);
-    $this->customGroupDelete($customGroup['id']);
-    $this->contactDelete($contactID);
   }
 
   /**
@@ -70,16 +71,12 @@ class CRM_Core_BAO_CustomValueTableTest extends CiviUnitTestCase {
     ];
 
     CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
-
-    $this->customFieldDelete($customField['id']);
-    $this->customGroupDelete($customGroup['id']);
-    $this->contactDelete($contactID);
   }
 
   /**
    * Test store function for state province.
    */
-  public function testStoreStateProvince() {
+  public function testStoreStateProvince(): void {
     $contactID = $this->individualCreate();
     $customGroup = $this->customGroupCreate();
     $fields = [
@@ -104,16 +101,12 @@ class CRM_Core_BAO_CustomValueTableTest extends CiviUnitTestCase {
     ];
 
     CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
-
-    $this->customFieldDelete($customField['id']);
-    $this->customGroupDelete($customGroup['id']);
-    $this->contactDelete($contactID);
   }
 
   /**
    * Test store function for date.
    */
-  public function testStoreDate() {
+  public function testStoreDate(): void {
     $params = [];
     $contactID = $this->individualCreate();
     $customGroup = $this->customGroupCreate();
@@ -139,16 +132,12 @@ class CRM_Core_BAO_CustomValueTableTest extends CiviUnitTestCase {
     ];
 
     CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
-
-    $this->customFieldDelete($customField['id']);
-    $this->customGroupDelete($customGroup['id']);
-    $this->contactDelete($contactID);
   }
 
   /**
    * Test store function for rich text editor.
    */
-  public function testStoreRichTextEditor() {
+  public function testStoreRichTextEditor(): void {
     $params = [];
     $contactID = $this->individualCreate();
     $customGroup = $this->customGroupCreate();
@@ -173,16 +162,14 @@ class CRM_Core_BAO_CustomValueTableTest extends CiviUnitTestCase {
     ];
 
     CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
-
-    $this->customFieldDelete($customField['id']);
-    $this->customGroupDelete($customGroup['id']);
-    $this->contactDelete($contactID);
   }
 
   /**
    * Test store function for multiselect int.
+   *
+   * @throws \API_Exception
    */
-  public function testStoreMultiSelectInt() {
+  public function testStoreMultiSelectInt(): void {
     $contactID = $this->individualCreate();
     $customGroup = $this->customGroupCreate();
     $fields = [
@@ -219,17 +206,12 @@ class CRM_Core_BAO_CustomValueTableTest extends CiviUnitTestCase {
       ->addWhere('id', '=', $contactID)
       ->execute()->first();
     $this->assertEquals([1, 2], $customData['new_custom_group.Custom_Field']);
-
-    $this->customFieldDelete($customField['id']);
-    $this->customGroupDelete($customGroup['id']);
-    $this->contactDelete($contactID);
   }
 
   /**
    * Test getEntityValues function for stored value.
    */
-  public function testGetEntityValues() {
-
+  public function testGetEntityValues(): void {
     $params = [];
     $contactID = $this->individualCreate();
     $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
@@ -257,16 +239,12 @@ class CRM_Core_BAO_CustomValueTableTest extends CiviUnitTestCase {
 
     $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($contactID, 'Individual');
 
-    $this->assertEquals($entityValues[$customField['id']], '<p><strong>This is a <u>test</u></p>',
+    $this->assertEquals('<p><strong>This is a <u>test</u></p>', $entityValues[$customField['id']],
       'Checking same for returned value.'
     );
-    $this->customFieldDelete($customField['id']);
-    $this->customGroupDelete($customGroup['id']);
-    $this->contactDelete($contactID);
   }
 
-  public function testCustomGroupMultiple() {
-    $params = [];
+  public function testCustomGroupMultiple(): void {
     $contactID = $this->individualCreate();
     $customGroup = $this->customGroupCreate();
 
@@ -292,10 +270,6 @@ class CRM_Core_BAO_CustomValueTableTest extends CiviUnitTestCase {
 
     $this->assertEquals($params['custom_' . $customField['id'] . '_-1'], $result['custom_' . $customField['id']]);
     $this->assertEquals($params['entityID'], $result['entityID']);
-
-    $this->customFieldDelete($customField['id']);
-    $this->customGroupDelete($customGroup['id']);
-    $this->contactDelete($contactID);
   }
 
 }
index 44355f7656aa244aac284a304bc9c28e1e65ff27..f7f986e86368708d61e9625900b552fc2e42d22a 100644 (file)
@@ -35,6 +35,7 @@ class CRM_Utils_Mail_EmailProcessorInboundTest extends CiviUnitTestCase {
     $this->callAPISuccess('MailSettings', 'delete', [
       'id' => $this->mailSettingsId,
     ]);
+    $this->quickCleanup(['civicrm_file', 'civicrm_entity_file']);
     parent::tearDown();
   }
 
index 06580a382bd4c824d942ec51454bdf97c3cbfcc2..7931d92ee89bb68f7b186aae06c825b9ab8c0dd3 100644 (file)
@@ -516,7 +516,6 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
   /**
    *  Common teardown functions for all unit tests.
    *
-   * @throws \CRM_Core_Exception
    * @throws \API_Exception
    */
   protected function tearDown(): void {
@@ -1857,13 +1856,10 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
    *
    * @param array $tablesToTruncate
    * @param bool $dropCustomValueTables
-   *
-   * @throws \CRM_Core_Exception
-   * @throws \API_Exception
    */
-  public function quickCleanup($tablesToTruncate, $dropCustomValueTables = FALSE) {
+  public function quickCleanup(array $tablesToTruncate, $dropCustomValueTables = FALSE): void {
     if ($this->tx) {
-      throw new \CRM_Core_Exception("CiviUnitTestCase: quickCleanup() is not compatible with useTransaction()");
+      $this->fail('CiviUnitTestCase: quickCleanup() is not compatible with useTransaction()');
     }
     if ($dropCustomValueTables) {
       $this->cleanupCustomGroups();
@@ -1874,20 +1870,18 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
 
     $tablesToTruncate = array_unique(array_merge($this->_tablesToTruncate, $tablesToTruncate));
 
-    CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 0;");
+    CRM_Core_DAO::executeQuery('SET FOREIGN_KEY_CHECKS = 0;');
     foreach ($tablesToTruncate as $table) {
       $sql = "TRUNCATE TABLE $table";
       CRM_Core_DAO::executeQuery($sql);
     }
-    CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 1;");
+    CRM_Core_DAO::executeQuery('SET FOREIGN_KEY_CHECKS = 1;');
   }
 
   /**
    * Clean up financial entities after financial tests (so we remember to get all the tables :-))
-   *
-   * @throws \CRM_Core_Exception
    */
-  public function quickCleanUpFinancialEntities() {
+  public function quickCleanUpFinancialEntities(): void {
     $tablesToTruncate = [
       'civicrm_activity',
       'civicrm_activity_contact',
@@ -1923,9 +1917,19 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
     $this->restoreDefaultPriceSetConfig();
     $this->disableTaxAndInvoicing();
     $this->setCurrencySeparators(',');
-    FinancialType::delete(FALSE)->addWhere(
-      'name', 'NOT IN', ['Donation' , 'Member Dues', 'Campaign Contribution', 'Event Fee']
-    )->execute();
+    try {
+      FinancialType::delete(FALSE)->addWhere(
+        'name', 'NOT IN', [
+          'Donation',
+          'Member Dues',
+          'Campaign Contribution',
+          'Event Fee',
+        ]
+      )->execute();
+    }
+    catch (API_Exception $e) {
+      $this->fail('failed to cleanup financial types ' . $e->getMessage());
+    }
     CRM_Core_PseudoConstant::flush('taxRates');
     System::singleton()->flushProcessors();
     // @fixme this parameter is leaking - it should not be defined as a class static
@@ -3923,19 +3927,22 @@ WHERE a1.is_primary = 0
 
   /**
    * Delete any existing custom data groups.
-   *
-   * @throws \API_Exception
    */
   protected function cleanupCustomGroups(): void {
-    CustomField::get(FALSE)->setSelect(['option_group_id', 'custom_group_id'])
-      ->addChain('delete_options', OptionGroup::delete()
-        ->addWhere('id', '=', '$option_group_id')
-      )
-      ->addChain('delete_fields', CustomField::delete()
-        ->addWhere('id', '=', '$id')
-      )->execute();
+    try {
+      CustomField::get(FALSE)->setSelect(['option_group_id', 'custom_group_id'])
+        ->addChain('delete_options', OptionGroup::delete()
+          ->addWhere('id', '=', '$option_group_id')
+        )
+        ->addChain('delete_fields', CustomField::delete()
+          ->addWhere('id', '=', '$id')
+        )->execute();
 
-    CustomGroup::delete(FALSE)->addWhere('id', '>', 0)->execute();
+      CustomGroup::delete(FALSE)->addWhere('id', '>', 0)->execute();
+    }
+    catch (API_Exception $e) {
+      $this->fail('failed to cleanup custom groups ' . $e->getMessage());
+    }
   }
 
   /**
index 6eb0b086247ff0f5932463e130e0bac46ea36ee3..616616c09ac877a5fe0e7d0fbd0bb0687d08a87c 100644 (file)
@@ -90,6 +90,8 @@ class api_v3_ContactTest extends CiviUnitTestCase {
       'civicrm_website',
       'civicrm_relationship',
       'civicrm_uf_match',
+      'civicrm_file',
+      'civicrm_entity_file',
       'civicrm_phone',
       'civicrm_address',
       'civicrm_acl_contact_cache',