summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/linker/LinkRendererTest.php
blob: 6d096c208bbd57c45926aeab41749527c51f9d06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php

use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Linker\LinkRendererFactory;
use MediaWiki\MediaWikiServices;

/**
 * @covers MediaWiki\Linker\LinkRenderer
 */
class LinkRendererTest extends MediaWikiLangTestCase {

	/**
	 * @var LinkRendererFactory
	 */
	private $factory;

	public function setUp() {
		parent::setUp();
		$this->setMwGlobals( [
			'wgArticlePath' => '/wiki/$1',
			'wgServer' => '//example.org',
			'wgCanonicalServer' => 'http://example.org',
			'wgScriptPath' => '/w',
			'wgScript' => '/w/index.php',
		] );
		$this->factory = MediaWikiServices::getInstance()->getLinkRendererFactory();
	}

	public function testMergeAttribs() {
		$target = new TitleValue( NS_SPECIAL, 'Blankpage' );
		$linkRenderer = $this->factory->create();
		$link = $linkRenderer->makeBrokenLink( $target, null, [
			// Appended to class
			'class' => 'foobar',
			// Suppresses href attribute
			'href' => false,
			// Extra attribute
			'bar' => 'baz'
		] );
		$this->assertEquals(
			'<a href="/wiki/Special:BlankPage" class="new foobar" '
			. 'title="Special:BlankPage (page does not exist)" bar="baz">'
			. 'Special:BlankPage</a>',
			$link
		);
	}

	public function testMakeKnownLink() {
		$target = new TitleValue( NS_MAIN, 'Foobar' );
		$linkRenderer = $this->factory->create();

		// Query added
		$this->assertEquals(
			'<a href="/w/index.php?title=Foobar&amp;foo=bar" '. 'title="Foobar">Foobar</a>',
			$linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
		);

		// forcearticlepath
		$linkRenderer->setForceArticlePath( true );
		$this->assertEquals(
			'<a href="/wiki/Foobar?foo=bar" title="Foobar">Foobar</a>',
			$linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
		);

		// expand = HTTPS
		$linkRenderer->setForceArticlePath( false );
		$linkRenderer->setExpandURLs( PROTO_HTTPS );
		$this->assertEquals(
			'<a href="https://example.org/wiki/Foobar" title="Foobar">Foobar</a>',
			$linkRenderer->makeKnownLink( $target )
		);
	}

	public function testMakeBrokenLink() {
		$target = new TitleValue( NS_MAIN, 'Foobar' );
		$special = new TitleValue( NS_SPECIAL, 'Foobar' );
		$linkRenderer = $this->factory->create();

		// action=edit&redlink=1 added
		$this->assertEquals(
			'<a href="/w/index.php?title=Foobar&amp;action=edit&amp;redlink=1" '
			. 'class="new" title="Foobar (page does not exist)">Foobar</a>',
			$linkRenderer->makeBrokenLink( $target )
		);

		// action=edit&redlink=1 not added due to action query parameter
		$this->assertEquals(
			'<a href="/w/index.php?title=Foobar&amp;action=foobar" class="new" '
			. 'title="Foobar (page does not exist)">Foobar</a>',
			$linkRenderer->makeBrokenLink( $target, null, [], [ 'action' => 'foobar' ] )
		);

		// action=edit&redlink=1 not added due to NS_SPECIAL
		$this->assertEquals(
			'<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
			. '(page does not exist)">Special:Foobar</a>',
			$linkRenderer->makeBrokenLink( $special )
		);

		// fragment stripped
		$this->assertEquals(
			'<a href="/w/index.php?title=Foobar&amp;action=edit&amp;redlink=1" '
			. 'class="new" title="Foobar (page does not exist)">Foobar</a>',
			$linkRenderer->makeBrokenLink( $target->createFragmentTarget( 'foobar' ) )
		);
	}

	public function testMakeLink() {
		$linkRenderer = $this->factory->create();
		$foobar = new TitleValue( NS_SPECIAL, 'Foobar' );
		$blankpage = new TitleValue( NS_SPECIAL, 'Blankpage' );
		$this->assertEquals(
			'<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
			. '(page does not exist)">foo</a>',
			$linkRenderer->makeLink( $foobar, 'foo' )
		);

		$this->assertEquals(
			'<a href="/wiki/Special:BlankPage" title="Special:BlankPage">blank</a>',
			$linkRenderer->makeLink( $blankpage, 'blank' )
		);

		$this->assertEquals(
			'<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
			. '(page does not exist)">&lt;script&gt;evil()&lt;/script&gt;</a>',
			$linkRenderer->makeLink( $foobar, '<script>evil()</script>' )
		);

		$this->assertEquals(
			'<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
			. '(page does not exist)"><script>evil()</script></a>',
			$linkRenderer->makeLink( $foobar, new HtmlArmor( '<script>evil()</script>' ) )
		);
	}

	public function testGetLinkClasses() {
		$wanCache = ObjectCache::getMainWANInstance();
		$titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
		$linkCache = new LinkCache( $titleFormatter, $wanCache );
		$foobarTitle = new TitleValue( NS_MAIN, 'FooBar' );
		$redirectTitle = new TitleValue( NS_MAIN, 'Redirect' );
		$userTitle = new TitleValue( NS_USER, 'Someuser' );
		$linkCache->addGoodLinkObj(
			1, // id
			$foobarTitle,
			10, // len
			0 // redir
		);
		$linkCache->addGoodLinkObj(
			2, // id
			$redirectTitle,
			10, // len
			1 // redir
		);

		$linkCache->addGoodLinkObj(
			3, // id
			$userTitle,
			10, // len
			0 // redir
		);

		$linkRenderer = new LinkRenderer( $titleFormatter, $linkCache );
		$linkRenderer->setStubThreshold( 0 );
		$this->assertEquals(
			'',
			$linkRenderer->getLinkClasses( $foobarTitle )
		);

		$linkRenderer->setStubThreshold( 20 );
		$this->assertEquals(
			'stub',
			$linkRenderer->getLinkClasses( $foobarTitle )
		);

		$linkRenderer->setStubThreshold( 0 );
		$this->assertEquals(
			'mw-redirect',
			$linkRenderer->getLinkClasses( $redirectTitle )
		);

		$linkRenderer->setStubThreshold( 20 );
		$this->assertEquals(
			'',
			$linkRenderer->getLinkClasses( $userTitle )
		);
	}

}