Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / structure / ResourcesTest.php
blob3282665b672e64ae35563698c3d65d5202957b91
1 <?php
2 /**
3 * Sanity checks for making sure registered resources are sane.
5 * @file
6 * @author Antoine Musso
7 * @author Niklas Laxström
8 * @author Santhosh Thottingal
9 * @author Timo Tijhof
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 {
18 /**
19 * @dataProvider provideResourceFiles
21 public function testFileExistence( $filename, $module, $resource ) {
22 $this->assertFileExists( $filename,
23 "File '$resource' referenced by '$module' must exist."
27 /**
28 * @dataProvider provideMediaStylesheets
30 public function testStyleMedia( $moduleName, $media, $filename, $css ) {
31 $cssText = CSSMin::minify( $css->cssText );
33 $this->assertTrue(
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" );
47 /**
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(
62 $illegalDep,
63 $module->getDependencies( $data['context'] ),
64 "Module '$moduleName' must not depend on '$illegalDep'"
70 /**
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(
81 $dep,
82 $validDeps,
83 "The module '$dep' required by '$moduleName' must exist"
89 /**
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
106 continue;
108 $targets = $data['modules'][$dep]->getTargets();
109 foreach ( $moduleTargets as $moduleTarget ) {
110 $this->assertContains(
111 $moduleTarget,
112 $targets,
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(
131 $expected,
132 $files,
133 'Url(...) expression in comment should be omitted.'
138 * Get all registered modules from ResouceLoader.
139 * @return array
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();
152 $modules = array();
154 foreach ( $rl->getModuleNames() as $moduleName ) {
155 $modules[$moduleName] = $rl->getModule( $moduleName );
158 // Restore settings
159 $wgEnableJavaScriptTest = $org_wgEnableJavaScriptTest;
161 return array(
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();
174 $cases = array();
176 foreach ( $data['modules'] as $moduleName => $module ) {
177 if ( !$module instanceof ResourceLoaderFileModule ) {
178 continue;
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 ) {
196 $cases[] = array(
197 $moduleName,
198 $media,
199 $file,
200 // XXX: Wrapped in an object to keep it out of PHPUnit output
201 (object)array(
202 'cssText' => $readStyleFile->invoke(
203 $module,
204 $file,
205 $flip,
206 $data['context']
215 return $cases;
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();
227 $cases = array();
229 // See also ResourceLoaderFileModule::__construct
230 $filePathProps = array(
231 // Lists of file paths
232 'lists' => array(
233 'scripts',
234 'debugScripts',
235 'styles',
238 // Collated lists of file paths
239 'nested-lists' => array(
240 'languageScripts',
241 'skinScripts',
242 'skinStyles',
246 foreach ( $data['modules'] as $moduleName => $module ) {
247 if ( !$module instanceof ResourceLoaderFileModule ) {
248 continue;
251 $reflectedModule = new ReflectionObject( $module );
253 $files = array();
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 ) ) {
265 $files[] = $value;
266 } else {
267 $files[] = $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 ) ) {
281 $files[] = $value;
282 } else {
283 $files[] = $key;
289 // Get method for resolving the paths to full paths
290 $method = $reflectedModule->getMethod( 'getLocalPath' );
291 $method->setAccessible( true );
293 // Populate cases
294 foreach ( $files as $file ) {
295 $cases[] = array(
296 $method->invoke( $module, $file ),
297 $moduleName,
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'] ),
306 $data['context']
309 $property = $reflectedModule->getProperty( 'missingLocalFileRefs' );
310 $property->setAccessible( true );
311 $missingLocalFileRefs = $property->getValue( $module );
313 foreach ( $missingLocalFileRefs as $file ) {
314 $cases[] = array(
315 $file,
316 $moduleName,
317 $file,
322 return $cases;