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

Test fixes - addresses more invalid setup data #15802

Merged
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function setUp() {

/**
* Check method create().
*
* @throws \CRM_Core_Exception
*/
public function testCreate() {
$contactId = $this->individualCreate();
Expand Down
22 changes: 17 additions & 5 deletions tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -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());
Expand All @@ -224,6 +234,8 @@ public function testIPNPaymentMembershipRecurSuccess() {
'contribution_id' => $contribution['values'][1]['id'],
'entity_table' => 'civicrm_membership',
]);
$this->validateAllContributions();
$this->validateAllPayments();
}

/**
Expand Down Expand Up @@ -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',
Expand Down
125 changes: 125 additions & 0 deletions tests/phpunit/CRMTraits/Financial/OrderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2020 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
* Trait OrderTrait
*
* Trait for setting up orders for tests.
*/
trait CRMTraits_Financial_OrderTrait {

use \Civi\Test\Api3TestTrait;

/**
* Create a pending membership from a recurring order.
*
* @throws \CRM_Core_Exception
*/
public function createRepeatMembershipOrder() {
$this->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',
]);
}

}
23 changes: 23 additions & 0 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@ public function pledgeDelete($pledgeId) {
*
* @return int
* id of created contribution
* @throws \CRM_Core_Exception
*/
public function contributionCreate($params) {

Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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']);
}
}

}
6 changes: 4 additions & 2 deletions tests/phpunit/api/v3/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
Expand All @@ -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'],
]);
Expand Down