From b8e6ed750c282908f09fe52d9265b0141309e619 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Wed, 30 Aug 2017 10:23:53 +1000 Subject: [PATCH] Add test of CRM-17151 ported from kainuk PR --- CRM/Price/BAO/LineItem.php | 35 ++++++++++++------- ...273Test.php => ChangeFeeSelectionTest.php} | 35 +++++++++++++++---- tests/phpunit/CiviTest/CiviUnitTestCase.php | 2 +- 3 files changed, 53 insertions(+), 19 deletions(-) rename tests/phpunit/CRM/Event/BAO/{CRM19273Test.php => ChangeFeeSelectionTest.php} (87%) diff --git a/CRM/Price/BAO/LineItem.php b/CRM/Price/BAO/LineItem.php index d020cb2944ec..e0d8ec558b64 100644 --- a/CRM/Price/BAO/LineItem.php +++ b/CRM/Price/BAO/LineItem.php @@ -1030,18 +1030,23 @@ protected function updateEntityRecordOnChangeFeeSelection($inputParams, $entityI * @return array $financialTrxn * */ - protected function getRelatedCancelFinancialTrxn($financialItemID) { - $financialTrxn = civicrm_api3('EntityFinancialTrxn', 'getsingle', array( - 'entity_table' => 'civicrm_financial_item', - 'entity_id' => $financialItemID, - 'options' => array( - 'sort' => 'id DESC', - 'limit' => 1, - ), - 'api.FinancialTrxn.getsingle' => array( - 'id' => "\$value.financial_trxn_id", - ), - )); + protected function _getRelatedCancelFinancialTrxn($financialItemID) { + try { + $financialTrxn = civicrm_api3('EntityFinancialTrxn', 'getsingle', array( + 'entity_table' => 'civicrm_financial_item', + 'entity_id' => $financialItemID, + 'options' => array( + 'sort' => 'id DESC', + 'limit' => 1, + ), + 'api.FinancialTrxn.getsingle' => array( + 'id' => "\$value.financial_trxn_id", + ), + )); + } + catch (CiviCRM_API3_Exception $e) { + return array(); + } $financialTrxn = array_merge($financialTrxn['api.FinancialTrxn.getsingle'], array( 'trxn_date' => date('YmdHis'), @@ -1122,6 +1127,12 @@ protected function _recordAdjustedAmt($updatedAmount, $contributionId, $taxAmoun ); $adjustedTrxn = CRM_Core_BAO_FinancialTrxn::create($adjustedTrxnValues); } + // CRM-17151: Update the contribution status to completed if balance is zero, + // because due to sucessive fee change will leave the related contribution status incorrect + else { + CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'contribution_status_id', $completedStatusId); + } + return $adjustedTrxn; } diff --git a/tests/phpunit/CRM/Event/BAO/CRM19273Test.php b/tests/phpunit/CRM/Event/BAO/ChangeFeeSelectionTest.php similarity index 87% rename from tests/phpunit/CRM/Event/BAO/CRM19273Test.php rename to tests/phpunit/CRM/Event/BAO/ChangeFeeSelectionTest.php index e38efb18bda5..894c6011975c 100644 --- a/tests/phpunit/CRM/Event/BAO/CRM19273Test.php +++ b/tests/phpunit/CRM/Event/BAO/ChangeFeeSelectionTest.php @@ -3,7 +3,7 @@ * Class CRM_Event_BAO_AdditionalPaymentTest * @group headless */ -class CRM_Event_BAO_CRM19273Test extends CiviUnitTestCase { +class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { protected $_priceSetID; protected $_cheapFee = 80; @@ -258,7 +258,7 @@ public function testCRM19273() { public function testCRM21245() { $this->registerParticipantAndPay(50); $partiallyPaidContribuitonStatus = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Partially paid'); - $this->assertEquals($this->callAPISuccessGetValue('Contribution', array('id' => $this->contributionID, 'return' => 'contribution_status_id')), $partiallyPaidContribuitonStatus); + $this->assertEquals($this->callAPISuccessGetValue('Contribution', array('id' => $this->_contributionId, 'return' => 'contribution_status_id')), $partiallyPaidContribuitonStatus); $priceSetParams['price_1'] = 3; $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant'); @@ -271,13 +271,13 @@ public function testCRM21245() { */ public function testCRM20611() { $priceSetParams['price_1'] = 1; - $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant'); - CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee); + $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant'); + CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem); $this->balanceCheck($this->_expensiveFee); $priceSetParams['price_1'] = 2; - $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant'); - CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee); + $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant'); + CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem); $this->balanceCheck($this->_cheapFee); //Complete the refund payment. @@ -419,4 +419,27 @@ public function testCRM21513() { $this->balanceCheck(20); } + /** + * CRM-17151: Test that Contribution status change to 'Completed' if balance is zero. + */ + public function testCRM17151() { + $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); + $partiallyPaidStatusId = array_search('Partially paid', $contributionStatuses); + $pendingRefundStatusId = array_search('Pending refund', $contributionStatuses); + $completedStatusId = array_search('Completed', $contributionStatuses); + $this->assertDBCompareValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'contribution_status_id', 'id', $completedStatusId, 'Payment t be completed'); + $priceSetParams['price_1'] = 2; + $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant'); + CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem); + $this->assertDBCompareValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'contribution_status_id', 'id', $pendingRefundStatusId, 'Contribution must be refunding'); + $priceSetParams['price_1'] = 1; + $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant'); + CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem); + $this->assertDBCompareValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'contribution_status_id', 'id', $completedStatusId, 'Contribution must, after complete payment be in state completed'); + $priceSetParams['price_1'] = 3; + $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant'); + CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->_participantId, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem); + $this->assertDBCompareValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'contribution_status_id', 'id', $partiallyPaidStatusId, 'Partial Paid'); + } + } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index be424eb7c622..6a720653c239 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -552,7 +552,7 @@ public function assertDBCompareValue( $expectedValue, $message ) { $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE); - $this->assertEquals($value, $expectedValue, $message); + $this->assertEquals($expectedValue, $value, $message); } /**