From 260763483340e894334119b24a7ae35a15960575 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 7 Jul 2021 10:04:27 +1200 Subject: [PATCH] Fix BaseIPN test to (mostly) pass invalid financials --- api/v3/Order.php | 2 +- .../phpunit/CRM/Core/Payment/BaseIPNTest.php | 103 ++++++++++-------- .../CRM/Event/BAO/ChangeFeeSelectionTest.php | 3 + tests/phpunit/CiviTest/CiviUnitTestCase.php | 7 +- .../phpunit/api/v3/SyntaxConformanceTest.php | 10 ++ 5 files changed, 73 insertions(+), 52 deletions(-) diff --git a/api/v3/Order.php b/api/v3/Order.php index cf0e8ee50e..bf0fc398d2 100644 --- a/api/v3/Order.php +++ b/api/v3/Order.php @@ -144,7 +144,7 @@ function civicrm_api3_order_create(array $params): array { } $contribution = civicrm_api3('Contribution', 'create', $contributionParams); - $contribution['values'][$contribution['id']]['line_item'] = $order->getLineItems(); + $contribution['values'][$contribution['id']]['line_item'] = array_values($order->getLineItems()); // add payments if ($entity && !empty($contribution['id'])) { diff --git a/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php b/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php index 662b1e5ec5..59ec6a7c9e 100644 --- a/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php +++ b/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php @@ -17,6 +17,16 @@ use Civi\Api4\Contribution; */ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { + /** + * 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; + protected $_financialTypeId; protected $_contributionParams; protected $_contactId; @@ -78,18 +88,10 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { 'financial_type_id' => $this->_financialTypeId, 'receive_date' => date('Ymd'), 'total_amount' => 150.00, - 'invoice_id' => 'c8acb91e080ad7bd8a2adc119c192885', 'currency' => 'USD', 'contribution_recur_id' => $this->_recurId, 'contribution_status_id' => 2, ]; - $contribution = $this->callAPISuccess('contribution', 'create', $this->_contributionParams); - $this->_contributionId = $contribution['id']; - - $contribution = new CRM_Contribute_BAO_Contribution(); - $contribution->id = $this->_contributionId; - $contribution->find(TRUE); - $this->objects['contribution'] = $contribution; } /** @@ -106,6 +108,7 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { public function testLoadMembershipObjectsLoadAll() { $this->_setUpMembershipObjects(); $this->_setUpRecurringContribution(); + $this->_membershipId = $this->ids['membership']; unset($this->ids['membership']); $contribution = new CRM_Contribute_BAO_Contribution(); $contribution->id = $this->_contributionId; @@ -258,6 +261,8 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { * @throws \CiviCRM_API3_Exception */ public function testThatCancellingEventPaymentWillCancelAllAdditionalPendingParticipantsAndCreateCancellationActivities(): void { + // Test fails - reason not yet investigated. + $this->isValidateFinancialsOnPostAssert = FALSE; $this->_setUpParticipantObjects('Pending from incomplete transaction'); $additionalParticipantId = $this->participantCreate([ 'event_id' => $this->_eventId, @@ -357,7 +362,7 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { 'component' => 'contribute', 'contribution_page_id' => $contributionPageID, 'total_amount' => 110.00, - 'invoiceID' => "c8acb91e080ad7777a2adc119c192885", + 'invoiceID' => 'c8acb91e080ad7777a2adc119c192885', 'contactID' => $this->_contactId, 'contributionID' => $this->objects['contribution']->id, ]; @@ -367,55 +372,45 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { * Prepare for membership test. */ public function _setUpMembershipObjects(): void { - try { - if (!$this->_membershipTypeID) { - $this->_membershipTypeID = $this->membershipTypeCreate(); - } - if (!$this->_membershipStatusID) { - $this->_membershipStatusID = $this->membershipStatusCreate('test status'); - } + if (!$this->_membershipTypeID) { + $this->_membershipTypeID = $this->membershipTypeCreate(); } - catch (Exception$e) { - echo $e->getMessage(); + if (!$this->_membershipStatusID) { + $this->_membershipStatusID = $this->membershipStatusCreate('test status'); } - CRM_Member_PseudoConstant::membershipType($this->_membershipTypeID, TRUE); - CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE); - $this->_membershipParams = [ - 'contact_id' => $this->_contactId, - 'membership_type_id' => $this->_membershipTypeID, - 'join_date' => '2009-01-21', - 'start_date' => '2009-01-21', - 'end_date' => '2009-12-21', - 'source' => 'Payment', - 'is_override' => 1, - 'status_id' => $this->_membershipStatusID, - 'version' => 3, - ]; - $membership = $this->callAPISuccess('membership', 'create', $this->_membershipParams); - if ($this->objects['contribution']->id != $this->_contributionId) { - $contribution = new CRM_Contribute_BAO_Contribution(); - $contribution->id = $this->_contributionId; - $contribution->find(TRUE); - $this->objects = ['contribution' => $contribution]; - } - $this->_membershipId = $membership['id']; - //we'll create membership payment here because to make setup more re-usable - $this->callAPISuccess('membership_payment', 'create', [ - 'contribution_id' => $this->_contributionId, - 'membership_id' => $this->_membershipId, - ]); + $orderParams = $this->_contributionParams; + $orderParams['line_items'] = [ + [ + 'line_item' => [['membership_type_id' => $this->_membershipTypeID, 'line_total' => 150]], + 'params' => [ + 'contact_id' => $this->_contactId, + 'membership_type_id' => $this->_membershipTypeID, + 'join_date' => '2009-01-21', + 'start_date' => '2009-01-21', + 'end_date' => '2009-12-21', + 'source' => 'Payment', + 'is_override' => 1, + 'status_id' => $this->_membershipStatusID, + ], + ], + ]; + $order = $this->callAPISuccess('Order', 'create', $orderParams); + $this->_contributionId = $order['id']; + $contribution = new CRM_Contribute_BAO_Contribution(); + $contribution->id = $this->_contributionId; + $contribution->find(TRUE); + $this->objects = ['contribution' => $contribution]; + $this->ids['membership'] = (int) $order['values'][$order['id']]['line_item'][0]['entity_id']; $this->input = [ 'component' => 'contribute', 'total_amount' => 150.00, - 'invoiceID' => "c8acb91e080ad7bd8a2adc119c192885", + 'invoiceID' => 'c8acb91e080ad7bd8a2adc119c192885', 'contactID' => $this->_contactId, 'contributionID' => $this->_contributionId, - 'membershipID' => $this->_membershipId, + 'membershipID' => $this->ids['membership'], ]; - - $this->ids['membership'] = $this->_membershipId; } public function _setUpRecurringContribution() { @@ -450,6 +445,7 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { */ public function _setUpParticipantObjects($participantStatus = 'Attended'): void { $event = $this->eventCreate(['is_email_confirm' => 1]); + $this->setupContribution(); $this->_eventId = $event['id']; $this->_participantId = $this->participantCreate([ @@ -485,6 +481,7 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { * Set up participant requirements for test. */ public function _setUpPledgeObjects() { + $this->setupContribution(); $this->_pledgeId = $this->pledgeCreate(['contact_id' => $this->_contactId]); //we'll create membership payment here because to make setup more re-usable $pledgePayment = $this->callAPISuccess('pledge_payment', 'create', [ @@ -507,4 +504,14 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { $this->ids['pledge_payment'][] = $pledgePayment['id']; } + protected function setupContribution(): void { + $contribution = $this->callAPISuccess('contribution', 'create', $this->_contributionParams); + $this->_contributionId = $contribution['id']; + + $contribution = new CRM_Contribute_BAO_Contribution(); + $contribution->id = $this->_contributionId; + $contribution->find(TRUE); + $this->objects['contribution'] = $contribution; + } + } diff --git a/tests/phpunit/CRM/Event/BAO/ChangeFeeSelectionTest.php b/tests/phpunit/CRM/Event/BAO/ChangeFeeSelectionTest.php index 85524bd91b..66bdd315ab 100644 --- a/tests/phpunit/CRM/Event/BAO/ChangeFeeSelectionTest.php +++ b/tests/phpunit/CRM/Event/BAO/ChangeFeeSelectionTest.php @@ -276,6 +276,9 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { * @throws \CiviCRM_API3_Exception */ public function testCRM19273() { + // When a line item is 'resurrected' the financial_items attached to it are wrong. + // We have to skip validatePayments until fixed. + $this->isValidateFinancialsOnPostAssert = FALSE; $this->registerParticipantAndPay(); $priceSetParams['price_' . $this->priceSetFieldID] = $this->cheapFeeValueID; diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index b985146efc..902a6f0fe5 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -26,6 +26,7 @@ * . */ +use Civi\Api4\Contribution; use Civi\Api4\CustomField; use Civi\Api4\CustomGroup; use Civi\Api4\OptionGroup; @@ -151,7 +152,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { * 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. + * are some test data issues and some real bugs currently blocking. * * @var bool */ @@ -3639,10 +3640,10 @@ VALUES /** * Validate all created contributions. * - * @throws \CRM_Core_Exception + * @throws \API_Exception */ protected function validateAllContributions(): void { - $contributions = $this->callAPISuccess('Contribution', 'get', ['return' => ['tax_amount', 'total_amount']])['values']; + $contributions = Contribution::get(FALSE)->setSelect(['total_amount', 'tax_amount'])->execute(); foreach ($contributions as $contribution) { $lineItems = $this->callAPISuccess('LineItem', 'get', [ 'contribution_id' => $contribution['id'], diff --git a/tests/phpunit/api/v3/SyntaxConformanceTest.php b/tests/phpunit/api/v3/SyntaxConformanceTest.php index 01e7c2a8a4..d591736395 100644 --- a/tests/phpunit/api/v3/SyntaxConformanceTest.php +++ b/tests/phpunit/api/v3/SyntaxConformanceTest.php @@ -24,6 +24,16 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase { protected $_apiversion = 3; + /** + * Do not check financial entities in this test class. + * + * The class uses lots of crud to do things by-passing + * BAO logic & entities are often not valid as a result. + * + * @var bool + */ + protected $isValidateFinancialsOnPostAssert = FALSE; + /** * @var array * e.g. $this->deletes['CRM_Contact_DAO_Contact'][] = $contactID; -- 2.25.1