MediaWiki  1.32.5
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 
56  public function testIllegalDependencies() {
57  $data = self::getAllModules();
58 
59  $illegalDeps = [];
60  foreach ( $data['modules'] as $moduleName => $module ) {
61  if ( $module->isRaw() ) {
62  $illegalDeps[] = $moduleName;
63  }
64  }
65 
67  foreach ( $data['modules'] as $moduleName => $module ) {
68  foreach ( $illegalDeps as $illegalDep ) {
69  $this->assertNotContains(
70  $illegalDep,
71  $module->getDependencies( $data['context'] ),
72  "Module '$moduleName' must not depend on '$illegalDep'"
73  );
74  }
75  }
76  }
77 
81  public function testMissingDependencies() {
82  $data = self::getAllModules();
83  $validDeps = array_keys( $data['modules'] );
84 
86  foreach ( $data['modules'] as $moduleName => $module ) {
87  foreach ( $module->getDependencies( $data['context'] ) as $dep ) {
88  $this->assertContains(
89  $dep,
90  $validDeps,
91  "The module '$dep' required by '$moduleName' must exist"
92  );
93  }
94  }
95  }
96 
100  public function testMissingMessages() {
101  $data = self::getAllModules();
102  $lang = Language::factory( 'en' );
103 
105  foreach ( $data['modules'] as $moduleName => $module ) {
106  foreach ( $module->getMessages() as $msgKey ) {
107  $this->assertTrue(
108  wfMessage( $msgKey )->useDatabase( false )->inLanguage( $lang )->exists(),
109  "Message '$msgKey' required by '$moduleName' must exist"
110  );
111  }
112  }
113  }
114 
122  public function testUnsatisfiableDependencies() {
123  $data = self::getAllModules();
124 
126  foreach ( $data['modules'] as $moduleName => $module ) {
127  $moduleTargets = $module->getTargets();
128  foreach ( $module->getDependencies( $data['context'] ) as $dep ) {
129  if ( !isset( $data['modules'][$dep] ) ) {
130  // Missing dependencies reported by testMissingDependencies
131  continue;
132  }
133  $targets = $data['modules'][$dep]->getTargets();
134  foreach ( $moduleTargets as $moduleTarget ) {
135  $this->assertContains(
136  $moduleTarget,
137  $targets,
138  "The module '$moduleName' must not have target '$moduleTarget' "
139  . "because its dependency '$dep' does not have it"
140  );
141  }
142  }
143  }
144  }
145 
151  $basepath = __DIR__ . '/../data/css/';
152  $css = file_get_contents( $basepath . 'comments.css' );
153  $files = CSSMin::getLocalFileReferences( $css, $basepath );
154  $expected = [ $basepath . 'not-commented.gif' ];
155  $this->assertArrayEquals(
156  $expected,
157  $files,
158  'Url(...) expression in comment should be omitted.'
159  );
160  }
161 
166  protected static function getAllModules() {
168 
169  // Test existance of test suite files as well
170  // (can't use setUp or setMwGlobals because providers are static)
171  $org_wgEnableJavaScriptTest = $wgEnableJavaScriptTest;
173 
174  // Initialize ResourceLoader
175  $rl = new ResourceLoader();
176 
177  $modules = [];
178 
179  foreach ( $rl->getModuleNames() as $moduleName ) {
180  $modules[$moduleName] = $rl->getModule( $moduleName );
181  }
182 
183  // Restore settings
184  $wgEnableJavaScriptTest = $org_wgEnableJavaScriptTest;
185 
186  return [
187  'modules' => $modules,
188  'resourceloader' => $rl,
189  'context' => new ResourceLoaderContext( $rl, new FauxRequest() )
190  ];
191  }
192 
197  public static function provideMediaStylesheets() {
198  $data = self::getAllModules();
199  $cases = [];
200 
201  foreach ( $data['modules'] as $moduleName => $module ) {
202  if ( !$module instanceof ResourceLoaderFileModule ) {
203  continue;
204  }
205 
206  $reflectedModule = new ReflectionObject( $module );
207 
208  $getStyleFiles = $reflectedModule->getMethod( 'getStyleFiles' );
209  $getStyleFiles->setAccessible( true );
210 
211  $readStyleFile = $reflectedModule->getMethod( 'readStyleFile' );
212  $readStyleFile->setAccessible( true );
213 
214  $styleFiles = $getStyleFiles->invoke( $module, $data['context'] );
215 
216  $flip = $module->getFlip( $data['context'] );
217 
218  foreach ( $styleFiles as $media => $files ) {
219  if ( $media && $media !== 'all' ) {
220  foreach ( $files as $file ) {
221  $cases[] = [
222  $moduleName,
223  $media,
224  $file,
225  // XXX: Wrapped in an object to keep it out of PHPUnit output
226  (object)[
227  'cssText' => $readStyleFile->invoke(
228  $module,
229  $file,
230  $flip,
231  $data['context']
232  )
233  ],
234  ];
235  }
236  }
237  }
238  }
239 
240  return $cases;
241  }
242 
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  $reflectedModule = new ReflectionObject( $module );
277 
278  $files = [];
279 
280  foreach ( $filePathProps['lists'] as $propName ) {
281  $property = $reflectedModule->getProperty( $propName );
282  $property->setAccessible( true );
283  $list = $property->getValue( $module );
284  foreach ( $list as $key => $value ) {
285  // 'scripts' are numeral arrays.
286  // 'styles' can be numeral or associative.
287  // In case of associative the key is the file path
288  // and the value is the 'media' attribute.
289  if ( is_int( $key ) ) {
290  $files[] = $value;
291  } else {
292  $files[] = $key;
293  }
294  }
295  }
296 
297  foreach ( $filePathProps['nested-lists'] as $propName ) {
298  $property = $reflectedModule->getProperty( $propName );
299  $property->setAccessible( true );
300  $lists = $property->getValue( $module );
301  foreach ( $lists as $list ) {
302  foreach ( $list as $key => $value ) {
303  // We need the same filter as for 'lists',
304  // due to 'skinStyles'.
305  if ( is_int( $key ) ) {
306  $files[] = $value;
307  } else {
308  $files[] = $key;
309  }
310  }
311  }
312  }
313 
314  // Get method for resolving the paths to full paths
315  $method = $reflectedModule->getMethod( 'getLocalPath' );
316  $method->setAccessible( true );
317 
318  // Populate cases
319  foreach ( $files as $file ) {
320  $cases[] = [
321  $method->invoke( $module, $file ),
322  $moduleName,
323  ( $file instanceof ResourceLoaderFilePath ? $file->getPath() : $file ),
324  ];
325  }
326 
327  // To populate missingLocalFileRefs. Not sure how sane this is inside this test...
328  $module->readStyleFiles(
329  $module->getStyleFiles( $data['context'] ),
330  $module->getFlip( $data['context'] ),
331  $data['context']
332  );
333 
334  $property = $reflectedModule->getProperty( 'missingLocalFileRefs' );
335  $property->setAccessible( true );
336  $missingLocalFileRefs = $property->getValue( $module );
337 
338  foreach ( $missingLocalFileRefs as $file ) {
339  $cases[] = [
340  $file,
341  $moduleName,
342  $file,
343  ];
344  }
345  }
346 
347  return $cases;
348  }
349 }
ResourceLoaderContext
Object passed around to modules which contains information about the state of a specific loader reque...
Definition: ResourceLoaderContext.php:32
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
MediaWikiTestCase\assertArrayEquals
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Definition: MediaWikiTestCase.php:2036
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
ResourcesTest\testCommentedLocalFileReferences
testCommentedLocalFileReferences()
CSSMin::getLocalFileReferences should ignore url(...) expressions that have been commented out.
Definition: ResourcesTest.php:150
ResourceLoaderFilePath
An object to represent a path to a JavaScript/CSS file, along with a remote and local base path,...
Definition: ResourceLoaderFilePath.php:28
ResourcesTest
Definition: ResourcesTest.php:16
ResourcesTest\provideMediaStylesheets
static provideMediaStylesheets()
Get all stylesheet files from modules that are an instance of ResourceLoaderFileModule (or one of its...
Definition: ResourcesTest.php:197
php
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
ResourceLoaderFileModule
ResourceLoader module based on local JavaScript/CSS files.
Definition: ResourceLoaderFileModule.php:28
ResourcesTest\getAllModules
static getAllModules()
Get all registered modules from ResouceLoader.
Definition: ResourcesTest.php:166
ResourcesTest\testStyleMedia
testStyleMedia( $moduleName, $media, $filename, $css)
provideMediaStylesheets
Definition: ResourcesTest.php:30
$css
$css
Definition: styleTest.css.php:54
$property
$property
Definition: styleTest.css.php:48
MediaWikiTestCase
Definition: MediaWikiTestCase.php:16
$modules
$modules
Definition: HTMLFormElement.php:12
ResourcesTest\testMissingDependencies
testMissingDependencies()
Verify that all modules specified as dependencies of other modules actually exist.
Definition: ResourcesTest.php:81
$value
$value
Definition: styleTest.css.php:49
ResourcesTest\testIllegalDependencies
testIllegalDependencies()
Verify that nothing explicitly depends on raw modules (such as "query").
Definition: ResourcesTest.php:56
ResourcesTest\provideResourceFiles
static provideResourceFiles()
Get all resource files from modules that are an instance of ResourceLoaderFileModule (or one of its s...
Definition: ResourcesTest.php:250
ResourcesTest\testMissingMessages
testMissingMessages()
Verify that all specified messages actually exist.
Definition: ResourcesTest.php:100
ResourcesTest\testVersionHash
testVersionHash()
Definition: ResourcesTest.php:39
ResourcesTest\testUnsatisfiableDependencies
testUnsatisfiableDependencies()
Verify that all dependencies of all modules are always satisfiable with the 'targets' defined for the...
Definition: ResourcesTest.php:122
$wgEnableJavaScriptTest
$wgEnableJavaScriptTest
Allow running of javascript test suites via [[Special:JavaScriptTest]] (such as QUnit).
Definition: DefaultSettings.php:6508
as
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
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:214
object
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
wfMessage
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
ResourcesTest\testFileExistence
testFileExistence( $filename, $module, $resource)
provideResourceFiles
Definition: ResourcesTest.php:21
ResourceLoaderFilePath\getPath
getPath()
Definition: ResourceLoaderFilePath.php:68