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