Add isCurrentWikiId()/isCurrentWikiDomain()/getCurrentWikiDomain() to WikiMap
[lhc/web/wiklou.git] / tests / phpunit / includes / WikiMapTest.php
1 <?php
2 use Wikimedia\Rdbms\DatabaseDomain;
3
4 /**
5 * @covers WikiMap
6 *
7 * @group Database
8 */
9 class WikiMapTest extends MediaWikiLangTestCase {
10
11 public function setUp() {
12 parent::setUp();
13
14 $conf = new SiteConfiguration();
15 $conf->settings = [
16 'wgServer' => [
17 'enwiki' => 'http://en.example.org',
18 'ruwiki' => '//ru.example.org',
19 'nopathwiki' => '//nopath.example.org',
20 'thiswiki' => '//this.wiki.org'
21 ],
22 'wgArticlePath' => [
23 'enwiki' => '/w/$1',
24 'ruwiki' => '/wiki/$1',
25 ],
26 ];
27 $conf->suffixes = [ 'wiki' ];
28 $this->setMwGlobals( [
29 'wgConf' => $conf,
30 'wgLocalDatabases' => [ 'enwiki', 'ruwiki', 'nopathwiki' ],
31 'wgCanonicalServer' => '//this.wiki.org',
32 'wgDBname' => 'thiswiki',
33 'wgDBprefix' => ''
34 ] );
35
36 TestSites::insertIntoDb();
37 }
38
39 public function provideGetWiki() {
40 // As provided by $wgConf
41 $enwiki = new WikiReference( 'http://en.example.org', '/w/$1' );
42 $ruwiki = new WikiReference( '//ru.example.org', '/wiki/$1' );
43
44 // Created from site objects
45 $nlwiki = new WikiReference( 'https://nl.wikipedia.org', '/wiki/$1' );
46 // enwiktionary doesn't have an interwiki id, thus this falls back to minor = lang code
47 $enwiktionary = new WikiReference( 'https://en.wiktionary.org', '/wiki/$1' );
48
49 return [
50 'unknown' => [ null, 'xyzzy' ],
51 'enwiki (wgConf)' => [ $enwiki, 'enwiki' ],
52 'ruwiki (wgConf)' => [ $ruwiki, 'ruwiki' ],
53 'nlwiki (sites)' => [ $nlwiki, 'nlwiki', false ],
54 'enwiktionary (sites)' => [ $enwiktionary, 'enwiktionary', false ],
55 'non MediaWiki site' => [ null, 'spam', false ],
56 'boguswiki' => [ null, 'boguswiki' ],
57 'nopathwiki' => [ null, 'nopathwiki' ],
58 ];
59 }
60
61 /**
62 * @dataProvider provideGetWiki
63 */
64 public function testGetWiki( $expected, $wikiId, $useWgConf = true ) {
65 if ( !$useWgConf ) {
66 $this->setMwGlobals( [
67 'wgConf' => new SiteConfiguration(),
68 ] );
69 }
70
71 $this->assertEquals( $expected, WikiMap::getWiki( $wikiId ) );
72 }
73
74 public function provideGetWikiName() {
75 return [
76 'unknown' => [ 'xyzzy', 'xyzzy' ],
77 'enwiki' => [ 'en.example.org', 'enwiki' ],
78 'ruwiki' => [ 'ru.example.org', 'ruwiki' ],
79 'enwiktionary (sites)' => [ 'en.wiktionary.org', 'enwiktionary' ],
80 ];
81 }
82
83 /**
84 * @dataProvider provideGetWikiName
85 */
86 public function testGetWikiName( $expected, $wikiId ) {
87 $this->assertEquals( $expected, WikiMap::getWikiName( $wikiId ) );
88 }
89
90 public function provideMakeForeignLink() {
91 return [
92 'unknown' => [ false, 'xyzzy', 'Foo' ],
93 'enwiki' => [
94 '<a class="external" rel="nofollow" ' .
95 'href="http://en.example.org/w/Foo">Foo</a>',
96 'enwiki',
97 'Foo'
98 ],
99 'ruwiki' => [
100 '<a class="external" rel="nofollow" ' .
101 'href="//ru.example.org/wiki/%D0%A4%D1%83">вар</a>',
102 'ruwiki',
103 'Фу',
104 'вар'
105 ],
106 'enwiktionary (sites)' => [
107 '<a class="external" rel="nofollow" ' .
108 'href="https://en.wiktionary.org/wiki/Kitten">Kittens!</a>',
109 'enwiktionary',
110 'Kitten',
111 'Kittens!'
112 ],
113 ];
114 }
115
116 /**
117 * @dataProvider provideMakeForeignLink
118 */
119 public function testMakeForeignLink( $expected, $wikiId, $page, $text = null ) {
120 $this->assertEquals(
121 $expected,
122 WikiMap::makeForeignLink( $wikiId, $page, $text )
123 );
124 }
125
126 public function provideForeignUserLink() {
127 return [
128 'unknown' => [ false, 'xyzzy', 'Foo' ],
129 'enwiki' => [
130 '<a class="external" rel="nofollow" ' .
131 'href="http://en.example.org/w/User:Foo">User:Foo</a>',
132 'enwiki',
133 'Foo'
134 ],
135 'ruwiki' => [
136 '<a class="external" rel="nofollow" ' .
137 'href="//ru.example.org/wiki/User:%D0%A4%D1%83">вар</a>',
138 'ruwiki',
139 'Фу',
140 'вар'
141 ],
142 'enwiktionary (sites)' => [
143 '<a class="external" rel="nofollow" ' .
144 'href="https://en.wiktionary.org/wiki/User:Dummy">Whatever</a>',
145 'enwiktionary',
146 'Dummy',
147 'Whatever'
148 ],
149 ];
150 }
151
152 /**
153 * @dataProvider provideForeignUserLink
154 */
155 public function testForeignUserLink( $expected, $wikiId, $user, $text = null ) {
156 $this->assertEquals( $expected, WikiMap::foreignUserLink( $wikiId, $user, $text ) );
157 }
158
159 public function provideGetForeignURL() {
160 return [
161 'unknown' => [ false, 'xyzzy', 'Foo' ],
162 'enwiki' => [ 'http://en.example.org/w/Foo', 'enwiki', 'Foo' ],
163 'enwiktionary (sites)' => [
164 'https://en.wiktionary.org/wiki/Testme',
165 'enwiktionary',
166 'Testme'
167 ],
168 'ruwiki with fragment' => [
169 '//ru.example.org/wiki/%D0%A4%D1%83#%D0%B2%D0%B0%D1%80',
170 'ruwiki',
171 'Фу',
172 'вар'
173 ],
174 ];
175 }
176
177 /**
178 * @dataProvider provideGetForeignURL
179 */
180 public function testGetForeignURL( $expected, $wikiId, $page, $fragment = null ) {
181 $this->assertEquals( $expected, WikiMap::getForeignURL( $wikiId, $page, $fragment ) );
182 }
183
184 /**
185 * @covers WikiMap::getCanonicalServerInfoForAllWikis()
186 */
187 public function testGetCanonicalServerInfoForAllWikis() {
188 $expected = [
189 'thiswiki' => [
190 'url' => '//this.wiki.org',
191 'parts' => [ 'scheme' => '', 'host' => 'this.wiki.org', 'delimiter' => '//' ]
192 ],
193 'enwiki' => [
194 'url' => 'http://en.example.org',
195 'parts' => [
196 'scheme' => 'http', 'host' => 'en.example.org', 'delimiter' => '://' ]
197 ],
198 'ruwiki' => [
199 'url' => '//ru.example.org',
200 'parts' => [ 'scheme' => '', 'host' => 'ru.example.org', 'delimiter' => '//' ]
201 ]
202 ];
203
204 $this->assertArrayEquals(
205 $expected,
206 WikiMap::getCanonicalServerInfoForAllWikis(),
207 true,
208 true
209 );
210 }
211
212 public function provideGetWikiFromUrl() {
213 return [
214 [ 'http://this.wiki.org', 'thiswiki' ],
215 [ 'https://this.wiki.org', 'thiswiki' ],
216 [ 'http://this.wiki.org/$1', 'thiswiki' ],
217 [ 'https://this.wiki.org/$2', 'thiswiki' ],
218 [ 'http://en.example.org', 'enwiki' ],
219 [ 'https://en.example.org', 'enwiki' ],
220 [ 'http://en.example.org/$1', 'enwiki' ],
221 [ 'https://en.example.org/$2', 'enwiki' ],
222 [ 'http://ru.example.org', 'ruwiki' ],
223 [ 'https://ru.example.org', 'ruwiki' ],
224 [ 'http://ru.example.org/$1', 'ruwiki' ],
225 [ 'https://ru.example.org/$2', 'ruwiki' ],
226 [ 'http://not.defined.org', false ]
227 ];
228 }
229
230 /**
231 * @dataProvider provideGetWikiFromUrl
232 * @covers WikiMap::getWikiFromUrl()
233 */
234 public function testGetWikiFromUrl( $url, $wiki ) {
235 $this->assertEquals( $wiki, WikiMap::getWikiFromUrl( $url ) );
236 }
237
238 public function provideGetWikiIdFromDomain() {
239 return [
240 [ 'db-prefix', 'db-prefix' ],
241 [ wfWikiID(), wfWikiID() ],
242 [ new DatabaseDomain( 'db-dash', null, 'prefix' ), 'db-dash-prefix' ]
243 ];
244 }
245
246 /**
247 * @dataProvider provideGetWikiIdFromDomain
248 * @covers WikiMap::getWikiIdFromDomain()
249 */
250 public function testGetWikiIdFromDomain( $domain, $wikiId ) {
251 $this->assertEquals( $wikiId, WikiMap::getWikiIdFromDomain( $domain ) );
252 }
253
254 /**
255 * @covers WikiMap::isCurrentWikiDomain()
256 * @covers WikiMap::getCurrentWikiDomain()
257 */
258 public function testIsCurrentWikiDomain() {
259 $this->assertTrue( WikiMap::isCurrentWikiDomain( wfWikiID() ) );
260
261 $localDomain = DatabaseDomain::newFromId( wfWikiID() );
262 $domain1 = new DatabaseDomain(
263 $localDomain->getDatabase(), 'someschema', $localDomain->getTablePrefix() );
264 $domain2 = new DatabaseDomain(
265 $localDomain->getDatabase(), null, $localDomain->getTablePrefix() );
266
267 $this->assertTrue( WikiMap::isCurrentWikiDomain( $domain1 ), 'Schema ignored' );
268 $this->assertTrue( WikiMap::isCurrentWikiDomain( $domain2 ), 'Schema ignored' );
269
270 $this->assertTrue( WikiMap::isCurrentWikiDomain( WikiMap::getCurrentWikiDomain() ) );
271 }
272
273 public function provideIsCurrentWikiId() {
274 return [
275 [ 'db', 'db', null, '' ],
276 [ 'db','db', 'schema', '' ],
277 [ 'db-prefix', 'db', null, 'prefix' ],
278 [ 'db-prefix', 'db', 'schema', 'prefix' ],
279 // Bad hyphen cases (best effort support)
280 [ 'db-stuff', 'db-stuff', null, '' ],
281 [ 'db-stuff-prefix', 'db-stuff', null, 'prefix' ]
282 ];
283 }
284
285 /**
286 * @dataProvider provideIsCurrentWikiId
287 * @covers WikiMap::isCurrentWikiId()
288 * @covers WikiMap::getCurrentWikiDomain()
289 * @covers WikiMap::getWikiIdFromDomain()
290 */
291 public function testIsCurrentWikiId( $wikiId, $db, $schema, $prefix ) {
292 $this->setMwGlobals(
293 [ 'wgDBname' => $db, 'wgDBmwschema' => $schema, 'wgDBprefix' => $prefix ] );
294
295 $this->assertTrue( WikiMap::isCurrentWikiId( $wikiId ), "ID matches" );
296 $this->assertNotTrue( WikiMap::isCurrentWikiId( $wikiId . '-more' ), "Bogus ID" );
297 }
298 }