From 8256753d0077e6003cb23825522ef875d70691be Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 9 Aug 2022 13:49:17 +1200 Subject: [PATCH 1/2] Move a little code from --- CRM/Contribute/BAO/Contribution.php | 38 ++++++++++++------- CRM/Contribute/Form/Task/Invoice.php | 10 +++++ .../phpunit/CRM/Core/Payment/BaseIPNTest.php | 1 + 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index ccd3b85223da..57aa175402b0 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -1830,6 +1830,15 @@ public static function transitionComponents($params) { $contribution->id = $ids['contribution']; $contribution->find(); + if (empty($contribution->_component)) { + if (!empty($ids['event'])) { + $contribution->_component = 'event'; + } + else { + $contribution->_component = strtolower(CRM_Utils_Array::value('component', $input, 'contribute')); + } + } + $contribution->loadRelatedObjects($input, $ids); $memberships = $contribution->_relatedObjects['membership'] ?? []; @@ -2324,20 +2333,6 @@ public function loadRelatedObjects($input, &$ids) { // 4) make ->_relatedObjects protected // 5) hone up the individual functions to not use rely on this having been called // 6) deprecate like mad - if (empty($this->_component)) { - if (!empty($ids['event'])) { - $this->_component = 'event'; - } - else { - $this->_component = strtolower(CRM_Utils_Array::value('component', $input, 'contribute')); - } - } - - // If the object is not fully populated then make sure it is - this is a more about legacy paths & cautious - // refactoring than anything else, and has unit test coverage. - if (empty($this->financial_type_id)) { - $this->find(TRUE); - } $paymentProcessorID = CRM_Utils_Array::value('payment_processor_id', $input, CRM_Utils_Array::value( 'paymentProcessor', @@ -2463,6 +2458,21 @@ public function composeMessageArray(&$input, &$ids, &$values, $returnMessageText if (empty($ids['contact']) && isset($this->contact_id)) { $ids['contact'] = $this->contact_id; } + + if (empty($this->_component)) { + if (!empty($ids['event'])) { + $this->_component = 'event'; + } + else { + $this->_component = strtolower(CRM_Utils_Array::value('component', $input, 'contribute')); + } + } + + // If the object is not fully populated then make sure it is - this is a more about legacy paths & cautious + // refactoring than anything else, and has unit test coverage. + if (empty($this->financial_type_id)) { + $this->find(TRUE); + } $this->loadRelatedObjects($input, $ids); if (empty($this->_component)) { diff --git a/CRM/Contribute/Form/Task/Invoice.php b/CRM/Contribute/Form/Task/Invoice.php index 0451bd6c8936..13eb2721cb93 100644 --- a/CRM/Contribute/Form/Task/Invoice.php +++ b/CRM/Contribute/Form/Task/Invoice.php @@ -244,6 +244,16 @@ public static function printPDF($contribIDs, &$params, $contactIds) { // and calls deprecated code. If we decide a contribution title is a // 'real thing' then we should create a token. $ids = array_merge(CRM_Contribute_BAO_Contribution::getComponentDetails($contributionID), $ids); + + if (empty($contribution->_component)) { + if (!empty($ids['event'])) { + $contribution->_component = 'event'; + } + else { + $contribution->_component = strtolower(CRM_Utils_Array::value('component', $input, 'contribute')); + } + } + $contribution->loadRelatedObjects($input, $ids); $input['amount'] = $contribution->total_amount; diff --git a/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php b/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php index ea963d511511..a68f1c700222 100644 --- a/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php +++ b/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php @@ -114,6 +114,7 @@ public function testLoadMembershipObjectsLoadAll() { $contribution = new CRM_Contribute_BAO_Contribution(); $contribution->id = $this->_contributionId; $contribution->find(TRUE); + $contribution->_component = 'contribute'; $ids = array_merge(CRM_Contribute_BAO_Contribution::getComponentDetails($this->_contributionId), $this->ids); $contribution->loadRelatedObjects($this->input, $ids); $this->assertNotEmpty($contribution->_relatedObjects['membership']); From 219ac01b5e65fbd88ce56edf90262b7a1bdabb1f Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 9 Aug 2022 20:08:30 +1200 Subject: [PATCH 2/2] It seems we need find(TRUE) to reliably load --- CRM/Contribute/BAO/Contribution.php | 2 +- CRM/Contribute/Form/Task/Invoice.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 57aa175402b0..2f393e51206b 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -1828,7 +1828,7 @@ public static function transitionComponents($params) { $contribution = new CRM_Contribute_BAO_Contribution(); $contribution->id = $ids['contribution']; - $contribution->find(); + $contribution->find(TRUE); if (empty($contribution->_component)) { if (!empty($ids['event'])) { diff --git a/CRM/Contribute/Form/Task/Invoice.php b/CRM/Contribute/Form/Task/Invoice.php index 13eb2721cb93..82aa6996707f 100644 --- a/CRM/Contribute/Form/Task/Invoice.php +++ b/CRM/Contribute/Form/Task/Invoice.php @@ -239,7 +239,7 @@ public static function printPDF($contribIDs, &$params, $contactIds) { $contribution = new CRM_Contribute_BAO_Contribution(); $contribution->id = $contributionID; - $contribution->fetch(); + $contribution->find(TRUE); // @todo this is only used now to load the event title, it causes an enotice // and calls deprecated code. If we decide a contribution title is a // 'real thing' then we should create a token.