3 use Wikimedia\Rdbms\DatabaseDomain
;
10 class WikiMapTest
extends MediaWikiLangTestCase
{
12 public function setUp() {
15 $conf = new SiteConfiguration();
18 'enwiki' => 'http://en.example.org',
19 'ruwiki' => '//ru.example.org',
20 'nopathwiki' => '//nopath.example.org',
21 'thiswiki' => '//this.wiki.org'
25 'ruwiki' => '/wiki/$1',
28 $conf->suffixes
= [ 'wiki' ];
29 $this->setMwGlobals( [
31 'wgLocalDatabases' => [ 'enwiki', 'ruwiki', 'nopathwiki' ],
32 'wgCanonicalServer' => '//this.wiki.org',
33 'wgDBname' => 'thiswiki',
37 TestSites
::insertIntoDb();
40 public function provideGetWiki() {
41 // As provided by $wgConf
42 $enwiki = new WikiReference( 'http://en.example.org', '/w/$1' );
43 $ruwiki = new WikiReference( '//ru.example.org', '/wiki/$1' );
45 // Created from site objects
46 $nlwiki = new WikiReference( 'https://nl.wikipedia.org', '/wiki/$1' );
47 // enwiktionary doesn't have an interwiki id, thus this falls back to minor = lang code
48 $enwiktionary = new WikiReference( 'https://en.wiktionary.org', '/wiki/$1' );
51 'unknown' => [ null, 'xyzzy' ],
52 'enwiki (wgConf)' => [ $enwiki, 'enwiki' ],
53 'ruwiki (wgConf)' => [ $ruwiki, 'ruwiki' ],
54 'nlwiki (sites)' => [ $nlwiki, 'nlwiki', false ],
55 'enwiktionary (sites)' => [ $enwiktionary, 'enwiktionary', false ],
56 'non MediaWiki site' => [ null, 'spam', false ],
57 'boguswiki' => [ null, 'boguswiki' ],
58 'nopathwiki' => [ null, 'nopathwiki' ],
63 * @dataProvider provideGetWiki
65 public function testGetWiki( $expected, $wikiId, $useWgConf = true ) {
67 $this->setMwGlobals( [
68 'wgConf' => new SiteConfiguration(),
72 $this->assertEquals( $expected, WikiMap
::getWiki( $wikiId ) );
75 public function provideGetWikiName() {
77 'unknown' => [ 'xyzzy', 'xyzzy' ],
78 'enwiki' => [ 'en.example.org', 'enwiki' ],
79 'ruwiki' => [ 'ru.example.org', 'ruwiki' ],
80 'enwiktionary (sites)' => [ 'en.wiktionary.org', 'enwiktionary' ],
85 * @dataProvider provideGetWikiName
87 public function testGetWikiName( $expected, $wikiId ) {
88 $this->assertEquals( $expected, WikiMap
::getWikiName( $wikiId ) );
91 public function provideMakeForeignLink() {
93 'unknown' => [ false, 'xyzzy', 'Foo' ],
95 '<a class="external" rel="nofollow" ' .
96 'href="http://en.example.org/w/Foo">Foo</a>',
101 '<a class="external" rel="nofollow" ' .
102 'href="//ru.example.org/wiki/%D0%A4%D1%83">вар</a>',
107 'enwiktionary (sites)' => [
108 '<a class="external" rel="nofollow" ' .
109 'href="https://en.wiktionary.org/wiki/Kitten">Kittens!</a>',
118 * @dataProvider provideMakeForeignLink
120 public function testMakeForeignLink( $expected, $wikiId, $page, $text = null ) {
123 WikiMap
::makeForeignLink( $wikiId, $page, $text )
127 public function provideForeignUserLink() {
129 'unknown' => [ false, 'xyzzy', 'Foo' ],
131 '<a class="external" rel="nofollow" ' .
132 'href="http://en.example.org/w/User:Foo">User:Foo</a>',
137 '<a class="external" rel="nofollow" ' .
138 'href="//ru.example.org/wiki/User:%D0%A4%D1%83">вар</a>',
143 'enwiktionary (sites)' => [
144 '<a class="external" rel="nofollow" ' .
145 'href="https://en.wiktionary.org/wiki/User:Dummy">Whatever</a>',
154 * @dataProvider provideForeignUserLink
156 public function testForeignUserLink( $expected, $wikiId, $user, $text = null ) {
157 $this->assertEquals( $expected, WikiMap
::foreignUserLink( $wikiId, $user, $text ) );
160 public function provideGetForeignURL() {
162 'unknown' => [ false, 'xyzzy', 'Foo' ],
163 'enwiki' => [ 'http://en.example.org/w/Foo', 'enwiki', 'Foo' ],
164 'enwiktionary (sites)' => [
165 'https://en.wiktionary.org/wiki/Testme',
169 'ruwiki with fragment' => [
170 '//ru.example.org/wiki/%D0%A4%D1%83#%D0%B2%D0%B0%D1%80',
179 * @dataProvider provideGetForeignURL
181 public function testGetForeignURL( $expected, $wikiId, $page, $fragment = null ) {
182 $this->assertEquals( $expected, WikiMap
::getForeignURL( $wikiId, $page, $fragment ) );
186 * @covers WikiMap::getCanonicalServerInfoForAllWikis()
188 public function testGetCanonicalServerInfoForAllWikis() {
191 'url' => '//this.wiki.org',
192 'parts' => [ 'scheme' => '', 'host' => 'this.wiki.org', 'delimiter' => '//' ]
195 'url' => 'http://en.example.org',
197 'scheme' => 'http', 'host' => 'en.example.org', 'delimiter' => '://' ]
200 'url' => '//ru.example.org',
201 'parts' => [ 'scheme' => '', 'host' => 'ru.example.org', 'delimiter' => '//' ]
205 $this->assertArrayEquals(
207 WikiMap
::getCanonicalServerInfoForAllWikis(),
213 public function provideGetWikiFromUrl() {
215 [ 'http://this.wiki.org', 'thiswiki' ],
216 [ 'https://this.wiki.org', 'thiswiki' ],
217 [ 'http://this.wiki.org/$1', 'thiswiki' ],
218 [ 'https://this.wiki.org/$2', 'thiswiki' ],
219 [ 'http://en.example.org', 'enwiki' ],
220 [ 'https://en.example.org', 'enwiki' ],
221 [ 'http://en.example.org/$1', 'enwiki' ],
222 [ 'https://en.example.org/$2', 'enwiki' ],
223 [ 'http://ru.example.org', 'ruwiki' ],
224 [ 'https://ru.example.org', 'ruwiki' ],
225 [ 'http://ru.example.org/$1', 'ruwiki' ],
226 [ 'https://ru.example.org/$2', 'ruwiki' ],
227 [ 'http://not.defined.org', false ]
232 * @dataProvider provideGetWikiFromUrl
233 * @covers WikiMap::getWikiFromUrl()
235 public function testGetWikiFromUrl( $url, $wiki ) {
236 $this->assertEquals( $wiki, WikiMap
::getWikiFromUrl( $url ) );
239 public function provideGetWikiIdFromDbDomain() {
241 [ 'db-prefix_', 'db-prefix_' ],
242 [ wfWikiID(), wfWikiID() ],
243 [ new DatabaseDomain( 'db-dash', null, 'prefix_' ), 'db-dash-prefix_' ],
244 [ wfWikiID(), wfWikiID() ],
245 [ new DatabaseDomain( 'db-dash', null, 'prefix_' ), 'db-dash-prefix_' ],
246 [ new DatabaseDomain( 'db', 'mediawiki', 'prefix_' ), 'db-prefix_' ], // schema ignored
247 [ new DatabaseDomain( 'db', 'custom', 'prefix_' ), 'db-custom-prefix_' ],
252 * @dataProvider provideGetWikiIdFromDbDomain
253 * @covers WikiMap::getWikiIdFromDbDomain()
255 public function testGetWikiIdFromDbDomain( $domain, $wikiId ) {
256 $this->assertEquals( $wikiId, WikiMap
::getWikiIdFromDbDomain( $domain ) );
260 * @covers WikiMap::isCurrentWikiDbDomain()
261 * @covers WikiMap::getCurrentWikiDbDomain()
263 public function testIsCurrentWikiDomain() {
264 $this->setMwGlobals( 'wgDBmwschema', 'mediawiki' );
266 $localDomain = WikiMap
::getCurrentWikiDbDomain()->getId();
267 $this->assertTrue( WikiMap
::isCurrentWikiDbDomain( $localDomain ) );
269 $localDomain = DatabaseDomain
::newFromId( $localDomain );
270 $domain1 = new DatabaseDomain(
271 $localDomain->getDatabase(), 'someschema', $localDomain->getTablePrefix() );
272 $domain2 = new DatabaseDomain(
273 $localDomain->getDatabase(), null, $localDomain->getTablePrefix() );
275 $this->assertFalse( WikiMap
::isCurrentWikiDbDomain( $domain1 ), 'Schema not ignored' );
276 $this->assertFalse( WikiMap
::isCurrentWikiDbDomain( $domain2 ), 'Null schema not ignored' );
278 $this->assertTrue( WikiMap
::isCurrentWikiDbDomain( WikiMap
::getCurrentWikiDbDomain() ) );
281 public function provideIsCurrentWikiId() {
283 [ 'db', 'db', null, '' ],
284 [ 'db-schema-','db', 'schema', '' ],
285 [ 'db','db', 'mediawiki', '' ], // common b/c case
286 [ 'db-prefix_', 'db', null, 'prefix_' ],
287 [ 'db-schema-prefix_', 'db', 'schema', 'prefix_' ],
288 [ 'db-prefix_', 'db', 'mediawiki', 'prefix_' ], // common b/c case
289 // Bad hyphen cases (best effort support)
290 [ 'db-stuff', 'db-stuff', null, '' ],
291 [ 'db-stuff-prefix_', 'db-stuff', null, 'prefix_' ],
292 [ 'db-stuff-schema-', 'db-stuff', 'schema', '' ],
293 [ 'db-stuff-schema-prefix_', 'db-stuff', 'schema', 'prefix_' ],
294 [ 'db-stuff-prefix_', 'db-stuff', 'mediawiki', 'prefix_' ] // common b/c case
299 * @dataProvider provideIsCurrentWikiId
300 * @covers WikiMap::isCurrentWikiId()
301 * @covers WikiMap::getCurrentWikiDbDomain()
302 * @covers WikiMap::getWikiIdFromDbDomain()
304 public function testIsCurrentWikiId( $wikiId, $db, $schema, $prefix ) {
306 [ 'wgDBname' => $db, 'wgDBmwschema' => $schema, 'wgDBprefix' => $prefix ] );
308 $this->assertTrue( WikiMap
::isCurrentWikiId( $wikiId ), "ID matches" );
309 $this->assertNotTrue( WikiMap
::isCurrentWikiId( $wikiId . '-more' ), "Bogus ID" );