Pass specific ids rather than ids array
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 4 Sep 2023 00:38:51 +0000 (12:38 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 4 Sep 2023 05:00:45 +0000 (17:00 +1200)
It is unclear when we pass ids what ids we care about (& in general how many ids we are using from the
various functions). In fact 2 specific IDs are used (one from the _relatedObjects array) so pass
these in and stop passing in ids. Also stop setting an id that we never use

CRM/Contribute/BAO/Contribution.php
tests/phpunit/CRM/PCP/BAO/PCPTest.php

index 6495ed0031f857b3ac424dc20d61e152a0bb0f38..2a9eaccc6e7ddc514820d431f40255f173bbd6a1 100644 (file)
@@ -2533,9 +2533,11 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
 
     $this->loadRelatedObjects($paymentProcessorID, $ids);
     $paymentProcessor = $this->_relatedObjects['paymentProcessor'] ?? NULL;
+    $eventID = isset($ids['event']) ? (int) $ids['event'] : NULL;
+    $participantID = isset($ids['participant']) ? (int) $ids['participant'] : NULL;
 
     //not really sure what params might be passed in but lets merge em into values
-    $values = array_merge($this->_gatherMessageValues($input, $values, $ids), $values);
+    $values = array_merge($this->_gatherMessageValues($values, $eventID, $participantID), $values);
     $values['is_email_receipt'] = !$returnMessageText;
     foreach (['receipt_date', 'cc_receipt', 'bcc_receipt', 'receipt_from_name', 'receipt_from_email', 'receipt_text', 'pay_later_receipt'] as $fld) {
       if (!empty($input[$fld])) {
@@ -2569,7 +2571,6 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
     }
 
     if ($this->_component === 'event') {
-      $eventID = $this->_relatedObjects['participant']->event_id;
       $eventParams = ['id' => $eventID];
       $values['event'] = [];
 
@@ -2610,7 +2611,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
       }
 
       return CRM_Event_BAO_Event::sendMail($ids['contact'], $values,
-        $this->_relatedObjects['participant']->id, $this->is_test, $returnMessageText
+        $participantID, $this->is_test, $returnMessageText
       );
     }
     else {
@@ -2686,16 +2687,14 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
    * as part of CRM-9996 refactoring as a step towards simplifying the composeMessage function
    * Values related to the contribution in question are gathered
    *
-   * @param array $input
-   *   Input into function (probably from payment processor).
    * @param array $values
-   * @param array $ids
-   *   The set of ids related to the input.
+   * @param int|null $eventID
+   * @param int|null $participantID
    *
    * @return array
    * @throws \CRM_Core_Exception
    */
-  public function _gatherMessageValues($input, &$values, $ids = []) {
+  public function _gatherMessageValues($values, ?int $eventID, ?int $participantID) {
     // set display address of contributor
     $values['billingName'] = '';
     if ($this->address_id) {
@@ -2771,11 +2770,11 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
       );
       // if this is onbehalf of contribution then set related contact
       if (!empty($relatedContact['individual_id'])) {
-        $values['related_contact'] = $ids['related_contact'] = $relatedContact['individual_id'];
+        $values['related_contact'] = $relatedContact['individual_id'];
       }
     }
     else {
-      $values = array_merge($values, $this->loadEventMessageTemplateParams((int) $ids['event'], (int) $this->_relatedObjects['participant']->id, $this->id));
+      $values = array_merge($values, $this->loadEventMessageTemplateParams($eventID, $participantID, $this->id));
     }
 
     $groupTree = CRM_Core_BAO_CustomGroup::getTree('Contribution', NULL, $this->id);
index 3280d4042f2d129c2fae3afed0de378ab97c7faa..595274b91cf1023a6b73ce37df582efa5ca529ce 100644 (file)
@@ -234,24 +234,7 @@ class CRM_PCP_BAO_PCPTest extends CiviUnitTestCase {
       'receipt_from_email' => 'donationFake@civicrm.org',
       'contribution_status' => 'Completed',
     ];
-    $gathered_values = $contribution_bao->_gatherMessageValues(
-      [
-        'payment_processor_id' => $payment_processor['id'],
-        'is_email_receipt' => TRUE,
-      ],
-      $values,
-      [
-        'component' => 'contribute',
-        'contact_id' => $contact_contributor,
-        'contact' => $contact_contributor,
-        'financialType' => $contribution['values'][$contribution['id']]['financial_type_id'],
-        'contributionType' => $contribution['values'][$contribution['id']]['contribution_type_id'],
-        'contributionPage' => $contributionPage['id'],
-        'membership' => [],
-        'paymentProcessor' => $payment_processor['id'],
-        'contribution' => $contribution['id'],
-      ]
-    );
+    $gathered_values = $contribution_bao->_gatherMessageValues($values, NULL, NULL);
 
     $this->assertEquals([
       'receipt_from_name' => 'CiviCRM Fundraising Dept.',