Merge "docs: Fix typo"
[mediawiki.git] / tests / phpunit / includes / WikiMap / WikiMapTest.php
blob68fdf46806d4ff7d845483476712797ca9a72959
1 <?php
3 use MediaWiki\Config\SiteConfiguration;
4 use MediaWiki\MainConfigNames;
5 use MediaWiki\Tests\Site\TestSites;
6 use MediaWiki\WikiMap\WikiMap;
7 use MediaWiki\WikiMap\WikiReference;
8 use Wikimedia\Rdbms\DatabaseDomain;
10 /**
11 * @covers \MediaWiki\WikiMap\WikiMap
13 * @group Database
15 class WikiMapTest extends MediaWikiLangTestCase {
17 protected function setUp(): void {
18 parent::setUp();
20 $conf = new SiteConfiguration();
21 $conf->settings = [
22 'wgServer' => [
23 'enwiki' => 'http://en.example.org',
24 'ruwiki' => '//ru.example.org',
25 'nopathwiki' => '//nopath.example.org',
26 'thiswiki' => '//this.wiki.org'
28 'wgArticlePath' => [
29 'enwiki' => '/w/$1',
30 'ruwiki' => '/wiki/$1',
33 $conf->suffixes = [ 'wiki' ];
34 $this->setMwGlobals( 'wgConf', $conf );
35 $this->overrideConfigValues( [
36 MainConfigNames::LocalDatabases => [ 'enwiki', 'ruwiki', 'nopathwiki' ],
37 MainConfigNames::CanonicalServer => '//this.wiki.org',
38 MainConfigNames::DBname => 'thiswiki',
39 MainConfigNames::DBprefix => ''
40 ] );
42 TestSites::insertIntoDb();
45 public static function provideGetWiki() {
46 // As provided by $wgConf
47 $enwiki = new WikiReference( 'http://en.example.org', '/w/$1' );
48 $ruwiki = new WikiReference( '//ru.example.org', '/wiki/$1' );
50 // Created from site objects
51 $nlwiki = new WikiReference( 'https://nl.wikipedia.org', '/wiki/$1' );
52 // enwiktionary doesn't have an interwiki id, thus this falls back to minor = lang code
53 $enwiktionary = new WikiReference( 'https://en.wiktionary.org', '/wiki/$1' );
55 return [
56 'unknown' => [ null, 'xyzzy' ],
57 'enwiki (wgConf)' => [ $enwiki, 'enwiki' ],
58 'ruwiki (wgConf)' => [ $ruwiki, 'ruwiki' ],
59 'nlwiki (sites)' => [ $nlwiki, 'nlwiki', false ],
60 'enwiktionary (sites)' => [ $enwiktionary, 'enwiktionary', false ],
61 'non MediaWiki site' => [ null, 'spam', false ],
62 'boguswiki' => [ null, 'boguswiki' ],
63 'nopathwiki' => [ null, 'nopathwiki' ],
67 /**
68 * @dataProvider provideGetWiki
70 public function testGetWiki( $expected, $wikiId, $useWgConf = true ) {
71 if ( !$useWgConf ) {
72 $this->setMwGlobals( [
73 'wgConf' => new SiteConfiguration(),
74 ] );
77 $this->assertEquals( $expected, WikiMap::getWiki( $wikiId ) );
80 public static function provideGetWikiName() {
81 return [
82 'unknown' => [ 'xyzzy', 'xyzzy' ],
83 'enwiki' => [ 'en.example.org', 'enwiki' ],
84 'ruwiki' => [ 'ru.example.org', 'ruwiki' ],
85 'enwiktionary (sites)' => [ 'en.wiktionary.org', 'enwiktionary' ],
89 /**
90 * @dataProvider provideGetWikiName
92 public function testGetWikiName( $expected, $wikiId ) {
93 $this->assertEquals( $expected, WikiMap::getWikiName( $wikiId ) );
96 public static function provideMakeForeignLink() {
97 return [
98 'unknown' => [ false, 'xyzzy', 'Foo' ],
99 'enwiki' => [
100 '<a class="external" rel="nofollow" ' .
101 'href="http://en.example.org/w/Foo">Foo</a>',
102 'enwiki',
103 'Foo'
105 'ruwiki' => [
106 '<a class="external" rel="nofollow" ' .
107 'href="//ru.example.org/wiki/%D0%A4%D1%83">вар</a>',
108 'ruwiki',
109 'Фу',
110 'вар'
112 'enwiktionary (sites)' => [
113 '<a class="external" rel="nofollow" ' .
114 'href="https://en.wiktionary.org/wiki/Kitten">Kittens!</a>',
115 'enwiktionary',
116 'Kitten',
117 'Kittens!'
123 * @dataProvider provideMakeForeignLink
125 public function testMakeForeignLink( $expected, $wikiId, $page, $text = null ) {
126 $this->assertEquals(
127 $expected,
128 WikiMap::makeForeignLink( $wikiId, $page, $text )
132 public static function provideForeignUserLink() {
133 return [
134 'unknown' => [ false, 'xyzzy', 'Foo' ],
135 'enwiki' => [
136 '<a class="external" rel="nofollow" ' .
137 'href="http://en.example.org/w/User:Foo">User:Foo</a>',
138 'enwiki',
139 'Foo'
141 'ruwiki' => [
142 '<a class="external" rel="nofollow" ' .
143 'href="//ru.example.org/wiki/User:%D0%A4%D1%83">вар</a>',
144 'ruwiki',
145 'Фу',
146 'вар'
148 'enwiktionary (sites)' => [
149 '<a class="external" rel="nofollow" ' .
150 'href="https://en.wiktionary.org/wiki/User:Dummy">Whatever</a>',
151 'enwiktionary',
152 'Dummy',
153 'Whatever'
159 * @dataProvider provideForeignUserLink
161 public function testForeignUserLink( $expected, $wikiId, $user, $text = null ) {
162 $this->assertEquals( $expected, WikiMap::foreignUserLink( $wikiId, $user, $text ) );
165 public static function provideGetForeignURL() {
166 return [
167 'unknown' => [ false, 'xyzzy', 'Foo' ],
168 'enwiki' => [ 'http://en.example.org/w/Foo', 'enwiki', 'Foo' ],
169 'enwiktionary (sites)' => [
170 'https://en.wiktionary.org/wiki/Testme',
171 'enwiktionary',
172 'Testme'
174 'ruwiki with fragment' => [
175 '//ru.example.org/wiki/%D0%A4%D1%83#%D0%B2%D0%B0%D1%80',
176 'ruwiki',
177 'Фу',
178 'вар'
184 * @dataProvider provideGetForeignURL
186 public function testGetForeignURL( $expected, $wikiId, $page, $fragment = null ) {
187 $this->assertEquals( $expected, WikiMap::getForeignURL( $wikiId, $page, $fragment ) );
191 * @covers \MediaWiki\WikiMap\WikiMap::getCanonicalServerInfoForAllWikis()
193 public function testGetCanonicalServerInfoForAllWikis() {
194 $expected = [
195 'thiswiki' => [
196 'url' => '//this.wiki.org',
197 'parts' => [ 'scheme' => '', 'host' => 'this.wiki.org', 'delimiter' => '//' ]
199 'enwiki' => [
200 'url' => 'http://en.example.org',
201 'parts' => [
202 'scheme' => 'http', 'host' => 'en.example.org', 'delimiter' => '://' ]
204 'ruwiki' => [
205 'url' => '//ru.example.org',
206 'parts' => [ 'scheme' => '', 'host' => 'ru.example.org', 'delimiter' => '//' ]
210 $this->assertArrayEquals(
211 $expected,
212 WikiMap::getCanonicalServerInfoForAllWikis(),
213 true,
214 true
218 public static function provideGetWikiFromUrl() {
219 return [
220 [ 'http://this.wiki.org', 'thiswiki' ],
221 [ 'https://this.wiki.org', 'thiswiki' ],
222 [ 'http://this.wiki.org/$1', 'thiswiki' ],
223 [ 'https://this.wiki.org/$2', 'thiswiki' ],
224 [ 'http://en.example.org', 'enwiki' ],
225 [ 'https://en.example.org', 'enwiki' ],
226 [ 'http://en.example.org/$1', 'enwiki' ],
227 [ 'https://en.example.org/$2', 'enwiki' ],
228 [ 'http://ru.example.org', 'ruwiki' ],
229 [ 'https://ru.example.org', 'ruwiki' ],
230 [ 'http://ru.example.org/$1', 'ruwiki' ],
231 [ 'https://ru.example.org/$2', 'ruwiki' ],
232 [ 'http://not.defined.org', false ]
237 * @dataProvider provideGetWikiFromUrl
238 * @covers \MediaWiki\WikiMap\WikiMap::getWikiFromUrl()
240 public function testGetWikiFromUrl( $url, $wiki ) {
241 $this->assertEquals( $wiki, WikiMap::getWikiFromUrl( $url ) );
244 public static function provideGetWikiIdFromDbDomain() {
245 return [
246 [ 'db-prefix_', 'db-prefix_' ],
247 [ WikiMap::getCurrentWikiId(), WikiMap::getCurrentWikiId() ],
248 [ new DatabaseDomain( 'db-dash', null, 'prefix_' ), 'db-dash-prefix_' ],
249 [ WikiMap::getCurrentWikiId(), WikiMap::getCurrentWikiId() ],
250 [ new DatabaseDomain( 'db-dash', null, 'prefix_' ), 'db-dash-prefix_' ],
251 [ new DatabaseDomain( 'db', 'mediawiki', 'prefix_' ), 'db-prefix_' ], // schema ignored
252 [ new DatabaseDomain( 'db', 'custom', 'prefix_' ), 'db-custom-prefix_' ],
257 * @dataProvider provideGetWikiIdFromDbDomain
258 * @covers \MediaWiki\WikiMap\WikiMap::getWikiIdFromDbDomain()
260 public function testGetWikiIdFromDbDomain( $domain, $wikiId ) {
261 $this->assertEquals( $wikiId, WikiMap::getWikiIdFromDbDomain( $domain ) );
265 * @covers \MediaWiki\WikiMap\WikiMap::isCurrentWikiDbDomain()
266 * @covers \MediaWiki\WikiMap\WikiMap::getCurrentWikiDbDomain()
268 public function testIsCurrentWikiDomain() {
269 $this->overrideConfigValue( MainConfigNames::DBmwschema, 'mediawiki' );
271 $localDomain = WikiMap::getCurrentWikiDbDomain()->getId();
272 $this->assertTrue( WikiMap::isCurrentWikiDbDomain( $localDomain ) );
274 $localDomain = DatabaseDomain::newFromId( $localDomain );
275 $domain1 = new DatabaseDomain(
276 $localDomain->getDatabase(), 'someschema', $localDomain->getTablePrefix() );
277 $domain2 = new DatabaseDomain(
278 $localDomain->getDatabase(), null, $localDomain->getTablePrefix() );
280 $this->assertFalse( WikiMap::isCurrentWikiDbDomain( $domain1 ), 'Schema not ignored' );
281 $this->assertFalse( WikiMap::isCurrentWikiDbDomain( $domain2 ), 'Null schema not ignored' );
283 $this->assertTrue( WikiMap::isCurrentWikiDbDomain( WikiMap::getCurrentWikiDbDomain() ) );
286 public static function provideIsCurrentWikiId() {
287 return [
288 [ 'db', 'db', null, '' ],
289 [ 'db-schema-', 'db', 'schema', '' ],
290 [ 'db', 'db', 'mediawiki', '' ], // common b/c case
291 [ 'db-prefix_', 'db', null, 'prefix_' ],
292 [ 'db-schema-prefix_', 'db', 'schema', 'prefix_' ],
293 [ 'db-prefix_', 'db', 'mediawiki', 'prefix_' ], // common b/c case
294 // Bad hyphen cases (best effort support)
295 [ 'db-stuff', 'db-stuff', null, '' ],
296 [ 'db-stuff-prefix_', 'db-stuff', null, 'prefix_' ],
297 [ 'db-stuff-schema-', 'db-stuff', 'schema', '' ],
298 [ 'db-stuff-schema-prefix_', 'db-stuff', 'schema', 'prefix_' ],
299 [ 'db-stuff-prefix_', 'db-stuff', 'mediawiki', 'prefix_' ] // common b/c case
304 * @dataProvider provideIsCurrentWikiId
305 * @covers \MediaWiki\WikiMap\WikiMap::isCurrentWikiId()
306 * @covers \MediaWiki\WikiMap\WikiMap::getCurrentWikiDbDomain()
307 * @covers \MediaWiki\WikiMap\WikiMap::getWikiIdFromDbDomain()
309 public function testIsCurrentWikiId( $wikiId, $db, $schema, $prefix ) {
310 $this->overrideConfigValues( [
311 MainConfigNames::DBname => $db,
312 MainConfigNames::DBmwschema => $schema,
313 MainConfigNames::DBprefix => $prefix
314 ] );
316 $this->assertTrue( WikiMap::isCurrentWikiId( $wikiId ), "ID matches" );
317 $this->assertNotTrue( WikiMap::isCurrentWikiId( $wikiId . '-more' ), "Bogus ID" );