Merge "Revert "Re-enable tests from TitlePermissionTest""
[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->setMwGlobals( 'wgBaz', 'somevalue' );
12 // Check prefix is set to 'wg'
13 $this->assertEquals( 'somevalue', $config->get( 'Baz' ) );
14 }
15
16 /**
17 * @covers GlobalVarConfig::__construct
18 * @dataProvider provideConstructor
19 */
20 public function testConstructor( $prefix ) {
21 $var = $prefix . 'GlobalVarConfigTest';
22 $rand = wfRandomString();
23 $this->setMwGlobals( $var, $rand );
24 $config = new GlobalVarConfig( $prefix );
25 $this->assertInstanceOf( GlobalVarConfig::class, $config );
26 $this->assertEquals( $rand, $config->get( 'GlobalVarConfigTest' ) );
27 }
28
29 public static function provideConstructor() {
30 return [
31 [ 'wg' ],
32 [ 'ef' ],
33 [ 'smw' ],
34 [ 'blahblahblahblah' ],
35 [ '' ],
36 ];
37 }
38
39 /**
40 * @covers GlobalVarConfig::has
41 * @covers GlobalVarConfig::hasWithPrefix
42 */
43 public function testHas() {
44 $this->setMwGlobals( 'wgGlobalVarConfigTestHas', wfRandomString() );
45 $config = new GlobalVarConfig();
46 $this->assertTrue( $config->has( 'GlobalVarConfigTestHas' ) );
47 $this->assertFalse( $config->has( 'GlobalVarConfigTestNotHas' ) );
48 }
49
50 public static function provideGet() {
51 $set = [
52 'wgSomething' => 'default1',
53 'wgFoo' => 'default2',
54 'efVariable' => 'default3',
55 'BAR' => 'default4',
56 ];
57
58 foreach ( $set as $var => $value ) {
59 $GLOBALS[$var] = $value;
60 }
61
62 return [
63 [ 'Something', 'wg', 'default1' ],
64 [ 'Foo', 'wg', 'default2' ],
65 [ 'Variable', 'ef', 'default3' ],
66 [ 'BAR', '', 'default4' ],
67 [ 'ThisGlobalWasNotSetAbove', 'wg', false ]
68 ];
69 }
70
71 /**
72 * @dataProvider provideGet
73 * @covers GlobalVarConfig::get
74 * @covers GlobalVarConfig::getWithPrefix
75 * @param string $name
76 * @param string $prefix
77 * @param string $expected
78 */
79 public function testGet( $name, $prefix, $expected ) {
80 $config = new GlobalVarConfig( $prefix );
81 if ( $expected === false ) {
82 $this->setExpectedException( ConfigException::class, 'GlobalVarConfig::get: undefined option:' );
83 }
84 $this->assertEquals( $config->get( $name ), $expected );
85 }
86 }