Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / linker / LinkRendererTest.php
blob6d096c208bbd57c45926aeab41749527c51f9d06
1 <?php
3 use MediaWiki\Linker\LinkRenderer;
4 use MediaWiki\Linker\LinkRendererFactory;
5 use MediaWiki\MediaWikiServices;
7 /**
8 * @covers MediaWiki\Linker\LinkRenderer
9 */
10 class LinkRendererTest extends MediaWikiLangTestCase {
12 /**
13 * @var LinkRendererFactory
15 private $factory;
17 public function setUp() {
18 parent::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',
25 ] );
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, [
33 // Appended to class
34 'class' => 'foobar',
35 // Suppresses href attribute
36 'href' => false,
37 // Extra attribute
38 'bar' => 'baz'
39 ] );
40 $this->assertEquals(
41 '<a href="/wiki/Special:BlankPage" class="new foobar" '
42 . 'title="Special:BlankPage (page does not exist)" bar="baz">'
43 . 'Special:BlankPage</a>',
44 $link
48 public function testMakeKnownLink() {
49 $target = new TitleValue( NS_MAIN, 'Foobar' );
50 $linkRenderer = $this->factory->create();
52 // Query added
53 $this->assertEquals(
54 '<a href="/w/index.php?title=Foobar&amp;foo=bar" '. 'title="Foobar">Foobar</a>',
55 $linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
58 // forcearticlepath
59 $linkRenderer->setForceArticlePath( true );
60 $this->assertEquals(
61 '<a href="/wiki/Foobar?foo=bar" title="Foobar">Foobar</a>',
62 $linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
65 // expand = HTTPS
66 $linkRenderer->setForceArticlePath( false );
67 $linkRenderer->setExpandURLs( PROTO_HTTPS );
68 $this->assertEquals(
69 '<a href="https://example.org/wiki/Foobar" title="Foobar">Foobar</a>',
70 $linkRenderer->makeKnownLink( $target )
74 public function testMakeBrokenLink() {
75 $target = new TitleValue( NS_MAIN, 'Foobar' );
76 $special = new TitleValue( NS_SPECIAL, 'Foobar' );
77 $linkRenderer = $this->factory->create();
79 // action=edit&redlink=1 added
80 $this->assertEquals(
81 '<a href="/w/index.php?title=Foobar&amp;action=edit&amp;redlink=1" '
82 . 'class="new" title="Foobar (page does not exist)">Foobar</a>',
83 $linkRenderer->makeBrokenLink( $target )
86 // action=edit&redlink=1 not added due to action query parameter
87 $this->assertEquals(
88 '<a href="/w/index.php?title=Foobar&amp;action=foobar" class="new" '
89 . 'title="Foobar (page does not exist)">Foobar</a>',
90 $linkRenderer->makeBrokenLink( $target, null, [], [ 'action' => 'foobar' ] )
93 // action=edit&redlink=1 not added due to NS_SPECIAL
94 $this->assertEquals(
95 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
96 . '(page does not exist)">Special:Foobar</a>',
97 $linkRenderer->makeBrokenLink( $special )
100 // fragment stripped
101 $this->assertEquals(
102 '<a href="/w/index.php?title=Foobar&amp;action=edit&amp;redlink=1" '
103 . 'class="new" title="Foobar (page does not exist)">Foobar</a>',
104 $linkRenderer->makeBrokenLink( $target->createFragmentTarget( 'foobar' ) )
108 public function testMakeLink() {
109 $linkRenderer = $this->factory->create();
110 $foobar = new TitleValue( NS_SPECIAL, 'Foobar' );
111 $blankpage = new TitleValue( NS_SPECIAL, 'Blankpage' );
112 $this->assertEquals(
113 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
114 . '(page does not exist)">foo</a>',
115 $linkRenderer->makeLink( $foobar, 'foo' )
118 $this->assertEquals(
119 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">blank</a>',
120 $linkRenderer->makeLink( $blankpage, 'blank' )
123 $this->assertEquals(
124 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
125 . '(page does not exist)">&lt;script&gt;evil()&lt;/script&gt;</a>',
126 $linkRenderer->makeLink( $foobar, '<script>evil()</script>' )
129 $this->assertEquals(
130 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
131 . '(page does not exist)"><script>evil()</script></a>',
132 $linkRenderer->makeLink( $foobar, new HtmlArmor( '<script>evil()</script>' ) )
136 public function testGetLinkClasses() {
137 $wanCache = ObjectCache::getMainWANInstance();
138 $titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
139 $linkCache = new LinkCache( $titleFormatter, $wanCache );
140 $foobarTitle = new TitleValue( NS_MAIN, 'FooBar' );
141 $redirectTitle = new TitleValue( NS_MAIN, 'Redirect' );
142 $userTitle = new TitleValue( NS_USER, 'Someuser' );
143 $linkCache->addGoodLinkObj(
144 1, // id
145 $foobarTitle,
146 10, // len
147 0 // redir
149 $linkCache->addGoodLinkObj(
150 2, // id
151 $redirectTitle,
152 10, // len
153 1 // redir
156 $linkCache->addGoodLinkObj(
157 3, // id
158 $userTitle,
159 10, // len
160 0 // redir
163 $linkRenderer = new LinkRenderer( $titleFormatter, $linkCache );
164 $linkRenderer->setStubThreshold( 0 );
165 $this->assertEquals(
167 $linkRenderer->getLinkClasses( $foobarTitle )
170 $linkRenderer->setStubThreshold( 20 );
171 $this->assertEquals(
172 'stub',
173 $linkRenderer->getLinkClasses( $foobarTitle )
176 $linkRenderer->setStubThreshold( 0 );
177 $this->assertEquals(
178 'mw-redirect',
179 $linkRenderer->getLinkClasses( $redirectTitle )
182 $linkRenderer->setStubThreshold( 20 );
183 $this->assertEquals(
185 $linkRenderer->getLinkClasses( $userTitle )