From 8efccf9b7ce62cfd175212f4902f7a2f0f967175 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 18 Jun 2023 11:50:19 +1200 Subject: [PATCH] Superficial clean up in test class --- .../CRM/Core/Payment/PayPalIPNTest.php | 67 +++++++++---------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/tests/phpunit/CRM/Core/Payment/PayPalIPNTest.php b/tests/phpunit/CRM/Core/Payment/PayPalIPNTest.php index 931a568d2b47..131075ad8ac4 100644 --- a/tests/phpunit/CRM/Core/Payment/PayPalIPNTest.php +++ b/tests/phpunit/CRM/Core/Payment/PayPalIPNTest.php @@ -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. */ @@ -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 */ @@ -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 @@ -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', @@ -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()); @@ -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', @@ -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, @@ -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', 'payment_fee' => '0.63', 'first_name' => 'Robert', 'txn_id' => '8XA571746W2698126', @@ -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'); @@ -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'); @@ -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 @@ -333,18 +329,18 @@ 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', @@ -352,8 +348,7 @@ protected function createCustomField() { '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; }