3 namespace MediaWiki\Tests\ResourceLoader
;
5 use MediaWiki\Config\Config
;
6 use MediaWiki\Config\HashConfig
;
7 use MediaWiki\MainConfigNames
;
8 use MediaWiki\Request\FauxRequest
;
9 use MediaWiki\ResourceLoader\Context
;
10 use MediaWiki\ResourceLoader\FileModule
;
11 use MediaWiki\ResourceLoader\Module
;
12 use MediaWiki\ResourceLoader\ResourceLoader
;
13 use MediaWikiIntegrationTestCase
;
14 use Psr\Log\LoggerInterface
;
16 abstract class ResourceLoaderTestCase
extends MediaWikiIntegrationTestCase
{
17 // Version hash for a blank file module.
18 // Result of ResourceLoader::makeHash(), ResourceLoaderTestModule
19 // and FileModule::getDefinitionSummary().
20 public const BLANK_VERSION
= 'dukpe';
21 // Result of ResourceLoader::makeVersionQuery() for a blank file module.
22 // In other words, result of ResourceLoader::makeHash( BLANK_VERSION );
23 public const BLANK_COMBI
= '1xz0a';
26 * @param array|string $options Language code or options array
27 * - string 'lang' Language code
28 * - string 'dir' Language direction (ltr or rtl)
29 * - string 'modules' Pipe-separated list of module names
30 * - string|null 'only' "scripts" (unwrapped script), "styles" (stylesheet), or null
31 * (mw.loader.implement).
32 * @param ResourceLoader|null $rl
35 protected function getResourceLoaderContext( $options = [], ?ResourceLoader
$rl = null ) {
36 if ( is_string( $options ) ) {
37 // Back-compat for extension tests
38 $options = [ 'lang' => $options ];
45 'modules' => 'startup',
50 $resourceLoader = $rl ?
: new ResourceLoader(
51 $this->getServiceContainer()->getMainConfig(),
55 'loadScript' => '/w/load.php',
58 $request = new FauxRequest( [
59 'debug' => $options['debug'],
60 'lang' => $options['lang'],
61 'modules' => $options['modules'],
62 'only' => $options['only'],
63 'safemode' => $options['safemode'],
64 'skin' => $options['skin'],
65 'sourcemap' => $options['sourcemap'],
66 'target' => 'phpunit',
68 $ctx = $this->getMockBuilder( Context
::class )
69 ->setConstructorArgs( [ $resourceLoader, $request ] )
70 ->onlyMethods( [ 'getDirection' ] )
72 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
76 public static function getSettings() {
78 // For ResourceLoader::respond
79 MainConfigNames
::ResourceLoaderEnableSourceMapLinks
=> false,
82 MainConfigNames
::ResourceLoaderValidateJS
=> false,
85 MainConfigNames
::Logos
=> false,
86 MainConfigNames
::Logo
=> '/logo.png',
87 MainConfigNames
::ResourceBasePath
=> '/w',
88 MainConfigNames
::ParserEnableLegacyMediaDOM
=> true,
90 // For ResourceLoader::getSiteConfigSettings and StartUpModule
91 MainConfigNames
::Server
=> 'https://example.org',
92 MainConfigNames
::ScriptPath
=> '/w',
93 MainConfigNames
::Script
=> '/w/index.php',
94 MainConfigNames
::ResourceLoaderEnableJSProfiler
=> false,
97 MainConfigNames
::CodexDevelopmentDir
=> null,
101 public static function getMinimalConfig() {
102 return new HashConfig( self
::getSettings() );
106 * The annotation causes this to be called immediately before setUp()
109 final protected function mediaWikiResourceLoaderSetUp(): void
{
110 ResourceLoader
::clearCache();
112 $this->overrideConfigValues( self
::getSettings() );
118 class ResourceLoaderTestModule
extends Module
{
120 protected $messages = [];
122 protected $dependencies = [];
123 /** @var string|null */
124 protected $group = null;
126 protected $source = 'local';
128 protected $script = '';
130 protected $styles = '';
131 /** @var string|null */
132 protected $skipFunction = null;
134 protected $isRaw = false;
136 protected $isKnownEmpty = false;
138 protected $type = Module
::LOAD_GENERAL
;
139 /** @var bool|null */
140 protected $shouldEmbed = null;
142 protected $mayValidateScript = false;
144 public function __construct( $options = [] ) {
145 foreach ( $options as $key => $value ) {
146 if ( $key === 'class' ||
$key === 'factory' ) {
149 $this->$key = $value;
153 public function getScript( Context
$context ) {
154 if ( $this->mayValidateScript
) {
155 // This enables the validation check that replaces invalid
156 // scripts with a warning message.
157 // Based on $wgResourceLoaderValidateJS
158 return $this->validateScriptFile( 'input', $this->script
);
160 return $this->script
;
164 public function getStyles( Context
$context ) {
165 return [ '' => $this->styles
];
168 public function getMessages() {
169 return $this->messages
;
172 public function getDependencies( ?Context
$context = null ) {
173 return $this->dependencies
;
176 public function getGroup() {
180 public function getSource() {
181 return $this->source
;
184 public function getType() {
188 public function getSkipFunction() {
189 return $this->skipFunction
;
192 public function requiresES6() {
196 public function isRaw() {
200 public function isKnownEmpty( Context
$context ) {
201 return $this->isKnownEmpty
;
204 public function shouldEmbedModule( Context
$context ) {
205 return $this->shouldEmbed ?? parent
::shouldEmbedModule( $context );
208 public function enableModuleContentVersion() {
214 * A more constrained and testable variant of FileModule.
216 * - Implements getLessVars() support.
217 * - Disables database persistance of discovered file dependencies.
219 class ResourceLoaderFileTestModule
extends FileModule
{
221 protected $lessVars = [];
223 public function __construct( $options = [] ) {
224 if ( isset( $options['lessVars'] ) ) {
225 $this->lessVars
= $options['lessVars'];
226 unset( $options['lessVars'] );
229 parent
::__construct( $options );
232 public function getLessVars( Context
$context ) {
233 return $this->lessVars
;
238 class ResourceLoaderFileModuleTestingSubclass
extends FileModule
{
241 class EmptyResourceLoader
extends ResourceLoader
{
242 public function __construct( ?Config
$config = null, ?LoggerInterface
$logger = null ) {
243 parent
::__construct( $config ?
: ResourceLoaderTestCase
::getMinimalConfig(), $logger );
247 /** @deprecated class alias since 1.42 */
248 class_alias( ResourceLoaderTestModule
::class, 'ResourceLoaderTestModule' );
250 /** @deprecated class alias since 1.42 */
251 class_alias( ResourceLoaderTestCase
::class, 'ResourceLoaderTestCase' );
253 /** @deprecated class alias since 1.42 */
254 class_alias( ResourceLoaderFileTestModule
::class, 'ResourceLoaderFileTestModule' );
256 /** @deprecated class alias since 1.42 */
257 class_alias( ResourceLoaderFileModuleTestingSubclass
::class, 'ResourceLoaderFileModuleTestingSubclass' );
259 /** @deprecated class alias since 1.42 */
260 class_alias( EmptyResourceLoader
::class, 'EmptyResourceLoader' );