2 use Wikimedia\Rdbms\DatabaseDomain
;
9 class WikiMapTest
extends MediaWikiLangTestCase
{
11 public function setUp() {
14 $conf = new SiteConfiguration();
17 'enwiki' => 'http://en.example.org',
18 'ruwiki' => '//ru.example.org',
19 'nopathwiki' => '//nopath.example.org',
20 'thiswiki' => '//this.wiki.org'
24 'ruwiki' => '/wiki/$1',
27 $conf->suffixes
= [ 'wiki' ];
28 $this->setMwGlobals( [
30 'wgLocalDatabases' => [ 'enwiki', 'ruwiki', 'nopathwiki' ],
31 'wgCanonicalServer' => '//this.wiki.org',
32 'wgDBname' => 'thiswiki',
36 TestSites
::insertIntoDb();
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' );
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' );
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' ],
62 * @dataProvider provideGetWiki
64 public function testGetWiki( $expected, $wikiId, $useWgConf = true ) {
66 $this->setMwGlobals( [
67 'wgConf' => new SiteConfiguration(),
71 $this->assertEquals( $expected, WikiMap
::getWiki( $wikiId ) );
74 public function provideGetWikiName() {
76 'unknown' => [ 'xyzzy', 'xyzzy' ],
77 'enwiki' => [ 'en.example.org', 'enwiki' ],
78 'ruwiki' => [ 'ru.example.org', 'ruwiki' ],
79 'enwiktionary (sites)' => [ 'en.wiktionary.org', 'enwiktionary' ],
84 * @dataProvider provideGetWikiName
86 public function testGetWikiName( $expected, $wikiId ) {
87 $this->assertEquals( $expected, WikiMap
::getWikiName( $wikiId ) );
90 public function provideMakeForeignLink() {
92 'unknown' => [ false, 'xyzzy', 'Foo' ],
94 '<a class="external" rel="nofollow" ' .
95 'href="http://en.example.org/w/Foo">Foo</a>',
100 '<a class="external" rel="nofollow" ' .
101 'href="//ru.example.org/wiki/%D0%A4%D1%83">вар</a>',
106 'enwiktionary (sites)' => [
107 '<a class="external" rel="nofollow" ' .
108 'href="https://en.wiktionary.org/wiki/Kitten">Kittens!</a>',
117 * @dataProvider provideMakeForeignLink
119 public function testMakeForeignLink( $expected, $wikiId, $page, $text = null ) {
122 WikiMap
::makeForeignLink( $wikiId, $page, $text )
126 public function provideForeignUserLink() {
128 'unknown' => [ false, 'xyzzy', 'Foo' ],
130 '<a class="external" rel="nofollow" ' .
131 'href="http://en.example.org/w/User:Foo">User:Foo</a>',
136 '<a class="external" rel="nofollow" ' .
137 'href="//ru.example.org/wiki/User:%D0%A4%D1%83">вар</a>',
142 'enwiktionary (sites)' => [
143 '<a class="external" rel="nofollow" ' .
144 'href="https://en.wiktionary.org/wiki/User:Dummy">Whatever</a>',
153 * @dataProvider provideForeignUserLink
155 public function testForeignUserLink( $expected, $wikiId, $user, $text = null ) {
156 $this->assertEquals( $expected, WikiMap
::foreignUserLink( $wikiId, $user, $text ) );
159 public function provideGetForeignURL() {
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',
168 'ruwiki with fragment' => [
169 '//ru.example.org/wiki/%D0%A4%D1%83#%D0%B2%D0%B0%D1%80',
178 * @dataProvider provideGetForeignURL
180 public function testGetForeignURL( $expected, $wikiId, $page, $fragment = null ) {
181 $this->assertEquals( $expected, WikiMap
::getForeignURL( $wikiId, $page, $fragment ) );
185 * @covers WikiMap::getCanonicalServerInfoForAllWikis()
187 public function testGetCanonicalServerInfoForAllWikis() {
190 'url' => '//this.wiki.org',
191 'parts' => [ 'scheme' => '', 'host' => 'this.wiki.org', 'delimiter' => '//' ]
194 'url' => 'http://en.example.org',
196 'scheme' => 'http', 'host' => 'en.example.org', 'delimiter' => '://' ]
199 'url' => '//ru.example.org',
200 'parts' => [ 'scheme' => '', 'host' => 'ru.example.org', 'delimiter' => '//' ]
204 $this->assertArrayEquals(
206 WikiMap
::getCanonicalServerInfoForAllWikis(),
212 public function provideGetWikiFromUrl() {
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 ]
231 * @dataProvider provideGetWikiFromUrl
232 * @covers WikiMap::getWikiFromUrl()
234 public function testGetWikiFromUrl( $url, $wiki ) {
235 $this->assertEquals( $wiki, WikiMap
::getWikiFromUrl( $url ) );
238 public function provideGetWikiIdFromDomain() {
240 [ 'db-prefix', 'db-prefix' ],
241 [ wfWikiID(), wfWikiID() ],
242 [ new DatabaseDomain( 'db-dash', null, 'prefix' ), 'db-dash-prefix' ],
243 [ wfWikiID(), wfWikiID() ],
244 [ new DatabaseDomain( 'db-dash', null, 'prefix' ), 'db-dash-prefix' ],
245 [ new DatabaseDomain( 'db', 'mediawiki', 'prefix' ), 'db-prefix' ], // schema ignored
246 [ new DatabaseDomain( 'db', 'custom', 'prefix' ), 'db-custom-prefix' ],
251 * @dataProvider provideGetWikiIdFromDomain
252 * @covers WikiMap::getWikiIdFromDomain()
254 public function testGetWikiIdFromDomain( $domain, $wikiId ) {
255 $this->assertEquals( $wikiId, WikiMap
::getWikiIdFromDomain( $domain ) );
259 * @covers WikiMap::isCurrentWikiDomain()
260 * @covers WikiMap::getCurrentWikiDomain()
262 public function testIsCurrentWikiDomain() {
263 $this->assertTrue( WikiMap
::isCurrentWikiDomain( wfWikiID() ) );
265 $localDomain = DatabaseDomain
::newFromId( wfWikiID() );
266 $domain1 = new DatabaseDomain(
267 $localDomain->getDatabase(), 'someschema', $localDomain->getTablePrefix() );
268 $domain2 = new DatabaseDomain(
269 $localDomain->getDatabase(), null, $localDomain->getTablePrefix() );
271 $this->assertTrue( WikiMap
::isCurrentWikiDomain( $domain1 ), 'Schema ignored' );
272 $this->assertTrue( WikiMap
::isCurrentWikiDomain( $domain2 ), 'Schema ignored' );
274 $this->assertTrue( WikiMap
::isCurrentWikiDomain( WikiMap
::getCurrentWikiDomain() ) );
277 public function provideIsCurrentWikiId() {
279 [ 'db', 'db', null, '' ],
280 [ 'db-schema-','db', 'schema', '' ],
281 [ 'db','db', 'mediawiki', '' ], // common b/c case
282 [ 'db-prefix', 'db', null, 'prefix' ],
283 [ 'db-schema-prefix', 'db', 'schema', 'prefix' ],
284 [ 'db-prefix', 'db', 'mediawiki', 'prefix' ], // common b/c case
285 // Bad hyphen cases (best effort support)
286 [ 'db-stuff', 'db-stuff', null, '' ],
287 [ 'db-stuff-prefix', 'db-stuff', null, 'prefix' ],
288 [ 'db-stuff-schema-', 'db-stuff', 'schema', '' ],
289 [ 'db-stuff-schema-prefix', 'db-stuff', 'schema', 'prefix' ],
290 [ 'db-stuff-prefix', 'db-stuff', 'mediawiki', 'prefix' ] // common b/c case
295 * @dataProvider provideIsCurrentWikiId
296 * @covers WikiMap::isCurrentWikiId()
297 * @covers WikiMap::getCurrentWikiDomain()
298 * @covers WikiMap::getWikiIdFromDomain()
300 public function testIsCurrentWikiId( $wikiId, $db, $schema, $prefix ) {
302 [ 'wgDBname' => $db, 'wgDBmwschema' => $schema, 'wgDBprefix' => $prefix ] );
304 $this->assertTrue( WikiMap
::isCurrentWikiId( $wikiId ), "ID matches" );
305 $this->assertNotTrue( WikiMap
::isCurrentWikiId( $wikiId . '-more' ), "Bogus ID" );