123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- <?php
- /**
- * @group Database
- */
- class LinkerTest extends MediaWikiLangTestCase {
- /**
- * @dataProvider provideCasesForUserLink
- * @covers Linker::userLink
- */
- public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
- $this->setMwGlobals( [
- 'wgArticlePath' => '/wiki/$1',
- ] );
- $this->assertEquals(
- $expected,
- Linker::userLink( $userId, $userName, $altUserName ),
- $msg
- );
- }
- public static function provideCasesForUserLink() {
- # Format:
- # - expected
- # - userid
- # - username
- # - optional altUserName
- # - optional message
- return [
- # ## 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'
- ],
- # ## Regular user ##########################################
- # TODO!
- ];
- }
- /**
- * @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,
- ] );
- 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<script>b',
- ],
- [
- 'a—b',
- 'a—b',
- ],
- [
- "'''not bolded'''",
- "'''not bolded'''",
- ],
- [
- "try <script>evil</scipt> things",
- "try <script>evil</scipt> things",
- ],
- // Linker::formatAutocomments
- [
- '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
- "/* autocomment */",
- ],
- [
- '<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?]] */",
- ],
- [
- '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
- "/* autocomment */ post",
- ],
- [
- 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
- "pre /* autocomment */",
- ],
- [
- 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
- "pre /* autocomment */ post",
- ],
- [
- '<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>',
- "/* autocomment */ multiple? /* autocomment2 */ ",
- ],
- [
- '<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"
- ],
- [
- '<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\" */"
- ],
- [
- '<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> */"
- ],
- [
- '<a href="#autocomment">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
- "/* autocomment */",
- false, true
- ],
- [
- '<span dir="auto"><span class="autocomment">autocomment</span></span>',
- "/* autocomment */",
- null
- ],
- [
- '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
- "/* autocomment */",
- false, false
- ],
- [
- '<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
- ],
- // Linker::formatLinksInComment
- [
- '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",
- ],
- [
- '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",
- ],
- [
- 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
- "abc [[Special:BlankPage|]] def",
- ],
- [
- '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",
- ],
- [
- 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
- "abc [[#section]] def",
- ],
- [
- '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",
- ],
- [
- '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",
- ],
- [
- 'abc [[<script>very evil</script>]] def',
- "abc [[<script>very evil</script>]] def",
- ],
- [
- 'abc [[|]] def',
- "abc [[|]] def",
- ],
- [
- '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
- ],
- [
- 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> 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 )
- );
- }
- public static function provideCasesForFormatLinksInComment() {
- // phpcs:disable Generic.Files.LineLength
- return [
- [
- 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
- 'foo bar [[Special:BlankPage]]',
- null,
- ],
- [
- '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
- '[[ :Special:BlankPage]]',
- null,
- ],
- [
- '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
- "[[Foo'bar]]",
- 'enwiki',
- ],
- [
- 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
- 'foo bar [[Special:BlankPage]]',
- 'enwiki',
- ],
- [
- 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
- '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';
- },
- '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
- ],
- // Modify $attribs
- [
- function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
- $attribs['bar'] = 'baz';
- },
- '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
- ],
- // Modify $query
- [
- function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
- $query['bar'] = 'baz';
- },
- '<a href="/w/index.php?title=Special:BlankPage&bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
- ],
- // Force HTTP $options
- [
- function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
- $options = [ 'http' ];
- },
- '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
- ],
- // Force 'forcearticlepath' in $options
- [
- function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
- $options = [ 'forcearticlepath' ];
- $query['foo'] = 'bar';
- },
- '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
- ],
- // 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';
- },
- '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
- ],
- // Modify $attribs
- [
- function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
- $attribs['bar'] = 'baz';
- },
- '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
- ],
- // 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 );
- }
- }
|