3 use MediaWiki\MediaWikiServices
;
4 use Psr\Log\LoggerInterface
;
6 abstract class ResourceLoaderTestCase
extends MediaWikiTestCase
{
7 // Version hash for a blank file module.
8 // Result of ResourceLoader::makeHash(), ResourceLoaderTestModule
9 // and ResourceLoaderFileModule::getDefinitionSummary().
10 const BLANK_VERSION
= '9p30q';
11 // Result of ResoureLoader::makeVersionQuery() for a blank file module.
12 // In other words, result of ResourceLoader::makeHash( BLANK_VERSION );
13 const BLANK_COMBI
= 'rbml8';
16 * @param array|string $options Language code or options array
17 * - string 'lang' Language code
18 * - string 'dir' Language direction (ltr or rtl)
19 * - string 'modules' Pipe-separated list of module names
20 * - string|null 'only' "scripts" (unwrapped script), "styles" (stylesheet), or null
21 * (mw.loader.implement).
22 * @param ResourceLoader|null $rl
23 * @return ResourceLoaderContext
25 protected function getResourceLoaderContext( $options = [], ResourceLoader
$rl = null ) {
26 if ( is_string( $options ) ) {
27 // Back-compat for extension tests
28 $options = [ 'lang' => $options ];
35 'modules' => 'startup',
39 $resourceLoader = $rl ?
: new ResourceLoader( MediaWikiServices
::getInstance()->getMainConfig() );
40 $request = new FauxRequest( [
41 'debug' => $options['debug'],
42 'lang' => $options['lang'],
43 'modules' => $options['modules'],
44 'only' => $options['only'],
45 'safemode' => $options['safemode'],
46 'skin' => $options['skin'],
47 'target' => 'phpunit',
49 $ctx = $this->getMockBuilder( ResourceLoaderContext
::class )
50 ->setConstructorArgs( [ $resourceLoader, $request ] )
51 ->setMethods( [ 'getDirection' ] )
53 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
57 public static function getSettings() {
59 // For ResourceLoader::inDebugMode since it doesn't have context
60 'ResourceLoaderDebug' => true,
62 // For ResourceLoaderStartUpModule and ResourceLoader::__construct()
64 'Script' => '/w/index.php',
65 'LoadScript' => '/w/load.php',
67 // For ResourceLoader::register() - TODO: Inject somehow T32956
68 'ResourceModuleSkinStyles' => [],
70 // For ResourceLoader::respond() - TODO: Inject somehow T32956
71 'UseFileCache' => false,
75 public static function getMinimalConfig() {
76 return new HashConfig( self
::getSettings() );
79 protected function setUp() {
82 ResourceLoader
::clearCache();
85 foreach ( self
::getSettings() as $key => $value ) {
86 $globals['wg' . $key] = $value;
88 $this->setMwGlobals( $globals );
94 class ResourceLoaderTestModule
extends ResourceLoaderModule
{
95 protected $messages = [];
96 protected $dependencies = [];
97 protected $group = null;
98 protected $source = 'local';
99 protected $script = '';
100 protected $styles = '';
101 protected $skipFunction = null;
102 protected $isRaw = false;
103 protected $isKnownEmpty = false;
104 protected $type = ResourceLoaderModule
::LOAD_GENERAL
;
105 protected $targets = [ 'phpunit' ];
106 protected $shouldEmbed = null;
107 protected $mayValidateScript = false;
109 public function __construct( $options = [] ) {
110 foreach ( $options as $key => $value ) {
111 $this->$key = $value;
115 public function getScript( ResourceLoaderContext
$context ) {
116 if ( $this->mayValidateScript
) {
117 // This enables the validation check that replaces invalid
118 // scripts with a warning message.
119 // Based on $wgResourceLoaderValidateJS
120 return $this->validateScriptFile( 'input', $this->script
);
122 return $this->script
;
126 public function getStyles( ResourceLoaderContext
$context ) {
127 return [ '' => $this->styles
];
130 public function getMessages() {
131 return $this->messages
;
134 public function getDependencies( ResourceLoaderContext
$context = null ) {
135 return $this->dependencies
;
138 public function getGroup() {
142 public function getSource() {
143 return $this->source
;
146 public function getType() {
150 public function getSkipFunction() {
151 return $this->skipFunction
;
154 public function isRaw() {
158 public function isKnownEmpty( ResourceLoaderContext
$context ) {
159 return $this->isKnownEmpty
;
162 public function shouldEmbedModule( ResourceLoaderContext
$context ) {
163 return $this->shouldEmbed ?? parent
::shouldEmbedModule( $context );
166 public function enableModuleContentVersion() {
172 * A more constrained and testable variant of ResourceLoaderFileModule.
174 * - Implements getLessVars() support.
175 * - Disables database persistance of discovered file dependencies.
177 class ResourceLoaderFileTestModule
extends ResourceLoaderFileModule
{
178 protected $lessVars = [];
180 public function __construct( $options = [] ) {
181 if ( isset( $options['lessVars'] ) ) {
182 $this->lessVars
= $options['lessVars'];
183 unset( $options['lessVars'] );
186 parent
::__construct( $options );
189 public function getLessVars( ResourceLoaderContext
$context ) {
190 return $this->lessVars
;
194 protected function getFileDependencies( ResourceLoaderContext
$context ) {
199 protected function saveFileDependencies( ResourceLoaderContext
$context, $refs ) {
204 class ResourceLoaderFileModuleTestingSubclass
extends ResourceLoaderFileModule
{
207 class EmptyResourceLoader
extends ResourceLoader
{
208 public function __construct( Config
$config = null, LoggerInterface
$logger = null ) {
209 parent
::__construct( $config ?
: ResourceLoaderTestCase
::getMinimalConfig(), $logger );
212 public function getErrors() {
213 return $this->errors
;