Index: trunk/phase3/tests/SiteConfigurationTest.php |
— | — | @@ -0,0 +1,311 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +function getSiteParams( $conf, $wiki ) { |
| 5 | + $site = null; |
| 6 | + $lang = null; |
| 7 | + foreach( $conf->suffixes as $suffix ) { |
| 8 | + if ( substr( $wiki, -strlen( $suffix ) ) == $suffix ) { |
| 9 | + $site = $suffix; |
| 10 | + $lang = substr( $wiki, 0, -strlen( $suffix ) ); |
| 11 | + break; |
| 12 | + } |
| 13 | + } |
| 14 | + return array( |
| 15 | + 'suffix' => $site, |
| 16 | + 'lang' => $lang, |
| 17 | + 'params' => array( |
| 18 | + 'lang' => $lang, |
| 19 | + 'site' => $site, |
| 20 | + 'wiki' => $wiki, |
| 21 | + ), |
| 22 | + 'tags' => array( 'tag' ), |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +class SiteConfigurationTest extends PHPUnit_Framework_TestCase { |
| 27 | + var $mConf; |
| 28 | + |
| 29 | + function setUp() { |
| 30 | + $this->mConf = new SiteConfiguration; |
| 31 | + |
| 32 | + $this->mConf->suffixes = array( 'wiki' ); |
| 33 | + $this->mConf->wikis = array( 'enwiki', 'dewiki', 'frwiki' ); |
| 34 | + $this->mConf->settings = array( |
| 35 | + 'simple' => array( |
| 36 | + 'wiki' => 'wiki', |
| 37 | + 'tag' => 'tag', |
| 38 | + 'enwiki' => 'enwiki', |
| 39 | + 'dewiki' => 'dewiki', |
| 40 | + 'frwiki' => 'frwiki', |
| 41 | + ), |
| 42 | + |
| 43 | + 'fallback' => array( |
| 44 | + 'default' => 'default', |
| 45 | + 'wiki' => 'wiki', |
| 46 | + 'tag' => 'tag', |
| 47 | + ), |
| 48 | + |
| 49 | + 'params' => array( |
| 50 | + 'default' => '$lang $site $wiki', |
| 51 | + ), |
| 52 | + |
| 53 | + '+global' => array( |
| 54 | + 'wiki' => array( |
| 55 | + 'wiki' => 'wiki', |
| 56 | + ), |
| 57 | + 'tag' => array( |
| 58 | + 'tag' => 'tag', |
| 59 | + ), |
| 60 | + 'enwiki' => array( |
| 61 | + 'enwiki' => 'enwiki', |
| 62 | + ), |
| 63 | + 'dewiki' => array( |
| 64 | + 'dewiki' => 'dewiki', |
| 65 | + ), |
| 66 | + 'frwiki' => array( |
| 67 | + 'frwiki' => 'frwiki', |
| 68 | + ), |
| 69 | + ), |
| 70 | + |
| 71 | + 'merge' => array( |
| 72 | + '+wiki' => array( |
| 73 | + 'wiki' => 'wiki', |
| 74 | + ), |
| 75 | + '+tag' => array( |
| 76 | + 'tag' => 'tag', |
| 77 | + ), |
| 78 | + 'default' => array( |
| 79 | + 'default' => 'default', |
| 80 | + ), |
| 81 | + '+enwiki' => array( |
| 82 | + 'enwiki' => 'enwiki', |
| 83 | + ), |
| 84 | + '+dewiki' => array( |
| 85 | + 'dewiki' => 'dewiki', |
| 86 | + ), |
| 87 | + '+frwiki' => array( |
| 88 | + 'frwiki' => 'frwiki', |
| 89 | + ), |
| 90 | + ), |
| 91 | + ); |
| 92 | + |
| 93 | + $GLOBALS['global'] = array( 'global' => 'global' ); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + function testSiteFromDB() { |
| 98 | + $this->assertEquals( |
| 99 | + array( 'wikipedia', 'en' ), |
| 100 | + $this->mConf->siteFromDB( 'enwiki' ), |
| 101 | + 'siteFromDB()' |
| 102 | + ); |
| 103 | + $this->assertEquals( |
| 104 | + array( 'wikipedia', '' ), |
| 105 | + $this->mConf->siteFromDB( 'wiki' ), |
| 106 | + 'siteFromDB() on a suffix' |
| 107 | + ); |
| 108 | + $this->assertEquals( |
| 109 | + array( null, null ), |
| 110 | + $this->mConf->siteFromDB( 'wikien' ), |
| 111 | + 'siteFromDB() on a non-existing wiki' |
| 112 | + ); |
| 113 | + |
| 114 | + $this->mConf->suffixes = array( 'wiki', '' ); |
| 115 | + $this->assertEquals( |
| 116 | + array( '', 'wikien' ), |
| 117 | + $this->mConf->siteFromDB( 'wikien' ), |
| 118 | + 'siteFromDB() on a non-existing wiki (2)' |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + function testGetLocalDatabases() { |
| 123 | + $this->assertEquals( |
| 124 | + array( 'enwiki', 'dewiki', 'frwiki' ), |
| 125 | + $this->mConf->getLocalDatabases(), |
| 126 | + 'getLocalDatabases()' |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + function testGet() { |
| 131 | + $this->assertEquals( |
| 132 | + 'enwiki', |
| 133 | + $this->mConf->get( 'simple', 'enwiki', 'wiki' ), |
| 134 | + 'get(): simple setting on an existing wiki' |
| 135 | + ); |
| 136 | + $this->assertEquals( |
| 137 | + 'dewiki', |
| 138 | + $this->mConf->get( 'simple', 'dewiki', 'wiki' ), |
| 139 | + 'get(): simple setting on an existing wiki (2)' |
| 140 | + ); |
| 141 | + $this->assertEquals( |
| 142 | + 'frwiki', |
| 143 | + $this->mConf->get( 'simple', 'frwiki', 'wiki' ), |
| 144 | + 'get(): simple setting on an existing wiki (3)' |
| 145 | + ); |
| 146 | + $this->assertEquals( |
| 147 | + 'wiki', |
| 148 | + $this->mConf->get( 'simple', 'wiki', 'wiki' ), |
| 149 | + 'get(): simple setting on an suffix' |
| 150 | + ); |
| 151 | + $this->assertEquals( |
| 152 | + 'wiki', |
| 153 | + $this->mConf->get( 'simple', 'eswiki', 'wiki' ), |
| 154 | + 'get(): simple setting on an non-existing wiki' |
| 155 | + ); |
| 156 | + |
| 157 | + $this->assertEquals( |
| 158 | + 'wiki', |
| 159 | + $this->mConf->get( 'fallback', 'enwiki', 'wiki' ), |
| 160 | + 'get(): fallback setting on an existing wiki' |
| 161 | + ); |
| 162 | + $this->assertEquals( |
| 163 | + 'tag', |
| 164 | + $this->mConf->get( 'fallback', 'dewiki', 'wiki', array(), array( 'tag' ) ), |
| 165 | + 'get(): fallback setting on an existing wiki (with wiki tag)' |
| 166 | + ); |
| 167 | + $this->assertEquals( |
| 168 | + 'wiki', |
| 169 | + $this->mConf->get( 'fallback', 'wiki', 'wiki' ), |
| 170 | + 'get(): fallback setting on an suffix' |
| 171 | + ); |
| 172 | + $this->assertEquals( |
| 173 | + 'wiki', |
| 174 | + $this->mConf->get( 'fallback', 'wiki', 'wiki', array(), array( 'tag' ) ), |
| 175 | + 'get(): fallback setting on an suffix (with wiki tag)' |
| 176 | + ); |
| 177 | + $this->assertEquals( |
| 178 | + 'wiki', |
| 179 | + $this->mConf->get( 'fallback', 'eswiki', 'wiki' ), |
| 180 | + 'get(): fallback setting on an non-existing wiki' |
| 181 | + ); |
| 182 | + $this->assertEquals( |
| 183 | + 'tag', |
| 184 | + $this->mConf->get( 'fallback', 'eswiki', 'wiki', array(), array( 'tag' ) ), |
| 185 | + 'get(): fallback setting on an non-existing wiki (with wiki tag)' |
| 186 | + ); |
| 187 | + |
| 188 | + $common = array( 'wiki' => 'wiki', 'default' => 'default' ); |
| 189 | + $commonTag = array( 'tag' => 'tag', 'wiki' => 'wiki', 'default' => 'default' ); |
| 190 | + $this->assertEquals( |
| 191 | + array( 'enwiki' => 'enwiki' ) + $common, |
| 192 | + $this->mConf->get( 'merge', 'enwiki', 'wiki' ), |
| 193 | + 'get(): merging setting on an existing wiki' |
| 194 | + ); |
| 195 | + $this->assertEquals( |
| 196 | + array( 'enwiki' => 'enwiki' ) + $commonTag, |
| 197 | + $this->mConf->get( 'merge', 'enwiki', 'wiki', array(), array( 'tag' ) ), |
| 198 | + 'get(): merging setting on an existing wiki (with tag)' |
| 199 | + ); |
| 200 | + $this->assertEquals( |
| 201 | + array( 'dewiki' => 'dewiki' ) + $common, |
| 202 | + $this->mConf->get( 'merge', 'dewiki', 'wiki' ), |
| 203 | + 'get(): merging setting on an existing wiki (2)' |
| 204 | + ); |
| 205 | + $this->assertEquals( |
| 206 | + array( 'dewiki' => 'dewiki' ) + $commonTag, |
| 207 | + $this->mConf->get( 'merge', 'dewiki', 'wiki', array(), array( 'tag' ) ), |
| 208 | + 'get(): merging setting on an existing wiki (2) (with tag)' |
| 209 | + ); |
| 210 | + $this->assertEquals( |
| 211 | + array( 'frwiki' => 'frwiki' ) + $common, |
| 212 | + $this->mConf->get( 'merge', 'frwiki', 'wiki' ), |
| 213 | + 'get(): merging setting on an existing wiki (3)' |
| 214 | + ); |
| 215 | + $this->assertEquals( |
| 216 | + array( 'frwiki' => 'frwiki' ) + $commonTag, |
| 217 | + $this->mConf->get( 'merge', 'frwiki', 'wiki', array(), array( 'tag' ) ), |
| 218 | + 'get(): merging setting on an existing wiki (3) (with tag)' |
| 219 | + ); |
| 220 | + $this->assertEquals( |
| 221 | + array( 'wiki' => 'wiki' ) + $common, |
| 222 | + $this->mConf->get( 'merge', 'wiki', 'wiki' ), |
| 223 | + 'get(): merging setting on an suffix' |
| 224 | + ); |
| 225 | + $this->assertEquals( |
| 226 | + array( 'wiki' => 'wiki' ) + $commonTag, |
| 227 | + $this->mConf->get( 'merge', 'wiki', 'wiki', array(), array( 'tag' ) ), |
| 228 | + 'get(): merging setting on an suffix (with tag)' |
| 229 | + ); |
| 230 | + $this->assertEquals( |
| 231 | + $common, |
| 232 | + $this->mConf->get( 'merge', 'eswiki', 'wiki' ), |
| 233 | + 'get(): merging setting on an non-existing wiki' |
| 234 | + ); |
| 235 | + $this->assertEquals( |
| 236 | + $commonTag, |
| 237 | + $this->mConf->get( 'merge', 'eswiki', 'wiki', array(), array( 'tag' ) ), |
| 238 | + 'get(): merging setting on an non-existing wiki (with tag)' |
| 239 | + ); |
| 240 | + } |
| 241 | + |
| 242 | + function testSiteFromDBWithCallback() { |
| 243 | + $this->mConf->siteParamsCallback = 'getSiteParams'; |
| 244 | + |
| 245 | + $this->assertEquals( |
| 246 | + array( 'wiki', 'en' ), |
| 247 | + $this->mConf->siteFromDB( 'enwiki' ), |
| 248 | + 'siteFromDB() with callback' |
| 249 | + ); |
| 250 | + $this->assertEquals( |
| 251 | + array( 'wiki', '' ), |
| 252 | + $this->mConf->siteFromDB( 'wiki' ), |
| 253 | + 'siteFromDB() with callback on a suffix' |
| 254 | + ); |
| 255 | + $this->assertEquals( |
| 256 | + array( null, null ), |
| 257 | + $this->mConf->siteFromDB( 'wikien' ), |
| 258 | + 'siteFromDB() with callback on a non-existing wiki' |
| 259 | + ); |
| 260 | + } |
| 261 | + |
| 262 | + function testParamReplacement() { |
| 263 | + $this->mConf->siteParamsCallback = 'getSiteParams'; |
| 264 | + |
| 265 | + $this->assertEquals( |
| 266 | + 'en wiki enwiki', |
| 267 | + $this->mConf->get( 'params', 'enwiki', 'wiki' ), |
| 268 | + 'get(): parameter replacement on an existing wiki' |
| 269 | + ); |
| 270 | + $this->assertEquals( |
| 271 | + 'de wiki dewiki', |
| 272 | + $this->mConf->get( 'params', 'dewiki', 'wiki' ), |
| 273 | + 'get(): parameter replacement on an existing wiki (2)' |
| 274 | + ); |
| 275 | + $this->assertEquals( |
| 276 | + 'fr wiki frwiki', |
| 277 | + $this->mConf->get( 'params', 'frwiki', 'wiki' ), |
| 278 | + 'get(): parameter replacement on an existing wiki (3)' |
| 279 | + ); |
| 280 | + $this->assertEquals( |
| 281 | + ' wiki wiki', |
| 282 | + $this->mConf->get( 'params', 'wiki', 'wiki' ), |
| 283 | + 'get(): parameter replacement on an suffix' |
| 284 | + ); |
| 285 | + $this->assertEquals( |
| 286 | + 'es wiki eswiki', |
| 287 | + $this->mConf->get( 'params', 'eswiki', 'wiki' ), |
| 288 | + 'get(): parameter replacement on an non-existing wiki' |
| 289 | + ); |
| 290 | + } |
| 291 | + |
| 292 | + function testGetAll() { |
| 293 | + $this->mConf->siteParamsCallback = 'getSiteParams'; |
| 294 | + |
| 295 | + $getall = array( |
| 296 | + 'simple' => 'enwiki', |
| 297 | + 'fallback' => 'tag', |
| 298 | + 'params' => 'en wiki enwiki', |
| 299 | + 'global' => array( 'enwiki' => 'enwiki' ) + $GLOBALS['global'], |
| 300 | + 'merge' => array( 'enwiki' => 'enwiki', 'tag' => 'tag', 'wiki' => 'wiki', 'default' => 'default' ), |
| 301 | + ); |
| 302 | + $this->assertEquals( $getall, $this->mConf->getAll( 'enwiki' ), 'getAll()' ); |
| 303 | + |
| 304 | + $this->mConf->extractAllGlobals( 'enwiki', 'wiki' ); |
| 305 | + |
| 306 | + $this->assertEquals( $getall['simple'], $GLOBALS['simple'], 'extractAllGlobals(): simple setting' ); |
| 307 | + $this->assertEquals( $getall['fallback'], $GLOBALS['fallback'], 'extractAllGlobals(): fallback setting' ); |
| 308 | + $this->assertEquals( $getall['params'], $GLOBALS['params'], 'extractAllGlobals(): parameter replacement' ); |
| 309 | + $this->assertEquals( $getall['global'], $GLOBALS['global'], 'extractAllGlobals(): merging with global' ); |
| 310 | + $this->assertEquals( $getall['merge'], $GLOBALS['merge'], 'extractAllGlobals(): merging setting' ); |
| 311 | + } |
| 312 | +} |
Property changes on: trunk/phase3/tests/SiteConfigurationTest.php |
___________________________________________________________________ |
Name: svn:eol-style |
1 | 313 | + native |