MediaWiki
REL1_24
|
00001 <?php 00002 00003 class GlobalVarConfigTest extends MediaWikiTestCase { 00004 00008 public function testNewInstance() { 00009 $config = GlobalVarConfig::newInstance(); 00010 $this->assertInstanceOf( 'GlobalVarConfig', $config ); 00011 $this->maybeStashGlobal( 'wgBaz' ); 00012 $GLOBALS['wgBaz'] = 'somevalue'; 00013 // Check prefix is set to 'wg' 00014 $this->assertEquals( 'somevalue', $config->get( 'Baz' ) ); 00015 } 00016 00021 public function testConstructor( $prefix ) { 00022 $var = $prefix . 'GlobalVarConfigTest'; 00023 $rand = wfRandomString(); 00024 $this->maybeStashGlobal( $var ); 00025 $GLOBALS[$var] = $rand; 00026 $config = new GlobalVarConfig( $prefix ); 00027 $this->assertInstanceOf( 'GlobalVarConfig', $config ); 00028 $this->assertEquals( $rand, $config->get( 'GlobalVarConfigTest' ) ); 00029 } 00030 00031 public static function provideConstructor() { 00032 return array( 00033 array( 'wg' ), 00034 array( 'ef' ), 00035 array( 'smw' ), 00036 array( 'blahblahblahblah' ), 00037 array( '' ), 00038 ); 00039 } 00040 00044 public function testHas() { 00045 $this->maybeStashGlobal( 'wgGlobalVarConfigTestHas' ); 00046 $GLOBALS['wgGlobalVarConfigTestHas'] = wfRandomString(); 00047 $this->maybeStashGlobal( 'wgGlobalVarConfigTestNotHas' ); 00048 $config = new GlobalVarConfig(); 00049 $this->assertTrue( $config->has( 'GlobalVarConfigTestHas' ) ); 00050 $this->assertFalse( $config->has( 'GlobalVarConfigTestNotHas' ) ); 00051 } 00052 00053 public static function provideGet() { 00054 $set = array( 00055 'wgSomething' => 'default1', 00056 'wgFoo' => 'default2', 00057 'efVariable' => 'default3', 00058 'BAR' => 'default4', 00059 ); 00060 00061 foreach ( $set as $var => $value ) { 00062 $GLOBALS[$var] = $value; 00063 } 00064 00065 return array( 00066 array( 'Something', 'wg', 'default1' ), 00067 array( 'Foo', 'wg', 'default2' ), 00068 array( 'Variable', 'ef', 'default3' ), 00069 array( 'BAR', '', 'default4' ), 00070 array( 'ThisGlobalWasNotSetAbove', 'wg', false ) 00071 ); 00072 } 00073 00082 public function testGet( $name, $prefix, $expected ) { 00083 $config = new GlobalVarConfig( $prefix ); 00084 if ( $expected === false ) { 00085 $this->setExpectedException( 'ConfigException', 'GlobalVarConfig::get: undefined option:' ); 00086 } 00087 $this->assertEquals( $config->get( $name ), $expected ); 00088 } 00089 00090 public static function provideSet() { 00091 return array( 00092 array( 'Foo', 'wg', 'wgFoo' ), 00093 array( 'SomethingRandom', 'wg', 'wgSomethingRandom' ), 00094 array( 'FromAnExtension', 'eg', 'egFromAnExtension' ), 00095 array( 'NoPrefixHere', '', 'NoPrefixHere' ), 00096 ); 00097 } 00098 00099 private function maybeStashGlobal( $var ) { 00100 if ( array_key_exists( $var, $GLOBALS ) ) { 00101 // Will be reset after this test is over 00102 $this->stashMwGlobals( $var ); 00103 } 00104 } 00105 00111 public function testSet( $name, $prefix, $var ) { 00112 $this->hideDeprecated( 'GlobalVarConfig::set' ); 00113 $this->maybeStashGlobal( $var ); 00114 $config = new GlobalVarConfig( $prefix ); 00115 $random = wfRandomString(); 00116 $config->set( $name, $random ); 00117 $this->assertArrayHasKey( $var, $GLOBALS ); 00118 $this->assertEquals( $random, $GLOBALS[$var] ); 00119 } 00120 }