From 62ec89b0b24fee8f5cfd0fa696aa472c7bc15001 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 9 Apr 2021 10:19:55 +1200 Subject: [PATCH] [NFC] [Test] Minor preliminary cleanup --- tests/phpunit/api/v3/PaymentTest.php | 135 +++++++++++++++------------ 1 file changed, 75 insertions(+), 60 deletions(-) diff --git a/tests/phpunit/api/v3/PaymentTest.php b/tests/phpunit/api/v3/PaymentTest.php index e319f50407..4d1cff33bb 100644 --- a/tests/phpunit/api/v3/PaymentTest.php +++ b/tests/phpunit/api/v3/PaymentTest.php @@ -25,7 +25,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { /** * Setup function. * - * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ public function setUp(): void { parent::setUp(); @@ -52,7 +52,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testGetPayment() { + public function testGetPayment(): void { $p = [ 'contact_id' => $this->_individualId, 'receive_date' => '2010-01-20', @@ -70,7 +70,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'administer CiviCRM']; $this->callAPIFailure('payment', 'get', $params, 'API permission check failed for Payment/get call; insufficient permission: require access CiviCRM and access CiviContribute'); - array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviContribute'); + CRM_Core_Config::singleton()->userPermissionClass->permissions[] = 'access CiviContribute'; $this->callAPISuccess('payment', 'get', $params); $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__); @@ -98,7 +98,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testMultiplePaymentsForContribution() { + public function testMultiplePaymentsForContribution(): void { $params = [ 'contact_id' => $this->_individualId, 'total_amount' => 100, @@ -141,9 +141,10 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * Retrieve Payment using trxn_id. * * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ - public function testGetPaymentWithTrxnID() { - $this->_individualId2 = $this->individualCreate(); + public function testGetPaymentWithTrxnID(): void { + $individual2 = $this->individualCreate(); $params1 = [ 'contact_id' => $this->_individualId, 'trxn_id' => 111111, @@ -152,7 +153,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { $contributionID1 = $this->contributionCreate($params1); $params2 = [ - 'contact_id' => $this->_individualId2, + 'contact_id' => $individual2, 'trxn_id' => 222222, 'total_amount' => 20, ]; @@ -192,7 +193,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testPaymentSendContributionReceipt() { + public function testPaymentSendContributionReceipt(): void { $mut = new CiviMailUtils($this); $contribution = $this->createPartiallyPaidParticipantOrder(); $event = $this->callAPISuccess('Event', 'get', []); @@ -216,8 +217,10 @@ class api_v3_PaymentTest extends CiviUnitTestCase { /** * Test full refund when no payment has actually been record. + * + * @throws \CRM_Core_Exception */ - public function testFullRefundWithPaymentAlreadyRefunded() { + public function testFullRefundWithPaymentAlreadyRefunded(): void { $params1 = [ 'contact_id' => $this->_individualId, 'trxn_id' => 111111, @@ -226,13 +229,16 @@ class api_v3_PaymentTest extends CiviUnitTestCase { $contributionID1 = $this->contributionCreate($params1); $paymentParams = ['contribution_id' => $contributionID1]; $this->callAPISuccess('Payment', 'create', ['total_amount' => '-10', 'contribution_id' => $contributionID1]); - $payment = $this->callAPISuccess('payment', 'get', $paymentParams); + $this->callAPISuccess('payment', 'get', $paymentParams); $this->callAPISuccess('Payment', 'create', ['total_amount' => '-10', 'contribution_id' => $contributionID1]); - $payment = $this->callAPISuccess('payment', 'get', $paymentParams); + $this->callAPISuccess('payment', 'get', $paymentParams); $this->validateAllPayments(); } - public function testNegativePaymentWithNegativeContribution() { + /** + * @throws \CRM_Core_Exception + */ + public function testNegativePaymentWithNegativeContribution(): void { $params1 = [ 'contact_id' => $this->_individualId, 'trxn_id' => 111111, @@ -241,7 +247,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { $contributionID1 = $this->contributionCreate($params1); $this->callAPISuccess('Payment', 'create', ['total_amount' => '-20', 'contribution_id' => $contributionID1]); $paymentParams = ['contribution_id' => $contributionID1]; - $payment = $this->callAPISuccess('payment', 'get', $paymentParams); + $this->callAPISuccess('payment', 'get', $paymentParams); $this->validateAllPayments(); } @@ -250,7 +256,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testPaymentEmailReceipt() { + public function testPaymentEmailReceipt(): void { $mut = new CiviMailUtils($this); $contribution = $this->createPartiallyPaidParticipantOrder(); $event = $this->callAPISuccess('Event', 'get', []); @@ -296,7 +302,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testPaymentEmailReceiptFullyPaid() { + public function testPaymentEmailReceiptFullyPaid(): void { $mut = new CiviMailUtils($this); CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviContribute', 'edit contributions', 'access CiviCRM']; $contribution = $this->createPartiallyPaidParticipantOrder(); @@ -332,7 +338,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testRefundEmailReceipt($thousandSeparator) { + public function testRefundEmailReceipt(string $thousandSeparator): void { $this->setCurrencySeparators($thousandSeparator); $decimalSeparator = ($thousandSeparator === ',' ? '.' : ','); $mut = new CiviMailUtils($this); @@ -382,7 +388,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testCreatePaymentPendingOrderNoLineItems() { + public function testCreatePaymentPendingOrderNoLineItems(): void { $order = $this->createPendingParticipantOrder(); $this->callAPISuccess('Payment', 'create', [ 'order_id' => $order['id'], @@ -401,8 +407,9 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * that in an artificial way. * * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ - public function testAddPaymentMissingFinancialItems() { + public function testAddPaymentMissingFinancialItems(): void { $contribution = $this->callAPISuccess('Contribution', 'create', [ 'total_amount' => 50, 'financial_type_id' => 'Donation', @@ -421,7 +428,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - protected function createPendingParticipantOrder() { + protected function createPendingParticipantOrder(): array { return $this->callAPISuccess('Order', 'create', $this->getParticipantOrderParams()); } @@ -430,7 +437,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testCreatePaymentNoLineItems() { + public function testCreatePaymentNoLineItems(): void { $contribution = $this->createPartiallyPaidParticipantOrder(); //Create partial payment @@ -479,14 +486,14 @@ class api_v3_PaymentTest extends CiviUnitTestCase { // Check contribution for completed status $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]); - $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed'); - $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00); + $this->assertEquals('Completed', $contribution['values'][$contribution['id']]['contribution_status']); + $this->assertEquals(300.00, $contribution['values'][$contribution['id']]['total_amount']); $paymentParticipant = [ 'contribution_id' => $contribution['id'], ]; $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant); $participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]); - $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered'); + $this->assertEquals('Registered', $participant['values'][$participant['id']]['participant_status']); $this->callAPISuccess('Contribution', 'Delete', [ 'id' => $contribution['id'], ]); @@ -496,9 +503,12 @@ class api_v3_PaymentTest extends CiviUnitTestCase { /** * Function to assert db values * + * @param array $payment + * @param array $expectedResult + * * @throws \CRM_Core_Exception */ - public function checkPaymentResult($payment, $expectedResult) { + public function checkPaymentResult(array $payment, array $expectedResult): void { $refreshedPayment = $this->callAPISuccessGetSingle('Payment', ['financial_trxn_id' => $payment['id']]); foreach ($expectedResult[$payment['id']] as $key => $value) { $this->assertEquals($refreshedPayment[$key], $value, 'mismatch on ' . $key); $this->assertEquals($refreshedPayment[$key], $value, 'mismatch on ' . $key); @@ -510,7 +520,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testCreatePaymentLineItems() { + public function testCreatePaymentLineItems(): void { $contribution = $this->createPartiallyPaidParticipantOrder(); $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $contribution['id']])['values']; @@ -571,14 +581,14 @@ class api_v3_PaymentTest extends CiviUnitTestCase { // Check contribution for completed status $contribution = $this->callAPISuccess('Contribution', 'get', ['id' => $contribution['id']]); - $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed'); - $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 300.00); + $this->assertEquals('Completed', $contribution['values'][$contribution['id']]['contribution_status']); + $this->assertEquals(300.00, $contribution['values'][$contribution['id']]['total_amount']); $paymentParticipant = [ 'contribution_id' => $contribution['id'], ]; $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant); $participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]); - $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Registered'); + $this->assertEquals('Registered', $participant['values'][$participant['id']]['participant_status']); $this->validateAllPayments(); } @@ -587,7 +597,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testRefundPayment() { + public function testRefundPayment(): void { $result = $this->callAPISuccess('Contribution', 'create', [ 'financial_type_id' => "Donation", 'total_amount' => 100, @@ -606,7 +616,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { 'id' => $contributionID, ]); //Still we've a status of Completed after refunding a partial amount. - $this->assertEquals($contribution['contribution_status'], 'Completed'); + $this->assertEquals('Completed', $contribution['contribution_status']); //Refund the complete amount. $this->callAPISuccess('Payment', 'create', [ @@ -618,7 +628,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { 'id' => $contributionID, ]); //Assert if main contribution status is updated to "Refunded". - $this->assertEquals($contribution['contribution_status'], 'Refunded Label**'); + $this->assertEquals('Refunded Label**', $contribution['contribution_status']); } /** @@ -626,7 +636,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testRefundPaymentWithCancelledPaymentId() { + public function testRefundPaymentWithCancelledPaymentId(): void { $result = $this->callAPISuccess('Contribution', 'create', [ 'financial_type_id' => "Donation", 'total_amount' => 100, @@ -645,7 +655,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { 'id' => $contributionID, ]); //Assert if main contribution status is updated to "Refunded". - $this->assertEquals($contribution['contribution_status'], 'Refunded Label**'); + $this->assertEquals('Refunded Label**', $contribution['contribution_status']); } /** @@ -653,7 +663,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testCancelPayment() { + public function testCancelPayment(): void { CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute']; $contribution = $this->createPartiallyPaidParticipantOrder(); @@ -692,7 +702,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testDeletePayment() { + public function testDeletePayment(): void { CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute']; $contribution = $this->createPartiallyPaidParticipantOrder(); @@ -741,7 +751,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testUpdatePayment() { + public function testUpdatePayment(): void { CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute', 'edit contributions']; $contribution = $this->createPartiallyPaidParticipantOrder(); @@ -850,8 +860,9 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * Test that a contribution can be overpaid with the payment api. * * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ - public function testCreatePaymentOverPay() { + public function testCreatePaymentOverPay(): void { $contributionID = $this->contributionCreate(['contact_id' => $this->individualCreate()]); $payment = $this->callAPISuccess('Payment', 'create', ['total_amount' => 5, 'order_id' => $contributionID]); $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contributionID]); @@ -866,7 +877,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testCreatePaymentPayLater() { + public function testCreatePaymentPayLater(): void { $this->createLoggedInUser(); $processorID = $this->paymentProcessorCreate(); $contributionParams = [ @@ -911,17 +922,17 @@ class api_v3_PaymentTest extends CiviUnitTestCase { 'financial_trxn_id' => $payment['id'], ]; $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); - $this->assertEquals($eft['values'][$eft['id']]['amount'], 100); + $this->assertEquals(100, $eft['values'][$eft['id']]['amount']); $params = [ 'entity_table' => 'civicrm_financial_item', 'financial_trxn_id' => $payment['id'], ]; $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); - $this->assertEquals($eft['values'][$eft['id']]['amount'], 100); + $this->assertEquals(100, $eft['values'][$eft['id']]['amount']); // Check contribution for completed status $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]); - $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed'); - $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); + $this->assertEquals('Completed', $contribution['values'][$contribution['id']]['contribution_status']); + $this->assertEquals(100.00, $contribution['values'][$contribution['id']]['total_amount']); $this->callAPISuccess('Contribution', 'Delete', [ 'id' => $contribution['id'], ]); @@ -933,7 +944,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testNetAmount() { + public function testNetAmount(): void { $order = $this->createPendingParticipantOrder(); $payment = $this->callAPISuccess('Payment', 'create', ['order_id' => $order['id'], 'total_amount' => 10, 'fee_amount' => .25]); $this->assertEquals('9.75', $this->callAPISuccessGetValue('Payment', ['id' => $payment['id'], 'return' => 'net_amount'])); @@ -945,7 +956,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * https://lab.civicrm.org/dev/financial/issues/69 * @throws \CRM_Core_Exception */ - public function testCreatePaymentIncompletePaymentPartialPayment() { + public function testCreatePaymentIncompletePaymentPartialPayment(): void { $contributionParams = [ 'total_amount' => 100, 'currency' => 'USD', @@ -983,7 +994,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testCreatePaymentOnFailedContribution() { + public function testCreatePaymentOnFailedContribution(): void { $this->createLoggedInUser(); //Create a direct Failed Contribution (no ft record inserted). $contributionParams = [ @@ -1000,7 +1011,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { 'contribution_id' => $contribution['id'], 'total_amount' => 50, ]; - $payment = $this->callAPISuccess('Payment', 'create', $params); + $this->callAPISuccess('Payment', 'create', $params); //Verify 2 rows are added to the financial trxn as payment is moved from //Failed -> Pending -> Completed, i.e, 0 -> 7(Account receivable) -> 6 (Deposit Bank). @@ -1009,7 +1020,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { 'entity_table' => 'civicrm_contribution', ]; $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); - $this->assertEquals($eft['count'], 2); + $this->assertEquals(2, $eft['count']); //Test 2 //Create a Pending Contribution so an FT record is inserted. @@ -1055,7 +1066,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { 'entity_table' => 'civicrm_contribution', ]; $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); - $this->assertEquals($eft['count'], 4); + $this->assertEquals(4, $eft['count']); $this->validateAllPayments(); } @@ -1066,8 +1077,10 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * @param $contributionID * @param $partialAmount * @param $totalAmount + * + * @throws \CRM_Core_Exception */ - public function createPartialPaymentOnContribution($contributionID, $partialAmount, $totalAmount) { + public function createPartialPaymentOnContribution($contributionID, $partialAmount, $totalAmount): void { //Create partial payment $params = [ 'contribution_id' => $contributionID, @@ -1097,7 +1110,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); $this->assertEquals($eft['values'][$eft['id']]['amount'], $partialAmount); $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contributionID]); - $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Partially paid'); + $this->assertEquals('Partially paid', $contribution['values'][$contribution['id']]['contribution_status']); $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], $totalAmount); } @@ -1106,7 +1119,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testCreatePaymentPayLaterPartialPayment() { + public function testCreatePaymentPayLaterPartialPayment(): void { $this->createLoggedInUser(); $contributionParams = [ 'total_amount' => 100, @@ -1144,17 +1157,17 @@ class api_v3_PaymentTest extends CiviUnitTestCase { 'financial_trxn_id' => $payment['id'], ]; $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); - $this->assertEquals($eft['values'][$eft['id']]['amount'], 40); + $this->assertEquals(40, $eft['values'][$eft['id']]['amount']); $params = [ 'entity_table' => 'civicrm_financial_item', 'financial_trxn_id' => $payment['id'], ]; $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params); - $this->assertEquals($eft['values'][$eft['id']]['amount'], 40); + $this->assertEquals(40, $eft['values'][$eft['id']]['amount']); // Check contribution for completed status $contribution = $this->callAPISuccess('contribution', 'get', ['id' => $contribution['id']]); - $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Unicorn'); - $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); + $this->assertEquals('Unicorn', $contribution['values'][$contribution['id']]['contribution_status']); + $this->assertEquals(100.00, $contribution['values'][$contribution['id']]['total_amount']); $this->callAPISuccess('Contribution', 'Delete', [ 'id' => $contribution['id'], ]); @@ -1169,7 +1182,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * @throws \CiviCRM_API3_Exception * @throws \CRM_Core_Exception */ - public function testPaymentWithProcessorWithOddFinancialAccount() { + public function testPaymentWithProcessorWithOddFinancialAccount(): void { $processor = $this->dummyProcessorCreate(['financial_account_id' => 'Deposit Bank Account', 'payment_instrument_id' => 'Cash']); $processor2 = $this->dummyProcessorCreate(['financial_account_id' => 'Payment Processor Account', 'name' => 'p2', 'payment_instrument_id' => 'EFT']); $contributionParams = [ @@ -1200,7 +1213,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - protected function addLocationToEvent($eventID) { + protected function addLocationToEvent(int $eventID): void { $addressParams = [ 'name' => 'event place', 'street_address' => 'streety street', @@ -1230,7 +1243,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - protected function checkPaymentIsValid($paymentID, $contributionID, $amount = 50) { + protected function checkPaymentIsValid(int $paymentID, int $contributionID, $amount = 50): void { $payment = $this->callAPISuccess('Payment', 'getsingle', ['financial_trxn_id' => $paymentID]); $this->assertEquals(7, $payment['from_financial_account_id']); $this->assertEquals(6, $payment['to_financial_account_id']); @@ -1271,8 +1284,10 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * @see https://github.com/civicrm/civicrm-core/pull/17688 * @see https://lab.civicrm.org/dev/financial/-/issues/139 * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ - public function testPaymentCreateTrxnIdAndDates() { + public function testPaymentCreateTrxnIdAndDates(): void { $trxnDate = '2010-01-01 09:00:00'; $trxnID = 'aabbccddeeffggh'; @@ -1285,7 +1300,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { 'receive_date' => $originalReceiveDate, ]); - $payment = $this->callAPISuccess('Payment', 'create', [ + $this->callAPISuccess('Payment', 'create', [ 'total_amount' => 100, 'order_id' => $contributionID, 'trxn_date' => $trxnDate, -- 2.25.1