MediaWiki  master
ResourcesTest.php
Go to the documentation of this file.
1 <?php
2 
5 
20 
24  public function testFileExistence( $filename, $module, $resource ) {
25  $this->assertFileExists( $filename,
26  "File '$resource' referenced by '$module' must exist."
27  );
28  }
29 
33  public function testStyleMedia( $moduleName, $media, $filename, $css ) {
34  $cssText = CSSMin::minify( $css->cssText );
35 
36  $this->assertTrue(
37  strpos( $cssText, '@media' ) === false,
38  'Stylesheets should not both specify "media" and contain @media'
39  );
40  }
41 
42  public function testVersionHash() {
43  $data = self::getAllModules();
44  foreach ( $data['modules'] as $moduleName => $module ) {
45  $version = $module->getVersionHash( $data['context'] );
46  $this->assertEquals( 7, strlen( $version ), "$moduleName must use ResourceLoader::makeHash" );
47  }
48  }
49 
59  public function testIllegalDependencies() {
60  $data = self::getAllModules();
61 
62  $illegalDeps = [];
63  foreach ( $data['modules'] as $moduleName => $module ) {
64  if ( $module->isRaw() ) {
65  $illegalDeps[] = $moduleName;
66  }
67  }
68 
70  foreach ( $data['modules'] as $moduleName => $module ) {
71  foreach ( $illegalDeps as $illegalDep ) {
72  $this->assertNotContains(
73  $illegalDep,
74  $module->getDependencies( $data['context'] ),
75  "Module '$moduleName' must not depend on '$illegalDep'"
76  );
77  }
78  }
79  }
80 
84  public function testMissingDependencies() {
85  $data = self::getAllModules();
86  $validDeps = array_keys( $data['modules'] );
87 
89  foreach ( $data['modules'] as $moduleName => $module ) {
90  foreach ( $module->getDependencies( $data['context'] ) as $dep ) {
91  $this->assertContains(
92  $dep,
93  $validDeps,
94  "The module '$dep' required by '$moduleName' must exist"
95  );
96  }
97  }
98  }
99 
103  public function testMissingMessages() {
104  $data = self::getAllModules();
105  $lang = Language::factory( 'en' );
106 
108  foreach ( $data['modules'] as $moduleName => $module ) {
109  foreach ( $module->getMessages() as $msgKey ) {
110  $this->assertTrue(
111  wfMessage( $msgKey )->useDatabase( false )->inLanguage( $lang )->exists(),
112  "Message '$msgKey' required by '$moduleName' must exist"
113  );
114  }
115  }
116  }
117 
125  public function testUnsatisfiableDependencies() {
126  $data = self::getAllModules();
127 
129  foreach ( $data['modules'] as $moduleName => $module ) {
130  $moduleTargets = $module->getTargets();
131  foreach ( $module->getDependencies( $data['context'] ) as $dep ) {
132  if ( !isset( $data['modules'][$dep] ) ) {
133  // Missing dependencies reported by testMissingDependencies
134  continue;
135  }
136  $targets = $data['modules'][$dep]->getTargets();
137  foreach ( $moduleTargets as $moduleTarget ) {
138  $this->assertContains(
139  $moduleTarget,
140  $targets,
141  "The module '$moduleName' must not have target '$moduleTarget' "
142  . "because its dependency '$dep' does not have it"
143  );
144  }
145  }
146  }
147  }
148 
154  $basepath = __DIR__ . '/../data/css/';
155  $css = file_get_contents( $basepath . 'comments.css' );
156  $files = CSSMin::getLocalFileReferences( $css, $basepath );
157  $expected = [ $basepath . 'not-commented.gif' ];
158  $this->assertSame(
159  $expected,
160  $files,
161  'Url(...) expression in comment should be omitted.'
162  );
163  }
164 
169  protected static function getAllModules() {
171 
172  // Test existance of test suite files as well
173  // (can't use setUp or setMwGlobals because providers are static)
174  $org_wgEnableJavaScriptTest = $wgEnableJavaScriptTest;
175  $wgEnableJavaScriptTest = true;
176 
177  // Get main ResourceLoader
178  $rl = MediaWikiServices::getInstance()->getResourceLoader();
179 
180  $modules = [];
181 
182  foreach ( $rl->getModuleNames() as $moduleName ) {
183  $modules[$moduleName] = $rl->getModule( $moduleName );
184  }
185 
186  // Restore settings
187  $wgEnableJavaScriptTest = $org_wgEnableJavaScriptTest;
188 
189  return [
190  'modules' => $modules,
191  'resourceloader' => $rl,
192  'context' => new ResourceLoaderContext( $rl, new FauxRequest() )
193  ];
194  }
195 
200  public static function provideMediaStylesheets() {
201  $data = self::getAllModules();
202  $cases = [];
203 
204  foreach ( $data['modules'] as $moduleName => $module ) {
205  if ( !$module instanceof ResourceLoaderFileModule ) {
206  continue;
207  }
208 
209  $reflectedModule = new ReflectionObject( $module );
210 
211  $getStyleFiles = $reflectedModule->getMethod( 'getStyleFiles' );
212  $getStyleFiles->setAccessible( true );
213 
214  $readStyleFile = $reflectedModule->getMethod( 'readStyleFile' );
215  $readStyleFile->setAccessible( true );
216 
217  $styleFiles = $getStyleFiles->invoke( $module, $data['context'] );
218 
219  $flip = $module->getFlip( $data['context'] );
220 
221  foreach ( $styleFiles as $media => $files ) {
222  if ( $media && $media !== 'all' ) {
223  foreach ( $files as $file ) {
224  $cases[] = [
225  $moduleName,
226  $media,
227  $file,
228  // XXX: Wrapped in an object to keep it out of PHPUnit output
229  (object)[
230  'cssText' => $readStyleFile->invoke(
231  $module,
232  $file,
233  $flip,
234  $data['context']
235  )
236  ],
237  ];
238  }
239  }
240  }
241  }
242 
243  return $cases;
244  }
245 
250  public static function provideResourceFiles() {
251  $data = self::getAllModules();
252  $cases = [];
253 
254  // See also ResourceLoaderFileModule::__construct
255  $filePathProps = [
256  // Lists of file paths
257  'lists' => [
258  'scripts',
259  'debugScripts',
260  'styles',
261  ],
262 
263  // Collated lists of file paths
264  'nested-lists' => [
265  'languageScripts',
266  'skinScripts',
267  'skinStyles',
268  ],
269  ];
270 
271  foreach ( $data['modules'] as $moduleName => $module ) {
272  if ( !$module instanceof ResourceLoaderFileModule ) {
273  continue;
274  }
275 
276  $moduleProxy = TestingAccessWrapper::newFromObject( $module );
277 
278  $files = [];
279 
280  foreach ( $filePathProps['lists'] as $propName ) {
281  $list = $moduleProxy->$propName;
282  foreach ( $list as $key => $value ) {
283  // 'scripts' are numeral arrays.
284  // 'styles' can be numeral or associative.
285  // In case of associative the key is the file path
286  // and the value is the 'media' attribute.
287  if ( is_int( $key ) ) {
288  $files[] = $value;
289  } else {
290  $files[] = $key;
291  }
292  }
293  }
294 
295  foreach ( $filePathProps['nested-lists'] as $propName ) {
296  $lists = $moduleProxy->$propName;
297  foreach ( $lists as $list ) {
298  foreach ( $list as $key => $value ) {
299  // We need the same filter as for 'lists',
300  // due to 'skinStyles'.
301  if ( is_int( $key ) ) {
302  $files[] = $value;
303  } else {
304  $files[] = $key;
305  }
306  }
307  }
308  }
309 
310  // Populate cases
311  foreach ( $files as $file ) {
312  $cases[] = [
313  $moduleProxy->getLocalPath( $file ),
314  $moduleName,
315  ( $file instanceof ResourceLoaderFilePath ? $file->getPath() : $file ),
316  ];
317  }
318 
319  // To populate missingLocalFileRefs. Not sure how sane this is inside this test...
320  $moduleProxy->readStyleFiles(
321  $module->getStyleFiles( $data['context'] ),
322  $module->getFlip( $data['context'] ),
323  $data['context']
324  );
325 
326  $missingLocalFileRefs = $moduleProxy->missingLocalFileRefs;
327 
328  foreach ( $missingLocalFileRefs as $file ) {
329  $cases[] = [
330  $file,
331  $moduleName,
332  $file,
333  ];
334  }
335  }
336 
337  return $cases;
338  }
339 }
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
testMissingMessages()
Verify that all specified messages actually exist.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
static provideMediaStylesheets()
Get all stylesheet files from modules that are an instance of ResourceLoaderFileModule (or one of its...
if(!isset( $args[0])) $lang
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 $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
$value
static minify( $css)
Removes whitespace from CSS data.
Definition: CSSMin.php:540
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 MediaWikiServices
Definition: injection.txt:23
testMissingDependencies()
Verify that all modules specified as dependencies of other modules actually exist.
$wgEnableJavaScriptTest
Allow running of javascript test suites via [[Special:JavaScriptTest]] (such as QUnit).
Sanity checks for making sure registered resources are sane.
static getAllModules()
Get all registered modules from ResouceLoader.
$modules
testStyleMedia( $moduleName, $media, $filename, $css)
provideMediaStylesheets
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 use $formDescriptor instead 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 unset offset - wrap String Wrap the message in html(usually something like "&lt
$css
testFileExistence( $filename, $module, $resource)
provideResourceFiles
ResourceLoader module based on local JavaScript/CSS files.
testCommentedLocalFileReferences()
CSSMin::getLocalFileReferences should ignore url(...) expressions that have been commented out...
static string [] static getLocalFileReferences( $source, $path)
Get a list of local files referenced in a stylesheet (includes non-existent files).
Definition: CSSMin.php:63
testIllegalDependencies()
Verify that nothing explicitly depends on raw modules (such as "query").
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:215
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 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 &#39;targets&#39; defined for the...
Object passed around to modules which contains information about the state of a specific loader reque...