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

Superficial clean up in test class #26559

Merged
merged 1 commit into from
Jun 19, 2023
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
67 changes: 31 additions & 36 deletions tests/phpunit/CRM/Core/Payment/PayPalIPNTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ class CRM_Core_Payment_PayPalIPNTest extends CiviUnitTestCase {
protected $_paymentProcessorID;
protected $_customFieldID;

/**
* 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 blockinng.
*
* @var bool
*/
protected $isValidateFinancialsOnPostAssert = TRUE;

/**
* Set up function.
*/
Expand Down Expand Up @@ -63,7 +53,7 @@ public function tearDown(): void {
* Test IPN response updates contribution and invoice is attached in mail reciept
*
* The scenario is that a pending contribution exists and the IPN call will update it to completed.
* And also if Tax and Invoicing is enabled, this unit test ensure that invoice pdf is attached with email recipet
* And also if Tax and Invoicing is enabled, this unit test ensure that invoice pdf is attached with email receipt
*
* @throws \CRM_Core_Exception
*/
Expand All @@ -90,8 +80,7 @@ public function testInvoiceSentOnIPNPaymentSuccess(): void {
$_REQUEST = ['q' => CRM_Utils_System::url('civicrm/payment/ipn/' . $this->_paymentProcessorID)] + $this->getPaypalTransaction();

$mut = new CiviMailUtils($this, TRUE);
$paymentProcesors = $this->callAPISuccessGetSingle('PaymentProcessor', ['id' => $this->_paymentProcessorID]);
$payment = Civi\Payment\System::singleton()->getByProcessor($paymentProcesors);
$payment = Civi\Payment\System::singleton()->getByProcessor($this->callAPISuccessGetSingle('PaymentProcessor', ['id' => $this->_paymentProcessorID]));
$payment->handlePaymentNotification();

// Check if invoice pdf is attached with contribution mail reciept
Expand Down Expand Up @@ -145,7 +134,7 @@ public function testIPNPaymentRecurSuccess(): void {
]);
$this->assertEquals(2, $contributions['count']);
$contribution2 = $contributions['values'][1];
$this->assertEquals('secondone', $contribution2['trxn_id']);
$this->assertEquals('second_one', $contribution2['trxn_id']);
$paramsThatShouldMatch = [
'total_amount',
'net_amount',
Expand Down Expand Up @@ -177,7 +166,7 @@ public function testIPNPaymentMembershipRecurSuccess(): void {
$this->assertEquals(1, $contribution['contribution_status_id']);
$this->assertEquals('8XA571746W2698126', $contribution['trxn_id']);
// source gets set by processor
$this->assertTrue(substr($contribution['contribution_source'], 0, 20) === "Online Contribution:");
$this->assertSame(substr($contribution['contribution_source'], 0, 20), 'Online Contribution:');
$contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $this->_contributionRecurID]);
$this->assertEquals(5, $contributionRecur['contribution_status_id']);
$paypalIPN = new CRM_Core_Payment_PayPalIPN($this->getPaypalRecurSubsequentTransaction());
Expand All @@ -189,7 +178,7 @@ public function testIPNPaymentMembershipRecurSuccess(): void {
'sequential' => 1,
]);
$this->assertEquals(2, $contribution['count']);
$this->assertEquals('secondone', $contribution['values'][1]['trxn_id']);
$this->assertEquals('second_one', $contribution['values'][1]['trxn_id']);
$this->callAPISuccessGetCount('line_item', [
'entity_id' => $this->ids['membership'],
'entity_table' => 'civicrm_membership',
Expand Down Expand Up @@ -227,7 +216,7 @@ public function getPaypalRecurTransaction(): array {
/**
* Get IPN style details for an incoming paypal standard transaction.
*/
public function getPaypalTransaction() {
public function getPaypalTransaction(): array {
return [
'contactID' => $this->_contactID,
'contributionID' => $this->_contributionID,
Expand All @@ -240,7 +229,7 @@ public function getPaypalTransaction() {
'payment_status' => 'Completed',
'receiver_email' => 'sunil._1183377782_biz_api1.webaccess.co.in',
'txn_type' => 'web_accept',
'last_name' => 'Roberty',
'last_name' => 'Robert',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may have been just so it was different from first name, but not a blocker.

Also the _customFieldID declaration at the top of the file can go now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@demeritcowboy yeah - I tend to clean up stuff that needlessly upsets spell checker

'payment_fee' => '0.63',
'first_name' => 'Robert',
'txn_id' => '8XA571746W2698126',
Expand All @@ -254,17 +243,21 @@ public function getPaypalTransaction() {
*
* @return array
*/
public function getPaypalRecurSubsequentTransaction() {
return array_merge($this->getPaypalRecurTransaction(), ['txn_id' => 'secondone']);
public function getPaypalRecurSubsequentTransaction(): array {
return array_merge($this->getPaypalRecurTransaction(), ['txn_id' => 'second_one']);
}

/**
* Test IPN response updates contribution and invoice is attached in mail reciept
* Test also AlterIPNData intercepts at the right point and allows for custom processing
* The scenario is that a pending contribution exists and the IPN call will update it to completed.
* And also if Tax and Invoicing is enabled, this unit test ensure that invoice pdf is attached with email recipet
* Test IPN response updates contribution and invoice is attached in mail
* reciept Test also AlterIPNData intercepts at the right point and allows
* for custom processing The scenario is that a pending contribution exists
* and the IPN call will update it to completed. And also if Tax and
* Invoicing is enabled, this unit test ensure that invoice pdf is attached
* with email receipt
*
* @throws \CRM_Core_Exception
*/
public function testhookAlterIPNDataOnIPNPaymentSuccess() {
public function testHookAlterIPNDataOnIPNPaymentSuccess(): void {

$pendingStatusID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending');
$completedStatusID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
Expand All @@ -286,20 +279,23 @@ public function testhookAlterIPNDataOnIPNPaymentSuccess() {
global $_REQUEST;
$_REQUEST = ['q' => CRM_Utils_System::url('civicrm/payment/ipn/' . $this->_paymentProcessorID)] + $this->getPaypalTransaction();

$mut = new CiviMailUtils($this, TRUE);
CRM_Core_Payment::handlePaymentMethod('PaymentNotification', ['processor_id' => $this->_paymentProcessorID]);

$contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $this->_contributionID, 'sequential' => 1]);
// assert that contribution is completed after getting response from paypal standard which has transaction id set and completed status
$this->assertEquals($_REQUEST['txn_id'], $contribution['trxn_id']);
$this->assertEquals($completedStatusID, $contribution['contribution_status_id']);
$this->assertEquals('test12345', $contribution['custom_' . $this->_customFieldID]);
$this->assertEquals('test12345', $contribution['custom_' . $this->ids['CustomField']['Contribution']]);
}

/**
* Allow IPNs to validate when the supplied contact_id has been deleted from the database but there is a valid contact id in the contribution recur object or contribution object
* Allow IPNs to validate when the supplied contact_id has been deleted from
* the database but there is a valid contact id in the contribution recur
* object or contribution object
*
* @throws \CRM_Core_Exception
*/
public function testPayPalIPNSuccessDeletedContact() {
public function testPayPalIPNSuccessDeletedContact(): void {
$contactTobeDeleted = $this->individualCreate();
$pendingStatusID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending');
$completedStatusID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
Expand All @@ -322,7 +318,7 @@ public function testPayPalIPNSuccessDeletedContact() {
global $_REQUEST;
$_REQUEST = ['q' => CRM_Utils_System::url('civicrm/payment/ipn/' . $this->_paymentProcessorID)] + $payPalIPNParams;
// Now process the IPN noting that the contact id that was supplied with the IPN has been deleted but there is still a valid one on the contribution id
$payment = CRM_Core_Payment::handlePaymentMethod('PaymentNotification', ['processor_id' => $this->_paymentProcessorID]);
CRM_Core_Payment::handlePaymentMethod('PaymentNotification', ['processor_id' => $this->_paymentProcessorID]);

$contribution = $this->callAPISuccess('contribution', 'get', ['id' => $this->_contributionID, 'sequential' => 1]);
// assert that contribution is completed after getting response from paypal standard which has transaction id set and completed status
Expand All @@ -333,27 +329,26 @@ public function testPayPalIPNSuccessDeletedContact() {
/**
* Store Custom data passed in from the PayPalIPN in a custom field
*/
public function hookCiviCRMAlterIPNData($data) {
public function hookCiviCRMAlterIPNData($data): void {
if (!empty($data['custom'])) {
$customData = json_decode($data['custom'], TRUE);
$customField = $this->callAPISuccess('custom_field', 'get', ['label' => 'TestCustomFieldIPNHook']);
$this->callAPISuccess('contribution', 'create', ['id' => $this->_contributionID, 'custom_' . $customField['id'] => $customData['cgid']]);
$customField = $this->callAPISuccess('CustomField', 'get', ['label' => 'TestCustomFieldIPNHook']);
$this->callAPISuccess('Contribution', 'create', ['id' => $this->_contributionID, 'custom_' . $customField['id'] => $customData['cgid']]);
}
}

/**
* @return array
*/
protected function createCustomField() {
protected function createCustomField(): array {
$customGroup = $this->customGroupCreate(['extends' => 'Contribution']);
$fields = [
'label' => 'TestCustomFieldIPNHook',
'data_type' => 'String',
'html_type' => 'Text',
'custom_group_id' => $customGroup['id'],
];
$field = CRM_Core_BAO_CustomField::create($fields);
$this->_customFieldID = $field->id;
$this->ids['CustomField']['Contribution'] = $this->callAPISuccess('CustomField', 'create', $fields)['id'];
return $customGroup;
}

Expand Down