3 class GlobalVarConfigTest
extends MediaWikiTestCase
{
6 * @covers GlobalVarConfig::newInstance
8 public function testNewInstance() {
9 $config = GlobalVarConfig
::newInstance();
10 $this->assertInstanceOf( 'GlobalVarConfig', $config );
11 $this->maybeStashGlobal( 'wgBaz' );
12 $GLOBALS['wgBaz'] = 'somevalue';
13 // Check prefix is set to 'wg'
14 $this->assertEquals( 'somevalue', $config->get( 'Baz' ) );
18 * @covers GlobalVarConfig::__construct
19 * @dataProvider provideConstructor
21 public function testConstructor( $prefix ) {
22 $var = $prefix . 'GlobalVarConfigTest';
23 $rand = wfRandomString();
24 $this->maybeStashGlobal( $var );
25 $GLOBALS[$var] = $rand;
26 $config = new GlobalVarConfig( $prefix );
27 $this->assertInstanceOf( 'GlobalVarConfig', $config );
28 $this->assertEquals( $rand, $config->get( 'GlobalVarConfigTest' ) );
31 public static function provideConstructor() {
36 array( 'blahblahblahblah' ),
42 * @covers GlobalVarConfig::has
44 public function testHas() {
45 $this->maybeStashGlobal( 'wgGlobalVarConfigTestHas' );
46 $GLOBALS['wgGlobalVarConfigTestHas'] = wfRandomString();
47 $this->maybeStashGlobal( 'wgGlobalVarConfigTestNotHas' );
48 $config = new GlobalVarConfig();
49 $this->assertTrue( $config->has( 'GlobalVarConfigTestHas' ) );
50 $this->assertFalse( $config->has( 'GlobalVarConfigTestNotHas' ) );
53 public static function provideGet() {
55 'wgSomething' => 'default1',
56 'wgFoo' => 'default2',
57 'efVariable' => 'default3',
61 foreach ( $set as $var => $value ) {
62 $GLOBALS[$var] = $value;
66 array( 'Something', 'wg', 'default1' ),
67 array( 'Foo', 'wg', 'default2' ),
68 array( 'Variable', 'ef', 'default3' ),
69 array( 'BAR', '', 'default4' ),
70 array( 'ThisGlobalWasNotSetAbove', 'wg', false )
76 * @param string $prefix
77 * @param string $expected
78 * @dataProvider provideGet
79 * @covers GlobalVarConfig::get
80 * @covers GlobalVarConfig::getWithPrefix
82 public function testGet( $name, $prefix, $expected ) {
83 $config = new GlobalVarConfig( $prefix );
84 if ( $expected === false ) {
85 $this->setExpectedException( 'ConfigException', 'GlobalVarConfig::get: undefined option:' );
87 $this->assertEquals( $config->get( $name ), $expected );
90 public static function provideSet() {
92 array( 'Foo', 'wg', 'wgFoo' ),
93 array( 'SomethingRandom', 'wg', 'wgSomethingRandom' ),
94 array( 'FromAnExtension', 'eg', 'egFromAnExtension' ),
95 array( 'NoPrefixHere', '', 'NoPrefixHere' ),
99 private function maybeStashGlobal( $var ) {
100 if ( array_key_exists( $var, $GLOBALS ) ) {
101 // Will be reset after this test is over
102 $this->stashMwGlobals( $var );
107 * @dataProvider provideSet
108 * @covers GlobalVarConfig::set
109 * @covers GlobalVarConfig::setWithPrefix
111 public function testSet( $name, $prefix, $var ) {
112 $this->hideDeprecated( 'GlobalVarConfig::set' );
113 $this->maybeStashGlobal( $var );
114 $config = new GlobalVarConfig( $prefix );
115 $random = wfRandomString();
116 $config->set( $name, $random );
117 $this->assertArrayHasKey( $var, $GLOBALS );
118 $this->assertEquals( $random, $GLOBALS[$var] );