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();
29 public function testMergeAttribs() {
30 $target = new TitleValue( NS_SPECIAL
, 'Blankpage' );
31 $linkRenderer = $this->factory
->create();
32 $link = $linkRenderer->makeBrokenLink( $target, null, [
35 // Suppresses href attribute
41 '<a href="/wiki/Special:BlankPage" class="new foobar" '
42 . 'title="Special:BlankPage (page does not exist)" bar="baz">'
43 . 'Special:BlankPage</a>',
48 public function testMakeKnownLink() {
49 $target = new TitleValue( NS_MAIN
, 'Foobar' );
50 $linkRenderer = $this->factory
->create();
54 '<a href="/w/index.php?title=Foobar&foo=bar" title="Foobar">Foobar</a>',
55 $linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
58 $linkRenderer->setForceArticlePath( true );
60 '<a href="/wiki/Foobar?foo=bar" title="Foobar">Foobar</a>',
61 $linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
65 $linkRenderer->setForceArticlePath( false );
66 $linkRenderer->setExpandURLs( PROTO_HTTPS
);
68 '<a href="https://example.org/wiki/Foobar" title="Foobar">Foobar</a>',
69 $linkRenderer->makeKnownLink( $target )
73 public function testMakeBrokenLink() {
74 $target = new TitleValue( NS_MAIN
, 'Foobar' );
75 $special = new TitleValue( NS_SPECIAL
, 'Foobar' );
76 $linkRenderer = $this->factory
->create();
78 // action=edit&redlink=1 added
80 '<a href="/w/index.php?title=Foobar&action=edit&redlink=1" '
81 . 'class="new" title="Foobar (page does not exist)">Foobar</a>',
82 $linkRenderer->makeBrokenLink( $target )
85 // action=edit&redlink=1 not added due to action query parameter
87 '<a href="/w/index.php?title=Foobar&action=foobar" class="new" '
88 . 'title="Foobar (page does not exist)">Foobar</a>',
89 $linkRenderer->makeBrokenLink( $target, null, [], [ 'action' => 'foobar' ] )
92 // action=edit&redlink=1 not added due to NS_SPECIAL
94 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
95 . '(page does not exist)">Special:Foobar</a>',
96 $linkRenderer->makeBrokenLink( $special )
101 '<a href="/w/index.php?title=Foobar&action=edit&redlink=1" '
102 . 'class="new" title="Foobar (page does not exist)">Foobar</a>',
103 $linkRenderer->makeBrokenLink( $target->createFragmentTarget( 'foobar' ) )
107 public function testMakeLink() {
108 $linkRenderer = $this->factory
->create();
109 $foobar = new TitleValue( NS_SPECIAL
, 'Foobar' );
110 $blankpage = new TitleValue( NS_SPECIAL
, 'Blankpage' );
112 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
113 . '(page does not exist)">foo</a>',
114 $linkRenderer->makeLink( $foobar, 'foo' )
118 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">blank</a>',
119 $linkRenderer->makeLink( $blankpage, 'blank' )
123 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
124 . '(page does not exist)"><script>evil()</script></a>',
125 $linkRenderer->makeLink( $foobar, '<script>evil()</script>' )
129 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
130 . '(page does not exist)"><script>evil()</script></a>',
131 $linkRenderer->makeLink( $foobar, new HtmlArmor( '<script>evil()</script>' ) )
135 '<a href="#fragment">fragment</a>',
136 $linkRenderer->makeLink( Title
::newFromText( '#fragment' ) )
140 public function testGetLinkClasses() {
141 $services = MediaWikiServices
::getInstance();
142 $wanCache = $services->getMainWANObjectCache();
143 $titleFormatter = $services->getTitleFormatter();
144 $nsInfo = $services->getNamespaceInfo();
145 $linkCache = new LinkCache( $titleFormatter, $wanCache, $nsInfo );
146 $foobarTitle = new TitleValue( NS_MAIN
, 'FooBar' );
147 $redirectTitle = new TitleValue( NS_MAIN
, 'Redirect' );
148 $userTitle = new TitleValue( NS_USER
, 'Someuser' );
149 $linkCache->addGoodLinkObj(
155 $linkCache->addGoodLinkObj(
162 $linkCache->addGoodLinkObj(
169 $linkRenderer = new LinkRenderer( $titleFormatter, $linkCache, $nsInfo );
170 $linkRenderer->setStubThreshold( 0 );
173 $linkRenderer->getLinkClasses( $foobarTitle )
176 $linkRenderer->setStubThreshold( 20 );
179 $linkRenderer->getLinkClasses( $foobarTitle )
182 $linkRenderer->setStubThreshold( 0 );
185 $linkRenderer->getLinkClasses( $redirectTitle )
188 $linkRenderer->setStubThreshold( 20 );
191 $linkRenderer->getLinkClasses( $userTitle )
195 function tearDown() {
196 Title
::clearCaches();