CRM-20752: Editing a Cancelled Pledge updates the status of it to Pending
authorJitendra Purohit <jitendra@fuzion.co.nz>
Wed, 21 Jun 2017 04:57:09 +0000 (10:27 +0530)
committerJitendra Purohit <jitendra@fuzion.co.nz>
Wed, 21 Jun 2017 05:02:49 +0000 (10:32 +0530)
CRM/Pledge/BAO/PledgePayment.php
tests/phpunit/api/v3/PledgePaymentTest.php

index 484f2296a5a02aeab22ed4a20fec72dadc53bbac..18113b1a2e6c547ba17954f1697587f140fda8c3 100644 (file)
@@ -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');
index 084e447644677cff6fde4f017197a29a06de55e1..8b03ae5d1837334e36c00a0fd818ce02813411c1 100644 (file)
@@ -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
    */