File: ResourcesTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (351 lines) | stat: -rw-r--r-- 9,948 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php

use JsonSchema\Validator;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Request\FauxRequest;
use MediaWiki\ResourceLoader as RL;
use Wikimedia\Minify\CSSMin;
use Wikimedia\TestingAccessWrapper;

/**
 * Checks for making sure registered resources are sensible.
 *
 * @author Antoine Musso
 * @author Niklas Laxström
 * @author Santhosh Thottingal
 * @copyright © 2012, Antoine Musso
 * @copyright © 2012, Niklas Laxström
 * @copyright © 2012, Santhosh Thottingal
 *
 * @coversNothing
 * @group Database
 */
class ResourcesTest extends MediaWikiIntegrationTestCase {

	public function testStyleMedia() {
		foreach ( self::provideMediaStylesheets() as [ $moduleName, $media, $filename, $css ] ) {
			$cssText = CSSMin::minify( $css->cssText );

			$this->assertStringNotContainsString(
				'@media',
				$cssText,
				'Stylesheets should not both specify "media" and contain @media'
			);
		}
	}

	/**
	 * Verify that all modules specified as dependencies of other modules actually
	 * exist and are not illegal.
	 *
	 * @todo Modules can dynamically choose dependencies based on context. This method
	 * does not find all such variations.
	 */
	public function testValidDependencies() {
		$data = self::getAllModules();
		$illegalDeps = [ 'startup' ];
		// Can't depend on modules in the `noscript` group, find all such module names
		// to add to $illegalDeps. See T291735
		/** @var RL\Module $module */
		foreach ( $data['modules'] as $moduleName => $module ) {
			if ( $module->getGroup() === 'noscript' ) {
				$illegalDeps[] = $moduleName;
			}
		}

		// Avoid an assert for each module to keep the test fast.
		// Instead, perform a single assertion against everything at once.
		// When all is good, actual/expected are both empty arrays.
		// When we find issues, add the violations to 'actual' and add an empty
		// key to 'expected'. These keys in expected are because the PHPUnit diff
		// (as of 6.5) only goes one level deep.
		$actualUnknown = [];
		$expectedUnknown = [];
		$actualIllegal = [];
		$expectedIllegal = [];

		/** @var RL\Module $module */
		foreach ( $data['modules'] as $moduleName => $module ) {
			foreach ( $module->getDependencies( $data['context'] ) as $dep ) {
				if ( !isset( $data['modules'][$dep] ) ) {
					$actualUnknown[$moduleName][] = $dep;
					$expectedUnknown[$moduleName] = [];
				}
				if ( in_array( $dep, $illegalDeps, true ) ) {
					$actualIllegal[$moduleName][] = $dep;
					$expectedIllegal[$moduleName] = [];
				}
			}
		}
		$this->assertEquals( $expectedUnknown, $actualUnknown, 'Dependencies that do not exist' );
		$this->assertEquals( $expectedIllegal, $actualIllegal, 'Dependencies that are not legal' );
	}

	public function testSchema() {
		$data = include __DIR__ . '/../../../resources/Resources.php';
		$schemaPath = __DIR__ . '/../../../docs/extension.schema.v2.json';

		// Replace inline functions with fake callables
		array_walk_recursive( $data, static function ( &$item, $key ) {
			if ( $item instanceof Closure ) {
				$item = 'Test::test';
			}
		} );
		// Convert PHP associative arrays to stdClass objects recursively
		$data = json_decode( json_encode( $data ) );

		$validator = new Validator;
		$validator->validate( $data, (object)[ '$ref' => 'file://' . $schemaPath . '#/properties/ResourceModules' ] );

		$this->assertEquals(
			[],
			$validator->getErrors(),
			'Found errors when validating Resources.php against the ResourceModules schema: ' .
				json_encode( $validator->getErrors(), JSON_PRETTY_PRINT )
		);
	}

	/**
	 * Verify that all specified messages actually exist.
	 */
	public function testMissingMessages() {
		$data = self::getAllModules();
		$lang = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' );

		/** @var RL\Module $module */
		foreach ( $data['modules'] as $moduleName => $module ) {
			foreach ( $module->getMessages() as $msgKey ) {
				$this->assertTrue(
					wfMessage( $msgKey )->useDatabase( false )->inLanguage( $lang )->exists(),
					"Message '$msgKey' required by '$moduleName' must exist"
				);
			}
		}
	}

	/**
	 * Get all registered modules from ResouceLoader.
	 * @return array
	 */
	protected static function getAllModules() {
		global $wgEnableJavaScriptTest;

		// Test existance of test suite files as well
		// (can't use setUp or setMwGlobals because providers are static)
		$org_wgEnableJavaScriptTest = $wgEnableJavaScriptTest;
		$wgEnableJavaScriptTest = true;

		// Get main ResourceLoader
		$rl = MediaWikiServices::getInstance()->getResourceLoader();

		$modules = [];

		foreach ( $rl->getModuleNames() as $moduleName ) {
			$modules[$moduleName] = $rl->getModule( $moduleName );
		}

		// Restore settings
		$wgEnableJavaScriptTest = $org_wgEnableJavaScriptTest;

		return [
			'modules' => $modules,
			'resourceloader' => $rl,
			'context' => new RL\Context( $rl, new FauxRequest() )
		];
	}

