TitleTest: Break secure and split test into two tests with providers
[mediawiki.git] / tests / phpunit / includes / config / GlobalVarConfigTest.php
bloba99908155a157af0a769450f40e36d6b144a879e
1 <?php
3 class GlobalVarConfigTest extends MediaWikiTestCase {
5 /**
6 * @covers GlobalVarConfig::newInstance
7 */
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' ) );
17 /**
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() {
32 return array(
33 array( 'wg' ),
34 array( 'ef' ),
35 array( 'smw' ),
36 array( 'blahblahblahblah' ),
37 array( '' ),
41 public function provideGet() {
42 $set = array(
43 'wgSomething' => 'default1',
44 'wgFoo' => 'default2',
45 'efVariable' => 'default3',
46 'BAR' => 'default4',
49 foreach ( $set as $var => $value ) {
50 $GLOBALS[$var] = $value;
53 return array(
54 array( 'Something', 'wg', 'default1' ),
55 array( 'Foo', 'wg', 'default2' ),
56 array( 'Variable', 'ef', 'default3' ),
57 array( 'BAR', '', 'default4' ),
58 array( 'ThisGlobalWasNotSetAbove', 'wg', false )
62 /**
63 * @param string $name
64 * @param string $prefix
65 * @param string $expected
66 * @dataProvider provideGet
67 * @covers GlobalVarConfig::get
68 * @covers GlobalVarConfig::getWithPrefix
70 public function testGet( $name, $prefix, $expected ) {
71 $config = new GlobalVarConfig( $prefix );
72 if ( $expected === false ) {
73 $this->setExpectedException( 'ConfigException', 'GlobalVarConfig::getWithPrefix: undefined variable:' );
75 $this->assertEquals( $config->get( $name ), $expected );
78 public static function provideSet() {
79 return array(
80 array( 'Foo', 'wg', 'wgFoo' ),
81 array( 'SomethingRandom', 'wg', 'wgSomethingRandom' ),
82 array( 'FromAnExtension', 'eg', 'egFromAnExtension' ),
83 array( 'NoPrefixHere', '', 'NoPrefixHere' ),
87 private function maybeStashGlobal( $var ) {
88 if ( array_key_exists( $var, $GLOBALS ) ) {
89 // Will be reset after this test is over
90 $this->stashMwGlobals( $var );
94 /**
95 * @dataProvider provideSet
96 * @covers GlobalVarConfig::set
97 * @covers GlobalVarConfig::setWithPrefix
99 public function testSet( $name, $prefix, $var ) {
100 $this->maybeStashGlobal( $var );
101 $config = new GlobalVarConfig( $prefix );
102 $random = wfRandomString();
103 $config->set( $name, $random );
104 $this->assertArrayHasKey( $var, $GLOBALS );
105 $this->assertEquals( $random, $GLOBALS[$var] );