dev/core#2998 - Fix standalone throwing exception when fetching unknown user
authorPatrick Figel <pfigel@greenpeace.org>
Sun, 3 Dec 2023 11:27:41 +0000 (11:27 +0000)
committerPatrick Figel <pfigel@greenpeace.org>
Sun, 3 Dec 2023 11:27:41 +0000 (11:27 +0000)
This fixes an issue where getUserIDFromUsername() was throwing an exception
when fetching unknown users, rather than return NULL.

ext/standaloneusers/Civi/Standalone/Security.php
ext/standaloneusers/tests/phpunit/Civi/Standalone/SecurityTest.php

index 587f1f504e14c18f469756737c52c4368444ea47..34b4efca4ea14a2a4c64f1ad96a571f5d438a74e 100644 (file)
@@ -81,7 +81,7 @@ class Security {
     return \Civi\Api4\User::get(FALSE)
       ->addWhere('username', '=', $username)
       ->execute()
-      ->single()['id'] ?? NULL;
+      ->first()['id'] ?? NULL;
   }
 
   /**
index d22c669a229d81213cd9636c07d7125e1267de77..a63ec2c98638b24fff51747aa28a313cf0d5ef3b 100644 (file)
@@ -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();
   }