Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / interwiki / ClassicInterwikiLookupTest.php
blob3c163cadfa99646e16317b9b218d9432a94669ef
1 <?php
3 use MediaWiki\Config\ServiceOptions;
4 use MediaWiki\Interwiki\ClassicInterwikiLookup;
5 use MediaWiki\MainConfigNames;
6 use MediaWiki\WikiMap\WikiMap;
7 use Wikimedia\ObjectCache\WANObjectCache;
9 /**
10 * @covers \MediaWiki\Interwiki\ClassicInterwikiLookup
11 * @group Database
13 class ClassicInterwikiLookupTest extends MediaWikiIntegrationTestCase {
15 private function populateDB( $iwrows ) {
16 $this->db->newInsertQueryBuilder()
17 ->insertInto( 'interwiki' )
18 ->rows( $iwrows )
19 ->caller( __METHOD__ )
20 ->execute();
23 /**
24 * @param string[]|false $interwikiData
25 * @return ClassicInterwikiLookup
27 private function getClassicInterwikiLookup( $interwikiData ): ClassicInterwikiLookup {
28 $services = $this->getServiceContainer();
29 $lang = $services->getLanguageFactory()->getLanguage( 'en' );
30 $config = [
31 MainConfigNames::InterwikiExpiry => 60 * 60,
32 MainConfigNames::InterwikiCache => $interwikiData,
33 MainConfigNames::InterwikiFallbackSite => 'en',
34 MainConfigNames::InterwikiScopes => 3,
35 MainConfigNames::InterwikiMagic => true,
36 MainConfigNames::VirtualDomainsMapping => [],
37 'wikiId' => WikiMap::getCurrentWikiId(),
40 return new ClassicInterwikiLookup(
41 new ServiceOptions(
42 ClassicInterwikiLookup::CONSTRUCTOR_OPTIONS,
43 $config
45 $lang,
46 WANObjectCache::newEmpty(),
47 $services->getHookContainer(),
48 $services->getConnectionProvider(),
49 $services->getLanguageNameUtils()
53 public function testDatabaseStorage() {
54 // NOTE: database setup is expensive, so we only do
55 // it once and run all the tests in one go.
56 $dewiki = [
57 'iw_prefix' => 'de',
58 'iw_url' => 'http://de.wikipedia.org/wiki/',
59 'iw_api' => 'http://de.wikipedia.org/w/api.php',
60 'iw_wikiid' => 'dewiki',
61 'iw_local' => 1,
62 'iw_trans' => 0
65 $zzwiki = [
66 'iw_prefix' => 'zz',
67 'iw_url' => 'http://zzwiki.org/wiki/',
68 'iw_api' => 'http://zzwiki.org/w/api.php',
69 'iw_wikiid' => 'zzwiki',
70 'iw_local' => 0,
71 'iw_trans' => 0
74 $this->populateDB( [ $dewiki, $zzwiki ] );
75 $lookup = $this->getClassicInterwikiLookup( false );
77 $this->assertEquals(
78 [ $dewiki, $zzwiki ],
79 $lookup->getAllPrefixes(),
80 'getAllPrefixes()'
82 $this->assertEquals(
83 [ $dewiki ],
84 $lookup->getAllPrefixes( true ),
85 'getAllPrefixes()'
87 $this->assertEquals(
88 [ $zzwiki ],
89 $lookup->getAllPrefixes( false ),
90 'getAllPrefixes()'
93 $this->assertTrue( $lookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
94 $this->assertFalse( $lookup->isValidInterwiki( 'xyz' ), 'unknown prefix is valid' );
96 $this->assertNull( $lookup->fetch( null ), 'no prefix' );
97 $this->assertFalse( $lookup->fetch( 'xyz' ), 'unknown prefix' );
99 $interwiki = $lookup->fetch( 'de' );
100 $this->assertInstanceOf( Interwiki::class, $interwiki );
101 $this->assertSame( $interwiki, $lookup->fetch( 'de' ), 'in-process caching' );
103 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
104 $this->assertSame( 'http://de.wikipedia.org/w/api.php', $interwiki->getAPI(), 'getAPI' );
105 $this->assertSame( 'dewiki', $interwiki->getWikiID(), 'getWikiID' );
106 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
107 $this->assertSame( false, $interwiki->isTranscludable(), 'isTranscludable' );
109 $lookup->invalidateCache( 'de' );
110 $this->assertNotSame( $interwiki, $lookup->fetch( 'de' ), 'invalidate cache' );
114 * @param string $thisSite
115 * @param string[][] $local
116 * @param string[][] $global
118 * @return string[]
120 private function populateHash( $thisSite, $local, $global ) {
121 $hash = [];
122 $hash[ '__sites:' . WikiMap::getCurrentWikiId() ] = $thisSite;
124 $globals = [];
125 $locals = [];
127 foreach ( $local as $row ) {
128 $prefix = $row['iw_prefix'];
129 $data = $row['iw_local'] . ' ' . $row['iw_url'];
130 $locals[] = $prefix;
131 $hash[ "_{$thisSite}:{$prefix}" ] = $data;
134 foreach ( $global as $row ) {
135 $prefix = $row['iw_prefix'];
136 $data = $row['iw_local'] . ' ' . $row['iw_url'];
137 $globals[] = $prefix;
138 $hash[ "__global:{$prefix}" ] = $data;
141 $hash[ '__list:__global' ] = implode( ' ', $globals );
142 $hash[ '__list:_' . $thisSite ] = implode( ' ', $locals );
144 return $hash;
147 public function testArrayStorage() {
148 $zzwiki = [
149 'iw_prefix' => 'zz',
150 'iw_url' => 'http://zzwiki.org/wiki/',
151 'iw_local' => 0
153 $dewiki = [
154 'iw_prefix' => 'de',
155 'iw_url' => 'http://de.wikipedia.org/wiki/',
156 'iw_local' => 1
159 $hash = $this->populateHash(
160 'en',
161 [ $dewiki ],
162 [ $zzwiki ]
164 $lookup = $this->getClassicInterwikiLookup( $hash );
166 $this->assertEquals(
167 [ $zzwiki, $dewiki ],
168 $lookup->getAllPrefixes(),
169 'getAllPrefixes()'
172 $this->assertTrue( $lookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
173 $this->assertTrue( $lookup->isValidInterwiki( 'zz' ), 'known prefix is valid' );
175 $interwiki = $lookup->fetch( 'de' );
176 $this->assertInstanceOf( Interwiki::class, $interwiki );
178 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
179 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
181 $interwiki = $lookup->fetch( 'zz' );
182 $this->assertInstanceOf( Interwiki::class, $interwiki );
184 $this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
185 $this->assertSame( false, $interwiki->isLocal(), 'isLocal' );
188 public function testGetAllPrefixes() {
189 $zz = [
190 'iw_prefix' => 'zz',
191 'iw_url' => 'https://azz.example.org/',
192 'iw_local' => 1
194 $de = [
195 'iw_prefix' => 'de',
196 'iw_url' => 'https://de.example.org/',
197 'iw_local' => 1
199 $azz = [
200 'iw_prefix' => 'azz',
201 'iw_url' => 'https://azz.example.org/',
202 'iw_local' => 1
205 $hash = $this->populateHash(
206 'en',
208 [ $zz, $de, $azz ]
210 $lookup = $this->getClassicInterwikiLookup( $hash );
212 $this->assertEquals(
213 [ $zz, $de, $azz ],
214 $lookup->getAllPrefixes(),
215 'getAllPrefixes() - preserves order'