From 7aeb7f063b1ca8d12277653e202a06936ce76d5f Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 11 Nov 2019 12:46:09 +1300 Subject: [PATCH] Fix data to be valid for one of the a.net tests The tests were munging the line items in set up - this fixes it - but only for one so far --- .../CRM/Core/BAO/FinancialTrxnTest.php | 2 + .../CRM/Core/Payment/AuthorizeNetIPNTest.php | 22 ++- .../CRMTraits/Financial/OrderTrait.php | 125 ++++++++++++++++++ tests/phpunit/CiviTest/CiviUnitTestCase.php | 23 ++++ tests/phpunit/api/v3/OrderTest.php | 6 +- 5 files changed, 171 insertions(+), 7 deletions(-) create mode 100644 tests/phpunit/CRMTraits/Financial/OrderTrait.php diff --git a/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php b/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php index e465da641f1e..dc5f9951d62a 100644 --- a/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php +++ b/tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php @@ -37,6 +37,8 @@ public function setUp() { /** * Check method create(). + * + * @throws \CRM_Core_Exception */ public function testCreate() { $contactId = $this->individualCreate(); diff --git a/tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php b/tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php index 8ca640090982..8b57971ee192 100644 --- a/tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php +++ b/tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php @@ -7,6 +7,8 @@ * @group headless */ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase { + use CRMTraits_Financial_OrderTrait; + protected $_contributionID; protected $_invoiceID = 'c2r9c15f7be20b4f3fef1f77e4c37424'; protected $_financialTypeID = 1; @@ -15,12 +17,16 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase { protected $_contributionPageID; protected $_paymentProcessorID; + /** + * + * @throws \CRM_Core_Exception + */ public function setUp() { parent::setUp(); $this->_paymentProcessorID = $this->paymentProcessorAuthorizeNetCreate(['is_test' => 0]); $this->_contactID = $this->individualCreate(); $contributionPage = $this->callAPISuccess('contribution_page', 'create', [ - 'title' => "Test Contribution Page", + 'title' => 'Test Contribution Page', 'financial_type_id' => $this->_financialTypeID, 'currency' => 'USD', 'payment_processor' => $this->_paymentProcessorID, @@ -101,6 +107,7 @@ public function testIPNPaymentRecurNoReceipt() { $this->markTestSkipped('Error from A.net - cannot proceed'); } $this->_contributionID = $contribution->id; + $this->ids['Contribution'][0] = $contribution->id; $this->_contributionRecurID = $contribution->contribution_recur_id; $recur_params = [ 'id' => $this->_contributionRecurID, @@ -126,6 +133,8 @@ public function testIPNPaymentRecurNoReceipt() { /** * Test IPN response updates contribution_recur & contribution for first & second contribution + * + * @throws \CRM_Core_Exception */ public function testIPNPaymentRecurSuccess() { $this->setupRecurringPaymentProcessorTransaction(); @@ -198,14 +207,15 @@ public function testIPNPaymentRecurSuccessSuppliedReceiveDate() { * Test IPN response updates contribution_recur & contribution for first & second contribution */ public function testIPNPaymentMembershipRecurSuccess() { - $this->setupMembershipRecurringPaymentProcessorTransaction(); + $this->createRepeatMembershipOrder(); $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurTransaction()); $IPN->main(); - $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->_contributionID]); + $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $this->ids['Contribution'][0]]); $this->assertEquals(1, $contribution['contribution_status_id']); $this->assertEquals('6511143069', $contribution['trxn_id']); + // source gets set by processor - $this->assertTrue(substr($contribution['contribution_source'], 0, 20) == "Online Contribution:"); + $this->assertEquals('Online Contribution:', substr($contribution['contribution_source'], 0, 20)); $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $this->_contributionRecurID]); $this->assertEquals(5, $contributionRecur['contribution_status_id']); $IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->getRecurSubsequentTransaction()); @@ -224,6 +234,8 @@ public function testIPNPaymentMembershipRecurSuccess() { 'contribution_id' => $contribution['values'][1]['id'], 'entity_table' => 'civicrm_membership', ]); + $this->validateAllContributions(); + $this->validateAllPayments(); } /** @@ -394,7 +406,7 @@ public function getRecurTransaction($params = []) { 'x_state' => 'WA', 'x_city' => 'Dallas', 'x_address' => '41 My ST', - 'x_invoice_num' => $this->_contributionID, + 'x_invoice_num' => $this->ids['Contribution'][0], 'x_cust_id' => $this->_contactID, 'x_company' => 'nowhere@civicrm.org', 'x_last_name' => 'Roberts', diff --git a/tests/phpunit/CRMTraits/Financial/OrderTrait.php b/tests/phpunit/CRMTraits/Financial/OrderTrait.php new file mode 100644 index 000000000000..147493013eda --- /dev/null +++ b/tests/phpunit/CRMTraits/Financial/OrderTrait.php @@ -0,0 +1,125 @@ +createExtraneousContribution(); + $this->ids['contact'][0] = $this->individualCreate(); + $this->ids['membership_type'][0] = $this->membershipTypeCreate(); + + $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([ + 'contact_id' => $this->_contactID, + 'amount' => 1000, + 'sequential' => 1, + 'installments' => 5, + 'frequency_unit' => 'Month', + 'frequency_interval' => 1, + 'invoice_id' => $this->_invoiceID, + 'contribution_status_id' => 2, + 'payment_processor_id' => $this->_paymentProcessorID, + // processor provided ID - use contact ID as proxy. + 'processor_id' => $this->_contactID, + ])); + + $orderID = $this->callAPISuccess('Order', 'create', [ + 'total_amount' => '200', + 'financial_type_id' => 'Donation', + 'contribution_status_id' => 'Pending', + 'contact_id' => $this->_contactID, + 'contribution_page_id' => $this->_contributionPageID, + 'payment_processor_id' => $this->_paymentProcessorID, + 'is_test' => 0, + 'receive_date' => '2019-07-25 07:34:23', + 'skipCleanMoney' => TRUE, + 'contribution_recur_id' => $contributionRecur['id'], + 'line_items' => [ + [ + 'params' => [ + 'contact_id' => $this->ids['contact'][0], + 'membership_type_id' => $this->ids['membership_type'][0], + 'contribution_recur_id' => $contributionRecur['id'], + 'source' => 'Payment', + ], + 'line_item' => [ + [ + 'label' => 'General', + 'qty' => 1, + 'unit_price' => 200, + 'line_total' => 200, + 'financial_type_id' => 1, + 'entity_table' => 'civicrm_membership', + 'price_field_id' => $this->callAPISuccess('price_field', 'getvalue', [ + 'return' => 'id', + 'label' => 'Membership Amount', + 'options' => ['limit' => 1, 'sort' => 'id DESC'], + ]), + 'price_field_value_id' => $this->callAPISuccess('price_field_value', 'getvalue', [ + 'return' => 'id', + 'label' => 'General', + 'options' => ['limit' => 1, 'sort' => 'id DESC'], + ]), + ], + ], + ], + ], + ])['id']; + + $this->ids['ContributionRecur'][0] = $contributionRecur['id']; + $this->ids['Contribution'][0] = $orderID; + } + + /** + * Create an extraneous contribution to throw off any 'number one bugs'. + * + * Ie this means our real data starts from 2 & we won't hit 'pretend passes' + * just because the number 1 is used for multiple entities. + */ + protected function createExtraneousContribution() { + $this->contributionCreate([ + 'contact_id' => $this->_contactID, + 'is_test' => 1, + 'financial_type_id' => 1, + 'invoice_id' => 'abcd', + 'trxn_id' => 345, + 'receive_date' => '2019-07-25 07:34:23', + ]); + } + +} diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 610be7dab843..d5ca8938cee0 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -954,6 +954,7 @@ public function pledgeDelete($pledgeId) { * * @return int * id of created contribution + * @throws \CRM_Core_Exception */ public function contributionCreate($params) { @@ -2426,6 +2427,8 @@ public function paymentProcessorCreate($params = []) { * * @param array $recurParams (Optional) * @param array $contributionParams (Optional) + * + * @throws \CRM_Core_Exception */ public function setupRecurringPaymentProcessorTransaction($recurParams = [], $contributionParams = []) { $contributionParams = array_merge([ @@ -2456,12 +2459,15 @@ public function setupRecurringPaymentProcessorTransaction($recurParams = [], $co ], $recurParams)); $this->_contributionRecurID = $contributionRecur['id']; $this->_contributionID = $contributionRecur['values']['0']['api.contribution.create']['id']; + $this->ids['Contribution'][0] = $this->_contributionID; } /** * We don't have a good way to set up a recurring contribution with a membership so let's just do one then alter it * * @param array $params Optionally modify params for membership/recur (duration_unit/frequency_unit) + * + * @throws \CRM_Core_Exception */ public function setupMembershipRecurringPaymentProcessorTransaction($params = []) { $membershipParams = $recurParams = []; @@ -3431,4 +3437,21 @@ protected function validateAllPayments() { $this->validatePayments($payments); } + /** + * Validate all created contributions. + * + * @throws \CRM_Core_Exception + */ + protected function validateAllContributions() { + $contributions = $this->callAPISuccess('Contribution', 'get', [])['values']; + foreach ($contributions as $contribution) { + $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $contribution['id']])['values']; + $total = 0; + foreach ($lineItems as $lineItem) { + $total += $lineItem['line_total']; + } + $this->assertEquals($total, $contribution['total_amount']); + } + } + } diff --git a/tests/phpunit/api/v3/OrderTest.php b/tests/phpunit/api/v3/OrderTest.php index 4dbb9074ca6b..eb8112b8dd1d 100644 --- a/tests/phpunit/api/v3/OrderTest.php +++ b/tests/phpunit/api/v3/OrderTest.php @@ -244,7 +244,7 @@ public function testAddOrderForMembership() { 'is_override' => 1, ], ]; - $order = $this->callAPIAndDocument('order', 'create', $p, __FUNCTION__, __FILE__); + $order = $this->callAPIAndDocument('Order', 'create', $p, __FUNCTION__, __FILE__); $params = [ 'contribution_id' => $order['id'], ]; @@ -258,7 +258,9 @@ public function testAddOrderForMembership() { ], ]; $this->checkPaymentResult($order, $expectedResult); - $this->callAPISuccessGetCount('MembershipPayment', $params, 1); + $membershipPayment = $this->callAPISuccessGetSingle('MembershipPayment', $params); + + $membership = $this->callAPISuccessGetSingle('Membership', ['id' => $membershipPayment['id']]); $this->callAPISuccess('Contribution', 'Delete', [ 'id' => $order['id'], ]);