Merge "Fix for Bug 63980 - Comparison of limits in pingLimiter is incorrect"
[lhc/web/wiklou.git] / tests / phpunit / includes / config / GlobalVarConfigTest.php
1 <?php
2
3 class GlobalVarConfigTest extends MediaWikiTestCase {
4
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' ) );
15 }
16
17 /**
18 * @covers GlobalVarConfig::__construct
19 * @dataProvider provideConstructor
20 */
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' ) );
29 }
30
31 public static function provideConstructor() {
32 return array(
33 array( 'wg' ),
34 array( 'ef' ),
35 array( 'smw' ),
36 array( 'blahblahblahblah' ),
37 array( '' ),
38 );
39 }
40
41 /**
42 * @covers GlobalVarConfig::has
43 */
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' ) );
51 }
52
53 public static function provideGet() {
54 $set = array(
55 'wgSomething' => 'default1',
56 'wgFoo' => 'default2',
57 'efVariable' => 'default3',
58 'BAR' => 'default4',
59 );
60
61 foreach ( $set as $var => $value ) {
62 $GLOBALS[$var] = $value;
63 }
64
65 return array(
66 array( 'Something', 'wg', 'default1' ),
67 array( 'Foo', 'wg', 'default2' ),
68 array( 'Variable', 'ef', 'default3' ),
69 array( 'BAR', '', 'default4' ),
70 array( 'ThisGlobalWasNotSetAbove', 'wg', false )
71 );
72 }
73
74 /**
75 * @param string $name
76 * @param string $prefix
77 * @param string $expected
78 * @dataProvider provideGet
79 * @covers GlobalVarConfig::get
80 * @covers GlobalVarConfig::getWithPrefix
81 */
82 public function testGet( $name, $prefix, $expected ) {
83 $config = new GlobalVarConfig( $prefix );
84 if ( $expected === false ) {
85 $this->setExpectedException( 'ConfigException', 'GlobalVarConfig::get: undefined option:' );
86 }
87 $this->assertEquals( $config->get( $name ), $expected );
88 }
89
90 public static function provideSet() {
91 return array(
92 array( 'Foo', 'wg', 'wgFoo' ),
93 array( 'SomethingRandom', 'wg', 'wgSomethingRandom' ),
94 array( 'FromAnExtension', 'eg', 'egFromAnExtension' ),
95 array( 'NoPrefixHere', '', 'NoPrefixHere' ),
96 );
97 }
98
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 );
103 }
104 }
105
106 /**
107 * @dataProvider provideSet
108 * @covers GlobalVarConfig::set
109 * @covers GlobalVarConfig::setWithPrefix
110 */
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] );
119 }
120 }