	/**
	 * Get all stylesheet files from modules that are an instance of
	 * RL\FileModule (or one of its subclasses).
	 */
	public static function provideMediaStylesheets() {
		$data = self::getAllModules();
		$context = $data['context'];

		foreach ( $data['modules'] as $moduleName => $module ) {
			if ( !$module instanceof RL\FileModule ) {
				continue;
			}

			$moduleProxy = TestingAccessWrapper::newFromObject( $module );

			$styleFiles = $moduleProxy->getStyleFiles( $context );

			foreach ( $styleFiles as $media => $files ) {
				if ( $media && $media !== 'all' ) {
					foreach ( $files as $file ) {
						yield [
							$moduleName,
							$media,
							$file,
							// XXX: Wrapped in an object to keep it out of PHPUnit output
							(object)[
								'cssText' => $moduleProxy->readStyleFile( $file, $context )
							],
						];
					}
				}
			}
		}
	}

	/**
	 * Check all resource files from RL\FileModule modules.
	 */
	public function testResourceFiles() {
		$this->overrideConfigValues( [
			MainConfigNames::Logo => false,
			MainConfigNames::Logos => [],
		] );

		$data = self::getAllModules();

		// See also RL\FileModule::__construct
		$filePathProps = [
			// Lists of file paths
			'lists' => [
				'scripts',
				'debugScripts',
				'styles',
				'packageFiles',
			],

			// Collated lists of file paths
			'nested-lists' => [
				'languageScripts',
				'skinScripts',
				'skinStyles',
			],
		];

		foreach ( $data['modules'] as $moduleName => $module ) {
			if ( !$module instanceof RL\FileModule ) {
				continue;
			}

			$moduleProxy = TestingAccessWrapper::newFromObject( $module );

			$files = [];

			foreach ( $filePathProps['lists'] as $propName ) {
				$list = $moduleProxy->$propName;
				if ( $list === null ) {
					continue;
				}
				foreach ( $list as $key => $value ) {
					// 'scripts' are numeral arrays.
					// 'styles' can be numeral or associative.
					// In case of associative the key is the file path
					// and the value is the 'media' attribute.
					if ( is_int( $key ) ) {
						$files[] = $value;
					} else {
						$files[] = $key;
					}
				}
			}

			foreach ( $filePathProps['nested-lists'] as $propName ) {
				$lists = $moduleProxy->$propName;
				foreach ( $lists as $list ) {
					foreach ( $list as $key => $value ) {
						// We need the same filter as for 'lists',
						// due to 'skinStyles'.
						if ( is_int( $key ) ) {
							$files[] = $value;
						} else {
							$files[] = $key;
						}
					}
				}
			}

			foreach ( $files as $key => $file ) {
				$fileInfo = $moduleProxy->expandFileInfo( $data['context'], $file, "files[$key]" );
				if ( !isset( $fileInfo['filePath'] ) ) {
					continue;
				}
				$relativePath = $fileInfo['filePath']->getPath();
				$localPath = $fileInfo['filePath']->getLocalPath();
				$this->assertFileExists(
					$localPath,
					"File '$relativePath' referenced by '$moduleName' must exist."
				);
			}

			// To populate missingLocalFileRefs. Not sure how sensible this is inside this test...
			$moduleProxy->readStyleFiles(
				$module->getStyleFiles( $data['context'] ),
				$data['context']
			);

			$missingLocalFileRefs = $moduleProxy->missingLocalFileRefs;

			foreach ( $missingLocalFileRefs as $file ) {
				$this->assertFileExists(
					$file,
					"File '$file' referenced by '$moduleName' must exist."
				);
			}
		}
	}

	/**
	 * Check all image files from RL\ImageModule modules.
	 */
	public function testImageFiles() {
		$data = self::getAllModules();

		foreach ( $data['modules'] as $moduleName => $module ) {
			if ( !$module instanceof RL\ImageModule ) {
				continue;
			}

			$imagesFiles = $module->getImages( $data['context'] );
			foreach ( $imagesFiles as $file ) {
				$relativePath = $file->getName();
				$this->assertFileExists(
					$file->getPath( $data['context'] ),
					"File '$relativePath' referenced by '$moduleName' must exist."
				);
			}
		}
	}

	public static function provideRespond() {
		$services = MediaWikiServices::getInstance();
		$rl = $services->getResourceLoader();
		$skinFactory = $services->getSkinFactory();
		foreach ( array_keys( $skinFactory->getInstalledSkins() ) as $skin ) {
			foreach ( $rl->getModuleNames() as $moduleName ) {
				yield [ $moduleName, $skin ];
			}
		}
	}

	/**
	 * @dataProvider provideRespond
	 * @param string $moduleName
	 * @param string $skin
	 */
	public function testRespond( $moduleName, $skin ) {
		$rl = $this->getServiceContainer()->getResourceLoader();
		$module = $rl->getModule( $moduleName );
		if ( $module->shouldSkipStructureTest() ) {
			// Private modules cannot be served from load.php
			$this->assertTrue( true );
			return;
		}
		// Test only general (scripts) or only=styles responses.
		$only = $module->getType() === RL\Module::LOAD_STYLES ? 'styles' : null;
		$context = new RL\Context(
			$rl,
			new FauxRequest( [ 'modules' => $moduleName, 'only' => $only, 'skin' => $skin ] )
		);
		ob_start();
		$rl->respond( $context );
		ob_end_clean();
		$this->assertSame( [], $rl->getErrors() );
	}
}