Remove hard deprecation of PasswordPolicyChecks::checkPopularPasswordBlacklist
[lhc/web/wiklou.git] / tests / phpunit / includes / WikiMapTest.php
1 <?php
2
3 use Wikimedia\Rdbms\DatabaseDomain;
4
5 /**
6 * @covers WikiMap
7 *
8 * @group Database
9 */
10 class WikiMapTest extends MediaWikiLangTestCase {
11
12 public function setUp() {
13 parent::setUp();
14
15 $conf = new SiteConfiguration();
16 $conf->settings = [
17 'wgServer' => [
18 'enwiki' => 'http://en.example.org',
19 'ruwiki' => '//ru.example.org',
20 'nopathwiki' => '//nopath.example.org',
21 'thiswiki' => '//this.wiki.org'
22 ],
23 'wgArticlePath' => [
24 'enwiki' => '/w/$1',
25 'ruwiki' => '/wiki/$1',
26 ],
27 ];
28 $conf->suffixes = [ 'wiki' ];
29 $this->setMwGlobals( [
30 'wgConf' => $conf,
31 'wgLocalDatabases' => [ 'enwiki', 'ruwiki', 'nopathwiki' ],
32 'wgCanonicalServer' => '//this.wiki.org',
33 'wgDBname' => 'thiswiki',
34 'wgDBprefix' => ''
35 ] );
36
37 TestSites::insertIntoDb();
38 }
39
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' );
44
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' );
49
50 return [
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' ],
59 ];
60 }
61
62 /**
63 * @dataProvider provideGetWiki
64 */
65 public function testGetWiki( $expected, $wikiId, $useWgConf = true ) {
66 if ( !$useWgConf ) {
67 $this->setMwGlobals( [
68 'wgConf' => new SiteConfiguration(),
69 ] );
70 }
71
72 $this->assertEquals( $expected, WikiMap::getWiki( $wikiId ) );
73 }
74
75 public function provideGetWikiName() {
76 return [
77 'unknown' => [ 'xyzzy', 'xyzzy' ],
78 'enwiki' => [ 'en.example.org', 'enwiki' ],
79 'ruwiki' => [ 'ru.example.org', 'ruwiki' ],
80 'enwiktionary (sites)' => [ 'en.wiktionary.org', 'enwiktionary' ],
81 ];
82 }
83
84 /**
85 * @dataProvider provideGetWikiName
86 */
87 public function testGetWikiName( $expected, $wikiId ) {
88 $this->assertEquals( $expected, WikiMap::getWikiName( $wikiId ) );
89 }
90
91 public function provideMakeForeignLink() {
92 return [
93 'unknown' => [ false, 'xyzzy', 'Foo' ],
94 'enwiki' => [
95 '<a class="external" rel="nofollow" ' .
96 'href="http://en.example.org/w/Foo">Foo</a>',
97 'enwiki',
98 'Foo'
99 ],
100 'ruwiki' => [
101 '<a class="external" rel="nofollow" ' .
102 'href="//ru.example.org/wiki/%D0%A4%D1%83">вар</a>',
103 'ruwiki',
104 'Фу',
105 'вар'
106 ],
107 'enwiktionary (sites)' => [
108 '<a class="external" rel="nofollow" ' .
109 'href="https://en.wiktionary.org/wiki/Kitten">Kittens!</a>',
110 'enwiktionary',
111 'Kitten',
112 'Kittens!'
113 ],
114 ];
115 }
116
117 /**
118 * @dataProvider provideMakeForeignLink
119 */
120 public function testMakeForeignLink( $expected, $wikiId, $page, $text = null ) {
121 $this->assertEquals(
122 $expected,
123 WikiMap::makeForeignLink( $wikiId, $page, $text )
124 );
125 }
126
127 public function provideForeignUserLink() {
128 return [
129 'unknown' => [ false, 'xyzzy', 'Foo' ],
130 'enwiki' => [
131 '<a class="external" rel="nofollow" ' .
132 'href="http://en.example.org/w/User:Foo">User:Foo</a>',
133 'enwiki',
134 'Foo'
135 ],
136 'ruwiki' => [
137 '<a class="external" rel="nofollow" ' .
138 'href="//ru.example.org/wiki/User:%D0%A4%D1%83">вар</a>',
139 'ruwiki',
140 'Фу',
141 'вар'
142 ],
143 'enwiktionary (sites)' => [
144 '<a class="external" rel="nofollow" ' .
145 'href="https://en.wiktionary.org/wiki/User:Dummy">Whatever</a>',
146 'enwiktionary',
147 'Dummy',
148 'Whatever'
149 ],
150 ];
151 }
152
153 /**
154 * @dataProvider provideForeignUserLink
155 */
156 public function testForeignUserLink( $expected, $wikiId, $user, $text = null ) {
157 $this->assertEquals( $expected, WikiMap::foreignUserLink( $wikiId, $user, $text ) );
158 }
159
160 public function provideGetForeignURL() {
161 return [
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',
166 'enwiktionary',
167 'Testme'
168 ],
169 'ruwiki with fragment' => [
170 '//ru.example.org/wiki/%D0%A4%D1%83#%D0%B2%D0%B0%D1%80',
171 'ruwiki',
172 'Фу',
173 'вар'
174 ],
175 ];
176 }
177
178 /**
179 * @dataProvider provideGetForeignURL
180 */
181 public function testGetForeignURL( $expected, $wikiId, $page, $fragment = null ) {
182 $this->assertEquals( $expected, WikiMap::getForeignURL( $wikiId, $page, $fragment ) );
183 }
184
185 /**
186 * @covers WikiMap::getCanonicalServerInfoForAllWikis()
187 */
188 public function testGetCanonicalServerInfoForAllWikis() {
189 $expected = [
190 'thiswiki' => [
191 'url' => '//this.wiki.org',
192 'parts' => [ 'scheme' => '', 'host' => 'this.wiki.org', 'delimiter' => '//' ]
193 ],
194 'enwiki' => [
195 'url' => 'http://en.example.org',
196 'parts' => [
197 'scheme' => 'http', 'host' => 'en.example.org', 'delimiter' => '://' ]
198 ],
199 'ruwiki' => [
200 'url' => '//ru.example.org',
201 'parts' => [ 'scheme' => '', 'host' => 'ru.example.org', 'delimiter' => '//' ]
202 ]
203 ];
204
205 $this->assertArrayEquals(
206 $expected,
207 WikiMap::getCanonicalServerInfoForAllWikis(),
208 true,
209 true
210 );
211 }
212
213 public function provideGetWikiFromUrl() {
214 return [
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 ]
228 ];
229 }
230
231 /**
232 * @dataProvider provideGetWikiFromUrl
233 * @covers WikiMap::getWikiFromUrl()
234 */
235 public function testGetWikiFromUrl( $url, $wiki ) {
236 $this->assertEquals( $wiki, WikiMap::getWikiFromUrl( $url ) );
237 }
238
239 public function provideGetWikiIdFromDbDomain() {
240 return [
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_' ],
248 ];
249 }
250
251 /**
252 * @dataProvider provideGetWikiIdFromDbDomain
253 * @covers WikiMap::getWikiIdFromDbDomain()
254 */
255 public function testGetWikiIdFromDbDomain( $domain, $wikiId ) {
256 $this->assertEquals( $wikiId, WikiMap::getWikiIdFromDbDomain( $domain ) );
257 }
258
259 /**
260 * @covers WikiMap::isCurrentWikiDbDomain()
261 * @covers WikiMap::getCurrentWikiDbDomain()
262 */
263 public function testIsCurrentWikiDomain() {
264 $this->setMwGlobals( 'wgDBmwschema', 'mediawiki' );
265
266 $localDomain = WikiMap::getCurrentWikiDbDomain()->getId();
267 $this->assertTrue( WikiMap::isCurrentWikiDbDomain( $localDomain ) );
268
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() );
274
275 $this->assertFalse( WikiMap::isCurrentWikiDbDomain( $domain1 ), 'Schema not ignored' );
276 $this->assertFalse( WikiMap::isCurrentWikiDbDomain( $domain2 ), 'Null schema not ignored' );
277
278 $this->assertTrue( WikiMap::isCurrentWikiDbDomain( WikiMap::getCurrentWikiDbDomain() ) );
279 }
280
281 public function provideIsCurrentWikiId() {
282 return [
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
295 ];
296 }
297
298 /**
299 * @dataProvider provideIsCurrentWikiId
300 * @covers WikiMap::isCurrentWikiId()
301 * @covers WikiMap::getCurrentWikiDbDomain()
302 * @covers WikiMap::getWikiIdFromDbDomain()
303 */
304 public function testIsCurrentWikiId( $wikiId, $db, $schema, $prefix ) {
305 $this->setMwGlobals(
306 [ 'wgDBname' => $db, 'wgDBmwschema' => $schema, 'wgDBprefix' => $prefix ] );
307
308 $this->assertTrue( WikiMap::isCurrentWikiId( $wikiId ), "ID matches" );
309 $this->assertNotTrue( WikiMap::isCurrentWikiId( $wikiId . '-more' ), "Bogus ID" );
310 }
311 }