From 6573357b0be6b259c7ae182a12754c2fa0548112 Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Wed, 7 Oct 2020 17:19:37 +0530 Subject: [PATCH] unit test for #18306 - order create api test for contribution --- .../CRMTraits/Financial/OrderTrait.php | 42 +++++++++++++++++++ tests/phpunit/api/v3/ContributionTest.php | 29 +++++++++++++ 2 files changed, 71 insertions(+) diff --git a/tests/phpunit/CRMTraits/Financial/OrderTrait.php b/tests/phpunit/CRMTraits/Financial/OrderTrait.php index c927e7185747..4126ddbbb906 100644 --- a/tests/phpunit/CRMTraits/Financial/OrderTrait.php +++ b/tests/phpunit/CRMTraits/Financial/OrderTrait.php @@ -88,6 +88,48 @@ public function createRepeatMembershipOrder() { $this->ids['Contribution'][0] = $orderID; } + /** + * Create an order for a contribution. + * + * @throws \CRM_Core_Exception + */ + protected function createContributionOrder() { + $orderID = $this->callAPISuccess('Order', 'create', [ + 'total_amount' => 100, + 'financial_type_id' => 'Donation', + 'contact_id' => $this->_contactID, + 'is_test' => 0, + 'payment_instrument_id' => 'Check', + 'receive_date' => date('Y-m-d'), + 'line_items' => [ + [ + 'params' => [ + 'contact_id' => $this->_contactID, + 'source' => 'Payment', + ], + 'line_item' => [ + [ + 'label' => 'Contribution Amount', + 'qty' => 1, + 'unit_price' => 100, + 'line_total' => 100, + 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'), + 'entity_table' => 'civicrm_contribution', + 'price_field_id' => $this->callAPISuccessGetValue('price_field', [ + 'return' => 'id', + 'label' => 'Contribution Amount', + 'options' => ['limit' => 1, 'sort' => 'id DESC'], + ]), + 'price_field_value_id' => NULL, + ], + ], + ], + ], + ])['id']; + + $this->ids['Contribution'][0] = $orderID; + } + /** * Create an order with more than one membership. * diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index deb222613378..ab66bc1005b5 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -22,6 +22,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase { use CRMTraits_Profile_ProfileTrait; use CRMTraits_Custom_CustomDataTrait; + use CRMTraits_Financial_OrderTrait; protected $_individualId; protected $_contribution; @@ -2767,6 +2768,34 @@ public function testRepeatTransactionPassedInFinancialType() { $this->assertEquals($expectedLineItem, $lineItem2['values'][0]); } + /** + * Test Contribution with Order api. + * + * @throws \CRM_Core_Exception + */ + public function testContributionOrder() { + $this->_contactID = $this->individualCreate(); + $this->createContributionOrder(); + $contribution = $this->callAPISuccess('contribution', 'get')['values'][$this->ids['Contribution'][0]]; + $this->assertEquals('Pending Label**', $contribution['contribution_status']); + + $this->callAPISuccess('Payment', 'create', [ + 'contribution_id' => $this->ids['Contribution'][0], + 'payment_instrument_id' => 'Check', + 'total_amount' => 100, + ]); + $contribution = $this->callAPISuccess('contribution', 'get')['values'][$this->ids['Contribution'][0]]; + $this->assertEquals('Completed', $contribution['contribution_status']); + + $lineItem = $this->callAPISuccess('line_item', 'get', [ + 'sequential' => 1, + 'contribution_id' => $this->ids['Contribution'][0], + ]); + $this->assertEquals(1, $lineItem['count']); + $this->assertEquals($this->ids['Contribution'][0], $lineItem['values'][0]['entity_id']); + $this->assertEquals('100.00', $lineItem['values'][0]['line_total']); + } + /** * Test financial_type_id override behaviour with a single line item. *