MediaWiki  1.27.2
ResourcesTest.php
Go to the documentation of this file.
1 <?php
17 
21  public function testFileExistence( $filename, $module, $resource ) {
22  $this->assertFileExists( $filename,
23  "File '$resource' referenced by '$module' must exist."
24  );
25  }
26 
30  public function testStyleMedia( $moduleName, $media, $filename, $css ) {
31  $cssText = CSSMin::minify( $css->cssText );
32 
33  $this->assertTrue(
34  strpos( $cssText, '@media' ) === false,
35  'Stylesheets should not both specify "media" and contain @media'
36  );
37  }
38 
39  public function testVersionHash() {
40  $data = self::getAllModules();
41  foreach ( $data['modules'] as $moduleName => $module ) {
42  $version = $module->getVersionHash( $data['context'] );
43  $this->assertEquals( 8, strlen( $version ), "$moduleName must use ResourceLoader::makeHash" );
44  }
45  }
46 
54  public function testIllegalDependencies() {
55  $data = self::getAllModules();
56  $illegalDeps = [ 'jquery', 'mediawiki' ];
57 
59  foreach ( $data['modules'] as $moduleName => $module ) {
60  foreach ( $illegalDeps as $illegalDep ) {
61  $this->assertNotContains(
62  $illegalDep,
63  $module->getDependencies( $data['context'] ),
64  "Module '$moduleName' must not depend on '$illegalDep'"
65  );
66  }
67  }
68  }
69 
73  public function testMissingDependencies() {
74  $data = self::getAllModules();
75  $validDeps = array_keys( $data['modules'] );
76 
78  foreach ( $data['modules'] as $moduleName => $module ) {
79  foreach ( $module->getDependencies( $data['context'] ) as $dep ) {
80  $this->assertContains(
81  $dep,
82  $validDeps,
83  "The module '$dep' required by '$moduleName' must exist"
84  );
85  }
86  }
87  }
88 
96  public function testUnsatisfiableDependencies() {
97  $data = self::getAllModules();
98  $validDeps = array_keys( $data['modules'] );
99 
101  foreach ( $data['modules'] as $moduleName => $module ) {
102  $moduleTargets = $module->getTargets();
103  foreach ( $module->getDependencies( $data['context'] ) as $dep ) {
104  if ( !isset( $data['modules'][$dep] ) ) {
105  // Missing dependencies reported by testMissingDependencies
106  continue;
107  }
108  $targets = $data['modules'][$dep]->getTargets();
109  foreach ( $moduleTargets as $moduleTarget ) {
110  $this->assertContains(
111  $moduleTarget,
112  $targets,
113  "The module '$moduleName' must not have target '$moduleTarget' "
114  . "because its dependency '$dep' does not have it"
115  );
116  }
117  }
118  }
119  }
120 
126  $basepath = __DIR__ . '/../data/css/';
127  $css = file_get_contents( $basepath . 'comments.css' );
129  $expected = [ $basepath . 'not-commented.gif' ];
130  $this->assertArrayEquals(
131  $expected,
132  $files,
133  'Url(...) expression in comment should be omitted.'
134  );
135  }
136 
141  protected static function getAllModules() {
142  global $wgEnableJavaScriptTest;
143 
144  // Test existance of test suite files as well
145  // (can't use setUp or setMwGlobals because providers are static)
146  $org_wgEnableJavaScriptTest = $wgEnableJavaScriptTest;
147  $wgEnableJavaScriptTest = true;
148 
149  // Initialize ResourceLoader
150  $rl = new ResourceLoader();
151 
152  $modules = [];
153 
154  foreach ( $rl->getModuleNames() as $moduleName ) {
155  $modules[$moduleName] = $rl->getModule( $moduleName );
156  }
157 
158  // Restore settings
159  $wgEnableJavaScriptTest = $org_wgEnableJavaScriptTest;
160 
161  return [
162  'modules' => $modules,
163  'resourceloader' => $rl,
164  'context' => new ResourceLoaderContext( $rl, new FauxRequest() )
165  ];
166  }
167 
172  public static function provideMediaStylesheets() {
173  $data = self::getAllModules();
174  $cases = [];
175 
176  foreach ( $data['modules'] as $moduleName => $module ) {
177  if ( !$module instanceof ResourceLoaderFileModule ) {
178  continue;
179  }
180 
181  $reflectedModule = new ReflectionObject( $module );
182 
183  $getStyleFiles = $reflectedModule->getMethod( 'getStyleFiles' );
184  $getStyleFiles->setAccessible( true );
185 
186  $readStyleFile = $reflectedModule->getMethod( 'readStyleFile' );
187  $readStyleFile->setAccessible( true );
188 
189  $styleFiles = $getStyleFiles->invoke( $module, $data['context'] );
190 
191  $flip = $module->getFlip( $data['context'] );
192 
193  foreach ( $styleFiles as $media => $files ) {
194  if ( $media && $media !== 'all' ) {
195  foreach ( $files as $file ) {
196  $cases[] = [
197  $moduleName,
198  $media,
199  $file,
200  // XXX: Wrapped in an object to keep it out of PHPUnit output
201  (object)[
202  'cssText' => $readStyleFile->invoke(
203  $module,
204  $file,
205  $flip,
206  $data['context']
207  )
208  ],
209  ];
210  }
211  }
212  }
213  }
214 
215  return $cases;
216  }
217 
225  public static function provideResourceFiles() {
226  $data = self::getAllModules();
227  $cases = [];
228 
229  // See also ResourceLoaderFileModule::__construct
230  $filePathProps = [
231  // Lists of file paths
232  'lists' => [
233  'scripts',
234  'debugScripts',
235  'styles',
236  ],
237 
238  // Collated lists of file paths
239  'nested-lists' => [
240  'languageScripts',
241  'skinScripts',
242  'skinStyles',
243  ],
244  ];
245 
246  foreach ( $data['modules'] as $moduleName => $module ) {
247  if ( !$module instanceof ResourceLoaderFileModule ) {
248  continue;
249  }
250 
251  $reflectedModule = new ReflectionObject( $module );
252 
253  $files = [];
254 
255  foreach ( $filePathProps['lists'] as $propName ) {
256  $property = $reflectedModule->getProperty( $propName );
257  $property->setAccessible( true );
258  $list = $property->getValue( $module );
259  foreach ( $list as $key => $value ) {
260  // 'scripts' are numeral arrays.
261  // 'styles' can be numeral or associative.
262  // In case of associative the key is the file path
263  // and the value is the 'media' attribute.
264  if ( is_int( $key ) ) {
265  $files[] = $value;
266  } else {
267  $files[] = $key;
268  }
269  }
270  }
271 
272  foreach ( $filePathProps['nested-lists'] as $propName ) {
273  $property = $reflectedModule->getProperty( $propName );
274  $property->setAccessible( true );
275  $lists = $property->getValue( $module );
276  foreach ( $lists as $list ) {
277  foreach ( $list as $key => $value ) {
278  // We need the same filter as for 'lists',
279  // due to 'skinStyles'.
280  if ( is_int( $key ) ) {
281  $files[] = $value;
282  } else {
283  $files[] = $key;
284  }
285  }
286  }
287  }
288 
289  // Get method for resolving the paths to full paths
290  $method = $reflectedModule->getMethod( 'getLocalPath' );
291  $method->setAccessible( true );
292 
293  // Populate cases
294  foreach ( $files as $file ) {
295  $cases[] = [
296  $method->invoke( $module, $file ),
297  $moduleName,
298  ( $file instanceof ResourceLoaderFilePath ? $file->getPath() : $file ),
299  ];
300  }
301 
302  // To populate missingLocalFileRefs. Not sure how sane this is inside this test...
303  $module->readStyleFiles(
304  $module->getStyleFiles( $data['context'] ),
305  $module->getFlip( $data['context'] ),
306  $data['context']
307  );
308 
309  $property = $reflectedModule->getProperty( 'missingLocalFileRefs' );
310  $property->setAccessible( true );
311  $missingLocalFileRefs = $property->getValue( $module );
312 
313  foreach ( $missingLocalFileRefs as $file ) {
314  $cases[] = [
315  $file,
316  $moduleName,
317  $file,
318  ];
319  }
320  }
321 
322  return $cases;
323  }
324 }
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
$property
static provideMediaStylesheets()
Get all stylesheet files from modules that are an instance of ResourceLoaderFileModule (or one of its...
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
$value
$files
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition: globals.txt:25
testFileExistence($filename, $module, $resource)
provideResourceFiles
testMissingDependencies()
Verify that all modules specified as dependencies of other modules actually exist.
static array static getLocalFileReferences($source, $path)
Get a list of local files referenced in a stylesheet (includes non-existent files).
Definition: CSSMin.php:69
static getAllModules()
Get all registered modules from ResouceLoader.
$css
ResourceLoader module based on local JavaScript/CSS files.
testStyleMedia($moduleName, $media, $filename, $css)
provideMediaStylesheets
testCommentedLocalFileReferences()
CSSMin::getLocalFileReferences should ignore url(...) expressions that have been commented out...
testIllegalDependencies()
Verify that nothing explicitly depends on the 'jquery' and 'mediawiki' modules.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
An object to represent a path to a JavaScript/CSS file, along with a remote and local base path...
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static minify($css)
Removes whitespace from CSS data.
Definition: CSSMin.php:453
$version
Definition: parserTests.php:85
Dynamic JavaScript and CSS resource loading system.
static provideResourceFiles()
Get all resource files from modules that are an instance of ResourceLoaderFileModule (or one of its s...
testUnsatisfiableDependencies()
Verify that all dependencies of all modules are always satisfiable with the 'targets' defined for the...
Object passed around to modules which contains information about the state of a specific loader reque...