MediaWiki  1.33.1
SiteConfigurationTest.php
Go to the documentation of this file.
1 <?php
2 
4 
8  protected $mConf;
9 
10  protected function setUp() {
11  parent::setUp();
12 
13  $this->mConf = new SiteConfiguration;
14 
15  $this->mConf->suffixes = [ 'wikipedia' => 'wiki' ];
16  $this->mConf->wikis = [ 'enwiki', 'dewiki', 'frwiki' ];
17  $this->mConf->settings = [
18  'SimpleKey' => [
19  'wiki' => 'wiki',
20  'tag' => 'tag',
21  'enwiki' => 'enwiki',
22  'dewiki' => 'dewiki',
23  'frwiki' => 'frwiki',
24  ],
25 
26  'Fallback' => [
27  'default' => 'default',
28  'wiki' => 'wiki',
29  'tag' => 'tag',
30  'frwiki' => 'frwiki',
31  'null_wiki' => null,
32  ],
33 
34  'WithParams' => [
35  'default' => '$lang $site $wiki',
36  ],
37 
38  '+SomeGlobal' => [
39  'wiki' => [
40  'wiki' => 'wiki',
41  ],
42  'tag' => [
43  'tag' => 'tag',
44  ],
45  'enwiki' => [
46  'enwiki' => 'enwiki',
47  ],
48  'dewiki' => [
49  'dewiki' => 'dewiki',
50  ],
51  'frwiki' => [
52  'frwiki' => 'frwiki',
53  ],
54  ],
55 
56  'MergeIt' => [
57  '+wiki' => [
58  'wiki' => 'wiki',
59  ],
60  '+tag' => [
61  'tag' => 'tag',
62  ],
63  'default' => [
64  'default' => 'default',
65  ],
66  '+enwiki' => [
67  'enwiki' => 'enwiki',
68  ],
69  '+dewiki' => [
70  'dewiki' => 'dewiki',
71  ],
72  '+frwiki' => [
73  'frwiki' => 'frwiki',
74  ],
75  ],
76  ];
77 
78  $GLOBALS['SomeGlobal'] = [ 'SomeGlobal' => 'SomeGlobal' ];
79  }
80 
84  public static function getSiteParamsCallback( $conf, $wiki ) {
85  $site = null;
86  $lang = null;
87  foreach ( $conf->suffixes as $suffix ) {
88  if ( substr( $wiki, -strlen( $suffix ) ) == $suffix ) {
89  $site = $suffix;
90  $lang = substr( $wiki, 0, -strlen( $suffix ) );
91  break;
92  }
93  }
94 
95  return [
96  'suffix' => $site,
97  'lang' => $lang,
98  'params' => [
99  'lang' => $lang,
100  'site' => $site,
101  'wiki' => $wiki,
102  ],
103  'tags' => [ 'tag' ],
104  ];
105  }
106 
110  public function testSiteFromDb() {
111  $this->assertEquals(
112  [ 'wikipedia', 'en' ],
113  $this->mConf->siteFromDB( 'enwiki' ),
114  'siteFromDB()'
115  );
116  $this->assertEquals(
117  [ 'wikipedia', '' ],
118  $this->mConf->siteFromDB( 'wiki' ),
119  'siteFromDB() on a suffix'
120  );
121  $this->assertEquals(
122  [ null, null ],
123  $this->mConf->siteFromDB( 'wikien' ),
124  'siteFromDB() on a non-existing wiki'
125  );
126 
127  $this->mConf->suffixes = [ 'wiki', '' ];
128  $this->assertEquals(
129  [ '', 'wikien' ],
130  $this->mConf->siteFromDB( 'wikien' ),
131  'siteFromDB() on a non-existing wiki (2)'
132  );
133  }
134 
138  public function testGetLocalDatabases() {
139  $this->assertEquals(
140  [ 'enwiki', 'dewiki', 'frwiki' ],
141  $this->mConf->getLocalDatabases(),
142  'getLocalDatabases()'
143  );
144  }
145 
149  public function testGetConfVariables() {
150  // Simple
151  $this->assertEquals(
152  'enwiki',
153  $this->mConf->get( 'SimpleKey', 'enwiki', 'wiki' ),
154  'get(): simple setting on an existing wiki'
155  );
156  $this->assertEquals(
157  'dewiki',
158  $this->mConf->get( 'SimpleKey', 'dewiki', 'wiki' ),
159  'get(): simple setting on an existing wiki (2)'
160  );
161  $this->assertEquals(
162  'frwiki',
163  $this->mConf->get( 'SimpleKey', 'frwiki', 'wiki' ),
164  'get(): simple setting on an existing wiki (3)'
165  );
166  $this->assertEquals(
167  'wiki',
168  $this->mConf->get( 'SimpleKey', 'wiki', 'wiki' ),
169  'get(): simple setting on an suffix'
170  );
171  $this->assertEquals(
172  'wiki',
173  $this->mConf->get( 'SimpleKey', 'eswiki', 'wiki' ),
174  'get(): simple setting on an non-existing wiki'
175  );
176 
177  // Fallback
178  $this->assertEquals(
179  'wiki',
180  $this->mConf->get( 'Fallback', 'enwiki', 'wiki' ),
181  'get(): fallback setting on an existing wiki'
182  );
183  $this->assertEquals(
184  'tag',
185  $this->mConf->get( 'Fallback', 'dewiki', 'wiki', [], [ 'tag' ] ),
186  'get(): fallback setting on an existing wiki (with wiki tag)'
187  );
188  $this->assertEquals(
189  'frwiki',
190  $this->mConf->get( 'Fallback', 'frwiki', 'wiki', [], [ 'tag' ] ),
191  'get(): no fallback if wiki has its own setting (matching tag)'
192  );
193  $this->assertSame(
194  // Potential regression test for T192855
195  null,
196  $this->mConf->get( 'Fallback', 'null_wiki', 'wiki', [], [ 'tag' ] ),
197  'get(): no fallback if wiki has its own setting (matching tag and uses null)'
198  );
199  $this->assertEquals(
200  'wiki',
201  $this->mConf->get( 'Fallback', 'wiki', 'wiki' ),
202  'get(): fallback setting on an suffix'
203  );
204  $this->assertEquals(
205  'wiki',
206  $this->mConf->get( 'Fallback', 'wiki', 'wiki', [], [ 'tag' ] ),
207  'get(): fallback setting on an suffix (with wiki tag)'
208  );
209  $this->assertEquals(
210  'wiki',
211  $this->mConf->get( 'Fallback', 'eswiki', 'wiki' ),
212  'get(): fallback setting on an non-existing wiki'
213  );
214  $this->assertEquals(
215  'tag',
216  $this->mConf->get( 'Fallback', 'eswiki', 'wiki', [], [ 'tag' ] ),
217  'get(): fallback setting on an non-existing wiki (with wiki tag)'
218  );
219 
220  // Merging
221  $common = [ 'wiki' => 'wiki', 'default' => 'default' ];
222  $commonTag = [ 'tag' => 'tag', 'wiki' => 'wiki', 'default' => 'default' ];
223  $this->assertEquals(
224  [ 'enwiki' => 'enwiki' ] + $common,
225  $this->mConf->get( 'MergeIt', 'enwiki', 'wiki' ),
226  'get(): merging setting on an existing wiki'
227  );
228  $this->assertEquals(
229  [ 'enwiki' => 'enwiki' ] + $commonTag,
230  $this->mConf->get( 'MergeIt', 'enwiki', 'wiki', [], [ 'tag' ] ),
231  'get(): merging setting on an existing wiki (with tag)'
232  );
233  $this->assertEquals(
234  [ 'dewiki' => 'dewiki' ] + $common,
235  $this->mConf->get( 'MergeIt', 'dewiki', 'wiki' ),
236  'get(): merging setting on an existing wiki (2)'
237  );
238  $this->assertEquals(
239  [ 'dewiki' => 'dewiki' ] + $commonTag,
240  $this->mConf->get( 'MergeIt', 'dewiki', 'wiki', [], [ 'tag' ] ),
241  'get(): merging setting on an existing wiki (2) (with tag)'
242  );
243  $this->assertEquals(
244  [ 'frwiki' => 'frwiki' ] + $common,
245  $this->mConf->get( 'MergeIt', 'frwiki', 'wiki' ),
246  'get(): merging setting on an existing wiki (3)'
247  );
248  $this->assertEquals(
249  [ 'frwiki' => 'frwiki' ] + $commonTag,
250  $this->mConf->get( 'MergeIt', 'frwiki', 'wiki', [], [ 'tag' ] ),
251  'get(): merging setting on an existing wiki (3) (with tag)'
252  );
253  $this->assertEquals(
254  [ 'wiki' => 'wiki' ] + $common,
255  $this->mConf->get( 'MergeIt', 'wiki', 'wiki' ),
256  'get(): merging setting on an suffix'
257  );
258  $this->assertEquals(
259  [ 'wiki' => 'wiki' ] + $commonTag,
260  $this->mConf->get( 'MergeIt', 'wiki', 'wiki', [], [ 'tag' ] ),
261  'get(): merging setting on an suffix (with tag)'
262  );
263  $this->assertEquals(
264  $common,
265  $this->mConf->get( 'MergeIt', 'eswiki', 'wiki' ),
266  'get(): merging setting on an non-existing wiki'
267  );
268  $this->assertEquals(
269  $commonTag,
270  $this->mConf->get( 'MergeIt', 'eswiki', 'wiki', [], [ 'tag' ] ),
271  'get(): merging setting on an non-existing wiki (with tag)'
272  );
273  }
274 
278  public function testSiteFromDbWithCallback() {
279  $this->mConf->siteParamsCallback = 'SiteConfigurationTest::getSiteParamsCallback';
280 
281  $this->assertEquals(
282  [ 'wiki', 'en' ],
283  $this->mConf->siteFromDB( 'enwiki' ),
284  'siteFromDB() with callback'
285  );
286  $this->assertEquals(
287  [ 'wiki', '' ],
288  $this->mConf->siteFromDB( 'wiki' ),
289  'siteFromDB() with callback on a suffix'
290  );
291  $this->assertEquals(
292  [ null, null ],
293  $this->mConf->siteFromDB( 'wikien' ),
294  'siteFromDB() with callback on a non-existing wiki'
295  );
296  }
297 
301  public function testParameterReplacement() {
302  $this->mConf->siteParamsCallback = 'SiteConfigurationTest::getSiteParamsCallback';
303 
304  $this->assertEquals(
305  'en wiki enwiki',
306  $this->mConf->get( 'WithParams', 'enwiki', 'wiki' ),
307  'get(): parameter replacement on an existing wiki'
308  );
309  $this->assertEquals(
310  'de wiki dewiki',
311  $this->mConf->get( 'WithParams', 'dewiki', 'wiki' ),
312  'get(): parameter replacement on an existing wiki (2)'
313  );
314  $this->assertEquals(
315  'fr wiki frwiki',
316  $this->mConf->get( 'WithParams', 'frwiki', 'wiki' ),
317  'get(): parameter replacement on an existing wiki (3)'
318  );
319  $this->assertEquals(
320  ' wiki wiki',
321  $this->mConf->get( 'WithParams', 'wiki', 'wiki' ),
322  'get(): parameter replacement on an suffix'
323  );
324  $this->assertEquals(
325  'es wiki eswiki',
326  $this->mConf->get( 'WithParams', 'eswiki', 'wiki' ),
327  'get(): parameter replacement on an non-existing wiki'
328  );
329  }
330 
334  public function testGetAllGlobals() {
335  $this->mConf->siteParamsCallback = 'SiteConfigurationTest::getSiteParamsCallback';
336 
337  $getall = [
338  'SimpleKey' => 'enwiki',
339  'Fallback' => 'tag',
340  'WithParams' => 'en wiki enwiki',
341  'SomeGlobal' => [ 'enwiki' => 'enwiki' ] + $GLOBALS['SomeGlobal'],
342  'MergeIt' => [
343  'enwiki' => 'enwiki',
344  'tag' => 'tag',
345  'wiki' => 'wiki',
346  'default' => 'default'
347  ],
348  ];
349  $this->assertEquals( $getall, $this->mConf->getAll( 'enwiki' ), 'getAll()' );
350 
351  $this->mConf->extractAllGlobals( 'enwiki', 'wiki' );
352 
353  $this->assertEquals(
354  $getall['SimpleKey'],
355  $GLOBALS['SimpleKey'],
356  'extractAllGlobals(): simple setting'
357  );
358  $this->assertEquals(
359  $getall['Fallback'],
360  $GLOBALS['Fallback'],
361  'extractAllGlobals(): fallback setting'
362  );
363  $this->assertEquals(
364  $getall['WithParams'],
365  $GLOBALS['WithParams'],
366  'extractAllGlobals(): parameter replacement'
367  );
368  $this->assertEquals(
369  $getall['SomeGlobal'],
370  $GLOBALS['SomeGlobal'],
371  'extractAllGlobals(): merging with global'
372  );
373  $this->assertEquals(
374  $getall['MergeIt'],
375  $GLOBALS['MergeIt'],
376  'extractAllGlobals(): merging setting'
377  );
378  }
379 }
SiteConfigurationTest\setUp
setUp()
Definition: SiteConfigurationTest.php:10
SiteConfigurationTest\testGetLocalDatabases
testGetLocalDatabases()
SiteConfiguration::getLocalDatabases.
Definition: SiteConfigurationTest.php:138
SiteConfiguration
This is a class for holding configuration settings, particularly for multi-wiki sites.
Definition: SiteConfiguration.php:124
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
SiteConfigurationTest\testGetAllGlobals
testGetAllGlobals()
SiteConfiguration::getAll.
Definition: SiteConfigurationTest.php:334
SiteConfigurationTest\$mConf
SiteConfiguration $mConf
Definition: SiteConfigurationTest.php:8
SiteConfigurationTest\testSiteFromDb
testSiteFromDb()
SiteConfiguration::siteFromDB.
Definition: SiteConfigurationTest.php:110
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
Definition: MediaWikiTestCase.php:17
SiteConfigurationTest
Definition: SiteConfigurationTest.php:3
SiteConfigurationTest\testGetConfVariables
testGetConfVariables()
SiteConfiguration::get.
Definition: SiteConfigurationTest.php:149
SiteConfigurationTest\testSiteFromDbWithCallback
testSiteFromDbWithCallback()
SiteConfiguration::siteFromDB.
Definition: SiteConfigurationTest.php:278
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
SiteConfigurationTest\testParameterReplacement
testParameterReplacement()
SiteConfiguration::get.
Definition: SiteConfigurationTest.php:301
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6
SiteConfigurationTest\getSiteParamsCallback
static getSiteParamsCallback( $conf, $wiki)
This function is used as a callback within the tests below.
Definition: SiteConfigurationTest.php:84