Add sslCAFile option to DatabaseMysqli
[mediawiki.git] / tests / phpunit / includes / interwiki / ClassicInterwikiLookupTest.php
blobfd3b0b856a10dddecc850686ef4ed649d3af5928
1 <?php
2 /**
3 * @covers MediaWiki\Interwiki\ClassicInterwikiLookup
5 * @group MediaWiki
6 * @group Database
7 */
8 class ClassicInterwikiLookupTest extends MediaWikiTestCase {
10 private function populateDB( $iwrows ) {
11 $dbw = wfGetDB( DB_MASTER );
12 $dbw->delete( 'interwiki', '*', __METHOD__ );
13 $dbw->insert( 'interwiki', array_values( $iwrows ), __METHOD__ );
14 $this->tablesUsed[] = 'interwiki';
17 public function testDatabaseStorage() {
18 // NOTE: database setup is expensive, so we only do
19 // it once and run all the tests in one go.
20 $dewiki = [
21 'iw_prefix' => 'de',
22 'iw_url' => 'http://de.wikipedia.org/wiki/',
23 'iw_api' => 'http://de.wikipedia.org/w/api.php',
24 'iw_wikiid' => 'dewiki',
25 'iw_local' => 1,
26 'iw_trans' => 0
29 $zzwiki = [
30 'iw_prefix' => 'zz',
31 'iw_url' => 'http://zzwiki.org/wiki/',
32 'iw_api' => 'http://zzwiki.org/w/api.php',
33 'iw_wikiid' => 'zzwiki',
34 'iw_local' => 0,
35 'iw_trans' => 0
38 $this->populateDB( [ $dewiki, $zzwiki ] );
39 $lookup = new \MediaWiki\Interwiki\ClassicInterwikiLookup(
40 Language::factory( 'en' ),
41 WANObjectCache::newEmpty(),
42 60 * 60,
43 false,
45 'en'
48 $this->assertEquals(
49 [ $dewiki, $zzwiki ],
50 $lookup->getAllPrefixes(),
51 'getAllPrefixes()'
53 $this->assertEquals(
54 [ $dewiki ],
55 $lookup->getAllPrefixes( true ),
56 'getAllPrefixes()'
58 $this->assertEquals(
59 [ $zzwiki ],
60 $lookup->getAllPrefixes( false ),
61 'getAllPrefixes()'
64 $this->assertTrue( $lookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
65 $this->assertFalse( $lookup->isValidInterwiki( 'xyz' ), 'unknown prefix is valid' );
67 $this->assertNull( $lookup->fetch( null ), 'no prefix' );
68 $this->assertFalse( $lookup->fetch( 'xyz' ), 'unknown prefix' );
70 $interwiki = $lookup->fetch( 'de' );
71 $this->assertInstanceOf( 'Interwiki', $interwiki );
72 $this->assertSame( $interwiki, $lookup->fetch( 'de' ), 'in-process caching' );
74 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
75 $this->assertSame( 'http://de.wikipedia.org/w/api.php', $interwiki->getAPI(), 'getAPI' );
76 $this->assertSame( 'dewiki', $interwiki->getWikiID(), 'getWikiID' );
77 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
78 $this->assertSame( false, $interwiki->isTranscludable(), 'isTranscludable' );
80 $lookup->invalidateCache( 'de' );
81 $this->assertNotSame( $interwiki, $lookup->fetch( 'de' ), 'invalidate cache' );
84 /**
85 * @param string $thisSite
86 * @param string[] $local
87 * @param string[] $global
89 * @return string[]
91 private function populateHash( $thisSite, $local, $global ) {
92 $hash = [];
93 $hash[ '__sites:' . wfWikiID() ] = $thisSite;
95 $globals = [];
96 $locals = [];
98 foreach ( $local as $row ) {
99 $prefix = $row['iw_prefix'];
100 $data = $row['iw_local'] . ' ' . $row['iw_url'];
101 $locals[] = $prefix;
102 $hash[ "_{$thisSite}:{$prefix}" ] = $data;
105 foreach ( $global as $row ) {
106 $prefix = $row['iw_prefix'];
107 $data = $row['iw_local'] . ' ' . $row['iw_url'];
108 $globals[] = $prefix;
109 $hash[ "__global:{$prefix}" ] = $data;
112 $hash[ '__list:__global' ] = implode( ' ', $globals );
113 $hash[ '__list:_' . $thisSite ] = implode( ' ', $locals );
115 return $hash;
118 private function populateCDB( $thisSite, $local, $global ) {
119 $cdbFile = tempnam( wfTempDir(), 'MW-ClassicInterwikiLookupTest-' ) . '.cdb';
120 $cdb = \Cdb\Writer::open( $cdbFile );
122 $hash = $this->populateHash( $thisSite, $local, $global );
124 foreach ( $hash as $key => $value ) {
125 $cdb->set( $key, $value );
128 $cdb->close();
129 return $cdbFile;
132 public function testCDBStorage() {
133 // NOTE: CDB setup is expensive, so we only do
134 // it once and run all the tests in one go.
136 $zzwiki = [
137 'iw_prefix' => 'zz',
138 'iw_url' => 'http://zzwiki.org/wiki/',
139 'iw_local' => 0
142 $dewiki = [
143 'iw_prefix' => 'de',
144 'iw_url' => 'http://de.wikipedia.org/wiki/',
145 'iw_local' => 1
148 $cdbFile = $this->populateCDB(
149 'en',
150 [ $dewiki ],
151 [ $zzwiki ]
153 $lookup = new \MediaWiki\Interwiki\ClassicInterwikiLookup(
154 Language::factory( 'en' ),
155 WANObjectCache::newEmpty(),
156 60 * 60,
157 $cdbFile,
159 'en'
162 $this->assertEquals(
163 [ $zzwiki, $dewiki ],
164 $lookup->getAllPrefixes(),
165 'getAllPrefixes()'
168 $this->assertTrue( $lookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
169 $this->assertTrue( $lookup->isValidInterwiki( 'zz' ), 'known prefix is valid' );
171 $interwiki = $lookup->fetch( 'de' );
172 $this->assertInstanceOf( 'Interwiki', $interwiki );
174 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
175 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
177 $interwiki = $lookup->fetch( 'zz' );
178 $this->assertInstanceOf( 'Interwiki', $interwiki );
180 $this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
181 $this->assertSame( false, $interwiki->isLocal(), 'isLocal' );
183 // cleanup temp file
184 unlink( $cdbFile );
187 public function testArrayStorage() {
188 $zzwiki = [
189 'iw_prefix' => 'zz',
190 'iw_url' => 'http://zzwiki.org/wiki/',
191 'iw_local' => 0
193 $dewiki = [
194 'iw_prefix' => 'de',
195 'iw_url' => 'http://de.wikipedia.org/wiki/',
196 'iw_local' => 1
199 $hash = $this->populateHash(
200 'en',
201 [ $dewiki ],
202 [ $zzwiki ]
204 $lookup = new \MediaWiki\Interwiki\ClassicInterwikiLookup(
205 Language::factory( 'en' ),
206 WANObjectCache::newEmpty(),
207 60 * 60,
208 $hash,
210 'en'
213 $this->assertEquals(
214 [ $zzwiki, $dewiki ],
215 $lookup->getAllPrefixes(),
216 'getAllPrefixes()'
219 $this->assertTrue( $lookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
220 $this->assertTrue( $lookup->isValidInterwiki( 'zz' ), 'known prefix is valid' );
222 $interwiki = $lookup->fetch( 'de' );
223 $this->assertInstanceOf( 'Interwiki', $interwiki );
225 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
226 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
228 $interwiki = $lookup->fetch( 'zz' );
229 $this->assertInstanceOf( 'Interwiki', $interwiki );
231 $this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
232 $this->assertSame( false, $interwiki->isLocal(), 'isLocal' );
235 public function testGetAllPrefixes() {
236 $zz = [
237 'iw_prefix' => 'zz',
238 'iw_url' => 'https://azz.example.org/',
239 'iw_local' => 1
241 $de = [
242 'iw_prefix' => 'de',
243 'iw_url' => 'https://de.example.org/',
244 'iw_local' => 1
246 $azz = [
247 'iw_prefix' => 'azz',
248 'iw_url' => 'https://azz.example.org/',
249 'iw_local' => 1
252 $hash = $this->populateHash(
253 'en',
255 [ $zz, $de, $azz ]
257 $lookup = new \MediaWiki\Interwiki\ClassicInterwikiLookup(
258 Language::factory( 'en' ),
259 WANObjectCache::newEmpty(),
260 60 * 60,
261 $hash,
263 'en'
266 $this->assertEquals(
267 [ $zz, $de, $azz ],
268 $lookup->getAllPrefixes(),
269 'getAllPrefixes() - preserves order'