Merge "MessagesGom_deva: Correct syntax in namespace alias"
[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 ],
20 'wgArticlePath' => [
21 'enwiki' => '/w/$1',
22 'ruwiki' => '/wiki/$1',
23 ],
24 ];
25 $conf->suffixes = [ 'wiki' ];
26 $this->setMwGlobals( [
27 'wgConf' => $conf,
28 ] );
29
30 TestSites::insertIntoDb();
31 }
32
33 public function provideGetWiki() {
34 // As provided by $wgConf
35 $enwiki = new WikiReference( 'http://en.example.org', '/w/$1' );
36 $ruwiki = new WikiReference( '//ru.example.org', '/wiki/$1' );
37
38 // Created from site objects
39 $nlwiki = new WikiReference( 'https://nl.wikipedia.org', '/wiki/$1' );
40 // enwiktionary doesn't have an interwiki id, thus this falls back to minor = lang code
41 $enwiktionary = new WikiReference( 'https://en.wiktionary.org', '/wiki/$1' );
42
43 return [
44 'unknown' => [ null, 'xyzzy' ],
45 'enwiki (wgConf)' => [ $enwiki, 'enwiki' ],
46 'ruwiki (wgConf)' => [ $ruwiki, 'ruwiki' ],
47 'nlwiki (sites)' => [ $nlwiki, 'nlwiki', false ],
48 'enwiktionary (sites)' => [ $enwiktionary, 'enwiktionary', false ],
49 'non MediaWiki site' => [ null, 'spam', false ],
50 'boguswiki' => [ null, 'boguswiki' ],
51 'nopathwiki' => [ null, 'nopathwiki' ],
52 ];
53 }
54
55 /**
56 * @dataProvider provideGetWiki
57 */
58 public function testGetWiki( $expected, $wikiId, $useWgConf = true ) {
59 if ( !$useWgConf ) {
60 $this->setMwGlobals( [
61 'wgConf' => new SiteConfiguration(),
62 ] );
63 }
64
65 $this->assertEquals( $expected, WikiMap::getWiki( $wikiId ) );
66 }
67
68 public function provideGetWikiName() {
69 return [
70 'unknown' => [ 'xyzzy', 'xyzzy' ],
71 'enwiki' => [ 'en.example.org', 'enwiki' ],
72 'ruwiki' => [ 'ru.example.org', 'ruwiki' ],
73 'enwiktionary (sites)' => [ 'en.wiktionary.org', 'enwiktionary' ],
74 ];
75 }
76
77 /**
78 * @dataProvider provideGetWikiName
79 */
80 public function testGetWikiName( $expected, $wikiId ) {
81 $this->assertEquals( $expected, WikiMap::getWikiName( $wikiId ) );
82 }
83
84 public function provideMakeForeignLink() {
85 return [
86 'unknown' => [ false, 'xyzzy', 'Foo' ],
87 'enwiki' => [
88 '<a class="external" rel="nofollow" ' .
89 'href="http://en.example.org/w/Foo">Foo</a>',
90 'enwiki',
91 'Foo'
92 ],
93 'ruwiki' => [
94 '<a class="external" rel="nofollow" ' .
95 'href="//ru.example.org/wiki/%D0%A4%D1%83">вар</a>',
96 'ruwiki',
97 'Фу',
98 'вар'
99 ],
100 'enwiktionary (sites)' => [
101 '<a class="external" rel="nofollow" ' .
102 'href="https://en.wiktionary.org/wiki/Kitten">Kittens!</a>',
103 'enwiktionary',
104 'Kitten',
105 'Kittens!'
106 ],
107 ];
108 }
109
110 /**
111 * @dataProvider provideMakeForeignLink
112 */
113 public function testMakeForeignLink( $expected, $wikiId, $page, $text = null ) {
114 $this->assertEquals(
115 $expected,
116 WikiMap::makeForeignLink( $wikiId, $page, $text )
117 );
118 }
119
120 public function provideForeignUserLink() {
121 return [
122 'unknown' => [ false, 'xyzzy', 'Foo' ],
123 'enwiki' => [
124 '<a class="external" rel="nofollow" ' .
125 'href="http://en.example.org/w/User:Foo">User:Foo</a>',
126 'enwiki',
127 'Foo'
128 ],
129 'ruwiki' => [
130 '<a class="external" rel="nofollow" ' .
131 'href="//ru.example.org/wiki/User:%D0%A4%D1%83">вар</a>',
132 'ruwiki',
133 'Фу',
134 'вар'
135 ],
136 'enwiktionary (sites)' => [
137 '<a class="external" rel="nofollow" ' .
138 'href="https://en.wiktionary.org/wiki/User:Dummy">Whatever</a>',
139 'enwiktionary',
140 'Dummy',
141 'Whatever'
142 ],
143 ];
144 }
145
146 /**
147 * @dataProvider provideForeignUserLink
148 */
149 public function testForeignUserLink( $expected, $wikiId, $user, $text = null ) {
150 $this->assertEquals( $expected, WikiMap::foreignUserLink( $wikiId, $user, $text ) );
151 }
152
153 public function provideGetForeignURL() {
154 return [
155 'unknown' => [ false, 'xyzzy', 'Foo' ],
156 'enwiki' => [ 'http://en.example.org/w/Foo', 'enwiki', 'Foo' ],
157 'enwiktionary (sites)' => [
158 'https://en.wiktionary.org/wiki/Testme',
159 'enwiktionary',
160 'Testme'
161 ],
162 'ruwiki with fragment' => [
163 '//ru.example.org/wiki/%D0%A4%D1%83#%D0%B2%D0%B0%D1%80',
164 'ruwiki',
165 'Фу',
166 'вар'
167 ],
168 ];
169 }
170
171 /**
172 * @dataProvider provideGetForeignURL
173 */
174 public function testGetForeignURL( $expected, $wikiId, $page, $fragment = null ) {
175 $this->assertEquals( $expected, WikiMap::getForeignURL( $wikiId, $page, $fragment ) );
176 }
177
178 }