Merge pull request #11642 from JKingsnorth/CRM-21739
[civicrm-core.git] / tests / phpunit / api / v3 / PledgePaymentTest.php
index 084e447644677cff6fde4f017197a29a06de55e1..c1e36869983ff66fe7109b623c81c4171ee9cdcb 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2017                                |
+ | Copyright CiviCRM LLC (c) 2004-2018                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -87,6 +87,89 @@ class api_v3_PledgePaymentTest extends CiviUnitTestCase {
     $this->assertEquals(1, $result['count'], " in line " . __LINE__);
   }
 
+  /**
+   * Test process_pledge job log.
+   */
+  public function testProcessPledgeJob() {
+    $pledgeStatuses = CRM_Core_OptionGroup::values('pledge_status',
+      FALSE, FALSE, FALSE, NULL, 'name'
+    );
+    //Make first payment.
+    $paymentParams = array(
+      'contact_id' => $this->_individualId,
+      'pledge_id' => $this->_pledgeID,
+      'contribution_id' => $this->_contributionID,
+      'scheduled_date' => date('Ymd', strtotime("-1 days")),
+      'status_id' => array_search('Pending', $pledgeStatuses),
+    );
+    $firstPayment = $this->callAPISuccess('pledge_payment', 'create', $paymentParams);
+    //Status should be 'Pending' after first incomplete payment.
+    $checkStatus = $this->callAPISuccess('pledge', 'getsingle', array(
+      'id' => $this->_pledgeID,
+      'return' => 'pledge_status',
+    ));
+    $this->assertEquals('Pending', $checkStatus['pledge_status']);
+
+    //Execute process_pledge job log.
+    $result = $this->callAPISuccess('Job', 'process_pledge', array());
+    $this->assertEquals("Checking if status update is needed for Pledge Id: {$this->_pledgeID} (current status is Pending)\n\r- status updated to: Overdue\n\r1 records updated.", $result['values']);
+
+    //Status should be 'Overdue' after processing.
+    $statusAfterProcessing = $this->callAPISuccess('pledge', 'getsingle', array(
+      'id' => $this->_pledgeID,
+      'return' => 'pledge_status',
+    ));
+    $this->assertEquals('Overdue', $statusAfterProcessing['pledge_status']);
+  }
+
+  /**
+   * 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
    */