CRM-16367 (backoffice Contribution page) testability refactoring
authorEileen McNaughton <eileen@fuzion.co.nz>
Thu, 30 Apr 2015 02:42:07 +0000 (20:42 -0600)
committerEileen McNaughton <eileen@fuzion.co.nz>
Thu, 30 Apr 2015 02:42:07 +0000 (20:42 -0600)
CRM/Contribute/Form/Contribution.php
tests/phpunit/CRM/Contribute/Form/ContributionTest.php

index f7fce622e5f2f4e2ad92b8a1baf3314f54a2b6a6..d705c5717c2221b29fe98aaded6facdb47654231 100644 (file)
@@ -69,13 +69,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
    */
   public $_ppID;
 
-  /**
-   * The id of the pledge that we are processing.
-   *
-   * @var int
-   */
-  public $_pledgeID;
-
   /**
    * Is this contribution associated with an online.
    * financial transaction
@@ -989,7 +982,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
 
     // Get the submitted form values.
     $submittedValues = $this->controller->exportValues($this->_name);
-    $contribution = $this->submit($submittedValues, $this->_action);
+    $contribution = $this->submit($submittedValues, $this->_action, $this->_ppID);
     $session = CRM_Core_Session::singleton();
     $buttonName = $this->controller->getButtonName();
     if ($this->_context == 'standalone') {
@@ -1029,7 +1022,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
    *
    * @throws CRM_Core_Exception
    */
-  protected function processCreditCard($submittedValues, $lineItem) {
+  protected function processCreditCard($submittedValues, $lineItem, $pledgePaymentID) {
     $sendReceipt = $contribution = FALSE;
 
     $unsetParams = array(
@@ -1304,7 +1297,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       // Store contribution id in payment record.
       CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID, 'contribution_id', $contribution->id);
 
-      CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($this->_pledgeID,
+      CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID,
         array($this->_ppID),
         $contribution->contribution_status_id,
         NULL,
@@ -1419,7 +1412,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       'cancel_date_time' => '',
     );
 
-    $this->submit(array_merge($defaults, $params), $action);
+    $this->submit(array_merge($defaults, $params), $action, CRM_Utils_Array::value('pledge_payment_id', $params));
   }
 
   /**
@@ -1432,7 +1425,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
    * @return array
    * @throws \Exception
    */
-  protected function submit($submittedValues, $action) {
+  protected function submit($submittedValues, $action, $pledgePaymentID) {
     $softParams = $softIDs = array();
     $pId = $contribution = $isRelatedId = FALSE;
     if (!empty($submittedValues['price_set_id']) && $action & CRM_Core_Action::UPDATE) {
@@ -1602,7 +1595,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
 
     // Credit Card Contribution.
     if ($this->_mode) {
-      $this->processCreditCard($submittedValues, $lineItem);
+      $this->processCreditCard($submittedValues, $lineItem, $pledgePaymentID);
       return FALSE;
     }
     else {
@@ -1834,26 +1827,26 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       );
 
       //update pledge payment status.
-      if ((($this->_ppID && $contribution->id) && $action & CRM_Core_Action::ADD) ||
+      if ((($pledgePaymentID && $contribution->id) && $action & CRM_Core_Action::ADD) ||
         (($pledgePaymentId) && $action & CRM_Core_Action::UPDATE)
       ) {
 
-        if ($this->_ppID) {
+        if ($pledgePaymentID) {
           //store contribution id in payment record.
-          CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID, 'contribution_id', $contribution->id);
+          CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $pledgePaymentID, 'contribution_id', $contribution->id);
         }
         else {
-          $this->_ppID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
+          $pledgePaymentID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
             $contribution->id,
             'id',
             'contribution_id'
           );
-          $this->_pledgeID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
-            $contribution->id,
-            'pledge_id',
-            'contribution_id'
-          );
         }
+        $pledgeID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
+          $contribution->id,
+          'pledge_id',
+          'contribution_id'
+        );
 
         $adjustTotalAmount = FALSE;
         if (CRM_Utils_Array::value('option_type', $formValues) == 2) {
@@ -1872,8 +1865,8 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
         }
 
         if ($updatePledgePaymentStatus) {
-          CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($this->_pledgeID,
-            array($this->_ppID),
+          CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID,
+            array($pledgePaymentID),
             $contribution->contribution_status_id,
             NULL,
             $contribution->total_amount,
index 7eea8f1d77d98f4f3632401ea32155653581495b..2945c77e5cf47f22848ee8e0f4f37a1d90eee1a9 100644 (file)
@@ -135,7 +135,8 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase {
       'receive_date_time' => '11:27PM',
       'contact_id' => $this->_individualId,
       'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
-    ));
+    ),
+      CRM_Core_Action::ADD);
     $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
   }
 
@@ -151,7 +152,7 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase {
       'receive_date_time' => '11:27PM',
       'contact_id' => $this->_individualId,
       'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
-    ));
+    ), CRM_Core_Action::ADD);
     $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
   }
 
@@ -170,7 +171,7 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase {
       'contact_id' => $this->_individualId,
       'is_email_receipt' => TRUE,
       'from_email_address' => 'test@test.com',
-    ));
+    ), CRM_Core_Action::ADD);
     $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
     $mut->checkMailLog(array(
         '<p>Please print this receipt for your records.</p>',
@@ -179,4 +180,42 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase {
     $mut->stop();
   }
 
+  /**
+   * Test that a contribution is assigned against a pledge.
+   */
+  public function testUpdatePledge() {
+    $pledge = $this->callAPISuccess('pledge', 'create', array(
+      'contact_id' => $this->_individualId,
+      'pledge_create_date' => date('Ymd'),
+      'start_date' => date('Ymd'),
+      'amount' => 100.00,
+      'pledge_status_id' => '2',
+      'pledge_financial_type_id' => '1',
+      'pledge_original_installment_amount' => 20,
+      'frequency_interval' => 5,
+      'frequency_unit' => 'year',
+      'frequency_day' => 15,
+      'installments' => 2,
+      'sequential' => 1,
+    ));
+    $pledgePaymentID = $this->callAPISuccess('pledge_payment', 'getvalue', array(
+      'pledge_id' => $pledge['id'],
+      'options' => array('limit' => 1),
+      'return' => 'id',
+    ));
+    $form = new CRM_Contribute_Form_Contribution();
+    $form->testSubmit(array(
+      'total_amount' => 50,
+      'financial_type_id' => 1,
+      'receive_date' => '04/21/2015',
+      'receive_date_time' => '11:27PM',
+      'contact_id' => $this->_individualId,
+      'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
+      'pledge_payment_id' => $pledgePaymentID,
+    ), CRM_Core_Action::ADD);
+    $pledgePayment = $this->callAPISuccess('pledge_payment', 'getsingle', array('id' => $pledgePaymentID));
+    $this->assertNotEmpty($pledgePayment['contribution_id']);
+    $this->assertEquals($pledgePayment['actual_amount'], 50);
+    $this->assertEquals(1, $pledgePayment['status_id']);
+  }
 }