From a4bbe01dfa913fca409316f67234897c9695e771 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 13 Aug 2023 09:54:29 +1200 Subject: [PATCH] Add test + external support for getAuthenticatedContactID() --- CRM/Contact/BAO/Contact/Utils.php | 4 ++-- CRM/Core/Form.php | 4 ++++ tests/phpunit/CRM/Core/FormTest.php | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/Contact/Utils.php b/CRM/Contact/BAO/Contact/Utils.php index 7e151c35e2..2b972e77f3 100644 --- a/CRM/Contact/BAO/Contact/Utils.php +++ b/CRM/Contact/BAO/Contact/Utils.php @@ -148,12 +148,12 @@ WHERE id IN ( $idString ) } if (!$hash) { - if ($entityType == 'contact') { + if ($entityType === 'contact') { $hash = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $entityId, 'hash' ); } - elseif ($entityType == 'mailing') { + elseif ($entityType === 'mailing') { $hash = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing', $entityId, 'hash' ); diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index eac61b71b1..4f2b055158 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -2478,6 +2478,10 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * - the logged in user * - 0 for none. * + * @api This function will not change in a minor release and is supported for + * use outside of core. This annotation / external support for properties + * is only given where there is specific test cover. + * * @return int * * @throws \CRM_Core_Exception diff --git a/tests/phpunit/CRM/Core/FormTest.php b/tests/phpunit/CRM/Core/FormTest.php index fe79745b04..baa41b46a8 100644 --- a/tests/phpunit/CRM/Core/FormTest.php +++ b/tests/phpunit/CRM/Core/FormTest.php @@ -109,4 +109,26 @@ class CRM_Core_FormTest extends CiviUnitTestCase { $this->callAPISuccess('PriceSet', 'delete', ['id' => $priceSetId]); } + /** + * Test the getAuthenticatedUser function. + * + * It should return a checksum validated user, falling back to the logged in user. + * + * @throws \CRM_Core_Exception + */ + public function testGetAuthenticatedUser(): void { + $_REQUEST['cid'] = $this->individualCreate(); + $_REQUEST['cs'] = CRM_Contact_BAO_Contact_Utils::generateChecksum($_REQUEST['cid']); + $form = $this->getFormObject('CRM_Core_Form'); + $this->assertEquals($_REQUEST['cid'], $form->getAuthenticatedContactID()); + + $_REQUEST['cs'] = 'abc'; + $form = $this->getFormObject('CRM_Core_Form'); + $this->assertEquals(0, $form->getAuthenticatedContactID()); + + $form = $this->getFormObject('CRM_Core_Form'); + $this->createLoggedInUser(); + $this->assertEquals($this->ids['Contact']['logged_in'], $form->getAuthenticatedContactID()); + } + } -- 2.25.1