diff options
author | Máté Szabó <mszabo@wikimedia.org> | 2025-02-13 17:38:18 +0100 |
---|---|---|
committer | Máté Szabó <mszabo@wikimedia.org> | 2025-02-14 19:27:11 +0100 |
commit | 2d558c70526d341e7733ad2e65489855871d3529 (patch) | |
tree | ddce3acecd78ccd2db6a3371758e181f080e61c9 /tests/phpunit/integration/includes | |
parent | 32dd3f30f6de4f84cec2ef8faef11d4969bc73b5 (diff) | |
download | mediawikicore-2d558c70526d341e7733ad2e65489855871d3529.tar.gz mediawikicore-2d558c70526d341e7733ad2e65489855871d3529.zip |
linker: Add process cache to UserLinkRenderer::userLink()
Why:
- Rendering user links via UserLinkRenderer::userLink() and its
predecessor Linker::userLink() can be fairly expensive, so
RecentChanges has been caching them since
I91588aebae7e49e3d3cb77702cf28677b4a14c8d.
- We would like to start rendering a custom tooltip for user links
associated with temporary accounts, which requires returning a unique
ID for each tooltip so that it can be associated with the
corresponding link via an aria-describedby attribute for
accessibility.
- This won't work with the existing caching logic because it doesn't
treat links differently.
- Now that the UserLinkRenderer service exists, we can move the caching
logic there instead and allow every page, not just RecentChanges, to
automatically enjoy its benefits while transparently handling special
cases like expired temporary account links.
What:
- Implement process caching for user links in UserLinkRenderer.
- Replace Linker::userLink() with UserLinkRenderer in
RCCacheEntryFactory, and remove the now-redundant local process cache.
Bug: T358469
Change-Id: I0259e860c19f356ab66f45955f2997d307daa6e1
Diffstat (limited to 'tests/phpunit/integration/includes')
-rw-r--r-- | tests/phpunit/integration/includes/linker/UserLinkRendererTest.php | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/phpunit/integration/includes/linker/UserLinkRendererTest.php b/tests/phpunit/integration/includes/linker/UserLinkRendererTest.php index 9bca3332d224..408067f54f94 100644 --- a/tests/phpunit/integration/includes/linker/UserLinkRendererTest.php +++ b/tests/phpunit/integration/includes/linker/UserLinkRendererTest.php @@ -194,4 +194,63 @@ class UserLinkRendererTest extends MediaWikiLangTestCase { ], ]; } + + /** + * @dataProvider provideCacheParams + * + * @param bool $shouldCache `true` if the user link should be cached, `false` otherwise + * @param UserIdentity $otherUser User to use in the second call to userLink(). + * @param string|null $altUserName Alternative user name to use in the second call to userLink(). + * @param string[] $attributes Attributes to use in the second call to userLink(). + */ + public function testUserLinkShouldCacheByUserNameAndParams( + bool $shouldCache, + UserIdentity $otherUser, + ?string $altUserName = null, + array $attributes = [] + ): void { + $user = new UserIdentityValue( 1, 'TestUser' ); + + $firstCall = $this->userLinkRenderer->userLink( + $user, + new FakeQqxMessageLocalizer() + ); + $otherCall = $this->userLinkRenderer->userLink( + $otherUser, + new FakeQqxMessageLocalizer(), + $altUserName, + $attributes + ); + + if ( $shouldCache ) { + $this->assertSame( $firstCall, $otherCall ); + } else { + $this->assertNotEquals( $firstCall, $otherCall ); + } + } + + public static function provideCacheParams(): iterable { + yield 'same user and params' => [ + true, + new UserIdentityValue( 1, 'TestUser' ) + ]; + + yield 'same user but different link text' => [ + false, + new UserIdentityValue( 1, 'TestUser' ), + 'foo' + ]; + + yield 'same user but different attributes' => [ + false, + new UserIdentityValue( 1, 'TestUser' ), + null, + [ 'class' => 'foo' ] + ]; + + yield 'different user' => [ + false, + new UserIdentityValue( 2, 'OtherUser' ) + ]; + } } |