From: Eileen McNaughton Date: Mon, 20 Sep 2021 01:45:42 +0000 (+1200) Subject: Test tearDown fixes X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=d706c14907840de5a3a5ebe98cea1c361fce211e;p=civicrm-core.git Test tearDown fixes 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 --- diff --git a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php index f615e8d892..ab631b0c18 100644 --- a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php +++ b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php @@ -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(); diff --git a/tests/phpunit/CRM/Batch/BAO/BatchTest.php b/tests/phpunit/CRM/Batch/BAO/BatchTest.php index c3fa203b0f..168c6ac3b3 100644 --- a/tests/phpunit/CRM/Batch/BAO/BatchTest.php +++ b/tests/phpunit/CRM/Batch/BAO/BatchTest.php @@ -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(); } /** diff --git a/tests/phpunit/CRM/Case/BAO/CaseTest.php b/tests/phpunit/CRM/Case/BAO/CaseTest.php index a8e4c1d280..251c2b6f61 100644 --- a/tests/phpunit/CRM/Case/BAO/CaseTest.php +++ b/tests/phpunit/CRM/Case/BAO/CaseTest.php @@ -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() { diff --git a/tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php b/tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php index 480e7a6ef8..04ddd1f0ca 100644 --- a/tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php @@ -23,6 +23,7 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase { */ public function tearDown(): void { $this->quickCleanUpFinancialEntities(); + parent::tearDown(); } /** diff --git a/tests/phpunit/CRM/Contribute/Form/Contribution/ThankYouTest.php b/tests/phpunit/CRM/Contribute/Form/Contribution/ThankYouTest.php index 28c7d5fc7e..895cc03947 100644 --- a/tests/phpunit/CRM/Contribute/Form/Contribution/ThankYouTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Contribution/ThankYouTest.php @@ -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(); } /** diff --git a/tests/phpunit/CRM/Contribute/Form/Task/InvoiceTest.php b/tests/phpunit/CRM/Contribute/Form/Task/InvoiceTest.php index a66569312d..50bd78df9a 100644 --- a/tests/phpunit/CRM/Contribute/Form/Task/InvoiceTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Task/InvoiceTest.php @@ -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(); } /** diff --git a/tests/phpunit/CRM/Core/BAO/CustomValueTableTest.php b/tests/phpunit/CRM/Core/BAO/CustomValueTableTest.php index ba5bfedaa4..61b74ad1b9 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomValueTableTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomValueTableTest.php @@ -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']], '

This is a test

', + $this->assertEquals('

This is a test

', $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); } } diff --git a/tests/phpunit/CRM/Utils/Mail/EmailProcessorInboundTest.php b/tests/phpunit/CRM/Utils/Mail/EmailProcessorInboundTest.php index 44355f7656..f7f986e863 100644 --- a/tests/phpunit/CRM/Utils/Mail/EmailProcessorInboundTest.php +++ b/tests/phpunit/CRM/Utils/Mail/EmailProcessorInboundTest.php @@ -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(); } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 06580a382b..7931d92ee8 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -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()); + } } /** diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 6eb0b08624..616616c09a 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -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',