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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
<?php
/**
* Sanity checks for making sure registered resources are sane.
*
* @file
* @author Antoine Musso
* @author Niklas Laxström
* @author Santhosh Thottingal
* @author Timo Tijhof
* @copyright © 2012, Antoine Musso
* @copyright © 2012, Niklas Laxström
* @copyright © 2012, Santhosh Thottingal
* @copyright © 2012, Timo Tijhof
*
*/
class ResourcesTest extends MediaWikiTestCase {
/**
* @dataProvider provideResourceFiles
*/
public function testFileExistence( $filename, $module, $resource ) {
$this->assertFileExists( $filename,
"File '$resource' referenced by '$module' must exist."
);
}
/**
* @dataProvider provideMediaStylesheets
*/
public function testStyleMedia( $moduleName, $media, $filename, $css ) {
$cssText = CSSMin::minify( $css->cssText );
$this->assertTrue(
strpos( $cssText, '@media' ) === false,
'Stylesheets should not both specify "media" and contain @media'
);
}
/**
* Verify that nothing explicitly depends on the 'jquery' and 'mediawiki' modules.
* They are always loaded, depending on them is unsupported and leads to unexpected behaviour.
*/
public function testIllegalDependencies() {
$data = self::getAllModules();
$illegalDeps = array( 'jquery', 'mediawiki' );
/** @var ResourceLoaderModule $module */
foreach ( $data['modules'] as $moduleName => $module ) {
foreach ( $illegalDeps as $illegalDep ) {
$this->assertNotContains(
$illegalDep,
$module->getDependencies(),
"Module '$moduleName' must not depend on '$illegalDep'"
);
}
}
}
/**
* Verify that all modules specified as dependencies of other modules actually exist.
*/
public function testMissingDependencies() {
$data = self::getAllModules();
$validDeps = array_keys( $data['modules'] );
/** @var ResourceLoaderModule $module */
foreach ( $data['modules'] as $moduleName => $module ) {
foreach ( $module->getDependencies() as $dep ) {
$this->assertContains(
$dep,
$validDeps,
"The module '$dep' required by '$moduleName' must exist"
);
}
}
}
/**
* Verify that all dependencies of all modules are always satisfiable with the 'targets' defined
* for the involved modules.
*
* Example: A depends on B. A has targets: mobile, desktop. B has targets: desktop. Therefore the
* dependency is sometimes unsatisfiable: it's impossible to load module A on mobile.
*/
public function testUnsatisfiableDependencies() {
$data = self::getAllModules();
$validDeps = array_keys( $data['modules'] );
/** @var ResourceLoaderModule $module */
foreach ( $data['modules'] as $moduleName => $module ) {
$moduleTargets = $module->getTargets();
foreach ( $module->getDependencies() as $dep ) {
if ( !isset( $data['modules'][$dep] ) ) {
// Missing dependencies reported by testMissingDependencies
continue;
}
$targets = $data['modules'][$dep]->getTargets();
foreach ( $moduleTargets as $moduleTarget ) {
$this->assertContains(
$moduleTarget,
$targets,
"The module '$moduleName' must not have target '$moduleTarget' "
. "because its dependency '$dep' does not have it"
);
}
}
}
}
/**
* Get all registered modules from ResouceLoader.
* @return array
*/
protected static function getAllModules() {
global $wgEnableJavaScriptTest;
// Test existance of test suite files as well
// (can't use setUp or setMwGlobals because providers are static)
$org_wgEnableJavaScriptTest = $wgEnableJavaScriptTest;
$wgEnableJavaScriptTest = true;
// Initialize ResourceLoader
$rl = new ResourceLoader();
$modules = array();
foreach ( $rl->getModuleNames() as $moduleName ) {
$modules[$moduleName] = $rl->getModule( $moduleName );
}
// Restore settings
$wgEnableJavaScriptTest = $org_wgEnableJavaScriptTest;
return array(
'modules' => $modules,
'resourceloader' => $rl,
'context' => new ResourceLoaderContext( $rl, new FauxRequest() )
);
}
/**
* Get all stylesheet files from modules that are an instance of
* ResourceLoaderFileModule (or one of its subclasses).
*/
public static function provideMediaStylesheets() {
$data = self::getAllModules();
$cases = array();
foreach ( $data['modules'] as $moduleName => $module ) {
if ( !$module instanceof ResourceLoaderFileModule ) {
continue;
}
$reflectedModule = new ReflectionObject( $module );
$getStyleFiles = $reflectedModule->getMethod( 'getStyleFiles' );
$getStyleFiles->setAccessible( true );
$readStyleFile = $reflectedModule->getMethod( 'readStyleFile' );
$readStyleFile->setAccessible( true );
$styleFiles = $getStyleFiles->invoke( $module, $data['context'] );
$flip = $module->getFlip( $data['context'] );
foreach ( $styleFiles as $media => $files ) {
if ( $media && $media !== 'all' ) {
foreach ( $files as $file ) {
$cases[] = array(
$moduleName,
$media,
$file,
// XXX: Wrapped in an object to keep it out of PHPUnit output
(object)array( 'cssText' => $readStyleFile->invoke( $module, $file, $flip ) ),
);
}
}
}
}
return $cases;
}
/**
* Get all resource files from modules that are an instance of
* ResourceLoaderFileModule (or one of its subclasses).
*
* Since the raw data is stored in protected properties, we have to
* overrride this through ReflectionObject methods.
*/
public static function provideResourceFiles() {
$data = self::getAllModules();
$cases = array();
// See also ResourceLoaderFileModule::__construct
$filePathProps = array(
// Lists of file paths
'lists' => array(
'scripts',
'debugScripts',
'loaderScripts',
'styles',
),
// Collated lists of file paths
'nested-lists' => array(
'languageScripts',
'skinScripts',
'skinStyles',
),
);
foreach ( $data['modules'] as $moduleName => $module ) {
if ( !$module instanceof ResourceLoaderFileModule ) {
continue;
}
$reflectedModule = new ReflectionObject( $module );
$files = array();
foreach ( $filePathProps['lists'] as $propName ) {
$property = $reflectedModule->getProperty( $propName );
$property->setAccessible( true );
$list = $property->getValue( $module );
foreach ( $list as $key => $value ) {
// 'scripts' are numeral arrays.
// 'styles' can be numeral or associative.
// In case of associative the key is the file path
// and the value is the 'media' attribute.
if ( is_int( $key ) ) {
$files[] = $value;
} else {
$files[] = $key;
}
}
}
foreach ( $filePathProps['nested-lists'] as $propName ) {
$property = $reflectedModule->getProperty( $propName );
$property->setAccessible( true );
$lists = $property->getValue( $module );
foreach ( $lists as $list ) {
foreach ( $list as $key => $value ) {
// We need the same filter as for 'lists',
// due to 'skinStyles'.
if ( is_int( $key ) ) {
$files[] = $value;
} else {
$files[] = $key;
}
}
}
}
// Get method for resolving the paths to full paths
$method = $reflectedModule->getMethod( 'getLocalPath' );
$method->setAccessible( true );
// Populate cases
foreach ( $files as $file ) {
$cases[] = array(
$method->invoke( $module, $file ),
$moduleName,
( $file instanceof ResourceLoaderFilePath ? $file->getPath() : $file ),
);
}
}
return $cases;
}
}
|