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