| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Config\ConfigException; |
| 4 |
use MediaWiki\Config\GlobalVarConfig; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
class GlobalVarConfigTest extends MediaWikiIntegrationTestCase { |
| 10 |
|
| 11 |
public function testNewInstance() { |
| 12 |
$config = GlobalVarConfig::newInstance(); |
| 13 |
$this->assertInstanceOf( GlobalVarConfig::class, $config ); |
| 14 |
$this->setMwGlobals( 'wgBaz', 'somevalue' ); |
| 15 |
|
| 16 |
$this->assertEquals( 'somevalue', $config->get( 'Baz' ) ); |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public function testConstructor( $prefix ) { |
| 23 |
$var = $prefix . 'GlobalVarConfigTest'; |
| 24 |
$this->setMwGlobals( $var, 'testvalue' ); |
| 25 |
$config = new GlobalVarConfig( $prefix ); |
| 26 |
$this->assertInstanceOf( GlobalVarConfig::class, $config ); |
| 27 |
$this->assertEquals( 'testvalue', $config->get( 'GlobalVarConfigTest' ) ); |
| 28 |
} |
| 29 |
|
| 30 |
public static function provideConstructor() { |
| 31 |
return [ |
| 32 |
[ 'wg' ], |
| 33 |
[ 'ef' ], |
| 34 |
[ 'smw' ], |
| 35 |
[ 'blahblahblahblah' ], |
| 36 |
[ '' ], |
| 37 |
]; |
| 38 |
} |
| 39 |
|
| 40 |
public function testHas() { |
| 41 |
$this->setMwGlobals( 'wgGlobalVarConfigTestHas', 'testvalue' ); |
| 42 |
$config = new GlobalVarConfig(); |
| 43 |
$this->assertTrue( $config->has( 'GlobalVarConfigTestHas' ) ); |
| 44 |
$this->assertFalse( $config->has( 'GlobalVarConfigTestNotHas' ) ); |
| 45 |
} |
| 46 |
|
| 47 |
public static function provideGet() { |
| 48 |
$set = [ |
| 49 |
'wgSomething' => 'default1', |
| 50 |
'wgFoo' => 'default2', |
| 51 |
'efVariable' => 'default3', |
| 52 |
'BAR' => 'default4', |
| 53 |
]; |
| 54 |
|
| 55 |
foreach ( $set as $var => $value ) { |
| 56 |
$GLOBALS[$var] = $value; |
| 57 |
} |
| 58 |
|
| 59 |
return [ |
| 60 |
[ 'Something', 'wg', 'default1' ], |
| 61 |
[ 'Foo', 'wg', 'default2' ], |
| 62 |
[ 'Variable', 'ef', 'default3' ], |
| 63 |
[ 'BAR', '', 'default4' ], |
| 64 |
[ 'ThisGlobalWasNotSetAbove', 'wg', false ] |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function testGet( $name, $prefix, $expected ) { |
| 75 |
$config = new GlobalVarConfig( $prefix ); |
| 76 |
if ( $expected === false ) { |
| 77 |
$this->expectException( ConfigException::class ); |
| 78 |
$this->expectExceptionMessage( 'GlobalVarConfig::get: undefined option:' ); |
| 79 |
} |
| 80 |
$this->assertEquals( $expected, $config->get( $name ) ); |
| 81 |
} |
| 82 |
} |