Merge "Fix sessionfailure i18n message during authentication"
[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::class, $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::class, $config );
28 $this->assertEquals( $rand, $config->get( 'GlobalVarConfigTest' ) );
29 }
30
31 public static function provideConstructor() {
32 return [
33 [ 'wg' ],
34 [ 'ef' ],
35 [ 'smw' ],
36 [ 'blahblahblahblah' ],
37 [ '' ],
38 ];
39 }
40
41 /**
42 * @covers GlobalVarConfig::has
43 * @covers GlobalVarConfig::hasWithPrefix
44 */
45 public function testHas() {
46 $this->maybeStashGlobal( 'wgGlobalVarConfigTestHas' );
47 $GLOBALS['wgGlobalVarConfigTestHas'] = wfRandomString();
48 $this->maybeStashGlobal( 'wgGlobalVarConfigTestNotHas' );
49 $config = new GlobalVarConfig();
50 $this->assertTrue( $config->has( 'GlobalVarConfigTestHas' ) );
51 $this->assertFalse( $config->has( 'GlobalVarConfigTestNotHas' ) );
52 }
53
54 public static function provideGet() {
55 $set = [
56 'wgSomething' => 'default1',
57 'wgFoo' => 'default2',
58 'efVariable' => 'default3',
59 'BAR' => 'default4',
60 ];
61
62 foreach ( $set as $var => $value ) {
63 $GLOBALS[$var] = $value;
64 }
65
66 return [
67 [ 'Something', 'wg', 'default1' ],
68 [ 'Foo', 'wg', 'default2' ],
69 [ 'Variable', 'ef', 'default3' ],
70 [ 'BAR', '', 'default4' ],
71 [ 'ThisGlobalWasNotSetAbove', 'wg', false ]
72 ];
73 }
74
75 /**
76 * @dataProvider provideGet
77 * @covers GlobalVarConfig::get
78 * @covers GlobalVarConfig::getWithPrefix
79 * @param string $name
80 * @param string $prefix
81 * @param string $expected
82 */
83 public function testGet( $name, $prefix, $expected ) {
84 $config = new GlobalVarConfig( $prefix );
85 if ( $expected === false ) {
86 $this->setExpectedException( ConfigException::class, 'GlobalVarConfig::get: undefined option:' );
87 }
88 $this->assertEquals( $config->get( $name ), $expected );
89 }
90
91 private function maybeStashGlobal( $var ) {
92 if ( array_key_exists( $var, $GLOBALS ) ) {
93 // Will be reset after this test is over
94 $this->stashMwGlobals( $var );
95 }
96 }
97 }