From 2c0f8c6797d810bc6a3202b7d0c18e762f5f22ec Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Wed, 21 Jun 2017 10:27:09 +0530 Subject: [PATCH] CRM-20752: Editing a Cancelled Pledge updates the status of it to Pending --- CRM/Pledge/BAO/PledgePayment.php | 6 +++ tests/phpunit/api/v3/PledgePaymentTest.php | 48 ++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/CRM/Pledge/BAO/PledgePayment.php b/CRM/Pledge/BAO/PledgePayment.php index 484f2296a5..18113b1a2e 100644 --- a/CRM/Pledge/BAO/PledgePayment.php +++ b/CRM/Pledge/BAO/PledgePayment.php @@ -604,6 +604,12 @@ WHERE civicrm_pledge.id = %2 public static function calculatePledgeStatus($pledgeId) { $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); + //return if the pledge is cancelled. + $currentPledgeStatus = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $pledgeId, 'status_id', 'id', TRUE); + if ($currentPledgeStatus == array_search('Cancelled', $paymentStatusTypes)) { + return $currentPledgeStatus; + } + // retrieve all pledge payments for this particular pledge $allPledgePayments = $allStatus = array(); $returnProperties = array('status_id'); diff --git a/tests/phpunit/api/v3/PledgePaymentTest.php b/tests/phpunit/api/v3/PledgePaymentTest.php index 084e447644..8b03ae5d18 100644 --- a/tests/phpunit/api/v3/PledgePaymentTest.php +++ b/tests/phpunit/api/v3/PledgePaymentTest.php @@ -87,6 +87,54 @@ class api_v3_PledgePaymentTest extends CiviUnitTestCase { $this->assertEquals(1, $result['count'], " in line " . __LINE__); } + /** + * Test status of pledge on payments and cancellation. + */ + public function testPledgeStatus() { + //Status should initially be Pending. + $checkStatus = $this->callAPISuccess('pledge', 'getsingle', array( + 'id' => $this->_pledgeID, + 'return' => 'pledge_status', + )); + $this->assertEquals('Pending', $checkStatus['pledge_status']); + + //Make first payment. + $paymentParams = array( + 'contact_id' => $this->_individualId, + 'pledge_id' => $this->_pledgeID, + 'contribution_id' => $this->_contributionID, + 'status_id' => 1, + ); + $firstPayment = $this->callAPISuccess('pledge_payment', 'create', $paymentParams); + + //Status should be 'In Progress' after first payment. + $checkStatus = $this->callAPISuccess('pledge', 'getsingle', array( + 'id' => $this->_pledgeID, + 'return' => 'pledge_status', + )); + $this->assertEquals('In Progress', $checkStatus['pledge_status']); + + //Cancel the Pledge. + $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); + $updateParams = array( + 'id' => $this->_pledgeID, + 'status_id' => array_search('Cancelled', $paymentStatusTypes), + ); + $this->callAPISuccess('pledge', 'create', $updateParams); + + //Status should be calculated as Cancelled. + $pledgeStatus = CRM_Pledge_BAO_PledgePayment::calculatePledgeStatus($this->_pledgeID); + $this->assertEquals('Cancelled', $paymentStatusTypes[$pledgeStatus]); + + //Already completed payments should not be cancelled. + $checkPaymentStatus = $this->callAPISuccess('pledge_payment', 'getsingle', array( + 'id' => $firstPayment['id'], + 'return' => 'status_id', + )); + $this->assertEquals(array_search('Completed', $paymentStatusTypes), $checkPaymentStatus['status_id']); + } + + /** * Test that passing in a single variable works:: status_id */ -- 2.25.1