MediaWiki  master
ResourcesTest.php
Go to the documentation of this file.
1 <?php
2 
5 
19 
23  public function testFileExistence( $filename, $module, $resource ) {
24  $this->assertFileExists( $filename,
25  "File '$resource' referenced by '$module' must exist."
26  );
27  }
28 
32  public function testStyleMedia( $moduleName, $media, $filename, $css ) {
33  $cssText = CSSMin::minify( $css->cssText );
34 
35  $this->assertTrue(
36  strpos( $cssText, '@media' ) === false,
37  'Stylesheets should not both specify "media" and contain @media'
38  );
39  }
40 
41  public function testVersionHash() {
42  $data = self::getAllModules();
43  foreach ( $data['modules'] as $moduleName => $module ) {
44  $version = $module->getVersionHash( $data['context'] );
45  $this->assertEquals( 7, strlen( $version ), "$moduleName must use ResourceLoader::makeHash" );
46  }
47  }
48 
58  public function testIllegalDependencies() {
59  $data = self::getAllModules();
60 
61  $illegalDeps = [];
62  foreach ( $data['modules'] as $moduleName => $module ) {
63  if ( $module->isRaw() ) {
64  $illegalDeps[] = $moduleName;
65  }
66  }
67 
69  foreach ( $data['modules'] as $moduleName => $module ) {
70  foreach ( $illegalDeps as $illegalDep ) {
71  $this->assertNotContains(
72  $illegalDep,
73  $module->getDependencies( $data['context'] ),
74  "Module '$moduleName' must not depend on '$illegalDep'"
75  );
76  }
77  }
78  }
79 
83  public function testMissingDependencies() {
84  $data = self::getAllModules();
85  $validDeps = array_keys( $data['modules'] );
86 
88  foreach ( $data['modules'] as $moduleName => $module ) {
89  foreach ( $module->getDependencies( $data['context'] ) as $dep ) {
90  $this->assertContains(
91  $dep,
92  $validDeps,
93  "The module '$dep' required by '$moduleName' must exist"
94  );
95  }
96  }
97  }
98 
102  public function testMissingMessages() {
103  $data = self::getAllModules();
104  $lang = Language::factory( 'en' );
105 
107  foreach ( $data['modules'] as $moduleName => $module ) {
108  foreach ( $module->getMessages() as $msgKey ) {
109  $this->assertTrue(
110  wfMessage( $msgKey )->useDatabase( false )->inLanguage( $lang )->exists(),
111  "Message '$msgKey' required by '$moduleName' must exist"
112  );
113  }
114  }
115  }
116 
124  public function testUnsatisfiableDependencies() {
125  $data = self::getAllModules();
126 
128  foreach ( $data['modules'] as $moduleName => $module ) {
129  $moduleTargets = $module->getTargets();
130  foreach ( $module->getDependencies( $data['context'] ) as $dep ) {
131  if ( !isset( $data['modules'][$dep] ) ) {
132  // Missing dependencies reported by testMissingDependencies
133  continue;
134  }
135  $targets = $data['modules'][$dep]->getTargets();
136  foreach ( $moduleTargets as $moduleTarget ) {
137  $this->assertContains(
138  $moduleTarget,
139  $targets,
140  "The module '$moduleName' must not have target '$moduleTarget' "
141  . "because its dependency '$dep' does not have it"
142  );
143  }
144  }
145  }
146  }
147 
153  $basepath = __DIR__ . '/../data/css/';
154  $css = file_get_contents( $basepath . 'comments.css' );
155  $files = CSSMin::getLocalFileReferences( $css, $basepath );
156  $expected = [ $basepath . 'not-commented.gif' ];
157  $this->assertSame(
158  $expected,
159  $files,
160  'Url(...) expression in comment should be omitted.'
161  );
162  }
163 
168  protected static function getAllModules() {
170 
171  // Test existance of test suite files as well
172  // (can't use setUp or setMwGlobals because providers are static)
173  $org_wgEnableJavaScriptTest = $wgEnableJavaScriptTest;
174  $wgEnableJavaScriptTest = true;
175 
176  // Get main ResourceLoader
177  $rl = MediaWikiServices::getInstance()->getResourceLoader();
178 
179  $modules = [];
180 
181  foreach ( $rl->getModuleNames() as $moduleName ) {
182  $modules[$moduleName] = $rl->getModule( $moduleName );
183  }
184 
185  // Restore settings
186  $wgEnableJavaScriptTest = $org_wgEnableJavaScriptTest;
187 
188  return [
189  'modules' => $modules,
190  'resourceloader' => $rl,
191  'context' => new ResourceLoaderContext( $rl, new FauxRequest() )
192  ];
193  }
194 
199  public static function provideMediaStylesheets() {
200  $data = self::getAllModules();
201  $cases = [];
202 
203  foreach ( $data['modules'] as $moduleName => $module ) {
204  if ( !$module instanceof ResourceLoaderFileModule ) {
205  continue;
206  }
207 
208  $reflectedModule = new ReflectionObject( $module );
209 
210  $getStyleFiles = $reflectedModule->getMethod( 'getStyleFiles' );
211  $getStyleFiles->setAccessible( true );
212 
213  $readStyleFile = $reflectedModule->getMethod( 'readStyleFile' );
214  $readStyleFile->setAccessible( true );
215 
216  $styleFiles = $getStyleFiles->invoke( $module, $data['context'] );
217 
218  $flip = $module->getFlip( $data['context'] );
219 
220  foreach ( $styleFiles as $media => $files ) {
221  if ( $media && $media !== 'all' ) {
222  foreach ( $files as $file ) {
223  $cases[] = [
224  $moduleName,
225  $media,
226  $file,
227  // XXX: Wrapped in an object to keep it out of PHPUnit output
228  (object)[
229  'cssText' => $readStyleFile->invoke(
230  $module,
231  $file,
232  $flip,
233  $data['context']
234  )
235  ],
236  ];
237  }
238  }
239  }
240  }
241 
242  return $cases;
243  }
244 
249  public static function provideResourceFiles() {
250  $data = self::getAllModules();
251  $cases = [];
252 
253  // See also ResourceLoaderFileModule::__construct
254  $filePathProps = [
255  // Lists of file paths
256  'lists' => [
257  'scripts',
258  'debugScripts',
259  'styles',
260  ],
261 
262  // Collated lists of file paths
263  'nested-lists' => [
264  'languageScripts',
265  'skinScripts',
266  'skinStyles',
267  ],
268  ];
269 
270  foreach ( $data['modules'] as $moduleName => $module ) {
271  if ( !$module instanceof ResourceLoaderFileModule ) {
272  continue;
273  }
274 
275  $moduleProxy = TestingAccessWrapper::newFromObject( $module );
276 
277  $files = [];
278 
279  foreach ( $filePathProps['lists'] as $propName ) {
280  $list = $moduleProxy->$propName;
281  foreach ( $list as $key => $value ) {
282  // 'scripts' are numeral arrays.
283  // 'styles' can be numeral or associative.
284  // In case of associative the key is the file path
285  // and the value is the 'media' attribute.
286  if ( is_int( $key ) ) {
287  $files[] = $value;
288  } else {
289  $files[] = $key;
290  }
291  }
292  }
293 
294  foreach ( $filePathProps['nested-lists'] as $propName ) {
295  $lists = $moduleProxy->$propName;
296  foreach ( $lists as $list ) {
297  foreach ( $list as $key => $value ) {
298  // We need the same filter as for 'lists',
299  // due to 'skinStyles'.
300  if ( is_int( $key ) ) {
301  $files[] = $value;
302  } else {
303  $files[] = $key;
304  }
305  }
306  }
307  }
308 
309  // Populate cases
310  foreach ( $files as $file ) {
311  $cases[] = [
312  $moduleProxy->getLocalPath( $file ),
313  $moduleName,
314  ( $file instanceof ResourceLoaderFilePath ? $file->getPath() : $file ),
315  ];
316  }
317 
318  // To populate missingLocalFileRefs. Not sure how sane this is inside this test...
319  $moduleProxy->readStyleFiles(
320  $module->getStyleFiles( $data['context'] ),
321  $module->getFlip( $data['context'] ),
322  $data['context']
323  );
324 
325  $missingLocalFileRefs = $moduleProxy->missingLocalFileRefs;
326 
327  foreach ( $missingLocalFileRefs as $file ) {
328  $cases[] = [
329  $file,
330  $moduleName,
331  $file,
332  ];
333  }
334  }
335 
336  return $cases;
337  }
338 }
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:216
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...