3 use MediaWiki\Linker\LinkRenderer
;
4 use MediaWiki\Linker\LinkRendererFactory
;
5 use MediaWiki\MediaWikiServices
;
8 * @covers MediaWiki\Linker\LinkRenderer
10 class LinkRendererTest
extends MediaWikiLangTestCase
{
13 * @var LinkRendererFactory
17 public function setUp() {
19 $this->setMwGlobals( [
20 'wgArticlePath' => '/wiki/$1',
21 'wgServer' => '//example.org',
22 'wgCanonicalServer' => 'http://example.org',
23 'wgScriptPath' => '/w',
24 'wgScript' => '/w/index.php',
26 $this->factory
= MediaWikiServices
::getInstance()->getLinkRendererFactory();
30 public function testMergeAttribs() {
31 $target = new TitleValue( NS_SPECIAL
, 'Blankpage' );
32 $linkRenderer = $this->factory
->create();
33 $link = $linkRenderer->makeBrokenLink( $target, null, [
36 // Suppresses href attribute
42 '<a href="/wiki/Special:BlankPage" class="new foobar" '
43 . 'title="Special:BlankPage (page does not exist)" bar="baz">'
44 . 'Special:BlankPage</a>',
49 public function testMakeKnownLink() {
50 $target = new TitleValue( NS_MAIN
, 'Foobar' );
51 $linkRenderer = $this->factory
->create();
55 '<a href="/w/index.php?title=Foobar&foo=bar" '. 'title="Foobar">Foobar</a>',
56 $linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
60 $linkRenderer->setForceArticlePath( true );
62 '<a href="/wiki/Foobar?foo=bar" title="Foobar">Foobar</a>',
63 $linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
67 $linkRenderer->setForceArticlePath( false );
68 $linkRenderer->setExpandURLs( PROTO_HTTPS
);
70 '<a href="https://example.org/wiki/Foobar" title="Foobar">Foobar</a>',
71 $linkRenderer->makeKnownLink( $target )
75 public function testMakeBrokenLink() {
76 $target = new TitleValue( NS_MAIN
, 'Foobar' );
77 $special = new TitleValue( NS_SPECIAL
, 'Foobar' );
78 $linkRenderer = $this->factory
->create();
80 // action=edit&redlink=1 added
82 '<a href="/w/index.php?title=Foobar&action=edit&redlink=1" '
83 . 'class="new" title="Foobar (page does not exist)">Foobar</a>',
84 $linkRenderer->makeBrokenLink( $target )
87 // action=edit&redlink=1 not added due to action query parameter
89 '<a href="/w/index.php?title=Foobar&action=foobar" class="new" '
90 . 'title="Foobar (page does not exist)">Foobar</a>',
91 $linkRenderer->makeBrokenLink( $target, null, [], [ 'action' => 'foobar' ] )
94 // action=edit&redlink=1 not added due to NS_SPECIAL
96 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
97 . '(page does not exist)">Special:Foobar</a>',
98 $linkRenderer->makeBrokenLink( $special )
103 '<a href="/w/index.php?title=Foobar&action=edit&redlink=1" '
104 . 'class="new" title="Foobar (page does not exist)">Foobar</a>',
105 $linkRenderer->makeBrokenLink( $target->createFragmentTarget( 'foobar' ) )
109 public function testMakeLink() {
110 $linkRenderer = $this->factory
->create();
111 $foobar = new TitleValue( NS_SPECIAL
, 'Foobar' );
112 $blankpage = new TitleValue( NS_SPECIAL
, 'Blankpage' );
114 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
115 . '(page does not exist)">foo</a>',
116 $linkRenderer->makeLink( $foobar, 'foo' )
120 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">blank</a>',
121 $linkRenderer->makeLink( $blankpage, 'blank' )
125 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
126 . '(page does not exist)"><script>evil()</script></a>',
127 $linkRenderer->makeLink( $foobar, '<script>evil()</script>' )
131 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
132 . '(page does not exist)"><script>evil()</script></a>',
133 $linkRenderer->makeLink( $foobar, new HtmlArmor( '<script>evil()</script>' ) )
137 public function testGetLinkClasses() {
138 $wanCache = ObjectCache
::getMainWANInstance();
139 $titleFormatter = MediaWikiServices
::getInstance()->getTitleFormatter();
140 $linkCache = new LinkCache( $titleFormatter, $wanCache );
141 $foobarTitle = new TitleValue( NS_MAIN
, 'FooBar' );
142 $redirectTitle = new TitleValue( NS_MAIN
, 'Redirect' );
143 $userTitle = new TitleValue( NS_USER
, 'Someuser' );
144 $linkCache->addGoodLinkObj(
150 $linkCache->addGoodLinkObj(
157 $linkCache->addGoodLinkObj(
164 $linkRenderer = new LinkRenderer( $titleFormatter, $linkCache );
165 $linkRenderer->setStubThreshold( 0 );
168 $linkRenderer->getLinkClasses( $foobarTitle )
171 $linkRenderer->setStubThreshold( 20 );
174 $linkRenderer->getLinkClasses( $foobarTitle )
177 $linkRenderer->setStubThreshold( 0 );
180 $linkRenderer->getLinkClasses( $redirectTitle )
183 $linkRenderer->setStubThreshold( 20 );
186 $linkRenderer->getLinkClasses( $userTitle )