MediaWiki  1.28.1
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( 7, 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 
92  public function testMissingMessages() {
93  $data = self::getAllModules();
94  $validDeps = array_keys( $data['modules'] );
95  $lang = Language::factory( 'en' );
96 
98  foreach ( $data['modules'] as $moduleName => $module ) {
99  foreach ( $module->getMessages() as $msgKey ) {
100  $this->assertTrue(
101  wfMessage( $msgKey )->useDatabase( false )->inLanguage( $lang )->exists(),
102  "Message '$msgKey' required by '$moduleName' must exist"
103  );
104  }
105  }
106  }
107 
115  public function testUnsatisfiableDependencies() {
116  $data = self::getAllModules();
117  $validDeps = array_keys( $data['modules'] );
118 
120  foreach ( $data['modules'] as $moduleName => $module ) {
121  $moduleTargets = $module->getTargets();
122  foreach ( $module->getDependencies( $data['context'] ) as $dep ) {
123  if ( !isset( $data['modules'][$dep] ) ) {
124  // Missing dependencies reported by testMissingDependencies
125  continue;
126  }
127  $targets = $data['modules'][$dep]->getTargets();
128  foreach ( $moduleTargets as $moduleTarget ) {
129  $this->assertContains(
130  $moduleTarget,
131  $targets,
132  "The module '$moduleName' must not have target '$moduleTarget' "
133  . "because its dependency '$dep' does not have it"
134  );
135  }
136  }
137  }
138  }
139 
145  $basepath = __DIR__ . '/../data/css/';
146  $css = file_get_contents( $basepath . 'comments.css' );
148  $expected = [ $basepath . 'not-commented.gif' ];
149  $this->assertArrayEquals(
150  $expected,
151  $files,
152  'Url(...) expression in comment should be omitted.'
153  );
154  }
155 
160  protected static function getAllModules() {
161  global $wgEnableJavaScriptTest;
162 
163  // Test existance of test suite files as well
164  // (can't use setUp or setMwGlobals because providers are static)
165  $org_wgEnableJavaScriptTest = $wgEnableJavaScriptTest;
166  $wgEnableJavaScriptTest = true;
167 
168  // Initialize ResourceLoader
169  $rl = new ResourceLoader();
170 
171  $modules = [];
172 
173  foreach ( $rl->getModuleNames() as $moduleName ) {
174  $modules[$moduleName] = $rl->getModule( $moduleName );
175  }
176 
177  // Restore settings
178  $wgEnableJavaScriptTest = $org_wgEnableJavaScriptTest;
179 
180  return [
181  'modules' => $modules,
182  'resourceloader' => $rl,
183  'context' => new ResourceLoaderContext( $rl, new FauxRequest() )
184  ];
185  }
186 
191  public static function provideMediaStylesheets() {
192  $data = self::getAllModules();
193  $cases = [];
194 
195  foreach ( $data['modules'] as $moduleName => $module ) {
196  if ( !$module instanceof ResourceLoaderFileModule ) {
197  continue;
198  }
199 
200  $reflectedModule = new ReflectionObject( $module );
201 
202  $getStyleFiles = $reflectedModule->getMethod( 'getStyleFiles' );
203  $getStyleFiles->setAccessible( true );
204 
205  $readStyleFile = $reflectedModule->getMethod( 'readStyleFile' );
206  $readStyleFile->setAccessible( true );
207 
208  $styleFiles = $getStyleFiles->invoke( $module, $data['context'] );
209 
210  $flip = $module->getFlip( $data['context'] );
211 
212  foreach ( $styleFiles as $media => $files ) {
213  if ( $media && $media !== 'all' ) {
214  foreach ( $files as $file ) {
215  $cases[] = [
216  $moduleName,
217  $media,
218  $file,
219  // XXX: Wrapped in an object to keep it out of PHPUnit output
220  (object)[
221  'cssText' => $readStyleFile->invoke(
222  $module,
223  $file,
224  $flip,
225  $data['context']
226  )
227  ],
228  ];
229  }
230  }
231  }
232  }
233 
234  return $cases;
235  }
236 
244  public static function provideResourceFiles() {
245  $data = self::getAllModules();
246  $cases = [];
247 
248  // See also ResourceLoaderFileModule::__construct
249  $filePathProps = [
250  // Lists of file paths
251  'lists' => [
252  'scripts',
253  'debugScripts',
254  'styles',
255  ],
256 
257  // Collated lists of file paths
258  'nested-lists' => [
259  'languageScripts',
260  'skinScripts',
261  'skinStyles',
262  ],
263  ];
264 
265  foreach ( $data['modules'] as $moduleName => $module ) {
266  if ( !$module instanceof ResourceLoaderFileModule ) {
267  continue;
268  }
269 
270  $reflectedModule = new ReflectionObject( $module );
271 
272  $files = [];
273 
274  foreach ( $filePathProps['lists'] as $propName ) {
275  $property = $reflectedModule->getProperty( $propName );
276  $property->setAccessible( true );
277  $list = $property->getValue( $module );
278  foreach ( $list as $key => $value ) {
279  // 'scripts' are numeral arrays.
280  // 'styles' can be numeral or associative.
281  // In case of associative the key is the file path
282  // and the value is the 'media' attribute.
283  if ( is_int( $key ) ) {
284  $files[] = $value;
285  } else {
286  $files[] = $key;
287  }
288  }
289  }
290 
291  foreach ( $filePathProps['nested-lists'] as $propName ) {
292  $property = $reflectedModule->getProperty( $propName );
293  $property->setAccessible( true );
294  $lists = $property->getValue( $module );
295  foreach ( $lists as $list ) {
296  foreach ( $list as $key => $value ) {
297  // We need the same filter as for 'lists',
298  // due to 'skinStyles'.
299  if ( is_int( $key ) ) {
300  $files[] = $value;
301  } else {
302  $files[] = $key;
303  }
304  }
305  }
306  }
307 
308  // Get method for resolving the paths to full paths
309  $method = $reflectedModule->getMethod( 'getLocalPath' );
310  $method->setAccessible( true );
311 
312  // Populate cases
313  foreach ( $files as $file ) {
314  $cases[] = [
315  $method->invoke( $module, $file ),
316  $moduleName,
317  ( $file instanceof ResourceLoaderFilePath ? $file->getPath() : $file ),
318  ];
319  }
320 
321  // To populate missingLocalFileRefs. Not sure how sane this is inside this test...
322  $module->readStyleFiles(
323  $module->getStyleFiles( $data['context'] ),
324  $module->getFlip( $data['context'] ),
325  $data['context']
326  );
327 
328  $property = $reflectedModule->getProperty( 'missingLocalFileRefs' );
329  $property->setAccessible( true );
330  $missingLocalFileRefs = $property->getValue( $module );
331 
332  foreach ( $missingLocalFileRefs as $file ) {
333  $cases[] = [
334  $file,
335  $moduleName,
336  $file,
337  ];
338  }
339  }
340 
341  return $cases;
342  }
343 }
$property
testMissingMessages()
Verify that all specified messages actually exist.
static provideMediaStylesheets()
Get all stylesheet files from modules that are an instance of ResourceLoaderFileModule (or one of its...
if(!isset($args[0])) $lang
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.
$modules
$css
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock()-offset Set to overwrite offset parameter in $wgRequest set to ''to unsetoffset-wrap String Wrap the message in html(usually something like"&lt
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
static factory($code)
Get a cached or new language object for a given language code.
Definition: Language.php:181
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...