2012-05-22 23:30:07 +00:00
< ? php
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' ,
2012-12-09 10:56:45 +00:00
'wgWellFormedXml' => true ,
2016-02-17 09:09:32 +00:00
] );
2012-05-22 23:30:07 +00:00
$this -> assertEquals ( $expected ,
Linker :: userLink ( $userId , $userName , $altUserName , $msg )
);
}
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 [
2012-05-22 23:30:07 +00:00
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" '
. 'title="Special:Contributions/JohnDoe" '
. 'class="mw-userlink mw-anonuserlink">JohnDoe</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" '
. 'title="Special:Contributions/::1" '
. 'class="mw-userlink mw-anonuserlink">::1</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" '
. 'title="Special:Contributions/0:0:0:0:0:0:0:1" '
. 'class="mw-userlink mw-anonuserlink">::1</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" '
. 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
. 'class="mw-userlink mw-anonuserlink">::1</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" '
. 'title="Special:Contributions/::1" '
. 'class="mw-userlink mw-anonuserlink">AlternativeUsername</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" '
. 'title="Special:Contributions/127.0.0.1" '
. 'class="mw-userlink mw-anonuserlink">127.0.0.1</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" '
. 'title="Special:Contributions/127.0.0.1" '
. 'class="mw-userlink mw-anonuserlink">AlternativeUsername</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
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
/**
* @ 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' ,
'wgWellFormedXml' => true ,
2014-07-07 23:21:09 +00:00
'wgCapitalLinks' => true ,
2015-09-07 17:02:24 +00:00
'wgConf' => $conf ,
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
2015-10-03 13:04:51 +00:00
// @codingStandardsIgnoreStart 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
[
2014-06-10 19:37:37 +00:00
'<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a> <span dir="auto"><span class="autocomment">autocomment</span></span>' ,
" /* autocomment */ " ,
2016-03-19 01:05:19 +00:00
],
[
2014-06-10 19:37:37 +00:00
'<a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→</a> <span dir="auto"><span class="autocomment"><a href="/wiki/index.php?title=Linkie%3F&action=edit&redlink=1" class="new" title="Linkie? (page does not exist)">linkie?</a></span></span>' ,
" /* [[linkie?]] */ " ,
2016-03-19 01:05:19 +00:00
],
[
2014-06-10 19:37:37 +00:00
'<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a> <span dir="auto"><span class="autocomment">autocomment: </span> post</span>' ,
" /* autocomment */ post " ,
2016-03-19 01:05:19 +00:00
],
[
2014-06-10 19:37:37 +00:00
'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a> <span dir="auto"><span class="autocomment">autocomment</span></span>' ,
" pre /* autocomment */ " ,
2016-03-19 01:05:19 +00:00
],
[
2014-06-10 19:37:37 +00:00
'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a> <span dir="auto"><span class="autocomment">autocomment: </span> post</span>' ,
" pre /* autocomment */ post " ,
2016-03-19 01:05:19 +00:00
],
[
2014-09-09 15:25:10 +00:00
'<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a> <span dir="auto"><span class="autocomment">autocomment: </span> multiple? <a href="/wiki/Special:BlankPage#autocomment2" title="Special:BlankPage">→</a> <span dir="auto"><span class="autocomment">autocomment2: </span> </span></span>' ,
2014-06-10 19:37:37 +00:00
" /* autocomment */ multiple? /* autocomment2 */ " ,
2016-03-19 01:05:19 +00:00
],
[
2014-09-09 15:25:10 +00:00
'<a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→</a> <span dir="auto"><span class="autocomment">autocomment containing /*: </span> T70361</span>' ,
" /* autocomment containing /* */ T70361 "
2016-03-19 01:05:19 +00:00
],
[
2015-09-07 17:02:24 +00:00
'<a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→</a> <span dir="auto"><span class="autocomment">autocomment containing "quotes"</span></span>' ,
" /* autocomment containing \" quotes \" */ "
2016-03-19 01:05:19 +00:00
],
[
2015-09-07 17:02:24 +00:00
'<a href="/wiki/Special:BlankPage#autocomment_containing_.3Cscript.3Etags.3C.2Fscript.3E" title="Special:BlankPage">→</a> <span dir="auto"><span class="autocomment">autocomment containing <script>tags</script></span></span>' ,
" /* autocomment containing <script>tags</script> */ "
2016-03-19 01:05:19 +00:00
],
[
2014-06-10 19:37:37 +00:00
'<a href="#autocomment">→</a> <span dir="auto"><span class="autocomment">autocomment</span></span>' ,
" /* autocomment */ " ,
false , true
2016-03-19 01:05:19 +00:00
],
[
2014-06-10 19:37:37 +00:00
' <span dir="auto"><span class="autocomment">autocomment</span></span>' ,
" /* autocomment */ " ,
null
2016-03-19 01:05:19 +00:00
],
[
2015-09-07 17:02:24 +00:00
'<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a> <span dir="auto"><span class="autocomment">autocomment</span></span>' ,
" /* autocomment */ " ,
false , false
2016-03-19 01:05:19 +00:00
],
[
2015-09-07 17:02:24 +00:00
'<a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→</a> <span dir="auto"><span class="autocomment">autocomment</span></span>' ,
" /* 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
],
];
2015-10-03 13:04:51 +00:00
// @codingStandardsIgnoreEnd
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' ,
'wgWellFormedXml' => true ,
'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 )
);
}
public static function provideCasesForFormatLinksInComment () {
2015-10-03 13:44:47 +00:00
// @codingStandardsIgnoreStart 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
],
[
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
],
];
2015-10-03 13:44:47 +00:00
// @codingStandardsIgnoreEnd
2015-01-02 18:33:04 +00:00
}
2012-05-22 23:30:07 +00:00
}