Merge "Do not add limit to ApiQueryPagePropNames when database type is mysql"
[lhc/web/wiklou.git] / tests / phpunit / includes / LinkerTest.php
index e50b4f1..3edf99f 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * @group Database
  */
@@ -13,11 +15,12 @@ class LinkerTest extends MediaWikiLangTestCase {
        public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
                $this->setMwGlobals( [
                        'wgArticlePath' => '/wiki/$1',
-                       'wgWellFormedXml' => true,
                ] );
 
-               $this->assertEquals( $expected,
-                       Linker::userLink( $userId, $userName, $altUserName, $msg )
+               $this->assertEquals(
+                       $expected,
+                       Linker::userLink( $userId, $userName, $altUserName ),
+                       $msg
                );
        }
 
@@ -33,35 +36,35 @@ class LinkerTest extends MediaWikiLangTestCase {
                        # ## ANONYMOUS USER ########################################
                        [
                                '<a href="/wiki/Special:Contributions/JohnDoe" '
-                                       . 'title="Special:Contributions/JohnDoe" '
-                                       . 'class="mw-userlink mw-anonuserlink">JohnDoe</a>',
+                                       . 'class="mw-userlink mw-anonuserlink" '
+                                       . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
                                0, 'JohnDoe', false,
                        ],
                        [
                                '<a href="/wiki/Special:Contributions/::1" '
-                                       . 'title="Special:Contributions/::1" '
-                                       . 'class="mw-userlink mw-anonuserlink">::1</a>',
+                                       . '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" '
-                                       . 'title="Special:Contributions/0:0:0:0:0:0:0:1" '
-                                       . 'class="mw-userlink mw-anonuserlink">::1</a>',
+                                       . '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" '
-                                       . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
-                                       . 'class="mw-userlink mw-anonuserlink">::1</a>',
+                                       . '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" '
-                                       . 'title="Special:Contributions/::1" '
-                                       . 'class="mw-userlink mw-anonuserlink">AlternativeUsername</a>',
+                                       . 'class="mw-userlink mw-anonuserlink" '
+                                       . 'title="Special:Contributions/::1"><bdi>AlternativeUsername</bdi></a>',
                                0, '::1', 'AlternativeUsername',
                                'Anonymous with pretty IPv6 and an alternative username'
                        ],
@@ -69,15 +72,15 @@ class LinkerTest extends MediaWikiLangTestCase {
                        # IPV4
                        [
                                '<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>',
+                                       . '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" '
-                                       . 'title="Special:Contributions/127.0.0.1" '
-                                       . 'class="mw-userlink mw-anonuserlink">AlternativeUsername</a>',
+                                       . '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'
                        ],
@@ -112,7 +115,6 @@ class LinkerTest extends MediaWikiLangTestCase {
                $this->setMwGlobals( [
                        'wgScript' => '/wiki/index.php',
                        'wgArticlePath' => '/wiki/$1',
-                       'wgWellFormedXml' => true,
                        'wgCapitalLinks' => true,
                        'wgConf' => $conf,
                ] );
@@ -277,7 +279,6 @@ class LinkerTest extends MediaWikiLangTestCase {
                $this->setMwGlobals( [
                        'wgScript' => '/wiki/index.php',
                        'wgArticlePath' => '/wiki/$1',
-                       'wgWellFormedXml' => true,
                        'wgCapitalLinks' => true,
                        'wgConf' => $conf,
                ] );
@@ -309,4 +310,168 @@ class LinkerTest extends MediaWikiLangTestCase {
                ];
                // @codingStandardsIgnoreEnd
        }
+
+       public static function provideLinkBeginHook() {
+               // @codingStandardsIgnoreStart 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&amp;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'
+                       ],
+               ];
+               // @codingStandardsIgnoreEnd
+       }
+
+       /**
+        * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
+        * @dataProvider provideLinkBeginHook
+        */
+       public function testLinkBeginHook( $callback, $expected ) {
+               $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->setMwGlobals( [
+                       'wgArticlePath' => '/wiki/$1',
+               ] );
+
+               $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
+
+               $title = SpecialPage::getTitleFor( 'Blankpage' );
+               $out = Linker::link( $title );
+               $this->assertEquals( $expected, $out );
+       }
+
+       /**
+        * @covers Linker::getLinkColour
+        */
+       public function testGetLinkColour() {
+               $this->hideDeprecated( 'Linker::getLinkColour' );
+               $linkCache = MediaWikiServices::getInstance()->getLinkCache();
+               $foobarTitle = Title::makeTitle( NS_MAIN, 'FooBar' );
+               $redirectTitle = Title::makeTitle( NS_MAIN, 'Redirect' );
+               $userTitle = Title::makeTitle( NS_USER, 'Someuser' );
+               $linkCache->addGoodLinkObj(
+                       1, // id
+                       $foobarTitle,
+                       10, // len
+                       0 // redir
+               );
+               $linkCache->addGoodLinkObj(
+                       2, // id
+                       $redirectTitle,
+                       10, // len
+                       1 // redir
+               );
+
+               $linkCache->addGoodLinkObj(
+                       3, // id
+                       $userTitle,
+                       10, // len
+                       0 // redir
+               );
+
+               $this->assertEquals(
+                       '',
+                       Linker::getLinkColour( $foobarTitle, 0 )
+               );
+
+               $this->assertEquals(
+                       'stub',
+                       Linker::getLinkColour( $foobarTitle, 20 )
+               );
+
+               $this->assertEquals(
+                       'mw-redirect',
+                       Linker::getLinkColour( $redirectTitle, 0 )
+               );
+
+               $this->assertEquals(
+                       '',
+                       Linker::getLinkColour( $userTitle, 20 )
+               );
+       }
 }