Cleanup input & output on paymentActivityCreate & improve test
authoreileen <emcnaughton@wikmedia.org>
Mon, 20 May 2019 02:21:41 +0000 (14:21 +1200)
committereileen <emcnaughton@wikmedia.org>
Mon, 20 May 2019 02:45:24 +0000 (14:45 +1200)
This also switches over to the api, per todo, but without the potential cleanup that
enables as yet.

CRM/Contribute/BAO/Contribution.php
tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php

index d5f7a0b76e0c8a0b379ba2a67e308960bafeaa34..6511e166cae342d20499874f55bda357a5282cc6 100644 (file)
@@ -885,14 +885,24 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
   }
 
   /**
-   * @param $contributionId
-   * @param $participantId
-   * @param array $financialTrxn
+   * Record an activity when a payment is received.
+   *
+   * @todo this is intended to be moved to payment BAO class as a protected function
+   * on that class. Currently being cleaned up. The addActivityForPayment doesn't really
+   * merit it's own function as it makes the code less rather than more readable.
+   *
+   * @param int $contributionId
+   * @param int $participantId
+   * @param string $totalAmount
+   * @param string $currency
+   * @param string $trxnDate
    *
-   * @param $financialTrxn
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
-  protected static function recordPaymentActivity($contributionId, $participantId, $financialTrxn) {
-    $activityType = ($financialTrxn->total_amount < 0) ? 'Refund' : 'Payment';
+  protected static function recordPaymentActivity($contributionId, $participantId, $totalAmount, $currency, $trxnDate) {
+    $activityType = ($totalAmount < 0) ? 'Refund' : 'Payment';
+
     if ($participantId) {
       $inputParams['id'] = $participantId;
       $values = [];
@@ -907,8 +917,8 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
       $entityObj->find(TRUE);
       $title = ts('Contribution');
     }
-
-    self::addActivityForPayment($entityObj->contact_id, $financialTrxn, $activityType, $title, $contributionId);
+    // @todo per block above this is not a logical splitting off of functionality.
+    self::addActivityForPayment($entityObj->contact_id, $activityType, $title, $contributionId, $totalAmount, $currency, $trxnDate);
   }
 
   /**
@@ -3935,7 +3945,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
     }
 
     if (!empty($financialTrxn)) {
-      self::recordPaymentActivity($contributionId, $participantId, $financialTrxn);
+      self::recordPaymentActivity($contributionId, $participantId, $financialTrxn->total_amount, $financialTrxn->currency, $financialTrxn->trxn_date);
       return $financialTrxn;
     }
 
@@ -3943,17 +3953,20 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
 
   /**
    * @param int $targetCid
-   * @param $trxnObj
    * @param $activityType
    * @param string $title
    * @param int $contributionId
+   * @param string $totalAmount
+   * @param string $currency
+   * @param string $trxn_date
    *
-   * @throws CRM_Core_Exception
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
-  public static function addActivityForPayment($targetCid, $trxnObj, $activityType, $title, $contributionId) {
-    $paymentAmount = CRM_Utils_Money::format($trxnObj->total_amount, $trxnObj->currency);
+  public static function addActivityForPayment($targetCid, $activityType, $title, $contributionId, $totalAmount, $currency, $trxn_date) {
+    $paymentAmount = CRM_Utils_Money::format($totalAmount, $currency);
     $subject = "{$paymentAmount} - Offline {$activityType} for {$title}";
-    $date = CRM_Utils_Date::isoToMysql($trxnObj->trxn_date);
+    $date = CRM_Utils_Date::isoToMysql($trxn_date);
     // source record id would be the contribution id
     $srcRecId = $contributionId;
 
@@ -3975,8 +3988,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
       $activityParams['source_contact_id'] = $id;
       $activityParams['target_contact_id'][] = $targetCid;
     }
-    // @todo use api.
-    CRM_Activity_BAO_Activity::create($activityParams);
+    civicrm_api3('Activity', 'create', $activityParams);
   }
 
   /**
index 7143f3b4062cfe0148e5f8bcf5c88ccae6f5b3e6..efedd30dfd75fcb36e1d15177d4d41c7c27b3b6e 100644 (file)
@@ -168,7 +168,7 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
   /**
    * Test the submit function that completes the partially paid Contribution with multiple payments.
    */
-  public function testMultiplePaymentForPartialyPaidContribution() {
+  public function testMultiplePaymentForPartiallyPaidContribution() {
     $this->createContribution('Partially paid');
 
     // pay additional amount
@@ -184,10 +184,14 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
       'activity_type_id' => 'Payment',
       'options' => ['sort' => 'id'],
       'sequential' => 1,
+      'return' => ['target_contact_id', 'assignee_contact_id', 'subject'],
     ])['values'];
     $this->assertEquals(2, count($activities));
     $this->assertEquals('$ 50.00 - Offline Payment for Contribution', $activities[0]['subject']);
     $this->assertEquals('$ 20.00 - Offline Payment for Contribution', $activities[1]['subject']);
+    $this->assertEquals(CRM_Core_Session::singleton()->getLoggedInContactID(), $activities[0]['source_contact_id']);
+    $this->assertEquals([$this->_individualId], $activities[0]['target_contact_id']);
+    $this->assertEquals([], $activities[0]['assignee_contact_id']);
   }
 
   /**