Merge "Move around "ا" to after "آ" and not before"
[lhc/web/wiklou.git] / tests / phpunit / includes / WikiMapTest.php
1 <?php
2
3 /**
4 * @covers WikiMap
5 *
6 * @group Database
7 */
8 class WikiMapTest extends MediaWikiLangTestCase {
9
10 public function setUp() {
11 parent::setUp();
12
13 $conf = new SiteConfiguration();
14 $conf->settings = [
15 'wgServer' => [
16 'enwiki' => 'http://en.example.org',
17 'ruwiki' => '//ru.example.org',
18 'nopathwiki' => '//nopath.example.org',
19 'thiswiki' => '//this.wiki.org'
20 ],
21 'wgArticlePath' => [
22 'enwiki' => '/w/$1',
23 'ruwiki' => '/wiki/$1',
24 ],
25 ];
26 $conf->suffixes = [ 'wiki' ];
27 $this->setMwGlobals( [
28 'wgConf' => $conf,
29 'wgLocalDatabases' => [ 'enwiki', 'ruwiki', 'nopathwiki' ],
30 'wgCanonicalServer' => '//this.wiki.org',
31 'wgDBname' => 'thiswiki',
32 'wgDBprefix' => ''
33 ] );
34
35 TestSites::insertIntoDb();
36 }
37
38 public function provideGetWiki() {
39 // As provided by $wgConf
40 $enwiki = new WikiReference( 'http://en.example.org', '/w/$1' );
41 $ruwiki = new WikiReference( '//ru.example.org', '/wiki/$1' );
42
43 // Created from site objects
44 $nlwiki = new WikiReference( 'https://nl.wikipedia.org', '/wiki/$1' );
45 // enwiktionary doesn't have an interwiki id, thus this falls back to minor = lang code
46 $enwiktionary = new WikiReference( 'https://en.wiktionary.org', '/wiki/$1' );
47
48 return [
49 'unknown' => [ null, 'xyzzy' ],
50 'enwiki (wgConf)' => [ $enwiki, 'enwiki' ],
51 'ruwiki (wgConf)' => [ $ruwiki, 'ruwiki' ],
52 'nlwiki (sites)' => [ $nlwiki, 'nlwiki', false ],
53 'enwiktionary (sites)' => [ $enwiktionary, 'enwiktionary', false ],
54 'non MediaWiki site' => [ null, 'spam', false ],
55 'boguswiki' => [ null, 'boguswiki' ],
56 'nopathwiki' => [ null, 'nopathwiki' ],
57 ];
58 }
59
60 /**
61 * @dataProvider provideGetWiki
62 */
63 public function testGetWiki( $expected, $wikiId, $useWgConf = true ) {
64 if ( !$useWgConf ) {
65 $this->setMwGlobals( [
66 'wgConf' => new SiteConfiguration(),
67 ] );
68 }
69
70 $this->assertEquals( $expected, WikiMap::getWiki( $wikiId ) );
71 }
72
73 public function provideGetWikiName() {
74 return [
75 'unknown' => [ 'xyzzy', 'xyzzy' ],
76 'enwiki' => [ 'en.example.org', 'enwiki' ],
77 'ruwiki' => [ 'ru.example.org', 'ruwiki' ],
78 'enwiktionary (sites)' => [ 'en.wiktionary.org', 'enwiktionary' ],
79 ];
80 }
81
82 /**
83 * @dataProvider provideGetWikiName
84 */
85 public function testGetWikiName( $expected, $wikiId ) {
86 $this->assertEquals( $expected, WikiMap::getWikiName( $wikiId ) );
87 }
88
89 public function provideMakeForeignLink() {
90 return [
91 'unknown' => [ false, 'xyzzy', 'Foo' ],
92 'enwiki' => [
93 '<a class="external" rel="nofollow" ' .
94 'href="http://en.example.org/w/Foo">Foo</a>',
95 'enwiki',
96 'Foo'
97 ],
98 'ruwiki' => [
99 '<a class="external" rel="nofollow" ' .
100 'href="//ru.example.org/wiki/%D0%A4%D1%83">вар</a>',
101 'ruwiki',
102 'Фу',
103 'вар'
104 ],
105 'enwiktionary (sites)' => [
106 '<a class="external" rel="nofollow" ' .
107 'href="https://en.wiktionary.org/wiki/Kitten">Kittens!</a>',
108 'enwiktionary',
109 'Kitten',
110 'Kittens!'
111 ],
112 ];
113 }
114
115 /**
116 * @dataProvider provideMakeForeignLink
117 */
118 public function testMakeForeignLink( $expected, $wikiId, $page, $text = null ) {
119 $this->assertEquals(
120 $expected,
121 WikiMap::makeForeignLink( $wikiId, $page, $text )
122 );
123 }
124
125 public function provideForeignUserLink() {
126 return [
127 'unknown' => [ false, 'xyzzy', 'Foo' ],
128 'enwiki' => [
129 '<a class="external" rel="nofollow" ' .
130 'href="http://en.example.org/w/User:Foo">User:Foo</a>',
131 'enwiki',
132 'Foo'
133 ],
134 'ruwiki' => [
135 '<a class="external" rel="nofollow" ' .
136 'href="//ru.example.org/wiki/User:%D0%A4%D1%83">вар</a>',
137 'ruwiki',
138 'Фу',
139 'вар'
140 ],
141 'enwiktionary (sites)' => [
142 '<a class="external" rel="nofollow" ' .
143 'href="https://en.wiktionary.org/wiki/User:Dummy">Whatever</a>',
144 'enwiktionary',
145 'Dummy',
146 'Whatever'
147 ],
148 ];
149 }
150
151 /**
152 * @dataProvider provideForeignUserLink
153 */
154 public function testForeignUserLink( $expected, $wikiId, $user, $text = null ) {
155 $this->assertEquals( $expected, WikiMap::foreignUserLink( $wikiId, $user, $text ) );
156 }
157
158 public function provideGetForeignURL() {
159 return [
160 'unknown' => [ false, 'xyzzy', 'Foo' ],
161 'enwiki' => [ 'http://en.example.org/w/Foo', 'enwiki', 'Foo' ],
162 'enwiktionary (sites)' => [
163 'https://en.wiktionary.org/wiki/Testme',
164 'enwiktionary',
165 'Testme'
166 ],
167 'ruwiki with fragment' => [
168 '//ru.example.org/wiki/%D0%A4%D1%83#%D0%B2%D0%B0%D1%80',
169 'ruwiki',
170 'Фу',
171 'вар'
172 ],
173 ];
174 }
175
176 /**
177 * @dataProvider provideGetForeignURL
178 */
179 public function testGetForeignURL( $expected, $wikiId, $page, $fragment = null ) {
180 $this->assertEquals( $expected, WikiMap::getForeignURL( $wikiId, $page, $fragment ) );
181 }
182
183 /**
184 * @covers WikiMap::getCanonicalServerInfoForAllWikis()
185 */
186 public function testGetCanonicalServerInfoForAllWikis() {
187 $expected = [
188 'thiswiki' => [
189 'url' => '//this.wiki.org',
190 'parts' => [ 'scheme' => '', 'host' => 'this.wiki.org', 'delimiter' => '//' ]
191 ],
192 'enwiki' => [
193 'url' => 'http://en.example.org',
194 'parts' => [
195 'scheme' => 'http', 'host' => 'en.example.org', 'delimiter' => '://' ]
196 ],
197 'ruwiki' => [
198 'url' => '//ru.example.org',
199 'parts' => [ 'scheme' => '', 'host' => 'ru.example.org', 'delimiter' => '//' ]
200 ]
201 ];
202
203 $this->assertArrayEquals(
204 $expected,
205 WikiMap::getCanonicalServerInfoForAllWikis(),
206 true,
207 true
208 );
209 }
210
211 public function provideGetWikiFromUrl() {
212 return [
213 [ 'http://this.wiki.org', 'thiswiki' ],
214 [ 'https://this.wiki.org', 'thiswiki' ],
215 [ 'http://this.wiki.org/$1', 'thiswiki' ],
216 [ 'https://this.wiki.org/$2', 'thiswiki' ],
217 [ 'http://en.example.org', 'enwiki' ],
218 [ 'https://en.example.org', 'enwiki' ],
219 [ 'http://en.example.org/$1', 'enwiki' ],
220 [ 'https://en.example.org/$2', 'enwiki' ],
221 [ 'http://ru.example.org', 'ruwiki' ],
222 [ 'https://ru.example.org', 'ruwiki' ],
223 [ 'http://ru.example.org/$1', 'ruwiki' ],
224 [ 'https://ru.example.org/$2', 'ruwiki' ],
225 [ 'http://not.defined.org', false ]
226 ];
227 }
228
229 /**
230 * @dataProvider provideGetWikiFromUrl
231 * @covers WikiMap::getWikiFromUrl()
232 */
233 public function testGetWikiFromUrl( $url, $wiki ) {
234 $this->assertEquals( $wiki, WikiMap::getWikiFromUrl( $url ) );
235 }
236 }