2012-05-22 23:30:07 +00:00
< ? php
2020-03-07 07:07:42 +00:00
use MediaWiki\MediaWikiServices ;
2020-04-18 02:39:58 +00:00
use MediaWiki\Revision\RevisionRecord ;
2020-03-07 07:07:42 +00:00
2014-08-15 21:11:26 +00:00
/**
* @ group Database
*/
2012-12-06 17:53:43 +00:00
class LinkerTest extends MediaWikiLangTestCase {
2012-05-22 23:30:07 +00:00
/**
* @ dataProvider provideCasesForUserLink
2013-03-11 03:16:28 +00:00
* @ covers Linker :: userLink
2012-05-22 23:30:07 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testUserLink ( $expected , $userId , $userName , $altUserName = false , $msg = '' ) {
2016-02-17 09:09:32 +00:00
$this -> setMwGlobals ( [
2012-05-22 23:30:07 +00:00
'wgArticlePath' => '/wiki/$1' ,
2016-02-17 09:09:32 +00:00
] );
2012-05-22 23:30:07 +00:00
2019-05-06 08:58:09 +00:00
// We'd also test the warning, but injecting a mock logger into a static method is tricky.
2019-05-22 20:12:11 +00:00
if ( ! $userName ) {
2019-05-06 08:58:09 +00:00
Wikimedia\suppressWarnings ();
}
$actual = Linker :: userLink ( $userId , $userName , $altUserName );
2019-05-22 20:12:11 +00:00
if ( ! $userName ) {
2019-05-06 08:58:09 +00:00
Wikimedia\restoreWarnings ();
}
$this -> assertEquals ( $expected , $actual , $msg );
2012-05-22 23:30:07 +00:00
}
2013-03-22 02:12:37 +00:00
public static function provideCasesForUserLink () {
2012-05-22 23:30:07 +00:00
# Format:
# - expected
# - userid
# - username
# - optional altUserName
# - optional message
2016-02-17 09:09:32 +00:00
return [
2019-05-06 08:58:09 +00:00
# Empty name (T222529)
'Empty username, userid 0' => [ '(no username available)' , 0 , '' ],
'Empty username, userid > 0' => [ '(no username available)' , 73 , '' ],
2012-05-22 23:30:07 +00:00
2019-05-22 20:12:11 +00:00
'false instead of username' => [ '(no username available)' , 73 , false ],
'null instead of username' => [ '(no username available)' , 0 , null ],
2015-09-11 13:44:59 +00:00
# ## ANONYMOUS USER ########################################
2016-02-17 09:09:32 +00:00
[
2014-04-24 10:05:52 +00:00
'<a href="/wiki/Special:Contributions/JohnDoe" '
2016-05-25 23:37:21 +00:00
. 'class="mw-userlink mw-anonuserlink" '
2016-08-31 20:58:05 +00:00
. 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>' ,
2012-05-22 23:30:07 +00:00
0 , 'JohnDoe' , false ,
2016-02-17 09:09:32 +00:00
],
[
2014-04-24 10:05:52 +00:00
'<a href="/wiki/Special:Contributions/::1" '
2016-05-25 23:37:21 +00:00
. 'class="mw-userlink mw-anonuserlink" '
2016-08-31 20:58:05 +00:00
. 'title="Special:Contributions/::1"><bdi>::1</bdi></a>' ,
2012-05-22 23:30:07 +00:00
0 , '::1' , false ,
'Anonymous with pretty IPv6'
2016-02-17 09:09:32 +00:00
],
[
2014-04-24 10:05:52 +00:00
'<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
2016-05-25 23:37:21 +00:00
. 'class="mw-userlink mw-anonuserlink" '
2016-08-31 20:58:05 +00:00
. 'title="Special:Contributions/0:0:0:0:0:0:0:1"><bdi>::1</bdi></a>' ,
2012-05-22 23:30:07 +00:00
0 , '0:0:0:0:0:0:0:1' , false ,
'Anonymous with almost pretty IPv6'
2016-02-17 09:09:32 +00:00
],
[
2014-04-24 10:05:52 +00:00
'<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
2016-05-25 23:37:21 +00:00
. 'class="mw-userlink mw-anonuserlink" '
2016-08-31 20:58:05 +00:00
. 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001"><bdi>::1</bdi></a>' ,
2012-05-22 23:30:07 +00:00
0 , '0000:0000:0000:0000:0000:0000:0000:0001' , false ,
'Anonymous with full IPv6'
2016-02-17 09:09:32 +00:00
],
[
2014-04-24 10:05:52 +00:00
'<a href="/wiki/Special:Contributions/::1" '
2016-05-25 23:37:21 +00:00
. 'class="mw-userlink mw-anonuserlink" '
2016-08-31 20:58:05 +00:00
. 'title="Special:Contributions/::1"><bdi>AlternativeUsername</bdi></a>' ,
2012-05-22 23:30:07 +00:00
0 , '::1' , 'AlternativeUsername' ,
'Anonymous with pretty IPv6 and an alternative username'
2016-02-17 09:09:32 +00:00
],
2012-05-22 23:30:07 +00:00
# IPV4
2016-02-17 09:09:32 +00:00
[
2014-04-24 10:05:52 +00:00
'<a href="/wiki/Special:Contributions/127.0.0.1" '
2016-05-25 23:37:21 +00:00
. 'class="mw-userlink mw-anonuserlink" '
2016-08-31 20:58:05 +00:00
. 'title="Special:Contributions/127.0.0.1"><bdi>127.0.0.1</bdi></a>' ,
2012-05-22 23:30:07 +00:00
0 , '127.0.0.1' , false ,
'Anonymous with IPv4'
2016-02-17 09:09:32 +00:00
],
[
2014-04-24 10:05:52 +00:00
'<a href="/wiki/Special:Contributions/127.0.0.1" '
2016-05-25 23:37:21 +00:00
. 'class="mw-userlink mw-anonuserlink" '
2016-08-31 20:58:05 +00:00
. 'title="Special:Contributions/127.0.0.1"><bdi>AlternativeUsername</bdi></a>' ,
2012-05-22 23:30:07 +00:00
0 , '127.0.0.1' , 'AlternativeUsername' ,
'Anonymous with IPv4 and an alternative username'
2016-02-17 09:09:32 +00:00
],
2012-05-22 23:30:07 +00:00
2021-02-11 14:18:01 +00:00
# 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'
],
2021-01-28 17:02:05 +00:00
# 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 \n Bar</bdi></span> " ,
0 , " Foo \n Bar " , 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'
],
2015-09-11 13:44:59 +00:00
# ## Regular user ##########################################
2012-05-22 23:30:07 +00:00
# TODO!
2016-02-17 09:09:32 +00:00
];
2012-05-22 23:30:07 +00:00
}
2014-06-10 19:37:37 +00:00
2019-05-06 08:58:09 +00:00
/**
* @ 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 , '' ],
];
}
2014-06-10 19:37:37 +00:00
/**
* @ dataProvider provideCasesForFormatComment
* @ covers Linker :: formatComment
* @ covers Linker :: formatAutocomments
* @ covers Linker :: formatLinksInComment
*/
2015-09-26 18:09:08 +00:00
public function testFormatComment (
$expected , $comment , $title = false , $local = false , $wikiId = null
) {
2015-09-07 17:02:24 +00:00
$conf = new SiteConfiguration ();
2016-02-17 09:09:32 +00:00
$conf -> settings = [
'wgServer' => [
2015-09-07 17:02:24 +00:00
'enwiki' => '//en.example.org' ,
'dewiki' => '//de.example.org' ,
2016-02-17 09:09:32 +00:00
],
'wgArticlePath' => [
2015-09-07 17:02:24 +00:00
'enwiki' => '/w/$1' ,
'dewiki' => '/w/$1' ,
2016-02-17 09:09:32 +00:00
],
];
$conf -> suffixes = [ 'wiki' ];
2015-09-07 17:02:24 +00:00
2016-02-17 09:09:32 +00:00
$this -> setMwGlobals ( [
2014-06-24 07:39:17 +00:00
'wgScript' => '/wiki/index.php' ,
2014-06-10 19:37:37 +00:00
'wgArticlePath' => '/wiki/$1' ,
2014-07-07 23:21:09 +00:00
'wgCapitalLinks' => true ,
2015-09-07 17:02:24 +00:00
'wgConf' => $conf ,
2019-10-06 19:22:13 +00:00
// TODO: update tests when the default changes
'wgFragmentMode' => [ 'legacy' ],
2016-02-17 09:09:32 +00:00
] );
2014-06-10 19:37:37 +00:00
if ( $title === false ) {
// We need a page title that exists
$title = Title :: newFromText ( 'Special:BlankPage' );
}
$this -> assertEquals (
$expected ,
2015-09-07 17:02:24 +00:00
Linker :: formatComment ( $comment , $title , $local , $wikiId )
2014-06-10 19:37:37 +00:00
);
}
2015-09-07 17:02:24 +00:00
public function provideCasesForFormatComment () {
$wikiId = 'enwiki' ; // $wgConf has a fake entry for this
2018-01-01 13:10:16 +00:00
// phpcs:disable Generic.Files.LineLength
2016-03-19 01:05:19 +00:00
return [
2014-06-10 19:37:37 +00:00
// Linker::formatComment
2016-03-19 01:05:19 +00:00
[
2014-06-10 19:37:37 +00:00
'a<script>b' ,
'a<script>b' ,
2016-03-19 01:05:19 +00:00
],
[
2014-06-10 19:37:37 +00:00
'a—b' ,
'a—b' ,
2016-03-19 01:05:19 +00:00
],
[
2014-06-10 19:37:37 +00:00
" '''not bolded''' " ,
" '''not bolded''' " ,
2016-03-19 01:05:19 +00:00
],
[
2015-09-07 17:02:24 +00:00
" try <script>evil</scipt> things " ,
" try <script>evil</scipt> things " ,
2016-03-19 01:05:19 +00:00
],
2014-06-10 19:37:37 +00:00
// Linker::formatAutocomments
2016-03-19 01:05:19 +00:00
[
2018-12-01 08:47:03 +00:00
'<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→ autocomment</a></span></span>' ,
2014-06-10 19:37:37 +00:00
" /* autocomment */ " ,
2016-03-19 01:05:19 +00:00
],
[
2018-12-01 09:19:33 +00:00
'<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→[[linkie?]]</a></span></span>' ,
2014-06-10 19:37:37 +00:00
" /* [[linkie?]] */ " ,
2016-03-19 01:05:19 +00:00
],
2019-05-19 11:17:23 +00:00
[
2019-05-21 10:16:22 +00:00
'<span dir="auto"><span class="autocomment">: </span> // Edit via via</span>' ,
2019-05-19 11:17:23 +00:00
// Regression test for T222857
" /* */ // Edit via via " ,
],
2019-10-06 19:16:53 +00:00
[
'<span dir="auto"><span class="autocomment">: </span> foobar</span>' ,
// Regression test for T222857
" /**/ foobar " ,
],
2016-03-19 01:05:19 +00:00
[
2018-12-01 08:47:03 +00:00
'<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→ autocomment</a>: </span> post</span>' ,
2014-06-10 19:37:37 +00:00
" /* autocomment */ post " ,
2016-03-19 01:05:19 +00:00
],
[
2018-12-01 08:47:03 +00:00
'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→ autocomment</a></span></span>' ,
2014-06-10 19:37:37 +00:00
" pre /* autocomment */ " ,
2016-03-19 01:05:19 +00:00
],
[
2018-12-01 08:47:03 +00:00
'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→ autocomment</a>: </span> post</span>' ,
2014-06-10 19:37:37 +00:00
" pre /* autocomment */ post " ,
2016-03-19 01:05:19 +00:00
],
[
2018-12-01 08:47:03 +00:00
'<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→ autocomment</a>: </span> multiple? <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment2" title="Special:BlankPage">→ autocomment2</a></span></span></span>' ,
2018-11-21 22:48:16 +00:00
" /* autocomment */ multiple? /* autocomment2 */ " ,
2016-03-19 01:05:19 +00:00
],
[
2018-12-01 08:47:03 +00:00
'<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→ autocomment containing /*</a>: </span> T70361</span>' ,
2014-09-09 15:25:10 +00:00
" /* autocomment containing /* */ T70361 "
2016-03-19 01:05:19 +00:00
],
[
2018-12-01 08:47:03 +00:00
'<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→ autocomment containing "quotes"</a></span></span>' ,
2015-09-07 17:02:24 +00:00
" /* autocomment containing \" quotes \" */ "
2016-03-19 01:05:19 +00:00
],
[
2018-12-01 08:47:03 +00:00
'<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.3Cscript.3Etags.3C.2Fscript.3E" title="Special:BlankPage">→ autocomment containing <script>tags</script></a></span></span>' ,
2015-09-07 17:02:24 +00:00
" /* autocomment containing <script>tags</script> */ "
2016-03-19 01:05:19 +00:00
],
[
2018-12-01 08:47:03 +00:00
'<span dir="auto"><span class="autocomment"><a href="#autocomment">→ autocomment</a></span></span>' ,
2014-06-10 19:37:37 +00:00
" /* autocomment */ " ,
false , true
2016-03-19 01:05:19 +00:00
],
[
2018-12-01 08:47:03 +00:00
'<span dir="auto"><span class="autocomment">autocomment</span></span>' ,
2014-06-10 19:37:37 +00:00
" /* autocomment */ " ,
null
2016-03-19 01:05:19 +00:00
],
2019-05-21 10:16:22 +00:00
[
'' ,
" /* */ " ,
false , true
],
[
'' ,
" /* */ " ,
null
],
[
'<span dir="auto"><span class="autocomment">[[</span></span>' ,
" /* [[ */ " ,
false , true
],
[
'<span dir="auto"><span class="autocomment">[[</span></span>' ,
" /* [[ */ " ,
null
],
[
" foo <span dir= \" auto \" ><span class= \" autocomment \" ><a href= \" #.23 \" >→[[#_ \t _]]</a></span></span> " ,
" foo /* [[#_ \t _]] */ " ,
false , true
],
[
" foo <span dir= \" auto \" ><span class= \" autocomment \" ><a href= \" #_.09 \" >#_ \t _</a></span></span> " ,
" foo /* [[#_ \t _]] */ " ,
null
],
2016-03-19 01:05:19 +00:00
[
2018-12-01 08:47:03 +00:00
'<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→ autocomment</a></span></span>' ,
2015-09-07 17:02:24 +00:00
" /* autocomment */ " ,
false , false
2016-03-19 01:05:19 +00:00
],
[
2018-12-01 08:47:03 +00:00
'<span dir="auto"><span class="autocomment"><a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→ autocomment</a></span></span>' ,
2015-09-07 17:02:24 +00:00
" /* autocomment */ " ,
false , false , $wikiId
2016-03-19 01:05:19 +00:00
],
2014-06-10 19:37:37 +00:00
// Linker::formatLinksInComment
2016-03-19 01:05:19 +00:00
[
2014-06-10 19:37:37 +00:00
'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def' ,
" abc [[link]] def " ,
2016-03-19 01:05:19 +00:00
],
[
2014-06-10 19:37:37 +00:00
'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">text</a> def' ,
" abc [[link|text]] def " ,
2016-03-19 01:05:19 +00:00
],
[
2014-06-10 19:37:37 +00:00
'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def' ,
" abc [[Special:BlankPage|]] def " ,
2016-03-19 01:05:19 +00:00
],
[
2014-06-10 19:37:37 +00:00
'abc <a href="/wiki/index.php?title=%C4%84%C5%9B%C5%BC&action=edit&redlink=1" class="new" title="Ąśż (page does not exist)">ąśż</a> def' ,
" abc [[%C4%85%C5%9B%C5%BC]] def " ,
2016-03-19 01:05:19 +00:00
],
[
2014-06-10 19:37:37 +00:00
'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def' ,
" abc [[#section]] def " ,
2016-03-19 01:05:19 +00:00
],
[
2014-06-10 19:37:37 +00:00
'abc <a href="/wiki/index.php?title=/subpage&action=edit&redlink=1" class="new" title="/subpage (page does not exist)">/subpage</a> def' ,
" abc [[/subpage]] def " ,
2016-03-19 01:05:19 +00:00
],
[
2015-09-07 17:02:24 +00:00
'abc <a href="/wiki/index.php?title=%22evil!%22&action=edit&redlink=1" class="new" title=""evil!" (page does not exist)">"evil!"</a> def' ,
" abc [[ \" evil! \" ]] def " ,
2016-03-19 01:05:19 +00:00
],
[
2015-09-07 17:02:24 +00:00
'abc [[<script>very evil</script>]] def' ,
" abc [[<script>very evil</script>]] def " ,
2016-03-19 01:05:19 +00:00
],
[
2015-09-07 17:02:24 +00:00
'abc [[|]] def' ,
" abc [[|]] def " ,
2016-03-19 01:05:19 +00:00
],
[
2015-09-07 17:02:24 +00:00
'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def' ,
" abc [[link]] def " ,
false , false
2016-03-19 01:05:19 +00:00
],
[
2015-09-07 17:02:24 +00:00
'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def' ,
" abc [[link]] def " ,
false , false , $wikiId
2016-03-19 01:05:19 +00:00
],
];
2018-01-01 13:10:16 +00:00
// phpcs:enable
2014-06-10 19:37:37 +00:00
}
2015-01-02 18:33:04 +00:00
/**
* @ covers Linker :: formatLinksInComment
* @ dataProvider provideCasesForFormatLinksInComment
*/
public function testFormatLinksInComment ( $expected , $input , $wiki ) {
$conf = new SiteConfiguration ();
2016-02-17 09:09:32 +00:00
$conf -> settings = [
'wgServer' => [
2015-01-02 18:33:04 +00:00
'enwiki' => '//en.example.org'
2016-02-17 09:09:32 +00:00
],
'wgArticlePath' => [
2015-01-02 18:33:04 +00:00
'enwiki' => '/w/$1' ,
2016-02-17 09:09:32 +00:00
],
];
$conf -> suffixes = [ 'wiki' ];
$this -> setMwGlobals ( [
2015-01-02 18:33:04 +00:00
'wgScript' => '/wiki/index.php' ,
'wgArticlePath' => '/wiki/$1' ,
'wgCapitalLinks' => true ,
'wgConf' => $conf ,
2016-02-17 09:09:32 +00:00
] );
2015-01-02 18:33:04 +00:00
$this -> assertEquals (
$expected ,
Linker :: formatLinksInComment ( $input , Title :: newFromText ( 'Special:BlankPage' ), false , $wiki )
);
}
2019-02-05 13:31:53 +00:00
/**
* @ covers Linker :: generateRollback
* @ dataProvider provideCasesForRollbackGeneration
*/
2019-05-29 21:05:10 +00:00
public function testGenerateRollback ( $rollbackEnabled , $expectedModules , $title ) {
2019-02-05 13:31:53 +00:00
$this -> markTestSkippedIfDbType ( 'postgres' );
$context = RequestContext :: getMain ();
$user = $context -> getUser ();
$user -> setOption ( 'showrollbackconfirmation' , $rollbackEnabled );
2019-09-17 14:31:49 +00:00
$this -> assertSame ( 0 , Title :: newFromText ( $title ) -> getArticleID () );
2019-05-29 21:05:10 +00:00
$pageData = $this -> insertPage ( $title );
2019-02-05 13:31:53 +00:00
$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 );
2020-04-18 00:21:26 +00:00
$rollbackOutput = Linker :: generateRollback ( $page -> getRevisionRecord (), $context );
2019-02-05 13:31:53 +00:00
$modules = $context -> getOutput () -> getModules ();
2020-04-18 02:39:58 +00:00
$currentRev = $page -> getRevisionRecord ();
2020-03-07 07:07:42 +00:00
$revisionLookup = MediaWikiServices :: getInstance () -> getRevisionLookup ();
$oldestRev = $revisionLookup -> getFirstRevision ( $page -> getTitle () );
2019-02-05 13:31:53 +00:00
$this -> assertEquals ( $expectedModules , $modules );
2020-04-18 02:39:58 +00:00
$this -> assertInstanceOf ( RevisionRecord :: class , $currentRev );
$this -> assertInstanceOf ( User :: class , $currentRev -> getUser () );
$this -> assertEquals ( $user -> getName (), $currentRev -> getUser () -> getName () );
2019-05-29 21:05:10 +00:00
$this -> assertEquals (
static :: getTestSysop () -> getUser (),
2020-03-07 07:07:42 +00:00
$oldestRev -> getUser () -> getName ()
2019-05-29 21:05:10 +00:00
);
$ids = [];
$r = $oldestRev ;
while ( $r ) {
$ids [] = $r -> getId ();
2020-03-07 07:07:42 +00:00
$r = $revisionLookup -> getNextRevision ( $r );
2019-05-29 21:05:10 +00:00
}
$this -> assertEquals ( [ $oldestRev -> getId (), $currentRev -> getId () ], $ids );
2019-12-14 10:27:56 +00:00
$this -> assertStringContainsString ( 'rollback 1 edit' , $rollbackOutput );
2019-02-05 13:31:53 +00:00
}
public static function provideCasesForRollbackGeneration () {
return [
[
true ,
2020-05-23 01:30:33 +00:00
[ 'mediawiki.misc-authed-curate' ],
2019-05-29 21:05:10 +00:00
'Rollback_Test_Page'
2019-02-05 13:31:53 +00:00
],
[
false ,
2019-05-29 21:05:10 +00:00
[],
'Rollback_Test_Page2'
2019-02-05 13:31:53 +00:00
]
];
}
2015-01-02 18:33:04 +00:00
public static function provideCasesForFormatLinksInComment () {
2018-01-01 13:10:16 +00:00
// phpcs:disable Generic.Files.LineLength
2016-03-19 01:05:19 +00:00
return [
[
2015-01-02 18:33:04 +00:00
'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>' ,
'foo bar [[Special:BlankPage]]' ,
null ,
2016-03-19 01:05:19 +00:00
],
2018-06-26 09:55:21 +00:00
[
'<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>' ,
'[[ :Special:BlankPage]]' ,
null ,
],
2018-06-22 15:20:23 +00:00
[
'[[Foo<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>' ,
'[[Foo[[Special:BlankPage]]' ,
null ,
],
2016-03-19 01:05:19 +00:00
[
2015-01-14 23:48:54 +00:00
'<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>' ,
" [[Foo'bar]] " ,
'enwiki' ,
2016-03-19 01:05:19 +00:00
],
[
2015-01-02 18:33:04 +00:00
'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>' ,
'foo bar [[Special:BlankPage]]' ,
'enwiki' ,
2016-03-19 01:05:19 +00:00
],
2017-06-29 13:06:24 +00:00
[
'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>' ,
'foo bar [[Image:Example]]' ,
'enwiki' ,
],
2016-03-19 01:05:19 +00:00
];
2018-01-01 13:10:16 +00:00
// phpcs:enable
2015-01-02 18:33:04 +00:00
}
2016-05-11 04:39:22 +00:00
2020-05-18 19:54:49 +00:00
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 );
}
2012-05-22 23:30:07 +00:00
}