From: Patrick Figel Date: Sun, 3 Dec 2023 11:27:41 +0000 (+0000) Subject: dev/core#2998 - Fix standalone throwing exception when fetching unknown user X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=de99de653f7d18c59e920c6b31c55e6d28c776bf;p=civicrm-core.git dev/core#2998 - Fix standalone throwing exception when fetching unknown user This fixes an issue where getUserIDFromUsername() was throwing an exception when fetching unknown users, rather than return NULL. --- diff --git a/ext/standaloneusers/Civi/Standalone/Security.php b/ext/standaloneusers/Civi/Standalone/Security.php index 587f1f504e..34b4efca4e 100644 --- a/ext/standaloneusers/Civi/Standalone/Security.php +++ b/ext/standaloneusers/Civi/Standalone/Security.php @@ -81,7 +81,7 @@ class Security { return \Civi\Api4\User::get(FALSE) ->addWhere('username', '=', $username) ->execute() - ->single()['id'] ?? NULL; + ->first()['id'] ?? NULL; } /** diff --git a/ext/standaloneusers/tests/phpunit/Civi/Standalone/SecurityTest.php b/ext/standaloneusers/tests/phpunit/Civi/Standalone/SecurityTest.php index d22c669a22..a63ec2c986 100644 --- a/ext/standaloneusers/tests/phpunit/Civi/Standalone/SecurityTest.php +++ b/ext/standaloneusers/tests/phpunit/Civi/Standalone/SecurityTest.php @@ -426,6 +426,12 @@ class SecurityTest extends \PHPUnit\Framework\TestCase implements EndToEndInterf $this->assertEquals('Password reset link for Demonstrators Anonymous', $result['subject']); } + public function testGetUserIDFromUsername() { + [$contactID, $adminUserID, $security] = $this->createFixtureContactAndUser(); + $this->assertEquals($adminUserID, $security->getUserIDFromUsername('user_one'), 'Should return admin user ID'); + $this->assertNull($security->getUserIDFromUsername('user_unknown'), 'Should return NULL for non-existent user'); + } + protected function deleteStuffWeMade() { User::delete(FALSE)->addWhere('username', '=', 'testuser1')->execute(); }