Merge "Fix PHP CodeSniffer warnings and errors"
[lhc/web/wiklou.git] / tests / phpunit / structure / ResourcesTest.php
1 <?php
2 /**
3 * Sanity checks for making sure registered resources are sane.
4 *
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
14 *
15 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
16 */
17 class ResourcesTest extends MediaWikiTestCase {
18
19 /**
20 * @dataProvider provideResourceFiles
21 */
22 public function testFileExistence( $filename, $module, $resource ) {
23 $this->assertFileExists( $filename,
24 "File '$resource' referenced by '$module' must exist."
25 );
26 }
27
28 /**
29 * This ask the ResouceLoader for all registered files from modules
30 * created by ResourceLoaderFileModule (or one of its descendants).
31 *
32 *
33 * Since the raw data is stored in protected properties, we have to
34 * overrride this through ReflectionObject methods.
35 */
36 public static function provideResourceFiles() {
37 global $wgEnableJavaScriptTest;
38
39 // Test existance of test suite files as well
40 // (can't use setUp or setMwGlobals because providers are static)
41 $live_wgEnableJavaScriptTest = $wgEnableJavaScriptTest;
42 $wgEnableJavaScriptTest = true;
43
44 // Array with arguments for the test function
45 $cases = array();
46
47 // Initialize ResourceLoader
48 $rl = new ResourceLoader();
49
50 // See also ResourceLoaderFileModule::__construct
51 $filePathProps = array(
52 // Lists of file paths
53 'lists' => array(
54 'scripts',
55 'debugScripts',
56 'loaderScripts',
57 'styles',
58 ),
59
60 // Collated lists of file paths
61 'nested-lists' => array(
62 'languageScripts',
63 'skinScripts',
64 'skinStyles',
65 ),
66 );
67
68 foreach ( $rl->getModuleNames() as $moduleName ) {
69 $module = $rl->getModule( $moduleName );
70 if ( !$module instanceof ResourceLoaderFileModule ) {
71 continue;
72 }
73
74 $reflectedModule = new ReflectionObject( $module );
75
76 $files = array();
77
78 foreach ( $filePathProps['lists'] as $propName ) {
79 $property = $reflectedModule->getProperty( $propName );
80 $property->setAccessible( true );
81 $list = $property->getValue( $module );
82 foreach ( $list as $key => $value ) {
83 // 'scripts' are numeral arrays.
84 // 'styles' can be numeral or associative.
85 // In case of associative the key is the file path
86 // and the value is the 'media' attribute.
87 if ( is_int( $key ) ) {
88 $files[] = $value;
89 } else {
90 $files[] = $key;
91 }
92 }
93 }
94
95 foreach ( $filePathProps['nested-lists'] as $propName ) {
96 $property = $reflectedModule->getProperty( $propName );
97 $property->setAccessible( true );
98 $lists = $property->getValue( $module );
99 foreach ( $lists as $list ) {
100 foreach ( $list as $key => $value ) {
101 // We need the same filter as for 'lists',
102 // due to 'skinStyles'.
103 if ( is_int( $key ) ) {
104 $files[] = $value;
105 } else {
106 $files[] = $key;
107 }
108 }
109 }
110 }
111
112 // Get method for resolving the paths to full paths
113 $method = $reflectedModule->getMethod( 'getLocalPath' );
114 $method->setAccessible( true );
115
116 // Populate cases
117 foreach ( $files as $file ) {
118 $cases[] = array(
119 $method->invoke( $module, $file ),
120 $module->getName(),
121 $file,
122 );
123 }
124 }
125
126 // Restore settings
127 $wgEnableJavaScriptTest = $live_wgEnableJavaScriptTest;
128
129 return $cases;
130 }
131 }