Merge "Move Linker::getLinkColour() into LinkRenderer"
[lhc/web/wiklou.git] / tests / phpunit / includes / linker / LinkRendererTest.php
1 <?php
2
3 use MediaWiki\Linker\LinkRenderer;
4 use MediaWiki\Linker\LinkRendererFactory;
5 use MediaWiki\MediaWikiServices;
6
7 /**
8 * @covers MediaWiki\Linker\LinkRenderer
9 */
10 class LinkRendererTest extends MediaWikiLangTestCase {
11
12 /**
13 * @var LinkRendererFactory
14 */
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
30 public function testMergeAttribs() {
31 $target = new TitleValue( NS_SPECIAL, 'Blankpage' );
32 $linkRenderer = $this->factory->create();
33 $link = $linkRenderer->makeBrokenLink( $target, null, [
34 // Appended to class
35 'class' => 'foobar',
36 // Suppresses href attribute
37 'href' => false,
38 // Extra attribute
39 'bar' => 'baz'
40 ] );
41 $this->assertEquals(
42 '<a href="/wiki/Special:BlankPage" class="new foobar" '
43 . 'title="Special:BlankPage (page does not exist)" bar="baz">'
44 . 'Special:BlankPage</a>',
45 $link
46 );
47 }
48
49 public function testMakeKnownLink() {
50 $target = new TitleValue( NS_MAIN, 'Foobar' );
51 $linkRenderer = $this->factory->create();
52
53 // Query added
54 $this->assertEquals(
55 '<a href="/w/index.php?title=Foobar&amp;foo=bar" '. 'title="Foobar">Foobar</a>',
56 $linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
57 );
58
59 // forcearticlepath
60 $linkRenderer->setForceArticlePath( true );
61 $this->assertEquals(
62 '<a href="/wiki/Foobar?foo=bar" title="Foobar">Foobar</a>',
63 $linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
64 );
65
66 // expand = HTTPS
67 $linkRenderer->setForceArticlePath( false );
68 $linkRenderer->setExpandURLs( PROTO_HTTPS );
69 $this->assertEquals(
70 '<a href="https://example.org/wiki/Foobar" title="Foobar">Foobar</a>',
71 $linkRenderer->makeKnownLink( $target )
72 );
73 }
74
75 public function testMakeBrokenLink() {
76 $target = new TitleValue( NS_MAIN, 'Foobar' );
77 $special = new TitleValue( NS_SPECIAL, 'Foobar' );
78 $linkRenderer = $this->factory->create();
79
80 // action=edit&redlink=1 added
81 $this->assertEquals(
82 '<a href="/w/index.php?title=Foobar&amp;action=edit&amp;redlink=1" '
83 . 'class="new" title="Foobar (page does not exist)">Foobar</a>',
84 $linkRenderer->makeBrokenLink( $target )
85 );
86
87 // action=edit&redlink=1 not added due to action query parameter
88 $this->assertEquals(
89 '<a href="/w/index.php?title=Foobar&amp;action=foobar" class="new" '
90 . 'title="Foobar (page does not exist)">Foobar</a>',
91 $linkRenderer->makeBrokenLink( $target, null, [], [ 'action' => 'foobar' ] )
92 );
93
94 // action=edit&redlink=1 not added due to NS_SPECIAL
95 $this->assertEquals(
96 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
97 . '(page does not exist)">Special:Foobar</a>',
98 $linkRenderer->makeBrokenLink( $special )
99 );
100
101 // fragment stripped
102 $this->assertEquals(
103 '<a href="/w/index.php?title=Foobar&amp;action=edit&amp;redlink=1" '
104 . 'class="new" title="Foobar (page does not exist)">Foobar</a>',
105 $linkRenderer->makeBrokenLink( $target->createFragmentTarget( 'foobar' ) )
106 );
107 }
108
109 public function testMakeLink() {
110 $linkRenderer = $this->factory->create();
111 $foobar = new TitleValue( NS_SPECIAL, 'Foobar' );
112 $blankpage = new TitleValue( NS_SPECIAL, 'Blankpage' );
113 $this->assertEquals(
114 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
115 . '(page does not exist)">foo</a>',
116 $linkRenderer->makeLink( $foobar, 'foo' )
117 );
118
119 $this->assertEquals(
120 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">blank</a>',
121 $linkRenderer->makeLink( $blankpage, 'blank' )
122 );
123
124 $this->assertEquals(
125 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
126 . '(page does not exist)">&lt;script&gt;evil()&lt;/script&gt;</a>',
127 $linkRenderer->makeLink( $foobar, '<script>evil()</script>' )
128 );
129
130 $this->assertEquals(
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>' ) )
134 );
135 }
136
137 public function testGetLinkClasses() {
138 $titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
139 $linkCache = new LinkCache( $titleFormatter );
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
148 );
149 $linkCache->addGoodLinkObj(
150 2, // id
151 $redirectTitle,
152 10, // len
153 1 // redir
154 );
155
156 $linkCache->addGoodLinkObj(
157 3, // id
158 $userTitle,
159 10, // len
160 0 // redir
161 );
162
163 $linkRenderer = new LinkRenderer( $titleFormatter, $linkCache );
164 $linkRenderer->setStubThreshold( 0 );
165 $this->assertEquals(
166 '',
167 $linkRenderer->getLinkClasses( $foobarTitle )
168 );
169
170 $linkRenderer->setStubThreshold( 20 );
171 $this->assertEquals(
172 'stub',
173 $linkRenderer->getLinkClasses( $foobarTitle )
174 );
175
176 $linkRenderer->setStubThreshold( 0 );
177 $this->assertEquals(
178 'mw-redirect',
179 $linkRenderer->getLinkClasses( $redirectTitle )
180 );
181
182 $linkRenderer->setStubThreshold( 20 );
183 $this->assertEquals(
184 '',
185 $linkRenderer->getLinkClasses( $userTitle )
186 );
187 }
188
189 }