MediaWiki  1.30.0
InterwikiTest.php
Go to the documentation of this file.
1 <?php
3 
11 
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',
18  1,
19  0
20  );
21 
22  $this->setContentLang( 'qqx' );
23 
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() );
31  }
32 
33  public function testGetUrl() {
34  $interwiki = new Interwiki(
35  'xyz',
36  'http://xyz.acme.test/wiki/$1'
37  );
38 
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' ) );
41  }
42 
44 
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';
50  }
51 
52  private function setWgInterwikiCache( $interwikiCache ) {
53  $this->overrideMwServices();
54  MediaWikiServices::getInstance()->resetServiceForTesting( 'InterwikiLookup' );
55  $this->setMwGlobals( 'wgInterwikiCache', $interwikiCache );
56  }
57 
58  public function testDatabaseStorage() {
59  $this->markTestSkipped( 'Needs I37b8e8018b3 <https://gerrit.wikimedia.org/r/#/c/270555/>' );
60 
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
70  ];
71 
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
79  ];
80 
81  $this->populateDB( [ $dewiki, $zzwiki ] );
82 
83  $this->setWgInterwikiCache( false );
84 
85  $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
86  $this->assertEquals(
87  [ $dewiki, $zzwiki ],
88  $interwikiLookup->getAllPrefixes(),
89  'getAllPrefixes()'
90  );
91  $this->assertEquals(
92  [ $dewiki ],
93  $interwikiLookup->getAllPrefixes( true ),
94  'getAllPrefixes()'
95  );
96  $this->assertEquals(
97  [ $zzwiki ],
98  $interwikiLookup->getAllPrefixes( false ),
99  'getAllPrefixes()'
100  );
101 
102  $this->assertTrue( $interwikiLookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
103  $this->assertFalse( $interwikiLookup->isValidInterwiki( 'xyz' ), 'unknown prefix is valid' );
104 
105  $this->assertNull( $interwikiLookup->fetch( null ), 'no prefix' );
106  $this->assertFalse( $interwikiLookup->fetch( 'xyz' ), 'unknown prefix' );
107 
108  $interwiki = $interwikiLookup->fetch( 'de' );
109  $this->assertInstanceOf( 'Interwiki', $interwiki );
110  $this->assertSame( $interwiki, $interwikiLookup->fetch( 'de' ), 'in-process caching' );
111 
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' );
117 
119  $this->assertNotSame( $interwiki, $interwikiLookup->fetch( 'de' ), 'invalidate cache' );
120  }
121 
122 }
Interwiki\invalidateCache
static invalidateCache( $prefix)
Purge the cache (local and persistent) for an interwiki prefix.
Definition: Interwiki.php:90
InterwikiTest\setWgInterwikiCache
setWgInterwikiCache( $interwikiCache)
Definition: InterwikiTest.php:52
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWikiTestCase\overrideMwServices
overrideMwServices(Config $configOverrides=null, array $services=[])
Stashes the global instance of MediaWikiServices, and installs a new one, allowing test cases to over...
Definition: MediaWikiTestCase.php:839
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2856
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:672
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
InterwikiTest\testConstructor
testConstructor()
Definition: InterwikiTest.php:12
DB_MASTER
const DB_MASTER
Definition: defines.php:26
MediaWikiTestCase\setContentLang
setContentLang( $lang)
Definition: MediaWikiTestCase.php:886
InterwikiTest\testDatabaseStorage
testDatabaseStorage()
Definition: InterwikiTest.php:58
InterwikiTest\testGetUrl
testGetUrl()
Definition: InterwikiTest.php:33
InterwikiTest\populateDB
populateDB( $iwrows)
Definition: InterwikiTest.php:45
Interwiki
Value object for representing interwiki records.
Definition: Interwiki.php:27
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
InterwikiTest
Interwiki.
Definition: InterwikiTest.php:10