Debian Codegraph @

tests/phpunit/includes/interwiki/ClassicInterwikiLookupTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Config\ServiceOptions;
4 use MediaWiki\Interwiki\ClassicInterwikiLookup;
5 use MediaWiki\MainConfigNames;
6 use MediaWiki\WikiMap\WikiMap;
7 use Wikimedia\ObjectCache\WANObjectCache;
8
9 /**
10 * @covers \MediaWiki\Interwiki\ClassicInterwikiLookup
11 * @group Database
12 */
13 class ClassicInterwikiLookupTest extends MediaWikiIntegrationTestCase {
14
15 private function populateDB( $iwrows ) {
16 $this->getDb()->newInsertQueryBuilder()
17 ->insertInto( 'interwiki' )
18 ->rows( $iwrows )
19 ->caller( __METHOD__ )
20 ->execute();
21 }
22
23 /**
24 * @param string[]|false $interwikiData
25 * @return ClassicInterwikiLookup
26 */
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 'wikiId' => WikiMap::getCurrentWikiId(),
36 ];
37
38 return new ClassicInterwikiLookup(
39 new ServiceOptions(
40 ClassicInterwikiLookup::CONSTRUCTOR_OPTIONS,
41 $config
42 ),
43 $lang,
44 WANObjectCache::newEmpty(),
45 $services->getHookContainer(),
46 $services->getConnectionProvider()
47 );
48 }
49
50 public function testDatabaseStorage() {
51 // NOTE: database setup is expensive, so we only do
52 // it once and run all the tests in one go.
53 $dewiki = [
54 'iw_prefix' => 'de',
55 'iw_url' => 'http://de.wikipedia.org/wiki/',
56 'iw_api' => 'http://de.wikipedia.org/w/api.php',
57 'iw_wikiid' => 'dewiki',
58 'iw_local' => 1,
59 'iw_trans' => 0
60 ];
61
62 $zzwiki = [
63 'iw_prefix' => 'zz',
64 'iw_url' => 'http://zzwiki.org/wiki/',
65 'iw_api' => 'http://zzwiki.org/w/api.php',
66 'iw_wikiid' => 'zzwiki',
67 'iw_local' => 0,
68 'iw_trans' => 0
69 ];
70
71 $this->populateDB( [ $dewiki, $zzwiki ] );
72 $lookup = $this->getClassicInterwikiLookup( false );
73
74 $this->assertEquals(
75 [ $dewiki, $zzwiki ],
76 $lookup->getAllPrefixes(),
77 'getAllPrefixes()'
78 );
79 $this->assertEquals(
80 [ $dewiki ],
81 $lookup->getAllPrefixes( true ),
82 'getAllPrefixes()'
83 );
84 $this->assertEquals(
85 [ $zzwiki ],
86 $lookup->getAllPrefixes( false ),
87 'getAllPrefixes()'
88 );
89
90 $this->assertTrue( $lookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
91 $this->assertFalse( $lookup->isValidInterwiki( 'xyz' ), 'unknown prefix is valid' );
92
93 $this->assertNull( $lookup->fetch( null ), 'no prefix' );
94 $this->assertFalse( $lookup->fetch( 'xyz' ), 'unknown prefix' );
95
96 $interwiki = $lookup->fetch( 'de' );
97 $this->assertInstanceOf( Interwiki::class, $interwiki );
98 $this->assertSame( $interwiki, $lookup->fetch( 'de' ), 'in-process caching' );
99
100 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
101 $this->assertSame( 'http://de.wikipedia.org/w/api.php', $interwiki->getAPI(), 'getAPI' );
102 $this->assertSame( 'dewiki', $interwiki->getWikiID(), 'getWikiID' );
103 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
104 $this->assertSame( false, $interwiki->isTranscludable(), 'isTranscludable' );
105
106 $lookup->invalidateCache( 'de' );
107 $this->assertNotSame( $interwiki, $lookup->fetch( 'de' ), 'invalidate cache' );
108 }
109
110 /**
111 * @param string $thisSite
112 * @param string[][] $local
113 * @param string[][] $global
114 *
115 * @return string[]
116 */
117 private function populateHash( $thisSite, $local, $global ) {
118 $hash = [];
119 $hash[ '__sites:' . WikiMap::getCurrentWikiId() ] = $thisSite;
120
121 $globals = [];
122 $locals = [];
123
124 foreach ( $local as $row ) {
125 $prefix = $row['iw_prefix'];
126 $data = $row['iw_local'] . ' ' . $row['iw_url'];
127 $locals[] = $prefix;
128 $hash[ "_{$thisSite}:{$prefix}" ] = $data;
129 }
130
131 foreach ( $global as $row ) {
132 $prefix = $row['iw_prefix'];
133 $data = $row['iw_local'] . ' ' . $row['iw_url'];
134 $globals[] = $prefix;
135 $hash[ "__global:{$prefix}" ] = $data;
136 }
137
138 $hash[ '__list:__global' ] = implode( ' ', $globals );
139 $hash[ '__list:_' . $thisSite ] = implode( ' ', $locals );
140
141 return $hash;
142 }
143
144 public function testArrayStorage() {
145 $zzwiki = [
146 'iw_prefix' => 'zz',
147 'iw_url' => 'http://zzwiki.org/wiki/',
148 'iw_local' => 0
149 ];
150 $dewiki = [
151 'iw_prefix' => 'de',
152 'iw_url' => 'http://de.wikipedia.org/wiki/',
153 'iw_local' => 1
154 ];
155
156 $hash = $this->populateHash(
157 'en',
158 [ $dewiki ],
159 [ $zzwiki ]
160 );
161 $lookup = $this->getClassicInterwikiLookup( $hash );
162
163 $this->assertEquals(
164 [ $zzwiki, $dewiki ],
165 $lookup->getAllPrefixes(),
166 'getAllPrefixes()'
167 );
168
169 $this->assertTrue( $lookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
170 $this->assertTrue( $lookup->isValidInterwiki( 'zz' ), 'known prefix is valid' );
171
172 $interwiki = $lookup->fetch( 'de' );
173 $this->assertInstanceOf( Interwiki::class, $interwiki );
174
175 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
176 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
177
178 $interwiki = $lookup->fetch( 'zz' );
179 $this->assertInstanceOf( Interwiki::class, $interwiki );
180
181 $this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
182 $this->assertSame( false, $interwiki->isLocal(), 'isLocal' );
183 }
184
185 public function testGetAllPrefixes() {
186 $zz = [
187 'iw_prefix' => 'zz',
188 'iw_url' => 'https://azz.example.org/',
189 'iw_local' => 1
190 ];
191 $de = [
192 'iw_prefix' => 'de',
193 'iw_url' => 'https://de.example.org/',
194 'iw_local' => 1
195 ];
196 $azz = [
197 'iw_prefix' => 'azz',
198 'iw_url' => 'https://azz.example.org/',
199 'iw_local' => 1
200 ];
201
202 $hash = $this->populateHash(
203 'en',
204 [],
205 [ $zz, $de, $azz ]
206 );
207 $lookup = $this->getClassicInterwikiLookup( $hash );
208
209 $this->assertEquals(
210 [ $zz, $de, $azz ],
211 $lookup->getAllPrefixes(),
212 'getAllPrefixes() - preserves order'
213 );
214 }
215
216 }