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 7e151c35e274..2b972e77f37f 100644 --- a/CRM/Contact/BAO/Contact/Utils.php +++ b/CRM/Contact/BAO/Contact/Utils.php @@ -148,12 +148,12 @@ public static function generateChecksum($entityId, $ts = NULL, $live = NULL, $ha } 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 eac61b71b1cd..4f2b05515824 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -2478,6 +2478,10 @@ public function getRequestedContactID(): ?int { * - 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 fe79745b0484..baa41b46a804 100644 --- a/tests/phpunit/CRM/Core/FormTest.php +++ b/tests/phpunit/CRM/Core/FormTest.php @@ -109,4 +109,26 @@ public function testNewPriceField(): void { $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()); + } + }