setMwGlobals( [
'wgArticlePath' => '/wiki/$1',
] );
// We'd also test the warning, but injecting a mock logger into a static method is tricky.
if ( !$userName ) {
Wikimedia\suppressWarnings();
}
$actual = Linker::userLink( $userId, $userName, $altUserName );
if ( !$userName ) {
Wikimedia\restoreWarnings();
}
$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 ########################################
[
'JohnDoe',
0, 'JohnDoe', false,
],
[
'::1',
0, '::1', false,
'Anonymous with pretty IPv6'
],
[
'::1',
0, '0:0:0:0:0:0:0:1', false,
'Anonymous with almost pretty IPv6'
],
[
'::1',
0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
'Anonymous with full IPv6'
],
[
'AlternativeUsername',
0, '::1', 'AlternativeUsername',
'Anonymous with pretty IPv6 and an alternative username'
],
# IPV4
[
'127.0.0.1',
0, '127.0.0.1', false,
'Anonymous with IPv4'
],
[
'AlternativeUsername',
0, '127.0.0.1', 'AlternativeUsername',
'Anonymous with IPv4 and an alternative username'
],
# ## Regular user ##########################################
# TODO!
];
}
/**
* @dataProvider provideUserToolLinks
* @covers 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 === '' ) {
Wikimedia\suppressWarnings();
}
$actual = Linker::userToolLinks( $userId, $userText );
if ( $userText === '' ) {
Wikimedia\restoreWarnings();
}
$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 provideUserTalkLink
* @covers 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 === '' ) {
Wikimedia\suppressWarnings();
}
$actual = Linker::userTalkLink( $userId, $userText );
if ( $userText === '' ) {
Wikimedia\restoreWarnings();
}
$this->assertSame( $expected, $actual );
}
public static function provideUserTalkLink() {
return [
// Empty name (T222529)
'Empty username, userid 0' => [ '(no username available)', 0, '' ],
'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
];
}
/**
* @dataProvider provideBlockLink
* @covers 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 === '' ) {
Wikimedia\suppressWarnings();
}
$actual = Linker::blockLink( $userId, $userText );
if ( $userText === '' ) {
Wikimedia\restoreWarnings();
}
$this->assertSame( $expected, $actual );
}
public static function provideBlockLink() {
return [
// Empty name (T222529)
'Empty username, userid 0' => [ '(no username available)', 0, '' ],
'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
];
}
/**
* @dataProvider provideEmailLink
* @covers 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 === '' ) {
Wikimedia\suppressWarnings();
}
$actual = Linker::emailLink( $userId, $userText );
if ( $userText === '' ) {
Wikimedia\restoreWarnings();
}
$this->assertSame( $expected, $actual );
}
public static function provideEmailLink() {
return [
// Empty name (T222529)
'Empty username, userid 0' => [ '(no username available)', 0, '' ],
'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
];
}
/**
* @dataProvider provideCasesForFormatComment
* @covers Linker::formatComment
* @covers Linker::formatAutocomments
* @covers Linker::formatLinksInComment
*/
public function testFormatComment(
$expected, $comment, $title = false, $local = false, $wikiId = null
) {
$conf = new SiteConfiguration();
$conf->settings = [
'wgServer' => [
'enwiki' => '//en.example.org',
'dewiki' => '//de.example.org',
],
'wgArticlePath' => [
'enwiki' => '/w/$1',
'dewiki' => '/w/$1',
],
];
$conf->suffixes = [ 'wiki' ];
$this->setMwGlobals( [
'wgScript' => '/wiki/index.php',
'wgArticlePath' => '/wiki/$1',
'wgCapitalLinks' => true,
'wgConf' => $conf,
// TODO: update tests when the default changes
'wgFragmentMode' => [ 'legacy' ],
] );
if ( $title === false ) {
// We need a page title that exists
$title = Title::newFromText( 'Special:BlankPage' );
}
$this->assertEquals(
$expected,
Linker::formatComment( $comment, $title, $local, $wikiId )
);
}
public function provideCasesForFormatComment() {
$wikiId = 'enwiki'; // $wgConf has a fake entry for this
// phpcs:disable Generic.Files.LineLength
return [
// Linker::formatComment
[
'a<script>b',
'a */"
],
[
'',
"/* autocomment */",
false, true
],
[
'',
"/* autocomment */",
null
],
[
'',
"/* */",
false, true
],
[
'',
"/* */",
null
],
[
'',
"/* [[ */",
false, true
],
[
'',
"/* [[ */",
null
],
[
"foo ",
"foo /* [[#_\t_]] */",
false, true
],
[
"foo ",
"foo /* [[#_\t_]] */",
null
],
[
'',
"/* autocomment */",
false, false
],
[
'',
"/* autocomment */",
false, false, $wikiId
],
// Linker::formatLinksInComment
[
'abc link def',
"abc [[link]] def",
],
[
'abc text def',
"abc [[link|text]] def",
],
[
'abc Special:BlankPage def',
"abc [[Special:BlankPage|]] def",
],
[
'abc ąśż def',
"abc [[%C4%85%C5%9B%C5%BC]] def",
],
[
'abc #section def',
"abc [[#section]] def",
],
[
'abc /subpage def',
"abc [[/subpage]] def",
],
[
'abc "evil!" def',
"abc [[\"evil!\"]] def",
],
[
'abc [[<script>very evil</script>]] def',
"abc [[]] def",
],
[
'abc [[|]] def',
"abc [[|]] def",
],
[
'abc link def',
"abc [[link]] def",
false, false
],
[
'abc link def',
"abc [[link]] def",
false, false, $wikiId
],
];
// phpcs:enable
}
/**
* @covers Linker::formatLinksInComment
* @dataProvider provideCasesForFormatLinksInComment
*/
public function testFormatLinksInComment( $expected, $input, $wiki ) {
$conf = new SiteConfiguration();
$conf->settings = [
'wgServer' => [
'enwiki' => '//en.example.org'
],
'wgArticlePath' => [
'enwiki' => '/w/$1',
],
];
$conf->suffixes = [ 'wiki' ];
$this->setMwGlobals( [
'wgScript' => '/wiki/index.php',
'wgArticlePath' => '/wiki/$1',
'wgCapitalLinks' => true,
'wgConf' => $conf,
] );
$this->assertEquals(
$expected,
Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki )
);
}
/**
* @covers Linker::generateRollback
* @dataProvider provideCasesForRollbackGeneration
*/
public function testGenerateRollback( $rollbackEnabled, $expectedModules, $title ) {
$this->markTestSkippedIfDbType( 'postgres' );
$context = RequestContext::getMain();
$user = $context->getUser();
$user->setOption( 'showrollbackconfirmation', $rollbackEnabled );
$this->assertSame( 0, Title::newFromText( $title )->getArticleID() );
$pageData = $this->insertPage( $title );
$page = WikiPage::factory( $pageData['title'] );
$updater = $page->newPageUpdater( $user );
$updater->setContent( \MediaWiki\Revision\SlotRecord::MAIN,
new TextContent( 'Technical Wishes 123!' )
);
$summary = CommentStoreComment::newUnsavedComment( 'Some comment!' );
$updater->saveRevision( $summary );
$rollbackOutput = Linker::generateRollback( $page->getRevisionRecord(), $context );
$modules = $context->getOutput()->getModules();
$currentRev = $page->getRevisionRecord();
$revisionLookup = MediaWikiServices::getInstance()->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 provideCasesForFormatLinksInComment() {
// phpcs:disable Generic.Files.LineLength
return [
[
'foo bar Special:BlankPage',
'foo bar [[Special:BlankPage]]',
null,
],
[
'Special:BlankPage',
'[[ :Special:BlankPage]]',
null,
],
[
'[[FooSpecial:BlankPage',
'[[Foo[[Special:BlankPage]]',
null,
],
[
'Foo\'bar',
"[[Foo'bar]]",
'enwiki',
],
[
'foo bar Special:BlankPage',
'foo bar [[Special:BlankPage]]',
'enwiki',
],
[
'foo bar Image:Example',
'foo bar [[Image:Example]]',
'enwiki',
],
];
// phpcs:enable
}
public static function provideLinkBeginHook() {
// phpcs:disable Generic.Files.LineLength
return [
// Modify $html
[
function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
$html = 'foobar';
},
'foobar'
],
// Modify $attribs
[
function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
$attribs['bar'] = 'baz';
},
'Special:BlankPage'
],
// Modify $query
[
function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
$query['bar'] = 'baz';
},
'Special:BlankPage'
],
// Force HTTP $options
[
function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
$options = [ 'http' ];
},
'Special:BlankPage'
],
// Force 'forcearticlepath' in $options
[
function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
$options = [ 'forcearticlepath' ];
$query['foo'] = 'bar';
},
'Special:BlankPage'
],
// Abort early
[
function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
$ret = 'foobar';
return false;
},
'foobar'
],
];
// phpcs:enable
}
/**
* @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
* @dataProvider provideLinkBeginHook
*/
public function testLinkBeginHook( $callback, $expected ) {
$this->hideDeprecated( 'LinkBegin hook (used in hook-LinkBegin-closure)' );
$this->setMwGlobals( [
'wgArticlePath' => '/wiki/$1',
'wgServer' => '//example.org',
'wgCanonicalServer' => 'http://example.org',
'wgScriptPath' => '/w',
'wgScript' => '/w/index.php',
] );
$this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
$title = SpecialPage::getTitleFor( 'Blankpage' );
$out = Linker::link( $title );
$this->assertEquals( $expected, $out );
}
public static function provideLinkEndHook() {
return [
// Override $html
[
function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
$html = 'foobar';
},
'foobar'
],
// Modify $attribs
[
function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
$attribs['bar'] = 'baz';
},
'Special:BlankPage'
],
// Fully override return value and abort hook
[
function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
$ret = 'blahblahblah';
return false;
},
'blahblahblah'
],
];
}
/**
* @covers MediaWiki\Linker\LinkRenderer::buildAElement
* @dataProvider provideLinkEndHook
*/
public function testLinkEndHook( $callback, $expected ) {
$this->hideDeprecated( 'LinkEnd hook (used in hook-LinkEnd-closure)' );
$this->setMwGlobals( [
'wgArticlePath' => '/wiki/$1',
] );
$this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
$title = SpecialPage::getTitleFor( 'Blankpage' );
$out = Linker::link( $title );
$this->assertEquals( $expected, $out );
}
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 Linker::tooltipAndAccesskeyAttribs
* @dataProvider provideTooltipAndAccesskeyAttribs
*/
public function testTooltipAndAccesskeyAttribs( $name, $msgParams, $options, $expected ) {
$this->setMwGlobals( [
'wgWatchlistExpiry' => true,
] );
$user = $this->createMock( User::class );
$user->method( 'isRegistered' )->willReturn( true );
$user->method( 'isLoggedIn' )->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 );
}
}