Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / interwiki / InterwikiTest.php
blobb1ad77a7e50284ff1d03c1b33eb8856b1baa18dc
1 <?php
2 use MediaWiki\MediaWikiServices;
4 /**
5 * @covers Interwiki
7 * @group MediaWiki
8 * @group Database
9 */
10 class InterwikiTest extends MediaWikiTestCase {
12 public function testConstructor() {
13 $interwiki = new Interwiki(
14 'xyz',
15 'http://xyz.acme.test/wiki/$1',
16 'http://xyz.acme.test/w/api.php',
17 'xyzwiki',
22 $this->setContentLang( 'qqx' );
24 $this->assertSame( '(interwiki-name-xyz)', $interwiki->getName() );
25 $this->assertSame( '(interwiki-desc-xyz)', $interwiki->getDescription() );
26 $this->assertSame( 'http://xyz.acme.test/w/api.php', $interwiki->getAPI() );
27 $this->assertSame( 'http://xyz.acme.test/wiki/$1', $interwiki->getURL() );
28 $this->assertSame( 'xyzwiki', $interwiki->getWikiID() );
29 $this->assertTrue( $interwiki->isLocal() );
30 $this->assertFalse( $interwiki->isTranscludable() );
33 public function testGetUrl() {
34 $interwiki = new Interwiki(
35 'xyz',
36 'http://xyz.acme.test/wiki/$1'
39 $this->assertSame( 'http://xyz.acme.test/wiki/$1', $interwiki->getURL() );
40 $this->assertSame( 'http://xyz.acme.test/wiki/Foo%26Bar', $interwiki->getURL( 'Foo&Bar' ) );
43 //// tests for static data access methods below ///////////////////////////////////////////////
45 private function populateDB( $iwrows ) {
46 $dbw = wfGetDB( DB_MASTER );
47 $dbw->delete( 'interwiki', '*', __METHOD__ );
48 $dbw->insert( 'interwiki', array_values( $iwrows ), __METHOD__ );
49 $this->tablesUsed[] = 'interwiki';
52 private function setWgInterwikiCache( $interwikiCache ) {
53 $this->overrideMwServices();
54 MediaWikiServices::getInstance()->resetServiceForTesting( 'InterwikiLookup' );
55 $this->setMwGlobals( 'wgInterwikiCache', $interwikiCache );
58 public function testDatabaseStorage() {
59 $this->markTestSkipped( 'Needs I37b8e8018b3 <https://gerrit.wikimedia.org/r/#/c/270555/>' );
61 // NOTE: database setup is expensive, so we only do
62 // it once and run all the tests in one go.
63 $dewiki = [
64 'iw_prefix' => 'de',
65 'iw_url' => 'http://de.wikipedia.org/wiki/',
66 'iw_api' => 'http://de.wikipedia.org/w/api.php',
67 'iw_wikiid' => 'dewiki',
68 'iw_local' => 1,
69 'iw_trans' => 0
72 $zzwiki = [
73 'iw_prefix' => 'zz',
74 'iw_url' => 'http://zzwiki.org/wiki/',
75 'iw_api' => 'http://zzwiki.org/w/api.php',
76 'iw_wikiid' => 'zzwiki',
77 'iw_local' => 0,
78 'iw_trans' => 0
81 $this->populateDB( [ $dewiki, $zzwiki ] );
83 $this->setWgInterwikiCache( false );
85 $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
86 $this->assertEquals(
87 [ $dewiki, $zzwiki ],
88 $interwikiLookup->getAllPrefixes(),
89 'getAllPrefixes()'
91 $this->assertEquals(
92 [ $dewiki ],
93 $interwikiLookup->getAllPrefixes( true ),
94 'getAllPrefixes()'
96 $this->assertEquals(
97 [ $zzwiki ],
98 $interwikiLookup->getAllPrefixes( false ),
99 'getAllPrefixes()'
102 $this->assertTrue( $interwikiLookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
103 $this->assertFalse( $interwikiLookup->isValidInterwiki( 'xyz' ), 'unknown prefix is valid' );
105 $this->assertNull( $interwikiLookup->fetch( null ), 'no prefix' );
106 $this->assertFalse( $interwikiLookup->fetch( 'xyz' ), 'unknown prefix' );
108 $interwiki = $interwikiLookup->fetch( 'de' );
109 $this->assertInstanceOf( 'Interwiki', $interwiki );
110 $this->assertSame( $interwiki, $interwikiLookup->fetch( 'de' ), 'in-process caching' );
112 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
113 $this->assertSame( 'http://de.wikipedia.org/w/api.php', $interwiki->getAPI(), 'getAPI' );
114 $this->assertSame( 'dewiki', $interwiki->getWikiID(), 'getWikiID' );
115 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
116 $this->assertSame( false, $interwiki->isTranscludable(), 'isTranscludable' );
118 Interwiki::invalidateCache( 'de' );
119 $this->assertNotSame( $interwiki, $interwikiLookup->fetch( 'de' ), 'invalidate cache' );
123 * @param string $thisSite
124 * @param string[] $local
125 * @param string[] $global
127 * @return string[]
129 private function populateHash( $thisSite, $local, $global ) {
130 $hash = [];
131 $hash[ '__sites:' . wfWikiID() ] = $thisSite;
133 $globals = [];
134 $locals = [];
136 foreach ( $local as $row ) {
137 $prefix = $row['iw_prefix'];
138 $data = $row['iw_local'] . ' ' . $row['iw_url'];
139 $locals[] = $prefix;
140 $hash[ "_{$thisSite}:{$prefix}" ] = $data;
143 foreach ( $global as $row ) {
144 $prefix = $row['iw_prefix'];
145 $data = $row['iw_local'] . ' ' . $row['iw_url'];
146 $globals[] = $prefix;
147 $hash[ "__global:{$prefix}" ] = $data;
150 $hash[ '__list:__global' ] = implode( ' ', $globals );
151 $hash[ '__list:_' . $thisSite ] = implode( ' ', $locals );
153 return $hash;
156 private function populateCDB( $thisSite, $local, $global ) {
157 $cdbFile = tempnam( wfTempDir(), 'MW-ClassicInterwikiLookupTest-' ) . '.cdb';
158 $cdb = CdbWriter::open( $cdbFile );
160 $hash = $this->populateHash( $thisSite, $local, $global );
162 foreach ( $hash as $key => $value ) {
163 $cdb->set( $key, $value );
166 $cdb->close();
167 return $cdbFile;
170 public function testCDBStorage() {
171 // NOTE: CDB setup is expensive, so we only do
172 // it once and run all the tests in one go.
174 $dewiki = [
175 'iw_prefix' => 'de',
176 'iw_url' => 'http://de.wikipedia.org/wiki/',
177 'iw_local' => 1
180 $zzwiki = [
181 'iw_prefix' => 'zz',
182 'iw_url' => 'http://zzwiki.org/wiki/',
183 'iw_local' => 0
186 $cdbFile = $this->populateCDB(
187 'en',
188 [ $dewiki ],
189 [ $zzwiki ]
192 $this->setWgInterwikiCache( $cdbFile );
194 $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
195 $this->assertEquals(
196 [ $dewiki, $zzwiki ],
197 $interwikiLookup->getAllPrefixes(),
198 'getAllPrefixes()'
201 $this->assertTrue( $interwikiLookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
202 $this->assertTrue( $interwikiLookup->isValidInterwiki( 'zz' ), 'known prefix is valid' );
204 $interwiki = $interwikiLookup->fetch( 'de' );
205 $this->assertInstanceOf( 'Interwiki', $interwiki );
207 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
208 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
210 $interwiki = $interwikiLookup->fetch( 'zz' );
211 $this->assertInstanceOf( 'Interwiki', $interwiki );
213 $this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
214 $this->assertSame( false, $interwiki->isLocal(), 'isLocal' );
216 // cleanup temp file
217 unlink( $cdbFile );
220 public function testArrayStorage() {
221 $dewiki = [
222 'iw_prefix' => 'de',
223 'iw_url' => 'http://de.wikipedia.org/wiki/',
224 'iw_local' => 1
227 $zzwiki = [
228 'iw_prefix' => 'zz',
229 'iw_url' => 'http://zzwiki.org/wiki/',
230 'iw_local' => 0
233 $cdbData = $this->populateHash(
234 'en',
235 [ $dewiki ],
236 [ $zzwiki ]
239 $this->setWgInterwikiCache( $cdbData );
241 $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
242 $this->assertEquals(
243 [ $dewiki, $zzwiki ],
244 $interwikiLookup->getAllPrefixes(),
245 'getAllPrefixes()'
248 $this->assertTrue( $interwikiLookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
249 $this->assertTrue( $interwikiLookup->isValidInterwiki( 'zz' ), 'known prefix is valid' );
251 $interwiki = $interwikiLookup->fetch( 'de' );
252 $this->assertInstanceOf( 'Interwiki', $interwiki );
254 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
255 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
257 $interwiki = $interwikiLookup->fetch( 'zz' );
258 $this->assertInstanceOf( 'Interwiki', $interwiki );
260 $this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
261 $this->assertSame( false, $interwiki->isLocal(), 'isLocal' );