Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix v3 api payment test to validate on postAssert #20530

Merged
merged 1 commit into from
Jun 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 28 additions & 38 deletions tests/phpunit/api/v3/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ class api_v3_PaymentTest extends CiviUnitTestCase {

protected $_financialTypeId = 1;

/**
* Should financials be checked after the test but before tear down.
*
* Ideally all tests (or at least all that call any financial api calls ) should do this but there
* are some test data issues and some real bugs currently blocking.
*
* @var bool
*/
protected $isValidateFinancialsOnPostAssert = TRUE;

/**
* Setup function.
*
Expand Down Expand Up @@ -326,7 +336,6 @@ public function testPaymentEmailReceiptFullyPaid(): void {
'Balance Owed: $ 0.00',
'Thank you for completing this payment.',
]);
$this->validateAllPayments();
}

/**
Expand Down Expand Up @@ -365,7 +374,7 @@ public function testRefundEmailReceipt(string $thousandSeparator): void {
'is_payment' => 1,
];
foreach ($expected as $key => $value) {
$this->assertEquals($expected[$key], $payment[$key], 'mismatch on key ' . $key);
$this->assertEquals($value, $payment[$key], 'mismatch on key ' . $key);
}

$this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
Expand All @@ -380,7 +389,6 @@ public function testRefundEmailReceipt(string $thousandSeparator): void {
'Transaction Date: November 13th, 2018 12:01 PM',
'Total Paid: $ 170' . $decimalSeparator . '00',
]);
$this->validateAllPayments();
}

/**
Expand All @@ -394,7 +402,6 @@ public function testCreatePaymentPendingOrderNoLineItems(): void {
'order_id' => $order['id'],
'total_amount' => 50,
]);
$this->validateAllPayments();
}

/**
Expand All @@ -418,7 +425,6 @@ public function testAddPaymentMissingFinancialItems(): void {
]);
CRM_Core_DAO::executeQuery('DELETE FROM civicrm_financial_item');
$this->callAPISuccess('Payment', 'create', ['contribution_id' => $contribution['id'], 'payment_instrument_id' => 'Check', 'total_amount' => 5]);
$this->validateAllPayments();
}

/**
Expand Down Expand Up @@ -494,10 +500,6 @@ public function testCreatePaymentNoLineItems(): void {
$participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
$participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]);
$this->assertEquals('Registered', $participant['values'][$participant['id']]['participant_status']);
$this->callAPISuccess('Contribution', 'Delete', [
'id' => $contribution['id'],
]);
$this->validateAllPayments();
}

/**
Expand Down Expand Up @@ -589,7 +591,6 @@ public function testCreatePaymentLineItems(): void {
$participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $paymentParticipant);
$participant = $this->callAPISuccess('participant', 'get', ['id' => $participantPayment['participant_id']]);
$this->assertEquals('Registered', $participant['values'][$participant['id']]['participant_status']);
$this->validateAllPayments();
}

/**
Expand All @@ -599,7 +600,7 @@ public function testCreatePaymentLineItems(): void {
*/
public function testRefundPayment(): void {
$result = $this->callAPISuccess('Contribution', 'create', [
'financial_type_id' => "Donation",
'financial_type_id' => 'Donation',
'total_amount' => 100,
'contact_id' => $this->_individualId,
]);
Expand All @@ -612,7 +613,7 @@ public function testRefundPayment(): void {
]);

$contribution = $this->callAPISuccessGetSingle('Contribution', [
'return' => ["contribution_status_id"],
'return' => ['contribution_status_id'],
'id' => $contributionID,
]);
//Still we've a status of Completed after refunding a partial amount.
Expand All @@ -638,7 +639,7 @@ public function testRefundPayment(): void {
*/
public function testRefundPaymentWithCancelledPaymentId(): void {
$result = $this->callAPISuccess('Contribution', 'create', [
'financial_type_id' => "Donation",
'financial_type_id' => 'Donation',
'total_amount' => 100,
'contact_id' => $this->_individualId,
]);
Expand All @@ -648,7 +649,7 @@ public function testRefundPaymentWithCancelledPaymentId(): void {
$this->callAPISuccess('Payment', 'create', [
'contribution_id' => $contributionID,
'total_amount' => -100,
'cancelled_payment_id' => 12345,
'cancelled_payment_id' => (int) $this->callAPISuccess('Payment', 'get', [])['id'],
]);
$contribution = $this->callAPISuccessGetSingle('Contribution', [
'return' => ['contribution_status_id'],
Expand Down Expand Up @@ -690,11 +691,6 @@ public function testCancelPayment(): void {
foreach ($payment['values'] as $value) {
$this->assertEquals($value['total_amount'], array_pop($amounts), 'Mismatch total amount');
}

$this->callAPISuccess('Contribution', 'Delete', [
'id' => $contribution['id'],
]);
$this->validateAllPayments();
}

/**
Expand Down Expand Up @@ -798,7 +794,7 @@ public function testUpdatePayment(): void {
$payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__, 'Update Payment', 'UpdatePayment');

$this->validateAllPayments();
// Check for proportional cancelled payment against lineitems.
// Check for proportional cancelled payment against line items.
$minParams = [
'entity_table' => 'civicrm_financial_item',
'financial_trxn_id' => $payment['id'] - 1,
Expand All @@ -812,7 +808,7 @@ public function testUpdatePayment(): void {
$this->assertEquals($value['amount'], array_pop($amounts));
}

// Check for proportional updated payment against lineitems.
// Check for proportional updated payment against line items.
$params = [
'entity_table' => 'civicrm_financial_item',
'financial_trxn_id' => $payment['id'],
Expand Down Expand Up @@ -849,18 +845,15 @@ public function testUpdatePayment(): void {
$eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
$this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
}

$this->callAPISuccess('Contribution', 'Delete', [
'id' => $contribution['id'],
]);
$this->validateAllPayments();
}

/**
* Test that a contribution can be overpaid with the payment api.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function testCreatePaymentOverPay(): void {
$contributionID = $this->contributionCreate(['contact_id' => $this->individualCreate()]);
Expand All @@ -873,9 +866,10 @@ public function testCreatePaymentOverPay(): void {
}

/**
* Test create payment api for paylater contribution
* Test create payment api for pay later contribution
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testCreatePaymentPayLater(): void {
$this->createLoggedInUser();
Expand Down Expand Up @@ -983,16 +977,17 @@ public function testCreatePaymentIncompletePaymentPartialPayment(): void {
'payment_instrument_id' => 'Check',
'check_number' => $checkNumber2,
]);
$expectedConcatanatedCheckNumbers = implode(',', [$checkNumber1, $checkNumber2]);
$expectedConcatenatedCheckNumbers = implode(',', [$checkNumber1, $checkNumber2]);
//Assert check number is concatenated on the main contribution.
$contributionValues = $this->callAPISuccess('Contribution', 'getsingle', ['id' => $contribution['id']]);
$this->assertEquals($expectedConcatanatedCheckNumbers, $contributionValues['check_number']);
$this->assertEquals($expectedConcatenatedCheckNumbers, $contributionValues['check_number']);
}

/**
* Test create payment api for failed contribution.
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testCreatePaymentOnFailedContribution(): void {
$this->createLoggedInUser();
Expand Down Expand Up @@ -1118,6 +1113,7 @@ public function createPartialPaymentOnContribution($contributionID, $partialAmou
* Test create payment api for pay later contribution with partial payment.
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testCreatePaymentPayLaterPartialPayment(): void {
$this->createLoggedInUser();
Expand Down Expand Up @@ -1173,7 +1169,6 @@ public function testCreatePaymentPayLaterPartialPayment(): void {
]);
$this->callAPISuccess('OptionValue', 'get', ['name' => 'Completed', 'option_group_id' => 'contribution_status', 'api.OptionValue.create' => ['label' => 'Completed']]);
$this->callAPISuccessGetCount('Activity', ['target_contact_id' => $this->_individualId, 'activity_type_id' => 'Payment'], 2);
$this->validateAllPayments();
}

/**
Expand Down Expand Up @@ -1229,7 +1224,6 @@ protected function addLocationToEvent(int $eventID): void {
'loc_block_id' => $location['id'],
'is_show_location' => TRUE,
]);
$this->validateAllPayments();
}

/**
Expand All @@ -1243,7 +1237,7 @@ protected function addLocationToEvent(int $eventID): void {
*
* @throws \CRM_Core_Exception
*/
protected function checkPaymentIsValid(int $paymentID, int $contributionID, $amount = 50): void {
protected function checkPaymentIsValid(int $paymentID, int $contributionID, int $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']);
Expand All @@ -1258,7 +1252,6 @@ protected function checkPaymentIsValid(int $paymentID, int $contributionID, $amo
]);

$this->assertEquals($eft['values'][$eft['id']]['amount'], $amount);
$this->validateAllPayments();
}

/**
Expand Down Expand Up @@ -1315,13 +1308,10 @@ public function testPaymentCreateTrxnIdAndDates(): void {
$this->assertEquals(99.8, $contribution['net_amount']);

$this->assertEquals($trxnID, $contribution['trxn_id'],
"Contribution trxn_id should have been set to that of the payment.");
'Contribution trxn_id should have been set to that of the payment.');

$this->assertEquals($originalReceiveDate, $contribution['receive_date'],
"Contribution receive date was changed, but should not have been.");

$this->validateAllPayments();
$this->validateAllContributions();
'Contribution receive date was changed, but should not have been.');

}

Expand Down