3 * Sanity checks for making sure registered resources are sane.
6 * @author Antoine Musso
7 * @author Niklas Laxström
8 * @author Santhosh Thottingal
10 * @copyright © 2012, Antoine Musso
11 * @copyright © 2012, Niklas Laxström
12 * @copyright © 2012, Santhosh Thottingal
13 * @copyright © 2012, Timo Tijhof
16 class ResourcesTest
extends MediaWikiTestCase
{
19 * @dataProvider provideResourceFiles
21 public function testFileExistence( $filename, $module, $resource ) {
22 $this->assertFileExists( $filename,
23 "File '$resource' referenced by '$module' must exist."
28 * @dataProvider provideMediaStylesheets
30 public function testStyleMedia( $moduleName, $media, $filename, $css ) {
31 $cssText = CSSMin
::minify( $css->cssText
);
34 strpos( $cssText, '@media' ) === false,
35 'Stylesheets should not both specify "media" and contain @media'
39 public function testVersionHash() {
40 $data = self
::getAllModules();
41 foreach ( $data['modules'] as $moduleName => $module ) {
42 $version = $module->getVersionHash( $data['context'] );
43 $this->assertEquals( 8, strlen( $version ), "$moduleName must use ResourceLoader::makeHash" );
48 * Verify that nothing explicitly depends on the 'jquery' and 'mediawiki' modules.
49 * They are always loaded, depending on them is unsupported and leads to unexpected behaviour.
50 * TODO Modules can dynamically choose dependencies based on context. This method does not
51 * test such dependencies. The same goes for testMissingDependencies() and
52 * testUnsatisfiableDependencies().
54 public function testIllegalDependencies() {
55 $data = self
::getAllModules();
56 $illegalDeps = array( 'jquery', 'mediawiki' );
58 /** @var ResourceLoaderModule $module */
59 foreach ( $data['modules'] as $moduleName => $module ) {
60 foreach ( $illegalDeps as $illegalDep ) {
61 $this->assertNotContains(
63 $module->getDependencies( $data['context'] ),
64 "Module '$moduleName' must not depend on '$illegalDep'"
71 * Verify that all modules specified as dependencies of other modules actually exist.
73 public function testMissingDependencies() {
74 $data = self
::getAllModules();
75 $validDeps = array_keys( $data['modules'] );
77 /** @var ResourceLoaderModule $module */
78 foreach ( $data['modules'] as $moduleName => $module ) {
79 foreach ( $module->getDependencies( $data['context'] ) as $dep ) {
80 $this->assertContains(
83 "The module '$dep' required by '$moduleName' must exist"
90 * Verify that all dependencies of all modules are always satisfiable with the 'targets' defined
91 * for the involved modules.
93 * Example: A depends on B. A has targets: mobile, desktop. B has targets: desktop. Therefore the
94 * dependency is sometimes unsatisfiable: it's impossible to load module A on mobile.
96 public function testUnsatisfiableDependencies() {
97 $data = self
::getAllModules();
98 $validDeps = array_keys( $data['modules'] );
100 /** @var ResourceLoaderModule $module */
101 foreach ( $data['modules'] as $moduleName => $module ) {
102 $moduleTargets = $module->getTargets();
103 foreach ( $module->getDependencies( $data['context'] ) as $dep ) {
104 if ( !isset( $data['modules'][$dep] ) ) {
105 // Missing dependencies reported by testMissingDependencies
108 $targets = $data['modules'][$dep]->getTargets();
109 foreach ( $moduleTargets as $moduleTarget ) {
110 $this->assertContains(
113 "The module '$moduleName' must not have target '$moduleTarget' "
114 . "because its dependency '$dep' does not have it"
122 * CSSMin::getAllLocalFileReferences should ignore url(...) expressions
123 * that have been commented out.
125 public function testCommentedLocalFileReferences() {
126 $basepath = __DIR__
. '/../data/css/';
127 $css = file_get_contents( $basepath . 'comments.css' );
128 $files = CSSMin
::getAllLocalFileReferences( $css, $basepath );
129 $expected = array( $basepath . 'not-commented.gif' );
130 $this->assertArrayEquals(
133 'Url(...) expression in comment should be omitted.'
138 * Get all registered modules from ResouceLoader.
141 protected static function getAllModules() {
142 global $wgEnableJavaScriptTest;
144 // Test existance of test suite files as well
145 // (can't use setUp or setMwGlobals because providers are static)
146 $org_wgEnableJavaScriptTest = $wgEnableJavaScriptTest;
147 $wgEnableJavaScriptTest = true;
149 // Initialize ResourceLoader
150 $rl = new ResourceLoader();
154 foreach ( $rl->getModuleNames() as $moduleName ) {
155 $modules[$moduleName] = $rl->getModule( $moduleName );
159 $wgEnableJavaScriptTest = $org_wgEnableJavaScriptTest;
162 'modules' => $modules,
163 'resourceloader' => $rl,
164 'context' => new ResourceLoaderContext( $rl, new FauxRequest() )
169 * Get all stylesheet files from modules that are an instance of
170 * ResourceLoaderFileModule (or one of its subclasses).
172 public static function provideMediaStylesheets() {
173 $data = self
::getAllModules();
176 foreach ( $data['modules'] as $moduleName => $module ) {
177 if ( !$module instanceof ResourceLoaderFileModule
) {
181 $reflectedModule = new ReflectionObject( $module );
183 $getStyleFiles = $reflectedModule->getMethod( 'getStyleFiles' );
184 $getStyleFiles->setAccessible( true );
186 $readStyleFile = $reflectedModule->getMethod( 'readStyleFile' );
187 $readStyleFile->setAccessible( true );
189 $styleFiles = $getStyleFiles->invoke( $module, $data['context'] );
191 $flip = $module->getFlip( $data['context'] );
193 foreach ( $styleFiles as $media => $files ) {
194 if ( $media && $media !== 'all' ) {
195 foreach ( $files as $file ) {
200 // XXX: Wrapped in an object to keep it out of PHPUnit output
202 'cssText' => $readStyleFile->invoke(
219 * Get all resource files from modules that are an instance of
220 * ResourceLoaderFileModule (or one of its subclasses).
222 * Since the raw data is stored in protected properties, we have to
223 * overrride this through ReflectionObject methods.
225 public static function provideResourceFiles() {
226 $data = self
::getAllModules();
229 // See also ResourceLoaderFileModule::__construct
230 $filePathProps = array(
231 // Lists of file paths
238 // Collated lists of file paths
239 'nested-lists' => array(
246 foreach ( $data['modules'] as $moduleName => $module ) {
247 if ( !$module instanceof ResourceLoaderFileModule
) {
251 $reflectedModule = new ReflectionObject( $module );
255 foreach ( $filePathProps['lists'] as $propName ) {
256 $property = $reflectedModule->getProperty( $propName );
257 $property->setAccessible( true );
258 $list = $property->getValue( $module );
259 foreach ( $list as $key => $value ) {
260 // 'scripts' are numeral arrays.
261 // 'styles' can be numeral or associative.
262 // In case of associative the key is the file path
263 // and the value is the 'media' attribute.
264 if ( is_int( $key ) ) {
272 foreach ( $filePathProps['nested-lists'] as $propName ) {
273 $property = $reflectedModule->getProperty( $propName );
274 $property->setAccessible( true );
275 $lists = $property->getValue( $module );
276 foreach ( $lists as $list ) {
277 foreach ( $list as $key => $value ) {
278 // We need the same filter as for 'lists',
279 // due to 'skinStyles'.
280 if ( is_int( $key ) ) {
289 // Get method for resolving the paths to full paths
290 $method = $reflectedModule->getMethod( 'getLocalPath' );
291 $method->setAccessible( true );
294 foreach ( $files as $file ) {
296 $method->invoke( $module, $file ),
298 ( $file instanceof ResourceLoaderFilePath ?
$file->getPath() : $file ),
302 // To populate missingLocalFileRefs. Not sure how sane this is inside this test...
303 $module->readStyleFiles(
304 $module->getStyleFiles( $data['context'] ),
305 $module->getFlip( $data['context'] ),
309 $property = $reflectedModule->getProperty( 'missingLocalFileRefs' );
310 $property->setAccessible( true );
311 $missingLocalFileRefs = $property->getValue( $module );
313 foreach ( $missingLocalFileRefs as $file ) {