From d0a93d4dfc2a771b095f26fa33eb5551d6c9dcdb Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 3 Jan 2023 09:43:04 +1300 Subject: [PATCH] Move part of deprecated function loadRelatedObjects to calling functions --- CRM/Contribute/BAO/Contribution.php | 69 +++++++++++-------- .../phpunit/CRM/Core/Payment/BaseIPNTest.php | 5 +- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index b9b696e4d7..ebc8c447f3 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -1833,7 +1833,22 @@ LEFT JOIN civicrm_contribution contribution ON ( componentPayment.contribution_ } } - $contribution->loadRelatedObjects($input, $ids); + $paymentProcessorID = CRM_Utils_Array::value('payment_processor_id', $input, CRM_Utils_Array::value( + 'paymentProcessor', + $ids + )); + + if (!isset($input['payment_processor_id']) && !$paymentProcessorID && $contribution->contribution_page_id) { + $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', + $contribution->contribution_page_id, + 'payment_processor' + ); + if ($paymentProcessorID) { + $intentionalEnotice = $CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage; + } + } + + $contribution->loadRelatedObjects($paymentProcessorID, $ids); $memberships = $contribution->_relatedObjects['membership'] ?? []; $participant = $contribution->_relatedObjects['participant'] ?? []; @@ -2310,38 +2325,20 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac * * Note that the unit test for the BaseIPN class tests this function * - * @param array $input - * Input as delivered from Payment Processor. + * @param int $paymentProcessorID + * Payment Processor ID. * @param array $ids * Ids as Loaded by Payment Processor. * * @return bool * @throws CRM_Core_Exception */ - public function loadRelatedObjects($input, &$ids) { - // @todo deprecate this function - the steps should be - // 1) add additional functions like 'getRelatedMemberships' - // 2) switch all calls that refer to ->_relatedObjects to - // using the helper functions - // 3) make ->_relatedObjects noisy in some way (deprecation won't work for properties - hmm - // 4) make ->_relatedObjects protected - // 5) hone up the individual functions to not use rely on this having been called - // 6) deprecate like mad - - $paymentProcessorID = CRM_Utils_Array::value('payment_processor_id', $input, CRM_Utils_Array::value( - 'paymentProcessor', - $ids - )); - - if (!isset($input['payment_processor_id']) && !$paymentProcessorID && $this->contribution_page_id) { - $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', - $this->contribution_page_id, - 'payment_processor' - ); - if ($paymentProcessorID) { - $intentionalEnotice = $CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage; - } - } + public function loadRelatedObjects($paymentProcessorID, &$ids) { + // @todo deprecate this function - we are slowly returning the functionality to + // the calling functions so this can be unravelled. It is only called from + // tests, composeMessage & transitionComponents. The last of these is itself + // deprecated, to be replaced by using Payment.create. Many parts of this are + // used by only one, or neither, of the actual calling functions. $ids['contributionType'] = $this->financial_type_id; $ids['financialType'] = $this->financial_type_id; @@ -2468,7 +2465,23 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac if (empty($this->financial_type_id)) { $this->find(TRUE); } - $this->loadRelatedObjects($input, $ids); + + $paymentProcessorID = CRM_Utils_Array::value('payment_processor_id', $input, CRM_Utils_Array::value( + 'paymentProcessor', + $ids + )); + + if (!isset($input['payment_processor_id']) && !$paymentProcessorID && $this->contribution_page_id) { + $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', + $this->contribution_page_id, + 'payment_processor' + ); + if ($paymentProcessorID) { + $intentionalEnotice = $CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage; + } + } + + $this->loadRelatedObjects($paymentProcessorID, $ids); if (empty($this->_component)) { $this->_component = $input['component'] ?? NULL; diff --git a/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php b/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php index 75b91fadf7..5d7ff4d8f4 100644 --- a/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php +++ b/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php @@ -106,7 +106,8 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { $contribution->find(TRUE); $contribution->_component = 'contribute'; $ids = array_merge(CRM_Contribute_BAO_Contribution::getComponentDetails($this->_contributionId), $this->ids); - $contribution->loadRelatedObjects($this->input, $ids); + + $contribution->loadRelatedObjects($this->_processorId, $ids); $this->assertNotEmpty($contribution->_relatedObjects['membership']); $this->assertArrayHasKey($this->_membershipId . '_' . $this->_membershipTypeID, $contribution->_relatedObjects['membership']); $this->assertTrue(is_a($contribution->_relatedObjects['membership'][$this->_membershipId . '_' . $this->_membershipTypeID], 'CRM_Member_BAO_Membership')); @@ -185,7 +186,7 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { $this->_setUpParticipantObjects(); $contribution = new CRM_Contribute_BAO_Contribution(); $contribution->id = $this->_contributionId; - $contribution->loadRelatedObjects($this->input, $this->ids); + $contribution->loadRelatedObjects($this->_processorId, $this->ids); $msg = $contribution->composeMessageArray($this->input, $this->ids, $values); $this->assertStringContainsString('registration has been received and your status has been updated to Attended.', $msg['body']); $this->assertStringContainsString('Annual CiviCRM meet', $msg['html']); -- 2.25.1