MediaWiki  1.33.1
WikiMapTest.php
Go to the documentation of this file.
1 <?php
3 
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 
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 
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 
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 
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 
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 
180  public function testGetForeignURL( $expected, $wikiId, $page, $fragment = null ) {
181  $this->assertEquals( $expected, WikiMap::getForeignURL( $wikiId, $page, $fragment ) );
182  }
183 
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,
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 
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  [ 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_' ],
247  ];
248  }
249 
254  public function testGetWikiIdFromDomain( $domain, $wikiId ) {
255  $this->assertEquals( $wikiId, WikiMap::getWikiIdFromDbDomain( $domain ) );
256  }
257 
262  public function testIsCurrentWikiDomain() {
263  $this->setMwGlobals( 'wgDBmwschema', 'mediawiki' );
264 
265  $localDomain = WikiMap::getCurrentWikiDbDomain()->getId();
266  $this->assertTrue( WikiMap::isCurrentWikiDbDomain( $localDomain ) );
267 
268  $localDomain = DatabaseDomain::newFromId( $localDomain );
269  $domain1 = new DatabaseDomain(
270  $localDomain->getDatabase(), 'someschema', $localDomain->getTablePrefix() );
271  $domain2 = new DatabaseDomain(
272  $localDomain->getDatabase(), null, $localDomain->getTablePrefix() );
273 
274  $this->assertFalse( WikiMap::isCurrentWikiDbDomain( $domain1 ), 'Schema not ignored' );
275  $this->assertFalse( WikiMap::isCurrentWikiDbDomain( $domain2 ), 'Null schema not ignored' );
276 
278  }
279 
280  public function provideIsCurrentWikiId() {
281  return [
282  [ 'db', 'db', null, '' ],
283  [ 'db-schema-','db', 'schema', '' ],
284  [ 'db','db', 'mediawiki', '' ], // common b/c case
285  [ 'db-prefix_', 'db', null, 'prefix_' ],
286  [ 'db-schema-prefix_', 'db', 'schema', 'prefix_' ],
287  [ 'db-prefix_', 'db', 'mediawiki', 'prefix_' ], // common b/c case
288  // Bad hyphen cases (best effort support)
289  [ 'db-stuff', 'db-stuff', null, '' ],
290  [ 'db-stuff-prefix_', 'db-stuff', null, 'prefix_' ],
291  [ 'db-stuff-schema-', 'db-stuff', 'schema', '' ],
292  [ 'db-stuff-schema-prefix_', 'db-stuff', 'schema', 'prefix_' ],
293  [ 'db-stuff-prefix_', 'db-stuff', 'mediawiki', 'prefix_' ] // common b/c case
294  ];
295  }
296 
303  public function testIsCurrentWikiId( $wikiId, $db, $schema, $prefix ) {
304  $this->setMwGlobals(
305  [ 'wgDBname' => $db, 'wgDBmwschema' => $schema, 'wgDBprefix' => $prefix ] );
306 
307  $this->assertTrue( WikiMap::isCurrentWikiId( $wikiId ), "ID matches" );
308  $this->assertNotTrue( WikiMap::isCurrentWikiId( $wikiId . '-more' ), "Bogus ID" );
309  }
310 }
WikiMap\isCurrentWikiDbDomain
static isCurrentWikiDbDomain( $domain)
Definition: WikiMap.php:303
WikiMapTest\provideGetWiki
provideGetWiki()
Definition: WikiMapTest.php:39
WikiMap\foreignUserLink
static foreignUserLink( $wikiID, $user, $text=null)
Convenience to get a link to a user page on a foreign wiki.
Definition: WikiMap.php:137
WikiMapTest\provideIsCurrentWikiId
provideIsCurrentWikiId()
Definition: WikiMapTest.php:280
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
MediaWikiTestCase\assertArrayEquals
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Definition: MediaWikiTestCase.php:2070
WikiMapTest\testForeignUserLink
testForeignUserLink( $expected, $wikiId, $user, $text=null)
provideForeignUserLink
Definition: WikiMapTest.php:155
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:292
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
SiteConfiguration
This is a class for holding configuration settings, particularly for multi-wiki sites.
Definition: SiteConfiguration.php:124
WikiMap\isCurrentWikiId
static isCurrentWikiId( $wikiId)
Definition: WikiMap.php:312
WikiMapTest\provideGetWikiIdFromDomain
provideGetWikiIdFromDomain()
Definition: WikiMapTest.php:238
WikiMap\getWikiFromUrl
static getWikiFromUrl( $url)
Definition: WikiMap.php:223
WikiMap\getForeignURL
static getForeignURL( $wikiID, $page, $fragmentId=null)
Convenience to get a url to a page on a foreign wiki.
Definition: WikiMap.php:171
WikiMapTest
WikiMap.
Definition: WikiMapTest.php:9
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
WikiMapTest\provideGetForeignURL
provideGetForeignURL()
Definition: WikiMapTest.php:159
WikiMapTest\testGetWikiFromUrl
testGetWikiFromUrl( $url, $wiki)
provideGetWikiFromUrl WikiMap::getWikiFromUrl()
Definition: WikiMapTest.php:234
WikiMapTest\testGetWikiIdFromDomain
testGetWikiIdFromDomain( $domain, $wikiId)
provideGetWikiIdFromDomain WikiMap::getWikiIdFromDbDomain()
Definition: WikiMapTest.php:254
WikiMap\getWikiIdFromDbDomain
static getWikiIdFromDbDomain( $domain)
Get the wiki ID of a database domain.
Definition: WikiMap.php:259
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
WikiMapTest\setUp
setUp()
Definition: WikiMapTest.php:11
WikiMapTest\testIsCurrentWikiId
testIsCurrentWikiId( $wikiId, $db, $schema, $prefix)
provideIsCurrentWikiId WikiMap::isCurrentWikiId() WikiMap::getCurrentWikiDbDomain() WikiMap::getWikiI...
Definition: WikiMapTest.php:303
WikiMap\getWiki
static getWiki( $wikiID)
Get a WikiReference object for $wikiID.
Definition: WikiMap.php:37
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
WikiMapTest\provideGetWikiFromUrl
provideGetWikiFromUrl()
Definition: WikiMapTest.php:212
wfWikiID
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Definition: GlobalFunctions.php:2602
WikiMap\makeForeignLink
static makeForeignLink( $wikiID, $page, $text=null)
Convenience to get a link to a page on a foreign wiki.
Definition: WikiMap.php:149
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:8
WikiMapTest\provideGetWikiName
provideGetWikiName()
Definition: WikiMapTest.php:74
WikiMapTest\provideForeignUserLink
provideForeignUserLink()
Definition: WikiMapTest.php:126
WikiMapTest\testGetForeignURL
testGetForeignURL( $expected, $wikiId, $page, $fragment=null)
provideGetForeignURL
Definition: WikiMapTest.php:180
WikiMapTest\testGetCanonicalServerInfoForAllWikis
testGetCanonicalServerInfoForAllWikis()
WikiMap::getCanonicalServerInfoForAllWikis()
Definition: WikiMapTest.php:187
WikiMapTest\testGetWiki
testGetWiki( $expected, $wikiId, $useWgConf=true)
provideGetWiki
Definition: WikiMapTest.php:64
WikiReference
Reference to a locally-hosted wiki.
Definition: WikiReference.php:26
WikiMapTest\testMakeForeignLink
testMakeForeignLink( $expected, $wikiId, $page, $text=null)
provideMakeForeignLink
Definition: WikiMapTest.php:119
WikiMapTest\testGetWikiName
testGetWikiName( $expected, $wikiId)
provideGetWikiName
Definition: WikiMapTest.php:86
TestSites\insertIntoDb
static insertIntoDb()
Inserts sites into the database for the unit tests that need them.
Definition: TestSites.php:107
WikiMapTest\provideMakeForeignLink
provideMakeForeignLink()
Definition: WikiMapTest.php:90
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/prefix specification for IDatabase domains.
Definition: DatabaseDomain.php:28
WikiMap\getWikiName
static getWikiName( $wikiID)
Convenience to get the wiki's display name.
Definition: WikiMap.php:120
WikiMap\getCanonicalServerInfoForAllWikis
static getCanonicalServerInfoForAllWikis()
Get canonical server info for all local wikis in the map that have one.
Definition: WikiMap.php:187
MediaWikiTestCase\$db
Database $db
Primary database.
Definition: MediaWikiTestCase.php:61
WikiMapTest\testIsCurrentWikiDomain
testIsCurrentWikiDomain()
WikiMap::isCurrentWikiDbDomain() WikiMap::getCurrentWikiDbDomain()
Definition: WikiMapTest.php:262