MediaWiki  1.32.0
ResourceLoaderTestCase.php
Go to the documentation of this file.
1 <?php
2 
4 use Psr\Log\LoggerInterface;
5 use Psr\Log\NullLogger;
6 
7 abstract class ResourceLoaderTestCase extends MediaWikiTestCase {
8  // Version hash for a blank file module.
9  // Result of ResourceLoader::makeHash(), ResourceLoaderTestModule
10  // and ResourceLoaderFileModule::getDefinitionSummary().
11  const BLANK_VERSION = '09p30q0';
12 
23  protected function getResourceLoaderContext( $options = [], ResourceLoader $rl = null ) {
24  if ( is_string( $options ) ) {
25  // Back-compat for extension tests
26  $options = [ 'lang' => $options ];
27  }
28  $options += [
29  'lang' => 'en',
30  'dir' => 'ltr',
31  'skin' => 'vector',
32  'modules' => 'startup',
33  'only' => 'scripts',
34  'safemode' => null,
35  ];
36  $resourceLoader = $rl ?: new ResourceLoader();
37  $request = new FauxRequest( [
38  'lang' => $options['lang'],
39  'modules' => $options['modules'],
40  'only' => $options['only'],
41  'safemode' => $options['safemode'],
42  'skin' => $options['skin'],
43  'target' => 'phpunit',
44  ] );
45  $ctx = $this->getMockBuilder( ResourceLoaderContext::class )
46  ->setConstructorArgs( [ $resourceLoader, $request ] )
47  ->setMethods( [ 'getDirection' ] )
48  ->getMock();
49  $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
50  return $ctx;
51  }
52 
53  public static function getSettings() {
54  return [
55  // For ResourceLoader::inDebugMode since it doesn't have context
56  'ResourceLoaderDebug' => true,
57 
58  // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
59  'CacheEpoch' => '20140101000000',
60 
61  // For ResourceLoader::__construct()
62  'ResourceLoaderSources' => [],
63 
64  // For wfScript()
65  'ScriptPath' => '/w',
66  'Script' => '/w/index.php',
67  'LoadScript' => '/w/load.php',
68  ];
69  }
70 
71  protected function setUp() {
72  parent::setUp();
73 
74  ResourceLoader::clearCache();
75 
76  $globals = [];
77  foreach ( self::getSettings() as $key => $value ) {
78  $globals['wg' . $key] = $value;
79  }
80  $this->setMwGlobals( $globals );
81  }
82 }
83 
84 /* Stubs */
85 
87  protected $messages = [];
88  protected $dependencies = [];
89  protected $group = null;
90  protected $source = 'local';
91  protected $script = '';
92  protected $styles = '';
93  protected $skipFunction = null;
94  protected $isRaw = false;
95  protected $isKnownEmpty = false;
97  protected $targets = [ 'phpunit' ];
98  protected $shouldEmbed = null;
99 
100  public function __construct( $options = [] ) {
101  foreach ( $options as $key => $value ) {
102  $this->$key = $value;
103  }
104  }
105 
107  return $this->validateScriptFile( 'input', $this->script );
108  }
109 
111  return [ '' => $this->styles ];
112  }
113 
114  public function getMessages() {
115  return $this->messages;
116  }
117 
118  public function getDependencies( ResourceLoaderContext $context = null ) {
119  return $this->dependencies;
120  }
121 
122  public function getGroup() {
123  return $this->group;
124  }
125 
126  public function getSource() {
127  return $this->source;
128  }
129 
130  public function getType() {
131  return $this->type;
132  }
133 
134  public function getSkipFunction() {
135  return $this->skipFunction;
136  }
137 
138  public function isRaw() {
139  return $this->isRaw;
140  }
142  return $this->isKnownEmpty;
143  }
144 
146  return $this->shouldEmbed ?? parent::shouldEmbedModule( $context );
147  }
148 
149  public function enableModuleContentVersion() {
150  return true;
151  }
152 }
153 
155  protected $lessVars = [];
156 
157  public function __construct( $options = [], $test = [] ) {
158  parent::__construct( $options );
159 
160  foreach ( $test as $key => $value ) {
161  $this->$key = $value;
162  }
163  }
164 
166  return $this->lessVars;
167  }
168 }
169 
171 }
172 
173 class EmptyResourceLoader extends ResourceLoader {
174  // TODO: This won't be needed once ResourceLoader is empty by default
175  // and default registrations are done from ServiceWiring instead.
176  public function __construct( Config $config = null, LoggerInterface $logger = null ) {
177  $this->setLogger( $logger ?: new NullLogger() );
178  $this->config = $config ?: MediaWikiServices::getInstance()->getMainConfig();
179  // Source "local" is required by StartupModule
180  $this->addSource( 'local', $this->config->get( 'LoadScript' ) );
181  $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
182  }
183 
184  public function getErrors() {
185  return $this->errors;
186  }
187 }
ResourceLoaderTestModule\enableModuleContentVersion
enableModuleContentVersion()
Whether to generate version hash based on module content.
Definition: ResourceLoaderTestCase.php:149
ResourceLoaderModule\LOAD_GENERAL
const LOAD_GENERAL
Definition: ResourceLoaderModule.php:45
ResourceLoaderTestCase\setUp
setUp()
Definition: ResourceLoaderTestCase.php:71
ResourceLoaderContext
Object passed around to modules which contains information about the state of a specific loader reque...
Definition: ResourceLoaderContext.php:32
ResourceLoaderTestModule\$source
$source
Definition: ResourceLoaderTestCase.php:90
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
$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 $context
Definition: hooks.txt:2675
ResourceLoaderFileTestModule
Definition: ResourceLoaderTestCase.php:154
ResourceLoaderFileTestModule\getLessVars
getLessVars(ResourceLoaderContext $context)
Get module-specific LESS variables, if any.
Definition: ResourceLoaderTestCase.php:165
ResourceLoaderTestModule\getStyles
getStyles(ResourceLoaderContext $context)
Get all CSS for this module for a given skin.
Definition: ResourceLoaderTestCase.php:110
ResourceLoaderFileModuleTestModule
Definition: ResourceLoaderTestCase.php:170
ResourceLoaderTestModule\getGroup
getGroup()
Get the group this module is in.
Definition: ResourceLoaderTestCase.php:122
EmptyResourceLoader\getErrors
getErrors()
Definition: ResourceLoaderTestCase.php:184
ResourceLoaderTestCase\getSettings
static getSettings()
Definition: ResourceLoaderTestCase.php:53
$resourceLoader
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:2675
ResourceLoaderTestModule\getMessages
getMessages()
Get the messages needed for this module.
Definition: ResourceLoaderTestCase.php:114
ResourceLoaderTestModule\isRaw
isRaw()
Whether this module's JS expects to work without the client-side ResourceLoader module.
Definition: ResourceLoaderTestCase.php:138
php
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
script
script(document.cookie)%253c/script%253e</pre ></div > !! end !! test XSS is escaped(inline) !!input< source lang
ResourceLoaderFileModule
ResourceLoader module based on local JavaScript/CSS files.
Definition: ResourceLoaderFileModule.php:28
Config
Interface for configuration instances.
Definition: Config.php:28
ResourceLoaderTestModule\$dependencies
$dependencies
Definition: ResourceLoaderTestCase.php:88
ResourceLoaderTestModule\shouldEmbedModule
shouldEmbedModule(ResourceLoaderContext $context)
Check whether this module should be embeded rather than linked.
Definition: ResourceLoaderTestCase.php:145
ResourceLoaderTestModule\$isKnownEmpty
$isKnownEmpty
Definition: ResourceLoaderTestCase.php:95
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:706
MediaWikiTestCase
Definition: MediaWikiTestCase.php:16
ResourceLoaderTestModule\$shouldEmbed
$shouldEmbed
Definition: ResourceLoaderTestCase.php:98
ResourceLoaderTestModule\$type
$type
Definition: ResourceLoaderTestCase.php:96
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ResourceLoaderTestModule\isKnownEmpty
isKnownEmpty(ResourceLoaderContext $context)
Check whether this module is known to be empty.
Definition: ResourceLoaderTestCase.php:141
ResourceLoaderTestModule\$script
$script
Definition: ResourceLoaderTestCase.php:91
EmptyResourceLoader\__construct
__construct(Config $config=null, LoggerInterface $logger=null)
Definition: ResourceLoaderTestCase.php:176
ResourceLoaderTestModule\$group
$group
Definition: ResourceLoaderTestCase.php:89
ResourceLoaderTestModule\$styles
$styles
Definition: ResourceLoaderTestCase.php:92
ResourceLoaderTestModule\__construct
__construct( $options=[])
Definition: ResourceLoaderTestCase.php:100
ResourceLoaderTestModule
Definition: ResourceLoaderTestCase.php:86
$request
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:2675
ResourceLoaderTestModule\$messages
$messages
Definition: ResourceLoaderTestCase.php:87
$value
$value
Definition: styleTest.css.php:49
ResourceLoaderTestModule\getSource
getSource()
Get the source of this module.
Definition: ResourceLoaderTestCase.php:126
ResourceLoaderTestModule\$isRaw
$isRaw
Definition: ResourceLoaderTestCase.php:94
ResourceLoaderModule\validateScriptFile
validateScriptFile( $fileName, $contents)
Validate a given script file; if valid returns the original source.
Definition: ResourceLoaderModule.php:931
ResourceLoaderTestModule\getScript
getScript(ResourceLoaderContext $context)
Get all JS for this module for a given language and skin.
Definition: ResourceLoaderTestCase.php:106
ResourceLoaderTestModule\$targets
$targets
Definition: ResourceLoaderTestCase.php:97
ResourceLoaderModule
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
Definition: ResourceLoaderModule.php:35
$options
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:2036
EmptyResourceLoader
Definition: ResourceLoaderTestCase.php:173
ResourceLoaderFileTestModule\$lessVars
$lessVars
Definition: ResourceLoaderTestCase.php:155
ResourceLoaderTestModule\getSkipFunction
getSkipFunction()
Get the skip function.
Definition: ResourceLoaderTestCase.php:134
ResourceLoaderTestCase\getResourceLoaderContext
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
Definition: ResourceLoaderTestCase.php:23
as
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
MessageBlobStore
This class generates message blobs for use by ResourceLoader modules.
Definition: MessageBlobStore.php:37
class
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
MediaWikiServices
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
ResourceLoaderTestModule\getDependencies
getDependencies(ResourceLoaderContext $context=null)
Get a list of modules this module depends on.
Definition: ResourceLoaderTestCase.php:118
ResourceLoaderTestModule\getType
getType()
Get the module's load type.
Definition: ResourceLoaderTestCase.php:130
ResourceLoaderTestCase\BLANK_VERSION
const BLANK_VERSION
Definition: ResourceLoaderTestCase.php:11
ResourceLoaderFileTestModule\__construct
__construct( $options=[], $test=[])
Definition: ResourceLoaderTestCase.php:157
ResourceLoaderTestModule\$skipFunction
$skipFunction
Definition: ResourceLoaderTestCase.php:93
ResourceLoaderTestCase
Definition: ResourceLoaderTestCase.php:7