MediaWiki  REL1_31
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  'simple' => [
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  ],
31 
32  'params' => [
33  'default' => '$lang $site $wiki',
34  ],
35 
36  '+global' => [
37  'wiki' => [
38  'wiki' => 'wiki',
39  ],
40  'tag' => [
41  'tag' => 'tag',
42  ],
43  'enwiki' => [
44  'enwiki' => 'enwiki',
45  ],
46  'dewiki' => [
47  'dewiki' => 'dewiki',
48  ],
49  'frwiki' => [
50  'frwiki' => 'frwiki',
51  ],
52  ],
53 
54  'merge' => [
55  '+wiki' => [
56  'wiki' => 'wiki',
57  ],
58  '+tag' => [
59  'tag' => 'tag',
60  ],
61  'default' => [
62  'default' => 'default',
63  ],
64  '+enwiki' => [
65  'enwiki' => 'enwiki',
66  ],
67  '+dewiki' => [
68  'dewiki' => 'dewiki',
69  ],
70  '+frwiki' => [
71  'frwiki' => 'frwiki',
72  ],
73  ],
74  ];
75 
76  $GLOBALS['global'] = [ 'global' => 'global' ];
77  }
78 
82  public static function getSiteParamsCallback( $conf, $wiki ) {
83  $site = null;
84  $lang = null;
85  foreach ( $conf->suffixes as $suffix ) {
86  if ( substr( $wiki, -strlen( $suffix ) ) == $suffix ) {
87  $site = $suffix;
88  $lang = substr( $wiki, 0, -strlen( $suffix ) );
89  break;
90  }
91  }
92 
93  return [
94  'suffix' => $site,
95  'lang' => $lang,
96  'params' => [
97  'lang' => $lang,
98  'site' => $site,
99  'wiki' => $wiki,
100  ],
101  'tags' => [ 'tag' ],
102  ];
103  }
104 
108  public function testSiteFromDb() {
109  $this->assertEquals(
110  [ 'wikipedia', 'en' ],
111  $this->mConf->siteFromDB( 'enwiki' ),
112  'siteFromDB()'
113  );
114  $this->assertEquals(
115  [ 'wikipedia', '' ],
116  $this->mConf->siteFromDB( 'wiki' ),
117  'siteFromDB() on a suffix'
118  );
119  $this->assertEquals(
120  [ null, null ],
121  $this->mConf->siteFromDB( 'wikien' ),
122  'siteFromDB() on a non-existing wiki'
123  );
124 
125  $this->mConf->suffixes = [ 'wiki', '' ];
126  $this->assertEquals(
127  [ '', 'wikien' ],
128  $this->mConf->siteFromDB( 'wikien' ),
129  'siteFromDB() on a non-existing wiki (2)'
130  );
131  }
132 
136  public function testGetLocalDatabases() {
137  $this->assertEquals(
138  [ 'enwiki', 'dewiki', 'frwiki' ],
139  $this->mConf->getLocalDatabases(),
140  'getLocalDatabases()'
141  );
142  }
143 
147  public function testGetConfVariables() {
148  $this->assertEquals(
149  'enwiki',
150  $this->mConf->get( 'simple', 'enwiki', 'wiki' ),
151  'get(): simple setting on an existing wiki'
152  );
153  $this->assertEquals(
154  'dewiki',
155  $this->mConf->get( 'simple', 'dewiki', 'wiki' ),
156  'get(): simple setting on an existing wiki (2)'
157  );
158  $this->assertEquals(
159  'frwiki',
160  $this->mConf->get( 'simple', 'frwiki', 'wiki' ),
161  'get(): simple setting on an existing wiki (3)'
162  );
163  $this->assertEquals(
164  'wiki',
165  $this->mConf->get( 'simple', 'wiki', 'wiki' ),
166  'get(): simple setting on an suffix'
167  );
168  $this->assertEquals(
169  'wiki',
170  $this->mConf->get( 'simple', 'eswiki', 'wiki' ),
171  'get(): simple setting on an non-existing wiki'
172  );
173 
174  $this->assertEquals(
175  'wiki',
176  $this->mConf->get( 'fallback', 'enwiki', 'wiki' ),
177  'get(): fallback setting on an existing wiki'
178  );
179  $this->assertEquals(
180  'tag',
181  $this->mConf->get( 'fallback', 'dewiki', 'wiki', [], [ 'tag' ] ),
182  'get(): fallback setting on an existing wiki (with wiki tag)'
183  );
184  $this->assertEquals(
185  'wiki',
186  $this->mConf->get( 'fallback', 'wiki', 'wiki' ),
187  'get(): fallback setting on an suffix'
188  );
189  $this->assertEquals(
190  'wiki',
191  $this->mConf->get( 'fallback', 'wiki', 'wiki', [], [ 'tag' ] ),
192  'get(): fallback setting on an suffix (with wiki tag)'
193  );
194  $this->assertEquals(
195  'wiki',
196  $this->mConf->get( 'fallback', 'eswiki', 'wiki' ),
197  'get(): fallback setting on an non-existing wiki'
198  );
199  $this->assertEquals(
200  'tag',
201  $this->mConf->get( 'fallback', 'eswiki', 'wiki', [], [ 'tag' ] ),
202  'get(): fallback setting on an non-existing wiki (with wiki tag)'
203  );
204 
205  $common = [ 'wiki' => 'wiki', 'default' => 'default' ];
206  $commonTag = [ 'tag' => 'tag', 'wiki' => 'wiki', 'default' => 'default' ];
207  $this->assertEquals(
208  [ 'enwiki' => 'enwiki' ] + $common,
209  $this->mConf->get( 'merge', 'enwiki', 'wiki' ),
210  'get(): merging setting on an existing wiki'
211  );
212  $this->assertEquals(
213  [ 'enwiki' => 'enwiki' ] + $commonTag,
214  $this->mConf->get( 'merge', 'enwiki', 'wiki', [], [ 'tag' ] ),
215  'get(): merging setting on an existing wiki (with tag)'
216  );
217  $this->assertEquals(
218  [ 'dewiki' => 'dewiki' ] + $common,
219  $this->mConf->get( 'merge', 'dewiki', 'wiki' ),
220  'get(): merging setting on an existing wiki (2)'
221  );
222  $this->assertEquals(
223  [ 'dewiki' => 'dewiki' ] + $commonTag,
224  $this->mConf->get( 'merge', 'dewiki', 'wiki', [], [ 'tag' ] ),
225  'get(): merging setting on an existing wiki (2) (with tag)'
226  );
227  $this->assertEquals(
228  [ 'frwiki' => 'frwiki' ] + $common,
229  $this->mConf->get( 'merge', 'frwiki', 'wiki' ),
230  'get(): merging setting on an existing wiki (3)'
231  );
232  $this->assertEquals(
233  [ 'frwiki' => 'frwiki' ] + $commonTag,
234  $this->mConf->get( 'merge', 'frwiki', 'wiki', [], [ 'tag' ] ),
235  'get(): merging setting on an existing wiki (3) (with tag)'
236  );
237  $this->assertEquals(
238  [ 'wiki' => 'wiki' ] + $common,
239  $this->mConf->get( 'merge', 'wiki', 'wiki' ),
240  'get(): merging setting on an suffix'
241  );
242  $this->assertEquals(
243  [ 'wiki' => 'wiki' ] + $commonTag,
244  $this->mConf->get( 'merge', 'wiki', 'wiki', [], [ 'tag' ] ),
245  'get(): merging setting on an suffix (with tag)'
246  );
247  $this->assertEquals(
248  $common,
249  $this->mConf->get( 'merge', 'eswiki', 'wiki' ),
250  'get(): merging setting on an non-existing wiki'
251  );
252  $this->assertEquals(
253  $commonTag,
254  $this->mConf->get( 'merge', 'eswiki', 'wiki', [], [ 'tag' ] ),
255  'get(): merging setting on an non-existing wiki (with tag)'
256  );
257  }
258 
262  public function testSiteFromDbWithCallback() {
263  $this->mConf->siteParamsCallback = 'SiteConfigurationTest::getSiteParamsCallback';
264 
265  $this->assertEquals(
266  [ 'wiki', 'en' ],
267  $this->mConf->siteFromDB( 'enwiki' ),
268  'siteFromDB() with callback'
269  );
270  $this->assertEquals(
271  [ 'wiki', '' ],
272  $this->mConf->siteFromDB( 'wiki' ),
273  'siteFromDB() with callback on a suffix'
274  );
275  $this->assertEquals(
276  [ null, null ],
277  $this->mConf->siteFromDB( 'wikien' ),
278  'siteFromDB() with callback on a non-existing wiki'
279  );
280  }
281 
285  public function testParameterReplacement() {
286  $this->mConf->siteParamsCallback = 'SiteConfigurationTest::getSiteParamsCallback';
287 
288  $this->assertEquals(
289  'en wiki enwiki',
290  $this->mConf->get( 'params', 'enwiki', 'wiki' ),
291  'get(): parameter replacement on an existing wiki'
292  );
293  $this->assertEquals(
294  'de wiki dewiki',
295  $this->mConf->get( 'params', 'dewiki', 'wiki' ),
296  'get(): parameter replacement on an existing wiki (2)'
297  );
298  $this->assertEquals(
299  'fr wiki frwiki',
300  $this->mConf->get( 'params', 'frwiki', 'wiki' ),
301  'get(): parameter replacement on an existing wiki (3)'
302  );
303  $this->assertEquals(
304  ' wiki wiki',
305  $this->mConf->get( 'params', 'wiki', 'wiki' ),
306  'get(): parameter replacement on an suffix'
307  );
308  $this->assertEquals(
309  'es wiki eswiki',
310  $this->mConf->get( 'params', 'eswiki', 'wiki' ),
311  'get(): parameter replacement on an non-existing wiki'
312  );
313  }
314 
318  public function testGetAllGlobals() {
319  $this->mConf->siteParamsCallback = 'SiteConfigurationTest::getSiteParamsCallback';
320 
321  $getall = [
322  'simple' => 'enwiki',
323  'fallback' => 'tag',
324  'params' => 'en wiki enwiki',
325  'global' => [ 'enwiki' => 'enwiki' ] + $GLOBALS['global'],
326  'merge' => [
327  'enwiki' => 'enwiki',
328  'tag' => 'tag',
329  'wiki' => 'wiki',
330  'default' => 'default'
331  ],
332  ];
333  $this->assertEquals( $getall, $this->mConf->getAll( 'enwiki' ), 'getAll()' );
334 
335  $this->mConf->extractAllGlobals( 'enwiki', 'wiki' );
336 
337  $this->assertEquals(
338  $getall['simple'],
339  $GLOBALS['simple'],
340  'extractAllGlobals(): simple setting'
341  );
342  $this->assertEquals(
343  $getall['fallback'],
344  $GLOBALS['fallback'],
345  'extractAllGlobals(): fallback setting'
346  );
347  $this->assertEquals(
348  $getall['params'],
349  $GLOBALS['params'],
350  'extractAllGlobals(): parameter replacement'
351  );
352  $this->assertEquals(
353  $getall['global'],
354  $GLOBALS['global'],
355  'extractAllGlobals(): merging with global'
356  );
357  $this->assertEquals(
358  $getall['merge'],
359  $GLOBALS['merge'],
360  'extractAllGlobals(): merging setting'
361  );
362  }
363 }
SiteConfigurationTest\setUp
setUp()
Definition: SiteConfigurationTest.php:10
SiteConfigurationTest\testGetLocalDatabases
testGetLocalDatabases()
SiteConfiguration::getLocalDatabases.
Definition: SiteConfigurationTest.php:136
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:318
SiteConfigurationTest\$mConf
SiteConfiguration $mConf
Definition: SiteConfigurationTest.php:8
SiteConfigurationTest\testSiteFromDb
testSiteFromDb()
SiteConfiguration::siteFromDB.
Definition: SiteConfigurationTest.php:108
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:37
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
SiteConfigurationTest
Definition: SiteConfigurationTest.php:3
SiteConfigurationTest\testGetConfVariables
testGetConfVariables()
SiteConfiguration::get.
Definition: SiteConfigurationTest.php:147
SiteConfigurationTest\testSiteFromDbWithCallback
testSiteFromDbWithCallback()
SiteConfiguration::siteFromDB.
Definition: SiteConfigurationTest.php:262
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:22
SiteConfigurationTest\testParameterReplacement
testParameterReplacement()
SiteConfiguration::get.
Definition: SiteConfigurationTest.php:285
$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:82