MediaWiki  1.29.1
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 
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  'lang' => 'en',
29  'dir' => 'ltr',
30  'skin' => 'vector',
31  'modules' => 'startup',
32  'only' => 'scripts',
33  ];
34  $resourceLoader = $rl ?: new ResourceLoader();
35  $request = new FauxRequest( [
36  'lang' => $options['lang'],
37  'modules' => $options['modules'],
38  'only' => $options['only'],
39  'skin' => $options['skin'],
40  'target' => 'phpunit',
41  ] );
42  $ctx = $this->getMockBuilder( 'ResourceLoaderContext' )
43  ->setConstructorArgs( [ $resourceLoader, $request ] )
44  ->setMethods( [ 'getDirection' ] )
45  ->getMock();
46  $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
47  return $ctx;
48  }
49 
50  public static function getSettings() {
51  return [
52  // For ResourceLoader::inDebugMode since it doesn't have context
53  'ResourceLoaderDebug' => true,
54 
55  // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
56  'CacheEpoch' => '20140101000000',
57 
58  // For ResourceLoader::__construct()
59  'ResourceLoaderSources' => [],
60 
61  // For wfScript()
62  'ScriptPath' => '/w',
63  'ScriptExtension' => '.php',
64  'Script' => '/w/index.php',
65  'LoadScript' => '/w/load.php',
66  ];
67  }
68 
69  protected function setUp() {
70  parent::setUp();
71 
72  ResourceLoader::clearCache();
73 
74  $globals = [];
75  foreach ( self::getSettings() as $key => $value ) {
76  $globals['wg' . $key] = $value;
77  }
78  $this->setMwGlobals( $globals );
79  }
80 }
81 
82 /* Stubs */
83 
85  protected $messages = [];
86  protected $dependencies = [];
87  protected $group = null;
88  protected $source = 'local';
89  protected $position = 'bottom';
90  protected $script = '';
91  protected $styles = '';
92  protected $skipFunction = null;
93  protected $isRaw = false;
94  protected $isKnownEmpty = false;
96  protected $targets = [ 'phpunit' ];
97 
98  public function __construct( $options = [] ) {
99  foreach ( $options as $key => $value ) {
100  $this->$key = $value;
101  }
102  }
103 
105  return $this->validateScriptFile( 'input', $this->script );
106  }
107 
109  return [ '' => $this->styles ];
110  }
111 
112  public function getMessages() {
113  return $this->messages;
114  }
115 
116  public function getDependencies( ResourceLoaderContext $context = null ) {
117  return $this->dependencies;
118  }
119 
120  public function getGroup() {
121  return $this->group;
122  }
123 
124  public function getSource() {
125  return $this->source;
126  }
127  public function getPosition() {
128  return $this->position;
129  }
130 
131  public function getType() {
132  return $this->type;
133  }
134 
135  public function getSkipFunction() {
136  return $this->skipFunction;
137  }
138 
139  public function isRaw() {
140  return $this->isRaw;
141  }
143  return $this->isKnownEmpty;
144  }
145 
146  public function enableModuleContentVersion() {
147  return true;
148  }
149 }
150 
152 }
153 
154 class EmptyResourceLoader extends ResourceLoader {
155  // TODO: This won't be needed once ResourceLoader is empty by default
156  // and default registrations are done from ServiceWiring instead.
157  public function __construct( Config $config = null, LoggerInterface $logger = null ) {
158  $this->setLogger( $logger ?: new NullLogger() );
159  $this->config = $config ?: MediaWikiServices::getInstance()->getMainConfig();
160  // Source "local" is required by StartupModule
161  $this->addSource( 'local', $this->config->get( 'LoadScript' ) );
162  $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
163  }
164 
165  public function getErrors() {
166  return $this->errors;
167  }
168 }
ResourceLoaderTestModule\enableModuleContentVersion
enableModuleContentVersion()
Whether to generate version hash based on module content.
Definition: ResourceLoaderTestCase.php:146
ResourceLoaderModule\LOAD_GENERAL
const LOAD_GENERAL
Definition: ResourceLoaderModule.php:44
ResourceLoaderTestCase\setUp
setUp()
Definition: ResourceLoaderTestCase.php:69
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:88
$context
error also a ContextSource you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2612
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
$request
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2612
ResourceLoaderTestModule\$position
$position
Definition: ResourceLoaderTestCase.php:89
ResourceLoaderTestModule\getPosition
getPosition()
From where in the page HTML should this module be loaded?
Definition: ResourceLoaderTestCase.php:127
ResourceLoaderTestModule\getStyles
getStyles(ResourceLoaderContext $context)
Get all CSS for this module for a given skin.
Definition: ResourceLoaderTestCase.php:108
ResourceLoaderFileModuleTestModule
Definition: ResourceLoaderTestCase.php:151
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\getGroup
getGroup()
Get the group this module is in.
Definition: ResourceLoaderTestCase.php:120
EmptyResourceLoader\getErrors
getErrors()
Definition: ResourceLoaderTestCase.php:165
ResourceLoaderTestCase\getSettings
static getSettings()
Definition: ResourceLoaderTestCase.php:50
ResourceLoaderTestModule\getMessages
getMessages()
Get the messages needed for this module.
Definition: ResourceLoaderTestCase.php:112
ResourceLoaderTestModule\isRaw
isRaw()
Whether this module's JS expects to work without the client-side ResourceLoader module.
Definition: ResourceLoaderTestCase.php:139
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
ResourceLoaderFileModule
ResourceLoader module based on local JavaScript/CSS files.
Definition: ResourceLoaderFileModule.php:28
ResourceLoaderTestModule\$dependencies
$dependencies
Definition: ResourceLoaderTestCase.php:86
ResourceLoaderTestModule\$isKnownEmpty
$isKnownEmpty
Definition: ResourceLoaderTestCase.php:94
script
script(document.cookie)%253c/script%253e</pre ></div > !! end !! test XSS is escaped(inline) !!input< source lang
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:658
MediaWikiTestCase
Definition: MediaWikiTestCase.php:13
ResourceLoaderTestModule\$type
$type
Definition: ResourceLoaderTestCase.php:95
ResourceLoaderTestModule\isKnownEmpty
isKnownEmpty(ResourceLoaderContext $context)
Check whether this module is known to be empty.
Definition: ResourceLoaderTestCase.php:142
ResourceLoaderTestModule\$script
$script
Definition: ResourceLoaderTestCase.php:90
EmptyResourceLoader\__construct
__construct(Config $config=null, LoggerInterface $logger=null)
Definition: ResourceLoaderTestCase.php:157
ResourceLoaderTestModule\$group
$group
Definition: ResourceLoaderTestCase.php:87
ResourceLoaderTestModule\$styles
$styles
Definition: ResourceLoaderTestCase.php:91
ResourceLoaderTestModule\__construct
__construct( $options=[])
Definition: ResourceLoaderTestCase.php:98
ResourceLoaderTestModule
Definition: ResourceLoaderTestCase.php:84
ResourceLoaderTestModule\$messages
$messages
Definition: ResourceLoaderTestCase.php:85
$value
$value
Definition: styleTest.css.php:45
ResourceLoaderTestModule\getSource
getSource()
Get the origin of this module.
Definition: ResourceLoaderTestCase.php:124
ResourceLoaderTestModule\$isRaw
$isRaw
Definition: ResourceLoaderTestCase.php:93
ResourceLoaderModule\validateScriptFile
validateScriptFile( $fileName, $contents)
Validate a given script file; if valid returns the original source.
Definition: ResourceLoaderModule.php:933
ResourceLoaderTestModule\getScript
getScript(ResourceLoaderContext $context)
Get all JS for this module for a given language and skin.
Definition: ResourceLoaderTestCase.php:104
ResourceLoaderTestModule\$targets
$targets
Definition: ResourceLoaderTestCase.php:96
$resourceLoader
error also a ContextSource 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:2612
ResourceLoaderModule
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
Definition: ResourceLoaderModule.php:34
EmptyResourceLoader
Definition: ResourceLoaderTestCase.php:154
ResourceLoaderTestModule\getSkipFunction
getSkipFunction()
Get the skip function.
Definition: ResourceLoaderTestCase.php:135
ResourceLoaderTestCase\getResourceLoaderContext
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
Definition: ResourceLoaderTestCase.php:22
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
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:116
ResourceLoaderTestModule\getType
getType()
Get the module's load type.
Definition: ResourceLoaderTestCase.php:131
$options
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1049
ResourceLoaderTestCase\BLANK_VERSION
const BLANK_VERSION
Definition: ResourceLoaderTestCase.php:11
ResourceLoaderTestModule\$skipFunction
$skipFunction
Definition: ResourceLoaderTestCase.php:92
ResourceLoaderTestCase
Definition: ResourceLoaderTestCase.php:7