From 659c2ba7ff5bad286f9ed3f456b5759b0906c8c6 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 18 Aug 2022 09:57:34 +0200 Subject: [PATCH] Use user displayname cache in activities Signed-off-by: Joas Schilling --- lib/Activity/Provider/Base.php | 17 +------ tests/php/Activity/Provider/BaseTest.php | 59 +++++------------------- 2 files changed, 12 insertions(+), 64 deletions(-) diff --git a/lib/Activity/Provider/Base.php b/lib/Activity/Provider/Base.php index f1cc6c499..0bb75b57c 100644 --- a/lib/Activity/Provider/Base.php +++ b/lib/Activity/Provider/Base.php @@ -43,9 +43,6 @@ abstract class Base implements IProvider { protected IUserManager $userManager; protected Manager $manager; - /** @var string[] */ - protected array $displayNames = []; - public function __construct(IFactory $languageFactory, IURLGenerator $url, Config $config, @@ -134,22 +131,10 @@ abstract class Base implements IProvider { } protected function getUser(string $uid): array { - if (!isset($this->displayNames[$uid])) { - $this->displayNames[$uid] = $this->getDisplayName($uid); - } - return [ 'type' => 'user', 'id' => $uid, - 'name' => $this->displayNames[$uid], + 'name' => $this->userManager->getDisplayName($uid) ?? $uid, ]; } - - protected function getDisplayName(string $uid): string { - $user = $this->userManager->get($uid); - if ($user instanceof IUser) { - return $user->getDisplayName(); - } - return $uid; - } } diff --git a/tests/php/Activity/Provider/BaseTest.php b/tests/php/Activity/Provider/BaseTest.php index 7a598279a..9fc2bc7dc 100644 --- a/tests/php/Activity/Provider/BaseTest.php +++ b/tests/php/Activity/Provider/BaseTest.php @@ -254,44 +254,7 @@ class BaseTest extends TestCase { ], self::invokePrivate($provider, 'getRoom', [$room, 'user'])); } - public function dataGetUser() { - return [ - ['test', [], false, 'Test'], - ['foo', ['admin' => 'Admin'], false, 'Bar'], - ['admin', ['admin' => 'Administrator'], true, 'Administrator'], - ]; - } - - /** - * @dataProvider dataGetUser - * - * @param string $uid - * @param array $cache - * @param bool $cacheHit - * @param string $name - */ - public function testGetUser($uid, $cache, $cacheHit, $name) { - $provider = $this->getProvider(['getDisplayName']); - - self::invokePrivate($provider, 'displayNames', [$cache]); - - if (!$cacheHit) { - $provider->expects($this->once()) - ->method('getDisplayName') - ->with($uid) - ->willReturn($name); - } else { - $provider->expects($this->never()) - ->method('getDisplayName'); - } - - $result = self::invokePrivate($provider, 'getUser', [$uid]); - $this->assertSame('user', $result['type']); - $this->assertSame($uid, $result['id']); - $this->assertSame($name, $result['name']); - } - - public function dataGetDisplayName() { + public function dataGetUser(): array { return [ ['test', true, 'Test'], ['foo', false, 'foo'], @@ -299,31 +262,31 @@ class BaseTest extends TestCase { } /** - * @dataProvider dataGetDisplayName + * @dataProvider dataGetUser * * @param string $uid * @param bool $validUser * @param string $name */ - public function testGetDisplayName($uid, $validUser, $name) { + public function testGetUser(string $uid, bool $validUser, string $name): void { $provider = $this->getProvider(); if ($validUser) { - $user = $this->createMock(IUser::class); - $user->expects($this->once()) - ->method('getDisplayName') - ->willReturn($name); $this->userManager->expects($this->once()) - ->method('get') + ->method('getDisplayName') ->with($uid) - ->willReturn($user); + ->willReturn($name); } else { $this->userManager->expects($this->once()) - ->method('get') + ->method('getDisplayName') ->with($uid) ->willReturn(null); } - $this->assertSame($name, self::invokePrivate($provider, 'getDisplayName', [$uid])); + $this->assertSame([ + 'type' => 'user', + 'id' => $uid, + 'name' => $name, + ], self::invokePrivate($provider, 'getUser', [$uid])); } }