MediaWiki REL1_32
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 // NOTE: database setup is expensive, so we only do
60 // it once and run all the tests in one go.
61 $dewiki = [
62 'iw_prefix' => 'de',
63 'iw_url' => 'http://de.wikipedia.org/wiki/',
64 'iw_api' => 'http://de.wikipedia.org/w/api.php',
65 'iw_wikiid' => 'dewiki',
66 'iw_local' => 1,
67 'iw_trans' => 0
68 ];
69
70 $zzwiki = [
71 'iw_prefix' => 'zz',
72 'iw_url' => 'http://zzwiki.org/wiki/',
73 'iw_api' => 'http://zzwiki.org/w/api.php',
74 'iw_wikiid' => 'zzwiki',
75 'iw_local' => 0,
76 'iw_trans' => 0
77 ];
78
79 $this->populateDB( [ $dewiki, $zzwiki ] );
80
81 $this->setWgInterwikiCache( false );
82
83 $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
84 $this->assertEquals(
85 [ $dewiki, $zzwiki ],
86 $interwikiLookup->getAllPrefixes(),
87 'getAllPrefixes()'
88 );
89 $this->assertEquals(
90 [ $dewiki ],
91 $interwikiLookup->getAllPrefixes( true ),
92 'getAllPrefixes()'
93 );
94 $this->assertEquals(
95 [ $zzwiki ],
96 $interwikiLookup->getAllPrefixes( false ),
97 'getAllPrefixes()'
98 );
99
100 $this->assertTrue( $interwikiLookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
101 $this->assertFalse( $interwikiLookup->isValidInterwiki( 'xyz' ), 'unknown prefix is valid' );
102
103 $this->assertNull( $interwikiLookup->fetch( null ), 'no prefix' );
104 $this->assertFalse( $interwikiLookup->fetch( 'xyz' ), 'unknown prefix' );
105
106 $interwiki = $interwikiLookup->fetch( 'de' );
107 $this->assertInstanceOf( Interwiki::class, $interwiki );
108 $this->assertSame( $interwiki, $interwikiLookup->fetch( 'de' ), 'in-process caching' );
109
110 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
111 $this->assertSame( 'http://de.wikipedia.org/w/api.php', $interwiki->getAPI(), 'getAPI' );
112 $this->assertSame( 'dewiki', $interwiki->getWikiID(), 'getWikiID' );
113 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
114 $this->assertSame( false, $interwiki->isTranscludable(), 'isTranscludable' );
115
116 $interwikiLookup->invalidateCache( 'de' );
117 $this->assertNotSame( $interwiki, $interwikiLookup->fetch( 'de' ), 'invalidate cache' );
118 }
119
120}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
setWgInterwikiCache( $interwikiCache)
populateDB( $iwrows)
Value object for representing interwiki records.
Definition Interwiki.php:27
overrideMwServices(Config $configOverrides=null, array $services=[])
Stashes the global instance of MediaWikiServices, and installs a new one, allowing test cases to over...
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
MediaWikiServices is the service locator for the application scope of MediaWiki.
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:37
const DB_MASTER
Definition defines.php:26