MediaWiki  master
ResourceLoaderTestCase.php
Go to the documentation of this file.
1 <?php
2 
5 
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 = '09p30q0';
11 
22  protected function getResourceLoaderContext( $options = [], ResourceLoader $rl = null ) {
23  if ( is_string( $options ) ) {
24  // Back-compat for extension tests
25  $options = [ 'lang' => $options ];
26  }
27  $options += [
28  'debug' => 'true',
29  'lang' => 'en',
30  'dir' => 'ltr',
31  'skin' => 'fallback',
32  'modules' => 'startup',
33  'only' => 'scripts',
34  'safemode' => null,
35  ];
36  $resourceLoader = $rl ?: new ResourceLoader( MediaWikiServices::getInstance()->getMainConfig() );
37  $request = new FauxRequest( [
38  'debug' => $options['debug'],
39  'lang' => $options['lang'],
40  'modules' => $options['modules'],
41  'only' => $options['only'],
42  'safemode' => $options['safemode'],
43  'skin' => $options['skin'],
44  'target' => 'phpunit',
45  ] );
46  $ctx = $this->getMockBuilder( ResourceLoaderContext::class )
47  ->setConstructorArgs( [ $resourceLoader, $request ] )
48  ->setMethods( [ 'getDirection' ] )
49  ->getMock();
50  $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
51  return $ctx;
52  }
53 
54  public static function getSettings() {
55  return [
56  // For ResourceLoader::inDebugMode since it doesn't have context
57  'ResourceLoaderDebug' => true,
58 
59  // For ResourceLoaderStartUpModule and ResourceLoader::__construct()
60  'ScriptPath' => '/w',
61  'Script' => '/w/index.php',
62  'LoadScript' => '/w/load.php',
63 
64  // For ResourceLoader::register() - TODO: Inject somehow T32956
65  'ResourceModuleSkinStyles' => [],
66 
67  // For ResourceLoader::respond() - TODO: Inject somehow T32956
68  'UseFileCache' => false,
69  ];
70  }
71 
72  public static function getMinimalConfig() {
73  return new HashConfig( self::getSettings() );
74  }
75 
76  protected function setUp() {
77  parent::setUp();
78 
80 
81  $globals = [];
82  foreach ( self::getSettings() as $key => $value ) {
83  $globals['wg' . $key] = $value;
84  }
85  $this->setMwGlobals( $globals );
86  }
87 }
88 
89 /* Stubs */
90 
92  protected $messages = [];
93  protected $dependencies = [];
94  protected $group = null;
95  protected $source = 'local';
96  protected $script = '';
97  protected $styles = '';
98  protected $skipFunction = null;
99  protected $isRaw = false;
100  protected $isKnownEmpty = false;
102  protected $targets = [ 'phpunit' ];
103  protected $shouldEmbed = null;
104  protected $mayValidateScript = false;
105 
106  public function __construct( $options = [] ) {
107  foreach ( $options as $key => $value ) {
108  $this->$key = $value;
109  }
110  }
111 
113  if ( $this->mayValidateScript ) {
114  // This enables the validation check that replaces invalid
115  // scripts with a warning message.
116  // Based on $wgResourceLoaderValidateJS
117  return $this->validateScriptFile( 'input', $this->script );
118  } else {
119  return $this->script;
120  }
121  }
122 
124  return [ '' => $this->styles ];
125  }
126 
127  public function getMessages() {
128  return $this->messages;
129  }
130 
132  return $this->dependencies;
133  }
134 
135  public function getGroup() {
136  return $this->group;
137  }
138 
139  public function getSource() {
140  return $this->source;
141  }
142 
143  public function getType() {
144  return $this->type;
145  }
146 
147  public function getSkipFunction() {
148  return $this->skipFunction;
149  }
150 
151  public function isRaw() {
152  return $this->isRaw;
153  }
154 
156  return $this->isKnownEmpty;
157  }
158 
160  return $this->shouldEmbed ?? parent::shouldEmbedModule( $context );
161  }
162 
163  public function enableModuleContentVersion() {
164  return true;
165  }
166 }
167 
175  protected $lessVars = [];
176 
177  public function __construct( $options = [] ) {
178  if ( isset( $options['lessVars'] ) ) {
179  $this->lessVars = $options['lessVars'];
180  unset( $options['lessVars'] );
181  }
182 
183  parent::__construct( $options );
184  }
185 
187  return $this->lessVars;
188  }
189 
192  // No-op
193  return [];
194  }
195 
196  protected function saveFileDependencies( ResourceLoaderContext $context, $refs ) {
197  // No-op
198  }
199 }
200 
202 }
203 
205  public function __construct( Config $config = null, LoggerInterface $logger = null ) {
206  parent::__construct( $config ?: ResourceLoaderTestCase::getMinimalConfig(), $logger );
207  }
208 
209  public function getErrors() {
210  return $this->errors;
211  }
212 }
__construct(Config $config=null, LoggerInterface $logger=null)
getDependencies(ResourceLoaderContext $context=null)
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
getFileDependencies(ResourceLoaderContext $context)
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext such as when responding to a resource loader request or generating HTML output & $resourceLoader
Definition: hooks.txt:2620
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$source
$value
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
isKnownEmpty(ResourceLoaderContext $context)
shouldEmbedModule(ResourceLoaderContext $context)
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
Interface for configuration instances.
Definition: Config.php:28
ResourceLoader module based on local JavaScript/CSS files.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1971
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
getScript(ResourceLoaderContext $context)
getStyles(ResourceLoaderContext $context)
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
saveFileDependencies(ResourceLoaderContext $context, $refs)
A more constrained and testable variant of ResourceLoaderFileModule.
static clearCache()
Reset static members used for caching.
getLessVars(ResourceLoaderContext $context)
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2620
$messages
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2620
Object passed around to modules which contains information about the state of a specific loader reque...