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
15 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
17 class ResourcesTest
extends MediaWikiTestCase
{
20 * @dataProvider provideResourceFiles
22 public function testFileExistence( $filename, $module, $resource ) {
23 $this->assertFileExists( $filename,
24 "File '$resource' referenced by '$module' must exist."
29 * @dataProvider provideMediaStylesheets
31 public function testStyleMedia( $moduleName, $media, $filename, $css ) {
32 $cssText = CSSMin
::minify( $css->cssText
);
34 $this->assertTrue( strpos( $cssText, '@media' ) === false, 'Stylesheets should not both specify "media" and contain @media' );
37 public function testDependencies() {
38 $data = self
::getAllModules();
39 $illegalDeps = array( 'jquery', 'mediawiki' );
41 foreach ( $data['modules'] as $moduleName => $module ) {
42 foreach ( $illegalDeps as $illegalDep ) {
43 $this->assertNotContains(
45 $module->getDependencies(),
46 "Module '$moduleName' must not depend on '$illegalDep'"
53 * Get all registered modules from ResouceLoader.
55 protected static function getAllModules() {
56 global $wgEnableJavaScriptTest;
58 // Test existance of test suite files as well
59 // (can't use setUp or setMwGlobals because providers are static)
60 $org_wgEnableJavaScriptTest = $wgEnableJavaScriptTest;
61 $wgEnableJavaScriptTest = true;
63 // Initialize ResourceLoader
64 $rl = new ResourceLoader();
68 foreach ( $rl->getModuleNames() as $moduleName ) {
69 $modules[$moduleName] = $rl->getModule( $moduleName );
73 $wgEnableJavaScriptTest = $org_wgEnableJavaScriptTest;
76 'modules' => $modules,
77 'resourceloader' => $rl,
78 'context' => new ResourceLoaderContext( $rl, new FauxRequest() )
83 * Get all stylesheet files from modules that are an instance of
84 * ResourceLoaderFileModule (or one of its subclasses).
86 public static function provideMediaStylesheets() {
87 $data = self
::getAllModules();
90 foreach ( $data['modules'] as $moduleName => $module ) {
91 if ( !$module instanceof ResourceLoaderFileModule
) {
95 $reflectedModule = new ReflectionObject( $module );
97 $getStyleFiles = $reflectedModule->getMethod( 'getStyleFiles' );
98 $getStyleFiles->setAccessible( true );
100 $readStyleFile = $reflectedModule->getMethod( 'readStyleFile' );
101 $readStyleFile->setAccessible( true );
103 $styleFiles = $getStyleFiles->invoke( $module, $data['context'] );
105 $flip = $module->getFlip( $data['context'] );
107 foreach ( $styleFiles as $media => $files ) {
108 if ( $media && $media !== 'all' ) {
109 foreach ( $files as $file ) {
114 // XXX: Wrapped in an object to keep it out of PHPUnit output
115 (object) array( 'cssText' => $readStyleFile->invoke( $module, $file, $flip ) ),
126 * Get all resource files from modules that are an instance of
127 * ResourceLoaderFileModule (or one of its subclasses).
129 * Since the raw data is stored in protected properties, we have to
130 * overrride this through ReflectionObject methods.
132 public static function provideResourceFiles() {
133 $data = self
::getAllModules();
136 // See also ResourceLoaderFileModule::__construct
137 $filePathProps = array(
138 // Lists of file paths
146 // Collated lists of file paths
147 'nested-lists' => array(
154 foreach ( $data['modules'] as $moduleName => $module ) {
155 if ( !$module instanceof ResourceLoaderFileModule
) {
159 $reflectedModule = new ReflectionObject( $module );
163 foreach ( $filePathProps['lists'] as $propName ) {
164 $property = $reflectedModule->getProperty( $propName );
165 $property->setAccessible( true );
166 $list = $property->getValue( $module );
167 foreach ( $list as $key => $value ) {
168 // 'scripts' are numeral arrays.
169 // 'styles' can be numeral or associative.
170 // In case of associative the key is the file path
171 // and the value is the 'media' attribute.
172 if ( is_int( $key ) ) {
180 foreach ( $filePathProps['nested-lists'] as $propName ) {
181 $property = $reflectedModule->getProperty( $propName );
182 $property->setAccessible( true );
183 $lists = $property->getValue( $module );
184 foreach ( $lists as $list ) {
185 foreach ( $list as $key => $value ) {
186 // We need the same filter as for 'lists',
187 // due to 'skinStyles'.
188 if ( is_int( $key ) ) {
197 // Get method for resolving the paths to full paths
198 $method = $reflectedModule->getMethod( 'getLocalPath' );
199 $method->setAccessible( true );
202 foreach ( $files as $file ) {
204 $method->invoke( $module, $file ),