1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
<?php
use MediaWiki\CommentStore\CommentStoreComment;
use MediaWiki\Content\TextContent;
use MediaWiki\Context\RequestContext;
use MediaWiki\Linker\Linker;
use MediaWiki\MainConfigNames;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\Title\Title;
use MediaWiki\User\User;
use MediaWiki\Watchlist\WatchedItem;
/**
* @group Database
*/
class LinkerTest extends MediaWikiLangTestCase {
/**
* @dataProvider provideCasesForUserLink
* @covers \MediaWiki\Linker\Linker::userLink
*/
public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
// We'd also test the warning, but injecting a mock logger into a static method is tricky.
if ( !$userName ) {
$actual = @Linker::userLink( $userId, $userName, $altUserName );
} else {
$actual = Linker::userLink( $userId, $userName, $altUserName );
}
$this->assertEquals( $expected, $actual, $msg );
}
public static function provideCasesForUserLink() {
# Format:
# - expected
# - userid
# - username
# - optional altUserName
# - optional message
return [
# Empty name (T222529)
'Empty username, userid 0' => [ '(no username available)', 0, '' ],
'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
'false instead of username' => [ '(no username available)', 73, false ],
'null instead of username' => [ '(no username available)', 0, null ],
# ## ANONYMOUS USER ########################################
[
'<a href="/wiki/Special:Contributions/JohnDoe" '
. 'class="mw-userlink mw-anonuserlink" '
. 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
0, 'JohnDoe', false,
],
[
'<a href="/wiki/Special:Contributions/::1" '
. 'class="mw-userlink mw-anonuserlink" '
. 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
0, '::1', false,
'Anonymous with pretty IPv6'
],
[
'<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
. 'class="mw-userlink mw-anonuserlink" '
. 'title="Special:Contributions/0:0:0:0:0:0:0:1"><bdi>::1</bdi></a>',
0, '0:0:0:0:0:0:0:1', false,
'Anonymous with almost pretty IPv6'
],
[
'<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
. 'class="mw-userlink mw-anonuserlink" '
. 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001"><bdi>::1</bdi></a>',
0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
'Anonymous with full IPv6'
],
[
'<a href="/wiki/Special:Contributions/::1" '
. 'class="mw-userlink mw-anonuserlink" '
. 'title="Special:Contributions/::1"><bdi>AlternativeUsername</bdi></a>',
0, '::1', 'AlternativeUsername',
'Anonymous with pretty IPv6 and an alternative username'
],
# IPV4
[
'<a href="/wiki/Special:Contributions/127.0.0.1" '
. 'class="mw-userlink mw-anonuserlink" '
. 'title="Special:Contributions/127.0.0.1"><bdi>127.0.0.1</bdi></a>',
0, '127.0.0.1', false,
'Anonymous with IPv4'
],
[
'<a href="/wiki/Special:Contributions/127.0.0.1" '
. 'class="mw-userlink mw-anonuserlink" '
. 'title="Special:Contributions/127.0.0.1"><bdi>AlternativeUsername</bdi></a>',
0, '127.0.0.1', 'AlternativeUsername',
'Anonymous with IPv4 and an alternative username'
],
# IP ranges
[
'<a href="/wiki/Special:Contributions/1.2.3.4/31" '
. 'class="mw-userlink mw-anonuserlink" '
. 'title="Special:Contributions/1.2.3.4/31"><bdi>1.2.3.4/31</bdi></a>',
0, '1.2.3.4/31', false,
'Anonymous with IPv4 range'
],
[
'<a href="/wiki/Special:Contributions/2001:db8::1/43" '
. 'class="mw-userlink mw-anonuserlink" '
. 'title="Special:Contributions/2001:db8::1/43"><bdi>2001:db8::1/43</bdi></a>',
0, '2001:db8::1/43', false,
'Anonymous with IPv6 range'
],
# External (imported) user, unknown prefix
[
'<span class="mw-userlink mw-extuserlink mw-anonuserlink"><bdi>acme>Alice</bdi></span>',
0, "acme>Alice", false,
'User from acme wiki'
],
# Corrupt user names
[
"<span class=\"mw-userlink mw-anonuserlink\"><bdi>Foo\nBar</bdi></span>",
0, "Foo\nBar", false,
'User name with line break'
],
[
'<span class="mw-userlink mw-anonuserlink"><bdi>Barf_</bdi></span>',
0, "Barf_", false,
'User name with trailing underscore'
],
[
'<span class="mw-userlink mw-anonuserlink"><bdi>abcd</bdi></span>',
0, "abcd", false,
'Lower case user name'
],
[
'<span class="mw-userlink mw-anonuserlink"><bdi>For/Bar</bdi></span>',
0, "For/Bar", false,
'User name with slash'
],
[
'<span class="mw-userlink mw-anonuserlink"><bdi>For#Bar</bdi></span>',
0, "For#Bar", false,
'User name with hash'
],
# ## Regular user ##########################################
# TODO!
];
}
/**
* @dataProvider provideUserToolLinks
* @covers \MediaWiki\Linker\Linker::userToolLinks
* @param string $expected
* @param int $userId
* @param string $userText
*/
public function testUserToolLinks( $expected, $userId, $userText ) {
// We'd also test the warning, but injecting a mock logger into a static method is tricky.
if ( $userText === '' ) {
$actual = @Linker::userToolLinks( $userId, $userText );
} else {
$actual = Linker::userToolLinks( $userId, $userText );
}
$this->assertSame( $expected, $actual );
}
public static function provideUserToolLinks() {
return [
// Empty name (T222529)
'Empty username, userid 0' => [ ' (no username available)', 0, '' ],
'Empty username, userid > 0' => [ ' (no username available)', 73, '' ],
];
}
/**
* @dataProvider provideUserLink
* @covers \MediaWiki\Linker\Linker::userTalkLink
* @param string $expected
* @param int $userId
* @param string $userText
*/
public function testUserTalkLink( $expected, $userId, $userText ) {
// We'd also test the warning, but injecting a mock logger into a static method is tricky.
if ( $userText === '' ) {
$actual = @Linker::userTalkLink( $userId, $userText );
} else {
$actual = Linker::userTalkLink( $userId, $userText );
}
$this->assertSame( $expected, $actual );
}
/**
* @dataProvider provideUserLink
* @covers \MediaWiki\Linker\Linker::blockLink
* @param string $expected
* @param int $userId
* @param string $userText
*/
public function testBlockLink( $expected, $userId, $userText ) {
// We'd also test the warning, but injecting a mock logger into a static method is tricky.
if ( $userText === '' ) {
$actual = @Linker::blockLink( $userId, $userText );
} else {
$actual = Linker::blockLink( $userId, $userText );
}
$this->assertSame( $expected, $actual );
}
/**
* @dataProvider provideUserLink
* @covers \MediaWiki\Linker\Linker::emailLink
* @param string $expected
* @param int $userId
* @param string $userText
*/
public function testEmailLink( $expected, $userId, $userText ) {
// We'd also test the warning, but injecting a mock logger into a static method is tricky.
if ( $userText === '' ) {
$actual = @Linker::emailLink( $userId, $userText );
} else {
$actual = Linker::emailLink( $userId, $userText );
}
$this->assertSame( $expected, $actual );
}
public static function provideUserLink() {
return [
// Empty name (T222529)
'Empty username, userid 0' => [ '(no username available)', 0, '' ],
'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
];
}
/**
* @covers \MediaWiki\Linker\Linker::generateRollback
* @dataProvider provideCasesForRollbackGeneration
*/
public function testGenerateRollback( $rollbackEnabled, $expectedModules, $title ) {
$context = RequestContext::getMain();
$user = $this->getTestUser()->getUser();
$context->setUser( $user );
$this->getServiceContainer()->getUserOptionsManager()->setOption(
$user,
'showrollbackconfirmation',
$rollbackEnabled
);
$this->assertSame( 0, Title::newFromText( $title )->getArticleID() );
$pageData = $this->insertPage( $title );
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $pageData['title'] );
$summary = CommentStoreComment::newUnsavedComment( 'Some comment!' );
$page->newPageUpdater( $user )
->setContent(
SlotRecord::MAIN,
new TextContent( 'Technical Wishes 123!' )
)
->saveRevision( $summary );
$rollbackOutput = Linker::generateRollback( $page->getRevisionRecord(), $context );
$modules = $context->getOutput()->getModules();
$currentRev = $page->getRevisionRecord();
$revisionLookup = $this->getServiceContainer()->getRevisionLookup();
$oldestRev = $revisionLookup->getFirstRevision( $page->getTitle() );
$this->assertEquals( $expectedModules, $modules );
$this->assertInstanceOf( RevisionRecord::class, $currentRev );
$this->assertInstanceOf( User::class, $currentRev->getUser() );
$this->assertEquals( $user->getName(), $currentRev->getUser()->getName() );
$this->assertEquals(
static::getTestSysop()->getUser(),
$oldestRev->getUser()->getName()
);
$ids = [];
$r = $oldestRev;
while ( $r ) {
$ids[] = $r->getId();
$r = $revisionLookup->getNextRevision( $r );
}
$this->assertEquals( [ $oldestRev->getId(), $currentRev->getId() ], $ids );
$this->assertStringContainsString( 'rollback 1 edit', $rollbackOutput );
}
public static function provideCasesForRollbackGeneration() {
return [
[
true,
[ 'mediawiki.misc-authed-curate' ],
'Rollback_Test_Page'
],
[
false,
[],
'Rollback_Test_Page2'
]
];
}
public static function provideTooltipAndAccesskeyAttribs() {
return [
'Watch no expiry' => [
'ca-watch', [], null, [ 'title' => 'Add this page to your watchlist [w]', 'accesskey' => 'w' ]
],
'Key does not exist' => [
'key-does-not-exist', [], null, []
],
'Unwatch no expiry' => [
'ca-unwatch', [], null, [ 'title' => 'Remove this page from your watchlist [w]',
'accesskey' => 'w' ]
],
];
}
/**
* @covers \MediaWiki\Linker\Linker::tooltipAndAccesskeyAttribs
* @dataProvider provideTooltipAndAccesskeyAttribs
*/
public function testTooltipAndAccesskeyAttribs( $name, $msgParams, $options, $expected ) {
$this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true );
$user = $this->createMock( User::class );
$user->method( 'isRegistered' )->willReturn( true );
$title = SpecialPage::getTitleFor( 'Blankpage' );
$context = RequestContext::getMain();
$context->setTitle( $title );
$context->setUser( $user );
$watchedItemWithoutExpiry = new WatchedItem( $user, $title, null, null );
$result = Linker::tooltipAndAccesskeyAttribs( $name, $msgParams, $options );
$this->assertEquals( $expected, $result );
}
/**
* @covers \MediaWiki\Linker\Linker::specialLink
* @dataProvider provideSpecialLink
*/
public function testSpecialLink( $expected, $target, $key = null ) {
$this->overrideConfigValues( [
MainConfigNames::Script => '/w/index.php',
MainConfigNames::ArticlePath => '/wiki/$1',
] );
$this->assertEquals( $expected, Linker::specialLink( $target, $key ) );
}
public static function provideSpecialLink() {
yield 'Recent Changes' => [
'<a href="/wiki/Special:RecentChanges" title="Special:RecentChanges">Recent changes</a>',
'Recentchanges'
];
yield 'Recent Changes, only for a given tag' => [
'<a href="/w/index.php?title=Special:RecentChanges&tagfilter=blanking" title="Special:RecentChanges">Recent changes</a>',
'Recentchanges?tagfilter=blanking'
];
yield 'Contributions' => [
'<a href="/wiki/Special:Contributions" title="Special:Contributions">User contributions</a>',
'Contributions'
];
yield 'Contributions, custom key' => [
'<a href="/wiki/Special:Contributions" title="Special:Contributions">⧼made-up-display-key⧽</a>',
'Contributions',
'made-up-display-key'
];
yield 'Contributions, targetted' => [
'<a href="/wiki/Special:Contributions/JohnDoe" title="Special:Contributions/JohnDoe">User contributions</a>',
'Contributions/JohnDoe'
];
yield 'Contributions, targetted, topOnly' => [
'<a href="/w/index.php?title=Special:Contributions/JohnDoe&topOnly=1" title="Special:Contributions/JohnDoe">User contributions</a>',
'Contributions/JohnDoe?topOnly=1'
];
yield 'Userlogin' => [
'<a href="/wiki/Special:UserLogin" title="Special:UserLogin">Log in</a>',
'Userlogin',
'login'
];
yield 'Userlogin, returnto' => [
'<a href="/w/index.php?title=Special:UserLogin&returnto=Main+Page" title="Special:UserLogin">Log in</a>',
'Userlogin?returnto=Main+Page',
'login'
];
yield 'Userlogin, targetted' => [
// Note that this special page doesn't have any support for and doesn't do anything with
// the subtitle; this is here as demonstration that Linker doesn't care.
'<a href="/wiki/Special:UserLogin/JohnDoe" title="Special:UserLogin/JohnDoe">Log in</a>',
'Userlogin/JohnDoe',
'login'
];
}
}
|