From 0246fe6b3aa744f8a40ecba98ae5c16d284cf660 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 14 Oct 2023 18:11:53 +1300 Subject: [PATCH] php8.2 fix undeclared properties on backoffice contribution form There are just 2 properties _payNow is distinctly internal, and easily searched in universe so I made it private - _contributionID is specifically set for the purposes of hooks - although I expect that was more relevant when the code was shared with the front end form. However, I have set it up to still work for now, albeit with a deprecation notice --- CRM/Contribute/Form/Contribution.php | 18 ++++++++++++---- Civi/Test/FormTrait.php | 12 +++++++++++ Civi/Test/FormWrapper.php | 21 +++++++++++++++++++ .../CRM/Contribute/Form/ContributionTest.php | 5 ++++- 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index 6ce3a35c2e..10d7e5f55e 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -209,6 +209,11 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP */ public $payment_instrument_id; + /** + * @var bool + */ + private $_payNow; + /** * Explicitly declare the form context. */ @@ -216,6 +221,14 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP return 'create'; } + public function __get($name) { + if ($name === '_contributionID') { + CRM_Core_Error::deprecatedWarning('_contributionID is not a form property - use getContributionID()'); + return $this->getContributionID(); + } + return NULL; + } + /** * Set variables up before form is built. * @@ -306,7 +319,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP // Set title if ($this->_mode && $this->_id) { $this->_payNow = TRUE; - $this->assign('payNow', $this->_payNow); $this->setTitle(ts('Pay with Credit Card')); } elseif ($this->_values['is_template']) { @@ -318,6 +330,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP else { $this->setPageTitle($this->_ppID ? ts('Pledge Payment') : ts('Contribution')); } + $this->assign('payNow', $this->_payNow); } private function preProcessPledge(): void { @@ -1468,9 +1481,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP $smarty->assign('dataArray', $dataArray); $smarty->assign('totalTaxAmount', $params['tax_amount'] ?? NULL); } - - // lets store it in the form variable so postProcess hook can get to this and use it - $form->_contributionID = $contribution->id; } // process soft credit / pcp params first diff --git a/Civi/Test/FormTrait.php b/Civi/Test/FormTrait.php index 1306b43d33..a194768c15 100644 --- a/Civi/Test/FormTrait.php +++ b/Civi/Test/FormTrait.php @@ -81,4 +81,16 @@ trait FormTrait { } } + /** + * Retrieve a deprecated property, ensuring a deprecation notice is thrown. + * + * @param string $property + * + * @return mixed + * @throws \CRM_Core_Exception + */ + protected function getDeprecatedProperty(string $property) { + return $this->form->getDeprecatedProperty($property); + } + } diff --git a/Civi/Test/FormWrapper.php b/Civi/Test/FormWrapper.php index b807a3880c..0989d4c09d 100644 --- a/Civi/Test/FormWrapper.php +++ b/Civi/Test/FormWrapper.php @@ -348,4 +348,25 @@ class FormWrapper { \Civi::settings()->set('mailing_backend', $this->originalMailSetting); } + /** + * Retrieve a deprecated property, ensuring a deprecation notice is thrown. + * + * @param string $property + * + * @return mixed + * @throws \CRM_Core_Exception + */ + public function getDeprecatedProperty(string $property) { + try { + $this->form->$property; + } + catch (\Exception $e) { + $oldErrorLevel = error_reporting(0); + $value = $this->form->$property; + error_reporting($oldErrorLevel); + return $value; + } + throw new \CRM_Core_Exception('Deprecation should have been triggered'); + } + } diff --git a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php index 12fe7b8106..95e33074b9 100644 --- a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php @@ -820,6 +820,8 @@ Receipt Date: ' . date('m/d/Y'), /** * Test the submit function on the contribution page. + * + * @throws \CRM_Core_Exception */ public function testSubmitWithNoteCreditCard(): void { $this->submitContributionForm([ @@ -830,9 +832,10 @@ Receipt Date: ' . date('m/d/Y'), 'contribution_status_id' => 1, 'note' => 'Super cool and interesting stuff', ] + $this->getCreditCardParams()); - $this->callAPISuccessGetCount('Contribution', ['contact_id' => $this->_individualId], 1); + $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $this->_individualId]); $note = $this->callAPISuccessGetSingle('note', ['entity_table' => 'civicrm_contribution']); $this->assertEquals('Super cool and interesting stuff', $note['note']); + $this->assertEquals($contribution['id'], $this->getDeprecatedProperty('_contributionID')); } /** -- 2.25.1