Merge "Fix typo and improve code comments in LinkTarget"
[lhc/web/wiklou.git] / tests / phpunit / includes / WikiMapTest.php
1 <?php
2
3 /**
4 * @covers WikiMap
5 *
6 * @group Database
7 */
8 class WikiMapTest extends MediaWikiLangTestCase {
9
10 public function setUp() {
11 parent::setUp();
12
13 $conf = new SiteConfiguration();
14 $conf->settings = [
15 'wgServer' => [
16 'enwiki' => 'http://en.example.org',
17 'ruwiki' => '//ru.example.org',
18 ],
19 'wgArticlePath' => [
20 'enwiki' => '/w/$1',
21 'ruwiki' => '/wiki/$1',
22 ],
23 ];
24 $conf->suffixes = [ 'wiki' ];
25 $this->setMwGlobals( [
26 'wgConf' => $conf,
27 ] );
28
29 TestSites::insertIntoDb();
30 }
31
32 public function provideGetWiki() {
33 // As provided by $wgConf
34 $enwiki = new WikiReference( 'http://en.example.org', '/w/$1' );
35 $ruwiki = new WikiReference( '//ru.example.org', '/wiki/$1' );
36
37 // Created from site objects
38 $nlwiki = new WikiReference( 'https://nl.wikipedia.org', '/wiki/$1' );
39 // enwiktionary doesn't have an interwiki id, thus this falls back to minor = lang code
40 $enwiktionary = new WikiReference( 'https://en.wiktionary.org', '/wiki/$1' );
41
42 return [
43 'unknown' => [ null, 'xyzzy' ],
44 'enwiki (wgConf)' => [ $enwiki, 'enwiki' ],
45 'ruwiki (wgConf)' => [ $ruwiki, 'ruwiki' ],
46 'nlwiki (sites)' => [ $nlwiki, 'nlwiki', false ],
47 'enwiktionary (sites)' => [ $enwiktionary, 'enwiktionary', false ],
48 'non MediaWiki site' => [ null, 'spam', false ],
49 ];
50 }
51
52 /**
53 * @dataProvider provideGetWiki
54 */
55 public function testGetWiki( $expected, $wikiId, $useWgConf = true ) {
56 if ( !$useWgConf ) {
57 $this->setMwGlobals( [
58 'wgConf' => new SiteConfiguration(),
59 ] );
60 }
61
62 $this->assertEquals( $expected, WikiMap::getWiki( $wikiId ) );
63 }
64
65 public function provideGetWikiName() {
66 return [
67 'unknown' => [ 'xyzzy', 'xyzzy' ],
68 'enwiki' => [ 'en.example.org', 'enwiki' ],
69 'ruwiki' => [ 'ru.example.org', 'ruwiki' ],
70 'enwiktionary (sites)' => [ 'en.wiktionary.org', 'enwiktionary' ],
71 ];
72 }
73
74 /**
75 * @dataProvider provideGetWikiName
76 */
77 public function testGetWikiName( $expected, $wikiId ) {
78 $this->assertEquals( $expected, WikiMap::getWikiName( $wikiId ) );
79 }
80
81 public function provideMakeForeignLink() {
82 return [
83 'unknown' => [ false, 'xyzzy', 'Foo' ],
84 'enwiki' => [
85 '<a class="external" rel="nofollow" ' .
86 'href="http://en.example.org/w/Foo">Foo</a>',
87 'enwiki',
88 'Foo'
89 ],
90 'ruwiki' => [
91 '<a class="external" rel="nofollow" ' .
92 'href="//ru.example.org/wiki/%D0%A4%D1%83">вар</a>',
93 'ruwiki',
94 'Фу',
95 'вар'
96 ],
97 'enwiktionary (sites)' => [
98 '<a class="external" rel="nofollow" ' .
99 'href="https://en.wiktionary.org/wiki/Kitten">Kittens!</a>',
100 'enwiktionary',
101 'Kitten',
102 'Kittens!'
103 ],
104 ];
105 }
106
107 /**
108 * @dataProvider provideMakeForeignLink
109 */
110 public function testMakeForeignLink( $expected, $wikiId, $page, $text = null ) {
111 $this->assertEquals(
112 $expected,
113 WikiMap::makeForeignLink( $wikiId, $page, $text )
114 );
115 }
116
117 public function provideForeignUserLink() {
118 return [
119 'unknown' => [ false, 'xyzzy', 'Foo' ],
120 'enwiki' => [
121 '<a class="external" rel="nofollow" ' .
122 'href="http://en.example.org/w/User:Foo">User:Foo</a>',
123 'enwiki',
124 'Foo'
125 ],
126 'ruwiki' => [
127 '<a class="external" rel="nofollow" ' .
128 'href="//ru.example.org/wiki/User:%D0%A4%D1%83">вар</a>',
129 'ruwiki',
130 'Фу',
131 'вар'
132 ],
133 'enwiktionary (sites)' => [
134 '<a class="external" rel="nofollow" ' .
135 'href="https://en.wiktionary.org/wiki/User:Dummy">Whatever</a>',
136 'enwiktionary',
137 'Dummy',
138 'Whatever'
139 ],
140 ];
141 }
142
143 /**
144 * @dataProvider provideForeignUserLink
145 */
146 public function testForeignUserLink( $expected, $wikiId, $user, $text = null ) {
147 $this->assertEquals( $expected, WikiMap::foreignUserLink( $wikiId, $user, $text ) );
148 }
149
150 public function provideGetForeignURL() {
151 return [
152 'unknown' => [ false, 'xyzzy', 'Foo' ],
153 'enwiki' => [ 'http://en.example.org/w/Foo', 'enwiki', 'Foo' ],
154 'enwiktionary (sites)' => [
155 'https://en.wiktionary.org/wiki/Testme',
156 'enwiktionary',
157 'Testme'
158 ],
159 'ruwiki with fragment' => [
160 '//ru.example.org/wiki/%D0%A4%D1%83#%D0%B2%D0%B0%D1%80',
161 'ruwiki',
162 'Фу',
163 'вар'
164 ],
165 ];
166 }
167
168 /**
169 * @dataProvider provideGetForeignURL
170 */
171 public function testGetForeignURL( $expected, $wikiId, $page, $fragment = null ) {
172 $this->assertEquals( $expected, WikiMap::getForeignURL( $wikiId, $page, $fragment ) );
173 }
174
175 }