MediaWiki
REL1_22
|
00001 <?php 00002 00003 function getSiteParams( $conf, $wiki ) { 00004 $site = null; 00005 $lang = null; 00006 foreach ( $conf->suffixes as $suffix ) { 00007 if ( substr( $wiki, -strlen( $suffix ) ) == $suffix ) { 00008 $site = $suffix; 00009 $lang = substr( $wiki, 0, -strlen( $suffix ) ); 00010 break; 00011 } 00012 } 00013 00014 return array( 00015 'suffix' => $site, 00016 'lang' => $lang, 00017 'params' => array( 00018 'lang' => $lang, 00019 'site' => $site, 00020 'wiki' => $wiki, 00021 ), 00022 'tags' => array( 'tag' ), 00023 ); 00024 } 00025 00026 class SiteConfigurationTest extends MediaWikiTestCase { 00027 00031 protected $mConf; 00032 00033 protected function setUp() { 00034 parent::setUp(); 00035 00036 $this->mConf = new SiteConfiguration; 00037 00038 $this->mConf->suffixes = array( 'wikipedia' => 'wiki' ); 00039 $this->mConf->wikis = array( 'enwiki', 'dewiki', 'frwiki' ); 00040 $this->mConf->settings = array( 00041 'simple' => array( 00042 'wiki' => 'wiki', 00043 'tag' => 'tag', 00044 'enwiki' => 'enwiki', 00045 'dewiki' => 'dewiki', 00046 'frwiki' => 'frwiki', 00047 ), 00048 00049 'fallback' => array( 00050 'default' => 'default', 00051 'wiki' => 'wiki', 00052 'tag' => 'tag', 00053 ), 00054 00055 'params' => array( 00056 'default' => '$lang $site $wiki', 00057 ), 00058 00059 '+global' => array( 00060 'wiki' => array( 00061 'wiki' => 'wiki', 00062 ), 00063 'tag' => array( 00064 'tag' => 'tag', 00065 ), 00066 'enwiki' => array( 00067 'enwiki' => 'enwiki', 00068 ), 00069 'dewiki' => array( 00070 'dewiki' => 'dewiki', 00071 ), 00072 'frwiki' => array( 00073 'frwiki' => 'frwiki', 00074 ), 00075 ), 00076 00077 'merge' => array( 00078 '+wiki' => array( 00079 'wiki' => 'wiki', 00080 ), 00081 '+tag' => array( 00082 'tag' => 'tag', 00083 ), 00084 'default' => array( 00085 'default' => 'default', 00086 ), 00087 '+enwiki' => array( 00088 'enwiki' => 'enwiki', 00089 ), 00090 '+dewiki' => array( 00091 'dewiki' => 'dewiki', 00092 ), 00093 '+frwiki' => array( 00094 'frwiki' => 'frwiki', 00095 ), 00096 ), 00097 ); 00098 00099 $GLOBALS['global'] = array( 'global' => 'global' ); 00100 } 00101 00105 public function testSiteFromDb() { 00106 $this->assertEquals( 00107 array( 'wikipedia', 'en' ), 00108 $this->mConf->siteFromDB( 'enwiki' ), 00109 'siteFromDB()' 00110 ); 00111 $this->assertEquals( 00112 array( 'wikipedia', '' ), 00113 $this->mConf->siteFromDB( 'wiki' ), 00114 'siteFromDB() on a suffix' 00115 ); 00116 $this->assertEquals( 00117 array( null, null ), 00118 $this->mConf->siteFromDB( 'wikien' ), 00119 'siteFromDB() on a non-existing wiki' 00120 ); 00121 00122 $this->mConf->suffixes = array( 'wiki', '' ); 00123 $this->assertEquals( 00124 array( '', 'wikien' ), 00125 $this->mConf->siteFromDB( 'wikien' ), 00126 'siteFromDB() on a non-existing wiki (2)' 00127 ); 00128 } 00129 00133 public function testGetLocalDatabases() { 00134 $this->assertEquals( 00135 array( 'enwiki', 'dewiki', 'frwiki' ), 00136 $this->mConf->getLocalDatabases(), 00137 'getLocalDatabases()' 00138 ); 00139 } 00140 00144 public function testGetConfVariables() { 00145 $this->assertEquals( 00146 'enwiki', 00147 $this->mConf->get( 'simple', 'enwiki', 'wiki' ), 00148 'get(): simple setting on an existing wiki' 00149 ); 00150 $this->assertEquals( 00151 'dewiki', 00152 $this->mConf->get( 'simple', 'dewiki', 'wiki' ), 00153 'get(): simple setting on an existing wiki (2)' 00154 ); 00155 $this->assertEquals( 00156 'frwiki', 00157 $this->mConf->get( 'simple', 'frwiki', 'wiki' ), 00158 'get(): simple setting on an existing wiki (3)' 00159 ); 00160 $this->assertEquals( 00161 'wiki', 00162 $this->mConf->get( 'simple', 'wiki', 'wiki' ), 00163 'get(): simple setting on an suffix' 00164 ); 00165 $this->assertEquals( 00166 'wiki', 00167 $this->mConf->get( 'simple', 'eswiki', 'wiki' ), 00168 'get(): simple setting on an non-existing wiki' 00169 ); 00170 00171 $this->assertEquals( 00172 'wiki', 00173 $this->mConf->get( 'fallback', 'enwiki', 'wiki' ), 00174 'get(): fallback setting on an existing wiki' 00175 ); 00176 $this->assertEquals( 00177 'tag', 00178 $this->mConf->get( 'fallback', 'dewiki', 'wiki', array(), array( 'tag' ) ), 00179 'get(): fallback setting on an existing wiki (with wiki tag)' 00180 ); 00181 $this->assertEquals( 00182 'wiki', 00183 $this->mConf->get( 'fallback', 'wiki', 'wiki' ), 00184 'get(): fallback setting on an suffix' 00185 ); 00186 $this->assertEquals( 00187 'wiki', 00188 $this->mConf->get( 'fallback', 'wiki', 'wiki', array(), array( 'tag' ) ), 00189 'get(): fallback setting on an suffix (with wiki tag)' 00190 ); 00191 $this->assertEquals( 00192 'wiki', 00193 $this->mConf->get( 'fallback', 'eswiki', 'wiki' ), 00194 'get(): fallback setting on an non-existing wiki' 00195 ); 00196 $this->assertEquals( 00197 'tag', 00198 $this->mConf->get( 'fallback', 'eswiki', 'wiki', array(), array( 'tag' ) ), 00199 'get(): fallback setting on an non-existing wiki (with wiki tag)' 00200 ); 00201 00202 $common = array( 'wiki' => 'wiki', 'default' => 'default' ); 00203 $commonTag = array( 'tag' => 'tag', 'wiki' => 'wiki', 'default' => 'default' ); 00204 $this->assertEquals( 00205 array( 'enwiki' => 'enwiki' ) + $common, 00206 $this->mConf->get( 'merge', 'enwiki', 'wiki' ), 00207 'get(): merging setting on an existing wiki' 00208 ); 00209 $this->assertEquals( 00210 array( 'enwiki' => 'enwiki' ) + $commonTag, 00211 $this->mConf->get( 'merge', 'enwiki', 'wiki', array(), array( 'tag' ) ), 00212 'get(): merging setting on an existing wiki (with tag)' 00213 ); 00214 $this->assertEquals( 00215 array( 'dewiki' => 'dewiki' ) + $common, 00216 $this->mConf->get( 'merge', 'dewiki', 'wiki' ), 00217 'get(): merging setting on an existing wiki (2)' 00218 ); 00219 $this->assertEquals( 00220 array( 'dewiki' => 'dewiki' ) + $commonTag, 00221 $this->mConf->get( 'merge', 'dewiki', 'wiki', array(), array( 'tag' ) ), 00222 'get(): merging setting on an existing wiki (2) (with tag)' 00223 ); 00224 $this->assertEquals( 00225 array( 'frwiki' => 'frwiki' ) + $common, 00226 $this->mConf->get( 'merge', 'frwiki', 'wiki' ), 00227 'get(): merging setting on an existing wiki (3)' 00228 ); 00229 $this->assertEquals( 00230 array( 'frwiki' => 'frwiki' ) + $commonTag, 00231 $this->mConf->get( 'merge', 'frwiki', 'wiki', array(), array( 'tag' ) ), 00232 'get(): merging setting on an existing wiki (3) (with tag)' 00233 ); 00234 $this->assertEquals( 00235 array( 'wiki' => 'wiki' ) + $common, 00236 $this->mConf->get( 'merge', 'wiki', 'wiki' ), 00237 'get(): merging setting on an suffix' 00238 ); 00239 $this->assertEquals( 00240 array( 'wiki' => 'wiki' ) + $commonTag, 00241 $this->mConf->get( 'merge', 'wiki', 'wiki', array(), array( 'tag' ) ), 00242 'get(): merging setting on an suffix (with tag)' 00243 ); 00244 $this->assertEquals( 00245 $common, 00246 $this->mConf->get( 'merge', 'eswiki', 'wiki' ), 00247 'get(): merging setting on an non-existing wiki' 00248 ); 00249 $this->assertEquals( 00250 $commonTag, 00251 $this->mConf->get( 'merge', 'eswiki', 'wiki', array(), array( 'tag' ) ), 00252 'get(): merging setting on an non-existing wiki (with tag)' 00253 ); 00254 } 00255 00259 public function testSiteFromDbWithCallback() { 00260 $this->mConf->siteParamsCallback = 'getSiteParams'; 00261 00262 $this->assertEquals( 00263 array( 'wiki', 'en' ), 00264 $this->mConf->siteFromDB( 'enwiki' ), 00265 'siteFromDB() with callback' 00266 ); 00267 $this->assertEquals( 00268 array( 'wiki', '' ), 00269 $this->mConf->siteFromDB( 'wiki' ), 00270 'siteFromDB() with callback on a suffix' 00271 ); 00272 $this->assertEquals( 00273 array( null, null ), 00274 $this->mConf->siteFromDB( 'wikien' ), 00275 'siteFromDB() with callback on a non-existing wiki' 00276 ); 00277 } 00278 00282 public function testParameterReplacement() { 00283 $this->mConf->siteParamsCallback = 'getSiteParams'; 00284 00285 $this->assertEquals( 00286 'en wiki enwiki', 00287 $this->mConf->get( 'params', 'enwiki', 'wiki' ), 00288 'get(): parameter replacement on an existing wiki' 00289 ); 00290 $this->assertEquals( 00291 'de wiki dewiki', 00292 $this->mConf->get( 'params', 'dewiki', 'wiki' ), 00293 'get(): parameter replacement on an existing wiki (2)' 00294 ); 00295 $this->assertEquals( 00296 'fr wiki frwiki', 00297 $this->mConf->get( 'params', 'frwiki', 'wiki' ), 00298 'get(): parameter replacement on an existing wiki (3)' 00299 ); 00300 $this->assertEquals( 00301 ' wiki wiki', 00302 $this->mConf->get( 'params', 'wiki', 'wiki' ), 00303 'get(): parameter replacement on an suffix' 00304 ); 00305 $this->assertEquals( 00306 'es wiki eswiki', 00307 $this->mConf->get( 'params', 'eswiki', 'wiki' ), 00308 'get(): parameter replacement on an non-existing wiki' 00309 ); 00310 } 00311 00315 public function testGetAllGlobals() { 00316 $this->mConf->siteParamsCallback = 'getSiteParams'; 00317 00318 $getall = array( 00319 'simple' => 'enwiki', 00320 'fallback' => 'tag', 00321 'params' => 'en wiki enwiki', 00322 'global' => array( 'enwiki' => 'enwiki' ) + $GLOBALS['global'], 00323 'merge' => array( 'enwiki' => 'enwiki', 'tag' => 'tag', 'wiki' => 'wiki', 'default' => 'default' ), 00324 ); 00325 $this->assertEquals( $getall, $this->mConf->getAll( 'enwiki' ), 'getAll()' ); 00326 00327 $this->mConf->extractAllGlobals( 'enwiki', 'wiki' ); 00328 00329 $this->assertEquals( $getall['simple'], $GLOBALS['simple'], 'extractAllGlobals(): simple setting' ); 00330 $this->assertEquals( $getall['fallback'], $GLOBALS['fallback'], 'extractAllGlobals(): fallback setting' ); 00331 $this->assertEquals( $getall['params'], $GLOBALS['params'], 'extractAllGlobals(): parameter replacement' ); 00332 $this->assertEquals( $getall['global'], $GLOBALS['global'], 'extractAllGlobals(): merging with global' ); 00333 $this->assertEquals( $getall['merge'], $GLOBALS['merge'], 'extractAllGlobals(): merging setting' ); 00334 } 00335 }