MediaWiki  1.29.2
ResourcesTest.php
Go to the documentation of this file.
1 <?php
16 
20  public function testFileExistence( $filename, $module, $resource ) {
21  $this->assertFileExists( $filename,
22  "File '$resource' referenced by '$module' must exist."
23  );
24  }
25 
29  public function testStyleMedia( $moduleName, $media, $filename, $css ) {
30  $cssText = CSSMin::minify( $css->cssText );
31 
32  $this->assertTrue(
33  strpos( $cssText, '@media' ) === false,
34  'Stylesheets should not both specify "media" and contain @media'
35  );
36  }
37 
38  public function testVersionHash() {
39  $data = self::getAllModules();
40  foreach ( $data['modules'] as $moduleName => $module ) {
41  $version = $module->getVersionHash( $data['context'] );
42  $this->assertEquals( 7, strlen( $version ), "$moduleName must use ResourceLoader::makeHash" );
43  }
44  }
45 
53  public function testIllegalDependencies() {
54  $data = self::getAllModules();
55  $illegalDeps = [ 'jquery', 'mediawiki' ];
56 
58  foreach ( $data['modules'] as $moduleName => $module ) {
59  foreach ( $illegalDeps as $illegalDep ) {
60  $this->assertNotContains(
61  $illegalDep,
62  $module->getDependencies( $data['context'] ),
63  "Module '$moduleName' must not depend on '$illegalDep'"
64  );
65  }
66  }
67  }
68 
72  public function testMissingDependencies() {
73  $data = self::getAllModules();
74  $validDeps = array_keys( $data['modules'] );
75 
77  foreach ( $data['modules'] as $moduleName => $module ) {
78  foreach ( $module->getDependencies( $data['context'] ) as $dep ) {
79  $this->assertContains(
80  $dep,
81  $validDeps,
82  "The module '$dep' required by '$moduleName' must exist"
83  );
84  }
85  }
86  }
87 
91  public function testMissingMessages() {
92  $data = self::getAllModules();
93  $lang = Language::factory( 'en' );
94 
96  foreach ( $data['modules'] as $moduleName => $module ) {
97  foreach ( $module->getMessages() as $msgKey ) {
98  $this->assertTrue(
99  wfMessage( $msgKey )->useDatabase( false )->inLanguage( $lang )->exists(),
100  "Message '$msgKey' required by '$moduleName' must exist"
101  );
102  }
103  }
104  }
105 
113  public function testUnsatisfiableDependencies() {
114  $data = self::getAllModules();
115 
117  foreach ( $data['modules'] as $moduleName => $module ) {
118  $moduleTargets = $module->getTargets();
119  foreach ( $module->getDependencies( $data['context'] ) as $dep ) {
120  if ( !isset( $data['modules'][$dep] ) ) {
121  // Missing dependencies reported by testMissingDependencies
122  continue;
123  }
124  $targets = $data['modules'][$dep]->getTargets();
125  foreach ( $moduleTargets as $moduleTarget ) {
126  $this->assertContains(
127  $moduleTarget,
128  $targets,
129  "The module '$moduleName' must not have target '$moduleTarget' "
130  . "because its dependency '$dep' does not have it"
131  );
132  }
133  }
134  }
135  }
136 
142  $basepath = __DIR__ . '/../data/css/';
143  $css = file_get_contents( $basepath . 'comments.css' );
144  $files = CSSMin::getLocalFileReferences( $css, $basepath );
145  $expected = [ $basepath . 'not-commented.gif' ];
146  $this->assertArrayEquals(
147  $expected,
148  $files,
149  'Url(...) expression in comment should be omitted.'
150  );
151  }
152 
157  protected static function getAllModules() {
158  global $wgEnableJavaScriptTest;
159 
160  // Test existance of test suite files as well
161  // (can't use setUp or setMwGlobals because providers are static)
162  $org_wgEnableJavaScriptTest = $wgEnableJavaScriptTest;
163  $wgEnableJavaScriptTest = true;
164 
165  // Initialize ResourceLoader
166  $rl = new ResourceLoader();
167 
168  $modules = [];
169 
170  foreach ( $rl->getModuleNames() as $moduleName ) {
171  $modules[$moduleName] = $rl->getModule( $moduleName );
172  }
173 
174  // Restore settings
175  $wgEnableJavaScriptTest = $org_wgEnableJavaScriptTest;
176 
177  return [
178  'modules' => $modules,
179  'resourceloader' => $rl,
180  'context' => new ResourceLoaderContext( $rl, new FauxRequest() )
181  ];
182  }
183 
188  public static function provideMediaStylesheets() {
189  $data = self::getAllModules();
190  $cases = [];
191 
192  foreach ( $data['modules'] as $moduleName => $module ) {
193  if ( !$module instanceof ResourceLoaderFileModule ) {
194  continue;
195  }
196 
197  $reflectedModule = new ReflectionObject( $module );
198 
199  $getStyleFiles = $reflectedModule->getMethod( 'getStyleFiles' );
200  $getStyleFiles->setAccessible( true );
201 
202  $readStyleFile = $reflectedModule->getMethod( 'readStyleFile' );
203  $readStyleFile->setAccessible( true );
204 
205  $styleFiles = $getStyleFiles->invoke( $module, $data['context'] );
206 
207  $flip = $module->getFlip( $data['context'] );
208 
209  foreach ( $styleFiles as $media => $files ) {
210  if ( $media && $media !== 'all' ) {
211  foreach ( $files as $file ) {
212  $cases[] = [
213  $moduleName,
214  $media,
215  $file,
216  // XXX: Wrapped in an object to keep it out of PHPUnit output
217  (object)[
218  'cssText' => $readStyleFile->invoke(
219  $module,
220  $file,
221  $flip,
222  $data['context']
223  )
224  ],
225  ];
226  }
227  }
228  }
229  }
230 
231  return $cases;
232  }
233 
241  public static function provideResourceFiles() {
242  $data = self::getAllModules();
243  $cases = [];
244 
245  // See also ResourceLoaderFileModule::__construct
246  $filePathProps = [
247  // Lists of file paths
248  'lists' => [
249  'scripts',
250  'debugScripts',
251  'styles',
252  ],
253 
254  // Collated lists of file paths
255  'nested-lists' => [
256  'languageScripts',
257  'skinScripts',
258  'skinStyles',
259  ],
260  ];
261 
262  foreach ( $data['modules'] as $moduleName => $module ) {
263  if ( !$module instanceof ResourceLoaderFileModule ) {
264  continue;
265  }
266 
267  $reflectedModule = new ReflectionObject( $module );
268 
269  $files = [];
270 
271  foreach ( $filePathProps['lists'] as $propName ) {
272  $property = $reflectedModule->getProperty( $propName );
273  $property->setAccessible( true );
274  $list = $property->getValue( $module );
275  foreach ( $list as $key => $value ) {
276  // 'scripts' are numeral arrays.
277  // 'styles' can be numeral or associative.
278  // In case of associative the key is the file path
279  // and the value is the 'media' attribute.
280  if ( is_int( $key ) ) {
281  $files[] = $value;
282  } else {
283  $files[] = $key;
284  }
285  }
286  }
287 
288  foreach ( $filePathProps['nested-lists'] as $propName ) {
289  $property = $reflectedModule->getProperty( $propName );
290  $property->setAccessible( true );
291  $lists = $property->getValue( $module );
292  foreach ( $lists as $list ) {
293  foreach ( $list as $key => $value ) {
294  // We need the same filter as for 'lists',
295  // due to 'skinStyles'.
296  if ( is_int( $key ) ) {
297  $files[] = $value;
298  } else {
299  $files[] = $key;
300  }
301  }
302  }
303  }
304 
305  // Get method for resolving the paths to full paths
306  $method = $reflectedModule->getMethod( 'getLocalPath' );
307  $method->setAccessible( true );
308 
309  // Populate cases
310  foreach ( $files as $file ) {
311  $cases[] = [
312  $method->invoke( $module, $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  $module->readStyleFiles(
320  $module->getStyleFiles( $data['context'] ),
321  $module->getFlip( $data['context'] ),
322  $data['context']
323  );
324 
325  $property = $reflectedModule->getProperty( 'missingLocalFileRefs' );
326  $property->setAccessible( true );
327  $missingLocalFileRefs = $property->getValue( $module );
328 
329  foreach ( $missingLocalFileRefs as $file ) {
330  $cases[] = [
331  $file,
332  $moduleName,
333  $file,
334  ];
335  }
336  }
337 
338  return $cases;
339  }
340 }
ResourceLoaderContext
Object passed around to modules which contains information about the state of a specific loader reque...
Definition: ResourceLoaderContext.php:32
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 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
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:1498
CSSMin\minify
static minify( $css)
Removes whitespace from CSS data.
Definition: CSSMin.php:452
$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:141
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:15
ResourcesTest\provideMediaStylesheets
static provideMediaStylesheets()
Get all stylesheet files from modules that are an instance of ResourceLoaderFileModule (or one of its...
Definition: ResourcesTest.php:188
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:157
ResourcesTest\testStyleMedia
testStyleMedia( $moduleName, $media, $filename, $css)
provideMediaStylesheets
Definition: ResourcesTest.php:29
$css
$css
Definition: styleTest.css.php:50
$property
$property
Definition: styleTest.css.php:44
MediaWikiTestCase
Definition: MediaWikiTestCase.php:13
$modules
$modules
Definition: HTMLFormElement.php:12
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
ResourcesTest\testMissingDependencies
testMissingDependencies()
Verify that all modules specified as dependencies of other modules actually exist.
Definition: ResourcesTest.php:72
$value
$value
Definition: styleTest.css.php:45
CSSMin\getLocalFileReferences
static getLocalFileReferences( $source, $path)
Get a list of local files referenced in a stylesheet (includes non-existent files).
Definition: CSSMin.php:69
ResourcesTest\testIllegalDependencies
testIllegalDependencies()
Verify that nothing explicitly depends on the 'jquery' and 'mediawiki' modules.
Definition: ResourcesTest.php:53
ResourcesTest\provideResourceFiles
static provideResourceFiles()
Get all resource files from modules that are an instance of ResourceLoaderFileModule (or one of its s...
Definition: ResourcesTest.php:241
ResourcesTest\testMissingMessages
testMissingMessages()
Verify that all specified messages actually exist.
Definition: ResourcesTest.php:91
ResourcesTest\testVersionHash
testVersionHash()
Definition: ResourcesTest.php:38
ResourcesTest\testUnsatisfiableDependencies
testUnsatisfiableDependencies()
Verify that all dependencies of all modules are always satisfiable with the 'targets' defined for the...
Definition: ResourcesTest.php:113
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:183
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 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:20
ResourceLoaderFilePath\getPath
getPath()
Definition: ResourceLoaderFilePath.php:71