Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / linker / LinkerTest.php
blobcba9c8efa8bd1e279b80954500d1f3d26e4b9038
1 <?php
3 use MediaWiki\CommentStore\CommentStoreComment;
4 use MediaWiki\Content\TextContent;
5 use MediaWiki\Context\RequestContext;
6 use MediaWiki\Linker\Linker;
7 use MediaWiki\MainConfigNames;
8 use MediaWiki\Revision\RevisionRecord;
9 use MediaWiki\Revision\SlotRecord;
10 use MediaWiki\SpecialPage\SpecialPage;
11 use MediaWiki\Title\Title;
12 use MediaWiki\User\User;
13 use MediaWiki\Watchlist\WatchedItem;
15 /**
16 * @group Database
18 class LinkerTest extends MediaWikiLangTestCase {
19 /**
20 * @dataProvider provideCasesForUserLink
21 * @covers \MediaWiki\Linker\Linker::userLink
23 public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
24 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
25 if ( !$userName ) {
26 $actual = @Linker::userLink( $userId, $userName, $altUserName );
27 } else {
28 $actual = Linker::userLink( $userId, $userName, $altUserName );
31 $this->assertEquals( $expected, $actual, $msg );
34 public static function provideCasesForUserLink() {
35 # Format:
36 # - expected
37 # - userid
38 # - username
39 # - optional altUserName
40 # - optional message
41 return [
42 # Empty name (T222529)
43 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
44 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
46 'false instead of username' => [ '(no username available)', 73, false ],
47 'null instead of username' => [ '(no username available)', 0, null ],
49 # ## ANONYMOUS USER ########################################
51 '<a href="/wiki/Special:Contributions/JohnDoe" '
52 . 'class="mw-userlink mw-anonuserlink" '
53 . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
54 0, 'JohnDoe', false,
57 '<a href="/wiki/Special:Contributions/::1" '
58 . 'class="mw-userlink mw-anonuserlink" '
59 . 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
60 0, '::1', false,
61 'Anonymous with pretty IPv6'
64 '<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
65 . 'class="mw-userlink mw-anonuserlink" '
66 . 'title="Special:Contributions/0:0:0:0:0:0:0:1"><bdi>::1</bdi></a>',
67 0, '0:0:0:0:0:0:0:1', false,
68 'Anonymous with almost pretty IPv6'
71 '<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
72 . 'class="mw-userlink mw-anonuserlink" '
73 . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001"><bdi>::1</bdi></a>',
74 0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
75 'Anonymous with full IPv6'
78 '<a href="/wiki/Special:Contributions/::1" '
79 . 'class="mw-userlink mw-anonuserlink" '
80 . 'title="Special:Contributions/::1"><bdi>AlternativeUsername</bdi></a>',
81 0, '::1', 'AlternativeUsername',
82 'Anonymous with pretty IPv6 and an alternative username'
85 # IPV4
87 '<a href="/wiki/Special:Contributions/127.0.0.1" '
88 . 'class="mw-userlink mw-anonuserlink" '
89 . 'title="Special:Contributions/127.0.0.1"><bdi>127.0.0.1</bdi></a>',
90 0, '127.0.0.1', false,
91 'Anonymous with IPv4'
94 '<a href="/wiki/Special:Contributions/127.0.0.1" '
95 . 'class="mw-userlink mw-anonuserlink" '
96 . 'title="Special:Contributions/127.0.0.1"><bdi>AlternativeUsername</bdi></a>',
97 0, '127.0.0.1', 'AlternativeUsername',
98 'Anonymous with IPv4 and an alternative username'
101 # IP ranges
103 '<a href="/wiki/Special:Contributions/1.2.3.4/31" '
104 . 'class="mw-userlink mw-anonuserlink" '
105 . 'title="Special:Contributions/1.2.3.4/31"><bdi>1.2.3.4/31</bdi></a>',
106 0, '1.2.3.4/31', false,
107 'Anonymous with IPv4 range'
110 '<a href="/wiki/Special:Contributions/2001:db8::1/43" '
111 . 'class="mw-userlink mw-anonuserlink" '
112 . 'title="Special:Contributions/2001:db8::1/43"><bdi>2001:db8::1/43</bdi></a>',
113 0, '2001:db8::1/43', false,
114 'Anonymous with IPv6 range'
117 # External (imported) user, unknown prefix
119 '<span class="mw-userlink mw-extuserlink mw-anonuserlink"><bdi>acme&gt;Alice</bdi></span>',
120 0, "acme>Alice", false,
121 'User from acme wiki'
124 # Corrupt user names
126 "<span class=\"mw-userlink mw-anonuserlink\"><bdi>Foo\nBar</bdi></span>",
127 0, "Foo\nBar", false,
128 'User name with line break'
131 '<span class="mw-userlink mw-anonuserlink"><bdi>Barf_</bdi></span>',
132 0, "Barf_", false,
133 'User name with trailing underscore'
136 '<span class="mw-userlink mw-anonuserlink"><bdi>abcd</bdi></span>',
137 0, "abcd", false,
138 'Lower case user name'
141 '<span class="mw-userlink mw-anonuserlink"><bdi>For/Bar</bdi></span>',
142 0, "For/Bar", false,
143 'User name with slash'
146 '<span class="mw-userlink mw-anonuserlink"><bdi>For#Bar</bdi></span>',
147 0, "For#Bar", false,
148 'User name with hash'
151 # ## Regular user ##########################################
152 # TODO!
157 * @dataProvider provideUserToolLinks
158 * @covers \MediaWiki\Linker\Linker::userToolLinks
159 * @param string $expected
160 * @param int $userId
161 * @param string $userText
163 public function testUserToolLinks( $expected, $userId, $userText ) {
164 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
165 if ( $userText === '' ) {
166 $actual = @Linker::userToolLinks( $userId, $userText );
167 } else {
168 $actual = Linker::userToolLinks( $userId, $userText );
171 $this->assertSame( $expected, $actual );
174 public static function provideUserToolLinks() {
175 return [
176 // Empty name (T222529)
177 'Empty username, userid 0' => [ ' (no username available)', 0, '' ],
178 'Empty username, userid > 0' => [ ' (no username available)', 73, '' ],
183 * @dataProvider provideUserLink
184 * @covers \MediaWiki\Linker\Linker::userTalkLink
185 * @param string $expected
186 * @param int $userId
187 * @param string $userText
189 public function testUserTalkLink( $expected, $userId, $userText ) {
190 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
191 if ( $userText === '' ) {
192 $actual = @Linker::userTalkLink( $userId, $userText );
193 } else {
194 $actual = Linker::userTalkLink( $userId, $userText );
197 $this->assertSame( $expected, $actual );
201 * @dataProvider provideUserLink
202 * @covers \MediaWiki\Linker\Linker::blockLink
203 * @param string $expected
204 * @param int $userId
205 * @param string $userText
207 public function testBlockLink( $expected, $userId, $userText ) {
208 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
209 if ( $userText === '' ) {
210 $actual = @Linker::blockLink( $userId, $userText );
211 } else {
212 $actual = Linker::blockLink( $userId, $userText );
215 $this->assertSame( $expected, $actual );
219 * @dataProvider provideUserLink
220 * @covers \MediaWiki\Linker\Linker::emailLink
221 * @param string $expected
222 * @param int $userId
223 * @param string $userText
225 public function testEmailLink( $expected, $userId, $userText ) {
226 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
227 if ( $userText === '' ) {
228 $actual = @Linker::emailLink( $userId, $userText );
229 } else {
230 $actual = Linker::emailLink( $userId, $userText );
233 $this->assertSame( $expected, $actual );
236 public static function provideUserLink() {
237 return [
238 // Empty name (T222529)
239 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
240 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
245 * @covers \MediaWiki\Linker\Linker::generateRollback
246 * @dataProvider provideCasesForRollbackGeneration
248 public function testGenerateRollback( $rollbackEnabled, $expectedModules, $title ) {
249 $context = RequestContext::getMain();
250 $user = $this->getTestUser()->getUser();
251 $context->setUser( $user );
252 $this->getServiceContainer()->getUserOptionsManager()->setOption(
253 $user,
254 'showrollbackconfirmation',
255 $rollbackEnabled
258 $this->assertSame( 0, Title::newFromText( $title )->getArticleID() );
259 $pageData = $this->insertPage( $title );
260 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $pageData['title'] );
262 $summary = CommentStoreComment::newUnsavedComment( 'Some comment!' );
263 $page->newPageUpdater( $user )
264 ->setContent(
265 SlotRecord::MAIN,
266 new TextContent( 'Technical Wishes 123!' )
268 ->saveRevision( $summary );
270 $rollbackOutput = Linker::generateRollback( $page->getRevisionRecord(), $context );
271 $modules = $context->getOutput()->getModules();
272 $currentRev = $page->getRevisionRecord();
273 $revisionLookup = $this->getServiceContainer()->getRevisionLookup();
274 $oldestRev = $revisionLookup->getFirstRevision( $page->getTitle() );
276 $this->assertEquals( $expectedModules, $modules );
277 $this->assertInstanceOf( RevisionRecord::class, $currentRev );
278 $this->assertInstanceOf( User::class, $currentRev->getUser() );
279 $this->assertEquals( $user->getName(), $currentRev->getUser()->getName() );
280 $this->assertEquals(
281 static::getTestSysop()->getUser(),
282 $oldestRev->getUser()->getName()
285 $ids = [];
286 $r = $oldestRev;
287 while ( $r ) {
288 $ids[] = $r->getId();
289 $r = $revisionLookup->getNextRevision( $r );
291 $this->assertEquals( [ $oldestRev->getId(), $currentRev->getId() ], $ids );
293 $this->assertStringContainsString( 'rollback 1 edit', $rollbackOutput );
296 public static function provideCasesForRollbackGeneration() {
297 return [
299 true,
300 [ 'mediawiki.misc-authed-curate' ],
301 'Rollback_Test_Page'
304 false,
306 'Rollback_Test_Page2'
311 public static function provideTooltipAndAccesskeyAttribs() {
312 return [
313 'Watch no expiry' => [
314 'ca-watch', [], null, [ 'title' => 'Add this page to your watchlist [w]', 'accesskey' => 'w' ]
316 'Key does not exist' => [
317 'key-does-not-exist', [], null, []
319 'Unwatch no expiry' => [
320 'ca-unwatch', [], null, [ 'title' => 'Remove this page from your watchlist [w]',
321 'accesskey' => 'w' ]
327 * @covers \MediaWiki\Linker\Linker::tooltipAndAccesskeyAttribs
328 * @dataProvider provideTooltipAndAccesskeyAttribs
330 public function testTooltipAndAccesskeyAttribs( $name, $msgParams, $options, $expected ) {
331 $this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true );
332 $user = $this->createMock( User::class );
333 $user->method( 'isRegistered' )->willReturn( true );
335 $title = SpecialPage::getTitleFor( 'Blankpage' );
337 $context = RequestContext::getMain();
338 $context->setTitle( $title );
339 $context->setUser( $user );
341 $watchedItemWithoutExpiry = new WatchedItem( $user, $title, null, null );
343 $result = Linker::tooltipAndAccesskeyAttribs( $name, $msgParams, $options );
345 $this->assertEquals( $expected, $result );
349 * @covers \MediaWiki\Linker\Linker::specialLink
350 * @dataProvider provideSpecialLink
352 public function testSpecialLink( $expected, $target, $key = null ) {
353 $this->overrideConfigValues( [
354 MainConfigNames::Script => '/w/index.php',
355 MainConfigNames::ArticlePath => '/wiki/$1',
356 ] );
358 $this->assertEquals( $expected, Linker::specialLink( $target, $key ) );
361 public static function provideSpecialLink() {
362 yield 'Recent Changes' => [
363 '<a href="/wiki/Special:RecentChanges" title="Special:RecentChanges">Recent changes</a>',
364 'Recentchanges'
367 yield 'Recent Changes, only for a given tag' => [
368 '<a href="/w/index.php?title=Special:RecentChanges&amp;tagfilter=blanking" title="Special:RecentChanges">Recent changes</a>',
369 'Recentchanges?tagfilter=blanking'
372 yield 'Contributions' => [
373 '<a href="/wiki/Special:Contributions" title="Special:Contributions">User contributions</a>',
374 'Contributions'
377 yield 'Contributions, custom key' => [
378 '<a href="/wiki/Special:Contributions" title="Special:Contributions">⧼made-up-display-key⧽</a>',
379 'Contributions',
380 'made-up-display-key'
383 yield 'Contributions, targetted' => [
384 '<a href="/wiki/Special:Contributions/JohnDoe" title="Special:Contributions/JohnDoe">User contributions</a>',
385 'Contributions/JohnDoe'
388 yield 'Contributions, targetted, topOnly' => [
389 '<a href="/w/index.php?title=Special:Contributions/JohnDoe&amp;topOnly=1" title="Special:Contributions/JohnDoe">User contributions</a>',
390 'Contributions/JohnDoe?topOnly=1'
393 yield 'Userlogin' => [
394 '<a href="/wiki/Special:UserLogin" title="Special:UserLogin">Log in</a>',
395 'Userlogin',
396 'login'
399 yield 'Userlogin, returnto' => [
400 '<a href="/w/index.php?title=Special:UserLogin&amp;returnto=Main+Page" title="Special:UserLogin">Log in</a>',
401 'Userlogin?returnto=Main+Page',
402 'login'
405 yield 'Userlogin, targetted' => [
406 // Note that this special page doesn't have any support for and doesn't do anything with
407 // the subtitle; this is here as demonstration that Linker doesn't care.
408 '<a href="/wiki/Special:UserLogin/JohnDoe" title="Special:UserLogin/JohnDoe">Log in</a>',
409 'Userlogin/JohnDoe',
410 'login'