SECURITY: blacklist CSS var()
[lhc/web/wiklou.git] / tests / phpunit / ResourceLoaderTestCase.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
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
13 /**
14 * @param array|string $options Language code or options array
15 * - string 'lang' Language code
16 * - string 'dir' Language direction (ltr or rtl)
17 * - string 'modules' Pipe-separated list of module names
18 * - string|null 'only' "scripts" (unwrapped script), "styles" (stylesheet), or null
19 * (mw.loader.implement).
20 * @param ResourceLoader|null $rl
21 * @return ResourceLoaderContext
22 */
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 'debug' => 'true',
30 'lang' => 'en',
31 'dir' => 'ltr',
32 'skin' => 'fallback',
33 'modules' => 'startup',
34 'only' => 'scripts',
35 'safemode' => null,
36 ];
37 $resourceLoader = $rl ?: new ResourceLoader();
38 $request = new FauxRequest( [
39 'debug' => $options['debug'],
40 'lang' => $options['lang'],
41 'modules' => $options['modules'],
42 'only' => $options['only'],
43 'safemode' => $options['safemode'],
44 'skin' => $options['skin'],
45 'target' => 'phpunit',
46 ] );
47 $ctx = $this->getMockBuilder( ResourceLoaderContext::class )
48 ->setConstructorArgs( [ $resourceLoader, $request ] )
49 ->setMethods( [ 'getDirection' ] )
50 ->getMock();
51 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
52 return $ctx;
53 }
54
55 public static function getSettings() {
56 return [
57 // For ResourceLoader::inDebugMode since it doesn't have context
58 'ResourceLoaderDebug' => true,
59
60 // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
61 'CacheEpoch' => '20140101000000',
62
63 // For wfScript()
64 'ScriptPath' => '/w',
65 'Script' => '/w/index.php',
66 'LoadScript' => '/w/load.php',
67 ];
68 }
69
70 protected function setUp() {
71 parent::setUp();
72
73 ResourceLoader::clearCache();
74
75 $globals = [];
76 foreach ( self::getSettings() as $key => $value ) {
77 $globals['wg' . $key] = $value;
78 }
79 $this->setMwGlobals( $globals );
80 }
81 }
82
83 /* Stubs */
84
85 class ResourceLoaderTestModule extends ResourceLoaderModule {
86 protected $messages = [];
87 protected $dependencies = [];
88 protected $group = null;
89 protected $source = 'local';
90 protected $script = '';
91 protected $styles = '';
92 protected $skipFunction = null;
93 protected $isRaw = false;
94 protected $isKnownEmpty = false;
95 protected $type = ResourceLoaderModule::LOAD_GENERAL;
96 protected $targets = [ 'phpunit' ];
97 protected $shouldEmbed = null;
98
99 public function __construct( $options = [] ) {
100 foreach ( $options as $key => $value ) {
101 $this->$key = $value;
102 }
103 }
104
105 public function getScript( ResourceLoaderContext $context ) {
106 return $this->validateScriptFile( 'input', $this->script );
107 }
108
109 public function getStyles( ResourceLoaderContext $context ) {
110 return [ '' => $this->styles ];
111 }
112
113 public function getMessages() {
114 return $this->messages;
115 }
116
117 public function getDependencies( ResourceLoaderContext $context = null ) {
118 return $this->dependencies;
119 }
120
121 public function getGroup() {
122 return $this->group;
123 }
124
125 public function getSource() {
126 return $this->source;
127 }
128
129 public function getType() {
130 return $this->type;
131 }
132
133 public function getSkipFunction() {
134 return $this->skipFunction;
135 }
136
137 public function isRaw() {
138 return $this->isRaw;
139 }
140
141 public function isKnownEmpty( ResourceLoaderContext $context ) {
142 return $this->isKnownEmpty;
143 }
144
145 public function shouldEmbedModule( ResourceLoaderContext $context ) {
146 return $this->shouldEmbed ?? parent::shouldEmbedModule( $context );
147 }
148
149 public function enableModuleContentVersion() {
150 return true;
151 }
152 }
153
154 class ResourceLoaderFileTestModule extends ResourceLoaderFileModule {
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
165 public function getLessVars( ResourceLoaderContext $context ) {
166 return $this->lessVars;
167 }
168 }
169
170 class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
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 }