Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / SiteConfigurationTest.php
blobbdd4b1e53b4dd0bf1cd40c78bc94fdfbab0424cc
1 <?php
3 class SiteConfigurationTest extends MediaWikiTestCase {
5 /**
6 * @var SiteConfiguration
7 */
8 protected $mConf;
10 protected function setUp() {
11 parent::setUp();
13 $this->mConf = new SiteConfiguration;
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',
26 'fallback' => [
27 'default' => 'default',
28 'wiki' => 'wiki',
29 'tag' => 'tag',
32 'params' => [
33 'default' => '$lang $site $wiki',
36 '+global' => [
37 'wiki' => [
38 'wiki' => 'wiki',
40 'tag' => [
41 'tag' => 'tag',
43 'enwiki' => [
44 'enwiki' => 'enwiki',
46 'dewiki' => [
47 'dewiki' => 'dewiki',
49 'frwiki' => [
50 'frwiki' => 'frwiki',
54 'merge' => [
55 '+wiki' => [
56 'wiki' => 'wiki',
58 '+tag' => [
59 'tag' => 'tag',
61 'default' => [
62 'default' => 'default',
64 '+enwiki' => [
65 'enwiki' => 'enwiki',
67 '+dewiki' => [
68 'dewiki' => 'dewiki',
70 '+frwiki' => [
71 'frwiki' => 'frwiki',
76 $GLOBALS['global'] = [ 'global' => 'global' ];
79 /**
80 * This function is used as a callback within the tests below
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;
93 return [
94 'suffix' => $site,
95 'lang' => $lang,
96 'params' => [
97 'lang' => $lang,
98 'site' => $site,
99 'wiki' => $wiki,
101 'tags' => [ 'tag' ],
106 * @covers SiteConfiguration::siteFromDB
108 public function testSiteFromDb() {
109 $this->assertEquals(
110 [ 'wikipedia', 'en' ],
111 $this->mConf->siteFromDB( 'enwiki' ),
112 'siteFromDB()'
114 $this->assertEquals(
115 [ 'wikipedia', '' ],
116 $this->mConf->siteFromDB( 'wiki' ),
117 'siteFromDB() on a suffix'
119 $this->assertEquals(
120 [ null, null ],
121 $this->mConf->siteFromDB( 'wikien' ),
122 'siteFromDB() on a non-existing wiki'
125 $this->mConf->suffixes = [ 'wiki', '' ];
126 $this->assertEquals(
127 [ '', 'wikien' ],
128 $this->mConf->siteFromDB( 'wikien' ),
129 'siteFromDB() on a non-existing wiki (2)'
134 * @covers SiteConfiguration::getLocalDatabases
136 public function testGetLocalDatabases() {
137 $this->assertEquals(
138 [ 'enwiki', 'dewiki', 'frwiki' ],
139 $this->mConf->getLocalDatabases(),
140 'getLocalDatabases()'
145 * @covers SiteConfiguration::get
147 public function testGetConfVariables() {
148 $this->assertEquals(
149 'enwiki',
150 $this->mConf->get( 'simple', 'enwiki', 'wiki' ),
151 'get(): simple setting on an existing wiki'
153 $this->assertEquals(
154 'dewiki',
155 $this->mConf->get( 'simple', 'dewiki', 'wiki' ),
156 'get(): simple setting on an existing wiki (2)'
158 $this->assertEquals(
159 'frwiki',
160 $this->mConf->get( 'simple', 'frwiki', 'wiki' ),
161 'get(): simple setting on an existing wiki (3)'
163 $this->assertEquals(
164 'wiki',
165 $this->mConf->get( 'simple', 'wiki', 'wiki' ),
166 'get(): simple setting on an suffix'
168 $this->assertEquals(
169 'wiki',
170 $this->mConf->get( 'simple', 'eswiki', 'wiki' ),
171 'get(): simple setting on an non-existing wiki'
174 $this->assertEquals(
175 'wiki',
176 $this->mConf->get( 'fallback', 'enwiki', 'wiki' ),
177 'get(): fallback setting on an existing wiki'
179 $this->assertEquals(
180 'tag',
181 $this->mConf->get( 'fallback', 'dewiki', 'wiki', [], [ 'tag' ] ),
182 'get(): fallback setting on an existing wiki (with wiki tag)'
184 $this->assertEquals(
185 'wiki',
186 $this->mConf->get( 'fallback', 'wiki', 'wiki' ),
187 'get(): fallback setting on an suffix'
189 $this->assertEquals(
190 'wiki',
191 $this->mConf->get( 'fallback', 'wiki', 'wiki', [], [ 'tag' ] ),
192 'get(): fallback setting on an suffix (with wiki tag)'
194 $this->assertEquals(
195 'wiki',
196 $this->mConf->get( 'fallback', 'eswiki', 'wiki' ),
197 'get(): fallback setting on an non-existing wiki'
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)'
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'
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)'
217 $this->assertEquals(
218 [ 'dewiki' => 'dewiki' ] + $common,
219 $this->mConf->get( 'merge', 'dewiki', 'wiki' ),
220 'get(): merging setting on an existing wiki (2)'
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)'
227 $this->assertEquals(
228 [ 'frwiki' => 'frwiki' ] + $common,
229 $this->mConf->get( 'merge', 'frwiki', 'wiki' ),
230 'get(): merging setting on an existing wiki (3)'
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)'
237 $this->assertEquals(
238 [ 'wiki' => 'wiki' ] + $common,
239 $this->mConf->get( 'merge', 'wiki', 'wiki' ),
240 'get(): merging setting on an suffix'
242 $this->assertEquals(
243 [ 'wiki' => 'wiki' ] + $commonTag,
244 $this->mConf->get( 'merge', 'wiki', 'wiki', [], [ 'tag' ] ),
245 'get(): merging setting on an suffix (with tag)'
247 $this->assertEquals(
248 $common,
249 $this->mConf->get( 'merge', 'eswiki', 'wiki' ),
250 'get(): merging setting on an non-existing wiki'
252 $this->assertEquals(
253 $commonTag,
254 $this->mConf->get( 'merge', 'eswiki', 'wiki', [], [ 'tag' ] ),
255 'get(): merging setting on an non-existing wiki (with tag)'
260 * @covers SiteConfiguration::siteFromDB
262 public function testSiteFromDbWithCallback() {
263 $this->mConf->siteParamsCallback = 'SiteConfigurationTest::getSiteParamsCallback';
265 $this->assertEquals(
266 [ 'wiki', 'en' ],
267 $this->mConf->siteFromDB( 'enwiki' ),
268 'siteFromDB() with callback'
270 $this->assertEquals(
271 [ 'wiki', '' ],
272 $this->mConf->siteFromDB( 'wiki' ),
273 'siteFromDB() with callback on a suffix'
275 $this->assertEquals(
276 [ null, null ],
277 $this->mConf->siteFromDB( 'wikien' ),
278 'siteFromDB() with callback on a non-existing wiki'
283 * @covers SiteConfiguration::get
285 public function testParameterReplacement() {
286 $this->mConf->siteParamsCallback = 'SiteConfigurationTest::getSiteParamsCallback';
288 $this->assertEquals(
289 'en wiki enwiki',
290 $this->mConf->get( 'params', 'enwiki', 'wiki' ),
291 'get(): parameter replacement on an existing wiki'
293 $this->assertEquals(
294 'de wiki dewiki',
295 $this->mConf->get( 'params', 'dewiki', 'wiki' ),
296 'get(): parameter replacement on an existing wiki (2)'
298 $this->assertEquals(
299 'fr wiki frwiki',
300 $this->mConf->get( 'params', 'frwiki', 'wiki' ),
301 'get(): parameter replacement on an existing wiki (3)'
303 $this->assertEquals(
304 ' wiki wiki',
305 $this->mConf->get( 'params', 'wiki', 'wiki' ),
306 'get(): parameter replacement on an suffix'
308 $this->assertEquals(
309 'es wiki eswiki',
310 $this->mConf->get( 'params', 'eswiki', 'wiki' ),
311 'get(): parameter replacement on an non-existing wiki'
316 * @covers SiteConfiguration::getAll
318 public function testGetAllGlobals() {
319 $this->mConf->siteParamsCallback = 'SiteConfigurationTest::getSiteParamsCallback';
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'
333 $this->assertEquals( $getall, $this->mConf->getAll( 'enwiki' ), 'getAll()' );
335 $this->mConf->extractAllGlobals( 'enwiki', 'wiki' );
337 $this->assertEquals(
338 $getall['simple'],
339 $GLOBALS['simple'],
340 'extractAllGlobals(): simple setting'
342 $this->assertEquals(
343 $getall['fallback'],
344 $GLOBALS['fallback'],
345 'extractAllGlobals(): fallback setting'
347 $this->assertEquals(
348 $getall['params'],
349 $GLOBALS['params'],
350 'extractAllGlobals(): parameter replacement'
352 $this->assertEquals(
353 $getall['global'],
354 $GLOBALS['global'],
355 'extractAllGlobals(): merging with global'
357 $this->assertEquals(
358 $getall['merge'],
359 $GLOBALS['merge'],
360 'extractAllGlobals(): merging setting'