MediaWiki REL1_32
LinkRendererTest.php
Go to the documentation of this file.
1<?php
2
6
11
15 private $factory;
16
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();
27 }
28
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
45 );
46 }
47
48 public function testMakeKnownLink() {
49 $target = new TitleValue( NS_MAIN, 'Foobar' );
50 $linkRenderer = $this->factory->create();
51
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' ] )
56 );
57
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' ] )
63 );
64
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 )
71 );
72 }
73
74 public function testMakeBrokenLink() {
75 $target = new TitleValue( NS_MAIN, 'Foobar' );
76 $special = new TitleValue( NS_SPECIAL, 'Foobar' );
77 $linkRenderer = $this->factory->create();
78
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 )
84 );
85
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' ] )
91 );
92
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 )
98 );
99
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' ) )
105 );
106 }
107
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' )
116 );
117
118 $this->assertEquals(
119 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">blank</a>',
120 $linkRenderer->makeLink( $blankpage, 'blank' )
121 );
122
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>' )
127 );
128
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>' ) )
133 );
134
135 $this->assertEquals(
136 '<a href="#fragment">fragment</a>',
137 $linkRenderer->makeLink( Title::newFromText( '#fragment' ) )
138 );
139 }
140
141 public function testGetLinkClasses() {
142 $wanCache = ObjectCache::getMainWANInstance();
143 $titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
144 $linkCache = new LinkCache( $titleFormatter, $wanCache );
145 $foobarTitle = new TitleValue( NS_MAIN, 'FooBar' );
146 $redirectTitle = new TitleValue( NS_MAIN, 'Redirect' );
147 $userTitle = new TitleValue( NS_USER, 'Someuser' );
148 $linkCache->addGoodLinkObj(
149 1, // id
150 $foobarTitle,
151 10, // len
152 0 // redir
153 );
154 $linkCache->addGoodLinkObj(
155 2, // id
156 $redirectTitle,
157 10, // len
158 1 // redir
159 );
160
161 $linkCache->addGoodLinkObj(
162 3, // id
163 $userTitle,
164 10, // len
165 0 // redir
166 );
167
168 $linkRenderer = new LinkRenderer( $titleFormatter, $linkCache );
169 $linkRenderer->setStubThreshold( 0 );
170 $this->assertEquals(
171 '',
172 $linkRenderer->getLinkClasses( $foobarTitle )
173 );
174
175 $linkRenderer->setStubThreshold( 20 );
176 $this->assertEquals(
177 'stub',
178 $linkRenderer->getLinkClasses( $foobarTitle )
179 );
180
181 $linkRenderer->setStubThreshold( 0 );
182 $this->assertEquals(
183 'mw-redirect',
184 $linkRenderer->getLinkClasses( $redirectTitle )
185 );
186
187 $linkRenderer->setStubThreshold( 20 );
188 $this->assertEquals(
189 '',
190 $linkRenderer->getLinkClasses( $userTitle )
191 );
192 }
193
194}
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:28
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition LinkCache.php:34
MediaWiki\Linker\LinkRenderer.
LinkRendererFactory $factory
Base class that store and restore the Language objects.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Factory to create LinkRender objects.
Class that generates HTML links for pages.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Represents a page (or page fragment) title within MediaWiki.
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition hooks.txt:3106
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form before processing starts Return false to skip default processing and return $ret $linkRenderer
Definition hooks.txt:2105
const PROTO_HTTPS
Definition Defines.php:220
const NS_MAIN
Definition Defines.php:64
const NS_SPECIAL
Definition Defines.php:53