Skip to content

Commit

Permalink
Add test + external support for getAuthenticatedContactID()
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Aug 12, 2023
1 parent 1841b63 commit a4bbe01
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions CRM/Contact/BAO/Contact/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
Expand Down
4 changes: 4 additions & 0 deletions CRM/Core/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions tests/phpunit/CRM/Core/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}

0 comments on commit a4bbe01

Please sign in to comment.