summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/registration/ExtensionRegistryTest.php
blob: 1b24628c6b4046747ad89d8b99691ae5ff42f94d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php

class ExtensionRegistryTest extends MediaWikiTestCase {

	/**
	 * @covers ExtensionRegistry::exportExtractedData
	 * @dataProvider provideExportExtractedDataGlobals
	 */
	public function testExportExtractedDataGlobals( $desc, $before, $globals, $expected ) {
		// Set globals for test
		if ( $before ) {
			foreach ( $before as $key => $value ) {
				// mw prefixed globals does not exist normally
				if ( substr( $key, 0, 2 ) == 'mw' ) {
					$GLOBALS[$key] = $value;
				} else {
					$this->setMwGlobals( $key, $value );
				}
			}
		}

		$info = array(
			'globals' => $globals,
			'callbacks' => array(),
			'defines' => array(),
			'credits' => array(),
			'attributes' => array(),
		);
		$registry = new ExtensionRegistry();
		$class = new ReflectionClass( 'ExtensionRegistry' );
		$method = $class->getMethod( 'exportExtractedData' );
		$method->setAccessible( true );
		$method->invokeArgs( $registry, array( $info ) );
		foreach ( $expected as $name => $value ) {
			$this->assertArrayHasKey( $name, $GLOBALS, $desc );
			$this->assertEquals( $value, $GLOBALS[$name], $desc );
		}

		// Remove mw prefixed globals
		if ( $before ) {
			foreach ( $before as $key => $value ) {
				if ( substr( $key, 0, 2 ) == 'mw' ) {
					unset( $GLOBALS[$key] );
				}
			}
		}
	}

	public static function provideExportExtractedDataGlobals() {
		// "mwtest" prefix used instead of "$wg" to avoid potential conflicts
		return array(
			array(
				'Simple non-array values',
				array(
					'mwtestFooBarConfig' => true,
					'mwtestFooBarConfig2' => 'string',
				),
				array(
					'mwtestFooBarDefault' => 1234,
					'mwtestFooBarConfig' => false,
				),
				array(
					'mwtestFooBarConfig' => true,
					'mwtestFooBarConfig2' => 'string',
					'mwtestFooBarDefault' => 1234,
				),
			),
			array(
				'No global already set, simple array',
				null,
				array(
					'mwtestDefaultOptions' => array(
						'foobar' => true,
					)
				),
				array(
					'mwtestDefaultOptions' => array(
						'foobar' => true,
					)
				),
			),
			array(
				'Global already set, simple array',
				array(
					'mwtestDefaultOptions' => array(
						'foobar' => true,
						'foo' => 'string'
					),
				),
				array(
					'mwtestDefaultOptions' => array(
						'barbaz' => 12345,
						'foobar' => false,
					),
				),
				array(
					'mwtestDefaultOptions' => array(
						'barbaz' => 12345,
						'foo' => 'string',
						'foobar' => true,
					),
				)
			),
			array(
				'No global already set, $wgHooks',
				array(
					'wgHooks' => array(),
				),
				array(
					'wgHooks' => array(
						'FooBarEvent' => array(
							'FooBarClass::onFooBarEvent'
						),
					),
				),
				array(
					'wgHooks' => array(
						'FooBarEvent' => array(
							'FooBarClass::onFooBarEvent'
						),
					),
				),
			),
			array(
				'Global already set, $wgHooks',
				array(
					'wgHooks' => array(
						'FooBarEvent' => array(
							'FooBarClass::onFooBarEvent'
						),
						'BazBarEvent' => array(
							'FooBarClass::onBazBarEvent',
						),
					),
				),
				array(
					'wgHooks' => array(
						'FooBarEvent' => array(
							'BazBarClass::onFooBarEvent',
						),
					),
				),
				array(
					'wgHooks' => array(
						'FooBarEvent' => array(
							'FooBarClass::onFooBarEvent',
							'BazBarClass::onFooBarEvent',
						),
						'BazBarEvent' => array(
							'FooBarClass::onBazBarEvent',
						),
					),
				),
			),
			array(
				'Global already set, $wgGroupPermissions',
				array(
					'wgGroupPermissions' => array(
						'sysop' => array(
							'something' => true,
						),
						'user' => array(
							'somethingtwo' => true,
						)
					),
				),
				array(
					'wgGroupPermissions' => array(
						'customgroup' => array(
							'right' => true,
						),
						'user' => array(
							'right' => true,
							'somethingtwo' => false,
						)
					),
				),
				array(
					'wgGroupPermissions' => array(
						'customgroup' => array(
							'right' => true,
						),
						'sysop' => array(
							'something' => true,
						),
						'user' => array(
							'somethingtwo' => true,
							'right' => true,
						)
					),
				),
			)
		);
	}
}