From 009dcce336c66078b1607775f46b788ac6d2364f Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 2 Mar 2023 13:06:17 +1300 Subject: [PATCH] [NFC] Superficial cleanup in test class --- tests/phpunit/api/v3/ContributionTest.php | 144 +++++++++------------- 1 file changed, 61 insertions(+), 83 deletions(-) diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index f41051ef5f1d..a6d9dab77177 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -34,7 +34,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase { protected $individualID; protected $_contribution; - protected $_financialTypeId = 1; + protected $financialTypeID = 1; protected $entity = 'Contribution'; protected $_params; protected $_ids = []; @@ -66,13 +66,6 @@ class api_v3_ContributionTest extends CiviUnitTestCase { */ protected $mut; - /** - * Should financials be checked after the test but before tear down. - * - * @var bool - */ - protected $isValidateFinancialsOnPostAssert = TRUE; - /** * Setup function. */ @@ -85,7 +78,7 @@ public function setUp(): void { 'contact_id' => $this->individualID, 'receive_date' => '20120511', 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'non_deductible_amount' => 10.00, 'fee_amount' => 5.00, 'net_amount' => 95.00, @@ -148,7 +141,7 @@ public function testGetContribution(): void { 'contact_id' => $this->individualID, 'receive_date' => '2010-01-20', 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'non_deductible_amount' => 10.00, 'fee_amount' => 5.00, 'net_amount' => 95.00, @@ -228,8 +221,6 @@ public function testGetTestContribution(): void { /** * Test Creating a check contribution with original check_number field - * - * @throws \CRM_Core_Exception */ public function testCreateCheckContribution(): void { $params = $this->_params; @@ -359,8 +350,6 @@ public function testGetContributionReturnFunctionality(): void { /** * Test cancel reason works as a filter. - * - * @throws \CRM_Core_Exception */ public function testFilterCancelReason(): void { $params = $this->_params; @@ -376,15 +365,13 @@ public function testFilterCancelReason(): void { /** * We need to ensure previous tested api contract behaviour still works. - * - * @throws \CRM_Core_Exception */ public function testGetContributionLegacyBehaviour(): void { $p = [ 'contact_id' => $this->individualID, 'receive_date' => '2010-01-20', 'total_amount' => 100.00, - 'contribution_type_id' => $this->_financialTypeId, + 'contribution_type_id' => $this->financialTypeID, 'non_deductible_amount' => 10.00, 'fee_amount' => 5.00, 'net_amount' => 95.00, @@ -402,49 +389,49 @@ public function testGetContributionLegacyBehaviour(): void { $params['return'][] = 'financial_type_id'; $params['return'][] = 'contribution_source'; $contribution = $this->callAPISuccess('Contribution', 'get', $params); - $this->assertEquals(1, $contribution['count']); - $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->individualID); - $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId); - $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId); - $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); - $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00); - $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00); - $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00); - $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456); - $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910); - $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF'); - $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed'); + $contribution = $contribution['values'][$contribution['id']]; + $this->assertEquals($contribution['contact_id'], $this->individualID); + $this->assertEquals($this->financialTypeID, $contribution['financial_type_id']); + $this->assertEquals($this->financialTypeID, $contribution['contribution_type_id']); + $this->assertEquals(100.00, $contribution['total_amount']); + $this->assertEquals(10.00, $contribution['non_deductible_amount']); + $this->assertEquals(5.00, $contribution['fee_amount']); + $this->assertEquals(95.00, $contribution['net_amount']); + $this->assertEquals(23456, $contribution['trxn_id']); + $this->assertEquals(78910, $contribution['invoice_id']); + $this->assertEquals('SSF', $contribution['contribution_source']); + $this->assertEquals('Completed', $contribution['contribution_status']); // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id). $p['trxn_id'] = '3847'; $p['invoice_id'] = '3847'; - $contribution2 = $this->callAPISuccess('contribution', 'create', $p); + $contribution2 = $this->callAPISuccess('Contribution', 'create', $p); // now we have 2 - test getcount - $contribution = $this->callAPISuccess('contribution', 'getcount', []); + $contribution = $this->callAPISuccess('Contribution', 'getcount', []); $this->assertEquals(2, $contribution); //test id only format - $contribution = $this->callAPISuccess('contribution', 'get', [ + $contribution = $this->callAPISuccess('Contribution', 'get', [ 'id' => $this->_contribution['id'], 'format.only_id' => 1, ]); $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE)); //test id only format - $contribution = $this->callAPISuccess('contribution', 'get', [ + $contribution = $this->callAPISuccess('Contribution', 'get', [ 'id' => $contribution2['id'], 'format.only_id' => 1, ]); $this->assertEquals($contribution2['id'], $contribution); - $contribution = $this->callAPISuccess('contribution', 'get', [ + $contribution = $this->callAPISuccess('Contribution', 'get', [ 'id' => $this->_contribution['id'], ]); //test id as field $this->assertEquals(1, $contribution['count']); // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ; //test get by contact id works - $contribution = $this->callAPISuccess('contribution', 'get', ['contact_id' => $this->individualID]); + $contribution = $this->callAPISuccess('Contribution', 'get', ['contact_id' => $this->individualID]); $this->assertEquals(2, $contribution['count']); $this->callAPISuccess('Contribution', 'Delete', [ @@ -458,7 +445,7 @@ public function testGetContributionLegacyBehaviour(): void { /** * Create an contribution_id=FALSE and financial_type_id=Donation. */ - public function testCreateEmptyContributionIDUseDonation() { + public function testCreateEmptyContributionIDUseDonation(): void { $params = [ 'contribution_id' => FALSE, 'contact_id' => 1, @@ -527,7 +514,7 @@ public function testCreateContributionNoLineItems(): void { 'contact_id' => $this->individualID, 'receive_date' => '20120511', 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'payment_instrument_id' => 1, 'non_deductible_amount' => 10.00, 'fee_amount' => 50.00, @@ -561,7 +548,7 @@ public function testCreateContributionChainedLineItems(): void { 'contact_id' => $this->individualID, 'receive_date' => '20120511', 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'payment_instrument_id' => 1, 'non_deductible_amount' => 10.00, 'fee_amount' => 50.00, @@ -778,7 +765,7 @@ public function testCreateLocalizedContribution($totalAmount, string $decimalPoi 'contact_id' => $this->individualID, 'receive_date' => '20120511', 'total_amount' => $totalAmount, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'contribution_status_id' => 1, ]; @@ -867,7 +854,7 @@ public function testCreateContributionSource() { 'contact_id' => $this->individualID, 'receive_date' => date('Ymd'), 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'payment_instrument_id' => 1, 'non_deductible_amount' => 10.00, 'fee_amount' => 50.00, @@ -934,7 +921,7 @@ public function testCreateContributionSourceInvalidContact() { 'contact_id' => 999, 'receive_date' => date('Ymd'), 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'payment_instrument_id' => 1, 'non_deductible_amount' => 10.00, 'fee_amount' => 50.00, @@ -960,7 +947,7 @@ public function testCreateContributionWithNote(): void { 'contact_id' => $this->individualID, 'receive_date' => '2012-01-01', 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'payment_instrument_id' => 1, 'non_deductible_amount' => 10.00, 'fee_amount' => 50.00, @@ -991,7 +978,7 @@ public function testCreateContributionWithNoteUniqueNameAliases(): void { 'contact_id' => $this->individualID, 'receive_date' => '2012-01-01', 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'payment_instrument_id' => 1, 'non_deductible_amount' => 10.00, 'fee_amount' => 50.00, @@ -1365,7 +1352,7 @@ public function testCreateUpdateContributionPayLater() { 'contact_id' => $this->individualID, 'receive_date' => '2012-01-01', 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'payment_instrument_id' => 1, 'contribution_status_id' => 2, 'is_pay_later' => 1, @@ -1392,7 +1379,7 @@ public function testCreateUpdateContributionPaymentInstrument(): void { $contributionParams = [ 'contact_id' => $this->individualID, 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'payment_instrument_id' => 4, 'contribution_status_id' => 1, @@ -1419,7 +1406,7 @@ public function testCreateUpdateNegativeContributionPaymentInstrument() { $contributionParams = [ 'contact_id' => $this->individualID, 'total_amount' => -100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'payment_instrument_id' => 4, 'contribution_status_id' => 1, @@ -1448,7 +1435,7 @@ public function testCreateUpdateContributionRefund() { 'contact_id' => $this->individualID, 'receive_date' => '2012-01-01', 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'payment_instrument_id' => 4, 'contribution_status_id' => 1, 'trxn_id' => 'original_payment', @@ -1515,7 +1502,7 @@ public function testCreateUpdateChargebackContributionCustomAccount() { ]); $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', [ - 'entity_id' => $this->_financialTypeId, + 'entity_id' => $this->financialTypeID, 'entity_table' => 'civicrm_financial_type', 'account_relationship' => 'Chargeback Account is', 'financial_account_id' => 'Chargeback Account', @@ -1553,7 +1540,7 @@ public function testCreateUpdateRefundContributionConfiguredContraAccount() { ]); $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', [ - 'entity_id' => $this->_financialTypeId, + 'entity_id' => $this->financialTypeID, 'entity_table' => 'civicrm_financial_type', 'account_relationship' => 'Credit/Contra Revenue Account is', 'financial_account_id' => 'Refund Account', @@ -1587,7 +1574,7 @@ public function testCreateUpdateContributionRefundTrxnIDPassedIn() { 'contact_id' => $this->individualID, 'receive_date' => '2012-01-01', 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'payment_instrument_id' => 4, 'contribution_status_id' => 1, 'trxn_id' => 'original_payment', @@ -1621,7 +1608,7 @@ public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() { 'contact_id' => $this->individualID, 'receive_date' => '2012-01-01', 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'payment_instrument_id' => 4, 'contribution_status_id' => 1, 'trxn_id' => 'original_payment', @@ -1656,7 +1643,7 @@ public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() { 'contact_id' => $this->individualID, 'receive_date' => '2012-01-01', 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'payment_instrument_id' => 4, 'contribution_status_id' => 1, 'trxn_id' => 'original_payment', @@ -1710,7 +1697,7 @@ public function testCreateUpdateContributionCancelPending() { 'contact_id' => $this->individualID, 'receive_date' => '2012-01-01', 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'payment_instrument_id' => 1, 'contribution_status_id' => 2, 'is_pay_later' => 1, @@ -1804,7 +1791,7 @@ public function testCreateUpdateContribution() { $contributionID = $this->contributionCreate([ 'contact_id' => $this->individualID, 'trxn_id' => 212355, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'invoice_id' => 'old_invoice', ]); $old_params = [ @@ -1834,7 +1821,7 @@ public function testCreateUpdateContribution() { 'contact_id' => $this->individualID, 'total_amount' => 105.00, 'fee_amount' => 7.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'non_deductible_amount' => 22.00, 'contribution_status_id' => 1, 'note' => 'Donating for Noble Cause', @@ -1849,7 +1836,7 @@ public function testCreateUpdateContribution() { $this->assertEquals($contribution['contact_id'], $this->individualID); $this->assertEquals($contribution['total_amount'], 105.00); - $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId); + $this->assertEquals($contribution['financial_type_id'], $this->financialTypeID); $this->assertEquals($contribution['financial_type'], 'Donation'); $this->assertEquals($contribution['instrument_id'], $old_payment_instrument); $this->assertEquals($contribution['non_deductible_amount'], 22.00); @@ -1945,7 +1932,7 @@ public function testDeleteWrongParamContribution(): void { public function testDeleteContribution(): void { $contributionID = $this->contributionCreate([ 'contact_id' => $this->individualID, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, ]); $params = [ 'id' => $contributionID, @@ -1965,7 +1952,7 @@ public function testSearchEmptyParams(): void { 'contact_id' => $this->individualID, 'receive_date' => date('Ymd'), 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'non_deductible_amount' => 10.00, 'fee_amount' => 5.00, 'net_amount' => 95.00, @@ -2003,7 +1990,7 @@ public function testSearch() { 'contact_id' => $this->individualID, 'receive_date' => date('Ymd'), 'total_amount' => 100.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'non_deductible_amount' => 10.00, 'contribution_status_id' => 1, ]; @@ -2013,7 +2000,7 @@ public function testSearch() { 'contact_id' => $this->individualID, 'receive_date' => date('Ymd'), 'total_amount' => 200.00, - 'financial_type_id' => $this->_financialTypeId, + 'financial_type_id' => $this->financialTypeID, 'non_deductible_amount' => 20.00, 'trxn_id' => 5454565, 'invoice_id' => 1212124, @@ -4682,10 +4669,10 @@ public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFro * @param array $params * Params to overwrite with. * - * @return array|int + * @return array */ - protected function createReceiptableContributionPage($params = []) { - $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge([ + protected function createReceiptableContributionPage(array $params = []): array { + return $this->callAPISuccess('ContributionPage', 'create', array_merge([ 'receipt_from_name' => 'Mickey Mouse', 'receipt_from_email' => 'mickey@mouse.com', 'title' => 'Test Contribution Page', @@ -4694,15 +4681,12 @@ protected function createReceiptableContributionPage($params = []) { 'is_monetary' => TRUE, 'is_email_receipt' => TRUE, ], $params)); - return $contributionPage; } /** * function to test card_type and pan truncation. - * - * @throws \CRM_Core_Exception */ - public function testCardTypeAndPanTruncation() { + public function testCardTypeAndPanTruncation(): void { $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id')); $contactId = $this->individualCreate(); $params = [ @@ -4723,14 +4707,14 @@ public function testCardTypeAndPanTruncation() { 'return' => ['card_type_id', 'pan_truncation'], ] ); - $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']); - $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 4567); + $this->assertEquals($creditCardTypeIDs['Visa'], $financialTrxn['card_type_id']); + $this->assertEquals(4567, $financialTrxn['pan_truncation']); $params = [ 'id' => $contribution['id'], 'pan_truncation' => 2345, 'card_type_id' => $creditCardTypeIDs['Amex'], ]; - $this->callAPISuccess('contribution', 'create', $params); + $this->callAPISuccess('Contribution', 'create', $params); $financialTrxn = $this->callAPISuccessGetSingle( 'FinancialTrxn', [ @@ -4738,17 +4722,16 @@ public function testCardTypeAndPanTruncation() { 'return' => ['card_type_id', 'pan_truncation'], ] ); - $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']); - $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 2345); + $this->assertEquals($creditCardTypeIDs['Amex'], $financialTrxn['card_type_id']); + $this->assertEquals(2345, $financialTrxn['pan_truncation']); } /** * Test repeat contribution uses non default currency * * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678 - * @throws \CRM_Core_Exception */ - public function testRepeatTransactionWithDifferenceCurrency() { + public function testRepeatTransactionWithDifferenceCurrency(): void { $originalContribution = $this->setUpRepeatTransaction(['currency' => 'AUD'], 'single', ['currency' => 'AUD']); $contribution = $this->callAPISuccess('Contribution', 'repeattransaction', [ 'original_contribution_id' => $originalContribution['id'], @@ -4766,7 +4749,7 @@ public function testRepeatTransactionWithDifferenceCurrency() { * @return array * Array of associated financial items. */ - protected function getFinancialTransactionsForContribution($contributionID) { + protected function getFinancialTransactionsForContribution(int $contributionID): array { $trxnParams = [ 'entity_id' => $contributionID, 'entity_table' => 'civicrm_contribution', @@ -4777,7 +4760,7 @@ protected function getFinancialTransactionsForContribution($contributionID) { /** * Test getunique api call for Contribution entity */ - public function testContributionGetUnique() { + public function testContributionGetUnique(): void { $result = $this->callAPIAndDocument($this->entity, 'getunique', [], __FUNCTION__, __FILE__); $this->assertEquals(2, $result['count']); $this->assertEquals(['trxn_id'], $result['values']['UI_contrib_trxn_id']); @@ -4841,10 +4824,9 @@ public function testGetCurrencyOptions() { } /** - * @throws \CRM_Core_Exception - * @throws \Civi\API\Exception\UnauthorizedException + * Test setting custom data in the create hook. */ - public function testSetCustomDataInCreateAndHook() { + public function testSetCustomDataInCreateAndHook(): void { $this->createCustomGroupWithFieldOfType([], 'int'); $this->ids['CustomField']['text'] = (int) $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup']['Custom Group']])['id']; $this->hookClass->setHook('civicrm_post', [ @@ -4888,8 +4870,6 @@ public function civicrmPostContributionCustom(string $op, string $objectName, ?i * works * * @see dev/core#1816 - * - * @throws \CRM_Core_Exception */ public function testCustomValueOptionLabelTest(): void { $this->createCustomGroupWithFieldOfType([], 'radio'); @@ -4904,8 +4884,6 @@ public function testCustomValueOptionLabelTest(): void { * @dataProvider getRepeatTransactionNextSchedData * * @param array $dataSet - * - * @throws \CRM_Core_Exception */ public function testRepeatTransactionUpdateNextSchedContributionDate(array $dataSet): void { $paymentProcessorID = $this->paymentProcessorCreate();