Debian Codegraph @

tests/phpunit/includes/linker/LinkerTest.php

raw symbols SCIP
1 <?php
2
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;
14
15 /**
16 * @group Database
17 */
18 class LinkerTest extends MediaWikiLangTestCase {
19 /**
20 * @dataProvider provideCasesForUserLink
21 * @covers \MediaWiki\Linker\Linker::userLink
22 */
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 );
29 }
30
31 $this->assertEquals( $expected, $actual, $msg );
32 }
33
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, '' ],
45
46 'false instead of username' => [ '(no username available)', 73, false ],
47 'null instead of username' => [ '(no username available)', 0, null ],
48
49 # ## ANONYMOUS USER ########################################
50 [
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,
55 ],
56 [
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'
62 ],
63 [
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'
69 ],
70 [
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'
76 ],
77 [
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'
83 ],
84
85 # IPV4
86 [
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'
92 ],
93 [
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'
99 ],
100
101 # IP ranges
102 [
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'
108 ],
109 [
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'
115 ],
116
117 # External (imported) user, unknown prefix
118 [
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'
122 ],
123
124 # Corrupt user names
125 [
126 "<span class=\"mw-userlink mw-anonuserlink\"><bdi>Foo\nBar</bdi></span>",
127 0, "Foo\nBar", false,
128 'User name with line break'
129 ],
130 [
131 '<span class="mw-userlink mw-anonuserlink"><bdi>Barf_</bdi></span>',
132 0, "Barf_", false,
133 'User name with trailing underscore'
134 ],
135 [
136 '<span class="mw-userlink mw-anonuserlink"><bdi>abcd</bdi></span>',
137 0, "abcd", false,
138 'Lower case user name'
139 ],
140 [
141 '<span class="mw-userlink mw-anonuserlink"><bdi>For/Bar</bdi></span>',
142 0, "For/Bar", false,
143 'User name with slash'
144 ],
145 [
146 '<span class="mw-userlink mw-anonuserlink"><bdi>For#Bar</bdi></span>',
147 0, "For#Bar", false,
148 'User name with hash'
149 ],
150
151 # ## Regular user ##########################################
152 # TODO!
153 ];
154 }
155
156 /**
157 * @dataProvider provideUserToolLinks
158 * @covers \MediaWiki\Linker\Linker::userToolLinks
159 * @param string $expected
160 * @param int $userId
161 * @param string $userText
162 */
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 );
169 }
170
171 $this->assertSame( $expected, $actual );
172 }
173
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, '' ],
179 ];
180 }
181
182 /**
183 * @dataProvider provideUserLink
184 * @covers \MediaWiki\Linker\Linker::userTalkLink
185 * @param string $expected
186 * @param int $userId
187 * @param string $userText
188 */
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 );
195 }
196
197 $this->assertSame( $expected, $actual );
198 }
199
200 /**
201 * @dataProvider provideUserLink
202 * @covers \MediaWiki\Linker\Linker::blockLink
203 * @param string $expected
204 * @param int $userId
205 * @param string $userText
206 */
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 );
213 }
214
215 $this->assertSame( $expected, $actual );
216 }
217
218 /**
219 * @dataProvider provideUserLink
220 * @covers \MediaWiki\Linker\Linker::emailLink
221 * @param string $expected
222 * @param int $userId
223 * @param string $userText
224 */
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 );
231 }
232
233 $this->assertSame( $expected, $actual );
234 }
235
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, '' ],
241 ];
242 }
243
244 /**
245 * @covers \MediaWiki\Linker\Linker::generateRollback
246 * @dataProvider provideCasesForRollbackGeneration
247 */
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
256 );
257
258 $this->assertSame( 0, Title::newFromText( $title )->getArticleID() );
259 $pageData = $this->insertPage( $title );
260 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $pageData['title'] );
261
262 $summary = CommentStoreComment::newUnsavedComment( 'Some comment!' );
263 $page->newPageUpdater( $user )
264 ->setContent(
265 SlotRecord::MAIN,
266 new TextContent( 'Technical Wishes 123!' )
267 )
268 ->saveRevision( $summary );
269
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() );
275
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()
283 );
284
285 $ids = [];
286 $r = $oldestRev;
287 while ( $r ) {
288 $ids[] = $r->getId();
289 $r = $revisionLookup->getNextRevision( $r );
290 }
291 $this->assertEquals( [ $oldestRev->getId(), $currentRev->getId() ], $ids );
292
293 $this->assertStringContainsString( 'rollback 1 edit', $rollbackOutput );
294 }
295
296 public static function provideCasesForRollbackGeneration() {
297 return [
298 [
299 true,
300 [ 'mediawiki.misc-authed-curate' ],
301 'Rollback_Test_Page'
302 ],
303 [
304 false,
305 [],
306 'Rollback_Test_Page2'
307 ]
308 ];
309 }
310
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' ]
315 ],
316 'Key does not exist' => [
317 'key-does-not-exist', [], null, []
318 ],
319 'Unwatch no expiry' => [
320 'ca-unwatch', [], null, [ 'title' => 'Remove this page from your watchlist [w]',
321 'accesskey' => 'w' ]
322 ],
323 ];
324 }
325
326 /**
327 * @covers \MediaWiki\Linker\Linker::tooltipAndAccesskeyAttribs
328 * @dataProvider provideTooltipAndAccesskeyAttribs
329 */
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 );
334
335 $title = SpecialPage::getTitleFor( 'Blankpage' );
336
337 $context = RequestContext::getMain();
338 $context->setTitle( $title );
339 $context->setUser( $user );
340
341 $watchedItemWithoutExpiry = new WatchedItem( $user, $title, null, null );
342
343 $result = Linker::tooltipAndAccesskeyAttribs( $name, $msgParams, $options );
344
345 $this->assertEquals( $expected, $result );
346 }
347
348 /**
349 * @covers \MediaWiki\Linker\Linker::specialLink
350 * @dataProvider provideSpecialLink
351 */
352 public function testSpecialLink( $expected, $target, $key = null ) {
353 $this->overrideConfigValues( [
354 MainConfigNames::Script => '/w/index.php',
355 MainConfigNames::ArticlePath => '/wiki/$1',
356 ] );
357
358 $this->assertEquals( $expected, Linker::specialLink( $target, $key ) );
359 }
360
361 public static function provideSpecialLink() {
362 yield 'Recent Changes' => [
363 '<a href="/wiki/Special:RecentChanges" title="Special:RecentChanges">Recent changes</a>',
364 'Recentchanges'
365 ];
366
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'
370 ];
371
372 yield 'Contributions' => [
373 '<a href="/wiki/Special:Contributions" title="Special:Contributions">User contributions</a>',
374 'Contributions'
375 ];
376
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'
381 ];
382
383 yield 'Contributions, targetted' => [
384 '<a href="/wiki/Special:Contributions/JohnDoe" title="Special:Contributions/JohnDoe">User contributions</a>',
385 'Contributions/JohnDoe'
386 ];
387
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'
391 ];
392
393 yield 'Userlogin' => [
394 '<a href="/wiki/Special:UserLogin" title="Special:UserLogin">Log in</a>',
395 'Userlogin',
396 'login'
397 ];
398
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'
403 ];
404
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'
411 ];
412 }
413 }