Merge "Fix importation of weird file names in importTextFiles.php"
[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 $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(
145 1, // id
146 $foobarTitle,
147 10, // len
148 0 // redir
149 );
150 $linkCache->addGoodLinkObj(
151 2, // id
152 $redirectTitle,
153 10, // len
154 1 // redir
155 );
156
157 $linkCache->addGoodLinkObj(
158 3, // id
159 $userTitle,
160 10, // len
161 0 // redir
162 );
163
164 $linkRenderer = new LinkRenderer( $titleFormatter, $linkCache );
165 $linkRenderer->setStubThreshold( 0 );
166 $this->assertEquals(
167 '',
168 $linkRenderer->getLinkClasses( $foobarTitle )
169 );
170
171 $linkRenderer->setStubThreshold( 20 );
172 $this->assertEquals(
173 'stub',
174 $linkRenderer->getLinkClasses( $foobarTitle )
175 );
176
177 $linkRenderer->setStubThreshold( 0 );
178 $this->assertEquals(
179 'mw-redirect',
180 $linkRenderer->getLinkClasses( $redirectTitle )
181 );
182
183 $linkRenderer->setStubThreshold( 20 );
184 $this->assertEquals(
185 '',
186 $linkRenderer->getLinkClasses( $userTitle )
187 );
188 }
189
190 }